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)
88 $editable = ($order->order_no == 0);
90 start_outer_table(TABLESTYLE2, "width=80%");
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)
193 display_heading(_("Order Items"));
195 div_start('items_table');
196 start_table(TABLESTYLE, "width=80%");
198 $th = array(_("Item Code"), _("Item Description"), _("Quantity"),
199 _("Received"), _("Unit"),
200 _("Required Delivery Date"), _("Price"), _("Line Total"), "");
201 if ($order->trans_type != ST_PURCHORDER)
202 array_remove($th, 5);
204 if (count($order->line_items)) $th[] = '';
207 $id = find_submit('Edit');
210 foreach ($order->line_items as $line_no => $po_line)
212 $line_total = round($po_line->quantity * $po_line->price, user_price_dec());
213 if (!$editable || ($id != $line_no))
215 alt_table_row_color($k);
216 label_cell($po_line->stock_id);
217 label_cell($po_line->item_description);
218 qty_cell($po_line->quantity, false, get_qty_dec($po_line->stock_id));
219 qty_cell($po_line->qty_received, false, get_qty_dec($po_line->stock_id));
220 label_cell($po_line->units);
221 if ($order->trans_type == ST_PURCHORDER)
222 label_cell($po_line->req_del_date);
223 amount_decimal_cell($po_line->price);
224 amount_cell($line_total);
228 edit_button_cell("Edit$line_no", _("Edit"),
229 _('Edit document line'));
230 delete_button_cell("Delete$line_no", _("Delete"),
231 _('Remove line from document'));
237 po_item_controls($order, $k, $line_no);
239 $total += $line_total;
242 if ($id==-1 && $editable)
243 po_item_controls($order, $k);
245 $display_total = price_format($total);
246 label_row(_("Total Excluding Shipping/Tax"), $display_total, "colspan="
247 .(count($order->line_items) ? 7:6)." align=right",
248 "nowrap align=right", 2);
254 //---------------------------------------------------------------------------------------------------
256 function display_po_summary(&$po, $is_self=false, $editable=false)
258 start_table(TABLESTYLE, "width=90%");
261 label_cells(_("Reference"), $po->reference, "class='tableheader2'");
263 label_cells(_("Supplier"), $po->supplier_name, "class='tableheader2'");
265 if (!is_company_currency($po->curr_code))
266 label_cells(_("Order Currency"), $po->curr_code, "class='tableheader2'");
270 label_cells(_("Purchase Order"), get_trans_view_str(ST_PURCHORDER, $po->order_no),
271 "class='tableheader2'");
275 label_cells(_("Date"), $po->orig_order_date, "class='tableheader2'");
279 if (!isset($_POST['Location']))
280 $_POST['Location'] = $po->Location;
281 label_cell(_("Deliver Into Location"), "class='tableheader2'");
282 locations_list_cells(null, 'Location', $_POST['Location']);
286 label_cells(_("Deliver Into Location"), get_location_name($po->Location),
287 "class='tableheader2'");
290 if ($po->supp_ref != "")
291 label_cells(_("Supplier's Reference"), $po->supp_ref, "class='tableheader2'");
295 label_row(_("Delivery Address"), $po->delivery_address, "class='tableheader2'",
298 if ($po->Comments != "")
299 label_row(_("Order Comments"), $po->Comments, "class='tableheader2'",
304 //--------------------------------------------------------------------------------
306 function po_item_controls(&$order, &$rowcounter, $line_no=-1)
310 alt_table_row_color($rowcounter);
313 $id = find_submit('Edit');
314 if (($id != -1) && $line_no == $id)
316 // hidden('line_no', $id);
318 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
319 $dec = get_qty_dec($_POST['stock_id']);
320 $_POST['qty'] = qty_format($order->line_items[$id]->quantity, $_POST['stock_id'], $dec);
321 //$_POST['price'] = price_format($order->line_items[$id]->price);
322 $_POST['price'] = price_decimal_format($order->line_items[$id]->price, $dec2);
323 if ($order->trans_type == ST_PURCHORDER)
324 $_POST['req_del_date'] = $order->line_items[$id]->req_del_date;
326 $_POST['units'] = $order->line_items[$id]->units;
327 $_POST['item_description'] = $order->line_items[$id]->item_description;
329 hidden('stock_id', $_POST['stock_id']);
330 label_cell($_POST['stock_id']);
332 if ($order->line_items[$id]->descr_editable)
333 text_cells(null,'item_description', null, 45, 150);
335 hidden('item_description', $_POST['item_description']);
336 // label_cell($_POST['item_description']);
337 label_cell($order->line_items[$id]->item_description);
340 $Ajax->activate('items_table');
341 $qty_rcvd = $order->line_items[$id]->qty_received;
345 // hidden('line_no', ($_SESSION['PO']->lines_on_order + 1));
347 stock_purchasable_items_list_cells(null, 'stock_id', null, false, true, true);
348 if (list_updated('stock_id')) {
349 $Ajax->activate('price');
350 $Ajax->activate('units');
351 $Ajax->activate('qty');
352 $Ajax->activate('req_del_date');
353 $Ajax->activate('line_total');
355 $item_info = get_item_edit_info($_POST['stock_id']);
356 $_POST['units'] = $item_info["units"];
358 $dec = $item_info["decimals"];
359 $_POST['qty'] = number_format2(get_purchase_conversion_factor ($order->supplier_id, $_POST['stock_id']), $dec);
360 //$_POST['price'] = price_format(get_purchase_price ($order->supplier_id, $_POST['stock_id']));
361 $_POST['price'] = price_decimal_format(get_purchase_price ($order->supplier_id, $_POST['stock_id']), $dec2);
362 if ($order->trans_type == ST_PURCHORDER)
363 $_POST['req_del_date'] = add_days(Today(), 10);
367 qty_cells(null, 'qty', null, null, null, $dec);
368 qty_cell($qty_rcvd, false, $dec);
370 label_cell($_POST['units'], '', 'units');
371 if ($order->trans_type == ST_PURCHORDER)
372 date_cells(null, 'req_del_date', '', null, 0, 0, 0);
373 amount_cells(null, 'price', null, null, null, $dec2);
375 //$line_total = $_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc'] / 100);
376 $line_total = round(input_num('qty') * input_num('price'), user_price_dec());
377 amount_cell($line_total, false, '','line_total');
381 button_cell('UpdateLine', _("Update"),
382 _('Confirm changes'), ICON_UPDATE);
383 button_cell('CancelUpdate', _("Cancel"),
384 _('Cancel changes'), ICON_CANCEL);
385 hidden('line_no', $line_no);
390 submit_cells('EnterLine', _("Add Item"), "colspan=2",
391 _('Add new item to document'), true);
397 //---------------------------------------------------------------------------------------------------