-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1733 from bakaphp/feat-download-all-zoho-leads
feat: download all zoho leads
- Loading branch information
Showing
9 changed files
with
449 additions
and
169 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
app/Console/Commands/Connectors/Zoho/ZohoLeadsDownloadCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Console\Commands\Connectors\Zoho; | ||
|
||
use Baka\Traits\KanvasJobsTrait; | ||
use Illuminate\Console\Command; | ||
use Kanvas\Apps\Models\Apps; | ||
use Kanvas\Companies\Models\Companies; | ||
use Kanvas\Connectors\Zoho\Actions\DownloadAllZohoLeadAction; | ||
use Kanvas\Guild\Leads\Models\LeadReceiver; | ||
|
||
class ZohoLeadsDownloadCommand extends Command | ||
{ | ||
use KanvasJobsTrait; | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'kanvas:guild-zoho-lead-sync {app_id} {company_id} {receiver_id} {page=50} {leadsPerPage=200}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string|null | ||
*/ | ||
protected $description = 'Download all leads from Zoho to this branch'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
$app = Apps::getById((int) $this->argument('app_id')); | ||
$this->overwriteAppService($app); | ||
$company = Companies::getById((int) $this->argument('company_id')); | ||
$leadReceiver = LeadReceiver::getByIdFromCompanyApp((int) $this->argument('receiver_id'), $company, $app); | ||
$page = (int) $this->argument('page'); | ||
$leadsPerPage = (int) $this->argument('leadsPerPage'); | ||
|
||
$downloadAllLeads = new DownloadAllZohoLeadAction($app, $company, $leadReceiver); | ||
$downloadAllLeads->execute($page, $leadsPerPage); | ||
|
||
$this->info($downloadAllLeads->getTotalLeadsProcessed() . ' leads downloaded from Zoho to ' . $leadReceiver->name); | ||
|
||
return; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/Domains/Connectors/Zoho/Actions/DownloadAllZohoLeadAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kanvas\Connectors\Zoho\Actions; | ||
|
||
use Baka\Contracts\AppInterface; | ||
use Baka\Contracts\CompanyInterface; | ||
use Generator; | ||
use Kanvas\Connectors\Zoho\Client; | ||
use Kanvas\Guild\Leads\Models\LeadReceiver; | ||
|
||
class DownloadAllZohoLeadAction | ||
{ | ||
protected int $totalLeadsProcessed = 0; | ||
|
||
public function __construct( | ||
protected AppInterface $app, | ||
protected CompanyInterface $company, | ||
protected LeadReceiver $receiver, | ||
) { | ||
} | ||
|
||
public function execute($totalPages = 50, $leadsPerPage = 200): Generator | ||
{ | ||
$zohoClient = Client::getInstance($this->app, $this->company); | ||
|
||
for ($page = 1; $page <= $totalPages; $page++) { | ||
$leads = $zohoClient->leads->getList(['page' => $page, 'per_page' => $leadsPerPage]); | ||
foreach ($leads as $lead) { | ||
$syncZohoLead = new SyncZohoLeadAction( | ||
$this->app, | ||
$this->company, | ||
$this->receiver, | ||
$lead->getId() | ||
); | ||
|
||
$localLead = $syncZohoLead->execute($lead); | ||
|
||
if (! $localLead) { | ||
continue; | ||
} | ||
|
||
$this->totalLeadsProcessed++; | ||
yield $localLead; | ||
} | ||
} | ||
} | ||
|
||
public function getTotalLeadsProcessed(): int | ||
{ | ||
return $this->totalLeadsProcessed; | ||
} | ||
} |
186 changes: 186 additions & 0 deletions
186
src/Domains/Connectors/Zoho/Actions/SyncLeadToZohoAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kanvas\Connectors\Zoho\Actions; | ||
|
||
use Baka\Contracts\AppInterface; | ||
use Baka\Contracts\CompanyInterface; | ||
use Kanvas\Companies\Models\Companies; | ||
use Kanvas\Connectors\Zoho\Client; | ||
use Kanvas\Connectors\Zoho\DataTransferObject\ZohoLead; | ||
use Kanvas\Connectors\Zoho\Enums\CustomFieldEnum; | ||
use Kanvas\Connectors\Zoho\ZohoService; | ||
use Kanvas\Guild\Agents\Models\Agent; | ||
use Kanvas\Guild\Leads\Models\Lead; | ||
use Throwable; | ||
use Webleit\ZohoCrmApi\Models\Model; | ||
use Webleit\ZohoCrmApi\Modules\Leads as ZohoLeadModule; | ||
|
||
class SyncLeadToZohoAction | ||
{ | ||
public function __construct( | ||
protected AppInterface $app, | ||
protected Lead $lead | ||
) { | ||
} | ||
|
||
public function execute(): Model | ||
{ | ||
$zohoLead = ZohoLead::fromLead($this->lead); | ||
$zohoData = $zohoLead->toArray(); | ||
$lead = $this->lead; | ||
$company = $lead->company; | ||
$usesAgentsModule = $company->get(CustomFieldEnum::ZOHO_HAS_AGENTS_MODULE->value); | ||
|
||
$zohoCrm = Client::getInstance($this->app, $company); | ||
|
||
if (! $zohoLeadId = $lead->get(CustomFieldEnum::ZOHO_LEAD_ID->value)) { | ||
if ($usesAgentsModule) { | ||
$this->assignAgent($this->app, $zohoLead, $lead, $company, $zohoData); | ||
} | ||
|
||
$zohoLead = $zohoCrm->leads->create($zohoData); | ||
$zohoLeadId = $zohoLead->getId(); | ||
|
||
$zohoData['Lead_Status'] = 'New Lead'; | ||
|
||
$lead->set( | ||
CustomFieldEnum::ZOHO_LEAD_ID->value, | ||
$zohoLeadId | ||
); | ||
} else { | ||
$zohoLeadInfo = $zohoCrm->leads->get((string) $zohoLeadId)->getData(); | ||
if (! empty($zohoLeadInfo)) { | ||
$zohoLead = $zohoCrm->leads->update( | ||
(string) $zohoLeadId, | ||
$zohoData | ||
); | ||
} else { | ||
$lead->close(); | ||
} | ||
} | ||
|
||
$this->uploadAttachments($zohoCrm->leads, $lead); | ||
|
||
return $zohoLead; | ||
} | ||
|
||
protected function assignAgent( | ||
AppInterface $app, | ||
ZohoLead $zohoLead, | ||
Lead $lead, | ||
Companies $company, | ||
array &$zohoData | ||
): void { | ||
$memberNumber = (string) $zohoLead->getMemberNumber(); | ||
|
||
if (empty($memberNumber) && $lead->user()->exists()) { | ||
$memberNumber = (string) $lead->user()->firstOrFail()->get('member_number_' . $company->getId()); | ||
} | ||
|
||
if (! empty($memberNumber)) { | ||
$zohoMemberField = $company->get(CustomFieldEnum::ZOHO_MEMBER_FIELD->value) ?? 'Member_ID'; | ||
$zohoData[$zohoMemberField] = $memberNumber; | ||
} | ||
|
||
$zohoService = new ZohoService($app, $company); | ||
|
||
try { | ||
$agent = $zohoService->getAgentByMemberNumber($memberNumber); | ||
} catch (Throwable $e) { | ||
$agent = null; | ||
} | ||
|
||
try { | ||
$agentInfo = Agent::getByMemberNumber($memberNumber, $company); | ||
} catch (Throwable $e) { | ||
$agentInfo = null; | ||
} | ||
|
||
$defaultLeadSource = $company->get(CustomFieldEnum::ZOHO_DEFAULT_LEAD_SOURCE->value); | ||
if (! empty($defaultLeadSource)) { | ||
$zohoData['Lead_Source'] = $defaultLeadSource; //$lead->receiver ? $lead->receiver->name : $defaultLeadSource; | ||
} | ||
|
||
if (is_object($agent)) { | ||
try { | ||
///lead owner should match lead routing | ||
$leadRoutingEmailCleanUp = preg_replace('/[^a-zA-Z0-9@._-]/', '', $agent->Lead_Routing); | ||
$zohoData['Owner'] = $zohoService->getAgentByEmail($leadRoutingEmailCleanUp)->Owner['id']; | ||
} catch (Throwable $e) { | ||
//send fail notification and assign to default lead routing email | ||
$zohoData['Owner'] = (int) ($app->get(CustomFieldEnum::DEFAULT_OWNER->value) ?? $agent->Owner['id']); | ||
} | ||
|
||
if ($agent->Sponsor) { | ||
$zohoData['Sponsor'] = (string) $agent->Sponsor; | ||
} | ||
|
||
if ($agentInfo) { | ||
$lead->users_id = $agentInfo->users_id; | ||
$lead->saveOrFail(); | ||
} | ||
|
||
if ($agentInfo && $agentInfo->get('over_write_owner')) { | ||
$zohoData['Owner'] = (int) $agentInfo->get('over_write_owner'); | ||
} | ||
if (empty($defaultLeadSource)) { | ||
$zohoData['Lead_Source'] = $agent->name ?? $agent->Name; | ||
} | ||
} elseif ($agentInfo instanceof Agent) { | ||
$zohoData['Owner'] = (int) $agentInfo->owner_linked_source_id; | ||
if (empty($defaultLeadSource)) { | ||
$zohoData['Lead_Source'] = $agentInfo->name; | ||
} | ||
|
||
if ($agentInfo->user && ! empty($agentInfo->user->get('sponsor'))) { | ||
$zohoData['Sponsor'] = (string) $agentInfo->user->get('sponsor'); | ||
} | ||
} | ||
|
||
if ($company->get(CustomFieldEnum::ZOHO_USE_AGENT_NAME->value) && ! empty($agentInfo->name)) { | ||
$zohoData['Agent_Name'] = $agentInfo->name; | ||
} | ||
|
||
//if value is 0 or empty, remove it | ||
if (empty($zohoData['Owner'])) { | ||
unset($zohoData['Owner']); | ||
} | ||
} | ||
|
||
protected function uploadAttachments(ZohoLeadModule $zohoLead, Lead $lead): void | ||
{ | ||
$lead->load('files'); | ||
if (! $lead->files()->count()) { | ||
return; | ||
} | ||
|
||
$syncFiles = $lead->get(CustomFieldEnum::ZOHO_LEAD_SYNC_FILES->value) ?? []; | ||
|
||
foreach ($lead->files()->get() as $file) { | ||
if (isset($syncFiles[$file->id])) { | ||
continue; | ||
} | ||
|
||
try { | ||
$fileContent = file_get_contents($file->url); | ||
|
||
$zohoLead->uploadAttachment( | ||
(string) $lead->get(CustomFieldEnum::ZOHO_LEAD_ID->value), | ||
$file->name, | ||
$fileContent | ||
); | ||
|
||
$syncFiles[$file->id] = $file->id; | ||
} catch (Throwable $e) { | ||
//do nothing | ||
} | ||
} | ||
|
||
$lead->set( | ||
CustomFieldEnum::ZOHO_LEAD_SYNC_FILES->value, | ||
$syncFiles | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.