partial update create toggle active product and mutations
This commit is contained in:
@@ -7,10 +7,12 @@ use App\Models\Dealer;
|
||||
use App\Models\Menu;
|
||||
use App\Models\Product;
|
||||
use App\Models\ProductCategory;
|
||||
use App\Models\StockMutation;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class ProductsController extends Controller
|
||||
{
|
||||
@@ -37,13 +39,18 @@ class ProductsController extends Controller
|
||||
->addColumn('action', function ($row) use ($menu) {
|
||||
$btn = '<div class="d-flex">';
|
||||
|
||||
if (Auth::user()->can('delete', $menu)) {
|
||||
$btn .= '<button style="margin-right: 8px;" class="btn btn-danger btn-sm btn-destroy-product" data-action="' . route('products.destroy', $row->id) . '" data-id="' . $row->id . '">Hapus</button>';
|
||||
}
|
||||
// if (Auth::user()->can('delete', $menu)) {
|
||||
// $btn .= '<button style="margin-right: 8px;" class="btn btn-danger btn-sm btn-destroy-product" data-action="' . route('products.destroy', $row->id) . '" data-id="' . $row->id . '">Hapus</button>';
|
||||
// }
|
||||
|
||||
if (Auth::user()->can('update', $menu)) {
|
||||
$btn .= '<a href="' . route('products.edit', $row->id) . '" class="btn btn-warning btn-sm">Edit</a>';
|
||||
$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.'">'
|
||||
. ($row->active ? 'Nonaktifkan' : 'Aktifkan') . '</button>';
|
||||
|
||||
$btn .= '</div>';
|
||||
|
||||
@@ -76,9 +83,15 @@ class ProductsController extends Controller
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'code' => 'required|string|unique:products,code',
|
||||
'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',
|
||||
'dealer_stock' => 'nullable|array',
|
||||
'dealer_stock.*.dealer_id' => 'required|exists:dealers,id',
|
||||
@@ -89,19 +102,33 @@ class ProductsController extends Controller
|
||||
$product = Product::create([
|
||||
'code' => $request->code,
|
||||
'name' => $request->name,
|
||||
'unit' => $request->unit,
|
||||
'description' => $request->description,
|
||||
'product_category_id' => $request->product_category_id,
|
||||
]);
|
||||
|
||||
// Prepare dealer stock for pivot
|
||||
// Prepare dealer stock for pivot and create mutation records
|
||||
$pivotData = [];
|
||||
if ($request->has('dealer_stock')) {
|
||||
foreach ($request->dealer_stock as $stockData) {
|
||||
if (empty($stockData['dealer_id']) || !isset($stockData['quantity'])) continue;
|
||||
|
||||
$pivotData[$stockData['dealer_id']] = ['quantity' => $stockData['quantity']];
|
||||
|
||||
$dealerId = $stockData['dealer_id'];
|
||||
$quantity = $stockData['quantity'];
|
||||
|
||||
$pivotData[$dealerId] = ['quantity' => $quantity];
|
||||
|
||||
// Create stock mutation for initial stock "in"
|
||||
StockMutation::create([
|
||||
'product_id' => $product->id,
|
||||
'dealer_id' => $dealerId,
|
||||
'mutation_type' => 'in', // karena ini penambahan stok awal
|
||||
'quantity' => $quantity,
|
||||
'description' => 'Initial stock added when product created',
|
||||
'user_id' => Auth::id(),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
// Attach dealer stock using pivot table
|
||||
$product->dealers()->attach($pivotData);
|
||||
}
|
||||
@@ -146,28 +173,75 @@ class ProductsController extends Controller
|
||||
public function update(Request $request, Product $product)
|
||||
{
|
||||
$request->validate([
|
||||
'code' => 'required|string|unique:products,code,' . $product->id,
|
||||
'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',
|
||||
'dealer_stock' => 'nullable|array',
|
||||
'dealer_stock.*.dealer_id' => 'required|exists:dealers,id',
|
||||
'dealer_stock.*.quantity' => 'required|integer|min:0',
|
||||
]);
|
||||
|
||||
$product->update($request->only(['code', 'name', 'description', 'product_category_id']));
|
||||
$product->update($request->only(['code', 'name', 'description', 'unit', 'product_category_id']));
|
||||
|
||||
// Prepare pivot sync data
|
||||
// Ambil stok lama dari pivot
|
||||
$oldStocks = $product->dealers()->pluck('quantity', 'dealer_id')->toArray();
|
||||
|
||||
// Data baru untuk sync pivot
|
||||
$syncData = [];
|
||||
|
||||
$newStocks = [];
|
||||
if ($request->has('dealer_stock')) {
|
||||
foreach ($request->dealer_stock as $item) {
|
||||
$syncData[$item['dealer_id']] = ['quantity' => $item['quantity']];
|
||||
$dealerId = $item['dealer_id'];
|
||||
$newQty = $item['quantity'];
|
||||
$syncData[$dealerId] = ['quantity' => $newQty];
|
||||
$newStocks[$dealerId] = $newQty;
|
||||
}
|
||||
}
|
||||
|
||||
// Sync with pivot table
|
||||
// Sync pivot table
|
||||
$product->dealers()->sync($syncData);
|
||||
|
||||
// Hitung mutasi stok (selisih)
|
||||
// Mutasi stok untuk stok baru atau perubahan stok
|
||||
foreach ($newStocks as $dealerId => $newQty) {
|
||||
$oldQty = $oldStocks[$dealerId] ?? 0;
|
||||
$diff = $newQty - $oldQty;
|
||||
|
||||
if ($diff != 0) {
|
||||
StockMutation::create([
|
||||
'product_id' => $product->id,
|
||||
'dealer_id' => $dealerId,
|
||||
'mutation_type' => $diff > 0 ? 'in' : 'out',
|
||||
'quantity' => abs($diff),
|
||||
'description' => 'Stock updated via product update',
|
||||
'user_id' => auth()->id(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// Mutasi stok untuk dealer yang dihapus (stok jadi 0)
|
||||
$deletedDealers = array_diff_key($oldStocks, $newStocks);
|
||||
foreach ($deletedDealers as $dealerId => $oldQty) {
|
||||
if ($oldQty > 0) {
|
||||
StockMutation::create([
|
||||
'product_id' => $product->id,
|
||||
'dealer_id' => $dealerId,
|
||||
'mutation_type' => 'out',
|
||||
'quantity' => $oldQty,
|
||||
'description' => 'Stock removed via product update (dealer removed)',
|
||||
'user_id' => auth()->id(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()->route('products.index')->with('success', 'Produk berhasil diperbarui.');
|
||||
}
|
||||
|
||||
@@ -179,12 +253,41 @@ class ProductsController extends Controller
|
||||
*/
|
||||
public function destroy(Product $product)
|
||||
{
|
||||
// Detach all dealer relationships (optional if using cascade on delete)
|
||||
// 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();
|
||||
|
||||
// Delete the product
|
||||
// Hapus produk
|
||||
$product->delete();
|
||||
|
||||
return response()->json(['success' => true, 'message' => 'Produk berhasil dihapus.']);
|
||||
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.'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user