IMMREX7
<?php
namespace App\Http\Controllers\School\Stock;
use Illuminate\Http\Request;
use App\Http\Controllers\School\SchoolController;
use DB;
use Session;
use Auth;
class StoreLocationController extends SchoolController {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$locations = \App\StoreLocation::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idStoreLocation', 'desc')->get();
return view('schools.stock.store-locations', compact('locations'));
}
/**
* 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) {
$rules = [];
foreach ($request->locations as $key => $val) {
$rules['locations.' . $key . '.locationName'] = 'unique:store_locations,locationName,NULL,idStoreLocation,idSchool,' . Auth::guard('school')->user()->idSchool;
}
$message = [
'locations.*locationName.unique' => 'Store Location Already Exist.',
];
$this->Validate($request, $rules, $message);
$location = new \App\StoreLocation();
$location->fill($request->all());
$location->idSchool = Auth::guard('school')->user()->idSchool;
$location->idFinancialYear = Session::get('idFinancialYear');
$location->save();
foreach ($request->locations as $var) {
$sublocation = new \App\SubLocation();
$sublocation->idStoreLocation = $location->idStoreLocation;
$sublocation->subLocation = $var['subLocation'];
$sublocation->save();
}
flash('Store Location has been added successfully!!');
if ($request->ajax()) {
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
return redirect('school/storelocation');
}
/**
* 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) {
$locations = \App\StoreLocation::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idStoreLocation', 'desc')->get();
$location = \App\StoreLocation::where('idStoreLocation', '=', $id)->first();
return view('schools.stock.store-locations', compact('locations', 'location'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id) {
$location = \App\StoreLocation::where('idStoreLocation', '=', $id)->first();
$rules = [
'locationName' => 'required|unique:store_locations,locationName,' . $id . ',idStoreLocation,idSchool,' . Auth::guard('school')->user()->idSchool,
];
$this->validate($request, $rules);
$location->fill($request->all());
$location->update();
foreach ($request->locations as $a => $v) {
if ($v['subLocation'] != null) {
if (isset($v['idStoreLocation'])) {
$subloc = \App\SubLocation::where('idStoreLocation', '=', $v['idStoreLocation'])->first();
} else {
$subloc = new \App\SubLocation();
}
$subloc->idStoreLocation = $location->idStoreLocation;
$subloc->subLocation = $v['subLocation'];
$subloc->save();
}
}
return redirect('school/storelocation');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$location = \App\StoreLocation::where('idStoreLocation', '=', $id)->first();
DB::beginTransaction();
$location->delete();
$location->sublocations()->delete();
DB::commit();
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
public function subLocations($id) {
$sublocation = \App\SubLocation::where('idStoreLocation', '=', $id)
->get()->pluck('subLocation','idSubLocation')->toArray();
return json_encode($sublocation);
}
}
Copyright © 2021 -