Payment_terms table normalization.
[fa-stable.git] / sales / includes / db / sales_invoice_db.inc
index 1f79aa09db3780410457b0c71af0a167d1172298..9b9ab467ed7eea9b4dd89990929fd19b23bc8b4a 100644 (file)
@@ -23,7 +23,7 @@ function write_sales_invoice(&$invoice)
        $date_ = $invoice->document_date;
        $charge_shipping =$invoice->freight_cost;
 
-       begin_transaction();
+       begin_transaction(__FUNCTION__, func_get_args());
 
        hook_db_prewrite($invoice, ST_SALESINVOICE);
        $company_data = get_company_prefs();
@@ -158,8 +158,10 @@ function write_sales_invoice(&$invoice)
        }
        $to_allocate = ($items_total + $charge_shipping + $items_added_tax + $freight_added_tax);
 
+       $freight = get_item($invoice->ship_via);
+
        if ($charge_shipping != 0) {
-               $total += add_gl_trans_customer(ST_SALESINVOICE, $invoice_no, $date_, $company_data["freight_act"], 0, 0,
+               $total += add_gl_trans_customer(ST_SALESINVOICE, $invoice_no, $date_, $freight["sales_account"], 0, 0,
                        -$invoice->get_tax_free_shipping()*$prepaid_factor, $invoice->customer_id);
        }
        // post all taxes
@@ -182,7 +184,7 @@ function write_sales_invoice(&$invoice)
 
        if ($trans_no == 0) {
                $Refs->save(ST_SALESINVOICE, $invoice_no, $invoice->reference, null, $invoice->fixed_asset);
-               if ($invoice->payment_terms['cash_sale'] && $invoice->pos['pos_account']) {
+               if ($invoice->payment_terms['type'] == PTT_CASH && $invoice->pos['pos_account']) {
                        $amount = $items_total + $items_added_tax + $invoice->freight_cost 
                                + $freight_added_tax;
 
@@ -214,7 +216,7 @@ function write_sales_invoice(&$invoice)
 
 function void_sales_invoice($type, $type_no)
 {
-       begin_transaction();
+       begin_transaction(__FUNCTION__, func_get_args());
 
        hook_db_prevoid($type, $type_no);
        void_bank_trans($type, $type_no, true);
@@ -294,9 +296,9 @@ function prepaid_invoice_remainder($order)
                        ." AND so.trans_type=".ST_SALESORDER
                        ." AND inv.type=".ST_SALESINVOICE
                        ." AND inv.order_=so.order_no"
-                       ." AND so.payment_terms=pt.terms_indicator"
-                       ." AND inv.payment_terms=pt.terms_indicator"
-                       ." AND pt.days_before_due = -1";
+                       ." AND so.payment_terms=pt.id"
+                       ." AND inv.payment_terms=pt.id"
+                       ." AND pt.type = ".PTT_PREPAY;
 
        $result = db_fetch(db_query($sql, "cannot find final invoice value"));
        return $result[0] ? $result[0] : 0;