+
+ /*
+ 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.
+ */
+ function is_prepaid()
+ {
+ return $this->prepaid;
+ }
+ /*
+ Order is ready for delivery in prepament mode.
+ */
+ function is_released()
+ {
+ return floatcmp($this->sum_paid, $this->prep_amount) >= 0;
+ }
+
+ /*
+ Check whether order has been already invoiced/send or not.
+ */
+ function is_started()
+ {
+ if ($this->trans_no==0)
+ return false;
+ $order_no = array_keys($this->trans_no);
+
+ return is_sales_order_started(reset($order_no));
+ }
+
+ /*
+ Check payment terms and prepayments selected for this invoice,
+ and updates partial/final invoice value respectively.
+ */
+ function update_payments()
+ {
+ $remainder = prepaid_invoice_remainder($this->order_no);
+
+ // recalculate prepaid part from payments
+ if ($this->payment_terms['days_before_due'] == -1)
+ { // this is partial invoice for selected prepayments made.
+ $paid = 0;
+ foreach($this->prepayments as $payment)
+ $paid += $payment['amt'];
+ $this->prep_amount = $this->trans_no ? $paid : min($remainder, $paid);
+ } else // this is final invoice
+ $this->prep_amount = $remainder;
+ }