X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;f=sales%2Fincludes%2Fdb%2Fbranches_db.inc;h=2688d1b3e2d341e521db4deebfbfaeb753ec1121;hb=49fa30e88e27b311edaca3ae3789d62e4629eb39;hp=3728dbbc35eb6a415755109567aa08c7360d1141;hpb=502c2fc8bb683df7737e6700c984d7584405355a;p=fa-stable.git diff --git a/sales/includes/db/branches_db.inc b/sales/includes/db/branches_db.inc index 3728dbbc..2688d1b3 100644 --- a/sales/includes/db/branches_db.inc +++ b/sales/includes/db/branches_db.inc @@ -9,21 +9,20 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License here . ***********************************************************************/ -function add_branch($customer_id, $br_name, $br_ref, $br_address, $phone, $phone2, $fax, $contact_name, - $salesman, $area, $email, $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) +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) { $sql = "INSERT INTO ".TB_PREF."cust_branch (debtor_no, br_name, branch_ref, br_address, - salesman, phone, phone2, fax, - contact_name, area, email, tax_group_id, sales_account, receivables_account, payment_discount_account, sales_discount_account, default_location, + 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) VALUES (".db_escape($customer_id). ",".db_escape($br_name) . ", " .db_escape($br_ref) . ", " .db_escape($br_address) . ", ".db_escape($salesman) . ", " - .db_escape($phone) . ", ".db_escape($phone2) . ", " - .db_escape($fax) . "," - .db_escape($contact_name) . ", ".db_escape($area) . "," - .db_escape($email) . ", ".db_escape($tax_group_id) . ", " + .db_escape($area) . "," + .db_escape($tax_group_id) . ", " .db_escape($sales_account) . ", " .db_escape($receivables_account) . ", " .db_escape($payment_discount_account) . ", " @@ -33,24 +32,20 @@ function add_branch($customer_id, $br_name, $br_ref, $br_address, $phone, $phone .db_escape($disable_trans) . ", " .db_escape($group_no) . ", " .db_escape($default_ship_via). ", " - .db_escape($notes) . ")"; + .db_escape($notes).")"; db_query($sql,"The branch record could not be added"); } -function update_branch($customer_id, $branch_code, $br_name, $br_ref, $br_address, $phone, $phone2, $fax, $contact_name, - $salesman, $area, $email, $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) +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) { $sql = "UPDATE ".TB_PREF."cust_branch SET br_name = " . db_escape($br_name) . ", branch_ref = " . db_escape($br_ref) . ", br_address = ".db_escape($br_address). ", - phone=".db_escape($phone). ", - phone2=".db_escape($phone2). ", - fax=".db_escape($fax).", - contact_name=".db_escape($contact_name) . ", salesman= ".db_escape($salesman) . ", area=".db_escape($area) . ", - email=".db_escape($email) . ", tax_group_id=".db_escape($tax_group_id). ", sales_account=".db_escape($sales_account) . ", sales_discount_account=".db_escape($sales_discount_account) . ", @@ -69,6 +64,8 @@ function update_branch($customer_id, $branch_code, $br_name, $br_ref, $br_addres function delete_branch($customer_id, $branch_code) { + delete_entity_contacts('cust_branch', $branch_code); + $sql="DELETE FROM ".TB_PREF."cust_branch WHERE branch_code=".db_escape($branch_code)." AND debtor_no=".db_escape($customer_id); db_query($sql,"could not delete branch"); } @@ -91,7 +88,7 @@ function get_branch($branch_id) $result = db_query($sql, "Cannot retreive a customer branch"); - return db_fetch($result); + return db_fetch($result); } function get_cust_branch($customer_id, $branch_code) @@ -110,7 +107,7 @@ function get_branch_accounts($branch_id) $result = db_query($sql, "Cannot retreive a customer branch"); - return db_fetch($result); + return db_fetch($result); } function get_branch_name($branch_id) @@ -120,7 +117,7 @@ function get_branch_name($branch_id) $result = db_query($sql,"could not retreive name for branch" . $branch_id); - $myrow = db_fetch_row($result); + $myrow = db_fetch_row($result); return $myrow[0]; } @@ -134,7 +131,7 @@ function get_cust_branches_from_group($group_no) function get_default_info_for_branch($customer_id) { - $sql = "SELECT name, address, email, debtor_ref + $sql = "SELECT name, address, debtor_ref FROM ".TB_PREF."debtors_master WHERE debtor_no = ".db_escape($customer_id); $result = db_query($sql,"check failed"); return db_fetch($result); @@ -146,21 +143,22 @@ function get_sql_for_customer_branches() ."b.branch_code, " ."b.branch_ref, " ."b.br_name, " - ."b.contact_name, " + ."p.name as contact_name, " ."s.salesman_name, " ."a.description, " - ."b.phone, " - ."b.fax, " - ."b.email, " + ."p.phone, " + ."p.fax, " + ."p.email, " ."t.name AS tax_group_name, " ."b.inactive - FROM ".TB_PREF."cust_branch b, " - .TB_PREF."debtors_master c, " + 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," .TB_PREF."areas a, " .TB_PREF."salesman s, " .TB_PREF."tax_groups t - WHERE b.debtor_no=c.debtor_no - AND b.tax_group_id=t.id + WHERE b.tax_group_id=t.id AND b.area=a.area_code AND b.salesman=s.salesman_code AND b.debtor_no = ".db_escape($_POST['customer_id']); @@ -170,4 +168,87 @@ function get_sql_for_customer_branches() return $sql; } +/* + 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 +*/ +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).($default ? " OR r.action='general'":'').')'; + + 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' + 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 = ''; + while($contact = db_fetch($res)) { + 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; +} + ?> \ No newline at end of file