Added Direct GRN and Direct invice in Purchases module.
[fa-stable.git] / purchasing / includes / ui / po_ui.inc
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 include_once($path_to_root . "/purchasing/includes/purchasing_db.inc");
13
14 //--------------------------------------------------------------------------------------------------
15
16 function copy_from_cart()
17 {
18         $cart = &$_SESSION['PO'];
19
20         $_POST['supplier_id'] = $cart->supplier_id;
21         $_POST['OrderDate'] = $cart->orig_order_date;
22     $_POST['supp_ref'] = $cart->supp_ref;
23     $_POST['ref'] = $cart->reference;
24         $_POST['Comments'] = $cart->Comments;
25     $_POST['StkLocation'] = $cart->Location;
26     $_POST['delivery_address'] = $cart->delivery_address;
27 }
28
29 function copy_to_cart()
30 {
31         $cart = &$_SESSION['PO'];
32
33         $cart->supplier_id = $_POST['supplier_id'];     
34         $cart->orig_order_date = $_POST['OrderDate'];
35         $cart->reference = $_POST['ref'];
36         $cart->supp_ref = $_POST['supp_ref'];
37         $cart->Comments = $_POST['Comments'];   
38         $cart->Location = $_POST['StkLocation'];
39         $cart->delivery_address = $_POST['delivery_address'];
40 }
41 // ------------------------------------------------------------------------------
42
43 function get_supplier_details_to_order(&$order, $supplier_id)
44 {
45         $sql = "SELECT curr_code,supp_name FROM ".TB_PREF."suppliers
46                 WHERE supplier_id = '$supplier_id'";
47         $result = db_query($sql, "The supplier details could not be retreived");
48
49         $myrow = db_fetch($result);
50
51         $order->curr_code = $_POST['curr_code'] = $myrow["curr_code"];
52         $order->supplier_name = $_POST['supplier_name'] = $myrow["supp_name"];
53         $order->supplier_id = $_POST['supplier_id'] = $supplier_id;
54 }
55
56 //---------------------------------------------------------------------------------------------------
57
58 function create_new_po($trans_type, $trans_no)
59 {
60         global $Refs;
61
62         if (isset($_SESSION['PO']))
63                 unset ($_SESSION['PO']->line_items, $_SESSION['PO']);
64
65         $cart = new purch_order;
66         $_POST['OrderDate'] = new_doc_date();
67         if (!is_date_in_fiscalyear($_POST['OrderDate']))
68                 $_POST['OrderDate'] = end_fiscalyear();
69         $cart->orig_order_date = $_POST['OrderDate'];
70
71         $cart->trans_type = $trans_type;
72         $cart->order_no = $trans_no;
73         /*read in all the selected order into the Items cart  */
74         if ($trans_no) {
75                 read_po($trans_no, $cart);
76                 $cart->order_no = $trans_no;
77         } else
78                 $cart->reference = $Refs->get_next($trans_type);
79         $_SESSION['PO'] = &$cart;
80 }
81
82 //---------------------------------------------------------------------------------------------------
83
84 function display_po_header(&$order)
85 {
86         global $table_style2, $Ajax, $Refs;
87
88         $editable = ($order->order_no == 0);
89
90         start_outer_table("width=80% $table_style2");
91
92         table_section(1);
93     if ($editable)
94     {
95         if (!isset($_POST['supplier_id']) && (get_global_supplier() != ALL_TEXT))
96                 $_POST['supplier_id'] = get_global_supplier();
97
98         supplier_list_row(_("Supplier:"), 'supplier_id', null, false, true, false, true);
99         }
100         else
101         {
102                 hidden('supplier_id', $order->supplier_id);
103                 label_row(_("Supplier:"), $order->supplier_name);
104     }
105
106         if ($order->supplier_id != get_post('supplier_id',-1)) {
107                 get_supplier_details_to_order($order, $_POST['supplier_id']);
108                 // supplier default price update
109                 foreach ($order->line_items as $line_no=>$item) {
110                         $line = &$order->line_items[$line_no];
111                         $line->price =  get_purchase_price ($order->supplier_id, $_POST['stock_id']);
112                         $line->quantity =  get_purchase_conversion_factor ($order->supplier_id, $_POST['stock_id']);
113                 }
114             $Ajax->activate('items_table');
115         }
116         set_global_supplier($_POST['supplier_id']);
117
118         if (!is_company_currency($order->curr_code))
119         {
120                 label_row(_("Supplier Currency:"), $order->curr_code);
121                 exchange_rate_display($order->curr_code, get_company_currency(),
122                         $_POST['OrderDate']);
123         }
124
125     if ($editable)
126     {
127         ref_row(_("Reference:"), 'ref');
128     }
129     else
130     {
131         hidden('ref', $order->reference);
132         label_row(_("Reference:"), $order->reference);
133     }
134
135         table_section(2);
136
137         // check this out?????????
138         //if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
139         //      $_POST['OrderDate'] = $order->orig_order_date;
140         //if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
141         //{
142         //      $_POST['OrderDate'] = Today();
143         //      if (!is_date_in_fiscalyear($_POST['OrderDate']))
144         //              $_POST['OrderDate'] = end_fiscalyear();
145         //}
146         date_row(_("Order Date:"), 'OrderDate', '', true, 0, 0, 0, null, true);
147         if (isset($_POST['_OrderDate_changed'])) {
148                 $Ajax->activate('_ex_rate');
149         }
150
151         text_row(_("Supplier's Reference:"), 'supp_ref', null, 16, 15);
152
153         echo "<tr><td>" . _("Receive Into:") . "</td>";
154         echo "<td>";
155     echo locations_list('StkLocation', null, false, true);
156         echo "</td></tr>";
157
158         table_section(3);
159
160     if (!isset($_POST['StkLocation']) || $_POST['StkLocation'] == "" ||
161         isset($_POST['_StkLocation_update']) || !isset($_POST['delivery_address']) ||
162         $_POST['delivery_address'] == "")
163     {
164         /*If this is the first time the form loaded set up defaults */
165
166         //$_POST['StkLocation'] = $_SESSION['UserStockLocation'];
167         $sql = "SELECT delivery_address, phone FROM ".TB_PREF."locations WHERE loc_code='" . $_POST['StkLocation'] . "'";
168         $result = db_query($sql,"could not get location info");
169
170         if (db_num_rows($result) == 1)
171         {
172                 $loc_row = db_fetch($result);
173                 $_POST['delivery_address'] = $loc_row["delivery_address"];
174                         $Ajax->activate('delivery_address');
175                 $_SESSION['PO']->Location = $_POST['StkLocation'];
176                 $_SESSION['PO']->delivery_address = $_POST['delivery_address'];
177
178         }
179         else
180         { /*The default location of the user is crook */
181                 display_error(_("The default stock location set up for this user is not a currently defined stock location. Your system administrator needs to amend your user record."));
182         }
183     }
184
185         textarea_row(_("Deliver to:"), 'delivery_address', $_POST['delivery_address'], 35, 4);
186
187         end_outer_table(); // outer table
188 }
189
190 //---------------------------------------------------------------------------------------------------
191
192 function display_po_items(&$order, $editable=true)
193 {
194         global $table_style;
195
196     display_heading(_("Order Items"));
197
198     div_start('items_table');
199     start_table("$table_style width=80%");
200
201         $th = array(_("Item Code"), _("Item Description"), _("Quantity"),
202                 _("Received"), _("Unit"),
203                 _("Required Delivery Date"), _("Price"), _("Line Total"), "");
204         if ($order->trans_type != ST_PURCHORDER)
205                 array_remove($th, 5);
206                 
207         if (count($order->line_items)) $th[] = '';
208         table_header($th);
209
210         $id = find_submit('Edit');
211         $total = 0;
212         $k = 0;
213         foreach ($order->line_items as $line_no => $po_line)
214         {
215         $line_total =   round($po_line->quantity * $po_line->price,  user_price_dec());
216         if (!$editable || ($id != $line_no))
217                 {
218                 alt_table_row_color($k);
219                 label_cell($po_line->stock_id);
220                 label_cell($po_line->item_description);
221             qty_cell($po_line->quantity, false, get_qty_dec($po_line->stock_id));
222             qty_cell($po_line->qty_received, false, get_qty_dec($po_line->stock_id));
223                 label_cell($po_line->units);
224                         if ($order->trans_type == ST_PURCHORDER)
225                     label_cell($po_line->req_del_date);
226                 amount_decimal_cell($po_line->price);
227             amount_cell($line_total);
228
229                         if ($editable)
230                         {
231                                         edit_button_cell("Edit$line_no", _("Edit"),
232                                           _('Edit document line'));
233                                         delete_button_cell("Delete$line_no", _("Delete"),
234                                                 _('Remove line from document'));
235                         }
236                 end_row();
237                 }
238                 else
239                 {
240                         po_item_controls($order, $k, $line_no);
241                 }
242                 $total += $line_total;
243     }
244
245         if ($id==-1 && $editable)
246                 po_item_controls($order, $k);
247
248     $display_total = price_format($total);
249     label_row(_("Total Excluding Shipping/Tax"), $display_total, "colspan="
250         .(count($order->line_items) ? 7:6)." align=right",
251         "nowrap align=right", 2);
252
253         end_table(1);
254         div_end();
255 }
256
257 //---------------------------------------------------------------------------------------------------
258
259 function display_po_summary(&$po, $is_self=false, $editable=false)
260 {
261         global $table_style2;
262     start_table("$table_style2 width=90%");
263
264     start_row();
265     label_cells(_("Reference"), $po->reference, "class='tableheader2'");
266
267     label_cells(_("Supplier"), $po->supplier_name, "class='tableheader2'");
268
269     if (!is_company_currency($po->curr_code))
270         label_cells(_("Order Currency"), $po->curr_code, "class='tableheader2'");
271
272     if (!$is_self)
273     {
274         label_cells(_("Purchase Order"), get_trans_view_str(ST_PURCHORDER, $po->order_no),
275                 "class='tableheader2'");
276     }
277         end_row();
278         start_row();
279     label_cells(_("Date"), $po->orig_order_date, "class='tableheader2'");
280
281     if ($editable)
282     {
283         if (!isset($_POST['Location']))
284                 $_POST['Location'] = $po->Location;
285         label_cell(_("Deliver Into Location"), "class='tableheader2'");
286         locations_list_cells(null, 'Location', $_POST['Location']);
287     }
288     else
289     {
290         label_cells(_("Deliver Into Location"), get_location_name($po->Location),
291                 "class='tableheader2'");
292     }
293
294     if ($po->supp_ref != "")
295         label_cells(_("Supplier's Reference"), $po->supp_ref, "class='tableheader2'");
296     end_row();
297
298     if (!$editable)
299         label_row(_("Delivery Address"), $po->delivery_address, "class='tableheader2'",
300                 "colspan=9");
301
302     if ($po->Comments != "")
303         label_row(_("Order Comments"), $po->Comments, "class='tableheader2'",
304                 "colspan=9");
305     end_table(1);
306 }
307
308 //--------------------------------------------------------------------------------
309
310 function po_item_controls(&$order, &$rowcounter, $line_no=-1)
311 {
312    global $Ajax;
313
314         alt_table_row_color($rowcounter);
315
316         $dec2 = 0;
317         $id = find_submit('Edit');
318         if (($id != -1) && $line_no == $id)
319         {
320 //              hidden('line_no', $id);
321
322                 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
323                 $dec = get_qty_dec($_POST['stock_id']);
324                 $_POST['qty'] = qty_format($order->line_items[$id]->quantity, $_POST['stock_id'], $dec);
325                 //$_POST['price'] = price_format($order->line_items[$id]->price);
326                 $_POST['price'] = price_decimal_format($order->line_items[$id]->price, $dec2);
327                 if ($order->trans_type == ST_PURCHORDER)
328                         $_POST['req_del_date'] = $order->line_items[$id]->req_del_date;
329
330                 $_POST['units'] = $order->line_items[$id]->units;
331                 $_POST['item_description'] = $order->line_items[$id]->item_description;
332
333                 hidden('stock_id', $_POST['stock_id']);
334                 label_cell($_POST['stock_id']);
335
336                 if ($order->line_items[$id]->descr_editable)
337                         text_cells(null,'item_description', null, 45, 150);
338                 else {
339                         hidden('item_description', $_POST['item_description']);
340 //                      label_cell($_POST['item_description']);
341                         label_cell($order->line_items[$id]->item_description); 
342                 }
343
344             $Ajax->activate('items_table');
345                 $qty_rcvd = $order->line_items[$id]->qty_received;
346         }
347         else
348         {
349 //              hidden('line_no', ($_SESSION['PO']->lines_on_order + 1));
350
351                 stock_purchasable_items_list_cells(null, 'stock_id', null, false, true, true);
352                 if (list_updated('stock_id')) {
353                             $Ajax->activate('price');
354                             $Ajax->activate('units');
355                             $Ajax->activate('qty');
356                             $Ajax->activate('req_del_date');
357                             $Ajax->activate('line_total');
358                 }
359         $item_info = get_item_edit_info($_POST['stock_id']);
360                 $_POST['units'] = $item_info["units"];
361
362                 $dec = $item_info["decimals"];
363                 $_POST['qty'] = number_format2(get_purchase_conversion_factor ($order->supplier_id, $_POST['stock_id']), $dec);
364                 //$_POST['price'] = price_format(get_purchase_price ($order->supplier_id, $_POST['stock_id']));
365                 $_POST['price'] = price_decimal_format(get_purchase_price ($order->supplier_id, $_POST['stock_id']), $dec2);
366                 if ($order->trans_type == ST_PURCHORDER)
367                         $_POST['req_del_date'] = add_days(Today(), 10);
368                 $qty_rcvd = '';
369         }
370
371         qty_cells(null, 'qty', null, null, null, $dec);
372         qty_cell($qty_rcvd, false, $dec);
373
374         label_cell($_POST['units'], '', 'units');
375         if ($order->trans_type == ST_PURCHORDER)
376                 date_cells(null, 'req_del_date', '', null, 0, 0, 0);
377         amount_cells(null, 'price', null, null, null, $dec2);
378
379         //$line_total = $_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc'] / 100);
380         $line_total = round(input_num('qty') * input_num('price'),  user_price_dec());
381         amount_cell($line_total, false, '','line_total');
382
383         if ($id!=-1)
384         {
385                 button_cell('UpdateLine', _("Update"),
386                                 _('Confirm changes'), ICON_UPDATE);
387                 button_cell('CancelUpdate', _("Cancel"),
388                                 _('Cancel changes'), ICON_CANCEL);
389                 hidden('line_no', $line_no);
390                 set_focus('qty');
391         }
392         else
393         {
394                 submit_cells('EnterLine', _("Add Item"), "colspan=2",
395                     _('Add new item to document'), true);
396         }
397
398         end_row();
399 }
400
401 //---------------------------------------------------------------------------------------------------
402
403
404
405 ?>