<?php

namespace Hilco\Models;

/**
 * Class DeliveryMethod
 * @package Hilco\Models
 * @property integer $id
 * @property string $delivery_method_code
 * @property string $description
 * @property string $carrier
 * @property bool $is_for_billrcvr
 * @property string $billrcvr_desc
 * @method static \Illuminate\Database\Query\Builder|\Hilco\Models\DeliveryMethod whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\Hilco\Models\DeliveryMethod whereDeliveryMethodCode($value)
 * @method static \Illuminate\Database\Query\Builder|\Hilco\Models\DeliveryMethod whereDescription($value)
 * @method static \Illuminate\Database\Query\Builder|\Hilco\Models\DeliveryMethod whereCarrier($value)
 * @method static \Illuminate\Database\Eloquent\Builder isForBillReceiver()
 * @method static \Illuminate\Database\Eloquent\Builder forCarriers(...$carriers)
 */
class DeliveryMethod extends WebModel {
    protected $table = "DeliveryMethods";

    // UPS Supersaver
    const EXPRESSRX_DELIVERY_METHOD = "USS";
    // Purolator
    const PUROLATOR_GROUND = "PRG";
    const PUROLATOR_EXPRESS = "PRX";

    public function scopeIsForBillReceiver($query) {
        return $query->whereRaw("is_for_billrcvr = 1");
    }

    public function scopeForCarriers ($query, ...$carriers) {
        if (count($carriers) > 1) {
            return $query->whereIn('carrier', $carriers);
        } else if (count($carriers)) {
            return $query->where('carrier', $carriers[0]);
        } else {
            return $query;
        }
    }

    /**
     * @return DeliveryMethod|null
     */
    public static function getExpressRxMethod(): ?DeliveryMethod {
        return DeliveryMethod::where('delivery_method_code', self::EXPRESSRX_DELIVERY_METHOD)->first();
    }

    public function getDeliveryMethodCode(): string {
        return $this->delivery_method_code;
    }

    /**
     * @return string|null
     */
    public function getCarrier(): ?string {
        return $this->carrier;
    }
}