<?php
namespace App\Security\Voters;
use App\Entity\CustomerQuotations;
use App\Entity\OrderClaim;
use App\Repository\PackagingTypeRepository;
use App\Service\WorkflowHelper;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class OrderVoter extends Voter
{
private Security $security;
public const CAN_RATE = 'order.can_rate';
public const CAN_DO_CLAIM = 'order.can_claim';
public const CAN_RESOLVE_CLAIM = 'order.can_resolve_claim';
public const CAN_VIEW = 'order.can_view_claim';
public const CAN_REORDER = 'order.can_reorder';
public const CAN_ADD_BATCH = 'order.can_add_batch';
public const CAN_GENERATE_PREPAYMENT_INVOICE = 'order.can_generate_prepayment_invoice';
public const CAN_GENERATE_BATCHES_INVOICE = 'order.can_generate_batches_invoice';
public const IS_STOCK_PRODUCT = 'order.is_stock_product';
private const ATTRIBUTES = [
self::CAN_RATE,
self::CAN_DO_CLAIM,
self::CAN_RESOLVE_CLAIM,
self::CAN_VIEW,
self::CAN_REORDER,
self::CAN_ADD_BATCH,
self::CAN_GENERATE_PREPAYMENT_INVOICE,
self::CAN_GENERATE_BATCHES_INVOICE,
self::IS_STOCK_PRODUCT,
];
private PackagingTypeRepository $packagingTypeRepo;
private WorkflowHelper $workflowHelper;
public function __construct(
Security $security,
WorkflowHelper $workflowHelper,
PackagingTypeRepository $packagingTypeRepo
)
{
$this->packagingTypeRepo = $packagingTypeRepo;
$this->security = $security;
$this->workflowHelper = $workflowHelper;
}
protected function supports($attribute, $subject): bool
{
return in_array($attribute, self::ATTRIBUTES);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
switch ($attribute) {
case self::CAN_RATE:
return $this->catRate($subject);
case self::CAN_DO_CLAIM:
return $this->canClaim($subject);
case self::CAN_RESOLVE_CLAIM:
return $this->canResolveClaim($subject);
case self::CAN_VIEW:
return $this->canViewClaim($subject);
case self::CAN_REORDER:
return $this->canReorder($subject);
case self::CAN_ADD_BATCH:
return $this->canAddBatch($subject);
case self::CAN_GENERATE_PREPAYMENT_INVOICE:
return $this->canGeneratePrePaymentInvoice($subject);
case self::CAN_GENERATE_BATCHES_INVOICE:
return $this->canGenerateBatchesInvoice($subject);
case self::IS_STOCK_PRODUCT:
return $this->isStockProduct($subject);
}
throw new \LogicException('Invalid attribute: ' . $attribute);
}
private function catRate(CustomerQuotations $order): bool
{
if ($this->security->isGranted('user.is_manufacturer')) {
return false;
}
if ($order->getOrderRating()) {
return false;
}
if ($order->getStatus() == CustomerQuotations::STATUS_FINISHED) {
return true;
}
return false;
}
private function canClaim(CustomerQuotations $order): bool
{
if ($this->security->isGranted('user.is_manufacturer')) {
return false;
}
if ($order->getClaim()) {
return false;
}
if ($order->getStatus() == CustomerQuotations::STATUS_FINISHED) {
return true;
}
return false;
}
private function canResolveClaim(CustomerQuotations $order): bool
{
if ($order->getClaim() && $order->getClaim()->getStatus() != OrderClaim::STATUS_RESOLVED) {
return true;
}
return false;
}
private function canViewClaim(CustomerQuotations $order): bool
{
return true;
}
private function canReorder(CustomerQuotations $order): bool
{
if ($this->security->isGranted('user.is_manufacturer')) {
return false;
}
if ($order->getStatus() !== CustomerQuotations::STATUS_FINISHED) {
return false;
}
if (!$this->security->isGranted('ROLE_SALES_MANAGER')) {
return false;
}
return true;
}
private function canAddBatch(CustomerQuotations $quotation): bool
{
return in_array($quotation->getStatus(), [
CustomerQuotations::STATUS_PRODUCTION,
CustomerQuotations::STATUS_PRODUCED,
CustomerQuotations::STATUS_PRODUCTION_FINISHED,
CustomerQuotations::STATUS_TRANSPORT
]);
}
private function canGeneratePrePaymentInvoice(CustomerQuotations $quotation): bool
{
if (!$this->workflowHelper->isSubjectStatusGreaterOrEqual($quotation, CustomerQuotations::STATUS_MF_CONTRACT_APPROVED)) {
return false;
}
return $this->security->isGranted('ROLE_SALES_MANAGER');
}
private function canGenerateBatchesInvoice(CustomerQuotations $quotation): bool
{
if (!$this->security->isGranted('ROLE_SALES_MANAGER')) {
return false;
}
if (!count($quotation->getProductionBatches())) {
return false;
}
foreach ($quotation->getProductionBatches() as $batch) {
if (!count($batch->getInvoices())) {
return true;
}
}
return false;
}
private function isStockProduct(CustomerQuotations $quotation): bool
{
return in_array($quotation->getPackagingType(), $this->packagingTypeRepo->getStockCodes());
}
}