X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;f=sales%2Fincludes%2Fdb%2Fbranches_db.inc;h=e51de6f149234bd8004084fa8545d0f0919025ae;hb=58ca6ac4ea7a903514388cf8c3f380f745d81551;hp=52c41a786ca8ef51ffd77e07471247897432fe6b;hpb=17b390efcf904072b02ec866b2a427490471a260;p=fa-stable.git diff --git a/sales/includes/db/branches_db.inc b/sales/includes/db/branches_db.inc index 52c41a78..e51de6f1 100644 --- a/sales/includes/db/branches_db.inc +++ b/sales/includes/db/branches_db.inc @@ -81,13 +81,15 @@ function branch_in_foreign_table($customer_id, $branch_code, $table) function get_branch($branch_id) { - $sql = "SELECT ".TB_PREF."cust_branch.*,".TB_PREF."salesman.salesman_name - FROM ".TB_PREF."cust_branch, ".TB_PREF."salesman - WHERE ".TB_PREF."cust_branch.salesman=".TB_PREF."salesman.salesman_code - AND branch_code=".db_escape($branch_id); - + $sql = "SELECT branch.*, salesman.salesman_name + FROM " + .TB_PREF."cust_branch branch," + .TB_PREF."salesman salesman + WHERE branch.salesman=salesman.salesman_code + AND branch_code=".db_escape($branch_id); + $result = db_query($sql, "Cannot retreive a customer branch"); - + return db_fetch($result); } @@ -184,8 +186,10 @@ function get_branch_contacts($branch_code, $action=null, $customer_id=null, $def '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' + 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).")"; @@ -216,7 +220,8 @@ function _get_branch_contacts($branch_code, $action=null, $customer_id=null, $de { $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' + .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) @@ -225,13 +230,14 @@ function _get_branch_contacts($branch_code, $action=null, $customer_id=null, $de if($customer_id) { $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' + .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).($default ? " OR r.action='general'":'').')'; $sql .= ')'; } - + $res = db_query($sql, "can't retrieve branch contacts"); $results = array(); $type = ''; @@ -251,3 +257,39 @@ function _get_branch_contacts($branch_code, $action=null, $customer_id=null, $de return $results; } +function get_default_branch($customer_id, $ar_account=null) +{ + $sql = "SELECT * + FROM ".TB_PREF."cust_branch WHERE debtor_no = ".db_escape($customer_id); + if($ar_account) + $sql .= " AND receivables_account=".db_escape($ar_account); + $result = db_query($sql,"cannot retrieve default branch"); + return db_fetch($result); +} + +function get_branches_search($customer, $branch) +{ + global $SysPrefs; + + if (isset($SysPrefs->max_rows_in_search)) + $limit = $SysPrefs->max_rows_in_search; + else + $limit = 10; + + $sql = "SELECT + b.branch_code, + b.branch_ref, + b.br_name, + p.name as contact_name, + p.phone + FROM ".TB_PREF."cust_branch b + LEFT JOIN ".TB_PREF."crm_contacts c + ON c.entity_id=b.branch_code AND c.type='cust_branch' AND c.action='general' + LEFT JOIN ".TB_PREF."crm_persons p ON c.person_id=p.id + WHERE b.debtor_no = ".db_escape($customer)." + AND b.br_name LIKE " . db_escape("%" . $branch. "%") . " + ORDER BY b.br_name LIMIT 0,".(int)($limit); + + return db_query($sql, "Failed in retreiving branches list."); +} +