UserProNetMediaZaliczka.php 15 KB

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