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