X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;f=sales%2Fincludes%2Fcart_class.inc;h=78f8309aaae42bb4debab6a444ec97d569c7e3f1;hb=1b9351ae81a67726e757eba55895a4f059c7f128;hp=fe2130d38a7384cb0834d390144d396805694abb;hpb=7e830126b96477e969fe3b48d9fc0e78f6c1fe00;p=fa-stable.git diff --git a/sales/includes/cart_class.inc b/sales/includes/cart_class.inc index fe2130d3..78f8309a 100644 --- a/sales/includes/cart_class.inc +++ b/sales/includes/cart_class.inc @@ -73,13 +73,14 @@ class cart var $payment; var $payment_terms; // cached payment terms var $credit; - // prepayment mode: var $prepaid; // true for documents issued in prepayment mode var $prep_amount; // prepayment required for SO, invoiced amount for prepaiament invoice var $sum_paid; // sum of all allocated prepayments both to order and related invoices var $alloc; // sum of payments allocated to this document var $prepayments = array(); // allocation records for this document + var $ex_rate; + //------------------------------------------------------------------------- // // $trans_no==0 => open new/direct document @@ -101,7 +102,28 @@ class cart $this->read($type, $trans_no, $prepare_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. // @@ -114,6 +136,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++) { @@ -135,9 +160,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; } @@ -254,12 +276,20 @@ class cart // Makes parent documents for direct delivery/invoice by recurent call. // $policy - 0 or 1: writeoff/return for IV, back order/cancel for DN function write($policy=0) { + begin_transaction(); // prevents partial database changes in case of direct delivery/invoice + if ($this->reference != 'auto' && $this->trans_no == 0 && !is_new_reference($this->reference, $this->trans_type)) + { + commit_transaction(); + return -1; + } if (count($this->src_docs) == 0 && ($this->trans_type == ST_SALESINVOICE || $this->trans_type == ST_CUSTDELIVERY) && !$this->is_prepaid()) { // this is direct document - first add parent $ref = $this->reference; $date = $this->document_date; $due_date = $this->due_date; + $dimension_id = $this->dimension_id; + $dimension2_id = $this->dimension2_id; $this->trans_type = get_parent_type($this->trans_type); $this->reference = 'auto'; @@ -270,6 +300,8 @@ class cart $this->document_date = $date; $this->reference = $ref; $this->due_date = $due_date; + $this->dimension_id = $dimension_id; + $this->dimension2_id = $dimension2_id; } $this->reference = @html_entity_decode($this->reference, ENT_QUOTES); $this->Comments = @html_entity_decode($this->Comments, ENT_QUOTES); @@ -526,6 +558,40 @@ 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; + } + /* Returns true for documents issued in prepayment cycle. */