mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 20:44:19 +00:00
90 lines
2.6 KiB
PHP
90 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Transformers;
|
|
|
|
use App\Classes\ValueObjects\Constants\ContainerTypes;
|
|
use App\Models\Container;
|
|
use Illuminate\Support\Facades\DB;
|
|
use League\Fractal\Resource\Collection;
|
|
use League\Fractal\Resource\Item;
|
|
use League\Fractal\TransformerAbstract;
|
|
|
|
class ContainerTransformer extends TransformerAbstract
|
|
{
|
|
/**
|
|
* List of resources to automatically include
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $defaultIncludes = [
|
|
'transport',
|
|
'remarks'
|
|
];
|
|
|
|
/**
|
|
* List of resources possible to include
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $availableIncludes = [
|
|
'packing_lists'
|
|
];
|
|
|
|
/**
|
|
* A Fractal transformer.
|
|
*
|
|
* @param Container $container
|
|
* @return array
|
|
*/
|
|
public function transform(Container $container): array
|
|
{
|
|
return [
|
|
'id' => $container->id,
|
|
'container_reference' => $container->reference,
|
|
'container_specification' => ContainerTypes::CONTAINER_SPECIFICATION[$container->container_type],
|
|
'container_number' => $container->container_number,
|
|
'seal_reference' => $container->seal_reference,
|
|
'loading_date' => $container->loading_date ? $container->loading_date->format('d-m-Y') : $container->loading_date,
|
|
'packages_count' => $container->packages()->where('packages.type', '!=', 2)->sum('quantity'),
|
|
'total_cbm' => $container->packages()->sum(DB::raw('(width/100) * (height/100) * (length/100) * quantity')),
|
|
'status' => $container->status,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Include Transport
|
|
* @param Container $container
|
|
* @return Item
|
|
*/
|
|
public function includeTransport(Container $container): Item
|
|
{
|
|
$container = $container->transport()->first();
|
|
return $this->item($container, new TransportTransformer());
|
|
}
|
|
|
|
/**
|
|
* Include Remarks
|
|
* @param Container $container
|
|
* @return Collection
|
|
*/
|
|
public function includeRemarks(Container $container): Collection
|
|
{
|
|
$remarks = $container->remarks()->get();
|
|
return $this->collection($remarks, new RemarkTransformer());
|
|
}
|
|
|
|
/**
|
|
* include Packages Lists
|
|
* @param Container $container
|
|
* @return Collection
|
|
*/
|
|
public function includePackagesList(Container $container): Collection
|
|
{
|
|
$packages = $container->packingLists()->get()->sortBy(function ($packingList) {
|
|
return $packingList->owner->company_module_id;
|
|
});
|
|
return $this->collection($packages, new PackageListTransformer);
|
|
}
|
|
}
|
|
|