Server : Apache System : Linux 145.162.205.92.host.secureserver.net 5.14.0-611.45.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Apr 1 05:56:53 EDT 2026 x86_64 User : tradze ( 1001) PHP Version : 8.1.34 Disable Function : NONE Directory : /home/tradze/public_html/app/Modules/Corporate/Http/Controllers/Admin/ |
<?php
namespace App\Modules\Corporate\Http\Controllers\Admin;
use App\Http\Controllers\AdminController;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Modules\Accounts\Models\Account;
use App\Modules\Corporate\Http\Requests\CorporateEventInvoiceRequest;
use App\Modules\Corporate\Http\Requests\CorporateEventRequest;
use App\Modules\Corporate\Models\Client;
use App\Modules\Corporate\Models\CorporateEvent;
use App\Modules\Corporate\Models\CorporateEventTherapist;
use App\Modules\Invoices\Repositories\InvoiceRepository;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Laracasts\Flash\Flash;
use Yajra\Datatables\Datatables;
class EventsController extends AdminController
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$this->data['page_title'] = trans('corporate::events.page_title');
return view('corporate::admin.events.index', $this->data);
}
/**
* Show a list of all companies
*
* @return mixed
*/
public function data(Request $request)
{
//create object contact
$obj = $this->getData($request);
//create array with permissions access
$this->data['can'] = [
'edit' => $this->data['user']->roles->pluck('slug')[0] == 'developer',
'delete' => $this->data['user']->roles->pluck('slug')[0] == 'developer',
];
//return datatables data
return Datatables::of($obj)
->editColumn('account_id', function ($o) {
$company = $o->account()->withTrashed()->first()->company;
return $company;
})
->editColumn('client_id', function ($o) {
$company = $o->client()->withTrashed()->first()->company;
return $company;
})
->editColumn('event_date', function ($o) {
return $o->event_date->format('d M Y') . ' ' . $o->event_hour;
})
->editColumn('amount_value', function ($o) {
return number_format($o->amount_value, 2, ',', '.');
})
->editColumn('commision_value', function ($o) {
return number_format($o->commision_value, 2, ',', '.');
})
->addColumn('invoice', function ($o) {
$invoice = $o->invoice()->first();
$this->data['o'] = $o;
$this->data['invoice'] = $o->invoice()->first();
return view('corporate::admin.events.partial_invoice', $this->data)->render();
})
->addColumn('actions', function ($o) {
$this->data['o'] = $o;
return view('corporate::admin.events.list_actions', $this->data)->render();
})
->rawColumns([
'account_id',
'client_id',
'event_date',
'amount_value',
'commision_value',
'invoice',
'actions'
])
->removeColumn('id')
->removeColumn('updated_at')
->make(true);
}
/**
* Get object entries
* @param Request $request
*/
protected function getData(Request $request)
{
if (Auth::user()->hasRole('salon')) {
$obj = CorporateEvent::where('user_id', Auth::user()->id);
} else {
$obj = CorporateEvent::where('user_id', 0)->get();
}
//return object
return $obj;
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create(Request $request)
{
$this->data['page_title'] = trans('corporate::events.page_title');
$this->data['form']['therapists'] = $this->get_request_therapist($request);
// $this->data['form']['therapists_opt'] = User::ofTherapists()->pluck('name','id');
if (Auth::user()->hasRole('salon')) {
$this->data['form']['clients_opt'] = Client::where('user_id', Auth::user()->id)->pluck('company', 'id');
$this->data['form']['accounts_opt'] = Account::where('user_id', Auth::user()->id)->pluck('company', 'id');
} else {
$this->data['form']['clients_opt'] = Client::where('user_id', 0)->pluck('company', 'id');
$this->data['form']['accounts_opt'] = Account::where('user_id', 0)->pluck('company', 'id');
}
//show page
return view('corporate::admin.events.create_edit', $this->data);
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store(CorporateEventRequest $request)
{
DB::beginTransaction();
try {
$data = $request->validated();
// Set defaults for numeric fields
$data['commision_value'] = $data['commision_value'] ?? 0;
$data['amount_value'] = $request->amount_value ?? 0;
// Set defaults for string fields
$data['duration'] = $data['duration'] ?? '';
$data['addr_state'] = $data['addr_state'] ?? '';
$data['addr_city'] = $data['addr_city'] ?? '';
// Set user_id
$data['user_id'] = auth()->user()->hasRole('salon')
? auth()->id()
: 0;
// Create event
$obj = CorporateEvent::create([
'title' => $request->title,
'client_id' => $request->client_id,
'account_id' => $request->account_id,
'duration' => $data['duration'],
'event_date' => $request->event_date,
'event_hour' => $request->event_hour,
'addr_postcode' => $request->addr_postcode,
'addr_city' => $request->addr_city,
'addr_state' => $request->addr_state,
'addr_address' => $request->addr_address,
'amount_value' => $data['amount_value'],
'commision_value' => $data['commision_value']
]);
// Attach therapists
if ($request->has('therapist_id')) {
foreach ($request->therapist_id as $key => $thid) {
$obj->therapists()->attach($thid, [
'commision_value' => $request->get('therapist_commision')[$key] ?? 0,
]);
}
}
DB::commit();
if ($request->save) {
return redirect()->route('admin.corporate.events.edit', $obj->id);
}
return redirect()->route('admin.corporate.events.index');
} catch (\Exception $e) {
DB::rollBack();
throw $e;
}
}
/**
* Edit Service Type
*
* @param $label
* @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function edit(CorporateEvent $events, Request $request)
{
$this->data['page_title'] = trans('corporate::events.page_title');
$this->data['obj'] = $events::find($request->event);
// $this->data['form']['therapists'] = $this->get_request_therapist($request);
// $this->data['form']['therapists_opt'] = User::ofTherapists()->pluck('name','id');
// $this->data['form']['saved_therapists'] = $this->get_saved_therapists($events);
// $this->data['form']['clients_opt'] = Client::pluck('company','id');
// $this->data['form']['accounts_opt'] = Account::pluck('company','id');
if (Auth::user()->hasRole('salon')) {
$this->data['form']['clients_opt'] = Client::where('user_id', Auth::user()->id)->pluck('company', 'id');
$this->data['form']['accounts_opt'] = Account::where('user_id', Auth::user()->id)->pluck('company', 'id');
} else {
$this->data['form']['clients_opt'] = Client::where('user_id', 0)->pluck('company', 'id');
$this->data['form']['accounts_opt'] = Account::where('user_id', 0)->pluck('company', 'id');
}
//show page
return view('corporate::admin.events.create_edit', $this->data);
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update(CorporateEventRequest $request, CorporateEvent $events)
{
$obj = $events::find($request->event);
//start transaction
DB::beginTransaction();
//fields to be updated
$fillable = $obj->getFillable();
$fields = array();
foreach ($request->all() as $field => $f) {
if (in_array($field, $fillable)) {
$fields[$field] = $f;
}
} //end foreach
//update CorporateEvent fields
CorporateEvent::where('id', $obj->id)->update($fields);
//update existing event therapists values
foreach ($obj->therapists as $therapist) {
$obj->therapists()->updateExistingPivot($therapist->id, [
'commision_value' => $request->get("therapist_commision_{$therapist->pivot->id}"),
]);
} //end save existing event therapists
//save: attach new therapist to the event
if ($request->has('therapist_id')) {
foreach ($request->therapist_id as $key => $thid) {
$obj->therapists()->attach($thid, [
'commision_value' => $request->get('therapist_commision')[$key],
]);
} //endforeach
} //endif
//commit/rollback transaction
if ($obj) {
//commit transaction
DB::commit();
} else {
//rollback
DB::rollback();
} //endif commit/rollback
//redirect
if ($request->save) {
return redirect(route('admin.corporate.events.edit', ['event' => $obj->id]));
} elseif ($request->save_exit) {
return redirect(route('admin.corporate.events.index'));
}
}
/**
* Delete page
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function delete(CorporateEvent $events)
{
$this->data['obj'] = $events;
return view('corporate::admin.events.delete', $this->data);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(CorporateEvent $events)
{
$obj = $events;
//delete event therapists
$obj->therapists()->detach();
//delete CorporateEvent
$obj->delete();
//set success message
Flash::info(trans('corporate::events.message_confirm_delete', ['name' => $obj->title]));
//redirect to contacts list
return redirect(route('admin.corporate.events.index'));
}
/**
* Add new file to gallery
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function add_therapist()
{
$this->data['form']['therapists_opt'] = User::ofTherapists()->pluck('name', 'id');
return view('corporate::admin.events.partial_new_therapist', $this->data);
}
/**
* Remove file from gallery
* @param $id
*/
public function del_therapist($id)
{
$obj = CorporateEventTherapist::findOrFail($id);
//delete file entry from db
$obj->delete();
}
/**
* Create gallery array from request
* @param $request
* @return mixed
*/
protected function get_request_therapist($request)
{
$data['therapist_id'] = $request->old('therapist_id');
$data['therapist_commision'] = $request->old('therapist_commision');
return $data;
}
/**
* Get CorporateEvent gallery
* @param $obj
*/
protected function get_saved_therapists(CorporateEvent $obj)
{
$results = [];
$list = $obj->therapists;
foreach ($list as $li) {
$results[$li->pivot->id] = [
'id' => $li->pivot->id,
'therapist_id' => $li->pivot->therapist_id,
'commision_value' => $li->pivot->commision_value,
];
} //endforeach
//return results
return $results;
}
/**
* Create event invoice
* @param CorporateEvent $events
*/
public function create_invoice(CorporateEvent $events, CorporateEventInvoiceRequest $request)
{
$obj = $events;
if (!$obj->id) {
flash('Corporate event not found!', 'danger');
return response('', 200);
} //endif
if (isset($obj->invoice()->first()->number)) {
flash('The event has already an invoice attached!', 'danger');
return response('', 200);
} //endif
//create invoice repository object
$repo = new InvoiceRepository();
//generate invoice
$invoice = $repo->new_event_invoice($events, $request);
// dd($invoice);
if (!$invoice) {
return response(['messages' => 'Something went wrong. Please check if the account you are trying generate an invoice for has a "Document Series" created with type "Company Invoice"'], 403);
}
flash()->overlay('The invoice has been successfully created', "Invoice #{$invoice->prefix}{$invoice->number}");
return response('', 200);
}
}