partial update products
This commit is contained in:
@@ -7,10 +7,10 @@ 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 Illuminate\Support\Facades\Log;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
@@ -49,8 +49,10 @@ class ProductsController extends Controller
|
||||
|
||||
$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.'">'
|
||||
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>';
|
||||
|
||||
@@ -93,10 +95,7 @@ class ProductsController extends Controller
|
||||
'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_category_id' => 'required|exists:product_categories,id'
|
||||
]);
|
||||
|
||||
// Create product
|
||||
@@ -108,34 +107,6 @@ class ProductsController extends Controller
|
||||
'description' => $request->description,
|
||||
'product_category_id' => $request->product_category_id,
|
||||
]);
|
||||
|
||||
|
||||
|
||||
// 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;
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
return redirect()->route('products.index')->with('success', 'Produk berhasil ditambahkan.');
|
||||
}catch(\Exception $ex){
|
||||
@@ -179,77 +150,26 @@ class ProductsController extends Controller
|
||||
*/
|
||||
public function update(Request $request, Product $product)
|
||||
{
|
||||
$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',
|
||||
'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', 'unit', 'product_category_id']));
|
||||
|
||||
// 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) {
|
||||
$dealerId = $item['dealer_id'];
|
||||
$newQty = $item['quantity'];
|
||||
$syncData[$dealerId] = ['quantity' => $newQty];
|
||||
$newStocks[$dealerId] = $newQty;
|
||||
}
|
||||
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());
|
||||
}
|
||||
|
||||
// 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.');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user