partial update create page opnames
This commit is contained in:
@@ -82,58 +82,65 @@ class ProductsController extends Controller
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$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',
|
||||
'dealer_stock' => 'nullable|array',
|
||||
'dealer_stock.*.dealer_id' => 'required|exists:dealers,id',
|
||||
'dealer_stock.*.quantity' => 'required|integer|min:0',
|
||||
]);
|
||||
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',
|
||||
'dealer_stock' => 'nullable|array',
|
||||
'dealer_stock.*.dealer_id' => 'required|exists:dealers,id',
|
||||
'dealer_stock.*.quantity' => 'required|integer|min:0',
|
||||
]);
|
||||
|
||||
// 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,
|
||||
]);
|
||||
|
||||
|
||||
|
||||
// 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;
|
||||
|
||||
// Create product
|
||||
$product = Product::create([
|
||||
'code' => $request->code,
|
||||
'name' => $request->name,
|
||||
'unit' => $request->unit,
|
||||
'description' => $request->description,
|
||||
'product_category_id' => $request->product_category_id,
|
||||
]);
|
||||
$dealerId = $stockData['dealer_id'];
|
||||
$quantity = $stockData['quantity'];
|
||||
|
||||
// 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(),
|
||||
]);
|
||||
$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);
|
||||
}
|
||||
|
||||
// Attach dealer stock using pivot table
|
||||
$product->dealers()->attach($pivotData);
|
||||
|
||||
return redirect()->route('products.index')->with('success', 'Produk berhasil ditambahkan.');
|
||||
}catch(\Exception $ex){
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
return redirect()->route('products.index')->with('success', 'Produk berhasil ditambahkan.');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\WarehouseManagement;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\StockOpname;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class StockOpnamesController extends Controller
|
||||
{
|
||||
public function index(Request $request){
|
||||
try{
|
||||
if ($request->ajax()) {
|
||||
$query = StockOpname::with(['product', 'dealer', 'user']);
|
||||
|
||||
return DataTables::of($query)
|
||||
->addIndexColumn()
|
||||
->addColumn('product_name', function ($row) {
|
||||
return $row->product ? $row->product->name : '-';
|
||||
})
|
||||
->addColumn('dealer_name', function ($row) {
|
||||
return $row->dealer ? $row->dealer->name : '-';
|
||||
})
|
||||
->addColumn('user_name', function ($row) {
|
||||
return $row->user ? $row->user->name : '-';
|
||||
})
|
||||
->editColumn('opname_date', function ($row) {
|
||||
return $row->opname_date->format('d M Y');
|
||||
})
|
||||
->make(true);
|
||||
}
|
||||
|
||||
return view('warehouse_management.stock_opnames.index');
|
||||
}catch(\Exception $ex){
|
||||
Log::error($ex->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user