示例#1
0
 /**
  * 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();
                 }
             }
         }
     }
 }