Allow reuse of references previously used on voided documents.
[fa-stable.git] / purchasing / includes / db / invoice_db.inc
index d3837072e6f1b77e0806019cb58eb618dccc6e3e..c584115c858330bc3bd016d50c0939cbb91a674e 100644 (file)
@@ -98,18 +98,6 @@ function update_supp_received_items_for_invoice($id, $po_detail_item, $qty_invoi
        return array($ret, $date, $unit_price);
 }
 
-function get_deliveries_between($stock_id, $from, $to)
-{
-       $from = date2sql($from);
-       $to = date2sql($to);
-       $sql = "SELECT SUM(-qty), SUM(-qty*standard_cost) FROM ".TB_PREF."stock_moves
-               WHERE type=".ST_CUSTDELIVERY." AND stock_id=".db_escape($stock_id)." AND
-                       tran_date>='$from' AND tran_date<='$to' GROUP BY stock_id";
-
-       $result = db_query($sql, "The deliveries could not be updated");
-       return db_fetch_row($result);
-}
-
 function get_diff_in_home_currency($supplier, $old_date, $date, $amount1, $amount2)
 {
        $dec = user_price_dec();
@@ -143,11 +131,10 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
 
     $invoice_items_total = $supp_trans->get_total_charged($supp_trans->tax_group_id);
 
-       if ($supp_trans->is_invoice)
-               $trans_type = ST_SUPPINVOICE;
-       else
+       $trans_type = $supp_trans->trans_type;
+       
+       if ($trans_type == ST_SUPPCREDIT)
        {
-               $trans_type = ST_SUPPCREDIT;
                // let's negate everything because it's a credit note
                $invoice_items_total = -$invoice_items_total;
                $tax_total = -$tax_total;
@@ -187,7 +174,7 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
            /*GL Items are straight forward - just do the debit postings to the GL accounts specified -
            the credit is to creditors control act  done later for the total invoice value + tax*/
 
-               if (!$supp_trans->is_invoice)
+               if ($trans_type == ST_SUPPCREDIT)
                        $entered_gl_code->amount = -$entered_gl_code->amount;
 
                $memo_ = $entered_gl_code->memo_;
@@ -198,7 +185,7 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
                        $entered_gl_code->amount, $memo_);
 
                // store tax details if the gl account is a tax account
-               if (!$supp_trans->is_invoice)
+               if ($trans_type == ST_SUPPCREDIT)
                        $entered_gl_code->amount = -$entered_gl_code->amount;
                add_gl_tax_details($entered_gl_code->gl_code, 
                        $trans_type, $invoice_id, $entered_gl_code->amount,
@@ -207,7 +194,7 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
     foreach ($supp_trans->grn_items as $entered_grn)
     {
 
-       if (!$supp_trans->is_invoice)
+       if ($trans_type == ST_SUPPCREDIT)
        {
                        $entered_grn->this_quantity_inv = -$entered_grn->this_quantity_inv;
                        set_grn_item_credited($entered_grn, $supp_trans->supplier_id, $invoice_id, $date_);
@@ -220,7 +207,7 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
                        $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'],
                        $entered_grn->this_quantity_inv * $line_taxfree, $supp_trans->supplier_id);
        // -------------- if price changed since po received. 16 Aug 2008 Joe Hunt
-       if($supp_trans->is_invoice)
+       if ($trans_type == ST_SUPPINVOICE)
        {
                $old = update_supp_received_items_for_invoice($entered_grn->id, $entered_grn->po_detail_item,
                        $entered_grn->this_quantity_inv, $entered_grn->chg_price);
@@ -253,12 +240,17 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
                                $amt = ($mat_cost - $deliveries[1]) * $deliveries[0]; // $amt in home currency
                                if ($amt != 0.0)
                                {
+                                       $_s = $entered_grn->item_code;
+                                       $_d = $deliveries[0];
+                                       $_od = $old_date;
+                                       $_cd = $mat_cost - $deliveries[1];
+                                       $memo = _("Cost difference adjustment for $_s. $_d items delivered since $_od. The cost difference is $_cd");
                                        add_gl_trans($trans_type, $invoice_id, $date_,  $stock_gl_code["cogs_account"],
-                                               $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], _("Cost diff."),
+                                               $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
                                                $amt, null, null, null,
                                                "The general ledger transaction could not be added for the price variance of the inventory item");
                                        add_gl_trans($trans_type, $invoice_id, $date_,  $iv_act,
-                                               0, 0, _("Cost diff."), -$amt, null, null, null,
+                                               0, 0, $memo, -$amt, null, null, null,
                                                "The general ledger transaction could not be added for the price variance of the inventory item");
                                }               
                                update_stock_move_pid(ST_CUSTDELIVERY, $entered_grn->item_code, $old_date, $date_, 0, $mat_cost);
@@ -279,7 +271,7 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
        if ($taxitem['Net'] != 0)
        {
 
-               if (!$supp_trans->is_invoice)
+               if ($trans_type == ST_SUPPCREDIT)
                {
                        $taxitem['Net'] = -$taxitem['Net'];
                        $taxitem['Value'] = -$taxitem['Value'];
@@ -289,7 +281,7 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
                                $taxitem['tax_type_id'], $taxitem['rate'], 0, $taxitem['Value'],
                                $taxitem['Net'], $ex_rate, $date_, $supp_trans->supp_reference);
 
-               if (!$supp_trans->is_invoice)
+               if ($trans_type == ST_SUPPCREDIT)
                        $taxitem['Value'] = -$taxitem['Value'];
                $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_,
                        $taxitem['purchasing_gl_code'], 0, 0, $taxitem['Value'],
@@ -396,7 +388,7 @@ function read_supp_invoice($trans_no, $trans_type, &$supp_trans)
                {
                        $supp_trans->add_grn_to_trans($details_row["grn_item_id"], $details_row["po_detail_item_id"], $details_row["stock_id"],
                                        $details_row["description"], 0, 0, $details_row["quantity"], 0, $details_row["FullUnitPrice"],
-                                       false, 0, 0);
+                                       0, 0);
                }
                else
                {