Security statements update against sql injection attacks.
[fa-stable.git] / manufacturing / includes / db / work_order_issues_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 //--------------------------------------------------------------------------------------
13
14 function add_work_order_issue($woid, $ref, $to_work_order, $items, $location, $workcentre,
15         $date_, $memo_)
16 {
17         begin_transaction();
18
19         $details = get_work_order($woid);
20
21     if (strlen($details[0]) == 0)
22     {
23         echo _("The order number sent is not valid.");
24         cancel_transaction();
25         exit;
26     }
27
28         if (work_order_is_closed($woid))
29         {
30                 display_error("UNEXPECTED : Issuing items for a closed Work Order");
31                 cancel_transaction();
32                 exit;
33         }
34
35         // insert the actual issue
36         $sql = "INSERT INTO ".TB_PREF."wo_issues (workorder_id, reference, issue_date, loc_code, workcentre_id)
37                 VALUES (".db_escape($woid).", ".db_escape($ref).", '" .
38                 date2sql($date_) . "', ".db_escape($location).", ".db_escape($workcentre).")";
39         db_query($sql,"The work order issue could not be added");
40
41         $number = db_insert_id();
42
43         foreach ($items as $item)
44         {
45
46                 if ($to_work_order)
47                         $item->quantity = -$item->quantity;
48
49                 // insert a -ve stock move for each item
50                 add_stock_move(28, $item->stock_id, $number,
51                         $location, $date_, $memo_, -$item->quantity, 0);
52
53                 $sql = "INSERT INTO ".TB_PREF."wo_issue_items (issue_id, stock_id, qty_issued)
54                         VALUES (".db_escape($number).", ".db_escape($item->stock_id).", "
55                         .db_escape($item->quantity).")";
56                 db_query($sql,"A work order issue item could not be added");
57         }
58
59         if ($memo_)
60                 add_comments(28, $number, $date_, $memo_);
61
62         references::save_last($ref, 28);
63
64         commit_transaction();
65 }
66
67 //--------------------------------------------------------------------------------------
68
69 function get_work_order_issues($woid)
70 {
71         $sql = "SELECT * FROM ".TB_PREF."wo_issues WHERE workorder_id=".db_escape($woid)
72         ." ORDER BY issue_no";
73     return db_query($sql, "The work order issues could not be retrieved");
74 }
75
76 function get_additional_issues($woid)
77 {
78         $sql = "SELECT ".TB_PREF."wo_issues.*, ".TB_PREF."wo_issue_items.*
79                 FROM ".TB_PREF."wo_issues, ".TB_PREF."wo_issue_items
80                 WHERE ".TB_PREF."wo_issues.issue_no=".TB_PREF."wo_issue_items.issue_id
81                 AND ".TB_PREF."wo_issues.workorder_id=".db_escape($woid)
82                 ." ORDER BY ".TB_PREF."wo_issue_items.id";
83     return db_query($sql, "The work order issues could not be retrieved");
84 }
85 //--------------------------------------------------------------------------------------
86
87 function get_work_order_issue($issue_no)
88 {
89         $sql = "SELECT DISTINCT ".TB_PREF."wo_issues.*, ".TB_PREF."workorders.stock_id,
90                 ".TB_PREF."stock_master.description, ".TB_PREF."locations.location_name, "
91                 .TB_PREF."workcentres.name AS WorkCentreName
92                 FROM ".TB_PREF."wo_issues, ".TB_PREF."workorders, ".TB_PREF."stock_master, "
93                 .TB_PREF."locations, ".TB_PREF."workcentres
94                 WHERE issue_no=".db_escape($issue_no)."
95                 AND ".TB_PREF."workorders.id = ".TB_PREF."wo_issues.workorder_id
96                 AND ".TB_PREF."locations.loc_code = ".TB_PREF."wo_issues.loc_code
97                 AND ".TB_PREF."workcentres.id = ".TB_PREF."wo_issues.workcentre_id
98                 AND ".TB_PREF."stock_master.stock_id = ".TB_PREF."workorders.stock_id";
99     $result = db_query($sql, "A work order issue could not be retrieved");
100
101     return db_fetch($result);
102 }
103
104 //--------------------------------------------------------------------------------------
105
106 function get_work_order_issue_details($issue_no)
107 {
108         $sql = "SELECT ".TB_PREF."wo_issue_items.*,"
109         .TB_PREF."stock_master.description, ".TB_PREF."stock_master.units
110                 FROM ".TB_PREF."wo_issue_items, ".TB_PREF."stock_master
111                 WHERE issue_id=".db_escape($issue_no)."
112                 AND ".TB_PREF."stock_master.stock_id=".TB_PREF."wo_issue_items.stock_id
113                 ORDER BY ".TB_PREF."wo_issue_items.id";
114     return db_query($sql, "The work order issue items could not be retrieved");
115 }
116
117 //--------------------------------------------------------------------------------------
118
119 function exists_work_order_issue($issue_no)
120 {
121         $sql = "SELECT issue_no FROM ".TB_PREF."wo_issues WHERE issue_no=".db_escape($issue_no);
122         $result = db_query($sql, "Cannot retreive a wo issue");
123
124     return (db_num_rows($result) > 0);
125 }
126
127 //--------------------------------------------------------------------------------------
128
129 function void_work_order_issue($type_no)
130 {
131         begin_transaction();
132
133         // void the actual issue items and their quantities
134         $sql = "UPDATE ".TB_PREF."wo_issue_items Set qty_issued = 0 WHERE issue_id="
135                 .db_escape($type_no);
136         db_query($sql,"A work order issue item could not be voided");
137
138         // void all related stock moves
139         void_stock_move(28, $type_no);
140
141         // void any related gl trans
142         void_gl_trans(28, $type_no, true);
143
144         commit_transaction();
145 }
146
147
148 //--------------------------------------------------------------------------------------
149
150 ?>