New files from unstable branch
[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         global $Refs;
18
19         begin_transaction();
20         $args = func_get_args();
21         $args = (object)array_combine(array('woid', 'ref', 'to_work_order', 'items', 'location', 
22                 'workcentre', 'date_', 'memo_'), $args);
23         $args->trans_no = 0;
24         hook_db_prewrite($args, ST_MANUISSUE);
25
26         $details = get_work_order($woid);
27
28     if (strlen($details[0]) == 0)
29     {
30         echo _("The order number sent is not valid.");
31         cancel_transaction();
32         exit;
33     }
34
35         if (work_order_is_closed($woid))
36         {
37                 display_error("UNEXPECTED : Issuing items for a closed Work Order");
38                 cancel_transaction();
39                 exit;
40         }
41
42         // insert the actual issue
43         $sql = "INSERT INTO ".TB_PREF."wo_issues (workorder_id, reference, issue_date, loc_code, workcentre_id)
44                 VALUES (".db_escape($woid).", ".db_escape($ref).", '" .
45                 date2sql($date_) . "', ".db_escape($location).", ".db_escape($workcentre).")";
46         db_query($sql,"The work order issue could not be added");
47
48         $number = db_insert_id();
49
50         $issue_total = $total_cost = 0;
51
52         foreach ($items as $item)
53         {
54
55                 if ($to_work_order)
56                         $item->quantity = -$item->quantity;
57
58                 // insert a -ve stock move for each item
59                 add_stock_move(ST_MANUISSUE, $item->stock_id, $number,
60                         $location, $date_, $memo_, -$item->quantity, 0);
61
62                 $sql = "INSERT INTO ".TB_PREF."wo_issue_items (issue_id, stock_id, qty_issued)
63                         VALUES (".db_escape($number).", ".db_escape($item->stock_id).", "
64                         .db_escape($item->quantity).")";
65                 db_query($sql,"A work order issue item could not be added");
66
67                 $standard_cost = get_standard_cost($item->stock_id);
68                 $issue_cost = $standard_cost * $item->quantity;
69                 $issue = get_stock_gl_code($item->stock_id);
70         $stockitem = get_item($item->stock_id);
71         $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $issue["inventory_account"], 0, 0,
72                 $date_.": "._("Issue of")." ".$stockitem["description"], -$issue_cost);                 
73                 $issue_total += $issue_cost;
74         }       
75         if ($issue_total != 0)
76                 add_issue_cost($details['stock_id'], $details['units_reqd'], $date_, $issue_total);
77         $issue = get_stock_gl_code($details['stock_id']);
78     $stockitem = get_item($details['stock_id']);
79     add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $issue["inventory_account"],
80         0, 0, $date_.": "._("Issue to")." ".$stockitem["description"], -$total_cost);   
81
82         if ($memo_)
83                 add_comments(ST_MANUISSUE, $number, $date_, $memo_);
84
85         $Refs->save(ST_MANUISSUE, $number, $ref);
86         add_audit_trail(ST_MANUISSUE, $number, $date_);
87
88         $args->trans_no = $number;
89         hook_db_postwrite($args, ST_MANUISSUE);
90         commit_transaction();
91 }
92
93 //--------------------------------------------------------------------------------------
94
95 function get_work_order_issues($woid)
96 {
97         $sql = "SELECT * FROM ".TB_PREF."wo_issues WHERE workorder_id=".db_escape($woid)
98         ." ORDER BY issue_no";
99     return db_query($sql, "The work order issues could not be retrieved");
100 }
101
102 function get_additional_issues($woid)
103 {
104         $sql = "SELECT ".TB_PREF."wo_issues.*, ".TB_PREF."wo_issue_items.*
105                 FROM ".TB_PREF."wo_issues, ".TB_PREF."wo_issue_items
106                 WHERE ".TB_PREF."wo_issues.issue_no=".TB_PREF."wo_issue_items.issue_id
107                 AND ".TB_PREF."wo_issues.workorder_id=".db_escape($woid)
108                 ." ORDER BY ".TB_PREF."wo_issue_items.id";
109     return db_query($sql, "The work order issues could not be retrieved");
110 }
111 //--------------------------------------------------------------------------------------
112
113 function get_work_order_issue($issue_no)
114 {
115         $sql = "SELECT DISTINCT ".TB_PREF."wo_issues.*, ".TB_PREF."workorders.stock_id,
116                 ".TB_PREF."stock_master.description, ".TB_PREF."locations.location_name, "
117                 .TB_PREF."workcentres.name AS WorkCentreName
118                 FROM ".TB_PREF."wo_issues, ".TB_PREF."workorders, ".TB_PREF."stock_master, "
119                 .TB_PREF."locations, ".TB_PREF."workcentres
120                 WHERE issue_no=".db_escape($issue_no)."
121                 AND ".TB_PREF."workorders.id = ".TB_PREF."wo_issues.workorder_id
122                 AND ".TB_PREF."locations.loc_code = ".TB_PREF."wo_issues.loc_code
123                 AND ".TB_PREF."workcentres.id = ".TB_PREF."wo_issues.workcentre_id
124                 AND ".TB_PREF."stock_master.stock_id = ".TB_PREF."workorders.stock_id";
125     $result = db_query($sql, "A work order issue could not be retrieved");
126
127     return db_fetch($result);
128 }
129
130 //--------------------------------------------------------------------------------------
131
132 function get_work_order_issue_details($issue_no)
133 {
134         $sql = "SELECT ".TB_PREF."wo_issue_items.*,"
135         .TB_PREF."stock_master.description, ".TB_PREF."stock_master.units
136                 FROM ".TB_PREF."wo_issue_items, ".TB_PREF."stock_master
137                 WHERE issue_id=".db_escape($issue_no)."
138                 AND ".TB_PREF."stock_master.stock_id=".TB_PREF."wo_issue_items.stock_id
139                 ORDER BY ".TB_PREF."wo_issue_items.id";
140     return db_query($sql, "The work order issue items could not be retrieved");
141 }
142
143 //--------------------------------------------------------------------------------------
144
145 function exists_work_order_issue($issue_no)
146 {
147         $sql = "SELECT issue_no FROM ".TB_PREF."wo_issues WHERE issue_no=".db_escape($issue_no);
148         $result = db_query($sql, "Cannot retreive a wo issue");
149
150     return (db_num_rows($result) > 0);
151 }
152
153 //--------------------------------------------------------------------------------------
154
155 function void_work_order_issue($type_no)
156 {
157         begin_transaction();
158         hook_db_prevoid(ST_MANUISSUE, $type_no);
159
160         // void the actual issue items and their quantities
161         $sql = "UPDATE ".TB_PREF."wo_issue_items Set qty_issued = 0 WHERE issue_id="
162                 .db_escape($type_no);
163         db_query($sql,"A work order issue item could not be voided");
164
165         // void all related stock moves
166         void_stock_move(ST_MANUISSUE, $type_no);
167
168         // void any related gl trans
169         void_gl_trans(ST_MANUISSUE, $type_no, true);
170
171         commit_transaction();
172 }
173
174
175 //--------------------------------------------------------------------------------------
176
177 ?>