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 if ($cart->trans_type == ST_SUPPINVOICE)
23 $_POST['due_date'] = $cart->due_date;
24 $_POST['supp_ref'] = $cart->supp_ref;
25 $_POST['ref'] = $cart->reference;
26 $_POST['Comments'] = $cart->Comments;
27 $_POST['StkLocation'] = $cart->Location;
28 $_POST['delivery_address'] = $cart->delivery_address;
29 $_POST['tax_algorithm'] = $cart->tax_algorithm;
30 $_POST['prep_amount'] = price_format($cart->prep_amount);
31 $_POST['_ex_rate'] = $cart->ex_rate;
32 foreach($cart->tax_overrides as $id => $value)
33 $_POST['mantax'][$id] = price_format($value);
36 function copy_to_cart()
38 $cart = &$_SESSION['PO'];
40 $cart->supplier_id = $_POST['supplier_id'];
41 $cart->orig_order_date = $_POST['OrderDate'];
42 if ($cart->trans_type == ST_SUPPINVOICE)
43 $cart->due_date = $_POST['due_date'];
44 $cart->reference = $_POST['ref'];
45 $cart->supp_ref = $_POST['supp_ref'];
46 $cart->Comments = $_POST['Comments'];
47 $cart->Location = $_POST['StkLocation'];
48 $cart->delivery_address = $_POST['delivery_address'];
49 if (isset($_POST['tax_algorithm']))
50 $cart->tax_algorithm = $_POST['tax_algorithm'];
51 $cart->prep_amount = input_num('prep_amount', 0);
52 $cart->ex_rate = input_num('_ex_rate', null);
54 if (isset($_POST['mantax'])) {
55 foreach($_POST['mantax'] as $id => $tax) {
56 $cart->tax_overrides[$id] = user_numeric($_POST['mantax'][$id]); }
59 // ------------------------------------------------------------------------------
61 function get_supplier_details_to_order(&$order, $supplier_id)
63 $sql = "SELECT curr_code, supp_name, tax_group_id, supp.tax_included, supp.tax_algorithm,
64 supp.credit_limit - Sum(IFNULL(IF(trans.type=".ST_SUPPCREDIT.", -1, 1)
65 * (ov_amount + ov_gst + ov_discount),0)) as cur_credit,
66 terms.terms, terms.days_before_due, terms.day_in_following_month
67 FROM ".TB_PREF."suppliers supp
68 LEFT JOIN ".TB_PREF."supp_trans trans ON supp.supplier_id = trans.supplier_id
69 LEFT JOIN ".TB_PREF."payment_terms terms ON supp.payment_terms=terms.terms_indicator
70 WHERE supp.supplier_id = ".db_escape($supplier_id)."
74 $result = db_query($sql, "The supplier details could not be retreived");
76 $myrow = db_fetch($result);
77 $order->credit = $myrow["cur_credit"];
78 $order->terms = array(
79 'description' => $myrow['terms'],
80 'days_before_due' => $myrow['days_before_due'],
81 'day_in_following_month' => $myrow['day_in_following_month'] );
83 $_POST['supplier_id'] = $supplier_id;
84 $_POST['supplier_name'] = $myrow["supp_name"];
85 $_POST['curr_code'] = $myrow["curr_code"];
87 $order->set_supplier($supplier_id, $myrow["supp_name"], $myrow["curr_code"],
88 $myrow["tax_group_id"], $myrow["tax_included"], $myrow["tax_algorithm"]);
91 //---------------------------------------------------------------------------------------------------
93 function create_new_po($trans_type, $trans_no)
97 if (isset($_SESSION['PO']))
98 unset ($_SESSION['PO']->line_items, $_SESSION['PO']);
100 $cart = new purch_order;
101 $_POST['OrderDate'] = new_doc_date();
102 if (!is_date_in_fiscalyear($_POST['OrderDate']))
103 $_POST['OrderDate'] = end_fiscalyear();
104 $cart->due_date = $cart->orig_order_date = $_POST['OrderDate'];
106 $cart->trans_type = $trans_type;
107 $cart->order_no = $trans_no;
108 /*read in all the selected order into the Items cart */
110 read_po($trans_no, $cart);
111 $cart->order_no = $trans_no;
113 $cart->reference = $Refs->get_next($trans_type);
114 $_SESSION['PO'] = &$cart;
117 //---------------------------------------------------------------------------------------------------
119 function display_po_header(&$order)
123 $editable = ($order->order_no == 0);
125 start_outer_table(TABLESTYLE2, 'width=80%');
130 if (!isset($_POST['supplier_id']) && (get_global_supplier() != ALL_TEXT))
131 $_POST['supplier_id'] = get_global_supplier();
133 supplier_list_row(_("Supplier:"), 'supplier_id', null, false, true, false, true);
137 hidden('supplier_id', $order->supplier_id);
138 label_row(_("Supplier:"), $order->supplier_name);
141 if ($order->supplier_id != get_post('supplier_id',-1)) {
142 $old_supp = $order->supplier_id;
143 get_supplier_details_to_order($order, $_POST['supplier_id']);
144 get_duedate_from_terms($order);
145 $_POST['due_date'] = $order->due_date;
146 $_POST['tax_algorithm'] = $order->tax_algorithm;
148 // supplier default price update
149 foreach ($order->line_items as $line_no=>$item) {
150 $line = &$order->line_items[$line_no];
151 $line->price = get_purchase_price ($order->supplier_id, $line->stock_id);
153 $line->quantity/get_purchase_conversion_factor ($old_supp, $line->stock_id)
154 *get_purchase_conversion_factor ($order->supplier_id, $line->stock_id);
156 $Ajax->activate('items_table');
157 $Ajax->activate('due_date');
158 $Ajax->activate('tax_algorithm');
160 set_global_supplier($_POST['supplier_id']);
162 date_row($order->trans_type==ST_PURCHORDER ? _("Order Date:") :
163 ($order->trans_type==ST_SUPPRECEIVE ? _("Delivery Date:") : _("Invoice Date:")),
164 'OrderDate', '', true, 0, 0, 0, null, true);
166 if (isset($_POST['_OrderDate_changed'])) {
167 $order->orig_order_date = $_POST['OrderDate'];
168 get_duedate_from_terms($order);
169 $_POST['due_date'] = $order->due_date;
170 $Ajax->activate('due_date');
172 supplier_credit_row($order->supplier_id, $order->credit);
174 if (!is_company_currency($order->curr_code))
176 label_row(_("Supplier Currency:"), $order->curr_code);
177 exchange_rate_display(get_company_currency(), $order->curr_code,
178 $_POST['OrderDate']);
184 ref_row(_("Reference:"), 'ref');
188 hidden('ref', $order->reference);
189 label_row(_("Reference:"), $order->reference);
194 if ($order->trans_type==ST_SUPPINVOICE)
195 date_row(_("Due Date:"), 'due_date', '', false, 0, 0, 0, null, true);
197 text_row(_("Supplier's Reference:"), 'supp_ref', null, 16, 15);
198 locations_list_row(_("Receive Into:"), 'StkLocation', null, false, true);
202 if (!isset($_POST['StkLocation']) || $_POST['StkLocation'] == "" ||
203 isset($_POST['_StkLocation_update']) || !isset($_POST['delivery_address']) ||
204 $_POST['delivery_address'] == "")
206 /*If this is the first time the form loaded set up defaults */
208 //$_POST['StkLocation'] = $_SESSION['UserStockLocation'];
209 $sql = "SELECT delivery_address, phone FROM ".TB_PREF."locations WHERE loc_code=".db_escape($_POST['StkLocation']);
210 $result = db_query($sql,"could not get location info");
212 if (db_num_rows($result) == 1)
214 $loc_row = db_fetch($result);
215 $_POST['delivery_address'] = $loc_row["delivery_address"];
216 $Ajax->activate('delivery_address');
217 $_SESSION['PO']->Location = $_POST['StkLocation'];
218 $_SESSION['PO']->delivery_address = $_POST['delivery_address'];
222 { /*The default location of the user is crook */
223 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."));
227 textarea_row(_("Deliver to:"), 'delivery_address', $_POST['delivery_address'], 35, 4);
229 end_outer_table(); // outer table
232 //---------------------------------------------------------------------------------------------------
234 function display_po_items(&$order, $editable=true)
236 display_heading(_("Order Items"));
238 div_start('items_table');
239 start_table(TABLESTYLE, "width=80%");
241 $th = array(_("Item Code"), _("Item Description"), _("Quantity"),
242 _("Received"), _("Unit"),
243 _("Required Delivery Date"), $order->tax_included ? _("Price after Tax") : _("Price before Tax"), _("Line Total"), "");
244 if ($order->trans_type != ST_PURCHORDER)
245 array_remove($th, 5);
247 if (count($order->line_items)) $th[] = '';
250 $id = find_submit('Edit');
253 foreach ($order->line_items as $line_no => $po_line)
255 $line_total = round($po_line->quantity * $po_line->price, user_price_dec());
256 if (!$editable || ($id != $line_no))
258 alt_table_row_color($k);
259 label_cell($po_line->stock_id);
260 label_cell($po_line->item_description);
261 qty_cell($po_line->quantity, false, get_qty_dec($po_line->stock_id));
262 qty_cell($po_line->qty_received, false, get_qty_dec($po_line->stock_id));
263 label_cell($po_line->units);
264 if ($order->trans_type == ST_PURCHORDER)
265 label_cell($po_line->req_del_date);
266 amount_decimal_cell($po_line->price);
267 amount_cell($line_total);
271 edit_button_cell("Edit$line_no", _("Edit"),
272 _('Edit document line'));
273 delete_button_cell("Delete$line_no", _("Delete"),
274 _('Remove line from document'));
280 po_item_controls($order, $k, $line_no);
282 $total += $line_total;
285 if ($id==-1 && $editable)
286 po_item_controls($order, $k);
288 $colspan = count($th)-2;
289 if (count($order->line_items))
292 $display_sub_total = price_format($total);
294 label_row(_("Sub-total"), $display_sub_total, "colspan=$colspan align=right","align=right", 2);
296 $taxes = $order->get_taxes(input_num('freight_cost'));
298 $tax_total = display_edit_tax_items($taxes, $colspan, $order->tax_included, 2, $order->trans_type==ST_SUPPINVOICE);
300 $display_total = price_format(($total + input_num('freight_cost') + $tax_total));
303 label_cells(_("Amount Total"), $display_total, "colspan=$colspan align='right'","align='right'");
304 $order->order_no ? submit_cells('update', _("Update"), "colspan=2 align='center'", _("Refresh"), true)
305 : label_cell('', "colspan=2");
312 //---------------------------------------------------------------------------------------------------
314 function display_po_summary(&$po, $is_self=false, $editable=false)
316 start_table(TABLESTYLE, "width=90%");
319 label_cells(_("Reference"), $po->reference, "class='tableheader2'");
321 label_cells(_("Supplier"), $po->supplier_name, "class='tableheader2'");
323 if (!is_company_currency($po->curr_code))
324 label_cells(_("Order Currency"), $po->curr_code, "class='tableheader2'");
328 label_cells(_("Purchase Order"), get_trans_view_str(ST_PURCHORDER, $po->order_no),
329 "class='tableheader2'");
333 label_cells(_("Date"), $po->orig_order_date, "class='tableheader2'");
337 if (!isset($_POST['Location']))
338 $_POST['Location'] = $po->Location;
339 label_cell(_("Deliver Into Location"), "class='tableheader2'");
340 locations_list_cells(null, 'Location', $_POST['Location']);
344 label_cells(_("Deliver Into Location"), get_location_name($po->Location),
345 "class='tableheader2'");
348 if ($po->supp_ref != "")
349 label_cells(_("Supplier's Reference"), $po->supp_ref, "class='tableheader2'");
353 label_row(_("Delivery Address"), $po->delivery_address, "class='tableheader2'",
356 if (!$editable && ($po->prep_amount > 0))
359 label_cells(_("Required Pre-Payment"), price_format($po->prep_amount), "class='tableheader2'");
360 label_cells(_("Pre-Payments Allocated"), price_format($po->alloc), "class='tableheader2'");
364 if ($po->Comments != "")
365 label_row(_("Order Comments"), $po->Comments, "class='tableheader2'",
370 //--------------------------------------------------------------------------------
372 function po_item_controls(&$order, &$rowcounter, $line_no=-1)
374 global $Ajax, $SysPrefs;
376 alt_table_row_color($rowcounter);
379 $id = find_submit('Edit');
380 if (($id != -1) && $line_no == $id)
382 // hidden('line_no', $id);
384 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
385 $dec = get_qty_dec($_POST['stock_id']);
386 $_POST['qty'] = qty_format($order->line_items[$id]->quantity, $_POST['stock_id'], $dec);
387 //$_POST['price'] = price_format($order->line_items[$id]->price);
388 $_POST['price'] = price_decimal_format($order->line_items[$id]->price, $dec2);
389 if ($order->trans_type == ST_PURCHORDER)
390 $_POST['req_del_date'] = $order->line_items[$id]->req_del_date;
392 $_POST['units'] = $order->line_items[$id]->units;
393 $_POST['item_description'] = $order->line_items[$id]->item_description;
395 hidden('stock_id', $_POST['stock_id']);
396 label_cell($_POST['stock_id']);
398 if ($order->line_items[$id]->descr_editable)
399 text_cells(null,'item_description', null, 45, 150);
401 hidden('item_description', $_POST['item_description']);
402 // label_cell($_POST['item_description']);
403 label_cell($order->line_items[$id]->item_description);
406 $Ajax->activate('items_table');
407 $qty_rcvd = $order->line_items[$id]->qty_received;
411 // hidden('line_no', ($_SESSION['PO']->lines_on_order + 1));
413 //Chaitanya : Manufcatured item can be purchased
414 stock_items_list_cells(null, 'stock_id', null, false, true, false, true);
415 //stock_purchasable_items_list_cells(null, 'stock_id', null, false, true, true);
416 if (list_updated('stock_id')) {
417 $Ajax->activate('price');
418 $Ajax->activate('units');
419 $Ajax->activate('qty');
420 $Ajax->activate('req_del_date');
421 $Ajax->activate('line_total');
423 $item_info = get_item_edit_info($_POST['stock_id']);
424 $_POST['units'] = $item_info["units"];
426 $dec = $item_info["decimals"];
427 $_POST['qty'] = number_format2(get_purchase_conversion_factor ($order->supplier_id, $_POST['stock_id']), $dec);
428 //$_POST['price'] = price_format(get_purchase_price ($order->supplier_id, $_POST['stock_id']));
429 $_POST['price'] = price_decimal_format(get_purchase_price ($order->supplier_id, $_POST['stock_id']), $dec2);
430 if ($order->trans_type == ST_PURCHORDER)
431 $_POST['req_del_date'] = add_days(Today(), $SysPrefs->default_receival_required_by());
435 qty_cells(null, 'qty', null, null, null, $dec);
436 qty_cell($qty_rcvd, false, $dec);
438 label_cell($_POST['units'], '', 'units');
439 if ($order->trans_type == ST_PURCHORDER)
440 date_cells(null, 'req_del_date', '', null, 0, 0, 0);
443 amount_decimal_cell($_POST['price']);
444 hidden('price', $_POST['price']);
447 amount_cells(null, 'price', null, null, null, $dec2);
449 //$line_total = $_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc'] / 100);
450 $line_total = round(input_num('qty') * input_num('price'), user_price_dec());
451 amount_cell($line_total, false, '','line_total');
455 button_cell('UpdateLine', _("Update"),
456 _('Confirm changes'), ICON_UPDATE);
457 button_cell('CancelUpdate', _("Cancel"),
458 _('Cancel changes'), ICON_CANCEL);
459 hidden('line_no', $line_no);
464 submit_cells('EnterLine', _("Add Item"), "colspan=2 align='center'",
465 _('Add new item to document'), true);
471 //---------------------------------------------------------------------------------------------------
473 function display_po_footer(&$trans)
477 start_table(TABLESTYLE2);
479 if (list_updated('tax_algorithm')) {
480 $trans->tax_algorithm = $_POST['tax_algorithm'];
481 $Ajax->activate('items_table');
484 if ($trans->trans_type == ST_SUPPINVOICE) {
485 tax_algorithm_list_row(_("Tax algorithm:"), 'tax_algorithm', null, true);
486 cash_accounts_list_row(_("Payment:"), 'cash_account', null, false, _('Delayed'));
487 } elseif ($trans->trans_type == ST_PURCHORDER)
488 amount_row(_("Pre-Payment Required:"), 'prep_amount');
490 textarea_row(_("Memo:"), 'Comments', null, 70, 4);
494 //---------------------------------------------------------------------------------------------------