Exemplo n.º 1
0
 /**
  * Set count for stock and sku. 
  * Make insert, update or delete depending on input parameters and current state (sku-stock record in table)
  * Take into account stocking log (@see shopProductStocksLog)
  * 
  * @param string[mixed] $data Specify sku ID, stock ID, count
  * @param int           $data['sku_id'] sku ID, obligatory
  * @param int           $data['product_id'] product ID, optional
  * @param int           $data['stock_id'] stock ID, obligatory
  * @param int|null      $data['count'] count, obligatory. 
  * 
  * <code>
  * array(
  *     'sku_id' => 123,      // sku ID, obligatory
  *     'product_id' => 111,  // product ID, optional
  *     'stock_id' => 23,     // stock ID, obligatory
  *     'count' => 12,        // Maybe null or integer greater or equals 0
  * )
  * </code>
  * 
  * @return boolean
  */
 public function set($data)
 {
     if (empty($data['sku_id']) || empty($data['stock_id'])) {
         return false;
     }
     // isset doesn't work correctly with null
     if (!array_key_exists('count', $data)) {
         return false;
     }
     $count = $data['count'];
     if (empty($data['product_id'])) {
         $product_skus_model = new shopProductSkusModel();
         $data['product_id'] = $product_skus_model->select('product_id')->where('sku_id = :sku_id', array('sku_id' => $data['sku_id']))->fetchField();
     }
     if (empty($data['product_id'])) {
         return false;
     }
     $key = array('sku_id' => $data['sku_id'], 'stock_id' => $data['stock_id']);
     $item = $this->getByField($key);
     if ($item && $count !== null && $count == $item['count'] || !$item && $count === null) {
         // nothing to update
         return true;
     }
     $log_data = array('product_id' => $data['product_id'], 'sku_id' => $data['sku_id'], 'stock_id' => $data['stock_id'], 'before_count' => null, 'after_count' => $count);
     if ($item) {
         $log_data['before_count'] = $item['count'];
     }
     if ($count === null) {
         $op = 'delete';
     } else {
         if ($item) {
             $op = 'update';
         } else {
             $op = 'insert';
         }
     }
     if ($op == 'delete') {
         $this->deleteByField($key);
     } else {
         if ($op == 'update') {
             $this->updateByField($key, $data);
         } else {
             $this->insert($data);
         }
     }
     $log_model = new shopProductStocksLogModel();
     $log_model->add($log_data);
     return true;
 }
Exemplo n.º 2
0
 private function writeOffCount($product_id, $sku_id)
 {
     /**
      * @var shopProductStocksLogModel
      */
     static $log_model = null;
     $count = $this->select('count')->where('id=i:sku_id', array('sku_id' => $sku_id))->fetchField();
     if ($count === null) {
         return;
     }
     if (!$log_model) {
         $log_model = new shopProductStocksLogModel();
     }
     $log_model->add(array('product_id' => $product_id, 'sku_id' => $sku_id, 'before_count' => $count, 'after_count' => 0));
 }