Files
Global-Jain/app/Imports/SantsImport.php

164 lines
6.0 KiB
PHP
Raw Permalink Normal View History

2025-11-05 10:37:10 +05:30
<?php
namespace App\Imports;
use App\Models\Sant;
use App\Models\Thana;
use App\Models\SantTemp;
use App\Constant\Constant;
use App\Models\ThanaMember;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithValidation;
class SantsImport implements ToModel, WithStartRow
{
use Importable;
/**
* @return int
*/
public function startRow(): int
{
return 2;
}
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
try {
$dharma = null;
$sampraday = null;
$birthDate = null;
$dikshaDate = null;
$honors = [];
//for dharma
if ($row[1] != '') {
$dharma = getDharmaID($row[1]);
}
//for sampraday
if ($row[2] != '') {
$sampraday = getSampradayID($row[2]);
}
//for birth date
if ($row[10] != '') {
$birthDate = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject((int)$row[10])->format('Y-m-d');
}
//for diksha date
if ($row[11] != '') {
$dikshaDate = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject((int)$row[11])->format('Y-m-d');
}
if ($row[3]) {
$honors = stringToArray($row[3]);
}
if($row[14] != '') {
$santData = [
'user_id' => loggedInUser()->id,
'dharma_id' => $dharma,
'sampraday_id' => $sampraday,
'name' => $row[4],
'honor' => $honors,
'gender' => getGenderType($row[5]),
'guru_name' => $row[6],
'father_name' => $row[7],
'mother_name' => $row[8],
'qualification' => $row[9],
'birth_date' => $birthDate,
'diksha_date' => $dikshaDate,
'diksha_place' => $row[12],
'about' => $row[13],
'status' => Constant::STATUS_TWO,
'verification_status' => Constant::STATUS_ONE,
'created_by' => loggedInUser()->id
];
$santData['reviewed_fields']['name'] = Constant::STATUS_TRUE;
$santData['reviewed_fields']['dharma'] = Constant::STATUS_TRUE;
$santData['reviewed_fields']['sampraday'] = Constant::STATUS_TRUE;
$santData['reviewed_fields']['birth_date'] = Constant::STATUS_TRUE;
$santData['reviewed_fields']['gender'] = Constant::STATUS_TRUE;
$santData['reviewed_fields']['diksha_date'] = Constant::STATUS_TRUE;
$santData['reviewed_fields']['diksha_place'] = Constant::STATUS_TRUE;
$santData['reviewed_fields']['guru'] = Constant::STATUS_TRUE;
$santData['reviewed_fields']['father_name'] = Constant::STATUS_TRUE;
$santData['reviewed_fields']['mother_name'] = Constant::STATUS_TRUE;
$santData['reviewed_fields']['about'] = Constant::STATUS_TRUE;
$santData['reviewed_fields']['avatar'] = Constant::STATUS_TRUE;
$sant = Sant::create($santData);
//storing to sant temp
$santID = $sant->id;
$santTemp = $sant->toArray();
$santTemp['sant_id'] = $santID;
$santTemp = SantTemp::create($santTemp);
$thana = new Thana();
$thana->sant_id = $sant->id;
$thana->name = randomStrings(9);
$thana->created_by = loggedInUser()->id;
$thana->updated_by = loggedInUser()->id;
$thana->save();
\Session::put('test', $thana);
if ($row[14] != 1) {
$thanaMember = new ThanaMember();
$thanaMember->thana_id = $thana->id;
$thanaMember->sant_id = $sant->id;
$thanaMember->is_leader = Constant::STATUS_ONE;
$thanaMember->is_approved = Constant::STATUS_ONE;
$thanaMember->save();
}
} else {
$santData = [
'user_id' => loggedInUser()->id,
'dharma_id' => $dharma,
'sampraday_id' => $sampraday,
'name' => $row[4],
'honor' => $honors,
'gender' => getGenderType($row[5]),
'guru_name' => $row[6],
'father_name' => $row[7],
'mother_name' => $row[8],
'qualification' => $row[9],
'birth_date' => $birthDate,
'diksha_date' => $dikshaDate,
'diksha_place' => $row[12],
'about' => $row[13],
'status' => Constant::STATUS_TWO,
'verification_status' => Constant::STATUS_ONE,
'created_by' => loggedInUser()->id
];
$sant = Sant::create($santData);
//storing to sant temp
$santID = $sant->id;
$santTemp = $sant->toArray();
$santTemp['sant_id'] = $santID;
$santTemp = SantTemp::create($santTemp);
$thanaMember = new ThanaMember();
$thanaMember->thana_id = \Session::get('test')->id;
$thanaMember->sant_id = $sant->id;
$thanaMember->is_approved = Constant::STATUS_ONE;
$thanaMember->save();
}
} catch(\Exception $ex) {
Log::error($ex);
}
}
}