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/Vouchers/Http/Controllers/Admin/ |
<?php
namespace App\Modules\Vouchers\Http\Controllers\Admin;
use App\Http\Controllers\AdminController;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Modules\Vouchers\Http\Requests\VoucherGeneralRequest;
use App\Modules\Vouchers\Http\Requests\VoucherOfferRequest;
use App\Modules\Vouchers\Models\VoucherGeneral;
use App\Modules\Vouchers\Repositories\VoucherRepository;
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 GeneralVoucherController extends AdminController
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$this->data['page_title'] = trans('vouchers::general.page_title');
return view('vouchers::admin.general_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('value', function($o){
return $this->text_right($o->value,$o->type);
})
->editColumn('is_active', function($o){
$active = trans_choice('vouchers::general.text_is_active',$o->is_active);
$active .= " [{$o->date_start} - {$o->date_end} ]";
return $active;
})
->addColumn('actions', function ($o) {
$this->data['o']=$o;
return view('vouchers::admin.general_list_actions',$this->data)->render();
})
->rawColumns([
'value',
'is_active',
'actions'
])
->removeColumn('id')
->removeColumn('updated_at')
->removeColumn('date_start')
->removeColumn('date_end')
->make(true);
}
/**
* Get object entries
* @param Request $request
*/
protected function getData(Request $request)
{
$obj = VoucherGeneral::query();
//return object
return $obj;
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
$this->data['page_title'] = trans('vouchers::general.page_title');
$this->data['form']['types'] = trans('vouchers::general.form_type_opt');
//show page
return view('vouchers::admin.general_create_edit', $this->data);
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store(VoucherGeneralRequest $request)
{
$data = [
'name' => $request->name,
'code' => $request->code,
'type' => $request->type,
'value' => $request->value,
'is_active' => $request->is_active,
'edited_by_id' => Auth::user()->id,
'date_start' => Carbon::createFromFormat('d F Y',$request->date_start)->format('Y-m-d'),
'date_end' => Carbon::createFromFormat('d F Y',$request->date_end)->format('Y-m-d'),
];
DB::beginTransaction();
//save new service type
$obj = VoucherGeneral::create($data);
//create voucher code in voucher list
$repo = new VoucherRepository();
$repo->store_general($obj);
if ($obj){
DB::commit();
}
else{
DB::rollBack();
}
//redirect
if ($request->save) {
return redirect(route('admin.vouchers.general.edit', ['id' => $obj->id]));
} elseif ($request->save_exit) {
return redirect(route('admin.vouchers.general.index'));
}
}
/**
* Edit Service Type
*
* @param $label
* @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function edit($obj)
{
$data = VoucherGeneral::find($obj);
$this->data['page_title'] = trans('vouchers::general.page_title');
$this->data['form']['types'] = trans('vouchers::general.form_type_opt');
$this->data['obj'] = $data;
//show page
return view('vouchers::admin.general_create_edit', $this->data);
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update(VoucherGeneralRequest $request, $obj)
{
$data = VoucherGeneral::find($obj);
//fields to be updated
$fillable = $data->getFillable();
$fields = array();
foreach ($request->all() as $field => $f) {
if (in_array($field, $fillable)) {
$fields[$field] = $f;
}
} //end foreach
$fields['is_active'] = (int)$request->is_active;
$fields['date_start'] = Carbon::createFromFormat('d F Y',$request->date_start)->format('Y-m-d');
$fields['date_end'] = Carbon::createFromFormat('d F Y',$request->date_end)->format('Y-m-d');
$fields['edited_by_id'] = Auth::user()->id;
DB::beginTransaction();
//update label
$updObj = VoucherGeneral::where('id', $data->id)->update($fields);
//update voucher code in voucher list
$repo = new VoucherRepository();
$repo->update_general(VoucherGeneral::find($data->id));
if ($updObj){
DB::commit();
}
else{
DB::rollBack();
}
//redirect
if ($request->save) {
return redirect(route('admin.vouchers.general.edit', ['general' => $data->id]));
} elseif ($request->save_exit) {
return redirect(route('admin.vouchers.general.index'));
}
}
/**
* Delete page
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function delete($obj)
{
$this->data['obj'] = $obj;
return view('vouchers::admin.general_delete', $this->data);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($obj)
{
//update voucher code in voucher list
$repo = new VoucherRepository();
$repo->delete_general($obj);
//set object name
$obj_name = $obj->name;
//delete contact
$obj->delete();
//set success message
Flash::info(trans('vouchers::general.message_confirm_delete', ['name' => $obj->name, 'code' =>$obj->code]));
//redirect to contacts list
return redirect(route('admin.vouchers.general.index'));
}
/**
* Format text right
* @param $string
* @param bool $number
* @return string
*/
protected function text_right($string,$type)
{
$value = $string;
switch($type){
case "proc":
$value = number_format($string,2,'.',',')." %";
break;
case "value":
$value = number_format($string,2,'.',',')." £";
break;
}
return "<div class='text-right'>{$value}</div>";
}
/**
* Generate new voucher code
* @return string
*/
public function newcode()
{
return strtolower(str_random(20));
}
}