101 lines
4.0 KiB
PHP
101 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
use App\Models\Table;
|
|
use App\Models\Product;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Maatwebsite\Excel\Concerns\ToCollection;
|
|
use App\Models\StockFundsRealEstateExchange;
|
|
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
|
|
|
class StockFundsRealEstateExchangeImport implements ToCollection, WithHeadingRow
|
|
{
|
|
/**
|
|
* @param Collection $collection
|
|
*/
|
|
protected $category_id;
|
|
|
|
public function __construct($category_id)
|
|
{
|
|
$this->category_id = $category_id;
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
// '*.name' => 'required|unique:stock_funds_real_estate_exchanges,name'
|
|
];
|
|
}
|
|
|
|
public function customValidationMessages(Collection $rows)
|
|
{
|
|
$messages = [];
|
|
foreach ($rows as $key => $val) {
|
|
$messages['' . $key . '.name.required'] = 'Name should not be empty at row ' . $key + 1;
|
|
// $messages['' . $key . '.name.unique'] = 'Name is not unique at row ' . $key + 1;
|
|
}
|
|
return $messages;
|
|
}
|
|
|
|
public function collection(Collection $rows)
|
|
{
|
|
Validator::make($rows->toArray(), $this->rules(), $this->customValidationMessages($rows))->validate();
|
|
// $name = '';
|
|
// if($rows[0]['stock_name'] == null)
|
|
// {
|
|
// $name = 'stck';
|
|
// }
|
|
// else
|
|
// {
|
|
// $name = 'no';
|
|
// }
|
|
// dd($rows);
|
|
foreach ($rows as $row) {
|
|
$product = Product::create([
|
|
'tables_id' => Table::StockFundsRealEstateExchangeTable,
|
|
'categories_id' => $this->category_id,
|
|
'total_views' => 0,
|
|
'status' => 0,
|
|
'created_by' => auth()->user()->id,
|
|
'description'=> $row['description'] ?? null
|
|
]);
|
|
// $count = StockFundsRealEstateExchange::where('name',$row['stock_name'])->count();
|
|
$count = StockFundsRealEstateExchange::where('name',$row['name'])->count();
|
|
// $count = StockFundsRealEstateExchange::where(function($query) use($row){
|
|
// $query->orWhere('stock_name');
|
|
// $query->orWhere('etf_name');
|
|
// $query->orWhere('reit_name');
|
|
// })->count();
|
|
|
|
|
|
StockFundsRealEstateExchange::create([
|
|
'products_id' => $product->id,
|
|
// 'slug' => $count < 2 ? Str::slug($row['stock_name'] ?? $row['etf_name'] ?? $row['reit_name']) : Str::slug($row['stock_name'].'-'.$count+1 ?? $row['etf_name'].'-'.$count+1 ?? $row['reit_name'].'-'.$count+1),
|
|
// 'name' => $row['stock_name'] ?? $row['etf_name'] ?? $row['reit_name'],
|
|
// 'slug' => $count < 2 ? Str::slug($row['stock_name'] ?? $row['etf_name'] ?? $row['reit_name']) : Str::slug($row['stock_name'].'-'.$count+1 ?? $row['etf_name'].'-'.$count+1 ?? $row['reit_name'].'-'.$count+1),
|
|
'slug' => $count < 2 ? Str::slug($row['name']) : Str::slug($row['name'].'-'.$count+1),
|
|
'name' => $row['name'] ?? null,
|
|
// 'type' => $this->fundType,
|
|
'ticker' => $row['ticker'],
|
|
'exchange' => $row['exchange'],
|
|
'about' => $row['about_company'] ?? $row['about_etf'] ?? $row['about_the_reit'],
|
|
'industry' => $row['industry'] ?? null,
|
|
'market_cap' => $row['market_cap'] ?? null,
|
|
'pe_ratio' => $row['pe_ratio'] ?? null,
|
|
'dividend_yield' => $row['dividend_yield'],
|
|
'beta' => $row['beta'],
|
|
'provider' => $row['provider'] ?? null,
|
|
'category' => $row['category'] ?? null,
|
|
'expense_ratio' => $row['expense_ratio'] ?? null,
|
|
'month1_return' => $row['1_month_return'],
|
|
'month6_return' => $row['6_month_return'],
|
|
'year1_return' => $row['1_year_return'],
|
|
'year3_return' => $row['3_year_return'],
|
|
]);
|
|
}
|
|
}
|
|
}
|