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");
14 //--------------------------------------------------------------------------------------------------
16 function copy_from_cart()
18 $cart = &$_SESSION['PO'];
20 $_POST['supplier_id'] = $cart->supplier_id;
21 $_POST['OrderDate'] = $cart->orig_order_date;
22 $_POST['supp_ref'] = $cart->supp_ref;
23 $_POST['ref'] = $cart->reference;
24 $_POST['Comments'] = $cart->Comments;
25 $_POST['StkLocation'] = $cart->Location;
26 $_POST['delivery_address'] = $cart->delivery_address;
29 function copy_to_cart()
31 $cart = &$_SESSION['PO'];
33 $cart->supplier_id = $_POST['supplier_id'];
34 $cart->orig_order_date = $_POST['OrderDate'];
35 $cart->reference = $_POST['ref'];
36 $cart->supp_ref = $_POST['supp_ref'];
37 $cart->Comments = $_POST['Comments'];
38 $cart->Location = $_POST['StkLocation'];
39 $cart->delivery_address = $_POST['delivery_address'];
41 // ------------------------------------------------------------------------------
43 function get_supplier_details_to_order(&$order, $supplier_id)
45 $sql = "SELECT curr_code,supp_name FROM ".TB_PREF."suppliers
46 WHERE supplier_id = '$supplier_id'";
47 $result = db_query($sql, "The supplier details could not be retreived");
49 $myrow = db_fetch($result);
51 $order->curr_code = $_POST['curr_code'] = $myrow["curr_code"];
52 $order->supplier_name = $_POST['supplier_name'] = $myrow["supp_name"];
53 $order->supplier_id = $_POST['supplier_id'] = $supplier_id;
56 //---------------------------------------------------------------------------------------------------
58 function create_new_po($trans_type, $trans_no)
62 if (isset($_SESSION['PO']))
63 unset ($_SESSION['PO']->line_items, $_SESSION['PO']);
65 $cart = new purch_order;
66 $_POST['OrderDate'] = new_doc_date();
67 if (!is_date_in_fiscalyear($_POST['OrderDate']))
68 $_POST['OrderDate'] = end_fiscalyear();
69 $cart->orig_order_date = $_POST['OrderDate'];
71 $cart->trans_type = $trans_type;
72 $cart->order_no = $trans_no;
73 /*read in all the selected order into the Items cart */
75 read_po($trans_no, $cart);
76 $cart->order_no = $trans_no;
78 $cart->reference = $Refs->get_next($trans_type);
79 $_SESSION['PO'] = &$cart;
82 //---------------------------------------------------------------------------------------------------
84 function display_po_header(&$order)
86 global $table_style2, $Ajax, $Refs;
88 $editable = ($order->order_no == 0);
90 start_outer_table("width=80% $table_style2");
95 if (!isset($_POST['supplier_id']) && (get_global_supplier() != ALL_TEXT))
96 $_POST['supplier_id'] = get_global_supplier();
98 supplier_list_row(_("Supplier:"), 'supplier_id', null, false, true, false, true);
102 hidden('supplier_id', $order->supplier_id);
103 label_row(_("Supplier:"), $order->supplier_name);
106 if ($order->supplier_id != get_post('supplier_id',-1)) {
107 $old_supp = $order->supplier_id;
108 get_supplier_details_to_order($order, $_POST['supplier_id']);
109 // supplier default price update
110 foreach ($order->line_items as $line_no=>$item) {
111 $line = &$order->line_items[$line_no];
112 $line->price = get_purchase_price ($order->supplier_id, $line->stock_id);
114 $line->quantity/get_purchase_conversion_factor ($old_supp, $line->stock_id)
115 *get_purchase_conversion_factor ($order->supplier_id, $line->stock_id);
117 $Ajax->activate('items_table');
119 set_global_supplier($_POST['supplier_id']);
121 if (!is_company_currency($order->curr_code))
123 label_row(_("Supplier Currency:"), $order->curr_code);
124 exchange_rate_display($order->curr_code, get_company_currency(),
125 $_POST['OrderDate']);
130 ref_row(_("Reference:"), 'ref');
134 hidden('ref', $order->reference);
135 label_row(_("Reference:"), $order->reference);
140 // check this out?????????
141 //if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
142 // $_POST['OrderDate'] = $order->orig_order_date;
143 //if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
145 // $_POST['OrderDate'] = Today();
146 // if (!is_date_in_fiscalyear($_POST['OrderDate']))
147 // $_POST['OrderDate'] = end_fiscalyear();
149 date_row(_("Order Date:"), 'OrderDate', '', true, 0, 0, 0, null, true);
150 if (isset($_POST['_OrderDate_changed'])) {
151 $Ajax->activate('_ex_rate');
154 text_row(_("Supplier's Reference:"), 'supp_ref', null, 16, 15);
155 locations_list_row(_("Receive Into:"), 'StkLocation', null, false, true);
159 if (!isset($_POST['StkLocation']) || $_POST['StkLocation'] == "" ||
160 isset($_POST['_StkLocation_update']) || !isset($_POST['delivery_address']) ||
161 $_POST['delivery_address'] == "")
163 /*If this is the first time the form loaded set up defaults */
165 //$_POST['StkLocation'] = $_SESSION['UserStockLocation'];
166 $sql = "SELECT delivery_address, phone FROM ".TB_PREF."locations WHERE loc_code='" . $_POST['StkLocation'] . "'";
167 $result = db_query($sql,"could not get location info");
169 if (db_num_rows($result) == 1)
171 $loc_row = db_fetch($result);
172 $_POST['delivery_address'] = $loc_row["delivery_address"];
173 $Ajax->activate('delivery_address');
174 $_SESSION['PO']->Location = $_POST['StkLocation'];
175 $_SESSION['PO']->delivery_address = $_POST['delivery_address'];
179 { /*The default location of the user is crook */
180 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."));
184 textarea_row(_("Deliver to:"), 'delivery_address', $_POST['delivery_address'], 35, 4);
186 end_outer_table(); // outer table
189 //---------------------------------------------------------------------------------------------------
191 function display_po_items(&$order, $editable=true)
195 display_heading(_("Order Items"));
197 div_start('items_table');
198 start_table("$table_style width=80%");
200 $th = array(_("Item Code"), _("Item Description"), _("Quantity"),
201 _("Received"), _("Unit"),
202 _("Required Delivery Date"), _("Price"), _("Line Total"), "");
203 if ($order->trans_type != ST_PURCHORDER)
204 array_remove($th, 5);
206 if (count($order->line_items)) $th[] = '';
209 $id = find_submit('Edit');
212 foreach ($order->line_items as $line_no => $po_line)
214 $line_total = round($po_line->quantity * $po_line->price, user_price_dec());
215 if (!$editable || ($id != $line_no))
217 alt_table_row_color($k);
218 label_cell($po_line->stock_id);
219 label_cell($po_line->item_description);
220 qty_cell($po_line->quantity, false, get_qty_dec($po_line->stock_id));
221 qty_cell($po_line->qty_received, false, get_qty_dec($po_line->stock_id));
222 label_cell($po_line->units);
223 if ($order->trans_type == ST_PURCHORDER)
224 label_cell($po_line->req_del_date);
225 amount_decimal_cell($po_line->price);
226 amount_cell($line_total);
230 edit_button_cell("Edit$line_no", _("Edit"),
231 _('Edit document line'));
232 delete_button_cell("Delete$line_no", _("Delete"),
233 _('Remove line from document'));
239 po_item_controls($order, $k, $line_no);
241 $total += $line_total;
244 if ($id==-1 && $editable)
245 po_item_controls($order, $k);
247 $display_total = price_format($total);
248 label_row(_("Total Excluding Shipping/Tax"), $display_total, "colspan="
249 .(count($order->line_items) ? 7:6)." align=right",
250 "nowrap align=right", 2);
256 //---------------------------------------------------------------------------------------------------
258 function display_po_summary(&$po, $is_self=false, $editable=false)
261 start_table("$table_style width=90%");
264 label_cells(_("Reference"), $po->reference, "class='tableheader2'");
266 label_cells(_("Supplier"), $po->supplier_name, "class='tableheader2'");
268 if (!is_company_currency($po->curr_code))
269 label_cells(_("Order Currency"), $po->curr_code, "class='tableheader2'");
273 label_cells(_("Purchase Order"), get_trans_view_str(ST_PURCHORDER, $po->order_no),
274 "class='tableheader2'");
278 label_cells(_("Date"), $po->orig_order_date, "class='tableheader2'");
282 if (!isset($_POST['Location']))
283 $_POST['Location'] = $po->Location;
284 label_cell(_("Deliver Into Location"), "class='tableheader2'");
285 locations_list_cells(null, 'Location', $_POST['Location']);
289 label_cells(_("Deliver Into Location"), get_location_name($po->Location),
290 "class='tableheader2'");
293 if ($po->supp_ref != "")
294 label_cells(_("Supplier's Reference"), $po->supp_ref, "class='tableheader2'");
298 label_row(_("Delivery Address"), $po->delivery_address, "class='tableheader2'",
301 if ($po->Comments != "")
302 label_row(_("Order Comments"), $po->Comments, "class='tableheader2'",
307 //--------------------------------------------------------------------------------
309 function po_item_controls(&$order, &$rowcounter, $line_no=-1)
313 alt_table_row_color($rowcounter);
316 $id = find_submit('Edit');
317 if (($id != -1) && $line_no == $id)
319 // hidden('line_no', $id);
321 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
322 $dec = get_qty_dec($_POST['stock_id']);
323 $_POST['qty'] = qty_format($order->line_items[$id]->quantity, $_POST['stock_id'], $dec);
324 //$_POST['price'] = price_format($order->line_items[$id]->price);
325 $_POST['price'] = price_decimal_format($order->line_items[$id]->price, $dec2);
326 if ($order->trans_type == ST_PURCHORDER)
327 $_POST['req_del_date'] = $order->line_items[$id]->req_del_date;
329 $_POST['units'] = $order->line_items[$id]->units;
330 $_POST['item_description'] = $order->line_items[$id]->item_description;
332 hidden('stock_id', $_POST['stock_id']);
333 label_cell($_POST['stock_id']);
335 if ($order->line_items[$id]->descr_editable)
336 text_cells(null,'item_description', null, 45, 150);
338 hidden('item_description', $_POST['item_description']);
339 // label_cell($_POST['item_description']);
340 label_cell($order->line_items[$id]->item_description);
343 $Ajax->activate('items_table');
344 $qty_rcvd = $order->line_items[$id]->qty_received;
348 // hidden('line_no', ($_SESSION['PO']->lines_on_order + 1));
350 stock_purchasable_items_list_cells(null, 'stock_id', null, false, true, true);
351 if (list_updated('stock_id')) {
352 $Ajax->activate('price');
353 $Ajax->activate('units');
354 $Ajax->activate('qty');
355 $Ajax->activate('req_del_date');
356 $Ajax->activate('line_total');
358 $item_info = get_item_edit_info($_POST['stock_id']);
359 $_POST['units'] = $item_info["units"];
361 $dec = $item_info["decimals"];
362 $_POST['qty'] = number_format2(get_purchase_conversion_factor ($order->supplier_id, $_POST['stock_id']), $dec);
363 //$_POST['price'] = price_format(get_purchase_price ($order->supplier_id, $_POST['stock_id']));
364 $_POST['price'] = price_decimal_format(get_purchase_price ($order->supplier_id, $_POST['stock_id']), $dec2);
365 if ($order->trans_type == ST_PURCHORDER)
366 $_POST['req_del_date'] = add_days(Today(), 10);
370 qty_cells(null, 'qty', null, null, null, $dec);
371 qty_cell($qty_rcvd, false, $dec);
373 label_cell($_POST['units'], '', 'units');
374 if ($order->trans_type == ST_PURCHORDER)
375 date_cells(null, 'req_del_date', '', null, 0, 0, 0);
376 amount_cells(null, 'price', null, null, null, $dec2);
378 //$line_total = $_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc'] / 100);
379 $line_total = round(input_num('qty') * input_num('price'), user_price_dec());
380 amount_cell($line_total, false, '','line_total');
384 button_cell('UpdateLine', _("Update"),
385 _('Confirm changes'), ICON_UPDATE);
386 button_cell('CancelUpdate', _("Cancel"),
387 _('Cancel changes'), ICON_CANCEL);
388 hidden('line_no', $line_no);
393 submit_cells('EnterLine', _("Add Item"), "colspan=2",
394 _('Add new item to document'), true);
400 //---------------------------------------------------------------------------------------------------