<?php

namespace App;

use App\Traits\CsvImportable;
use App\Traits\CsvSplitable;
use Carbon\Carbon;
use Validator;

class Staff2000Allocation extends RichSearchableModel
{
    use CsvImportable;
    use CsvSplitable;

    public $incrementing = false;
    public $errors = [];
    const CREATED_AT = NULL;
    const UPDATED_AT = 'modified';
    protected $table = 'tcd_staff2000allocation';
    protected $primaryKey = '2or_code';
    protected $fillable = [
        '2or_code',
        'order_name',
        'order_date',
        'order_category_name',
        '2co_code',
        'dispatch_destination_name',
        'dispatch_destination_dep',
        'order_status_name',
        'contract_period_start',
        'contract_period_end',
        '2st_code',
        'staffu_name',
        'contact_name',
        'work_place_name',
        'work_place_dep',
        'work_place_zip',
        'work_place_address_1',
        'work_place_address_2',
        'work_place_address_3',
        'work_place_tel',
        'contract_delivery_destination_code',
        'contract_delivery_destination_name',
        'contract_delivery_destination_dep',
        'billing_address_code',
        'billing_address_name',
        'billing_address_dep',
        'dispatch_manager_name',
        'dispatch_manager_position',
        'dispatch_manager_tel',
        'commander_name',
        'commander_position',
        'commander_tel',
        'saturday_segment_name',
        'contract_renewal_period',
        'contract_renewal_criteria_1',
        'contract_renewal_criteria_2',
        'start_time_1',
        'end_time_1',
        'break_start_time_1',
        'break_end_time_1',
        'break_1',
        'hours_in_contract_1',
        'deformation_work_time_1',
        'deformation_work_time_4',
        'billing_deadline_1',
        'payment_day_1',
        'billing_hourly_salary_1',
        'csv_created',
        'csv_updated',
        'allocation_note_1',
        'allocation_note_2',
        'allocation_note_3',
        'allocation_note_4',
        'allocation_note_5',
        'allocation_note_6',
        'allocation_note_7',
        'allocation_note_8',
        'allocation_note_9',
        'allocation_note_10',
        'referral_fee',
        'salary_closing_date_1',
        'salary_hourly_salary_1',
        'dispatch_destination_manager_affiliation',
        'commander_affiliation',
        'employed_organizational_unit',
        'individual_unit_dispatch_start_date',
        'individual_unit_conflict_day',
    ];
    const DEFAULT_LIST = [
        'number' => 100,
        'search_method' => 1,
        'custom_list_sorts' => [
            [
                'field' => '2or_code',
                'sort' => CustomListSort::DESC,
                'priority' => 1
            ]
        ],
        'custom_list_conditions' => [],
        'custom_list_fields' => [
            [
                'field' => 'order_name'
            ],
            [
                'field' => 'order_date'
            ],
            [
                'field' => 'order_category_name'
            ],
            [
                'field' => '2co_code'
            ],
            [
                'field' => 'dispatch_destination_name'
            ],
            [
                'field' => 'order_status_name'
            ],
            [
                'field' => 'contract_period_start'
            ],
            [
                'field' => 'contract_period_end'
            ],
            [
                'field' => '2st_code'
            ],
            [
                'field' => 'staffu_name'
            ],
            [
                'field' => 'contact_name'
            ],
            [
                'field' => 'modified'
            ],
        ],
    ];

    const CONTRACT_STATUS = [
        1 => '契約期間前',
        2 => '契約中',
        3 => '契約終了',
    ];

