Rearanged contacts edition.
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Sat, 6 Nov 2010 19:25:50 +0000 (19:25 +0000)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Sat, 6 Nov 2010 19:25:50 +0000 (19:25 +0000)
includes/db/crm_contacts_db.inc
includes/ui/contacts_view.inc
sales/includes/db/branches_db.inc

index 1f4051a6b4c3af57a3193be1f784b77a7ffed5d5..3f34b70e7d75ba2f4ecd5b3bf8aeee62da07b878 100644 (file)
@@ -108,7 +108,7 @@ function get_crm_persons($type=null, $action=null, $entity=null, $person=null, $
        if ($unique)
                $sql .= " GROUP BY person_id";
        else
-               $sql .= " ORDER BY t.id";
+               $sql .= " ORDER BY contact_id";
 
        return db_query($sql, "Can't get crm persons");
 }
@@ -241,13 +241,30 @@ function add_crm_contact($type, $action, $entity_id, $person_id)
                .db_escape($entity_id) . ")";
        return db_query($sql, "Can't insert crm contact");
 }
-
+/*
+       Delete contact selected by unique id.
+*/
 function delete_crm_contact($id)
 {
        $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE id=".db_escape($id);
 
        return db_query($sql, "Can't delete crm contact");
 }
+/*
+       Delete selected contacts for given person
+*/
+function delete_crm_contacts($person_id, $type = null, $entity_id=null, $action = null)
+{
+       $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE person_id=".db_escape($person_id);
+       if ($type)
+               $sql .= ' AND type='.db_escape($type);
+       if ($entity_id)
+               $sql .= ' AND entity_id='.db_escape($entity_id);
+       if ($action)
+               $sql .= ' AND action='.db_escape($action);
+
+       return db_query($sql, "Can't delete crm contact");
+}
 
 /*
        Returns person data for given contact id
index c2ff889df0acb124c5416dac9559d1ee548df58d..07e3066deff1224036ee083cf95d2501ebcb09bb 100644 (file)
@@ -45,26 +45,37 @@ class contacts extends simple_crud {
 
                        table_header($th);
                        $k = 0; 
-                       while ($myrow = db_fetch($contacts)) 
-                       {
-                               alt_table_row_color($k);
-                               label_cell($myrow["description"]);
-                               label_cell($myrow["ref"]);
-                               label_cell($myrow["name"].' '.$myrow["name2"]);
-                               label_cell($myrow["phone"]);
-                               label_cell($myrow["phone2"]);
-                               label_cell($myrow["fax"]);
-                               email_cell($myrow["email"]);
 
+                       $last = $myrow = db_fetch($contacts);
+                       while ($last)
+                       {
+                                       
+                               if ($myrow['id'] != $last['id']) {
+                                       alt_table_row_color($k);
+                                       label_cell(implode('<br>',$description));
+                                       label_cell($last["ref"]);
+                                       label_cell($last["name"].' '.$last["name2"]);
+                                       label_cell($last["phone"]);
+                                       label_cell($last["phone2"]);
+                                       label_cell($last["fax"]);
+                                       email_cell($last["email"]);
                //      inactive_control_cell($myrow["id"], $myrow["inactive"], 'crm_contacts', 'id');
 
-                               edit_button_cell("{$this->name}Edit[{$myrow['id']}]", _("Edit"));
+                                       edit_button_cell("{$this->name}Edit[{$last['id']}]", _("Edit"));
 
                        // delete button removes only category unless this is the last contact for this person
-                               delete_button_cell("{$this->name}Delete[{$myrow['contact_id']}]", _("Delete"));
-                               end_row();
+                                       delete_button_cell("{$this->name}Delete[{$last['id']}]", _("Delete"));
+                                       end_row();
+                                       $description = array();
+                                       $last = $myrow;
+                               }
+
+                               if ($myrow) {
+                                       $description[] = $myrow['description'];
+                                       $myrow = db_fetch($contacts);
+                               }
                        }
-       
+
 //inactive_control_row($th);
                        end_table(1);
        }
@@ -143,15 +154,14 @@ class contacts extends simple_crud {
                return true;
        }
        //
-       //      Delete single contact only (unless this is last contact for this person)
+       //      Delete all contacts for person in current class/entity
        //
        function db_delete() {
-               $rel = get_crm_contact($this->selected_id);
-               if (delete_crm_contact($this->selected_id)) {
+               if (delete_crm_contacts($this->selected_id, $this->class, $this->entity)) {
                        // if this is the last relation - delete contact data
-                       $rels = get_person_contacts($rel['person_id']);
+                       $rels = get_person_contacts($this->selected_id);
                        if (count($rels)==0) {
-                               return delete_crm_person($rel['person_id']);
+                               return delete_crm_person($this->selected_id);
                        }
                }
                return false;
index bc9543b41f0a8b77b1064806b636f7081cf7a195..2688d1b3e2d341e521db4deebfbfaeb753ec1121 100644 (file)
@@ -168,42 +168,86 @@ function get_sql_for_customer_branches()
 
        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;
 }