Files
freeu-project/app/Imports/RealEstateImport.php
Ritikesh yadav 0762441b74 fixed slug issues
2024-06-13 16:57:40 +05:30

120 lines
5.9 KiB
PHP

<?php
namespace App\Imports;
use App\Models\Table;
use App\Models\Product;
use App\Models\RealEstate;
use Illuminate\Support\Str;
// use Illuminate\Validation\Rule;
use Illuminate\Support\Collection;
// use Maatwebsite\Excel\Concerns\ToModel;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class RealEstateImport implements ToCollection, WithHeadingRow
{
/**
* @param Collection $collection
*/
protected $category_id;
public function __construct($category_id)
{
$this->category_id= $category_id;
}
public function customValidationMessages(Collection $rows)
{
$messages = [];
foreach ($rows as $key => $val) {
$messages[''.$key.'.property_name.required'] = 'Property Name should not be empty at row ' . $key+1;
$messages[''.$key.'.property_name.unique'] = 'Property Name is not unique at row ' . $key+1;
}
return $messages;
// return [
// '*.property_name.required' => 'Property name must not be empty!'
// // '*.min_investment.required' => 'Minimun Investment must not be empty!',
// // '*.return_rate.required' => 'Return Rate must not be empty!'
// ];
}
public function rules(): array
{
return [
'*.property_name' => 'required',
// '*.property_name' => 'required|unique:real_estates,property_name'
// '*.property_name' => 'required|unique:fractional_real_estates,property_name'
// '*.min_investment' => 'required',
// '*.return_rate' => 'required',
];
}
public function collection(Collection $rows)
{
Validator::make($rows->toArray(), $this->rules(), $this->customValidationMessages($rows))->validate();
// dd($rows, $this->category_id == 20 ? 'Global' : ($this->category_id == 21 ? 'Global' : ($this->category_id == 22 ? 'Global' : 'India')));
foreach ($rows as $row) {
$product = Product::create([
'tables_id' => Table::RealEstateTable,
'categories_id' => $this->category_id,
'total_views' => 0,
'status' => 0,
'created_by' => auth()->user()->id,
'description'=> $row['description'] ?? null
]);
$count = RealEstate::where('property_name',$row['property_name'])->count();
RealEstate::create([
'products_id' => $product->id,
// 'companies_id' => $row['companies_id'],
'slug' => Str::slug($row['property_name']).'-'.++$count,
'property_name' => $row['property_name'],
'geographic_focus' => $this->category_id == 20 ? 'Global' : ($this->category_id == 21 ? 'Global' : ($this->category_id == 22 ? 'Global' : 'India')),
'property_location' => $row['property_location'],
'project_type' => $row['project_type'],
'current_status' => $row['current_status'] ?? null,
'price_per_sq_ft' => $row['price_per_sq_ft'],
'booking_amount' => $row['booking_amount'] ?? null,
'price_range' => $row['price_range'] ?? null,
'total_price' => $row['total_price'] ?? null,
'transaction_type' => $row['transaction_type'] ?? null,
'project_code_or_rera_id' => $row['project_code_or_rera_id'] ?? null,
'built_up_area' => $row['built_up_area'] ?? null,
'carpet_area' => $row['carpet_area'] ?? null,
'construction_status' => $row['construction_status'] ?? null,
// 'launch_date' => getConvertedDate($row['launch_date']) ?? null,
// 'completed_in' => getConvertedDate($row['completed_in']) ?? null,
'launch_date' => getConvertedDate($row['launch_date']) ? getConvertedDate($row['launch_date']) : null,
'completed_in' => getConvertedDate($row['completed_in']) ? getConvertedDate($row['completed_in']) : null,
'total_units' => $row['total_units'] ?? null,
'unit_type' => $row['unit_type'] ?? null,
'no_of_restrooms' => $row['no_of_restrooms'] ?? null,
'no_of_floors' => $row['no_of_floors'] ?? null,
'furnished_status' => $row['furnished_status'] ?? null,
'commencement_certificate' => $row['commencement_certificate'] ?? null,
'occupancy_certificate' => $row['occupancy_certificate'] ?? null,
'total_towers' => $row['total_towers'] ?? null,
'builder_details' => $row['builder_details'] ?? null,
'landmarks' => $row['landmarks'] ?? null,
'amenities' => $row['amenities'] ?? null,
'elevators' => $row['elevators'] ?? null,
'car_parking' => $row['car_parking'] ?? null,
'electricity_status' => $row['electricity_status'] ?? null,
'fire_safety_measures' => $row['fire_safety_measures'],
'water_facility' => $row['water_facility'],
'price_negotiable' => $row['price_negotiable'] ?? null,
'maintenance_fees' => $row['maintenance_fees'] ?? null,
'nearest_railway_metro_station' => $row['nearest_railway_or_metro_station'] ?? null,
'pre_leased' => $row['pre_leased'] ?? null,
'tenant_details' => $row['tenant_details'] ?? null,
'facilities_features' => $row['facilities_features'] ?? null,
'construction_age' => $row['construction_age'] ?? null,
'remarks' => $row['remarks'],
'country' => $row['country'] ?? null,
'location' => $row['location'] ?? null,
'area_in_sq_ft' => $row['area_in_sq_ft'] ?? null
]);
}
}
}