<?php
 namespace App\Webhook; class ApplyEntityOperations { private $entity; private $id; private $operation; private $webhookActions = null; public function __construct(string $entity, int $id, string $operation) { $this->entity = $entity; $this->id = $id; $this->operation = $operation; } private function addWebhookAction(string $entity, int $id, string $action) { $this->webhookActions[] = ['entity'=>$entity, 'id'=>$id, 'action'=>$action]; } public function determineActions() { if ($this->webhookActions!==null) { return ; } $this->webhookActions = []; switch ($this->entity) { case 'Corporate': case 'Customer': case 'Supplier': $this->determineActions_partners(); break; case 'Service': $this->determineActions_service(); break; case 'Invoice': $this->determineActions_invoice(); break; case 'InvoiceCashing': $this->determineActions_invoiceCashing(); break; case 'Document': $this->determineActions_document(); break; } return $this->webhookActions; } private function determineActions_partners() : void { switch ($this->operation) { case 'insert': $this->addWebhookAction($this->entity, $this->id, 'insert'); break; case 'update': case 'markDuplicate': case 'unmarkDuplicate': $this->addWebhookAction($this->entity, $this->id, 'update'); break; case 'delete': $this->addWebhookAction($this->entity, $this->id, 'delete'); break; } } private function determineActions_service() :void { switch ($this->operation) { case 'insert': $this->addWebhookAction($this->entity, $this->id, 'insert'); break; case 'update': $this->addWebhookAction($this->entity, $this->id, 'update'); $this->addWebhookAction($this->entity, $this->id, 'statusOp'); $this->addWebhookAction($this->entity, $this->id, 'statusFin'); break; case 'cancel': $this->addWebhookAction($this->entity, $this->id, 'update'); $this->addWebhookAction($this->entity, $this->id, 'statusOp'); break; case 'createPackage': $this->addWebhookAction($this->entity, $this->id, 'insert'); break; } } private function determineActions_invoice() : void { switch ($this->operation) { case 'insert': $this->addWebhookAction('Invoice', $this->id, 'insert'); break; case 'update': case 'updateIssued': $this->addWebhookAction('Invoice', $this->id, 'update'); break; case 'reverse': break; case 'cancel': $this->addWebhookAction('Invoice', $this->id, 'cancel'); break; case 'duplicate': $this->addWebhookAction('Invoice', $this->id, 'cancel'); $newInvoiceEnt = \Entity\Invoice::select($this->id); $this->addWebhookAction('Invoice', $newInvoiceEnt->duplicatedOnInvoiceId, 'insert'); break; case 'cumulate': $this->addWebhookAction('Invoice', $this->id, 'insert'); break; } $invEnt = \Entity\Invoice::select($this->id); $includedServices = []; foreach ($invEnt->rowsList as $rowDet) { if ($includedServices[$rowDet['idReservationService']]) { continue; } $this->addWebhookAction('Service', $rowDet['idReservationService'], 'statusFin'); $includedServices[$rowDet['idReservationService']] = true; } } private function determineActions_invoiceCashing() : void { switch ($this->operation) { case 'insert': case 'update': case 'cancel': $this->addWebhookAction($this->entity, $this->id, $this->operation); $invCashEnt = \Entity\InvoiceCashing::select($this->id); $this->addWebhookAction('Invoice', $invCashEnt->idInvoice, 'cashedStatus'); break; case 'block': case 'unblock': $this->addWebhookAction($this->entity, $this->id, 'blockStatus'); break; } if (in_array($this->operation, ['insert', 'update', 'cancel'])) { $invCashEnt = \Entity\InvoiceCashing::select($this->id); $includedServices = []; foreach ($invCashEnt->invoice->rowsList as $rowDet) { if ($includedServices[$rowDet['idReservationService']]) { continue; } $this->addWebhookAction('Service', $rowDet['idReservationService'], 'cashedStatus'); $includedServices[$rowDet['idReservationService']] = true; } } } private function determineActions_document() : void { switch ($this->operation) { case 'insert': $this->addWebhookAction('Document', $this->id, 'insert'); $docEnt = \Entity\Document::select($this->id); if ( $docEnt->pivotCode=='invoice' and !$docEnt->preview and \App\Config\IssueDocuments::getInstance()->isMainDocOnPivot($docEnt->docType) ) { $this->addWebhookAction('Invoice', $docEnt->idPivot, 'issue'); } if ( $docEnt->pivotCode=='cashing' and !$docEnt->preview and \App\Config\IssueDocuments::getInstance()->isMainDocOnPivot($docEnt->docType) ) { $this->addWebhookAction('InvoiceCashing', $docEnt->idPivot, 'issue'); } break; } } public function addEntityActionsToQueue() { $this->determineActions(); $webhookCfg = \App\Config\WebhookQueue::getInstance(); foreach ($this->webhookActions as $whAct) { if ($webhookCfg->getAutoQueueEntityAction($whAct['entity'], $whAct['action'])) { \App\Webhook\QueueManager::getInstance()->insertEntityAction($whAct['entity'], $whAct['id'], $whAct['action']); } } } } 