UserProNetMediaZaliczka.php 13 KB

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