422e0019beea4f0959ee1d045d89ad2ab3d16e82
[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         $sql = "INSERT INTO ".TB_PREF."wo_issues (workorder_id, reference, issue_date, loc_code, workcentre_id)
27                 VALUES (".db_escape($woid).", ".db_escape($ref).", '" .
28                 date2sql($date_) . "', ".db_escape($location).", ".db_escape($workcentre).")";
29         db_query($sql,"The work order issue could not be added");
30
31         $number = db_insert_id();
32
33         $issue_total = $total_cost = 0;
34
35         $wo = get_work_order($woid);
36
37         foreach ($items as $item)
38         {
39                 if ($to_work_order)     // otherwise it is material return to inventory
40                         $item->quantity = -$item->quantity;
41
42                 $unit_cost = get_unit_cost($item->stock_id);
43                 // insert a -ve stock move for each item
44                 add_stock_move(ST_MANUISSUE, $item->stock_id, $number,
45                         $location, $date_, $memo_, -$item->quantity, $unit_cost);
46
47                 $sql = "INSERT INTO ".TB_PREF."wo_issue_items (issue_id, stock_id, qty_issued, unit_cost)
48                         SELECT ".db_escape($number).",".db_escape($item->stock_id).",".db_escape($item->quantity).", material_cost
49                         FROM ".TB_PREF."stock_master
50                         WHERE stock_id=".db_escape($item->stock_id);
51
52                 db_query($sql,"A work order issue item could not be added");
53
54                 $unit_cost = get_unit_cost($item->stock_id);
55                 $issue_cost = $unit_cost * $item->quantity;
56
57         $stockitem = get_item($item->stock_id);
58
59                 // Compatibility for Service Items
60                 if (!is_service($stockitem["mb_flag"]))
61                         $ivaccount = $stockitem["inventory_account"];
62                 else
63                         $ivaccount = $stockitem["cogs_account"];
64
65         $total_cost += add_gl_trans_std_cost(ST_MANUISSUE, $number, $date_, $ivaccount, 0, 0,
66                 $date_.": "._("Issue of")." ".$stockitem["description"], -$issue_cost);
67         }
68
69     $stockitem = get_item($wo['stock_id']);
70
71         $wip_account = $stockitem["wip_account"];
72
73     add_gl_trans_std_cost(ST_MANUISSUE, $number, $date_, $wip_account,
74         0, 0, $date_.": "._("Issue to")." ".$stockitem["description"], -$total_cost);
75
76         if ($memo_)
77                 add_comments(ST_MANUISSUE, $number, $date_, $memo_);
78
79         $Refs->save(ST_MANUISSUE, $number, $ref);
80         add_audit_trail(ST_MANUISSUE, $number, $date_);
81
82         $args->trans_no = $number;
83         hook_db_postwrite($args, ST_MANUISSUE);
84
85         commit_transaction();
86 }
87
88 //--------------------------------------------------------------------------------------
89
90 function get_work_order_issues($woid)
91 {
92         $sql = "SELECT * FROM ".TB_PREF."wo_issues WHERE workorder_id=".db_escape($woid)
93         ." ORDER BY issue_no";
94     return db_query($sql, "The work order issues could not be retrieved");
95 }
96
97 function get_additional_issues($woid)
98 {
99         $sql = "SELECT issue.*, item.*, stock.mb_flag
100                 FROM ".TB_PREF."wo_issues issue, "
101                         .TB_PREF."wo_issue_items item
102                         LEFT JOIN ".TB_PREF."stock_master stock ON stock.stock_id=item.stock_id
103                 WHERE issue.issue_no=item.issue_id
104                 AND issue.workorder_id=".db_escape($woid)
105                 ." ORDER BY item.id";
106     return db_query($sql, "The work order issues could not be retrieved");
107 }
108 //--------------------------------------------------------------------------------------
109
110 function get_work_order_issue($issue_no)
111 {
112         $sql = "SELECT DISTINCT issue.*, wo.stock_id, wo.closed,
113                 item.description, loc.location_name, center.name AS WorkCentreName
114                 FROM ".TB_PREF."wo_issues issue,"
115                         .TB_PREF."workorders wo,"
116                         .TB_PREF."stock_master item,"
117                         .TB_PREF."locations loc,"
118                         .TB_PREF."workcentres center
119                 WHERE issue_no=".db_escape($issue_no)."
120                 AND wo.id = issue.workorder_id
121                 AND loc.loc_code = issue.loc_code
122                 AND center.id = issue.workcentre_id
123                 AND item.stock_id = wo.stock_id";
124     $result = db_query($sql, "A work order issue could not be retrieved");
125
126     return db_fetch($result);
127 }
128
129 //--------------------------------------------------------------------------------------
130
131 function get_work_order_issue_details($issue_no)
132 {
133         $sql = "SELECT issue.*, item.description, item.units
134                 FROM ".TB_PREF."wo_issue_items issue,"
135                         .TB_PREF."stock_master item
136                 WHERE issue_id=".db_escape($issue_no)."
137                 AND item.stock_id=issue.stock_id
138                 ORDER BY issue.id";
139     return db_query($sql, "The work order issue items could not be retrieved");
140 }
141
142 //--------------------------------------------------------------------------------------
143
144 function exists_work_order_issue($issue_no)
145 {
146         $sql = "SELECT issue_no FROM ".TB_PREF."wo_issues WHERE issue_no=".db_escape($issue_no);
147         $result = db_query($sql, "Cannot retreive a wo issue");
148
149     return (db_num_rows($result) > 0);
150 }
151
152 //--------------------------------------------------------------------------------------
153
154 function check_void_wo_issue($issue_no)
155 {
156         $issue = get_work_order_issue($issue_no);
157
158         return $issue['closed'] ? $issue['workorder_id'] : 0;
159 }
160
161 //--------------------------------------------------------------------------------------
162
163 function void_work_order_issue($type_no)
164 {
165         begin_transaction();
166         hook_db_prevoid(ST_MANUISSUE, $type_no);
167
168         $void_entry = get_voided_entry(ST_MANUISSUE, $type_no);
169         if ($void_entry)
170                 return;
171
172         if (check_void_wo_issue($type_no))
173                 return;
174
175         // FIXME update issued material cost
176
177         // void the actual issue items and their quantities
178         $sql = "UPDATE ".TB_PREF."wo_issue_items SET qty_issued = 0
179                 WHERE issue_id=".db_escape($type_no);
180
181         db_query($sql,"A work order issue item could not be voided");
182
183         void_gl_trans(ST_MANUISSUE, $type_no);
184
185         void_stock_move(ST_MANUISSUE, $type_no);
186
187         commit_transaction();
188 }