Rearanged contacts edition.
[fa-stable.git] / includes / db / crm_contacts_db.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12
13 function add_crm_person($ref, $name, $name2, $address, $phone, $phone2, $fax, $email, $lang, $notes,
14         $cat_ids=null, $entity=null)
15 {
16         $sql = "INSERT INTO ".TB_PREF."crm_persons (ref, name, name2, address,
17                 phone, phone2, fax, email, lang, notes)
18                 VALUES ("
19                   .db_escape($ref) . ", "
20                   .db_escape($name) . ", "
21                   .db_escape($name2) . ", "
22                   .db_escape($address) . ", "
23                   .db_escape($phone) . ", "
24                   .db_escape($phone2) . ", "
25                   .db_escape($fax) . ", "
26                   .db_escape($email) . ", "
27                   .db_escape($lang) . ", "
28                   .db_escape($notes)
29                 .")";
30
31         begin_transaction();
32
33         $ret = db_query($sql, "Can't insert crm person");
34         $id = db_insert_id();
35         if ($ret && $cat_ids) {
36                 if(!update_person_contacts($id, $cat_ids, $entity))
37                         return null;
38         }
39         commit_transaction();
40         return $id;
41 }
42
43 function update_crm_person($id, $ref, $name, $name2, $address, $phone, $phone2, $fax, $email, 
44         $lang, $notes, $cat_ids, $entity=null)
45 {
46         $sql = "UPDATE ".TB_PREF."crm_persons SET "
47                   ."ref=".db_escape($ref) . ", "
48                   ."name=".db_escape($name) . ", "
49                   ."name2=".db_escape($name2) . ", "
50                   ."address=".db_escape($address) . ", "
51                   ."phone=".db_escape($phone) . ", "
52                   ."phone2=".db_escape($phone2) . ", "
53                   ."fax=".db_escape($fax) . ", "
54                   ."email=".db_escape($email) . ", "
55                   ."lang=".db_escape($lang) . ", "
56                   ."notes=".db_escape($notes)
57                   ." WHERE id = ".db_escape($id);
58
59         begin_transaction();
60
61         $ret = db_query($sql, "Can't update crm person");
62         if ($ret) {
63                 if(!update_person_contacts($id, $cat_ids, $entity))
64                         return null;
65         }
66         commit_transaction();
67         return $id;
68 }
69
70 function delete_crm_person($person, $with_contacts=false)
71 {
72         begin_transaction();
73
74         if ($with_contacts) {
75                 $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE person_id=".db_escape($person);
76                 db_query($sql, "Can't delete crm contacts");
77         }
78         $sql = "DELETE FROM ".TB_PREF."crm_persons WHERE id=".db_escape($person);
79         $ret = db_query($sql, "Can't delete crm person");
80
81         commit_transaction();
82         return $ret;
83 }
84 /*
85         Retrieve full contact data from database for selected type/action/entity or person
86 */
87 function get_crm_persons($type=null, $action=null, $entity=null, $person=null, $unique=false)
88 {
89         $sql = "SELECT t.*, p.*, r.id as contact_id FROM ".TB_PREF."crm_persons p,"
90                 .TB_PREF."crm_categories t, "
91                 .TB_PREF."crm_contacts r WHERE ";
92         $sel = array('r.type=t.type', 'r.action=t.action', 'r.person_id=p.id');
93
94         if ($type) 
95                 $sel[] = 't.type='.db_escape($type);
96
97         if ($action) 
98                 $sel[] = 't.action='.db_escape($action);
99
100         if ($entity) 
101                 $sel[] = 'r.entity_id='.db_escape($entity);
102
103         if ($person)
104                 $sel[] = 'r.person_id='.db_escape($person);
105
106         $sql .= implode (" AND ", $sel);
107
108         if ($unique)
109                 $sql .= " GROUP BY person_id";
110         else
111                 $sql .= " ORDER BY contact_id";
112
113         return db_query($sql, "Can't get crm persons");
114 }
115
116 function get_crm_person($id)
117 {
118         $sql = "SELECT * FROM ".TB_PREF."crm_persons WHERE id=".db_escape($id);
119
120         $res = db_query($sql, "Can't get crm persons");
121
122         $person = db_fetch($res);
123         $person['contacts'] = get_person_contacts($id);
124
125         return $person;
126 }
127
128 /*
129         Returns all contacts for given person id
130 */
131 function get_person_contacts($id)
132 {
133         $sql = "SELECT t.id FROM "
134                 .TB_PREF."crm_categories t, "
135                 .TB_PREF."crm_contacts r WHERE r.type=t.type AND r.action=t.action 
136                         AND r.person_id=".db_escape($id);
137
138         $contacts = array();
139         $ret = db_query($sql, "Can't get crm person contacts");
140         while($cont = db_fetch_row($ret))
141                 $contacts[] = $cont[0];
142         return $contacts;
143 }
144
145 function update_person_contacts($id, $cat_ids, $entity_id=null) 
146 {
147         $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE person_id=".db_escape($id);
148
149         begin_transaction();
150
151         $ret = db_query($sql, "Can't delete person contacts");
152
153         if($ret && count($cat_ids)) {
154                 array_walk($cat_ids,'db_escape');
155                 $sql = "INSERT INTO ".TB_PREF."crm_contacts (person_id,type,action,entity_id)
156                         SELECT ".db_escape($id).",t.type, t.action,".db_escape($entity_id, true)."
157                         FROM ".TB_PREF."crm_categories t WHERE t.id=".implode(' OR t.id=', $cat_ids);
158                 $ret = db_query($sql, "Can't update person contacts");
159         }
160         commit_transaction();
161         return $ret;
162 }
163
164 function delete_entity_contacts($class, $entity)
165 {
166         $res = get_crm_persons($class, null, $entity, null, true);
167
168         while($person = db_fetch($res)) {
169                 delete_crm_person($person['id'], true);
170         }
171 }
172
173 //-----------------------------------------------------------------------------------------------
174
175 function add_crm_category($type, $action, $name, $description)
176 {
177         $sql = "INSERT INTO ".TB_PREF."crm_categories (type, action, name, description)
178                 VALUES (".db_escape($type) . ", "
179                   .db_escape($action) . ", "
180                   .db_escape($name) . ", "
181                   .db_escape($description)
182                 .")";
183         db_query($sql,"The insert of the crm category failed");
184 }
185
186 function update_crm_category($selected_id, $type, $action, $name, $description)
187 {
188         $sql = "UPDATE ".TB_PREF."crm_categories SET ";
189         if ($type)
190                 $sql .= "type=".db_escape($type) . ",";
191         if ($action)
192                 $sql .= "action=".db_escape($action) . ",";
193         $sql .= "name=".db_escape($name) . ","
194                         ."description=".db_escape($description)
195                         ." WHERE id = ".db_escape($selected_id);
196         db_query($sql,"The update of the crm category failed");
197 }
198
199 function delete_crm_category($selected_id)
200 {
201         // preserve system categories
202         $sql="DELETE FROM ".TB_PREF."crm_categories WHERE system=0 AND id=".db_escape($selected_id);
203         db_query($sql,"could not delete crm category");
204 }
205
206 function get_crm_categories($show_inactive)
207 {
208         $sql = "SELECT * FROM ".TB_PREF."crm_categories";
209         if (!$show_inactive) $sql .= " WHERE !inactive";
210         $sql .= " ORDER BY type, action";
211         return db_query($sql,"could not get areas");
212 }
213
214 function get_crm_category($selected_id)
215 {
216         $sql = "SELECT * FROM ".TB_PREF."crm_categories WHERE id=".db_escape($selected_id);
217
218         $result = db_query($sql,"could not get crm category");
219         return db_fetch($result);
220 }
221
222 function get_crm_category_name($id)
223 {
224         $sql = "SELECT name FROM ".TB_PREF."crm_categories WHERE id=".db_escape($id);
225
226         $result = db_query($sql, "could not get sales type");
227
228         $row = db_fetch_row($result);
229         return $row[0];
230 }
231
232 //----------------------------------------------------------------------------------------
233 //      Contact is relation between person and entity in some category
234 //
235 function add_crm_contact($type, $action, $entity_id, $person_id)
236 {
237         $sql = "INSERT INTO ".TB_PREF."crm_contacts (person_id, type, action, entity_id) VALUES ("
238                 .db_escape($person_id) . ","
239                 .db_escape($type) . ","
240                 .db_escape($action) . ","
241                 .db_escape($entity_id) . ")";
242         return db_query($sql, "Can't insert crm contact");
243 }
244 /*
245         Delete contact selected by unique id.
246 */
247 function delete_crm_contact($id)
248 {
249         $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE id=".db_escape($id);
250
251         return db_query($sql, "Can't delete crm contact");
252 }
253 /*
254         Delete selected contacts for given person
255 */
256 function delete_crm_contacts($person_id, $type = null, $entity_id=null, $action = null)
257 {
258         $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE person_id=".db_escape($person_id);
259         if ($type)
260                 $sql .= ' AND type='.db_escape($type);
261         if ($entity_id)
262                 $sql .= ' AND entity_id='.db_escape($entity_id);
263         if ($action)
264                 $sql .= ' AND action='.db_escape($action);
265
266         return db_query($sql, "Can't delete crm contact");
267 }
268
269 /*
270         Returns person data for given contact id
271 */
272 function get_crm_contact($id)
273 {
274         $sql = "SELECT t.type, t.action, p.*, r.person_id, r.id  FROM ".TB_PREF."crm_persons p,"
275                 .TB_PREF."crm_categories t, "
276                 .TB_PREF."crm_contacts r WHERE r.type=t.type AND r.action=t.action AND r.person_id=p.id"
277                 ." AND r.id=".db_escape($id);
278         $ret = db_query($sql, "Can't get crm contact");
279         if($ret)
280                 return  db_fetch($ret, "Can't fetch contact data");
281         return $ret;
282 }
283
284 ?>