ZaliczkaPozycjaStorageAcl.php 7.4 KB

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