return false;
}
} else {
- if (($_SESSION['Items']->payment_terms['days_before_due'] < 0) && !count($_SESSION['Items']->prepayments)) {
+ if (($_SESSION['Items']->payment_terms['days_before_due'] == -1) && !count($_SESSION['Items']->prepayments)) {
display_error(_("There is no non-invoiced payments for this order. If you want to issue final invoice, select delayed or cash payment terms."));
return false;
}
if (isset($_POST['process_invoice']) && check_data()) {
$newinvoice= $_SESSION['Items']->trans_no == 0;
copy_to_cart();
+
if ($newinvoice)
new_doc_date($_SESSION['Items']->document_date);
$remainder = prepaid_invoice_remainder($this->order_no);
// recalculate prepaid part from payments
- if ($this->payment_terms['days_before_due'] < 0)
+ 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 = min($remainder, $paid);
+ $this->prep_amount = $this->trans_no ? $paid : min($remainder, $paid);
} else // this is final invoice
$this->prep_amount = $remainder;
}
return $row[0];
}
+/*
+ Return yet not invoiced amount of prepayment invoice (or 0 if normal invoice)
+*/
function prepaid_invoice_remainder($order)
{
$sql = "SELECT so.total - IFNULL(SUM(inv.prep_amount),0) FROM "
.TB_PREF."sales_orders so,
- ".TB_PREF."debtor_trans inv
- LEFT JOIN ".TB_PREF."payment_terms pt ON inv.payment_terms=pt.terms_indicator
+ ".TB_PREF."debtor_trans inv,
+ ".TB_PREF."payment_terms pt
WHERE so.order_no=".db_escape($order)
." AND so.trans_type=".ST_SALESORDER
." AND inv.type=".ST_SALESINVOICE
." AND inv.order_=so.order_no"
- ." AND pt.days_before_due<0";
+ ." AND so.payment_terms=pt.terms_indicator"
+ ." AND inv.payment_terms=pt.terms_indicator"
+ ." AND pt.days_before_due = -1";
$result = db_fetch(db_query($sql, "cannot find final invoice value"));
- return $result[0];
+ return $result[0] ? $result[0] : 0;
}
+
/*
Find oldest delivery date for sales invoice
*/
table_section(1);
locations_list_row(_("Deliver from Location:"), 'Location', null, false, true, $order->fixed_asset);
- if ($order->payment_terms['days_before_due'] < 0)
+ if ($order->payment_terms['days_before_due'] == -1)
{
$Ajax->addUpdate('items_table', 'prep_amount', price_format($order->get_trans_total())); // bind to items_table update
if (!$order->is_started())
return false;
}
if ($_SESSION['Items']->payment_terms['cash_sale'] == 0) {
- if (!$_SESSION['Items']->is_started() && ($_SESSION['Items']->payment_terms['days_before_due'] < 0) && ((input_num('prep_amount')<=0) ||
+ if (!$_SESSION['Items']->is_started() && ($_SESSION['Items']->payment_terms['days_before_due'] == -1) && ((input_num('prep_amount')<=0) ||
input_num('prep_amount')>$_SESSION['Items']->get_trans_total())) {
display_error(_("Pre-payment required have to be positive and less than total amount."));
set_focus('prep_amount');