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