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, $new_item, $new_item_qty, $price, $discount)
11 foreach ($order->line_items AS $order_item)
13 if (strcasecmp($order_item->stock_id, $new_item) == 0)
15 display_notification(_("For Part :") . $new_item . " " . _("This item is already on this order. You have been warned."));
20 $order->add_to_cart ($new_item, $new_item_qty, $price, $discount);
23 //---------------------------------------------------------------------------------
25 function get_customer_details_to_order(&$order, $customer_id, $branch_id)
28 // Now check to ensure this account is not on hold */
29 $sql = "SELECT ".TB_PREF."debtors_master.name, ".TB_PREF."debtors_master.address, ".TB_PREF."credit_status.dissallow_invoices,
30 ".TB_PREF."debtors_master.sales_type AS salestype, ".TB_PREF."sales_types.sales_type, ".TB_PREF."debtors_master.curr_code,
31 ".TB_PREF."debtors_master.discount
32 FROM ".TB_PREF."debtors_master, ".TB_PREF."credit_status, ".TB_PREF."sales_types
33 WHERE ".TB_PREF."debtors_master.sales_type=".TB_PREF."sales_types.id
34 AND ".TB_PREF."debtors_master.credit_status=".TB_PREF."credit_status.id
35 AND ".TB_PREF."debtors_master.debtor_no = '" . $customer_id . "'";
37 $result =db_query($sql,"Customer Record Retreive");
39 $myrow = db_fetch($result);
41 $order->customer_id = $customer_id;
42 $order->Branch = $branch_id;
43 $order->customer_name = $myrow['name'];
45 if ($myrow['dissallow_invoices'] == 1)
46 $ret_error = _("The selected customer account is currently on hold. Please contact the credit control personnel to discuss.");
48 if (!isset($_POST['branch_id']) || $_POST['branch_id'] == "")
50 $ret_error = _("The selected customer does not have any branches. Please create at least one branch.");
51 unset($_POST['branch_id']);
55 # the sales type determines the price list to be used by default
56 $order->default_sales_type = $myrow['salestype'];
57 $order->sales_type_name = $myrow['sales_type'];
58 $order->customer_currency = $myrow['curr_code'];
59 $order->default_discount = $myrow['discount'];
60 $deliver = $myrow['address']; // in case no deliveraddress.
62 if ($order->Branch != "")
64 # 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
65 $sql = "SELECT ".TB_PREF."cust_branch.br_name, ".TB_PREF."cust_branch.br_post_address, phone, email,
66 default_location, default_ship_via,
67 ".TB_PREF."tax_groups.name AS tax_group_name, ".TB_PREF."tax_groups.id AS tax_group_id
68 FROM ".TB_PREF."cust_branch, ".TB_PREF."tax_groups
69 WHERE ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
70 AND ".TB_PREF."cust_branch.branch_code='" . $order->Branch . "'
71 AND ".TB_PREF."cust_branch.debtor_no = '" . $order->customer_id . "'";
73 $result = db_query($sql,"Customer Branch Record Retreive");
75 if (db_num_rows($result) == 0)
77 return _("The selected customer and branch are not valid, or the customer does not have any branches.");
80 $myrow = db_fetch($result);
81 $order->deliver_to = $myrow["br_name"];
82 $order->delivery_address = $myrow["br_post_address"];
83 if (strlen($order->delivery_address) <= 1)
84 $order->delivery_address = $deliver;
85 $order->phone = $myrow["phone"];
86 $order->email = $myrow["email"];
87 $order->Location = $myrow["default_location"];
88 $order->ship_via = $myrow["default_ship_via"];
89 $order->tax_group_name = $myrow["tax_group_name"];
90 $order->tax_group_id = $myrow["tax_group_id"];
96 //---------------------------------------------------------------------------------
98 function display_order_summary($title, &$order, $editable_items=false)
100 global $table_style, $path_to_root;
102 display_heading($title);
103 start_table("$table_style colspan=7 width=90%");
104 $th = array(_("Item Code"), _("Item Description"), _("Quantity"),
105 _("Unit"), _("Price"), _("Discount %"), _("Total"), "");
107 if(count($order->line_items)) $th[]= '';
112 $k = 0; //row colour counter
114 foreach ($order->line_items as $line_no=>$stock_item)
117 $line_total = $stock_item->quantity * $stock_item->price * (1 - $stock_item->discount_percent);
119 if (!isset($_GET['Edit']))
123 if (!$editable_items || $id != $line_no)
125 alt_table_row_color($k);
127 view_stock_status_cell($stock_item->stock_id);
129 label_cell($stock_item->item_description);
130 qty_cell($stock_item->quantity);
131 label_cell($stock_item->units);
132 amount_cell($stock_item->price);
134 amount_cell($stock_item->discount_percent * 100);
135 amount_cell($line_total);
139 edit_link_cell(SID . "Edit=$line_no");
140 delete_link_cell(SID . "Delete=$line_no");
146 sales_order_item_controls($order, $line_no);
149 $total += $line_total;
152 if (!isset($_GET['Edit']) && $editable_items)
153 sales_order_item_controls($order);
155 $display_total = number_format2($total,user_price_dec());
156 label_row(_("Total Excluding Tax/Shipping"), $display_total, "colspan=6 align=right",
157 "nowrap align=right");
162 // ------------------------------------------------------------------------------
164 function display_order_header(&$order, $editable, $date_text, $display_tax_group=false)
167 start_table("width=80% $table_style");
168 echo "<tr><td valign=top>"; // outer table
171 $customer_error = "";
173 if (isset($order) && !$editable)
175 // can't change the customer/branch if items already received on this order
176 echo $order->customer_name . " - " . $order->deliver_to;
181 if (!isset($_POST['customer_id']) && (get_global_customer() != reserved_words::get_all()))
182 $_POST['customer_id'] = get_global_customer();
184 customer_list_row(_("Customer:"), 'customer_id', null, false, true);
186 if (!isset($_POST['delivery_date']))
188 if ($order->trans_type=='invoice')
189 $_POST['delivery_date'] = $_SESSION['Items']->delivery_date =
190 get_invoice_duedate($_POST['customer_id'], $_POST['OrderDate']);
192 $_POST['delivery_date'] = $_SESSION['Items']->delivery_date =
193 add_days($_POST['OrderDate'], 10);
195 if ($order->customer_id != $_POST['customer_id'])
197 // customer has changed
199 // delete all the order items - drastic but necessary because of
200 // change of currency, sales type, etc
201 $order->clear_items();
203 // clear the branch selection
204 unset($_POST['branch_id']);
207 customer_branches_list_row(_("Branch:"), $_POST['customer_id'], 'branch_id', null, false, true, true);
209 if (!isset($_POST['branch_id']))
210 $_POST['branch_id'] = "";
211 //set_global_customer($_POST['customer_id']);
212 if (($order->customer_id != $_POST['customer_id']) ||
213 ($order->Branch != $_POST['branch_id']))
214 $customer_error = get_customer_details_to_order($order, $_POST['customer_id'], $_POST['branch_id']);
216 set_global_customer($_POST['customer_id']);
220 echo "</td><td>"; // outer table
222 if (!is_company_currency($order->customer_currency))
224 echo "<table height='5'>";
225 label_row(_("Customer Currency:"), $order->customer_currency);
226 exchange_rate_display($order->customer_currency, get_company_currency(),
227 ($editable?$_POST['OrderDate']:$order->orig_order_date), $editable);
229 echo "</td><td>"; // outer table
232 echo "<table height='5'>";
233 label_row(_("Sales Type:"), $order->sales_type_name);
234 label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
237 echo "</td><td>"; // outer table
239 echo "<table height='5'>";
243 if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
244 $_POST['OrderDate'] = $order->orig_order_date;
246 date_row($date_text, 'OrderDate');
250 label_row($date_text, $order->orig_order_date);
251 hidden('OrderDate', $order->orig_order_date);
254 if ($display_tax_group)
258 if (!isset($_POST['tax_group_id']))
259 $_POST['tax_group_id'] = $_SESSION['Items']->tax_group_id;
260 tax_groups_list_row(_("Tax Group:"), 'tax_group_id', $_POST['tax_group_id'], true);
264 label_row(_("Tax Group:"), $order->tax_group_name);
265 hidden('tax_group_id', $_SESSION['Items']->tax_group_id);
272 end_table(1); // outer table
274 return $customer_error;
277 //--------------------------------------------------------------------------------
279 function sales_order_item_controls(&$order, $line_no=-1)
282 if (isset($_GET['Edit']) && $line_no != -1)
284 if (!isset($_POST['stock_id']))
285 $_POST['stock_id'] = $order->line_items[$_GET['Edit']]->stock_id;
286 if (!isset($_POST['qty']) || ($_POST['qty'] == ""))
287 $_POST['qty'] = $order->line_items[$_GET['Edit']]->quantity;
288 if (!isset($_POST['price']) || ($_POST['price'] == ""))
289 $_POST['price'] = $order->line_items[$_GET['Edit']]->price;
290 if (!isset($_POST['Disc']) || ($_POST['Disc'] == ""))
291 $_POST['Disc'] = ($order->line_items[$_GET['Edit']]->discount_percent)*100;
293 $_POST['units'] = $order->line_items[$_GET['Edit']]->units;
294 hidden('stock_id', $_POST['stock_id']);
295 label_cell($_POST['stock_id']);
296 label_cell($order->line_items[$line_no]->item_description);
300 global $no_item_list;
303 echo "<td colspan=2>\n";
304 stock_items_list('stock_id', null, false, true);
309 text_cells(null, "StockID2", "", 12, 10, "", "", "onkeyup='recalcAccounts();' onKeyDown='if(event.keyCode==13) event.keyCode=9;' onblur='return setAccount(0, true);'");
310 stock_items_list_cells(null, 'stock_id', null, false, false, "onchange='return setAccount(1, true)'");
312 $item_info = get_item_edit_info($_POST['stock_id']);
313 $_POST['units'] = $item_info["units"];
316 $_POST['price'] = get_price ($_POST['stock_id'], $order->customer_id);
317 // default to the customer's discount %
318 $_POST['Disc'] = $order->default_discount * 100;
321 text_cells(null, 'qty', $_POST['qty'], 12, 15);
323 label_cell($_POST['units']);
324 amount_cells(null, 'price');
325 text_cells(null, 'Disc', $_POST['Disc'], 7, 5);
327 $line_total = $_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc'] / 100);
328 amount_cell($line_total);
330 if (isset($_GET['Edit']))
332 submit_cells('UpdateItem', _("Update"));
333 submit_cells('CancelItemChanges', _("Cancel"));
334 hidden('LineNo', $line_no);
338 submit_cells('AddItem', _("Add Item"), "colspan=2");
344 //--------------------------------------------------------------------------------
346 function display_delivery_details(&$order)
348 global $table_style2;
350 if ($order->trans_type=='invoice')
352 $title = _("Invoice Delivery Details");
353 $delname = _("Due Date");
357 $title = _("Order Delivery Details");
358 $delname = _("Required Delivery Date:");
360 display_heading($title);
362 start_table("$table_style2 width=90%");
363 echo "<tr valign=top><td>"; // outer table
366 locations_list_row(_("Deliver from Location:"), 'Location', $order->Location);
368 date_row($delname, 'delivery_date', $order->delivery_date, 0, 0, 0);
370 text_row(_("Deliver To:"), 'deliver_to', $order->deliver_to, 40, 40);
372 textarea_row(_("Address:"), 'delivery_address', $order->delivery_address, 35, 5);
373 text_row(_("Contact Phone Number:"), 'phone', $order->phone, 25, 25);
377 echo "</td><td>"; // outer table
381 text_row(_("Customer Reference:"), 'cust_ref', $order->cust_ref, 25, 25);
382 textarea_row(_("Comments:"), "Comments", $order->Comments, 31, 5);
384 text_row(_("Shipping Charge:"), 'freight_cost', $order->freight_cost, 10, 10);
386 shippers_list_row(_("Shipping Company:"), 'ship_via', $order->ship_via);
387 if ($_SESSION['Items']->trans_type=='delivery')
388 textarea_row(_("Memo"), 'InvoiceText', null, 31, 3);
392 end_table(1); // outer table