src/Subscriber/Payout/WebmasterPayoutSubscriber.php line 77

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Payout;
  3. use App\Dictionary\PayoutArea;
  4. use App\Entity\Billing;
  5. use App\Event\Payment\PayoutCreatedEvent;
  6. use App\Event\Payment\PayoutSealedEvent;
  7. use App\Lib\Payment\Admin\WebmasterPayoutItem;
  8. use App\Service\Payment\BillingService;
  9. use App\Service\Payment\PayoutService;
  10. use App\Service\User\AccountService;
  11. use App\Service\Webmaster\BalanceWebmasterService;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class WebmasterPayoutSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var BalanceWebmasterService
  17.      */
  18.     protected $service;
  19.     /**
  20.      * @var PayoutService
  21.      */
  22.     protected $payoutService;
  23.     /**
  24.      * @var AccountService
  25.      */
  26.     protected $accountService;
  27.     /**
  28.      * @var BillingService
  29.      */
  30.     protected $billingService;
  31.     /**
  32.      * WebmasterPayoutSubscriber constructor.
  33.      */
  34.     public function __construct(BalanceWebmasterService $serviceBillingService $billingService,
  35.         PayoutService $payoutServiceAccountService $accountService)
  36.     {
  37.         $this->service $service;
  38.         $this->payoutService $payoutService;
  39.         $this->accountService $accountService;
  40.         $this->billingService $billingService;
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             PayoutCreatedEvent::class => [
  46.                 ['onPayoutCreated'0],
  47.             ],
  48.             PayoutSealedEvent::class => [
  49.                 ['onPayoutSealed'0],
  50.             ],
  51.         ];
  52.     }
  53.     public function onPayoutCreated(PayoutCreatedEvent $event)
  54.     {
  55.         $payout $event->getPayout();
  56.         if (PayoutArea::WEBMASTER !== $payout->getArea()) {
  57.             return;
  58.         }
  59.         $this->service->markBalancesWithPayout($payout);
  60.     }
  61.     /**
  62.      * @throws \Doctrine\DBAL\Exception
  63.      * @throws \Doctrine\ORM\ORMException
  64.      * @throws \Doctrine\ORM\OptimisticLockException
  65.      */
  66.     public function onPayoutSealed(PayoutSealedEvent $event)
  67.     {
  68.         $payout $event->getPayout();
  69.         if (PayoutArea::WEBMASTER !== $payout->getArea() || !$payout->getIsSealed()) {
  70.             return;
  71.         }
  72.         $payouts $this->payoutService->getPayoutListWebmaster($payout'sum''DESC');
  73.         $startBillingId $this->billingService->getMaxBillingId() + 1;
  74.         $month $payout->getBillingMonth();
  75.         $month->modify('-1 month');
  76.         foreach ($payouts as $item) {
  77.             $bill $this->createBillForPayoutItem($item);
  78.             $bill->setPayout($payout);
  79.             $bill->setBillingId($startBillingId);
  80.             $bill->setInvoiceNumber($month->format('Ym').'.'.$startBillingId);
  81.             $this->billingService->storeEntity($billfalse);
  82.             ++$startBillingId;
  83.         }
  84.         $this->billingService->delayedFlush();
  85.     }
  86.     protected function createBillForPayoutItem(WebmasterPayoutItem $item): Billing
  87.     {
  88.         $bill = new Billing();
  89.         $account $this->accountService->findById($item->getAccountId());
  90.         $bill->setWebmaster($account->getWebmaster());
  91.         $bill->setPerson($account->getWebmaster()->getPerson());
  92.         $bill->setIsGross($item->isGross());
  93.         return $bill;
  94.     }
  95. }