UserProNetMediaZaliczka.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. $this->zaliczkaFormView($data);
  35. }
  36. } catch (Exception $e) {
  37. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  38. }
  39. if (1 != V::get('_print', '', $_GET)) UI::dol();
  40. }
  41. public function syncStataAjaxAction() {
  42. Response::sendTryCatchJson(array($this, 'syncStataAjaxReponseCallback'), $args = 'JSON_FROM_REQUEST_BODY');// try to read json from request body
  43. }
  44. public function syncStataAjaxReponseCallback($args) {
  45. if (empty($args) || !array_key_exists('updates', $args)) throw new HttpException("Error Parsing Request - missing args", 400);
  46. $args['idUser'] = V::get('idUser', 0, $_REQUEST, 'int');// (int)$args['idUser'];
  47. if ($args['idUser'] <= 0) throw new HttpException("Error Parsing Request - missing idUser", 400);
  48. $args['idZaliczka'] = V::get('idZaliczka', 0, $_REQUEST, 'int');// (int)$args['idZaliczka'];
  49. if ($args['idZaliczka'] <= 0) throw new HttpException("Error Parsing Request - missing idZaliczka", 400);
  50. $acl = Core_AclHelper::getAclByNamespace("default_db/ZALICZKA/Zaliczka");
  51. $schema = $acl->getSimpleSchemaTree();
  52. if (!empty($args['updates'])) {
  53. throw new Exception("TODO: F." . __FUNCTION__ . " L." . __LINE__);
  54. }
  55. $zaliczkaData = $this->fetchDataBySchema($schema, $args['idUser']);
  56. return [
  57. 'nierozliczonaKwota' => V::get('nierozliczona_kwota', 0, $zaliczkaData),
  58. 'schema' => [
  59. 'zaliczka' => $schema,
  60. ],
  61. 'zaliczka' => $zaliczkaData,
  62. ];
  63. }
  64. public function fetchDataAjaxAction() {
  65. Response::sendTryCatchJson(array($this, 'fetchDataAjaxReponseCallback'), $args = 'JSON_FROM_REQUEST_BODY');// try to read json from request body
  66. }
  67. public function fetchDataAjaxReponseCallback($args) {
  68. if (empty($args) || empty($args['schema']['@namespace']) || empty($args['schema']['@namespace'])) throw new HttpException("Error Parsing Request - missing @namespace", 400);
  69. $nsUri = $args['schema']['@namespace'];
  70. if ('default_db/IN7_DZIENNIK_KORESP' == $nsUri) {
  71. return [
  72. 'options' => [
  73. [ 'id' => "65432", 'title' => "testowa koresp 1", 'kategoriaKosztu' => "KOSZT1" ],
  74. [ 'id' => "10008", 'title' => "testowa koresp 8", 'kategoriaKosztu' => "KOSZT8" ],
  75. [ 'id' => "10009", 'title' => "testowa koresp 9", 'kategoriaKosztu' => "KOSZT9" ],
  76. ]
  77. ];
  78. }
  79. if ('default_db/IN7_MK_BAZA_DYSTRYBUCJI' == $nsUri) {
  80. return [
  81. 'options' => [
  82. [ 'id' => 100, 'nrBudowy' => 'Budowa 100/2016' ],
  83. [ 'id' => 101, 'nrBudowy' => 'Budowa 101/2016' ],
  84. [ 'id' => 102, 'nrBudowy' => 'Budowa 102/2016' ],
  85. ]
  86. ];
  87. }
  88. return null;// msg?
  89. }
  90. public function fetchDataBySchema($schema, $idUser, $primaryKey = null) {// if $primaryKey is null then search for last row
  91. return [// TODO: fetch data from DB
  92. 'id' => 123,
  93. 'created' => '2016-11-22', // data wydania zaliczki - data utworzenia rekordu
  94. 'worker' => [
  95. 'id' => 666,
  96. 'name' => 'Kowalski', // 'TODO' => get name from admin_users - imię i nazwisko pracownika pobierającego zaliczkę
  97. 'login' => 'kowalski'
  98. ],
  99. 'kwota' => 555.55, // kwota wypłaconej zaliczki
  100. 'nierozliczona_kwota' => 123.56,
  101. 'pozycja' => [
  102. [ 'id' => 1,
  103. 'kwota' => 100.22,
  104. 'korespondencja' => [ 'id' => 65432, 'title' => 'testowa koresp 1', 'kategoriaKosztu' => 'KOSZT1' ],
  105. 'projekt' => [ 'nrBudowy' => 'Budowa/1' ]
  106. ],
  107. [ 'id' => 2,
  108. 'kwota' => 200.55,
  109. 'korespondencja' => [ 'id' => 76543, 'title' => 'testowa koresp 2', 'kategoriaKosztu' => 'KOSZT2' ],
  110. 'projekt' => [ 'id' => 123, 'nrBudowy' => 'Budowa/2' ]
  111. ]
  112. ]
  113. ];
  114. }
  115. public function zaliczkaListaView($data) {
  116. $idUser = V::get('idUser', User::getID(), $data);
  117. $acl = Core_AclHelper::getAclByNamespace("default_db/ZALICZKA/Zaliczka");
  118. $schema = $acl->getSimpleSchemaTree();
  119. $zaliczkaData = $this->fetchDataBySchema($schema, $idUser);
  120. UI::startContainer();
  121. UI::setTitle("Zaliczka");
  122. UI::startTag('form', ['action'=>'', 'method'=>'POST']);
  123. UI::emptyTag('input', ['type'=>"hidden", 'name'=>'_route', 'value'=>"UrlAction_UserProNetMediaZaliczka"]);
  124. UI::emptyTag('input', ['type'=>"hidden", 'name'=>'_task', 'value'=>"add"]);
  125. UI::emptyTag('input', ['type'=>"hidden", 'name'=>'idUser', 'value'=>$idUser]);
  126. UI::emptyTag('input', ['type'=>"submit", 'value'=>"Dodaj nową zaliczkę", 'class'=>'btn btn-primary']);
  127. UI::endTag('form');
  128. UI::alert('danger', "TODO: lista zaliczek");
  129. UI::alert('danger', "TODO: pierwsza na liście - aktualna - btn(Rozlicz)");// -> Edycja zaliczki
  130. UI::endContainer();
  131. }
  132. public function addAction() {
  133. $idUser = V::get('idUser', User::getID(), $_REQUEST);
  134. try {
  135. $acl = Core_AclHelper::getAclByNamespace("default_db/ZALICZKA/Zaliczka");
  136. $id = $acl->addItem([
  137. 'worker' => [
  138. 'id' => $idUser
  139. ]
  140. ]);
  141. if (!$id) throw new Exception("Nie udało się utworzyć zaliczki");
  142. } catch (Exception $e) {
  143. UI::gora();
  144. // UI::menu();// TODO: show menu
  145. UI::startContainer();
  146. UI::alert('danger', $e->getMessage());
  147. UI::endContainer();
  148. UI::dol();
  149. exit;
  150. }
  151. $redirectUrl = Request::getPathUri() . "index.php?_route=UrlAction_UserProNetMediaZaliczka&idUser={$idUser}&idZaliczka={$id}";
  152. if (!headers_sent()) {
  153. header("Location: {$redirectUrl}");
  154. } else {
  155. echo'<script type="text/javascript">'."
  156. window.location.href='{$redirectUrl}';
  157. ".'</script>';
  158. echo "\n".'<noscript>';
  159. echo "\n".'<meta http-equiv="refresh" content="0;url='.$redirectUrl.'" />';
  160. echo "\n".'</noscript>';
  161. echo'<p>'.'<a href="'.$redirectUrl.'">'."dalej".'</a>'.'</p>';
  162. }
  163. }
  164. public function reinstallAction() {
  165. UI::gora();
  166. try {
  167. Core_AclHelper::getAclByNamespace("default_db/ZALICZKA/Zaliczka")->reinstall();
  168. UI::startContainer();
  169. UI::alert('success', "Structure for 'default_db/ZALICZKA/Zaliczka' created in a database");
  170. $backUrl = Request::getPathUri() . "index.php?_route=UrlAction_UserProNetMediaZaliczka";
  171. UI::tag('a', ['href'=>$backUrl, 'class'=>"btn btn-primary"], "wróć");
  172. UI::table([
  173. 'caption' => "Zaliczka",
  174. 'rows' => DB::getPDO()->fetchAll("
  175. select t.TABLE_NAME, t.COLUMN_NAME, t.DATA_TYPE, t.COLUMN_TYPE
  176. from `information_schema`.`COLUMNS` t
  177. where t.TABLE_SCHEMA = 'SES_USERS2'
  178. and (t.TABLE_NAME like 'ZALICZKA'
  179. or t.TABLE_NAME like 'ZALICZKA\_\_%'
  180. or t.TABLE_NAME like 'ZALICZKA_HIST'
  181. or t.TABLE_NAME like 'Zaliczka\_\_%'
  182. )
  183. order by t.TABLE_NAME asc, t.COLUMN_NAME asc
  184. ")
  185. ]);
  186. UI::table([
  187. 'caption' => "Zaliczka Pozycja",
  188. 'rows' => DB::getPDO()->fetchAll("
  189. select t.TABLE_NAME, t.COLUMN_NAME, t.DATA_TYPE, t.COLUMN_TYPE
  190. from `information_schema`.`COLUMNS` t
  191. where t.TABLE_SCHEMA = 'SES_USERS2'
  192. and (t.TABLE_NAME like 'ZALICZKA_POZYCJA'
  193. or t.TABLE_NAME like 'ZALICZKA_POZYCJA\_\_%'
  194. or t.TABLE_NAME like 'ZALICZKA_POZYCJA_HIST'
  195. or t.TABLE_NAME like 'ZaliczkaPozycja\_\_%'
  196. )
  197. order by t.TABLE_NAME asc, t.COLUMN_NAME asc
  198. ")
  199. ]);
  200. $tableList = DB::getPDO()->fetchAll("
  201. select t.TABLE_NAME
  202. from `information_schema`.`COLUMNS` t
  203. where t.TABLE_SCHEMA = 'SES_USERS2'
  204. and (t.TABLE_NAME like 'ZALICZKA'
  205. or t.TABLE_NAME like 'ZALICZKA\_\_%'
  206. or t.TABLE_NAME like 'ZALICZKA_HIST'
  207. or t.TABLE_NAME like 'Zaliczka\_\_%'
  208. or t.TABLE_NAME like 'ZALICZKA_POZYCJA'
  209. or t.TABLE_NAME like 'ZALICZKA_POZYCJA\_\_%'
  210. or t.TABLE_NAME like 'ZALICZKA_POZYCJA_HIST'
  211. or t.TABLE_NAME like 'ZaliczkaPozycja\_\_%'
  212. )
  213. group by t.TABLE_NAME
  214. ");
  215. foreach ($tableList as $table) {
  216. $tableName = $table['TABLE_NAME'];
  217. UI::startTag('div', ['style'=>"margin:10px 0; max-width:100%; overflow:scroll; border:1px solid #ccc"]);
  218. UI::table([
  219. 'caption' => "Tabela '{$tableName}'",
  220. 'rows' => DB::getPDO()->fetchAll("
  221. select t.*
  222. from `{$tableName}` t
  223. ")
  224. ]);
  225. UI::endTag('div');
  226. }
  227. if ('1' == V::get('_clear', '', $_GET)) {
  228. foreach ($tableList as $table) {
  229. $tableName = $table['TABLE_NAME'];
  230. DB::getPDO()->exec("TRUNCATE `{$tableName}`");
  231. }
  232. UI::tag('a', ['class'=>"btn btn-primary", 'href'=>"index.php?_route=UrlAction_UserProNetMediaZaliczka&_task=reinstall"], "Dane usunięte - odśwież stronę");
  233. } else {
  234. UI::tag('a', ['class'=>"btn btn-danger", 'href'=>"index.php?_route=UrlAction_UserProNetMediaZaliczka&_task=reinstall&_clear=1"], "Wyczyść tabele");
  235. }
  236. UI::endContainer();
  237. } catch (Exception $e) {
  238. UI::alert('danger', $e->getMessage());
  239. }
  240. UI::dol();
  241. }
  242. public function zaliczkaFormView($data) {
  243. $idUser = V::get('idUser', User::getID(), $data);
  244. $idZaliczka = V::get('idZaliczka', 0, $data);
  245. UI::tag('div', ['id'=>"zaliczka-app", 'data-dbg'=>V::get('DBG', '', $_GET), 'data-sync-js-function'=>"syncZaliczkaState", 'data-fetch-data-js-function'=>"zaliczkaFetchData"]);
  246. UI::emptyTag('br');
  247. $acl = Core_AclHelper::getAclByNamespace("default_db/ZALICZKA/Zaliczka");
  248. $schema = $acl->getSimpleSchemaTree();
  249. $zaliczkaData = $this->fetchDataBySchema($schema, $idUser);
  250. DBG::nicePrint($zaliczkaData, '$zaliczkaData');// TODO: DBG
  251. ?>
  252. <script>
  253. function zaliczkaFetchData(query, fieldType, resultCallback) {
  254. console.warn('TODO: zaliczkaFetchData(query, fieldType, resultCallback) query('+query+') fieldType:', fieldType);
  255. // fieldType: Object {"@baseTypeName": "default_db:IN7_DZIENNIK_KORESP", id: "xsd:integer", title: "xsd:string", kategoriaKosztu: "xsd:string"}
  256. if ('object' === typeof fieldType) {
  257. if (!fieldType['@namespace']) {
  258. // TODO: log error
  259. console.error("BUG: Missing typeName in fieldType");
  260. return;
  261. }
  262. switch (fieldType['@namespace']) {
  263. case 'default_db/IN7_MK_BAZA_DYSTRYBUCJI':
  264. case 'default_db/IN7_DZIENNIK_KORESP':
  265. {
  266. superagent
  267. .post('<?= Request::getPathUri(); ?>/index.php?_route=UrlAction_UserProNetMediaZaliczka&_task=fetchDataAjax&idUser=<?= $idUser; ?>&idZaliczka=<?= $idZaliczka; ?>')
  268. .type('json') // header ĺapplication/x-www-form-urlencoded' requires type('form');
  269. .send({
  270. schema: fieldType,
  271. query: query
  272. })
  273. .set('Accept', 'application/json')
  274. .end(function(err, res) {
  275. var payload = (err || !res.ok || 'application/json' !== res.type)
  276. ? {type: 'warning', msg: (res.body && res.body.msg) ? res.body.msg : 'Request error', body: res.body}
  277. : {type: 'success', msg: res.body.msg || '', body: res.body}
  278. jQuery(document).trigger('DBG:notify', payload)
  279. console.log('F.fetchDataAjax res.body', res.body)
  280. console.log('F.fetchDataAjax payload', payload)
  281. jQuery.notify(payload.msg, payload.type)
  282. resultCallback(payload.body)
  283. });
  284. }
  285. break;
  286. default: {
  287. console.log("BUG: Unsupported type namespace '" + fieldType['@baseTypeName'] + "'");
  288. }
  289. }
  290. } else {
  291. console.log("BUG: Unsupported type '" + fieldType + "'");
  292. return;
  293. }
  294. }
  295. function syncZaliczkaState(state, updates, stateSyncSyccessCallback, stateSyncErrorCallback) {
  296. // TODO: if ajax success then stateSyncSyccessCallback(Response.data)
  297. // TODO: if ajax fail then stateSyncErrorCallback(Response.data)
  298. console.log('====== TODO: syncZaliczkaState updates:', updates, 'state:', state);
  299. superagent
  300. .post('<?= Request::getPathUri(); ?>/index.php?_route=UrlAction_UserProNetMediaZaliczka&_task=syncStataAjax&idUser=<?= $idUser; ?>&idZaliczka=<?= $idZaliczka; ?>') // TODO: add idUser & idZaliczka
  301. .type('json') // header ĺapplication/x-www-form-urlencoded' requires type('form');
  302. .send({
  303. updates: updates,
  304. })
  305. .set('Accept', 'application/json')
  306. .end(function(err, res) {
  307. var payload = (err || !res.ok || 'application/json' !== res.type)
  308. ? {type: 'warning', msg: (res.body && res.body.msg) ? res.body.msg : 'Request error', body: res.body}
  309. : {type: 'success', msg: res.body.msg || '', body: res.body}
  310. jQuery(document).trigger('DBG:notify', payload)
  311. console.log('F.syncZaliczkaState res.body', res.body)
  312. console.log('F.syncZaliczkaState payload', payload)
  313. jQuery.notify(payload.msg, payload.type)
  314. stateSyncSyccessCallback({data: payload.body})
  315. });
  316. }
  317. </script>
  318. <?php
  319. UI::tag('script', ['src'=>'static/vendor.js' . (V::get('DBG', '', $_GET) ? '?_ts=' . time() : ''), 'type'=>'text/javascript']);
  320. UI::tag('script', ['src'=>'static/zaliczka/main.js' . (V::get('DBG', '', $_GET) ? '?_ts=' . time() : ''), 'type'=>'text/javascript']);
  321. }
  322. }
  323. /* Akcje
  324. # dodanie pierwszej zaliczki - 500 zł
  325. $data = [ worker => [ login => 'Kowalski' ], kwota => 500.00 ]
  326. insert into `ZALICZKA` (`L_APPOITMENT_USER`, `KWOTA`) values('{$data['worker']['login']}', '{$data['kwota']}');
  327. -- return id = 123
  328. # rozliczenie częściowe pierwszej zaliczki np. 500 zł
  329. $data = [ id => 123, pozycja => [ [ kwota => 200.00, korespondencja => [ ... ], projekt => [ ... ] ] ] ]
  330. update `ZALICZKA__#REF__POZYCJA` set `A_STATUS` = 'DELETED' where `PRIMARY_KEY` = 123;
  331. */