Files
shipping-portal/app/Http/Resources/FetchOrderResource.php
T
2026-07-08 01:37:54 +08:00

57 lines
2.8 KiB
PHP

<?php
namespace App\Http\Resources;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\OrderRoleTypes;
use App\Classes\ValueObjects\Constants\TransactionType;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;
class FetchOrderResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'reference' => $this->reference,
'reference_contract' => (int) $this->type,
'type' => (int) $this->type,
'status' => (int) $this->status,
'company_module' => new CompanyModuleResource($this->companyModule),
'warehouse' => new CompanyModuleResource($this->orderRoles()->where('role_id', '=', OrderRoleTypes::ORIGIN_WAREHOUSE)->first()->appointee),
'address' => new AddressResource($this->addresses()->where('status', '=', ApprovalStatus::APPROVED)->first()),
'address_change_request' => new AddressResource($this->addressesPendingVerification()->first()),
'invoices' => $this->whenLoaded('packingLists', function() {
$transactions = $this->transactions()->whereNotIn('transactions.status', [0, 1])->whereIn('transactions.type', [TransactionType::SHIPPING_INVOICE, TransactionType::STORAGE_INVOICE])->get();
foreach ($transactions as $transaction) {
$eInvoice = false;
$eInvoiceStartDate = Carbon::parse(env('E_INVOICE_START_DATE', '2025-07-01 00:00:00'));
$transactionCreatedDate = Carbon::parse($transaction->created_at);
$eInvoiceRequestedDate = Carbon::parse($this->companyModule->company->e_invoice_requested_at);
if ($transactionCreatedDate->isAfter($eInvoiceStartDate)
&&($transactionCreatedDate->isAfter($eInvoiceRequestedDate) || $transactionCreatedDate->diffInMinutes($eInvoiceRequestedDate) <= 60)
&& $this->companyModule->company->e_invoice === 1)
{
$eInvoice = true;
}
$transaction['is_einvoice_applicable'] = $eInvoice;
// $transaction['is_einvoice_applicable'] = $this->companyModule->company->e_invoice === 1;
}
return TransactionWithStorageResource::collection($transactions);
}),
'storages' => $this->storages ? $this->storages : null, //from middleware
'remarks' => RemarkResource::collection($this->remarks),
'supplier_tax_rebate' => SupplierTaxRebateResource::collection($this->supplierTaxRebate),
'created_at' => $this->created_at->format('d-m-Y'),
];
}
}