ZaliczkaPozycjaStorageAcl.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 (array_key_exists('vat', $itemTodo)) {
  46. $sqlItem[ $this->getSqlFieldName('vat') ] = (int)$itemTodo['vat'];
  47. }
  48. if (!empty($itemTodo['kategoria_kosztu'])) {
  49. $sqlItem[ $this->getSqlFieldName('kategoria_kosztu') ] = $itemTodo['kategoria_kosztu'];
  50. }
  51. $sqlItem[ $this->getSqlFieldName('kwota_netto') ] = ($kwota > 0 && $vat > 0)
  52. ? $kwota / (1 + $vat / 100)
  53. : 0;
  54. if (empty($sqlItem)) throw new Exception("Empty record");
  55. return DB::getPDO()->insert($this->getRootTableName(), $sqlItem);
  56. }
  57. public function updateItem($itemPatch) {
  58. DBG::log(['msg'=> 'updateItem: $itemPatch', $itemPatch]);
  59. // 'id' => '1',
  60. // 'nr_faktury' => '100.',
  61. // 'kwota' => '123.46',
  62. // 'vat' => '23',
  63. // 'kwota_netto' => '100.37',
  64. // 'kategoria_kosztu' => 'BUDOWA: Wynagrodzenia osobowe + ZUS',
  65. $pkField = $this->getPrimaryKeyField();
  66. $pk = V::get($pkField, null, $itemPatch);
  67. if (null === $pk) throw new Exception("BUG missing primary key field for {$this->_namespace}");
  68. $oldItem = $this->getItem($pk);// TODO: only cols: ['id', 'nr_faktury', 'kwota', 'vat', 'kategoria_kosztu']
  69. DBG::log(['msg'=> 'updateItem: $oldItem', $oldItem]);
  70. if (!$oldItem) throw new Exception("BUG item '{$pk}' not found ({$this->_namespace})");
  71. $sqlPatch = array();
  72. if (array_key_exists('nr_faktury', $itemPatch) && $oldItem['nr_faktury'] != $itemPatch['nr_faktury']) {
  73. $sqlPatch[ $this->getSqlFieldName('nr_faktury') ] = $itemPatch['nr_faktury'];
  74. }
  75. $nettoToUpdate = false;
  76. $kwota = V::get('kwota', 0, $oldItem, 'price');
  77. if (array_key_exists('kwota', $itemPatch) && $oldItem['kwota'] != $itemPatch['kwota']) {
  78. $nettoToUpdate = true;
  79. $kwota = V::get('kwota', 0, $itemPatch, 'price');
  80. $sqlPatch[ $this->getSqlFieldName('kwota') ] = $kwota;
  81. }
  82. $vat = V::get('vat', 0, $oldItem, 'int');
  83. if (array_key_exists('vat', $itemPatch) && $oldItem['vat'] != $itemPatch['vat']) {
  84. $nettoToUpdate = true;
  85. $vat = V::get('vat', 0, $itemPatch, 'int');
  86. $sqlPatch[ $this->getSqlFieldName('vat') ] = $vat;
  87. }
  88. if ($nettoToUpdate) {
  89. $sqlPatch[ $this->getSqlFieldName('kwota_netto') ] = ($kwota > 0 && $vat > 0)
  90. ? $kwota / (1 + $vat / 100)
  91. : 0;
  92. }
  93. if (array_key_exists('kategoria_kosztu', $itemPatch) && $oldItem['kategoria_kosztu'] != $itemPatch['kategoria_kosztu']) {
  94. $sqlPatch[ $this->getSqlFieldName('kategoria_kosztu') ] = $itemPatch['kategoria_kosztu'];
  95. }
  96. DBG::log(['msg'=> 'updateItem: $sqlPatch', $sqlPatch]);
  97. if (empty($sqlPatch)) return 0;
  98. return DB::getPDO()->update(
  99. $this->getRootTableName(),
  100. $this->getSqlPrimaryKeyField(),
  101. $pk,
  102. $sqlPatch
  103. );
  104. }
  105. public function getItem($primaryKey, $params = []) {
  106. $row = DB::getPDO()->fetchAll("
  107. select t.*
  108. from `ZALICZKA_POZYCJA` t
  109. where t.ID = {$primaryKey}
  110. ");
  111. $row = (!empty($row)) ? reset($row) : null;
  112. if (!$row) return null;
  113. return $this->buildFromSqlRow($row, $params);
  114. }
  115. public function getItems($params = array()) {
  116. DBG::log(['msg'=> 'getItems', $params]);
  117. // $idUser = V::get('primaryKey', 0, $params['#refFrom'], 'int');
  118. $sqlWhereAnd = array();
  119. if (empty($params)) {
  120. } else if (!empty($params['@primaryKey'])) {// [@primaryKey] => Array ([0] => 59599)
  121. if (is_array($params['@primaryKey'])) {
  122. $sqlWhereAnd[] = "z.ID in(" . implode(", ", $params['@primaryKey']) . ")";
  123. }
  124. // } else if (!empty($params['f_title'])) {
  125. // DBG::log("return filter by K_ZAWARTOS, K_OD_KOGO like '%{$params['f_title']}%' order by last created rows by user or another users");
  126. // $sqlParamTitle = DB::getPDO()->quote("%{$params['f_title']}%", PDO::PARAM_STR);
  127. // $sqlWhereAnd[] = " (
  128. // k.ID like {$sqlParamTitle}
  129. // or k.K_ZAWARTOS like {$sqlParamTitle}
  130. // or k.K_OD_KOGO like {$sqlParamTitle}
  131. // ) ";
  132. }
  133. $sqlWhere = (!empty($sqlWhereAnd)) ? implode(" and ", $sqlWhereAnd) : "1=1";
  134. return array_map(
  135. function ($row) use ($params) {
  136. return $this->buildFromSqlRow($row, $params);
  137. },
  138. DB::getPDO()->fetchAll("
  139. select z.*
  140. from ZALICZKA_POZYCJA z
  141. where {$sqlWhere}
  142. order by z.ID ASC
  143. ")
  144. );
  145. }
  146. }