Files
freeu-project/app/Imports/FundImport.php

128 lines
4.8 KiB
PHP
Raw Normal View History

2024-03-28 14:52:40 +05:30
<?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
2024-03-28 14:52:40 +05:30
]);
if ($this->category_id == Category::GlobalMutualFundId) {
$init = 18;
} else if ($this->category_id == Category::GlobalPrivateEquityFundId) {
$init = 18;
} else if ($this->category_id == Category::GlobalVentureCapitalFundId) {
$init = 18;
} else if($this->category_id == Category::GlobalHedgeFundId){
$init = 18;
} else if($this->category_id == Category::GlobalVentureDebtId){
$init = 18;
} else if($this->category_id == Category::GlobalPrivateCreditFundId){
$init = 18;
2024-03-28 14:52:40 +05:30
}
$returns = array();
$arrayKeys = array_keys($row->toArray());
$totalKeys = count($row->toArray()) - 1;
for ($yearIndex = $init; $yearIndex <= $totalKeys; $yearIndex++) {
2024-06-07 16:35:40 +05:30
if($arrayKeys[$yearIndex] != 'description')
{
array_push($returns, $arrayKeys[$yearIndex]);
}
2024-03-28 14:52:40 +05:30
};
2024-04-12 17:47:47 +05:30
$count = Fund::where('issuer',$row['issuer'])->count();
2024-03-28 14:52:40 +05:30
$fund = Fund::create([
'products_id' => $product->id,
2024-04-12 17:47:47 +05:30
'slug' => Str::slug($row['issuer']).'-'.$count+1,
2024-03-28 14:52:40 +05:30
'issuer' => $row['issuer'],
2024-06-07 16:35:40 +05:30
'geographic_focus' => "Global",
2024-03-28 14:52:40 +05:30
'fund_name' => $row['fund_name'],
'fund_type' => $row['fund_type'],
'about_issuer' => $row['about_issuer'],
2024-05-09 13:02:24 +05:30
'fund_description' => $row['description'],
2024-03-28 14:52:40 +05:30
'sharpe_ratio' => $row['sharpe_ratio'],
'annualized_volatility' => $row['annualized_volatility'],
'max_dropdown' => $row['max_drawdown'],
'isin' => $row['isin'],
2024-05-30 11:43:16 +05:30
'inception_date' => getConvertedDate($row['inception_date']),
2024-03-28 14:52:40 +05:30
'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'],
2024-05-30 11:43:16 +05:30
// 'data_as_on' => getConvertedDate($row['data_as_on']) ?? null,
2024-03-28 14:52:40 +05:30
]);
if ($returns) {
foreach ($returns as $key => $value) {
FundReturn::create([
'funds_id' => $fund->id,
'label' => $value,
'value' => $row[$value],
]);
}
}
}
}
2024-05-31 15:10:50 +05:30
}