<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class SeverancePayment extends Model
{
	use SoftDeletes;
	
	const STATUS_DRAFT = 0;
	const STATUS_OFFICIAL = 1;
	
	protected $fillable = [
		'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',
		'payment_date',
		'payment_status',
		'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',
		'status'                     => 'integer',
		'calculation_detail'         => 'json'
	];
	
	public function employee()
	{
		return $this->belongsTo(Employee::class)->select(
			'id', 'employee_code', 'last_name', 'first_name', 'last_name_kana', 'first_name_kana',
			'full_name', 'full_name_kana', 'preferred_name', 'date_of_birth',
			'join_date', 'employment_status', 'resigned_date', 'resigned_reason', 'department_id',
		)->with(['department', 'retirementFund:id,employee_id,amount']);
	}
	
	public function creator()
	{
		return $this->belongsTo(User::class, 'created_by')->select('id', 'name', 'first_name', 'last_name', 'full_name');
	}
	
	public function scopeDraft($query)
	{
		return $query->where('status', self::STATUS_DRAFT);
	}
	
	public function scopeOfficial($query)
	{
		return $query->where('status', self::STATUS_OFFICIAL);
	}
}