File: /var/www/ipsremont-demo/app/Models/Shipment.php
<?php
namespace App\Models;
use App\Models\Service\Service;
use App\Services\Service\ServiceService;
use App\Services\Status\StatusService;
use App\Traits\Displayed;
use App\Traits\My;
use App\Traits\Sortable;
use FontLib\TrueType\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;
/**
* @property int $id
* @property string $track_number
* @property string $delivery_service
* @property string $recipient_name
* @property string $recipient_address
* @property string $recipient_phone
* @property string $departure_date
* @property int $service_id
* @property string $deleted_at
* @property string $created_at
* @property string $updated_at
* @property int $order_id
* @property string $status
* @property string $last_status_date
* @property int $error
* @property string $uuid
* @property int $branch_id
* @property int $errors_count
*
* @property Order $order
* @property Collection $shipment_server_response
* @property Collection $shipment_sdek_changes
* @property Collection $shipment_dpd_changes
* @property Collection $shipment_changes
*/
class Shipment extends BaseModel
{
use SoftDeletes, Sortable, Displayed, My;
protected $table = 'shipment';
const STATUS_NEW = 'new'; // Отправлена
const STATUS_AWAITING_DELIVERY = 'awaiting_delivery'; // Ожидает вручения
const STATUS_DELIVERED = 'delivered'; // Вручено
const STATUS_DELIVERING = 'delivering'; // Доставка
const STATUS_NOT_DELIVERED = 'not_delivered'; // Возврат
const STATUS_INVALID = 'invalid'; // Ошибка
protected $fillable = [
'track_number',
'delivery_service',
'departure_date',
'recipient_name',
'recipient_address',
'recipient_phone',
'order_id',
];
protected static $labels = [
'id' => 'shipment.id',
'track_number' => 'shipment.track_number',
'delivery_service' => 'shipment.delivery_service',
'recipient_name' => 'shipment.recipient_name',
'recipient_address' => 'shipment.recipient_address',
'recipient_phone' => 'shipment.recipient_phone',
'departure_date' => 'shipment.departure_date',
'updated_at' => 'shipment.date_of_last_update',
'service_id' => 'shipment.service_id',
'order_id' => 'shipment.order_id',
];
protected $casts = [
'departure_date' => 'datetime:Y-m-d',
];
public static function columns()
{
return [
'fields' => [
[
'displayName' => 'shipment.track_number',
'field' => 'track_number',
'sort' => true,
'sortType' => 'asc',
],
[
'displayName' => 'shipment.delivery_service',
'field' => 'delivery_service',
'sort' => true,
'sortType' => 'asc',
],
[
'displayName' => 'shipment.order_id',
'field' => 'order_id',
'sort' => true,
'sortType' => 'asc',
],
[
'displayName' => 'shipment.status',
'field' => 'status',
'sort' => true,
'sortType' => 'asc',
],
[
'displayName' => 'shipment.departure_date',
'field' => 'departure_date',
'sort' => true,
'sortType' => 'asc',
],
[
'displayName' => 'shipment.date_of_last_update',
'field' => 'updated_at',
'sort' => true,
'sortType' => 'asc',
],
],
'sortDefault' => [
[
'field' => 'created_at',
'sort' => 'desc',
],
],
];
}
public static function deliveryService(): array
{
return [
Service::TYPE_CDEK => 'CDEK',
Service::TYPE_PEK => 'ПЭК',
Service::TYPE_DPD => 'DPD',
];
}
public function scopeMy($query)
{
if (!Auth::check()) {
return $query;
}
if (Auth::user()->isService()) {
return $query
->whereIn('branch_id', Auth::user()->getBranchIds())
->where(['service_id' => Auth::user()->getCurrentServiceId()]);
}
if (can(Permission::branchesAll) && can(Permission::servicesAll)) {
return $query;
} elseif (can(Permission::servicesAll)) // если менеджер то видит заявки своего бранча,
{
return $query
->whereIn('branch_id', Auth::user()->getBranchIds());
} else {
// получить список доступных менеджеру сервисов. там сложная схема, это и привязанные через relation и обработка branchesAll и servicesAll
$services = ServiceService::getAvailable()->get()->pluck('id')->toArray();
return $query
->whereIn('service_id', $services);
}
}
public function status()
{
if (!$this->_status) {
$this->_status = StatusService::getShipmentBySlug($this->status);
}
return $this->_status;
}
public function getNumber()
{
return '#' . sprintf("%'.06d", $this->id);
}
public function getId()
{
return $this->id;
}
public function getTrackNumber()
{
return $this->track_number;
}
public function getDeliveryService(): string
{
return self::deliveryService()[$this->delivery_service];
}
public function getOrder()
{
return $this->order;
}
public function getOrderId()
{
return $this->order_id;
}
public function getDepartureDate()
{
return $this->departure_date;
}
public function getDateOfLastUpdate()
{
return $this->updated_at;
}
public function getRecipientName()
{
return $this->recipient_name;
}
public function getRecipientAddress()
{
return $this->recipient_address;
}
public function getRecipientPhone()
{
return $this->recipient_phone;
}
public function getStatus()
{
return $this->status;
}
public function shipment_parts()
{
return $this->hasMany(ShipmentParts::class);
}
public function shipment_pek_changes()
{
return $this->hasMany(ShipmentPekChanges::class)->orderBy('arrivalDateTime', 'desc');
}
public function status_update($status)
{
if ($this->status != $status) {
$this->status = $status;
$this->update();
}
}
// MARK: - Relations
public function order(): BelongsTo
{
return $this->belongsTo(Order::class);
}
public function shipment_server_response(): HasMany
{
return $this->hasMany(ShipmentServerResponse::class)->orderBy('created_at', 'desc');
}
public function shipment_sdek_changes(): HasMany
{
return $this->hasMany(ShipmentSdekChanges::class)->orderBy('date_time', 'desc');
}
public function shipment_dpd_changes(): HasMany
{
return $this->hasMany(ShipmentDpdChanges::class)->orderBy('id', 'desc');
}
public function shipment_changes(): HasMany
{
return $this->hasMany(ShipmentChanges::class)->orderBy('created_at', 'desc');
}
}