Commit fa23ef18 authored by Andrey Shevchuk's avatar Andrey Shevchuk

removed converted leads from search results

parent 5d2a4e79
......@@ -3,7 +3,6 @@ package Integration::CRM::SalesForce;
use strict;
use warnings;
use WWW::Salesforce;
use Data::Dumper;
......@@ -449,11 +448,11 @@ my %object_prefixes = (
CUSTOM_ENTITY_DATA => 'a00'
);
our $DEBUG = 1;
our $DEBUG = 0;
sub new {
my( $class, $args ) = @_;
my $self = bless( { }, $class );
my ( $class, $args ) = @_;
my $self = bless( {}, $class );
$DEBUG = $args->{debug} if exists $args->{debug};
......@@ -462,7 +461,10 @@ sub new {
$self->{id} = $args->{id} || 0;
die unless $args->{user}->{login} && $args->{user}->{password} && $args->{user}->{token};
die
unless $args->{user}->{login}
&& $args->{user}->{password}
&& $args->{user}->{token};
die unless $self->_connect;
return $self;
......@@ -471,7 +473,7 @@ sub new {
sub detect_type_by_id {
my ( $self, $id ) = @_;
my ( $object_prefix ) = $id =~ m/^(.*?)w.*?$/isg;
my ($object_prefix) = $id =~ m/^(.*?)w.*?$/isg;
foreach my $object_name ( keys %object_prefixes ) {
return $object_name if $object_prefixes{$object_name} eq $object_prefix;
......@@ -495,59 +497,75 @@ sub get_fields {
return \@fields;
}
sub _connect {
my $self = shift;
printf('login: %s pass: %s token: %s\n', $self->{config}->{user}->{login},
$self->{config}->{user}->{password}, $self->{config}->{user}->{token}) if $DEBUG;
printf(
'login: %s pass: %s token: %s\n',
$self->{config}->{user}->{login},
$self->{config}->{user}->{password},
$self->{config}->{user}->{token}
) if $DEBUG;
my $sforce = eval {
WWW::Salesforce->login(
username => $self->{config}->{user}->{login},
password => $self->{config}->{user}->{password} . $self->{config}->{user}->{token}
password => $self->{config}->{user}->{password}
. $self->{config}->{user}->{token}
);
};
$self->{connection} = $sforce unless $@;
$self->{server} = $self->{connection}->{sf_serverurl} unless $@;
( $self->{instance} ) = $self->{server} =~ m/https\:\/\/(.*?)\.salesforce\.com\// if $self->{server};
$self->{server} = sprintf("https://%s.salesforce.com", $self->{instance}) if $self->{instance};
( $self->{instance} ) =
$self->{server} =~ m/https\:\/\/(.*?)\.salesforce\.com\//
if $self->{server};
$self->{server} = sprintf( "https://%s.salesforce.com", $self->{instance} )
if $self->{instance};
return $self unless $@;
return undef;
}
sub get_url {
my( $self, $id ) = @_;
my ( $self, $id ) = @_;
return sprintf("%s/%s", $self->{server}, $id);
return sprintf( "%s/%s", $self->{server}, $id );
}
sub fix_object {
my( $self, $object ) = @_;
my ( $self, $object ) = @_;
return
if exists $object->{IsDeleted} and delete $object->{IsDeleted} eq 'true';
return
if exists $object->{IsConverted}
and delete $object->{IsConverted} eq 'true';
$object->{contact_source} = $self->{id};
if (exists $object->{Id}) {
if ( exists $object->{Id} ) {
$object->{contact_source_type} = "Salesforce";
if (ref $object->{Id} eq "ARRAY") {
$object->{URL} = $self->get_url($object->{Id}->[0]);
if ( ref $object->{Id} eq "ARRAY" ) {
$object->{URL} = $self->get_url( $object->{Id}->[0] );
$object->{Id} = [ $object->{Id}->[0] ];
} else {
$object->{URL} = $self->get_url($object->{Id});
}
else {
$object->{URL} = $self->get_url( $object->{Id} );
$object->{Id} = [ $object->{Id} ];
}
}
$object = $self->bless_hash($object);
$object = $self->fix_hash($object);
return $object;
}
sub upsert {
my( $self, $args ) = @_;
my ( $self, $args ) = @_;
my $want_object = delete $args->{want_object};
......@@ -557,51 +575,72 @@ sub upsert {
$args->{record}
);
for my $result ($response->valueof('//upsertResponse/result')) {
my $object = $self->query({object => $args->{object}, query => {criteria => sprintf ( "Id = '%s'", $result->{id} ) } }) if exists $result->{id};
return $object->[0] if ref $object eq "ARRAY" && scalar @{ $object };
for my $result ( $response->valueof('//upsertResponse/result') ) {
my $object = $self->query(
{
object => $args->{object},
query => { criteria => sprintf( "Id = '%s'", $result->{id} ) }
}
) if exists $result->{id};
return $object->[0] if ref $object eq "ARRAY" && scalar @{$object};
}
}
sub find {
my( $self, $args ) = @_;
my ( $self, $args ) = @_;
my @result;
my @returns;
# ALL FIELDS NAME FIELDS EMAIL FIELDS PHONE FIELDS SIDEBAR FIELDS
my $query = sprintf("find {%s} in %s fields returning ", $args->{query}->{what}, $args->{query}->{where} || "ALL");
my $query = sprintf(
"find {%s} in %s fields returning ",
$args->{query}->{what},
$args->{query}->{where} || "ALL"
);
foreach my $object ( keys %{ $args->{returns} } ) {
push @returns, sprintf("%s(%s)", $object, join ( ", ", @{ $args->{returns}->{$object} } ) );
push @returns,
sprintf( "%s(%s)",
$object, join( ", ", @{ $args->{returns}->{$object} } ) );
}
$query .= join ", ", @returns;
my $response = $self->{connection}->search( 'searchString' => $query );
for my $rec_result ($response->valueof('//searchResponse/result/searchRecords')) {
push @result, $self->fix_object($rec_result->{record});
for my $rec_result (
$response->valueof('//searchResponse/result/searchRecords') )
{
push @result, $self->fix_object( $rec_result->{record} );
}
return \@result;
return [ grep { defined $_ } @result ];
}
sub query {
my( $self, $args ) = @_;
my ( $self, $args ) = @_;
my @result;
my @returns;
my $query = sprintf("SELECT %s FROM %s WHERE %s",
join(" ,", @{ exists $args->{query}->{fields} ? $args->{query}->{fields} : $self->get_fields($args->{object}) } ),
my $query = sprintf(
"SELECT %s FROM %s WHERE %s",
join(
" ,",
@{
exists $args->{query}->{fields}
? $args->{query}->{fields}
: $self->get_fields( $args->{object} )
}
),
$args->{object},
$args->{query}->{criteria}
);
my $response = $self->{connection}->query( query => $query );
for my $rec_result ($response->valueof('//queryResponse/result/records')) {
for my $rec_result ( $response->valueof('//queryResponse/result/records') )
{
push @result, $self->fix_object($rec_result);
}
......@@ -609,19 +648,29 @@ sub query {
}
sub get_records {
my( $self, $args ) = @_;
my ( $self, $args ) = @_;
my @result;
my @returns;
my $query = sprintf("SELECT %s FROM %s",
join(" ,", @{ exists $args->{query}->{fields} ? $args->{query}->{fields} : $self->get_fields($args->{object}) } ),
my $query = sprintf(
"SELECT %s FROM %s",
join(
" ,",
@{
exists $args->{query}->{fields}
? $args->{query}->{fields}
: $self->get_fields( $args->{object} )
}
),
$args->{object}
);
my $response = $self->{connection}->query( query => $query, limit => $args->{limit} || 1000 );
my $response = $self->{connection}
->query( query => $query, limit => $args->{limit} || 1000 );
for my $rec_result ($response->valueof('//queryResponse/result/records')) {
for my $rec_result ( $response->valueof('//queryResponse/result/records') )
{
push @result, $self->fix_object($rec_result);
}
......@@ -629,7 +678,7 @@ sub get_records {
}
sub _get_data {
my( $self ) = @_;
my ($self) = @_;
my $response = $self->{connection}->get_session_header;
return {
session => $response,
......@@ -638,7 +687,7 @@ sub _get_data {
}
sub get_session_id {
my( $self ) = @_;
my ($self) = @_;
my $session_value = eval {
my $session = $self->{connection}->get_session_header->value;
......@@ -649,60 +698,82 @@ sub get_session_id {
}
sub find_phone {
my( $self, $phone ) = @_;
my ( $self, $phone ) = @_;
return $self->find({
return $self->find(
{
query => {
what => $phone,
where => "phone"
},
returns => {
contact => ["id", "phone", "firstname", "lastname", "department", "email"],
lead => ["id", "phone", "firstname", "lastname", "company", "email"]
contact => [
"id", "phone", "firstname", "lastname",
"department", "email"
],
lead => [
"id", "phone", "firstname", "lastname",
"company", "email", "isconverted", "isdeleted"
]
}
}
});
);
}
sub query_by_fields {
my( $self, $query, $fields ) = @_;
my ( $self, $query, $fields ) = @_;
my $lead_fields = {
"fullname" => qq{FirstName LIKE '%$query%' OR LastName LIKE '%$query%'},
"firstname"=> qq{FirstName LIKE '%$query%'},
"firstname" => qq{FirstName LIKE '%$query%'},
"lastname" => qq{LastName LIKE '%$query%'},
"comapny" => qq{Company LIKE '%$query%'},
"phone" => qq{Phone LIKE '%$query%' OR Fax LIKE '%$query%' OR MobilePhone LIKE '%$query%'},
"phone" =>
qq{Phone LIKE '%$query%' OR Fax LIKE '%$query%' OR MobilePhone LIKE '%$query%'},
"email" => qq{Email LIKE '%$query%'},
};
my $contact_fields = {
"fullname" => qq{FirstName LIKE '%$query%' OR LastName LIKE '%$query%'},
"firstname"=> qq{FirstName LIKE '%$query%'},
"firstname" => qq{FirstName LIKE '%$query%'},
"lastname" => qq{LastName LIKE '%$query%'},
"comapny" => qq{Department LIKE '%$query%'},
"phone" => qq{Phone LIKE '%$query%' OR Fax LIKE '%$query%' OR HomePhone LIKE '%$query%' OR MobilePhone LIKE '%$query%' OR OtherPhone LIKE '%$query%'},
"phone" =>
qq{Phone LIKE '%$query%' OR Fax LIKE '%$query%' OR HomePhone LIKE '%$query%' OR MobilePhone LIKE '%$query%' OR OtherPhone LIKE '%$query%'},
"email" => qq{Email LIKE '%$query%'},
};
my $contacts_query_criteria = [ ];
map { push @{ $contacts_query_criteria }, $contact_fields->{$_} if exists $contact_fields->{$_} } @{ $fields };
my $contacts_query_criteria = [];
map {
push @{$contacts_query_criteria}, $contact_fields->{$_}
if exists $contact_fields->{$_}
} @{$fields};
my $leads_query_criteria = [ ];
map { push @{ $leads_query_criteria }, $lead_fields->{$_} if exists $lead_fields->{$_} } @{ $fields };
my $leads_query_criteria = [];
map {
push @{$leads_query_criteria}, $lead_fields->{$_}
if exists $lead_fields->{$_}
} @{$fields};
my $contacts = $self->query({
my $contacts = $self->query(
{
query => {
criteria => join ' OR ', @{ $contacts_query_criteria }
criteria => join ' OR ',
@{$contacts_query_criteria}
},
object => 'Contact'
});
}
);
my $leads = $self->query({
my $leads = $self->query(
{
query => {
criteria => join ' OR ', @{ $leads_query_criteria }
criteria => join ' OR ',
@{$leads_query_criteria}
},
object => 'Lead'
});
}
);
my @result = ( @{$contacts}, @{$leads} );
......@@ -710,89 +781,118 @@ sub query_by_fields {
}
sub query_all {
my( $self, $query, $fields ) = @_;
my ( $self, $query, $fields ) = @_;
return $self->query_by_fields($query, $fields) if defined $fields && ref $fields eq 'ARRAY' && scalar @{ $fields };
return $self->query_by_fields( $query, $fields )
if defined $fields && ref $fields eq 'ARRAY' && scalar @{$fields};
return $self->find({
return $self->find(
{
query => {
what => $query,
#where => "phone"
where => "ALL"
},
returns => {
contact => ["id", "phone", "homephone", "mobilephone", "otherphone", "firstname", "lastname", "department", "email"],
lead => ["id", "phone", "mobilephone", "firstname", "lastname", "company", "email"]
contact => [
"id", "phone", "homephone", "mobilephone",
"otherphone", "firstname", "lastname", "department",
"email"
],
lead => [
"id", "phone",
"mobilephone", "firstname",
"lastname", "company",
"email", "isconverted",
"isdeleted"
]
}
});
}
);
}
sub query_tasks_by_rec_id {
my( $self, $rec_id ) = @_;
my ( $self, $rec_id ) = @_;
return $self->query({
return $self->query(
{
query => {
# fields => ["Id", "AccountId", "CallDisposition", "Description", "OwnerId", "Subject", "CallType", "WhatId", "WhoId"],
criteria => "CallDisposition = '" . $rec_id . "'"
},
object => "Task"
});
}
);
}
sub query_tasks_by_id {
my( $self, $id ) = @_;
my ( $self, $id ) = @_;
return $self->query({
return $self->query(
{
query => {
# fields => ["Id", "AccountId", "CallDisposition", "Description", "OwnerId", "Subject", "CallType", "WhatId", "WhoId"],
criteria => "Id = '" . $id . "'"
},
object => "Task"
});
}
);
}
sub query_leads_by_id {
my( $self, $id ) = @_;
my ( $self, $id ) = @_;
return $self->query({
return $self->query(
{
query => {
# fields => ["Id", "Company", "Description", "Email", "FirstName", "LastName", "Name", "Phone", "Title"],
criteria => "Id = '" . $id . "'"
},
object => "Lead"
});
}
);
}
sub query_contacts_by_id {
my( $self, $id ) = @_;
my ( $self, $id ) = @_;
return $self->query({
return $self->query(
{
query => {
# fields => ["Id", "Department", "Description", "Email", "FirstName", "LastName", "Name", "Phone", "Title"],
criteria => "Id = '" . $id . "'"
},
object => "Contact"
});
}
);
}
sub query_accounts_by_id {
my( $self, $id ) = @_;
my ( $self, $id ) = @_;
return $self->query({
return $self->query(
{
query => {
# fields => ["Id", "Department", "Description", "Email", "FirstName", "LastName", "Name", "Phone", "Title"],
criteria => "Id = '" . $id . "'"
},
object => "Account"
});
}
);
}
sub bless_hash {
my ($self, $object) = @_;
my $result = { };
sub fix_hash {
my ( $self, $object ) = @_;
my $result = {};
eval {
foreach my $key ( keys %{ $object } ) {
foreach my $key ( keys %{$object} ) {
next if $key =~ m/\_{2,}/;
$result->{$key} = $object->{$key};
$result->{$key} = '' unless defined $object->{$key};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment