239 lines
8.8 KiB
PHP
239 lines
8.8 KiB
PHP
<?php
|
|
|
|
namespace App\Policies;
|
|
|
|
use App\Enums\Order\DeliveryModeEnum;
|
|
use App\Enums\Order\DeliveryStatus;
|
|
use App\Enums\Order\PaymentModeEnum;
|
|
use App\Enums\Order\PaymentStatus;
|
|
use App\Enums\Order\ValidationStatus;
|
|
use App\Models\Customer\Customer;
|
|
use App\Models\Shop\Order;
|
|
use App\Models\User;
|
|
use Illuminate\Auth\Access\Response;
|
|
|
|
class OrderPolicy extends Policy
|
|
{
|
|
/**
|
|
* Can a user order things ?
|
|
*
|
|
* @param \App\Models\Customer|null $customer
|
|
*
|
|
* @return \Illuminate\Auth\Access\Response
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
|
|
*/
|
|
public function orderThings(?Customer $customer = null): Response
|
|
{
|
|
// phpcs:enable
|
|
if (!$this->anyDeliveryModeAvalable()) {
|
|
return $this->deny(trans('Aucun mode de livraison n\'est disponible'));
|
|
}
|
|
if (!$this->anyPaymentModeAvalable()) {
|
|
return $this->deny(trans('Aucun mode de paiement n\'est disponible'));
|
|
}
|
|
return $this->allow();
|
|
}
|
|
|
|
/**
|
|
* Can a user order things ?
|
|
*
|
|
* @param \App\Models\Customer|null $customer
|
|
* @param \App\Models\Shop\Order $order
|
|
*
|
|
* @return \Illuminate\Auth\Access\Response
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
*/
|
|
public function orderUsingDeliveryMode(
|
|
?Customer $customer = null,
|
|
Order $order
|
|
): Response {
|
|
// phpcs:enable
|
|
if (!$this->anyDeliveryModeAvalable()) {
|
|
return $this->deny(trans('Aucun mode de livraison n\'est disponible'));
|
|
}
|
|
if (!$this->anyPaymentModeAvalable()) {
|
|
return $this->deny(trans('Aucun mode de paiement n\'est disponible'));
|
|
}
|
|
if (!$this->deliveryModeIsAvalaible($order->deliveryMode->mode)) {
|
|
return $this->deny(trans("Le mode de livraison {$order->deliveryMode->mode->label} n'est disponible"));
|
|
}
|
|
// * Selection d'un mode de livraison autre que 'inShop'seulement
|
|
// * si un mode de paiement autre que 'inShop'est disponible.
|
|
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Shop\DeliveryMode> */
|
|
$deliveryModes = app('DeliveryModesGlobal');
|
|
if (
|
|
!$order->deliveryMode->mode->equals(DeliveryModeEnum::inShop()) and
|
|
!$this->deliveryModeIsAvalaible(DeliveryModeEnum::inShop()) and
|
|
$deliveryModes->count() < 2
|
|
) {
|
|
return $this->deny(trans("Le mode de livraison {$order->deliveryMode->mode->label} n'est disponible"));
|
|
}
|
|
return $this->allow();
|
|
}
|
|
|
|
/**
|
|
* Order can be prepared
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Shop\Order $order
|
|
* @return \Illuminate\Auth\Access\Response
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
*/
|
|
public function prepare(User $user, Order $order): Response
|
|
{
|
|
// phpcs:enable
|
|
if ($order->delivery_status->value >= DeliveryStatus::ready()->value) {
|
|
return $this->deny('The order has already been prepared');
|
|
}
|
|
if (
|
|
$order->payment_status->equals(PaymentStatus::canceled()) or
|
|
$order->payment_status->equals(PaymentStatus::failed())
|
|
) {
|
|
return $this->deny('The order status is :status and cannot be prepared.', [
|
|
'status' => $order->payment_status->label
|
|
]);
|
|
}
|
|
// ! Limit to only order where mail has been sent (order-tunnel has completed).
|
|
if (!$order->mail_sent) {
|
|
return $this->deny('The order was not completed (mail sent to client).');
|
|
}
|
|
if ($order->has_warning) {
|
|
return $this->deny('The order has warnings.');
|
|
}
|
|
if (!$order->validation_status->equals(ValidationStatus::validated())) {
|
|
return $this->deny('The order has to be validated to be prepared.');
|
|
}
|
|
return $this->allow();
|
|
}
|
|
|
|
/**
|
|
* Order validation status can be changed
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Shop\Order $order
|
|
* @return \Illuminate\Auth\Access\Response
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
*/
|
|
public function changeValidationStatus(User $user, Order $order): Response
|
|
{
|
|
$transaction = $order->getTransactionUsedForPayment();
|
|
if (!$transaction) {
|
|
return $this->deny('The order does not have a transaction associated.');
|
|
}
|
|
// ! Limit to only order where mail has been sent (order-tunnel has completed).
|
|
if (!$order->mail_sent) {
|
|
return $this->deny('The order was not completed (mail sent to client).');
|
|
}
|
|
if ($order->has_warning) {
|
|
return $this->deny('The order has warnings.');
|
|
}
|
|
if ($order->validation_status->equals(ValidationStatus::canceled())) {
|
|
return $this->deny('The order is already canceled.');
|
|
}
|
|
|
|
// phpcs:enable
|
|
return $this->allow();
|
|
}
|
|
|
|
/**
|
|
* Order validation status can be changed
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Shop\Order $order
|
|
* @return \Illuminate\Auth\Access\Response
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
*/
|
|
public function changeDeliveryStatus(User $user, Order $order): Response
|
|
{
|
|
// phpcs:enable
|
|
// ! Limit to only order where mail has been sent (order-tunnel has completed).
|
|
if (!$order->mail_sent) {
|
|
return $this->deny('The order was not completed (mail sent to client).');
|
|
}
|
|
if ($order->has_warning) {
|
|
return $this->deny('The order has warnings.');
|
|
}
|
|
if ($order->validation_status->equals(ValidationStatus::pending())) {
|
|
return $this->deny('The order still needs validation.');
|
|
}
|
|
return $this->allow();
|
|
}
|
|
|
|
/**
|
|
* Order can be prepared
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Shop\Order $order
|
|
* @return \Illuminate\Auth\Access\Response
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
*/
|
|
public function refund(User $user, Order $order): Response
|
|
{
|
|
// phpcs:enable
|
|
$transaction = $order->getTransactionUsedForPayment();
|
|
if (
|
|
$order->payment_status->equals(PaymentStatus::canceled()) or
|
|
$order->payment_status->equals(PaymentStatus::failed())
|
|
) {
|
|
return $this->deny('The order payment status is alread :status.', [
|
|
'status' => $order->payment_status->label
|
|
]);
|
|
}
|
|
if (!$transaction) {
|
|
return $this->deny('The order does not have a transaction associated.');
|
|
}
|
|
if ($transaction->payment_mode->equals(PaymentModeEnum::inShop())) {
|
|
return $this->deny('The order transaction payment mode is in shop, and cannot be refunded.');
|
|
}
|
|
// ! Limit to only order where mail has been sent (order-tunnel has completed).
|
|
if (!$order->mail_sent) {
|
|
return $this->deny('The order was not completed (mail sent to client).');
|
|
}
|
|
if ($order->has_warning) {
|
|
return $this->deny('The order has warnings.');
|
|
}
|
|
if (!$order->can_be_refund) {
|
|
return $this->deny('The order cannot be refunded (from model).');
|
|
}
|
|
return $this->allow();
|
|
}
|
|
|
|
/**
|
|
* Order can be resolved
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Shop\Order $order
|
|
* @return \Illuminate\Auth\Access\Response
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
*/
|
|
public function resolve(User $user, Order $order): Response
|
|
{
|
|
// phpcs:enable
|
|
if (!$order->has_warning) {
|
|
return $this->deny('The order has warnings.');
|
|
}
|
|
return $this->allow();
|
|
}
|
|
|
|
/**
|
|
* Can a user order download final order invoice
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\Shop\Order $order
|
|
*
|
|
* @return \Illuminate\Auth\Access\Response
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
*/
|
|
public function download(
|
|
User $user,
|
|
Order $order
|
|
): Response {
|
|
if (!$order->transactions()->count()) {
|
|
return $this->deny('No transactions are available on this order');
|
|
}
|
|
if ($order->transactions()->where('needs_validation', true)->count()) {
|
|
return $this->deny('There are transaction that needs to be resolved');
|
|
}
|
|
return $this->allow();
|
|
}
|
|
}
|