2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 //--------------------------------------------------------------------------------------
14 function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $additional_costs, $memo_)
18 // if unassembling, reverse the stock movements
19 if ($type == wo_types::unassemble())
20 $units_reqd = -$units_reqd;
22 add_material_cost($stock_id, $units_reqd, $date_);
24 $date = date2sql($date_);
25 if (!isset($additional_costs) || ($additional_costs == ""))
26 $additional_costs = 0;
27 if ($additional_costs != 0)
28 add_additional_cost($stock_id, $units_reqd, $date_, $additional_costs);
30 $sql = "INSERT INTO ".TB_PREF."workorders (wo_ref, loc_code, units_reqd, units_issued, stock_id,
31 type, additional_costs, date_, released_date, required_by, released, closed)
32 VALUES (".db_escape($wo_ref).", ".db_escape($loc_code).", $units_reqd, $units_reqd, '$stock_id',
33 $type, $additional_costs, '$date', '$date', '$date', 1, 1)";
34 db_query($sql, "could not add work order");
36 $woid = db_insert_id();
38 //--------------------------------------------------------------------------
40 // create Work Order Requirements based on the bom
41 $result = get_bom($stock_id);
43 while ($bom_item = db_fetch($result))
46 $unit_quantity = $bom_item["quantity"];
47 $item_quantity = $bom_item["quantity"] * $units_reqd;
50 $sql = "INSERT INTO ".TB_PREF."wo_requirements (workorder_id, stock_id, workcentre, units_req, units_issued, loc_code)
51 VALUES ($woid, " . "'" . $bom_item["component"] . "'" . ",
52 '". $bom_item["workcentre_added"] . "',
53 $unit_quantity, $item_quantity, '" . $bom_item["loc_code"] . "')";
55 db_query($sql, "The work order requirements could not be added");
57 // insert a -ve stock move for each item
58 add_stock_move(systypes::work_order(), $bom_item["component"], $woid,
59 $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, 0);
63 // -------------------------------------------------------------------------
65 // insert a +ve stock move for the item being manufactured
66 add_stock_move(systypes::work_order(), $stock_id, $woid, $loc_code, $date_,
67 $wo_ref, $units_reqd, 0);
69 // -------------------------------------------------------------------------
71 work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $additional_costs);
73 // -------------------------------------------------------------------------
75 add_comments(systypes::work_order(), $woid, $date_, $memo_);
77 references::save_last($wo_ref, systypes::work_order());
83 //--------------------------------------------------------------------------------------
85 function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $additional_costs, $advanced=false)
87 $result = get_bom($stock_id);
89 // credit all the components
91 while ($bom_item = db_fetch($result))
94 $bom_accounts = get_stock_gl_code($bom_item["component"]);
96 $bom_cost = $bom_item["standard_cost"] * $bom_item["quantity"] * $units_reqd;
100 // insert a -ve stock move for each item
101 add_stock_move(systypes::work_order(), $bom_item["component"], $woid,
102 $bom_item["loc_code"], $date_, "", -$bom_item["quantity"] * $units_reqd, 0);
104 add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $bom_accounts["inventory_account"], 0, 0,
107 $total_cost += $bom_cost;
111 // also take the additional issues
112 $res = get_additional_issues($woid);
114 while ($item = db_fetch($res))
116 $standard_cost = get_standard_cost($item['stock_id']);
117 $issue_cost = $standard_cost * $item['qty_issued'];
118 $issue = get_stock_gl_code($item['stock_id']);
119 add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $issue["inventory_account"], 0, 0,
121 $issue_total += $issue_cost;
123 add_issue_cost($stock_id, $units_reqd, $date_, $issue_total);
124 $total_cost += $issue_total;
125 $pcost = 0; // fix bank payments as additional cost.
126 $result = get_bank_trans(null, null, payment_person_types::WorkOrder(), $woid);
127 while ($row = db_fetch($result))
128 $pcost += -$row['amount'];
130 add_additional_cost($stock_id, $units_reqd, $date_, $pcost);
132 // credit additional costs
133 $item_accounts = get_stock_gl_code($stock_id);
134 add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["assembly_account"],
135 $item_accounts["dimension_id"], $item_accounts["dimension2_id"], null, -$additional_costs);
137 // debit total components + additional
138 $total_cost += $additional_costs;
139 add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["inventory_account"],
140 0, 0, null, $total_cost);
143 //--------------------------------------------------------------------------------------