Further changes in manufacturing, added unit_cost in issues and requirements
[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
40                 if ($to_work_order)
41                         $item->quantity = -$item->quantity;
42
43                 // Stamp the standard_cost
44                 $standard_cost = get_standard_cost($item->stock_id);
45                 // insert a -ve stock move for each item
46                 add_stock_move(ST_MANUISSUE, $item->stock_id, $number,
47                         $location, $date_, $memo_, -$item->quantity, $standard_cost);
48
49                 $sql = "INSERT INTO ".TB_PREF."wo_issue_items (issue_id, stock_id, qty_issued, unit_cost)
50                         SELECT ".db_escape($number).",".db_escape($item->stock_id).",".db_escape($item->quantity).", material_cost
51                         FROM ".TB_PREF."stock_master
52                         WHERE stock_id=".db_escape($item->stock_id);
53
54                 db_query($sql,"A work order issue item could not be added");
55
56                 $standard_cost = get_standard_cost($item->stock_id);
57                 $issue_cost = $standard_cost * $item->quantity;
58
59         $stockitem = get_item($item->stock_id);
60
61                 // Compatibility for Service Items
62                 if (!is_service($stockitem["mb_flag"]))
63                         $ivaccount = $stockitem["inventory_account"];
64                 else
65                         $ivaccount = $stockitem["assembly_account"];
66
67         $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,
68                 $date_.": "._("Issue of")." ".$stockitem["description"], -$issue_cost);
69                 $issue_total += $issue_cost;
70         }
71
72         if ($issue_total != 0)  // Apply cost to QOH as adjustment only
73                 add_issue_cost($wo['stock_id'], $wo['units_reqd'], $date_, $issue_total, true);
74
75     $stockitem = get_item($wo['stock_id']);
76     $wip_account = get_company_pref('wip_act'); 
77
78     if (!$wip_account)  // backward compatibility
79         $wip_account = $stockitem["inventory_account"];
80
81     add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $wip_account,
82         0, 0, $date_.": "._("Issue to")." ".$stockitem["description"], -$total_cost);
83
84         if ($memo_)
85                 add_comments(ST_MANUISSUE, $number, $date_, $memo_);
86
87         $Refs->save(ST_MANUISSUE, $number, $ref);
88         add_audit_trail(ST_MANUISSUE, $number, $date_);
89
90         $args->trans_no = $number;
91         hook_db_postwrite($args, ST_MANUISSUE);
92
93         commit_transaction();
94 }
95
96 //--------------------------------------------------------------------------------------
97
98 function get_work_order_issues($woid)
99 {
100         $sql = "SELECT * FROM ".TB_PREF."wo_issues WHERE workorder_id=".db_escape($woid)
101         ." ORDER BY issue_no";
102     return db_query($sql, "The work order issues could not be retrieved");
103 }
104
105 function get_additional_issues($woid)
106 {
107         $sql = "SELECT issue.*, item.*
108                 FROM ".TB_PREF."wo_issues issue, "
109                         .TB_PREF."wo_issue_items item
110                 WHERE issue.issue_no=item.issue_id
111                 AND issue.workorder_id=".db_escape($woid)
112                 ." ORDER BY item.id";
113     return db_query($sql, "The work order issues could not be retrieved");
114 }
115 //--------------------------------------------------------------------------------------
116
117 function get_work_order_issue($issue_no)
118 {
119         $sql = "SELECT DISTINCT issue.*, wo.stock_id,
120                 item.description, loc.location_name, center.name AS WorkCentreName
121                 FROM ".TB_PREF."wo_issues issue,"
122                         .TB_PREF."workorders wo,"
123                         .TB_PREF."stock_master item,"
124                         .TB_PREF."locations loc,"
125                         .TB_PREF."workcentres center
126                 WHERE issue_no=".db_escape($issue_no)."
127                 AND wo.id = issue.workorder_id
128                 AND loc.loc_code = issue.loc_code
129                 AND center.id = issue.workcentre_id
130                 AND item.stock_id = wo.stock_id";
131     $result = db_query($sql, "A work order issue could not be retrieved");
132
133     return db_fetch($result);
134 }
135
136 //--------------------------------------------------------------------------------------
137
138 function get_work_order_issue_details($issue_no)
139 {
140         $sql = "SELECT issue.*, item.description, item.units
141                 FROM ".TB_PREF."wo_issue_items issue,"
142                         .TB_PREF."stock_master item
143                 WHERE issue_id=".db_escape($issue_no)."
144                 AND item.stock_id=issue.stock_id
145                 ORDER BY issue.id";
146     return db_query($sql, "The work order issue items could not be retrieved");
147 }
148
149 //--------------------------------------------------------------------------------------
150
151 function exists_work_order_issue($issue_no)
152 {
153         $sql = "SELECT issue_no FROM ".TB_PREF."wo_issues WHERE issue_no=".db_escape($issue_no);
154         $result = db_query($sql, "Cannot retreive a wo issue");
155
156     return (db_num_rows($result) > 0);
157 }
158
159 //--------------------------------------------------------------------------------------
160
161 function void_work_order_issue($type_no)
162 {
163         begin_transaction();
164         hook_db_prevoid(ST_MANUISSUE, $type_no);
165
166         //Chaitanya : Skip processing already voided entry i.e. explicitly voided
167         $void_entry = get_voided_entry(ST_MANUISSUE, $type_no);
168         if ($void_entry)
169                 return;
170
171         // void the actual issue items and their quantities
172         $sql = "UPDATE ".TB_PREF."wo_issue_items Set qty_issued = 0 WHERE issue_id="
173                 .db_escape($type_no);
174         db_query($sql,"A work order issue item could not be voided");
175
176         // Reverse the gl posting
177         $issue = get_work_order_issue($type_no);
178         $manf_stock_id = $issue["stock_id"];
179         $date_ = sql2date($issue["issue_date"]);
180         $woid = $issue["workorder_id"];
181
182         $result = get_stock_moves(ST_MANUISSUE, $type_no);
183         $total_cost = 0;
184         $issue_total = 0;
185         while ($myrow = db_fetch($result))
186         {
187                 $issue_cost = $myrow["qty"]*$myrow["standard_cost"];
188                 $issue = get_stock_gl_code($myrow["stock_id"]);
189         $stockitem = get_item($myrow["stock_id"]);
190
191                 // Compatibility for Service Items
192                 if (!is_service($issue["mb_flag"]))
193                         $ivaccount = $issue["inventory_account"];
194                 else
195                         $ivaccount = $issue["assembly_account"];
196
197                 if ($issue_cost != 0)
198                 {
199                         $total_cost += add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $ivaccount, 0, 0,
200                                 $date_.": "._("Reversed the issue of")." ".$stockitem["description"],
201                                 -$issue_cost);
202                         $issue_total += $issue_cost;
203                 }
204         }
205         if ($issue_total != 0)
206                 // Revese cost effect on manfactured stock item as adjustment only
207                 add_issue_cost($manf_stock_id, 0, $date_, $issue_total, true);
208         $issue = get_stock_gl_code($manf_stock_id);
209     $stockitem = get_item($manf_stock_id);
210         if ($total_cost != 0)
211                 add_gl_trans_std_cost(ST_WORKORDER, $woid, $date_, $issue["inventory_account"],
212                         0, 0, $date_.": "._("Reversed the issue to")." ".$stockitem["description"], 
213                         -$total_cost);
214
215         // Shifted below void all related stock moves
216         void_stock_move(ST_MANUISSUE, $type_no);
217
218         commit_transaction();
219 }
220
221
222 //--------------------------------------------------------------------------------------
223