StocksNew/Modules/Product/app/Http/Controllers/ProductController.php
2024-09-17 16:34:40 +05:45

165 lines
5.3 KiB
PHP

<?php
namespace Modules\Product\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Modules\Product\Models\FabricCategory;
use Modules\Product\Models\Product;
use Modules\Product\Repositories\CategoryRepository;
use Modules\Product\Repositories\FabricCategoryRepository;
use Modules\Product\Repositories\ProductInterface;
use Modules\Product\Repositories\ProductRepository;
use Modules\Product\Repositories\SubCategoryRepository;
use Modules\Product\Repositories\WarehouseRepository;
use Modules\Supplier\Models\Supplier;
use Modules\Supplier\Repositories\SupplierRepository;
class ProductController extends Controller
{
private $productRepository;
private $categoryRepository;
private $subCategoryRepository;
private $fabricCategoryRepository;
private $warehouseRepository;
private $supplierRepository;
/**
* Display a listing of the resource.
*/
public function __construct(ProductRepository $productRepository,
CategoryRepository $categoryRepository,
SubCategoryRepository $subCategoryRepository,
SupplierRepository $supplierRepository,
FabricCategoryRepository $fabricCategoryRepository,
WarehouseRepository $warehouseRepository)
{
$this->productRepository = $productRepository;
$this->categoryRepository = $categoryRepository;
$this->subCategoryRepository = $subCategoryRepository;
$this->fabricCategoryRepository = $fabricCategoryRepository;
$this->supplierRepository = $supplierRepository;
$this->warehouseRepository = $warehouseRepository;
}
public function index()
{
$data['title'] = "Product Lists";
$data['products'] = $this->productRepository->findAll();
return view('product::product.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$data['title'] = 'Create Product';
$data['category'] = $this->categoryRepository->pluck();
$data['fabricCategory'] = $this->fabricCategoryRepository->pluck();
$data['subCategory'] = $this->subCategoryRepository->pluck();
$data['supplier'] = $this->supplierRepository->pluck();
$data['warehouse'] = $this->warehouseRepository->pluck();
$data['status'] = Product::STATUS;
return view('product::product.create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
$inputData = $request->all();
$this->productRepository->create($inputData);
toastr()->success('Product Created Succesfully');
return redirect()->route('product.index');
}
/**
* Show the specified resource.
*/
public function show($id)
{
$data['title'] = 'Show Product';
$data['product'] = $this->productRepository->getProductById($id);
$data['status'] = Product::STATUS;
return view('product::product.show', $data);
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$data['title'] = 'Show Product';
$data['category'] = $this->categoryRepository->pluck();
$data['subCategory'] = $this->subCategoryRepository->pluck();
$data['fabricCategory'] = $this->fabricCategoryRepository->pluck();
$data['supplier'] = $this->supplierRepository->pluck();
$data['warehouse'] = $this->warehouseRepository->pluck();
$data['product'] = $this->productRepository->getProductById($id);
$data['status'] = Product::STATUS;
return view('product::product.edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id): RedirectResponse
{
$inputData = $request->except(['_method', '_token']);
$this->productRepository->update($id, $inputData);
return redirect()->route('product.index');
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
try {
$ProductModel = $this->productRepository->getProductById($id);
$ProductModel->delete();
toastr()->success('Product Delete Succesfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return response()->json(['status' => true, 'message' => 'Product Delete Succesfully']);
}
public function getProductDetail(Request $request)
{
try {
$productModel = $this->productRepository->getProductById($request->id);
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return response()->json([
'status' => true,
'data' => $productModel,
]);
}
public function getProductsByCategory(Request $request)
{
$categoryId = $request->category_id;
try {
$products = $this->productRepository->getProductsByCategory($categoryId);
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return response()->json(['products' => $products]);
}
}