src/Security/Voters/PriceInquiryVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Entity\PriceInquiry;
  4. use App\Entity\CustomerQuotations;
  5. use App\Entity\User;
  6. use App\Service\Api\CountriesService;
  7. use App\Service\Constants\Defaults;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\Security;
  11. class PriceInquiryVoter extends Voter
  12. {
  13.     private Security $security;
  14.     private CountriesService $countriesService;
  15.     public const CAN_EDIT                   'inquiry.can_edit';
  16.     public const CAN_FILL_INQUIRY           'inquiry.can.fill';
  17.     public const CAN_SEE_PRICES             'inquiry.can.see_prices';
  18.     public const CAN_UPDATE                 'inquiry.can_update';
  19.     public const CAN_SEND_NOTIFICATION      'inquiry.can.send_notification';
  20.     public const CAN_BE_SHOWN_IN_PROPOSAL   'inquiry.can.be_shown_in_proposal';
  21.     public const CAN_BE_REJECTED            'inquiry.can.be_rejected';
  22.     public const CAN_BE_DUPLICATED          'inquiry.can.be_duplicated';
  23.     public const CAN_CHANGE_COMMISSION      'inquiry.commission.can_change';
  24.     public const CAN_VERIFY_MFM             'inquiry.can.verify';
  25.     public const CAN_VERIFY_SM              'inquiry.can.verify.sm';
  26.     public const CAN_UPDATE_CUSTOMER_PRICES 'inquiry.can.update.customer.prices';
  27.     public const CAN_UPDATE_MF_PRICES       'inquiry.can.update.mf.prices';
  28.     public const CAN_OPEN_PRICES_FORM       'inquiry.can.open.prices.form';
  29.     public const CAN_REMOVE_MFM_VERIFICATION        'inquiry.can.remove_mfm_verification';
  30.     public const CAN_REMOVE_SM_VERIFICATION         'inquiry.can.remove_sm_verification';
  31.     public const CAN_UPDATE_CUSTOMS_PRICES 'inquiry.can.update.customs.prices';
  32.     private const ATTRIBUTES = [
  33.         self::CAN_EDIT,
  34.         self::CAN_FILL_INQUIRY,
  35.         self::CAN_SEE_PRICES,
  36.         self::CAN_UPDATE,
  37.         self::CAN_SEND_NOTIFICATION,
  38.         self::CAN_BE_SHOWN_IN_PROPOSAL,
  39.         self::CAN_BE_REJECTED,
  40.         self::CAN_BE_DUPLICATED,
  41.         self::CAN_CHANGE_COMMISSION,
  42.         self::CAN_VERIFY_MFM,
  43.         self::CAN_VERIFY_SM,
  44.         self::CAN_UPDATE_CUSTOMER_PRICES,
  45.         self::CAN_UPDATE_MF_PRICES,
  46.         self::CAN_OPEN_PRICES_FORM,
  47.         self::CAN_REMOVE_MFM_VERIFICATION,
  48.         self::CAN_REMOVE_SM_VERIFICATION,
  49.         self::CAN_UPDATE_CUSTOMS_PRICES,
  50.     ];
  51.     public function __construct(
  52.         Security $security,
  53.         CountriesService $countriesService
  54.     )
  55.     {
  56.         $this->security $security;
  57.         $this->countriesService $countriesService;
  58.     }
  59.     protected function supports($attribute$subject): bool
  60.     {
  61.         return in_array($attributeself::ATTRIBUTES);
  62.     }
  63.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  64.     {
  65.         $user $token->getUser();
  66.         switch ($attribute) {
  67.             case self::CAN_EDIT:
  68.                 return $this->canEdit($subject);
  69.             case self::CAN_FILL_INQUIRY:
  70.                 return $this->canBeFilled($subject);
  71.             case self::CAN_SEE_PRICES:
  72.                 return $this->canSeePrices($subject$user);
  73.             case self::CAN_UPDATE:
  74.                 return $this->canUpdate($subject$user);
  75.             case self::CAN_SEND_NOTIFICATION:
  76.                 return $this->canSendNotification($subject);
  77.             case self::CAN_BE_SHOWN_IN_PROPOSAL:
  78.                 return $this->canBeShownInProposal($subject);
  79.             case self::CAN_BE_REJECTED:
  80.                 return $this->canBeRejected($subject);
  81.             case self::CAN_BE_DUPLICATED:
  82.                 return $this->canBeDuplicated($subject);
  83.             case self::CAN_CHANGE_COMMISSION:
  84.                 return $this->canChangeCommission($subject);
  85.             case self::CAN_VERIFY_MFM:
  86.                 return $this->canVerifyMFM($subject);
  87.             case self::CAN_VERIFY_SM:
  88.                 return $this->canVerifySM($subject);
  89.             case self::CAN_UPDATE_CUSTOMER_PRICES:
  90.                 return $this->canUpdateCustomerPrices($subject);
  91.             case self::CAN_UPDATE_CUSTOMS_PRICES:
  92.                 return $this->canUpdateCustomsPrices($subject);
  93.             case self::CAN_UPDATE_MF_PRICES:
  94.                 return $this->canUpdateManufacturerPrices($subject);
  95.             case self::CAN_OPEN_PRICES_FORM:
  96.                 return $this->canOpenPricesForm($subject);
  97.             case self::CAN_REMOVE_MFM_VERIFICATION:
  98.                 return $this->canRemoveMfmVerification($subject);
  99.             case self::CAN_REMOVE_SM_VERIFICATION:
  100.                 return $this->canRemoveSmVerification($subject);
  101.         }
  102.         throw new \LogicException('Invalid attribute: '.$attribute);
  103.     }
  104.     private function canEdit(PriceInquiry $inquiry): bool
  105.     {
  106.         if ( !$this->security->isGranted('ROLE_CAN_CREATE_INQUIRIES') ) {
  107.             return false;
  108.         }
  109.         // temporary disabled. 27.01.21 / Ivars
  110.         /*
  111.         if ( $inquiry->getOfferVerifiedSM() ) {
  112.             return false;
  113.         }
  114.         */
  115.         return true;
  116.     }
  117.     private function canUpdate(PriceInquiry $inquiry): bool
  118.     {
  119.         if (in_array($inquiry->getQuotation()->getStatus(), [CustomerQuotations::STATUS_REJECTED])) {
  120.             return false;
  121.         }
  122.         if ( $this->security->isGranted('ROLE_INQUIRY_UPDATE') ) {
  123.             return true;
  124.         }
  125.         return false;
  126.     }
  127.     private function canBeFilled(PriceInquiry $inquiry): bool
  128.     {
  129.         if ( in_array($inquiry->getStatus(),[PriceInquiry::STATUS_DRAFT,PriceInquiry::STATUS_SENT,PriceInquiry::STATUS_OPEN]) ) {
  130.             return true;
  131.         }
  132.         return false;
  133.     }
  134.     private function canBeRejected(PriceInquiry $inquiry): bool
  135.     {
  136.         if ($inquiry->getStatus() == PriceInquiry::STATUS_OPEN) {
  137.             return true;
  138.         }
  139.         if ($inquiry->getStatus() == PriceInquiry::STATUS_OFFER && $this->canUpdate($inquiry)) {
  140.             return true;
  141.         }
  142.         return false;
  143.     }
  144.     private function canSeePrices(PriceInquiry $inquiry$user): bool
  145.     {
  146.         if (!$user instanceof User) {
  147.             // the user must be logged in; if not, deny access
  148.             return false;
  149.         }
  150.         return true;
  151.     }
  152.     private function canSendNotification(PriceInquiry $inquiry): bool
  153.     {
  154.         if ( !$this->security->isGranted('ROLE_MANUFACTURING_MANAGER') ) {
  155.             return false;
  156.         }
  157.         if ( !in_array($inquiry->getStatus(), [PriceInquiry::STATUS_OFFER]) ) {
  158.             return false;
  159.         }
  160.         if ( !in_array($inquiry->getQuotation()->getStatus(), [CustomerQuotations::STATUS_INQUIRIES_SENTCustomerQuotations::STATUS_OFFERS_RECEIVEDCustomerQuotations::STATUS_OFFERS_PARTIALLY_RECEIVED]) ) {
  161.             return false;
  162.         }
  163.         return true;
  164.     }
  165.     private function canBeShownInProposal(PriceInquiry $inquiry): bool
  166.     {
  167.         if ($inquiry->getHideInProposal()) {
  168.             return false;
  169.         }
  170.         if ($inquiry->getOfferVerified() === false) {
  171.             return false;
  172.         }
  173.         if (!$this->security->isGranted('ROLE_SALES_MANAGER') && !$inquiry->getOfferVerifiedSM()) {
  174.             return false;
  175.         }
  176.         //if delivery prices are greater then 0
  177.         if ($inquiry->getIsDeliveryPrices()) {
  178.             return true;
  179.         }
  180.         return false;
  181.     }
  182.     private function canBeDuplicated(PriceInquiry $inquiry): bool
  183.     {
  184.         if( !in_array($inquiry->getQuotation()->getStatus(), [CustomerQuotations::STATUS_INQUIRIES_SENTCustomerQuotations::STATUS_OFFERS_RECEIVEDCustomerQuotations::STATUS_OFFERS_PARTIALLY_RECEIVEDCustomerQuotations::STATUS_PROPOSAL_SENT]) ) {
  185.             return false;
  186.         }
  187.         if ($inquiry->getStatus() === PriceInquiry::STATUS_OFFER) {
  188.             return true;
  189.         }
  190.         return false;
  191.     }
  192.     private function canChangeCommission(PriceInquiry $inquiry): bool
  193.     {
  194.         if ( !$this->security->isGranted('ROLE_CAN_CHANGE_OFFER_COMMISSION') ) {
  195.             return false;
  196.         }
  197.         $quotation $inquiry->getQuotation();
  198.         if ( !$this->security->isGranted('quotation.commission.can_change'$quotation) ) {
  199.             return false;
  200.         }
  201.         return true;
  202.     }
  203.   
  204.     private function canVerifyMFM(PriceInquiry $inquiry): bool
  205.     {
  206.         if ( !$this->security->isGranted('ROLE_MANUFACTURING_MANAGER') ) {
  207.             return false;
  208.         }
  209.         if ( $inquiry->getOfferVerified() ) {
  210.             return false;
  211.         }
  212.         if ( !in_array($inquiry->getStatus(), [PriceInquiry::STATUS_OFFERPriceInquiry::STATUS_ACCEPTED]) ) {
  213.             return false;
  214.         }
  215.         if ( !$inquiry->getCommission() ) {
  216.             return false;
  217.         }
  218.         if ( !$inquiry->getIsDeliveryPrices() ) {
  219.             return false;
  220.         }
  221.         return true;
  222.     }
  223.     private function canVerifySM(PriceInquiry $inquiry): bool
  224.     {
  225.         if ( !$this->security->isGranted('ROLE_SALES_MANAGER') ) {
  226.             return false;
  227.         }
  228.         if ( !$inquiry->getOfferVerified() ) {
  229.             return false;
  230.         }
  231.         if ( $inquiry->getOfferVerifiedSM() ) {
  232.             return false;
  233.         }
  234.         if ( !in_array($inquiry->getStatus(), [PriceInquiry::STATUS_OFFERPriceInquiry::STATUS_ACCEPTED]) ) {
  235.             return false;
  236.         }
  237.         return true;
  238.     }
  239.     private function canUpdateCustomerPrices(PriceInquiry $inquiry) : bool
  240.     {
  241.         if ( !$inquiry->getCustomerPriceOffer() ) {
  242.             return false;
  243.         }
  244.         if ( $inquiry->getOfferVerifiedSM() ) {
  245.             return false;
  246.         }
  247.         if ( $this->security->isGranted('ROLE_SALES_MANAGER') || $this->security->isGranted('ROLE_SUPER_PRICE_EDITOR')) {
  248.             return true;
  249.         }
  250.         return false;
  251.     }
  252.     private function canUpdateManufacturerPrices(PriceInquiry $inquiry) : bool
  253.     {
  254.         if ( !in_array($inquiry->getStatus(), [PriceInquiry::STATUS_OFFERPriceInquiry::STATUS_ACCEPTED]) ) {
  255.             return false;
  256.         }
  257.         if ( $inquiry->getOfferVerified() ) {
  258.             return false;
  259.         }
  260.         if ( $this->security->isGranted('ROLE_MANUFACTURING_MANAGER') || $this->security->isGranted('ROLE_SUPER_PRICE_EDITOR')) {
  261.             return true;
  262.         }
  263.         return false;
  264.     }
  265.     private function canOpenPricesForm(PriceInquiry $inquiry) : bool
  266.     {
  267.         if ( in_array($inquiry->getStatus(), [PriceInquiry::STATUS_DRAFT]) ) {
  268.             return false;
  269.         }
  270.         // commenting this out allowing open price form for fully accepted offers, so managers can see comission. 29.01.22 / Ivars
  271.         /*
  272.         if ( $this->canUpdateCustomerPrices($inquiry) || $this->canUpdateManufacturerPrices($inquiry) ) {
  273.             return true;
  274.         }
  275.         */
  276.         return true;
  277.     }
  278.     private function canRemoveMfmVerification(PriceInquiry $inquiry) : bool
  279.     {
  280.         // MFM verification cant be removed if there are active SM verification
  281.         if ( $inquiry->getOfferVerifiedSM() ) {
  282.             return false;
  283.         }
  284.         // if its already an order - cant update
  285.         if ( $inquiry->getOrderQuotation() ) {
  286.             return false;
  287.         }
  288.         if ( !$this->security->isGranted('ROLE_INQUIRY_CAN_REMOVE_MFM_VERIFY') ) {
  289.             return false;
  290.         }
  291.         return true;
  292.     }
  293.     private function canRemoveSmVerification(PriceInquiry $inquiry) : bool
  294.     {
  295.         // can be removed only if its verified before
  296.         if ( !$inquiry->getOfferVerifiedSM() ) {
  297.             return false;
  298.         }
  299.         // if its already an order - cant update
  300.         if ( $inquiry->getOrderQuotation() ) {
  301.             return false;
  302.         }
  303.         if ( !$this->security->isGranted('ROLE_INQUIRY_CAN_REMOVE_SM_VERIFY') ) {
  304.             return false;
  305.         }
  306.         return true;
  307.     }
  308.     private function canUpdateCustomsPrices(PriceInquiry $inquiry): bool
  309.     {
  310.         if ( !$inquiry->getCustomerPriceOffer() || !$inquiry->getCustomerDeliveryPrices() ) {
  311.             return false;
  312.         }
  313.         $pickupAddressDetails $inquiry->getManufacturerAddress();
  314.         $deliveryAddress $inquiry->getQuotation()->getDeliveryAddress();
  315.         if (
  316.             $this->countriesService->isInsideEu($pickupAddressDetails['country'] ?? Defaults::DEFAULT_COUNTRY_CODE) &&
  317.             $this->countriesService->isInsideEu($deliveryAddress['country'] ?? Defaults::DEFAULT_COUNTRY_CODE)
  318.         ) {
  319.             return false;
  320.         }
  321.         return true;
  322.     }
  323. }