- # 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
- $sql = "SELECT ".TB_PREF."cust_branch.br_name, ".TB_PREF."cust_branch.br_post_address, phone, email,
- default_location, default_ship_via,
- ".TB_PREF."tax_groups.name AS tax_group_name, ".TB_PREF."tax_groups.id AS tax_group_id
- FROM ".TB_PREF."cust_branch, ".TB_PREF."tax_groups
- WHERE ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
- AND ".TB_PREF."cust_branch.branch_code='" . $order->Branch . "'
- AND ".TB_PREF."cust_branch.debtor_no = '" . $order->customer_id . "'";
-
- $result = db_query($sql,"Customer Branch Record Retreive");
-
- if (db_num_rows($result) == 0)
- {
- return _("The selected customer and branch are not valid, or the customer does not have any branches.");
- }
-
- $myrow = db_fetch($result);
- $order->deliver_to = $myrow["br_name"];
- $order->delivery_address = $myrow["br_post_address"];
- if (strlen($order->delivery_address) <= 1)
- $order->delivery_address = $deliver;
- $order->phone = $myrow["phone"];
- $order->email = $myrow["email"];
- $order->Location = $myrow["default_location"];
- $order->ship_via = $myrow["default_ship_via"];
- $order->tax_group_name = $myrow["tax_group_name"];
- $order->tax_group_id = $myrow["tax_group_id"];
+ $result = get_branch_to_order($customer_id, $branch_id);
+
+ if (db_num_rows($result) == 0)
+ {
+ return _("The selected customer and branch are not valid, or the customer does not have any branches.");
+ }
+
+ $myrow = db_fetch($result);
+
+ $order->set_branch($branch_id, $myrow["tax_group_id"],
+ $myrow["tax_group_name"], $myrow["phone"], $myrow["email"]);
+
+ $address = $myrow["br_post_address"];
+
+ if (strlen($address) <= 1) // if branch has no address
+ $address = $deliver; // set sales order address
+
+ $order->set_location($myrow["default_location"], $myrow["location_name"]);
+ $order->set_delivery($myrow["default_ship_via"], $myrow["br_name"],
+ $address);