Merged changes from mai trunk up to 2.3.1
[fa-stable.git] / purchasing / includes / db / invoice_db.inc
index 58e3aeb7bf6c7ab2c126bbc78abfb8adcb51df37..06d27cde04265d5093cd79c1647c729b1a85678e 100644 (file)
@@ -189,6 +189,8 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
                        $trans_type, $invoice_id, $entered_gl_code->amount,
                        $ex_rate, $date_, $supp_trans->supp_reference, $supp_trans->tax_included);
     }
+    
+    $clearing_act = get_company_pref('grn_clearing_act');
     foreach ($supp_trans->grn_items as $line_no => $entered_grn)
     {
 
@@ -200,17 +202,20 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
                $line_taxfree = $entered_grn->taxfree_charge_price($supp_trans->tax_group_id);
                $line_tax = $entered_grn->full_charge_price($supp_trans->tax_group_id) - $line_taxfree;
                $stock_gl_code = get_stock_gl_code($entered_grn->item_code);
-               $iv_act = (is_inventory_item($entered_grn->item_code) ? $stock_gl_code["inventory_account"] : 
-                       ($supplier_accounts["purchase_account"] ? $supplier_accounts["purchase_account"] : $stock_gl_code["cogs_account"]));
-               $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $iv_act,
-                       $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'],
-                       $entered_grn->this_quantity_inv * $line_taxfree, $supp_trans->supplier_id);
-               /*$total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $iv_act,
-                       $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 ($trans_type == ST_SUPPINVOICE)
+               
+       if ($trans_type == ST_SUPPCREDIT)
+               {
+                       $iv_act = (is_inventory_item($entered_grn->item_code) ? $stock_gl_code["inventory_account"] : 
+                               ($supplier_accounts["purchase_account"] ? $supplier_accounts["purchase_account"] : $stock_gl_code["cogs_account"]));
+                       $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $iv_act,
+                               $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'],
+                               $entered_grn->this_quantity_inv * $line_taxfree, $supp_trans->supplier_id);
+               }
+       else
        {
+               // -------------- if price changed since po received. 16 Aug 2008 Joe Hunt
+                       $iv_act = is_inventory_item($entered_grn->item_code) ? ($clearing_act ? $clearing_act : $stock_gl_code["inventory_account"]) : 
+                               ($supplier_accounts["purchase_account"] ? $supplier_accounts["purchase_account"] : $stock_gl_code["cogs_account"]);
                $old = update_supp_received_items_for_invoice($entered_grn->id, $entered_grn->po_detail_item,
                        $entered_grn->this_quantity_inv, $entered_grn->chg_price);
                        // Since the standard cost is always calculated on basis of the po unit_price,
@@ -228,8 +233,17 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
                        //{
                        //$diff = $entered_grn->chg_price - $old_price;
                        $old_date = sql2date($old[1]);
-                       if (is_inventory_item($entered_grn->item_code))
+                       if (!is_inventory_item($entered_grn->item_code))
+                               $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $iv_act,
+                                       $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'],
+                                       $entered_grn->this_quantity_inv * $line_taxfree, $supp_trans->supplier_id);
+                       else
                        {
+                               $currency = get_supplier_currency($supp_trans->supplier_id);
+                               $ex_rate = get_exchange_rate_from_home_currency($currency, $old_date);
+                               $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $iv_act,
+                                       $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'],
+                                       $entered_grn->this_quantity_inv * $old_price, $supp_trans->supplier_id, "", $ex_rate);
                                $diff = get_diff_in_home_currency($supp_trans->supplier_id, $old_date, $date_, $old_price, 
                                        $entered_grn->chg_price);       
                        
@@ -243,14 +257,14 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
                                if (($diff * $entered_grn->this_quantity_inv) != 0 )
                                {
                                        $diff_amt = $diff * $entered_grn->this_quantity_inv;
-                                       add_gl_trans($trans_type, $invoice_id, $date_,  $iv_act,
+                                       $total += add_gl_trans($trans_type, $invoice_id, $date_,        $stock_gl_code["inventory_account"],
                                                $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], 'GRN Provision',
                                                $diff_amt, null, null, null,
                                                "The general ledger transaction could not be added for the GRN of the inventory item"); 
                                        
-                                       add_gl_trans($trans_type, $invoice_id, $date_,  $stock_gl_code["cogs_account"],
-                                               0, 0, 'GRN Provision', -$diff_amt, null, null, null,
-                                               "The general ledger transaction could not be added for the GRN of the inventory item");                         
+                                       //add_gl_trans($trans_type, $invoice_id, $date_,        $stock_gl_code["cogs_account"],
+                                       //      0, 0, 'GRN Provision', -$diff_amt, null, null, null,
+                                       //      "The general ledger transaction could not be added for the GRN of the inventory item");                         
                                }
                        }       
                        // added 2008-12-08 Joe Hunt. Update the purchase data table
@@ -566,6 +580,21 @@ function remove_not_invoice_item($id)
 
        add_stock_move(ST_SUPPRECEIVE, $myrow["item_code"], $myrow['grn_batch_id'], $grn['loc_code'], sql2date($grn["delivery_date"]), "",
                -$myrow["QtyOstdg"], $myrow['std_cost_unit'], $grn["supplier_id"], 1, $myrow['unit_price']);
+               
+    $clearing_act = get_company_pref('grn_clearing_act');
+       if ($clearing_act) {    // otherwise GRN clearing account is not used
+               if (is_inventory_item($myrow['item_code']))
+               {
+               $total = 0;
+                       $stock_gl_code = get_stock_gl_code($myrow['item_code']);
+                       $date = sql2date($grn["delivery_date"]);
+                       $total += add_gl_trans_supplier(ST_SUPPRECEIVE, $myrow['grn_batch_id'], $date, $stock_gl_code["inventory_account"],
+                               $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'],
+                               -$myrow['QtyOstdg'] * $myrow['unit_price'], $grn["supplier_id"], "", 0, _("GRN Removal"));
+                       $total += add_gl_trans_supplier(ST_SUPPRECEIVE, $myrow['grn_batch_id'], $date, $clearing_act,
+                               0, 0, -$total, null, "", 0, _("GRN Removal"));
+               }       
+       }
 
        commit_transaction();
 }