Files
freeu-project/app/Models/MarketplaceAlternativeInvestmentFundSeller.php
Ritikesh yadav 9a47695a1c added image key
2024-06-04 16:10:48 +05:30

185 lines
6.8 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;
class MarketplaceAlternativeInvestmentFundSeller extends Model
{
use HasFactory, Sluggable;
protected $table = 'marketplace_aif_sellers';
protected $appends = ['sold_status', 'discount', 'bid'];
protected $fillable = ['seller_forms_id', 'name_of_the_aif_fund', 'slug', 'fund_category', 'fund_structure', 'type_of_fund', 'fund_strategy', 'fund_manager_name', 'sponsor', 'credit_rating', 'total_capital_commitment', 'uncalled_capital_commitment', 'date_of_final_close', 'tenure_from_final_close', 'current_or_latest_nav', 'no_of_units_held', 'no_of_units_you_wish_to_sell', 'og_no_of_units_wish_to_sell', 'expected_sale_per_unit', 'latest_valuation_date', 'status', 'listing_status'];
public function seller()
{
return $this->belongsTo(MarketplaceSellerForm::class, 'seller_forms_id');
}
public function buyer()
{
return $this->belongsTo(MarketplaceBuyerForm::class, 'seller_forms_id');
}
public function Sluggable(): array
{
return [
'slug' => [
"source" => 'name_of_the_aif_fund',
'onUpdate' => true
]
];
}
public function getTotalCapitalCommitmentAttribute($value)
{
return $this->IND_money_format($value);
}
public function getUncalledCapitalCommitmentAttribute($value)
{
return $this->IND_money_format($value);
}
public function getExpectedSalePerUnitAttribute($value)
{
return $this->IND_money_format($value);
}
public function getCapitalOrLatestNavAttribute($value)
{
return $this->IND_money_format($value);
}
function IND_money_format($number)
{
$decimal = (string)($number - floor($number));
$money = floor($number);
$length = strlen($money);
$delimiter = '';
$money = strrev($money);
for ($i = 0; $i < $length; $i++) {
if (($i == 3 || ($i > 3 && ($i - 1) % 2 == 0)) && $i != $length) {
$delimiter .= ',';
}
$delimiter .= $money[$i];
}
$result = strrev($delimiter);
$decimal = preg_replace("/0\./i", ".", $decimal);
$decimal = substr($decimal, 0, 3);
if ($decimal != '0') {
$result = $result . $decimal;
}
return '₹ ' . $result;
}
public function company()
{
return $this->belongsTo(Company::class, 'platform');
}
public function getSoldStatusAttribute($id)
{
// return MarketplaceBuyerForm::where('associated_id', $id)->where('status', 'Sold')->exists() ? 'SOLD' : 'OPEN';
$id = $this->id;
$checkBIDExist = MarketplaceBuyerForm::where('associated_id', $id)->where('status', 'Sold')->exists();
if($checkBIDExist)
{
$buyerData = MarketplaceBuyerForm::where('associated_id', $id)->where('status', 'Sold')->get();
// dd($buyerData->toArray());
$totalSellUnits = 0;
$buyerData->each(function($data) use($totalSellUnits){
return $totalSellUnits += (int)$data->no_of_units_you_wish_to_buy;
});
if(MarketplaceAlternativeInvestmentFundSeller::where('id',$buyerData[0]->associated_id)->exists())
{
$getAIFData = MarketplaceAlternativeInvestmentFundSeller::where('id',$buyerData[0]->associated_id)->first();
$aifData = (int)$getAIFData->no_of_units_you_wish_to_sell;
$remainUnits = $aifData - $totalSellUnits;
// if($remainUnits == 0 || $remainUnits < 0)
if($remainUnits <= 0)
{
return 'SOLD';
}else{
return 'OPEN';
}
}
}else{
return 'OPEN';
}
}
public function getDiscountAttribute($id)
{
$values = MarketplaceAlternativeInvestmentFundSeller::where('id', $this->id)->first();
if($values->no_of_units_you_wish_to_sell <= 0)
{
return (string)0;
}
$espu = $this->getAttributes()['expected_sale_per_unit'];
// $coln = intval($this->current_or_latest_nav);
// $minus = $espu ? $espu - $coln : $coln - $espu ;
// $divide = (int)$minus / (int)$this->current_or_latest_nav;
// $multiply = (int)$divide;
// return (string)round($total, 3);
// return $espu;
// dd($espu);
if((int)$this->current_or_latest_nav <= 0)
{
return (string)0;
}
return (string)round(($espu - (int)$this->current_or_latest_nav) / (int)$this->current_or_latest_nav * 100, 3);
// dd((string)round((intval($this->expected_sale_per_unit) - intval($this->current_or_latest_nav)) / intval($this->current_or_latest_nav) * 100, 3));
}
public function getBidAttribute($id)
{
$values = MarketplaceAlternativeInvestmentFundSeller::where('id', $this->id)->first();
if($values->no_of_units_you_wish_to_sell <= 0)
{
return (string)0;
}
$bids = MarketplaceBuyerForm::where(['associated_id' => $this->id, 'table' => 'marketplace_aif_sellers'])->where('status','!=','Sold')->get();
// $bids = MarketplaceBuyerForm::where(['associated_id' => $this->id, 'table' => 'marketplace_aif_sellers'])->get();
$bidArray = collect();
$bids->each(function($value) use ($bidArray,$values){
$bidArray->push($value->getAttributes()['offer_price_per_unit']);
// if($value->no_of_units_you_wish_to_buy < $values->no_of_units_you_wish_to_sell)
// {
// // $bidArray->push($value->getAttributes()['total_purchase_value']);
// $bidArray->push($value->getAttributes()['offer_price_per_unit']);
// }
});
$noOfUnitHeld = (int)$values->current_or_latest_nav;
// $nav = intval($noOfUnitHeld) * intval($values->current_or_latest_nav);
$highestBid = intval($bidArray->max());
// $bid = ($highestBid - $nav)/(int)$nav * 100 ;
$bid = $highestBid != 0 ? ($highestBid - (int)$noOfUnitHeld)/(int)$noOfUnitHeld * 100 : 0 ;
// dd($bid);
if(count($bids))
{
$latestBid = round($bid,3);
return (string)$latestBid;
}
// dd($bids->toArray());
// $bidArr = [];
// foreach ($bids as $bid) {
// $bidArr[] = round(($bid->getAttributes()['total_purchase_value'] - $this->current_or_latest_nav) / $this->current_or_latest_nav * 100, 3);
// }
// if ($bidArr) {
// return (string)max($bidArr);
// }
return (string)0;
}
}