Annoyance 5683: Allow attachments from most needed post-creation option screens.
[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 ($SysPrefs->use_popup_windows)
22         $js .= get_js_open_window(900, 500);
23 if (user_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         hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), 
47                 "filterType=$trans_type&trans_no=$grn");
48
49         display_footer_exit();
50 }
51
52 //--------------------------------------------------------------------------------------------------
53
54 if ((!isset($_GET['PONumber']) || $_GET['PONumber'] == 0) && !isset($_SESSION['PO']))
55 {
56         die (_("This page can only be opened if a purchase order has been selected. Please select a purchase order first."));
57 }
58
59 //--------------------------------------------------------------------------------------------------
60
61 function display_po_receive_items()
62 {
63         div_start('grn_items');
64     start_table(TABLESTYLE, "colspan=7 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         $colspan = count($th)-1;
114
115         $display_sub_total = price_format($total/* + input_num('freight_cost')*/);
116
117         label_row(_("Sub-total"), $display_sub_total, "colspan=$colspan align=right","align=right");
118         $taxes = $_SESSION['PO']->get_taxes(input_num('freight_cost'), true);
119         
120         $tax_total = display_edit_tax_items($taxes, $colspan, $_SESSION['PO']->tax_included);
121
122         $display_total = price_format(($total + input_num('freight_cost') + $tax_total));
123
124         start_row();
125         label_cells(_("Amount Total"), $display_total, "colspan=$colspan align='right'","align='right'");
126         end_row();
127     end_table();
128         div_end();
129 }
130
131 //--------------------------------------------------------------------------------------------------
132
133 function check_po_changed()
134 {
135         /*Now need to check that the order details are the same as they were when they were read
136         into the Items array. If they've changed then someone else must have altered them */
137         // Compare against COMPLETED items only !!
138         // Otherwise if you try to fullfill item quantities separately will give error.
139         $result = get_po_items($_SESSION['PO']->order_no);
140
141         $line_no = 0;
142         while ($myrow = db_fetch($result))
143         {
144                 $ln_item = $_SESSION['PO']->line_items[$line_no];
145                 // only compare against items that are outstanding
146                 $qty_outstanding = $ln_item->quantity - $ln_item->qty_received;
147                 if ($qty_outstanding > 0)
148                 {
149                 if ($ln_item->qty_inv != $myrow["qty_invoiced"] ||
150                         $ln_item->stock_id != $myrow["item_code"] ||
151                         $ln_item->quantity != $myrow["quantity_ordered"] ||
152                         $ln_item->qty_received != $myrow["quantity_received"])
153                 {
154                         return true;
155                 }
156                 }
157                 $line_no++;
158         } /*loop through all line items of the order to ensure none have been invoiced */
159
160         return false;
161 }
162
163 //--------------------------------------------------------------------------------------------------
164
165 function can_process()
166 {
167         global $SysPrefs;
168         
169         if (count($_SESSION['PO']->line_items) <= 0)
170         {
171         display_error(_("There is nothing to process. Please enter valid quantities greater than zero."));
172         return false;
173         }
174
175         if (!is_date($_POST['DefaultReceivedDate']))
176         {
177                 display_error(_("The entered date is invalid."));
178                 set_focus('DefaultReceivedDate');
179                 return false;
180         }
181         if (!is_date_in_fiscalyear($_POST['DefaultReceivedDate'])) {
182                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
183                 set_focus('DefaultReceivedDate');
184                 return false;
185         }
186
187         if (!check_reference($_POST['ref'], ST_SUPPRECEIVE))
188         {
189                 set_focus('ref');
190                 return false;
191         }
192
193         $something_received = 0;
194         foreach ($_SESSION['PO']->line_items as $order_line)
195         {
196                 if ($order_line->receive_qty > 0)
197                 {
198                         $something_received = 1;
199                         break;
200                 }
201         }
202
203     // Check whether trying to deliver more items than are recorded on the actual purchase order (+ overreceive allowance)
204     $delivery_qty_too_large = 0;
205         foreach ($_SESSION['PO']->line_items as $order_line)
206         {
207                 if ($order_line->receive_qty+$order_line->qty_received >
208                         $order_line->quantity * (1+ ($SysPrefs->over_receive_allowance() / 100)))
209                 {
210                         $delivery_qty_too_large = 1;
211                         break;
212                 }
213         }
214
215     if ($something_received == 0)
216     {   /*Then dont bother proceeding cos nothing to do ! */
217         display_error(_("There is nothing to process. Please enter valid quantities greater than zero."));
218         return false;
219     }
220     elseif ($delivery_qty_too_large == 1)
221     {
222         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() ."%)."
223                 . "<br>" .
224                 _("Modify the ordered items on the purchase order if you wish to increase the quantities."));
225         return false;
226     }
227
228         return true;
229 }
230
231 //--------------------------------------------------------------------------------------------------
232
233 function process_receive_po()
234 {
235         global $path_to_root, $Ajax;
236
237         if (!can_process())
238                 return;
239
240         if (check_po_changed())
241         {
242                 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."));
243
244                 hyperlink_no_params("$path_to_root/purchasing/inquiry/po_search.php",
245                  _("Select a different purchase order for receiving goods against"));
246
247                 hyperlink_params("$path_to_root/purchasing/po_receive_items.php", 
248                          _("Re-Read the updated purchase order for receiving goods against"),
249                          "PONumber=" . $_SESSION['PO']->order_no);
250
251                 unset($_SESSION['PO']->line_items);
252                 unset($_SESSION['PO']);
253                 unset($_POST['ProcessGoodsReceived']);
254                 $Ajax->activate('_page_body');
255                 display_footer_exit();
256         }
257         
258         $grn = &$_SESSION['PO'];
259         $grn->orig_order_date = $_POST['DefaultReceivedDate'];
260         $grn->reference = $_POST['ref'];
261         $grn->Location = $_POST['Location'];
262         $grn->ex_rate = input_num('_ex_rate', null);
263
264         $grn_no = add_grn($grn);
265
266         new_doc_date($_POST['DefaultReceivedDate']);
267         unset($_SESSION['PO']->line_items);
268         unset($_SESSION['PO']);
269
270         meta_forward($_SERVER['PHP_SELF'], "AddedID=$grn_no");
271 }
272
273 //--------------------------------------------------------------------------------------------------
274
275 if (isset($_GET['PONumber']) && $_GET['PONumber'] > 0 && !isset($_POST['Update']))
276 {
277         create_new_po(ST_PURCHORDER, $_GET['PONumber']);
278         $_SESSION['PO']->trans_type = ST_SUPPRECEIVE;
279         $_SESSION['PO']->reference = $Refs->get_next(ST_SUPPRECEIVE, null,
280                 array('date' => Today(), 'supplier' => $_SESSION['PO']->supplier_id));
281         copy_from_cart();
282 }
283
284 //--------------------------------------------------------------------------------------------------
285
286 if (isset($_POST['Update']) || isset($_POST['ProcessGoodsReceived']))
287 {
288
289         /* if update quantities button is hit page has been called and ${$line->line_no} would have be
290         set from the post to the quantity to be received in this receival*/
291         foreach ($_SESSION['PO']->line_items as $line)
292         {
293          if( ($line->quantity - $line->qty_received)>0) {
294                 $_POST[$line->line_no] = max($_POST[$line->line_no], 0);
295                 if (!check_num($line->line_no))
296                         $_POST[$line->line_no] = number_format2(0, get_qty_dec($line->stock_id));
297
298                 if (!isset($_POST['DefaultReceivedDate']) || $_POST['DefaultReceivedDate'] == "")
299                         $_POST['DefaultReceivedDate'] = new_doc_date();
300
301                 $_SESSION['PO']->line_items[$line->line_no]->receive_qty = input_num($line->line_no);
302
303                 if (isset($_POST[$line->stock_id . "Desc"]) && strlen($_POST[$line->stock_id . "Desc"]) > 0)
304                 {
305                         $_SESSION['PO']->line_items[$line->line_no]->item_description = $_POST[$line->stock_id . "Desc"];
306                 }
307          }
308         }
309         $Ajax->activate('grn_items');
310 }
311
312 //--------------------------------------------------------------------------------------------------
313
314 if (isset($_POST['ProcessGoodsReceived']))
315 {
316         process_receive_po();
317 }
318
319 //--------------------------------------------------------------------------------------------------
320
321 start_form();
322
323 edit_grn_summary($_SESSION['PO'], true);
324 display_heading(_("Items to Receive"));
325 display_po_receive_items();
326
327 echo '<br>';
328 submit_center_first('Update', _("Update"), '', true);
329 submit_center_last('ProcessGoodsReceived', _("Process Receive Items"), _("Clear all GL entry fields"), 'default');
330
331 end_form();
332
333 //--------------------------------------------------------------------------------------------------
334 end_page();