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