Ejemplo n.º 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();
                 }
             }
         }
     }
 }
    // stock to reference
    $leftjoin['product_order_item'] = 'product_order_item.reference_id = product_reference.reference_id';
    // reference to order item
    $leftjoin['product_order'] = 'product_order.order_id = product_order_item.order_id';
    // order item to order
    $where[] = 'product_order_item.order_item_id NOT IN (
    SELECT product_order_item.order_item_id FROM product_order_item
    LEFT JOIN product_order_item_reception ON product_order_item_reception.order_item_id = product_order_item.order_item_id
    LEFT JOIN product_order ON product_order.order_id = product_order_item.order_id
    WHERE product_order.deleted = 0 AND product_order.cancelled = 0
    HAVING SUM(product_order_item_reception.quantity) < product_order_item.quantity
  )';
}
$pagination_size = CAppUI::conf("dPstock CProductStockGroup pagination_size");
$stock = new CProductStockGroup();
$list_stocks = $stock->loadList($where, 'product.name ASC', intval($start) . ",{$pagination_size}", "product_stock_group.stock_id", $leftjoin);
foreach ($list_stocks as $_stock) {
    $_stock->_ref_product->getPendingOrderItems(false);
}
if (!$only_ordered_stocks) {
    $list_stocks_count = $stock->countList($where, null, $leftjoin);
} else {
    $list_stocks_count = count($stock->loadList($where, null, null, "product_stock_group.stock_id", $leftjoin));
}
// Smarty template
$smarty = new CSmartyDP();
$smarty->assign('stock', $stock);
$smarty->assign('stock_id', $stock_id);
$smarty->assign('list_stocks', $list_stocks);
$smarty->assign('list_stocks_count', $list_stocks_count);
$smarty->assign('start', $start);