mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
59 lines
3.1 KiB
PHP
59 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
use App\Classes\Modules\Companies\Services\FetchesCompanyServices;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\BankAccountType;
|
|
use App\Classes\ValueObjects\Constants\BusinessType;
|
|
use App\Classes\ValueObjects\Constants\DocumentType;
|
|
use App\Classes\ValueObjects\Constants\RoleTypes;
|
|
use App\Classes\ValueObjects\Constants\SegmentConstants;
|
|
use App\Classes\ValueObjects\Constants\TransactionType;
|
|
use App\Models\Currency;
|
|
use App\Models\SegmentConstant;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class CompanyResource extends JsonResource
|
|
{
|
|
/**
|
|
* Transform the resource into an array.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return array
|
|
*/
|
|
public function toArray($request)
|
|
{
|
|
return [
|
|
'id' => $this->id,
|
|
'name' => $this->name,
|
|
'reference' => $this->reference,
|
|
'type' => (int) $this->type,
|
|
'business_type' => (int) $this->business_type,
|
|
'status' => (int) $this->status,
|
|
'contact' => new ContactResource ($this->when($this->has('contacts'), $this->contacts->first())),
|
|
'address' => new AddressResource($this->when($this->has('addresses'), $this->addresses->where('billing', true)->first())),
|
|
'employee' => new UserResource(Auth::user()->type === RoleTypes::USER ? $this->employees()->where('email', '=', Auth::user()->email)->first() : $this->employees()->where('users.status', '=', ApprovalStatus::APPROVED)->orderBy('id', 'DESC')->first()),
|
|
'identification' => new DocumentResource($this->documents->whereIn('document_type', DocumentType::IDENTIFICATION_DOCUMENTS)->first()),
|
|
'bookings' => $this->whenLoaded('bookings', $this->bookings()->orderBy('id', 'DESC')->get(), []),
|
|
'total_payments' => (float) $this->transactions()->where('transactions.type', TransactionType::PAYMENT)->whereIn('transactions.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->sum('amount'),
|
|
'personal_banks' => BankResource::collection($this->banks->where('type', BankAccountType::PERSONAL)),
|
|
'recipient_banks' => [
|
|
'accounts' => BankResource::collection($this->banks->where('type', BankAccountType::EXTERNAL)),
|
|
'default' => new BankResource($this->banks->where('type', BankAccountType::EXTERNAL)->where('default', true)->first())
|
|
],
|
|
'segments' => SegmentResource::collection($this->segments),
|
|
'services' => (new FetchesCompanyServices())->getServices($this->servicesConfigurations()),
|
|
'currencies' => $this->when($this->business_type === BusinessType::CURRENCY_VENDOR, function(){
|
|
$segment = SegmentConstant::where('reference', SegmentConstants::SUPPLIER_CURRENCIES)->where('detail->id', $this->id)->first();
|
|
return $segment ? CurrencyResource::collection(Currency::whereIn('id', $segment->detail->currencies)->get()) : [];
|
|
}),
|
|
'created_at' => $this->created_at->format('d-m-Y')
|
|
|
|
|
|
];
|
|
|
|
}
|
|
}
|