0003862: Straight Line Depreciation value calculation is wrong
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Mon, 15 May 2017 06:25:39 +0000 (08:25 +0200)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Mon, 15 May 2017 06:25:39 +0000 (08:25 +0200)
0003863: Fixed Assets Valuation Report Includes inactive Items with Valuation
0003864: [Fixed Assets] GL Transactions for Fixed Asset Sales are wrong (patch provided)

fixed_assets/includes/depreciation.inc
includes/db/inventory_db.inc
sales/includes/db/sales_delivery_db.inc

index e07a93de057b860602a9cb0e5d58dfec713a6dc3..b63ad1a8eb527a5869db59a96d1d3864eefa9472 100644 (file)
@@ -71,7 +71,7 @@ function compute_gl_rows_for_depreciation($item, $no_months, $period) {
 
     case 'S':  // purchase_cost stores start cost of item
                $done_months = months_between_dates($item['depreciation_start'], $item['depreciation_date']);
-               $remaining_months = 100.0/$item['depreciation_rate'] - $done_months;
+               $remaining_months = 12.0 * 100.0/$item['depreciation_rate'] - $done_months;
                $value = $item['purchase_cost']*$item['depreciation_rate']/100/12;
                break;
 
index 55e3b687bc965090ac3f56c0cbe293e8ae0e4b24..52c016746e570cc741e1c645e1f71fd73a22a7e7 100644 (file)
@@ -110,6 +110,20 @@ function get_unit_cost($stock_id)
        return $myrow[0];
 }
 
+//--------------------------------------------------------------------------------------
+function get_purchase_cost($stock_id)
+{
+       $sql = "SELECT purchase_cost
+               FROM ".TB_PREF."stock_master
+               WHERE stock_id=".db_escape($stock_id);
+       $result = db_query($sql, "The purchase cost cannot be retrieved");
+
+       $myrow = db_fetch_row($result);
+
+       return $myrow[0];
+}
+
 //--------------------------------------------------------------------------------------
 
 function is_inventory_item($stock_id)
index 3f8ffc87dad5e50342e4e3185c11fb762e19f615..273de917756c42006b4f25c4f47be154f3b3084e 100644 (file)
@@ -101,7 +101,7 @@ function write_sales_delivery(&$delivery,$bo_policy)
                        $mb_flag = get_mb_flag($delivery_line->stock_id);
 
                        if (is_fixed_asset($mb_flag)) {
-                               $sql = "UPDATE ".TB_PREF."stock_master SET inactive=1
+                               $sql = "UPDATE ".TB_PREF."stock_master SET inactive=1, material_cost=0
                                        WHERE stock_id=".db_escape($delivery_line->stock_id);
                                db_query($sql,"The cost details for the fixed asset could not be updated");
                        }
@@ -123,27 +123,47 @@ function write_sales_delivery(&$delivery,$bo_policy)
                                        $delivery->customer_id, "The sales price GL posting could not be inserted");
                        }
                        /* insert gl_trans to credit stock and debit cost of sales at standard cost*/
-                       if (is_inventory_item($delivery_line->stock_id) && $delivery_line->standard_cost != 0) {
-
-                               /*first the cost of sales entry*/
-
-                               $delivery_gl_code = $stock_gl_code["cogs_account"];
-                               if ($delivery->fixed_asset)
-                                       $delivery_gl_code = $stock_gl_code["adjustment_account"];
-
-                               add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no,
-                                       $delivery->document_date, $delivery_gl_code, $dim, $dim2, "",
-                                       $delivery_line->standard_cost * $delivery_line->qty_dispatched,
-                                       PT_CUSTOMER, $delivery->customer_id,
-                                       "The cost of sales GL posting could not be inserted");
-
-                               /*now the stock entry*/
-
-                               add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no, $delivery->document_date,
-                                       $stock_gl_code["inventory_account"], 0, 0, "",
-                                       (-$delivery_line->standard_cost * $delivery_line->qty_dispatched),
-                                       PT_CUSTOMER, $delivery->customer_id,
-                                       "The stock side of the cost of sales GL posting could not be inserted");
+                       
+                       if (is_inventory_item($delivery_line->stock_id)) {
+                               // Fixed Assets
+                               if ($delivery->fixed_asset) {
+                                   $fa_purchase_cost = get_purchase_cost($delivery_line->stock_id);
+                                       $fa_depreciation = $fa_purchase_cost - $delivery_line->standard_cost;
+                                       /*first remove depreciation*/
+                                       add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no,
+                                               $delivery->document_date, $stock_gl_code["adjustment_account"], $dim, $dim2, "",
+                                               $fa_depreciation,
+                                               PT_CUSTOMER, $delivery->customer_id,
+                                               "The cost of sales GL posting could not be inserted");
+                                       /*then remove asset*/
+                                       add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no, $delivery->document_date,
+                                               $stock_gl_code["inventory_account"], 0, 0, "",
+                                               -$fa_purchase_cost,
+                                               PT_CUSTOMER, $delivery->customer_id,
+                                               "The stock side of the cost of sales GL posting could not be inserted");
+                                       /*finally adjust sales account with the remaining*/
+                                       add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no, $delivery->document_date,
+                                               $stock_gl_code["sales_account"], 0, 0, "",
+                                               ($fa_purchase_cost - $fa_depreciation),
+                                               PT_CUSTOMER, $delivery->customer_id,
+                                               "The stock side of the cost of sales GL posting could not be inserted");
+                               }
+                               // Invetory Items
+                               else if ($delivery_line->standard_cost != 0) {
+                                       /*first the cost of sales entry*/
+                                       add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no,
+                                               $delivery->document_date, $stock_gl_code["cogs_account"], $dim, $dim2, "",
+                                               $delivery_line->standard_cost * $delivery_line->qty_dispatched,
+                                               PT_CUSTOMER, $delivery->customer_id,
+                                               "The cost of sales GL posting could not be inserted");
+
+                                       /*now the stock entry*/
+                                       add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no, $delivery->document_date,
+                                               $stock_gl_code["inventory_account"], 0, 0, "",
+                                               (-$delivery_line->standard_cost * $delivery_line->qty_dispatched),
+                                               PT_CUSTOMER, $delivery->customer_id,
+                                               "The stock side of the cost of sales GL posting could not be inserted");
+                               }                               
 
                        } /* end of if GL and stock integrated and standard cost !=0 */