Unstable release 2.
[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         table_header($th);
170
171         $total = 0;
172         $k = 0;
173         foreach ($order->line_items as $po_line)
174         {
175
176                 if ($po_line->Deleted == false)
177                 {
178                 $line_total =   $po_line->quantity * $po_line->price;
179                 if (!$editable || !isset($_GET['Edit']) || $_GET['Edit'] != $po_line->line_no)
180                         {
181                         alt_table_row_color($k);
182                         label_cell($po_line->stock_id);
183                         label_cell($po_line->item_description);
184                 qty_cell($po_line->quantity);
185                         label_cell($po_line->units);
186                 label_cell($po_line->req_del_date);
187                         amount_cell($po_line->price);
188                 amount_cell($line_total);
189
190                 if ($editable)
191                 {
192                     edit_link_cell(SID."Edit=" . $po_line->line_no);
193                     delete_link_cell(SID."Delete=" . $po_line->line_no);
194                 }
195                         end_row();
196                         }
197                         else
198                         {
199                                 po_item_controls($order, $po_line->stock_id);
200                         }
201                 $total += $line_total;
202                 }
203     }
204
205         if (!isset($_GET['Edit']) && $editable)
206                 po_item_controls($order);
207
208     $display_total = number_format2($total,user_price_dec());
209     label_row(_("Total Excluding Shipping/Tax"), $display_total, "colspan=6 align=right",
210         "nowrap align=right");
211
212         end_table(1);
213 }
214
215 //---------------------------------------------------------------------------------------------------
216
217 function display_po_summary(&$po, $is_self=false, $editable=false)
218 {
219         global $table_style2;
220     start_table("$table_style2 width=90%");
221
222     start_row();
223     label_cells(_("Reference"), $po->reference, "class='tableheader2'");
224
225     label_cells(_("Supplier"), $po->supplier_name, "class='tableheader2'");
226
227     if (!is_company_currency($po->curr_code))
228         label_cells(_("Order Currency"), $po->curr_code, "class='tableheader2'");
229
230     if (!$is_self)
231     {
232         label_cells(_("Purchase Order"), get_trans_view_str(systypes::po(), $po->order_no),
233                 "class='tableheader2'");
234     }
235         end_row();
236         start_row();
237     label_cells(_("Date"), $po->orig_order_date, "class='tableheader2'");
238
239     if ($editable)
240     {
241         if (!isset($_POST['Location']))
242                 $_POST['Location'] = $po->Location;
243         label_cell(_("Deliver Into Location"), "class='tableheader2'");
244         locations_list_cells(null, 'Location', $_POST['Location']);
245     }
246     else
247     {
248         label_cells(_("Deliver Into Location"), get_location_name($po->Location),
249                 "class='tableheader2'");
250     }
251
252     if ($po->requisition_no != "")
253         label_cells(_("Supplier's Reference"), $po->requisition_no, "class='tableheader2'");
254     end_row();
255
256     if (!$editable)
257         label_row(_("Delivery Address"), $po->delivery_address, "class='tableheader2'",
258                 "colspan=9");
259
260     if ($po->Comments != "")
261         label_row(_("Order Comments"), $po->Comments, "class='tableheader2'",
262                 "colspan=9");
263     end_table(1);
264 }
265
266 //--------------------------------------------------------------------------------
267
268 function po_item_controls(&$order, $stock_id=null)
269 {
270         start_row();
271
272         if (isset($_GET['Edit']) && $stock_id != null)
273         {
274                 hidden('line_no', $_GET['Edit']);
275
276                 if (!isset($_POST['stock_id']))
277                         $_POST['stock_id'] = $order->line_items[$_GET['Edit']]->stock_id;
278                 if (!isset($_POST['qty']) || ($_POST['qty'] == ""))
279                         $_POST['qty'] = $order->line_items[$_GET['Edit']]->quantity;
280                 if (!isset($_POST['price']) || ($_POST['price'] == ""))
281                         $_POST['price'] = $order->line_items[$_GET['Edit']]->price;
282                 if (!isset($_POST['req_del_date']) || ($_POST['req_del_date'] == ""))
283                         $_POST['req_del_date'] = $order->line_items[$_GET['Edit']]->req_del_date;
284
285                 $_POST['units'] = $order->line_items[$_GET['Edit']]->units;
286
287                 hidden('stock_id', $_POST['stock_id']);
288                 label_cell($_POST['stock_id']);
289                 label_cell($order->line_items[$_GET['Edit']]->item_description);
290         }
291         else
292         {
293                 hidden('line_no', ($_SESSION['PO']->lines_on_order + 1));
294
295                 $no_item_list = get_company_pref('no_item_list');
296                 if ($no_item_list)
297                 {
298                         echo "<td colspan=2>\n";
299                         stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true);
300                         echo "</td>\n";
301                 }
302                 else
303                 {
304                         text_cells(null, "StockID2", "", 12, 10, "", "", "onkeyup='recalcAccounts();' onKeyDown='if(event.keyCode==13) event.keyCode=9;' onblur='return setAccount(0, true);'");
305                         stock_purchasable_items_list_cells(null, 'stock_id', $_POST['stock_id'], false, false, "onchange='return setAccount(1, true)'");
306                 }
307         $item_info = get_item_edit_info($_POST['stock_id']);
308                 $_POST['units'] = $item_info["units"];
309
310                 $_POST['qty'] = 1;
311                 $_POST['price'] = get_purchase_price ($order->supplier_id, $_POST['stock_id']);
312                 $_POST['req_del_date'] = add_days(Today(), 10);
313         }
314
315         text_cells(null, 'qty', null, 13, 15);
316
317         label_cell($_POST['units']);
318         date_cells(null, 'req_del_date', null, 0, 0, 0);
319         text_cells(null, 'price', null, 15, 14);
320
321         //$line_total = $_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc'] / 100);
322         $line_total = $_POST['qty'] * $_POST['price'];
323         amount_cell($line_total);
324
325         if (isset($_GET['Edit']))
326         {
327         submit_cells('UpdateLine', _("Update"));
328         submit_cells('CancelUpdate', _("Cancel"));
329         }
330         else
331         {
332                 submit_cells('EnterLine', _("Add Item"), "colspan=2");
333         }
334
335         end_row();
336 }
337
338 //---------------------------------------------------------------------------------------------------
339
340
341
342 ?>