<?php

namespace App\Imports;

use App\Models\Texmaster;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class ImportTexMaster implements ToModel ,WithHeadingRow
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function startRow(): int
    {
        return 6;
    }
    public function model(array $row)
    {
        $Texmaster = Texmaster::select('id')->where(['gst' => $row['gst'],'compensation_cess' => $row['compensation_cess'],'status' => 'active'])->first();
        
        $data = [];
        if($Texmaster){
            session(['error' => 'Some Tax is already available on records!']);
            // return null;
        }else{
            $data = [
                'gst' => $row['gst'],
                'compensation_cess' => $row['compensation_cess'],
                'status' => 'active',
                'created_at' => date('Y-m-d H:i:s'),
                'created_by' => auth()->user()->id,
                'updated_at' => date('Y-m-d H:i:s'),
                'updated_by' => auth()->user()->id
            ];             
        }
        $insurance = [];
        if(!empty($data)){
            $insurance = Texmaster::create($data);               
        }        
        return $insurance;
    }
}
