Moving bugfixes from 2.3 to 2.4 and updated install languages for Denmark and Sweden.
[fa-stable.git] / manufacturing / includes / db / work_orders_quick_db.inc
index 4aa2a176a09d659af77efe37c26890c43c9dfbc2..e07ec8f35ad82ce3d61391547c4a64cd5d2efe68 100644 (file)
@@ -68,7 +68,7 @@ function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type,
                // insert a -ve stock move for each 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, $UnitCost);
+                       $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, $UnitCost, 0, 1, $UnitCost);
        }
 
        // -------------------------------------------------------------------------
@@ -81,7 +81,6 @@ function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type,
                        $cost_adjust = true;
        }
 
-
        //Negative Stock Handling First; Prior to add_stock_move
        if ($cost_adjust)
                adjust_deliveries($stock_id, get_standard_cost($stock_id), $date_);
@@ -113,10 +112,12 @@ function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type,
 
 function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced=0, $costs=0, $cr_acc="", $labour=0, $cr_lab_acc="")
 {
+       global $wo_cost_types;
        $result = get_bom($stock_id);
 
        // credit all the components
        $total_cost = 0;
+       $total_material_cost = 0;
        while ($bom_item = db_fetch($result))
        {
 
@@ -130,35 +131,59 @@ 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
+                       $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, 0);
+                               $bom_item["loc_code"], $date_, "", -$bom_item["quantity"] * $units_reqd, $UnitCost, 0, 1, $UnitCost);
                }
-               $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $bom_accounts["inventory_account"], 0, 0,
+               //Compatibility for Service Items
+               if (!is_service($bom_accounts["mb_flag"]))
+                       $ivaccount = $bom_accounts["inventory_account"];
+               else
+                       $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;
        }
-       $item_accounts = get_stock_gl_code($stock_id);
        if ($advanced)
        {
-               $wo = get_work_order($woid);
-               $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']);
-
-       } else { // only for quick
-               // credit additional costs
-
-               if ($costs != 0.0)
-               {
-                       add_wo_costs_journal($woid, $costs, WO_OVERHEAD, $cr_acc, $item_accounts["assembly_account"],
-                               $date_, $item_accounts["dimension_id"], $item_accounts["dimension2_id"]);
-               }
-               if ($labour != 0.0) // only for quick
-               {
-                       add_wo_costs_journal($woid, $labour, WO_LABOUR, $cr_lab_acc, $item_accounts["assembly_account"],
-                               $date_, $item_accounts["dimension_id"], $item_accounts["dimension2_id"]);
-               }
+               //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, $woid); 
+               // $wo = get_work_order($woid);
+               // also take the additional issues
+               // moved to work_order_issues_db.inc
+       }       // credit additional costs
+       $item_accounts = get_stock_gl_code($stock_id);
+       if ($costs != 0.0)
+       {
+               add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $cr_acc,
+                       0, 0, $wo_cost_types[WO_OVERHEAD], -$costs, PT_WORKORDER, WO_OVERHEAD);
+               $is_bank_to = is_bank_account($cr_acc);
+       if ($is_bank_to)
+       {
+               add_bank_trans(ST_WORKORDER, $woid, $is_bank_to, "",
+                       $date_, -$costs, PT_WORKORDER, WO_OVERHEAD, get_company_currency(),
+                       "Cannot insert a destination bank transaction");
+       }
+                       
+               add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["assembly_account"],
+                       $item_accounts["dimension_id"], $item_accounts["dimension2_id"], $wo_cost_types[WO_OVERHEAD], $costs, 
+                       PT_WORKORDER, WO_OVERHEAD);
+       }
+       if ($labour != 0.0) // only for quick
+       {
+               add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $cr_lab_acc,
+                       0, 0, $wo_cost_types[WO_LABOUR], -$labour, PT_WORKORDER, WO_LABOUR);
+               $is_bank_to = is_bank_account($cr_lab_acc);
+       if ($is_bank_to)
+       {
+               add_bank_trans(ST_WORKORDER, $woid, $is_bank_to, "",
+                       $date_, -$labour, PT_WORKORDER, WO_LABOUR, get_company_currency(),
+                       "Cannot insert a destination bank transaction");
+       }
+                       
+               add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["assembly_account"],
+                       $item_accounts["dimension_id"], $item_accounts["dimension2_id"], $wo_cost_types[WO_LABOUR], $labour, 
+                       PT_WORKORDER, WO_LABOUR);
        }
        // debit total components $total_cost
        $stockitem = get_item($stock_id);
@@ -167,5 +192,10 @@ 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);
+       if (!$advanced)
+       {
+               $UnitWOCost = ($total_material_cost + $costs + $labour)/$units_reqd;
+               update_stock_move(ST_WORKORDER, $woid, $stock_id, $UnitWOCost); 
+       }       
 }