87 lines
3.2 KiB
PHP
87 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
use Illuminate\Support\Collection;
|
|
use Maatwebsite\Excel\Concerns\ToCollection;
|
|
use App\Models\Product;
|
|
use App\Models\IndianFinancialAssets;
|
|
use App\Models\Table;
|
|
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class IndianFinancialAssetsImport 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:indian_financial_assets,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::IndianFinancialAssetsTable,
|
|
'categories_id' => $this->category_id,
|
|
'total_views' => 0,
|
|
'status' => 0,
|
|
'created_by' => auth()->user()->id
|
|
]);
|
|
|
|
$indianFinancialAssets = IndianFinancialAssets::create([
|
|
'products_id' => $product->id,
|
|
'slug' => Str::slug($row['product_name']),
|
|
'product_name' => $row['product_name'],
|
|
// 'type' => $this->fundType,
|
|
'investment_platform' => $row['investment_platform'],
|
|
'counter_party' => $row['counter_party'],
|
|
'investment_date' => $row['investment_date'],
|
|
'amount_invested' => $row['amount_invested'],
|
|
'total_gross_repaid_amount' => $row['total_gross_repaid_amount'],
|
|
'tenure' => $row['tenure'],
|
|
'principal_payment_frequency' => $row['principal_payment_frequency'],
|
|
'interest_payment_frequency' => $row['interest_payment_frequency'],
|
|
'next_repayment_due_date' => $row['next_repayment_due_date'],
|
|
'maturity_date' => $row['maturity_date'],
|
|
'next_repayment_amount' => $row['next_repayment_amount'],
|
|
'expected_irr' => $row['expected_irr'],
|
|
]);
|
|
}
|
|
}
|
|
}
|