root/vtigercrm/branches/5.2.0/modules/Leads/LeadConvertToEntities.php

Revision 13043, 28.0 kB (checked in by musavir, 6 months ago)

Campaigns-Accounts relationship - Sripriya
==========================================

1. added relationship b/w Accounts and Campaigns module.

  • Property svn:mime-type set to text/plain
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /*+********************************************************************************
3  * The contents of this file are subject to the vtiger CRM Public License Version 1.0
4  * ("License"); You may not use this file except in compliance with the License
5  * The Original Code is:  vtiger CRM Open Source
6  * The Initial Developer of the Original Code is vtiger.
7  * Portions created by vtiger are Copyright (C) vtiger.
8  * All Rights Reserved.
9  ********************************************************************************/
10
11 require_once('include/database/PearDatabase.php');
12 require_once('modules/Leads/Leads.php');
13 require_once('include/ComboUtil.php');
14 //Getting the Parameters from the ConvertLead Form
15 $id = vtlib_purify($_REQUEST["record"]);
16
17 $module = vtlib_purify($_REQUEST["module"]);
18 $createpotential = $_REQUEST["createpotential"];
19 $potential_name = $_REQUEST["potential_name"];
20 $close_date = getDBInsertDateValue($_REQUEST["closedate"]);
21 $current_user_id = $_REQUEST["current_user_id"];
22 $assigned_to = $_REQUEST["assigntype"];
23
24 if($assigned_to == "U")
25         $assigned_user_id = $_REQUEST["assigned_user_id"];
26 else
27         $assigned_user_id = $_REQUEST["assigned_group_id"];
28
29 $accountname = $_REQUEST['account_name'];
30 $potential_amount = $_REQUEST['potential_amount'];
31 $potential_sales_stage = $_REQUEST['potential_sales_stage'];
32
33 global $log,$current_user;
34 require('user_privileges/user_privileges_'.$current_user->id.'.php');
35 if($assigned_to == "U")
36         $log->debug("id = $id \n assigned_user_id = $assigned_user_id \n createpotential = $createpotential \n close date = $close_date \n current user id = $current_user_id \n accountname = $accountname \n module = $module");
37 else
38         $log->debug("id = $id \n assigned_user_id = $assigned_group_id \n createpotential = $createpotential \n close date = $close_date \n current user id = $current_user_id \n accountname = $accountname \n module = $module");
39
40 $rate_symbol=getCurrencySymbolandCRate($user_info['currency_id']);
41 $rate = $rate_symbol['rate'];
42 if($potential_amount != '')
43         $potential_amount = convertToDollar($potential_amount,$rate);
44
45 //Retrieve info from all the vtiger_tables related to leads
46 $focus = new Leads();
47 $focus->retrieve_entity_info($id,"Leads");
48
49 //get all the lead related columns
50 $row = $focus->column_fields;
51
52 $date_entered = $adb->formatDate(date('Y-m-d H:i:s'), true);
53 $date_modified = $adb->formatDate(date('Y-m-d H:i:s'), true);
54
55 /** Function for getting the custom values from leads and saving to vtiger_account/contact/potential custom vtiger_fields.
56  *  @param string $type - Field Type (eg: text, list)
57  *  @param integer $type_id - Field Type ID
58 */
59 function getInsertValues($type,$type_id)
60 {
61         global $id,$adb,$log;
62         $log->debug("Entering getInsertValues(".$type.",".$type_id.") method ...");
63         $sql_convert_lead="select * from vtiger_convertleadmapping ";
64         $convert_result = $adb->pquery($sql_convert_lead, array());
65         $noofrows = $adb->num_rows($convert_result);
66
67         $value_cf_array = Array();
68         for($i=0;$i<$noofrows;$i++)
69         {
70                 $flag="false";
71                 $log->info("In vtiger_convertleadmapping function");
72                 $lead_id=$adb->query_result($convert_result,$i,"leadfid"); 
73                 //Getting the relatd customfields for Accounts/Contact/potential from vtiger_convertleadmapping vtiger_table   
74                 $account_id_val=$adb->query_result($convert_result,$i,"accountfid");
75                 $contact_id_val=$adb->query_result($convert_result,$i,"contactfid");
76                 $potential_id_val=$adb->query_result($convert_result,$i,"potentialfid");
77                
78                 $sql_leads_column="select vtiger_field.uitype,vtiger_field.fieldid,vtiger_field.columnname from vtiger_field,vtiger_tab where vtiger_field.tabid=vtiger_tab.tabid and generatedtype=2 and vtiger_tab.name='Leads' and fieldid=? and vtiger_field.presence in (0,2)"; //getting the columnname for the customfield of the lead
79                  $log->debug("Lead's custom vtiger_field coumn name is ".$sql_leads_column);
80
81                 $lead_column_result = $adb->pquery($sql_leads_column, array($lead_id));
82                 $leads_no_rows = $adb->num_rows($lead_column_result);
83                 if($leads_no_rows>0)
84                 {
85                         $lead_column_name=$adb->query_result($lead_column_result,0,"columnname");
86                         $lead_uitype=$adb->query_result($lead_column_result,0,"uitype");
87                         $sql_leads_val="select $lead_column_name from vtiger_leadscf where leadid=?"; //custom vtiger_field value for lead
88                         $lead_val_result = $adb->pquery($sql_leads_val,array($id));
89                         $lead_value=$adb->query_result($lead_val_result,0,$lead_column_name);
90                          $log->debug("Lead's custom vtiger_field value is ".$lead_value);
91                 }       
92                 //Query for getting the column name for Accounts/Contacts/Potentials if custom vtiger_field for lead is mappped
93                 $sql_type="select vtiger_field.fieldid,vtiger_field.uitype,vtiger_field.columnname from vtiger_field,vtiger_tab where vtiger_field.tabid=vtiger_tab.tabid and generatedtype=2 and vtiger_field.presence in (0,2) and vtiger_tab.name=";
94                 $params = array();
95                 if($type=="Accounts")
96                 {
97                         if($account_id_val!="" && $account_id_val!=0)   
98                         {
99                                 $flag="true";
100                                 $log->info("Getting the  Accounts custom vtiger_field column name  ");
101                                 $sql_type.="'Accounts' and fieldid=?";
102                                 array_push($params, $account_id_val);
103                         }
104                 }
105                 else if($type == "Contacts")
106                 {       
107                         if($contact_id_val!="" && $contact_id_val!=0)   
108                         {
109                                 $flag="true";
110                                 $log->info("Getting the  Contacts custom vtiger_field column name  ");
111                                 $sql_type.="'Contacts' and fieldid=?";
112                                 array_push($params, $contact_id_val);
113                         }
114                 }
115                 else if($type == "Potentials")
116                 {
117                         if($potential_id_val!="" && $potential_id_val!=0)
118                         {
119                                 $flag="true";
120                                 $log->info("Getting the  Potentials custom vtiger_field column name  ");
121                                 $sql_type.="'Potentials' and fieldid=?";
122                                 array_push($params, $potential_id_val);                         
123                         }
124                 }
125                 if($flag=="true")
126                 {
127                         $type_result=$adb->pquery($sql_type, $params);
128                         //To construct the cf array
129             $colname = $adb->query_result($type_result,0,"columnname");
130                         $type_insert_column[] = $colname;
131                         $type_uitype =$adb->query_result($type_result,0,"uitype") ;
132
133                         //To construct the cf array
134                         $ins_val = $adb->query_result($lead_val_result,0,$lead_column_name);
135                        
136                         //This array is used to store the tablename as the key and the value for that table in the custom field of the uitype only for 15 and 33(Multiselect cf)...
137                         if($lead_uitype == 33 || $lead_uitype == 15) {
138                 $lead_val_arr[$colname] = $lead_column_name;
139                 $value_cf_array[$colname]=$ins_val;     
140                         }
141                        
142                         $insert_value[] = $ins_val;
143                 }
144         }
145         if(count($value_cf_array) > 0)
146         {
147                 if(count($type_insert_column) > 0)
148                 {
149                         foreach($value_cf_array as $key => $value)
150                         {
151                                 $tableName = $key;
152                                 $tableVal = $value;
153                                 if($tableVal != '')
154                                 {
155                                         $tab_val = explode ("|##|",$value);
156                                         if(count($tab_val)>0)
157                                         {
158                                                 for($k=0;$k<count($tab_val);$k++)
159                                                 {
160                                                         $val =$tab_val[$k];
161                                                         $sql = "select $tableName from vtiger_$tableName";
162                                                         $numRow = $adb->num_rows($adb->query($sql));
163                                                         $count=0;
164                                                         for($n=0;$n < $numRow;$n++)
165                                                         {
166                                                                 $exist_val = $adb->query_result($adb->query($sql),$n,$tableName);
167                                                                 if(trim($exist_val) == trim($val))
168                                                                 {
169                                                                         $count++;
170                                                                 }
171                                                         }
172                                                         if($count == 0)
173                                                         {
174                                                                 $cfId=$adb->getUniqueID("vtiger_$tableName");
175                                                                 $unique_picklist_value = getUniquePicklistID();
176
177                                                                 $qry="insert into vtiger_$tableName values(?,?,?,?)";
178                                                                 $adb->pquery($qry, array($cfId,trim($val),1,$unique_picklist_value));
179                                                                 //added to fix ticket#4492
180                                                                 $picklistId_qry = "select picklistid from vtiger_picklist where name=?";
181                                                                 $picklistId_res = $adb->pquery($picklistId_qry,array($tableName));
182                                                                 $picklist_Id = $adb->query_result($picklistId_res,0,'picklistid');
183                                                                 //While adding a new value into the picklist table, the roleid's which has permission for the lead cf will only given permission for contacts,potentials and account module while converting. -- refer ticket #4885 ---
184                                                                 $role_qry = "select roleid from vtiger_role2picklist where picklistvalueid in (select picklist_valueid from vtiger_".$lead_val_arr[$tableName]."  where ".$lead_val_arr[$tableName]."='".trim($val)."')";
185                                                                 $numOFRole=$adb->num_rows($adb->query($role_qry));
186                                                                 for($l=0;$l<$numOFRole;$l++)
187                                                                 {
188                                                                         $role_id = $adb->query_result($adb->query($role_qry),$l,'roleid');
189                                                                         $sort_qry = "select max(sortid)+1 as sortid from vtiger_role2picklist where picklistid=? and roleid=?";
190                                                                         $sort_qry_res = $adb->pquery($sort_qry,array($picklist_Id,$role_id));
191                                                                         $sort_id = $adb->query_result($sort_qry_res,0,'sortid');
192                                                                         $role_picklist = "insert into vtiger_role2picklist values (?,?,?,?)";
193                                                                         $adb->pquery($role_picklist,array($role_id,$unique_picklist_value,$picklist_Id,$sort_id));
194                                                                 }
195                                                                 //end
196                                                         }
197                                                 }
198                                         }
199
200                                 }
201                         }
202                 }
203         }
204         $log->debug("columns to be inserted are ".$type_insert_column);
205     $log->debug("columns to be inserted are ".$insert_value);
206         $values = array ('columns'=>$type_insert_column,'values'=>$insert_value);
207         $log->debug("Exiting getInsertValues method ...");
208         return $values;
209 }
210 //function Ends
211
212 /**     Function used to get the lead related Notes and Attachments with other entities Account, Contact and Potential
213  *      @param integer $id - leadid
214  *      @param integer $accountid -  related entity id (accountid)
215  */
216 function getRelatedNotesAttachments($id,$related_id)
217 {
218         global $adb,$log,$id;
219         $log->debug("Entering getRelatedNotesAttachments(".$id.",".$related_id.") method ...");
220        
221         $sql_lead_notes ="select * from vtiger_senotesrel where crmid=?";
222         $lead_notes_result = $adb->pquery($sql_lead_notes, array($id));
223         $noofrows = $adb->num_rows($lead_notes_result);
224
225         for($i=0; $i<$noofrows;$i++ )
226         {
227
228                 $lead_related_note_id=$adb->query_result($lead_notes_result,$i,"notesid");
229                 $log->debug("Lead related note id ".$lead_related_note_id);
230
231                 $sql_insert_notes="insert into vtiger_senotesrel(crmid,notesid) values (?,?)";
232                 $adb->pquery($sql_insert_notes, array($related_id, $lead_related_note_id));
233         }
234
235         $sql_lead_attachment="select * from vtiger_seattachmentsrel where crmid=?";
236     $lead_attachment_result = $adb->pquery($sql_lead_attachment, array($id));
237     $noofrows = $adb->num_rows($lead_attachment_result);
238
239     for($i=0;$i<$noofrows;$i++)
240     {
241         $lead_related_attachment_id=$adb->query_result($lead_attachment_result,$i,"attachmentsid");
242                 $log->debug("Lead related attachment id ".$lead_related_attachment_id);
243
244         $sql_insert_attachment="insert into vtiger_seattachmentsrel(crmid,attachmentsid) values (?,?)";                       
245         $adb->pquery($sql_insert_attachment, array($related_id, $lead_related_attachment_id));
246     }
247         $log->debug("Exiting getRelatedNotesAttachments method ...");
248        
249 }
250
251 /**     Function used to get the lead related activities with other entities Account and Contact
252  *      @param integer $accountid - related entity id
253  *      @param integer $contact_id -  related entity id
254  */
255 function getRelatedActivities($accountid,$contact_id)
256 {
257         global $adb,$log,$id;   
258         $log->debug("Entering getRelatedActivities(".$accountid.",".$contact_id.") method ...");
259         $sql_lead_activity="select * from vtiger_seactivityrel where crmid=?";
260         $lead_activity_result = $adb->pquery($sql_lead_activity, array($id));
261     $noofrows = $adb->num_rows($lead_activity_result);
262     for($i=0;$i<$noofrows;$i++)
263     {
264         $lead_related_activity_id=$adb->query_result($lead_activity_result,$i,"activityid");
265                 $log->debug("Lead related vtiger_activity id ".$lead_related_activity_id);
266
267                 $sql_type_email="select setype from vtiger_crmentity where crmid=?";
268                 $type_email_result = $adb->pquery($sql_type_email, array($lead_related_activity_id));
269         $type=$adb->query_result($type_email_result,0,"setype");
270                 $log->debug("type of vtiger_activity id ".$type);
271
272             $sql_delete_lead_activity="delete from vtiger_seactivityrel where crmid=?";
273             $adb->pquery($sql_delete_lead_activity, array($id));
274
275                 if($type != "Emails")
276                 {
277             $sql_insert_account_activity="insert into vtiger_seactivityrel(crmid,activityid) values (?,?)";
278                 $adb->pquery($sql_insert_account_activity, array($accountid, $lead_related_activity_id));
279
280                         $sql_insert_contact_activity="insert into vtiger_cntactivityrel(contactid,activityid) values (?,?)";
281             $adb->pquery($sql_insert_contact_activity, array($contact_id, $lead_related_activity_id));
282                 }
283                 else
284                 {
285                          $sql_insert_contact_activity="insert into vtiger_seactivityrel(crmid,activityid) values (?,?)";                                                                                     
286                          $adb->pquery($sql_insert_contact_activity, array($contact_id, $lead_related_activity_id));
287                 }
288     }
289         $log->debug("Exiting getRelatedActivities method ...");
290
291 }
292
293 /**     Function used to save the lead related products with other entities Account, Contact and Potential
294  *      $leadid - leadid
295  *      $relatedid - related entity id (accountid/contactid/potentialid)
296  *      $setype - related module(Accounts/Contacts/Potentials)
297  */
298 function saveLeadRelatedProducts($leadid, $relatedid, $setype)
299 {
300         global $adb, $log;
301         $log->debug("Entering into function saveLeadRelatedProducts($leadid, $relatedid)");
302
303         $product_result = $adb->pquery("select * from vtiger_seproductsrel where crmid=?", array($leadid));
304         $noofproducts = $adb->num_rows($product_result);
305         for($i = 0; $i < $noofproducts; $i++)
306         {
307                 $productid = $adb->query_result($product_result,$i,'productid');
308                 $adb->pquery("insert into vtiger_seproductsrel values(?,?,?)", array($relatedid, $productid, $setype));
309         }
310
311         $log->debug("Exit from function saveLeadRelatedProducts.");
312 }
313
314 /**     Function used to save the lead related Campaigns with Contact
315  *      $leadid - leadid
316  *      $relatedid - related entity id (contactid)
317  */
318 function saveLeadRelatedCampaigns($leadid, $relatedid)
319 {
320         global $adb, $log;
321         $log->debug("Entering into function saveLeadRelatedCampaigns($leadid, $relatedid)");
322
323         $campaign_result = $adb->pquery("select * from vtiger_campaignleadrel where leadid=?", array($leadid));
324         $noofcampaigns = $adb->num_rows($campaign_result);
325         for($i = 0; $i < $noofcampaigns; $i++)
326         {
327                 $campaignid = $adb->query_result($campaign_result,$i,'campaignid');
328
329                 $adb->pquery("insert into vtiger_campaigncontrel (campaignid, contactid, campaignrelstatusid) values(?,?,1)", array($campaignid, $relatedid));
330                 }
331         $log->debug("Exit from function saveLeadRelatedCampaigns.");
332 }
333 function saveLeadConvertedAccounts($leadid,$relatedid){
334         global $adb, $log;
335         $log->debug("Entering into function saveLeadConvertedAccounts($leadid, $relatedid)");
336         $campaign_result = $adb->pquery("select * from vtiger_campaignleadrel where leadid=?", array($leadid));
337         $noofcampaigns = $adb->num_rows($campaign_result);
338         for($i = 0; $i < $noofcampaigns; $i++)
339         {
340                 $campaignid = $adb->query_result($campaign_result,$i,'campaignid');
341                 $adb->pquery("INSERT INTO vtiger_campaignaccountrel (campaignid, accountid, campaignrelstatusid) values(?,?,1)",array($campaignid,$relatedid));
342                
343         }
344         $log->debug("Exit from function saveLeadConvertedAccounts.");
345 }
346 $crmid ='';
347 if(vtlib_isModuleActive('Accounts') && isPermitted("Accounts","EditView") =='yes'){
348         /*Code integrated to avoid duplicate Account creation during ConvertLead Operation  START-- by Bharathi*/
349         $acc_query = "select vtiger_account.accountid from vtiger_account left join vtiger_crmentity on vtiger_account.accountid = vtiger_crmentity.crmid where vtiger_crmentity.deleted=0 and vtiger_account.accountname = ?";
350         $acc_res = $adb->pquery($acc_query, array($accountname));
351         $acc_rows = $adb->num_rows($acc_res);
352         if($acc_rows != 0){
353                 $crmid = $adb->query_result($acc_res,0,"accountid");
354                 //Retrieve the lead related products and relate them with this new account
355                 getRelatedNotesAttachments($id,$crmid);
356                 saveLeadRelatedProducts($id, $crmid, "Accounts");
357                 saveLeadRelations($id, $crmid, "Accounts");
358                 $accountid=$crmid;
359                 saveLeadConvertedAccounts($id,$accountid);
360
361         } else if($accountname==''){
362                 $crmid='';
363         } else
364         {
365                 $crmid = $adb->getUniqueID("vtiger_crmentity");
366        
367                 //Saving Account - starts
368                
369                 $sql_crmentity = "insert into vtiger_crmentity(crmid,smcreatorid,smownerid,setype,presence,createdtime,modifiedtime,deleted,description) values(?,?,?,?,?,?,?,?,?)";
370                 $sql_params = array($crmid, $current_user_id, $assigned_user_id, 'Accounts', 1, $date_entered, $date_modified, 0, $row['description']);
371                 $adb->pquery($sql_crmentity, $sql_params);
372                
373                 //Module Sequence Numbering
374                 require_once('modules/Accounts/Accounts.php');
375                 $acc_no_focus = new Accounts();
376                 $account_no = $acc_no_focus->setModuleSeqNumber("increment",'Accounts');
377                 // END
378                
379                 /* Modified by Minnie to fix the convertlead issue -- START*/
380                 if(isset($row["annualrevenue"]) && !empty($row["annualrevenue"])) $annualrevenue = $row["annualrevenue"];
381                 else $annualrevenue = 'null';
382                
383                 if(isset($row["noofemployees"]) && !empty($row["noofemployees"])) $employees = $row["noofemployees"];
384                 else $employees = 'null';
385                
386                 $sql_insert_account = "INSERT INTO vtiger_account (account_no,accountid,accountname,industry,annualrevenue,phone,fax,rating,email1,website,employees) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
387                 /* Modified by Minnie -- END*/
388                 $account_params = array($account_no,$crmid, $accountname, $row["industry"], $annualrevenue, $row["phone"], $row["fax"], $row["rating"], $row["email"], $row["website"], $employees);
389                 $adb->pquery($sql_insert_account, $account_params);
390                
391                 /*if($assigned_to !="U"){
392                         $sql_crmentity = "insert into vtiger_accountgrouprelation(accountid,groupname) values(?,?)";
393                         $sql_params = array($crmid,$assigned_group_name);
394                         $adb->pquery($sql_crmentity, $sql_params);
395                 }*/
396                
397                 $sql_insert_accountbillads = "INSERT INTO vtiger_accountbillads (accountaddressid,bill_city,bill_code,bill_country,bill_state,bill_street,bill_pobox) VALUES (?,?,?,?,?,?,?)";
398                 $billads_params = array($crmid, $row["city"], $row["code"], $row["country"], $row["state"], $row["lane"], $row["pobox"]);
399                 $adb->pquery($sql_insert_accountbillads, $billads_params);
400        
401                 $sql_insert_accountshipads = "INSERT INTO vtiger_accountshipads (accountaddressid,ship_city,ship_code,ship_country,ship_pobox,ship_state,ship_street) VALUES (?,?,?,?,?,?,?)";
402                 $shipads_params = array($crmid, $row["city"], $row["code"], $row["country"], $row["pobox"], $row["state"], $row["lane"]);
403                 $adb->pquery($sql_insert_accountshipads, $shipads_params);
404        
405                 //Getting the custom vtiger_field values from leads and inserting into Accounts if the vtiger_field is mapped - Jaguar
406                 $col_val= getInsertValues("Accounts",$crmid);
407                 $insert_columns = $col_val['columns'];
408                 $insert_columns[] = "accountid";
409                 $insert_values = $col_val['values'];
410                 $insert_values[] = $crmid;
411                 $insert_val_str = generateQuestionMarks($insert_values);
412                 $sql_insert_accountcustomfield = "INSERT INTO vtiger_accountscf (". implode(",",$insert_columns) .") VALUES (".$insert_val_str.")";
413                 $adb->pquery($sql_insert_accountcustomfield, $insert_values);
414                 //Saving Account - ends
415                        
416                 getRelatedNotesAttachments($id,$crmid);
417                 //Retrieve the lead related products and relate them with this new account
418                 saveLeadRelatedProducts($id, $crmid, "Accounts");
419                 saveLeadRelations($id, $crmid, "Accounts");
420                 $accountid = $crmid;
421                 //Retrieve the lead related Campaigns and relate them with this new Account
422                 saveLeadConvertedAccounts($id, $accountid);
423         }
424 }
425 //Up to this, Account related data save finshed
426
427 if(vtlib_isModuleActive('Contacts') && isPermitted("Contacts","EditView") =='yes'){
428         $date_entered = $adb->formatDate(date('Y-m-d H:i:s'), true);
429         $date_modified = $adb->formatDate(date('Y-m-d H:i:s'), true);
430        
431         //Saving Contact - starts
432         $crmcontactid = $adb->getUniqueID("vtiger_crmentity");
433        
434         $sql_crmentity1 = "insert into vtiger_crmentity(crmid,smcreatorid,smownerid,setype,presence,deleted,description,createdtime,modifiedtime) values(?,?,?,?,?,?,?,?,?)";
435         $sql_params = array($crmcontactid, $current_user_id, $assigned_user_id, 'Contacts', 0, 0, $row['description'], $date_entered, $date_modified);
436         $adb->pquery($sql_crmentity1, $sql_params);
437        
438         $contact_id = $crmcontactid;
439         $log->debug("contact id is ".$contact_id);
440        
441         // Module Sequence Numbering
442         require_once('modules/Contacts/Contacts.php');
443         $cont_no_focus = new Contacts();
444         $contact_no = $cont_no_focus->setModuleSeqNumber("increment",'Contacts');
445         // END
446        
447         $sql_insert_contact = "INSERT INTO vtiger_contactdetails (contact_no,contactid,accountid,salutation,firstname,lastname,email,phone,mobile,title,fax,yahooid) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
448         $contact_params = array($contact_no, $contact_id, $crmid, $row["salutationtype"], $row["firstname"], $row["lastname"], $row["email"], $row["phone"], $row["mobile"], $row["designation"], $row["fax"], $row['yahooid']);
449         $adb->pquery($sql_insert_contact, $contact_params);
450        
451         /*if($assigned_to !="U"){
452                 $sql_crmentity = "insert into vtiger_contactgrouprelation(contactid,groupname) values(?,?)";
453                 $sql_params = array($contact_id,$assigned_group_name);
454                 $adb->pquery($sql_crmentity, $sql_params);
455         }*/
456         $sql_insert_contactsubdetails = "INSERT INTO vtiger_contactsubdetails (contactsubscriptionid,homephone,otherphone,leadsource) VALUES (?,?,?,?)";
457         $c_subd_params = array($contact_id, '', '', $row['leadsource']);
458         $adb->pquery($sql_insert_contactsubdetails, $c_subd_params);
459        
460         $sql_insert_contactaddress = "INSERT INTO vtiger_contactaddress (contactaddressid,mailingcity,mailingstreet,mailingstate,mailingcountry,mailingpobox,mailingzip) VALUES (?,?,?,?,?,?,?)";
461         $c_addr_params = array($contact_id, $row["city"], $row["lane"], $row['state'], $row["country"], $row["pobox"], $row['code']);
462         $adb->pquery($sql_insert_contactaddress, $c_addr_params);
463        
464         $sql_insert_customerdetails = "INSERT INTO vtiger_customerdetails (customerid) VALUES (?)";
465         $adb->pquery($sql_insert_customerdetails, array($contact_id));
466        
467         //Getting the customfield values from leads and inserting into the respected ContactCustomfield to which it is mapped - Jaguar
468         $col_val= getInsertValues("Contacts",$contact_id);
469         $insert_columns = $col_val['columns'];
470         $insert_columns[] = "contactid";
471         $insert_values = $col_val['values'];
472         $insert_values[] = $contact_id;
473         $insert_val_str = generateQuestionMarks($insert_values);
474         $sql_insert_contactcustomfield = "INSERT INTO vtiger_contactscf (". implode(",",$insert_columns) .") VALUES (".$insert_val_str.")";
475         $adb->pquery($sql_insert_contactcustomfield, $insert_values);
476         //Saving Contact - ends
477        
478         getRelatedActivities($crmid,$contact_id); //To convert relates Activites  and Email -Jaguar
479         getRelatedNotesAttachments($id,$contact_id);
480        
481         //Retrieve the lead related products and relate them with this new contact
482         saveLeadRelatedProducts($id, $contact_id, "Contacts");
483         saveLeadRelations($id, $contact_id, "Contacts");
484        
485         //Retrieve the lead related Campaigns and relate them with this new contact --Minnie
486         saveLeadRelatedCampaigns($id, $contact_id);
487 }
488        
489 //Up to this, Contact related data save finshed
490
491 //Saving Potential - starts
492 if((! isset($createpotential) || ! $createpotential == "on") && (!empty($potential_name)) && isPermitted("Potentials","EditView") =='yes' && ($crmid != '' || $crmcontactid != '') && vtlib_isModuleActive('Potentials'))
493 {
494         $log->info("createpotential is not set");
495
496         $date_entered = $adb->formatDate(date('Y-m-d H:i:s'), true);
497         $date_modified = $adb->formatDate(date('Y-m-d H:i:s'), true);
498  
499         $oppid = $adb->getUniqueID("vtiger_crmentity");
500        
501         $sql_crmentity = "insert into vtiger_crmentity(crmid,smcreatorid,smownerid,setype,presence,deleted,createdtime,modifiedtime,description) values(?,?,?,?,?,?,?,?,?)";
502         $sql_params = array($oppid, $current_user_id, $assigned_user_id, 'Potentials', 0, 0, $date_entered, $date_modified, $row['description']);
503         $adb->pquery($sql_crmentity, $sql_params);     
504        
505         // Module Sequence Numbering
506         require_once('modules/Potentials/Potentials.php');
507         $pot_no_focus = new Potentials();
508         $potential_no = $pot_no_focus->setModuleSeqNumber("increment",'Potentials');
509         // END
510
511         if(!isset($potential_amount) || $potential_amount == null)
512         {
513                 $potential_amount=0;
514         }
515        
516         if($crmid!=''){
517                 $related_to = $crmid;
518         } else {
519                 $related_to = $crmcontactid;
520         }
521        
522         $sql_insert_opp = "INSERT INTO vtiger_potential (potential_no,potentialid,related_to,potentialname,leadsource,closingdate,sales_stage,amount) VALUES (?,?,?,?,?,?,?,?)";
523         $opp_params = array($potential_no, $oppid, $related_to, $potential_name, $row['leadsource'], $close_date, $potential_sales_stage, $potential_amount);
524         $adb->pquery($sql_insert_opp, $opp_params);
525
526         //Getting the customfield values from leads and inserting into the respected PotentialCustomfield to which it is mapped - Jaguar
527         $col_val= getInsertValues("Potentials",$oppid);
528         $insert_columns = $col_val['columns'];
529         $insert_columns[] = "potentialid";
530         $insert_values = $col_val['values'];
531         $insert_values[] = $oppid;
532         $insert_val_str = generateQuestionMarks($insert_values);
533         $sql_insert_potentialcustomfield = "INSERT INTO vtiger_potentialscf (". implode(",",$insert_columns) .") VALUES (".$insert_val_str.")";
534         $adb->pquery($sql_insert_potentialcustomfield, $insert_values);
535
536         if($crmid != '' && $contact_id != ''){
537                 $sql_insert2contpotentialrel ="insert into vtiger_contpotentialrel values(?,?)";
538         $adb->pquery($sql_insert2contpotentialrel, array($contact_id, $oppid));
539         }
540         //Retrieve the lead related products and relate them with this new potential
541         saveLeadRelatedProducts($id, $oppid, "Potentials");
542         saveLeadRelations($id, $oppid, "Potentials");
543 }
544 //Saving Potential - ends
545 //Up to this, Potential related data save finshed
546
547
548 //Deleting from the vtiger_tracker
549 $sql_delete_tracker= "DELETE from vtiger_tracker where item_id=?";
550 $adb->pquery($sql_delete_tracker, array($id));
551 $category = getParentTab();
552 //Updating the deleted status
553 if($crmid != '' || $crmcontactid != ''){
554         $sql_update_converted = "UPDATE vtiger_leaddetails SET converted = 1 where leadid=?";
555         $adb->pquery($sql_update_converted, array($id));
556         //updating the campaign-lead relation --Minnie
557         $sql_update_campleadrel = "delete from vtiger_campaignleadrel where leadid=?";
558         $adb->pquery($sql_update_campleadrel, array($id));
559 }
560 if($crmid!=''){
561         header("Location: index.php?action=DetailView&module=Accounts&record=$crmid&parenttab=$category");
562 } elseif($crmcontactid != '') {
563         header("Location: index.php?action=DetailView&module=Contacts&record=$crmcontactid&parenttab=$category");
564 }else{
565                 echo "<link rel='stylesheet' type='text/css' href='themes/$theme/style.css'>"; 
566         echo "<table border='0' cellpadding='5' cellspacing='0' width='100%' height='450px'><tr><td align='center'>";
567         echo "<div style='border: 3px solid rgb(153, 153, 153); background-color: rgb(255, 255, 255); width: 55%; position: relative; z-index: 10000000;'>
568
569                 <table border='0' cellpadding='5' cellspacing='0' width='98%'>
570                 <tbody><tr>
571                 <td rowspan='2' width='11%'><img src='". vtiger_imageurl('denied.gif', $theme) . "' ></td>
572                 <td style='border-bottom: 1px solid rgb(204, 204, 204);' nowrap='nowrap' width='70%'><span class='genHeaderSmall'>$currentModule $app_strings[CANNOT_CONVERT]</span></td>
573                 </tr>
574                 <tr>
575                 <td class='small' align='right' nowrap='nowrap'>                               
576                 <a href='javascript:window.history.back();'>$app_strings[LBL_GO_BACK]</a><br>                                                                                                                </td>
577                 </tr>
578                 </tbody></table>
579                 </div>";
580         echo "</td></tr></table>";
581 }
582
583 /**     Function used to save the lead related services with other entities Account, Contact and Potential
584  *      $leadid - leadid
585  *      $relatedid - related entity id (accountid/contactid/potentialid)
586  *      $setype - related module(Accounts/Contacts/Potentials)
587  */
588 function saveLeadRelations($leadid, $relatedid, $setype)
589 {
590         global $adb, $log;
591         $log->debug("Entering into function saveLeadRelatedProducts($leadid, $relatedid)");
592
593         $result = $adb->pquery("select * from vtiger_crmentityrel where crmid=?", array($leadid));
594         $noofproducts = $adb->num_rows($result);
595         for($i = 0; $i < $noofproducts; $i++)
596         {
597                 $recordid = $adb->query_result($result,$i,'relcrmid');
598                 $recordmodule = $adb->query_result($result,$i,'relmodule');
599                 $adb->pquery("insert into vtiger_crmentityrel values(?,?,?,?)", array($relatedid, $setype, $recordid, $recordmodule));
600         }
601         $result = $adb->pquery("select * from vtiger_crmentityrel where relcrmid=?", array($leadid));
602         $noofproducts = $adb->num_rows($result);
603         for($i = 0; $i < $noofproducts; $i++)
604         {
605                 $recordid = $adb->query_result($result,$i,'crmid');
606                 $recordmodule = $adb->query_result($result,$i,'module');
607                 $adb->pquery("insert into vtiger_crmentityrel values(?,?,?,?)", array($relatedid, $setype, $recordid, $recordmodule));
608         }
609
610         $log->debug("Exit from function saveLeadRelatedProducts.");
611 }
612
613 ?>
Note: See TracBrowser for help on using the browser.