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