UserProNetMediaZaliczka.php 16 KB

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