2024-03-28 14:52:40 +05:30
< ? php
namespace App\Http\Controllers\Admin ;
use Carbon\Carbon ;
use App\Models\Lead ;
use App\Models\User ;
use App\Models\Company ;
use App\Models\Category ;
use Illuminate\Http\Request ;
use App\Models\LeadTasksMeeting ;
use App\Http\Controllers\Controller ;
use App\Models\MonthlyUpdateMaster ;
use App\Models\MonthlyUpdateMasterCommission ;
use App\Models\Product ;
use Illuminate\Support\Facades\Validator ;
class DashboardController extends Controller
{
function splitDates ( $min , $max , $parts = 5 , $output = " M d Y " )
{
$dataCollection [] = date ( $output , strtotime ( $min ));
$diff = ( strtotime ( $max ) - strtotime ( $min )) / $parts ;
$convert = strtotime ( $min ) + $diff ;
for ( $i = 1 ; $i < $parts ; $i ++ ) {
$dataCollection [] = date ( $output , $convert );
$convert += $diff ;
}
$dataCollection [] = date ( $output , strtotime ( $max ));
return $dataCollection ;
}
public function index ( Request $request )
{
2024-04-09 17:56:07 +05:30
$check = checkSidebarAccess ( 'dashboard' );
2024-05-29 16:29:37 +05:30
if ( ! $check ) {
2024-04-09 17:56:07 +05:30
abort ( 404 );
}
2024-03-28 14:52:40 +05:30
// dd(123);
// $a = ['1','2','3'];
// $b = User::where('id',1)->update([
// 'otp'=>json_encode($a)
// ]);
// // $b = User::query();
// $b = User::whereJsonContains('otp',1)->get();
// dd($b);
$user = User :: select ( 'name' );
$company = Company :: select ( 'company_name' );
// $a = 2;
// if($a == 2){
// $company->where('id',$a)->get();
// }
$categories = Category :: union ( $user ) -> union ( $company ) -> select ( 'category_name as product_name' ) -> get ();
// $user = User::all();
// $company = Company::all();
// $result = $user->union($company)->get();
// echo "<pre>";
// print_r($categories);
// die;
// dd($result);
$period = \Carbon\CarbonPeriod :: create ( now () -> subMonths ( 12 ) -> format ( 'Y-m-d' ), '1 month' , now () -> format ( 'Y-m-d' ));
$monthNames = array ();
foreach ( $period as $dt ) {
// echo $dt->format("Y-m") . "<br>\n";
$date = Carbon :: createFromFormat ( 'm' , $dt -> format ( " m " ));
$monthName = $date -> format ( 'F' ) . ' ' ;
array_push ( $monthNames , '' . $monthName . '' );
}
// array_walk($monthNames, fn(&$x) => $x = "'$x'");
// print_r(Carbon::now()->subMonths(6));
// print_r(implode(', ',$monthNames));
// die;
// $a = '"' . implode('", "', $monthNames) . '"';
$a = '"' . implode ( '","' , $monthNames ) . '"' ;
// print_r($a);
// die;
$companyCount = Company :: count ();
$userCount = User :: users () -> count ();
$investingUserCount = MonthlyUpdateMaster :: distinct ( 'users_id' ) -> count ( 'users_id' );
$totalProductCount = Product :: count ();
$leads = Lead :: with ( 'leadSource' ) -> where ( 'lead_owner' , auth () -> user () -> id ) -> get ();
$tasks = LeadTasksMeeting :: where ( 'owner' , auth () -> user () -> id ) -> tasks () -> get ();
$meetings = LeadTasksMeeting :: where ( 'host' , auth () -> user () -> id ) -> meetings () -> get ();
$users = User :: admins () -> get ();
// dd($leads);
2024-05-29 16:29:37 +05:30
return view ( 'Admin.general-dashboard' , compact ( 'companyCount' , 'a' , 'leads' , 'tasks' , 'meetings' , 'users' , 'userCount' , 'investingUserCount' , 'totalProductCount' ));
2024-03-28 14:52:40 +05:30
}
public function getCommissionData ( Request $request )
{
$getDates = MonthlyUpdateMasterCommission :: select ( \DB :: raw ( 'MAX(created_at) as latest_date' ), \DB :: raw ( 'MIN(created_at) as earliest_date' )) -> first ();
$getTotal = MonthlyUpdateMasterCommission :: query ();
if ( $request -> from ) {
$getTotal -> where ( 'created_at' , '>=' , $request -> from . ' 00:00:00' );
}
if ( $request -> to ) {
$getTotal -> where ( 'created_at' , '<=' , $request -> to . ' 00:00:00' );
}
$splitDates = $this -> splitDates ( $min = $request -> from ? ? $getDates [ 'earliest_date' ], $max = $request -> to ? ? $getDates [ 'latest_date' ]);
for ( $i = 0 ; $i < count ( $splitDates ); $i ++ ) {
$convertedDate = \Carbon\Carbon :: parse ( $splitDates [ $i ]) -> format ( 'Y-m-d' );
$data = MonthlyUpdateMasterCommission :: where ( 'created_at' , '>=' , $convertedDate . ' 00:00:00' ) -> select ( \DB :: raw ( 'SUM(gross_commissioned_earned_inr) as total_gross' ), \DB :: raw ( 'SUM(net_commission_received) as total_net' )) -> first ();
$intervalGross [] = $data -> total_gross ;
$intervalNet [] = $data -> total_net ;
}
$getTotal = $getTotal -> selectRaw ( \DB :: raw ( 'SUM(total_investment_or_commitment_amount) as total_commission, SUM(gross_commissioned_earned_inr) as total_gross, SUM(net_commission_received) as total_net' ))
-> first ();
return response () -> json ([
'total_investment' => $this -> IND_money_format ( $getTotal -> total_commission ),
'gross_commission' => $this -> IND_money_format ( $getTotal -> total_gross ),
'net_commission' => $this -> IND_money_format ( $getTotal -> total_net ),
'days' => $splitDates ,
'intervalGross' => $intervalGross ,
'intervalNet' => $intervalNet ,
]);
}
public 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 ;
}
2024-05-29 16:29:37 +05:30
public function personalDashboard ( Request $request )
{
2024-04-09 17:56:07 +05:30
$check = checkSidebarAccess ( 'personal-dashboard' );
2024-05-29 16:29:37 +05:30
if ( ! $check ) {
2024-04-09 17:56:07 +05:30
abort ( 404 );
}
2024-03-28 14:52:40 +05:30
// dd(123);
// $a = ['1','2','3'];
// $b = User::where('id',1)->update([
// 'otp'=>json_encode($a)
// ]);
// // $b = User::query();
// $b = User::whereJsonContains('otp',1)->get();
// dd($b);
$user = User :: select ( 'name' );
$company = Company :: select ( 'company_name' );
// $a = 2;
// if($a == 2){
// $company->where('id',$a)->get();
// }
$categories = Category :: union ( $user ) -> union ( $company ) -> select ( 'category_name as product_name' ) -> get ();
// $user = User::all();
// $company = Company::all();
// $result = $user->union($company)->get();
// echo "<pre>";
// print_r($categories);
// die;
// dd($result);
$period = \Carbon\CarbonPeriod :: create ( now () -> subMonths ( 12 ) -> format ( 'Y-m-d' ), '1 month' , now () -> format ( 'Y-m-d' ));
$monthNames = array ();
foreach ( $period as $dt ) {
// echo $dt->format("Y-m") . "<br>\n";
$date = Carbon :: createFromFormat ( 'm' , $dt -> format ( " m " ));
$monthName = $date -> format ( 'F' ) . ' ' ;
array_push ( $monthNames , '' . $monthName . '' );
}
// array_walk($monthNames, fn(&$x) => $x = "'$x'");
// print_r(Carbon::now()->subMonths(6));
// print_r(implode(', ',$monthNames));
// die;
// $a = '"' . implode('", "', $monthNames) . '"';
$a = '"' . implode ( '","' , $monthNames ) . '"' ;
// print_r($a);
// die;
$companyCount = Company :: count ();
$userCount = User :: users () -> count ();
$investingUserCount = MonthlyUpdateMaster :: distinct ( 'users_id' ) -> count ( 'users_id' );
$totalProductCount = Product :: count ();
2024-05-16 15:40:08 +05:30
$leads = Lead :: where ( 'lead_owner' , auth () -> user () -> id ) -> latest () -> get ();
2024-05-30 12:27:50 +05:30
// dd(auth()->user()->id);
2024-05-30 17:57:20 +05:30
$tasks = LeadTasksMeeting :: where ( 'owner' , auth () -> user () -> id ) -> orderBy ( 'created_at' , 'desc' ) -> tasks () -> get ();
2024-05-30 12:27:50 +05:30
// dd($tasks, auth()->user()->id);
2024-05-30 17:57:20 +05:30
$meetings = LeadTasksMeeting :: where ( 'host' , ( string ) auth () -> user () -> id ) -> orderBy ( 'created_at' , 'desc' ) -> meetings () -> get ();
2024-04-25 10:51:37 +05:30
// dd($meetings);
2024-03-28 14:52:40 +05:30
$users = User :: admins () -> get ();
2024-05-29 16:29:37 +05:30
return view ( 'Admin.personal-dashboard' , compact ( 'companyCount' , 'a' , 'leads' , 'tasks' , 'meetings' , 'users' , 'userCount' , 'investingUserCount' , 'totalProductCount' ));
2024-03-28 14:52:40 +05:30
}
public function editTask ( Request $request )
{
$validator = Validator :: make ( $request -> post (), [
'task_id' => 'required' ,
'subject' => 'required' ,
'due_date' => 'required' ,
'priority' => 'required' ,
'owner' => 'required' ,
'reminder' => 'required' ,
], [
'required' => 'The :attribute field must be required'
]);
$validationMessage = $this -> validationError ( $validator );
if ( $validationMessage ) {
return response () -> json ([ 'status' => 400 , 'message' => $validationMessage ]);
}
2024-05-29 16:29:37 +05:30
$leadNotes = LeadTasksMeeting :: where ( 'id' , $request -> task_id ) -> update ([
2024-03-28 14:52:40 +05:30
'subject' => $request -> subject ,
'due_date' => $request -> due_date ,
'priority' => $request -> priority ,
'owner' => $request -> owner ,
'reminder' => $request -> reminder ,
'status' => $request -> status ? '1' : '0' ,
// 'updated_by' => auth()->user()->id
]);
if ( $leadNotes ) {
return response () -> json ([ 'status' => 200 , 'message' => 'Lead Task Updated Succesfully!' ]);
} else {
return response () -> json ([ 'status' => 400 , 'message' => 'Error Creating Lead Task!' ]);
};
}
2024-05-29 16:29:37 +05:30
public function editMeeting ( Request $request )
{
2024-03-28 14:52:40 +05:30
$validator = Validator :: make ( $request -> post (), [
'meeting_id' => 'required' ,
'location' => 'required' ,
'from' => 'required' ,
'to' => 'required' ,
'host' => 'required' ,
'priority' => 'required' ,
'participants' => 'required' ,
'related_to' => 'required' ,
], [
'required' => 'The :attribute field must be required'
]);
$validationMessage = $this -> validationError ( $validator );
if ( $validationMessage ) {
return response () -> json ([ 'status' => 400 , 'message' => $validationMessage ]);
}
2024-05-29 16:29:37 +05:30
$leadMeeting = LeadTasksMeeting :: where ( 'id' , $request -> meeting_id ) -> update ([
2024-03-28 14:52:40 +05:30
'location' => $request -> location ,
'from' => $request -> from ,
'to' => $request -> to ,
'host' => $request -> host ,
'priority' => $request -> priority ,
'participants' => $request -> participants ,
'related_to' => $request -> related_to ,
'status' => $request -> status ? '1' : '0' ,
// 'updated_by' => auth()->user()->id
]);
if ( $leadMeeting ) {
return response () -> json ([ 'status' => 200 , 'message' => 'Lead Meeting Updated Succesfully!' ]);
} else {
return response () -> json ([ 'status' => 400 , 'message' => 'Error Creating Lead Meeting!' ]);
};
}
public function deleteTask ( Request $request )
{
try {
$task = LeadTasksMeeting :: findorFail ( $request -> id );
if ( $task ) {
$task -> delete ();
return response () -> json ([ 'status' => 200 , 'message' => 'Task Deleted!' ]);
}
} catch ( \Exception $e ) {
return response () -> json ([ 'status' => 400 , 'message' => 'Error deleting task!' ]);
}
}
public function deleteMeeting ( Request $request )
{
try {
$meeting = LeadTasksMeeting :: findorFail ( $request -> id );
if ( $meeting ) {
$meeting -> delete ();
return response () -> json ([ 'status' => 200 , 'message' => 'Meeting Deleted!' ]);
}
} catch ( \Exception $e ) {
return response () -> json ([ 'status' => 400 , 'message' => 'Error deleting meeting!' ]);
}
}
public function deleteLead ( Request $request )
{
try {
2024-05-30 12:27:50 +05:30
$lead = Lead :: findorFail ( $request -> id );
2024-03-28 14:52:40 +05:30
if ( $lead ) {
$lead -> delete ();
return response () -> json ([ 'status' => 200 , 'message' => 'Lead Deleted!' ]);
}
} catch ( \Exception $e ) {
return response () -> json ([ 'status' => 400 , 'message' => 'Error deleting lead!' ]);
}
}
2024-05-29 16:29:37 +05:30
public function updateSingleAdminNotification ( Request $request )
{
2024-03-28 14:52:40 +05:30
$user = User :: find ( auth () -> user () -> id );
$user -> notifications -> where ( 'id' , $request -> not_id ) -> markAsRead ();
return response () -> json ([ 'status' => 200 , 'message' => 'Notification mark as read' ]);
}
public function validationError ( $validator )
{
if ( $validator -> fails ()) {
$errors = $validator -> errors ();
$messages = '' ;
foreach ( $errors -> all () as $message ) {
$messages .= $message . '</br>' ;
}
return $messages ;
}
}
2024-05-29 16:29:37 +05:30
public function readSpecificNotification ( Request $request )
{
2024-03-28 14:52:40 +05:30
// dd($request->all());
2024-04-04 19:13:00 +05:30
$id = $request -> id ;
2024-05-29 16:29:37 +05:30
if ( $id ) {
2024-03-28 14:52:40 +05:30
$notification = auth () -> user () -> unreadNotifications -> where ( 'id' , $id ) -> markAsRead ();
// if(!$notification){
$newUser = '' ;
$submittedKyc = '' ;
$sellerForm = '' ;
$buyerForm = '' ;
$contactUs = '' ;
$leadAssigned = '' ;
2024-05-29 16:29:37 +05:30
foreach ( getAllAdminNotifications () as $data ) {
$message = $data -> data [ 'message' ];
2024-03-28 14:52:40 +05:30
$created_at = $data -> created_at -> diffForHumans ();
$id = $data -> id ;
2024-05-29 16:29:37 +05:30
$read_at = $data -> read_at ;
if ( array_key_exists ( 'notification_type' , $data -> data ) && $data -> data [ 'notification_type' ] == 'New Users' ) {
$newUser .= $this -> notificationData ( $message , $created_at , $id , $read_at );
} else if ( array_key_exists ( 'notification_type' , $data -> data ) && $data -> data [ 'notification_type' ] == 'Submitted KYC' ) {
$submittedKyc .= $this -> notificationData ( $message , $created_at , $id , $read_at );
} else if ( array_key_exists ( 'notification_type' , $data -> data ) && $data -> data [ 'notification_type' ] == 'Seller Form' ) {
$sellerForm .= $this -> notificationData ( $message , $created_at , $id , $read_at );
} else if ( array_key_exists ( 'notification_type' , $data -> data ) && $data -> data [ 'notification_type' ] == 'Buyer Form' ) {
$buyerForm .= $this -> notificationData ( $message , $created_at , $id , $read_at );
} else if ( array_key_exists ( 'notification_type' , $data -> data ) && $data -> data [ 'notification_type' ] == 'Contact Us' ) {
$contactUs .= $this -> notificationData ( $message , $created_at , $id , $read_at );
} else if ( array_key_exists ( 'notification_type' , $data -> data ) && $data -> data [ 'notification_type' ] == 'Leads Assigned' ) {
$leadAssigned .= $this -> notificationData ( $message , $created_at , $id , $read_at );
2024-03-28 14:52:40 +05:30
}
}
2024-06-04 12:00:48 +05:30
$notificationStatus = array ( 'newUser' => $newUser , 'submittedKyc' => $submittedKyc , 'sellerForm' => $sellerForm , 'buyerForm' => $buyerForm , 'contactUs' => $contactUs , 'leadAssigned' => $leadAssigned , 'unreadnotification' => \Auth :: user () -> unreadnotifications () -> count ());
2024-05-29 16:29:37 +05:30
return response () -> json ([ 'status' => 200 , 'message' => " marked as read " , 'data' => $notificationStatus ]);
// }
2024-03-28 14:52:40 +05:30
}
2024-05-29 16:29:37 +05:30
return response () -> json ([ 'status' => 201 , 'message' => " something error " ]);
2024-03-28 14:52:40 +05:30
}
2024-05-29 16:29:37 +05:30
public function notificationData ( $message , $created_at , $id , $read_at )
{
$color = $read_at ? '#808080' : 'black' ;
$font_weight = $read_at ? '300' : '600' ;
$icon_color_light = $read_at ? 'bg-light-warning' : '' ;
$icon_color_dark = $read_at ? '' : 'style="background-color: #c18948;"' ;
$data = ' <a class="notify_id" data-notify-id="' . $id . ' " >
< div class = " d-flex align-items-start mb-2 " style = " color:' . $color . ' " >
2024-03-28 14:52:40 +05:30
< div class = " symbol symbol-35px me-4 " >
2024-05-29 16:29:37 +05:30
< span class = " symbol-label '. $icon_color_light .' " '.$icon_color_dark.' >
2024-03-28 14:52:40 +05:30
< span class = " svg-icon svg-icon-2 svg-icon-warning " >
< svg width = " 24 " height = " 24 " viewBox = " 0 0 24 24 " fill = " none " xmlns = " http://www.w3.org/2000/svg " >
< path opacity = " 0.3 " d = " M20 15H4C2.9 15 2 14.1 2 13V7C2 6.4 2.4 6 3 6H21C21.6 6 22 6.4 22 7V13C22 14.1 21.1 15 20 15ZM13 12H11C10.5 12 10 12.4 10 13V16C10 16.5 10.4 17 11 17H13C13.6 17 14 16.6 14 16V13C14 12.4 13.6 12 13 12Z " fill = " currentColor " />
< path d = " M14 6V5H10V6H8V5C8 3.9 8.9 3 10 3H14C15.1 3 16 3.9 16 5V6H14ZM20 15H14V16C14 16.6 13.5 17 13 17H11C10.5 17 10 16.6 10 16V15H4C3.6 15 3.3 14.9 3 14.7V18C3 19.1 3.9 20 5 20H19C20.1 20 21 19.1 21 18V14.7C20.7 14.9 20.4 15 20 15Z " fill = " currentColor " />
</ svg >
</ span >
</ span >
</ div >
< div class = " mb-0 me-2 " >
2024-05-29 16:29:37 +05:30
< div class = " fs-7 " style = " font-weight: ' . $font_weight . '; " > ' . $message . ' </ div >
2024-03-28 14:52:40 +05:30
</ div >
2024-05-29 16:29:37 +05:30
< span class = " badge badge-light fs-8 me-2 " > ' . $created_at . ' </ span >
</ div > </ a > ' ;
// <a class="badge badge-light fs-8 notify_id" data-notify-id="'.$id.'"><i class="fa fa-trash" aria-hidden="true"></i></a>
2024-03-28 14:52:40 +05:30
return $data ;
}
}