Files
freeu-project/app/Http/Controllers/Frontend/MarketPlaceController.php
Ritikesh yadav c661166e1d first commit
2024-03-28 14:52:40 +05:30

411 lines
21 KiB
PHP

<?php
namespace App\Http\Controllers\Frontend;
use App\Http\Controllers\Controller;
use App\Models\MarketplaceAlternativeInvestmentFundSeller;
use App\Models\MarketplaceBuyerForm;
use App\Models\MarketplaceFractionalRealEstateSeller;
use App\Models\MarketplaceOtherProductsSeller;
use App\Models\MarketplaceSellerForm;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\User as ModelsUser;
use App\Notifications\UserAdmin;
class MarketPlaceController extends Controller
{
public function index()
{
$spotlightMarketPlaceListed = $this->spotlightInvestment()['data'];
$featuredMarketplaceListed = $this->featuredInvestments();
$nonFeaturedMarketplaceListed = $this->nonFeaturedInvestments();
return view('Frontend.Pages.marketplace.index', compact('spotlightMarketPlaceListed', 'featuredMarketplaceListed', 'nonFeaturedMarketplaceListed'));
}
public function spotlightInvestment()
{
$data = null;
if (MarketplaceFractionalRealEstateSeller::where('listing_status', 'Spotlight')->exists()) {
$data = MarketplaceFractionalRealEstateSeller::where('listing_status', 'Spotlight')->first();
$table = 'fractional-real-estate';
} elseif (MarketplaceAlternativeInvestmentFundSeller::where('listing_status', 'Spotlight')->exists()) {
$data = MarketplaceAlternativeInvestmentFundSeller::where('listing_status', 'Spotlight')->first();
$table = 'alternative-investment-fund';
} elseif (MarketplaceOtherProductsSeller::where('listing_status', 'Spotlight')->exists()) {
$data = MarketplaceOtherProductsSeller::where('listing_status', 'Spotlight')->first();
$table = 'other-products';
}
if ($data) {
$expectedSale = $data->getRawOriginal('expected_selling_price') ?? $data->getRawOriginal('expected_sale_per_unit') ?? $data->getRawOriginal('expected_sale_price_per_unit');
$data = [
'data' => [
'product_name' => $data->property_name ?? $data->name_of_the_aif_fund ?? $data->security_name,
'expected_sale' => $this->IND_money_format($expectedSale),
'category' => $data->type_of_fund ?? $data->product_category ?? 'Fractional Real Estate',
'slug' => $data->slug,
'table' => $table
]
];
} else {
$data = [
'data' => []
];
}
return $data;
}
public function featuredInvestments()
{
$freMarketplace['fractional-real-estate'] = MarketplaceFractionalRealEstateSeller::where(['status' => 'Approved', 'listing_status' => 'Featured'])
->select('id', 'property_name', 'asset_type', 'fractional_real_estate_platform', 'property_address', 'expected_selling_price', 'annual_rental_yield_earned', 'slug', 'rental_escalation', 'property_grade', 'current_market_value_of_the_property', 'id as sold_status', 'id as category', 'id as discount', 'id as bid')
->get();
$aifMarketplace['alternative-investment-fund'] = MarketplaceAlternativeInvestmentFundSeller::where(['status' => 'Approved', 'listing_status' => 'Featured'])
->select('id', 'name_of_the_aif_fund', 'current_or_latest_nav', 'credit_rating', 'fund_category', 'type_of_fund', 'expected_sale_per_unit', 'no_of_units_you_wish_to_sell', 'slug', 'id as sold_status', 'id as discount', 'id as bid')
->get();
$opMarketplace['other-products'] = MarketplaceOtherProductsSeller::where(['status' => 'Approved', 'listing_status' => 'Featured'])
->select('id', 'face_value_per_unit', 'security_name', 'product_category', 'instrument_issuer', 'no_of_units_offered_for_sale', 'expected_sale_price_per_unit', 'slug', 'principal_repaid', 'id as sold_status', 'payout_frequency', 'id as discount', 'id as bid')
->get();
$marketplaceListed = [];
array_push($marketplaceListed, $freMarketplace);
array_push($marketplaceListed, $aifMarketplace);
array_push($marketplaceListed, $opMarketplace);
return [
'data' => $marketplaceListed
];
// return [
// 'fractional-real-estate' => $freMarketplace,
// 'alternative-investment-fund' => $aifMarketplace,
// 'other-products' => $opMarketplace
// ];
}
public function nonFeaturedInvestments()
{
$freMarketplace['fractional-real-estate'] = MarketplaceFractionalRealEstateSeller::where(['status' => 'Approved', 'listing_status' => 'Non-Featured'])->select('id', 'property_name', 'asset_type', 'fractional_real_estate_platform', 'property_address', 'expected_selling_price', 'annual_rental_yield_earned', 'slug', 'rental_escalation', 'property_grade', 'current_market_value_of_the_property', 'id as sold_status', 'id as category', 'id as discount', 'id as bid')->get();
$aifMarketplace['alternative-investment-fund'] = MarketplaceAlternativeInvestmentFundSeller::where(['status' => 'Approved', 'listing_status' => 'Non-Featured'])->select('id', 'name_of_the_aif_fund', 'current_or_latest_nav', 'credit_rating', 'fund_category', 'type_of_fund', 'expected_sale_per_unit', 'no_of_units_you_wish_to_sell', 'slug', 'id as sold_status', 'id as discount', 'id as bid')
->get();
$opMarketplace['other-products'] = MarketplaceOtherProductsSeller::where(['status' => 'Approved', 'listing_status' => 'Non-Featured'])->select('id', 'face_value_per_unit', 'security_name', 'product_category', 'instrument_issuer', 'no_of_units_offered_for_sale', 'expected_sale_price_per_unit', 'slug', 'principal_repaid', 'id as sold_status', 'payout_frequency', 'id as discount', 'id as bid')
->get();
$marketplaceListed = [];
array_push($marketplaceListed, $freMarketplace);
array_push($marketplaceListed, $aifMarketplace);
array_push($marketplaceListed, $opMarketplace);
// return $marketplaceListed;
return [
'data' => $marketplaceListed
];
// return [
// 'fractional-real-estate' => $freMarketplace,
// 'alternative-investment-fund' => $aifMarketplace,
// 'other-products' => $opMarketplace
// ];
}
public function viewOffering($slug)
{
$offering = $this->viewOfferingData($slug);
$invested = $offering['invested'];
$totalInterestedBuyers = $offering['total-interested-buyers'];
$type = $offering['type'];
$ownProduct = $offering['own-product'];
$logged_in = auth()->guard('users')->check() == true ? true : false;
return view('Frontend.Pages.marketplace.view-offering', compact('offering', 'type', 'invested', 'totalInterestedBuyers', 'logged_in', 'ownProduct'));
}
public function viewOfferingData($slug)
{
// if(auth()->guard('users')->id())
// {
// }
$user_id = auth()->guard('users')->id();
$offering = array();
if (!auth()->guard('users')->check()) {
$offering['invested'] = false;
};
if (MarketplaceFractionalRealEstateSeller::where('slug', $slug)->exists()) {
$offering['type'] = 'fractional-real-estate';
$marketFRE = MarketplaceFractionalRealEstateSeller::where('slug', $slug)->first();
// if($user_id){
$offering['own-product'] = $user_id != null ? MarketplaceSellerForm::where(['id' => $marketFRE->seller_forms_id, 'users_id' => $user_id])->exists() : '';
// }
$offering['data'] = $marketFRE;
$offering['invested'] = $this->checkInvestmentInterested($offering['data'], 'marketplace_fre_sellers')['interested-status'];
$offering['total-interested-buyers'] = $this->checkInvestmentInterested($offering['data'], 'marketplace_fre_sellers')['total-interested-buyers'];
} elseif (MarketplaceAlternativeInvestmentFundSeller::where('slug', $slug)->exists()) {
$offering['type'] = 'alternative-investment-funds';
$marketAIF = MarketplaceAlternativeInvestmentFundSeller::where('slug', $slug)->first();
$offering['own-product'] = $user_id != null ? MarketplaceSellerForm::where(['id' => $marketAIF->seller_forms_id, 'users_id' => $user_id])->exists() : '';
$offering['data'] = $marketAIF;
$offering['invested'] = $this->checkInvestmentInterested($offering['data'], 'marketplace_aif_sellers')['interested-status'];
$offering['total-interested-buyers'] = $this->checkInvestmentInterested($offering['data'], 'marketplace_aif_sellers')['total-interested-buyers'];
} elseif (MarketplaceOtherProductsSeller::where('slug', $slug)->exists()) {
$offering['type'] = 'other-products';
$marketOP = MarketplaceOtherProductsSeller::where('slug', $slug)->first();
$offering['own-product'] = $user_id != null ? MarketplaceSellerForm::where(['id' => $marketOP->seller_forms_id, 'users_id' => $user_id])->exists() : '';
$offering['data'] = $marketOP;
$offering['invested'] = $this->checkInvestmentInterested($offering['data'], 'marketplace_op_sellers')['interested-status'];
$offering['total-interested-buyers'] = $this->checkInvestmentInterested($offering['data'], 'marketplace_op_sellers')['total-interested-buyers'];
}
return $offering;
}
public function checkInvestmentInterested($offering, $table)
{
$interestedStatus = false;
if (auth()->guard('users')->check()) {
$interestedStatus = MarketplaceBuyerForm::where(['users_id' => auth()->guard('users')->user()->id, 'table' => $table, 'associated_id' => $offering->id])->exists();
}
$totalInterestedBuyers = MarketplaceBuyerForm::where(['table' => $table, 'associated_id' => $offering->id])->count();
return [
'total-interested-buyers' => $totalInterestedBuyers . ' buyer(s) interested!',
'interested-status' => $interestedStatus
];
}
public function buyerForm($slug)
{
$offering = $this->viewOfferingData($slug);
$table = $offering['type'];
$id = $offering['data']->id;
$userData = array();
$user = ModelsUser::find(auth()->guard('users')->user()->id);
$userData = (object)[
'name' => $user->name ?? null,
'contact_number' => $user->contact_number ?? null,
'email' => $user->email ?? $user->email,
];
return view('Frontend.Pages.profile.market-list.buyer-form', compact('table', 'id', 'userData'));
}
public function buyerFormSubmit(Request $request)
{
$validator = Validator::make($request->all(), [
'id' => 'required',
'table' => 'required',
'name' => 'required',
'city' => 'required',
'country' => 'required',
'contact_number' => 'required|numeric|digits:10',
'email_id' => 'required',
'no_of_units_you_wish_to_buy' => 'required|numeric',
'offer_price_per_unit' => 'required|numeric',
], [
'required' => 'The :attribute field must be required',
'numeric' => 'The :attribute field must be in digits',
'digits' => 'The :attribute field must have 10 digits',
]);
$validationMessage = $this->validationError($validator);
if ($validationMessage) {
return response()->json(['status' => 400, 'message' => $validationMessage]);
}
if ($request->table == 'fractional-real-estate') {
$table = 'marketplace_fre_sellers';
$productName = \DB::table($table)->where('id', $request->id)->value('property_name');
} elseif ($request->table == 'alternative-investment-funds') {
$table = 'marketplace_aif_sellers';
$productName = \DB::table($table)->where('id', $request->id)->value('name_of_the_aif_fund');
} elseif ($request->table == 'other-products') {
$table = 'marketplace_op_sellers';
$productName = \DB::table($table)->where('id', $request->id)->value('security_name');
}
$buyerForm = MarketplaceBuyerForm::create([
'users_id' => auth()->guard('users')->user()->id,
'associated_id' => $request->id,
'table' => $table,
'name' => $request->name,
'city' => $request->city,
'country' => $request->country,
'contact_number' => $request->contact_number,
'email_id' => $request->email_id,
'no_of_units_you_wish_to_buy' => $request->no_of_units_you_wish_to_buy,
'offer_price_per_unit' => $request->offer_price_per_unit,
'total_purchase_value' => $request->total_purchase_value,
]);
if ($buyerForm) {
$name = auth()->guard('users')->user()->name;
$notify['message'] = "$name has submitted a buyer form for $productName!";
$type = 'Buyer Form';
$users = ModelsUser::admins()->get();
foreach ($users as $data) {
$data->notify(new UserAdmin($notify, $type));
}
return response()->json(['status' => 200, 'message' => "Buyer Form Submitted For Review"]);
}
return response()->json(['status' => 400, 'message' => "Buyer Form Could Not Be Submitted!"]);
}
public function buyerFormAPI($slug)
{
$offering = $this->viewOfferingData($slug);
$table = $offering['type'];
$id = $offering['data']->id;
$userData = array();
$user = ModelsUser::find(request()->user()->id);
$userData = (object)[
'name' => $user->name ?? null,
'contact_number' => $user->contact_number ?? null,
'email' => $user->email ?? $user->email,
];
$data['data'] = [
'table' => $offering['type'],
'id' => $offering['data']->id,
'user' => $userData
];
return $data;
}
public function buyerFormSubmitAPI(Request $request)
{
$validator = Validator::make($request->all(), [
'id' => 'required',
'table' => 'required',
'name' => 'required',
'city' => 'required',
'country' => 'required',
'contact_number' => 'required|numeric|digits:10',
'email_id' => 'required',
'no_of_units_you_wish_to_buy' => 'required|numeric',
'offer_price_per_unit' => 'required|numeric',
], [
'required' => 'The :attribute field must be required',
'numeric' => 'The :attribute field must be in digits',
'digits' => 'The :attribute field must have 10 digits',
]);
$validationMessage = $this->validationError($validator);
if ($validationMessage) {
return response()->json(['status' => 400, 'message' => $validationMessage], 400);
}
if ($request->table == 'fractional-real-estate') {
$table = 'marketplace_fre_sellers';
$productName = \DB::table($table)->where('id', $request->id)->value('property_name');
} elseif ($request->table == 'alternative-investment-funds') {
$table = 'marketplace_aif_sellers';
$productName = \DB::table($table)->where('id', $request->id)->value('name_of_the_aif_fund');
} elseif ($request->table == 'other-products') {
$table = 'marketplace_op_sellers';
$productName = \DB::table($table)->where('id', $request->id)->value('security_name');
}
$buyerForm = MarketplaceBuyerForm::create([
'users_id' => $request->user()->id,
'associated_id' => $request->id,
'table' => $table,
'name' => $request->name,
'city' => $request->city,
'country' => $request->country,
'contact_number' => $request->contact_number,
'email_id' => $request->email_id,
'no_of_units_you_wish_to_buy' => $request->no_of_units_you_wish_to_buy,
'offer_price_per_unit' => $request->offer_price_per_unit,
// 'total_purchase_value' => $request->total_purchase_value,
'total_purchase_value' => $request->no_of_units_you_wish_to_buy * $request->offer_price_per_unit
]);
$user = ModelsUser::find($request->user()->id);
if ($buyerForm) {
$name = $user->name;
$notify['message'] = "$name has submitted a buyer form for $productName!";
$type = 'Buyer Form';
$users = ModelsUser::admins()->get();
foreach ($users as $data) {
$data->notify(new UserAdmin($notify, $type));
}
return response()->json(['status' => 200, 'message' => "Buyer Form Submitted For Review"]);
}
return response()->json(['status' => 400, 'message' => "Buyer Form Could Not Be Submitted!"], 400);
}
public function viewOfferingDataAPI($slug)
{
$offering = array();
if (request()->user()) {
$offering['invested'] = false;
};
if (MarketplaceFractionalRealEstateSeller::where('slug', $slug)->exists()) {
$offering['type'] = 'fractional-real-estate';
$marketFRE = MarketplaceFractionalRealEstateSeller::where('slug', $slug)->first();
$offering['own-product'] = MarketplaceSellerForm::where(['id' => $marketFRE->seller_forms_id, 'users_id' => auth('sanctum')->id()])->exists();
$offering['data'] = $marketFRE;
$offering['invested'] = $this->checkInvestmentInterestedAPI($offering['data'], 'marketplace_fre_sellers')['interested-status'];
$offering['total-interested-buyers'] = $this->checkInvestmentInterestedAPI($offering['data'], 'marketplace_fre_sellers')['total-interested-buyers'];
} elseif (MarketplaceAlternativeInvestmentFundSeller::where('slug', $slug)->exists()) {
$offering['type'] = 'alternative-investment-funds';
$marketAIF = MarketplaceAlternativeInvestmentFundSeller::where('slug', $slug)->first();
$offering['own-product'] = MarketplaceSellerForm::where(['id' => $marketAIF->seller_forms_id, 'users_id' => auth('sanctum')->id()])->exists();
$offering['data'] = $marketAIF;
$offering['invested'] = $this->checkInvestmentInterestedAPI($offering['data'], 'marketplace_aif_sellers')['interested-status'];
$offering['total-interested-buyers'] = $this->checkInvestmentInterestedAPI($offering['data'], 'marketplace_aif_sellers')['total-interested-buyers'];
} elseif (MarketplaceOtherProductsSeller::where('slug', $slug)->exists()) {
$offering['type'] = 'other-products';
$marketOP = MarketplaceOtherProductsSeller::where('slug', $slug)->first();
$offering['own-product'] = MarketplaceSellerForm::where(['id' => $marketOP->seller_forms_id, 'users_id' => auth('sanctum')->id()])->exists();
$offering['data'] = $marketOP;
$offering['invested'] = $this->checkInvestmentInterestedAPI($offering['data'], 'marketplace_op_sellers')['interested-status'];
$offering['total-interested-buyers'] = $this->checkInvestmentInterestedAPI($offering['data'], 'marketplace_op_sellers')['total-interested-buyers'];
}
return $offering;
}
public function checkInvestmentInterestedAPI($offering, $table)
{
$interestedStatus = false;
if (request()->user()) {
$interestedStatus = MarketplaceBuyerForm::where(['users_id' => request()->user()->id, 'table' => $table, 'associated_id' => $offering->id])->exists();
}
$totalInterestedBuyers = MarketplaceBuyerForm::where(['table' => $table, 'associated_id' => $offering->id])->count();
return [
'total-interested-buyers' => $totalInterestedBuyers . ' buyer(s) interested!',
'interested-status' => $interestedStatus
];
}
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 validationError($validator)
{
if ($validator->fails()) {
$errors = $validator->errors();
$messages = '';
foreach ($errors->all() as $message) {
$messages .= $message . '</br>';
}
return $messages;
}
}
}