Merged last changes from stable.
[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 = 'SA_GRN';
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(_($help_context = "Receive Purchase Order Items"), false, false, "", $js);
26
27 //---------------------------------------------------------------------------------------------------------------
28
29 if (isset($_GET['AddedID']))
30 {
31         $grn = $_GET['AddedID'];
32         $trans_type = ST_SUPPRECEIVE;
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     $clearing_act = get_company_pref('grn_clearing_act');
39         if ($clearing_act)      
40                 display_note(get_gl_view_str($trans_type, $grn, _("View the GL Journal Entries for this Delivery")), 1);
41
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 {
53         die (_("This page can only be opened if a purchase order has been selected. Please select a purchase order first."));
54 }
55
56 //--------------------------------------------------------------------------------------------------
57
58 function display_po_receive_items()
59 {
60         div_start('grn_items');
61     start_table(TABLESTYLE, "colspan=7 width=90%");
62     $th = array(_("Item Code"), _("Description"), _("Ordered"), _("Units"), _("Received"),
63         _("Outstanding"), _("This Delivery"), _("Price"), _("Total"));
64     table_header($th);
65
66     /*show the line items on the order with the quantity being received for modification */
67
68     $total = 0;
69     $k = 0; //row colour counter
70
71     if (count($_SESSION['PO']->line_items)> 0 )
72     {
73         foreach ($_SESSION['PO']->line_items as $ln_itm)
74         {
75
76                         alt_table_row_color($k);
77
78                 $qty_outstanding = $ln_itm->quantity - $ln_itm->qty_received;
79
80                         if (!isset($_POST['Update']) && !isset($_POST['ProcessGoodsReceived']) && $ln_itm->receive_qty == 0)
81                 {   //If no quantites yet input default the balance to be received
82                 $ln_itm->receive_qty = $qty_outstanding;
83                 }
84
85                 $line_total = ($ln_itm->receive_qty * $ln_itm->price);
86                 $total += $line_total;
87
88                         label_cell($ln_itm->stock_id);
89                         if ($qty_outstanding > 0)
90                                 text_cells(null, $ln_itm->stock_id . "Desc", $ln_itm->item_description, 30, 50);
91                         else
92                                 label_cell($ln_itm->item_description);
93                         $dec = get_qty_dec($ln_itm->stock_id);
94                         qty_cell($ln_itm->quantity, false, $dec);
95                         label_cell($ln_itm->units);
96                         qty_cell($ln_itm->qty_received, false, $dec);
97                         qty_cell($qty_outstanding, false, $dec);
98
99                         if ($qty_outstanding > 0)
100                                 qty_cells(null, $ln_itm->line_no, number_format2($ln_itm->receive_qty, $dec), "align=right", null, $dec);
101                         else
102                                 label_cell(number_format2($ln_itm->receive_qty, $dec), "align=right");
103
104                         amount_decimal_cell($ln_itm->price);
105                         amount_cell($line_total);
106                         end_row();
107         }
108     }
109
110         $colspan = count($th)-1;
111
112         $display_sub_total = price_format($total/* + input_num('freight_cost')*/);
113
114         label_row(_("Sub-total"), $display_sub_total, "colspan=$colspan align=right","align=right");
115         $taxes = $_SESSION['PO']->get_taxes(input_num('freight_cost'), true);
116         
117         $tax_total = display_edit_tax_items($taxes, $colspan, $_SESSION['PO']->tax_included);
118
119         $display_total = price_format(($total + input_num('freight_cost') + $tax_total));
120
121         start_row();
122         label_cells(_("Amount Total"), $display_total, "colspan=$colspan align='right'","align='right'");
123         end_row();
124     end_table();
125         div_end();
126 }
127
128 //--------------------------------------------------------------------------------------------------
129
130 function check_po_changed()
131 {
132         /*Now need to check that the order details are the same as they were when they were read
133         into the Items array. If they've changed then someone else must have altered them */
134         // Sherifoz 22.06.03 Compare against COMPLETED items only !!
135         // Otherwise if you try to fullfill item quantities separately will give error.
136         $result = get_po_items($_SESSION['PO']->order_no);
137
138         $line_no = 0;
139         while ($myrow = db_fetch($result))
140         {
141                 $ln_item = $_SESSION['PO']->line_items[$line_no];
142                 // only compare against items that are outstanding
143                 $qty_outstanding = $ln_item->quantity - $ln_item->qty_received;
144                 if ($qty_outstanding > 0)
145                 {
146                 if ($ln_item->qty_inv != $myrow["qty_invoiced"] ||
147                         $ln_item->stock_id != $myrow["item_code"] ||
148                         $ln_item->quantity != $myrow["quantity_ordered"] ||
149                         $ln_item->qty_received != $myrow["quantity_received"])
150                 {
151                         return true;
152                 }
153                 }
154                 $line_no++;
155         } /*loop through all line items of the order to ensure none have been invoiced */
156
157         return false;
158 }
159
160 //--------------------------------------------------------------------------------------------------
161
162 function can_process()
163 {
164         global $SysPrefs, $Refs;
165         
166         if (count($_SESSION['PO']->line_items) <= 0)
167         {
168         display_error(_("There is nothing to process. Please enter valid quantities greater than zero."));
169         return false;
170         }
171
172         if (!is_date($_POST['DefaultReceivedDate']))
173         {
174                 display_error(_("The entered date is invalid."));
175                 set_focus('DefaultReceivedDate');
176                 return false;
177         }
178         if (!is_date_in_fiscalyear($_POST['DefaultReceivedDate'])) {
179                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
180                 set_focus('DefaultReceivedDate');
181                 return false;
182         }
183
184     if (!$Refs->is_valid($_POST['ref']))
185     {
186                 display_error(_("You must enter a reference."));
187                 set_focus('ref');
188                 return false;
189         }
190
191         if (!is_new_reference($_POST['ref'], ST_SUPPRECEIVE))
192         {
193                 display_error(_("The entered reference is already in use."));
194                 set_focus('ref');
195                 return false;
196         }
197
198         $something_received = 0;
199         foreach ($_SESSION['PO']->line_items as $order_line)
200         {
201                 if ($order_line->receive_qty > 0)
202                 {
203                         $something_received = 1;
204                         break;
205                 }
206         }
207
208     // Check whether trying to deliver more items than are recorded on the actual purchase order (+ overreceive allowance)
209     $delivery_qty_too_large = 0;
210         foreach ($_SESSION['PO']->line_items as $order_line)
211         {
212                 if ($order_line->receive_qty+$order_line->qty_received >
213                         $order_line->quantity * (1+ ($SysPrefs->over_receive_allowance() / 100)))
214                 {
215                         $delivery_qty_too_large = 1;
216                         break;
217                 }
218         }
219
220     if ($something_received == 0)
221     {   /*Then dont bother proceeding cos nothing to do ! */
222         display_error(_("There is nothing to process. Please enter valid quantities greater than zero."));
223         return false;
224     }
225     elseif ($delivery_qty_too_large == 1)
226     {
227         display_error(_("Entered quantities cannot be greater than the quantity entered on the purchase order including the allowed over-receive percentage") . " (" . $SysPrefs->over_receive_allowance() ."%)."
228                 . "<br>" .
229                 _("Modify the ordered items on the purchase order if you wish to increase the quantities."));
230         return false;
231     }
232
233         return true;
234 }
235
236 //--------------------------------------------------------------------------------------------------
237
238 function process_receive_po()
239 {
240         global $path_to_root, $Ajax;
241
242         if (!can_process())
243                 return;
244
245         if (check_po_changed())
246         {
247                 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."));
248
249                 hyperlink_no_params("$path_to_root/purchasing/inquiry/po_search.php",
250                  _("Select a different purchase order for receiving goods against"));
251
252                 hyperlink_params("$path_to_root/purchasing/po_receive_items.php", 
253                          _("Re-Read the updated purchase order for receiving goods against"),
254                          "PONumber=" . $_SESSION['PO']->order_no);
255
256                 unset($_SESSION['PO']->line_items);
257                 unset($_SESSION['PO']);
258                 unset($_POST['ProcessGoodsReceived']);
259                 $Ajax->activate('_page_body');
260                 display_footer_exit();
261         }
262         
263         $grn = &$_SESSION['PO'];
264         $grn->orig_order_date = $_POST['DefaultReceivedDate'];
265         $grn->reference = $_POST['ref'];
266         $grn->Location = $_POST['Location'];
267         $grn->ex_rate = input_num('_ex_rate', null);
268
269         $grn_no = add_grn($grn);
270
271         new_doc_date($_POST['DefaultReceivedDate']);
272         unset($_SESSION['PO']->line_items);
273         unset($_SESSION['PO']);
274
275         meta_forward($_SERVER['PHP_SELF'], "AddedID=$grn_no");
276 }
277
278 //--------------------------------------------------------------------------------------------------
279
280 if (isset($_GET['PONumber']) && $_GET['PONumber'] > 0 && !isset($_POST['Update']))
281 {
282         create_new_po(ST_PURCHORDER, $_GET['PONumber']);
283         $_SESSION['PO']->trans_type = ST_SUPPRECEIVE;
284         $_SESSION['PO']->reference = $Refs->get_next(ST_SUPPRECEIVE);
285         copy_from_cart();
286 }
287
288 //--------------------------------------------------------------------------------------------------
289
290 if (isset($_POST['Update']) || isset($_POST['ProcessGoodsReceived']))
291 {
292
293         /* if update quantities button is hit page has been called and ${$line->line_no} would have be
294         set from the post to the quantity to be received in this receival*/
295         foreach ($_SESSION['PO']->line_items as $line)
296         {
297          if( ($line->quantity - $line->qty_received)>0) {
298                 $_POST[$line->line_no] = max($_POST[$line->line_no], 0);
299                 if (!check_num($line->line_no))
300                         $_POST[$line->line_no] = number_format2(0, get_qty_dec($line->stock_id));
301
302                 if (!isset($_POST['DefaultReceivedDate']) || $_POST['DefaultReceivedDate'] == "")
303                         $_POST['DefaultReceivedDate'] = new_doc_date();
304
305                 $_SESSION['PO']->line_items[$line->line_no]->receive_qty = input_num($line->line_no);
306
307                 if (isset($_POST[$line->stock_id . "Desc"]) && strlen($_POST[$line->stock_id . "Desc"]) > 0)
308                 {
309                         $_SESSION['PO']->line_items[$line->line_no]->item_description = $_POST[$line->stock_id . "Desc"];
310                 }
311          }
312         }
313         $Ajax->activate('grn_items');
314 }
315
316 //--------------------------------------------------------------------------------------------------
317
318 if (isset($_POST['ProcessGoodsReceived']))
319 {
320         process_receive_po();
321 }
322
323 //--------------------------------------------------------------------------------------------------
324
325 start_form();
326
327 edit_grn_summary($_SESSION['PO'], true);
328 display_heading(_("Items to Receive"));
329 display_po_receive_items();
330
331 echo '<br>';
332 submit_center_first('Update', _("Update"), '', true);
333 submit_center_last('ProcessGoodsReceived', _("Process Receive Items"), _("Clear all GL entry fields"), 'default');
334
335 end_form();
336
337 //--------------------------------------------------------------------------------------------------
338 end_page();
339 ?>
340