src/Security/Voters/QuotationVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Entity\CustomerQuotations;
  4. use App\Entity\PriceInquiry;
  5. use App\Service\WebPop\WebShopService;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. class QuotationVoter extends Voter
  10. {
  11.     private Security $security;
  12.     public const CAN_OPEN_PORPOSAL 'quotation.proposal.can_open';
  13.     public const CAN_OPEN_MF_OFFERS 'quotation.mf_offers.can_open';
  14.     public const CAN_CHANGE_COMMISSION 'quotation.commission.can_change';
  15.     public const CAN_DELETE_QUOTATION 'quotation.can_delete';
  16.     public const CAN_OPEN_QUOTATION 'quotation.can_open';
  17.     public const CAN_CREATE_INQUIRY 'quotation.inquiry.can_create';
  18.     public const CAN_GENERATE_INQUIRIES 'quotation.inquiry.can_generate';
  19.     public const IS_CUSTOMER_QUOTATION 'quotation.is_customer';
  20.     public const CAN_BE_UPDATED_IN_CALCULATOR 'quotation.can_be_updated_in_calculator';
  21.     public const CAN_BE_UPDATED_IN_WEB_POP 'quotation.can_be_updated_in_web_pop';
  22.     public const CAN_CHANGE_MANAGER 'quotation.can_change_manager';
  23.     public const CAN_ROLLBACK 'quotation.can_rollback';
  24.     public const CAN_CREATE_CUSTOMER_FROM_SHOP_INVOICE 'quotation.customer.create_from_shop_invoice';
  25.     private const ATTRIBUTES = [
  26.         self::CAN_OPEN_PORPOSAL,
  27.         self::CAN_OPEN_MF_OFFERS,
  28.         self::CAN_DELETE_QUOTATION,
  29.         self::CAN_CHANGE_COMMISSION,
  30.         self::CAN_CREATE_INQUIRY,
  31.         self::CAN_OPEN_QUOTATION,
  32.         self::CAN_GENERATE_INQUIRIES,
  33.         self::IS_CUSTOMER_QUOTATION,
  34.         self::CAN_BE_UPDATED_IN_CALCULATOR,
  35.         self::CAN_BE_UPDATED_IN_WEB_POP,
  36.         self::CAN_CHANGE_MANAGER,
  37.         self::CAN_ROLLBACK,
  38.         self::CAN_CREATE_CUSTOMER_FROM_SHOP_INVOICE
  39.     ];
  40.     private WebShopService $shopService;
  41.     public function __construct(
  42.         Security $security,
  43.         WebShopService $shopService
  44.     )
  45.     {
  46.         $this->security $security;
  47.         $this->shopService $shopService;
  48.     }
  49.     protected function supports($attribute$subject): bool
  50.     {
  51.         return in_array($attributeself::ATTRIBUTES);
  52.     }
  53.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  54.     {
  55.         switch ($attribute) {
  56.             case self::CAN_OPEN_PORPOSAL:
  57.                 return $this->canOpenProposal($subject);
  58.             case self::CAN_OPEN_MF_OFFERS:
  59.                 return $this->canOpenMfOffers($subject);
  60.             case self::CAN_DELETE_QUOTATION:
  61.                 return $this->canDeleteQuotation($subject);
  62.             case self::CAN_CHANGE_COMMISSION:
  63.                 return $this->canChangeCommission($subject);
  64.             case self::CAN_CREATE_INQUIRY:
  65.                 return $this->canCreateInquiry($subject);
  66.             case self::CAN_OPEN_QUOTATION:
  67.                 return $this->canOpenQuotation($subject);
  68.             case self::CAN_GENERATE_INQUIRIES:
  69.                 return $this->canGenerateInquiries($subject);
  70.             case self::IS_CUSTOMER_QUOTATION:
  71.                 return $this->isCustomerQuotation($subject);
  72.             case self::CAN_BE_UPDATED_IN_CALCULATOR:
  73.             case self::CAN_BE_UPDATED_IN_WEB_POP:
  74.                 return $this->canBeUpdatedInCalculator($subject);
  75.             case self::CAN_CHANGE_MANAGER:
  76.                 return $this->canChangeManager($subject);
  77.             case self::CAN_ROLLBACK:
  78.                 return $this->canRollback($subject);
  79.             case self::CAN_CREATE_CUSTOMER_FROM_SHOP_INVOICE:
  80.                 return $this->canCreateCustomerFromShopInvoice($subject);
  81.         }
  82.         throw new \LogicException('Invalid attribute: ' $attribute);
  83.     }
  84.     private function isCustomerQuotation(CustomerQuotations $quotation): bool
  85.     {
  86.         if ($quotation->getCreatedBy() && $quotation->getCreatedBy()->getCustomer()) {
  87.             return true;
  88.         }
  89.         return false;
  90.     }
  91.     private function canOpenProposal(CustomerQuotations $quotation): bool
  92.     {
  93.         foreach ($quotation->getPriceInquiries() as $inquiry) {
  94.             if (!$inquiry->getOfferVerifiedSM()) {
  95.                 if (!$this->security->isGranted('ROLE_SALES_MANAGER')) {
  96.                     continue;
  97.                 }
  98.             }
  99.             if (in_array($inquiry->getStatus(), [PriceInquiry::STATUS_OFFERPriceInquiry::STATUS_DECLINEDPriceInquiry::STATUS_ACCEPTEDPriceInquiry::STATUS_HOLD])) {
  100.                 return true;
  101.             }
  102.         }
  103.         return false;
  104.     }
  105.     private function canOpenQuotation(CustomerQuotations $quotation): bool
  106.     {
  107.         if ($this->security->isGranted('ROLE_CAN_SEE_ALL_QUOTATIONS')) {
  108.             return true;
  109.         }
  110.         if ($this->security->getUser() !== null && $quotation->getManager() == $this->security->getUser()) {
  111.             return true;
  112.         }
  113.         if ($this->security->isGranted('ROLE_CAN_CREATE_INQUIRIES')
  114.             && !in_array($quotation->getStatus(), [CustomerQuotations::STATUS_DRAFT,
  115.                 CustomerQuotations::STATUS_NEW,
  116.                 CustomerQuotations::STATUS_PROCESSING,
  117.                 CustomerQuotations::STATUS_FINISHED,
  118.                 CustomerQuotations::STATUS_REJECTED,
  119.                 CustomerQuotations::STATUS_DELETED])) {
  120.             return true;
  121.         }
  122.         if ($quotation->getManager() === null) {
  123.             return true;
  124.         }
  125.         return false;
  126.     }
  127.     private function canChangeCommission(CustomerQuotations $quotation): bool
  128.     {
  129.         if (!$this->security->isGranted('ROLE_CAN_CHANGE_OFFER_COMMISSION')) {
  130.             return false;
  131.         }
  132.         if (!in_array($quotation->getStatus(), [CustomerQuotations::STATUS_READYCustomerQuotations::STATUS_INQUIRIES_SENTCustomerQuotations::STATUS_OFFERS_RECEIVEDCustomerQuotations::STATUS_OFFERS_PARTIALLY_RECEIVED])) {
  133.             return false;
  134.         }
  135.         return true;
  136.     }
  137.     private function canDeleteQuotation(CustomerQuotations $quotation): bool
  138.     {
  139.         if (!$this->security->isGranted('ROLE_QUOTATION_CAN_DELETE')) {
  140.             return false;
  141.         }
  142.         if (!in_array($quotation->getStatus(), [CustomerQuotations::STATUS_DRAFTCustomerQuotations::STATUS_NEW])) {
  143.             return false;
  144.         }
  145.         return true;
  146.     }
  147.     private function canCreateInquiry(CustomerQuotations $quotation): bool
  148.     {
  149.         if (!$this->security->isGranted('ROLE_CAN_CREATE_INQUIRIES')) {
  150.             return false;
  151.         }
  152.         if (!in_array($quotation->getStatus(), [CustomerQuotations::STATUS_READYCustomerQuotations::STATUS_INQUIRIES_SENTCustomerQuotations::STATUS_OFFERS_RECEIVEDCustomerQuotations::STATUS_OFFERS_PARTIALLY_RECEIVED])) {
  153.             return false;
  154.         }
  155.         return true;
  156.     }
  157.     private function canGenerateInquiries(CustomerQuotations $quotation): bool
  158.     {
  159.         if ($quotation->getManager() == $this->security->getUser() && in_array($quotation->getStatus(), [CustomerQuotations::STATUS_NEWCustomerQuotations::STATUS_READY])) {
  160.             return true;
  161.         }
  162.         return false;
  163.     }
  164.     private function canBeUpdatedInCalculator(CustomerQuotations $quotation): bool
  165.     {
  166.         if ($quotation->getStatus() === CustomerQuotations::STATUS_DRAFT) {
  167.             return true;
  168.         }
  169.         return false;
  170.     }
  171.     private function canChangeManager(CustomerQuotations $quotation): bool
  172.     {
  173.         if (!$this->security->isGranted('ROLE_QUOTATION_CAN_CHANGE_MANAGER')) {
  174.             return false;
  175.         }
  176. //        if ($quotation->getManager()) {
  177. //            return true;
  178. //        }
  179.         return true;
  180.     }
  181.     private function canOpenMfOffers(CustomerQuotations $quotation): bool
  182.     {
  183.         if (!$this->security->isGranted('ROLE_MANUFACTURING_MANAGER')) {
  184.             return false;
  185.         }
  186.         if (count($quotation->getPriceInquiries()) > 0) {
  187.             return true;
  188.         }
  189.         return false;
  190.     }
  191.     private function canRollback(CustomerQuotations $quotation): bool
  192.     {
  193.         if(!$this->security->isGranted('ROLE_CAN_ROLLBACK_QUOTATION')) {
  194.             return false;
  195.         }
  196.         if (in_array($quotation->getStatus(), [
  197.             CustomerQuotations::STATUS_DRAFT,
  198.             CustomerQuotations::STATUS_NEW,
  199. //                CustomerQuotations::STATUS_REJECTED,
  200.             CustomerQuotations::STATUS_DELETED,
  201.             CustomerQuotations::STATUS_HOLD
  202.         ])) {
  203.             return false;
  204.         }
  205.         return true;
  206.     }
  207.     private function canCreateCustomerFromShopInvoice(CustomerQuotations $quotation): bool
  208.     {
  209.         if (!$this->shopService->findWebShopInvoice($quotation)) {
  210.             return false;
  211.         }
  212.         if ($quotation->getCustomer()) {
  213.             return false;
  214.         }
  215.         return true;
  216.     }
  217. }