72 lines
1.7 KiB
PHP
72 lines
1.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Modules\HrmAddon\App\Models;
|
||
|
|
|
||
|
|
use App\Models\Branch;
|
||
|
|
use App\Models\PaymentType;
|
||
|
|
use App\Models\Scopes\BranchScope;
|
||
|
|
use App\Models\Transaction;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
|
|
|
||
|
|
class Payroll extends Model
|
||
|
|
{
|
||
|
|
use HasFactory;
|
||
|
|
|
||
|
|
protected $fillable = [
|
||
|
|
'business_id',
|
||
|
|
'branch_id',
|
||
|
|
'employee_id',
|
||
|
|
'payment_type_id',
|
||
|
|
'month',
|
||
|
|
'date',
|
||
|
|
'note',
|
||
|
|
'amount',
|
||
|
|
'payemnt_year',
|
||
|
|
];
|
||
|
|
|
||
|
|
public function branch(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Branch::class)->withTrashed();
|
||
|
|
}
|
||
|
|
|
||
|
|
protected static function booted()
|
||
|
|
{
|
||
|
|
static::addGlobalScope(new BranchScope);
|
||
|
|
|
||
|
|
if (auth()->check() && auth()->user()->accessToMultiBranch()) {
|
||
|
|
static::addGlobalScope('withBranch', function ($builder) {
|
||
|
|
$builder->with('branch:id,name');
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function employee()
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Employee::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function payment_type()
|
||
|
|
{
|
||
|
|
return $this->belongsTo(PaymentType::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function transactions()
|
||
|
|
{
|
||
|
|
return $this->hasMany(Transaction::class, 'reference_id')
|
||
|
|
->where('platform', 'payroll');
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function boot()
|
||
|
|
{
|
||
|
|
parent::boot();
|
||
|
|
|
||
|
|
static::creating(function ($model) {
|
||
|
|
$id = Payroll::where('business_id', auth()->user()->business_id)->count() + 1;
|
||
|
|
$model->puid = 'Ps_' . str_pad($id, 4, '0', STR_PAD_LEFT);
|
||
|
|
$model->branch_id = auth()->user()->branch_id ?? auth()->user()->active_branch_id;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|