Fixed correct calculation of stock_moves standard_cost when producing an Advanced...
[fa-stable.git] / manufacturing / includes / db / work_orders_quick_db.inc
index 31fbadb87202f5b835c0c1b12eb5c02113d50b5d..72b5c8df7e0d873a5b2ed3371df46105f21f66ea 100644 (file)
@@ -66,23 +66,29 @@ function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type,
         db_query($sql, "The work order requirements could not be added");
 
                // insert a -ve stock move for each item
-               // Chaitanya: Updated to record price and cost as std_cost for work order issued item\r
-               $UnitCost = get_standard_cost($bom_item["component"]);\r
+               // Chaitanya: Updated to record price and cost as std_cost for work order issued item
+               $UnitCost = get_standard_cost($bom_item["component"]);
                add_stock_move(ST_WORKORDER, $bom_item["component"], $woid,
-                       $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, $UnitCost, 0, 1, $UnitCost);\r
+                       $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, $UnitCost, 0, 1, $UnitCost);
+       }
+       
+       // -------------------------------------------------------------------------
+       //Negative Stock Handling
+       $qoh = get_qoh_on_date($stock_id);
+       $cost_adjust = false;
+       if ($qoh < 0)
+       {
+               if ($qoh + $units_reqd >= 0)
+                       $cost_adjust = true;
        }
-       \r
-       // -------------------------------------------------------------------------\r
-       //Negative Stock Handling\r
-       $qoh = get_qoh_on_date($stock_id);\r
-       $cost_adjust = false;\r
-       if ($qoh < 0)\r
-       {\r
-               if ($qoh + $units_reqd > 0)\r
-                       $cost_adjust = true;\r
-       }\r
 
        // -------------------------------------------------------------------------
+       
+       //Negative Stock Handling First; Prior to add_stock_move
+       if ($cost_adjust)
+               adjust_deliveries($stock_id, get_standard_cost($stock_id), $date_);
+               
+       // -------------------------------------------------------------------------    
 
        // insert a +ve stock move for the item being manufactured
        add_stock_move(ST_WORKORDER, $stock_id, $woid,  $loc_code, $date_,
@@ -91,14 +97,8 @@ function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type,
        // -------------------------------------------------------------------------
 
        work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, 0, $costs, $cr_acc, $labour, $cr_lab_acc);
-
-       // -------------------------------------------------------------------------
-       \r
-       //Negative Stock Handling\r
-       if ($cost_adjust)\r
-               adjust_deliveries($stock_id, get_standard_cost($stock_id), $date_);\r
-               \r
-       //--------------------------------------------------------------------------\r
+               
+       //--------------------------------------------------------------------------
 
        add_comments(ST_WORKORDER, $woid, $date_, $memo_);
 
@@ -120,8 +120,8 @@ function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced
 
        // credit all the components
        $total_cost = 0;
-       //Chaitanya : capture the exact i.e. non-rounded cost to avoid rounding error amplification\r
-       $total_material_cost = 0;\r
+       //Chaitanya : capture the exact i.e. non-rounded cost to avoid rounding error amplification
+       $total_material_cost = 0;
        while ($bom_item = db_fetch($result))
        {
                $bom_accounts = get_stock_gl_code($bom_item["component"]);
@@ -134,27 +134,28 @@ function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced
                        $memo = $date_.": ".$memo; 
                        update_wo_requirement_issued($woid, $bom_item['component'], $bom_item["quantity"] * $units_reqd);                       
                        // insert a -ve stock move for each item
-                       // Chaitanya: Updated to record price and cost as std_cost for work order issued item\r
-                       $UnitCost = get_standard_cost($bom_item["component"]);                  \r
+                       // Chaitanya: Updated to record price and cost as std_cost for work order issued item
+                       $UnitCost = get_standard_cost($bom_item["component"]);                  
                        add_stock_move(ST_MANURECEIVE, $bom_item["component"], $advanced,
-                               $bom_item["loc_code"], $date_, "", -$bom_item["quantity"] * $units_reqd, $UnitCost, 0, 1, $UnitCost);\r
+                               $bom_item["loc_code"], $date_, "", -$bom_item["quantity"] * $units_reqd, $UnitCost, 0, 1, $UnitCost);
                }
-               \r
-               //Chaitanya : Compatibility for Service Items\r
-               if (!is_service($bom_accounts["mb_flag"]))\r
-                       $ivaccount = $bom_accounts["inventory_account"];\r
-               else\r
-                       $ivaccount = $bom_accounts["cogs_account"];\r
-               $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,\r
+               
+               //Chaitanya : Compatibility for Service Items
+               if (!is_service($bom_accounts["mb_flag"]))
+                       $ivaccount = $bom_accounts["inventory_account"];
+               else
+                       // $ivaccount = $bom_accounts["cogs_account"];
+                       $ivaccount = $bom_accounts["assembly_account"]; // changed 2015.10.14 by Petros .               
+               $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,
                        $memo, -$bom_cost);
-               $total_material_cost += $bom_cost;\r
+               $total_material_cost += $bom_cost;
        }
        if ($advanced)
        {
-               //Chaitanya: Material cost added at time of production as per BOM at the time and in adjustment mode.\r
-               add_material_cost($stock_id, $units_reqd, $date_, true);        \r
-       \r
-               $wo = get_work_order($woid);
+               //Chaitanya: Material cost added at time of production as per BOM at the time and in adjustment mode.
+               add_material_cost($stock_id, $units_reqd, $date_, true);        
+       
+               // $wo = get_work_order($woid);
                // also take the additional issues
                // moved to work_order_issues_db.inc
                /*
@@ -173,13 +174,13 @@ function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced
                if ($issue_total != 0)
                        add_issue_cost($stock_id, $units_reqd, $date_, $issue_total);
                */      
-               \r
-               // Logic moved to work_order_costs.php\r
-               /*\r
+               
+               // Logic moved to work_order_costs.php
+               /*
                $lcost = get_gl_wo_cost($woid, WO_LABOUR);
                add_labour_cost($stock_id, $units_reqd, $date_, $lcost * $units_reqd / $wo['units_reqd']);
                $ocost = get_gl_wo_cost($woid, WO_OVERHEAD);
-               add_overhead_cost($stock_id, $units_reqd, $date_, $ocost * $units_reqd / $wo['units_reqd']); */\r
+               add_overhead_cost($stock_id, $units_reqd, $date_, $ocost * $units_reqd / $wo['units_reqd']); */
        }
        // credit additional costs
        $item_accounts = get_stock_gl_code($stock_id);
@@ -222,10 +223,13 @@ function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced
                $memo = $date_.": ".$memo;
     add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["inventory_account"],
         0, 0, $memo, -$total_cost);    
-               \r
-       //Chaitanya : Update cost of the manufactured item in stock moves table\r
-       $UnitWOCost = ($total_material_cost + $costs + $labour)/$units_reqd;\r
-       update_stock_move(ST_WORKORDER, $woid, $stock_id, $UnitWOCost);         \r
+               
+       //Chaitanya : Update cost of the manufactured item in stock moves table
+       if (!$advanced)
+       {
+               $UnitWOCost = ($total_material_cost + $costs + $labour)/$units_reqd;
+               update_stock_move(ST_WORKORDER, $woid, $stock_id, $UnitWOCost); 
+       }       
 }
 
 //--------------------------------------------------------------------------------------