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