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