CasperSecurity
<?php
namespace App\Imports;
use App\Models\EmployeeWageDetailsModel;
use Maatwebsite\Excel\Concerns\ToModel;
use Carbon\Carbon;
class EmployeeWageImport implements ToModel
{
private $month;
private $year;
private $isFirstRow = true; // Add the property to check for the first row
// Constructor to accept month and year from the Livewire component
public function __construct($month, $year)
{
$this->month = $month;
$this->year = $year;
}
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
// Skip the header row
if ($this->isFirstRow) {
$this->isFirstRow = false;
return null; // Don't process the first row
}
// Create the model instance with the data from the Excel row
return new EmployeeWageDetailsModel([
'name' => $row[0], // Corresponding to 'name' field
'sub' => $row[1], // Corresponding to 'sub' field
'father_name' => $row[2], // Corresponding to 'father_name' field
'designation' => $row[3], // Corresponding to 'designation' field
'month' => $this->month, // Set the month (from modal input)
'year' => $this->year, // Set the year (from modal input)
'paid_days' => (int)$row[4], // Corrected: Corresponding to 'paid_days' field (row[4])
'extra_duty' => (int)$row[5], // Corrected: Corresponding to 'extra_duty' field (row[5])
'rate_of_wage' => (float)$row[6], // Corrected: Corresponding to 'rate_of_wage' field (row[6])
'gross_pay' => (float)$row[7], // Corresponding to 'gross_pay' field (row[7])
'extra_wages' => (float)$row[8], // Corresponding to 'extra_wages' field (row[8])
'epf' => (float)$row[9], // Corresponding to 'epf' field (row[9])
'esic' => (float)$row[10], // Corresponding to 'esic' field (row[10])
'total_deduction' => (float)$row[11], // Corresponding to 'total_deduction' field (row[11])
'net_pay' => (float)$row[12], // Corresponding to 'net_pay' field (row[12])
'signature' => $row[13], // Corresponding to 'signature' field (row[13])
]);
}
}