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 $_POST['cash_account'] = $cart->cash_account;
33 $_POST['dimension'] = $cart->dimension;
34 $_POST['dimension2'] = $cart->dimension2;
35 foreach($cart->tax_overrides as $id => $value)
36 $_POST['mantax'][$id] = price_format($value);
39 function copy_to_cart()
41 $cart = &$_SESSION['PO'];
43 $cart->supplier_id = $_POST['supplier_id'];
44 $cart->orig_order_date = $_POST['OrderDate'];
45 if ($cart->trans_type == ST_SUPPINVOICE)
47 $cart->due_date = $_POST['due_date'];
48 $cart->cash_account = get_post('cash_account');
50 $cart->reference = $_POST['ref'];
51 $cart->supp_ref = $_POST['supp_ref'];
52 $cart->Comments = $_POST['Comments'];
53 $cart->Location = $_POST['StkLocation'];
54 $cart->delivery_address = $_POST['delivery_address'];
55 $cart->dimension = @$_POST['dimension'];
56 $cart->dimension2 = @$_POST['dimension2'];
57 if (isset($_POST['tax_algorithm']))
58 $cart->tax_algorithm = $_POST['tax_algorithm'];
59 $cart->prep_amount = input_num('prep_amount', 0);
60 $cart->ex_rate = input_num('_ex_rate', null);
62 if (isset($_POST['mantax'])) {
63 foreach($_POST['mantax'] as $id => $tax) {
64 $cart->tax_overrides[$id] = user_numeric($_POST['mantax'][$id]); }
68 //---------------------------------------------------------------------------------------------------
70 function create_new_po($trans_type, $trans_no)
74 if (isset($_SESSION['PO']))
75 unset ($_SESSION['PO']->line_items, $_SESSION['PO']);
77 $cart = new purch_order;
78 $_POST['OrderDate'] = new_doc_date();
79 if (!is_date_in_fiscalyear($_POST['OrderDate']))
80 $_POST['OrderDate'] = end_fiscalyear();
81 $cart->due_date = $cart->orig_order_date = $_POST['OrderDate'];
83 $cart->trans_type = $trans_type;
84 $cart->order_no = $trans_no;
85 /*read in all the selected order into the Items cart */
87 read_po($trans_no, $cart);
88 $cart->order_no = $trans_no;
90 $cart->reference = $Refs->get_next($trans_type);
91 $_SESSION['PO'] = &$cart;
94 //---------------------------------------------------------------------------------------------------
96 function display_po_header(&$order)
100 $editable = ($order->order_no == 0);
102 start_outer_table(TABLESTYLE2, "width='80%'");
107 if (!isset($_POST['supplier_id']) && (get_global_supplier() != ALL_TEXT))
108 $_POST['supplier_id'] = get_global_supplier();
110 supplier_list_row(_("Supplier:"), 'supplier_id', null, false, true, false, true);
114 hidden('supplier_id', $order->supplier_id);
115 label_row(_("Supplier:"), $order->supplier_name);
118 if ($order->supplier_id != get_post('supplier_id',-1)) {
119 $old_supp = $order->supplier_id;
120 get_supplier_details_to_order($order, $_POST['supplier_id']);
121 get_duedate_from_terms($order);
122 $_POST['due_date'] = $order->due_date;
123 $_POST['tax_algorithm'] = $order->tax_algorithm;
125 // supplier default price update
126 foreach ($order->line_items as $line_no=>$item) {
127 $line = &$order->line_items[$line_no];
128 $line->price = get_purchase_price ($order->supplier_id, $line->stock_id);
130 $line->quantity/get_purchase_conversion_factor ($old_supp, $line->stock_id)
131 *get_purchase_conversion_factor ($order->supplier_id, $line->stock_id);
133 $Ajax->activate('items_table');
134 $Ajax->activate('due_date');
135 $Ajax->activate('tax_algorithm');
137 set_global_supplier($_POST['supplier_id']);
139 date_row($order->trans_type==ST_PURCHORDER ? _("Order Date:") :
140 ($order->trans_type==ST_SUPPRECEIVE ? _("Delivery Date:") : _("Invoice Date:")),
141 'OrderDate', '', true, 0, 0, 0, null, true);
143 if (isset($_POST['_OrderDate_changed'])) {
144 $order->orig_order_date = $_POST['OrderDate'];
145 get_duedate_from_terms($order);
146 $_POST['due_date'] = $order->due_date;
147 $Ajax->activate('due_date');
149 supplier_credit_row($order->supplier_id, $order->credit);
151 if (!is_company_currency($order->curr_code))
153 label_row(_("Supplier Currency:"), $order->curr_code);
154 exchange_rate_display(get_company_currency(), $order->curr_code,
155 $_POST['OrderDate']);
160 ref_row(_("Reference:"), 'ref');
164 hidden('ref', $order->reference);
165 label_row(_("Reference:"), $order->reference);
170 if ($order->trans_type==ST_SUPPINVOICE)
171 date_row(_("Due Date:"), 'due_date', '', false, 0, 0, 0, null, true);
173 text_row(_("Supplier's Reference:"), 'supp_ref', null, 16, 15);
175 if (get_company_pref('use_dimension'))
176 dimensions_list_row(_('Dimension').':', 'dimension', null, true, _('Default'), false, 1);
177 if (get_company_pref('use_dimension') == 2)
178 dimensions_list_row(_('Dimension 2').':', 'dimension2', null, true, _('Default'), false, 2);
179 locations_list_row(_("Receive Into:"), 'StkLocation', null, false, true);
183 if (!isset($_POST['StkLocation']) || $_POST['StkLocation'] == "" ||
184 isset($_POST['_StkLocation_update']) || !isset($_POST['delivery_address']) ||
185 $_POST['delivery_address'] == "")
187 /*If this is the first time the form loaded set up defaults */
189 $loc_row = get_item_location(get_post('StkLocation'));
192 $_POST['delivery_address'] = $loc_row["delivery_address"];
193 $Ajax->activate('delivery_address');
194 $_SESSION['PO']->Location = $_POST['StkLocation'];
195 $_SESSION['PO']->delivery_address = $_POST['delivery_address'];
199 { /*The default location of the user is crook */
200 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."));
204 textarea_row(_("Deliver to:"), 'delivery_address', $_POST['delivery_address'], 35, 4);
206 end_outer_table(); // outer table
209 //---------------------------------------------------------------------------------------------------
211 function display_po_items(&$order, $editable=true)
213 display_heading(_("Order Items"));
215 div_start('items_table');
216 start_table(TABLESTYLE, "width='80%'");
218 $th = array(_("Item Code"), _("Item Description"), _("Quantity"),
219 _("Received"), _("Unit"),
220 _("Required Delivery Date"), $order->tax_included ? _("Price after Tax") : _("Price before Tax"), _("Line Total"), "");
221 if ($order->trans_type != ST_PURCHORDER)
222 array_remove($th, 5);
224 if (count($order->line_items)) $th[] = '';
227 $id = find_submit('Edit');
230 foreach ($order->line_items as $line_no => $po_line)
232 $line_total = round($po_line->quantity * $po_line->price, user_price_dec());
233 if (!$editable || ($id != $line_no))
235 alt_table_row_color($k);
236 label_cell($po_line->stock_id);
237 label_cell($po_line->item_description);
238 qty_cell($po_line->quantity, false, get_qty_dec($po_line->stock_id));
239 qty_cell($po_line->qty_received, false, get_qty_dec($po_line->stock_id));
240 label_cell($po_line->units);
241 if ($order->trans_type == ST_PURCHORDER)
242 label_cell($po_line->req_del_date);
243 amount_decimal_cell($po_line->price);
244 amount_cell($line_total);
248 edit_button_cell("Edit$line_no", _("Edit"),
249 _('Edit document line'));
250 delete_button_cell("Delete$line_no", _("Delete"),
251 _('Remove line from document'));
257 po_item_controls($order, $k, $line_no);
259 $total += $line_total;
262 if ($id==-1 && $editable)
263 po_item_controls($order, $k);
265 $colspan = count($th)-2;
266 if (count($order->line_items))
269 $display_sub_total = price_format($total);
271 label_row(_("Sub-total"), $display_sub_total, "colspan=$colspan align=right","align=right", 2);
273 $taxes = $order->get_taxes(input_num('freight_cost'));
275 $tax_total = display_edit_tax_items($taxes, $colspan, $order->tax_included, 2, $order->trans_type==ST_SUPPINVOICE);
277 $display_total = price_format(($total + input_num('freight_cost') + $tax_total));
280 label_cells(_("Amount Total"), $display_total, "colspan=$colspan align='right'","align='right'");
281 $order->order_no ? submit_cells('update', _("Update"), "colspan=2 align='center'", _("Refresh"), true)
282 : label_cell('', "colspan=2");
289 //---------------------------------------------------------------------------------------------------
291 function display_po_summary(&$po, $is_self=false, $editable=false)
293 start_table(TABLESTYLE, "width='90%'");
296 label_cells(_("Reference"), $po->reference, "class='tableheader2'");
298 label_cells(_("Supplier"), $po->supplier_name, "class='tableheader2'");
300 if (!is_company_currency($po->curr_code))
301 label_cells(_("Order Currency"), $po->curr_code, "class='tableheader2'");
305 label_cells(_("Purchase Order"), get_trans_view_str(ST_PURCHORDER, $po->order_no),
306 "class='tableheader2'");
310 label_cells(_("Date"), $po->orig_order_date, "class='tableheader2'");
314 if (!isset($_POST['Location']))
315 $_POST['Location'] = $po->Location;
316 label_cell(_("Deliver Into Location"), "class='tableheader2'");
317 locations_list_cells(null, 'Location', $_POST['Location']);
321 label_cells(_("Deliver Into Location"), get_location_name($po->Location),
322 "class='tableheader2'");
325 if ($po->supp_ref != "")
326 label_cells(_("Supplier's Reference"), $po->supp_ref, "class='tableheader2'");
330 label_row(_("Delivery Address"), $po->delivery_address, "class='tableheader2'",
333 if (!$editable && ($po->prep_amount > 0))
336 label_cells(_("Required Pre-Payment"), price_format($po->prep_amount), "class='tableheader2'");
337 label_cells(_("Pre-Payments Allocated"), price_format($po->alloc), "class='tableheader2'");
341 if ($po->Comments != "")
342 label_row(_("Order Comments"), $po->Comments, "class='tableheader2'",
347 //--------------------------------------------------------------------------------
349 function po_item_controls(&$order, &$rowcounter, $line_no=-1)
351 global $Ajax, $SysPrefs;
353 alt_table_row_color($rowcounter);
356 $id = find_submit('Edit');
357 if (($id != -1) && $line_no == $id)
359 // hidden('line_no', $id);
361 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
362 $dec = get_qty_dec($_POST['stock_id']);
363 $_POST['qty'] = qty_format($order->line_items[$id]->quantity, $_POST['stock_id'], $dec);
364 //$_POST['price'] = price_format($order->line_items[$id]->price);
365 $_POST['price'] = price_decimal_format($order->line_items[$id]->price, $dec2);
366 if ($order->trans_type == ST_PURCHORDER)
367 $_POST['req_del_date'] = $order->line_items[$id]->req_del_date;
369 $_POST['units'] = $order->line_items[$id]->units;
370 $_POST['item_description'] = $order->line_items[$id]->item_description;
372 hidden('stock_id', $_POST['stock_id']);
373 label_cell($_POST['stock_id']);
375 if ($order->line_items[$id]->descr_editable)
376 text_cells(null,'item_description', null, 45, 150);
378 hidden('item_description', $_POST['item_description']);
379 // label_cell($_POST['item_description']);
380 label_cell($order->line_items[$id]->item_description);
383 $Ajax->activate('items_table');
384 $qty_rcvd = $order->line_items[$id]->qty_received;
388 // hidden('line_no', ($_SESSION['PO']->lines_on_order + 1));
390 //Chaitanya : Manufcatured item can be purchased
391 stock_items_list_cells(null, 'stock_id', null, false, true, false, true);
392 //stock_purchasable_items_list_cells(null, 'stock_id', null, false, true, true);
393 if (list_updated('stock_id')) {
394 $Ajax->activate('price');
395 $Ajax->activate('units');
396 $Ajax->activate('qty');
397 $Ajax->activate('req_del_date');
398 $Ajax->activate('line_total');
400 $item_info = get_item_edit_info($_POST['stock_id']);
401 $_POST['units'] = $item_info["units"];
403 $dec = $item_info["decimals"];
404 $_POST['qty'] = number_format2(get_purchase_conversion_factor ($order->supplier_id, $_POST['stock_id']), $dec);
405 //$_POST['price'] = price_format(get_purchase_price ($order->supplier_id, $_POST['stock_id']));
406 $_POST['price'] = price_decimal_format(get_purchase_price ($order->supplier_id, $_POST['stock_id']), $dec2);
407 if ($order->trans_type == ST_PURCHORDER)
408 $_POST['req_del_date'] = add_days(Today(), $SysPrefs->default_receival_required_by());
412 qty_cells(null, 'qty', null, null, null, $dec);
413 qty_cell($qty_rcvd, false, $dec);
415 label_cell($_POST['units'], '', 'units');
416 if ($order->trans_type == ST_PURCHORDER)
417 date_cells(null, 'req_del_date', '', null, 0, 0, 0);
420 amount_decimal_cell($_POST['price']);
421 hidden('price', $_POST['price']);
424 amount_cells(null, 'price', null, null, null, $dec2);
426 //$line_total = $_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc'] / 100);
427 $line_total = round(input_num('qty') * input_num('price'), user_price_dec());
428 amount_cell($line_total, false, '','line_total');
432 button_cell('UpdateLine', _("Update"),
433 _('Confirm changes'), ICON_UPDATE);
434 button_cell('CancelUpdate', _("Cancel"),
435 _('Cancel changes'), ICON_CANCEL);
436 hidden('line_no', $line_no);
441 submit_cells('EnterLine', _("Add Item"), "colspan=2 align='center'",
442 _('Add new item to document'), true);
448 //---------------------------------------------------------------------------------------------------
450 function display_po_footer(&$trans)
454 start_table(TABLESTYLE2);
456 if (list_updated('tax_algorithm')) {
457 $trans->tax_algorithm = $_POST['tax_algorithm'];
458 $Ajax->activate('items_table');
461 if ($trans->trans_type == ST_SUPPINVOICE) {
462 tax_algorithm_list_row(_("Tax algorithm:"), 'tax_algorithm', null, true);
463 cash_accounts_list_row(_("Payment:"), 'cash_account', null, false, _('Delayed'));
464 } elseif ($trans->trans_type == ST_PURCHORDER)
465 amount_row(_("Pre-Payment Required:"), 'prep_amount');
467 textarea_row(_("Memo:"), 'Comments', null, 70, 4);
471 //---------------------------------------------------------------------------------------------------