Stable merged into unstable again (due to failure on binary file during previous...
[fa-stable.git] / manufacturing / includes / db / work_orders_quick_db.inc
index 455f918e0b43b68e4a5cf597ce5ff55694f15faf..bcf9688f0ef015e1d5270a564042ed5b91c95664 100644 (file)
 ***********************************************************************/
 //--------------------------------------------------------------------------------------
 
-function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc)
+function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, 
+       $date_, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc)
 {
        global $Refs;
 
        begin_transaction();
+       $args = func_get_args();
+       $args = (object)array_combine(array('wo_ref', 'loc_code', 'units_reqd', 'stock_id',
+               'type', 'date_', 'memo_', 'costs', 'cr_acc', 'labour', 'cr_lab_acc'), $args);
+       $args->woid = 0;
+       hook_db_prewrite($args, ST_WORKORDER);
 
        // if unassembling, reverse the stock movements
        if ($type == WO_UNASSEMBLY)
@@ -33,8 +39,9 @@ function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type,
                
        $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, units_issued, stock_id,
                type, additional_costs, date_, released_date, required_by, released, closed)
-       VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", $units_reqd, $units_reqd, '$stock_id',
-               $type, $costs, '$date', '$date', '$date', 1, 1)";
+       VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", ".db_escape($units_reqd)
+       .", ".db_escape($units_reqd).", ".db_escape($stock_id).",
+               ".db_escape($type).", ".db_escape($costs).", '$date', '$date', '$date', 1, 1)";
        db_query($sql, "could not add work order");
 
        $woid = db_insert_id();
@@ -72,7 +79,7 @@ function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type,
 
        // -------------------------------------------------------------------------
 
-       work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, false, $costs, $cr_acc, $labour, $cr_lab_acc);
+       work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, 0, $costs, $cr_acc, $labour, $cr_lab_acc);
 
        // -------------------------------------------------------------------------
 
@@ -80,13 +87,16 @@ function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type,
 
        $Refs->save(ST_WORKORDER, $woid, $wo_ref);
        add_audit_trail(ST_WORKORDER, $woid, $date_,_("Quick production."));
+
+       $args->woid = $woid;
+       hook_db_postwrite($args, ST_WORKORDER);
        commit_transaction();
        return $woid;
 }
 
 //--------------------------------------------------------------------------------------
 
-function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced=false, $costs=0, $cr_acc="", $labour=0, $cr_lab_acc="")
+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);
@@ -99,82 +109,68 @@ function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced
                $bom_accounts = get_stock_gl_code($bom_item["component"]);
 
                $bom_cost = $bom_item["ComponentCost"] * $units_reqd;
-
+               
+               $memo = $bom_item["quantity"] ." * ".$bom_item["description"];
                if ($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
-                       add_stock_move(ST_WORKORDER, $bom_item["component"], $woid,
+                       add_stock_move(ST_MANURECEIVE, $bom_item["component"], $advanced,
                                $bom_item["loc_code"], $date_, "", -$bom_item["quantity"] * $units_reqd, 0);
                }
                $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $bom_accounts["inventory_account"], 0, 0,
-                       null, -$bom_cost);
+                       $memo, -$bom_cost);
 
        }
+       $item_accounts = get_stock_gl_code($stock_id);
        if ($advanced)
        {
+               $wo = get_work_order($woid);
                // also take the additional issues
+               // moved to work_order_issues_db.inc
+               /*
                $res = get_additional_issues($woid);
-               $wo = get_work_order($woid);
                $issue_total = 0;
                while ($item = db_fetch($res))
                {
                        $standard_cost = get_standard_cost($item['stock_id']);
                        $issue_cost = $standard_cost * $item['qty_issued'] * $units_reqd / $wo['units_reqd'];
                        $issue = get_stock_gl_code($item['stock_id']);
-                       $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $issue["inventory_account"], 0, 0,
-                               null, -$issue_cost);
+            $stockitem = get_item($item['stock_id']);
+            $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $issue["inventory_account"], 0, 0,
+                $date_.": "._("Issue of")." ".$stockitem["description"], -$issue_cost);                        
                        $issue_total += $issue_cost;
                }
                if ($issue_total != 0)
                        add_issue_cost($stock_id, $units_reqd, $date_, $issue_total);
-               $result = get_gl_wo_cost_trans($woid, WO_LABOUR);
-               $lcost = 0;
-               while ($row = db_fetch($result))
-                       $lcost += -$row['amount'];
+               */      
+               $lcost = get_gl_wo_cost($woid, WO_LABOUR);
                add_labour_cost($stock_id, $units_reqd, $date_, $lcost * $units_reqd / $wo['units_reqd']);
-               $result = get_gl_wo_cost_trans($woid, WO_OVERHEAD);
-               $ocost = 0;
-               while ($row = db_fetch($result))
-                       $ocost += -$row['amount'];
+               $ocost = get_gl_wo_cost($woid, WO_OVERHEAD);
                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);
-       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)
-       {
-               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);
+
+       } 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"]);
+               }
        }
        // debit total components $total_cost
-       add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["inventory_account"],
-               0, 0, null, -$total_cost);
+       $stockitem = get_item($stock_id);
+       $memo = _("Produced")." ".$units_reqd. " * ".$stockitem["description"];
+       if ($advanced)
+               $memo = $date_.": ".$memo;
+    add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $item_accounts["inventory_account"],
+        0, 0, $memo, -$total_cost);
 }
 
 //--------------------------------------------------------------------------------------