222 lines
7.1 KiB
PHP
222 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\WarehouseManagement;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Dealer;
|
|
use App\Models\Menu;
|
|
use App\Models\Product;
|
|
use App\Models\ProductCategory;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class ProductsController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$menu = Menu::where('link','products.index')->first();
|
|
if($request->ajax()){
|
|
$data = Product::with(['category', 'stocks']);
|
|
return DataTables::of($data)
|
|
->addIndexColumn()
|
|
->addColumn('category_name', function ($row) {
|
|
return $row->category ? $row->category->name : '-';
|
|
})
|
|
->addColumn('total_stock', function ($row){
|
|
return number_format($row->current_total_stock, 2);
|
|
})
|
|
->addColumn('action', function ($row) use ($menu) {
|
|
$btn = '<div class="d-flex">';
|
|
|
|
if (Gate::allows('update', $menu)) {
|
|
$btn .= '<a href="' . route('products.edit', $row->id) . '" class="btn btn-warning btn-sm" style="margin-right: 8px;">Edit</a>';
|
|
}
|
|
|
|
$btn .= '<button class="btn btn-sm btn-toggle-active '
|
|
. ($row->active ? 'btn-danger' : 'btn-success') . '"
|
|
data-url="' . route('products.toggleActive', $row->id) . '" data-active="'.$row->active.'" style="margin-right: 8px;">'
|
|
. ($row->active ? 'Nonaktifkan' : 'Aktifkan') . '</button>';
|
|
|
|
$btn .= '<button class="btn btn-sm btn-secondary btn-product-stock-dealers"
|
|
data-id="'.$row->id.'"
|
|
data-url="'.route('products.dealers_stock').'"
|
|
data-name="'.$row->name.'">Dealer</button>';
|
|
|
|
$btn .= '</div>';
|
|
|
|
return $btn;
|
|
})
|
|
->rawColumns(['action'])
|
|
->make(true);
|
|
}
|
|
return view('warehouse_management.products.index');
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
$categories = ProductCategory::with('children')->whereNull('parent_id')->get();
|
|
$dealers = Dealer::all();
|
|
return view('warehouse_management.products.create', compact('categories', 'dealers'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
try{
|
|
$request->validate([
|
|
'code' => [
|
|
'required',
|
|
'string',
|
|
Rule::unique('products')->whereNull('deleted_at'),
|
|
],
|
|
'name' => 'required|string',
|
|
'description' => 'nullable|string',
|
|
'unit' => 'nullable|string',
|
|
'active' => 'required|boolean',
|
|
'product_category_id' => 'required|exists:product_categories,id'
|
|
]);
|
|
|
|
// Create product
|
|
$product = Product::create([
|
|
'code' => $request->code,
|
|
'name' => $request->name,
|
|
'unit' => $request->unit,
|
|
'active' => $request->active,
|
|
'description' => $request->description,
|
|
'product_category_id' => $request->product_category_id,
|
|
]);
|
|
|
|
return redirect()->route('products.index')->with('success', 'Produk berhasil ditambahkan.');
|
|
}catch(\Exception $ex){
|
|
Log::error($ex->getMessage());
|
|
throw $ex;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
$product = Product::findOrFail($id);
|
|
return view('warehouse_management.products.edit', [
|
|
'product' => $product->load('dealers'),
|
|
'dealers' => Dealer::all(),
|
|
'categories' => ProductCategory::with('children')->get(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, Product $product)
|
|
{
|
|
try{
|
|
$request->validate([
|
|
'code' => [
|
|
'required',
|
|
'string',
|
|
Rule::unique('products')->ignore($product->id)->whereNull('deleted_at'),
|
|
],
|
|
'name' => 'required|string',
|
|
'description' => 'nullable|string',
|
|
'unit' => 'nullable|string',
|
|
'active' => 'required|boolean',
|
|
'product_category_id' => 'required|exists:product_categories,id'
|
|
]);
|
|
|
|
$product->update($request->only(['code', 'name', 'description', 'unit','active', 'product_category_id']));
|
|
|
|
return redirect()->route('products.index')->with('success', 'Produk berhasil diperbarui.');
|
|
}catch(\Exception $ex){
|
|
Log::error($ex->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy(Product $product)
|
|
{
|
|
$product->delete();
|
|
|
|
return redirect()->route('products.index')->with('success', 'Produk berhasil dihapus.');
|
|
}
|
|
public function toggleActive(Request $request, Product $product)
|
|
{
|
|
$product->active = !$product->active;
|
|
$product->save();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'active' => $product->active,
|
|
'message' => 'Status produk berhasil diperbarui.'
|
|
]);
|
|
}
|
|
|
|
public function all_products(){
|
|
try{
|
|
$products = Product::where('is_active', true)->select('id','name')->get();
|
|
return response()->json($products);
|
|
}catch(\Exception $ex){
|
|
Log::error($ex->getMessage());
|
|
}
|
|
}
|
|
|
|
public function dealers_stock(Request $request){
|
|
$productId = $request->get('product_id');
|
|
|
|
$product = Product::with(['stocks.dealer'])->findOrFail($productId);
|
|
|
|
$data = $product->stocks->map(function ($stock) {
|
|
return [
|
|
'dealer_name' => $stock->dealer->name ?? '-',
|
|
'quantity' => $stock->quantity
|
|
];
|
|
});
|
|
|
|
return DataTables::of($data)->make(true);
|
|
}
|
|
}
|