ZaliczkaPozycjaStorageAcl.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. Lib::loadClass('Core_AclSimpleSchemaBase');// extends Core_AclBase
  3. Lib::loadClass('Core_AclHelper');
  4. class Schema_DefaultDb_zaliczka_pozycja_ZaliczkaPozycjaStorageAcl extends Core_AclSimpleSchemaBase {
  5. public $_simpleSchema = [
  6. 'root' => [
  7. '@namespace' => 'default_db/ZALICZKA_POZYCJA/ZaliczkaPozycja',// Api_WfsNs::getBaseWfsUri() . '/default_db/Zaliczka'
  8. '@primaryKey' => 'id',
  9. 'id' => [ '@type' => 'xsd:integer', '@alias' => 'ID' ],
  10. 'nr_faktury' => [ '@type' => "xsd:string" ],
  11. 'kwota' => [ '@type' => 'xsd:decimal', '@totalDigits' => 16, '@fractionDigits' => 2 ],
  12. 'vat' => [ '@type' => "xsd:integer", '@default' => 23 ],
  13. 'kwota_netto' => [ '@type' => 'xsd:decimal', '@totalDigits' => 16, '@fractionDigits' => 2 ],
  14. 'kategoria_kosztu' => [ '@type' => 'p5:enum', '@aliasFieldValues' => [
  15. 'namespace' => 'default_db/IN7_DZIENNIK_KORESP',
  16. 'childName' => 'KATEGORIA_KOSZTU'
  17. ] ],// default_db__x3A__ZALICZKA_POZYCJA:kategoria_kosztuType, default_db__x3A__IN7_DZIENNIK_KORESP:kategoria_kosztuType
  18. // 'korespondencja' => [ '@ref' => 'default_db/IN7_DZIENNIK_KORESP/ZaliczkaKoresp' ],
  19. // 'projekt' => [ '@ref' => 'default_db/IN7_MK_BAZA_DYSTRYBUCJI/ZaliczkaProjekt' ]
  20. ],
  21. // 'kategoria_kosztuType' => [// TODO: enumeration or config for remote data fetch - Typespecial
  22. // <xsd:simpleType name="A_STATUSType">
  23. // <xsd:restriction base="xsd:string">
  24. // <xsd:enumeration value="DELETED"/>
  25. // <xsd:enumeration value="MONITOR"/>
  26. // <xsd:enumeration value="NORMAL"/>
  27. // <xsd:enumeration value="OFF_HARD"/>
  28. // <xsd:enumeration value="OFF_SOFT"/>
  29. // <xsd:enumeration value="WAITING"/>
  30. // <xsd:enumeration value="WARNING"/>
  31. // </xsd:restriction>
  32. // </xsd:simpleType>
  33. // ]
  34. ];
  35. public function addItem($itemTodo) {
  36. DBG::log(['msg'=> 'addItem', $itemTodo]);
  37. $sqlItem = array();
  38. if (!empty($itemTodo['nr_faktury'])) {
  39. $sqlItem[ $this->getSqlFieldName('nr_faktury') ] = $itemTodo['nr_faktury'];
  40. }
  41. if (!empty($itemTodo['kwota'])) {
  42. $kwota = V::get('kwota', 0, $itemTodo, 'price');
  43. if ($kwota > 0) $sqlItem[ $this->getSqlFieldName('kwota') ] = $kwota;
  44. }
  45. if (!empty($itemTodo['vat'])) {
  46. $vat = (int)$itemTodo['vat'];
  47. if ($vat > 0) $sqlItem[ $this->getSqlFieldName('vat') ] = $vat;
  48. }
  49. if (!empty($itemTodo['kategoria_kosztu'])) {
  50. $sqlItem[ $this->getSqlFieldName('kategoria_kosztu') ] = $itemTodo['kategoria_kosztu'];
  51. }
  52. $sqlItem[ $this->getSqlFieldName('kwota_netto') ] = ($kwota > 0 && $vat > 0)
  53. ? $kwota / (1 + $vat / 100)
  54. : 0;
  55. if (empty($sqlItem)) throw new Exception("Empty record");
  56. return DB::getPDO()->insert($this->getRootTableName(), $sqlItem);
  57. }
  58. public function updateItem($itemPatch) {
  59. DBG::log(['msg'=> 'updateItem: $itemPatch', $itemPatch]);
  60. // 'id' => '1',
  61. // 'nr_faktury' => '100.',
  62. // 'kwota' => '123.46',
  63. // 'vat' => '23',
  64. // 'kwota_netto' => '100.37',
  65. // 'kategoria_kosztu' => 'BUDOWA: Wynagrodzenia osobowe + ZUS',
  66. $pkField = $this->getPrimaryKeyField();
  67. $pk = V::get($pkField, null, $itemPatch);
  68. if (null === $pk) throw new Exception("BUG missing primary key field for {$this->_namespace}");
  69. $oldItem = $this->getItem($pk);// TODO: only cols: ['id', 'nr_faktury', 'kwota', 'vat', 'kategoria_kosztu']
  70. DBG::log(['msg'=> 'updateItem: $oldItem', $oldItem]);
  71. if (!$oldItem) throw new Exception("BUG item '{$pk}' not found ({$this->_namespace})");
  72. $sqlPatch = array();
  73. if (array_key_exists('nr_faktury', $itemPatch) && $oldItem['nr_faktury'] != $itemPatch['nr_faktury']) {
  74. $sqlPatch[ $this->getSqlFieldName('nr_faktury') ] = $itemPatch['nr_faktury'];
  75. }
  76. $nettoToUpdate = false;
  77. $kwota = V::get('kwota', 0, $oldItem, 'price');
  78. if (array_key_exists('kwota', $itemPatch) && $oldItem['kwota'] != $itemPatch['kwota']) {
  79. $nettoToUpdate = true;
  80. $kwota = V::get('kwota', 0, $itemPatch, 'price');
  81. $sqlPatch[ $this->getSqlFieldName('kwota') ] = $kwota;
  82. }
  83. $vat = V::get('vat', 0, $oldItem, 'int');
  84. if (array_key_exists('vat', $itemPatch) && $oldItem['vat'] != $itemPatch['vat']) {
  85. $nettoToUpdate = true;
  86. $vat = V::get('vat', 0, $itemPatch, 'int');
  87. $sqlPatch[ $this->getSqlFieldName('vat') ] = $vat;
  88. }
  89. if ($nettoToUpdate) {
  90. $sqlPatch[ $this->getSqlFieldName('kwota_netto') ] = ($kwota > 0 && $vat > 0)
  91. ? $kwota / (1 + $vat / 100)
  92. : 0;
  93. }
  94. if (array_key_exists('kategoria_kosztu', $itemPatch) && $oldItem['kategoria_kosztu'] != $itemPatch['kategoria_kosztu']) {
  95. $sqlPatch[ $this->getSqlFieldName('kategoria_kosztu') ] = $itemPatch['kategoria_kosztu'];
  96. }
  97. DBG::log(['msg'=> 'updateItem: $sqlPatch', $sqlPatch]);
  98. if (empty($sqlPatch)) return 0;
  99. return DB::getPDO()->update(
  100. $this->getRootTableName(),
  101. $this->getSqlPrimaryKeyField(),
  102. $pk,
  103. $sqlPatch
  104. );
  105. }
  106. public function getItem($primaryKey, $params = []) {
  107. $row = DB::getPDO()->fetchAll("
  108. select t.*
  109. from `ZALICZKA_POZYCJA` t
  110. where t.ID = {$primaryKey}
  111. ");
  112. $row = (!empty($row)) ? reset($row) : null;
  113. if (!$row) return null;
  114. return $this->buildFromSqlRow($row, $params);
  115. }
  116. public function getItems($params = array()) {
  117. DBG::log(['msg'=> 'getItems', $params]);
  118. // $idUser = V::get('primaryKey', 0, $params['#refFrom'], 'int');
  119. $sqlWhereAnd = array();
  120. if (empty($params)) {
  121. } else if (!empty($params['@primaryKey'])) {// [@primaryKey] => Array ([0] => 59599)
  122. if (is_array($params['@primaryKey'])) {
  123. $sqlWhereAnd[] = "z.ID in(" . implode(", ", $params['@primaryKey']) . ")";
  124. }
  125. // } else if (!empty($params['f_title'])) {
  126. // DBG::log("return filter by K_ZAWARTOS, K_OD_KOGO like '%{$params['f_title']}%' order by last created rows by user or another users");
  127. // $sqlParamTitle = DB::getPDO()->quote("%{$params['f_title']}%", PDO::PARAM_STR);
  128. // $sqlWhereAnd[] = " (
  129. // k.ID like {$sqlParamTitle}
  130. // or k.K_ZAWARTOS like {$sqlParamTitle}
  131. // or k.K_OD_KOGO like {$sqlParamTitle}
  132. // ) ";
  133. }
  134. $sqlWhere = (!empty($sqlWhereAnd)) ? implode(" and ", $sqlWhereAnd) : "1=1";
  135. return array_map(
  136. function ($row) use ($params) {
  137. return $this->buildFromSqlRow($row, $params);
  138. },
  139. DB::getPDO()->fetchAll("
  140. select z.*
  141. from ZALICZKA_POZYCJA z
  142. where {$sqlWhere}
  143. order by z.ID ASC
  144. ")
  145. );
  146. }
  147. }