[0002950] Fixed false quantity on hand errors on sales delivery.
[fa-stable.git] / sales / includes / cart_class.inc
index 6a014d5aba85425fbc9f5df77e3ef5b0461a165e..a363562eeb4f39ab5101b977a8944e62ac9056a9 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
@@ -92,7 +93,28 @@ class cart
                $this->read($type, $trans_no, $prep_child);
                $this->cart_id = uniqid('');
        }
-       
+
+       /*
+               Optional sorting items by stock_id.
+       */
+       function _cmp_lines($a, $b)
+       {
+               return strcmp($a->stock_id, $b->stock_id);
+       }
+
+       /*
+               Returns items array optionally sorted by item code.
+       */
+       function get_items()
+       {
+               global $sort_sales_items;
+
+               $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.
        //
@@ -104,6 +126,9 @@ class cart
 
                $this->trans_type = $type;
                $this->reference = $Refs->get_next($this->trans_type);
+               if ($type == ST_CUSTCREDIT)
+                   $this->src_date = $this->document_date;
+
                $this->document_date = new_doc_date();
 
                for($line_no = 0; $line_no < count($this->line_items); $line_no++) {
@@ -124,9 +149,6 @@ class cart
                        $this->due_date = get_invoice_duedate($this->payment, $this->document_date);
                }
 
-               if ($type == ST_CUSTCREDIT)
-                   $this->src_date = $this->document_date;
-
                $this->src_docs = $this->trans_no;
                $this->trans_no = 0;
        }
@@ -522,6 +544,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