partial update create modal list dealers
This commit is contained in:
@@ -3,8 +3,11 @@
|
||||
namespace App\Http\Controllers\WarehouseManagement;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Dealer;
|
||||
use App\Models\Menu;
|
||||
use App\Models\Opname;
|
||||
use App\Models\OpnameDetail;
|
||||
use App\Models\Product;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
@@ -41,12 +44,52 @@ class OpnamesController extends Controller
|
||||
}
|
||||
|
||||
public function create(){
|
||||
return view('warehouse_management.opnames.create');
|
||||
try{
|
||||
$dealers = Dealer::all();
|
||||
$products = Product::all();
|
||||
return view('warehouse_management.opnames.create', compact('dealers','products'));
|
||||
}catch(\Exception $ex){
|
||||
Log::error($ex->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function store(Request $request){
|
||||
try{
|
||||
|
||||
$request->validate([
|
||||
'dealer' => 'required|exists:dealers,id',
|
||||
'product' => 'required|array',
|
||||
'product.*' => 'nullable|exists:products,id',
|
||||
'system_quantity' => 'required|array',
|
||||
'physical_quantity' => 'required|array',
|
||||
]);
|
||||
|
||||
// 1. Create Opname master record
|
||||
$opname = Opname::create([
|
||||
'dealer_id' => $request->dealer,
|
||||
'opname_date' => now(), // or $request->opname_date if you provide it
|
||||
'user_id' => auth()->id(), // assuming the user is logged in
|
||||
'note' => null, // or $request->note if needed
|
||||
]);
|
||||
|
||||
// 2. Loop over products to create OpnameDetails
|
||||
foreach ($request->product as $index => $productId) {
|
||||
if (!$productId) continue; // Skip empty rows
|
||||
|
||||
$system = $request->system_quantity[$index] ?? 0;
|
||||
$physical = $request->physical_quantity[$index] ?? 0;
|
||||
|
||||
OpnameDetail::create([
|
||||
'opname_id' => $opname->id,
|
||||
'product_id' => $productId,
|
||||
'system_stock' => $system,
|
||||
'physical_stock' => $physical,
|
||||
'difference' => $physical - $system,
|
||||
'note' => null, // or include from input
|
||||
]);
|
||||
}
|
||||
|
||||
return redirect()->route('opnames.index')
|
||||
->with('success', 'Opname berhasil disimpan.');
|
||||
}catch(\Exception $ex){
|
||||
Log::error($ex->getMessage());
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
32
public/js/warehouse_management/opnames/create.js
Normal file
32
public/js/warehouse_management/opnames/create.js
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -5,5 +5,6 @@
|
||||
"/js/warehouse_management/products/create.js": "/js/warehouse_management/products/create.js",
|
||||
"/js/warehouse_management/products/edit.js": "/js/warehouse_management/products/edit.js",
|
||||
"/js/warehouse_management/opnames/index.js": "/js/warehouse_management/opnames/index.js",
|
||||
"/js/warehouse_management/opnames/create.js": "/js/warehouse_management/opnames/create.js",
|
||||
"/css/app.css": "/css/app.css"
|
||||
}
|
||||
|
||||
60
resources/js/warehouse_management/opnames/create.js
Normal file
60
resources/js/warehouse_management/opnames/create.js
Normal file
@@ -0,0 +1,60 @@
|
||||
const productUrl = $("#product-container").data("url");
|
||||
|
||||
function createProductSelectOptions(callback) {
|
||||
$.ajax({
|
||||
url: productUrl,
|
||||
method: "GET",
|
||||
success: function (data) {
|
||||
let options = '<option value="">Pilih Produk</option>';
|
||||
data.forEach((product) => {
|
||||
options += `<option value="${product.id}">${product.name}</option>`;
|
||||
});
|
||||
callback(options);
|
||||
},
|
||||
error: function () {
|
||||
alert("Gagal memuat produk.");
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
$(document).ready(function () {
|
||||
// Initial load only for the first row
|
||||
createProductSelectOptions((options) => {
|
||||
$(".product-select").first().html(options);
|
||||
});
|
||||
|
||||
// When adding a new row
|
||||
$(document).on("click", ".btn-add-row", function () {
|
||||
const row = `
|
||||
<div class="form-row align-items-end product-row">
|
||||
<div class="form-group col-md-4">
|
||||
<select name="product[]" class="form-control product-select">
|
||||
<option>Loading...</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group col-md-3">
|
||||
<input type="text" name="system_quantity[]" class="form-control" placeholder="Stok sistem">
|
||||
</div>
|
||||
<div class="form-group col-md-3">
|
||||
<input type="text" name="physical_quantity[]" class="form-control" placeholder="Stok fisik">
|
||||
</div>
|
||||
<div class="form-group col-md-2">
|
||||
<button type="button" class="btn btn-danger btn-remove-row"><i class="flaticon2-delete"></i></button>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
const $newRow = $(row);
|
||||
$("#product-container").append($newRow);
|
||||
|
||||
// Load options only for the new select
|
||||
createProductSelectOptions((options) => {
|
||||
$newRow.find(".product-select").html(options);
|
||||
});
|
||||
});
|
||||
|
||||
// Remove row
|
||||
$(document).on("click", ".btn-remove-row", function () {
|
||||
$(this).closest(".product-row").remove();
|
||||
});
|
||||
});
|
||||
@@ -16,3 +16,34 @@ let table = $("#opnames-table").DataTable({
|
||||
{ data: "action", name: "action", orderable: false, searchable: false },
|
||||
],
|
||||
});
|
||||
$(document).on("click", ".btn-product-stock-dealers", function () {
|
||||
const productId = $(this).data("id");
|
||||
const productName = $(this).data("name");
|
||||
const ajaxUrl = $(this).data("url");
|
||||
|
||||
// Set product name in modal title
|
||||
$("#product-name-title").text(productName);
|
||||
|
||||
// Initialize or reload DataTable inside modal
|
||||
$("#dealer-stock-table").DataTable({
|
||||
destroy: true, // reinit if exists
|
||||
processing: true,
|
||||
serverSide: true,
|
||||
ajax: {
|
||||
url: ajaxUrl,
|
||||
data: {
|
||||
product_id: productId,
|
||||
},
|
||||
},
|
||||
columns: [
|
||||
{ data: "dealer_name", name: "dealer_name" },
|
||||
{ data: "system_stock", name: "system_stock" },
|
||||
{ data: "physical_stock", name: "physical_stock" },
|
||||
{ data: "difference", name: "difference" },
|
||||
{ data: "opname_date", name: "opname_date" },
|
||||
],
|
||||
});
|
||||
|
||||
// Show the modal
|
||||
$("#dealerStockModal").modal("show");
|
||||
});
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
<div class="kt-header__topbar-wrapper" data-toggle="dropdown" data-offset="0px,0px">
|
||||
<div class="kt-header__topbar-user">
|
||||
<span class="kt-header__topbar-welcome kt-hidden-mobile">Hi,</span>
|
||||
<span class="kt-header__topbar-username kt-hidden-mobile">John Doe</span>
|
||||
<span class="kt-header__topbar-username kt-hidden-mobile">{{ Auth::user()->name ?? '' }}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
<!--begin: Head -->
|
||||
<div class="kt-user-card kt-user-card--skin-dark kt-notification-item-padding-x" style="background-image: url('{{ url('assets/media/misc/bg-1.jpg') }}')">
|
||||
<div class="kt-user-card__name">
|
||||
John Doe
|
||||
{{ Auth::user()->name ?? '' }}
|
||||
</div>
|
||||
</div>
|
||||
<!--end: Head -->
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
@extends('layouts.backapp')
|
||||
|
||||
@section('content')
|
||||
<div class="kt-portlet kt-portlet--mobile" id="kt_blockui_datatable">
|
||||
<div class="kt-portlet__head kt-portlet__head--lg">
|
||||
<div class="kt-portlet__head-label">
|
||||
<span class="kt-portlet__head-icon">
|
||||
<i class="kt-font-brand flaticon2-plus"></i>
|
||||
</span>
|
||||
<h3 class="kt-portlet__head-title">Tambah Opnames</h3>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="kt-portlet__body">
|
||||
<form id="opname-form" action="{{ route('opnames.store') }}" method="POST">
|
||||
@csrf
|
||||
<div class="form-group">
|
||||
<label for="dealer">Dealer</label>
|
||||
<select name="dealer" id="dealer" class="form-control">
|
||||
<option value="">Pilih Dealer</option>
|
||||
@foreach($dealers as $dealer)
|
||||
<option value="{{ $dealer->id }}">{{ $dealer->name }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div id="product-container" data-url="{{ route('products.all') }}">
|
||||
<div class="form-row align-items-end product-row">
|
||||
<div class="form-group col-md-4">
|
||||
<label for="product[]">Produk</label>
|
||||
<select name="product[]" class="form-control product-select" >
|
||||
<option value="">Pilih Produk</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group col-md-3">
|
||||
<label>Stok Sistem</label>
|
||||
<input type="text" name="system_quantity[]" class="form-control" placeholder="Stok sistem">
|
||||
</div>
|
||||
<div class="form-group col-md-3">
|
||||
<label>Stok Fisik</label>
|
||||
<input type="text" name="physical_quantity[]" class="form-control" placeholder="Stok fisik">
|
||||
</div>
|
||||
<div class="form-group col-md-2">
|
||||
<button type="button" class="btn btn-success btn-add-row"><i class="flaticon2-plus"></i></button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group mt-4">
|
||||
<button type="submit" class="btn btn-primary">Simpan</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@section('javascripts')
|
||||
<script src="{{ mix('js/warehouse_management/opnames/create.js') }}"></script>
|
||||
@endsection
|
||||
|
||||
@@ -39,6 +39,34 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="modal fade" id="dealerStockModal" tabindex="-1" role="dialog" aria-labelledby="dealerStockModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog modal-lg" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">Dealer Stock for <span id="product-name-title"></span></h5>
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
||||
<span>×</span>
|
||||
</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<table id="dealer-stock-table" class="table table-bordered">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Dealer</th>
|
||||
<th>System Stock</th>
|
||||
<th>Physical Stock</th>
|
||||
<th>Difference</th>
|
||||
<th>Opname Date</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody></tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@endsection
|
||||
|
||||
@section('javascripts')
|
||||
|
||||
@@ -211,6 +211,8 @@ Route::group(['middleware' => 'auth'], function() {
|
||||
Route::get('/', 'index')->name('products.index');
|
||||
Route::get('create', 'create')->name('products.create');
|
||||
Route::post('/', 'store')->name('products.store');
|
||||
Route::get('all','all_products')->name('products.all');
|
||||
Route::get('dealers-stock')->name('products.dealers_stock');
|
||||
Route::get('{product}', 'show')->name('products.show');
|
||||
Route::get('{product}/edit', 'edit')->name('products.edit');
|
||||
Route::put('{product}', 'update')->name('products.update');
|
||||
@@ -233,6 +235,7 @@ Route::group(['middleware' => 'auth'], function() {
|
||||
Route::prefix('opnames')->controller(OpnamesController::class)->group(function (){
|
||||
Route::get('/','index')->name('opnames.index');
|
||||
Route::get('create','create')->name('opnames.create');
|
||||
Route::post('/','store')->name('opnames.store');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -33,6 +33,10 @@ mix.js("resources/js/app.js", "public/js")
|
||||
"resources/js/warehouse_management/opnames/index.js",
|
||||
"public/js/warehouse_management/opnames"
|
||||
)
|
||||
.js(
|
||||
"resources/js/warehouse_management/opnames/create.js",
|
||||
"public/js/warehouse_management/opnames"
|
||||
)
|
||||
.sourceMaps();
|
||||
|
||||
mix.browserSync({
|
||||
|
||||
Reference in New Issue
Block a user