215 lines
6.9 KiB
PHP
215 lines
6.9 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 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','dealers']);
|
|
return DataTables::of($data)
|
|
->addIndexColumn()
|
|
->addColumn('category_name', function ($row) {
|
|
return $row->category ? $row->category->name : '-';
|
|
})
|
|
->addColumn('total_stock', function ($row){
|
|
return 0;
|
|
})
|
|
->addColumn('action', function ($row) use ($menu) {
|
|
$btn = '<div class="d-flex">';
|
|
|
|
if (Auth::user()->can('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">Stock</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){
|
|
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)
|
|
{
|
|
// Ambil stok pivot sebelum hapus
|
|
$dealerStocks = $product->dealers()->pluck('quantity', 'dealer_id')->toArray();
|
|
|
|
// Buat mutasi stok keluar (out) untuk semua stok yang dihapus
|
|
foreach ($dealerStocks as $dealerId => $qty) {
|
|
if ($qty > 0) {
|
|
StockMutation::create([
|
|
'product_id' => $product->id,
|
|
'dealer_id' => $dealerId,
|
|
'mutation_type' => 'out',
|
|
'quantity' => $qty,
|
|
'description' => 'Stock removed due to product deletion',
|
|
'user_id' => auth()->id(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
// Hapus pivot stok dealer
|
|
$product->dealers()->detach();
|
|
|
|
// Hapus produk
|
|
$product->delete();
|
|
|
|
return redirect()->route('products.index')->with('success', 'Produk berhasil dihapus.');
|
|
}
|
|
public function toggleActive(Request $request, Product $product)
|
|
{
|
|
// You can add authorization here
|
|
$product->active = !$product->active;
|
|
$product->save();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'active' => $product->active,
|
|
'message' => 'Status produk berhasil diperbarui.'
|
|
]);
|
|
}
|
|
}
|