2024-03-28 14:52:40 +05:30
< ? php
namespace App\Http\Controllers\Admin ;
use App\Models\Product ;
use Illuminate\Http\Request ;
use Illuminate\Support\Facades\DB ;
use App\Http\Controllers\Controller ;
2024-06-04 14:45:27 +05:30
use App\Models\AlternativeInvestmentFund ;
use App\Models\FractionalRealEstate ;
2024-03-28 14:52:40 +05:30
use App\Models\MonthlyUpdateAlternativeInvestmentFund ;
use App\Models\MonthlyUpdateFractionalRealEstate ;
use App\Models\MonthlyUpdateIndianFinancialAssets ;
use App\Models\MonthlyUpdateMaster ;
use App\Models\MonthlyUpdateMasterCommission ;
use App\Models\ProductTrailCommission ;
use App\Services\Admin\CommissionService ;
use App\Traits\HttpResponse ;
use Illuminate\Support\Facades\Validator ;
use Yajra\DataTables\Contracts\DataTable ;
use Yajra\DataTables\Facades\DataTables ;
use Illuminate\Support\Carbon ;
use Carbon\CarbonPeriod ;
2024-06-12 19:45:21 +05:30
use File ;
2024-03-28 14:52:40 +05:30
class ManageCommissionController extends Controller
{
use HttpResponse ;
protected $commission ;
public function __construct ( CommissionService $commission )
{
$this -> commission = $commission ;
}
public function index ()
{
2024-04-09 17:56:07 +05:30
$check = checkSidebarAccess ( 'product-commission' );
if ( ! $check ) {
abort ( 404 );
}
2024-03-28 14:52:40 +05:30
$products = Product :: query ()
2024-05-30 17:57:20 +05:30
-> select ( DB :: raw ( 'products.id, coalesce(fund.fund_name, re.property_name, sdi.product_name, p2p.scheme, fre.property_name_and_location,id.company_name,aif.fund_name,caga.project_name,hyf.security_name,lbf.company,sdi.product_name,vd.company_name) as product_name' ), DB :: raw ( 'DATE_FORMAT(products.created_at, "%d-%b-%Y") as date' ), 'categories.category_name' , DB :: raw ( 'coalesce(sdi.minimum_investment,re.total_price, p2p.minimum_investment, fre.minimum_investment,id.minimum_investment,aif.minimum_investment,caga.minimum_investment,hyf.minimum_investment,lbf.minimum_investment,sdi.minimum_investment,vd.minimum_investment,fund.minimum_investment) as minimum_investment' ), 'products.commission_type' , 'products.rate' , 'products.commission' , 'products.description' , DB :: raw ( 'coalesce(fre.companies_id,aif.companies_id,caga.companies_id,vd.companies_id) as companies_id' ), 'upfront_rate' , 'trail_rate' )
2024-03-28 14:52:40 +05:30
-> leftJoin ( 'securitized_debt_instruments as sdi' , 'products.id' , 'sdi.products_id' )
-> leftJoin ( 'fractional_real_estates as fre' , 'products.id' , 'fre.products_id' )
-> leftJoin ( 'peer_to_peer_lendings as p2p' , 'products.id' , 'p2p.products_id' )
-> leftJoin ( 'invoice_discountings as id' , 'products.id' , 'id.products_id' )
-> leftJoin ( 'alternative_investment_funds as aif' , 'products.id' , 'aif.products_id' )
-> leftJoin ( 'clean_and_green_assets as caga' , 'products.id' , 'caga.products_id' )
-> leftJoin ( 'high_yield_finances as hyf' , 'products.id' , 'hyf.products_id' )
-> leftJoin ( 'lease_based_financings as lbf' , 'products.id' , 'lbf.products_id' )
-> leftJoin ( 'venture_debts as vd' , 'products.id' , 'vd.products_id' )
2024-05-30 17:57:20 +05:30
-> leftJoin ( 'funds as fund' , 'products.id' , 'fund.products_id' )
-> leftJoin ( 'real_estates as re' , 'products.id' , 're.products_id' )
2024-03-28 14:52:40 +05:30
// ->join('fractional_real_estates as fr', 'companies.id', 'fr.companies_id')
// ->leftJoin('alternative_investment_funds as aifp', 'companies.id', 'aifp.companies_id')
// ->leftJoin('clean_and_green_assets as cagap', 'companies.id', 'cagap.companies_id')
// ->leftJoin('venture_debts as vdp', 'companies.id', 'vdp.companies_id')
-> join ( 'categories' , 'products.categories_id' , 'categories.id' )
// ->orderBy('products.created_at')
-> latest ( 'products.created_at' )
-> get ();
2024-06-03 17:25:36 +05:30
// dd($products);
$products -> each ( function ( $value ) {
$monthlyData = MonthlyUpdateMaster :: where ( 'products_id' , $value -> id ) -> get ();
2024-05-31 15:52:03 +05:30
$totalCommission = 0 ;
2024-05-31 18:46:56 +05:30
$totalInvestment = 0 ;
2024-06-03 17:25:36 +05:30
if ( $monthlyData -> isNotEmpty ()) {
foreach ( $monthlyData as $monthlyDataRow ) {
if ( MonthlyUpdateFractionalRealEstate :: where ( 'custom_id' , $monthlyDataRow -> custom_id ) -> exists ()) {
$freData = MonthlyUpdateFractionalRealEstate :: where ( 'custom_id' , $monthlyDataRow -> custom_id ) -> first ();
2024-05-31 18:46:56 +05:30
$totalInvestment += floatval ( $freData -> getRawOriginal ()[ 'investment_value' ]);
}
2024-06-03 17:25:36 +05:30
if ( MonthlyUpdateAlternativeInvestmentFund :: where ( 'custom_id' , $monthlyDataRow -> custom_id ) -> exists ()) {
$aifData = MonthlyUpdateAlternativeInvestmentFund :: where ( 'custom_id' , $monthlyDataRow -> custom_id ) -> first ();
2024-05-31 18:46:56 +05:30
$totalInvestment += floatval ( $aifData -> getRawOriginal ()[ 'commitment_amount' ]);
}
2024-06-03 17:25:36 +05:30
$data = MonthlyUpdateMasterCommission :: where ( 'monthly_id' , $monthlyDataRow -> id ) -> get ();
if ( $data -> isNotEmpty ()) {
foreach ( $data as $dataRow ) {
2024-05-31 15:52:03 +05:30
$totalCommission += floatval ( $dataRow -> getRawOriginal ()[ 'gross_commissioned_earned_inr' ]);
}
}
}
}
2024-05-31 18:46:56 +05:30
$value -> totalInvestment = $totalInvestment ;
2024-05-31 15:52:03 +05:30
$value -> totalCommission = $totalCommission ;
});
2024-05-31 18:46:56 +05:30
// dd($products->)
2024-03-28 14:52:40 +05:30
return view ( 'Admin.Pages.manage_commission.manage_commission' , compact ( 'products' ));
}
public function add ()
{
return view ( 'Admin.Pages.manage_commission.add_commission' );
}
public function viewProduct ( $id , $product = 'product' )
{
2024-06-12 19:42:52 +05:30
// dd($id,$product);
2024-03-28 14:52:40 +05:30
return view ( 'Admin.Pages.manage_commission.view_commission' , [
'product' => Product :: with ( 'category' ) -> find ( $id ),
'product_name' => $product ,
]);
}
public function editCommission ( $id , $product = 'product' )
{
// dd($product);
// dd(Product::find($id));
// $product = Product::find($id)
return view ( 'Admin.Pages.manage_commission.edit_commission' , [
'product' => Product :: find ( $id ),
'product_name' => $product ,
]);
}
public function editProduct ( Request $request )
{
// dd($request->all());
$validator = Validator :: make ( $request -> post (), [
'commission_type' => 'required' ,
// 'profit_sharing' => 'required',
// 'upfront_rate' => 'required',
// 'trail_rate' => 'required',
'gst' => 'required' ,
'tds' => 'required' ,
// 'end_of_trail' => 'required',
], [
'required' => 'The :attribute field must be required' ,
'unique' => 'The :attribute field must be unique'
]);
$validationMessage = $this -> validationError ( $validator );
if ( $validationMessage ) {
return response () -> json ([ 'status' => 400 , 'message' => $validationMessage ]);
}
2024-06-07 10:58:43 +05:30
$product = Product :: where ( 'id' , $request -> products_id ) -> first ();
$productpresentation = '' ;
if ( $request -> has ( 'channel_partner_agreement' )) {
if ( $product && $product -> channel_partner_aggrement ) {
if ( \File :: exists ( public_path ( '/uploads/product/channel_partner_agreement/' . $product -> channel_partner_aggrement ))) {
\File :: delete ( public_path ( '/uploads/product/channel_partner_agreement/' . $product -> channel_partner_aggrement ));
}
}
$productpresentation .= time () . '_partner_agreement' . '.' . $request -> channel_partner_agreement -> extension ();
// dd($productpresentation);
$request -> channel_partner_agreement -> move ( public_path ( '/uploads/product/channel_partner_agreement' ), $productpresentation );
}
// dd($productpresentation);
2024-03-28 14:52:40 +05:30
Product :: where ( 'id' , $request -> products_id ) -> update ([
'commission_type' => $request -> commission_type ,
'profit_sharing' => $request -> profit_sharing ,
'upfront_rate' => $request -> upfront_rate ,
'return_type' => $request -> return_type ,
'trail_rate' => $request -> trail_rate ,
'gst' => $request -> gst ,
'tds' => $request -> tds ,
'end_of_trail' => $request -> end_of_trail ,
2024-06-07 10:58:43 +05:30
'channel_partner_aggrement' => $productpresentation ,
2024-03-28 14:52:40 +05:30
]);
2024-06-07 10:58:43 +05:30
2024-03-28 14:52:40 +05:30
$returnType = [
'Monthly' => '1 month' ,
'Quarterly' => '3 months' ,
'Half-Yearly' => '6 months' ,
'Yearly' => '12 months' ,
];
if ( $request -> commission_type == 'Trail Commission' || $request -> commission_type == 'Hybrid Structure' ) {
$start = Carbon :: today ();
ProductTrailCommission :: where ([ 'products_id' => $request -> products_id , 'commission_earned' => 'No' ]) -> delete ();
$getDates = ProductTrailCommission :: where ( 'products_id' , $request -> products_id ) -> pluck ( 'commission_date' );
foreach ( CarbonPeriod :: create ( $start , $returnType [ $request -> return_type ], $request -> end_of_trail . ' 00:00:00.0' ) as $month ) {
$month -> lastOfMonth ();
if ( $getDates -> contains ( $month -> format ( 'Y-m-d h:i:s' ))) {
continue ;
}
ProductTrailCommission :: create ([
'products_id' => $request -> products_id ,
'commission_date' => $month -> format ( 'Y-m-d h:i:s' )
]);
}
}
return response () -> json ([ 'status' => 200 , 'message' => 'Product Commission Updated!' ]);
}
public function validationError ( $validator )
{
if ( $validator -> fails ()) {
$errors = $validator -> errors ();
$messages = '' ;
foreach ( $errors -> all () as $message ) {
$messages .= $message . '</br>' ;
}
return $messages ;
}
}
public function userCommission ()
{
2024-04-09 17:56:07 +05:30
$check = checkSidebarAccess ( 'user-commission' );
if ( ! $check ) {
abort ( 404 );
}
2024-03-28 14:52:40 +05:30
return view ( 'Admin.Pages.manage_commission.user_commission' );
}
public function viewUserProduct ( $id )
{
2024-06-04 14:45:27 +05:30
$data = MonthlyUpdateMasterCommission :: with ( 'monthlyUpdate' ) -> where ( 'id' , $id ) -> first ();
if ( $data )
{
if ( AlternativeInvestmentFund :: where ( 'products_id' , $data -> monthlyUpdate -> products_id ) -> exists ())
{
$data -> product_name = AlternativeInvestmentFund :: where ( 'products_id' , $data -> monthlyUpdate -> products_id ) -> value ( 'fund_name' );
}
if ( FractionalRealEstate :: where ( 'products_id' , $data -> monthlyUpdate -> products_id ) -> exists ())
{
$data -> product_name = FractionalRealEstate :: where ( 'products_id' , $data -> monthlyUpdate -> products_id ) -> value ( 'property_name_and_location' );
}
}
// dd($data);
2024-03-28 14:52:40 +05:30
return view ( 'Admin.Pages.manage_commission.view_user_commission' , [
2024-06-04 14:45:27 +05:30
'product' => $data
2024-03-28 14:52:40 +05:30
]);
}
public function editUserCommission ( $id )
{
return view ( 'Admin.Pages.manage_commission.edit_user_commission' , [
'product' => MonthlyUpdateMasterCommission :: find ( $id )
]);
}
public function updateUserCommission ( Request $request )
{
$validator = Validator :: make ( $request -> post (), [
'total_investment_or_commitment_amount' => 'required' ,
'applicable_rate' => 'required' ,
'gst' => 'required' ,
'tds' => 'required' ,
], [
'required' => 'The :attribute field must be required' ,
'unique' => 'The :attribute field must be unique'
]);
$validationMessage = $this -> validationError ( $validator );
if ( $validationMessage ) {
return response () -> json ([ 'status' => 400 , 'message' => $validationMessage ]);
}
$grossCommissionedEarned = ( $request -> total_investment_or_commitment_amount * $request -> applicable_rate ) / 100 ;
$gst = ( $grossCommissionedEarned * $request -> gst ) / 100 ;
$tds = ( $grossCommissionedEarned * $request -> tds ) / 100 ;
MonthlyUpdateMasterCommission :: where ( 'id' , $request -> monthly_update_commissions_id ) -> update ([
'total_investment_or_commitment_amount' => $request -> total_investment_or_commitment_amount ,
'applicable_rate' => $request -> applicable_rate ,
'gst' => $request -> gst ,
'tds' => $request -> tds ,
'net_commission_received' => $grossCommissionedEarned + $gst - $tds ,
'gross_commissioned_earned_inr' => $grossCommissionedEarned ,
]);
return response () -> json ([ 'status' => 200 , 'message' => 'User Commission Updated!' ]);
}
public function dataTable ( Request $request )
{
if ( $request -> ajax ()) {
$data = MonthlyUpdateMasterCommission :: query ()
-> with ( 'monthlyUpdate.products.fractional_real_estate' )
-> latest ()
-> select ( '*' );
if ( $request -> from ) {
$data -> where ( 'created_at' , '>=' , $request -> from . ' 00:00:00' );
};
if ( $request -> to ) {
$data -> where ( 'created_at' , '<=' , $request -> to . ' 23:59:59' );
};
return DataTables :: of ( $data ) -> addIndexColumn ()
-> editColumn ( 'created_at' , function ( $row ) {
$formattedDate = $row -> created_at -> format ( 'd/m/Y' );
return '<div class="badge badge-light fw-bold">' . $formattedDate . '</div>' ;
})
-> editColumn ( 'investor_name' , function ( $row ) {
return '<div class="text-center">' . $row -> monthlyUpdate -> investor_name . ' </div>' ;
})
-> editColumn ( 'product_sold' , function ( $row ) {
2024-06-03 17:25:36 +05:30
return '<div class="text-center">' . $row -> monthlyUpdate -> product_name . '</div>' ;
2024-03-28 14:52:40 +05:30
})
-> editColumn ( 'month' , function ( $row ) {
return '<div class="text-center">' . $row -> created_at -> format ( 'M' ) . ' </div>' ;
})
-> editColumn ( 'total_investment' , function ( $row ) {
return '<div class="text-center">' . $row -> total_investment_or_commitment_amount . ' </div>' ;
})
-> editColumn ( 'applicable_rate' , function ( $row ) {
return '<div class="text-center">' . $row -> applicable_rate . '% </div>' ;
})
-> editColumn ( 'commission_type' , function ( $row ) {
return '<div class="text-center">' . $row -> type_of_commission . ' </div>' ;
})
-> addColumn ( 'action' , function ( $row ) {
$view = route ( " get-user-commission " , $row -> id );
$edit = route ( " edit-user-commission " , $row -> id );
$btn = " <div class='text-end d-flex align-items-center justify-content-around'>
< a href = $view >
< i class = 'fa-regular fa-eye' ></ i >
</ a >
</ div >
" ;
2024-06-03 17:25:36 +05:30
// <a href=$edit style='float:right'>
// <i class='fa-solid fa-pen-to-square'></i>
// </a>
2024-03-28 14:52:40 +05:30
return $btn ;
})
-> rawColumns ([ 'investor_name' , 'product_sold' , 'month' , 'total_investment' , 'applicable_rate' , 'commission_type' , 'created_at' , 'action' ])
-> make ( true );
}
}
public function trailCommission ()
{
2024-04-09 17:56:07 +05:30
$check = checkSidebarAccess ( 'trail-commission' );
2024-06-03 17:25:36 +05:30
if ( ! $check ) {
2024-04-09 17:56:07 +05:30
abort ( 404 );
}
2024-03-28 14:52:40 +05:30
return view ( 'Admin.Pages.manage_commission.trail_commission' , [
'trailCommission' => $this -> commission -> getTrailCommissions ()
]);
}
public function generateTrailCommission ( Request $request )
{
$monthlyUpdate = $this -> commission -> getMonthlyUpdate ( $request -> id );
$commissionUpdated = $this -> commission -> updateProductTrailCommission ( $request -> id , $request -> trail_id );
$product = $this -> commission -> getProduct ( $request -> id );
foreach ( $monthlyUpdate as $commission ) {
$investmentValue = $this -> commission -> getInvestmentValue ( $commission );
$commissionCalculation = $this -> commission -> commissionCalculation ( $product , $investmentValue );
$this -> commission -> storeMonthlyCommission ( $product , $commission , $investmentValue , $commissionCalculation );
}
return $commissionUpdated ?
$this -> response ( 'Commission Generated!' , 200 ) :
$this -> response ( 'Commission Could Not Be Generated!' , 400 );
}
}