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 ***********************************************************************/
13 function add_crm_person($ref, $name, $name2, $address, $phone, $phone2, $fax, $email, $lang, $notes,
14 $cat_ids=null, $entity=null)
16 $sql = "INSERT INTO ".TB_PREF."crm_persons (ref, name, name2, address,
17 phone, phone2, fax, email, lang, notes)
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) . ", "
33 $ret = db_query($sql, "Can't insert crm person");
35 if ($ret && $cat_ids) {
36 if(!update_person_contacts($id, $cat_ids, $entity))
43 function update_crm_person($id, $ref, $name, $name2, $address, $phone, $phone2, $fax, $email,
44 $lang, $notes, $cat_ids, $entity=null)
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);
61 $ret = db_query($sql, "Can't update crm person");
63 if(!update_person_contacts($id, $cat_ids, $entity))
70 function delete_crm_person($person, $with_contacts=false)
75 $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE person_id=".db_escape($person);
76 db_query($sql, "Can't delete crm contacts");
78 $sql = "DELETE FROM ".TB_PREF."crm_persons WHERE id=".db_escape($person);
79 $ret = db_query($sql, "Can't delete crm person");
85 Retrieve full contact data from database for selected type/action/entity or person
87 function get_crm_persons($type=null, $action=null, $entity=null, $person=null, $unique=false)
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');
95 $sel[] = 't.type='.db_escape($type);
98 $sel[] = 't.action='.db_escape($action);
101 $sel[] = 'r.entity_id='.db_escape($entity);
104 $sel[] = 'r.person_id='.db_escape($person);
106 $sql .= implode (" AND ", $sel);
109 $sql .= " GROUP BY person_id";
111 $sql .= " ORDER BY contact_id";
113 $result = db_query($sql, "Can't get crm persons");
114 // fallback to general contacts
115 if (!db_num_rows($result) && $action && $action != 'general')
116 return get_crm_persons($type, 'general', $entity, $person, $unique);
121 function get_crm_person($id)
123 $sql = "SELECT * FROM ".TB_PREF."crm_persons WHERE id=".db_escape($id);
125 $res = db_query($sql, "Can't get crm persons");
127 $person = db_fetch($res);
128 $person['contacts'] = get_person_contacts($id);
134 Returns all contacts for given person id
136 function get_person_contacts($id)
138 $sql = "SELECT t.id FROM "
139 .TB_PREF."crm_categories t, "
140 .TB_PREF."crm_contacts r WHERE r.type=t.type AND r.action=t.action
141 AND r.person_id=".db_escape($id);
144 $ret = db_query($sql, "Can't get crm person contacts");
145 while($cont = db_fetch_row($ret))
146 $contacts[] = $cont[0];
150 function update_person_contacts($id, $cat_ids, $entity_id=null)
152 $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE person_id=".db_escape($id);
156 $ret = db_query($sql, "Can't delete person contacts");
158 foreach($cat_ids as $n => $cid)
159 $cat_ids[$n] = db_escape($cid);
161 if($ret && count($cat_ids)) {
162 array_walk($cat_ids,'db_escape');
163 $sql = "INSERT INTO ".TB_PREF."crm_contacts (person_id,type,action,entity_id)
164 SELECT ".db_escape($id).",t.type, t.action,".db_escape($entity_id, true)."
165 FROM ".TB_PREF."crm_categories t WHERE t.id=".implode(' OR t.id=', $cat_ids);
166 $ret = db_query($sql, "Can't update person contacts");
168 commit_transaction();
172 function delete_entity_contacts($class, $entity)
174 $res = get_crm_persons($class, null, $entity, null, true);
176 while($person = db_fetch($res)) {
177 delete_crm_person($person['id'], true);
181 //-----------------------------------------------------------------------------------------------
183 function add_crm_category($type, $action, $name, $description)
185 $sql = "INSERT INTO ".TB_PREF."crm_categories (type, action, name, description)
186 VALUES (".db_escape($type) . ", "
187 .db_escape($action) . ", "
188 .db_escape($name) . ", "
189 .db_escape($description)
191 db_query($sql,"The insert of the crm category failed");
194 function update_crm_category($selected_id, $type, $action, $name, $description)
196 $sql = "UPDATE ".TB_PREF."crm_categories SET ";
198 $sql .= "type=".db_escape($type) . ",";
200 $sql .= "action=".db_escape($action) . ",";
201 $sql .= "name=".db_escape($name) . ","
202 ."description=".db_escape($description)
203 ." WHERE id = ".db_escape($selected_id);
204 db_query($sql,"The update of the crm category failed");
207 function delete_crm_category($selected_id)
209 // preserve system categories
210 $sql="DELETE FROM ".TB_PREF."crm_categories WHERE system=0 AND id=".db_escape($selected_id);
211 db_query($sql,"could not delete crm category");
214 function get_crm_categories($show_inactive)
216 $sql = "SELECT * FROM ".TB_PREF."crm_categories";
217 if (!$show_inactive) $sql .= " WHERE !inactive";
218 $sql .= " ORDER BY type, action";
219 return db_query($sql,"could not get areas");
222 function get_crm_category($selected_id)
224 $sql = "SELECT * FROM ".TB_PREF."crm_categories WHERE id=".db_escape($selected_id);
226 $result = db_query($sql,"could not get crm category");
227 return db_fetch($result);
230 function get_crm_category_name($id)
232 $sql = "SELECT name FROM ".TB_PREF."crm_categories WHERE id=".db_escape($id);
234 $result = db_query($sql, "could not get sales type");
236 $row = db_fetch_row($result);
240 //----------------------------------------------------------------------------------------
241 // Contact is relation between person and entity in some category
243 function add_crm_contact($type, $action, $entity_id, $person_id)
245 $sql = "INSERT INTO ".TB_PREF."crm_contacts (person_id, type, action, entity_id) VALUES ("
246 .db_escape($person_id) . ","
247 .db_escape($type) . ","
248 .db_escape($action) . ","
249 .db_escape($entity_id) . ")";
250 return db_query($sql, "Can't insert crm contact");
253 Delete contact selected by unique id.
255 function delete_crm_contact($id)
257 $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE id=".db_escape($id);
259 return db_query($sql, "Can't delete crm contact");
262 Delete selected contacts for given person
264 function delete_crm_contacts($person_id, $type = null, $entity_id=null, $action = null)
266 $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE person_id=".db_escape($person_id);
268 $sql .= ' AND type='.db_escape($type);
270 $sql .= ' AND entity_id='.db_escape($entity_id);
272 $sql .= ' AND action='.db_escape($action);
274 return db_query($sql, "Can't delete crm contact");
278 Returns person data for given contact id
280 function get_crm_contact($id)
282 $sql = "SELECT t.type, t.action, p.*, r.person_id, r.id FROM ".TB_PREF."crm_persons p,"
283 .TB_PREF."crm_categories t, "
284 .TB_PREF."crm_contacts r WHERE r.type=t.type AND r.action=t.action AND r.person_id=p.id"
285 ." AND r.id=".db_escape($id);
286 $ret = db_query($sql, "Can't get crm contact");
288 return db_fetch($ret, "Can't fetch contact data");