Files
sam-manage/app/Models/Finance/JournalEntry.php

67 lines
1.5 KiB
PHP

<?php
namespace App\Models\Finance;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
class JournalEntry extends Model
{
use SoftDeletes;
protected $table = 'journal_entries';
protected $fillable = [
'tenant_id',
'entry_no',
'entry_date',
'entry_type',
'description',
'total_debit',
'total_credit',
'status',
'created_by_name',
'attachment_note',
];
protected $casts = [
'entry_date' => 'date',
'total_debit' => 'integer',
'total_credit' => 'integer',
];
public function lines()
{
return $this->hasMany(JournalEntryLine::class)->orderBy('line_no');
}
public function scopeForTenant($query, $tenantId)
{
return $query->where('tenant_id', $tenantId);
}
/**
* 전표번호 자동채번: JE-YYYYMMDD-NNN
*/
public static function generateEntryNo($tenantId, $date)
{
$dateStr = date('Ymd', strtotime($date));
$prefix = "JE-{$dateStr}-";
$last = static::where('tenant_id', $tenantId)
->where('entry_no', 'like', $prefix . '%')
->lockForUpdate()
->orderByDesc('entry_no')
->value('entry_no');
if ($last) {
$seq = (int) substr($last, -3) + 1;
} else {
$seq = 1;
}
return $prefix . str_pad($seq, 3, '0', STR_PAD_LEFT);
}
}