Files
freeu-project/app/Imports/FundImport.php
meghamalore b1bce8eea9 bugs fixes
2024-06-20 16:31:42 +05:30

129 lines
5.0 KiB
PHP

<?php
namespace App\Imports;
use App\Models\Fund;
use App\Models\Table;
use App\Models\Product;
use App\Models\Category;
use App\Models\FundReturn;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class FundImport 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:funds,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();
// dd($rows->toArray());
foreach ($rows as $row) {
$product = Product::create([
'tables_id' => Table::FundTable,
'categories_id' => $this->category_id,
'total_views' => 0,
'status' => 0,
'created_by' => auth()->user()->id,
'description'=> $row['description'] ?? null
]);
if ($this->category_id == Category::GlobalMutualFundId) {
$init = 18;
} else if ($this->category_id == Category::GlobalPrivateEquityFundId) {
$init = 17;
} else if ($this->category_id == Category::GlobalVentureCapitalFundId) {
$init = 17;
} else if($this->category_id == Category::GlobalHedgeFundId){
$init = 17;
} else if($this->category_id == Category::GlobalVentureDebtId){
$init = 18;
} else if($this->category_id == Category::GlobalPrivateCreditFundId){
$init = 18;
}
$returns = array();
$arrayKeys = array_keys($row->toArray());
$totalKeys = count($row->toArray()) - 1;
for ($yearIndex = $init; $yearIndex <= $totalKeys; $yearIndex++) {
if($arrayKeys[$yearIndex] != 'description')
{
array_push($returns, $arrayKeys[$yearIndex]);
}
};
$count = Fund::where('issuer',$row['issuer'])->count();
$fund = Fund::create([
'products_id' => $product->id,
'slug' => Str::slug($row['issuer']).'-'.++$count,
'issuer' => $row['issuer'],
'geographic_focus' => "Global",
'fund_name' => $row['fund_name'],
'fund_type' => $row['fund_type'],
'about_issuer' => $row['about_issuer'],
'fund_description' => $row['description'],
'sharpe_ratio' => $row['sharpe_ratio'],
'annualized_volatility' => $row['annualized_volatility'],
'max_dropdown' => $row['max_drawdown'],
'isin' => $row['isin'],
'inception_date' => getConvertedDate($row['inception_date']),
'fund_aum' => $row['fund_aum'],
'expense_ratio' => $row['expense_ratio'],
'nav_per_unit' => $row['nav_per_unit_usd'],
'minimum_investment' => $row['minimum_investment_usd'],
'ytd' => $row['ytd'],
'year1_return' => $row['1_year_return'],
'year3_return' => $row['3_year_return'],
// 'date_as_on' => array_key_exists('data_as_on',$row->toArray()) ? getConvertedDate($row['data_as_on']) ?? null : null,
'date_as_on' => getConvertedDate($row['data_as_on']) ?? null ,
]);
if ($returns) {
foreach ($returns as $key => $value) {
FundReturn::create([
'funds_id' => $fund->id,
'label' => $value,
'value' => $row[$value],
]);
}
}
}
}
}