99 lines
3.7 KiB
PHP
99 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
use Illuminate\Support\Collection;
|
|
use Maatwebsite\Excel\Concerns\ToCollection;
|
|
use App\Models\Product;
|
|
use App\Models\Bonds;
|
|
use App\Models\Table;
|
|
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class BondImport 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 [
|
|
'*.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) {
|
|
$product = Product::create([
|
|
'tables_id' => Table::BondTable,
|
|
'categories_id' => $this->category_id,
|
|
'total_views' => 0,
|
|
'status' => 0,
|
|
'created_by' => auth()->user()->id
|
|
]);
|
|
|
|
|
|
Bonds::create([
|
|
'products_id' => $product->id,
|
|
'slug' => Str::slug($row['issuer']),
|
|
'issuer' => $row['issuer'],
|
|
// 'type' => $row['property_location'],
|
|
'bond_type' => $row['bond_type'],
|
|
'about_issuer' => $row['about_issuer'],
|
|
'industry' => $row['industry'],
|
|
'country_of_risk' => $row['country_of_risk'],
|
|
'country_of_incorporation' => $row['country_of_incorporation'],
|
|
'isin' => $row['isin'],
|
|
'bond_type_or_maturity_date' => $row['bond_typematurity_date'],
|
|
'call_date' => $row['call_date'],
|
|
'coupon' => $row['coupon'],
|
|
'indicative_yield_pa_mid' => $row['indicative_yield_pa_mid'],
|
|
'indicative_price_us_mid' => $row['indicative_price_usd_mid'] ?? $row['indicative_price_mid'],
|
|
'minimum_investment' => $row['minimum_investment_usd'] ?? $row['minimum_investment'],
|
|
'yield_to_worst_pa_ask' => $row['yield_to_worst_pa_ask'],
|
|
'yield_to_worst_pa_bid' => $row['yield_to_worst_pa_bid'],
|
|
'price_ask' => $row['price_ask'],
|
|
'price_bid' => $row['price_bid'],
|
|
'accrued_interest' => $row['accrued_interest'],
|
|
'yield_to_call' => $row['yield_to_call'],
|
|
'duration' => $row['duration_years'],
|
|
'amount_outstanding' => $row['amount_outstanding_usd'] ?? $row['amount_outstanding'],
|
|
'collateral_type' => $row['collateral_type'],
|
|
'credit_rating' => $row['credit_rating'],
|
|
'data_as_on' => $row['data_as_on']
|
|
]);
|
|
}
|
|
}
|
|
}
|