    const FIELD_NAME = [
        '2or_code' => 'ｵｰﾀﾞｰｺｰﾄﾞ',
        'order_name' => 'ｵｰﾀﾞｰ名',
        'contract_status' => '契約ステータス',
        'order_date' => '受注日',
        'order_category_name' => '受注区分名',
        '2co_code' => '派遣先CD',
        'dispatch_destination_name' => '派遣先名',
        'dispatch_destination_dep' => '派遣先部署',
        'order_status_name' => '受注状況名',
        'contract_period_start' => '契約期間開始',
        'contract_period_end' => '契約期間終了',
        '2st_code' => 'ｽﾀｯﾌｺｰﾄﾞ',
        'staffu_name' => 'ｽﾀｯﾌ名',
        'contact_name' => '担当者名',
        'work_place_name' => '就業先名',
        'work_place_dep' => '就業先部署',
        'work_place_zip' => '就業先住所No',
        'work_place_address_1' => '就業先住所都道府県',
        'work_place_address_2' => '就業先住所市区郡',
        'work_place_address_3' => '就業先住所詳細',
        'work_place_tel' => '電話番号',
        'contract_delivery_destination_code' => '契約書送付先ｺｰﾄﾞ',
        'contract_delivery_destination_name' => '契約書送付先名',
        'contract_delivery_destination_dep' => '契約書送付先部署名',
        'billing_address_code' => '請求書送付先ｺｰﾄﾞ',
        'billing_address_name' => '請求書送付先名',
        'billing_address_dep' => '請求書送付先部署名',
        'dispatch_manager_name' => '派遣先責任者名',
        'dispatch_manager_position' => '派遣先責任者役職',
        'dispatch_manager_tel' => '派遣先責任者電話',
        'commander_name' => '指揮命令者名',
        'commander_position' => '指揮命令者役職',
        'commander_tel' => '指揮命令者電話',
        'saturday_segment_name' => '土曜区分名',
        'contract_renewal_period' => '契約更新／期間の定め',
        'contract_renewal_criteria_1' => '契約更新の判断の基準1',
        'contract_renewal_criteria_2' => '契約更新の判断の基準2',
        'working_hours_type_1' => '就業時間名称1',
        'start_time_1' => '開始時刻1',
        'end_time_1' => '終了時刻1',
        'break_start_time_1' => '休憩開始時刻1',
        'break_end_time_1' => '休憩終了時刻1',
        'break_1' => '休憩分1',
        'hours_in_contract_1' => '契約内時間数1',
        'working_hours_type_2' => '就業時間名称2',
        'start_time_2' => '開始時刻2',
        'end_time_2' => '終了時刻2',
        'break_start_time_2' => '休憩開始時刻2',
        'break_end_time_2' => '休憩終了時刻2',
        'break_2' => '休憩分2',
        'hours_in_contract_2' => '契約内時間数2',
        'working_hours_type_3' => '就業時間名称3',
        'start_time_3' => '開始時刻3',
        'end_time_3' => '終了時刻3',
        'break_start_time_3' => '休憩開始時刻3',
        'break_end_time_3' => '休憩終了時刻3',
        'break_3' => '休憩分3',
        'hours_in_contract_3' => '契約内時間数3',
        'working_hours_type_4' => '就業時間名称4',
        'start_time_4' => '開始時刻4',
        'end_time_4' => '終了時刻4',
        'break_start_time_4' => '休憩開始時刻4',
        'break_end_time_4' => '休憩終了時刻4',
        'break_4' => '休憩分4',
        'hours_in_contract_4' => '契約内時間数4',
        'deformation_work_time_1' => '変形労働時間1',
        'deformation_work_time_4' => '変形労働時間4',
        'billing_deadline_1' => '請求締日1',
        'payment_day_1' => '入金日1',
        'billing_hourly_salary_1' => '請求時間給1',
        'csv_created' => '作成日',
        'csv_updated' => '更新日',
        'allocation_note_1' => '引当ﾒﾓ1業務',
        'allocation_note_2' => '引当ﾒﾓ2業務',
        'allocation_note_3' => '引当ﾒﾓ3業務',
        'allocation_note_4' => '引当ﾒﾓ4業務',
        'allocation_note_5' => '引当ﾒﾓ5S料金',
        'allocation_note_6' => '引当ﾒﾓ6S料金',
        'allocation_note_7' => '引当ﾒﾓ7C料金',
        'allocation_note_8' => '引当ﾒﾓ8C料金',
        'allocation_note_9' => '引当ﾒﾓ9備考',
        'allocation_note_10' => '引当ﾒﾓ10d-ID',
        'referral_fee' => '紹介手数料',
        'salary_closing_date_1' => '給与締日1',
        'salary_hourly_salary_1' => '給与時間給1',
        'dispatch_destination_manager_affiliation' => '派遣先責任者所属',
        'commander_affiliation' => '指揮命令者所属',
        'employed_organizational_unit' => '就業先組織単位',
        'individual_unit_dispatch_start_date' => '個人単位派遣開始日',
        'individual_unit_conflict_day' => '個人単位抵触日',
        'modified' => 'レコード更新日',
    ];

