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