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.
 
 
 
 
 
 

228 lines
11 KiB

<?php
namespace App\Http\Controllers;
use App\Models\Transfer;
use App\Models\m_warehouse;
use App\Models\asset_status;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
class HistoryController extends Controller
{
// Halaman index history peminjaman
public function historyPeminjaman(Request $request)
{
$perPage = $request->input('perPage', 5);
$query = asset_status::with(['customer', 'warehouseId', 'warehouse', 'warehouseEnter', 'kondisi_peti'])
->orderBy('created_at', 'desc');
//logika pencarian
$search = $request->input('search') ?? '';
if ($search) {
$searchArray = explode(' - ', $search);
if (count($searchArray) === 2) {
// Case: DPM - B100X63
list($codeCustomer, $typePeti) = $searchArray;
$query->whereHas('peti.customer', function ($customerQuery) use ($codeCustomer) {
$customerQuery->where('code_customer', 'like', "%$codeCustomer%");
})->whereHas('peti.tipe_peti', function ($tipePetiQuery) use ($typePeti) {
$tipePetiQuery->where('type', 'like', "%$typePeti%");
});
} else {
// Case: Other scenarios
$query->where(function ($q) use ($search) {
$q->where('mobile_id', 'like', "%$search%")
->orWhereHas('peti', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('fix_lot', 'like', "%$search%");
})
->orWhereHas('customer', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('name', 'like', "%$search%");
})
->orWhereHas('peti.customer', function ($customerQuery) use ($search) {
$customerQuery->where('code_customer', 'like', "%$search%");
})
->orWhereHas('peti.tipe_peti', function ($tipePetiQuery) use ($search) {
$tipePetiQuery->where('type', 'like', "%$search%");
})
->orWhere(function ($dateQuery) use ($search) {
try {
// Format tanggal yang diharapkan dari input pengguna
$formattedDate = \Carbon\Carbon::createFromFormat('d-m-Y', $search)->format('Y-m-d');
// Cek kesamaan tanggal
$dateQuery->whereDate('exit_at', $formattedDate);
} catch (\Exception $e) {
Log::error('Error parsing date: ' . $e->getMessage());
}
})
->orWhere(function ($dateQuery) use ($search) {
try {
// Format tanggal yang diharapkan dari input pengguna
$formattedDate = \Carbon\Carbon::createFromFormat('d-m-Y', $search)->format('Y-m-d');
// Cek kesamaan tanggal
$dateQuery->whereDate('est_pengembalian', $formattedDate);
} catch (\Exception $e) {
Log::error('Error parsing date: ' . $e->getMessage());
}
})
->orWhere('exit_pic', 'like', "%$search%")
->orWhereHas('warehouse', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('name', 'like', "%$search%")
->orWhere('address', 'like', "%$search%");
});
});
}
}
if ($perPage == 'Semua') {
$chunkSize = 100;
$stores = new Collection();
$currentPage = 1;
$query->chunk($chunkSize, function ($storesChunk) use ($stores, &$currentPage) {
foreach ($storesChunk as $store) {
$store->setAttribute('i', ($currentPage - 1) * $storesChunk->perPage() + 1);
$stores->push($store);
$currentPage++;
}
});
} else {
$stores = $query->paginate($perPage);
}
$data = [
'peminjaman' => $stores,
'warehouse' => m_warehouse::get(),
'i' => ($stores->currentPage() - 1) * $stores->perPage() + 1,
'search' => $search,
'active' => 'history-peminjaman',
];
return view('dashboard.History.Peminjaman.index', $data);
}
// Halaman index history pengembalian
public function historyPengembalian(Request $request)
{
$perPage = $request->input('perPage', 5);
$query = asset_status::with(['customer', 'warehouseId', 'warehouse', 'warehouseEnter', 'kondisi_peti'])
->where('status', '=', 1)
->orderBy('created_at', 'desc');
// Logika pencarian
$search = $request->input('search') ?? '';
if ($search) {
$query->where(function ($q) use ($search) {
$q->where('mobile_id', 'like', "%$search%")
->orWhereHas('peti', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('fix_lot', 'like', "%$search%");
}) // sudah cari fix lot
->orWhereHas('customer', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('name', 'like', "%$search%");
}) // sudah cari nama customer
->orWhere(function ($dateQuery) use ($search) {
try {
// Format tanggal yang diharapkan dari input pengguna
$formattedDate = \Carbon\Carbon::createFromFormat('d-m-Y', $search)->format('Y-m-d');
// Cek kesamaan tanggal
$dateQuery->whereDate('exit_at', $formattedDate);
} catch (\Exception $e) {
Log::error('Error parsing date: ' . $e->getMessage());
}
})
->orWhere(function ($dateQuery) use ($search) {
try {
// Format tanggal yang diharapkan dari input pengguna
$formattedDate = \Carbon\Carbon::createFromFormat('d-m-Y', $search)->format('Y-m-d');
// Cek kesamaan tanggal
$dateQuery->whereDate('est_pengembalian', $formattedDate);
} catch (\Exception $e) {
Log::error('Error parsing date: ' . $e->getMessage());
}
}) // sudah cari est pengembalian (tanggal pengembalian)
->orWhere('exit_pic', 'like', "%$search%") // sudah cari exit pic (nama peminjam)
->orWhereHas('warehouse', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('name', 'like', "%$search%")
->orWhere('address', 'like', "%$search%");
}) // sudah cari warehouse (lokasi peminjaman)
->orWhere(function ($dateQuery) use ($search) {
try {
// Format tanggal yang diharapkan dari input pengguna
$formattedDate = \Carbon\Carbon::createFromFormat('d-m-Y', $search)->format('Y-m-d');
// Cek kesamaan tanggal
$dateQuery->whereDate('enter_at', $formattedDate);
} catch (\Exception $e) {
Log::error('Error parsing date: ' . $e->getMessage());
}
}) // sudah cari enter at (tanggal pengembalian)
->orWhere('enter_pic', 'like', "%$search%") // sudah cari enter pic (nama pengembali)
->orWhereHas('warehouseEnter', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('name', 'like', "%$search%");
}) // sudah cari warehouse enter (lokasi pengembalian)
->orWhereHas('kondisi_peti', function ($warehouseQuery) use ($search) {
$warehouseQuery->where('nama_kondisi', 'like', "%$search%");
}) // sudah cari kondisi peti
->orWhere(function ($statusQuery) use ($search) {
if (strtolower($search) === 'return') {
$statusQuery->where('status', 1);
} else {
$statusQuery->where('status', 'like', "%$search%");
}
}); // sudah cari status
});
}
// Periksa apakah perPage adalah 'Semua'
if ($perPage == 'Semua') {
$chunkSize = 100;
$stores = new Collection();
$currentPage = 1;
$query->chunk($chunkSize, function ($storesChunk) use ($stores, &$currentPage) {
foreach ($storesChunk as $store) {
$store->setAttribute('i', ($currentPage - 1) * $storesChunk->perPage() + $store->getQueueableId());
$stores->push($store);
$currentPage++;
}
});
} else {
// Hitung halaman berdasarkan parameter halaman yang diberikan atau default ke halaman pertama
$page = $request->input('page') ?? 1;
// Paginasi dengan parameter halaman
$stores = $query->paginate($perPage, ['*'], 'page', $page);
// Hitung nomor urutan untuk setiap item di halaman
$stores->each(function ($store) use ($page, $perPage) {
$store->setAttribute('i', ($page - 1) * $perPage + $store->getQueueableId());
});
}
$data = [
'peminjaman' => $stores,
'warehouse' => m_warehouse::get(),
'i' => ($stores->currentPage() - 1) * $stores->perPage() + 1,
'search' => $search,
'active' => 'history-pengembalian',
];
return view('dashboard.History.Pengembalian.index', $data);
}
// Halaman index history transfer
public function historyTransfer()
{
$data = [
'transfer' => Transfer::withTrashed()->orderBy('created_at', 'desc')->get(),
'active' => 'history-transfer',
];
return view('dashboard.History.Transfer.index', $data);
}
}