File: /home/armgroup/libs/app/Models/Order.php
<?php
namespace App\Models;
use App\Enums\OrderPaymentType;
use App\Enums\OrderStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
use HasFactory;
protected $guarded = ['id'];
protected $casts = [
'status' => OrderStatus::class,
'payment_type' => OrderPaymentType::class,
];
public const SHIPPING_EXPRESS = 0;
public const SHIPPING_COURIER = 1;
public const SHIPPING_TRANSLATE = ['پست پیشتاز', 'پیک'];
public function user()
{
return $this->belongsTo(User::class)->withDefault();
}
public function addresses()
{
return $this->morphToMany(Address::class, 'addressable');
}
public function products()
{
return $this->belongsToMany(Product::class)
->using(OrderProduct::class)
->withPivot('quantity', 'price', 'discount', 'option_id', 'value_id', 'name');
}
public function lockedStocks()
{
return $this->hasMany(LockedStock::class);
}
public function scopeCreateDeliveryCode()
{
return rand(100000, 999999);
}
public function wallets()
{
return $this->morphMany(Wallet::class, 'walletable');
}
public function changeCustomerPoints($increase = true)
{
$rate = (int) settings('point-for-per-1000-toman');
if ($rate > 0 && $point = round(($this->price / 1000) * $rate)) {
Point::create([
'user_id' => $this->user_id,
'amount' => $increase ? $point : $point * -1,
'description' => $increase ? "تکمیل سفارش {$this->tracking_code}" : "عدم تکمیل سفارش {$this->tracking_code}",
'pointable_id' => $this->id,
'pointable_type' => Order::class,
]);
}
}
public function discountCode()
{
return $this->belongsTo(Discount::class, 'discount_id')->withDefault();
}
public function payments()
{
return $this->morphMany(Payment::class, 'paymentable');
}
public function city()
{
return $this->belongsTo(City::class)->withDefault();
}
}