3 include_once($path_to_root . "/sales/includes/cart_class.inc");
4 include_once($path_to_root . "/includes/manufacturing.inc");
6 //--------------------------------------------------------------------------------
8 function add_to_order(&$order, $line_no, $new_item, $new_item_qty, $price, $discount)
11 $already_on_order = 0;
13 foreach ($order->line_items AS $order_item)
15 if (strcasecmp($order_item->stock_id, $new_item) == 0)
17 $already_on_order = 1;
18 display_error(_("For Part :") . $new_item . " " . "This item is already on this order. You can change the quantity ordered of the existing line if necessary.");
23 if ($already_on_order != 1)
26 $order->add_to_cart ($line_no, 0, $new_item, $new_item_qty, $price, $discount);
29 //---------------------------------------------------------------------------------
31 function get_customer_details_to_order(&$order, $customer_id, $branch_id)
34 // Now check to ensure this account is not on hold */
35 $sql = "SELECT ".TB_PREF."debtors_master.name, ".TB_PREF."debtors_master.address, ".TB_PREF."credit_status.dissallow_invoices,
36 ".TB_PREF."debtors_master.sales_type AS salestype, ".TB_PREF."sales_types.sales_type, ".TB_PREF."debtors_master.curr_code,
37 ".TB_PREF."debtors_master.discount
38 FROM ".TB_PREF."debtors_master, ".TB_PREF."credit_status, ".TB_PREF."sales_types
39 WHERE ".TB_PREF."debtors_master.sales_type=".TB_PREF."sales_types.id
40 AND ".TB_PREF."debtors_master.credit_status=".TB_PREF."credit_status.id
41 AND ".TB_PREF."debtors_master.debtor_no = '" . $customer_id . "'";
43 $result =db_query($sql,"Customer Record Retreive");
45 $myrow = db_fetch($result);
47 $order->customer_id = $customer_id;
48 $order->Branch = $branch_id;
49 $order->customer_name = $myrow['name'];
51 if ($myrow['dissallow_invoices'] == 1)
52 $ret_error = _("The selected customer account is currently on hold. Please contact the credit control personnel to discuss.");
54 if (!isset($_POST['branch_id']) || $_POST['branch_id'] == "")
56 $ret_error = _("The selected customer does not have any branches. Please create at least one branch.");
57 unset($_POST['branch_id']);
61 # the sales type determines the price list to be used by default
62 $order->default_sales_type = $myrow['salestype'];
63 $order->sales_type_name = $myrow['sales_type'];
64 $order->customer_currency = $myrow['curr_code'];
65 $order->default_discount = $myrow['discount'];
66 $deliver = $myrow['address']; // in case no deliveraddress.
68 if ($order->Branch != "")
70 # the branch was also selected from the customer selection so default the delivery details from the customer branches table cust_branch. The order process will ask for branch details later anyway
71 $sql = "SELECT ".TB_PREF."cust_branch.br_name, ".TB_PREF."cust_branch.br_post_address, phone, email,
72 default_location, default_ship_via,
73 ".TB_PREF."tax_groups.name AS tax_group_name, ".TB_PREF."tax_groups.id AS tax_group_id
74 FROM ".TB_PREF."cust_branch, ".TB_PREF."tax_groups
75 WHERE ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
76 AND ".TB_PREF."cust_branch.branch_code='" . $order->Branch . "'
77 AND ".TB_PREF."cust_branch.debtor_no = '" . $order->customer_id . "'";
79 $result = db_query($sql,"Customer Branch Record Retreive");
81 if (db_num_rows($result) == 0)
83 return _("The selected customer and branch are not valid, or the customer does not have any branches.");
86 $myrow = db_fetch($result);
87 $order->deliver_to = $myrow["br_name"];
88 $order->delivery_address = $myrow["br_post_address"];
89 if (strlen($order->delivery_address) <= 1)
90 $order->delivery_address = $deliver;
91 $order->phone = $myrow["phone"];
92 $order->email = $myrow["email"];
93 $order->Location = $myrow["default_location"];
94 $order->ship_via = $myrow["default_ship_via"];
95 $order->tax_group_name = $myrow["tax_group_name"];
96 $order->tax_group_id = $myrow["tax_group_id"];
102 //---------------------------------------------------------------------------------
104 function display_order_summary($title, &$order, $editable_items=false)
106 global $table_style, $path_to_root;
108 display_heading($title);
109 start_table("$table_style colspan=7 width=90%");
110 $th = array(_("Item Code"), _("Item Description"), _("Quantity"),
111 _("Unit"), _("Price"), _("Discount %"), _("Total"), "", "");
115 $k = 0; //row colour counter
117 foreach ($order->line_items as $line)
119 if ($line->Deleted == false)
121 $line_total = $line->quantity * $line->price * (1 - $line->discount_percent);
123 if (!isset($_GET['Edit']))
127 if (!$editable_items || $id != $line->line_no)
129 alt_table_row_color($k);
131 view_stock_status_cell($line->stock_id);
133 label_cell($line->item_description);
134 qty_cell($line->quantity);
135 label_cell($line->units);
136 amount_cell($line->price);
138 amount_cell($line->discount_percent * 100);
139 amount_cell($line_total);
143 edit_link_cell(SID . "Edit=$line->line_no");
144 delete_link_cell(SID . "Delete=$line->line_no");
150 sales_order_item_controls($order, $line->stock_id);
153 $total += $line_total;
157 if (!isset($_GET['Edit']) && $editable_items)
158 sales_order_item_controls($order);
160 $display_total = number_format2($total,user_price_dec());
161 label_row(_("Total Excluding Tax/Shipping"), $display_total, "colspan=6 align=right",
162 "nowrap align=right");
167 // ------------------------------------------------------------------------------
169 function display_order_header(&$order, $editable, $date_text, $display_tax_group=false)
172 start_table("width=80% $table_style");
173 echo "<tr><td valign=top>"; // outer table
176 $customer_error = "";
178 if (isset($order) && !$editable)
180 // can't change the customer/branch if items already received on this order
181 echo $order->customer_name . " - " . $order->deliver_to;
186 if (!isset($_POST['customer_id']) && (get_global_customer() != reserved_words::get_all()))
187 $_POST['customer_id'] = get_global_customer();
189 customer_list_row(_("Customer:"), 'customer_id', null, false, true);
191 if (!isset($_POST['delivery_date']))
193 if ($order->direct_invoice)
194 $_POST['delivery_date'] = $_SESSION['Items']->delivery_date =
195 get_invoice_duedate($_POST['customer_id'], $_POST['OrderDate']);
197 $_POST['delivery_date'] = $_SESSION['Items']->delivery_date =
198 add_days($_POST['OrderDate'], 10);
200 if ($order->customer_id != $_POST['customer_id'])
202 // customer has changed
204 // delete all the order items - drastic but necessary because of
205 // change of currency, sales type, etc
206 $order->clear_items();
208 // clear the branch selection
209 unset($_POST['branch_id']);
212 customer_branches_list_row(_("Branch:"), $_POST['customer_id'], 'branch_id', null, false, true, true);
214 if (!isset($_POST['branch_id']))
215 $_POST['branch_id'] = "";
216 //set_global_customer($_POST['customer_id']);
217 if (($order->customer_id != $_POST['customer_id']) ||
218 ($order->Branch != $_POST['branch_id']))
219 $customer_error = get_customer_details_to_order($order, $_POST['customer_id'], $_POST['branch_id']);
221 set_global_customer($_POST['customer_id']);
225 echo "</td><td>"; // outer table
227 if (!is_company_currency($order->customer_currency))
229 echo "<table height='5'>";
230 label_row(_("Customer Currency:"), $order->customer_currency);
231 exchange_rate_display($order->customer_currency, get_company_currency(),
232 ($editable?$_POST['OrderDate']:$order->orig_order_date), $editable);
234 echo "</td><td>"; // outer table
237 echo "<table height='5'>";
238 label_row(_("Sales Type:"), $order->sales_type_name);
239 label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
242 echo "</td><td>"; // outer table
244 echo "<table height='5'>";
248 if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
249 $_POST['OrderDate'] = $order->orig_order_date;
251 date_row($date_text, 'OrderDate');
255 label_row($date_text, $order->orig_order_date);
256 hidden('OrderDate', $order->orig_order_date);
259 if ($display_tax_group)
263 if (!isset($_POST['tax_group_id']))
264 $_POST['tax_group_id'] = $_SESSION['Items']->tax_group_id;
265 tax_groups_list_row(_("Tax Group:"), 'tax_group_id', $_POST['tax_group_id'], true);
269 label_row(_("Tax Group:"), $order->tax_group_name);
270 hidden('tax_group_id', $_SESSION['Items']->tax_group_id);
277 end_table(1); // outer table
279 return $customer_error;
282 //--------------------------------------------------------------------------------
284 function sales_order_item_controls(&$order, $stock_id=null)
288 if (isset($_GET['Edit']) && $stock_id != null)
290 $line_no = $_GET['Edit'];
291 hidden('line_no', $line_no);
293 if (!isset($_POST['stock_id']))
294 $_POST['stock_id'] = $order->line_items[$line_no]->stock_id;
295 if (!isset($_POST['qty']) || ($_POST['qty'] == ""))
296 $_POST['qty'] = $order->line_items[$line_no]->quantity;
297 if (!isset($_POST['price']) || ($_POST['price'] == ""))
298 $_POST['price'] = $order->line_items[$line_no]->price;
299 if (!isset($_POST['Disc']) || ($_POST['Disc'] == ""))
300 $_POST['Disc'] = ($order->line_items[$line_no]->discount_percent)*100;
302 $_POST['units'] = $order->line_items[$line_no]->units;
304 hidden('stock_id', $_POST['stock_id']);
305 label_cell($_POST['stock_id']);
306 label_cell($order->line_items[$line_no]->item_description);
310 hidden('line_no', ($_SESSION['Items']->lines_on_order + 1));
311 $no_item_list = get_company_pref('no_item_list');
314 echo "<td colspan=2>\n";
315 stock_items_list('stock_id', null, false, true);
320 text_cells(null, "StockID2", "", 12, 10, "", "", "onkeyup='recalcAccounts();' onKeyDown='if(event.keyCode==13) event.keyCode=9;' onblur='return setAccount(0, true);'");
321 stock_items_list_cells(null, 'stock_id', null, false, false, "onchange='return setAccount(1, true)'");
323 $item_info = get_item_edit_info($_POST['stock_id']);
324 $_POST['units'] = $item_info["units"];
327 $_POST['price'] = get_price ($_POST['stock_id'], $order->customer_id);
328 // default to the customer's discount %
329 $_POST['Disc'] = $order->default_discount * 100;
332 text_cells(null, 'qty', $_POST['qty'], 12, 15);
334 label_cell($_POST['units']);
335 amount_cells(null, 'price');
336 text_cells(null, 'Disc', $_POST['Disc'], 7, 5);
338 $line_total = $_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc'] / 100);
339 amount_cell($line_total);
341 if (isset($_GET['Edit']))
343 submit_cells('UpdateItem', _("Update"));
344 submit_cells('CancelItemChanges', _("Cancel"));
348 submit_cells('AddItem', _("Add Item"), "colspan=2");
354 //--------------------------------------------------------------------------------
356 function display_delivery_details(&$order)
358 global $table_style2;
360 if ($order->direct_invoice)
362 $title = _("Invoice Delivery Details");
363 $delname = _("Due Date");
367 $title = _("Order Delivery Details");
368 $delname = _("Required Delivery Date:");
370 display_heading($title);
372 start_table("$table_style2 width=90%");
373 echo "<tr valign=top><td>"; // outer table
376 locations_list_row(_("Deliver from Location:"), 'Location', $order->Location);
378 date_row($delname, 'delivery_date', $order->delivery_date, 0, 0, 0);
380 text_row(_("Deliver To:"), 'deliver_to', $order->deliver_to, 40, 40);
382 textarea_row(_("Address:"), 'delivery_address', $order->delivery_address, 35, 5);
383 text_row(_("Contact Phone Number:"), 'phone', $order->phone, 25, 25);
387 echo "</td><td>"; // outer table
391 text_row(_("Customer Reference:"), 'cust_ref', $order->cust_ref, 25, 25);
392 textarea_row(_("Comments:"), "Comments", $order->Comments, 31, 5);
394 text_row(_("Shipping Charge:"), 'freight_cost', $order->freight_cost, 10, 10);
396 shippers_list_row(_("Shipping Company:"), 'ship_via', $order->ship_via);
397 if ($_SESSION['Items']->direct_invoice)
398 textarea_row(_("Memo"), 'InvoiceText', null, 31, 3);
402 end_table(1); // outer table