52 lines
1.7 KiB
PHP
52 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Policies;
|
|
|
|
use App\Enums\Order\TransactionStatus;
|
|
use App\Models\Customer\Customer;
|
|
use App\Models\Payment\Transaction;
|
|
use App\Models\User;
|
|
use Illuminate\Auth\Access\Response;
|
|
|
|
class TransactionPolicy extends Policy
|
|
{
|
|
/**
|
|
* Can a user download transaction invoice
|
|
*
|
|
* ! Only owner customer or a Bo user
|
|
* ! + a validated transaction.
|
|
* ! + a !needs_validation transaction.
|
|
*
|
|
* @param \App\Models\Customer\Customer|\App\Models\User|null $user
|
|
* @param \App\Models\Payment\Transaction|null $transaction
|
|
*
|
|
* @return \Illuminate\Auth\Access\Response
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
*/
|
|
public function download(
|
|
$user = null,
|
|
Transaction $transaction = null
|
|
): Response {
|
|
if (!$transaction) {
|
|
return Response::deny(\trans('No transaction provided'));
|
|
}
|
|
if (!$transaction->status->equals(TransactionStatus::validated())) {
|
|
return Response::deny(trans('Transaction is not validated'));
|
|
}
|
|
if ($transaction->needs_validation) {
|
|
return Response::deny(\trans('Transaction needs to be validated first'));
|
|
}
|
|
if ($user and $user instanceof User) {
|
|
return Response::allow();
|
|
}
|
|
if (
|
|
$user and $user instanceof Customer and
|
|
$transaction->order->customer and
|
|
!\is_null($user->hashcode) and
|
|
$transaction->order->customer->hashcode === $user->hashcode
|
|
) {
|
|
return Response::allow();
|
|
}
|
|
return Response::deny(\trans('Could not satisfy any conditions'));
|
|
}
|
|
}
|