|
Server : LiteSpeed System : Linux barito.iixcp.rumahweb.net 5.14.0-611.49.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Apr 21 16:39:08 EDT 2026 x86_64 User : elvh3918 ( 1528) PHP Version : 8.2.31 Disable Function : mail Directory : /home/elvh3918/public_html/pmm/app/Http/Controllers/Master/ |
<?php
namespace App\Http\Controllers\Master;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Request;
use Illuminate\Http\Request as HttpRequest;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
use App\Models\Master\Tenant;
use App\Models\Master\Item;
use Illuminate\Support\Facades\Auth;
class ItemController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$breadcrumbs = [
['link' => "dashboard", 'name' => "Dashboard"], ['link' => "master.items.index", 'name' => "Items"]
];
$data_input = [
'tenant_id' => Request::input('tenant_id'),
'customer_id' => Request::input('customer_id'),
];
$query = Item::with('customer', 'customer.tenant');
$tenant = Auth::user()->load('model');
if ($tenant->model_type == 'App\Models\Master\Tenant') {
$tenant = Tenant::where('id', '=', Auth::user()->model_id)->orderBy('name', 'asc')->get();
if ($data_input['customer_id'] == 0) {
$query = $query->whereRelation('customer.tenant', 'id', '=', Auth::user()->model_id)->orderBy('customer_id', 'asc')->orderBy('name', 'asc')->get();
} else {
$query = $query->whereRelation('customer.tenant', 'id', '=', Auth::user()->model_id)->where('customer_id', '=', $data_input['customer_id'])->orderBy('customer_id', 'asc')->orderBy('name', 'asc')->get();
}
} else if ($tenant->model_type == 'App\Models\Bussiness') {
$tenant = Tenant::orderBy('name', 'asc')->get();
if ($data_input['tenant_id'] != 0) {
$query = $query->whereRelation('customer.tenant', 'id', '=', $data_input['tenant_id']);
}
if ($data_input['customer_id'] == 0) {
$query = $query->orderBy('customer_id', 'asc')->orderBy('name', 'asc')->get();
} else {
$query = $query->where('customer_id', '=', $data_input['customer_id'])->orderBy('customer_id', 'asc')->orderBy('name', 'asc')->get();
}
}
return view('content.master.items.index', compact('query', 'tenant', 'data_input'), ['breadcrumbs' => $breadcrumbs]);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$breadcrumbs = [
['link' => "dashboard", 'name' => "Dashboard"], ['link' => "master.items.index", 'name' => "Items"], ['link' => "master.items.create", 'name' => "Create Item"]
];
$tenant = Auth::user()->load('model');
if ($tenant->model_type == 'App\Models\Master\Tenant') {
$tenant = Tenant::where('id', '=', Auth::user()->model_id)->orderBy('name', 'asc')->get();
} else if ($tenant->model_type == 'App\Models\Bussiness') {
$tenant = Tenant::orderBy('name', 'asc')->get();
}
return view('content.master.items.create', compact('tenant'), ['breadcrumbs' => $breadcrumbs]);
}
/**
* Store a newly created resource in storage.
*/
public function store(HttpRequest $request)
{
$user = Auth::user()->load('model');
if ($user->model_type == 'App\Models\Master\Tenant' && $user->model->id != Request::get('tenant_id')) {
return redirect()->route('master.items.create')->withInput()->with('message', 'system blocked, wrong id');
}
$validator = Validator::make(Request::all(), [
// 'code' => ['required', 'string', 'max:100',
// Rule::unique('items')->where(function ($query) {
// return $query->where('code', '=', Request::get('code'))->where('customer_id', '=', Request::get('customer_id'));
// }),
// ],
'name' => ['required', 'string', 'max:100'],
]);
if ($validator->fails()) {
return redirect()->route('master.items.create')->withErrors($validator)->withInput()->with('message', 'input failed');
}
$image = null;
DB::beginTransaction();
try {
// check uploaded image
if (isset($request->image)) {
$files = $request->image;
$filename = $files->store(config('app.dir_file_item'), 'public');
$image = pathinfo(storage_path($filename), PATHINFO_BASENAME);
} else {
$image = null;
}
Item::create([
'code' => Request::get('code'),
'name' => Request::get('name'),
'image' => $image,
'description' => Request::get('description'),
'customer_id' => Request::get('customer_id'),
'is_active' => Request::get('is_active') != null ? 1: 0,
]);
DB::commit();
return redirect()->route('master.items.index')->with('message', 'store success');
} catch (\Exception $e) {
DB::rollBack();
// remove image failed insert data
if (Storage::disk('public')->exists(config('app.dir_file_item') . '/'. $image)) {
Storage::disk('public')->delete(config('app.dir_file_item') . '/' . $image);
}
return redirect()->route('master.items.create')->withErrors($validator)->withInput()->with('message', 'store error');
}
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Item $item)
{
$tenant = $item->load('customer', 'customer.tenant');
$user = Auth::user()->load('model');
if ($user->model_type == 'App\Models\Master\Tenant' && $user->model->id != $tenant->customer->tenant->id) {
return redirect()->route('master.items.index')->with('message', 'system blocked, wrong id');
}
$breadcrumbs = [
['link' => "dashboard", 'name' => "Dashboard"], ['link' => "master.items.index", 'name' => "Items"], ['link' => "master/items/edit/$item->id", 'name' => "Edit Item"]
];
$query = [
'id' => $item->id,
'code' => $item->code,
'name' => $item->name,
'image' => $item->image,
'description' => $item->description,
'customer_id' => $item->load('customer', 'customer.tenant'),
'is_active' => $item->is_active,
'updated_at' => $item->updated_at,
];
$tenant = Auth::user()->load('model');
if ($tenant->model_type == 'App\Models\Master\Tenant') {
$tenant = Tenant::where('id', '=', Auth::user()->model_id)->orderBy('name', 'asc')->get();
} else if ($tenant->model_type == 'App\Models\Bussiness') {
$tenant = Tenant::orderBy('name', 'asc')->get();
}
return view('content.master.items.edit', compact('query', 'tenant'), ['breadcrumbs' => $breadcrumbs]);
}
/**
* Update the specified resource in storage.
*/
public function update(HttpRequest $request, Item $item)
{
$user = Auth::user()->load('model');
if ($user->model_type == 'App\Models\Master\Tenant' && $user->model->id != Request::get('tenant_id')) {
return redirect()->route('master.items.edit', $item->id)->with('message', 'system blocked, wrong id');
}
$validator = Validator::make(Request::all(), [
// 'code' => ['required', 'string', 'max:100',
// Rule::unique('items')->where(function ($query) use ($item) {
// return $query->where('code', '=', Request::get('code'))->where('customer_id', '=', Request::get('customer_id'))->ignore($item->id);
// }),
// ],
'name' => ['required', 'string', 'max:100'],
]);
if ($validator->fails()) {
return redirect()->route('master.items.edit', $item->id)->withErrors($validator)->withInput()->with('message', 'input failed');
}
$image = null;
DB::beginTransaction();
try {
// check uploaded image
if (isset($request->image)) {
$files = $request->image;
$filename = $files->store(config('app.dir_file_item'), 'public');
$image = pathinfo(storage_path($filename), PATHINFO_BASENAME);
// remove image for replace new image
$iimage = $item->image;
$exists = Storage::disk('public')->exists(config('app.dir_file_item') . '/'. $iimage);
if ($exists) {
Storage::disk('public')->delete(config('app.dir_file_item') . '/' . $iimage);
}
} else {
$image = null;
}
$item->update([
'code' => Request::get('code'),
'name' => Request::get('name'),
'image' => $image,
'description' => Request::get('description'),
'customer_id' => Request::get('customer_id'),
'is_active' => Request::get('is_active') != null ? 1: 0,
]);
DB::commit();
return redirect()->route('master.items.index')->with('message', 'update success');
} catch (\Exception $e) {
DB::rollBack();
// remove image failed insert data
if (Storage::disk('public')->exists(config('app.dir_file_item') . '/'. $image)) {
Storage::disk('public')->delete(config('app.dir_file_item') . '/' . $image);
}
return redirect()->route('master.items.edit', $item->id)->withErrors($validator)->withInput()->with('message', 'update error');
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Item $item)
{
$tenant = $item->load('customer', 'customer.tenant');
$user = Auth::user()->load('model');
if ($user->model_type == 'App\Models\Master\Tenant' && $user->model->id != $tenant->customer->tenant->id) {
return redirect()->route('master.items.index')->with('message', 'system blocked, wrong id');
}
// remove image for replace new image
$exists = Storage::disk('public')->exists(config('app.dir_file_item') . '/'. $item->image);
if ($exists) {
Storage::disk('public')->delete(config('app.dir_file_item') . '/' . $item->image);
}
$item->delete();
return redirect()->route('master.items.index')->with('message', 'delete success');
}
}