/** * Fills the order in function of the stocks and future stocks * * @return void */ function autofill() { $this->updateFormFields(); $this->completeField('societe_id'); // if the order has not been ordered yet // and not partially received // and not totally received // and not cancelled // and not deleted if (!$this->date_ordered && !$this->_received && !$this->cancelled && !$this->deleted) { // we empty the order foreach ($this->_ref_order_items as $item) { $item->delete(); } } // we retrieve all the stocks $stock = new CProductStockGroup(); $list_stocks = $stock->loadList(); // for every stock foreach ($list_stocks as $stock) { $stock->loadRefsFwd(); // if the stock is in the "red" or "orange" zone if ($stock->_zone_future < 2) { $current_stock = $stock->quantity; $expected_stock = $stock->getOptimumQuantity(); if ($current_stock < $expected_stock) { // we get the best reference for this product $where = array('product_id' => " = '{$stock->_ref_product->_id}'", 'societe_id' => " = '{$this->societe_id}'"); $orderby = 'price ASC'; $best_reference = new CProductReference(); if ($best_reference->loadObject($where, $orderby) && $best_reference->quantity > 0) { // we store the new order item in the current order $order_item = new CProductOrderItem(); $order_item->order_id = $this->_id; $order_item->quantity = $expected_stock - $current_stock; $order_item->reference_id = $best_reference->_id; $order_item->unit_price = $best_reference->price; $order_item->store(); } } } } }
$stock->loadRefsFwd(); $stock->_ref_product->loadRefsFwd(); } else { if ($product_id) { $product = new CProduct(); $product->load($product_id); $stock->product_id = $product_id; $stock->_ref_product = $product; } else { $stock->loadRefsFwd(); } } $stock->updateFormFields(); $list_services = CProductStockGroup::getServicesList(); foreach ($list_services as $_service) { $stock_service = new CProductStockService(); $stock_service->object_id = $_service->_id; $stock_service->object_class = $_service->_class; $stock_service->product_id = $stock->product_id; if (!$stock_service->loadMatchingObject()) { $stock_service->quantity = $stock->_ref_product->quantity; $stock_service->order_threshold_min = $stock->_ref_product->quantity; $stock_service->order_threshold_optimum = max($stock->getOptimumQuantity(), $stock_service->quantity); } $_service->_ref_stock = $stock_service; } // Création du template $smarty = new CSmartyDP(); $smarty->assign('stock', $stock); $smarty->assign('list_services', $list_services); $smarty->display('inc_edit_stock_group.tpl');