Added db_write/db_void hooks.
[fa-stable.git] / manufacturing / includes / db / work_order_produce_items_db.inc
1 <?php
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 function work_order_produce($woid, $ref, $quantity, $date_, $memo_, $close_wo)
13 {
14         global $Refs;
15
16         begin_transaction();
17         $args = func_get_args();
18         $args = (object)array_combine(array('woid', 'ref', 'quantity', 'date_', 'memo_','close_wo'),
19                 $args);
20         $args->trans_no = 0;
21         hook_db_prewrite($args, ST_MANURECEIVE);
22
23         $details = get_work_order($woid);
24
25     if (strlen($details[0]) == 0)
26     {
27         echo _("The order number sent is not valid.");
28         exit;
29     }
30
31         if (work_order_is_closed($woid))
32         {
33                 display_error("UNEXPECTED : Producing Items for a closed Work Order");
34                 cancel_transaction();
35                 exit;
36         }
37
38     $date = date2sql($date_);
39
40     $sql = "INSERT INTO ".TB_PREF."wo_manufacture (workorder_id, reference, quantity, date_)
41                 VALUES (".db_escape($woid).", ".db_escape($ref).", ".db_escape($quantity)
42                 .", '$date')";
43
44         db_query($sql,"A work order manufacture could not be added");
45
46         $id = db_insert_id();
47
48         // -------------------------------------------------------------------------
49
50         work_order_quick_costs($woid, $details["stock_id"], $quantity, $date_, $id);
51
52         // -------------------------------------------------------------------------
53         // insert a +ve stock move for the item being manufactured
54         // negative means "unproduce" or unassemble
55         add_stock_move(ST_MANURECEIVE, $details["stock_id"], $id,
56                 $details["loc_code"], $date_, $ref, $quantity, 0);
57         // update wo quantity and close wo if requested
58         work_order_update_finished_quantity($woid, $quantity, $close_wo);
59
60
61         if ($memo_)
62                 add_comments(ST_MANURECEIVE, $id, $date_, $memo_);
63
64         $Refs->save(ST_MANURECEIVE, $id, $ref);
65         add_audit_trail(ST_MANURECEIVE, $id, $date_, _("Production."));
66
67         $args->trans_no = $id;
68         hook_db_postwrite($args, ST_MANURECEIVE);
69         commit_transaction();
70 }
71
72 //--------------------------------------------------------------------------------------------
73
74 function get_work_order_produce($id)
75 {
76         $sql = "SELECT ".TB_PREF."wo_manufacture.*,".TB_PREF."workorders.stock_id, "
77                 .TB_PREF."stock_master.description AS StockDescription
78                 FROM ".TB_PREF."wo_manufacture, ".TB_PREF."workorders, ".TB_PREF."stock_master
79                 WHERE ".TB_PREF."wo_manufacture.workorder_id=".TB_PREF."workorders.id
80                 AND ".TB_PREF."stock_master.stock_id=".TB_PREF."workorders.stock_id
81                 AND ".TB_PREF."wo_manufacture.id=".db_escape($id);
82     $result = db_query($sql, "The work order production could not be retrieved");
83
84     return db_fetch($result);
85 }
86
87 //--------------------------------------------------------------------------------------
88
89 function get_work_order_productions($woid)
90 {
91         $sql = "SELECT * FROM ".TB_PREF."wo_manufacture WHERE workorder_id="
92                 .db_escape($woid)." ORDER BY id";
93     return db_query($sql, "The work order issues could not be retrieved");
94 }
95
96 //--------------------------------------------------------------------------------------
97
98 function exists_work_order_produce($id)
99 {
100         $sql = "SELECT id FROM ".TB_PREF."wo_manufacture WHERE id=".db_escape($id);
101         $result = db_query($sql, "Cannot retreive a wo production");
102
103     return (db_num_rows($result) > 0);
104 }
105
106 //--------------------------------------------------------------------------------------------
107
108 function void_work_order_produce($type_no)
109 {
110         begin_transaction();
111         hook_db_prevoid(ST_MANURECEIVE, $type_no);
112
113         $row = get_work_order_produce($type_no);
114
115         // deduct the quantity of this production from the parent work order
116         work_order_update_finished_quantity($row["workorder_id"], -$row["quantity"]);
117
118         work_order_quick_costs($row['workorder_id'], $row['stock_id'], -$row['quantity'], sql2date($row['date_']), $type_no);
119
120         // clear the production record
121         $sql = "UPDATE ".TB_PREF."wo_manufacture SET quantity=0 WHERE id=".db_escape($type_no);
122         db_query($sql, "Cannot void a wo production");
123
124         // void all related stock moves
125         void_stock_move(ST_MANURECEIVE, $type_no);
126
127         // void any related gl trans
128         void_gl_trans(ST_MANURECEIVE, $type_no, true);
129
130         commit_transaction();
131 }
132
133
134 ?>