Files
freeu-project/app/Models/MarketplaceFractionalRealEstateSeller.php
2024-05-13 14:00:37 +05:30

97 lines
3.8 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;
class MarketplaceFractionalRealEstateSeller extends Model
{
use HasFactory, Sluggable;
protected $table = 'marketplace_fre_sellers';
protected $appends = ['sold_status', 'category', 'discount', 'bid'];
protected $fillable = ['seller_forms_id','property_name','slug','property_address','property_grade','asset_type','annual_rental_yield_earned','rental_escalation','fractional_real_estate_platform','date_of_investment','original_amount_invested','current_market_value_of_the_property','expected_selling_price', 'og_current_market_value_of_the_property', 'og_expected_selling_price', '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 company(){
return $this->belongsTo(Company::class,'fractional_real_estate_platform');
}
public function Sluggable(): array
{
return [
'slug' => [
"source" => 'property_name',
'onUpdate' => true
]
];
}
public function getCategoryAttribute()
{
return 'Fractional Real Estate';
}
public function getDiscountAttribute($id){
$values = MarketplaceFractionalRealEstateSeller::where('id', $this->id)->first();
// dd($values->expected_selling_price);
// return (string)round(((int)$values->expected_selling_price - (int)$values->current_market_value_of_the_property)/(int)$values->current_market_value_of_the_property * 100,3);
if($values->current_market_value_of_the_property <= 0)
{
return (string)0;
}
return (string)round((intval($values->expected_selling_price) - intval($values->current_market_value_of_the_property))/intval($values->current_market_value_of_the_property) * 100,3);
}
public function getBidAttribute($id){
$values = MarketplaceFractionalRealEstateSeller::where('id', $this->id)->first();
if($values->current_market_value_of_the_property <= 0)
{
return 0;
}
$bids = MarketplaceBuyerForm::where(['associated_id' => $this->id, 'table' => 'marketplace_fre_sellers'])->where('status','!=','Sold')->get();
$bidArr = [];
foreach($bids as $bid){
$bidArr[] = round(($bid->getAttributes()['total_purchase_value'] - $this->current_market_value_of_the_property)/$this->current_market_value_of_the_property * 100,3);
}
if($bidArr){
return max((int)$bidArr);
}
return 0;
}
public function getSoldStatusAttribute($id)
{
// return MarketplaceBuyerForm::where('associated_id', $id)->where('status', 'Sold')->exists() ? 'SOLD' : 'OPEN';
// dd($id);
// $buyerData = MarketplaceBuyerForm::where('associated_id', $this->id)->where('status', 'Sold')->get();
// $totalSellUnits = 0;
// $buyerData->each(function($data) use($totalSellUnits){
// return $totalSellUnits += (int)$data->no_of_units_you_wish_to_buy;
// });
if(MarketplaceFractionalRealEstateSeller::where('id',$id)->exists())
{
$getFREData = MarketplaceFractionalRealEstateSeller::where('id',$id)->first();
// $aifData = (int)$getFREData->no_of_units_you_wish_to_sell;
// $remainUnits = $aifData - $totalSellUnits;
if($getFREData->current_market_value_of_the_property <= 0)
{
return 'SOLD';
}
return 'OPEN';
}else{
return 'OPEN';
}
}
}