mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 12:33:56 +00:00
105 lines
7.1 KiB
PHP
105 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
use App\Classes\Modules\Bookings\Services\CalculatesBookingFloatingAmount;
|
|
use App\Classes\Modules\Bookings\Services\CalculatesBookingOutstanding;
|
|
use App\Classes\Modules\Bookings\Services\CalculatesBookingPayableAmount;
|
|
use App\Classes\Modules\Bookings\Services\CalculatesBookingRefundAmount;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\BookingAttributeNames;
|
|
use App\Classes\ValueObjects\Constants\TransactionType;
|
|
use App\Classes\ValueObjects\Constants\DocumentType;
|
|
use App\Classes\ValueObjects\Constants\KVPKey;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class BookingResource extends JsonResource
|
|
{
|
|
/**
|
|
* Transform the resource into an array.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return array
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
*/
|
|
public function toArray($request)
|
|
{
|
|
$eInvoice = false;
|
|
$eInvoiceStartDate = Carbon::parse(env('E_INVOICE_START_DATE', '2025-07-01 00:00:00'));
|
|
$bookingCreatedDate = Carbon::parse($this->created_at);
|
|
$eInvoiceRequestedDate = Carbon::parse($this->company->e_invoice_requested_at);
|
|
//cief todo: 90 - for testing
|
|
if ($bookingCreatedDate->isAfter($eInvoiceStartDate) && $this->company->e_invoice === 1) { //&& $bookingCreatedDate->diffInMinutes($eInvoiceRequestedDate) <= 480 cief todo: 90
|
|
$eInvoice = true;
|
|
}
|
|
|
|
$kvp = $this->attributesKVP()->where('key', KVPKey::BOOKING_EINVOICE_ELIGIBLE)->first();
|
|
if($kvp){
|
|
$eInvoice = true;
|
|
}
|
|
|
|
$outStandingAmountWithRefund = floatval((App()->make(CalculatesBookingOutstanding::class))->execute($this->resource)) - floatval((App()->make(CalculatesBookingRefundAmount::class))->execute($this->resource, $this->fix_currency_id));
|
|
|
|
return [
|
|
'id' => $this->id,
|
|
'company' => new CompanyResource($this->company),
|
|
'bank' => new BankResource($this->bank),
|
|
'service' => new ServiceTypeResource($this->service),
|
|
'marking' => $this->marking,
|
|
'amount' => $this->fix_amount,
|
|
'floating_amount' => floatval((App()->make(CalculatesBookingFloatingAmount::class))->execute($this->resource, $this->fix_currency_id)),
|
|
// 'paid_amount_with_refund' => floatval((App()->make(CalculatesBookingPayableAmount::class))->execute($this->resource, $this->fix_currency_id)) - floatval((App()->make(CalculatesBookingRefundAmount::class))->execute($this->resource, $this->fix_currency_id)),
|
|
'paid_amount' => floatval((App()->make(CalculatesBookingPayableAmount::class))->execute($this->resource, $this->fix_currency_id)),
|
|
'outstanding_amount_with_refund' => $outStandingAmountWithRefund > 0 ? $outStandingAmountWithRefund : 0,
|
|
'refunded_amount' => floatval((App()->make(CalculatesBookingRefundAmount::class))->execute($this->resource, $this->fix_currency_id)),
|
|
'outstanding_amount' => floatval((App()->make(CalculatesBookingOutstanding::class))->execute($this->resource)),
|
|
'fixed_currency' => new CurrencyResource($this->fixedCurrency),
|
|
'convertible_currency' => new CurrencyResource($this->convertibleCurrency),
|
|
'conversion_currency' => new CurrencyResource($this->conversionCurrency),
|
|
'documents' => [
|
|
'purchase_order' => new DocumentResource($this->documents()->where('document_type', DocumentType::PURCHASE_ORDER)->latest()->first()),
|
|
'delivery_order' => new DocumentResource($this->documents()->where('document_type', DocumentType::DELIVER_ORDER)->latest()->first()),
|
|
'invoice' => new DocumentResource($this->documents()->where('document_type', DocumentType::INVOICE)->latest()->first()),
|
|
'e_invoice' => new DocumentResource($this->documents()->where('document_type', DocumentType::EINVOICE)->latest()->first()),
|
|
'supplier_delivery_order' => new DocumentResource($this->documents()->where('document_type', DocumentType::SUPPLIER_DELIVER_ORDER)->latest()->first()),
|
|
'proforma_invoice' => new DocumentResource($this->documents()->where('document_type', DocumentType::PROFORMA_INVOICE)->whereNotIn('status', [ApprovalStatus::REJECTED, ApprovalStatus::EXPIRED])->latest()->first()),
|
|
'ecommerce_purchase_order' => new DocumentResource($this->documents()->where('document_type', DocumentType::ECOMMERCE_PURCHASE_ORDER)->latest()->first()),
|
|
],
|
|
'order_reference_no' => $this->modelAttributes()->where('name', BookingAttributeNames::ORDER_REFERENCE_NO)->get()->map(function ($attr) {
|
|
return [
|
|
'id' => $attr->id,
|
|
'value' => $attr->value
|
|
];
|
|
}),
|
|
'status' => $this->status,
|
|
'created_at' => Carbon::parse($this->created_at)->format('d-m-Y'),
|
|
'created_at_with_time' => Carbon::parse($this->created_at)->format('d-m-Y h:i:s A'),
|
|
$this->mergeWhen($this->relationLoaded('transactions'), [
|
|
'purchase_order' => new TransactionResource($this->transactions()->where('type', TransactionType::PURCHASE_ORDER)->first()),
|
|
'payment_attempts' => TransactionResource::collection(
|
|
$this->transactions()
|
|
->payments()->where('status', ApprovalStatus::PENDING_SUBMISSION)
|
|
->whereDate('expires_on', '>=', Carbon::now())
|
|
->get()
|
|
),
|
|
// 'expired_payment_attempts' => TransactionResource::collection($this->transactions()->payments()->where('status', ApprovalStatus::PENDING_SUBMISSION)->whereDate('expires_on', '>=', Carbon::now())->where('expires_on', '>', Carbon::now()->toTimeString())->get()),
|
|
'expired_payment_attempts' => TransactionResource::collection($this->transactions()->payments()->where('status', ApprovalStatus::EXPIRED)->get()),
|
|
'payment_history' => TransactionResource::collection($this->transactions()->where(function($query){
|
|
$query->where(function($query){
|
|
$query->payments()->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::COMPLETED, ApprovalStatus::REJECTED, ApprovalStatus::REFUNDED]);
|
|
})->orWhere(function($query){
|
|
$query->where(function($query){
|
|
$query->where('type', TransactionType::REFUND)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::REJECTED, ApprovalStatus::COMPLETED]);
|
|
})->orWhere(function($query){
|
|
$query->where('type', TransactionType::CREDIT_NOTE)->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
|
|
});
|
|
});
|
|
})->latest()->get())
|
|
]),
|
|
'einvoice' => $eInvoice,
|
|
'invoice_status' => $this->invoice_status,
|
|
];
|
|
}
|
|
}
|