فهرست منبع

fixed Zaliczka update pozycje

Piotr Labudda 9 سال پیش
والد
کامیت
e7c9fca8fa

+ 20 - 0
SE/se-lib/Core/AclBase.php

@@ -705,6 +705,26 @@ class Core_AclBase {
     ");
   }
 
+  public function removeRef($childName, $pk, $refPk) {// TODO: $idTransaction
+    $refTable = ACL::getRefTable($this->getNamespace(), $childName);
+    $sqlPk = DB::getPDO()->quote($pk, PDO::PARAM_STR);
+    $sqlRefPk = DB::getPDO()->quote($refPk, PDO::PARAM_STR);
+
+    DB::getPDO()->exec("
+      update `{$refTable}` set A_STATUS = 'DELETED'
+      where PRIMARY_KEY = {$sqlPk}
+        and REMOTE_PRIMARY_KEY = {$sqlRefPk}
+    ");
+
+    // TODO: REMOVE legacy code below:
+    $refTable = Core_AclHelper::getRefTable($this->getName(), $childName);
+    DB::getPDO()->exec("
+      update `{$refTable}` set A_STATUS = 'DELETED'
+      where PRIMARY_KEY = {$sqlPk}
+        and REMOTE_PRIMARY_KEY = {$sqlRefPk}
+    ");
+  }
+
   public function getChildHistTable($childName) {
     throw new Exception("Not implemented getChildHistTable in AclBase - only SimpleSchema is supported");
     // return Core_AclHelper::getChildHistTable($this->getRootTableName(), $childName, $schema);

+ 3 - 0
SE/se-lib/Core/AclSimpleSchemaBase.php

@@ -432,6 +432,9 @@ class Core_AclSimpleSchemaBase extends Core_AclBase {
         if ('xsd:' === substr($field['@type'], 0, 4)) {
           $sqlFieldName = (!empty($field['@alias'])) ? $field['@alias'] : $fieldName;
           $object[$fieldName] = V::get($sqlFieldName, '', $row);
+        } else if ('p5:enum' === $field['@type']) {
+          $sqlFieldName = (!empty($field['@alias'])) ? $field['@alias'] : $fieldName;
+          $object[$fieldName] = V::get($sqlFieldName, '', $row);
         } else if ('p5:' === substr($field['@type'], 0, 3)) {
           $object[$fieldName] = "TODO: generate value for type {$field['@type']} - field '{$fieldName}' ns({$this->_namespace})";// TODO: single field method like addP5Types
         } else throw new Exception("Not Implemented type for field '{$fieldName}' ns({$this->_namespace})");

+ 52 - 8
SE/se-lib/Schema/DefaultDb/zaliczka/ZaliczkaStorageAcl.php

@@ -41,12 +41,18 @@ class Schema_DefaultDb_zaliczka_ZaliczkaStorageAcl extends Core_AclSimpleSchemaB
       throw new Exception("Missing param #refFrom/primaryKey (id user)");
     }
     $idUser = V::get('primaryKey', 0, $params['#refFrom'], 'int');
-    return DB::getPDO()->fetchAll("
-      select z.*
-      from ZALICZKA z
-        join ADMIN_USERS u on(z.L_APPOITMENT_USER = u.ADM_ACCOUNT and u.ID = {$idUser})
-      order by z.ID DESC
-    ");
+
+    return array_map(
+      function ($row) use ($params) {
+        return $this->buildFromSqlRow($row, $params);
+      },
+      DB::getPDO()->fetchAll("
+        select z.*
+        from ZALICZKA z
+          join ADMIN_USERS u on(z.L_APPOITMENT_USER = u.ADM_ACCOUNT and u.ID = {$idUser})
+        order by z.ID DESC
+      ")
+    );
   }
 
   public function addItem($itemTodo) {
@@ -205,12 +211,13 @@ class Schema_DefaultDb_zaliczka_ZaliczkaStorageAcl extends Core_AclSimpleSchemaB
 
     // pozycja
     $pozAcl = ACL::getAclByNamespace('default_db/ZALICZKA_POZYCJA/ZaliczkaPozycja');
+    $pozPkField = $pozAcl->getPrimaryKeyField();
     if (empty($oldItem['pozycja'])) {
       DBG::log("empty \$oldItem['pozycja']");
       if (!empty($itemPatch['pozycja'])) {
         foreach ($itemPatch['pozycja'] as $pozTodo) {
-          if ($pozTodo['id'] !== -1) throw new Exception("BUG ZaliczkaPozycja id not equal -1");
-          unset($pozTodo['id']);
+          if ($pozTodo[ $pozPkField ] !== -1) throw new Exception("BUG ZaliczkaPozycja id not equal -1");
+          unset($pozTodo[ $pozPkField ]);
           $pkPoz = $pozAcl->addItem($pozTodo);
           DBG::log(['msg'=> '$pkPoz', $pkPoz]);
           $this->insertRef('pozycja', $pk, $pkPoz);
@@ -218,7 +225,44 @@ class Schema_DefaultDb_zaliczka_ZaliczkaStorageAcl extends Core_AclSimpleSchemaB
       }
     } else {
       DBG::log("not empty \$oldItem['pozycja'] " . ((!empty($itemPatch['pozycja'])) ? "not empty" : "empty") . " patch pozycja");
+      $pozToAdd = array();
+      $pozToUpdate = array();
+      $pozToRemove = array();
+      if (!empty($itemPatch['pozycja'])) {
+        foreach ($itemPatch['pozycja'] as $pozTodo) {
+          if ($pozTodo[ $pozPkField ] == -1) {
+            unset($pozTodo[ $pozPkField ]);
+            $pozToAdd[] = $pozTodo;
+          } else {
+            $pozToUpdate[ $pozTodo[ $pozPkField ] ] = $pozTodo;
+          }
+        }
+      }
+      foreach ($oldItem['pozycja'] as $pozOld) {
+        if ($pozOld[ $pozPkField ] > 0 && !array_key_exists($pozOld[ $pozPkField ], $pozToUpdate)) {
+          $pozToRemove[] = $pozOld[ $pozPkField ];
+        }
+      }
+
+      DBG::log(['msg'=> '$pozToAdd', $pozToAdd]);
+      DBG::log(['msg'=> '$pozToUpdate', $pozToUpdate]);
+      DBG::log(['msg'=> '$pozToRemove', $pozToRemove]);
 
+      foreach ($pozToAdd as $pozTodo) {
+        $pkPoz = $pozAcl->addItem($pozTodo);
+        DBG::log(['msg'=> '$pkPoz', $pkPoz]);
+        $this->insertRef('pozycja', $pk, $pkPoz);
+      }
+      foreach ($pozToRemove as $idPoz) {
+        // $rmPozItem = [];
+        // $rmPozItem[$pozPkField] = $idPoz;
+        // $rmPozItem['A_STATUS'] = 'DELETED';
+        // $pozAcl->updateItem($rmPozItem);// TODO: update instance and A_STATUS (cache)
+        $this->removeRef('pozycja', $pk, $idPoz);
+      }
+      foreach ($pozToUpdate as $pozTodo) {
+        $pozAcl->updateItem($pozTodo);
+      }
     }
 
     // korespondencja

+ 72 - 7
SE/se-lib/Schema/DefaultDb/zaliczka_pozycja/ZaliczkaPozycjaStorageAcl.php

@@ -9,7 +9,7 @@ class Schema_DefaultDb_zaliczka_pozycja_ZaliczkaPozycjaStorageAcl extends Core_A
     'root' => [
       '@namespace' => 'default_db/ZALICZKA_POZYCJA/ZaliczkaPozycja',// Api_WfsNs::getBaseWfsUri() . '/default_db/Zaliczka'
       '@primaryKey' => 'id',
-      'id' => 'xsd:integer',
+      '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 ],
@@ -62,6 +62,66 @@ class Schema_DefaultDb_zaliczka_pozycja_ZaliczkaPozycjaStorageAcl extends Core_A
     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;
+    }
+    if ($nettoToUpdate) {
+      $sqlPatch[ $this->getSqlFieldName('kwota_netto') ] = ($kwota > 0 && $vat > 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'];
+    }
+    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');
@@ -81,12 +141,17 @@ class Schema_DefaultDb_zaliczka_pozycja_ZaliczkaPozycjaStorageAcl extends Core_A
     //   ) ";
     }
     $sqlWhere = (!empty($sqlWhereAnd)) ? implode(" and ", $sqlWhereAnd) : "1=1";
-    return DB::getPDO()->fetchAll("
-      select z.*
-      from ZALICZKA_POZYCJA z
-      where {$sqlWhere}
-      order by z.ID ASC
-    ");
+    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
+      ")
+    );
   }
 
 }