Removed many global variables from config.default.php to GL Setup, Company Setup...
[fa-stable.git] / sales / includes / db / branches_db.inc
index bc9543b41f0a8b77b1064806b636f7081cf7a195..09b1fbe4f28af171c013cd574c5bbc028b5bdf9c 100644 (file)
 function add_branch($customer_id, $br_name, $br_ref, $br_address, $salesman, $area, 
        $tax_group_id, $sales_account, $sales_discount_account, $receivables_account, 
        $payment_discount_account, $default_location, $br_post_address, $disable_trans, $group_no,
-       $default_ship_via, $notes)
+       $default_ship_via, $notes, $bank_account)
 {
        $sql = "INSERT INTO ".TB_PREF."cust_branch (debtor_no, br_name, branch_ref, br_address,
                salesman, area, tax_group_id, sales_account, receivables_account, payment_discount_account, 
                sales_discount_account, default_location,
-               br_post_address, disable_trans, group_no, default_ship_via, notes)
+               br_post_address, disable_trans, group_no, default_ship_via, notes, bank_account)
                VALUES (".db_escape($customer_id). ",".db_escape($br_name) . ", "
                        .db_escape($br_ref) . ", "
                        .db_escape($br_address) . ", ".db_escape($salesman) . ", "
@@ -32,14 +32,15 @@ function add_branch($customer_id, $br_name, $br_ref, $br_address, $salesman, $ar
                        .db_escape($disable_trans) . ", "
                        .db_escape($group_no) . ", "
                        .db_escape($default_ship_via). ", "
-                       .db_escape($notes).")";
+                       .db_escape($notes). ", "
+                       .db_escape($bank_account, true).")";
        db_query($sql,"The branch record could not be added");
 }
 
 function update_branch($customer_id, $branch_code, $br_name, $br_ref, $br_address,
        $salesman, $area, $tax_group_id, $sales_account, $sales_discount_account, $receivables_account, 
        $payment_discount_account, $default_location, $br_post_address, $disable_trans, $group_no,
-       $default_ship_via, $notes)
+       $default_ship_via, $notes, $bank_account)
 {
        $sql = "UPDATE ".TB_PREF."cust_branch SET br_name = " . db_escape($br_name) . ",
                branch_ref = " . db_escape($br_ref) . ",
@@ -56,7 +57,8 @@ function update_branch($customer_id, $branch_code, $br_name, $br_ref, $br_addres
                disable_trans=".db_escape($disable_trans) . ",
                group_no=".db_escape($group_no) . ", 
                default_ship_via=".db_escape($default_ship_via) . ",
-               notes=".db_escape($notes) . "
+               notes=".db_escape($notes) . ",
+               bank_account=".db_escape($bank_account, true)."
                WHERE branch_code =".db_escape($branch_code) . "
                AND debtor_no=".db_escape($customer_id);
        db_query($sql,"The branch record could not be updated");
@@ -164,46 +166,90 @@ function get_sql_for_customer_branches()
                AND b.debtor_no = ".db_escape($_POST['customer_id']);
 
        if (!get_post('show_inactive')) $sql .= " AND !b.inactive";
-       $sql .= " ORDER BY branch_ref";
+       $sql .= " GROUP BY b.branch_code ORDER BY branch_ref";
 
        return $sql;
 }
-
-function get_branch_contacts($branch_code, $action=null, $customer_id=null)
-{
 /*
-       $ret = get_crm_persons('cust_branch', $action, $branch_code);
-       if ($customer_id && !db_num_rows($ret)) {
-               $ret = get_crm_persons('customer', $action, $customer_id);
-       }
-       return $ret;
+       Get contacts of given type for customer branch.
+       $branch_code - branch id
+       $action - type of contact
+       $customer_id - if passed: get also customer contacts for given action
+       $default - return only single record selected with defaults order defined in $defs array,
+        otherways get all $action contacts
 */
-       $sql = "SELECT p.*, r.action, r.type FROM ".TB_PREF."crm_persons p,"
+function get_branch_contacts($branch_code, $action=null, $customer_id=null, $default = true)
+{
+       $defs = array('cust_branch.'.$action, 
+                               'customer.'.$action,
+                               'cust_branch.general',
+                               'customer.general');
+
+       $sql = "SELECT p.*, r.action, r.type, CONCAT(r.type,'.',r.action) as ext_type 
+               FROM ".TB_PREF."crm_persons p,"
+               .TB_PREF."crm_contacts r WHERE r.person_id=p.id AND ((r.type='cust_branch' 
+                       AND r.entity_id=".db_escape($branch_code).')';
+       if($customer_id) {
+               $sql .= " OR (r.type='customer' AND r.entity_id=".db_escape($customer_id).")";
+       }
+       $sql .= ')';
+       
+       if ($action)
+               $sql .= ' AND (r.action='.db_escape($action).($default ? " OR r.action='general'" : '').')';
+       
+       $res = db_query($sql, "can't retrieve branch contacts");
+
+       $results = array();
+       while($contact = db_fetch($res))
+               $results[] = $contact;
+
+       if ($results && $default) {
+               // select first available contact in $defs order
+               foreach($defs as $type) {
+                       if ($n = array_search_value($type, $results, 'ext_type'))
+                               return $n;
+               }
+               return null;
+       }
+       return $results;
+}
+
+function _get_branch_contacts($branch_code, $action=null, $customer_id=null, $default = false)
+{
+       $sql = "SELECT p.*, r.action, r.type, CONCAT(r.type,'.',r.action) as ext_type 
+               FROM ".TB_PREF."crm_persons p,"
                .TB_PREF."crm_contacts r WHERE r.person_id=p.id AND r.type='cust_branch' 
                        AND r.entity_id=".db_escape($branch_code);
 
-       if ($action) 
-               $sql .= ' AND r.action='.db_escape($action);
-
-       $sql .= " GROUP BY person_id";
+       if ($action)
+               $sql .= ' AND (r.action='.db_escape($action).($default ? " OR r.action='general'":'').')';
 
        if($customer_id) {
-               $sql = "($sql) UNION (SELECT p.*, r.action, r.type FROM ".TB_PREF."crm_persons p,"
+               $sql = "($sql) UNION (SELECT p.*, r.action, r.type, CONCAT(r.type,'.',r.action) as ext_type
+               FROM ".TB_PREF."crm_persons p,"
                .TB_PREF."crm_contacts r WHERE r.person_id=p.id AND r.type='customer' 
                        AND r.entity_id=".db_escape($customer_id);
                if ($action) 
-                       $sql .= ' AND r.action='.db_escape($action);
-               $sql .= " GROUP BY person_id)";
+                       $sql .= ' AND (r.action='.db_escape($action).($default ? " OR r.action='general'":'').')';
+               $sql .= ')';
        }
        
        $res = db_query($sql, "can't retrieve branch contacts");
        $results = array();
        $type = '';
        while($contact = db_fetch($res)) {
-               if ($type && $type != $contact['type']) break;
+               if ($type && $type != $contact['type']) break; // skip cust when branch contacts found
                $results[] = $contact;
                $type = $contact['type'];
        }
+       if ($results && $default) {
+               // here we have general and action contacts selected
+               if ($n = array_search_value($action, $results, 'action')) {
+                       return $n;
+               }
+               // only general contact found
+               return $results[0];
+       }
        return $results;
 }