HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Shipments/Shipment.php
<?php

namespace App\Shipments;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Komma\KMS\Core\Attributes\Models\Traits\HasThumbnailInterface;
use Komma\KMS\Core\Attributes\Models\Traits\HasThumbnailTrait;
use Komma\KMS\Core\Entities\DisplayNameInterface;
use Komma\KMS\Core\Entities\DisplayNameTrait;
use App\Orders\Models\Order;
use App\Orders\Product\OrderedProduct;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Shipments\Shipment
 *
 * @property int $id
 * @property string $status
 * @property string $tracking_code
 * @property string $comment
 * @property-read bool $can_be_announced
 * @property-read bool $is_announced
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|OrderedProduct[] $orderedProducts
 * @property-read int|null $ordered_products_count
 * @method static \Illuminate\Database\Eloquent\Builder|Shipment newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Shipment newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Shipment query()
 * @method static \Illuminate\Database\Eloquent\Builder|Shipment whereComment($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Shipment whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Shipment whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Shipment whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Shipment whereTrackingCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Shipment whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property string $ssp_reference a reference to the shipment at the shipment service provider. For retrieving it later on
 * @method static \Illuminate\Database\Eloquent\Builder|Shipment whereSspReference($value)
 * @property int $order_id
 * @property string|null $carrier_name
 * @property string|null $tracking_url
 * @property-read Order $order
 * @method static \Illuminate\Database\Eloquent\Builder|Shipment whereCarrierName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Shipment whereOrderId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Shipment whereTrackingUrl($value)
 */
class Shipment extends Model implements HasThumbnailInterface, DisplayNameInterface
{
    //State tracking constants. Must match the states as defined in Model.js
    const NEW = 1;
    const PRISTINE = 2;
    const DIRTY = 3;
    const DELETED = 4;

    protected $name = '';
    use DisplayNameTrait;
    use HasThumbnailTrait;

    protected $fillable = ['status', 'tracking_code', 'comment'];

    public function __construct(array $attributes = [])
    {
        $this->name = ucfirst(__('KMS::shipments.shipment'));
        parent::__construct($attributes);
    }

    public function orderedProducts(): HasMany
    {
        return $this->hasMany(OrderedProduct::class);
    }

    public function order(): ? BelongsTo {
        return $this->belongsTo(Order::class);
    }

    /**
     * Returns a status name for css that eventually will determine the color when this transaction is displayed.
     *
     * @return string
     */
    public function statusColor(): string {
        switch ($this->status) {
            case ShipmentStatus::READY_TO_SHIP:
            case ShipmentStatus::CARRIER_NOTIFIED:
            case ShipmentStatus::IN_TRANSIT:
            case ShipmentStatus::OUT_FOR_DELIVERY:
                return 'pending';
            case ShipmentStatus::DELIVERED:
                return 'positive';
            case ShipmentStatus::FAILED_ATTEMPT:
            case ShipmentStatus::EXCEPTION:
                return 'negative';
            case ShipmentStatus::NEW:
            default:
                return 'default';
        }
    }

    public function displayTrackingCode() {
        if(!$this->tracking_code || $this->tracking_code == '') return '-';
        return $this->tracking_code;
    }

    public function getDisplayName(): ?string
    {
        return ucfirst(__('KMS::shipments.shipment')).' '.$this->id;
    }

    public function orderDisplayName(): string {
        $orderDisplayName = __('KMS::shipments.no_order');

        /** @var Order|null $order */
        $order = $this->order();
        if($order) $orderDisplayName = $order->getDisplayName();

        return $orderDisplayName;
    }

    public function orderLinkOrText(): ? string
    {
        $order = $this->order()->first();
        if(!$order) return __('KMS::shipments.no_order');

        return '<a href="' . route('orders.show', $order) . '">'. $order->getDisplayName() .'</a>';
    }

    public function customerLinkOrText(): string {
        /** @var Order $order */
        $order = $this->order()->first();
        if(!$order) return __('KMS::shipments.no_order');

        return '<a href="' . route('site_users.show', $order->customer->id) . '">'. $order->customerDisplayName() .'</a>';
    }

    /**
     * Wheter or not the shipment can be announced at a shipment provider,
     * so they can handle the package
     *
     * @return bool
     */
    public function getCanBeAnnouncedAttribute() {
        return (
            $this->status === ShipmentStatus::READY_TO_SHIP &&
            empty($this->ssp_reference)
        );
    }

    /**
     * Whether or not the package is announced at the shipping service provider
     *
     * @return bool
     */
    public function getIsAnnouncedAttribute() {
        return !empty($this->ssp_reference);
    }

    public function setStatusAttribute($value) {
        if(!ShipmentStatus::isValidItem($value)) throw new \InvalidArgumentException('The status attribute must be one of the ShipmentStatus enum values but was not.');
        $this->attributes['status'] = $value;
    }
}