<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class SeveranceCalculationHistory extends Model
{
	const STATUS_PENDING = 0;
	const STATUS_PROCESSING = 1;
	const STATUS_SUCCESS = 2;
	const STATUS_FAILED = 3;
	
	protected $fillable = [
		'code',
		'employee_id',
		'calculation_date',
		'tenure_year',
		'tenure_points',
		'level_points',
		'position_points',
		'tenure_transition_points',
		'level_transition_points',
		'position_transition_points',
		'total_points',
		'point_rate',
		'tenure_amount',
		'level_amount',
		'position_amount',
		'total_amount',
		'payment_percent',
		'final_amount',
		'retirement_fund_amount',
		'severance_amount',
		'calculation_detail',
		'created_by',
		'not_allow_edit',
		'payment_status',
		'payment_date',
		'status'
	];
	
	protected $casts = [
		'tenure_year'                => 'decimal:2',
		'tenure_points'              => 'decimal:2',
		'level_points'               => 'decimal:2',
		'position_points'            => 'decimal:2',
		'tenure_transition_points'   => 'decimal:2',
		'level_transition_points'    => 'decimal:2',
		'position_transition_points' => 'decimal:2',
		'total_points'               => 'decimal:2',
		'point_rate'                 => 'decimal:2',
		'tenure_amount'              => 'decimal:2',
		'level_amount'               => 'decimal:2',
		'position_amount'            => 'decimal:2',
		'total_amount'               => 'decimal:2',
		'payment_percent'            => 'decimal:2',
		'final_amount'               => 'decimal:2',
		'retirement_fund_amount'     => 'decimal:2',
		'severance_amount'           => 'decimal:2',
		'payment_status'             => 'integer',
		'not_allow_edit'             => 'integer',
		'status'                     => 'integer',
		'calculation_detail'         => 'json'
	];
	
	public function employee()
	{
		return $this->belongsTo(Employee::class)
			->with('department')
			->select(
				'id', 'employee_code',
				'last_name', 'first_name', 'last_name_kana', 'first_name_kana', 'full_name', 'full_name_kana',
				'date_of_birth', 'join_date', 'department_id', 'position_id', 'level_id', 'salary_grade',
				'employment_status', 'resigned_date', 'resigned_reason', 'employee_code'
			);
	}
	
	public function creator()
	{
		return $this->belongsTo(User::class, 'created_by');
	}
	
	public function scopePending($query)
	{
		return $query->where('status', self::STATUS_PENDING);
	}
	
	public function scopeProcessing($query)
	{
		return $query->where('status', self::STATUS_PROCESSING);
	}
	
	public function scopeSuccess($query)
	{
		return $query->where('status', self::STATUS_SUCCESS);
	}
	
	public function scopeFailed($query)
	{
		return $query->where('status', self::STATUS_FAILED);
	}
}