3 include_once($path_to_root . "/sales/includes/cart_class.inc");
4 include_once($path_to_root . "/includes/manufacturing.inc");
6 //--------------------------------------------------------------------------------
7 function add_to_order(&$order, $new_item, $new_item_qty, $price, $discount)
10 foreach ($order->line_items AS $order_item)
12 if (strcasecmp($order_item->stock_id, $new_item) == 0)
14 display_notification(_("For Part :") . $new_item . " " . _("This item is already on this order. You have been warned."));
19 $order->add_to_cart (count($order->line_items),$new_item, $new_item_qty, $price, $discount);
22 //---------------------------------------------------------------------------------
24 function get_customer_details_to_order(&$order, $customer_id, $branch_id)
28 $myrow = get_customer_to_order($customer_id);
30 $name = $myrow['name'];
32 if ($myrow['dissallow_invoices'] == 1)
33 $ret_error = _("The selected customer account is currently on hold. Please contact the credit control personnel to discuss.");
35 $deliver = $myrow['address']; // in case no delivery address.
37 $order->set_customer($customer_id, $name, $myrow['curr_code'],
38 $myrow['discount'], $myrow['pymt_discount']);
40 // the sales type determines the price list to be used by default
41 $order->set_sales_type($myrow['salestype'], $myrow['sales_type'], $myrow['tax_included'],
44 $result = get_branch_to_order($customer_id, $branch_id);
46 if (db_num_rows($result) == 0)
48 return _("The selected customer and branch are not valid, or the customer does not have any branches.");
51 $myrow = db_fetch($result);
53 $order->set_branch($branch_id, $myrow["tax_group_id"],
54 $myrow["tax_group_name"], $myrow["phone"], $myrow["email"]);
56 $address = $myrow["br_post_address"];
58 if (strlen($address) <= 1) // if branch has no address
59 $address = $deliver; // set sales order address
60 $order->set_delivery($myrow["default_ship_via"], $myrow["br_name"],
62 if ($order->trans_type == 10) {
63 $order->due_date = get_invoice_duedate($customer_id, $order->document_date);
64 if ($order->pos != -1)
65 $order->cash = date_diff($order->due_date, Today(), 'd')<2;
68 if($order->pos != -1) {
69 $paym = get_sales_point($order->pos);
70 $order->set_location($paym["pos_location"], $paym["location_name"]);
73 $order->set_location($myrow["default_location"], $myrow["location_name"]);
78 //---------------------------------------------------------------------------------
80 function display_order_summary($title, &$order, $editable_items=false)
82 global $table_style, $path_to_root;
84 display_heading($title);
86 div_start('items_table');
87 start_table("$table_style colspan=7 width=90%");
88 $th = array(_("Item Code"), _("Item Description"), _("Quantity"),
90 _("Unit"), _("Price"), _("Discount %"), _("Total"), "");
92 if ($order->trans_no == 0) {
96 if (count($order->line_items))
102 $k = 0; //row colour counter
104 $id = find_submit('Edit');
105 foreach ($order->line_items as $line_no=>$stock_item)
108 $line_total = round($stock_item->qty_dispatched * $stock_item->price * (1 - $stock_item->discount_percent),
112 if (!$editable_items || $id != $line_no)
114 alt_table_row_color($k);
116 view_stock_status_cell($stock_item->stock_id);
118 label_cell($stock_item->item_description, "nowrap" );
119 qty_cell($stock_item->qty_dispatched, false, get_qty_dec($stock_item->stock_id));
121 if ($order->trans_no!=0)
122 amount_cell($stock_item->qty_done);
124 label_cell($stock_item->units);
125 amount_cell($stock_item->price);
127 percent_cell($stock_item->discount_percent * 100);
128 amount_cell($line_total);
132 edit_button_cell("Edit$line_no", _("Edit"),
133 _('Edit document line'));
134 edit_button_cell("Delete$line_no", _("Delete"),
135 _('Remove line from document'));
141 sales_order_item_controls($order, $k, $line_no);
144 $total += $line_total;
147 if ($id==-1 && $editable_items)
148 sales_order_item_controls($order, $k);
150 $display_total = price_format($total);
151 label_row(_("Total Excluding Shipping"), $display_total, "colspan=6 align=right",
152 "nowrap align=right", 2);
158 // ------------------------------------------------------------------------------
160 function display_order_header(&$order, $editable, $date_text, $display_tax_group=false)
162 global $table_style, $Ajax;
164 start_table("width=80% $table_style");
165 echo "<tr><td valign=top>"; // outer table
168 $customer_error = "";
171 if (isset($order) && !$editable)
173 // can't change the customer/branch if items already received on this order
174 echo $order->customer_name . " - " . $order->deliver_to;
175 hidden('customer_id', $order->customer_id);
176 hidden('branch_id', $order->Branch);
177 hidden('sales_type', $order->sales_type);
181 customer_list_row(_("Customer:"), 'customer_id', null, false, true);
183 if ($order->customer_id != get_post('customer_id', -1))
185 // customer has changed
186 $Ajax->activate('branch_id');
188 customer_branches_list_row(_("Branch:"),
189 $_POST['customer_id'], 'branch_id', null, false, true, true);
191 if( ($order->customer_id != get_post('customer_id', -1)) ||
192 ($order->Branch != get_post('branch_id', -1)) ||
193 list_updated('customer_id')) {
195 if (!isset($_POST['branch_id']) || $_POST['branch_id'] == "")
197 // ignore errors on customer search box call
198 if ($_POST['customer_id'] == '')
199 $customer_error = _("No customer found for entered text.");
201 $customer_error = _("The selected customer does not have any branches. Please create at least one branch.");
202 unset($_POST['branch_id']);
207 $old_order = (PHP_VERSION<5) ? $order : clone( $order );
208 $customer_error = get_customer_details_to_order($order, $_POST['customer_id'], $_POST['branch_id']);
210 $_POST['Location'] = $order->Location;
211 $_POST['deliver_to'] = $order->deliver_to;
212 $_POST['delivery_address'] = $order->delivery_address;
213 $_POST['phone'] = $order->phone;
214 if (get_post('cash') !== $order->cash) {
215 $_POST['cash'] = $order->cash;
216 $Ajax->activate('delivery');
217 $Ajax->activate('cash');
219 if ($order->trans_type == 10)
221 $_POST['delivery_date'] = $order->due_date;
222 $Ajax->activate('delivery_date');
224 $Ajax->activate('Location');
225 $Ajax->activate('deliver_to');
226 $Ajax->activate('phone');
227 $Ajax->activate('delivery_address');
229 // change prices if necessary
230 // what about discount in template case?
231 if ($old_order->customer_currency != $order->customer_currency) {
234 if ($old_order->sales_type != $order->sales_type) {
235 // || $old_order->default_discount!=$order->default_discount
236 $_POST['sales_type'] = $order->sales_type;
237 $Ajax->activate('sales_type');
242 set_global_customer($_POST['customer_id']);
246 if ($_SESSION['Items']->trans_type!=30) {
247 ref_cells(_("Reference").':', 'ref', _('Reference number unique for this document type'), null, '');
252 echo "</td><td>"; // outer table
254 if (!is_company_currency($order->customer_currency))
256 div_start('currency');
257 echo "<table height='5'>";
258 label_row(_("Customer Currency:"), $order->customer_currency);
259 exchange_rate_display($order->customer_currency, get_company_currency(),
260 ($editable ? $_POST['OrderDate'] : $order->document_date));
263 echo "</td><td>"; // outer table
267 echo "<table height='5'>";
268 if ($order->trans_type == 10 && $order->pos != -1) {
269 sale_payment_list_cells(_('Payment:'), 'cash', null, true);
270 $cash_payment = get_post('cash', 0);
271 // current user can issue both credit and cash invoices
272 if ($order->cash != $cash_payment) {
273 $order->cash = $cash_payment;
275 $paym = get_sales_point(user_pos());
276 $order->cash_account = $paym['pos_account'];
277 $order->account_name = $paym['bank_account_name'];
278 $_POST['Location'] = $order->Location = $paym['pos_location'];
279 $order->location_name = $paym['location_name'];
282 $Ajax->activate('delivery');
283 set_focus($order->pos == -1 ? 'delivery_date' : 'account');
286 hidden('cash', $order->cash);
289 $str = sales_types_list_row(_("Price List"), 'sales_type', null, true);
291 label_row(_("Price List:"), $order->sales_type_name);
293 if ($order->sales_type != $_POST['sales_type']) {
294 $myrow = get_sales_type($_POST['sales_type']);
295 $order->set_sales_type($myrow['id'], $myrow['sales_type'],
296 $myrow['tax_included'], $myrow['factor']);
297 $Ajax->activate('sales_type');
301 label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
304 echo "</td><td>"; // outer table
306 echo "<table height='5'>";
310 if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
311 $_POST['OrderDate'] = $order->document_date;
313 date_row($date_text, 'OrderDate',
314 _('Date of order receive'), null, 0, 0, 0, null, true);
315 if (isset($_POST['_OrderDate_changed'])) {
317 $Ajax->activate('currency');
318 if ($order->trans_type == 10) {
319 $_POST['delivery_date'] = get_invoice_duedate(get_post('customer_id'), get_post('OrderDate'));
321 $_POST['delivery_date'] = add_days(get_post('OrderDate'), sys_prefs::default_delivery_required_by());
322 $Ajax->activate('delivery_date');
327 label_row($date_text, $order->document_date);
328 hidden('OrderDate', $order->document_date);
331 if ($display_tax_group)
333 label_row(_("Tax Group:"), $order->tax_group_name);
334 hidden('tax_group_id', $_SESSION['Items']->tax_group_id);
340 end_table(1); // outer table
342 if ($change_prices != 0) {
343 foreach ($order->line_items as $line_no=>$item) {
344 $line = &$order->line_items[$line_no];
345 $line->price = get_price($line->stock_id, $order->customer_currency,
346 $order->sales_type, $order->price_factor, get_post('OrderDate'));
347 // $line->discount_percent = $order->default_discount;
349 $Ajax->activate('items_table');
352 return $customer_error;
355 //--------------------------------------------------------------------------------
357 function sales_order_item_controls(&$order, &$rowcounter, $line_no=-1)
361 alt_table_row_color($rowcounter);
363 $id = find_submit('Edit');
364 if ($line_no!=-1 && $line_no == $id)
366 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
367 $dec = get_qty_dec($_POST['stock_id']);
368 $_POST['qty'] = number_format2($order->line_items[$id]->qty_dispatched, $dec);
369 $_POST['price'] = price_format($order->line_items[$id]->price);
370 $_POST['Disc'] = percent_format($order->line_items[$id]->discount_percent*100);
371 $units = $order->line_items[$id]->units;
372 hidden('stock_id', $_POST['stock_id']);
373 label_cell($_POST['stock_id']);
374 label_cell($order->line_items[$line_no]->item_description, "nowrap");
375 $Ajax->activate('items_table');
379 stock_items_list_cells(null,'stock_id', null, false, true);
380 if (list_updated('stock_id')) {
381 $Ajax->activate('price');
382 $Ajax->activate('units');
383 $Ajax->activate('qty');
384 $Ajax->activate('line_total');
387 $item_info = get_item_edit_info($_POST['stock_id']);
388 $units = $item_info["units"];
389 $dec = get_qty_dec($_POST['stock_id']);
390 $_POST['qty'] = number_format2(1, $dec);
392 $_POST['price'] = price_format(get_price ($_POST['stock_id'],
393 $order->customer_currency, $order->sales_type,
394 $order->price_factor, get_post('OrderDate')));
395 // default to the customer's discount %
396 $_POST['Disc'] = percent_format($order->default_discount * 100);
400 qty_cells(null, 'qty', $_POST['qty'], null, null, $dec);
402 if ($order->trans_no!=0) {
403 qty_cell($line_no==-1 ? 0 :$order->line_items[$line_no]->qty_done, false, $dec);
406 label_cell($units, '', 'units');
408 $str = amount_cells(null, 'price');
410 small_amount_cells(null, 'Disc', percent_format($_POST['Disc']), null, null, user_percent_dec());
412 $line_total = input_num('qty') * input_num('price') * (1 - input_num('Disc') / 100);
414 amount_cell($line_total, false, '','line_total');
418 edit_button_cell('UpdateItem', _("Update"),
419 _('Confirm changes'));
420 edit_button_cell('CancelItemChanges', _("Cancel"),
421 _('Cancel changes'));
422 hidden('LineNo', $line_no);
427 submit_cells('AddItem', _("Add Item"), "colspan=2",
428 _('Add new item to document'), true);
434 //--------------------------------------------------------------------------------
436 function display_delivery_details(&$order)
438 global $table_style2, $Ajax;
440 div_start('delivery');
442 if (get_post('cash', 0)) { // Direct payment sale
443 display_heading(_('Cash payment'));
444 echo "<table $table_style2 width=60% align=center>";
445 label_row(_("Deliver from Location:"), $order->location_name);
447 hidden('Location', $order->Location);
448 label_row(_("Cash account:"), $order->account_name);
449 textarea_row(_("Comments:"), "Comments", $order->Comments, 31, 5);
453 if ($order->trans_type==10)
455 $title = _("Delivery Details");
456 $delname = _("Due Date").':';
458 elseif ($order->trans_type==13)
460 $title = _("Invoice Delivery Details");
461 $delname = _("Invoice before").':';
465 $title = _("Order Delivery Details");
466 $delname = _("Required Delivery Date").':';
468 display_heading($title);
470 start_table("$table_style2 width=90%");
471 echo "<tr valign=top><td>"; // outer table
474 locations_list_row(_("Deliver from Location:"), 'Location', null,
476 if (list_updated('Location'))
479 date_row($delname, 'delivery_date',
480 $order->trans_type==30 ? _('Enter requested day of delivery') : '',
481 $order->due_date, 0, 0, 0);
482 text_row(_("Deliver To:"), 'deliver_to', $order->deliver_to, 40, 40,
483 _('Additional identifier for delivery e.g. name of receiving person'));
485 textarea_row(_("Address:"), 'delivery_address', $order->delivery_address, 35, 5,
486 _('Delivery address. Default is address of customer branch'));
487 text_row(_("Contact Phone Number:"), 'phone', $order->phone, 25, 25,
488 _('Phone number of ordering person. Defaults to branch phone number'));
492 echo "</td><td>"; // outer table
496 text_row(_("Customer Reference:"), 'cust_ref', $order->cust_ref, 25, 25,
497 _('Customer reference number for this order (if any)'));
498 textarea_row(_("Comments:"), "Comments", $order->Comments, 31, 5);
500 small_amount_row(_("Shipping Charge:"), 'freight_cost',
501 price_format(get_post('freight_cost',0)));
503 shippers_list_row(_("Shipping Company:"), 'ship_via', $order->ship_via);
506 end_table(1); // outer table
511 function check_qoh($order)
514 foreach($order->line_items as $line_no => $line) {
515 $qoh = get_qoh_on_date($line->stock_id, $_POST['Location'], $_POST['OrderDate']);
516 if ($line->qty_dispatched > $qoh) {
517 $msg .= $line->stock_id . " - " . $line->item_description . ": " .
518 _("Quantity On Hand") . " = "
519 . number_format2($qoh, get_qty_dec($line->stock_id)) . '<br>';
523 display_error(_("The delivery cannot be processed because there is an insufficient quantity for item:")