- // -------------------------------------------------------------------------
- // insert a +ve stock move for the item being manufactured
- // negative means "unproduce" or unassemble
- add_stock_move(29, $details["stock_id"], $id,
- $details["loc_code"], $date_, $memo_, $quantity, 0);
- // update wo quantity and close wo if requested
- 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)
+ {
+ // 1. calculate sums of material/labour/overhead costs
+
+ // sum collected BOM material & labour costs (no way for separate overhead here for now - needs flag in bom or stock_master)
+ $bom = get_wo_requirements($woid);
+ $m_cost = $l_cost = 0;
+ while ($component = db_fetch($bom))
+ {
+ if (!is_service($component["mb_flag"]))
+ $m_cost += $component['unit_cost']*$component['units_issued'];
+ else
+ $l_cost += $component['unit_cost']*$component['units_issued'];
+ }
+
+ // add additional material issues
+ $issues = get_additional_issues($woid);
+ while ($issue = db_fetch($issues))
+ {
+ if (!is_service($issue["mb_flag"]))
+ $m_cost += $issue['unit_cost']*$issue['qty_issued'];
+ else
+ $l_cost += $issue['unit_cost']*$issue['qty_issued'];
+ }
+
+ // and additional costs
+ $o_cost = get_gl_wo_cost($woid, WO_OVERHEAD);
+ $l_cost += get_gl_wo_cost($woid, WO_LABOUR);
+
+ $total_cost = $o_cost + $m_cost + $l_cost;
+
+ add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $product['wip_account'],
+ 0, 0, $memo, -$total_cost);
+
+ add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $product['inventory_account'],
+ 0, 0, $memo, $total_cost);
+
+ $unit_cost = $total_cost/($product['units_issued']+$quantity);
+
+ // first update material cost
+ update_material_cost($product['stock_id'], $product['units_issued']+$quantity, $unit_cost, $date_);
+
+ add_stock_move(ST_WORKORDER, $product["stock_id"], $woid,
+ $product["loc_code"], $date_, $ref, $product['units_issued']+$quantity, $unit_cost);
+ }