Modifying Supplier Credit: reverted GL postings - fixed.
[fa-stable.git] / purchasing / includes / db / invoice_db.inc
index 0a7f360b647af2638404c59b4d0347aded6f0de5..844994521b9dace304d612dc28e80f459596e5c5 100644 (file)
@@ -39,10 +39,7 @@ function read_supplier_details_to_trans(&$supp_trans, $supplier_id)
                if (!is_date_in_fiscalyear($supp_trans->tran_date))
                        $supp_trans->tran_date = end_fiscalyear();
        }
-       if ($supp_trans->supplier_id != $supplier_id)
-           get_duedate_from_terms($supp_trans);
 
-    $supp_trans->supplier_id = $supplier_id;
     $supp_trans->tax_included = $myrow['tax_included'];
     $supp_trans->supplier_name = $myrow['supp_name'];
        $supp_trans->terms = array( 
@@ -50,6 +47,11 @@ function read_supplier_details_to_trans(&$supp_trans, $supplier_id)
                'days_before_due' => $myrow['days_before_due'], 
                'day_in_following_month' => $myrow['day_in_following_month'] );
 
+       if ($supp_trans->supplier_id != $supplier_id)
+           get_duedate_from_terms($supp_trans);
+
+    $supp_trans->supplier_id = $supplier_id;
+
        $supp_trans->credit = $myrow['cur_credit'];
 
        $supp_trans->tax_description = $myrow['tax_group_name'];
@@ -122,7 +124,6 @@ function get_diff_in_home_currency($supplier, $old_date, $date, $amount1, $amoun
        return $diff;
 }
 //----------------------------------------------------------------------------------------
-
 function add_supp_invoice(&$supp_trans)
 {
        global $Refs;
@@ -131,6 +132,10 @@ function add_supp_invoice(&$supp_trans)
        $trans_no = $supp_trans->trans_no;
        $trans_type = $supp_trans->trans_type;
        $supplier = get_supplier($supp_trans->supplier_id);
+    $dim = !empty($supp_trans->dimension) ? $supp_trans->dimension :
+        ($supplier['dimension_id'] ? $supplier['dimension_id'] : 0);
+    $dim2 = !empty($supp_trans->dimension2) ? $supp_trans->dimension2 :
+        ($supplier['dimension2_id'] ? $supplier['dimension2_id'] : 0);
 
        begin_transaction();
        hook_db_prewrite($supp_trans, $trans_type);
@@ -138,7 +143,7 @@ function add_supp_invoice(&$supp_trans)
     $taxes = $supp_trans->get_taxes($supp_trans->tax_group_id);
        if ($trans_no) {
                $allocs = get_payments_for($trans_no, $trans_type, $supp_trans->supplier_id); // save allocations
-               void_transaction($trans_type, $trans_no, Today(), _("Document reentered."));
+               void_transaction($trans_type, $trans_no, $supp_trans->tran_date, _("Document reentered."));
                $Refs->restore_last($trans_type, $trans_no);
        } else
                $allocs = get_po_prepayments($supp_trans);
@@ -160,7 +165,6 @@ function add_supp_invoice(&$supp_trans)
            foreach ($taxes as $n => $taxitem)
                        $item_added_tax += isset($taxitem['Override']) ? $taxitem['Override'] : round2($taxitem['Value'], user_price_dec());
        }
-
        if ($trans_type == ST_SUPPCREDIT)
        {
                // let's negate everything because it's a credit note
@@ -209,7 +213,7 @@ function add_supp_invoice(&$supp_trans)
                                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'],
+                                       $taxitem['purchasing_gl_code'], $dim, $dim2, $taxitem['Value'],
                                        $supp_trans->supplier_id,
                                        "A general ledger transaction for the tax amount could not be added");
                        }
@@ -219,7 +223,7 @@ function add_supp_invoice(&$supp_trans)
                $net_diff = -$net_diff;
 
     /* Now the AP account */
-    $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $supplier["payable_account"], 0, 0,
+    $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $supplier["payable_account"], $dim, $dim2,
                -($invoice_items_total +  $item_added_tax + $supp_trans->ov_discount),
                $supp_trans->supplier_id,
                "The general ledger transaction for the control total could not be added");
@@ -337,7 +341,6 @@ function add_supp_invoice(&$supp_trans)
                                                $ref = $Refs->get_next(ST_JOURNAL, null, $date_);
                                                add_journal(ST_JOURNAL, $id, $diff, $date_, get_company_currency(), $ref);
                                                $stock_id = $entered_grn->item_code;
-                                               $stock_gl_code = get_stock_gl_code($stock_id);
                                                $memo = _("Supplier invoice adjustment for zero inventory of ").$stock_id." "._("Invoice")." ".$supp_trans->reference;
                                                //Reverse the inventory effect if $qoh <=0
                                                add_gl_trans_std_cost(ST_JOURNAL, $id, $date_, 
@@ -491,8 +494,8 @@ 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"], 
                                        $details_row['qty_recd'],
-                                       $details_row['quantity_inv']-$details_row["quantity"],
-                                       $details_row["quantity"], 0, $details_row["FullUnitPrice"],
+                                       $details_row['quantity_inv'],
+                                       ($trans_type == ST_SUPPCREDIT ? -1 : 1) * $details_row["quantity"], 0, $details_row["FullUnitPrice"], // SUPPCREDIT quantities are stored negative in database
                                        0, 0);
                }
                else
@@ -614,11 +617,11 @@ function void_supp_invoice($type, $type_no)
                                                //Reverse the inventory effect if $qoh <=0
                                                add_gl_trans_std_cost(ST_JOURNAL, $id, $old_date, 
                                                        $stock_gl_code["inventory_account"],
-                                                       $dim, $dim2, $memo, $details_row["quantity"] * $diff);
+                                                       $details_row['dimension_id'], $details_row['dimension2_id'], $memo, $details_row["quantity"] * $diff);
                                                //GL Posting to inventory adjustment account
                                                add_gl_trans_std_cost(ST_JOURNAL, $id, $old_date, 
                                                        $stock_gl_code["adjustment_account"],
-                                                       $dim, $dim2, $memo, -$details_row["quantity"] * $diff);
+                                                       $details_row['dimension_id'], $details_row['dimension2_id'], $memo, -$details_row["quantity"] * $diff);
                                                
                                                add_audit_trail(ST_JOURNAL, $id, $old_date);
                                                add_comments(ST_JOURNAL, $id, $old_date, $memo);