IMMREX7
<?php
namespace App\Http\Controllers\School;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use DB;
use Session;
class ShiftController extends SchoolController {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request) {
$weekdays = \App\Weekday::orderBy('idWeekday')->get()->pluck('dayName','idWeekday')->toArray();
$shifts = \App\Shift::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idShift', 'desc');
if ($request->has('idFinancialYear')) {
$shifts = $shifts->where('idFinancialYear', '=', $request->idFinancialYear)->get();
} else {
$shifts = $shifts->where('idFinancialYear', '=', Session::get('idFinancialYear'))->get();
}
return view('schools.hrms.workshift', compact('shifts','weekdays'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request) {
$shift = new \App\Shift();
$shift->fill($request->all());
$shift->idSchool = Auth::guard('school')->user()->idSchool;
$shift->idFinancialYear = Session::get('idFinancialYear');
DB::beginTransaction();
$shift->save();
foreach ($request->shifts as $key => $value) {
//dd($value);
$details = new \App\ShiftDetail();
$details->fill($request->all());
$details->idWeekday = $key;
$details->idShift = $shift->idShift;
$details->fromTime = $value['fromTime'];
$details->toTime = $value['toTime'];
if (array_key_exists('shift_from', $value))
$details->shift_from = $value['shift_from'];
if (array_key_exists('shift_to', $value))
$details->shift_to = $value['shift_to'];
$details->weekOff = isset($value['weekOff']) ? 'Y' : 'N';
if($details->weekOff == "Y"){
$details->shift_from = null;
$details->shift_to = null;
}
$details->save();
}
DB::commit();
return redirect('school/shifts');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id) {
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id) {
$shifts = \App\Shift::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idShift', 'desc')->get();
$shift = \App\Shift::where('idShift', '=', $id)->first();
return view('schools.hrms.workshift', compact('shifts', 'shift'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id) {
$shift = \App\Shift::where('idShift', '=', $id)->first();
$shift->fill($request->all());
DB::beginTransaction();
$shift->update();
foreach ($request->shifts as $key => $value) {
//dd($value);
$details = \App\ShiftDetail::where('idShift','=',$shift->idShift)->where('idWeekday','=',$key)->first();
$details->fill($request->all());
$details->idWeekday = $key;
$details->idShift = $shift->idShift;
$details->fromTime = $value['fromTime'];
$details->toTime = $value['toTime'];
if (array_key_exists('shift_from', $value))
$details->shift_from = $value['shift_from'];
if (array_key_exists('shift_to', $value))
$details->shift_to = $value['shift_to'];
$details->weekOff = isset($value['weekOff']) ? 'Y' : 'N';
if($details->weekOff == "Y"){
$details->shift_from = null;
$details->shift_to = null;
}
$details->update();
}
DB::commit();
return redirect('school/shifts');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$shift = \App\Shift::where('idShift', '=', $id)->first();
DB::beginTransaction();
$shift->details()->delete();
$shift->delete();
DB::commit();
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
}
Copyright © 2021 -