UserProNetMediaZaliczka.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. Lib::loadClass('Typespecial');
  4. Lib::loadClass('TypespecialVariable');
  5. Lib::loadClass('Request');
  6. Lib::loadClass('Response');
  7. Lib::loadClass('ProcesHelper');
  8. Lib::loadClass('Route_UrlAction');
  9. Lib::loadClass('UI');
  10. Lib::loadClass('Core_AclHelper');
  11. // - [ ] zaliczkaListaView:
  12. // - [ ] btn "Dodaj Zaliczkę" $acl->addItem($item); // @require only 'worker' => [ 'id', 'login', 'name' ]
  13. // - [ ] lista wszystkich zaliczek danego usera + btn "load more ..."
  14. // - [x] Rozlicz / Podgląd - tylko najnowsza Zaliczka
  15. // - [ ] Rozlicz / Podgląd - edytowć może tylko właściciel / grupa / user connected by REF ?
  16. class Route_UrlAction_UserProNetMediaZaliczka extends RouteBase {// TODO: UrlActionBase @see Route_UrlAction
  17. public function handleAuth() {
  18. if (!User::logged()) {
  19. User::authByRequest();
  20. }
  21. }
  22. public function defaultAction() {
  23. UI::gora();
  24. UI::emptyTag('link', ['href'=>'static/zaliczka/main.css', 'rel'=>'stylesheet']);
  25. if (1 != V::get('_print', '', $_GET)) UI::menu();
  26. try {
  27. $data = array();
  28. $data['idUser'] = V::get('idUser', User::getID(), $_REQUEST, 'int');
  29. $data['idZaliczka'] = V::get('idZaliczka', 0, $_REQUEST, 'int');// TODO: idZaliczka is always MAX(z.ID);
  30. if (!$data['idZaliczka']) {
  31. $this->zaliczkaListaView($data);
  32. } else {
  33. UI::startContainer();
  34. UI::tag('a', [
  35. 'href' => "index.php?_route=UrlAction_UserProNetMediaZaliczka&idUser={$data['idUser']}",
  36. 'class' => "btn btn-link"
  37. ], "<i class=\"glyphicon glyphicon-arrow-left\"></i> wróć");
  38. UI::endContainer();
  39. $this->zaliczkaFormView($data);
  40. }
  41. } catch (Exception $e) {
  42. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  43. }
  44. if (1 != V::get('_print', '', $_GET)) UI::dol();
  45. }
  46. public function syncStataAjaxAction() {
  47. Response::sendTryCatchJson(array($this, 'syncStataAjaxReponseCallback'), $args = 'JSON_FROM_REQUEST_BODY');// try to read json from request body
  48. }
  49. public function syncStataAjaxReponseCallback($args) {
  50. if (empty($args) || !array_key_exists('updates', $args)) throw new HttpException("Error Parsing Request - missing args", 400);
  51. $args['idUser'] = V::get('idUser', 0, $_REQUEST, 'int');// (int)$args['idUser'];
  52. if ($args['idUser'] <= 0) throw new HttpException("Error Parsing Request - missing idUser", 400);
  53. $args['idZaliczka'] = V::get('idZaliczka', 0, $_REQUEST, 'int');// (int)$args['idZaliczka'];
  54. if ($args['idZaliczka'] <= 0) throw new HttpException("Error Parsing Request - missing idZaliczka", 400);
  55. $acl = ACL::getAclByNamespace("default_db/ZALICZKA/Zaliczka");
  56. $schema = $acl->getSimpleSchemaTree();
  57. if (!empty($args['updates'])) {
  58. throw new Exception("TODO: updates! F." . __FUNCTION__ . " L." . __LINE__);
  59. }
  60. $zaliczkaData = $acl->getItem($args['idZaliczka'], [
  61. 'cols' => [
  62. '@instance',
  63. 'id',
  64. 'created',
  65. 'kwota',
  66. 'nierozliczona_kwota',
  67. 'pozycja',
  68. 'worker',
  69. ]
  70. ]);// $this->fetchDataBySchema($schema, $args['idUser']);
  71. return [
  72. 'type' => 'success',
  73. 'nierozliczonaKwota' => V::get('nierozliczona_kwota', 0, $zaliczkaData),
  74. 'schema' => [
  75. 'zaliczka' => $schema,
  76. ],
  77. 'zaliczka' => $zaliczkaData,
  78. ];
  79. }
  80. public function fetchDataAjaxAction() {
  81. Response::sendTryCatchJson(array($this, 'fetchDataAjaxReponseCallback'), $args = 'JSON_FROM_REQUEST_BODY');// try to read json from request body
  82. }
  83. public function fetchDataAjaxReponseCallback($args) {
  84. if (empty($args) || empty($args['schema']['@namespace']) || empty($args['schema']['@namespace'])) throw new HttpException("Error Parsing Request - missing @namespace", 400);
  85. $nsUri = $args['schema']['@namespace'];
  86. if ('default_db/IN7_DZIENNIK_KORESP/ZaliczkaKoresp' == $nsUri) {
  87. return [
  88. 'options' => [
  89. [ 'id' => "65432", 'title' => "testowa koresp 1", 'kategoriaKosztu' => "KOSZT1" ],
  90. [ 'id' => "10008", 'title' => "testowa koresp 8", 'kategoriaKosztu' => "KOSZT8" ],
  91. [ 'id' => "10009", 'title' => "testowa koresp 9", 'kategoriaKosztu' => "KOSZT9" ],
  92. ]
  93. ];
  94. }
  95. if ('default_db/IN7_MK_BAZA_DYSTRYBUCJI/ZaliczkaProjekt' == $nsUri) {
  96. return [
  97. 'options' => [
  98. [ 'id' => 100, 'nrBudowy' => 'Budowa 100/2016' ],
  99. [ 'id' => 101, 'nrBudowy' => 'Budowa 101/2016' ],
  100. [ 'id' => 102, 'nrBudowy' => 'Budowa 102/2016' ],
  101. ]
  102. ];
  103. }
  104. return null;// msg?
  105. }
  106. public function fetchDataBySchema($schema, $idUser, $primaryKey = null) {// if $primaryKey is null then search for last row
  107. return [// TODO: fetch data from DB
  108. 'id' => 123,
  109. 'created' => '2016-11-22', // data wydania zaliczki - data utworzenia rekordu
  110. 'worker' => [
  111. 'id' => 666,
  112. 'name' => 'Kowalski', // 'TODO' => get name from admin_users - imię i nazwisko pracownika pobierającego zaliczkę
  113. 'login' => 'kowalski'
  114. ],
  115. 'kwota' => 555.55, // kwota wypłaconej zaliczki
  116. 'nierozliczona_kwota' => 123.56,
  117. 'pozycja' => [
  118. [ 'id' => 1,
  119. 'kwota' => 100.22,
  120. 'korespondencja' => [ 'id' => 65432, 'title' => 'testowa koresp 1', 'kategoriaKosztu' => 'KOSZT1' ],
  121. 'projekt' => [ 'nrBudowy' => 'Budowa/1' ]
  122. ],
  123. [ 'id' => 2,
  124. 'kwota' => 200.55,
  125. 'korespondencja' => [ 'id' => 76543, 'title' => 'testowa koresp 2', 'kategoriaKosztu' => 'KOSZT2' ],
  126. 'projekt' => [ 'id' => 123, 'nrBudowy' => 'Budowa/2' ]
  127. ]
  128. ]
  129. ];
  130. }
  131. public function zaliczkaListaView($data) {
  132. $idUser = V::get('idUser', User::getID(), $data);
  133. $ownerAcl = Core_AclHelper::getAclByNamespace("default_objects/AccessOwner");
  134. $user = $ownerAcl->getItem($idUser);// [id, login, name]
  135. if (!$user) throw new Exception("Nie znaleziono pracownika o nr '{$idUser}'");
  136. $acl = Core_AclHelper::getAclByNamespace("default_db/ZALICZKA/Zaliczka");
  137. $schema = $acl->getSimpleSchemaTree();
  138. $zaliczkaData = $this->fetchDataBySchema($schema, $idUser);
  139. UI::startContainer(['style' => "padding-top:20px"]);
  140. UI::setTitle("Zaliczka");
  141. UI::table([
  142. 'caption' => "Zaliczki pracownika {$user['name']} " . UI::hButtonPost("Dodaj nową zaliczkę", [
  143. 'data' => [
  144. '_route' => "UrlAction_UserProNetMediaZaliczka",
  145. '_task' => "add",
  146. 'idUser' => $idUser,
  147. ],
  148. 'class' => 'btn-xs btn-primary'
  149. ]),
  150. 'rows' => array_map(
  151. function ($row) use ($idUser) {
  152. $instance = DB::getPDO()->fetchValue("select INSTANCE_NAME from `ZALICZKA__#INSTANCE` where PRIMARY_KEY = {$row['ID']}");
  153. $editLink = Request::getPathUri() . "index.php?_route=UrlAction_UserProNetMediaZaliczka&idUser={$idUser}&idZaliczka={$row['ID']}";
  154. $editLabel = ('ZaliczkaAktywna' == $instance) ? "Rozlicz / Podgląd" : "Podgląd";
  155. $histLink = Request::getPathUri() . "index.php?_route=UrlAction_UserProNetMediaZaliczka&_task=hist&idUser={$idUser}&idZaliczka={$row['ID']}";
  156. return [
  157. 'Nr' => $row['ID'],
  158. 'Status' => $row['A_STATUS'],
  159. 'INSTANCE' => $instance,
  160. 'L_APPOITMENT_USER' => $row['L_APPOITMENT_USER'],
  161. 'KWOTA' => $row['KWOTA'],
  162. 'NIEROZLICZONA_KWOTA' => $row['NIEROZLICZONA_KWOTA'],
  163. '#' => '<a href="' . $editLink . '">' . $editLabel . '</a>',
  164. 'DBG' => '<a href="' . $histLink . '">' . "DBG HIST" . '</a>'
  165. ];
  166. },
  167. $acl->getItems([
  168. '#refFrom' => [
  169. 'namespace' => 'default_db__x3A__ADMIN_USERS/Worker',
  170. 'primaryKey' => $idUser
  171. ]
  172. ])
  173. ),
  174. 'empty_msg' => "Brak",
  175. 'disable_lp' => true,
  176. ]);
  177. UI::endContainer();
  178. }
  179. public function addAction() {
  180. $idUser = V::get('idUser', User::getID(), $_REQUEST);
  181. try {
  182. $acl = Core_AclHelper::getAclByNamespace("default_db/ZALICZKA/Zaliczka");
  183. $id = $acl->addItem([
  184. 'worker' => [
  185. 'id' => $idUser
  186. ]
  187. ]);
  188. if (!$id) throw new Exception("Nie udało się utworzyć zaliczki");
  189. } catch (Exception $e) {
  190. UI::gora();
  191. // UI::menu();// TODO: show menu
  192. UI::startContainer();
  193. UI::alert('danger', $e->getMessage());
  194. UI::endContainer();
  195. UI::dol();
  196. exit;
  197. }
  198. $redirectUrl = Request::getPathUri() . "index.php?_route=UrlAction_UserProNetMediaZaliczka&idUser={$idUser}&idZaliczka={$id}";
  199. if (!headers_sent()) {
  200. header("Location: {$redirectUrl}");
  201. } else {
  202. echo'<script type="text/javascript">'."
  203. window.location.href='{$redirectUrl}';
  204. ".'</script>';
  205. echo "\n".'<noscript>';
  206. echo "\n".'<meta http-equiv="refresh" content="0;url='.$redirectUrl.'" />';
  207. echo "\n".'</noscript>';
  208. echo'<p>'.'<a href="'.$redirectUrl.'">'."dalej".'</a>'.'</p>';
  209. }
  210. }
  211. public function reinstallAction() {
  212. UI::gora();
  213. try {
  214. Core_AclHelper::getAclByNamespace("default_db/ZALICZKA/Zaliczka")->reinstall();
  215. UI::startContainer();
  216. UI::alert('success', "Structure for 'default_db/ZALICZKA/Zaliczka' created in a database");
  217. $backUrl = Request::getPathUri() . "index.php?_route=UrlAction_UserProNetMediaZaliczka";
  218. UI::tag('a', ['href'=>$backUrl, 'class'=>"btn btn-primary"], "wróć");
  219. UI::table([
  220. 'caption' => "Zaliczka",
  221. 'rows' => DB::getPDO()->fetchAll("
  222. select t.TABLE_NAME, t.COLUMN_NAME, t.DATA_TYPE, t.COLUMN_TYPE
  223. from `information_schema`.`COLUMNS` t
  224. where t.TABLE_SCHEMA = 'SES_USERS2'
  225. and (t.TABLE_NAME like 'ZALICZKA'
  226. or t.TABLE_NAME like 'ZALICZKA\_\_%'
  227. or t.TABLE_NAME like 'ZALICZKA_HIST'
  228. or t.TABLE_NAME like 'Zaliczka\_\_%'
  229. )
  230. order by t.TABLE_NAME asc, t.COLUMN_NAME asc
  231. ")
  232. ]);
  233. UI::table([
  234. 'caption' => "Zaliczka Pozycja",
  235. 'rows' => DB::getPDO()->fetchAll("
  236. select t.TABLE_NAME, t.COLUMN_NAME, t.DATA_TYPE, t.COLUMN_TYPE
  237. from `information_schema`.`COLUMNS` t
  238. where t.TABLE_SCHEMA = 'SES_USERS2'
  239. and (t.TABLE_NAME like 'ZALICZKA_POZYCJA'
  240. or t.TABLE_NAME like 'ZALICZKA_POZYCJA\_\_%'
  241. or t.TABLE_NAME like 'ZALICZKA_POZYCJA_HIST'
  242. or t.TABLE_NAME like 'ZaliczkaPozycja\_\_%'
  243. )
  244. order by t.TABLE_NAME asc, t.COLUMN_NAME asc
  245. ")
  246. ]);
  247. $tableList = array_map(
  248. function($item) {
  249. return $item['TABLE_NAME'];
  250. }
  251. , DB::getPDO()->fetchAll("
  252. select t.TABLE_NAME
  253. from `information_schema`.`COLUMNS` t
  254. where t.TABLE_SCHEMA = 'SES_USERS2'
  255. and (t.TABLE_NAME like 'ZALICZKA'
  256. or t.TABLE_NAME like 'ZALICZKA\_\_%'
  257. or t.TABLE_NAME like 'ZALICZKA_HIST'
  258. or t.TABLE_NAME like 'Zaliczka\_\_%'
  259. or t.TABLE_NAME like 'ZALICZKA_POZYCJA'
  260. or t.TABLE_NAME like 'ZALICZKA_POZYCJA\_\_%'
  261. or t.TABLE_NAME like 'ZALICZKA_POZYCJA_HIST'
  262. or t.TABLE_NAME like 'ZaliczkaPozycja\_\_%'
  263. )
  264. group by t.TABLE_NAME
  265. ")
  266. );
  267. foreach ($tableList as $tableName) {
  268. UI::startTag('div', ['style'=>"margin:10px 0; max-width:100%; overflow:scroll; border:1px solid #ccc"]);
  269. UI::table([
  270. 'caption' => "Tabela '{$tableName}'",
  271. 'rows' => DB::getPDO()->fetchAll("
  272. select t.*
  273. from `{$tableName}` t
  274. ")
  275. ]);
  276. UI::endTag('div');
  277. }
  278. if ('1' == V::get('_clear', '', $_GET)) {
  279. foreach ($tableList as $tableName) {
  280. DB::getPDO()->exec("TRUNCATE `{$tableName}`");
  281. }
  282. UI::tag('a', ['class'=>"btn btn-primary", 'href'=>"index.php?_route=UrlAction_UserProNetMediaZaliczka&_task=reinstall"], "Dane usunięte - odśwież stronę");
  283. } else {
  284. UI::tag('a', ['class'=>"btn btn-danger", 'href'=>"index.php?_route=UrlAction_UserProNetMediaZaliczka&_task=reinstall&_clear=1"], "Wyczyść tabele");
  285. }
  286. UI::startTag('pre');
  287. foreach ($tableList as $tableName) {
  288. echo "DROP TABLE `{$tableName}`;\n";
  289. }
  290. UI::endTag('pre');
  291. ACL::getRefTable('default_db/ZALICZKA/Zaliczka', 'worker');
  292. ACL::getRefTable('default_db/ZALICZKA/Zaliczka', 'pozycja');
  293. ACL::getRefTable('default_db/ZALICZKA_POZYCJA/ZaliczkaPozycja', 'projekt');
  294. ACL::getRefTable('default_db/ZALICZKA_POZYCJA/ZaliczkaPozycja', 'korespondencja');
  295. $refZaliczkaTables = DB::getPDO()->fetchAll("
  296. select t.*
  297. from `CRM_REF_CONFIG` t
  298. where t.ROOT_OBJECT_NS like 'default_db/ZALICZKA%'
  299. ");
  300. UI::startTag('div', ['style'=>"margin:10px 0; max-width:100%; overflow:scroll; border:1px solid #ccc"]);
  301. UI::table([
  302. 'caption' => "Tabela 'CRM_REF_CONFIG'",
  303. 'rows' => $refZaliczkaTables
  304. ]);
  305. UI::endTag('div');
  306. foreach ($refZaliczkaTables as $refInfo) {
  307. $tableName = "CRM__#REF_TABLE__" . $refInfo['ID'];
  308. UI::startTag('div', ['style'=>"margin:10px 0; max-width:100%; overflow:scroll; border:1px solid #ccc"]);
  309. UI::table([
  310. 'caption' => "Tabela REF '{$tableName}' - <small><i>{$refInfo['ROOT_OBJECT_NS']} REF {$refInfo['CHILD_NAME']} ({$refInfo['CHILD_NS']})</i></small>",
  311. 'rows' => DB::getPDO()->fetchAll("
  312. select t.*
  313. from `{$tableName}` t
  314. ")
  315. ]);
  316. UI::endTag('div');
  317. }
  318. if ('1' == V::get('_clear', '', $_GET)) {
  319. foreach ($refZaliczkaTables as $refInfo) {
  320. $tableName = "CRM__#REF_TABLE__" . $refInfo['ID'];
  321. DB::getPDO()->exec("TRUNCATE `{$tableName}`");
  322. }
  323. UI::tag('a', ['class'=>"btn btn-primary", 'href'=>"index.php?_route=UrlAction_UserProNetMediaZaliczka&_task=reinstall"], "Dane usunięte - odśwież stronę");
  324. }
  325. UI::endContainer();
  326. } catch (Exception $e) {
  327. UI::alert('danger', $e->getMessage());
  328. }
  329. UI::dol();
  330. }
  331. public function zaliczkaFormView($data) {
  332. $idUser = V::get('idUser', User::getID(), $data);
  333. $idZaliczka = V::get('idZaliczka', 0, $data);
  334. UI::tag('div', ['id'=>"zaliczka-app", 'data-dbg'=>V::get('DBG', '', $_GET), 'data-sync-js-function'=>"syncZaliczkaState", 'data-fetch-data-js-function'=>"zaliczkaFetchData"]);
  335. UI::emptyTag('br');
  336. $acl = Core_AclHelper::getAclByNamespace("default_db/ZALICZKA/Zaliczka");
  337. $schema = $acl->getSimpleSchemaTree();
  338. // DBG::nicePrint($schema, '$schema');
  339. UI::inlineJS(__FILE__ . '.fetchData.js', [
  340. 'SYNC_URL' => Request::getPathUri() . "index.php?_route=UrlAction_UserProNetMediaZaliczka&_task=fetchDataAjax&idUser={$idUser}&idZaliczka={$idZaliczka}",
  341. 'DBG' => (DBG::isActive())
  342. ]);
  343. UI::inlineJS(__FILE__ . '.syncState.js', [
  344. 'SYNC_URL' => Request::getPathUri() . "index.php?_route=UrlAction_UserProNetMediaZaliczka&_task=syncStataAjax&idUser={$idUser}&idZaliczka={$idZaliczka}",
  345. 'DBG' => (DBG::isActive())
  346. ]);
  347. UI::tag('script', ['src'=>'static/vendor.js' . (V::get('DBG', '', $_GET) ? '?_ts=' . time() : ''), 'type'=>'text/javascript']);
  348. UI::tag('script', ['src'=>'static/zaliczka/main.js' . (V::get('DBG', '', $_GET) ? '?_ts=' . time() : ''), 'type'=>'text/javascript']);
  349. }
  350. public function histAction() {
  351. UI::gora();
  352. // UI::menu();
  353. try {
  354. $idZaliczka = V::get('idZaliczka', 0, $_GET, 'int');
  355. if ($idZaliczka <= 0) throw new Exception("Missing idZaliczka");
  356. $acl = Core_AclHelper::getAclByNamespace("default_db/ZALICZKA/Zaliczka");
  357. $zaliczka = $acl->getItem($idZaliczka);
  358. if (!$zaliczka) throw new Exception("Zaliczka not found!");
  359. DBG::nicePrint($zaliczka, '$zaliczka');
  360. } catch (Exception $e) {
  361. UI::alert('danger', $e->getMessage());
  362. }
  363. UI::dol();
  364. }
  365. }
  366. /* Akcje
  367. # dodanie pierwszej zaliczki - 500 zł
  368. $data = [ worker => [ login => 'Kowalski' ], kwota => 500.00 ]
  369. insert into `ZALICZKA` (`L_APPOITMENT_USER`, `KWOTA`) values('{$data['worker']['login']}', '{$data['kwota']}');
  370. -- return id = 123
  371. # rozliczenie częściowe pierwszej zaliczki np. 500 zł
  372. $data = [ id => 123, pozycja => [ [ kwota => 200.00, korespondencja => [ ... ], projekt => [ ... ] ] ] ]
  373. update `ZALICZKA__#REF__POZYCJA` set `A_STATUS` = 'DELETED' where `PRIMARY_KEY` = 123;
  374. */