1484b91c142570cdd4cb748047647879a14b383b
[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, null, true);
108         if (isset($_POST['_OrderDate_changed'])) {
109                 $Ajax->activate('_ex_rate');
110         }
111
112         text_row(_("Supplier's Reference:"), 'Requisition', null, 16, 15);
113
114         echo "</table>";
115
116         echo "</td><td valign=center>"; // outer table
117
118         echo "<table height='5'>";
119
120         echo "<tr><td>" . _("Receive Into:") . "</td>";
121         echo "<td>";
122     locations_list('StkLocation', null, false, true);
123         echo "</td></tr>";
124
125     if (!isset($_POST['StkLocation']) || $_POST['StkLocation'] == "" ||
126         isset($_POST['_StkLocation_update']) || !isset($_POST['delivery_address']) ||
127         $_POST['delivery_address'] == "")
128     {
129         /*If this is the first time the form loaded set up defaults */
130
131         //$_POST['StkLocation'] = $_SESSION['UserStockLocation'];
132         $sql = "SELECT delivery_address, phone FROM ".TB_PREF."locations WHERE loc_code='" . $_POST['StkLocation'] . "'";
133         $result = db_query($sql,"could not get location info");
134
135         if (db_num_rows($result) == 1)
136         {
137                 $loc_row = db_fetch($result);
138                 $_POST['delivery_address'] = $loc_row["delivery_address"];
139                         $Ajax->activate('delivery_address');
140                 $_SESSION['PO']->Location = $_POST['StkLocation'];
141                 $_SESSION['PO']->delivery_address = $_POST['delivery_address'];
142
143         }
144         else
145         { /*The default location of the user is crook */
146                 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."));
147         }
148     }
149
150         textarea_row(_("Deliver to:"), 'delivery_address', $_POST['delivery_address'], 35, 4);
151         echo "</table>";
152
153         echo "</td></tr>";
154         end_table(); // outer table
155 }
156
157 //---------------------------------------------------------------------------------------------------
158
159 function display_po_items(&$order, $editable=true)
160 {
161         global $table_style;
162
163     display_heading(_("Order Items"));
164
165     div_start('items_table');
166     start_table("$table_style width=80%");
167
168         $th = array(_("Item Code"), _("Item Description"), _("Quantity"), _("Unit"),
169                 _("Required Delivery Date"), _("Price"), _("Line Total"));
170
171         if (count($order->line_items)) $th[] = '';
172         table_header($th);
173
174         $id = find_submit('Edit');
175         $total = 0;
176         $k = 0;
177         foreach ($order->line_items as $line_no => $po_line)
178         {
179
180                 if ($po_line->Deleted == false)
181                 {
182                 $line_total =   round($po_line->quantity * $po_line->price,  user_price_dec());
183                 if (!$editable || ($id != $line_no))
184                         {
185                         alt_table_row_color($k);
186                         label_cell($po_line->stock_id);
187                         label_cell($po_line->item_description);
188                 qty_cell($po_line->quantity, false, get_qty_dec($po_line->stock_id));
189                         label_cell($po_line->units);
190                 label_cell($po_line->req_del_date);
191                         amount_cell($po_line->price);
192                 amount_cell($line_total);
193
194                 if ($editable)
195                 {
196                                         edit_button_cell("Edit$line_no", _("Edit"),
197                                           _('Edit document line'));
198                                         edit_button_cell("Delete$line_no", _("Delete"),
199                                           _('Remove line from document'));
200                 }
201                         end_row();
202                         }
203                         else
204                         {
205                                 po_item_controls($order, $po_line->stock_id);
206                         }
207                 $total += $line_total;
208                 }
209     }
210
211         if ($id==-1 && $editable)
212                 po_item_controls($order);
213
214     $display_total = price_format($total);
215     label_row(_("Total Excluding Shipping/Tax"), $display_total, "colspan=6 align=right",
216         "nowrap align=right");
217
218         end_table(1);
219         div_end();
220 }
221
222 //---------------------------------------------------------------------------------------------------
223
224 function display_po_summary(&$po, $is_self=false, $editable=false)
225 {
226         global $table_style2;
227     start_table("$table_style2 width=90%");
228
229     start_row();
230     label_cells(_("Reference"), $po->reference, "class='tableheader2'");
231
232     label_cells(_("Supplier"), $po->supplier_name, "class='tableheader2'");
233
234     if (!is_company_currency($po->curr_code))
235         label_cells(_("Order Currency"), $po->curr_code, "class='tableheader2'");
236
237     if (!$is_self)
238     {
239         label_cells(_("Purchase Order"), get_trans_view_str(systypes::po(), $po->order_no),
240                 "class='tableheader2'");
241     }
242         end_row();
243         start_row();
244     label_cells(_("Date"), $po->orig_order_date, "class='tableheader2'");
245
246     if ($editable)
247     {
248         if (!isset($_POST['Location']))
249                 $_POST['Location'] = $po->Location;
250         label_cell(_("Deliver Into Location"), "class='tableheader2'");
251         locations_list_cells(null, 'Location', $_POST['Location']);
252     }
253     else
254     {
255         label_cells(_("Deliver Into Location"), get_location_name($po->Location),
256                 "class='tableheader2'");
257     }
258
259     if ($po->requisition_no != "")
260         label_cells(_("Supplier's Reference"), $po->requisition_no, "class='tableheader2'");
261     end_row();
262
263     if (!$editable)
264         label_row(_("Delivery Address"), $po->delivery_address, "class='tableheader2'",
265                 "colspan=9");
266
267     if ($po->Comments != "")
268         label_row(_("Order Comments"), $po->Comments, "class='tableheader2'",
269                 "colspan=9");
270     end_table(1);
271 }
272
273 //--------------------------------------------------------------------------------
274
275 function po_item_controls(&$order, $stock_id=null)
276 {
277    global $Ajax;
278         start_row();
279
280         $id = find_submit('Edit');
281         if (($id != -1) && $stock_id != null)
282         {
283                 hidden('line_no', $id);
284
285                 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
286                         $dec = get_qty_dec($_POST['stock_id']);
287                 $_POST['qty'] = qty_format($order->line_items[$id]->quantity, $_POST['stock_id'], $dec);
288                 $_POST['price'] = price_format($order->line_items[$id]->price);
289                 $_POST['req_del_date'] = $order->line_items[$id]->req_del_date;
290
291                 $_POST['units'] = $order->line_items[$id]->units;
292
293                 hidden('stock_id', $_POST['stock_id']);
294                 label_cell($_POST['stock_id']);
295                 label_cell($order->line_items[$id]->item_description);
296             $Ajax->activate('items_table');
297         }
298         else
299         {
300                 hidden('line_no', ($_SESSION['PO']->lines_on_order + 1));
301
302                 stock_purchasable_items_list_cells(null, 'stock_id', null, false, true);
303                 if (list_updated('stock_id')) {
304                             $Ajax->activate('price');
305                             $Ajax->activate('units');
306                             $Ajax->activate('qty');
307                             $Ajax->activate('req_del_date');
308                             $Ajax->activate('line_total');
309                 }
310         $item_info = get_item_edit_info($_POST['stock_id']);
311                 $_POST['units'] = $item_info["units"];
312
313                 $dec = $item_info["decimals"];
314                 $_POST['qty'] = number_format2(1, $dec);
315                 $_POST['price'] = price_format(get_purchase_price ($order->supplier_id, $_POST['stock_id']));
316                 $_POST['req_del_date'] = add_days(Today(), 10);
317         }
318
319         qty_cells(null, 'qty', null, null, null, $dec);
320
321         label_cell($_POST['units'], '', 'units');
322         date_cells(null, 'req_del_date', '', null, 0, 0, 0);
323         amount_cells(null, 'price', null);
324
325         //$line_total = $_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc'] / 100);
326         $line_total = round(input_num('qty') * input_num('price'),  user_price_dec());
327         amount_cell($line_total, false, '','line_total');
328
329         if ($id!=-1)
330         {
331                 edit_button_cell('UpdateLine', _("Update"),
332                                 _('Confirm changes'));
333                 edit_button_cell('CancelUpdate', _("Cancel"),
334                                 _('Cancel changes'));
335                 set_focus('qty');
336         }
337         else
338         {
339                 submit_cells('EnterLine', _("Add Item"), "colspan=2",
340                     _('Add new item to document'), true);
341         }
342
343         end_row();
344 }
345
346 //---------------------------------------------------------------------------------------------------
347
348
349
350 ?>