Edit File: Order.php
<?php namespace App\Models; use App\Enums\OrderType; use App\Models\BaseModel; use App\Enums\OrderStatus; use App\Enums\OrderPayType; use App\Traits\UploadTrait; use App\Enums\OrderPayStatus; use App\Enums\OrderStatusEnum; use App\Enums\OrderWhenEnum; use App\Enums\ProductDeliveryTypesEnum; use Illuminate\Support\Facades\Storage; use SimpleSoftwareIO\QrCode\Facades\QrCode; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\BelongsTo; class Order extends BaseModel { use UploadTrait; protected $fillable = [ 'order_num', 'user_id', 'provider_id', 'delegate_id', 'order_type', 'receiving_method', 'schedule_execution_date', 'schedule_execution_time', 'cancellation_reason_id', 'city_id', 'neighborhood_id', 'coupon_id', 'coupon_num', 'coupon_type', 'coupon_value', 'vat_per', 'vat_amount', 'total_products', 'coupon_amount', 'delivery_price', 'final_total', 'admin_commission_per', 'admin_commission_value', 'status', 'provider_due_amount', 'commission_ratio_from_provider', 'commission_value_from_provider', 'delegate_due_amount', 'commission_ratio_from_delegate', 'commission_value_from_delegate', 'pay_type', 'pay_status', 'pay_data', 'lat', 'lng', 'map_desc', 'notes', 'car_color', 'car_model', 'car_plat_number', 'receiving_in_store_notes', 'order_picked_at', 'is_settlement', 'expiry_period', 'expired_at', //calls 'api_key', 'session_id', 'token', 'secret_key', // For Notification [When no delegates assigned to admins] 'is_notified_admin_no_delegate' ]; protected $casts = [ 'pay_data' => 'array', 'expired_at' => 'datetime', 'is_notified_admin_no_delegate' => 'boolean', ]; public function getQrCodeAttribute() { /** * generate image file and store with order id * composer require simplesoftwareio/simple-qrcode "~4" * https://www.simplesoftware.io/#/docs/simple-qrcode */ if (!Storage::exists("images/qrcodes/$this->id")) { QrCode::format('png')->generate("$this->id", base_path() . "/storage/app/public/images/qrcodes/$this->id.png"); } return dashboard_url("storage/images/qrcodes/$this->id.png"); } public function getStatusAttribute($value): array { return [ 'value' => $value, 'slug' => OrderStatusEnum::from((int)$value)->name, 'text' => __('order.status.' . OrderStatusEnum::from((int)$value)->name), ]; } public function getReceivingMethodAttribute($value): array { return [ 'value' => $value, 'slug' => ProductDeliveryTypesEnum::from((int)$value)->name, 'text' => __('order.receiving_method.' . ProductDeliveryTypesEnum::from((int)$value)->name), ]; } public function getPayTypeTextAttribute(): string { return trans('order.pay_type.' . strtolower(OrderPayType::from((int)$this->attributes['pay_type'])->name)); } public function getOrderTypeTextAttribute(): string { return trans('order.order_type.' . $this->attributes['order_type']); } public function getPayStatusTextAttribute($value) { return trans('order.pay_status_' . strtolower(OrderPayStatus::from((int)$this->attributes['pay_status'])->name)); } public function user(): BelongsTo { return $this->belongsTo(User::class); } public function coupon() { return $this->belongsTo(Coupon::class); } public function provider(): BelongsTo { return $this->belongsTo(Provider::class); } public function delegate(): BelongsTo { return $this->belongsTo(Delegate::class); } public function rates(): HasMany { return $this->hasMany(Rate::class); } public function statusHistory(): HasMany { return $this->hasMany(OrderStatusHistory::class); } public function orderItems(): HasMany { return $this->hasMany(OrderItem::class); } public function orderTransactions(): HasMany { return $this->hasMany(OrderTransaction::class); } public function cancellationReason(): BelongsTo { return $this->belongsTo(CancelReason::class, 'cancellation_reason_id', 'id'); } public function room(): HasOne { return $this->hasOne(Room::class, 'order_id', 'id'); } public function productImages() { return $this->hasManyThrough(ProductImage::class, OrderItem::class, 'order_id', 'product_id', 'id', 'product_id'); } static function checkPreviousStatus($id, $newStatus, int|array $oldStatus, $user) { try { $order = self::where(['id' => $id, $user => auth()->id()])->firstOrFail(); $getValueFromOrderStatus = $order->status['value']; if ($getValueFromOrderStatus == $newStatus) { return __('apis.this_status_already_taken'); } elseif (is_array($oldStatus) && !in_array($getValueFromOrderStatus, $oldStatus)) { return __('apis.not_correct_status'); } elseif (is_numeric($oldStatus) && $getValueFromOrderStatus != $oldStatus) { return __('apis.not_correct_status'); } else { return ['order' => $order]; } } catch (\Exception $e) { return __('apis.order_not_found'); } } public static function boot(): void { parent::boot(); self::creating(function ($model) { $lastId = self::max('id') ?? 0; $model->order_num = date('Y') . ($lastId + 1); }); } }
Back to File Manager