Fixed Delivery note bug : Parent constraints not working if sales order line deleted...
[fa-stable.git] / sales / includes / cart_class.inc
index 9b8c27edf86573716a4b81aa8bff5934615a644c..8a4e11e6eb6d56ed1473e45ac02bbba57d294f27 100644 (file)
@@ -71,7 +71,8 @@ class cart
        var $payment;
        var $payment_terms; // cached payment terms
        var $credit;
-       
+       var $ex_rate;
+
        //-------------------------------------------------------------------------
        //
        //  $trans_no==0 => open new/direct document
@@ -101,12 +102,18 @@ class cart
                return strcmp($a->stock_id, $b->stock_id);
        }
 
-       function sort_items()
+       /*
+               Returns items array optionally sorted by item code.
+       */
+       function get_items()
        {
                global $sort_sales_items;
 
-               if (@$sort_sales_items && !$this->trans_no) // sort items optionally for new transaction
-                       usort($this->line_items, array($this, '_cmp_lines'));
+               $items = $this->line_items;
+               if (@$sort_sales_items)
+                       uasort($items, array($this, '_cmp_lines'));
+
+               return $items;
        }
        //
        //      Prepare cart to new child document entry, just after initial parent doc read.
@@ -161,6 +168,11 @@ class cart
                while (($line_no < count($this->line_items)) && ($srcline = db_fetch($srcdetails))) {
                        $line = &$this->line_items[$line_no];
                        $src_docs[] = $src_type == ST_SALESORDER ?  $srcline['order_no'] : $srcline['debtor_trans_no'];
+                       while($srcline['id'] != $line->src_id) // Logic : This will increment the line_items array till sales_order line is matched.
+                       {       // 0002259: Fixes Delivery note bug : Parent constraints not working if sales order line deleted after delivery 
+                       $line_no++;
+                       $line = &$this->line_items[$line_no];
+                       }
                        if ($srcline['id'] == $line->src_id) {
                                if ($this->trans_type == ST_SALESINVOICE)
                                        $line->src_no = $srcline['debtor_trans_no'];
@@ -366,7 +378,6 @@ class cart
 
                if ($line->valid) {
                        $this->line_items[$line_no] = $line;
-                       $this->sort_items();
                        return 1;
                } else
                        display_error(_("You have to enter valid stock code or nonempty description"));
@@ -381,7 +392,6 @@ class cart
                $this->line_items[$line_no]->qty_dispatched = $qty;
                $this->line_items[$line_no]->price = $price;
                $this->line_items[$line_no]->discount_percent = $disc;
-               $this->sort_items();
        }
 
        function update_add_cart_item_qty($line_no, $qty)
@@ -539,6 +549,41 @@ class cart
 
                return $total;
        }
+
+       /*
+               Checks cart quantities on document_date.
+               Returns array of stock_ids which stock quantities would go negative on some day.
+       */
+       function check_qoh($date=null, $location=null)
+       {
+               $low_stock = array();
+               // check only for customer delivery and direct sales invoice 
+               if (!($this->trans_type == ST_CUSTDELIVERY || ($this->trans_type == ST_SALESINVOICE && $this->trans_no==0)))
+                       return $low_stock;
+
+               // collect quantities by stock_id
+               $qtys = array();
+               foreach ($this->line_items as $line_no => $line_item)
+               {
+                       if (has_stock_holding($line_item->mb_flag))
+                       {
+                               if (!$this->trans_no) // new delivery
+                                       $qtys[$line_item->stock_id]['qty'] = $line_item->qty_dispatched + @$qtys[$line_item->stock_id]['qty'];
+                               else    // DN modification: check change in quantity
+                                       $qtys[$line_item->stock_id]['qty'] = ($line_item->qty_dispatched-$line_item->qty_old) + @$qtys[$line_item->stock_id]['qty'];
+                               $qtys[$line_item->stock_id]['line'] = $line_no;
+                       }
+               }
+
+               foreach($qtys as $stock_id => $sum)
+               {
+                       if (check_negative_stock($stock_id, -$sum['qty'], $location ? $location : $this->Location, $date ? $date : $this->document_date))
+                               $low_stock[] = $stock_id;
+               }
+
+               return $low_stock;
+       }
+
 } /* end of class defintion */
 
 class line_details