84 lines
3.0 KiB
PHP
84 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
use Illuminate\Support\Collection;
|
|
use Maatwebsite\Excel\Concerns\ToCollection;
|
|
use App\Models\Product;
|
|
use App\Models\SecuritizedDebtInstrument;
|
|
use App\Models\Table;
|
|
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class SecuritizedDebtInstrumentImport implements ToCollection, WithHeadingRow
|
|
{
|
|
/**
|
|
* @param Collection $collection
|
|
*/
|
|
protected $category_id;
|
|
|
|
public function __construct($category_id,)
|
|
{
|
|
$this->category_id= $category_id;
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
// return null;
|
|
return [
|
|
'*.product_name' => 'required|unique:securitized_debt_instruments,product_name'
|
|
// '*.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.'.product_name.required'] = 'Product Name should not be empty at row ' . $key+1;
|
|
$messages[''.$key.'.product_name.unique'] = 'Product 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 collection(Collection $rows)
|
|
{
|
|
Validator::make($rows->toArray(), $this->rules(), $this->customValidationMessages($rows))->validate();
|
|
|
|
|
|
foreach ($rows as $row) {
|
|
$product = Product::create([
|
|
'tables_id' => Table::SecuritizedDebtInstrumentTable,
|
|
'categories_id' => $this->category_id,
|
|
'total_views' => 0,
|
|
'status' => 0,
|
|
'created_by' => auth()->user()->id
|
|
]);
|
|
|
|
$securitizedDebtInstrument = SecuritizedDebtInstrument::create([
|
|
'products_id' => $product->id,
|
|
'product_name' => $row['product_name'],
|
|
'total_deal_size' => $row['total_deal_size'],
|
|
'minimum_investment' => $row['minimum_investment'],
|
|
'credit_rating' => $row['credit_rating'],
|
|
'deal_tenure' => $row['deal_tenure'],
|
|
'target_irr' => $row['target_irr'],
|
|
'target_multiple' => $row['target_multiple'],
|
|
'payout_frequency' => $row['payout_frequency'],
|
|
'principal_returned_in' => $row['principal_returned_in'],
|
|
'average_annual_payback' => $row['average_annual_payback'],
|
|
'security_enhancement' => $row['security_enhancement'],
|
|
'listing_details' => $row['listing_details'],
|
|
]);
|
|
}
|
|
}
|
|
}
|