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