case ST_MANUISSUE : // it's a work order issue
if (!exists_work_order_issue($type_no))
return _('Selected transaction does not exists.');
+ if ($wo = check_void_wo_issue($type_no))
+ return sprintf(_('Selected document cannot be voided because related Work Order %s is already closed.'), $wo);
void_work_order_issue($type_no);
break;
case ST_MANURECEIVE : // it's a work order production
if (!exists_work_order_produce($type_no))
return _('Selected transaction does not exists.');
+ if ($wo = check_void_wo_production($type_no))
+ return sprintf(_('Selected document cannot be voided because related Work Order %s is already closed.'), $wo);
void_work_order_produce($type_no);
break;
if (!is_date_in_fiscalyear($cart->tran_date))
$cart->tran_date = end_fiscalyear();
- $cart->reference = $Refs->get_next(ST_COSTUPDATE, null, $cart->tran_date, $date);
+ $cart->reference = $Refs->get_next(ST_COSTUPDATE, null, $cart->tran_date);
$cart->memo_ = sprintf(_("COGS changed from %s to %s for %d %s of '%s'"), $avg_cost, $unit_cost, $qty_delayed, $item['units'], $stock_id);
db_query($sql,"The cost details for the inventory item could not be updated");
}
-function add_issue_cost($stock_id, $qty, $date_, $costs, $adj_only=false)
-{
- if ($qty != 0)
- $costs /= $qty;
- $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id = "
- .db_escape($stock_id);
- $result = db_query($sql);
- $myrow = db_fetch($result);
- $material_cost = $myrow['material_cost'];
- $qoh = get_qoh_on_date($stock_id);
- if ($qoh < 0)
- $qoh = 0;
- if ($adj_only)
- {
- if ($qoh>0)
- $material_cost = ($qoh * $material_cost + $costs) / $qoh;
- else // Journal Entry if QOH is 0/negative
- {
- global $Refs;
-
- $id = get_next_trans_no(ST_JOURNAL);
- $ref = $Refs->get_next(ST_JOURNAL, null, $date_);
- add_journal(ST_JOURNAL, $id, $costs, $date_, get_company_currency(), $ref);
-
- $stock_gl_code = get_stock_gl_code($stock_id);
- $memo = "WO Issue settlement JV for zero/negative respository of ".$stock_id;
- //Reverse the inventory effect if $qoh <=0
- add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
- $stock_gl_code["inventory_account"],
- $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
- -$costs);
- //GL Posting to inventory adjustment account
- add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
- $stock_gl_code["wip_account"],
- $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
- $costs);
-
- add_audit_trail(ST_JOURNAL, $id, $date_);
- add_comments(ST_JOURNAL, $id, $date_, $memo);
- $Refs->save(ST_JOURNAL, $id, $ref);
- if ($qty != 0)
- $material_cost = $costs / $qty;
- }
- }
- else
- {
- if ($qoh + $qty != 0)
- $material_cost = ($qoh * $material_cost + $qty * $costs) / ($qoh + $qty);
- }
- $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=material_cost+"
- .db_escape($material_cost)
- ." WHERE stock_id=".db_escape($stock_id);
- db_query($sql,"The cost details for the inventory item could not be updated");
-}
-
/*
Create journal entry for WO related costs.
*/
foreach ($items as $item)
{
- if ($to_work_order)
+ if ($to_work_order) // otherwise it is material return to inventory
$item->quantity = -$item->quantity;
$unit_cost = get_unit_cost($item->stock_id);
$total_cost += add_gl_trans_std_cost(ST_MANUISSUE, $number, $date_, $ivaccount, 0, 0,
$date_.": "._("Issue of")." ".$stockitem["description"], -$issue_cost);
- $issue_total += $issue_cost;
}
- if ($issue_total != 0) // Apply cost to QOH as adjustment only
- add_issue_cost($wo['stock_id'], $wo['units_reqd'], $date_, $issue_total, true);
-
$stockitem = get_item($wo['stock_id']);
$wip_account = $stockitem["wip_account"];
function get_work_order_issue($issue_no)
{
- $sql = "SELECT DISTINCT issue.*, wo.stock_id,
+ $sql = "SELECT DISTINCT issue.*, wo.stock_id, wo.closed,
item.description, loc.location_name, center.name AS WorkCentreName
FROM ".TB_PREF."wo_issues issue,"
.TB_PREF."workorders wo,"
//--------------------------------------------------------------------------------------
+function check_void_wo_issue($issue_no)
+{
+ $issue = get_work_order_issue($issue_no);
+ return $issue['closed'] ? $issue['workorder_id'] : 0;
+}
+
+//--------------------------------------------------------------------------------------
+
function void_work_order_issue($type_no)
{
begin_transaction();
hook_db_prevoid(ST_MANUISSUE, $type_no);
- //Chaitanya : Skip processing already voided entry i.e. explicitly voided
$void_entry = get_voided_entry(ST_MANUISSUE, $type_no);
if ($void_entry)
return;
+ $issue = get_work_order_issue($type_no);
+
+ if (work_order_is_closed($issue['workorder_id']))
+ return;
+
+ // FIXME update issued material cost
+
// void the actual issue items and their quantities
- $sql = "UPDATE ".TB_PREF."wo_issue_items Set qty_issued = 0 WHERE issue_id="
- .db_escape($type_no);
+ $sql = "UPDATE ".TB_PREF."wo_issue_items SET qty_issued = 0
+ WHERE issue_id=".db_escape($type_no);
+
db_query($sql,"A work order issue item could not be voided");
- // Reverse the gl posting
- $issue = get_work_order_issue($type_no);
- $manf_stock_id = $issue["stock_id"];
- $date_ = sql2date($issue["issue_date"]);
- $woid = $issue["workorder_id"];
-
- $result = get_stock_moves(ST_MANUISSUE, $type_no);
- $total_cost = 0;
- $issue_total = 0;
- while ($myrow = db_fetch($result))
- {
- $issue_cost = $myrow["qty"]*$myrow["standard_cost"];
- $issue = get_stock_gl_code($myrow["stock_id"]);
- $stockitem = get_item($myrow["stock_id"]);
+ void_gl_trans(ST_MANUISSUE, $type_no);
- // Compatibility for Service Items
- if (!is_service($issue["mb_flag"]))
- $ivaccount = $issue["inventory_account"];
- else
- $ivaccount = $issue["cogs_account"];
-
- if ($issue_cost != 0)
- {
- $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,
- $date_.": "._("Reversed the issue of")." ".$stockitem["description"],
- -$issue_cost);
- $issue_total += $issue_cost;
- }
- }
- if ($issue_total != 0)
- // Revese cost effect on manfactured stock item as adjustment only
- add_issue_cost($manf_stock_id, 0, $date_, $issue_total, true);
- $issue = get_stock_gl_code($manf_stock_id);
- $stockitem = get_item($manf_stock_id);
- if ($total_cost != 0)
- add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $issue["inventory_account"],
- 0, 0, $date_.": "._("Reversed the issue to")." ".$stockitem["description"],
- -$total_cost);
-
- // Shifted below void all related stock moves
void_stock_move(ST_MANUISSUE, $type_no);
commit_transaction();
}
-
-
-//--------------------------------------------------------------------------------------
-
work_order_production_gl($woid, $product["stock_id"], $quantity, $date_, $id);
// update wo quantity and close wo if requested (or finished)
- $closed = work_order_update_finished_quantity($woid, $quantity, $date_, $close_wo);
+ $closed = work_order_update_finished_quantity($woid, $quantity, $close_wo);
// unit_cost is known when WO is finished, then generate +ve for all items
if ($closed)
function get_work_order_produce($id)
{
- $sql = "SELECT prod.*, wo.stock_id, item.description AS StockDescription
+ $sql = "SELECT prod.*, wo.stock_id, item.description AS StockDescription, wo.closed
FROM ".TB_PREF."wo_manufacture prod,"
.TB_PREF."workorders wo,"
.TB_PREF."stock_master item
return (db_num_rows($result) > 0);
}
+//--------------------------------------------------------------------------------------
+
+function check_void_wo_production($prod_no)
+{
+ $prod = get_work_order_produce($prod_no);
+ return $prod['closed'] ? $prod['workorder_id'] : 0;
+}
+
//--------------------------------------------------------------------------------------------
function void_work_order_produce($type_no)
// Skip processing already voided entry i.e. explicitly voided
$void_entry = get_voided_entry(ST_MANURECEIVE, $type_no);
if ($void_entry)
- return;
-
- $row = get_work_order_produce($type_no);
-
- // deduct the quantity of this production from the parent work order
- work_order_update_finished_quantity($row["workorder_id"], -$row["quantity"]);
+ return;
- // void any related gl trans
+ $prod = get_work_order_produce($type_no);
- $woid = $row["workorder_id"];
- $date_ = sql2date($row["date_"]);
-
- $result = get_stock_moves(ST_MANURECEIVE, $type_no);
- while ($myrow = db_fetch($result))
- {
- $issue_cost = $myrow["qty"]*$myrow["standard_cost"];
- $issue = get_stock_gl_code($myrow["stock_id"]);
- $stockitem = get_item($myrow["stock_id"]);
+ if (work_order_is_closed($prod['workorder_id']))
+ return;
- // Compatibility for Service Items
- if (!is_service($issue["mb_flag"]))
- $ivaccount = $issue["inventory_account"];
- else
- $ivaccount = $issue["cogs_account"];
+ // deduct the quantity of this production from the parent work order
+ work_order_update_finished_quantity($prod["workorder_id"], -$prod["quantity"], false);
- if ($issue_cost != 0)
- {
- add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,
- $date_.": "._("Reversed the production ")." ".$stockitem["description"],
- -$issue_cost);
- }
- }
// clear the production record
$sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".db_escape($type_no);
db_query($sql, "Cannot void a wo production");
- // Shifted below
- // void all related stock moves
+ void_gl_trans(ST_MANURECEIVE, $type_no);
+
void_stock_move(ST_MANURECEIVE, $type_no);
commit_transaction();
Update finished items quantity in work order, and close order either if all the order is produced,
or on user demand. Returns calculated unit cost on close, or null otherwise.
*/
-function work_order_update_finished_quantity($woid, $quantity, $date, $force_close=0)
+function work_order_update_finished_quantity($woid, $quantity, $force_close=0)
{
$sql = "UPDATE ".TB_PREF."workorders SET units_issued = units_issued + ".db_escape($quantity)."
return db_num_affected_rows(); // returns 1 if WO has been closed
}
-//--------------------------------------------------------------------------------------
+function reopen_work_order($woid)
+{
+ // FIXME: update_material_cost
+// update_material_cost($work_order['stock_id'], -$work_order['units_issued'], ... );
+
+ void_gl_trans(ST_WORKORDER, $woid);
+ void_stock_move(ST_WORKORDER, $woid);
+
+ $sql = "UPDATE ".TB_PREF."workorders SET closed=0 WHERE id = "
+ .db_escape($woid);
+ db_query($sql, "The work order couldn't be reopened");
+}
+
+//--------------------------------------------------------------------------------------
+/*
+ Void all work order related operations
+*/
function void_work_order($woid)
{
begin_transaction();
hook_db_prevoid(ST_WORKORDER, $woid);
$work_order = get_work_order($woid);
- if (!($work_order["type"] == WO_ADVANCED))
- {
- // restore average product costs
- $date = sql2date($work_order['date_']);
- $qty = $work_order['units_reqd'];
- // FIXME: update_material_cost
-
- // close workorder
- $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
- .db_escape($woid);
- db_query($sql, "The work order couldn't be voided");
-
- // void all related stock moves
- void_stock_move(ST_WORKORDER, $woid);
-
- // void any related costing records
- void_wo_costing($woid);
+ if ($work_order['closed'])
+ reopen_work_order($woid);
- // clear the requirements units received
- void_wo_requirements($woid);
- }
- else
+ if ($work_order['units_issued'])
{
- // void everything inside the work order : issues, productions, payments
- $date = sql2date($work_order['date_']);
+ $prods = get_work_order_productions($woid);
+ while ($prod = db_fetch($prods))
+ void_work_order_produce($prod['id']);
+ }
- // FIXME: update_material_cost
+ // close workorder
+ $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
+ .db_escape($woid);
+ db_query($sql, "The work order couldn't be voided");
- $result = get_work_order_productions($woid); // check the produced quantity
- $qty = 0;
- while ($row = db_fetch($result))
- {
- $qty += $row['quantity'];
- // clear the production record
- $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".$row['id'];
- db_query($sql, "Cannot void a wo production");
-
- void_stock_move(ST_MANURECEIVE, $row['id']); // and void the stock moves;
- }
- $result = get_additional_issues($woid); // check the issued quantities
- $cost = 0;
- $issue_no = 0;
- while ($row = db_fetch($result))
- {
- $unit_cost = get_unit_cost($row['stock_id']);
- $icost = $unit_cost * $row['qty_issued'];
- $cost += $icost;
- if ($issue_no == 0)
- $issue_no = $row['issue_no'];
- // void the actual issue items and their quantities
- $sql = "UPDATE ".TB_PREF."wo_issue_items SET qty_issued = 0 WHERE issue_id="
- .db_escape($row['id']);
- db_query($sql,"A work order issue item could not be voided");
- }
- if ($issue_no != 0)
- void_stock_move(ST_MANUISSUE, $issue_no); // and void the stock moves
-
- if ($cost != 0)
- add_issue_cost($work_order['stock_id'], -$qty, $date, $cost);
-
- $sql = "UPDATE ".TB_PREF."workorders SET closed=1,units_reqd=0,units_issued=0 WHERE id = "
- .db_escape($woid);
- db_query($sql, "The work order couldn't be voided");
+ // void addtional material issues
+ $issues = get_work_order_issues($woid);
+ while($issue = db_fetch($issues))
+ void_work_order_issue($issue['issue_no']);
- // void all related stock moves
- void_stock_move(ST_WORKORDER, $woid);
+ // void all related stock moves
+ void_stock_move(ST_WORKORDER, $woid);
- // void any related bank/gl trans
- void_wo_costing($woid);
+ // void any related costing records
+ void_wo_costing($woid);
- // clear the requirements units received
- void_wo_requirements($woid);
- }
+ // clear the requirements units received
+ void_wo_requirements($woid);
commit_transaction();
}
//-------------------------------------------------------------------------------------
if (!isset($_POST['ref']))
- $_POST['ref'] = $Refs->get_next(ST_JOURNAL, null, get_post('date_'));
+ $_POST['ref'] = $Refs->get_next(ST_JOURNAL, null, Today());
start_form();