mirror of
https://gitlab.com/uldvstar/exchange-2.0.git
synced 2026-08-19 12:34:24 +00:00
60 lines
3.7 KiB
PHP
60 lines
3.7 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\CalculatesBookingPaidAmount;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\TransactionType;
|
|
use App\Classes\ValueObjects\Constants\DocumentType;
|
|
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)
|
|
{
|
|
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' => floatval((App()->make(CalculatesBookingPaidAmount::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),
|
|
'purchase_order_document' => new DocumentResource($this->documents()->where('document_type', DocumentType::PURCHASE_ORDER)->first()),
|
|
'deliver_order_document' => new DocumentResource($this->documents()->where('document_type', DocumentType::DELIVER_ORDER)->first()),
|
|
'payment_order_document' => new DocumentResource($this->documents()->where('document_type', DocumentType::PAYMENT_ORDER)->first()),
|
|
'supplier_order_document' => new DocumentResource($this->documents()->where('document_type', DocumentType::SUPPLIER_DELIVER_ORDER)->first()),
|
|
'status' => $this->status,
|
|
'created_at' => Carbon::parse($this->created_at)->format('d-m-Y'),
|
|
$this->mergeWhen($this->relationLoaded('transactions'), [
|
|
'purchase_order' => new TransactionResource($this->transactions()->where('type', TransactionType::PURCHASE_ORDER)->first()),
|
|
'payment_attempts' => TransactionResource::collection($this->transactions()->where('type', TransactionType::PAYMENT)->where('status', ApprovalStatus::PENDING_SUBMISSION)->whereDate('expires_on', '>=', Carbon::now())->get()),
|
|
'expired_payment_attempts' => TransactionResource::collection($this->transactions()->where('type', TransactionType::PAYMENT)->where('status', ApprovalStatus::PENDING_SUBMISSION)->whereDate('expires_on', '<', Carbon::now())->get()),
|
|
'payment_history' => TransactionResource::collection($this->transactions()->where(function($query){
|
|
$query->where(function($query){
|
|
$query->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::REJECTED]);
|
|
})->orWhere(function($query){
|
|
$query->where('type', TransactionType::BILL)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
|
|
});
|
|
})->latest()->get())
|
|
])
|
|
];
|
|
}
|
|
}
|