<?php
namespace EADPlataforma\Services\Marketing;
use Doctrine\ORM\EntityManagerInterface;
use EADPlataforma\Services\Marketing\RdStationService;
use EADPlataforma\Services\Marketing\LaharService;
use EADPlataforma\Services\Marketing\MailChimpService;
use EADPlataforma\Services\Marketing\ActiveCampaignService;
use EADPlataforma\Services\Marketing\EadStationService;
use EADPlataforma\Services\SlackService;
use EADPlataforma\Services\DiscordService;
use EADPlataforma\Services\GeneralService;
use EADPlataforma\Services\ConfigurationService;
use EADPlataforma\Entity\User;
use EADPlataforma\Entity\City;
use EADPlataforma\Entity\State;
use EADPlataforma\Entity\Country;
use EADPlataforma\Entity\ProductOffer;
use EADPlataforma\Enum\TagsMarketingEnum;
use EADPlataforma\Enum\ProductOpportunityEnum;
class MarketingService
{
/**
* @var RdStationService
*/
protected $rdStationService;
/**
* @var LaharService
*/
protected $laharService;
/**
* @var MailChimpService
*/
protected $mailChimpService;
/**
* @var SlackService
*/
protected $slackService;
/**
* @var DiscordService
*/
protected $discordService;
/**
* @var ActiveCampaignService
*/
protected $activeCampaignService;
/**
* @var EadStationService
*/
protected $eadStationService;
/**
* @var ConfigurationService
*/
private $configuration;
/**
* @var GeneralService
*/
protected $generalService;
/**
* @var \Client
*/
protected $client;
/**
* @var SchoolEntityManager $em
*/
protected $em;
/**
* @var \User
*/
protected $user;
/**
* @var \ProductOffer
*/
protected $productOffer;
/**
* @var string
*/
protected $email;
/**
* @var string
*/
protected $phone = null;
/**
* @var string
*/
protected $birthday = null;
/**
* @var string
*/
protected $bio = null;
/**
* @var string
*/
protected $city = null;
/**
* @var string
*/
protected $state = null;
/**
* @var string
*/
protected $country = null;
/**
* @var string
*/
protected $website = null;
/**
* @var string
*/
protected $twitter = null;
/**
* @var string
*/
protected $linkedin = null;
/**
* @var string
*/
protected $facebook = null;
/**
* @var int
*/
protected $tag = TagsMarketingEnum::TAG_VISITED_PAGE;
/**
* @var int
*/
protected $opportunity = null;
/**
* @var string
*/
protected $textComplement = '';
/**
* @var Bool
*/
protected $debug = false;
/**
* Constructor
*
* @param RdStationService $rdStationService
* @param LaharService $laharService
* @param MailChimpService $mailChimpService
* @param ActiveCampaignService $activeCampaignService
* @param EadStationService $eadStationService
* @param SlackService $slackService
* @param DiscordService $discordService
* @param ConfigurationService $configuration
* @param GeneralService $generalService
*/
public function __construct(
RdStationService $rdStationService,
LaharService $laharService,
MailChimpService $mailChimpService,
ActiveCampaignService $activeCampaignService,
EadStationService $eadStationService,
SlackService $slackService,
DiscordService $discordService,
ConfigurationService $configuration,
GeneralService $generalService
)
{
$this->rdStationService = $rdStationService;
$this->laharService = $laharService;
$this->mailChimpService = $mailChimpService;
$this->activeCampaignService = $activeCampaignService;
$this->eadStationService = $eadStationService;
$this->slackService = $slackService;
$this->discordService = $discordService;
$this->configuration = $configuration;
$this->client = $this->configuration->getClient();
$this->generalService = $generalService;
$this->em = $this->generalService->getService('SchoolEntityManager');
}
public function setDebug(?bool $debug)
{
$this->debug = $debug;
return $this;
}
public function getTagInfo()
{
$info = [
TagsMarketingEnum::TAG_REGISTERED_EAD => (object)[
"flag" => 'user',
"textTag" => 'Cadastrou-se no EAD',
"actionLahar" => 'Cadastro',
],
TagsMarketingEnum::TAG_CONFIRM_REGISTER => (object)[
"flag" => 'user',
"textTag" => 'Confirmou cadastro no EAD',
"actionLahar" => 'Cadastro',
],
TagsMarketingEnum::TAG_SIGNUP_NEWSLETTER => (object)[
"flag" => 'user',
"textTag" => 'Assinou Newsletter',
"actionLahar" => 'Cadastro',
],
TagsMarketingEnum::TAG_REGISTERED_EAD_FACEBOOK => (object)[
"flag" => 'user',
"textTag" => 'Cadastrou-se no EAD com Facebook',
"actionLahar" => 'Cadastro',
],
TagsMarketingEnum::TAG_REGISTERED_EAD_GOOGLE => (object)[
"flag" => 'user',
"textTag" => 'Cadastrou-se no EAD com Google',
"actionLahar" => 'Cadastro',
],
TagsMarketingEnum::TAG_VISITED_COURSE_PAGE => (object)[
"flag" => 'visit',
"textTag" => 'Visitou Página: ',
"actionLahar" => 'Visitou Página',
],
TagsMarketingEnum::TAG_ADD_CART => (object)[
"flag" => 'product',
"textTag" => 'Adicionou ao Carrinho: ',
"actionLahar" => 'Adicionou ao Carrinho',
],
TagsMarketingEnum::TAG_REMOVE_CART => (object)[
"flag" => 'product',
"textTag" => 'Removeu do Carrinho: ',
"actionLahar" => 'Removeu do Carrinho',
],
TagsMarketingEnum::TAG_ADD_DESIRE_LIST => (object)[
"flag" => 'product',
"textTag" => 'Lista Desejo: ',
"actionLahar" => 'Lista de Desejo',
],
TagsMarketingEnum::TAG_ADD_WAIT_LIST => (object)[
"flag" => 'product',
"textTag" => 'Lista de espera: ',
"actionLahar" => 'Lista de Espera',
],
TagsMarketingEnum::TAG_VISITED_PAGE => (object)[
"flag" => 'visit',
"textTag" => 'Visitou Página: ',
"actionLahar" => 'Visitou Página',
],
TagsMarketingEnum::TAG_ISSUE_CERTIFICATE => (object)[
"flag" => 'course',
"textTag" => 'Emitiu Certificado: ',
"actionLahar" => 'Emitiu Certificado',
],
TagsMarketingEnum::TAG_BEGIN_COURSE => (object)[
"flag" => 'course',
"textTag" => 'Iniciou o curso: ',
"actionLahar" => 'Cadastro',
],
TagsMarketingEnum::TAG_FINISH_COURSE => (object)[
"flag" => 'course',
"textTag" => 'Concluiu o curso: ',
"actionLahar" => 'Concluiu Curso',
],
TagsMarketingEnum::TAG_WAITING_PAYMENT => (object)[
"flag" => 'payment',
"textTag" => 'Aguardando Pagamento: ',
"actionLahar" => 'Matrícula',
],
TagsMarketingEnum::TAG_PAYMENT_APPROVED => (object)[
"flag" => 'payment',
"textTag" => 'Pagamento Aprovado: ',
"actionLahar" => 'Matrícula',
],
TagsMarketingEnum::TAG_PAYMENT_CANCELED => (object)[
"flag" => 'payment',
"textTag" => 'Pagamento Cancelado: ',
"actionLahar" => 'Matrícula',
],
TagsMarketingEnum::TAG_PAYMENT_DISPUTE => (object)[
"flag" => 'payment',
"textTag" => 'Pagamento em disputa: ',
"actionLahar" => 'Matrícula',
],
TagsMarketingEnum::TAG_PAYMENT_REPAYMENT => (object)[
"flag" => 'payment',
"textTag" => 'Reembolso: ',
"actionLahar" => 'Matrícula',
],
TagsMarketingEnum::TAG_PAYMENT_REPAYMENT_PROGRESS => (object)[
"flag" => 'payment',
"textTag" => 'Estorno em andamento: ',
"actionLahar" => 'Matrícula',
],
TagsMarketingEnum::TAG_PAYMENT_CHARGEBACK => (object)[
"flag" => 'payment',
"textTag" => 'Chargeback: ',
"actionLahar" => 'Matrícula',
],
TagsMarketingEnum::TAG_START_SUBSCRIPTION => (object)[
"flag" => 'subscription',
"textTag" => 'Assinou o plano: ',
"actionLahar" => 'Matrícula',
],
TagsMarketingEnum::TAG_CANCELED_SUBSCRIPTION => (object)[
"flag" => 'subscription',
"textTag" => 'Cancelou o plano: ',
"actionLahar" => 'Cancelou plano'
],
TagsMarketingEnum::TAG_BILL_EXPIRATION => (object)[
"flag" => 'subscription',
"textTag" => 'Boleto Vencido: ',
"actionLahar" => 'Boleto Vencido',
],
TagsMarketingEnum::TAG_PIX_EXPIRATION => (object)[
"flag" => 'subscription',
"textTag" => 'Pix Vencido: ',
"actionLahar" => 'PIX Vencido',
],
TagsMarketingEnum::TAG_TRIAL_PERIOD => (object)[
"flag" => 'subscription',
"textTag" => 'Período experimental: ',
"actionLahar" => 'Período experimental',
],
TagsMarketingEnum::TAG_ASSOCIATE_GROUP => (object)[
"flag" => 'group',
"textTag" => 'Participa do grupo: ',
"actionLahar" => 'Cadastro',
],
TagsMarketingEnum::TAG_ENROLLMENT_MANUALLY => (object)[
"flag" => 'enrollment',
"textTag" => 'Matriculado Manualmente: ',
"actionLahar" => 'Matrícula',
],
TagsMarketingEnum::TAG_ENROLLMENT_SUBSCRIPTION => (object)[
"flag" => 'enrollment',
"textTag" => 'Matriculado via assinatura: ',
"actionLahar" => 'Matrícula',
],
TagsMarketingEnum::TAG_ENROLLMENT_GROUP => (object)[
"flag" => 'enrollment',
"textTag" => 'Matriculado via grupo: ',
"actionLahar" => 'Matrícula',
],
TagsMarketingEnum::TAG_ENROLLMENT_COUPON => (object)[
"flag" => 'enrollment',
"textTag" => 'Matriculado via cupom: ',
"actionLahar" => 'Matrícula',
],
TagsMarketingEnum::TAG_FREE_COURSE_ACCESS => (object)[
"flag" => 'enrollment',
"textTag" => 'Acessou Curso Grátis: ',
"actionLahar" => 'Cadastro',
],
];
return $info[$this->tag];
}
public function setUser(User $user)
{
$this->user = $user;
return $this;
}
public function setProductOffer(ProductOffer $productOffer)
{
$this->productOffer = $productOffer;
return $this;
}
public function setEmail(string $email)
{
$this->email = $email;
return $this;
}
public function setPhone(string $phone)
{
$this->phone = (!empty($phone) ? $phone : $this->phone);
return $this;
}
public function setTag(int $tag)
{
$this->tag = (!empty($tag) ? $tag : $this->tag);
return $this;
}
public function setOpportunity(int $opportunity)
{
$this->opportunity = (!empty($opportunity) ? $opportunity : $this->opportunity);
return $this;
}
public function setTextComplement(string $textComplement)
{
$this->textComplement = (
!empty($textComplement) ? $textComplement : $this->textComplement
);
return $this;
}
public function getTagMarketingDomain()
{
$tagMarketingDomain = '';
if(
$this->configuration->get('tags_marketing_domain') == TagsMarketingEnum::YES
&& !empty($this->client->getDomainPrimary())
){
$tagMarketingDomain = "{$this->client->getDomainPrimary()} - ";
}
return $tagMarketingDomain;
}
public function getData()
{
$tagInfo = $this->getTagInfo();
$data = [
"userName" => $this->email,
"email" => $this->email,
"phone" => $this->phone,
"userId" => null,
"birthday" => null,
"bio" => null,
"city" => null,
"state" => null,
"country" => null,
"website" => null,
"twitter" => null,
"linkedin" => null,
"facebook" => null,
"tag" => $this->getTagMarketingDomain() . $tagInfo->textTag . $this->textComplement,
"actionLahar" => $tagInfo->actionLahar,
"actionActiveCampaign" => 'contact_tag_add',
"pipedrivePersonId" => null,
"productOfferId" => null,
"opportunity" => null,
"traffic_medium" => (!empty($this->client->getDomainPrimary()) ? $this->client->getDomainPrimary() : null)
];
if(
$this->tag == TagsMarketingEnum::TAG_REGISTERED_EAD ||
$this->tag == TagsMarketingEnum::TAG_CONFIRM_REGISTER
){
$data["actionActiveCampaign"] = 'contact_add';
}
if($this->user)
{
$user = $this->user;
$data["userId"] = $this->user->getId();
$data["userName"] = $this->user->getName();
$data["email"] = $this->user->getEmail();
$data["phone"] = $this->user->getPhone();
$data['birthday'] = $this->user->getBirthDate();
$data['bio'] = $this->user->getBiography();
$data['city'] = (!empty($user->getCity()) ? $user->getCity()->getName() : null);
$data['state'] = (!empty($user->getState()) ? $user->getState()->getUf() : null);
$data['country'] = (!empty($user->getCountry()) ? $user->getCountry()->getName() : null);
$data['website'] = $this->user->getWebsite();
$data['twitter'] = $this->user->getTwitter();
$data['linkedin'] = $this->user->getLinkedin();
$data['facebook'] = $this->user->getFacebook();
$data["pipedrivePersonId"] = $this->user->getPipedrivePerson();
}
if(!empty($this->opportunity)){
if($this->productOffer){
$data["productOfferId"] = $this->productOffer->getId();
}
$data["opportunity"] = $this->opportunity;
}
if($this->debug){
$discordService = $this->generalService->getService('DiscordService');
$discordService->setChannel('debug');
$discordService->setMessage(json_encode($data));
$discordService->sendDiscord();
}
return $data;
}
public function send()
{
if($this->debug){
$discordService = $this->generalService->getService(
'DiscordService'
);
$discordService->setChannel('debug');
$discordService->setMessage("Start Marketing");
$discordService->sendDiscord();
}
$data = $this->getData();
$tagInfo = $this->getTagInfo();
$this->veriFyTagRdStation($data, $tagInfo->flag);
$this->verifyTagLahar($data, $tagInfo->flag);
$this->verifyTagActiveCampaign($data, $tagInfo->flag);
$this->verifyTagMailChimp($data);
$this->verifySlack($data, $tagInfo->flag);
$this->verifyDiscord($data, $tagInfo->flag);
$this->verifyOpportunity($data);
$this->sendNotesPipedrive($data);
}
public function veriFyTagRdStation($data, $flag){
if($this->configuration->get("app_rd_{$flag}") == TagsMarketingEnum::YES){
$this->rdStationService->setDebug($this->debug);
$this->rdStationService->prepareConversion($data);
}
}
public function verifyTagLahar($data, $flag){
if($this->configuration->get("app_lahar_{$flag}") == TagsMarketingEnum::YES){
$this->laharService->prepareConversion($data);
}
}
public function verifyTagActiveCampaign($data, $flag){
if($this->configuration->get("app_activecampaign_{$flag}") == TagsMarketingEnum::YES){
$this->activeCampaignService->prepareConversion($data);
}
}
public function verifyTagMailChimp($data){
$this->mailChimpService->prepareConversion($data);
}
public function verifySlack($data, $flag){
if($this->configuration->get("app_slack_{$flag}") == TagsMarketingEnum::YES){
if($this->tag == TagsMarketingEnum::TAG_SIGNUP_NEWSLETTER)
{
$data["email"] = $this->email;
$data["userName"] = $this->email;
$data["phone"] = '---';
$data["userName"] = '---';
}
$this->slackService->sendSlack($data);
}
}
public function verifyDiscord($data, $flag){
if($this->configuration->get("app_discord_{$flag}") == TagsMarketingEnum::YES){
if($this->tag == TagsMarketingEnum::TAG_SIGNUP_NEWSLETTER)
{
$data["email"] = $this->email;
$data["userName"] = $this->email;
$data["phone"] = '---';
$data["userId"] = '---';
}
$this->discordService->setChannel('clientServer');
$this->discordService->prepareMessage($data);
}
}
public function verifyOpportunity($data){
$this->eadStationService->sendOpportunity($data);
}
public function sendNotesPipedrive($data)
{
$crmService = $this->generalService->getService('CRM\\CrmService');
if($data["pipedrivePersonId"] != null)
{
$pipedriveId = $data["pipedrivePersonId"];
$tag = $data["tag"];
$crmService->saveNote($pipedriveId, $tag);
}
}
}