Files
exchange-2.0/app/Classes/Modules/Rules/Standards/Rules/CanPassPurchaseOrderRule.php
T

97 lines
3.2 KiB
PHP

<?php
namespace App\Classes\Modules\Rules\Standards\Rules;
use App\Classes\Exceptions\CriteriaNotFulfilledException;
use App\Classes\General\Abstracts\AbstractRule;
use App\Classes\Modules\Bookings\Services\FetchesBooking;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\SegmentNameConstants;
use App\Classes\ValueObjects\Constants\ServiceTypeNameConstants;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\ServiceType;
use Illuminate\Support\Facades\Log;
class CanPassPurchaseOrderRule extends AbstractRule
{
/** @var FetchesBooking */
private $fetchesBooking;
/** @var FetchesCompany */
private $fetchesCompany;
/**
* CanPassPurchaseOrderRule constructor.
* @param FetchesBooking $fetchesBooking
* @param FetchesCompany $fetchesCompany
*/
public function __construct(FetchesBooking $fetchesBooking, FetchesCompany $fetchesCompany)
{
$this->fetchesBooking = $fetchesBooking;
$this->fetchesCompany = $fetchesCompany;
}
/**
* @return bool
*/
protected function authorized($object): bool
{
return true;
}
/**
* @return bool
*/
protected function validators($object): bool
{
return true;
}
/**
* @return bool
*/
protected function criteria($object): bool
{
//Check if the transfer/booking already has purchase order filled
$booking = $this->fetchesBooking->execute(['id' => $object->bookingId]);
$purchaseOrder = $booking->transactions()->where('type', TransactionType::PURCHASE_ORDER)->first();
$isPOEmptyException = false;
if (!$purchaseOrder || $purchaseOrder->status === ApprovalStatus::PENDING_SUBMISSION) {
$isPOEmptyException = true;
}
if($isPOEmptyException){ //If PO is indeed empty, there are some scenarios where PO actually can be left empty
$ids = ServiceType::whereIn('name', [
ServiceTypeNameConstants::PAYMENT_1688,
ServiceTypeNameConstants::VIP_1688,
])->get()->pluck('id');
//It can be left empty when booking is of type 1688: specifcally: 1688 Payment and 1688 VIP
if (in_array($booking->service_id, $ids->all())) {
$isPOEmptyException = false;
}
//However when the customer falls under the segment '1688 Manual PO Periodic', even if their booking is of type 1688 (1688 Payment and 1688 VIP),
//they still must fill up the PO. Confusing?? IKR
$company = $this->fetchesCompany->execute(['id' => $object->companyId]);
$filteredSegments = $company->segments()->whereIn('name', [SegmentNameConstants::MANUAL_PO_PERIODIC_1688, SegmentNameConstants::MANUAL_PO_1688])->get();
if(!$isPOEmptyException){
if (!$filteredSegments->isEmpty()) {
$isPOEmptyException = true;
}
}
}
if($isPOEmptyException){
throw new CriteriaNotFulfilledException("Please complete the purchase order form.");
}
return true;
}
}