UserProNetMediaZaliczka.php 16 KB

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