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

110 lines
3.2 KiB
PHP

<?php
namespace Modules\Stock\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Modules\Product\Repositories\ProductRepository;
use Modules\Stock\Models\Stock;
use Modules\Stock\Repositories\StockLocationRepository;
use Modules\Stock\Repositories\StockRepository;
class StockController extends Controller
{
private $stockRepository;
private $productRepository;
private $stockLocationRepository;
public function __construct(StockRepository $stockRepository,
StockLocationRepository $stockLocationRepository,
ProductRepository $productRepository)
{
$this->stockRepository = $stockRepository;
$this->productRepository = $productRepository;
$this->stockLocationRepository = $stockLocationRepository;
}
public function index()
{
$data['title'] = 'Stocks List';
$data['stocks'] = $this->stockRepository->findAll();
return view('stock::stock.index', $data);
}
public function create()
{
$data['title'] = 'Create Stock';
$data['stockLocation'] = $this->stockLocationRepository->pluck();
$data['product'] = $this->productRepository->pluck();
$data['status'] = Stock::STATUS;
return view('stock::stock.create', $data);
}
public function store(Request $request): RedirectResponse
{
$request->request->add(['slug' => slugify($request->title)]);
$inputData = $request->all();
$this->stockRepository->create($inputData);
toastr()->success('Stock Created Successfully');
return redirect()->route('stock.index');
}
public function show($id)
{
$data['title'] = 'Show Stock';
$data['status'] = Stock::STATUS;
$data['stock'] = $this->stockRepository->getStockById($id);
return view('stock::stock.show', $data);
}
public function edit($id)
{
$data['title'] = 'Edit Stock';
$data['stockLocation'] = $this->stockLocationRepository->pluck();
$data['product'] = $this->productRepository->pluck();
$data['status'] = Stock::STATUS;
$data['stock'] = $this->stockRepository->getStockById($id);
return view('stock::stock.edit', $data);
}
public function update(Request $request, $id): RedirectResponse
{
$inputData = $request->except(['_method', '_token']);
$this->stockRepository->update($id, $inputData);
return redirect()->route('stock.index');
}
public function destroy($id)
{
try {
$stock = $this->stockRepository->getStockById($id);
$stock->delete();
toastr()->success('Stock Deleted Successfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return response()->json(['status' => true, 'message' => 'Stock Deleted Successfully']);
}
public function getStocksByProduct(Request $request)
{
$productId = $request->product_id;
try {
$stocks = $this->stockRepository->getStocksByProduct($productId);
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return response()->json(['stocks' => $stocks]);
}
}