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 (count($order->line_items),$new_item, $new_item_qty, $price, $discount);
23 //---------------------------------------------------------------------------------
25 function get_customer_details_to_order(&$order, $customer_id, $branch_id)
29 $myrow = get_customer_to_order($customer_id);
31 $name = $myrow['name'];
33 if ($myrow['dissallow_invoices'] == 1)
34 $ret_error = _("The selected customer account is currently on hold. Please contact the credit control personnel to discuss.");
36 /* if (!isset($_POST['branch_id']) || $_POST['branch_id'] == "")
38 $ret_error = _("The selected customer does not have any branches. Please create at least one branch.");
39 unset($_POST['branch_id']);
43 $deliver = $myrow['address']; // in case no delivery address.
45 $order->set_customer($customer_id, $name, $myrow['curr_code'], $myrow['discount']);
47 // the sales type determines the price list to be used by default
48 $order->set_sales_type($myrow['salestype'], $myrow['sales_type'], $myrow['tax_included']);
50 // if ($order->Branch != 0)
52 $result = get_branch_to_order($customer_id, $branch_id);
54 if (db_num_rows($result) == 0)
56 return _("The selected customer and branch are not valid, or the customer does not have any branches.");
59 $myrow = db_fetch($result);
61 $order->set_branch($branch_id, $myrow["tax_group_id"],
62 $myrow["tax_group_name"], $myrow["phone"], $myrow["email"]);
64 $address = $myrow["br_post_address"];
66 if (strlen($address) <= 1) // if branch has no address
67 $address = $deliver; // set sales order address
69 $order->set_location($myrow["default_location"], $myrow["location_name"]);
70 $order->set_delivery($myrow["default_ship_via"], $myrow["br_name"],
77 //---------------------------------------------------------------------------------
79 function display_order_summary($title, &$order, $editable_items=false)
81 global $table_style, $path_to_root;
83 display_heading($title);
84 start_table("$table_style colspan=7 width=90%");
85 $th = array(_("Item Code"), _("Item Description"), _("Quantity"),
87 _("Unit"), _("Price"), _("Discount %"), _("Total"), "");
89 if ($order->trans_no==0) {
93 if (count($order->line_items)) $th[]= '';
98 $k = 0; //row colour counter
100 foreach ($order->line_items as $line_no=>$stock_item)
103 $line_total = $stock_item->qty_dispatched * $stock_item->price * (1 - $stock_item->discount_percent);
105 if (!isset($_GET['Edit']))
110 if (!$editable_items || $id != $line_no)
112 alt_table_row_color($k);
114 view_stock_status_cell($stock_item->stock_id);
116 label_cell($stock_item->item_description);
117 qty_cell($stock_item->qty_dispatched);
119 if ($order->trans_no!=0)
120 amount_cell($stock_item->qty_done);
122 label_cell($stock_item->units);
123 amount_cell($stock_item->price);
125 amount_cell($stock_item->discount_percent * 100);
126 amount_cell($line_total);
130 edit_link_cell(SID . "Edit=$line_no");
131 delete_link_cell(SID . "Delete=$line_no");
137 sales_order_item_controls($order, $line_no);
140 $total += $line_total;
143 if (!isset($_GET['Edit']) && $editable_items)
144 sales_order_item_controls($order);
146 $display_total = number_format2($total,user_price_dec());
147 label_row(_("Total Excluding Shipping"), $display_total, "colspan=6 align=right",
148 "nowrap align=right");
153 // ------------------------------------------------------------------------------
155 function display_order_header(&$order, $editable, $date_text, $display_tax_group=false)
158 start_table("width=80% $table_style");
159 echo "<tr><td valign=top>"; // outer table
162 $customer_error = "";
164 if (isset($order) && !$editable)
166 // can't change the customer/branch if items already received on this order
167 echo $order->customer_name . " - " . $order->deliver_to;
168 hidden('customer_id', $order->customer_id);
169 hidden('branch_id', $order->Branch);
174 if (!isset($_POST['customer_id']) && (get_global_customer() != reserved_words::get_all()))
175 $_POST['customer_id'] = get_global_customer();
177 customer_list_row(_("Customer:"), 'customer_id', null, false, true);
179 if (!isset($_POST['delivery_date']))
181 if ($order->trans_type==10)
182 $_POST['delivery_date'] = $_SESSION['Items']->due_date =
183 get_invoice_duedate($_POST['customer_id'], $_POST['OrderDate']);
185 $_POST['delivery_date'] = $_SESSION['Items']->due_date =
186 add_days($_POST['OrderDate'], 10); // FIX this should be in company prefs
188 if ($order->customer_id != $_POST['customer_id'])
190 // customer has changed
192 // delete all the order items - drastic but necessary because of
193 // change of currency, sales type, etc
194 // $order->clear_items();
195 // clear the branch selection
196 unset($_POST['branch_id']);
198 if (!isset($_POST['branch_id']))
199 $_POST['branch_id'] = "";
201 customer_branches_list_row(_("Branch:"), $_POST['customer_id'], 'branch_id', null, false, true, true);
203 if (!isset($_POST['branch_id']) || $_POST['branch_id'] == "")
205 $customer_error = _("The selected customer does not have any branches. Please create at least one branch.");
206 unset($_POST['branch_id']);
212 //set_global_customer($_POST['customer_id']);
213 if (($order->customer_id != $_POST['customer_id']) ||
214 ($order->Branch != $_POST['branch_id'])) {
216 $customer_error = get_customer_details_to_order($order, $_POST['customer_id'], $_POST['branch_id']);
217 $_POST['Location'] = $order->Location;
218 $_POST['deliver_to'] = $order->deliver_to;
219 $_POST['delivery_address'] = $order->delivery_address;
220 $_POST['phone'] = $order->phone;
222 // change prices if necessary
223 // what about discount in template case?
224 if ( $old_order->customer_currency!=$order->customer_currency
225 || $old_order->default_sales_type!=$order->default_sales_type
226 // || $old_order->default_discount!=$order->default_discount
229 foreach ($order->line_items as $line_no=>$item) {
230 $line = &$order->line_items[$line_no];
231 $line->price = get_price($line->stock_id, $order->customer_id);
232 // $line->discount_percent = $order->default_discount;
238 set_global_customer($_POST['customer_id']);
241 if ($_SESSION['Items']->trans_type!=30) {
242 ref_cells(_("Reference").':', 'ref', null, "");
247 echo "</td><td>"; // outer table
249 if (!is_company_currency($order->customer_currency))
251 echo "<table height='5'>";
252 label_row(_("Customer Currency:"), $order->customer_currency);
253 exchange_rate_display($order->customer_currency, get_company_currency(),
254 ($editable?$_POST['OrderDate']:$order->document_date), $editable);
256 echo "</td><td>"; // outer table
259 echo "<table height='5'>";
260 label_row(_("Sales Type:"), $order->sales_type_name);
261 label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
264 echo "</td><td>"; // outer table
266 echo "<table height='5'>";
270 if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
271 $_POST['OrderDate'] = $order->document_date;
273 date_row($date_text, 'OrderDate');
277 label_row($date_text, $order->document_date);
278 hidden('OrderDate', $order->document_date);
281 if ($display_tax_group)
285 if (!isset($_POST['tax_group_id']))
286 $_POST['tax_group_id'] = $_SESSION['Items']->tax_group_id;
287 tax_groups_list_row(_("Tax Group:"), 'tax_group_id', $_POST['tax_group_id'], true);
291 label_row(_("Tax Group:"), $order->tax_group_name);
292 hidden('tax_group_id', $_SESSION['Items']->tax_group_id);
299 end_table(1); // outer table
301 return $customer_error;
304 //--------------------------------------------------------------------------------
306 function sales_order_item_controls(&$order, $line_no=-1)
309 if (isset($_GET['Edit']) && $line_no != -1)
311 if (!isset($_POST['stock_id']))
312 $_POST['stock_id'] = $order->line_items[$_GET['Edit']]->stock_id;
313 if (!isset($_POST['qty']) || ($_POST['qty'] == ""))
314 $_POST['qty'] = $order->line_items[$_GET['Edit']]->qty_dispatched;
315 if (!isset($_POST['price']) || ($_POST['price'] == ""))
316 $_POST['price'] = $order->line_items[$_GET['Edit']]->price;
317 if (!isset($_POST['Disc']) || ($_POST['Disc'] == ""))
318 $_POST['Disc'] = ($order->line_items[$_GET['Edit']]->discount_percent)*100;
320 $_POST['units'] = $order->line_items[$_GET['Edit']]->units;
321 hidden('stock_id', $_POST['stock_id']);
322 label_cell($_POST['stock_id']);
323 label_cell($order->line_items[$line_no]->item_description);
327 global $no_item_list;
330 echo "<td colspan=2>\n";
331 stock_items_list('stock_id', null, false, true);
336 text_cells(null, "StockID2", "", 12, 10, "", "", "onkeyup='recalcAccounts();' onKeyDown='if (event.keyCode==13) event.keyCode=9;' onblur='return setAccount(0, true);'");
337 stock_items_list_cells(null, 'stock_id', null, false, false, "onchange='return setAccount(1, true)'");
339 $item_info = get_item_edit_info($_POST['stock_id']);
340 $_POST['units'] = $item_info["units"];
343 $_POST['price'] = get_price ($_POST['stock_id'], $order->customer_id);
344 // default to the customer's discount %
345 $_POST['Disc'] = $order->default_discount * 100;
348 text_cells(null, 'qty', $_POST['qty'], 12, 15);
350 if ($order->trans_no!=0) {
351 amount_cell($line_no==-1 ? 0 :$order->line_items[$line_no]->qty_done);
354 label_cell($_POST['units']);
355 amount_cells(null, 'price');
356 text_cells(null, 'Disc', $_POST['Disc'], 7, 5);
358 $line_total = $_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc'] / 100);
359 amount_cell($line_total);
361 if (isset($_GET['Edit']))
363 submit_cells('UpdateItem', _("Update"));
364 submit_cells('CancelItemChanges', _("Cancel"));
365 hidden('LineNo', $line_no);
369 submit_cells('AddItem', _("Add Item"), "colspan=2");
375 //--------------------------------------------------------------------------------
377 function display_delivery_details(&$order)
379 global $table_style2;
380 if ($order->trans_type==10)
382 $title = _("Delivery Details");
383 $delname = _("Due Date").':';
385 elseif ($order->trans_type==13)
387 $title = _("Invoice Delivery Details");
388 $delname = _("Invoice before").':';
392 $title = _("Order Delivery Details");
393 $delname = _("Required Delivery Date").':';
395 display_heading($title);
397 start_table("$table_style2 width=90%");
398 echo "<tr valign=top><td>"; // outer table
402 // echo $order->Location, $_POST['Location'];
404 locations_list_row(_("Deliver from Location:"), 'Location', $_POST['Location']);
406 date_row($delname, 'delivery_date', $order->due_date, 0, 0, 0);
408 text_row(_("Deliver To:"), 'deliver_to', $order->deliver_to, 40, 40);
410 textarea_row(_("Address:"), 'delivery_address', $order->delivery_address, 35, 5);
411 text_row(_("Contact Phone Number:"), 'phone', $order->phone, 25, 25);
415 echo "</td><td>"; // outer table
419 text_row(_("Customer Reference:"), 'cust_ref', $order->cust_ref, 25, 25);
420 textarea_row(_("Comments:"), "Comments", $order->Comments, 31, 5);
422 text_row(_("Shipping Charge:"), 'freight_cost', $order->freight_cost, 10, 10);
424 shippers_list_row(_("Shipping Company:"), 'ship_via', $order->ship_via);
429 end_table(1); // outer table