Merged changes from main CVS up to 2.1.5
[fa-stable.git] / purchasing / po_receive_items.php
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 $page_security = 11;
13 $path_to_root="..";
14 include_once($path_to_root . "/purchasing/includes/po_class.inc");
15
16 include_once($path_to_root . "/includes/session.inc");
17 include_once($path_to_root . "/purchasing/includes/purchasing_db.inc");
18 include_once($path_to_root . "/purchasing/includes/purchasing_ui.inc");
19
20 $js = "";
21 if ($use_popup_windows)
22         $js .= get_js_open_window(900, 500);
23 if ($use_date_picker)
24         $js .= get_js_date_picker();
25 page(_("Receive Purchase Order Items"), false, false, "", $js);
26
27 //---------------------------------------------------------------------------------------------------------------
28
29 if (isset($_GET['AddedID']))
30 {
31         $grn = $_GET['AddedID'];
32         $trans_type = 25;
33
34         display_notification_centered(_("Purchase Order Delivery has been processed"));
35
36         display_note(get_trans_view_str($trans_type, $grn, _("&View this Delivery")));
37
38         //echo "<BR>";
39         //echo get_gl_view_str(25, $grn, _("View the GL &Journal Entries for this Delivery"));
40
41 //      echo "<br>";
42         hyperlink_params("$path_to_root/purchasing/supplier_invoice.php", _("Entry purchase &invoice for this receival"), "New=1");
43
44         hyperlink_no_params("$path_to_root/purchasing/inquiry/po_search.php", _("Select a different &purchase order for receiving items against"));
45
46         display_footer_exit();
47 }
48
49 //--------------------------------------------------------------------------------------------------
50
51 if ((!isset($_GET['PONumber']) || $_GET['PONumber'] == 0) && !isset($_SESSION['PO']))
52 //if (isset($_GET['PONumber']) && !$_GET['PONumber'] > 0 && !isset($_SESSION['PO']))
53 {
54         die (_("This page can only be opened if a purchase order has been selected. Please select a purchase order first."));
55 }
56
57 //--------------------------------------------------------------------------------------------------
58
59 function display_po_receive_items()
60 {
61         global $table_style;
62
63         div_start('grn_items');
64     start_table("colspan=7 $table_style width=90%");
65     $th = array(_("Item Code"), _("Description"), _("Ordered"), _("Units"), _("Received"),
66         _("Outstanding"), _("This Delivery"), _("Price"), _("Total"));
67     table_header($th);
68
69     /*show the line items on the order with the quantity being received for modification */
70
71     $total = 0;
72     $k = 0; //row colour counter
73
74     if (count($_SESSION['PO']->line_items)> 0 )
75     {
76         foreach ($_SESSION['PO']->line_items as $ln_itm)
77         {
78
79                         alt_table_row_color($k);
80
81                 $qty_outstanding = $ln_itm->quantity - $ln_itm->qty_received;
82
83                         if (!isset($_POST['Update']) && !isset($_POST['ProcessGoodsReceived']) && $ln_itm->receive_qty == 0)
84                 {   //If no quantites yet input default the balance to be received
85                 $ln_itm->receive_qty = $qty_outstanding;
86                 }
87
88                 $line_total = ($ln_itm->receive_qty * $ln_itm->price);
89                 $total += $line_total;
90
91                         label_cell($ln_itm->stock_id);
92                         if ($qty_outstanding > 0)
93                                 text_cells(null, $ln_itm->stock_id . "Desc", $ln_itm->item_description, 30, 50);
94                         else
95                                 label_cell($ln_itm->item_description);
96                         $dec = get_qty_dec($ln_itm->stock_id);
97                         qty_cell($ln_itm->quantity, false, $dec);
98                         label_cell($ln_itm->units);
99                         qty_cell($ln_itm->qty_received, false, $dec);
100                         qty_cell($qty_outstanding, false, $dec);
101
102                         if ($qty_outstanding > 0)
103                                 qty_cells(null, $ln_itm->line_no, number_format2($ln_itm->receive_qty, $dec), "align=right", null, $dec);
104                         else
105                                 label_cell(number_format2($ln_itm->receive_qty, $dec), "align=right");
106
107                         amount_decimal_cell($ln_itm->price);
108                         amount_cell($line_total);
109                         end_row();
110         }
111     }
112
113     $display_total = number_format2($total,user_price_dec());
114     label_row(_("Total value of items received"), $display_total, "colspan=8 align=right",
115         "nowrap align=right");
116     end_table();
117         div_end();
118 }
119
120 //--------------------------------------------------------------------------------------------------
121
122 function check_po_changed()
123 {
124         /*Now need to check that the order details are the same as they were when they were read into the Items array. If they've changed then someone else must have altered them */
125         // Sherifoz 22.06.03 Compare against COMPLETED items only !!
126         // Otherwise if you try to fullfill item quantities separately will give error.
127         $sql = "SELECT item_code, quantity_ordered, quantity_received, qty_invoiced
128                 FROM ".TB_PREF."purch_order_details
129                 WHERE order_no=" . $_SESSION['PO']->order_no 
130                 ." ORDER BY po_detail_item";
131
132         $result = db_query($sql, "could not query purch order details");
133     check_db_error("Could not check that the details of the purchase order had not been changed by another user ", $sql);
134
135         $line_no = 1;
136         while ($myrow = db_fetch($result))
137         {
138                 $ln_item = $_SESSION['PO']->line_items[$line_no];
139                 // only compare against items that are outstanding
140                 $qty_outstanding = $ln_item->quantity - $ln_item->qty_received;
141                 if ($qty_outstanding > 0)
142                 {
143                 if ($ln_item->qty_inv != $myrow["qty_invoiced"] ||
144                         $ln_item->stock_id != $myrow["item_code"] ||
145                         $ln_item->quantity != $myrow["quantity_ordered"] ||
146                         $ln_item->qty_received != $myrow["quantity_received"])
147                 {
148                         return true;
149                 }
150                 }
151                 $line_no++;
152         } /*loop through all line items of the order to ensure none have been invoiced */
153
154         return false;
155 }
156
157 //--------------------------------------------------------------------------------------------------
158
159 function can_process()
160 {
161         if (count($_SESSION['PO']->line_items) <= 0)
162         {
163         display_error(_("There is nothing to process. Please enter valid quantities greater than zero."));
164         return false;
165         }
166
167         if (!is_date($_POST['DefaultReceivedDate']))
168         {
169                 display_error(_("The entered date is invalid."));
170                 set_focus('DefaultReceivedDate');
171                 return false;
172         }
173
174     if (!references::is_valid($_POST['ref']))
175     {
176                 display_error(_("You must enter a reference."));
177                 set_focus('ref');
178                 return false;
179         }
180
181         if (!is_new_reference($_POST['ref'], 25))
182         {
183                 display_error(_("The entered reference is already in use."));
184                 set_focus('ref');
185                 return false;
186         }
187
188         $something_received = 0;
189         foreach ($_SESSION['PO']->line_items as $order_line)
190         {
191                 if ($order_line->receive_qty > 0)
192                 {
193                         $something_received = 1;
194                         break;
195                 }
196         }
197
198     // Check whether trying to deliver more items than are recorded on the actual purchase order (+ overreceive allowance)
199     $delivery_qty_too_large = 0;
200         foreach ($_SESSION['PO']->line_items as $order_line)
201         {
202                 if ($order_line->receive_qty+$order_line->qty_received >
203                         $order_line->quantity * (1+ (sys_prefs::over_receive_allowance() / 100)))
204                 {
205                         $delivery_qty_too_large = 1;
206                         break;
207                 }
208         }
209
210     if ($something_received == 0)
211     {   /*Then dont bother proceeding cos nothing to do ! */
212         display_error(_("There is nothing to process. Please enter valid quantities greater than zero."));
213         return false;
214     }
215     elseif ($delivery_qty_too_large == 1)
216     {
217         display_error(_("Entered quantities cannot be greater than the quantity entered on the purchase order including the allowed over-receive percentage") . " (" . sys_prefs::over_receive_allowance() ."%)."
218                 . "<br>" .
219                 _("Modify the ordered items on the purchase order if you wish to increase the quantities."));
220         return false;
221     }
222
223         return true;
224 }
225
226 //--------------------------------------------------------------------------------------------------
227
228 function process_receive_po()
229 {
230         global $path_to_root, $Ajax;
231
232         if (!can_process())
233                 return;
234
235         if (check_po_changed())
236         {
237                 display_error(_("This order has been changed or invoiced since this delivery was started to be actioned. Processing halted. To enter a delivery against this purchase order, it must be re-selected and re-read again to update the changes made by the other user."));
238                 hyperlink_no_params("$path_to_root/purchasing/inquiry/po_search.php",
239                  _("Select a different purchase order for receiving goods against"));
240                 hyperlink_params("$path_to_root/purchasing/po_receive_items.php", 
241                          _("Re-Read the updated purchase order for receiving goods against"),
242                          "PONumber=" . $_SESSION['PO']->order_no);
243                 unset($_SESSION['PO']->line_items);
244                 unset($_SESSION['PO']);
245                 unset($_POST['ProcessGoodsReceived']);
246                 $Ajax->activate('_page_body');
247                 display_footer_exit();
248         }
249
250         $grn = add_grn($_SESSION['PO'], $_POST['DefaultReceivedDate'],
251                 $_POST['ref'], $_POST['Location']);
252
253         new_doc_date($_POST['DefaultReceivedDate']);
254         unset($_SESSION['PO']->line_items);
255         unset($_SESSION['PO']);
256
257         meta_forward($_SERVER['PHP_SELF'], "AddedID=$grn");
258 }
259
260 //--------------------------------------------------------------------------------------------------
261
262 if (isset($_GET['PONumber']) && $_GET['PONumber'] > 0 && !isset($_POST['Update']))
263 {
264
265         create_new_po();
266
267         /*read in all the selected order into the Items cart  */
268         read_po($_GET['PONumber'], $_SESSION['PO']);
269 }
270
271 //--------------------------------------------------------------------------------------------------
272
273 if (isset($_POST['Update']) || isset($_POST['ProcessGoodsReceived']))
274 {
275
276         /* if update quantities button is hit page has been called and ${$line->line_no} would have be
277         set from the post to the quantity to be received in this receival*/
278         foreach ($_SESSION['PO']->line_items as $line)
279         {
280          if( ($line->quantity - $line->qty_received)>0) {
281                 $_POST[$line->line_no] = max($_POST[$line->line_no], 0);
282                 if (!check_num($line->line_no))
283                         $_POST[$line->line_no] = number_format2(0, get_qty_dec($line->stock_id));
284
285                 if (!isset($_POST['DefaultReceivedDate']) || $_POST['DefaultReceivedDate'] == "")
286                         $_POST['DefaultReceivedDate'] = new_doc_date();
287
288                 $_SESSION['PO']->line_items[$line->line_no]->receive_qty = input_num($line->line_no);
289
290                 if (isset($_POST[$line->stock_id . "Desc"]) && strlen($_POST[$line->stock_id . "Desc"]) > 0)
291                 {
292                         $_SESSION['PO']->line_items[$line->line_no]->item_description = $_POST[$line->stock_id . "Desc"];
293                 }
294          }
295         }
296         $Ajax->activate('grn_items');
297 }
298
299 //--------------------------------------------------------------------------------------------------
300
301 if (isset($_POST['ProcessGoodsReceived']))
302 {
303         process_receive_po();
304 }
305
306 //--------------------------------------------------------------------------------------------------
307
308 start_form();
309
310 display_grn_summary($_SESSION['PO'], true);
311 display_heading(_("Items to Receive"));
312 display_po_receive_items();
313
314 echo '<br>';
315 submit_center_first('Update', _("Update"), '', true);
316 submit_center_last('ProcessGoodsReceived', _("Process Receive Items"), _("Clear all GL entry fields"), 'default');
317
318 end_form();
319
320 //--------------------------------------------------------------------------------------------------
321
322 end_page();
323 ?>
324