partial update create modal list dealers

This commit is contained in:
2025-06-05 12:05:20 +07:00
parent ff498cd98f
commit ce0a4718e0
12 changed files with 303 additions and 31 deletions

View File

@@ -25,14 +25,14 @@ class ProductsController extends Controller
{
$menu = Menu::where('link','products.index')->first();
if($request->ajax()){
$data = Product::with(['category','dealers']);
$data = Product::with(['category','opnameDetails']);
return DataTables::of($data)
->addIndexColumn()
->addColumn('category_name', function ($row) {
return $row->category ? $row->category->name : '-';
})
->addColumn('total_stock', function ($row){
return 0;
return $row->opnameDetails->sum('system_stock');
})
->addColumn('action', function ($row) use ($menu) {
$btn = '<div class="d-flex">';
@@ -46,8 +46,11 @@ class ProductsController extends Controller
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 .= '<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;
@@ -104,6 +107,7 @@ class ProductsController extends Controller
return redirect()->route('products.index')->with('success', 'Produk berhasil ditambahkan.');
}catch(\Exception $ex){
Log::error($ex->getMessage());
throw $ex;
}
}
@@ -174,34 +178,12 @@ class ProductsController extends Controller
*/
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();
@@ -211,4 +193,33 @@ class ProductsController extends Controller
'message' => 'Status produk berhasil diperbarui.'
]);
}
public function all_products(){
try{
$products = Product::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(['opnameDetails.opname.dealer'])->findOrFail($productId);
$opnameDetails = $product->opnameDetails;
$data = $opnameDetails->map(function ($detail) {
return [
'dealer_name' => $detail->opname->dealer->name ?? '-',
'system_stock' => $detail->system_stock,
'physical_stock' => $detail->physical_stock,
'difference' => $detail->physical_stock - $detail->system_stock,
'opname_date' => optional($detail->opname)->created_at->format('d M Y')
];
});
return DataTables::of($data)->make(true);
}
}