<?php
-
+/**********************************************************************
+ Copyright (C) FrontAccounting, LLC.
+ Released under the terms of the GNU General Public License, GPL,
+ as published by the Free Software Foundation, either version 3
+ of the License, or (at your option) any later version.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
+***********************************************************************/
//--------------------------------------------------------------------------------------
-function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $additional_costs, $memo_)
+function add_work_order_quick($wo_ref, $loc_code, $units_reqd, $stock_id, $type, $date_, $memo_, $costs, $cr_acc, $labour, $cr_lab_acc)
{
begin_transaction();
-
+
// if unassembling, reverse the stock movements
if ($type == wo_types::unassemble())
$units_reqd = -$units_reqd;
-
+
+ add_material_cost($stock_id, $units_reqd, $date_);
+
$date = date2sql($date_);
- if (!isset($additional_costs) || ($additional_costs == ""))
- $additional_costs = 0;
-
- $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 ('$wo_ref', '$loc_code', $units_reqd, $units_reqd, '$stock_id',
- $type, $additional_costs, '$date', '$date', '$date', 1, 1)";
+ if (!isset($costs) || ($costs == ""))
+ $costs = 0;
+ if ($costs != 0)
+ add_overhead_cost($stock_id, $units_reqd, $date_, $costs);
+ if (!isset($labour) || ($labour == ""))
+ $labour = 0;
+ if ($labour != 0)
+ add_labour_cost($stock_id, $units_reqd, $date_, $labour);
+
+ $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)";
db_query($sql, "could not add work order");
-
+
$woid = db_insert_id();
-
+
//--------------------------------------------------------------------------
// create Work Order Requirements based on the bom
- $result = get_bom($stock_id);
-
- while ($bom_item = db_fetch($result))
+ $result = get_bom($stock_id);
+
+ while ($bom_item = db_fetch($result))
{
-
+
$unit_quantity = $bom_item["quantity"];
$item_quantity = $bom_item["quantity"] * $units_reqd;
-
-
+
+
$sql = "INSERT INTO ".TB_PREF."wo_requirements (workorder_id, stock_id, workcentre, units_req, units_issued, loc_code)
- VALUES ($woid, " . "'" . $bom_item["component"] . "'" . ",
- ". $bom_item["workcentre_added"] . ",
+ VALUES ($woid, " . "'" . $bom_item["component"] . "'" . ",
+ '". $bom_item["workcentre_added"] . "',
$unit_quantity, $item_quantity, '" . $bom_item["loc_code"] . "')";
-
+
db_query($sql, "The work order requirements could not be added");
-
+
// insert a -ve stock move for each item
- add_stock_move(systypes::work_order(), $bom_item["component"], $woid,
- $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, 0);
+ add_stock_move(systypes::work_order(), $bom_item["component"], $woid,
+ $bom_item["loc_code"], $date_, $wo_ref, -$item_quantity, 0);
}
// -------------------------------------------------------------------------
// insert a +ve stock move for the item being manufactured
- add_stock_move(systypes::work_order(), $stock_id, $woid, $loc_code, $date_,
- $wo_ref, $units_reqd, 0);
+ add_stock_move(systypes::work_order(), $stock_id, $woid, $loc_code, $date_,
+ $wo_ref, $units_reqd, 0);
// -------------------------------------------------------------------------
- work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $additional_costs);
-
+ work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, false, $costs, $cr_acc, $labour, $cr_lab_acc);
+
// -------------------------------------------------------------------------
- add_comments(systypes::work_order(), $woid, $date_, $memo_);
-
- add_forms_for_sys_type(systypes::work_order(), $woid, $type);
-
+ add_comments(systypes::work_order(), $woid, $date_, $memo_);
+
references::save_last($wo_ref, systypes::work_order());
-
- commit_transaction();
+
+ commit_transaction();
return $woid;
}
//--------------------------------------------------------------------------------------
-function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $additional_costs)
+function work_order_quick_costs($woid, $stock_id, $units_reqd, $date_, $advanced=false, $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;
- while ($bom_item = db_fetch($result))
+ while ($bom_item = db_fetch($result))
{
-
+
$bom_accounts = get_stock_gl_code($bom_item["component"]);
-
- $bom_cost = $bom_item["standard_cost"] * $bom_item["quantity"] * $units_reqd;
-
- add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $bom_accounts["inventory_account"], 0, 0,
+
+ $bom_cost = $bom_item["ComponentCost"] * $units_reqd;
+
+ if ($advanced)
+ {
+ // insert a -ve stock move for each item
+ add_stock_move(systypes::work_order(), $bom_item["component"], $woid,
+ $bom_item["loc_code"], $date_, "", -$bom_item["quantity"] * $units_reqd, 0);
+ }
+ $total_cost += add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $bom_accounts["inventory_account"], 0, 0,
null, -$bom_cost);
-
- $total_cost += $bom_cost;
- }
+
+ }
+ if ($advanced)
+ {
+ // also take the additional issues
+ $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(systypes::work_order(), $woid, $date_, $issue["inventory_account"], 0, 0,
+ null, -$issue_cost);
+ $issue_total += $issue_cost;
+ }
+ 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'];
+ if ($lcost != 0)
+ 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'];
+ if ($ocost != 0)
+ 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);
- add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["assembly_account"],
- $item_accounts["dimension_id"], $item_accounts["dimension2_id"], null, -$additional_costs);
-
- // debit total components + additional
- $total_cost += $additional_costs;
- add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["inventory_account"],
- 0, 0, null, $total_cost);
-}
+ if ($costs != 0.0)
+ {
+ add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $cr_acc,
+ 0, 0, $wo_cost_types[WO_OVERHEAD], -$costs, payment_person_types::WorkOrder(), WO_OVERHEAD);
+ $is_bank_to = is_bank_account($cr_acc);
+ if ($is_bank_to)
+ {
+ add_bank_trans(systypes::work_order(), $woid, $is_bank_to, "",
+ $date_, -$costs, payment_person_types::WorkOrder(), WO_OVERHEAD, get_company_currency(),
+ "Cannot insert a destination bank transaction");
+ }
+
+ add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["assembly_account"],
+ $item_accounts["dimension_id"], $item_accounts["dimension2_id"], $wo_cost_types[WO_OVERHEAD], $costs,
+ payment_person_types::WorkOrder(), WO_OVERHEAD);
+ }
+ if ($labour != 0.0)
+ {
+ add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $cr_lab_acc,
+ 0, 0, $wo_cost_types[WO_LABOUR], -$labour, payment_person_types::WorkOrder(), WO_LABOUR);
+ $is_bank_to = is_bank_account($cr_lab_acc);
+ if ($is_bank_to)
+ {
+ add_bank_trans(systypes::work_order(), $woid, $is_bank_to, "",
+ $date_, -$labour, payment_person_types::WorkOrder(), WO_LABOUR, get_company_currency(),
+ "Cannot insert a destination bank transaction");
+ }
+
+ add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["assembly_account"],
+ $item_accounts["dimension_id"], $item_accounts["dimension2_id"], $wo_cost_types[WO_LABOUR], $labour,
+ payment_person_types::WorkOrder(), WO_LABOUR);
+ }
+ // debit total components $total_cost
+ add_gl_trans_std_cost(systypes::work_order(), $woid, $date_, $item_accounts["inventory_account"],
+ 0, 0, null, -$total_cost);
+}
//--------------------------------------------------------------------------------------