165 lines
6.7 KiB
PHP
165 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace Modules\Maintenance\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\Maintenance\Http\Controllers\Concerns\AuthorizesMaintenance;
|
|
use Modules\Maintenance\Models\SparePart;
|
|
use Modules\Maintenance\Services\SparePartInventoryService;
|
|
use Modules\Maintenance\Utils\MaintenanceUtil;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class SparePartController extends Controller
|
|
{
|
|
use AuthorizesMaintenance;
|
|
|
|
public function __construct(
|
|
protected MaintenanceUtil $maintenanceUtil,
|
|
protected SparePartInventoryService $inventoryService
|
|
) {}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.spare_parts.view');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
|
|
if ($request->ajax()) {
|
|
$query = SparePart::where('business_id', $business_id)
|
|
->with(['supplier:id,name'])
|
|
->orderBy('name');
|
|
|
|
if ($request->boolean('low_stock')) {
|
|
$query->whereColumn('quantity', '<=', 'min_stock');
|
|
}
|
|
|
|
return DataTables::of($query)
|
|
->addColumn('supplier_name', fn ($row) => $row->supplier?->name ?? '—')
|
|
->addColumn('stock_status', function ($row) {
|
|
if ($row->quantity <= $row->min_stock) {
|
|
return '<span class="label bg-red">کمبود موجودی</span>';
|
|
}
|
|
|
|
return '<span class="label bg-green">کافی</span>';
|
|
})
|
|
->addColumn('action', function ($row) {
|
|
$html = '<div class="btn-group">';
|
|
if (auth()->user()->can('maintenance.spare_parts.update')) {
|
|
$html .= '<button type="button" data-href="'.action([self::class, 'edit'], [$row->id]).'" class="btn btn-xs btn-primary btn-modal" data-container=".maintenance_spare_part_modal"><i class="glyphicon glyphicon-edit"></i></button>';
|
|
}
|
|
if (auth()->user()->can('maintenance.spare_parts.delete')) {
|
|
$html .= ' <button type="button" data-href="'.action([self::class, 'destroy'], [$row->id]).'" class="btn btn-xs btn-danger delete_maintenance_spare_part"><i class="glyphicon glyphicon-trash"></i></button>';
|
|
}
|
|
$html .= '</div>';
|
|
|
|
return $html;
|
|
})
|
|
->editColumn('purchase_price', fn ($row) => $this->maintenanceUtil->formatMoney($row->purchase_price))
|
|
->rawColumns(['stock_status', 'action'])
|
|
->make(true);
|
|
}
|
|
|
|
return view('maintenance::spare_parts.index');
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.spare_parts.create');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
|
|
return view('maintenance::spare_parts.create', [
|
|
'customers' => $this->maintenanceUtil->getCustomersDropdown($business_id),
|
|
'products' => $this->maintenanceUtil->getProductsDropdown($business_id),
|
|
'locations' => $this->maintenanceUtil->getLocationsDropdown($business_id),
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.spare_parts.create');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
|
|
$data = $request->validate([
|
|
'part_number' => 'required|string|max:100',
|
|
'name' => 'required|string|max:255',
|
|
'warehouse_id' => 'nullable|integer',
|
|
'supplier_id' => 'nullable|exists:contacts,id',
|
|
'product_id' => 'nullable|exists:products,id',
|
|
'variation_id' => 'nullable|exists:variations,id',
|
|
'location_id' => 'nullable|integer',
|
|
'quantity' => 'sometimes|numeric|min:0',
|
|
'min_stock' => 'sometimes|numeric|min:0',
|
|
'max_stock' => 'nullable|numeric|min:0',
|
|
'purchase_price' => 'sometimes|integer|min:0',
|
|
'unit' => 'nullable|string|max:50',
|
|
'notes' => 'nullable|string',
|
|
]);
|
|
|
|
$part = SparePart::create(array_merge($data, [
|
|
'business_id' => $business_id,
|
|
]));
|
|
|
|
$this->inventoryService->syncSparePartQuantityFromPos($part, $business_id);
|
|
|
|
return $this->maintenanceUtil->jsonOrRedirect($request, true, 'قطعه یدکی ایجاد شد.');
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.spare_parts.update');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
$spare_part = SparePart::where('business_id', $business_id)->findOrFail($id);
|
|
|
|
return view('maintenance::spare_parts.edit', [
|
|
'spare_part' => $spare_part,
|
|
'customers' => $this->maintenanceUtil->getCustomersDropdown($business_id),
|
|
'products' => $this->maintenanceUtil->getProductsDropdown($business_id),
|
|
'locations' => $this->maintenanceUtil->getLocationsDropdown($business_id),
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.spare_parts.update');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
$spare_part = SparePart::where('business_id', $business_id)->findOrFail($id);
|
|
|
|
$data = $request->validate([
|
|
'part_number' => 'required|string|max:100',
|
|
'name' => 'required|string|max:255',
|
|
'warehouse_id' => 'nullable|integer',
|
|
'supplier_id' => 'nullable|exists:contacts,id',
|
|
'product_id' => 'nullable|exists:products,id',
|
|
'variation_id' => 'nullable|exists:variations,id',
|
|
'location_id' => 'nullable|integer',
|
|
'quantity' => 'sometimes|numeric|min:0',
|
|
'min_stock' => 'sometimes|numeric|min:0',
|
|
'max_stock' => 'nullable|numeric|min:0',
|
|
'purchase_price' => 'sometimes|integer|min:0',
|
|
'unit' => 'nullable|string|max:50',
|
|
'notes' => 'nullable|string',
|
|
]);
|
|
|
|
$spare_part->update($data);
|
|
$this->inventoryService->syncSparePartQuantityFromPos($spare_part->fresh(), $business_id);
|
|
|
|
return $this->maintenanceUtil->jsonOrRedirect($request, true, 'قطعه یدکی بهروزرسانی شد.');
|
|
}
|
|
|
|
public function destroy(Request $request, $id)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.spare_parts.delete');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
$spare_part = SparePart::where('business_id', $business_id)->findOrFail($id);
|
|
$spare_part->delete();
|
|
|
|
return $this->maintenanceUtil->jsonOrRedirect($request, true, 'قطعه یدکی حذف شد.');
|
|
}
|
|
}
|