Siopas Inventory PETI for ISTW Website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

186 lines
6.3 KiB

<?php
namespace App\Http\Controllers;
use App\Models\Customer;
use Illuminate\Http\Request;
use App\Imports\CustomerImport;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Requests\ValidasiCreateCustomer;
use App\Http\Requests\ValidasiUpdateCustomer;
class CustomerController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$perPage = $request->input('perPage', 5);
$query = Customer::orderBy('created_at', 'desc');
// Add search logic similar to the Transfer index controller
$search = $request->input('search') ?? '';
if ($search) {
$query->where(function ($q) use ($search) {
$q->where('name', 'like', "%$search%")
->orWhere('code_customer', 'like', "%$search%")
->orWhere('lot_no', 'like', "%$search%")
->orWhere('no_tlp', 'like', "%$search%")
->orWhere('address', 'like', "%$search%");
});
}
if ($perPage == 'Semua') {
$chunkSize = 100;
$customers = new Collection();
$currentPage = 1;
$query->chunk($chunkSize, function ($customerChunk) use ($customers, &$currentPage) {
foreach ($customerChunk as $customer) {
$customer->setAttribute('i', ($currentPage - 1) * $customerChunk->perPage() + 1);
$customers->push($customer);
$currentPage++;
}
});
} else {
$customers = $query->paginate($perPage);
}
$data = [
'customers' => $customers,
'i' => ($customers->currentPage() - 1) * $customers->perPage() + 1,
'search' => $search,
'active' => 'menu-customer',
];
return view('dashboard.Master_Data.Customer.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$data = [
'active' => 'menu-customer',
];
return view('dashboard.Master_Data.Customer.create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(ValidasiCreateCustomer $request)
{
// dd($request);
try {
$currentUser = Auth::user();
$validatedData = $request->except('_token');
$validatedData['name'] = strtoupper($validatedData['name']);
$validatedData['code_customer'] = strtoupper($validatedData['code_customer']);
$validatedData['lot_no'] = strtoupper($validatedData['lot_no']);
$validatedData['address'] = strtoupper($validatedData['address']);
$validatedData['created_by'] = $currentUser->fullname; // Menggunakan nama pengguna sebagai created_by
$validatedData['updated_by'] = $currentUser->fullname; // Menggunakan nama pengguna sebagai updated_by
Customer::create($validatedData);
return redirect()->route('dashboard.customer.index')->with('success', 'Data customer berhasil ditambahkan');
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data customer Gagal Ditambah.');
}
}
/**
* Display the specified resource.
*/
public function show($id)
{
$data = [
'customer' => Customer::findOrFail($id),
'active' => 'menu-customer',
];
return view('dashboard.Master_Data.Customer.show', $data);
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$data = [
'customer' => Customer::findOrFail($id),
'active' => 'menu-customer',
];
return view('dashboard.Master_Data.Customer.edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(ValidasiUpdateCustomer $request, $id)
{
try {
$customer = Customer::findOrFail($id);
$customerData = $request->all();
// Menambahkan nama pengguna yang melakukan pembaruan
$customerData['name'] = strtoupper($customerData['name']);
$customerData['code_customer'] = strtoupper($customerData['code_customer']);
$customerData['lot_no'] = strtoupper($customerData['lot_no']);
$customerData['address'] = strtoupper($customerData['address']);
$customerData['updated_by'] = Auth::user()->fullname;
$customer->update($customerData);
return redirect()->route('dashboard.customer.index')->with('success', 'Data customer berhasil diperbaharui');
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data customer gagal diperbaharui');
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
// dd("oke");
try {
$customer = Customer::findOrFail($id);
$customer->delete();
return redirect()->back()->with('success', 'Data customer berhasil dihapus');
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data customer gagal dihapus');
}
}
public function importCustomer(Request $request)
{
$import = new CustomerImport;
try {
Excel::import($import, request()->file('file'));
if ($import->getRowCount() > 0) {
return redirect()->route('dashboard.customer.index')->with('success', 'Data Customer berhasil diimport');
} else {
$errors = $import->getErrors();
if (!empty($errors)) {
session()->flash('error', $errors);
}
return redirect()->back()->with('import', $import)->with('error', 'Data Customer gagal di import.');
}
} catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
$failures = $e->failures();
foreach ($failures as $failure) {
session()->flash('error', ['Baris ' . $failure->row() => implode(', ', $failure->errors())]);
}
}
}
}