    public static $function_id = CustomList::STAFF2000_ALLOCATION;
    public static $simple_searchable_fields = [
        '2or_code',
        'order_name',
        'order_date',
        'order_category_name',
        '2co_code',
        'dispatch_destination_name',
        'dispatch_destination_dep',
        'order_status_name',
        'contract_period_start',
        'contract_period_end',
        '2st_code',
        'staffu_name',
        'contact_name',
        'work_place_name',
        'work_place_dep',
        'work_place_zip',
        'work_place_address_1',
        'work_place_address_2',
        'work_place_address_3',
        'work_place_tel',
        'contract_delivery_destination_code',
        'contract_delivery_destination_name',
        'contract_delivery_destination_dep',
        'billing_address_code',
        'billing_address_name',
        'billing_address_dep',
        'dispatch_manager_name',
        'dispatch_manager_position',
        'dispatch_manager_tel',
        'commander_name',
        'commander_position',
        'commander_tel',
        'saturday_segment_name',
        'contract_renewal_period',
        'contract_renewal_criteria_1',
        'contract_renewal_criteria_2',
        'start_time_1',
        'end_time_1',
        'break_start_time_1',
        'break_end_time_1',
        'break_1',
        'hours_in_contract_1',
        'deformation_work_time_1',
        'deformation_work_time_4',
        'billing_deadline_1',
        'payment_day_1',
        'billing_hourly_salary_1',
        'csv_created',
        'csv_updated',
        'allocation_note_1',
        'allocation_note_2',
        'allocation_note_3',
        'allocation_note_4',
        'allocation_note_5',
        'allocation_note_6',
        'allocation_note_7',
        'allocation_note_8',
        'allocation_note_9',
        'allocation_note_10',
        'referral_fee',
        'salary_closing_date_1',
        'salary_hourly_salary_1',
        'dispatch_destination_manager_affiliation',
        'commander_affiliation',
        'employed_organizational_unit',
        'individual_unit_dispatch_start_date',
        'individual_unit_conflict_day',
        'modified',
    ];

    protected $validateFields = [
        'date_field' => ['date', 'datetime'],
        'num_field' => [],
        'bool_field' => [],
    ];

    // ソート不可フィールド
    public static $sort_exception_field = [
        'contract_status'
    ];

    public static function getSearchOptions()
    {
        $searchOptions = [];
        $searchOptions['select'] = [];
        $searchOptions['select']['contract_status'] = self::CONTRACT_STATUS;
        return $searchOptions;
    }

    // 一覧画面向けアクセサ
    public function getIndex2orCodeAttribute()
    {
        $value = $this['2or_code'];
        if (empty($value)) return $value;
        return $this->getLinkHtml(url('staff2000_order/view/'.$value), $value, true);
    }

    public function getIndexOrderNameAttribute()
    {
        if (empty($this->{'2or_code'})|| empty($this->order_name)) return $this->order_name;
        return $this->getLinkHtml(url('staff2000_order/view/'.$this->{'2or_code'}), $this->order_name, true);
    }

    public function getIndex2coCodeAttribute()
    {
        $value = $this['2co_code'];
        if (empty($value)) return $value;
        return $this->getLinkHtml(url('staff2000_company/view/'.$value), $value, true);
    }

    public function getIndexDispatchDestinationNameAttribute()
    {
        if (empty($this->{'2co_code'})|| empty($this->dispatch_destination_name)) return $this->dispatch_destination_name;
        return $this->getLinkHtml(url('staff2000_company/view/'.$this->{'2co_code'}), $this->dispatch_destination_name, true);
    }

    public function getIndex2stCodeAttribute()
    {
        $value = $this['2st_code'];
        if (empty($value)) return $value;
        return $this->getLinkHtml(url('staff2000_staffu/view/'.$value), $value, true);
    }

    public function getIndexStaffuNameAttribute()
    {
        if (empty($this->{'2st_code'})|| empty($this->staffu_name)) return $this->staffu_name;
        return $this->getLinkHtml(url('staff2000_staffu/view/'.$this->{'2st_code'}), $this->staffu_name, true);
    }

    public function scopeModified($query, $condition, $searchMethod)
    {
        return $query->datetime($condition, $searchMethod);
    }

    public static function scopeContractStatus($query, $condition, $searchMethod)
    {
        $today = Carbon::now()->toDateString();
        if($condition['condition'] == 2){
            $query->$searchMethod(function ($q) use ($today){
                $q->whereDate('contract_period_start', '<=', $today)
                    ->whereDate('contract_period_end', '>=', $today);
            });
        }elseif($condition['condition'] == 1){
            $query->$searchMethod('contract_period_start', '>', $today);
        }elseif($condition['condition'] == 3){
            $query->$searchMethod('contract_period_end', '<', $today);
        }
        return $query;
    }

    public function getContractStatusAttribute()
    {
        $today = Carbon::now();
        $start = new Carbon($this->contract_period_start);
        $end = new Carbon($this->contract_period_end);
        if($today->between($start, $end)){
            return self::CONTRACT_STATUS[2];
        }elseif($today->gt($end)){
            return self::CONTRACT_STATUS[3];
        }else{
            return self::CONTRACT_STATUS[1];
        }
    }

    public function getIndexContractStatusAttribute()
    {
        return $this->contract_status;
    }

    public function getFullAddressAttribute()
    {
        return $this->work_place_address_1.$this->work_place_address_2.$this->work_place_address_3;
    }
}
