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 $_POST['cash'] = $order->cash;
215 if ($order->trans_type == 10)
217 $_POST['delivery_date'] = $order->due_date;
218 $Ajax->activate('delivery_date');
220 $Ajax->activate('Location');
221 $Ajax->activate('deliver_to');
222 $Ajax->activate('phone');
223 $Ajax->activate('delivery_address');
224 $Ajax->activate('cash');
225 // change prices if necessary
226 // what about discount in template case?
227 if ($old_order->customer_currency != $order->customer_currency) {
230 if ($old_order->sales_type != $order->sales_type) {
231 // || $old_order->default_discount!=$order->default_discount
232 $_POST['sales_type'] = $order->sales_type;
233 $Ajax->activate('sales_type');
238 set_global_customer($_POST['customer_id']);
242 if ($_SESSION['Items']->trans_type!=30) {
243 ref_cells(_("Reference").':', 'ref', _('Reference number unique for this document type'), null, '');
248 echo "</td><td>"; // outer table
250 if (!is_company_currency($order->customer_currency))
252 div_start('currency');
253 echo "<table height='5'>";
254 label_row(_("Customer Currency:"), $order->customer_currency);
255 exchange_rate_display($order->customer_currency, get_company_currency(),
256 ($editable ? $_POST['OrderDate'] : $order->document_date));
259 echo "</td><td>"; // outer table
263 echo "<table height='5'>";
264 if ($order->trans_type == 10 && $order->pos != -1) {
265 sale_payment_list_cells(_('Payment:'), 'cash', null, true);
266 $cash_payment = get_post('cash', 0);
267 // current user can issue both credit and cash invoices
268 if ($order->cash != $cash_payment) {
269 $order->cash = $cash_payment;
271 $paym = get_sales_point(user_pos());
272 $order->cash_account = $paym['pos_account'];
273 $order->account_name = $paym['bank_account_name'];
274 $_POST['Location'] = $order->Location = $paym['pos_location'];
275 $order->location_name = $paym['location_name'];
278 $Ajax->activate('delivery');
279 set_focus($order->pos == -1 ? 'delivery_date' : 'account');
282 hidden('cash', $order->cash);
285 $str = sales_types_list_row(_("Price List"), 'sales_type', null, true);
287 label_row(_("Price List:"), $order->sales_type_name);
289 if ($order->sales_type != $_POST['sales_type']) {
290 $myrow = get_sales_type($_POST['sales_type']);
291 $order->set_sales_type($myrow['id'], $myrow['sales_type'],
292 $myrow['tax_included'], $myrow['factor']);
293 $Ajax->activate('sales_type');
297 label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
300 echo "</td><td>"; // outer table
302 echo "<table height='5'>";
306 if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
307 $_POST['OrderDate'] = $order->document_date;
309 date_row($date_text, 'OrderDate',
310 _('Date of order receive'), null, 0, 0, 0, null, true);
311 if (isset($_POST['_OrderDate_changed'])) {
313 $Ajax->activate('currency');
314 if ($order->trans_type == 10) {
315 $_POST['delivery_date'] = get_invoice_duedate(get_post('customer_id'), get_post('OrderDate'));
317 $_POST['delivery_date'] = add_days(get_post('OrderDate'), sys_prefs::default_delivery_required_by());
318 $Ajax->activate('delivery_date');
323 label_row($date_text, $order->document_date);
324 hidden('OrderDate', $order->document_date);
327 if ($display_tax_group)
329 label_row(_("Tax Group:"), $order->tax_group_name);
330 hidden('tax_group_id', $_SESSION['Items']->tax_group_id);
336 end_table(1); // outer table
338 if ($change_prices != 0) {
339 foreach ($order->line_items as $line_no=>$item) {
340 $line = &$order->line_items[$line_no];
341 $line->price = get_price($line->stock_id, $order->customer_currency,
342 $order->sales_type, $order->price_factor, get_post('OrderDate'));
343 // $line->discount_percent = $order->default_discount;
345 $Ajax->activate('items_table');
348 return $customer_error;
351 //--------------------------------------------------------------------------------
353 function sales_order_item_controls(&$order, &$rowcounter, $line_no=-1)
357 alt_table_row_color($rowcounter);
359 $id = find_submit('Edit');
360 if ($line_no!=-1 && $line_no == $id)
362 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
363 $dec = get_qty_dec($_POST['stock_id']);
364 $_POST['qty'] = number_format2($order->line_items[$id]->qty_dispatched, $dec);
365 $_POST['price'] = price_format($order->line_items[$id]->price);
366 $_POST['Disc'] = percent_format($order->line_items[$id]->discount_percent*100);
367 $units = $order->line_items[$id]->units;
368 hidden('stock_id', $_POST['stock_id']);
369 label_cell($_POST['stock_id']);
370 label_cell($order->line_items[$line_no]->item_description, "nowrap");
371 $Ajax->activate('items_table');
375 stock_items_list_cells(null,'stock_id', null, false, true);
376 if (list_updated('stock_id')) {
377 $Ajax->activate('price');
378 $Ajax->activate('units');
379 $Ajax->activate('qty');
380 $Ajax->activate('line_total');
383 $item_info = get_item_edit_info($_POST['stock_id']);
384 $units = $item_info["units"];
385 $dec = get_qty_dec($_POST['stock_id']);
386 $_POST['qty'] = number_format2(1, $dec);
388 $_POST['price'] = price_format(get_price ($_POST['stock_id'],
389 $order->customer_currency, $order->sales_type,
390 $order->price_factor, get_post('OrderDate')));
391 // default to the customer's discount %
392 $_POST['Disc'] = percent_format($order->default_discount * 100);
396 qty_cells(null, 'qty', $_POST['qty'], null, null, $dec);
398 if ($order->trans_no!=0) {
399 qty_cell($line_no==-1 ? 0 :$order->line_items[$line_no]->qty_done, false, $dec);
402 label_cell($units, '', 'units');
404 $str = amount_cells(null, 'price');
406 small_amount_cells(null, 'Disc', percent_format($_POST['Disc']), null, null, user_percent_dec());
408 $line_total = input_num('qty') * input_num('price') * (1 - input_num('Disc') / 100);
410 amount_cell($line_total, false, '','line_total');
414 edit_button_cell('UpdateItem', _("Update"),
415 _('Confirm changes'));
416 edit_button_cell('CancelItemChanges', _("Cancel"),
417 _('Cancel changes'));
418 hidden('LineNo', $line_no);
423 submit_cells('AddItem', _("Add Item"), "colspan=2",
424 _('Add new item to document'), true);
430 //--------------------------------------------------------------------------------
432 function display_delivery_details(&$order)
434 global $table_style2, $Ajax;
436 div_start('delivery');
438 if (get_post('cash', 0)) { // Direct payment sale
439 display_heading(_('Cash payment'));
440 echo "<table $table_style2 width=60% align=center>";
441 label_row(_("Deliver from Location:"), $order->location_name);
443 hidden('Location', $order->Location);
444 label_row(_("Cash account:"), $order->account_name);
445 textarea_row(_("Comments:"), "Comments", $order->Comments, 31, 5);
449 if ($order->trans_type==10)
451 $title = _("Delivery Details");
452 $delname = _("Due Date").':';
454 elseif ($order->trans_type==13)
456 $title = _("Invoice Delivery Details");
457 $delname = _("Invoice before").':';
461 $title = _("Order Delivery Details");
462 $delname = _("Required Delivery Date").':';
464 display_heading($title);
466 start_table("$table_style2 width=90%");
467 echo "<tr valign=top><td>"; // outer table
470 locations_list_row(_("Deliver from Location:"), 'Location', null,
472 if (list_updated('Location'))
475 date_row($delname, 'delivery_date',
476 $order->trans_type==30 ? _('Enter requested day of delivery') : '',
477 $order->due_date, 0, 0, 0);
478 text_row(_("Deliver To:"), 'deliver_to', $order->deliver_to, 40, 40,
479 _('Additional identifier for delivery e.g. name of receiving person'));
481 textarea_row(_("Address:"), 'delivery_address', $order->delivery_address, 35, 5,
482 _('Delivery address. Default is address of customer branch'));
483 text_row(_("Contact Phone Number:"), 'phone', $order->phone, 25, 25,
484 _('Phone number of ordering person. Defaults to branch phone number'));
488 echo "</td><td>"; // outer table
492 text_row(_("Customer Reference:"), 'cust_ref', $order->cust_ref, 25, 25,
493 _('Customer reference number for this order (if any)'));
494 textarea_row(_("Comments:"), "Comments", $order->Comments, 31, 5);
496 small_amount_row(_("Shipping Charge:"), 'freight_cost',
497 price_format(get_post('freight_cost',0)));
499 shippers_list_row(_("Shipping Company:"), 'ship_via', $order->ship_via);
502 end_table(1); // outer table
507 function check_qoh($order)
510 foreach($order->line_items as $line_no => $line) {
511 $qoh = get_qoh_on_date($line->stock_id, $_POST['Location'], $_POST['OrderDate']);
512 if ($line->qty_dispatched > $qoh) {
513 $msg .= $line->stock_id . " - " . $line->item_description . ": " .
514 _("Quantity On Hand") . " = "
515 . number_format2($qoh, get_qty_dec($line->stock_id)) . '<br>';
519 display_error(_("The delivery cannot be processed because there is an insufficient quantity for item:")