UserProNetMediaZaliczka.php 16 KB

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