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