X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;f=includes%2Fdb%2Fcrm_contacts_db.inc;h=463a3fde1cf25a59351332eb70995a468a356547;hb=7618d9d62ee39a932f56565a4ce25d965bf53462;hp=94a5a991594034f10bba5c8a1d81732171628f29;hpb=a01cbf378c448da44758b9b9898eb1acc970e84a;p=fa-stable.git diff --git a/includes/db/crm_contacts_db.inc b/includes/db/crm_contacts_db.inc index 94a5a991..463a3fde 100644 --- a/includes/db/crm_contacts_db.inc +++ b/includes/db/crm_contacts_db.inc @@ -13,6 +13,7 @@ function add_crm_person($ref, $name, $name2, $address, $phone, $phone2, $fax, $email, $lang, $notes, $cat_ids=null, $entity=null) { + begin_transaction(__FUNCTION__, func_get_args()); $sql = "INSERT INTO ".TB_PREF."crm_persons (ref, name, name2, address, phone, phone2, fax, email, lang, notes) VALUES (" @@ -27,10 +28,8 @@ function add_crm_person($ref, $name, $name2, $address, $phone, $phone2, $fax, $e .db_escape($lang) . ", " .db_escape($notes) .")"; - - begin_transaction(); - $ret = db_query($sql, "Can't insert crm person"); + $id = db_insert_id(); if ($ret && $cat_ids) { if(!update_person_contacts($id, $cat_ids, $entity)) @@ -41,8 +40,10 @@ function add_crm_person($ref, $name, $name2, $address, $phone, $phone2, $fax, $e } function update_crm_person($id, $ref, $name, $name2, $address, $phone, $phone2, $fax, $email, - $lang, $notes, $cat_ids, $entity=null) + $lang, $notes, $cat_ids, $entity=null, $type=null) { + begin_transaction(__FUNCTION__, func_get_args()); + $sql = "UPDATE ".TB_PREF."crm_persons SET " ."ref=".db_escape($ref) . ", " ."name=".db_escape($name) . ", " @@ -56,11 +57,9 @@ function update_crm_person($id, $ref, $name, $name2, $address, $phone, $phone2, ."notes=".db_escape($notes) ." WHERE id = ".db_escape($id); - begin_transaction(); - $ret = db_query($sql, "Can't update crm person"); if ($ret) { - if(!update_person_contacts($id, $cat_ids, $entity)) + if(!update_person_contacts($id, $cat_ids, $entity, $type)) return null; } commit_transaction(); @@ -69,7 +68,7 @@ function update_crm_person($id, $ref, $name, $name2, $address, $phone, $phone2, function delete_crm_person($person, $with_contacts=false) { - begin_transaction(); + begin_transaction(__FUNCTION__, func_get_args()); if ($with_contacts) { $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE person_id=".db_escape($person); @@ -147,11 +146,14 @@ function get_person_contacts($id) return $contacts; } -function update_person_contacts($id, $cat_ids, $entity_id=null) +function update_person_contacts($id, $cat_ids, $entity_id=null, $type=null) { + begin_transaction(__FUNCTION__, func_get_args()); + $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE person_id=".db_escape($id); + if ($type) + $sql .= " AND type=".db_escape($type); - begin_transaction(); $ret = db_query($sql, "Can't delete person contacts"); @@ -163,6 +165,8 @@ function update_person_contacts($id, $cat_ids, $entity_id=null) $sql = "INSERT INTO ".TB_PREF."crm_contacts (person_id,type,action,entity_id) SELECT ".db_escape($id).",t.type, t.action,".db_escape($entity_id, true)." FROM ".TB_PREF."crm_categories t WHERE t.id=".implode(' OR t.id=', $cat_ids); + if ($type) + $sql .= " AND t.type=".db_escape($type); $ret = db_query($sql, "Can't update person contacts"); } commit_transaction(); @@ -171,17 +175,27 @@ function update_person_contacts($id, $cat_ids, $entity_id=null) function delete_entity_contacts($class, $entity) { - $res = get_crm_persons($class, null, $entity, null, true); + begin_transaction(__FUNCTION__, func_get_args()); + delete_crm_contacts(null, $class, $entity); + // cleanup + $res = get_crm_persons($class, null, $entity, null, true); while($person = db_fetch($res)) { - delete_crm_person($person['id'], true); + $rels = get_person_contacts($person['id']); + if (count($rels) == 0) { + delete_crm_person($person['id']); + } } + + commit_transaction(); } //----------------------------------------------------------------------------------------------- function add_crm_category($type, $action, $name, $description) { + begin_transaction(__FUNCTION__, func_get_args()); + $sql = "INSERT INTO ".TB_PREF."crm_categories (type, action, name, description) VALUES (".db_escape($type) . ", " .db_escape($action) . ", " @@ -189,10 +203,14 @@ function add_crm_category($type, $action, $name, $description) .db_escape($description) .")"; db_query($sql,"The insert of the crm category failed"); + + commit_transaction(); } function update_crm_category($selected_id, $type, $action, $name, $description) { + begin_transaction(__FUNCTION__, func_get_args()); + $sql = "UPDATE ".TB_PREF."crm_categories SET "; if ($type) $sql .= "type=".db_escape($type) . ","; @@ -202,13 +220,19 @@ function update_crm_category($selected_id, $type, $action, $name, $description) ."description=".db_escape($description) ." WHERE id = ".db_escape($selected_id); db_query($sql,"The update of the crm category failed"); + + commit_transaction(); } function delete_crm_category($selected_id) { + begin_transaction(__FUNCTION__, func_get_args()); + // preserve system categories $sql="DELETE FROM ".TB_PREF."crm_categories WHERE system=0 AND id=".db_escape($selected_id); db_query($sql,"could not delete crm category"); + + commit_transaction(); } function get_crm_categories($show_inactive) @@ -261,17 +285,20 @@ function delete_crm_contact($id) /* Delete selected contacts for given person */ -function delete_crm_contacts($person_id, $type = null, $entity_id=null, $action = null) +function delete_crm_contacts($person_id = null, $type = null, $entity_id=null, $action = null) { - $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE person_id=".db_escape($person_id); + $sql = "DELETE FROM ".TB_PREF."crm_contacts WHERE "; + + if ($person_id) + $where[] = 'person_id='.db_escape($person_id); if ($type) - $sql .= ' AND type='.db_escape($type); + $where[] = 'type='.db_escape($type); if ($entity_id) - $sql .= ' AND entity_id='.db_escape($entity_id); + $where[] = 'entity_id='.db_escape($entity_id); if ($action) - $sql .= ' AND action='.db_escape($action); + $where[] = 'action='.db_escape($action); - return db_query($sql, "Can't delete crm contact"); + return db_query($sql.implode(' AND ', $where), "Can't delete crm contact"); } /* @@ -289,4 +316,16 @@ function get_crm_contact($id) return $ret; } -?> \ No newline at end of file +/* + Check for whether category is used in contacts. +*/ +function is_crm_category_used($id) +{ + $row = get_crm_category($id); + $sql = "SELECT COUNT(*) FROM ".TB_PREF."crm_contacts WHERE type='".$row['type']."' AND action='".$row['action']."'"; + $result = db_query($sql, "check relations for crm_contacts failed"); + $contacts = db_fetch($result); + return $contacts[0]; +} + +