Changed API for input/lists functions, added empty hints when needed
[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;
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', $_POST['supplier_id'], false, true);
58
59         if ($order->supplier_id != $_POST['supplier_id'])
60         {
61                 // supplier has changed
62                 // delete all the order items - drastic but necessary because of
63                 // change of currency, etc
64                 $order->clear_items();
65         }
66         }
67         else
68         {
69                 hidden('supplier_id', $order->supplier_id);
70                 label_row(_("Supplier:"), $order->supplier_name);
71     }
72
73         //if ($editable || ($order->supplier_id != $_POST['supplier_id']))
74         get_supplier_details_to_order($order, $_POST['supplier_id']);
75
76         set_global_supplier($_POST['supplier_id']);
77
78         if (!is_company_currency($order->curr_code))
79         {
80                 label_row(_("Supplier Currency:"), $order->curr_code);
81                 exchange_rate_display($order->curr_code, get_company_currency(),
82                         $_POST['OrderDate']);
83         }
84
85     if ($editable)
86     {
87         ref_row(_("Reference:"), 'ref', '', references::get_next(systypes::po()));
88     }
89     else
90     {
91         hidden('ref', $order->reference);
92         label_row(_("Reference:"), $order->reference);
93     }
94
95         echo "</table>";
96
97         echo "</td><td valign=center>"; // outer table
98
99         echo "<table height='5'>";
100         // check this out?????????
101         //if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
102         //      $_POST['OrderDate'] = $order->orig_order_date;
103         //if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
104         //{
105         //      $_POST['OrderDate'] = Today();
106         //      if (!is_date_in_fiscalyear($_POST['OrderDate']))
107         //              $_POST['OrderDate'] = end_fiscalyear();
108         //}
109         date_row(_("Order Date:"), 'OrderDate', '', $_POST['OrderDate'], 0, 0, 0);
110
111         text_row(_("Supplier's Reference:"), 'Requisition', null, 16, 15);
112
113         echo "</table>";
114
115         echo "</td><td valign=center>"; // outer table
116
117         echo "<table height='5'>";
118
119         echo "<tr><td>" . _("Receive Into:") . "</td>";
120         echo "<td>";
121     locations_list('StkLocation', null);
122     echo "  ";
123     submit('GetAddress', _("Get Address"));
124         echo "</td></tr>";
125
126     if (!isset($_POST['StkLocation']) || $_POST['StkLocation'] == "" ||
127         isset($_POST['GetAddress']) || !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                 $_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                 echo "<br>" . _("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     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         $total = 0;
174         $k = 0;
175         foreach ($order->line_items as $po_line)
176         {
177
178                 if ($po_line->Deleted == false)
179                 {
180                 $line_total =   round($po_line->quantity * $po_line->price,  user_price_dec());
181                 if (!$editable || !isset($_GET['Edit']) || $_GET['Edit'] != $po_line->line_no)
182                         {
183                         alt_table_row_color($k);
184                         label_cell($po_line->stock_id);
185                         label_cell($po_line->item_description);
186                 qty_cell($po_line->quantity);
187                         label_cell($po_line->units);
188                 label_cell($po_line->req_del_date);
189                         amount_cell($po_line->price);
190                 amount_cell($line_total);
191
192                 if ($editable)
193                 {
194                     edit_link_cell(SID."Edit=" . $po_line->line_no);
195                     delete_link_cell(SID."Delete=" . $po_line->line_no);
196                 }
197                         end_row();
198                         }
199                         else
200                         {
201                                 po_item_controls($order, $po_line->stock_id);
202                         }
203                 $total += $line_total;
204                 }
205     }
206
207         if (!isset($_GET['Edit']) && $editable)
208                 po_item_controls($order);
209
210     $display_total = price_format($total);
211     label_row(_("Total Excluding Shipping/Tax"), $display_total, "colspan=6 align=right",
212         "nowrap align=right");
213
214         end_table(1);
215 }
216
217 //---------------------------------------------------------------------------------------------------
218
219 function display_po_summary(&$po, $is_self=false, $editable=false)
220 {
221         global $table_style2;
222     start_table("$table_style2 width=90%");
223
224     start_row();
225     label_cells(_("Reference"), $po->reference, "class='tableheader2'");
226
227     label_cells(_("Supplier"), $po->supplier_name, "class='tableheader2'");
228
229     if (!is_company_currency($po->curr_code))
230         label_cells(_("Order Currency"), $po->curr_code, "class='tableheader2'");
231
232     if (!$is_self)
233     {
234         label_cells(_("Purchase Order"), get_trans_view_str(systypes::po(), $po->order_no),
235                 "class='tableheader2'");
236     }
237         end_row();
238         start_row();
239     label_cells(_("Date"), $po->orig_order_date, "class='tableheader2'");
240
241     if ($editable)
242     {
243         if (!isset($_POST['Location']))
244                 $_POST['Location'] = $po->Location;
245         label_cell(_("Deliver Into Location"), "class='tableheader2'");
246         locations_list_cells(null, 'Location', $_POST['Location']);
247     }
248     else
249     {
250         label_cells(_("Deliver Into Location"), get_location_name($po->Location),
251                 "class='tableheader2'");
252     }
253
254     if ($po->requisition_no != "")
255         label_cells(_("Supplier's Reference"), $po->requisition_no, "class='tableheader2'");
256     end_row();
257
258     if (!$editable)
259         label_row(_("Delivery Address"), $po->delivery_address, "class='tableheader2'",
260                 "colspan=9");
261
262     if ($po->Comments != "")
263         label_row(_("Order Comments"), $po->Comments, "class='tableheader2'",
264                 "colspan=9");
265     end_table(1);
266 }
267
268 //--------------------------------------------------------------------------------
269
270 function po_item_controls(&$order, $stock_id=null)
271 {
272         start_row();
273
274         if (isset($_GET['Edit']) && $stock_id != null)
275         {
276                 hidden('line_no', $_GET['Edit']);
277
278                 if (!isset($_POST['stock_id']))
279                         $_POST['stock_id'] = $order->line_items[$_GET['Edit']]->stock_id;
280                 if (!isset($_POST['qty']) || ($_POST['qty'] == ""))
281                         $_POST['qty'] = qty_format($order->line_items[$_GET['Edit']]->quantity);
282                 if (!isset($_POST['price']) || ($_POST['price'] == ""))
283                         $_POST['price'] = price_format($order->line_items[$_GET['Edit']]->price);
284                 if (!isset($_POST['req_del_date']) || ($_POST['req_del_date'] == ""))
285                         $_POST['req_del_date'] = $order->line_items[$_GET['Edit']]->req_del_date;
286
287                 $_POST['units'] = $order->line_items[$_GET['Edit']]->units;
288
289                 hidden('stock_id', $_POST['stock_id']);
290                 label_cell($_POST['stock_id']);
291                 label_cell($order->line_items[$_GET['Edit']]->item_description);
292         }
293         else
294         {
295                 hidden('line_no', ($_SESSION['PO']->lines_on_order + 1));
296
297                 stock_purchasable_items_list_cells(null, 'stock_id', null, false, true);
298         $item_info = get_item_edit_info($_POST['stock_id']);
299                 $_POST['units'] = $item_info["units"];
300
301                 $_POST['qty'] = qty_format(1);
302                 $_POST['price'] = price_format(get_purchase_price ($order->supplier_id, $_POST['stock_id']));
303                 $_POST['req_del_date'] = add_days(Today(), 10);
304         }
305
306         qty_cells(null, 'qty', null);
307
308         label_cell($_POST['units']);
309         date_cells(null, 'req_del_date', '', null, 0, 0, 0);
310         amount_cells(null, 'price', null);
311
312         //$line_total = $_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc'] / 100);
313         $line_total = round(input_num('qty') * input_num('price'),  user_price_dec());
314         amount_cell($line_total);
315
316         if (isset($_GET['Edit']))
317         {
318         submit_cells('UpdateLine', _("Update"));
319         submit_cells('CancelUpdate', _("Cancel"));
320         }
321         else
322         {
323                 submit_cells('EnterLine', _("Add Item"), "colspan=2");
324         }
325
326         end_row();
327 }
328
329 //---------------------------------------------------------------------------------------------------
330
331
332
333 ?>