406 lines
17 KiB
PHP
406 lines
17 KiB
PHP
<?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)
|
|
{
|
|
$check = checkSidebarAccess('dashboard');
|
|
if (!$check) {
|
|
abort(404);
|
|
}
|
|
// 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);
|
|
return view('Admin.general-dashboard', compact('companyCount', 'a', 'leads', 'tasks', 'meetings', 'users', 'userCount', 'investingUserCount', 'totalProductCount'));
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public function personalDashboard(Request $request)
|
|
{
|
|
$check = checkSidebarAccess('personal-dashboard');
|
|
if (!$check) {
|
|
abort(404);
|
|
}
|
|
// 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::where('lead_owner', auth()->user()->id)->latest()->get();
|
|
// dd(auth()->user()->id);
|
|
|
|
$tasks = LeadTasksMeeting::where('owner', auth()->user()->id)->orderBy('created_at', 'desc')->tasks()->get();
|
|
// dd($tasks, auth()->user()->id);
|
|
$meetings = LeadTasksMeeting::where('host', (string)auth()->user()->id)->orderBy('created_at', 'desc')->meetings()->get();
|
|
// dd($meetings);
|
|
$users = User::admins()->get();
|
|
return view('Admin.personal-dashboard', compact('companyCount', 'a', 'leads', 'tasks', 'meetings', 'users', 'userCount', 'investingUserCount', 'totalProductCount'));
|
|
}
|
|
|
|
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]);
|
|
}
|
|
|
|
$leadNotes = LeadTasksMeeting::where('id', $request->task_id)->update([
|
|
'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!']);
|
|
};
|
|
}
|
|
|
|
public function editMeeting(Request $request)
|
|
{
|
|
$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]);
|
|
}
|
|
|
|
$leadMeeting = LeadTasksMeeting::where('id', $request->meeting_id)->update([
|
|
'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 {
|
|
$lead = Lead::findorFail($request->id);
|
|
if ($lead) {
|
|
$lead->delete();
|
|
return response()->json(['status' => 200, 'message' => 'Lead Deleted!']);
|
|
}
|
|
} catch (\Exception $e) {
|
|
return response()->json(['status' => 400, 'message' => 'Error deleting lead!']);
|
|
}
|
|
}
|
|
|
|
public function updateSingleAdminNotification(Request $request)
|
|
{
|
|
$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;
|
|
}
|
|
}
|
|
|
|
public function readSpecificNotification(Request $request)
|
|
{
|
|
// dd($request->all());
|
|
$id = $request->id;
|
|
if ($id) {
|
|
$notification = auth()->user()->unreadNotifications->where('id', $id)->markAsRead();
|
|
// if(!$notification){
|
|
$newUser = '';
|
|
$submittedKyc = '';
|
|
$sellerForm = '';
|
|
$buyerForm = '';
|
|
$contactUs = '';
|
|
$leadAssigned = '';
|
|
foreach (getAllAdminNotifications() as $data) {
|
|
$message = $data->data['message'];
|
|
$created_at = $data->created_at->diffForHumans();
|
|
$id = $data->id;
|
|
$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);
|
|
}
|
|
}
|
|
$notificationStatus = array('newUser' => $newUser, 'submittedKyc' => $submittedKyc, 'sellerForm' => $sellerForm, 'buyerForm' => $buyerForm, 'contactUs' => $contactUs, 'leadAssigned' => $leadAssigned, 'unreadnotification' => \Auth::user()->unreadnotifications()->count());
|
|
return response()->json(['status' => 200, 'message' => "marked as read", 'data' => $notificationStatus]);
|
|
// }
|
|
}
|
|
return response()->json(['status' => 201, 'message' => "something error"]);
|
|
}
|
|
|
|
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 . '">
|
|
<div class="symbol symbol-35px me-4">
|
|
<span class="symbol-label '.$icon_color_light.'" '.$icon_color_dark.'>
|
|
<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">
|
|
<div class="fs-7" style="font-weight: ' . $font_weight . ';">' . $message . '</div>
|
|
</div>
|
|
<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>
|
|
return $data;
|
|
}
|
|
}
|