|
Server : Apache/2.4.41 (Ubuntu) System : Linux vmi1525618.contaboserver.net 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64 User : www-data ( 33) PHP Version : 8.2.12 Disable Function : NONE Directory : /var/www/account.appointkrypt.com/app/Http/Controllers/ |
Upload File : |
<?php
namespace App\Http\Controllers;
use App\Models\Order;
use App\Models\AddOn;
use App\Models\Appointment;
use App\Models\Plan;
use App\Models\Service;
use App\Models\User;
use App\Models\Setting;
use App\Models\Business;
use App\Models\Location;
use App\Models\Staff;
use App\Models\BusinessHours;
use App\Models\BusinessHoliday;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Nwidart\Modules\Facades\Module;
use Illuminate\Support\Facades\File;
class HomeController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
if(Auth::check())
{
return redirect('dashboard');
}
else
{
if(!file_exists(storage_path() . "/installed"))
{
header('location:install');
die;
}
else
{
if(admin_setting('landing_page') == 'on')
{
if(module_is_active('LandingPage'))
{
return view('landingpage::layouts.landingpage');
}
else
{
return view('marketplace.landing');
}
}
else
{
$uri = url()->full();
if($uri == env('APP_URL'))
{
return redirect('login');
}
else
{
$segments = explode('/', str_replace('' . url('') . '', '', $uri));
$segments = $segments[1] ?? null;
if ($segments == null) {
$local = parse_url(config('app.url'))['host'];
// Get the request host
$remote = request()->getHost();
// Get the remote domain
// remove WWW
$remote = str_replace('www.', '', $remote);
$domain = Setting::where('key', '=', 'domains')->where('value', '=', $remote)->first();
if($domain)
{
$enable_domain = Setting::where('key', '=', 'enable_domain')->where('value','on')->where('business',$domain->business)->first();
if($enable_domain)
{
$business = Business::find($enable_domain->business);
}
}
$sub_domain = Setting::where('key', '=', 'subdomain')->where('value', '=', $remote)->first();
if($sub_domain)
{
$enable_subdomain = Setting::where('key', '=', 'enable_subdomain')->where('value','on')->where('business',$sub_domain->business)->first();
if($enable_subdomain)
{
$business = Business::find($enable_subdomain->business);
}
}
if(isset($business))
{
$slug = $business->slug;
$services = Service::where('business_id',$business->id)->get();
$locations = Location::where('business_id',$business->id)->get();
$staffs = Staff::where('business_id',$business->id)->get();
$busineshours = BusinessHours::where('created_by', $business->created_by)
->where('business_id', $business->id)
->where('day_off', 'on')
->select('day_name')
->get()
->pluck('day_name')
->map(function ($day) {
return date('w', strtotime($day));
})
->toArray();
$businesholiday = BusinessHoliday::where('created_by', $business->created_by)
->where('business_id', $business->id)
->select('date')
->get()
->pluck('date')
->map(function ($date) {
return date('w', strtotime($date));
})
->toArray();
$combinedArray = array_merge($busineshours, $businesholiday);
$company_settings = getCompanyAllSetting($business->created_by,$business->id);
$customCss = isset($company_settings['custom_css']) ? $company_settings['custom_css'] : null;
$customJs = isset($company_settings['custom_js']) ? $company_settings['custom_js'] : null;
$files = File::where('business_id', $business->id)->where('created_by', $business->created_by)->first();
$custom_field = company_setting('custom_field_enable',$business->created_by,$business->id);
$custom_fields = CustomField::where('created_by',$business->created_by)->where('business_id',$business->id)->get();
return view('embeded_appointment.index',compact('slug','business','services','locations','staffs','customCss','customJs','combinedArray','files','custom_field','custom_fields'));
}
else
{
return redirect('login');
}
}
}
// return redirect('login');
}
}
}
}
public function Dashboard()
{
if(Auth::check())
{
if(Auth::user()->type == 'super admin')
{
$user = Auth::user();
$user['total_user'] = $user->countCompany();
$user['total_paid_user'] = $user->countPaidCompany();
$user['total_orders'] = Order::total_orders();
$user['total_orders_price'] = Order::total_orders_price();
$chartData = $this->getOrderChart(['duration' => 'week']);
$user['total_plans'] = Plan::all()->count();
$popular_plan = DB::table('orders')
->select('orders.plan_id', 'plans.*', DB::raw('count(*) as count'))
->join('plans', 'orders.plan_id', '=', 'plans.id')
->groupBy('orders.plan_id')
->orderByDesc('count')
->first();
$user['popular_plan'] = $popular_plan;
return view('dashboard.dashboard', compact('user', 'chartData'));
}
else
{
$user = auth()->user();
$menu = new \App\Classes\Menu($user);
event(new \App\Events\CompanyMenuEvent($menu));
$menu_items = $menu->menu;
$dashboardItem = collect($menu_items)->first(function ($item) {
return $item['parent'] === 'dashboard';
});
if ($dashboardItem) {
$route = isset($dashboardItem['route']) ? $dashboardItem['route'] : null;
if($route)
{
return redirect()->route($route);
}
}
$total_business = getBusiness()->count();
$total_service = Service::where('business_id',getActiveBusiness())->where('created_by',creatorId())->count();
$total_appointment = Appointment::where('business_id',getActiveBusiness())->where('created_by',creatorId())->count();
$total_staff = User::where('type','staff')->where('business_id',getActiveBusiness())->where('created_by',creatorId())->count();
$latest_services = Service::where('business_id',getActiveBusiness())
->where('created_by',creatorId())
->latest()
->take(5)
->get();
$latest_appointments = Appointment::where('business_id',getActiveBusiness())
->where('created_by',creatorId())
->latest()
->take(5)
->get();
$business = Business::find(getActiveBusiness());
$chartData = $this->getAppointmentChart(['duration' => 'week']);
$compact = ['total_business','total_service','total_appointment','total_staff','latest_services','latest_appointments','business','chartData'];
return view('dashboard',compact($compact));
}
}
else
{
return redirect()->route('start');
}
}
public function getAppointmentChart($arrParam)
{
$arrDuration = [];
if($arrParam['duration'])
{
if($arrParam['duration'] == 'week')
{
$previous_week = strtotime("-1 week +1 day");
for($i = 0; $i < 7; $i++)
{
$arrDuration[date('Y-m-d', $previous_week)] = date('d-M', $previous_week);
$previous_week = strtotime(date('Y-m-d', $previous_week) . " +1 day");
}
}
}
// Create an array of dates from your $arrDuration array
$dates = array_keys($arrDuration);
$orders = Appointment::select(
DB::raw('DATE(created_at) as date'),
DB::raw('count(*) as total')
)
->whereIn(DB::raw('DATE(created_at)'), $dates)
->groupBy(DB::raw('DATE(created_at)'))
->get();
// Initialize an empty $arrTask array
$arrTask = ['label' => [], 'data' => []];
foreach ($dates as $date) {
$label = $arrDuration[$date];
$total = 0;
foreach ($orders as $item) {
if ($item->date == $date) {
$total = $item->total;
break;
}
}
$arrTask['label'][] = $label;
$arrTask['data'][] = $total;
}
return $arrTask;
}
public function getOrderChart($arrParam)
{
$arrDuration = [];
if($arrParam['duration'])
{
if($arrParam['duration'] == 'week')
{
$previous_week = strtotime("-2 week +1 day");
for($i = 0; $i < 14; $i++)
{
$arrDuration[date('Y-m-d', $previous_week)] = date('d-M', $previous_week);
$previous_week = strtotime(date('Y-m-d', $previous_week) . " +1 day");
}
}
}
// Create an array of dates from your $arrDuration array
$dates = array_keys($arrDuration);
$orders = Order::select(
DB::raw('DATE(created_at) as date'),
DB::raw('count(*) as total')
)
->whereIn(DB::raw('DATE(created_at)'), $dates)
->groupBy(DB::raw('DATE(created_at)'))
->get();
// Initialize an empty $arrTask array
$arrTask = ['label' => [], 'data' => []];
foreach ($dates as $date) {
$label = $arrDuration[$date];
$total = 0;
foreach ($orders as $item) {
if ($item->date == $date) {
$total = $item->total;
break;
}
}
$arrTask['label'][] = $label;
$arrTask['data'][] = $total;
}
return $arrTask;
}
public function SoftwareDetails($slug)
{
$modules_all = Module::getByStatus(1);
$modules = [];
if(count($modules_all) > 0)
{
$modules = array_intersect_key(
$modules_all, // the array with all keys
array_flip(array_rand($modules_all,(count($modules_all) < 6) ? count($modules_all) : 6 )) // keys to be extracted
);
}
$plan = Plan::first();
$addon = AddOn::where('name',$slug)->first();
if(!empty($addon) && !empty($addon->module))
{
$module = Module::find($addon->module);
if(!empty($module))
{
try {
if(module_is_active('LandingPage'))
{
return view('landingpage::marketplace.index',compact('modules','module','plan'));
}
else{
return view($module->getLowerName().'::marketplace.index',compact('modules','module','plan'));
}
} catch (\Throwable $th) {
}
}
}
if (module_is_active('LandingPage')) {
$layout = 'landingpage::layouts.marketplace';
} else {
$layout = 'marketplace.marketplace';
}
return view('marketplace.detail_not_found',compact('modules','layout'));
}
public function Software()
{
$modules = Module::getByStatus(1);
if (module_is_active('LandingPage')) {
$layout = 'landingpage::layouts.marketplace';
} else {
$layout = 'marketplace.marketplace';
}
return view('marketplace.software',compact('modules','layout'));
}
public function Pricing()
{
if(Auth::check())
{
if(Auth::user()->type == 'company')
{
return redirect('plans');
}
else
{
return redirect('dashboard');
}
}
else
{
$plan = Plan::first();
$modules = Module::getByStatus(1);
if (module_is_active('LandingPage')) {
$layout = 'landingpage::layouts.marketplace';
} else {
$layout = 'marketplace.marketplace';
}
return view('marketplace.pricing',compact('modules','plan','layout'));
}
}
}