[ Index ]
 

Code source de vtiger CRM 5.0.2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/modules/Leads/ -> LeadConvertToEntities.php (source)

   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  
  12  require_once ('include/database/PearDatabase.php');
  13  require_once ('modules/Leads/Leads.php');
  14  //Getting the Parameters from the ConvertLead Form
  15  $id = $_REQUEST["record"];
  16  
  17  
  18  $module = $_REQUEST["module"];
  19  $createpotential = $_REQUEST["createpotential"];
  20  $potential_name = $_REQUEST["potential_name"];
  21  $close_date = getDBInsertDateValue($_REQUEST["closedate"]);
  22  $current_user_id = $_REQUEST["current_user_id"];
  23  $assigned_user_id = $_REQUEST["assigned_user_id"];
  24  $accountname = $_REQUEST['account_name'];
  25  $potential_amount = $_REQUEST['potential_amount'];
  26  $potential_sales_stage = $_REQUEST['potential_sales_stage'];
  27  
  28  global $log,$current_user;
  29  require('user_privileges/user_privileges_'.$current_user->id.'.php');
  30  $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");
  31  
  32  $rate_symbol=getCurrencySymbolandCRate($user_info['currency_id']);
  33  $rate = $rate_symbol['rate'];
  34  if($potential_amount != '')
  35          $potential_amount = convertToDollar($potential_amount,$rate);
  36      
  37  $check_unit = explode("-",$potential_name);
  38  if($check_unit[1] == "")
  39          $potential_name = $check_unit[0];
  40  
  41  //Retrieve info from all the vtiger_tables related to leads
  42  $focus = new Leads();
  43  $focus->retrieve_entity_info($id,"Leads");
  44  
  45  //get all the lead related columns 
  46  $row = $focus->column_fields;
  47  
  48  $date_entered = $adb->formatDate(date('YmdHis'));
  49  $date_modified = $adb->formatDate(date('YmdHis'));
  50  
  51  /** Function for getting the custom values from leads and saving to vtiger_account/contact/potential custom vtiger_fields.
  52   *  @param string $type - Field Type (eg: text, list)
  53   *  @param integer $type_id - Field Type ID 
  54  */
  55  function getInsertValues($type,$type_id)
  56  {
  57      global $id,$adb,$log;
  58      $log->debug("Entering getInsertValues(".$type.",".$type_id.") method ...");
  59      $sql_convert_lead="select * from vtiger_convertleadmapping ";
  60      $convert_result = $adb->query($sql_convert_lead);
  61      $noofrows = $adb->num_rows($convert_result);
  62  
  63      for($i=0;$i<$noofrows;$i++)
  64      {
  65          $flag="false";
  66           $log->info("In vtiger_convertleadmapping function");
  67          $lead_id=$adb->query_result($convert_result,$i,"leadfid");  
  68          //Getting the relatd customfields for Accounts/Contact/potential from vtiger_convertleadmapping vtiger_table    
  69          $account_id_val=$adb->query_result($convert_result,$i,"accountfid");
  70          $contact_id_val=$adb->query_result($convert_result,$i,"contactfid");
  71          $potential_id_val=$adb->query_result($convert_result,$i,"potentialfid");
  72          
  73          $sql_leads_column="select 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=".$lead_id; //getting the columnname for the customfield of the lead
  74  
  75           $log->debug("Lead's custom vtiger_field coumn name is ".$sql_leads_column);
  76  
  77          $lead_column_result = $adb->query($sql_leads_column);
  78          $leads_no_rows = $adb->num_rows($lead_column_result);
  79          if($leads_no_rows>0)
  80          {
  81              $lead_column_name=$adb->query_result($lead_column_result,0,"columnname");
  82              $sql_leads_val="select ".$lead_column_name." from vtiger_leadscf where leadid=".$id; //custom vtiger_field value for lead
  83              $lead_val_result = $adb->query($sql_leads_val);
  84              $lead_value=$adb->query_result($lead_val_result,0,$lead_column_name);
  85               $log->debug("Lead's custom vtiger_field value is ".$lead_value);
  86          }    
  87          //Query for getting the column name for Accounts/Contacts/Potentials if custom vtiger_field for lead is mappped
  88          $sql_type="select 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="; 
  89          if($type=="Accounts")
  90          {
  91              if($account_id_val!="" && $account_id_val!=0)    
  92              {
  93                  $flag="true";
  94                   $log->info("Getting the  Accounts custom vtiger_field column name  ");
  95                  $sql_type.="'Accounts' and fieldid=".$account_id_val;
  96              }
  97          }
  98          else if($type == "Contacts")
  99          {    
 100              if($contact_id_val!="" && $contact_id_val!=0)    
 101              {
 102                  $flag="true";
 103                   $log->info("Getting the  Contacts custom vtiger_field column name  ");
 104                  $sql_type.="'Contacts' and fieldid=".$contact_id_val;
 105              }
 106          }
 107          else if($type == "Potentials")
 108          {
 109              if($potential_id_val!="" && $potential_id_val!=0)
 110                          {
 111                  $flag="true";
 112                                    $log->info("Getting the  Potentials custom vtiger_field column name  ");
 113                  $sql_type.="'Potentials' and fieldid=".$potential_id_val;
 114                          }
 115  
 116          }
 117          if($flag=="true")
 118          { 
 119              $type_result=$adb->query($sql_type);
 120          
 121              if(isset($type_insert_column))
 122                  $type_insert_column.=",";
 123                      $type_insert_column.=$adb->query_result($type_result,0,"columnname") ;
 124  
 125              if(isset($insert_value))
 126                  $insert_value.=",";
 127              
 128              $insert_value.="'".$adb->query_result($lead_val_result,0,$lead_column_name)."'";
 129          }
 130  
 131      }
 132      $log->debug("columns to be inserted are ".$type_insert_column);
 133          $log->debug("columns to be inserted are ".$insert_value);
 134      $values = array ($type_insert_column,$insert_value);
 135      $log->debug("Exiting getInsertValues method ...");
 136  
 137      return $values;    
 138  }
 139  //function Ends
 140  
 141  /**    Function used to get the lead related Notes and Attachments with other entities Account, Contact and Potential
 142   *    @param integer $id - leadid
 143   *    @param integer $accountid -  related entity id (accountid)
 144   */
 145  function getRelatedNotesAttachments($id,$accountid)
 146  {
 147      global $adb,$log,$id;
 148      $log->debug("Entering getRelatedNotesAttachments(".$id.",".$accountid.") method ...");
 149      
 150      $sql_lead_notes    ="select * from vtiger_senotesrel where crmid=".$id;
 151      $lead_notes_result = $adb->query($sql_lead_notes);
 152      $noofrows = $adb->num_rows($lead_notes_result);
 153  
 154      for($i=0; $i<$noofrows;$i++ )
 155      {
 156  
 157          $lead_related_note_id=$adb->query_result($lead_notes_result,$i,"notesid");
 158           $log->debug("Lead related note id ".$lead_related_note_id);
 159          $sql_delete_lead_notes="delete from vtiger_senotesrel where crmid=".$id;
 160          $adb->query($sql_delete_lead_notes);
 161  
 162          $sql_insert_account_notes="insert into vtiger_senotesrel(crmid,notesid) values (".$accountid.",".$lead_related_note_id.")";
 163          $adb->query($sql_insert_account_notes);
 164      }
 165  
 166      $sql_lead_attachment="select * from vtiger_seattachmentsrel where crmid=".$id;
 167          $lead_attachment_result = $adb->query($sql_lead_attachment);
 168          $noofrows = $adb->num_rows($lead_attachment_result);
 169  
 170          for($i=0;$i<$noofrows;$i++)
 171          {
 172                          
 173                  $lead_related_attachment_id=$adb->query_result($lead_attachment_result,$i,"attachmentsid");
 174           $log->debug("Lead related attachment id ".$lead_related_attachment_id);
 175  
 176                  $sql_delete_lead_attachment="delete from vtiger_seattachmentsrel where crmid=".$id;
 177                  $adb->query($sql_delete_lead_attachment);
 178  
 179                  $sql_insert_account_attachment="insert into vtiger_seattachmentsrel(crmid,attachmentsid) values (".$accountid.",".$lead_related_attachment_id.")";                        
 180                  $adb->query($sql_insert_account_attachment);
 181          }
 182      $log->debug("Exiting getRelatedNotesAttachments method ...");
 183      
 184  }
 185  
 186  /**    Function used to get the lead related activities with other entities Account and Contact 
 187   *    @param integer $accountid - related entity id
 188   *    @param integer $contact_id -  related entity id 
 189   */
 190  function getRelatedActivities($accountid,$contact_id)
 191  {
 192      global $adb,$log,$id;    
 193      $log->debug("Entering getRelatedActivities(".$accountid.",".$contact_id.") method ...");
 194      $sql_lead_activity="select * from vtiger_seactivityrel where crmid=".$id;
 195      $lead_activity_result = $adb->query($sql_lead_activity);
 196          $noofrows = $adb->num_rows($lead_activity_result);
 197          for($i=0;$i<$noofrows;$i++)
 198          {
 199  
 200                  $lead_related_activity_id=$adb->query_result($lead_activity_result,$i,"activityid");
 201           $log->debug("Lead related vtiger_activity id ".$lead_related_activity_id);
 202  
 203          $sql_type_email="select setype from vtiger_crmentity where crmid=".$lead_related_activity_id;
 204          $type_email_result = $adb->query($sql_type_email);
 205                  $type=$adb->query_result($type_email_result,0,"setype");
 206          $log->debug("type of vtiger_activity id ".$type);
 207  
 208                  $sql_delete_lead_activity="delete from vtiger_seactivityrel where crmid=".$id;
 209                  $adb->query($sql_delete_lead_activity);
 210  
 211          if($type != "Emails")
 212          {
 213                      $sql_insert_account_activity="insert into vtiger_seactivityrel(crmid,activityid) values (".$accountid.",".$lead_related_activity_id.")";
 214                      $adb->query($sql_insert_account_activity);
 215  
 216              $sql_insert_account_activity="insert into vtiger_cntactivityrel(contactid,activityid) values (".$contact_id.",".$lead_related_activity_id.")";
 217                      $adb->query($sql_insert_account_activity);
 218          }
 219          else
 220          {
 221               $sql_insert_account_activity="insert into vtiger_seactivityrel(crmid,activityid) values (".$contact_id.",".$lead_related_activity_id.")";                                                                                     $adb->query($sql_insert_account_activity);
 222          }
 223          }
 224      $log->debug("Exiting getRelatedActivities method ...");
 225  
 226  }
 227  
 228  /**    Function used to save the lead related products with other entities Account, Contact and Potential
 229   *    $leadid - leadid
 230   *    $relatedid - related entity id (accountid/contactid/potentialid)
 231   *    $relatedmodule - related entity module name - optional, but for contacts we have to pass Contact because we have to update contactid in vtiger_products table.
 232   */
 233  function saveLeadRelatedProducts($leadid, $relatedid, $relatedmodule = '')
 234  {
 235      global $adb, $log;
 236      $log->debug("Entering into function saveLeadRelatedProducts($leadid, $relatedid, \"$relatedmodule\")");
 237  
 238      $product_result = $adb->query("select * from vtiger_seproductsrel where crmid=$leadid");
 239      $noofproducts = $adb->num_rows($product_result);
 240      for($i = 0; $i < $noofproducts; $i++)
 241      {
 242          $productid = $adb->query_result($product_result,$i,'productid');
 243  
 244          $adb->query("insert into vtiger_seproductsrel (productid, crmid) values($productid, $relatedid)");
 245  
 246          if($relatedmodule == 'Contacts')
 247          {
 248              //update contactid in products table then only the products will be shown in contact relatedlist
 249              $adb->query("update vtiger_products set contactid=$relatedid where productid=$productid");
 250          }
 251      }
 252  
 253      $log->debug("Exit from function saveLeadRelatedProducts.");
 254  }
 255  
 256  /**     Function used to save the lead related Campaigns with Contact
 257   *      $leadid - leadid
 258   *      $relatedid - related entity id (contactid)
 259   */
 260  function saveLeadRelatedCampaigns($leadid, $relatedid)
 261  {
 262      global $adb, $log;
 263      $log->debug("Entering into function saveLeadRelatedCampaigns($leadid, $relatedid)");
 264  
 265      $campaign_result = $adb->query("select * from vtiger_campaignleadrel where leadid=$leadid");
 266      $noofcampaigns = $adb->num_rows($campaign_result);
 267      for($i = 0; $i < $noofcampaigns; $i++)
 268      {
 269          $campaignid = $adb->query_result($campaign_result,$i,'campaignid');
 270  
 271          $adb->query("insert into vtiger_campaigncontrel (campaignid, contactid) values($campaignid, $relatedid)");
 272      }
 273      $log->debug("Exit from function saveLeadRelatedCampaigns.");
 274  }
 275  
 276  
 277  
 278  /*Code integrated to avoid duplicate Account creation during ConvertLead Operation  START-- by Bharathi*/
 279  $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 = '$accountname'";
 280  $acc_res = $adb->query($acc_query);
 281  $acc_rows = $adb->num_rows($acc_res);
 282  if($acc_rows != 0)
 283          $crmid = $adb->query_result($acc_res,0,"accountid");
 284  else
 285  {
 286      $crmid = $adb->getUniqueID("vtiger_crmentity");
 287  
 288      //Saving Account - starts
 289      $sql_crmentity = "insert into vtiger_crmentity(crmid,smcreatorid,smownerid,setype,presence,createdtime,modifiedtime,deleted,description) values(".$crmid.",".$current_user_id.",".$assigned_user_id.",'Accounts',1,".$date_entered.",".$date_modified.",0,'".$row['description']."')";
 290      $adb->query($sql_crmentity);
 291      /* Modified by Minnie to fix the convertlead issue -- START*/
 292      if(isset($row["annualrevenue"]) && !empty($row["annualrevenue"])) $annualrevenue = $row["annualrevenue"];
 293      else $annualrevenue = 'null';
 294      if(isset($row["noofemployees"]) && !empty($row["noofemployees"])) $employees = $row["noofemployees"];
 295      else $employees = 'null';
 296      $sql_insert_account = "INSERT INTO vtiger_account (accountid,accountname,industry,annualrevenue,phone,fax,rating,email1,website,employees) VALUES (".$crmid.",'".addslashes($accountname)."','".$row["industry"] ."',".$annualrevenue.",'" .$row["phone"] ."','".$row["fax"] ."','" .$row["rating"] ."','" .$row["email"] ."','" .$row["website"] ."',".$employees.")";
 297      /* Modified by Minnie -- END*/
 298      $adb->query($sql_insert_account);
 299  
 300      $sql_insert_accountbillads = "INSERT INTO vtiger_accountbillads (accountaddressid,city,code,country,state,street) VALUES (".$crmid.",'".$row["city"] ."','" .$row["code"] ."','" .$row["country"] ."','".$row["state"] ."','" .$row["lane"]."')";
 301      $adb->query($sql_insert_accountbillads);
 302  
 303      $sql_insert_accountshipads = "INSERT INTO vtiger_accountshipads (accountaddressid,city,code,country,state,street) VALUES (".$crmid.",'".$row["city"] ."','" .$row["code"] ."','" .$row["country"] ."','".$row["state"] ."','" .$row["lane"]."')";
 304      $adb->query($sql_insert_accountshipads);
 305  
 306      //Getting the custom vtiger_field values from leads and inserting into Accounts if the vtiger_field is mapped - Jaguar
 307      $insert_value=$crmid;
 308      $insert_column="accountid";    
 309      $val= getInsertValues("Accounts",$insert_value);
 310      if($val[0]!="")
 311          $insert_column.=",";
 312      if($val[1]!="")
 313          $insert_value.=",";
 314  
 315      $insert_column.=$val[0];
 316      $insert_value.=$val[1];
 317      $sql_insert_accountcustomfield = "INSERT INTO vtiger_accountscf (".$insert_column.") VALUES (".$insert_value.")";
 318      $adb->query($sql_insert_accountcustomfield);
 319      //Saving Account - ends
 320  }
 321  /*Code integrated to avoid duplicate Account creation during ConvertLead Operation  END-- by Bharathi*/
 322  
 323  $account_id=$crmid;
 324  getRelatedNotesAttachments($id,$crmid); //To Convert Related Notes & Attachments -Jaguar
 325  
 326  //Retrieve the lead related products and relate them with this new account
 327  saveLeadRelatedProducts($id, $crmid);
 328  
 329  //Up to this, Account related data save finshed
 330  
 331  
 332  
 333  $date_entered = $adb->formatDate(date('YmdHis'));
 334  $date_modified = $adb->formatDate(date('YmdHis'));
 335  
 336  //Saving Contact - starts
 337  $crmcontactid = $adb->getUniqueID("vtiger_crmentity");
 338  $sql_crmentity1 = "insert into vtiger_crmentity(crmid,smcreatorid,smownerid,setype,presence,deleted,description,createdtime,modifiedtime) values(".$crmcontactid.",".$current_user_id.",".$assigned_user_id.",'Contacts',0,0,'".$row['description']."',".$date_entered.",".$date_modified.")";
 339  
 340  $adb->query($sql_crmentity1);
 341  
 342  $contact_id = $crmcontactid;
 343  $log->debug("contact id is ".$contact_id);
 344  
 345  $sql_insert_contact = "INSERT INTO vtiger_contactdetails (contactid,accountid,salutation,firstname,lastname,email,phone,mobile,title,fax,yahooid) VALUES (".$contact_id.",".$crmid.",'".$row["salutationtype"] ."','" .$row["firstname"] ."','" .$row["lastname"] ."','" .$row["email"] ."','" .$row["phone"]. "','" .$row["mobile"] ."','" .$row["designation"] ."','".$row["fax"] ."','".$row['yahooid']."')";
 346  
 347  $adb->query($sql_insert_contact);
 348  
 349  
 350  $sql_insert_contactsubdetails = "INSERT INTO vtiger_contactsubdetails (contactsubscriptionid,homephone,otherphone,leadsource) VALUES (".$contact_id.",'','','".$row['leadsource']."')";
 351  
 352  $adb->query($sql_insert_contactsubdetails);
 353  
 354  $sql_insert_contactaddress = "INSERT INTO vtiger_contactaddress (contactaddressid,mailingcity,mailingstreet,mailingstate,mailingcountry,mailingzip) VALUES (".$contact_id.",'".$row["city"] ."','" .$row["lane"] ."','".$row['state']."','" .$row["country"] ."','".$row['code']."')";
 355  
 356  $adb->query($sql_insert_contactaddress);
 357  
 358  
 359  //Getting the customfield values from leads and inserting into the respected ContactCustomfield to which it is mapped - Jaguar
 360  $insert_column="contactid";
 361  $insert_value=$contact_id;
 362  $val= getInsertValues("Contacts",$contact_id);
 363  
 364  if($val[0]!="")
 365      $insert_column.=",";    
 366  if($val[1]!="")
 367      $insert_value.=",";    
 368  
 369  $insert_column.=$val[0];
 370  $insert_value.=$val[1];
 371  $sql_insert_contactcustomfield = "INSERT INTO vtiger_contactscf (".$insert_column.") VALUES (".$insert_value.")";
 372  
 373  $adb->query($sql_insert_contactcustomfield);
 374  //Saving Contact - ends
 375  
 376  getRelatedActivities($account_id,$contact_id); //To convert relates Activites  and Email -Jaguar
 377  
 378  //Retrieve the lead related products and relate them with this new contact
 379  saveLeadRelatedProducts($id, $contact_id, "Contacts");
 380  
 381  //Retrieve the lead related Campaigns and relate them with this new contact --Minnie
 382  saveLeadRelatedCampaigns($id, $contact_id);
 383  
 384  //Up to this, Contact related data save finshed
 385  
 386  
 387  
 388  
 389  
 390  //Saving Potential - starts
 391  if(! isset($createpotential) || ! $createpotential == "on")
 392  {
 393      $log->info("createpotential is not set");
 394  
 395      $date_entered = $adb->formatDate(date('YmdHis'));
 396      $date_modified = $adb->formatDate(date('YmdHis'));
 397    
 398  
 399      $oppid = $adb->getUniqueID("vtiger_crmentity");
 400      $sql_crmentity = "insert into vtiger_crmentity(crmid,smcreatorid,smownerid,setype,presence,deleted,createdtime,modifiedtime,description) values(".$oppid.",".$current_user_id.",".$assigned_user_id.",'Potentials',0,0,".$date_entered.",".$date_modified.",'".$row['description']."')";
 401    
 402      $adb->query($sql_crmentity);
 403  
 404  
 405      if(!isset($potential_amount) || $potential_amount == null)
 406      {
 407          $potential_amount=0;
 408          }
 409  
 410      $sql_insert_opp = "INSERT INTO vtiger_potential (potentialid,accountid,potentialname,leadsource,closingdate,sales_stage,amount) VALUES (".$oppid.",".$crmid .",'".addslashes($potential_name)."','".$row['leadsource']."','".$close_date."','".$potential_sales_stage."',".$potential_amount.")";
 411  
 412      $adb->query($sql_insert_opp);
 413  
 414      //Getting the customfield values from leads and inserting into the respected PotentialCustomfield to which it is mapped - Jaguar
 415      $insert_column="potentialid";
 416      $insert_value=$oppid;
 417      $val= getInsertValues("Potentials",$oppid);
 418      if($val[0]!="")
 419          $insert_column.=",";        
 420      if($val[1]!="")
 421          $insert_value.=",";        
 422      
 423      $insert_column.=$val[0];
 424      $insert_value.=$val[1];
 425  
 426      $sql_insert_potentialcustomfield = "INSERT INTO vtiger_potentialscf (".$insert_column.") VALUES (".$insert_value.")";
 427  
 428      $adb->query($sql_insert_potentialcustomfield);
 429  
 430      $sql_insert2contpotentialrel ="insert into vtiger_contpotentialrel values(".$contact_id.",".$oppid .")";
 431          $adb->query($sql_insert2contpotentialrel);
 432  
 433      //Retrieve the lead related products and relate them with this new potential
 434      saveLeadRelatedProducts($id, $oppid);
 435  
 436  }
 437  //Saving Potential - ends
 438  //Up to this, Potential related data save finshed
 439  
 440  
 441  //Deleting from the vtiger_tracker
 442  $sql_delete_tracker= "DELETE from vtiger_tracker where item_id='" .$id ."'";
 443  $adb->query($sql_delete_tracker);
 444  $category = getParentTab();
 445  //Updating the deleted status
 446  $sql_update_converted = "UPDATE vtiger_leaddetails SET converted = 1 where leadid='" .$id ."'";
 447  $adb->query($sql_update_converted); 
 448  //updating the campaign-lead relation --Minnie
 449  $sql_update_campleadrel = "delete from vtiger_campaignleadrel where leadid=".$id;
 450  $adb->query($sql_update_campleadrel);
 451  header("Location: index.php?action=DetailView&module=Accounts&record=$crmid&parenttab=$category");
 452  
 453  ?>


Généré le : Sun Feb 25 10:22:19 2007 par Balluche grâce à PHPXref 0.7