[ '@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 // // // // // // // // // // // // ] ]; public function addItem($itemTodo) { DBG::log(['msg'=> 'addItem', $itemTodo]); $sqlItem = array(); $kwota = V::get('kwota', 0, $itemTodo, 'price'); if ($kwota > 0) $sqlItem[ $this->getSqlFieldName('kwota') ] = $kwota; if (!empty($itemTodo['nr_faktury'])) { $sqlItem[ $this->getSqlFieldName('nr_faktury') ] = $itemTodo['nr_faktury']; } 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) : $kwota; if (empty($sqlItem)) throw new Exception("Empty record"); $sqlItem['A_RECORD_CREATE_DATE'] = 'NOW()'; $sqlItem['A_RECORD_CREATE_AUTHOR'] = User::getLogin(); $idCreated = DB::getPDO()->insert($this->getRootTableName(), $sqlItem); if ($idCreated > 0) { $sqlHistItem = $sqlItem; $sqlHistItem['ID_USERS2'] = $idCreated; DB::getPDO()->insert($this->getRootTableName() . "_HIST", $sqlHistItem); } return $idCreated; } 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 p.ID, p.kwota, p.kwota_netto , p.vat , round(p.kwota / (1 + (p.vat / 100)), 2), p.A_RECORD_CREATE_DATE, p.A_RECORD_CREATE_AUTHOR, p.A_RECORD_UPDATE_DATE, p.A_RECORD_UPDATE_AUTHOR , j.* , z.* FROM `ZALICZKA_POZYCJA` p left join `CRM__#REF_TABLE__2` j on (j.REMOTE_PRIMARY_KEY = p.ID) left join `ZALICZKA` z on(z.ID = j.PRIMARY_KEY) where p.kwota > 0 and p.kwota_netto = 0 update `ZALICZKA_POZYCJA` set kwota_netto = round(kwota / (1 + (vat / 100)), 2) where kwota > 0 and kwota_netto = 0 */