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