|
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\Business;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\Location;
use App\Models\category;
use App\Models\Service;
use App\Models\Setting;
use App\Models\File;
use App\Models\Staff;
use App\Models\CustomField;
use App\Models\BusinessHoliday;
use App\Events\DestroyBusiness;
use App\Events\DefaultData;
use Illuminate\Support\Facades\Hash;
class BusinessController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
if(Auth::user()->isAbleTo('business manage'))
{
$businesses = Business::where('created_by',creatorId())->get();
return view('business.index',compact('businesses'));
}
else
{
return response()->json(['error' => __('Permission denied.')], 401);
}
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
if(Auth::user()->isAbleTo('business create'))
{
return view('business.create');
}
else
{
return response()->json(['error' => __('Permission denied.')], 401);
}
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
if(Auth::user()->isAbleTo('business create'))
{
if(Auth::user()->type != 'super admin'){
$canUse= PlanCheck('Business',Auth::user()->id);
if($canUse == false)
{
return redirect()->back()->with('error','You have maxed out the total number of Workspace allowed on your current plan');
}
}
$validator = \Validator::make(
$request->all(), [
'name' => 'required',
]
);
if($validator->fails())
{
$messages = $validator->getMessageBag();
return redirect()->back()->with('error', $messages->first());
}
try {
$business = new Business();
$business->name = $request->name;
$business->created_by = \Auth::user()->id;
$business->save();
$user = \Auth::user();
$user->active_business =$business->id;
$user->save();
User::CompanySetting(\Auth::user()->id,$business->id);
if(!empty(\Auth::user()->active_module))
{
event(new DefaultData(\Auth::user()->id,$business->id,\Auth::user()->active_module));
}
return redirect()->route('dashboard')->with('success', __('Business create successfully!'));
}catch (\Exception $e) {
return redirect()->back()->with('error', $e->getMessage());
}
}
else
{
return redirect()->back()->with('error', __('Permission denied.'));
}
}
/**
* Display the specified resource.
*/
public function show(Business $business)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
if(Auth::user()->isAbleTo('business edit'))
{
$business = Business::find($id);
return view('business.edit',compact('business'));
}
else
{
return response()->json(['error' => __('Permission denied.')], 401);
}
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id)
{
if(Auth::user()->isAbleTo('business edit'))
{
$business = Business::find($id);
$validator = \Validator::make(
$request->all(), [
'name' => 'required',
]
);
if($validator->fails())
{
$messages = $validator->getMessageBag();
return redirect()->back()->with('error', $messages->first());
}
$business->name = $request->name;
$business->slug = $request->slug;
$business->save();
return redirect()->back()->with('success', __('Business updated successfully!'));
}
else
{
return redirect()->back()->with('error', __('Permission denied.'));
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy($business_id)
{
if(Auth::user()->isAbleTo('business delete'))
{
$objUser = \Auth::user();
$business = Business::find($business_id);
if($business && $business->created_by == $objUser->id)
{
$other_business = Business::where('created_by',$objUser->id)->where('is_disable',1)->where('id','!=',$business->id)->first();
if($other_business)
{
if(!empty($other_business))
{
$objUser->active_business = $other_business->id;
$objUser->save();
}
// first parameter workspace
event(new DestroyBusiness($business));
$business->delete();
return redirect()->route('dashboard')->with('success', __('Business Deleted Successfully!'));
}
return redirect()->route('dashboard')->with('errors', __("You can't delete Workspace! because your other workspaces are disabled "));
}
else
{
return redirect()->route('dashboard')->with('errors', __("You can't delete Workspace!"));
}
}
else
{
return redirect()->back()->with('error', __('Permission denied.'));
}
}
public function businessCheck(Request $request)
{
if(isset($request->slug))
{
$business = Business::where('slug',$request->slug)->where('id','!=',$request->business)->exists();
if(!$business)
{
return response()->json(['success' => __('This Slug is Available.')]);
}
}
return response()->json(['error' => __('This Slug Not Available.')]);
}
public function change($business_id)
{
$check = Business::find($business_id);
if(!empty($check))
{
$users = User::where('email',\Auth::user()->email)->where('business_id',$business_id)->where('created_by',Auth::user()->created_by)->first();
if(empty($users))
{
$users = User::where('email',\Auth::user()->email)->Where('id',$check->created_by)->first();
}
if(empty($users))
{
$users = User::where('email',\Auth::user()->email)->where('business_id',$business_id)->first();
}
$user = User::find($users->id);
$user->active_business = $business_id;
$user->save();
if(!empty($user)){
Auth::login($user);
return redirect()->route('dashboard')->with('success', 'User Business change successfully.');
}
return redirect()->route('dashboard')->with('success', 'User Business change successfully.');
}else{
return redirect()->route('dashboard')->with('error', "Business not found.");
}
}
public function businessManage($id)
{
if(Auth::user()->isAbleTo('business update'))
{
$business = Business::find($id);
$company_settings = getCompanyAllSetting($business->created_by,$id);
$business_url = route('appointments.form',$business->slug);
$serverName = str_replace(
[
'http://',
'https://',
],
'',
env('APP_URL')
);
$serverIp = gethostbyname($serverName);
if ($serverIp == $_SERVER['SERVER_ADDR']) {
$serverIp;
} else {
$serverIp = request()->server('SERVER_ADDR');
}
if (!empty($company_settings['enable_subdomain']) && $company_settings['enable_subdomain'] == 'on') {
// Remove the http://, www., and slash(/) from the URL
$input = env('APP_URL');
// If URI is like, eg. www.way2tutorial.com/
$input = trim($input, '/');
// If not have http:// or https:// then prepend it
if (!preg_match('#^http(s)?://#', $input)) {
$input = 'http://' . $input;
}
$urlParts = parse_url($input);
// Remove www.
$subdomain_name = preg_replace('/^www\./', '', $urlParts['host']);
// Output way2tutorial.com
} else {
$subdomain_name = str_replace(
[
'http://',
'https://',
],
'',
env('APP_URL')
);
}
$subdomain_Ip = '';
$subdomainPointing = '';
$domainip = '';
$domainPointing = '';
$locations = Location::where('business_id',$id)->where('created_by',creatorId())->get();
$categories = category::where('business_id',$id)->where('created_by',creatorId())->get();
$services = Service::where('business_id',$id)->where('created_by',creatorId())->get();
$staffes = Staff::where('business_id',$id)->where('created_by',creatorId())->get();
$businessholidays = BusinessHoliday::where('business_id',$id)->where('created_by',creatorId())->get();
$files = File::where('created_by',creatorId())->where('business_id',$id)->first();
$custom_fields = CustomField::where('created_by',creatorId())->where('business_id',$id)->get();
$custom_field = company_setting('custom_field_enable',creatorId(),$id);
return view('business.manage',compact('business','locations','categories','services','staffes','businessholidays','business_url','subdomain_Ip','subdomainPointing','domainip','domainPointing','serverIp','subdomain_name','company_settings','files','custom_field','custom_fields'));
}
else
{
return redirect()->back()->with('error', __('Permission denied.'));
}
}
public function domainsetting($id, Request $request)
{
if(Auth::user()->isAbleTo('business update'))
{
$business = Business::find($id);
$post = $request->all();
unset($post['_token']);
if ($request->enable_domain == 'enable_domain') {
// Remove the http://, www., and slash(/) from the URL
$input = $request->domains;
// If URI is like, eg. www.way2tutorial.com/
$input = trim($input, '/');
// If not have http:// or https:// then prepend it
if (!preg_match('#^http(s)?://#', $input)) {
$input = 'http://' . $input;
}
$urlParts = parse_url($input);
// Remove www.
$domain_name = preg_replace('/^www\./', '', $urlParts['host'] ?? null);
// Output way2tutorial.com
}
if ($request->enable_domain == 'enable_subdomain') {
// Remove the http://, www., and slash(/) from the URL
$input = env('APP_URL');
// If URI is like, eg. www.way2tutorial.com/
$input = trim($input, '/');
// If not have http:// or https:// then prepend it
if (!preg_match('#^http(s)?://#', $input)) {
$input = 'http://' . $input;
}
$urlParts = parse_url($input);
// Remove www.
$subdomain_name = preg_replace('/^www\./', '', $urlParts['host']);
// Output way2tutorial.com
$subdomain_name = $request->subdomain . '.' . $subdomain_name;
}
if ($request->enable_domain == 'enable_domain') {
$post['domains'] = $domain_name;
}
$post['enable_businesslink'] = ($request->enable_domain == 'enable_businesslink' || empty($request->enable_domain)) ? 'on' : 'off';
$post['enable_domain'] = ($request->enable_domain == 'enable_domain') ? 'on' : 'off';
$post['enable_subdomain'] = ($request->enable_domain == 'enable_subdomain') ? 'on' : 'off';
if ($request->enable_domain == 'enable_subdomain') {
$post['subdomain'] = $subdomain_name;
}
foreach ($post as $key => $value) {
// Define the data to be updated or inserted
$data = [
'key' => $key,
'business' => $id,
'created_by' => $business->created_by,
];
// Check if the record exists, and update or insert accordingly
Setting::updateOrInsert($data, ['value' => $value]);
}
// Settings Cache forget
comapnySettingCacheForget();
return redirect()->back()->with('success', __('Custom setting save sucessfully.'));
}
else
{
return redirect()->back()->with('error', __('Permission denied.'));
}
}
public function slotCapacitysetting($id, Request $request)
{
if(Auth::user()->isAbleTo('business update'))
{
$business = Business::find($id);
$validator = \Validator::make($request->all(),
[
'maximum_slot' => 'required',
]);
if($validator->fails()){
$messages = $validator->getMessageBag();
return redirect()->back()->with('error', $messages->first());
}
$data = [
'key' => 'maximum_slot',
'business' => $id,
'created_by' => $business->created_by,
];
// Check if the record exists, and update or insert accordingly
Setting::updateOrInsert($data, ['value' => $request->maximum_slot]);
// Settings Cache forget
comapnySettingCacheForget();
return redirect()->back()->with('success', __('Custom setting save sucessfully.'));
}
else
{
return redirect()->back()->with('error', __('Permission denied.'));
}
}
public function appointmentRemindersetting($id, Request $request)
{
if(Auth::user()->isAbleTo('business update'))
{
$business = Business::find($id);
$validator = \Validator::make($request->all(),
[
'reminder_interval' => 'required',
]);
if($validator->fails()){
$messages = $validator->getMessageBag();
return redirect()->back()->with('error', $messages->first());
}
$data = [
'key' => 'reminder_interval',
'business' => $id,
'created_by' => $business->created_by,
];
// Check if the record exists, and update or insert accordingly
Setting::updateOrInsert($data, ['value' => $request->reminder_interval]);
// Settings Cache forget
comapnySettingCacheForget();
return redirect()->back()->with('success', __('Appointment Reminder setting save sucessfully.'));
}
else
{
return redirect()->back()->with('error', __('Permission denied.'));
}
}
}