//Convert lead Save handle logic
//Added by Yesha 05-03-2019
public function covert_lead_save_handle($record_id, $convert_lead_args)
{
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
$convert_lead_args = $this->replace_null_to_blank($convert_lead_args);
if(is_array($convert_lead_args)){
//$GLOBALS['log']->fatal("YES ARRAY");
}else{
//$GLOBALS['log']->fatal("NOT ARRAY");
$convert_lead_args = (array)json_decode($convert_lead_args);
}
// $GLOBALS['log']->fatal("IN UTIL FILE : ".print_r($convert_lead_args,true));
require_once('modules/Campaigns/utils.php');
require_once("include/formbase.php");
require_once("custom/modules/Leads/views/view.convertlead.php");
$method_loadDefs = new ReflectionMethod('ViewConvertLead', 'loadDefs');
$method_loadDefs->setAccessible(true);
$loaddef = $method_loadDefs->invoke(new ViewConvertLead);
//$GLOBALS['log']->fatal("record_id : ".$record_id);
$lead = false;
if (!empty($record_id))
{
$lead = new Lead();
$lead_data = $lead->retrieve($record_id);
$check_exist_id = $lead_data->id;
}
if (!empty($check_exist_id))
{
$lead = new Lead();
$lead_data = $lead->retrieve($record_id);
// $GLOBALS['log']->fatal("get lead data: ".print_r($lead_data,true));
// file_put_contents('print_log.txt', print_r($lead_data, true));
// die;
global $beanList;
// $class_ViewConvertLead->loadDefs();
$beans = array();
$selectedBeans = array();
$selects = array();
$response_arr = array();
// $GLOBALS['log']->fatal("hiiii");
$request_arr = array();
if($convert_lead_args["Contacts"]["first_name"] != ''){
$request_arr['first_name'] = $convert_lead_args["Contacts"]["first_name"];
}
if($convert_lead_args["Contacts"]["last_name"] != ''){
$request_arr['last_name'] = $convert_lead_args["Contacts"]["last_name"];
}
//$GLOBALS['log']->fatal("hiiii passsss");
$request_arr = (object)$request_arr;
//$GLOBALS['log']->fatal("reqqq : ".print_r($request_arr,true));
// die;
// Make sure the contact object is availible for relationships.
$beans['Contacts'] = new Contact();
//$GLOBALS['log']->fatal("IN SAVE : ".$_REQUEST['selectedContact']);
// Contacts
require_once('custom/modules/Contacts/ContactFormBase.php');
$contactForm = new ContactFormBase();
if(!empty($request_arr)){
//$GLOBALS['log']->fatal("IN REQ IF ");
$duplicateContacts = $contactForm->checkForDuplicatesApi('Contacts',$request_arr);
}else{
///$GLOBALS['log']->fatal("IN REQ ELSE ");
$duplicateContacts = $contactForm->checkForDuplicatesApi('Contacts',$lead_data);
}
//$GLOBALS['log']->fatal("Check duplicate entry person : ".print_r($duplicateContacts,true));
// if (isset($duplicateContacts))
if (isset($duplicateContacts))
{
//echo $contactForm->buildTableForm($duplicateContacts, 'Contacts');
// return;
//$GLOBALS['log']->fatal("IN IF 1 - yes found duplicate perosn : ".$convert_lead_args["Contacts"]["ContinueContacts"]);
$beans['Contacts']->retrieve($duplicateContacts[0]['id']);
if($convert_lead_args["Contacts"]["ContinueContacts"] == "1"){
$is_continue_contact = 1;
//$GLOBALS['log']->fatal("Yes create duplicate contact.");
}else{
if (!empty($beans['Contacts']->id))
{
$beans['Contacts']->new_with_id = false;
unset($convert_lead_args["Contacts"]);
$response_arr['status'] = 2;
$response_arr['message'] = preg_replace('/\
/'," ", nl2br("The contact record you are about to create might be a duplicate of an contact record that already exists. Contacts records containing similar names are listed below.Click Create Contacts to continue creating this new contact, or select an existing contact listed below."));
$response_arr['contact_record'] = $duplicateContacts;
$is_continue_contact = 0;
}
}
// $response_arr['check'] = $is_continue_contact;
// if($is_continue_contact == 0){
// break;
// }else{
// continue;
// }
}else{
$is_continue_contact = 1;
// continue;
}
if($is_continue_contact == 1){
//$GLOBALS['log']->fatal("if Yes create duplicate account then enter.");
$is_continue_contact = 1;
// Accounts
require_once('custom/modules/Accounts/AccountFormBase.php');
$response_arr_account = array();
$response_arr_account = array();
if($convert_lead_args["Accounts"]["name"] != ''){
$response_arr_account['name'] = $convert_lead_args["Accounts"]["name"];
}
$response_arr_account = (object)$response_arr_account;
//$GLOBALS['log']->fatal("response_arr_account : ".print_r($response_arr_account,true));
$accountForm = new AccountFormBase();
if(!empty($response_arr_account)){
$duplicateAccounts = $accountForm->checkForDuplicatesApi('Accounts',$response_arr_account);
}else{
$duplicateAccounts = $accountForm->checkForDuplicatesApi('Accounts',$lead_data);
}
//$GLOBALS['log']->fatal("Check duplicate entry account: ".print_r($duplicateAccounts,true));
if (isset($duplicateAccounts))
{
if($convert_lead_args["Accounts"]["ContinueAccounts"] == "1"){
$is_continue_account = 1;
//$GLOBALS['log']->fatal("Yes create duplicate account.");
}else{
//$GLOBALS['log']->fatal("yes found duplicate account - selected that account.");
// $_REQUEST['account_id'] = $_REQUEST['selectedAccount'];
unset($convert_lead_args["Accounts"]);
$response_arr['status'] = 2;
$response_arr['message'] = preg_replace('/\
/'," ", nl2br("The Company record you are about to create might be a duplicate of an Company record that already exists.\nCompany records containing similar names are listed below.\nClick Create Company to continue creating this new company, or select an existing company listed below."));
$response_arr['account_record'] = $duplicateAccounts;
$is_continue_account = 0;
}
}else{
$is_continue_account = 1;
}
if($is_continue_account == 1){
// $is_continue_account = 1;
//$GLOBALS['log']->fatal("continue contact : ".$is_continue_contact);
//$GLOBALS['log']->fatal("continue account : ".$is_continue_account);
if($is_continue_contact == 1 || $is_continue_account == 1){
foreach ($loaddef as $module => $vdef)
{
//$GLOBALS['log']->fatal("IN IF 6 ::::::::::::: Main Loop : ".$module);
//Create a new record if "create" was selected
if (!empty($convert_lead_args[$module]["convert_create_$module"]) && $convert_lead_args[$module]["convert_create_$module"] != "false"){
//$GLOBALS['log']->fatal("Creating New ".$module);
//Save the new record
$bean = $beanList[$module];
if (empty($beans[$module]))
$beans[$module] = new $bean();
$method_populateNewBean = new ReflectionMethod('ViewConvertLead', 'populateNewBean');
$method_populateNewBean->setAccessible(true);
//pass value to bean
// if($module == 'Contacts'){
if(!empty($request_arr)){
if($request_arr->first_name != ''){
$beans['Contacts']->first_name = $convert_lead_args['Contacts']['first_name'];
}else{
$beans['Contacts']->first_name = $lead_data->first_name;
}
$beans['Contacts']->last_name = $convert_lead_args['Contacts']['last_name'];
}else{
$beans['Contacts']->first_name = $lead_data->first_name;
$beans['Contacts']->last_name = $lead_data->last_name;
}
$beans['Contacts']->primary_address_street = $convert_lead_args['Contacts']['primary_address_street'];
$beans['Contacts']->primary_address_state = $convert_lead_args['Contacts']['primary_address_state'];
$beans['Contacts']->primary_address_postalcode = $convert_lead_args['Contacts']['primary_address_postalcode'];
$beans['Contacts']->primary_address_country = $convert_lead_args['Contacts']['primary_address_country'];
$beans['Contacts']->title = $convert_lead_args['Contacts']['title'];
$beans['Contacts']->department = $convert_lead_args['Contacts']['department'];
$beans['Contacts']->phone_work = $convert_lead_args['Contacts']['phone_work'];
$beans['Contacts']->phone_mobile = $convert_lead_args['Contacts']['phone_mobile'];
$beans['Contacts']->phone_other = $convert_lead_args['Contacts']['phone_other'];
$beans['Contacts']->phone_fax = $convert_lead_args['Contacts']['phone_fax'];
$beans['Contacts']->lead_source = $convert_lead_args['Contacts']['lead_source'];
$beans['Contacts']->description = $convert_lead_args['Contacts']['description'];
$beans['Contacts']->account_name = $convert_lead_args['Accounts']['name'];
$beans['Contacts']->alt_address_postalcode = $convert_lead_args['Contacts']['alt_address_postalcode'];
$beans['Contacts']->email_verification_check_c = $convert_lead_args['Contacts']['email_verification_check_c'];
$beans['Contacts']->alt_address_country = $convert_lead_args['Contacts']['alt_address_country'];
$beans['Contacts']->alt_address_state = $convert_lead_args['Contacts']['alt_address_state'];
$beans['Contacts']->email1 = $convert_lead_args['Contacts']['email1'];
// file_put_contents('print_log3.txt', print_r($beans['Contacts'], true));
$populateNewBean = $method_populateNewBean->invoke(new ViewConvertLead(),$module, $beans[$module], $beans['Contacts'], $lead);
//$GLOBALS['log']->fatal("pass");
// }
// $this->populateNewBean($module, $beans[$module], $beans['Contacts'], $lead);
// when creating a new contact, create the id for linking with other modules
// and do not populate it with lead's old account_id
if ($module == 'Contacts')
{
$beans[$module]->id = create_guid();
$beans[$module]->new_with_id = true;
$beans[$module]->account_id = '';
}
$response_arr[$module]['success'] = 1;
$response_arr[$module]['record_status'] = "new";
$response_arr[$module]['message'] = "New ".$module." has been created";
}
//If an existing bean was selected, relate it to the contact
else if (!empty($vdef['ConvertLead']['select']))
{
//$GLOBALS['log']->fatal("Choosing existing ".$module);
//Save the new record
$select = $vdef['ConvertLead']['select'];
//$GLOBALS['log']->fatal("IN IF 6 -2.1 -> select : ".$select);
$fieldDef = $beans['Contacts']->field_defs[$select];
//$GLOBALS['log']->fatal("IN IF 6 -2.2 -> fieldDef : ".print_r($fieldDef,true));
//$GLOBALS['log']->fatal("IN IF 6 -2.3 -> id name : ".$convert_lead_args[$module][$fieldDef['id_name']]);
if (!empty($fieldDef['id_name']) && !empty($convert_lead_args[$module][$fieldDef['id_name']]))
{
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//$GLOBALS['log']->fatal("IN IF 6 -2 - in if - If found id name");
$beans['Contacts']->$fieldDef['id_name'] = $convert_lead_args[$module][$fieldDef['id_name']];
$selects[$module] = $convert_lead_args[$module][$fieldDef['id_name']];
$passed_record_id = $convert_lead_args[$module][$fieldDef['id_name']];
//get bean
if($passed_record_id != ''){
$report_to_name = BeanFactory::getBean($module, $passed_record_id);
if($module == 'Contacts'){
//$GLOBALS['log']->fatal("IN IF 6 -2.4 - lastname : ".$report_to_name->last_name);
$report_to_name = $report_to_name->last_name;
}else{
//$GLOBALS['log']->fatal("IN IF 6 -2.4 - act name : ".$report_to_name->name);
$report_to_name = $report_to_name->name;
}
}
if (!empty($report_to_name))
{
$beans['Contacts']->$select = $report_to_name;
}
// Bug 39268 - Add the existing beans to a list of beans we'll potentially add the lead's activities to
$bean = loadBean($module);
$bean->retrieve($convert_lead_args[$module][$fieldDef['id_name']]);
$selectedBeans[$module] = $bean;
// If we selected the Contact, just overwrite the $beans['Contacts']
if ($module == 'Contacts')
{
$beans[$module] = $bean;
}
}
//link selected account to lead if it exists
//$GLOBALS['log']->fatal("IN IF 7 - link selected account to lead if it exists");
$lead->account_id = $convert_lead_args['Accounts']['account_id'];
$response_arr[$module]['success'] = 1;
$response_arr[$module]['record_status'] = "existing";
$response_arr[$module]['message'] = "Existing ".$module." has been updated";
}
// file_put_contents('print_log_fromapi_oppor.txt', print_r($beans, true));
}//end foreach
// $this->handleActivities($lead, $beans);
$method_handleActivities = new ReflectionMethod('ViewConvertLead', 'handleActivities');
$method_handleActivities->setAccessible(true);
$handleActivities1 = $method_handleActivities->invoke(new ViewConvertLead(),$lead, $beans);
// Bug 39268 - Add the lead's activities to the selected beans
// $this->handleActivities($lead, $selectedBeans);
$handleActivities2 = $method_handleActivities->invoke(new ViewConvertLead(),$lead, $selectedBeans);
// link account to contact, if we picked an existing contact and created a new account
if (!empty($beans['Accounts']->id) && !empty($beans['Contacts']->account_id)
&& $beans['Accounts']->id != $beans['Contacts']->account_id)
{
//$GLOBALS['log']->fatal("IN IF 8");
$beans['Contacts']->account_id = $beans['Accounts']->id;
}
// Saving beans with priorities.
// Contacts and Accounts should be saved before lead activities to create correct relations
$saveBeanPriority = array('Contacts', 'Accounts');
$tempBeans = array();
foreach ($saveBeanPriority as $name)
{
//$GLOBALS['log']->fatal("save loop :".$name);
if (isset($beans[$name]))
{
$tempBeans[$name] = $beans[$name];
if($name == 'Accounts'){
// $tempBeans['Accounts']->name = $convert_lead_args['Accounts']['account_name'];
$tempBeans['Accounts']->name = $convert_lead_args['Accounts']['name'];
$tempBeans['Accounts']->phone_office = $convert_lead_args['Accounts']['phone_office'];
$tempBeans['Accounts']->description = $convert_lead_args['Accounts']['description'];
$tempBeans['Accounts']->website = $convert_lead_args['Accounts']['website'];
}
}
}
// file_put_contents('print_logaccount12.txt', print_r($tempBeans['Accounts'], true));
$beans = array_merge($tempBeans, $beans);
unset($tempBeans);
file_put_contents('print_log_mainbean.txt', print_r($beans, true));
foreach ($beans as $bean)
{
//$GLOBALS['log']->fatal("IN LOOP - YESHA");
if (!empty($lead))
{
if(empty($bean->team_id))
{
$bean->team_id = $lead->team_id;
$bean->team_set_id = $lead->team_set_id;
}
if (empty($bean->assigned_user_id))
{
$bean->assigned_user_id = $lead->assigned_user_id;
}
// $leadsRel = $this->findRelationship($bean, $lead);
$method_findRelationship = new ReflectionMethod('ViewConvertLead', 'findRelationship');
$method_findRelationship->setAccessible(true);
$leadsRel = $method_findRelationship->invoke(new ViewConvertLead(),$bean, $lead);
// $GLOBALS['log']->fatal("IN LOOP - lead rel : ".print_r($leadsRel,true));
if (!empty($leadsRel))
{
$bean->load_relationship($leadsRel);
$relObject = $bean->$leadsRel->getRelationshipObject();
// $GLOBALS['log']->fatal("IN LOOP - rel object: ".print_r($relObject,true));
if ($relObject->relationship_type == "one-to-many" && $bean->$leadsRel->_get_bean_position())
{
//$GLOBALS['log']->fatal("IN if".$relObject->relationship_type);
$id_field = $relObject->rhs_key;
$lead->$id_field = $bean->id;
}
else
{
//$GLOBALS['log']->fatal("IN else".$lead->id);
$bean->$leadsRel->add($lead->id);
}
}
}
//$GLOBALS['log']->fatal("special : ".$bean->object_name);
//Special case code for opportunities->Accounts
//if ($bean->object_name == "Opportunity" && empty($bean->account_id))
if ($bean->object_name == "Opportunity")
{
//$GLOBALS['log']->fatal("special INNN : ".$bean->object_name);
if (isset($beans['Accounts']))
{
//$GLOBALS['log']->fatal("********** 1 YESHA : ".$convert_lead_args['Opportunities']['date_closed']);
$bean->account_id = $beans['Accounts']->id;
$bean->account_name = $beans['Accounts']->name;
}
else if (!empty($convert_lead_args['Accounts']['account_id']))
{
$bean->account_id = $convert_lead_args['Accounts']['account_id'];
$bean->name = $convert_lead_args['Opportunities']['name'];
}else{
//$GLOBALS['log']->fatal("IN ELSEEEEEEEE");
$bean->name = $convert_lead_args['Opportunities']['name'];
}
$bean->name = $convert_lead_args['Opportunities']['name'];
$bean->next_followup_date_c = $convert_lead_args['Opportunities']['next_followup_date_c'];
$bean->date_closed = $convert_lead_args['Opportunities']['date_closed'];
$bean->sales_stage = $convert_lead_args['Opportunities']['sales_stage'];
$bean->enjay_product_c = $convert_lead_args['Opportunities']['enjay_product_c'];
$bean->description = $convert_lead_args['Opportunities']['description'];
$bean->amount = $convert_lead_args['Opportunities']['amount'];
}
//create meetings-users relationship
if ($bean->object_name == "Meeting")
{
// $bean = $this->setMeetingsUsersRelationship($bean);
$method_setMeetingsUsersRelationship = new ReflectionMethod('ViewConvertLead', 'setMeetingsUsersRelationship');
$method_setMeetingsUsersRelationship->setAccessible(true);
$bean = $method_setMeetingsUsersRelationship->invoke(new ViewConvertLead(),$bean);
}
// $this->copyAddressFields($bean, $beans['Contacts']);
$method_copyAddressFields = new ReflectionMethod('ViewConvertLead', 'copyAddressFields');
$method_copyAddressFields->setAccessible(true);
$method_copyAddressFields->invoke(new ViewConvertLead(),$bean, $beans['Contacts']);
$bean->save();
//if campaign id exists then there should be an entry in campaign_log table for the newly created contact: bug 44522
if (isset($lead->campaign_id) && $lead->campaign_id != null && $bean->object_name == "Contact")
{
campaign_log_lead_or_contact_entry($lead->campaign_id, $lead, $beans['Contacts'], 'contact');
}
}
if (!empty($lead))
{ //Mark the original Lead converted
$lead->status = "Converted";
$lead->converted = '1';
$lead->in_workflow = true;
$lead->save();
}
//Call logic hook
// $my_hook = array(2, 'Lead Multi Product', 'custom/modules/Configurator/Multi_Product/Lead/lead_logic_hook.php', 'lead_multi_product_class', 'opportunity_save_method');
// check_logic_hook_file("Leads", "after_save", $my_hook);
// die;
// file_put_contents('print_log_contactnew.txt', print_r($beans['Contacts'], true));
//
// file_put_contents('print_log_oppnew.txt', print_r($beans['Opportunities'], true));
//$GLOBALS['log']->fatal("create New Person : ".print_r($beans['Contacts']->id,true));
//$GLOBALS['log']->fatal("RESPONSE : ".print_r($response_arr,true));
//$GLOBALS['log']->fatal("RESPONSE COUNT : ".count($response_arr));
$response_count = count($response_arr);
$dis_count_new = 0;
$dis_count_existing = 0;
foreach ($response_arr as $key => $value) {
$total_res_count += $value['success'];
//$GLOBALS['log']->fatal("val : ".$total_res_count);
if($value['record_status'] == 'new'){
if($dis_count_new == 0){
$msg = "New ".$key;
}else if($dis_count_new == 1){
$msg = $msg." and ".$key;
}else{
}
$msg = $msg." has been created";
$dis_count_new++;
}else if($value['record_status'] == 'existing'){
$exi_msg = " has been updated";
if($dis_count_existing == 0){
$msge = "Existing ".$key;
}else if($dis_count_existing == 1){
$exi_msg = '';
$msge = $msge." and ".$key;
}else{
}
$msge = $msge.$exi_msg;
$dis_count_existing++;
}
}
//$GLOBALS['log']->fatal("msgg : ".$msg.",".$msge);
unset($response_arr);
$response_arr['success'] = $total_res_count/$response_count;
$su_para = $total_res_count/$response_count;
if($su_para == 1){
$response_arr['message'] = "Successfully Converted";
}else{
$response_arr['message'] = "Something Went Wrong, Please try again later";
}
}
}
}
}else{
$response_arr['status'] = 0;
$response_arr['message'] = preg_replace('/\
/'," ", nl2br("Error retrieving record. This record may be deleted or you may not be authorized to view it."));
}
return $this->replace_null_to_blank($response_arr);
}
public function sms_gateway_handle($request_data)
{
$GLOBALS['log']->fatal('IN SMS GATEWAY API & output is'.print_r($request_data,true));
$module_name = $request_data['module_name'];
$field_name = $request_data['field_name'];
$record_id = $request_data['record_id'];
$mobile = $request_data['mobile'];
if(strlen($mobile) > 10){
$mobile = substr($mobile, -10);
}
$GLOBALS['log']->fatal('Number : '.$mobile);
$message = $request_data['message'];
$message = rawurlencode($message);
$template = $request_data['template_id'];
require_once("custom/modules/Configurator/enjaysms_utils.php");
$enjaysms = new enjaysms_utils();
$send_url = $enjaysms->createsmsURL($mobile, $message);
$output = file_get_contents($send_url);
if($output!=false)
{
$GLOBALS['log']->fatal('IN SMS GATEWAY API & output is'.$output);
$m = json_decode($output);
$GLOBALS['log']->fatal('IN SMS GATEWAY API & output is'.print_r($m,true));
if(isset($m->ErrorCode) && $m->ErrorCode == 13){
$return = array(result => "False", message => $m->ErrorMessage, record_id => $record_id);
}else{
$message_pass = $m->MessageData[0]->MessageParts[0]->Text;
$GLOBALS['log']->fatal('mesg1 : '.print_r($message_pass,true));
// $ms = $message_pass."\n\n Sent to number - ".$mobile;
$ms = $message_pass;
$GLOBALS['log']->fatal('mesg2 : '.$ms);
$enjaysms->createSMS($template, $ms, 'Held', 'Outbound', $module_name, $record_id,$mobile);
$return = array(result => "True", message => "SMS Sent Successfully.",record_id => $record_id);
}
}
else
{
$return = array(result => "False", message => "SMS Not sent",record_id => $record_id);
}
// return $this->replace_null_to_blank($GLOBALS['sugar_config']);
return $this->replace_null_to_blank($return);
}