| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- <?php
- Lib::loadClass('RouteBase');
- Lib::loadClass('Typespecial');
- Lib::loadClass('TypespecialVariable');
- Lib::loadClass('Request');
- Lib::loadClass('Response');
- Lib::loadClass('ProcesHelper');
- Lib::loadClass('Route_UrlAction');
- Lib::loadClass('UI');
- Lib::loadClass('Core_AclHelper');
- // - [ ] zaliczkaListaView:
- // - [ ] btn "Dodaj Zaliczkę" $acl->addItem($item); // @require only 'worker' => [ 'id', 'login', 'name' ]
- // - [ ] lista wszystkich zaliczek danego usera + btn "load more ..."
- // - [x] Rozlicz / Podgląd - tylko najnowsza Zaliczka
- // - [ ] Rozlicz / Podgląd - edytowć może tylko właściciel / grupa / user connected by REF ?
- class Route_UrlAction_UserProNetMediaZaliczka extends RouteBase {// TODO: UrlActionBase @see Route_UrlAction
- public function handleAuth() {
- if (!User::logged()) {
- User::authByRequest();
- }
- }
- public function defaultAction() {
- UI::gora();
- UI::emptyTag('link', ['href'=>'static/zaliczka/main.css', 'rel'=>'stylesheet']);
- if (1 != V::get('_print', '', $_GET)) UI::menu();
- try {
- $data = array();
- $data['idUser'] = V::get('idUser', User::getID(), $_REQUEST, 'int');
- $data['idZaliczka'] = V::get('idZaliczka', 0, $_REQUEST, 'int');// TODO: idZaliczka is always MAX(z.ID);
- if (!$data['idZaliczka']) {
- $this->zaliczkaListaView($data);
- } else {
- echo UI::h('div', ['class' => "container", 'style' => 'margin-top:12px; margin-bottom:6px; padding-bottom:6px; border-bottom:1px solid #ddd'], [
- UI::h('a', [
- 'href' => "index.php?_route=UrlAction_UserProNetMediaZaliczka&idUser={$data['idUser']}",
- 'class' => "btn btn-link pull-left"
- ], "<i class=\"glyphicon glyphicon-arrow-left\"></i> wróć"),
- UI::h('a', [
- 'href' => "index.php?_route=ViewTableAjax&typeName=p5_default_db:IN7_DZIENNIK_KORESP#CREATE",
- 'class' => "btn btn-sm btn-default pull-right"
- ], "<span class=\"glyphicon glyphicon-plus\"></span> Dodaj dokument <i>(Korespondencję)</i>")
- ]);
- $this->zaliczkaFormView($data);
- }
- } catch (Exception $e) {
- UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
- }
- if (1 != V::get('_print', '', $_GET)) UI::dol();
- }
- public function syncStataAjaxAction() {
- Response::sendTryCatchJson(array($this, 'syncStataAjaxReponseCallback'), $args = 'JSON_FROM_REQUEST_BODY');// try to read json from request body
- }
- public function syncStataAjaxReponseCallback($args) {
- if (empty($args) || !array_key_exists('updates', $args)) throw new HttpException("Error Parsing Request - missing args", 400);
- $args['idUser'] = V::get('idUser', 0, $_REQUEST, 'int');// (int)$args['idUser'];
- if ($args['idUser'] <= 0) throw new HttpException("Error Parsing Request - missing idUser", 400);
- $args['idZaliczka'] = V::get('idZaliczka', 0, $_REQUEST, 'int');// (int)$args['idZaliczka'];
- if ($args['idZaliczka'] <= 0) throw new HttpException("Error Parsing Request - missing idZaliczka", 400);
- $acl = ACL::getAclByNamespace("default_db/ZALICZKA/Zaliczka");
- $schema = $acl->getSimpleSchemaTree();
- if (!empty($args['updates'])) {
- $acl->updateItem($args['updates']);
- }
- $zaliczkaData = $acl->getItem($args['idZaliczka'], [
- 'cols' => [
- '@instance',
- 'id',
- 'created',
- 'kwota',
- 'nierozliczona_kwota',
- 'pozycja',
- 'worker',
- 'korespondencja',
- ]
- ]);// $this->fetchDataBySchema($schema, $args['idUser']);
- return [
- 'type' => 'success',
- 'nierozliczonaKwota' => V::get('nierozliczona_kwota', 0, $zaliczkaData),
- 'schema' => [
- 'zaliczka' => $schema,
- ],
- 'zaliczka' => $zaliczkaData,
- ];
- }
- public function fetchDataAjaxAction() {
- Response::sendTryCatchJson(array($this, 'fetchDataAjaxReponseCallback'), $args = 'JSON_FROM_REQUEST_BODY');// try to read json from request body
- }
- public function fetchDataAjaxReponseCallback($args) {
- DBG::log(['msg' => 'args', 'args' => $args]);
- if (empty($args)) throw new HttpException("Error Parsing Request - missing args", 400);
- if (empty($args['schema']['@namespace']) && empty($args['objectNamespace'])) throw new HttpException("Error Parsing Request - missing @namespace", 400);
- if (!empty($args['schema']['@namespace'])) {
- if ('default_db/IN7_DZIENNIK_KORESP/ZaliczkaKoresp' == $args['schema']['@namespace']) {
- return [
- 'type' => 'success',
- 'options' => array_values(Core_AclHelper::getAclByNamespace($args['schema']['@namespace'])->getItems([
- 'f_title' => V::get('query', '', $args)
- ]))
- ];
- } else {
- return [ 'type' => "error", 'msg' => "Unsupported @namespace" ];
- }
- } else if (!empty($args['objectNamespace'])) {
- // childName : "kategoria_kosztu"
- // objectNamespace : "default_db/ZALICZKA_POZYCJA/ZaliczkaPozycja"
- if (empty($args['childName'])) throw new Exception("Missing childName");
- $acl = Core_AclHelper::getAclByNamespace($args['objectNamespace']);
- return [
- 'type' => 'success',
- 'options' => $acl->getEnumValues($args['childName'])
- ];
- }
- return [ 'type' => "error", 'msg' => "Unsupported @namespace or xsdType" ];
- }
- public function fetchDataBySchema($schema, $idUser, $primaryKey = null) {// if $primaryKey is null then search for last row
- return [// TODO: fetch data from DB
- 'id' => 123,
- 'created' => '2016-11-22', // data wydania zaliczki - data utworzenia rekordu
- 'worker' => [
- 'id' => 666,
- 'name' => 'Kowalski', // 'TODO' => get name from admin_users - imię i nazwisko pracownika pobierającego zaliczkę
- 'login' => 'kowalski'
- ],
- 'kwota' => 555.55, // kwota wypłaconej zaliczki
- 'nierozliczona_kwota' => 123.56,
- 'pozycja' => [
- [ 'id' => 1,
- 'kwota' => 100.22,
- 'korespondencja' => [ 'id' => 65432, 'title' => 'testowa koresp 1', 'kategoriaKosztu' => 'KOSZT1' ],
- 'projekt' => [ 'nrBudowy' => 'Budowa/1' ]
- ],
- [ 'id' => 2,
- 'kwota' => 200.55,
- 'korespondencja' => [ 'id' => 76543, 'title' => 'testowa koresp 2', 'kategoriaKosztu' => 'KOSZT2' ],
- 'projekt' => [ 'id' => 123, 'nrBudowy' => 'Budowa/2' ]
- ]
- ]
- ];
- }
- public function zaliczkaListaView($data) {
- $idUser = V::get('idUser', User::getID(), $data);
- $ownerAcl = Core_AclHelper::getAclByNamespace("default_objects/AccessOwner");
- $user = $ownerAcl->getItem($idUser);// [id, login, name]
- if (!$user) throw new Exception("Nie znaleziono pracownika o nr '{$idUser}'");
- $acl = Core_AclHelper::getAclByNamespace("default_db/ZALICZKA/Zaliczka");
- UI::startContainer(['style' => "padding-top:20px"]);
- UI::setTitle("Zaliczka");
- UI::table([
- 'caption' => "Zaliczki pracownika {$user['name']} " . '<br>' . UI::hButtonPost("Dodaj nową zaliczkę", [
- 'data' => [
- '_route' => "UrlAction_UserProNetMediaZaliczka",
- '_task' => "add",
- 'idUser' => $idUser,
- ],
- 'fields' => [
- [ 'name' => "kwota", 'type' => 'text', 'class' => 'form-control input-sm', 'style' => "width:200px", 'placeholder' => "kwota" ]
- ],
- 'class' => 'btn-sm btn-primary',
- 'form.style' => 'display:block; margin-top:4px'
- ]),
- 'rows' => array_map(
- function ($row) use ($idUser) {
- $instance = DB::getPDO()->fetchValue("select INSTANCE_NAME from `ZALICZKA__#INSTANCE` where PRIMARY_KEY = {$row['ID']}");
- $editLink = Request::getPathUri() . "index.php?_route=UrlAction_UserProNetMediaZaliczka&idUser={$idUser}&idZaliczka={$row['ID']}";
- $editLabel = ('ZaliczkaAktywna' == $instance) ? "Rozlicz / Podgląd" : "Podgląd";
- $histLink = Request::getPathUri() . "index.php?_route=UrlAction_UserProNetMediaZaliczka&_task=hist&idUser={$idUser}&idZaliczka={$row['ID']}";
- if (V::get('DBG', '', $_GET)) {
- return [
- 'Nr' => $row['ID'],
- 'Status' => $row['A_STATUS'],
- 'INSTANCE' => $instance,
- 'L_APPOITMENT_USER' => $row['L_APPOITMENT_USER'],
- 'KWOTA' => $row['KWOTA'],
- 'NIEROZLICZONA_KWOTA' => $row['NIEROZLICZONA_KWOTA'],
- '#' => '<a href="' . $editLink . '">' . $editLabel . '</a>',
- 'DBG' => '<a href="' . $histLink . '">' . "DBG HIST" . '</a>'
- ];
- }
- return [
- 'Nr' => $row['ID'],
- 'Typ' => $instance,
- 'Pracownik' => $row['L_APPOITMENT_USER'],
- 'Kwota' => $row['KWOTA'],
- 'Nierozliczona kwota' => $row['NIEROZLICZONA_KWOTA'],
- '#' => '<a href="' . $editLink . '">' . $editLabel . '</a>',
- ];
- },
- $acl->getItems([
- '#refFrom' => [
- 'namespace' => 'default_db__x3A__ADMIN_USERS/Worker',
- 'primaryKey' => $idUser
- ]
- ])
- ),
- 'empty_msg' => "Brak",
- 'disable_lp' => true,
- ]);
- UI::endContainer();
- }
- public function addAction() {
- $idUser = V::get('idUser', User::getID(), $_REQUEST);
- $kwota = V::get('kwota', 0, $_REQUEST, 'price');
- try {
- $acl = Core_AclHelper::getAclByNamespace("default_db/ZALICZKA/Zaliczka");
- $id = $acl->addItem([
- 'kwota' => $kwota,
- 'worker' => [
- 'id' => $idUser
- ]
- ]);
- if (!$id) throw new Exception("Nie udało się utworzyć zaliczki");
- } catch (Exception $e) {
- UI::gora();
- UI::startContainer();
- UI::alert('danger', $e->getMessage() . UI::h('p', [], [
- UI::h('a', [ 'href' => "index.php?_route=UrlAction_UserProNetMediaZaliczka&idUser={$idUser}" ], [
- '<i class="glyphicon glyphicon-arrow-left"></i>',
- " wróć",
- ])
- ]));
- UI::endContainer();
- UI::dol();
- DBG::log($e);
- exit;
- }
- $redirectUrl = Request::getPathUri() . "index.php?_route=UrlAction_UserProNetMediaZaliczka&idUser={$idUser}&idZaliczka={$id}";
- if (!headers_sent()) {
- header("Location: {$redirectUrl}");
- } else {
- echo'<script type="text/javascript">'."
- window.location.href='{$redirectUrl}';
- ".'</script>';
- echo "\n".'<noscript>';
- echo "\n".'<meta http-equiv="refresh" content="0;url='.$redirectUrl.'" />';
- echo "\n".'</noscript>';
- echo'<p>'.'<a href="'.$redirectUrl.'">'."dalej".'</a>'.'</p>';
- }
- }
- public function reinstallAction() {
- UI::gora();
- try {
- Core_AclHelper::getAclByNamespace("default_db/ZALICZKA/Zaliczka")->reinstall();
- UI::startContainer();
- UI::alert('success', "Structure for 'default_db/ZALICZKA/Zaliczka' created in a database");
- $backUrl = Request::getPathUri() . "index.php?_route=UrlAction_UserProNetMediaZaliczka";
- UI::tag('a', ['href'=>$backUrl, 'class'=>"btn btn-primary"], "wróć");
- $databaseName = DB::getPDO()->getDatabaseName();
- UI::table([
- 'caption' => "Zaliczka",
- 'rows' => DB::getPDO()->fetchAll("
- select t.TABLE_NAME, t.COLUMN_NAME, t.DATA_TYPE, t.COLUMN_TYPE
- from `information_schema`.`COLUMNS` t
- where t.TABLE_SCHEMA = '{$databaseName}'
- and (t.TABLE_NAME like 'ZALICZKA'
- or t.TABLE_NAME like 'ZALICZKA\_\_%'
- or t.TABLE_NAME like 'ZALICZKA_HIST'
- or t.TABLE_NAME like 'Zaliczka\_\_%'
- )
- order by t.TABLE_NAME asc, t.COLUMN_NAME asc
- ")
- ]);
- UI::table([
- 'caption' => "Zaliczka Pozycja",
- 'rows' => DB::getPDO()->fetchAll("
- select t.TABLE_NAME, t.COLUMN_NAME, t.DATA_TYPE, t.COLUMN_TYPE
- from `information_schema`.`COLUMNS` t
- where t.TABLE_SCHEMA = '{$databaseName}'
- and (t.TABLE_NAME like 'ZALICZKA_POZYCJA'
- or t.TABLE_NAME like 'ZALICZKA_POZYCJA\_\_%'
- or t.TABLE_NAME like 'ZALICZKA_POZYCJA_HIST'
- or t.TABLE_NAME like 'ZaliczkaPozycja\_\_%'
- )
- order by t.TABLE_NAME asc, t.COLUMN_NAME asc
- ")
- ]);
- $tableList = array_map(
- function($item) {
- return $item['TABLE_NAME'];
- }
- , DB::getPDO()->fetchAll("
- select t.TABLE_NAME
- from `information_schema`.`COLUMNS` t
- where t.TABLE_SCHEMA = '{$databaseName}'
- and (t.TABLE_NAME like 'ZALICZKA'
- or t.TABLE_NAME like 'ZALICZKA\_\_%'
- or t.TABLE_NAME like 'ZALICZKA_HIST'
- or t.TABLE_NAME like 'Zaliczka\_\_%'
- or t.TABLE_NAME like 'ZALICZKA_POZYCJA'
- or t.TABLE_NAME like 'ZALICZKA_POZYCJA\_\_%'
- or t.TABLE_NAME like 'ZALICZKA_POZYCJA_HIST'
- or t.TABLE_NAME like 'ZaliczkaPozycja\_\_%'
- )
- group by t.TABLE_NAME
- ")
- );
- foreach ($tableList as $tableName) {
- UI::startTag('div', ['style'=>"margin:10px 0; max-width:100%; overflow:scroll; border:1px solid #ccc"]);
- UI::table([
- 'caption' => "Tabela '{$tableName}'",
- 'rows' => DB::getPDO()->fetchAll("
- select t.*
- from `{$tableName}` t
- ")
- ]);
- UI::endTag('div');
- }
- if ('1' == V::get('_clear', '', $_GET)) {
- foreach ($tableList as $tableName) {
- DB::getPDO()->exec("TRUNCATE `{$tableName}`");
- }
- UI::tag('a', ['class'=>"btn btn-primary", 'href'=>"index.php?_route=UrlAction_UserProNetMediaZaliczka&_task=reinstall"], "Dane usunięte - odśwież stronę");
- } else {
- UI::tag('a', ['class'=>"btn btn-danger", 'href'=>"index.php?_route=UrlAction_UserProNetMediaZaliczka&_task=reinstall&_clear=1"], "Wyczyść tabele");
- }
- UI::startTag('pre');
- foreach ($tableList as $tableName) {
- echo "DROP TABLE `{$tableName}`;\n";
- }
- UI::endTag('pre');
- ACL::getRefTable('default_db/ZALICZKA/Zaliczka', 'worker');
- ACL::getRefTable('default_db/ZALICZKA/Zaliczka', 'pozycja');
- ACL::getRefTable('default_db/ZALICZKA/Zaliczka', 'korespondencja');
- $refZaliczkaTables = DB::getPDO()->fetchAll("
- select t.*
- from `CRM_REF_CONFIG` t
- where t.ROOT_OBJECT_NS like 'default_db/ZALICZKA%'
- ");
- UI::startTag('div', ['style'=>"margin:10px 0; max-width:100%; overflow:scroll; border:1px solid #ccc"]);
- UI::table([
- 'caption' => "Tabela 'CRM_REF_CONFIG'",
- 'rows' => $refZaliczkaTables
- ]);
- UI::endTag('div');
- foreach ($refZaliczkaTables as $refInfo) {
- $tableName = "CRM__#REF_TABLE__" . $refInfo['ID'];
- UI::startTag('div', ['style'=>"margin:10px 0; max-width:100%; overflow:scroll; border:1px solid #ccc"]);
- UI::table([
- 'caption' => "Tabela REF '{$tableName}' - <small><i>{$refInfo['ROOT_OBJECT_NS']} REF {$refInfo['CHILD_NAME']} ({$refInfo['CHILD_NS']})</i></small>",
- 'rows' => DB::getPDO()->fetchAll("
- select t.*
- from `{$tableName}` t
- ")
- ]);
- UI::endTag('div');
- }
- if ('1' == V::get('_clear', '', $_GET)) {
- foreach ($refZaliczkaTables as $refInfo) {
- $tableName = "CRM__#REF_TABLE__" . $refInfo['ID'];
- DB::getPDO()->exec("TRUNCATE `{$tableName}`");
- }
- UI::tag('a', ['class'=>"btn btn-primary", 'href'=>"index.php?_route=UrlAction_UserProNetMediaZaliczka&_task=reinstall"], "Dane usunięte - odśwież stronę");
- }
- UI::endContainer();
- } catch (Exception $e) {
- UI::alert('danger', $e->getMessage());
- DBG::log($e);
- }
- UI::dol();
- }
- public function zaliczkaFormView($data) {
- $idUser = V::get('idUser', User::getID(), $data);
- $idZaliczka = V::get('idZaliczka', 0, $data);
- UI::tag('div', ['id'=>"zaliczka-app", 'data-dbg'=>V::get('DBG', '', $_GET), 'data-sync-js-function'=>"syncZaliczkaState", 'data-fetch-data-js-function'=>"zaliczkaFetchData"]);
- UI::emptyTag('br');
- $acl = Core_AclHelper::getAclByNamespace("default_db/ZALICZKA/Zaliczka");
- $schema = $acl->getSimpleSchemaTree();
- // DBG::nicePrint($schema, '$schema');
- UI::inlineJS(__FILE__ . '.fetchData.js', [
- 'SYNC_URL' => Request::getPathUri() . "index.php?_route=UrlAction_UserProNetMediaZaliczka&_task=fetchDataAjax&idUser={$idUser}&idZaliczka={$idZaliczka}",
- 'DBG' => (DBG::isActive())
- ]);
- UI::inlineJS(__FILE__ . '.syncState.js', [
- 'SYNC_URL' => Request::getPathUri() . "index.php?_route=UrlAction_UserProNetMediaZaliczka&_task=syncStataAjax&idUser={$idUser}&idZaliczka={$idZaliczka}",
- 'DBG' => (DBG::isActive())
- ]);
- UI::tag('script', ['src'=>'static/vendor.js' . (V::get('DBG', '', $_GET) ? '?_ts=' . time() : ''), 'type'=>'text/javascript']);
- UI::tag('script', ['src'=>'static/zaliczka/main.js' . (V::get('DBG', '', $_GET) ? '?_ts=' . time() : ''), 'type'=>'text/javascript']);
- }
- public function histAction() {
- UI::gora();
- // UI::menu();
- try {
- $idZaliczka = V::get('idZaliczka', 0, $_GET, 'int');
- if ($idZaliczka <= 0) throw new Exception("Missing idZaliczka");
- $acl = Core_AclHelper::getAclByNamespace("default_db/ZALICZKA/Zaliczka");
- $zaliczka = $acl->getItem($idZaliczka);
- if (!$zaliczka) throw new Exception("Zaliczka not found!");
- DBG::nicePrint($zaliczka, '$zaliczka');
- } catch (Exception $e) {
- UI::alert('danger', $e->getMessage());
- }
- UI::dol();
- }
- }
- /* Akcje
- # dodanie pierwszej zaliczki - 500 zł
- $data = [ worker => [ login => 'Kowalski' ], kwota => 500.00 ]
- insert into `ZALICZKA` (`L_APPOITMENT_USER`, `KWOTA`) values('{$data['worker']['login']}', '{$data['kwota']}');
- -- return id = 123
- # rozliczenie częściowe pierwszej zaliczki np. 500 zł
- $data = [ id => 123, pozycja => [ [ kwota => 200.00, korespondencja => [ ... ], projekt => [ ... ] ] ] ]
- update `ZALICZKA__#REF__POZYCJA` set `A_STATUS` = 'DELETED' where `PRIMARY_KEY` = 123;
- */
|