<?php
namespace EADPlataforma\Controller\Website;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use EADPlataforma\Entity\Banner;
use EADPlataforma\Entity\Cart;
use EADPlataforma\Entity\Category;
use EADPlataforma\Entity\City;
use EADPlataforma\Entity\Country;
use EADPlataforma\Entity\Course;
use EADPlataforma\Entity\CourseCertificate;
use EADPlataforma\Entity\CourseCertificateTemplate;
use EADPlataforma\Entity\CourseTeam;
use EADPlataforma\Entity\CourseTestimonial;
use EADPlataforma\Entity\Configuration;
use EADPlataforma\Entity\CycleItem;
use EADPlataforma\Entity\Dns;
use EADPlataforma\Entity\Enrollment;
use EADPlataforma\Entity\Exam;
use EADPlataforma\Entity\ExamUser;
use EADPlataforma\Entity\Faq;
use EADPlataforma\Entity\Forum;
use EADPlataforma\Entity\ForumCategory;
use EADPlataforma\Entity\Group;
use EADPlataforma\Entity\Lesson;
use EADPlataforma\Entity\LessonLog;
use EADPlataforma\Entity\LessonLogOrigin;
use EADPlataforma\Entity\LessonModule;
use EADPlataforma\Entity\LessonSupport;
use EADPlataforma\Entity\LessonXLibrary;
use EADPlataforma\Entity\Library;
use EADPlataforma\Entity\Newsletter;
use EADPlataforma\Entity\Page;
use EADPlataforma\Entity\Product;
use EADPlataforma\Entity\ProductCharge;
use EADPlataforma\Entity\ProductCoupon;
use EADPlataforma\Entity\ProductOffer;
use EADPlataforma\Entity\ProductPage;
use EADPlataforma\Entity\ProductSuggestion;
use EADPlataforma\Entity\ProductTeam;
use EADPlataforma\Entity\Question;
use EADPlataforma\Entity\Receiver;
use EADPlataforma\Entity\ReceiverDocument;
use EADPlataforma\Entity\ReceiverInvoice;
use EADPlataforma\Entity\ReceiverTransfer;
use EADPlataforma\Entity\State;
use EADPlataforma\Entity\Session;
use EADPlataforma\Entity\TaskQueue;
use EADPlataforma\Entity\Transaction;
use EADPlataforma\Entity\TransactionItem;
use EADPlataforma\Entity\User;
use EADPlataforma\Entity\UserCheckoutInfo;
use EADPlataforma\Entity\UserCustomField;
use EADPlataforma\Entity\UserProfile;
use EADPlataforma\Entity\UserSubscription;
use EADPlataforma\Entity\Webhook;
use EADPlataforma\Entity\WebhookQueue;
use EADPlataforma\Entity\UserLog;
use EADPlataforma\Entity\Trash;
use EADPlataforma\Enum\CourseEnum;
use EADPlataforma\Enum\ConfigurationEnum;
use EADPlataforma\Enum\TransactionEnum;
use EADPlataforma\Enum\ProductEnum;
use EADPlataforma\Enum\ProductCouponEnum;
use EADPlataforma\Enum\ProductChargeEnum;
use EADPlataforma\Enum\ProductOfferEnum;
use EADPlataforma\Enum\LessonModuleEnum;
use EADPlataforma\Enum\EnrollmentEnum;
use EADPlataforma\Enum\ExamUserEnum;
use EADPlataforma\Enum\UserSubscriptionEnum;
use EADPlataforma\Enum\ClientEnum;
use EADPlataforma\Enum\ServicesEnum;
use EADPlataforma\Enum\ReceiverEnum;
use EADPlataforma\Enum\UserEnum;
use EADPlataforma\Enum\TagsMarketingEnum;
use EADPlataforma\Enum\NotificationEnum;
use EADPlataforma\Enum\ErrorEnum;
/**
* @Route(
* path = "",
* schemes = {"http|https"}
* )
* @Cache(
* maxage = "0",
* smaxage = "0",
* expires = "now",
* public = false
* )
*/
class CronController extends AbstractWebsiteController {
/**
* @Route(
* path = "/general/{clientToken}",
* name = "cronGeneral",
* methods = {"GET"}
* )
*/
public function general(Request $request) {
$token = $request->headers->get('AUTH-TOKEN');
if($token != $this->generalService->getTokenCron()){
return $this->eadResponse(null, ErrorEnum::AUTH_INVALID);
}
if($request->get('clientToken') != $this->clientConfig->getToken()){
return $this->eadResponse(null, ErrorEnum::AUTH_INVALID);
}
$discordService = $this->generalService->getService('DiscordService');
$discordService->setChannel('debug');
try{
$cronService = $this->generalService->getService('Aws\\AwsEventBridge');
$cronService->deleteApiDestinationByCronName(ServicesEnum::CRON_GENERAL);
}catch(Exception $e){
$discordService->setMessage("{$this->eadDomain} - {$e->getMessage()}");
$discordService->sendDiscord();
}
$usRepository = $this->em->getRepository(UserSubscription::class);
$transactionRepository = $this->em->getRepository(Transaction::class);
$cartRepository = $this->em->getRepository(Cart::class);
$examUserRepository = $this->em->getRepository(ExamUser::class);
$examUserRepository->expiredExams();
$cartRepository->updateCartsWithProductCouponExpired();
$bill = TransactionEnum::PAYMENT_BILL;
$pix = TransactionEnum::PAYMENT_PIX;
$todayDate = date('Y-m-d');
$lastExecutionDay = $this->configuration->get('cron_date');
if(!empty($lastExecutionDay)){
$lastExecutionDay = date('Y-m-d', strtotime($lastExecutionDay));
if($todayDate == $lastExecutionDay){
return $this->eadResponse([ "success" => ErrorEnum::YES ]);
}
}
$hour = rand(7,9);
$min = rand(10,59);
if(!$this->configuration->checkModuleIsAbleOnPlan('unlimitedCourseFunction')){
$courseRepository = $this->em->getRepository(Course::class);
$publishCourses = $courseRepository->count([
"deleted" => CourseEnum::ITEM_NO_DELETED,
"status" => CourseEnum::PUBLISHED
]);
if($publishCourses > CourseEnum::YES){
$coursesPublic = $courseRepository->findBy([
"deleted" => CourseEnum::ITEM_NO_DELETED,
"status" => CourseEnum::PUBLISHED
]);
$courseNew = null;
foreach ($coursesPublic as $key => $course) {
$course->setStatus(CourseEnum::DRAFT);
if(!$courseNew){
$courseNew = $course;
}
if($course->getId() > $courseNew->getId()){
$courseNew = $course;
}
}
if($courseNew){
$course->setStatus(CourseEnum::PUBLISHED);
}
$this->em->flush();
}
}
$this->configuration->set('cron_date', "{$todayDate} 0{$hour}:{$min}:00", false);
$this->em->getRepository(Product::class)->notifyEnrollmentByProductPeriod();
$usRepository->completeUserSubscription();
$usRepository->expiredUserSubscription();
$usRepository->canceledUserSubscription();
$usRepository->notifyUserSubscritpionWithCardExpired();
$usRepository->alertExpireCardUserSubscription();
//$transactionRepository->updateTransactionPaidAndAnticipated();
try{
$transactionRepository->processTransactionCardWait();
}catch(Exception $e){
$discordService->setMessage("{$this->eadDomain} - {$e->getMessage()}");
$discordService->sendDiscord();
}
try{
$transactionRepository->cancelPaymentMethod($bill);
$transactionRepository->cancelPaymentMethod($pix);
}catch(Exception $e){
$discordService->setMessage("{$this->eadDomain} - {$e->getMessage()}");
$discordService->sendDiscord();
}
try{
$transactionRepository->alertPaymentMethod($bill);
$transactionRepository->alertPaymentMethod($pix);
}catch(Exception $e){
$discordService->setMessage("{$this->eadDomain} - {$e->getMessage()}");
$discordService->sendDiscord();
}
try{
$cartRepository->alertCheckoutWait();
}catch(Exception $e){
$discordService->setMessage("{$this->eadDomain} - {$e->getMessage()}");
$discordService->sendDiscord();
}
try{
$cartRepository->alertCartWait();
}catch(Exception $e){
$discordService->setMessage("{$this->eadDomain} - {$e->getMessage()}");
$discordService->sendDiscord();
}
try{
$this->em->getRepository(ReceiverInvoice::class)->turnInExpiredInvoices();
}catch(Exception $e){
$discordService->setMessage("{$this->eadDomain} - {$e->getMessage()}");
$discordService->sendDiscord();
}
$platformStatus = $this->clientConfig->getPlatformStatus();
$suspended = ClientEnum::PLATFORM_STATUS_SUSPENDED;
$canceled = ClientEnum::PLATFORM_STATUS_CANCELED;
/*$amountLastDay = $transactionRepository->getAmountLastDay();
if($amountLastDay >= 500){
$receiverRepository = $this->em->getRepository(Receiver::class);
$receiversNotTrustable = $receiverRepository->findBy([
"isTrustable" => ReceiverEnum::NO,
"deleted" => ReceiverEnum::ITEM_NO_DELETED,
"accountType" => ReceiverEnum::EAD_CHECKOUT,
]);
if(!empty($receiversNotTrustable)){
foreach ($receiversNotTrustable as $key => $receiver) {
$receiverHash = $receiver->getReceiverHash();
$urlAdmin = "https://eadmin.eadplataforma.com/index.php?mod=editar-recebedor-v8&recebedor_id={$receiverHash}";
$discordService = $this->generalService->getService('DiscordService');
$discordService->setChannel('recebedores');
$discordService->setMessage("Recebedor sem marcação de confiável - {$urlAdmin}");
$discordService->sendDiscord();
}
}
}*/
$usWasCharge = [];
if($platformStatus != $suspended && $platformStatus != $canceled){
if($this->configuration->isModuleActive("product_subscription_module")){
$transactionItemRepository = $this->em->getRepository(TransactionItem::class);
$usRepository = $this->em->getRepository(UserSubscription::class);
$productCouponRepository = $this->em->getRepository(ProductCoupon::class);
$poRepository = $this->em->getRepository(ProductOffer::class);
$transactionService = $this->generalService->getService(
'Transaction\\TransactionService'
);
$notificationService = $this->generalService->getService('NotificationService');
// send marketing
$marketingService = $this->generalService->getService(
'Marketing\\MarketingService'
);
$baseDate = $request->get('baseDate');
if(!isset($baseDate) && empty($baseDate)){
$baseDate = date('Y-m-d');
}
$card = TransactionEnum::PAYMENT_CARD;
$bill = TransactionEnum::PAYMENT_BILL;
$pix = TransactionEnum::PAYMENT_PIX;
foreach ([$bill, $pix] as $key => $paymentMethodType) {
foreach ([3, 6] as $key => $day) {
$items = $transactionItemRepository->getTransactionItemForSubscription(
$paymentMethodType,
$day,
$baseDate
);
$transactionItemRepository->sendEmailTransactionItemForSubscription(
$items,
$paymentMethodType,
$day
);
}
}
$usNoPaidCard = $usRepository->getAllUserSubscriptionNoPaidCard();
foreach ($usNoPaidCard as $key => $userSubscription) {
$userSubscription->setStatus(UserSubscriptionEnum::STATUS_CANCELED);
$userSubscription->setCancelReason(UserSubscriptionEnum::CANCELED_NO_PAYMENT);
$userSubscription->setDateCancel($baseDate);
$userSubscription->setDateNextPayment($baseDate);
$this->em->flush();
$return = $userSubscription->toReturn();
$this->userLogService->logUpdate(
"user_subscription",
$userSubscription->getId(),
$return
);
$marketingService->setTag(TagsMarketingEnum::TAG_CANCELED_SUBSCRIPTION);
$marketingService->setTextComplement(
$userSubscription->getProduct()->getTitle()
);
$marketingService->setUser($userSubscription->getUser());
$marketingService->send();
$notificationService->create(
$userSubscription->getProduct()->getUser(),
$userSubscription->getUser(),
NotificationEnum::ORIGIN_USER_SUBSCRIPTION_CANCELED,
$userSubscription->getId()
);
$notificationService->create(
$userSubscription->getUser(),
$userSubscription->getProduct()->getUser(),
NotificationEnum::ORIGIN_USER_SUBSCRIPTION_CANCELED,
$userSubscription->getId()
);
$usRepository->sendEmailUserSubscription(
$userSubscription,
UserSubscriptionEnum::SUBSCRIPTION_NO_PAYMENT5
);
}
$date30 = date('Y-m-d', strtotime("{$baseDate} + 30 day"));
$userSubscriptionCard = $usRepository->getUserSubscriptionToRenew(
$card,
$date30
);
$userSubscriptionBill = $usRepository->getUserSubscriptionToRenew(
$bill,
$date30
);
$userSubscriptionPix = $usRepository->getUserSubscriptionToRenew(
$pix,
$date30
);
$userSubscriptions30 = array_merge(
$userSubscriptionCard,
$userSubscriptionBill,
$userSubscriptionPix
);
foreach ($userSubscriptions30 as $key => $userSubscription) {
if($userSubscription->getCycle() != UserSubscriptionEnum::CYCLE_MONTHLY){
$usRepository->sendEmailUserSubscription(
$userSubscription,
UserSubscriptionEnum::SUBSCRIPTION_RENEW_ALERT,
30
);
}
}
$date7 = date('Y-m-d', strtotime("{$baseDate} + 7 day"));
$userSubscriptionCard = $usRepository->getUserSubscriptionToRenew(
$card,
$date7
);
$userSubscriptionBill = $usRepository->getUserSubscriptionToRenew(
$bill,
$date7
);
$userSubscriptionPix = $usRepository->getUserSubscriptionToRenew(
$pix,
$date7
);
$userSubscriptions7 = array_merge(
$userSubscriptionCard,
$userSubscriptionBill,
$userSubscriptionPix
);
foreach ($userSubscriptions7 as $key => $userSubscription) {
$usRepository->sendEmailUserSubscription(
$userSubscription,
UserSubscriptionEnum::SUBSCRIPTION_RENEW_ALERT,
7
);
}
$userSubscriptionRenewWrong = $usRepository->getUserSubscriptionToRenewWrong();
foreach ($userSubscriptionRenewWrong as $key => $userSubscription) {
$dateRenew = date(
'Y-m-d',
strtotime($userSubscription->getDateRenew() . ' + 1 month ')
);
$userSubscription->setDateRenew($dateRenew);
$this->em->flush();
$return = $userSubscription->toReturn();
$this->userLogService->logUpdate(
"user_subscription",
$userSubscription->getId(),
$return
);
}
$userSubscriptionsRenewCard = $usRepository->getUserSubscriptionToRenew(
$card,
$baseDate
);
$userSubscriptionsPayCard = $usRepository->getUserSubscriptionToPay(
$card,
$baseDate,
true
);
$userSubscriptionsRenewBill = $usRepository->getUserSubscriptionToRenew(
$bill,
$baseDate
);
$userSubscriptionsPayBill = $usRepository->getUserSubscriptionToPay(
$bill,
$baseDate,
true
);
$userSubscriptionsRenewPix = $usRepository->getUserSubscriptionToRenew(
$pix,
$baseDate
);
$userSubscriptionsPayPix = $usRepository->getUserSubscriptionToPay(
$pix,
$baseDate,
true
);
$allUserSubscriptions = array_merge(
$userSubscriptionsRenewCard,
$userSubscriptionsPayCard,
$userSubscriptionsRenewBill,
$userSubscriptionsPayBill,
$userSubscriptionsRenewPix,
$userSubscriptionsPayPix
);
$pagarMeTransaction = $this->generalService->getService(
'PagarMe\\PagarMeTransaction'
);
$paymentConfig = $this->configuration->getPaymentConfig();
foreach ($allUserSubscriptions as $key => $userSubscription) {
try{
$paymentMethod = $card;
$userCard = $userSubscription->getUserCard();
if(empty($userCard) && $userSubscription->getPaymentMethod() == $card){
$userCard = null;
$paymentMethod = $bill;
}else if($userSubscription->getPaymentMethod() == $bill){
$userCard = null;
$paymentMethod = $bill;
}else if($userSubscription->getPaymentMethod() == $pix){
$userCard = null;
$paymentMethod = $pix;
}
$amount = $userSubscription->getPrice();
$membershipFee = $userSubscription->getMembershipFee();
$chargeNumbers = $userSubscription->getChargeNumber();
$productCoupon = $userSubscription->getProductCoupon();
$isRenew = ($userSubscription->getDateRenew() == $baseDate);
if(
$userSubscription->getPaymentMethod() != UserSubscriptionEnum::PAYMENT_CARD
){
$dateCharge = date('Y-m-d', strtotime("{$baseDate} +7 days"));
$isRenew = ($userSubscription->getDateRenew() == $dateCharge);
}
$isLifetime = $userSubscription->getLifetime() == UserSubscriptionEnum::YES;
$isRenew = ($isRenew && !$isLifetime) || empty($chargeNumbers);
if(!empty($userSubscription->getCouponKey()) && !empty($productCoupon)){
if(
$userSubscription->getCouponLifetime() == UserSubscriptionEnum::YES ||
$chargeNumbers < $userSubscription->getCouponNumberCharges()
){
if(
$userSubscription->getCouponApplyMembershipFee() == ProductCouponEnum::YES
){
if($isRenew){
$membershipFee = $productCouponRepository->applyDiscount(
$productCoupon,
$membershipFee
);
}
}else{
$amount = $productCouponRepository->applyDiscount(
$productCoupon,
$amount
);
}
}else{
$productCoupon = null;
}
}
if($isRenew){
$amount = $amount + $membershipFee;
$userSubscription->setChargeNumber(0);
}
$amountCents = round($amount * 100);
$items = [
(object)[
"courseId" => null,
"userSubscriptionId" => $userSubscription->getId(),
"productOfferId" => $userSubscription->getProductOffer()->getId(),
"productId" => $userSubscription->getProduct()->getId(),
"productCouponId" => (
$productCoupon ? $productCoupon->getId() : null
),
"amount" => $amount,
"cartId" => null,
]
];
if(!in_array($userSubscription->getId(), $usWasCharge)){
$usWasCharge[] = $userSubscription->getId();
$transactionOrigin = TransactionEnum::ORIGIN_RECURRENCE;
$recurrenceCycle = "subsequent";
$initiatedType = null;
$recurrenceModel = null;
if(empty($userSubscription->getDateLastPayment())){
$transactionOrigin = TransactionEnum::ORIGIN_INTERNAL;
$recurrenceCycle = "first";
$initiatedType = "Retry";
}
$installments = $userSubscription->getInstallments();
if(empty($installments)){
$installments = UserSubscriptionEnum::YES;
}
if($paymentMethod == $card){
$freeInstallments = $userSubscription->getInstallmentsFree();
if(
empty($freeInstallments) ||
$freeInstallments == UserSubscriptionEnum::YES
){
$freeInstallments = $poRepository->getFreeInstallment(
$userSubscription->getProductOffer()
);
}
$installmentsOptions = $pagarMeTransaction->calculateInstallments([
'amount' => $amountCents,
'free_installments' => $freeInstallments,
'max_installments' => $installments,
'interest_rate' => $paymentConfig->installmentInterest
]);
$amountCents = $installmentsOptions->{$installments}->amount;
}
$data = $transactionService->createTransactionEAD(
$userSubscription->getUser(),
$userSubscription->getUserCheckoutInfo(),
$amountCents,
$paymentMethod,
$installments,
$items,
$userCard,
null,
null,
null,
null,
$transactionOrigin,
"cron",
$recurrenceCycle,
$initiatedType,
$recurrenceModel
);
if(!empty($data['errorMessage'])){
$userSubscription->setErrorMessage($data['errorMessage']);
$this->em->flush();
}else if(
$isRenew &&
!empty($userSubscription->getDateLastPayment())
){
$notificationService->create(
$userSubscription->getProduct()->getUser(),
$userSubscription->getUser(),
NotificationEnum::ORIGIN_USER_SUBSCRIPTION_RENEW,
$userSubscription->getId()
);
$notificationService->create(
$userSubscription->getUser(),
$userSubscription->getProduct()->getUser(),
NotificationEnum::ORIGIN_USER_SUBSCRIPTION_RENEW,
$userSubscription->getId()
);
$usRepository->sendEmailUserSubscription(
$userSubscription,
UserSubscriptionEnum::SUBSCRIPTION_RENEW
);
}
}
}catch(\Exception $e){
$discordService->setMessage("{$this->eadDomain} - {$e->getMessage()}");
$discordService->sendDiscord();
}
}
}
}
$class = [
Banner::class,
Category::class,
City::class,
Country::class,
Course::class,
CourseCertificate::class,
CourseCertificateTemplate::class,
CourseTeam::class,
CourseTestimonial::class,
CycleItem::class,
Enrollment::class,
Exam::class,
ExamUser::class,
Faq::class,
Forum::class,
ForumCategory::class,
Group::class,
Lesson::class,
LessonModule::class,
LessonSupport::class,
LessonXLibrary::class,
Library::class,
Newsletter::class,
Page::class,
Product::class,
ProductCharge::class,
ProductCoupon::class,
ProductOffer::class,
ProductPage::class,
ProductSuggestion::class,
ProductTeam::class,
Question::class,
Receiver::class,
ReceiverDocument::class,
State::class,
Transaction::class,
TransactionItem::class,
User::class,
UserCheckoutInfo::class,
UserCustomField::class,
UserProfile::class,
UserSubscription::class,
Webhook::class,
Trash::class
];
foreach ($class as $key => $className) {
$repository = $this->em->getRepository($className);
$repository->deleteTrashCron();
}
return $this->eadResponse([ "success" => ErrorEnum::YES ]);
}
/**
* @Route(
* path = "/cron/delete/log/configuration/{authToken}",
* name = "deleteLogConfiguration",
* methods = {"GET"}
* )
*/
public function deleteLogConfiguration(Request $request)
{
//$token = $request->headers->get('X-AUTH-TOKEN');
$authToken = $request->get('authToken');
if($authToken != $this->generalService->getTokenCron()){
return $this->eadResponse(null, ErrorEnum::AUTH_INVALID);
}
/*if($request->get('clientToken') != $this->clientConfig->getToken()){
return $this->eadResponse(null, ErrorEnum::AUTH_INVALID);
}*/
$numberLogs = $this->emEadmin->getRepository(UserLog::class)->deleteLogConfiguration();
return $this->eadResponse([
"success" => ErrorEnum::YES,
"configurationNumberLogs" => $numberLogs,
]);
}
/**
* @Route(
* path = "/cron/callback/vimeo/{token}",
* name = "callbackVimeo",
* methods = {"GET"}
* )
*/
public function callbackVimeo(Request $request)
{
$accessToken = $request->get('token');
$vimeoClientAccessToken = $this->configuration->get("vimeo_client_access_token");
if(!empty($accessToken) && ($accessToken != $vimeoClientAccessToken)){
$this->configuration->set('vimeo_client_access_token', $accessToken);
}
return $this->eadResponse([ "success" => ErrorEnum::YES ]);
}
/**
* @Route(
* path = "/cron/token/access/rd",
* name = "getTokenAccessRd",
* methods = {"GET"}
* )
*/
public function getTokenAccessRd()
{
$rdStationService = $this->generalService->getService('Marketing\\RdStationService');
$rdstationAccess = $this->configuration->get('rdstation_access');
$rdstationCode = $this->configuration->get('rdstation_code');
if(!empty($rdstationAccess)){
$rdStationService->getTokenAccess($rdstationCode, true);
}
return $this->eadResponse([ "success" => ErrorEnum::YES ]);
}
/**
* @Route(
* path = "/cron/token/access/bling",
* name = "getTokenAccessBling",
* methods = {"GET"}
* )
*/
public function getTokenAccessBling()
{
$blingAuthorization = $this->generalService->getService(
'Transaction\\Invoice\\BlingAuthorization'
);
$blingToken = $this->configuration->get('bling_access');
$blingCode = $this->configuration->get('bling_code');
if(!empty($blingToken)){
$blingAuthorization->getTokenAccess($blingCode, true);
}
return $this->eadResponse([ "success" => ErrorEnum::YES ]);
}
/**
* @Route(
* path = "/cron/calculate/anticipation",
* name = "executeCalculateAnticipation",
* methods = {"GET"}
* )
*/
public function executeCalculateAnticipation()
{
$transactionRepository = $this->em->getRepository(Transaction::class);
$transactionRepository->updateTransactionPaidAndAnticipated();
return $this->eadResponse([ "success" => ErrorEnum::YES ]);
}
/**
* @Route(
* path = "/cron/ead/checkout/fee/{clientToken}",
* name = "getEADCheckoutFee",
* methods = {"GET"}
* )
*/
public function getEADCheckoutFee(Request $request)
{
$token = $request->headers->get('X-AUTH-TOKEN');
if($token != $this->generalService->getTokenCron()){
return $this->eadResponse(null, ErrorEnum::AUTH_INVALID);
}
if($request->get('clientToken') != $this->clientConfig->getToken()){
return $this->eadResponse(null, ErrorEnum::AUTH_INVALID);
}
$receiverRepository = $this->em->getRepository(Receiver::class);
$transactionRepository = $this->em->getRepository(Transaction::class);
$pagarMeReceiver = $this->generalService->getService('PagarMe\\PagarMeReceiver');
$card = TransactionEnum::PAYMENT_CARD;
$bill = TransactionEnum::PAYMENT_BILL;
$pix = TransactionEnum::PAYMENT_PIX;
$date1 = $request->get('date1');
if(!isset($date1) && empty($date1)){
$date1 = date('Y-m-d', strtotime("first day of last month"));
}
$date2 = $request->get('date2');
if(!isset($date2) && empty($date2)){
$date2 = date('Y-m-d', strtotime("last day of last month"));
}
$receivers = $receiverRepository->findBy([
"type" => ReceiverEnum::SCHOOL,
"accountType" => ReceiverEnum::EAD_CHECKOUT
]);
$data = [];
foreach ($receivers as $key => $receiver) {
$feeCard = $transactionRepository->getEADFeePaymentMethod(
$receiver->getId(),
$date1,
$date2,
$card
);
$feeBill = $transactionRepository->getEADFeePaymentMethod(
$receiver->getId(),
$date1,
$date2,
$bill
);
$feePix = $transactionRepository->getEADFeePaymentMethod(
$receiver->getId(),
$date1,
$date2,
$pix
);
$feeTransfer = $pagarMeReceiver->getTransferFee(
$receiver->getReceiverHash(),
$date1,
$date2
);
$rData = (object)[
"feeCard" => (float)$feeCard,
"feeBill" => (float)$feeBill,
"feePix" => (float)$feePix,
"feeTransfer" => $feeTransfer,
"receiverId" => $receiver->getReceiverHash(),
"id" => $receiver->getId(),
"deleted" => $receiver->isLive() ? ReceiverEnum::NO : ReceiverEnum::YES,
];
$data[] = $rData;
}
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/cron/delete/nfe/ead/checkout/{code}/{hash}",
* name = "deleteNfeEADCheckout",
* methods = {"GET"}
* )
*/
public function deleteNfeEADCheckout()
{
$token = $request->headers->get('AUTH-TOKEN');
if($token != $this->generalService->getTokenCron()){
return $this->eadResponse(null, ErrorEnum::AUTH_INVALID);
}
$nfeCode = $request->get('code');
$nfeHash = $request->get('hash');
$receiverNfeRepository = $this->em->getRepository(ReceiverNfe::class);
$receiverNfeRepository->deleteNfeEADCheckout($nfeCode, $nfeHash);
$this->em->flush();
return $this->eadResponse([ "success" => ErrorEnum::YES ]);
}
/**
* @Route(
* path = "/email/queue/{clientToken}",
* name = "executeCronListEmail",
* methods = {"GET"}
* )
*/
public function executeEmailQueue(Request $request)
{
$token = $request->headers->get('AUTH-TOKEN');
if($token != $this->generalService->getTokenCron()){
return $this->eadResponse(null, ErrorEnum::AUTH_INVALID);
}
if($request->get('clientToken') != $this->clientConfig->getToken()){
return $this->eadResponse(null, ErrorEnum::AUTH_INVALID);
}
$taskQueueRepository = $this->em->getRepository(TaskQueue::class);
$tasks = $taskQueueRepository->getTaskQueue();
$date = date('Y-m-d', strtotime("- 7 day"));
if($tasks){
foreach ($tasks as $key => $task) {
try{
if($this->configuration->checkModuleIsAbleOnPlan('notificationFunction')){
$user = $task->getUser();
if($user->getId() != UserEnum::YES && $user->isLive()){
if($task->getDate('Y-m-d') >= $date){
$taskQueueRepository->executeTask($task);
}
}
}
$this->em->remove($task);
}catch(\Exception $e){
$discordService = $this->generalService->getService('DiscordService');
$discordService->setChannel('debug');
$discordService->setMessage($e->getMessage());
$discordService->sendDiscord();
}
}
$this->em->flush();
}
if(count($tasks) == ServicesEnum::NO){
//delete cron
$cronService = $this->generalService->getService('Aws\\AwsEventBridge');
$cronService->deleteApiDestination(ServicesEnum::CRON_QUEUE);
}
return $this->eadResponse([ "success" => ErrorEnum::YES ]);
}
/**
* @Route(
* path = "/webhook/queue/send/{clientToken}",
* name = "sendWebhookQueue",
* methods = {"GET"}
* )
*/
public function sendWebhookQueue(Request $request)
{
$token = $request->headers->get('AUTH-TOKEN');
if($token != $this->generalService->getTokenCron()){
return $this->eadResponse(null, ErrorEnum::AUTH_INVALID);
}
if($request->get('clientToken') != $this->clientConfig->getToken()){
return $this->eadResponse(null, ErrorEnum::AUTH_INVALID);
}
$webhookQueueRepository = $this->em->getRepository(WebhookQueue::class);
$dataSend = $webhookQueueRepository->getWebhookQueue();
$webhookService = $this->generalService->getService('WebhookService');
foreach ($dataSend as $key => $webhookQueue) {
$webhookService->sendWebhookQueue($webhookQueue);
}
return $this->eadResponse([ "success" => ErrorEnum::YES ]);
}
/**
* @Route(
* path = "/metrics/{token}",
* name = "getMetricsEAD",
* methods = {"GET"}
* )
*/
public function getMetricsEAD(Request $request)
{
$token = $request->headers->get('AUTH-TOKEN');
/*if($token != $this->generalService->getTokenCron()){
return $this->eadResponse(null, ErrorEnum::AUTH_INVALID);
}
if($request->get('clientToken') != $this->clientConfig->getToken()){
return $this->eadResponse(null, ErrorEnum::AUTH_INVALID);
}*/
$metricsEadService = $this->generalService->getService('MetricsEadService');
$data = $metricsEadService->getMetrics();
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/remove/cron/{token}",
* name = "removeCron",
* methods = {"GET"}
* )
*/
public function removeCron(Request $request)
{
$token = $request->headers->get('AUTH-TOKEN');
if($token != $this->generalService->getTokenCron()){
return $this->eadResponse(null, ErrorEnum::AUTH_INVALID);
}
if($request->get('clientToken') != $this->clientConfig->getToken()){
return $this->eadResponse(null, ErrorEnum::AUTH_INVALID);
}
$cronService = $this->generalService->getService('Aws\\AwsEventBridge');
if($cronService->deleteApiDestinationByCronName(ServicesEnum::CRON_GENERAL)){
return $this->eadResponse([ "success" => ServicesEnum::YES ]);
}
return $this->eadResponse([ "error" => ServicesEnum::YES ]);
}
}