78 lines
3.2 KiB
PHP
78 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Maatwebsite\Excel\Concerns\ToCollection;
|
|
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
|
use App\Models\MonthlyUpdateFractionalRealEstate;
|
|
use Carbon\Carbon;
|
|
|
|
class ImportMontlyUpdateFractionalRealEstate implements ToCollection, WithHeadingRow
|
|
{
|
|
/**
|
|
* @param Collection $collection
|
|
*/
|
|
|
|
public function rules(): array
|
|
{
|
|
// return null;
|
|
return [
|
|
// '*.issuer' => 'required|unique:bonds,issuer'
|
|
// '*.property_name' => 'required|unique:fractional_real_estates,property_name'
|
|
// '*.min_investment' => 'required',
|
|
// '*.return_rate' => 'required',
|
|
];
|
|
}
|
|
|
|
public function customValidationMessages(Collection $rows)
|
|
{
|
|
$messages = [];
|
|
foreach ($rows as $key => $val) {
|
|
$messages['' . $key . '.issuer.required'] = 'Issuer should not be empty at row ' . $key + 1;
|
|
$messages['' . $key . '.issuer.unique'] = 'Issuer 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 collection(Collection $rows)
|
|
{
|
|
Validator::make($rows->toArray(), $this->rules(), $this->customValidationMessages($rows))->validate();
|
|
|
|
|
|
foreach ($rows as $row) {
|
|
// dd($row);
|
|
// dd(getConvertedDate($row['investment_date']));
|
|
if($row['investment_value'] != null && $row['total_net_interest'] != null && $row['total_gross_interest'] != null && $row['total_value_of_the_property'] != null)
|
|
{
|
|
$latestId = MonthlyUpdateFractionalRealEstate::insertGetId([
|
|
'custom_id' => $row['custom_id'],
|
|
'total_value_of_the_property' => $row['total_value_of_the_property'] ?? 0,
|
|
'investment_value' => $row['investment_value'] ?? 0,
|
|
'investment_date' => getConvertedDate($row['investment_date']) ?? 0,
|
|
'total_gross_interest' => $row['total_gross_interest'] ?? 0,
|
|
'tds' => $row['tds'] ?? 0,
|
|
'total_net_interest' => $row['total_net_interest'] ?? 0,
|
|
'gross_entry_yield_in_pct' => $row['gross_entry_yield'] ?? 0,
|
|
'target_return_in_pct' => $row['target_return'] ?? 0,
|
|
'absolute_return_till_date' => $row['absolute_return_till_date'],
|
|
'created_at' => Carbon::now(),
|
|
'updated_at' => Carbon::now(),
|
|
]);
|
|
|
|
$falseAll = MonthlyUpdateFractionalRealEstate::where('custom_id', $row['custom_id'])->update(['status'=>false]);
|
|
if($falseAll)
|
|
{
|
|
MonthlyUpdateFractionalRealEstate::where(['id'=>$latestId,'custom_id'=>$row['custom_id']])->update(['status'=>true]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|