/** * @see StockManagerInterface::addProduct() */ public function addProduct($id_product, $id_product_attribute = 0, Warehouse $warehouse, $quantity, $id_stock_mvt_reason, $price_te, $is_usable = true, $id_supply_order = null) { if (!Validate::isLoadedObject($warehouse) || !$price_te || !$quantity || !$id_product) { return false; } $price_te = (double) round($price_te, 6); if (!StockMvtReason::exists($id_stock_mvt_reason)) { $id_stock_mvt_reason = Configuration::get('PS_STOCK_MVT_INC_REASON_DEFAULT'); } $context = Context::getContext(); $mvt_params = array('id_stock' => null, 'physical_quantity' => $quantity, 'id_stock_mvt_reason' => $id_stock_mvt_reason, 'id_supply_order' => $id_supply_order, 'price_te' => $price_te, 'last_wa' => null, 'current_wa' => null, 'id_employee' => $context->employee->id, 'employee_firstname' => $context->employee->firstname, 'employee_lastname' => $context->employee->lastname, 'sign' => 1); $stock_exists = false; // switch on MANAGEMENT_TYPE switch ($warehouse->management_type) { // case CUMP mode case 'WA': $stock_collection = $this->getStockCollection($id_product, $id_product_attribute, $warehouse->id); // if this product is already in stock if (count($stock_collection) > 0) { $stock_exists = true; // for a warehouse using WA, there is one and only one stock for a given product $stock = $stock_collection->current(); // calculates WA price $last_wa = $stock->price_te; $current_wa = $this->calculateWA($stock, $quantity, $price_te); $mvt_params['id_stock'] = $stock->id; $mvt_params['last_wa'] = $last_wa; $mvt_params['current_wa'] = $current_wa; $stock_params = array('physical_quantity' => $stock->physical_quantity + $quantity, 'price_te' => $current_wa, 'usable_quantity' => $is_usable ? $stock->usable_quantity + $quantity : $stock->usable_quantity, 'id_warehouse' => $warehouse->id); // saves stock in warehouse $stock->hydrate($stock_params); $stock->update(); } else { $mvt_params['last_wa'] = 0; $mvt_params['current_wa'] = $price_te; } break; // case FIFO / LIFO mode // case FIFO / LIFO mode case 'FIFO': case 'LIFO': $stock_collection = $this->getStockCollection($id_product, $id_product_attribute, $warehouse->id, $price_te); // if this product is already in stock if (count($stock_collection) > 0) { $stock_exists = true; // there is one and only one stock for a given product in a warehouse and at the current unit price $stock = $stock_collection->current(); $stock_params = array('physical_quantity' => $stock->physical_quantity + $quantity, 'usable_quantity' => $is_usable ? $stock->usable_quantity + $quantity : $stock->usable_quantity); // updates stock in warehouse $stock->hydrate($stock_params); $stock->update(); // sets mvt_params $mvt_params['id_stock'] = $stock->id; } break; default: return false; break; } if (!$stock_exists) { $stock = new Stock(); $stock_params = array('id_product_attribute' => $id_product_attribute, 'id_product' => $id_product, 'physical_quantity' => $quantity, 'price_te' => $price_te, 'usable_quantity' => $is_usable ? $quantity : 0, 'id_warehouse' => $warehouse->id); // saves stock in warehouse $stock->hydrate($stock_params); $stock->add(); $mvt_params['id_stock'] = $stock->id; } // saves stock mvt $stock_mvt = new StockMvt(); $stock_mvt->hydrate($mvt_params); $stock_mvt->add(); return true; }
public function addStockMvt($quantity, $id_reason, $id_product_attribute = null, $id_order = null, $id_employee = null) { $stockMvt = new StockMvt(); $stockMvt->id_product = (int) $this->id; $stockMvt->id_product_attribute = (int) $id_product_attribute; $stockMvt->id_order = (int) $id_order; $stockMvt->id_employee = (int) $id_employee; $stockMvt->quantity = (int) $quantity; $stockMvt->id_stock_mvt_reason = (int) $id_reason; // adding stock mouvement, this action update the sotck of product in database only if ($stockMvt->add()) { // update quantity in object after adding the stock movement $this->quantity = $this->getStockAvailable(); Hook::updateQuantity($this, null); return true; } return false; }
public function addStockMvt($quantity, $id_reason, $id_product_attribute = NULL, $id_order = NULL, $id_employee = NULL) { $stockMvt = new StockMvt(); $stockMvt->id_product = (int) $this->id; $stockMvt->id_product_attribute = (int) $id_product_attribute; $stockMvt->id_order = (int) $id_order; $stockMvt->id_employee = (int) $id_employee; $stockMvt->quantity = (int) $quantity; $stockMvt->id_stock_mvt_reason = (int) $id_reason; $result = $stockMvt->add(); $last_quantity = $this->quantity; // Increase or decrease current product quantity value if ($id_reason == 1) { $this->quantity += abs($quantity); } else { if ($id_reason == 2) { $this->quantity -= abs($quantity); } } Hook::updateQuantity($this, null); //reindex the updated product if ($this->quantity < 1 || $last_quantity < 1 && $this->quantity > 0) { SolrSearch::updateProduct($this->id); } return $result; }
public function addStockMvt($quantity, $id_reason, $id_product_attribute = NULL, $id_order = NULL, $id_employee = NULL) { $stockMvt = new StockMvt(); $stockMvt->id_product = (int) $this->id; $stockMvt->id_product_attribute = (int) $id_product_attribute; $stockMvt->id_order = (int) $id_order; $stockMvt->id_employee = (int) $id_employee; $stockMvt->quantity = (int) $quantity; $stockMvt->id_stock_mvt_reason = (int) $id_reason; return $stockMvt->add(); }
public static function addMissingMvt($id_employee) { $products_without_attributes = Db::getInstance()->ExecuteS(' SELECT p.`id_product`, pa.`id_product_attribute`, (p.`quantity` - SUM(IFNULL(sm.`quantity`, 0))) quantity FROM `' . _DB_PREFIX_ . 'product` p LEFT JOIN `' . _DB_PREFIX_ . 'stock_mvt` sm ON (sm.`id_product` = p.`id_product`) LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa ON (pa.`id_product` = p.`id_product`) WHERE pa.`id_product_attribute` IS NULL GROUP BY p.`id_product` '); $products_with_attributes = Db::getInstance()->ExecuteS(' SELECT p.`id_product`, pa.`id_product_attribute`, SUM(pa.`quantity`) - SUM(IFNULL(sm.`quantity`, 0)) quantity FROM `' . _DB_PREFIX_ . 'product` p LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa ON (pa.`id_product` = p.`id_product`) LEFT JOIN `' . _DB_PREFIX_ . 'stock_mvt` sm ON (sm.`id_product` = pa.`id_product` AND sm.`id_product_attribute` = pa.`id_product_attribute`) WHERE pa.`id_product_attribute` IS NOT NULL GROUP BY pa.`id_product_attribute` '); $products = array_merge($products_without_attributes, $products_with_attributes); if ($products) { foreach ($products as $product) { if (!$product['quantity']) { continue; } $mvt = new StockMvt(); foreach ($product as $k => $row) { $mvt->{$k} = $row; } $mvt->id_employee = (int) $id_employee; $mvt->id_stock_mvt_reason = _STOCK_MOVEMENT_MISSING_REASON_; $mvt->add(true, false, false); } } }
public static function addMissingMvt($id_employee) { $products_attributes = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT p.`id_product`, coalesce(pa.`id_product_attribute`, 0) as id_product_attribute, (coalesce(pa.quantity,p.quantity) - SUM(coalesce(sm.`quantity`, 0))) as quantity FROM `' . _DB_PREFIX_ . 'product` p LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa ON (pa.`id_product` = p.`id_product`) LEFT JOIN `' . _DB_PREFIX_ . 'stock_mvt` sm ON (sm.`id_product` = p.`id_product` and sm.id_product_attribute = coalesce (pa.id_product_attribute,0)) WHERE p.`active` = 1 GROUP BY p.`id_product`, pa.`id_product_attribute` HAVING quantity != 0', false); while ($product = Db::getInstance()->nextRow($products_attributes)) { if (!isset($product) || !is_array($product) || !isset($product['quantity']) || !$product['quantity']) { continue; } $mvt = new StockMvt(); foreach ($product as $k => $row) { $mvt->{$k} = $row; } $mvt->id_employee = (int) $id_employee; $mvt->id_stock_mvt_reason = _STOCK_MOVEMENT_MISSING_REASON_; $mvt->add(true, false, false); } }