| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- Lib::loadClass('Core_AclSimpleSchemaBase');// extends Core_AclBase
- Lib::loadClass('Core_AclHelper');
- class Schema_DefaultDb_zaliczka_pozycja_ZaliczkaPozycjaStorageAcl extends Core_AclSimpleSchemaBase {
- public $_simpleSchema = [
- 'root' => [
- '@namespace' => 'default_db/ZALICZKA_POZYCJA/ZaliczkaPozycja',// Api_WfsNs::getBaseWfsUri() . '/default_db/Zaliczka'
- '@primaryKey' => 'id',
- 'id' => [ '@type' => 'xsd:integer', '@alias' => 'ID' ],
- 'nr_faktury' => [ '@type' => "xsd:string" ],
- 'kwota' => [ '@type' => 'xsd:decimal', '@totalDigits' => 16, '@fractionDigits' => 2 ],
- 'vat' => [ '@type' => "xsd:integer", '@default' => 23 ],
- 'kwota_netto' => [ '@type' => 'xsd:decimal', '@totalDigits' => 16, '@fractionDigits' => 2 ],
- 'kategoria_kosztu' => [ '@type' => 'p5:enum', '@aliasFieldValues' => [
- 'namespace' => 'default_db/IN7_DZIENNIK_KORESP',
- 'childName' => 'KATEGORIA_KOSZTU'
- ] ],// default_db__x3A__ZALICZKA_POZYCJA:kategoria_kosztuType, default_db__x3A__IN7_DZIENNIK_KORESP:kategoria_kosztuType
- 'typ_dokumentu' => [ '@type' => 'p5:enum' ], // enum('faktura', 'inne')
- // 'korespondencja' => [ '@ref' => 'default_db/IN7_DZIENNIK_KORESP/ZaliczkaKoresp' ],
- // 'projekt' => [ '@ref' => 'default_db/IN7_MK_BAZA_DYSTRYBUCJI/ZaliczkaProjekt' ]
- ],
- // 'kategoria_kosztuType' => [// TODO: enumeration or config for remote data fetch - Typespecial
- // <xsd:simpleType name="A_STATUSType">
- // <xsd:restriction base="xsd:string">
- // <xsd:enumeration value="DELETED"/>
- // <xsd:enumeration value="MONITOR"/>
- // <xsd:enumeration value="NORMAL"/>
- // <xsd:enumeration value="OFF_HARD"/>
- // <xsd:enumeration value="OFF_SOFT"/>
- // <xsd:enumeration value="WAITING"/>
- // <xsd:enumeration value="WARNING"/>
- // </xsd:restriction>
- // </xsd:simpleType>
- // ]
- ];
- public function addItem($itemTodo) {
- DBG::log(['msg'=> 'addItem', $itemTodo]);
- $sqlItem = array();
- if (!empty($itemTodo['nr_faktury'])) {
- $sqlItem[ $this->getSqlFieldName('nr_faktury') ] = $itemTodo['nr_faktury'];
- }
- if (!empty($itemTodo['kwota'])) {
- $kwota = V::get('kwota', 0, $itemTodo, 'price');
- if ($kwota > 0) $sqlItem[ $this->getSqlFieldName('kwota') ] = $kwota;
- }
- if (array_key_exists('vat', $itemTodo)) {
- $sqlItem[ $this->getSqlFieldName('vat') ] = (int)$itemTodo['vat'];
- }
- if (!empty($itemTodo['kategoria_kosztu'])) {
- $sqlItem[ $this->getSqlFieldName('kategoria_kosztu') ] = $itemTodo['kategoria_kosztu'];
- }
- if (!empty($itemTodo['typ_dokumentu'])) {
- $sqlItem[ $this->getSqlFieldName('typ_dokumentu') ] = $itemTodo['typ_dokumentu'];
- }
- $vat = V::get('vat', 23, $itemTodo, 'int');
- $sqlItem[ $this->getSqlFieldName('kwota_netto') ] = ($kwota > 0 && $vat > 0)
- ? $kwota / (1 + $vat / 100)
- : 0;
- if (empty($sqlItem)) throw new Exception("Empty record");
- return DB::getPDO()->insert($this->getRootTableName(), $sqlItem);
- }
- public function updateItem($itemPatch) {
- DBG::log(['msg'=> 'updateItem: $itemPatch', $itemPatch]);
- // 'id' => '1',
- // 'nr_faktury' => '100.',
- // 'kwota' => '123.46',
- // 'vat' => '23',
- // 'kwota_netto' => '100.37',
- // 'kategoria_kosztu' => 'BUDOWA: Wynagrodzenia osobowe + ZUS',
- $pkField = $this->getPrimaryKeyField();
- $pk = V::get($pkField, null, $itemPatch);
- if (null === $pk) throw new Exception("BUG missing primary key field for {$this->_namespace}");
- $oldItem = $this->getItem($pk);// TODO: only cols: ['id', 'nr_faktury', 'kwota', 'vat', 'kategoria_kosztu']
- DBG::log(['msg'=> 'updateItem: $oldItem', $oldItem]);
- if (!$oldItem) throw new Exception("BUG item '{$pk}' not found ({$this->_namespace})");
- $sqlPatch = array();
- if (array_key_exists('nr_faktury', $itemPatch) && $oldItem['nr_faktury'] != $itemPatch['nr_faktury']) {
- $sqlPatch[ $this->getSqlFieldName('nr_faktury') ] = $itemPatch['nr_faktury'];
- }
- $nettoToUpdate = false;
- $kwota = V::get('kwota', 0, $oldItem, 'price');
- if (array_key_exists('kwota', $itemPatch) && $oldItem['kwota'] != $itemPatch['kwota']) {
- $nettoToUpdate = true;
- $kwota = V::get('kwota', 0, $itemPatch, 'price');
- $sqlPatch[ $this->getSqlFieldName('kwota') ] = $kwota;
- }
- $vat = V::get('vat', 0, $oldItem, 'int');
- if (array_key_exists('vat', $itemPatch) && $oldItem['vat'] != $itemPatch['vat']) {
- $nettoToUpdate = true;
- $vat = V::get('vat', 0, $itemPatch, 'int');
- $sqlPatch[ $this->getSqlFieldName('vat') ] = $vat;
- }
- // DBG::log("\$nettoToUpdate = ({$nettoToUpdate})");
- if ($nettoToUpdate) {
- $vat = V::get('vat', 0, $oldItem, 'int');
- $vat = V::get('vat', $vat, $itemPatch, 'int');
- // DBG::log("\$vat = ({$vat}) \$kwota=({$kwota})");
- $sqlPatch[ $this->getSqlFieldName('kwota_netto') ] = ($kwota > 0)
- ? $kwota / (1 + $vat / 100)
- : 0;
- }
- if (array_key_exists('kategoria_kosztu', $itemPatch) && $oldItem['kategoria_kosztu'] != $itemPatch['kategoria_kosztu']) {
- $sqlPatch[ $this->getSqlFieldName('kategoria_kosztu') ] = $itemPatch['kategoria_kosztu'];
- }
- if (array_key_exists('typ_dokumentu', $itemPatch) && $oldItem['typ_dokumentu'] != $itemPatch['typ_dokumentu']) {
- $sqlPatch[ $this->getSqlFieldName('typ_dokumentu') ] = $itemPatch['typ_dokumentu'];
- }
- DBG::log(['msg'=> 'updateItem: $sqlPatch', $sqlPatch]);
- if (empty($sqlPatch)) return 0;
- return DB::getPDO()->update(
- $this->getRootTableName(),
- $this->getSqlPrimaryKeyField(),
- $pk,
- $sqlPatch
- );
- }
- public function getItem($primaryKey, $params = []) {
- $row = DB::getPDO()->fetchAll("
- select t.*
- from `ZALICZKA_POZYCJA` t
- where t.ID = {$primaryKey}
- ");
- $row = (!empty($row)) ? reset($row) : null;
- if (!$row) return null;
- return $this->buildFromSqlRow($row, $params);
- }
- public function getItems($params = array()) {
- DBG::log(['msg'=> 'getItems', $params]);
- // $idUser = V::get('primaryKey', 0, $params['#refFrom'], 'int');
- $sqlWhereAnd = array();
- if (empty($params)) {
- } else if (!empty($params['@primaryKey'])) {// [@primaryKey] => Array ([0] => 59599)
- if (is_array($params['@primaryKey'])) {
- $sqlWhereAnd[] = "z.ID in(" . implode(", ", $params['@primaryKey']) . ")";
- }
- // } else if (!empty($params['f_title'])) {
- // DBG::log("return filter by K_ZAWARTOS, K_OD_KOGO like '%{$params['f_title']}%' order by last created rows by user or another users");
- // $sqlParamTitle = DB::getPDO()->quote("%{$params['f_title']}%", PDO::PARAM_STR);
- // $sqlWhereAnd[] = " (
- // k.ID like {$sqlParamTitle}
- // or k.K_ZAWARTOS like {$sqlParamTitle}
- // or k.K_OD_KOGO like {$sqlParamTitle}
- // ) ";
- }
- $sqlWhere = (!empty($sqlWhereAnd)) ? implode(" and ", $sqlWhereAnd) : "1=1";
- return array_map(
- function ($row) use ($params) {
- return $this->buildFromSqlRow($row, $params);
- },
- DB::getPDO()->fetchAll("
- select z.*
- from ZALICZKA_POZYCJA z
- where {$sqlWhere}
- order by z.ID ASC
- ")
- );
- }
- }
- /* FIX BUG kwota_netto = 0
- SELECT ID, kwota, kwota_netto , vat , round(kwota / (1 + (vat / 100)), 2) FROM `ZALICZKA_POZYCJA`
- where kwota > 0 and kwota_netto = 0
- update `ZALICZKA_POZYCJA` set kwota_netto = round(kwota / (1 + (vat / 100)), 2) where kwota > 0 and kwota_netto = 0
- */
|