Merged bugfixes upto [0000072] (version 2.0.3).
[fa-stable.git] / purchasing / supplier_invoice_grns.php
1 <?php
2
3 $page_security = 5;
4 $path_to_root="..";
5
6 include_once($path_to_root . "/purchasing/includes/supp_trans_class.inc");
7 include_once($path_to_root . "/includes/session.inc");
8 include_once($path_to_root . "/purchasing/includes/purchasing_ui.inc");
9 include_once($path_to_root . "/purchasing/includes/purchasing_db.inc");
10
11 $js = "";
12 if ($use_date_picker)
13         $js .= get_js_date_picker();
14 if ($use_popup_windows)
15         $js .= get_js_open_window(900, 500);
16 page(_("Select Received Items to Add"), false, false, "", $js);
17
18 if (!isset($_SESSION['supp_trans']))
19 {
20         display_note("To enter supplier transactions the supplier must first be selected from the supplier selection screen, then the link to enter a supplier credit note must be clicked on.", 1, 0);;
21         exit;
22 }
23
24 //-----------------------------------------------------------------------------------------
25
26 display_heading($_SESSION['supp_trans']->supplier_name);
27
28 echo "<br>";
29
30 //-----------------------------------------------------------------------------------------
31
32 function check_data()
33 {
34         global $check_price_charged_vs_order_price,
35                 $check_qty_charged_vs_del_qty;
36         if (!check_num('this_quantity_inv', 0) || input_num('this_quantity_inv')==0)
37         {
38                 display_error( _("The quantity to invoice must be numeric and greater than zero."));
39                 set_focus('this_quantity_inv');
40                 return false;
41         }
42
43         if (!check_num('ChgPrice'))
44         {
45                 display_error( _("The price is not numeric."));
46                 set_focus('ChgPrice');
47                 return false;
48         }
49
50         if ($check_price_charged_vs_order_price == True)
51         {
52                 if ($_POST['order_price']!=input_num('ChgPrice')) {
53                      if ($_POST['order_price']==0 ||
54                         input_num('ChgPrice')/$_POST['order_price'] >
55                             (1 + (sys_prefs::over_charge_allowance() / 100)))
56                     {
57                         display_error(_("The price being invoiced is more than the purchase order price by more than the allowed over-charge percentage. The system is set up to prohibit this. See the system administrator to modify the set up parameters if necessary.") .
58                         _("The over-charge percentage allowance is :") . sys_prefs::over_charge_allowance() . "%");
59                         set_focus('ChgPrice');
60                         return false;
61                     }
62                 }
63         }
64
65         if ($check_qty_charged_vs_del_qty == True)
66         {
67                 if (input_num('this_quantity_inv') / ($_POST['qty_recd'] - $_POST['prev_quantity_inv']) >
68                         (1+ (sys_prefs::over_charge_allowance() / 100)))
69                 {
70                         display_error( _("The quantity being invoiced is more than the outstanding quantity by more than the allowed over-charge percentage. The system is set up to prohibit this. See the system administrator to modify the set up parameters if necessary.")
71                         . _("The over-charge percentage allowance is :") . sys_prefs::over_charge_allowance() . "%");
72                         set_focus('this_quantity_inv');
73                         return false;
74                 }
75         }
76
77         return true;
78 }
79
80 //-----------------------------------------------------------------------------------------
81
82 if (isset($_POST['AddGRNToTrans']))
83 {
84
85         if (check_data())
86         {
87         if (input_num('this_quantity_inv') >= ($_POST['qty_recd'] - $_POST['prev_quantity_inv']))
88         {
89                 $complete = true;
90         }
91         else
92         {
93                 $complete = false;
94         }
95
96                 $_SESSION['supp_trans']->add_grn_to_trans($_POST['GRNNumber'], $_POST['po_detail_item'],
97                         $_POST['item_code'], $_POST['item_description'], $_POST['qty_recd'],
98                         $_POST['prev_quantity_inv'], input_num('this_quantity_inv'),
99                         $_POST['order_price'], input_num('ChgPrice'), $complete,
100                         $_POST['std_cost_unit'], "");
101         }
102 }
103
104 //-----------------------------------------------------------------------------------------
105 $id = find_submit('Delete');
106 if ($id != -1)
107 {
108         $_SESSION['supp_trans']->remove_grn_from_trans($id);
109         $Ajax->activate('grn_items');
110         $Ajax->activate('grn_table');
111 }
112
113 //-----------------------------------------------------------------------------------------
114 start_form(false,true);
115 display_grn_items($_SESSION['supp_trans'], 1);
116 end_form();
117 echo "<br>";
118
119 hyperlink_no_params("$path_to_root/purchasing/supplier_invoice.php", _("Back to Supplier Invoice Entry"));
120 echo "<hr>";
121
122 //-----------------------------------------------------------------------------------------
123
124 function display_grn_items_for_selection()
125 {
126         global $table_style;
127
128         $result = get_grn_items(0, $_SESSION['supp_trans']->supplier_id, true);
129
130     if (db_num_rows($result) == 0)
131     {
132         display_note(_("There are no outstanding items received from this supplier that have not been invoiced by them."), 0, 1);
133         end_page();
134         exit;
135     }
136
137     /*Set up a table to show the outstanding GRN items for selection */
138     start_form(false, true);
139
140     display_heading2(_("Items Received Yet to be Invoiced"));
141         div_start('grn_table');
142     start_table("$table_style colspan=7 width=95%");
143     $th = array(_("Delivery"), _("Sequence #"), _("P.O."), _("Item"), _("Description"),
144         _("Received On"), _("Quantity Received"), _("Quantity Invoiced"),
145         _("Uninvoiced Quantity"), _("Order Price"), _("Total"));
146     table_header($th);
147     $i = $k = 0;
148
149     while ($myrow = db_fetch($result))
150     {
151                 $grn_already_on_invoice = false;
152
153         foreach ($_SESSION['supp_trans']->grn_items as $entered_grn)
154         {
155                 if ($entered_grn->id == $myrow["id"])
156                 {
157                         $grn_already_on_invoice = true;
158                 }
159         }
160         if ($grn_already_on_invoice == false)
161         {
162
163                         alt_table_row_color($k);
164
165                 label_cell(get_trans_view_str(25, $myrow["grn_batch_id"]));
166                 //text_cells(null, 'grn_item_id', $myrow["id"]);
167                 submit_cells('grn_item_id'.$myrow["id"], $myrow["id"], '', '', true);
168                 label_cell(get_trans_view_str(systypes::po(), $myrow["purch_order_no"]));
169             label_cell($myrow["item_code"]);
170             label_cell($myrow["description"]);
171             label_cell(sql2date($myrow["delivery_date"]));
172             $dec = get_qty_dec($myrow["item_code"]);
173             qty_cell($myrow["qty_recd"], false, $dec);
174             qty_cell($myrow["quantity_inv"], false, $dec);
175             qty_cell($myrow["qty_recd"] - $myrow["quantity_inv"], false, $dec);
176             amount_cell($myrow["unit_price"]);
177             amount_cell(round($myrow["unit_price"] * ($myrow["qty_recd"] - $myrow["quantity_inv"]),
178                            user_price_dec()));
179                         end_row();
180
181                 $i++;
182                 if ($i > 15)
183                 {
184                         $i = 0;
185                         table_header($th);
186                 }
187         }
188     }
189
190     end_table();
191         div_end();
192 }
193
194 //-----------------------------------------------------------------------------------------
195 if (find_submit('grn_item_id') || get_post('AddGRNToTrans'))
196 {
197         $Ajax->activate('grn_selector');
198 }
199 if (get_post('AddGRNToTrans'))
200 {
201         $Ajax->activate('grn_table');
202         $Ajax->activate('grn_items');
203 }
204
205 display_grn_items_for_selection();
206
207 //-----------------------------------------------------------------------------------------
208
209 div_start('grn_selector');
210
211 $id = find_submit('grn_item_id');
212 if ($id != -1)
213 {
214
215         $myrow = get_grn_item_detail($id);
216
217         echo "<br>";
218         display_heading2(_("Delivery Item Selected For Adding To A Supplier Invoice"));
219         start_table("$table_style width=80%");
220         $th = array(_("Sequence #"), _("Item"), _("Description"), _("Quantity Outstanding"),
221                 _("Quantity to Invoice"), _("Order Price"), _("Actual Price"));
222         table_header($th);
223
224         start_row();
225         label_cell($id);
226         label_cell($myrow['item_code']);
227         label_cell($myrow['description']);
228         $dec = get_qty_dec($myrow['item_code']);
229         qty_cell($myrow['QtyOstdg'], false, $dec);
230         qty_cells(null, 'this_quantity_inv', number_format2($myrow['QtyOstdg'], $dec), null, null, $dec);
231         amount_cell($myrow['unit_price']);
232         small_amount_cells(null, 'ChgPrice', price_format($myrow['unit_price']));
233         end_row();
234         end_table(1);;
235
236         submit_center('AddGRNToTrans', _("Add to Invoice"), true, '', true);
237
238         hidden('GRNNumber', $id);
239         hidden('item_code', $myrow['item_code']);
240         hidden('item_description', $myrow['description']);;
241         hidden('qty_recd', $myrow['qty_recd']);
242         hidden('prev_quantity_inv', $myrow['quantity_inv']);
243         hidden('order_price', $myrow['unit_price']);
244         hidden('std_cost_unit', $myrow['std_cost_unit']);
245
246         hidden('po_detail_item', $myrow['po_detail_item']);
247 }
248 div_end();
249
250 //----------------------------------------------------------------------------------------
251
252 end_form();
253 echo '<br>';
254 end_page(false, true);
255
256 ?>