/** * when saving, update supply needs for product * */ protected function _afterSave() { parent::_afterSave(); $productId = $this->getProductId(); //check if stock changed. If so, add a stock movement to if ($this->getqty() != $this->getOrigData('qty')) { //get product stock from stock movement to check if it is different $stockMovementResult = mage::getModel('Purchase/Productstock')->ComputeProductStock($productId); if ($this->getqty() != $stockMovementResult) { //add stock movement $diff = $this->getqty() - $stockMovementResult; $model = mage::getModel('Purchase/StockMovement'); //Cree le movement $model->setsm_product_id($productId)->setsm_qty($diff)->setsm_coef($model->GetTypeCoef('adjustment'))->setsm_description('')->setsm_type('adjustment')->setsm_date(date('Y-m-d'))->save(); } } //check if we have to update supply needs $updateSupplyNeeds = false; if ($this->getqty() != $this->getOrigData('qty')) { $updateSupplyNeeds = true; } if ($this->getmin_qty() != $this->getOrigData('min_qty')) { $updateSupplyNeeds = true; } if ($this->getuse_config_min_qty() != $this->getOrigData('use_config_min_qty')) { $updateSupplyNeeds = true; } if ($this->getmanage_stock() != $this->getOrigData('manage_stock')) { $updateSupplyNeeds = true; } if ($this->getuse_config_manage_stock() != $this->getOrigData('use_config_manage_stock')) { $updateSupplyNeeds = true; } if ($this->getnotify_stock_qty() != $this->getOrigData('notify_stock_qty')) { $updateSupplyNeeds = true; } if ($this->getuse_config_notify_stock_qty() != $this->getOrigData('use_config_notify_stock_qty')) { $updateSupplyNeeds = true; } if ($updateSupplyNeeds) { Mage::dispatchEvent('purchase_update_supply_needs_for_product', array('product_id' => $productId, 'from' => 'stockitem aftersave')); } }