src/Security/Voters/InvoiceVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Entity\CustomerQuotations;
  4. use App\Entity\Invoice;
  5. use App\Service\Constants\InvoiceStatus;
  6. use App\Service\Constants\InvoiceType;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. class InvoiceVoter extends Voter
  11. {
  12.     private Security $security;
  13.     public const CAN_ADD_PAYMENT 'invoice.can_add_payment';
  14.     public const CAN_CREATE_SALES 'invoice.can_create_sales';
  15.     public const CAN_UPDATE_INVOICE_JSON_DATA 'invoice.can_update_json_data';
  16.     public const CAN_BE_DELETED 'invoice.can_be_deleted';
  17.     public const CAN_BE_EDITED 'invoice.can_be_edited';
  18.     public const CAN_BE_CANCELED 'invoice.can_be_canceled';
  19.     public const CAN_BE_COPIED 'invoice.can_be_copied';
  20.     public const CAN_SEND_TO_CUSTOMER 'invoice.can_be_sent_to_customer';
  21.     public const VISIBLE_IN_CUSTOMER_PROF 'invoice.visible_in_customer_prof';
  22.     public const CAN_BE_UPDATED 'invoice.can_be_updated';
  23.     public const CAN_ROWS_BE_DELETED 'invoice.rows_can_be_deleted';
  24.     public const HAS_PDF 'invoice.has_pdf';
  25.     private const ATTRIBUTES = [
  26.         self::CAN_ADD_PAYMENT,
  27.         self::CAN_UPDATE_INVOICE_JSON_DATA,
  28.         self::CAN_BE_DELETED,
  29.         self::CAN_BE_EDITED,
  30.         self::CAN_BE_CANCELED,
  31.         self::CAN_BE_COPIED,
  32.         self::VISIBLE_IN_CUSTOMER_PROF,
  33.         self::CAN_SEND_TO_CUSTOMER,
  34.         self::CAN_BE_UPDATED,
  35.         self::CAN_ROWS_BE_DELETED,
  36.         self::HAS_PDF,
  37.         self::CAN_CREATE_SALES,
  38.     ];
  39.     public function __construct(Security $security)
  40.     {
  41.         $this->security $security;
  42.     }
  43.     protected function supports($attribute$subject): bool
  44.     {
  45.         return in_array($attributeself::ATTRIBUTES);
  46.     }
  47.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  48.     {
  49.         switch ($attribute) {
  50.             case self::CAN_ADD_PAYMENT:
  51.                 return $this->canAddPayment($subject);
  52.             case self::CAN_CREATE_SALES:
  53.                 return $this->canCreateSales($subject);
  54.             case self::CAN_UPDATE_INVOICE_JSON_DATA:
  55.             case self::CAN_BE_DELETED:
  56.             case self::CAN_BE_EDITED:
  57.                 return $this->canBeEdited($subject);
  58.             case self::CAN_BE_CANCELED:
  59.                 return $this->canBeCanceled($subject);
  60.             case self::CAN_BE_COPIED:
  61.                 return $this->canBeCopied($subject);
  62.             case self::VISIBLE_IN_CUSTOMER_PROF:
  63.                 return $this->visibleInCustomerProfile($subject);
  64.             case self::CAN_SEND_TO_CUSTOMER:
  65.                 return $this->canSendToCustomer($subject);
  66.             case self::CAN_BE_UPDATED:
  67.                 return $this->canBeUpdated($subject);
  68.             case self::CAN_ROWS_BE_DELETED:
  69.                 return $this->canRowsBeDeleted($subject);
  70.             case self::HAS_PDF:
  71.                 return $this->hasPdf($subject);
  72.         }
  73.         throw new \LogicException('Invalid attribute: ' $attribute);
  74.     }
  75.     private function canAddPayment(Invoice $invoice): bool
  76.     {
  77.         if (!$this->security->isGranted('ROLE_SALES_MANAGER')) {
  78.             return false;
  79.         }
  80.         if ($invoice->getType() == InvoiceType::INVOICE_TYPE_SALES && (!$invoice->getSenderAddress() || !$invoice->getDeliveryAddress())) {
  81.             return false;
  82.         }
  83.         if (!$this->canBeUpdated($invoice)) {
  84.             return false;
  85.         }
  86.         return true;
  87.     }
  88.     private function canBeEdited(Invoice $invoice): bool
  89.     {
  90.         if (!$this->security->isGranted('ROLE_SALES_MANAGER')) {
  91.             return false;
  92.         }
  93.         if (!$this->canBeUpdated($invoice)) {
  94.             return false;
  95.         }
  96.         return !$invoice->getIsSent();
  97.     }
  98.     private function canBeCanceled(Invoice $invoice): bool
  99.     {
  100.         if (!$this->security->isGranted('ROLE_SALES_MANAGER')) {
  101.             return false;
  102.         }
  103.         if ($invoice->getInvoiceDirection() == Invoice::INVOICE_DIRECTION_INCOMING && !$invoice->getQuotation()) {
  104.             return false;
  105.         }
  106.         if (!$this->canBeUpdated($invoice)) {
  107.             return false;
  108.         }
  109.         return true;
  110.     }
  111.     private function visibleInCustomerProfile(Invoice $invoice): bool
  112.     {
  113.         if (!$invoice->getIsSent()) {
  114.             return false;
  115.         }
  116.         if (in_array($invoice->getStatus(), [
  117.             InvoiceStatus::INVOICE_STATUS_CANCELED
  118.         ])) {
  119.             return false;
  120.         }
  121.         return true;
  122.     }
  123.     private function canSendToCustomer(Invoice $invoice): bool
  124.     {
  125.         if ($invoice->getInvoiceDirection() === Invoice::INVOICE_DIRECTION_INCOMING) {
  126.             return false;
  127.         }
  128.         if (!$this->canBeUpdated($invoice)) {
  129.             return false;
  130.         }
  131.         return !$invoice->getIsSent();
  132.     }
  133.     private function canBeCopied(Invoice $invoice): bool
  134.     {
  135.         return !$invoice->getIsWebShopRelated();
  136.     }
  137.     private function canBeUpdated(Invoice $invoice): bool
  138.     {
  139.         if (count($invoice->getBatchInvoices())) {
  140.             return false;
  141.         }
  142.         if (in_array($invoice->getStatus(), [
  143.             InvoiceStatus::INVOICE_STATUS_CANCELED,
  144.             InvoiceStatus::INVOICE_STATUS_ARCHIVED,
  145.         ])) {
  146.             return false;
  147.         }
  148.         return true;
  149.     }
  150.     private function canRowsBeDeleted(Invoice $invoice): bool
  151.     {
  152.         if ($invoice->getType() === InvoiceType::INVOICE_TYPE_ADVANCE) {
  153.             return false;
  154.         }
  155.         return true;
  156.     }
  157.     private function hasPdf(Invoice $invoice): bool
  158.     {
  159.         if ($invoice->getInvoiceDirection() !== Invoice::INVOICE_DIRECTION_OUTGOING) {
  160.             return false;
  161.         }
  162.         if (!$invoice->getCustomer()) {
  163.             return false;
  164.         }
  165.         if (!$invoice->getCustomerData()) {
  166.             return false;
  167.         }
  168.         return true;
  169.     }
  170.     private function createdFromAdvance(Invoice $invoice): bool
  171.     {
  172.         foreach ($invoice->getPrepaymentInvoices() as $prepaymentInvoice) {
  173.             if ($prepaymentInvoice->getStatus() === InvoiceStatus::INVOICE_STATUS_ARCHIVED) {
  174.                 return true;
  175.             }
  176.         }
  177.         return false;
  178.     }
  179.     private function canCreateSales(Invoice $invoice): bool
  180.     {
  181.         if (
  182.             $invoice->getIsBatchAdvance()
  183.             && !in_array($invoice->getStatus(), [
  184.                 InvoiceStatus::INVOICE_STATUS_ARCHIVED,
  185.                 InvoiceStatus::INVOICE_STATUS_CANCELED
  186.             ])
  187.         ) {
  188.             return true;
  189.         }
  190.         return false;
  191.     }
  192. }