StocksNew/Modules/Ingredient/app/Http/Controllers/IngredientController.php
2024-09-19 18:33:08 +05:45

153 lines
4.8 KiB
PHP

<?php
namespace Modules\Ingredient\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Modules\Ingredient\Models\Unit;
use Modules\Ingredient\Models\Ingredient;
use Modules\Ingredient\Repositories\IngredientCategoryRepository;
use Modules\Ingredient\Repositories\UnitRepository;
use Modules\Ingredient\Repositories\IngredientInterface;
use Modules\Ingredient\Repositories\IngredientRepository;
use Modules\Supplier\Models\Supplier;
use Modules\Supplier\Repositories\SupplierRepository;
class IngredientController extends Controller
{
private $ingredientRepository;
private $ingredientCategoryRepository;
private $unitRepository;
private $supplierRepository;
/**
* Display a listing of the resource.
*/
public function __construct(IngredientRepository $ingredientRepository,
IngredientCategoryRepository $ingredientCategoryRepository,
SupplierRepository $supplierRepository,
UnitRepository $unitRepository)
{
$this->ingredientRepository = $ingredientRepository;
$this->ingredientCategoryRepository = $ingredientCategoryRepository;
$this->unitRepository = $unitRepository;
$this->supplierRepository = $supplierRepository;
}
public function index()
{
$data['title'] = "Ingredient Lists";
$data['ingredients'] = $this->ingredientRepository->findAll();
return view('ingredient::ingredient.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$data['title'] = 'Create Ingredient';
$data['ingredientCategory'] = $this->ingredientCategoryRepository->pluck();
$data['unit'] = $this->unitRepository->pluck();
$data['supplier'] = $this->supplierRepository->pluck();
$data['status'] = Ingredient::STATUS;
return view('ingredient::ingredient.create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
$inputData = $request->all();
$this->ingredientRepository->create($inputData);
toastr()->success('Ingredient Created Succesfully');
return redirect()->route('ingredient.index');
}
/**
* Show the specified resource.
*/
public function show($id)
{
$data['title'] = 'Show Ingredient';
$data['ingredient'] = $this->ingredientRepository->getIngredientById($id);
$data['status'] = Ingredient::STATUS;
return view('ingredient::ingredient.show', $data);
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$data['title'] = 'Show Ingredient';
$data['ingredientCategory'] = $this->ingredientCategoryRepository->pluck();
$data['unit'] = $this->unitRepository->pluck();
$data['supplier'] = $this->supplierRepository->pluck();
$data['ingredient'] = $this->ingredientRepository->getIngredientById($id);
$data['status'] = Ingredient::STATUS;
return view('ingredient::ingredient.edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id): RedirectResponse
{
$inputData = $request->except(['_method', '_token']);
$this->ingredientRepository->update($id, $inputData);
return redirect()->route('ingredient.index');
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
try {
$IngredientModel = $this->ingredientRepository->getIngredientById($id);
$IngredientModel->delete();
toastr()->success('Ingredient Delete Succesfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return response()->json(['status' => true, 'message' => 'Ingredient Delete Succesfully']);
}
public function getIngredientDetail(Request $request)
{
try {
$ingredientModel = $this->ingredientRepository->getIngredientById($request->id);
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return response()->json([
'status' => true,
'data' => $ingredientModel,
]);
}
public function getIngredientsByCategory(Request $request)
{
$ingredientCategoryId = $request->ingredient_category_id;
try {
$ingredients = $this->ingredientRepository->getIngredientsByCategory($ingredientCategoryId);
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return response()->json(['ingredients' => $ingredients]);
}
}