94 lines
2.4 KiB
PHP
94 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Traits\BelongsToCompany;
|
|
use App\Traits\HasAuditTrail;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class SalesInvoiceReturn extends Model
|
|
{
|
|
use BelongsToCompany, HasAuditTrail;
|
|
|
|
protected $fillable = [
|
|
'return_number',
|
|
'return_date',
|
|
'customer_id',
|
|
'warehouse_id',
|
|
'original_invoice_id',
|
|
'reason',
|
|
'subtotal',
|
|
'tax_amount',
|
|
'discount_amount',
|
|
'total_amount',
|
|
'status',
|
|
'notes',
|
|
'creator_id',
|
|
'created_by'
|
|
];
|
|
|
|
protected $casts = [
|
|
'return_date' => 'date',
|
|
'subtotal' => 'decimal:2',
|
|
'tax_amount' => 'decimal:2',
|
|
'discount_amount' => 'decimal:2',
|
|
'total_amount' => 'decimal:2'
|
|
];
|
|
|
|
public function items(): HasMany
|
|
{
|
|
return $this->hasMany(SalesInvoiceReturnItem::class, 'return_id');
|
|
}
|
|
|
|
public function customer(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'customer_id');
|
|
}
|
|
|
|
public function warehouse(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Warehouse::class, 'warehouse_id');
|
|
}
|
|
|
|
public function customerDetails(): BelongsTo
|
|
{
|
|
return $this->belongsTo(\Workdo\Account\Models\Customer::class, 'customer_id', 'user_id');
|
|
}
|
|
|
|
public function originalInvoice(): BelongsTo
|
|
{
|
|
return $this->belongsTo(SalesInvoice::class, 'original_invoice_id');
|
|
}
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::creating(function ($return) {
|
|
if (empty($return->return_number)) {
|
|
$return->return_number = static::generateReturnNumber();
|
|
}
|
|
});
|
|
}
|
|
|
|
public static function generateReturnNumber(): string
|
|
{
|
|
$year = date('Y');
|
|
$month = date('m');
|
|
$lastReturn = static::where('return_number', 'like', "SR-{$year}-{$month}-%")
|
|
->where('created_by', creatorId())
|
|
->orderBy('return_number', 'desc')
|
|
->first();
|
|
|
|
if ($lastReturn) {
|
|
$lastNumber = (int) substr($lastReturn->return_number, -3);
|
|
$nextNumber = $lastNumber + 1;
|
|
} else {
|
|
$nextNumber = 1;
|
|
}
|
|
|
|
return "SR-{$year}-{$month}-" . str_pad($nextNumber, 3, '0', STR_PAD_LEFT);
|
|
}
|
|
} |