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