112 lines
2.4 KiB
PHP
112 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Transaksi extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'transaksis';
|
|
|
|
protected $fillable = [
|
|
'invoice_number',
|
|
'user_id',
|
|
'customer_name',
|
|
'customer_phone',
|
|
'customer_email',
|
|
'shipping_address',
|
|
'province',
|
|
'city',
|
|
'postal_code',
|
|
'subtotal',
|
|
'shipping_cost',
|
|
'total_amount',
|
|
'payment_method',
|
|
'payment_status',
|
|
'order_status',
|
|
'notes',
|
|
'paid_at'
|
|
];
|
|
|
|
protected $casts = [
|
|
'paid_at' => 'datetime',
|
|
'subtotal' => 'decimal:2',
|
|
'shipping_cost' => 'decimal:2',
|
|
'total_amount' => 'decimal:2',
|
|
];
|
|
|
|
// Relationships
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function details()
|
|
{
|
|
return $this->hasMany(TransaksiDetail::class);
|
|
}
|
|
|
|
// Scopes
|
|
public function scopePending($query)
|
|
{
|
|
return $query->where('order_status', 'pending');
|
|
}
|
|
|
|
public function scopeProcessing($query)
|
|
{
|
|
return $query->where('order_status', 'processing');
|
|
}
|
|
|
|
public function scopeShipped($query)
|
|
{
|
|
return $query->where('order_status', 'shipped');
|
|
}
|
|
|
|
public function scopeDelivered($query)
|
|
{
|
|
return $query->where('order_status', 'delivered');
|
|
}
|
|
|
|
public function scopeCancelled($query)
|
|
{
|
|
return $query->where('order_status', 'cancelled');
|
|
}
|
|
|
|
public function scopePaid($query)
|
|
{
|
|
return $query->where('payment_status', 'paid');
|
|
}
|
|
|
|
public function scopeUnpaid($query)
|
|
{
|
|
return $query->where('payment_status', 'pending');
|
|
}
|
|
|
|
// Methods
|
|
public function canBeCancelled()
|
|
{
|
|
return in_array($this->order_status, ['pending', 'processing']);
|
|
}
|
|
|
|
public function markAsPaid()
|
|
{
|
|
return $this->update([
|
|
'payment_status' => 'paid',
|
|
'paid_at' => now()
|
|
]);
|
|
}
|
|
|
|
public function updateOrderStatus($status)
|
|
{
|
|
return $this->update(['order_status' => $status]);
|
|
}
|
|
|
|
// Accessor
|
|
public function getPaymentMethodLabelAttribute()
|
|
{
|
|
return $this->payment_method === 'transfer' ? 'Transfer Bank' : 'COD';
|
|
}
|
|
} |