Ejemplo n.º 1
0
 /**
  * 
  * @param array $params
  * Required field id_warehouse, id_product, id_uom, qty
  * Optional field app, id_ref
  * @return boolean
  * @throws UserException
  */
 public function updateStock($params)
 {
     $stock = ProductStock::findOne(['id_warehouse' => $params['id_warehouse'], 'id_product' => $params['id_product']]);
     $qty_per_uom = Helper::getQtyProductUom($params['id_product'], $params['id_uom']);
     if (!$stock) {
         $smallest_uom = Helper::getSmallestProductUom($params['id_product']);
         $stock = new ProductStock();
         $stock->setAttributes(['id_warehouse' => $params['id_warehouse'], 'id_product' => $params['id_product'], 'id_uom' => $smallest_uom, 'qty_stock' => 0]);
     }
     $stock->qty_stock = $stock->qty_stock + $params['qty'] * $qty_per_uom;
     if ($stock->canSetProperty('logParams')) {
         $logParams = ['mv_qty' => $params['qty'] * $qty_per_uom];
         foreach (['app', 'id_ref'] as $key) {
             if (isset($params[$key]) || array_key_exists($key, $params)) {
                 $logParams[$key] = $params[$key];
             }
         }
         $stock->logParams = $logParams;
     }
     if (!$stock->save()) {
         throw new UserException(implode(",\n", $stock->firstErrors));
     }
     return true;
 }
Ejemplo n.º 2
0
 public static function updateStock($params, $logs = [])
 {
     $stock = ProductStock::findOne(['id_warehouse' => $params['id_warehouse'], 'id_product' => $params['id_product']]);
     if (!$stock) {
         $stock = new ProductStock();
         $stock->setAttributes(['id_warehouse' => $params['id_warehouse'], 'id_product' => $params['id_product'], 'id_uom' => $params['id_uom'], 'qty_stock' => 0]);
     }
     $stock->qty_stock = $stock->qty_stock + $params['qty'];
     if (!empty($logs) && $stock->canSetProperty('logParams')) {
         $stock->logParams = $logs;
     }
     if (!$stock->save()) {
         throw new UserException(implode(",\n", $stock->firstErrors));
     }
     return true;
 }