Update Fix
This commit is contained in:
27
app/Console/Kernel.php
Normal file
27
app/Console/Kernel.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||
|
||||
class Kernel extends ConsoleKernel
|
||||
{
|
||||
/**
|
||||
* Define the application's command schedule.
|
||||
*/
|
||||
protected function schedule(Schedule $schedule): void
|
||||
{
|
||||
// $schedule->command('inspire')->hourly();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the commands for the application.
|
||||
*/
|
||||
protected function commands(): void
|
||||
{
|
||||
$this->load(__DIR__.'/Commands');
|
||||
|
||||
require base_path('routes/console.php');
|
||||
}
|
||||
}
|
||||
30
app/Exceptions/Handler.php
Normal file
30
app/Exceptions/Handler.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||
use Throwable;
|
||||
|
||||
class Handler extends ExceptionHandler
|
||||
{
|
||||
/**
|
||||
* The list of the inputs that are never flashed to the session on validation exceptions.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $dontFlash = [
|
||||
'current_password',
|
||||
'password',
|
||||
'password_confirmation',
|
||||
];
|
||||
|
||||
/**
|
||||
* Register the exception handling callbacks for the application.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
$this->reportable(function (Throwable $e) {
|
||||
//
|
||||
});
|
||||
}
|
||||
}
|
||||
181
app/Http/Controllers/Admin/DashboardController.php
Normal file
181
app/Http/Controllers/Admin/DashboardController.php
Normal file
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class DashboardController extends Controller
|
||||
{
|
||||
public function overview(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'date' => ['nullable', 'date'],
|
||||
'lokasi_kode' => ['nullable', 'string', 'max:20'],
|
||||
'low_stock_limit' => ['nullable', 'integer', 'min:1', 'max:200'],
|
||||
'low_stock_threshold' => ['nullable', 'integer', 'min:0', 'max:1000000'],
|
||||
]);
|
||||
|
||||
$date = $data['date'] ?? now()->toDateString();
|
||||
$start = $date . ' 00:00:00';
|
||||
$end = $date . ' 23:59:59';
|
||||
|
||||
$basePenjualan = DB::table('penjualan')->whereBetween('waktu', [$start, $end]);
|
||||
$pendapatan = (int) (clone $basePenjualan)->sum('total');
|
||||
$transaksi = (int) (clone $basePenjualan)->count();
|
||||
|
||||
$itemTerjual = (int) DB::table('penjualan_barang as pb')
|
||||
->join('penjualan as p', 'p.id', '=', 'pb.penjualan_id')
|
||||
->whereBetween('p.waktu', [$start, $end])
|
||||
->sum('pb.jumlah');
|
||||
|
||||
$margin = (int) DB::table('penjualan_barang as pb')
|
||||
->join('penjualan as p', 'p.id', '=', 'pb.penjualan_id')
|
||||
->leftJoin('produk as pr', 'pr.id', '=', 'pb.produk_id')
|
||||
->whereBetween('p.waktu', [$start, $end])
|
||||
->selectRaw('COALESCE(SUM((pb.harga - COALESCE(pr.harga_beli, 0)) * pb.jumlah),0) as margin')
|
||||
->value('margin');
|
||||
|
||||
$role = $request->user()?->role ?: 'admin';
|
||||
$marginValue = $role === 'admin' ? $margin : null;
|
||||
|
||||
$summary = [
|
||||
'pendapatan' => $pendapatan,
|
||||
'transaksi' => $transaksi,
|
||||
'item_terjual' => $itemTerjual,
|
||||
'margin' => $marginValue,
|
||||
];
|
||||
|
||||
$chartStart = Carbon::parse($date)->subDays(6)->toDateString();
|
||||
$chartRows = DB::table('penjualan')
|
||||
->selectRaw('DATE(waktu) as tanggal, SUM(total) as total')
|
||||
->whereBetween('waktu', [$chartStart . ' 00:00:00', $end])
|
||||
->groupBy('tanggal')
|
||||
->orderBy('tanggal')
|
||||
->get()
|
||||
->keyBy('tanggal');
|
||||
|
||||
$chart = [];
|
||||
for ($i = 6; $i >= 0; $i--) {
|
||||
$d = Carbon::parse($date)->subDays($i)->toDateString();
|
||||
$chart[] = [
|
||||
'tanggal' => $d,
|
||||
'total' => (int) ($chartRows->get($d)?->total ?? 0),
|
||||
];
|
||||
}
|
||||
|
||||
$topProducts = DB::table('penjualan_barang as pb')
|
||||
->join('penjualan as p', 'p.id', '=', 'pb.penjualan_id')
|
||||
->join('produk as pr', 'pr.id', '=', 'pb.produk_id')
|
||||
->whereBetween('p.waktu', [$start, $end])
|
||||
->selectRaw('pr.id, pr.nama, pr.barcode, SUM(pb.jumlah) as qty, SUM(pb.total) as total')
|
||||
->groupBy('pr.id', 'pr.nama', 'pr.barcode')
|
||||
->orderByDesc('qty')
|
||||
->limit(10)
|
||||
->get()
|
||||
->map(function ($r) {
|
||||
return [
|
||||
'id' => (int) $r->id,
|
||||
'nama' => $r->nama,
|
||||
'barcode' => $r->barcode,
|
||||
'qty' => (int) $r->qty,
|
||||
'total' => (int) $r->total,
|
||||
];
|
||||
});
|
||||
|
||||
$limit = (int) ($data['low_stock_limit'] ?? 10);
|
||||
$thresholdToko = (int) ($data['low_stock_threshold'] ?? $this->getSetting('stok.low_threshold_toko', 5));
|
||||
$thresholdGudang = (int) $this->getSetting('stok.low_threshold_gudang', 5);
|
||||
|
||||
$lowStockToko = $this->lowStockByKode('TOKO', $thresholdToko, $limit);
|
||||
$lowStockGudang = $this->lowStockByKode('GUDANG', $thresholdGudang, $limit);
|
||||
|
||||
$recent = DB::table('penjualan')
|
||||
->orderByDesc('waktu')
|
||||
->limit(10)
|
||||
->get()
|
||||
->map(function ($r) {
|
||||
return [
|
||||
'id' => (int) $r->id,
|
||||
'nomor_penjualan' => $r->nomor_penjualan,
|
||||
'waktu' => $r->waktu,
|
||||
'total' => (int) $r->total,
|
||||
'metode_pembayaran' => $r->metode_pembayaran,
|
||||
];
|
||||
});
|
||||
|
||||
$dataIssues = [
|
||||
'tanpa_barcode' => (int) DB::table('produk')
|
||||
->whereNull('barcode')
|
||||
->orWhere('barcode', '')
|
||||
->count(),
|
||||
'tanpa_harga_jual' => (int) DB::table('produk')
|
||||
->whereNull('harga_jual')
|
||||
->orWhere('harga_jual', 0)
|
||||
->count(),
|
||||
];
|
||||
|
||||
return response()->json([
|
||||
'date' => $date,
|
||||
'summary' => $summary,
|
||||
'chart_7d' => $chart,
|
||||
'top_products' => $topProducts,
|
||||
'low_stock_toko' => $lowStockToko,
|
||||
'low_stock_gudang' => $lowStockGudang,
|
||||
'recent' => $recent,
|
||||
'data_issues' => $dataIssues,
|
||||
]);
|
||||
}
|
||||
|
||||
private function getSetting(string $key, $default = null)
|
||||
{
|
||||
$value = DB::table('settings')->where('key', $key)->value('value');
|
||||
return $value === null ? $default : $value;
|
||||
}
|
||||
|
||||
private function lowStockByKode(string $kode, int $threshold, int $limit): array
|
||||
{
|
||||
$lokasi = DB::table('lokasi')->where('kode', $kode)->first();
|
||||
if (!$lokasi) {
|
||||
return [
|
||||
'lokasi' => null,
|
||||
'threshold' => $threshold,
|
||||
'data' => [],
|
||||
];
|
||||
}
|
||||
|
||||
$lokasiId = (int) $lokasi->id;
|
||||
$rows = DB::table('produk')
|
||||
->select('produk.id', 'produk.nama', 'produk.barcode', 'produk.sku')
|
||||
->selectRaw(
|
||||
'COALESCE(SUM(CASE WHEN pergerakan_stok.ke_lokasi_id = ? THEN pergerakan_stok.jumlah ELSE 0 END),0) - ' .
|
||||
'COALESCE(SUM(CASE WHEN pergerakan_stok.dari_lokasi_id = ? THEN pergerakan_stok.jumlah ELSE 0 END),0) as stok',
|
||||
[$lokasiId, $lokasiId]
|
||||
)
|
||||
->leftJoin('pergerakan_stok', 'pergerakan_stok.produk_id', '=', 'produk.id')
|
||||
->groupBy('produk.id', 'produk.nama', 'produk.barcode', 'produk.sku')
|
||||
->having('stok', '<=', $threshold)
|
||||
->orderBy('stok')
|
||||
->limit($limit)
|
||||
->get()
|
||||
->map(function ($r) {
|
||||
return [
|
||||
'id' => (int) $r->id,
|
||||
'nama' => $r->nama,
|
||||
'barcode' => $r->barcode,
|
||||
'sku' => $r->sku,
|
||||
'stok' => (int) $r->stok,
|
||||
];
|
||||
})
|
||||
->all();
|
||||
|
||||
return [
|
||||
'lokasi' => ['id' => (int) $lokasi->id, 'kode' => $lokasi->kode, 'nama' => $lokasi->nama],
|
||||
'threshold' => $threshold,
|
||||
'data' => $rows,
|
||||
];
|
||||
}
|
||||
}
|
||||
239
app/Http/Controllers/Admin/DistributorController.php
Normal file
239
app/Http/Controllers/Admin/DistributorController.php
Normal file
@@ -0,0 +1,239 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Distributor;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class DistributorController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$q = trim((string) $request->query('q', ''));
|
||||
|
||||
$query = Distributor::query()->orderByDesc('id');
|
||||
if ($q !== '') {
|
||||
$query->where(function ($qq) use ($q) {
|
||||
$qq->where('nama', 'like', "%{$q}%")
|
||||
->orWhere('alamat', 'like', "%{$q}%")
|
||||
->orWhere('telepon', 'like', "%{$q}%");
|
||||
});
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'data' => $query->get([
|
||||
'id',
|
||||
'nama',
|
||||
'alamat',
|
||||
'telepon',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'nama' => ['required', 'string', 'max:150'],
|
||||
'alamat' => ['nullable', 'string', 'max:255'],
|
||||
'telepon' => ['nullable', 'string', 'max:50'],
|
||||
]);
|
||||
|
||||
$row = Distributor::create([
|
||||
'nama' => $data['nama'],
|
||||
'alamat' => $data['alamat'] ?? null,
|
||||
'telepon' => $data['telepon'] ?? null,
|
||||
]);
|
||||
|
||||
return response()->json(['data' => $row], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, Distributor $distributor): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'nama' => ['required', 'string', 'max:150'],
|
||||
'alamat' => ['nullable', 'string', 'max:255'],
|
||||
'telepon' => ['nullable', 'string', 'max:50'],
|
||||
]);
|
||||
|
||||
$distributor->update([
|
||||
'nama' => $data['nama'],
|
||||
'alamat' => $data['alamat'] ?? null,
|
||||
'telepon' => $data['telepon'] ?? null,
|
||||
]);
|
||||
|
||||
return response()->json(['data' => $distributor->fresh()]);
|
||||
}
|
||||
|
||||
public function destroy(Distributor $distributor): JsonResponse
|
||||
{
|
||||
$distributor->delete();
|
||||
return response()->json(['message' => 'Distributor dihapus.']);
|
||||
}
|
||||
|
||||
public function import(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'file' => ['required', 'file', 'mimes:xlsx,xls,csv,txt', 'max:5120'],
|
||||
]);
|
||||
|
||||
$file = $data['file'];
|
||||
$ext = strtolower($file->getClientOriginalExtension());
|
||||
|
||||
if (in_array($ext, ['xlsx', 'xls'], true)) {
|
||||
if (!class_exists(\PhpOffice\PhpSpreadsheet\IOFactory::class)) {
|
||||
return response()->json([
|
||||
'message' => 'Import XLSX membutuhkan paket phpoffice/phpspreadsheet.',
|
||||
], 422);
|
||||
}
|
||||
$rows = $this->readXlsx($file->getRealPath());
|
||||
} else {
|
||||
$rows = $this->readCsv($file->getRealPath());
|
||||
}
|
||||
|
||||
if (count($rows) === 0) {
|
||||
return response()->json(['message' => 'File kosong atau format tidak valid.'], 422);
|
||||
}
|
||||
|
||||
$inserted = 0;
|
||||
$skipped = 0;
|
||||
|
||||
DB::transaction(function () use ($rows, &$inserted, &$skipped) {
|
||||
foreach ($rows as $row) {
|
||||
$nama = trim((string) ($row['nama'] ?? ''));
|
||||
if ($nama === '') {
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
Distributor::create([
|
||||
'nama' => $nama,
|
||||
'alamat' => isset($row['alamat']) && $row['alamat'] !== '' ? $row['alamat'] : null,
|
||||
'telepon' => isset($row['telepon']) && $row['telepon'] !== '' ? $row['telepon'] : null,
|
||||
]);
|
||||
$inserted++;
|
||||
}
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'message' => "Import selesai. Berhasil: {$inserted}, Dilewati: {$skipped}.",
|
||||
'data' => [
|
||||
'inserted' => $inserted,
|
||||
'skipped' => $skipped,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function template(): \Symfony\Component\HttpFoundation\StreamedResponse
|
||||
{
|
||||
$headers = [
|
||||
'Content-Type' => 'text/csv',
|
||||
'Content-Disposition' => 'attachment; filename="template_distributor.csv"',
|
||||
];
|
||||
|
||||
$callback = function () {
|
||||
$handle = fopen('php://output', 'w');
|
||||
fputcsv($handle, ['nama', 'alamat', 'telepon']);
|
||||
fputcsv($handle, ['PT Contoh Distributor', 'Jl. Contoh No. 1', '08123456789']);
|
||||
fclose($handle);
|
||||
};
|
||||
|
||||
return response()->stream($callback, 200, $headers);
|
||||
}
|
||||
|
||||
private function readCsv(string $path): array
|
||||
{
|
||||
$rows = [];
|
||||
$handle = fopen($path, 'r');
|
||||
if (!$handle) {
|
||||
return $rows;
|
||||
}
|
||||
|
||||
$header = null;
|
||||
while (($data = fgetcsv($handle, 0, ',')) !== false) {
|
||||
if ($data === [null] || $data === false) {
|
||||
continue;
|
||||
}
|
||||
if ($header === null) {
|
||||
$header = $this->normalizeHeader($data);
|
||||
if ($this->isHeaderRow($header)) {
|
||||
continue;
|
||||
}
|
||||
$rows[] = $this->mapRowByIndex($data);
|
||||
continue;
|
||||
}
|
||||
$rows[] = $this->mapRowByHeader($data, $header);
|
||||
}
|
||||
fclose($handle);
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
private function readXlsx(string $path): array
|
||||
{
|
||||
$rows = [];
|
||||
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($path);
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$data = $sheet->toArray(null, true, true, true);
|
||||
if (count($data) === 0) {
|
||||
return $rows;
|
||||
}
|
||||
|
||||
$first = array_shift($data);
|
||||
$header = $this->normalizeHeader(array_values($first ?? []));
|
||||
$hasHeader = $this->isHeaderRow($header);
|
||||
if (!$hasHeader && $first) {
|
||||
$rows[] = $this->mapRowByIndex(array_values($first));
|
||||
}
|
||||
|
||||
foreach ($data as $row) {
|
||||
$values = array_values($row);
|
||||
$rows[] = $hasHeader
|
||||
? $this->mapRowByHeader($values, $header)
|
||||
: $this->mapRowByIndex($values);
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
private function normalizeHeader(array $row): array
|
||||
{
|
||||
return array_map(function ($v) {
|
||||
$v = strtolower(trim((string) $v));
|
||||
return preg_replace('/\s+/', '_', $v);
|
||||
}, $row);
|
||||
}
|
||||
|
||||
private function isHeaderRow(array $header): bool
|
||||
{
|
||||
$fields = ['nama', 'alamat', 'telepon'];
|
||||
foreach ($fields as $f) {
|
||||
if (in_array($f, $header, true)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private function mapRowByHeader(array $row, array $header): array
|
||||
{
|
||||
$map = array_flip($header);
|
||||
return [
|
||||
'nama' => trim((string) ($row[$map['nama']] ?? '')),
|
||||
'alamat' => trim((string) ($row[$map['alamat']] ?? '')),
|
||||
'telepon' => trim((string) ($row[$map['telepon']] ?? '')),
|
||||
];
|
||||
}
|
||||
|
||||
private function mapRowByIndex(array $row): array
|
||||
{
|
||||
return [
|
||||
'nama' => trim((string) ($row[0] ?? '')),
|
||||
'alamat' => trim((string) ($row[1] ?? '')),
|
||||
'telepon' => trim((string) ($row[2] ?? '')),
|
||||
];
|
||||
}
|
||||
}
|
||||
242
app/Http/Controllers/Admin/LaporanController.php
Normal file
242
app/Http/Controllers/Admin/LaporanController.php
Normal file
@@ -0,0 +1,242 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class LaporanController extends Controller
|
||||
{
|
||||
public function harian(Request $request): JsonResponse
|
||||
{
|
||||
[$from, $to, $rows, $summary] = $this->buildHarian($request);
|
||||
|
||||
return response()->json([
|
||||
'from' => $from,
|
||||
'to' => $to,
|
||||
'summary' => $summary,
|
||||
'data' => $rows,
|
||||
]);
|
||||
}
|
||||
|
||||
public function bulanan(Request $request): JsonResponse
|
||||
{
|
||||
[$year, $rows, $summary] = $this->buildBulanan($request);
|
||||
|
||||
return response()->json([
|
||||
'year' => $year,
|
||||
'summary' => $summary,
|
||||
'data' => $rows,
|
||||
]);
|
||||
}
|
||||
|
||||
public function exportHarian(Request $request)
|
||||
{
|
||||
[$from, $to] = $this->resolveHarianRange($request);
|
||||
$rows = $this->buildDetailByRange($from, $to);
|
||||
|
||||
$filename = "laporan-harian-detail-{$from}-{$to}.xls";
|
||||
|
||||
return $this->downloadXls($filename, $rows);
|
||||
}
|
||||
|
||||
public function exportBulanan(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'year' => ['nullable', 'integer', 'min:2000', 'max:2100'],
|
||||
]);
|
||||
$year = (int) ($data['year'] ?? now()->year);
|
||||
$from = "{$year}-01-01";
|
||||
$to = "{$year}-12-31";
|
||||
$rows = $this->buildDetailByRange($from, $to);
|
||||
|
||||
$filename = "laporan-bulanan-detail-{$year}.xls";
|
||||
|
||||
return $this->downloadXls($filename, $rows);
|
||||
}
|
||||
|
||||
private function buildHarian(Request $request): array
|
||||
{
|
||||
[$from, $to] = $this->resolveHarianRange($request);
|
||||
|
||||
$penjualan = DB::table('penjualan')
|
||||
->selectRaw('DATE(waktu) as tanggal, COUNT(*) as transaksi, SUM(total) as pendapatan')
|
||||
->whereBetween('waktu', [$from . ' 00:00:00', $to . ' 23:59:59'])
|
||||
->groupBy('tanggal')
|
||||
->orderBy('tanggal', 'desc')
|
||||
->get();
|
||||
|
||||
$itemsByDay = DB::table('penjualan_barang as pb')
|
||||
->join('penjualan as p', 'p.id', '=', 'pb.penjualan_id')
|
||||
->selectRaw('DATE(p.waktu) as tanggal, SUM(pb.jumlah) as item_terjual')
|
||||
->whereBetween('p.waktu', [$from . ' 00:00:00', $to . ' 23:59:59'])
|
||||
->groupBy('tanggal')
|
||||
->get()
|
||||
->keyBy('tanggal');
|
||||
|
||||
$rows = $penjualan->map(function ($r) use ($itemsByDay) {
|
||||
$items = $itemsByDay->get($r->tanggal);
|
||||
return [
|
||||
'tanggal' => $r->tanggal,
|
||||
'transaksi' => (int) $r->transaksi,
|
||||
'pendapatan' => (int) ($r->pendapatan ?? 0),
|
||||
'item_terjual' => (int) ($items?->item_terjual ?? 0),
|
||||
];
|
||||
});
|
||||
|
||||
$summary = [
|
||||
'pendapatan' => (int) $rows->sum('pendapatan'),
|
||||
'transaksi' => (int) $rows->sum('transaksi'),
|
||||
'item_terjual' => (int) $rows->sum('item_terjual'),
|
||||
];
|
||||
|
||||
return [$from, $to, $rows, $summary];
|
||||
}
|
||||
|
||||
private function buildBulanan(Request $request): array
|
||||
{
|
||||
$data = $request->validate([
|
||||
'year' => ['nullable', 'integer', 'min:2000', 'max:2100'],
|
||||
]);
|
||||
|
||||
$year = (int) ($data['year'] ?? now()->year);
|
||||
|
||||
$penjualan = DB::table('penjualan')
|
||||
->selectRaw("DATE_FORMAT(waktu, '%Y-%m') as bulan, COUNT(*) as transaksi, SUM(total) as pendapatan")
|
||||
->whereYear('waktu', $year)
|
||||
->groupBy('bulan')
|
||||
->orderBy('bulan', 'desc')
|
||||
->get();
|
||||
|
||||
$itemsByMonth = DB::table('penjualan_barang as pb')
|
||||
->join('penjualan as p', 'p.id', '=', 'pb.penjualan_id')
|
||||
->selectRaw("DATE_FORMAT(p.waktu, '%Y-%m') as bulan, SUM(pb.jumlah) as item_terjual")
|
||||
->whereYear('p.waktu', $year)
|
||||
->groupBy('bulan')
|
||||
->get()
|
||||
->keyBy('bulan');
|
||||
|
||||
$rows = $penjualan->map(function ($r) use ($itemsByMonth) {
|
||||
$items = $itemsByMonth->get($r->bulan);
|
||||
return [
|
||||
'bulan' => $r->bulan,
|
||||
'transaksi' => (int) $r->transaksi,
|
||||
'pendapatan' => (int) ($r->pendapatan ?? 0),
|
||||
'item_terjual' => (int) ($items?->item_terjual ?? 0),
|
||||
];
|
||||
});
|
||||
|
||||
$summary = [
|
||||
'pendapatan' => (int) $rows->sum('pendapatan'),
|
||||
'transaksi' => (int) $rows->sum('transaksi'),
|
||||
'item_terjual' => (int) $rows->sum('item_terjual'),
|
||||
];
|
||||
|
||||
return [$year, $rows, $summary];
|
||||
}
|
||||
|
||||
private function resolveHarianRange(Request $request): array
|
||||
{
|
||||
$data = $request->validate([
|
||||
'from' => ['nullable', 'date'],
|
||||
'to' => ['nullable', 'date'],
|
||||
]);
|
||||
$from = $data['from'] ?? now()->subDays(14)->toDateString();
|
||||
$to = $data['to'] ?? now()->toDateString();
|
||||
return [$from, $to];
|
||||
}
|
||||
|
||||
private function buildDetailByRange(string $from, string $to): array
|
||||
{
|
||||
$rows = DB::table('penjualan as p')
|
||||
->join('penjualan_barang as pb', 'pb.penjualan_id', '=', 'p.id')
|
||||
->leftJoin('produk as pr', 'pr.id', '=', 'pb.produk_id')
|
||||
->leftJoin('users as u', 'u.id', '=', 'p.user_id')
|
||||
->selectRaw('p.waktu, p.nomor_penjualan, u.name as kasir, pr.nama as item, pr.barcode, pb.jumlah as qty, pb.harga, pb.total as total_item, p.subtotal, p.diskon, p.pajak, p.total, p.metode_pembayaran, p.jumlah_bayar, p.kembalian')
|
||||
->whereBetween('p.waktu', [$from . ' 00:00:00', $to . ' 23:59:59'])
|
||||
->orderByDesc('p.waktu')
|
||||
->get();
|
||||
|
||||
return $rows->map(function ($r) {
|
||||
$dt = new \DateTime($r->waktu);
|
||||
return [
|
||||
'tanggal' => $dt->format('Y-m-d'),
|
||||
'jam' => $dt->format('H:i:s'),
|
||||
'nomor_penjualan' => $r->nomor_penjualan,
|
||||
'kasir' => $r->kasir ?? '-',
|
||||
'item' => $r->item ?? '-',
|
||||
'barcode' => $r->barcode ?? '-',
|
||||
'qty' => (int) $r->qty,
|
||||
'harga' => (int) $r->harga,
|
||||
'total_item' => (int) $r->total_item,
|
||||
'subtotal' => (int) $r->subtotal,
|
||||
'diskon' => (int) $r->diskon,
|
||||
'pajak' => (int) $r->pajak,
|
||||
'total' => (int) $r->total,
|
||||
'metode' => $r->metode_pembayaran,
|
||||
'bayar' => (int) $r->jumlah_bayar,
|
||||
'kembalian' => (int) $r->kembalian,
|
||||
];
|
||||
})->all();
|
||||
}
|
||||
|
||||
private function downloadXls(string $filename, array $rows)
|
||||
{
|
||||
$headers = [
|
||||
'Tanggal',
|
||||
'Jam',
|
||||
'No Transaksi',
|
||||
'Kasir',
|
||||
'Item',
|
||||
'Barcode',
|
||||
'Qty',
|
||||
'Harga',
|
||||
'Total Item',
|
||||
'Subtotal',
|
||||
'Diskon',
|
||||
'Pajak',
|
||||
'Total',
|
||||
'Metode',
|
||||
'Bayar',
|
||||
'Kembalian',
|
||||
];
|
||||
|
||||
return response()->streamDownload(function () use ($headers, $rows) {
|
||||
$escape = fn ($v) => htmlspecialchars((string) $v, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
||||
|
||||
echo "\xEF\xBB\xBF";
|
||||
echo "<table border='1'>\n<thead><tr>";
|
||||
foreach ($headers as $h) {
|
||||
echo "<th>{$escape($h)}</th>";
|
||||
}
|
||||
echo "</tr></thead>\n<tbody>";
|
||||
|
||||
foreach ($rows as $r) {
|
||||
echo "<tr>";
|
||||
echo "<td>{$escape($r['tanggal'])}</td>";
|
||||
echo "<td>{$escape($r['jam'])}</td>";
|
||||
echo "<td>{$escape($r['nomor_penjualan'])}</td>";
|
||||
echo "<td>{$escape($r['kasir'])}</td>";
|
||||
echo "<td>{$escape($r['item'])}</td>";
|
||||
echo "<td>{$escape($r['barcode'])}</td>";
|
||||
echo "<td>{$escape($r['qty'])}</td>";
|
||||
echo "<td>{$escape($r['harga'])}</td>";
|
||||
echo "<td>{$escape($r['total_item'])}</td>";
|
||||
echo "<td>{$escape($r['subtotal'])}</td>";
|
||||
echo "<td>{$escape($r['diskon'])}</td>";
|
||||
echo "<td>{$escape($r['pajak'])}</td>";
|
||||
echo "<td>{$escape($r['total'])}</td>";
|
||||
echo "<td>{$escape($r['metode'])}</td>";
|
||||
echo "<td>{$escape($r['bayar'])}</td>";
|
||||
echo "<td>{$escape($r['kembalian'])}</td>";
|
||||
echo "</tr>\n";
|
||||
}
|
||||
|
||||
echo "</tbody></table>";
|
||||
}, $filename, [
|
||||
'Content-Type' => 'application/vnd.ms-excel; charset=UTF-8',
|
||||
]);
|
||||
}
|
||||
}
|
||||
17
app/Http/Controllers/Admin/LokasiController.php
Normal file
17
app/Http/Controllers/Admin/LokasiController.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Lokasi;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class LokasiController extends Controller
|
||||
{
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'data' => Lokasi::query()->orderBy('id')->get(['id', 'kode', 'nama']),
|
||||
]);
|
||||
}
|
||||
}
|
||||
55
app/Http/Controllers/Admin/MeController.php
Normal file
55
app/Http/Controllers/Admin/MeController.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class MeController extends Controller
|
||||
{
|
||||
public function show(Request $request): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'id' => $request->user()?->id,
|
||||
'name' => $request->user()?->name,
|
||||
'email' => $request->user()?->email,
|
||||
'role' => $request->user()?->role,
|
||||
'nip' => $request->user()?->nip,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request): JsonResponse
|
||||
{
|
||||
$user = $request->user();
|
||||
|
||||
$data = $request->validate([
|
||||
'name' => ['required', 'string', 'max:100'],
|
||||
'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users', 'email')->ignore($user?->id)],
|
||||
]);
|
||||
|
||||
$user->update($data);
|
||||
|
||||
return response()->json(['message' => 'Profil berhasil diperbarui.']);
|
||||
}
|
||||
|
||||
public function updatePassword(Request $request): JsonResponse
|
||||
{
|
||||
$user = $request->user();
|
||||
|
||||
$data = $request->validate([
|
||||
'current_password' => ['required', 'current_password'],
|
||||
'password' => ['required', 'string', 'min:6', 'confirmed'],
|
||||
]);
|
||||
|
||||
$user->update([
|
||||
'password' => Hash::make($data['password']),
|
||||
]);
|
||||
|
||||
return response()->json(['message' => 'Password berhasil diperbarui.']);
|
||||
}
|
||||
}
|
||||
136
app/Http/Controllers/Admin/PegawaiController.php
Normal file
136
app/Http/Controllers/Admin/PegawaiController.php
Normal file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Pegawai;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class PegawaiController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$q = trim((string) $request->query('q', ''));
|
||||
|
||||
$query = Pegawai::query()->orderByDesc('id');
|
||||
if ($q !== '') {
|
||||
$query->where(function ($qq) use ($q) {
|
||||
$qq->where('nama', 'like', "%{$q}%")
|
||||
->orWhere('nip', 'like', "%{$q}%")
|
||||
->orWhere('nik', 'like', "%{$q}%")
|
||||
->orWhere('jabatan', 'like', "%{$q}%")
|
||||
->orWhere('telepon', 'like', "%{$q}%");
|
||||
});
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'data' => $query->get([
|
||||
'id',
|
||||
'nip',
|
||||
'nama',
|
||||
'jabatan',
|
||||
'telepon',
|
||||
'alamat',
|
||||
'tempat_lahir',
|
||||
'tanggal_lahir',
|
||||
'jenis_kelamin',
|
||||
'kota',
|
||||
'nik',
|
||||
'foto',
|
||||
'is_aktif',
|
||||
'created_at',
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'nip' => ['nullable', 'string', 'max:30', Rule::unique('pegawai', 'nip')],
|
||||
'nama' => ['required', 'string', 'max:100'],
|
||||
'jabatan' => ['nullable', 'string', 'max:100'],
|
||||
'telepon' => ['nullable', 'string', 'max:30'],
|
||||
'alamat' => ['nullable', 'string', 'max:255'],
|
||||
'tempat_lahir' => ['nullable', 'string', 'max:100'],
|
||||
'tanggal_lahir' => ['nullable', 'date'],
|
||||
'jenis_kelamin' => ['nullable', 'string', 'max:10'],
|
||||
'kota' => ['nullable', 'string', 'max:100'],
|
||||
'nik' => ['nullable', 'string', 'max:30', Rule::unique('pegawai', 'nik')],
|
||||
'foto' => ['nullable', 'string', 'max:255'],
|
||||
'is_aktif' => ['nullable', 'boolean'],
|
||||
]);
|
||||
|
||||
$pegawai = Pegawai::create([
|
||||
'nip' => $data['nip'] ?? null,
|
||||
'nama' => $data['nama'],
|
||||
'jabatan' => $data['jabatan'] ?? null,
|
||||
'telepon' => $data['telepon'] ?? null,
|
||||
'alamat' => $data['alamat'] ?? null,
|
||||
'tempat_lahir' => $data['tempat_lahir'] ?? null,
|
||||
'tanggal_lahir' => $data['tanggal_lahir'] ?? null,
|
||||
'jenis_kelamin' => $data['jenis_kelamin'] ?? null,
|
||||
'kota' => $data['kota'] ?? null,
|
||||
'nik' => $data['nik'] ?? null,
|
||||
'foto' => $data['foto'] ?? null,
|
||||
'is_aktif' => (bool) ($data['is_aktif'] ?? true),
|
||||
]);
|
||||
|
||||
return response()->json(['data' => $pegawai], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, Pegawai $pegawai): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'nip' => ['nullable', 'string', 'max:30', Rule::unique('pegawai', 'nip')->ignore($pegawai->id)],
|
||||
'nama' => ['required', 'string', 'max:100'],
|
||||
'jabatan' => ['nullable', 'string', 'max:100'],
|
||||
'telepon' => ['nullable', 'string', 'max:30'],
|
||||
'alamat' => ['nullable', 'string', 'max:255'],
|
||||
'tempat_lahir' => ['nullable', 'string', 'max:100'],
|
||||
'tanggal_lahir' => ['nullable', 'date'],
|
||||
'jenis_kelamin' => ['nullable', 'string', 'max:10'],
|
||||
'kota' => ['nullable', 'string', 'max:100'],
|
||||
'nik' => ['nullable', 'string', 'max:30', Rule::unique('pegawai', 'nik')->ignore($pegawai->id)],
|
||||
'foto' => ['nullable', 'string', 'max:255'],
|
||||
'is_aktif' => ['nullable', 'boolean'],
|
||||
]);
|
||||
|
||||
$pegawai->update([
|
||||
'nip' => $data['nip'] ?? null,
|
||||
'nama' => $data['nama'],
|
||||
'jabatan' => $data['jabatan'] ?? null,
|
||||
'telepon' => $data['telepon'] ?? null,
|
||||
'alamat' => $data['alamat'] ?? null,
|
||||
'tempat_lahir' => $data['tempat_lahir'] ?? null,
|
||||
'tanggal_lahir' => $data['tanggal_lahir'] ?? null,
|
||||
'jenis_kelamin' => $data['jenis_kelamin'] ?? null,
|
||||
'kota' => $data['kota'] ?? null,
|
||||
'nik' => $data['nik'] ?? null,
|
||||
'foto' => $data['foto'] ?? null,
|
||||
'is_aktif' => (bool) ($data['is_aktif'] ?? true),
|
||||
]);
|
||||
|
||||
return response()->json(['data' => $pegawai->fresh()]);
|
||||
}
|
||||
|
||||
public function destroy(Pegawai $pegawai): JsonResponse
|
||||
{
|
||||
$pegawai->delete();
|
||||
return response()->json(['message' => 'Pegawai dihapus.']);
|
||||
}
|
||||
|
||||
public function uploadFoto(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'foto' => ['required', 'image', 'max:2048'],
|
||||
]);
|
||||
|
||||
$path = $data['foto']->store('pegawai', 'public');
|
||||
$url = Storage::url($path);
|
||||
|
||||
return response()->json(['data' => ['foto' => $url]]);
|
||||
}
|
||||
}
|
||||
296
app/Http/Controllers/Admin/PenerimaanBarangController.php
Normal file
296
app/Http/Controllers/Admin/PenerimaanBarangController.php
Normal file
@@ -0,0 +1,296 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Distributor;
|
||||
use App\Models\Lokasi;
|
||||
use App\Models\PenerimaanBarang;
|
||||
use App\Models\PenerimaanBarangItem;
|
||||
use App\Models\PergerakanStok;
|
||||
use App\Models\ProductUnit;
|
||||
use App\Models\Produk;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class PenerimaanBarangController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'from' => ['nullable', 'date'],
|
||||
'to' => ['nullable', 'date'],
|
||||
'q' => ['nullable', 'string', 'max:100'],
|
||||
'limit' => ['nullable', 'integer', 'min:1', 'max:200'],
|
||||
]);
|
||||
|
||||
$limit = (int) ($data['limit'] ?? 50);
|
||||
|
||||
$query = PenerimaanBarang::query()
|
||||
->with(['distributor:id,nama', 'lokasi:id,nama,kode'])
|
||||
->withCount('items')
|
||||
->orderByDesc('id');
|
||||
|
||||
if (!empty($data['from'])) {
|
||||
$query->where('tanggal_datang', '>=', $data['from']);
|
||||
}
|
||||
if (!empty($data['to'])) {
|
||||
$query->where('tanggal_datang', '<=', $data['to']);
|
||||
}
|
||||
if (!empty($data['q'])) {
|
||||
$s = trim((string) $data['q']);
|
||||
$query->where(function ($qq) use ($s) {
|
||||
$qq->where('nomor_penerimaan', 'like', "%{$s}%")
|
||||
->orWhere('no_faktur', 'like', "%{$s}%");
|
||||
});
|
||||
}
|
||||
|
||||
$rows = $query->limit($limit)->get()->map(function (PenerimaanBarang $p) {
|
||||
return [
|
||||
'id' => $p->id,
|
||||
'nomor_penerimaan' => $p->nomor_penerimaan,
|
||||
'no_faktur' => $p->no_faktur,
|
||||
'tanggal_faktur' => $p->tanggal_faktur,
|
||||
'tanggal_datang' => $p->tanggal_datang,
|
||||
'status' => $p->status,
|
||||
'items_count' => (int) ($p->items_count ?? 0),
|
||||
'distributor' => $p->distributor?->nama,
|
||||
'lokasi' => $p->lokasi ? ($p->lokasi->nama . ' (' . $p->lokasi->kode . ')') : null,
|
||||
'created_at' => $p->created_at,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json(['data' => $rows]);
|
||||
}
|
||||
|
||||
public function show(PenerimaanBarang $penerimaan): JsonResponse
|
||||
{
|
||||
$penerimaan->load([
|
||||
'items.produk:id,nama,barcode,sku',
|
||||
'distributor:id,nama',
|
||||
'lokasi:id,nama,kode',
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'id' => $penerimaan->id,
|
||||
'nomor_penerimaan' => $penerimaan->nomor_penerimaan,
|
||||
'no_faktur' => $penerimaan->no_faktur,
|
||||
'tanggal_faktur' => $penerimaan->tanggal_faktur,
|
||||
'tanggal_datang' => $penerimaan->tanggal_datang,
|
||||
'distributor_id' => $penerimaan->distributor_id,
|
||||
'lokasi_id' => $penerimaan->lokasi_id,
|
||||
'status' => $penerimaan->status,
|
||||
'catatan' => $penerimaan->catatan,
|
||||
'applied_at' => $penerimaan->applied_at,
|
||||
'items' => $penerimaan->items->map(function (PenerimaanBarangItem $i) {
|
||||
return [
|
||||
'id' => $i->id,
|
||||
'produk_id' => $i->produk_id,
|
||||
'produk_nama' => $i->produk?->nama,
|
||||
'produk_barcode' => $i->produk?->barcode,
|
||||
'produk_sku' => $i->produk?->sku,
|
||||
'unit_id' => $i->unit_id,
|
||||
'qty' => (int) $i->qty,
|
||||
'qty_input' => (int) ($i->qty_input ?? $i->qty),
|
||||
'harga' => $i->harga !== null ? (int) $i->harga : null,
|
||||
'unit' => $i->unit,
|
||||
'multiplier' => (int) ($i->multiplier ?? 1),
|
||||
];
|
||||
}),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$payload = $this->validatePayload($request);
|
||||
|
||||
$penerimaan = DB::transaction(function () use ($payload, $request) {
|
||||
$nomor = $this->generateNomor();
|
||||
$row = PenerimaanBarang::create([
|
||||
'nomor_penerimaan' => $nomor,
|
||||
'no_faktur' => $payload['no_faktur'] ?? null,
|
||||
'tanggal_faktur' => $payload['tanggal_faktur'] ?? null,
|
||||
'tanggal_datang' => $payload['tanggal_datang'] ?? null,
|
||||
'distributor_id' => (int) $payload['distributor_id'],
|
||||
'lokasi_id' => (int) $payload['lokasi_id'],
|
||||
'user_id' => $request->user()?->id,
|
||||
'status' => $payload['status'],
|
||||
'catatan' => $payload['catatan'] ?? null,
|
||||
]);
|
||||
|
||||
$this->syncItems($row, $payload['items']);
|
||||
|
||||
if (in_array($payload['status'], ['SELESAI', 'SEBAGIAN'], true)) {
|
||||
$this->applyStock($row);
|
||||
}
|
||||
|
||||
return $row->fresh();
|
||||
});
|
||||
|
||||
return response()->json(['data' => $penerimaan], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, PenerimaanBarang $penerimaan): JsonResponse
|
||||
{
|
||||
$payload = $this->validatePayload($request);
|
||||
|
||||
if ($penerimaan->applied_at) {
|
||||
if (in_array($payload['status'], ['DRAFT', 'BATAL'], true)) {
|
||||
return response()->json([
|
||||
'message' => 'Penerimaan sudah diproses, tidak bisa dibatalkan.',
|
||||
], 422);
|
||||
}
|
||||
return response()->json([
|
||||
'message' => 'Penerimaan sudah diproses, tidak bisa diubah.',
|
||||
], 422);
|
||||
}
|
||||
|
||||
$updated = DB::transaction(function () use ($penerimaan, $payload) {
|
||||
$penerimaan->update([
|
||||
'no_faktur' => $payload['no_faktur'] ?? null,
|
||||
'tanggal_faktur' => $payload['tanggal_faktur'] ?? null,
|
||||
'tanggal_datang' => $payload['tanggal_datang'] ?? null,
|
||||
'distributor_id' => (int) $payload['distributor_id'],
|
||||
'lokasi_id' => (int) $payload['lokasi_id'],
|
||||
'status' => $payload['status'],
|
||||
'catatan' => $payload['catatan'] ?? null,
|
||||
]);
|
||||
|
||||
$this->syncItems($penerimaan, $payload['items']);
|
||||
|
||||
if (in_array($payload['status'], ['SELESAI', 'SEBAGIAN'], true)) {
|
||||
$this->applyStock($penerimaan);
|
||||
}
|
||||
|
||||
return $penerimaan->fresh();
|
||||
});
|
||||
|
||||
return response()->json(['data' => $updated]);
|
||||
}
|
||||
|
||||
private function validatePayload(Request $request): array
|
||||
{
|
||||
return $request->validate([
|
||||
'no_faktur' => ['nullable', 'string', 'max:100'],
|
||||
'tanggal_faktur' => ['nullable', 'date'],
|
||||
'tanggal_datang' => ['nullable', 'date'],
|
||||
'distributor_id' => ['required', 'integer', 'exists:distributors,id'],
|
||||
'lokasi_id' => ['required', 'integer', 'exists:lokasi,id'],
|
||||
'status' => ['required', 'string', 'max:20'],
|
||||
'catatan' => ['nullable', 'string', 'max:255'],
|
||||
'items' => ['required', 'array', 'min:1'],
|
||||
'items.*.produk_id' => ['required', 'integer', 'exists:produk,id'],
|
||||
'items.*.qty' => ['required', 'integer', 'min:1'],
|
||||
'items.*.harga' => ['nullable', 'integer', 'min:0'],
|
||||
'items.*.unit_id' => ['nullable', 'integer', 'exists:produk_unit,id'],
|
||||
]);
|
||||
}
|
||||
|
||||
private function syncItems(PenerimaanBarang $penerimaan, array $items): void
|
||||
{
|
||||
PenerimaanBarangItem::query()
|
||||
->where('penerimaan_id', $penerimaan->id)
|
||||
->delete();
|
||||
|
||||
$produkIds = collect($items)->pluck('produk_id')->unique()->values();
|
||||
$baseUnits = ProductUnit::query()
|
||||
->whereIn('produk_id', $produkIds)
|
||||
->where('is_base', true)
|
||||
->get()
|
||||
->keyBy('produk_id');
|
||||
|
||||
$unitIds = collect($items)
|
||||
->pluck('unit_id')
|
||||
->filter()
|
||||
->unique()
|
||||
->values();
|
||||
$unitMap = $unitIds->isEmpty()
|
||||
? collect()
|
||||
: ProductUnit::query()->whereIn('id', $unitIds)->get()->keyBy('id');
|
||||
|
||||
$rows = [];
|
||||
foreach ($items as $item) {
|
||||
$unit = null;
|
||||
if (!empty($item['unit_id']) && $unitMap->has((int) $item['unit_id'])) {
|
||||
$unit = $unitMap->get((int) $item['unit_id']);
|
||||
}
|
||||
if (!$unit || (int) $unit->produk_id !== (int) $item['produk_id']) {
|
||||
$unit = $baseUnits->get((int) $item['produk_id']);
|
||||
}
|
||||
$multiplier = (int) ($unit?->multiplier ?? 1);
|
||||
$qtyInput = (int) $item['qty'];
|
||||
$qtyPcs = max(1, $qtyInput * $multiplier);
|
||||
$rows[] = [
|
||||
'penerimaan_id' => $penerimaan->id,
|
||||
'produk_id' => (int) $item['produk_id'],
|
||||
'unit_id' => $unit?->id,
|
||||
'qty' => $qtyPcs,
|
||||
'harga' => isset($item['harga']) ? (int) $item['harga'] : null,
|
||||
'unit' => $unit?->nama_unit ?? 'PCS',
|
||||
'qty_input' => $qtyInput,
|
||||
'multiplier' => $multiplier,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
];
|
||||
}
|
||||
|
||||
PenerimaanBarangItem::insert($rows);
|
||||
}
|
||||
|
||||
private function applyStock(PenerimaanBarang $penerimaan): void
|
||||
{
|
||||
if ($penerimaan->applied_at) return;
|
||||
|
||||
$items = PenerimaanBarangItem::query()
|
||||
->where('penerimaan_id', $penerimaan->id)
|
||||
->get();
|
||||
|
||||
$produkIds = $items->pluck('produk_id')->unique()->values();
|
||||
$baseUnits = ProductUnit::query()
|
||||
->whereIn('produk_id', $produkIds)
|
||||
->where('is_base', true)
|
||||
->get()
|
||||
->keyBy('produk_id');
|
||||
|
||||
$rows = [];
|
||||
foreach ($items as $item) {
|
||||
$unit = $baseUnits->get($item->produk_id);
|
||||
$unitName = $unit?->nama_unit ?? 'PCS';
|
||||
$multiplier = (int) ($unit?->multiplier ?? 1);
|
||||
$rows[] = [
|
||||
'waktu' => now(),
|
||||
'produk_id' => $item->produk_id,
|
||||
'dari_lokasi_id' => null,
|
||||
'ke_lokasi_id' => $penerimaan->lokasi_id,
|
||||
'jenis' => 'PENERIMAAN',
|
||||
'jumlah' => (int) $item->qty,
|
||||
'unit' => $item->unit ?? $unitName,
|
||||
'qty_input' => (int) ($item->qty_input ?? $item->qty),
|
||||
'multiplier' => (int) ($item->multiplier ?? $multiplier),
|
||||
'catatan' => "Penerimaan {$penerimaan->nomor_penerimaan}",
|
||||
'referensi_tipe' => 'PENERIMAAN',
|
||||
'referensi_id' => $penerimaan->id,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
];
|
||||
}
|
||||
|
||||
if (!empty($rows)) {
|
||||
PergerakanStok::insert($rows);
|
||||
}
|
||||
|
||||
$penerimaan->update(['applied_at' => now()]);
|
||||
}
|
||||
|
||||
private function generateNomor(): string
|
||||
{
|
||||
do {
|
||||
$nomor = 'RCV' . now()->format('ymdHis') . random_int(100, 999);
|
||||
} while (PenerimaanBarang::query()->where('nomor_penerimaan', $nomor)->exists());
|
||||
|
||||
return $nomor;
|
||||
}
|
||||
}
|
||||
116
app/Http/Controllers/Admin/PengaturanController.php
Normal file
116
app/Http/Controllers/Admin/PengaturanController.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class PengaturanController extends Controller
|
||||
{
|
||||
public function show(): JsonResponse
|
||||
{
|
||||
$settings = Setting::query()
|
||||
->whereIn('key', [
|
||||
'toko.nama',
|
||||
'toko.telepon',
|
||||
'toko.alamat',
|
||||
'toko.logo',
|
||||
'transaksi.pajak_default',
|
||||
'transaksi.diskon_default',
|
||||
'transaksi.metode_default',
|
||||
'stok.low_threshold',
|
||||
'stok.low_threshold_toko',
|
||||
'stok.low_threshold_gudang',
|
||||
'struk.footer',
|
||||
])
|
||||
->get()
|
||||
->keyBy('key')
|
||||
->map(fn ($s) => $s->value);
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'toko' => [
|
||||
'nama' => $settings['toko.nama'] ?? '',
|
||||
'telepon' => $settings['toko.telepon'] ?? '',
|
||||
'alamat' => $settings['toko.alamat'] ?? '',
|
||||
'logo' => $settings['toko.logo'] ?? '',
|
||||
],
|
||||
'transaksi' => [
|
||||
'pajak_default' => (int) ($settings['transaksi.pajak_default'] ?? 0),
|
||||
'diskon_default' => (int) ($settings['transaksi.diskon_default'] ?? 0),
|
||||
'metode_default' => $settings['transaksi.metode_default'] ?? 'CASH',
|
||||
],
|
||||
'stok' => [
|
||||
'low_threshold_toko' => (int) ($settings['stok.low_threshold_toko'] ?? $settings['stok.low_threshold'] ?? 5),
|
||||
'low_threshold_gudang' => (int) ($settings['stok.low_threshold_gudang'] ?? 5),
|
||||
],
|
||||
'struk' => [
|
||||
'footer' => $settings['struk.footer'] ?? '',
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'toko.nama' => ['nullable', 'string', 'max:100'],
|
||||
'toko.telepon' => ['nullable', 'string', 'max:30'],
|
||||
'toko.alamat' => ['nullable', 'string', 'max:255'],
|
||||
'toko.logo' => ['nullable', 'string', 'max:255'],
|
||||
'transaksi.pajak_default' => ['nullable', 'integer', 'min:0'],
|
||||
'transaksi.diskon_default' => ['nullable', 'integer', 'min:0'],
|
||||
'transaksi.metode_default' => [
|
||||
'nullable',
|
||||
'string',
|
||||
Rule::in(['CASH', 'QRIS', 'PIUTANG', 'VOUCHER', 'VOUCHER_CASH']),
|
||||
],
|
||||
'stok.low_threshold_toko' => ['nullable', 'integer', 'min:0', 'max:1000000'],
|
||||
'stok.low_threshold_gudang' => ['nullable', 'integer', 'min:0', 'max:1000000'],
|
||||
'struk.footer' => ['nullable', 'string', 'max:2000'],
|
||||
]);
|
||||
|
||||
$flat = [
|
||||
'toko.nama' => $data['toko']['nama'] ?? null,
|
||||
'toko.telepon' => $data['toko']['telepon'] ?? null,
|
||||
'toko.alamat' => $data['toko']['alamat'] ?? null,
|
||||
'toko.logo' => $data['toko']['logo'] ?? null,
|
||||
'transaksi.pajak_default' => $data['transaksi']['pajak_default'] ?? null,
|
||||
'transaksi.diskon_default' => $data['transaksi']['diskon_default'] ?? null,
|
||||
'transaksi.metode_default' => $data['transaksi']['metode_default'] ?? null,
|
||||
'stok.low_threshold_toko' => $data['stok']['low_threshold_toko'] ?? null,
|
||||
'stok.low_threshold_gudang' => $data['stok']['low_threshold_gudang'] ?? null,
|
||||
'struk.footer' => $data['struk']['footer'] ?? null,
|
||||
];
|
||||
|
||||
foreach ($flat as $key => $value) {
|
||||
if ($value === null) {
|
||||
continue;
|
||||
}
|
||||
Setting::updateOrCreate(['key' => $key], ['value' => (string) $value]);
|
||||
}
|
||||
|
||||
return response()->json(['message' => 'Pengaturan berhasil disimpan.']);
|
||||
}
|
||||
|
||||
public function uploadLogo(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'logo' => ['required', 'image', 'max:2048'],
|
||||
]);
|
||||
|
||||
$file = $data['logo'];
|
||||
$path = $file->store('logos', 'public');
|
||||
$url = '/storage/' . $path;
|
||||
|
||||
Setting::updateOrCreate(['key' => 'toko.logo'], ['value' => $url]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Logo berhasil diunggah.',
|
||||
'data' => ['logo' => $url],
|
||||
]);
|
||||
}
|
||||
}
|
||||
376
app/Http/Controllers/Admin/PiutangController.php
Normal file
376
app/Http/Controllers/Admin/PiutangController.php
Normal file
@@ -0,0 +1,376 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Penjualan;
|
||||
use App\Models\PiutangPayment;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class PiutangController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'from' => ['nullable', 'date'],
|
||||
'to' => ['nullable', 'date'],
|
||||
'q' => ['nullable', 'string', 'max:100'],
|
||||
'limit' => ['nullable', 'integer', 'min:1', 'max:200'],
|
||||
]);
|
||||
|
||||
$limit = (int) ($data['limit'] ?? 100);
|
||||
|
||||
$query = Penjualan::query()
|
||||
->with('user:id,name')
|
||||
->where('sisa_piutang', '>', 0)
|
||||
->orderByDesc('waktu');
|
||||
|
||||
if (!empty($data['from'])) {
|
||||
$query->where('waktu', '>=', $data['from'] . ' 00:00:00');
|
||||
}
|
||||
if (!empty($data['to'])) {
|
||||
$query->where('waktu', '<=', $data['to'] . ' 23:59:59');
|
||||
}
|
||||
if (!empty($data['q'])) {
|
||||
$s = trim((string) $data['q']);
|
||||
$query->where('nomor_penjualan', 'like', "%{$s}%");
|
||||
}
|
||||
|
||||
$rows = $query->limit($limit)->get()->map(function (Penjualan $p) {
|
||||
return [
|
||||
'id' => $p->id,
|
||||
'nomor_penjualan' => $p->nomor_penjualan,
|
||||
'waktu' => $p->waktu,
|
||||
'total' => (int) $p->total,
|
||||
'jumlah_bayar' => (int) $p->jumlah_bayar,
|
||||
'sisa_piutang' => (int) ($p->sisa_piutang ?? 0),
|
||||
'status_pembayaran' => $p->status_pembayaran ?? 'PIUTANG',
|
||||
'unit_piutang' => $p->unit_piutang,
|
||||
'kasir' => $p->user?->name,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json(['data' => $rows]);
|
||||
}
|
||||
|
||||
public function pay(Request $request, Penjualan $penjualan): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'jumlah' => ['required', 'integer', 'min:1'],
|
||||
'metode' => ['nullable', 'string', 'max:50'],
|
||||
'catatan' => ['nullable', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$metode = $data['metode'] ?? 'CASH';
|
||||
|
||||
if ((int) ($penjualan->sisa_piutang ?? 0) <= 0) {
|
||||
return response()->json([
|
||||
'message' => 'Piutang sudah lunas.',
|
||||
], 422);
|
||||
}
|
||||
|
||||
try {
|
||||
$updated = DB::transaction(function () use ($penjualan, $data, $metode, $request) {
|
||||
$p = Penjualan::query()->where('id', $penjualan->id)->lockForUpdate()->firstOrFail();
|
||||
$sisa = (int) ($p->sisa_piutang ?? 0);
|
||||
if ($sisa <= 0) {
|
||||
throw new \RuntimeException('PIUTANG_LUNAS');
|
||||
}
|
||||
$jumlah = (int) $data['jumlah'];
|
||||
if ($jumlah > $sisa) {
|
||||
throw new \RuntimeException('PIUTANG_OVER');
|
||||
}
|
||||
|
||||
$sisaAfter = $sisa - $jumlah;
|
||||
$p->jumlah_bayar = (int) $p->jumlah_bayar + $jumlah;
|
||||
$p->sisa_piutang = $sisaAfter;
|
||||
$p->status_pembayaran = $sisaAfter > 0 ? 'PIUTANG' : 'LUNAS';
|
||||
$p->save();
|
||||
|
||||
$payment = PiutangPayment::create([
|
||||
'penjualan_id' => $p->id,
|
||||
'jumlah' => $jumlah,
|
||||
'metode' => $metode,
|
||||
'catatan' => $data['catatan'] ?? null,
|
||||
'user_id' => $request->user()?->id,
|
||||
'sisa_before' => $sisa,
|
||||
'sisa_after' => $sisaAfter,
|
||||
]);
|
||||
|
||||
return [
|
||||
'penjualan' => $p->fresh(),
|
||||
'payment' => $payment,
|
||||
];
|
||||
});
|
||||
} catch (\RuntimeException $e) {
|
||||
if ($e->getMessage() === 'PIUTANG_LUNAS') {
|
||||
return response()->json(['message' => 'Piutang sudah lunas.'], 422);
|
||||
}
|
||||
if ($e->getMessage() === 'PIUTANG_OVER') {
|
||||
return response()->json(['message' => 'Jumlah bayar melebihi sisa piutang.'], 422);
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return response()->json(['data' => $updated]);
|
||||
}
|
||||
|
||||
public function history(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'from' => ['nullable', 'date'],
|
||||
'to' => ['nullable', 'date'],
|
||||
'q' => ['nullable', 'string', 'max:100'],
|
||||
'limit' => ['nullable', 'integer', 'min:1', 'max:200'],
|
||||
]);
|
||||
|
||||
$limit = (int) ($data['limit'] ?? 100);
|
||||
|
||||
$query = DB::table('piutang_payments as pp')
|
||||
->leftJoin('penjualan as p', 'p.id', '=', 'pp.penjualan_id')
|
||||
->leftJoin('users as u', 'u.id', '=', 'pp.user_id')
|
||||
->select([
|
||||
'pp.id',
|
||||
'pp.penjualan_id',
|
||||
'p.nomor_penjualan',
|
||||
'p.unit_piutang',
|
||||
'pp.jumlah',
|
||||
'pp.metode',
|
||||
'pp.catatan',
|
||||
'pp.sisa_before',
|
||||
'pp.sisa_after',
|
||||
'pp.created_at',
|
||||
'u.name as kasir',
|
||||
])
|
||||
->orderByDesc('pp.id');
|
||||
|
||||
if (!empty($data['from'])) {
|
||||
$query->where('pp.created_at', '>=', $data['from'] . ' 00:00:00');
|
||||
}
|
||||
if (!empty($data['to'])) {
|
||||
$query->where('pp.created_at', '<=', $data['to'] . ' 23:59:59');
|
||||
}
|
||||
if (!empty($data['q'])) {
|
||||
$s = trim((string) $data['q']);
|
||||
$query->where('p.nomor_penjualan', 'like', "%{$s}%");
|
||||
}
|
||||
|
||||
$rows = $query->limit($limit)->get();
|
||||
|
||||
return response()->json(['data' => $rows]);
|
||||
}
|
||||
|
||||
public function historyByPenjualan(Penjualan $penjualan): JsonResponse
|
||||
{
|
||||
$rows = PiutangPayment::query()
|
||||
->where('penjualan_id', $penjualan->id)
|
||||
->orderByDesc('id')
|
||||
->get()
|
||||
->map(function (PiutangPayment $p) {
|
||||
return [
|
||||
'id' => $p->id,
|
||||
'jumlah' => (int) $p->jumlah,
|
||||
'metode' => $p->metode,
|
||||
'catatan' => $p->catatan,
|
||||
'sisa_before' => (int) $p->sisa_before,
|
||||
'sisa_after' => (int) $p->sisa_after,
|
||||
'created_at' => $p->created_at,
|
||||
'unit_piutang' => $penjualan->unit_piutang,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json(['data' => $rows]);
|
||||
}
|
||||
|
||||
public function export(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'from' => ['nullable', 'date'],
|
||||
'to' => ['nullable', 'date'],
|
||||
'q' => ['nullable', 'string', 'max:100'],
|
||||
]);
|
||||
|
||||
$query = Penjualan::query()
|
||||
->with('user:id,name')
|
||||
->where('sisa_piutang', '>', 0)
|
||||
->orderByDesc('waktu');
|
||||
|
||||
if (!empty($data['from'])) {
|
||||
$query->where('waktu', '>=', $data['from'] . ' 00:00:00');
|
||||
}
|
||||
if (!empty($data['to'])) {
|
||||
$query->where('waktu', '<=', $data['to'] . ' 23:59:59');
|
||||
}
|
||||
if (!empty($data['q'])) {
|
||||
$s = trim((string) $data['q']);
|
||||
$query->where('nomor_penjualan', 'like', "%{$s}%");
|
||||
}
|
||||
|
||||
$rows = $query->get()->map(function (Penjualan $p) {
|
||||
$dt = new \DateTime($p->waktu);
|
||||
return [
|
||||
'tanggal' => $dt->format('Y-m-d'),
|
||||
'jam' => $dt->format('H:i:s'),
|
||||
'nomor_penjualan' => $p->nomor_penjualan,
|
||||
'kasir' => $p->user?->name ?? '-',
|
||||
'total' => (int) $p->total,
|
||||
'bayar' => (int) $p->jumlah_bayar,
|
||||
'sisa_piutang' => (int) ($p->sisa_piutang ?? 0),
|
||||
'status' => $p->status_pembayaran ?? 'PIUTANG',
|
||||
'unit_piutang' => $p->unit_piutang ?? '-',
|
||||
];
|
||||
})->all();
|
||||
|
||||
$filename = 'piutang-' . now()->format('YmdHis') . '.xls';
|
||||
|
||||
return $this->downloadXls($filename, $rows);
|
||||
}
|
||||
|
||||
public function exportHistory(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'from' => ['nullable', 'date'],
|
||||
'to' => ['nullable', 'date'],
|
||||
'q' => ['nullable', 'string', 'max:100'],
|
||||
]);
|
||||
|
||||
$query = DB::table('piutang_payments as pp')
|
||||
->leftJoin('penjualan as p', 'p.id', '=', 'pp.penjualan_id')
|
||||
->leftJoin('users as u', 'u.id', '=', 'pp.user_id')
|
||||
->select([
|
||||
'pp.created_at',
|
||||
'p.nomor_penjualan',
|
||||
'p.unit_piutang',
|
||||
'u.name as kasir',
|
||||
'pp.metode',
|
||||
'pp.jumlah',
|
||||
'pp.sisa_before',
|
||||
'pp.sisa_after',
|
||||
'pp.catatan',
|
||||
])
|
||||
->orderByDesc('pp.id');
|
||||
|
||||
if (!empty($data['from'])) {
|
||||
$query->where('pp.created_at', '>=', $data['from'] . ' 00:00:00');
|
||||
}
|
||||
if (!empty($data['to'])) {
|
||||
$query->where('pp.created_at', '<=', $data['to'] . ' 23:59:59');
|
||||
}
|
||||
if (!empty($data['q'])) {
|
||||
$s = trim((string) $data['q']);
|
||||
$query->where('p.nomor_penjualan', 'like', "%{$s}%");
|
||||
}
|
||||
|
||||
$rows = $query->get()->map(function ($r) {
|
||||
$dt = new \DateTime($r->created_at);
|
||||
return [
|
||||
'tanggal' => $dt->format('Y-m-d'),
|
||||
'jam' => $dt->format('H:i:s'),
|
||||
'nomor_penjualan' => $r->nomor_penjualan ?? '-',
|
||||
'unit_piutang' => $r->unit_piutang ?? '-',
|
||||
'kasir' => $r->kasir ?? '-',
|
||||
'metode' => $r->metode,
|
||||
'jumlah' => (int) $r->jumlah,
|
||||
'sisa_before' => (int) $r->sisa_before,
|
||||
'sisa_after' => (int) $r->sisa_after,
|
||||
'catatan' => $r->catatan ?? '-',
|
||||
];
|
||||
})->all();
|
||||
|
||||
$filename = 'piutang-riwayat-' . now()->format('YmdHis') . '.xls';
|
||||
|
||||
return $this->downloadXlsHistory($filename, $rows);
|
||||
}
|
||||
|
||||
private function downloadXls(string $filename, array $rows)
|
||||
{
|
||||
$headers = [
|
||||
'Tanggal',
|
||||
'Jam',
|
||||
'No Transaksi',
|
||||
'Kasir',
|
||||
'Total',
|
||||
'Bayar',
|
||||
'Sisa Piutang',
|
||||
'Status',
|
||||
'Unit',
|
||||
];
|
||||
|
||||
return response()->streamDownload(function () use ($headers, $rows) {
|
||||
$escape = fn ($v) => htmlspecialchars((string) $v, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
||||
|
||||
echo "\xEF\xBB\xBF";
|
||||
echo "<table border='1'>\n<thead><tr>";
|
||||
foreach ($headers as $h) {
|
||||
echo "<th>{$escape($h)}</th>";
|
||||
}
|
||||
echo "</tr></thead>\n<tbody>";
|
||||
|
||||
foreach ($rows as $r) {
|
||||
echo "<tr>";
|
||||
echo "<td>{$escape($r['tanggal'])}</td>";
|
||||
echo "<td>{$escape($r['jam'])}</td>";
|
||||
echo "<td>{$escape($r['nomor_penjualan'])}</td>";
|
||||
echo "<td>{$escape($r['kasir'])}</td>";
|
||||
echo "<td>{$escape($r['total'])}</td>";
|
||||
echo "<td>{$escape($r['bayar'])}</td>";
|
||||
echo "<td>{$escape($r['sisa_piutang'])}</td>";
|
||||
echo "<td>{$escape($r['status'])}</td>";
|
||||
echo "<td>{$escape($r['unit_piutang'])}</td>";
|
||||
echo "</tr>\n";
|
||||
}
|
||||
|
||||
echo "</tbody></table>";
|
||||
}, $filename, [
|
||||
'Content-Type' => 'application/vnd.ms-excel; charset=UTF-8',
|
||||
]);
|
||||
}
|
||||
|
||||
private function downloadXlsHistory(string $filename, array $rows)
|
||||
{
|
||||
$headers = [
|
||||
'Tanggal',
|
||||
'Jam',
|
||||
'No Transaksi',
|
||||
'Unit',
|
||||
'Kasir',
|
||||
'Metode',
|
||||
'Jumlah Bayar',
|
||||
'Sisa Sebelum',
|
||||
'Sisa Sesudah',
|
||||
'Catatan',
|
||||
];
|
||||
|
||||
return response()->streamDownload(function () use ($headers, $rows) {
|
||||
$escape = fn ($v) => htmlspecialchars((string) $v, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
||||
|
||||
echo "\xEF\xBB\xBF";
|
||||
echo "<table border='1'>\n<thead><tr>";
|
||||
foreach ($headers as $h) {
|
||||
echo "<th>{$escape($h)}</th>";
|
||||
}
|
||||
echo "</tr></thead>\n<tbody>";
|
||||
|
||||
foreach ($rows as $r) {
|
||||
echo "<tr>";
|
||||
echo "<td>{$escape($r['tanggal'])}</td>";
|
||||
echo "<td>{$escape($r['jam'])}</td>";
|
||||
echo "<td>{$escape($r['nomor_penjualan'])}</td>";
|
||||
echo "<td>{$escape($r['unit_piutang'])}</td>";
|
||||
echo "<td>{$escape($r['kasir'])}</td>";
|
||||
echo "<td>{$escape($r['metode'])}</td>";
|
||||
echo "<td>{$escape($r['jumlah'])}</td>";
|
||||
echo "<td>{$escape($r['sisa_before'])}</td>";
|
||||
echo "<td>{$escape($r['sisa_after'])}</td>";
|
||||
echo "<td>{$escape($r['catatan'])}</td>";
|
||||
echo "</tr>\n";
|
||||
}
|
||||
|
||||
echo "</tbody></table>";
|
||||
}, $filename, [
|
||||
'Content-Type' => 'application/vnd.ms-excel; charset=UTF-8',
|
||||
]);
|
||||
}
|
||||
}
|
||||
216
app/Http/Controllers/Admin/ProdukController.php
Normal file
216
app/Http/Controllers/Admin/ProdukController.php
Normal file
@@ -0,0 +1,216 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\ProductUnit;
|
||||
use App\Models\Produk;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class ProdukController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$withStock = $request->boolean('with_stock');
|
||||
$withUnits = $request->boolean('with_units');
|
||||
|
||||
$q = Produk::query()->select(['id', 'sku', 'nama', 'barcode', 'harga_beli', 'harga_jual', 'is_aktif']);
|
||||
|
||||
if ($withUnits) {
|
||||
$q->with(['units' => function ($q) {
|
||||
$q->select(['id', 'produk_id', 'nama_unit', 'multiplier', 'is_base'])->orderBy('multiplier');
|
||||
}]);
|
||||
}
|
||||
|
||||
if ($withStock) {
|
||||
// stok total = masuk (ke_lokasi_id tidak null) - keluar (dari_lokasi_id tidak null)
|
||||
$q->addSelect([
|
||||
'stok_total' => DB::table('pergerakan_stok')
|
||||
->selectRaw(
|
||||
'COALESCE(SUM(CASE WHEN ke_lokasi_id IS NOT NULL THEN jumlah ELSE 0 END),0) - ' .
|
||||
'COALESCE(SUM(CASE WHEN dari_lokasi_id IS NOT NULL THEN jumlah ELSE 0 END),0)'
|
||||
)
|
||||
->whereColumn('pergerakan_stok.produk_id', 'produk.id'),
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'data' => $q->orderBy('id', 'desc')->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(Request $request, Produk $produk): JsonResponse
|
||||
{
|
||||
$withUnits = $request->boolean('with_units');
|
||||
$withStock = $request->boolean('with_stock');
|
||||
|
||||
if ($withUnits) {
|
||||
$produk->load(['units' => function ($q) {
|
||||
$q->select(['id', 'produk_id', 'nama_unit', 'multiplier', 'is_base'])->orderBy('multiplier');
|
||||
}]);
|
||||
}
|
||||
|
||||
if ($withStock) {
|
||||
$stokTotal = DB::table('pergerakan_stok')
|
||||
->selectRaw(
|
||||
'COALESCE(SUM(CASE WHEN ke_lokasi_id IS NOT NULL THEN jumlah ELSE 0 END),0) - ' .
|
||||
'COALESCE(SUM(CASE WHEN dari_lokasi_id IS NOT NULL THEN jumlah ELSE 0 END),0) AS stok_total'
|
||||
)
|
||||
->where('produk_id', $produk->id)
|
||||
->value('stok_total');
|
||||
|
||||
$produk->setAttribute('stok_total', (int) ($stokTotal ?? 0));
|
||||
}
|
||||
|
||||
return response()->json(['data' => $produk]);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'sku' => ['nullable', 'string', 'max:255'],
|
||||
'nama' => ['required', 'string', 'max:255'],
|
||||
'barcode' => ['required', 'string', 'max:255', 'unique:produk,barcode'],
|
||||
'harga_beli' => ['nullable', 'integer', 'min:0'],
|
||||
'harga_jual' => ['required', 'integer', 'min:0'],
|
||||
'is_aktif' => ['nullable', 'boolean'],
|
||||
|
||||
// satuan (opsional)
|
||||
'isi_pack' => ['nullable', 'integer', 'min:2'],
|
||||
'isi_dus' => ['nullable', 'integer', 'min:2'],
|
||||
'isi_box' => ['nullable', 'integer', 'min:2'],
|
||||
'units' => ['nullable', 'array'],
|
||||
]);
|
||||
|
||||
if (empty($data['sku'])) {
|
||||
$data['sku'] = null;
|
||||
}
|
||||
|
||||
// ambil unit config dari payload
|
||||
$units = $this->normalizeUnits($request, $data);
|
||||
|
||||
// remove unit-only keys before create
|
||||
unset($data['isi_pack'], $data['isi_dus'], $data['isi_box'], $data['units']);
|
||||
|
||||
$data['is_aktif'] = isset($data['is_aktif']) ? (bool) $data['is_aktif'] : true;
|
||||
|
||||
$p = Produk::create($data);
|
||||
if (!$p->sku) {
|
||||
$p->sku = $this->generateSku($p->id);
|
||||
$p->save();
|
||||
}
|
||||
$this->syncUnits($p, $units);
|
||||
|
||||
return response()->json([
|
||||
'data' => $p,
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, Produk $produk): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'sku' => ['nullable', 'string', 'max:255'],
|
||||
'nama' => ['required', 'string', 'max:255'],
|
||||
'barcode' => ['required', 'string', 'max:255', Rule::unique('produk', 'barcode')->ignore($produk->id)],
|
||||
'harga_beli' => ['nullable', 'integer', 'min:0'],
|
||||
'harga_jual' => ['required', 'integer', 'min:0'],
|
||||
'is_aktif' => ['nullable', 'boolean'],
|
||||
|
||||
'isi_pack' => ['nullable', 'integer', 'min:2'],
|
||||
'isi_dus' => ['nullable', 'integer', 'min:2'],
|
||||
'isi_box' => ['nullable', 'integer', 'min:2'],
|
||||
'units' => ['nullable', 'array'],
|
||||
]);
|
||||
|
||||
$units = $this->normalizeUnits($request, $data);
|
||||
unset($data['isi_pack'], $data['isi_dus'], $data['isi_box'], $data['units']);
|
||||
|
||||
if (array_key_exists('is_aktif', $data)) {
|
||||
$data['is_aktif'] = (bool) $data['is_aktif'];
|
||||
}
|
||||
|
||||
$produk->update($data);
|
||||
if (!$produk->sku) {
|
||||
$produk->sku = $this->generateSku($produk->id);
|
||||
$produk->save();
|
||||
}
|
||||
$this->syncUnits($produk, $units);
|
||||
|
||||
return response()->json([
|
||||
'data' => $produk->fresh(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalisasi payload unit.
|
||||
* - Selalu ada PCS (multiplier 1)
|
||||
* - pack/dus/box optional
|
||||
*/
|
||||
private function normalizeUnits(Request $request, array $validated): array
|
||||
{
|
||||
$units = [];
|
||||
|
||||
// 1) kalau user kirim units array
|
||||
if ($request->has('units') && is_array($request->input('units'))) {
|
||||
foreach ((array) $request->input('units') as $name => $mul) {
|
||||
$name = strtoupper(trim((string) $name));
|
||||
$mul = (int) $mul;
|
||||
if ($name && $mul >= 1) {
|
||||
$units[$name] = $mul;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2) fallback dari field isi_pack/isi_dus/isi_box
|
||||
foreach ([
|
||||
'PACK' => $validated['isi_pack'] ?? null,
|
||||
'DUS' => $validated['isi_dus'] ?? null,
|
||||
'BOX' => $validated['isi_box'] ?? null,
|
||||
] as $name => $mul) {
|
||||
if ($mul !== null) {
|
||||
$mul = (int) $mul;
|
||||
if ($mul >= 2) {
|
||||
$units[$name] = $mul;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// base unit
|
||||
$units['PCS'] = 1;
|
||||
|
||||
// hapus yang invalid
|
||||
$units = array_filter($units, fn ($m) => is_int($m) && $m >= 1);
|
||||
|
||||
// pastikan PCS = 1
|
||||
$units['PCS'] = 1;
|
||||
|
||||
return $units;
|
||||
}
|
||||
|
||||
private function syncUnits(Produk $produk, array $units): void
|
||||
{
|
||||
// upsert units
|
||||
foreach ($units as $name => $multiplier) {
|
||||
$isBase = ($name === 'PCS');
|
||||
ProductUnit::updateOrCreate(
|
||||
['produk_id' => $produk->id, 'nama_unit' => $name],
|
||||
['multiplier' => (int) $multiplier, 'is_base' => $isBase]
|
||||
);
|
||||
}
|
||||
|
||||
// hapus unit yang tidak ada di payload (kecuali PCS)
|
||||
$keep = array_keys($units);
|
||||
ProductUnit::query()
|
||||
->where('produk_id', $produk->id)
|
||||
->whereNotIn('nama_unit', $keep)
|
||||
->delete();
|
||||
}
|
||||
|
||||
private function generateSku(int $id): string
|
||||
{
|
||||
return 'SKU' . str_pad((string) $id, 6, '0', STR_PAD_LEFT);
|
||||
}
|
||||
}
|
||||
303
app/Http/Controllers/Admin/StokController.php
Normal file
303
app/Http/Controllers/Admin/StokController.php
Normal file
@@ -0,0 +1,303 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Lokasi;
|
||||
use App\Models\PergerakanStok;
|
||||
use App\Models\ProductUnit;
|
||||
use App\Models\Produk;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class StokController extends Controller
|
||||
{
|
||||
/**
|
||||
* List stok per lokasi berdasarkan kode lokasi (contoh: GUDANG / TOKO).
|
||||
*/
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$kode = $request->string('kode')->toString();
|
||||
if (!$kode) {
|
||||
return response()->json(['message' => 'Parameter kode lokasi wajib.'], 422);
|
||||
}
|
||||
|
||||
$role = $request->user()?->role ?: 'admin';
|
||||
$kodeUpper = strtoupper($kode);
|
||||
if ($role === 'gudang' && $kodeUpper !== 'GUDANG') {
|
||||
return response()->json(['message' => 'Akses ditolak.'], 403);
|
||||
}
|
||||
if ($role === 'kasir' && $kodeUpper !== 'TOKO') {
|
||||
return response()->json(['message' => 'Akses ditolak.'], 403);
|
||||
}
|
||||
|
||||
$lokasi = Lokasi::query()->where('kode', $kode)->first();
|
||||
if (!$lokasi) {
|
||||
return response()->json(['message' => 'Lokasi tidak ditemukan.'], 404);
|
||||
}
|
||||
|
||||
$lokasiId = $lokasi->id;
|
||||
|
||||
$rows = Produk::query()
|
||||
->with(['units' => function ($q) {
|
||||
$q->selectRaw('id, produk_id, nama_unit, nama_unit as name, multiplier, is_base')
|
||||
->orderBy('multiplier');
|
||||
}])
|
||||
->select(['produk.id', 'produk.sku', 'produk.nama', 'produk.barcode', 'produk.harga_jual'])
|
||||
->addSelect([
|
||||
'stok' => DB::table('pergerakan_stok')
|
||||
->selectRaw(
|
||||
'COALESCE(SUM(CASE WHEN ke_lokasi_id = ? THEN jumlah ELSE 0 END),0) - ' .
|
||||
'COALESCE(SUM(CASE WHEN dari_lokasi_id = ? THEN jumlah ELSE 0 END),0)',
|
||||
[$lokasiId, $lokasiId]
|
||||
)
|
||||
->whereColumn('pergerakan_stok.produk_id', 'produk.id'),
|
||||
])
|
||||
->orderBy('produk.nama')
|
||||
->get();
|
||||
|
||||
return response()->json([
|
||||
'lokasi' => $lokasi,
|
||||
'data' => $rows,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer stok antar lokasi.
|
||||
*/
|
||||
public function transfer(Request $request): JsonResponse
|
||||
{
|
||||
$role = $request->user()?->role ?: 'admin';
|
||||
if ($role === 'kasir') {
|
||||
return response()->json(['message' => 'Akses ditolak.'], 403);
|
||||
}
|
||||
|
||||
$data = $request->validate([
|
||||
'produk_id' => ['required', 'integer', 'exists:produk,id'],
|
||||
'dari_lokasi_id' => ['required', 'integer', 'exists:lokasi,id'],
|
||||
'ke_lokasi_id' => ['required', 'integer', 'exists:lokasi,id', 'different:dari_lokasi_id'],
|
||||
'jumlah' => ['required', 'integer', 'min:1'],
|
||||
'referensi_tipe' => ['nullable', 'string', 'max:255'],
|
||||
'catatan' => ['nullable', 'string', 'max:500'],
|
||||
]);
|
||||
|
||||
$produk = Produk::findOrFail($data['produk_id']);
|
||||
|
||||
$dari = (int) $data['dari_lokasi_id'];
|
||||
$jumlah = (int) $data['jumlah'];
|
||||
|
||||
if ($role === 'gudang') {
|
||||
$lokasiDari = Lokasi::find($dari);
|
||||
if (!$lokasiDari || strtoupper((string) $lokasiDari->kode) !== 'GUDANG') {
|
||||
return response()->json(['message' => 'Akses ditolak.'], 403);
|
||||
}
|
||||
}
|
||||
|
||||
// cek stok cukup di lokasi asal
|
||||
$stokAsal = $this->stokProdukDiLokasi($produk->id, $dari);
|
||||
if ($stokAsal < $jumlah) {
|
||||
return response()->json([
|
||||
'message' => "Stok tidak cukup di lokasi asal. Stok tersedia: {$stokAsal}",
|
||||
], 422);
|
||||
}
|
||||
|
||||
$ref = $data['referensi_tipe'] ?? null;
|
||||
if (!$ref) {
|
||||
$ref = $this->generateTransferRef();
|
||||
}
|
||||
|
||||
$movement = DB::transaction(function () use ($data, $ref) {
|
||||
return PergerakanStok::create([
|
||||
'waktu' => now(),
|
||||
'produk_id' => $data['produk_id'],
|
||||
'dari_lokasi_id' => $data['dari_lokasi_id'],
|
||||
'ke_lokasi_id' => $data['ke_lokasi_id'],
|
||||
'jenis' => 'TRANSFER',
|
||||
'jumlah' => $data['jumlah'],
|
||||
'referensi_tipe' => $ref,
|
||||
'referensi_id' => null,
|
||||
]);
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'data' => $movement,
|
||||
], 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stok masuk / keluar / penyesuaian dengan satuan (PCS/PACK/DUS/BOX).
|
||||
* qty yang disimpan di pergerakan_stok.jumlah selalu dalam PCS.
|
||||
*/
|
||||
public function adjust(Request $request): JsonResponse
|
||||
{
|
||||
$role = $request->user()?->role ?: 'admin';
|
||||
if ($role === 'kasir') {
|
||||
return response()->json(['message' => 'Akses ditolak.'], 403);
|
||||
}
|
||||
|
||||
$data = $request->validate([
|
||||
'produk_id' => ['required', 'integer', 'exists:produk,id'],
|
||||
'lokasi_id' => ['required', 'integer', 'exists:lokasi,id'],
|
||||
'tipe' => ['required', 'string', 'in:IN,OUT,ADJUST'],
|
||||
'qty' => ['required', 'integer', 'min:1'],
|
||||
'unit' => ['required', 'string', 'max:20'],
|
||||
'catatan' => ['nullable', 'string', 'max:500'],
|
||||
]);
|
||||
|
||||
$produk = Produk::query()->with('units')->findOrFail((int) $data['produk_id']);
|
||||
$lokasiId = (int) $data['lokasi_id'];
|
||||
|
||||
if ($role === 'gudang') {
|
||||
$lokasi = Lokasi::find($lokasiId);
|
||||
if (!$lokasi || strtoupper((string) $lokasi->kode) !== 'GUDANG') {
|
||||
return response()->json(['message' => 'Akses ditolak.'], 403);
|
||||
}
|
||||
}
|
||||
|
||||
$unitName = strtoupper(trim($data['unit']));
|
||||
$unit = ProductUnit::query()
|
||||
->where('produk_id', $produk->id)
|
||||
->where('nama_unit', $unitName)
|
||||
->first();
|
||||
|
||||
if (!$unit) {
|
||||
return response()->json(['message' => 'Satuan tidak valid untuk produk ini.'], 422);
|
||||
}
|
||||
|
||||
$qtyInput = (int) $data['qty'];
|
||||
$multiplier = (int) $unit->multiplier;
|
||||
$pcsQty = $qtyInput * $multiplier;
|
||||
|
||||
// hitung stok sekarang di lokasi
|
||||
$stokNow = $this->stokProdukDiLokasi($produk->id, $lokasiId);
|
||||
|
||||
$tipe = $data['tipe'];
|
||||
$deltaPcs = $pcsQty;
|
||||
if ($tipe === 'OUT') {
|
||||
$deltaPcs = -$pcsQty;
|
||||
} elseif ($tipe === 'ADJUST') {
|
||||
// ADJUST artinya set stok menjadi qty (dalam unit input)
|
||||
$target = $pcsQty;
|
||||
$deltaPcs = $target - $stokNow;
|
||||
}
|
||||
|
||||
if (($stokNow + $deltaPcs) < 0) {
|
||||
return response()->json([
|
||||
'message' => "Stok tidak cukup. Stok tersedia: {$stokNow} PCS",
|
||||
], 422);
|
||||
}
|
||||
|
||||
$movement = DB::transaction(function () use ($produk, $lokasiId, $tipe, $deltaPcs, $unitName, $qtyInput, $multiplier, $data) {
|
||||
// representasikan IN/OUT lewat dari/ke lokasi
|
||||
$dari = null;
|
||||
$ke = null;
|
||||
if ($deltaPcs >= 0) {
|
||||
$ke = $lokasiId;
|
||||
} else {
|
||||
$dari = $lokasiId;
|
||||
}
|
||||
|
||||
return PergerakanStok::create([
|
||||
'waktu' => now(),
|
||||
'produk_id' => $produk->id,
|
||||
'dari_lokasi_id' => $dari,
|
||||
'ke_lokasi_id' => $ke,
|
||||
'jenis' => $tipe,
|
||||
'jumlah' => abs($deltaPcs),
|
||||
'unit' => $unitName,
|
||||
'qty_input' => $qtyInput,
|
||||
'multiplier' => $multiplier,
|
||||
'catatan' => $data['catatan'] ?? null,
|
||||
'referensi_tipe' => null,
|
||||
'referensi_id' => null,
|
||||
]);
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'data' => $movement,
|
||||
'stok_sekarang' => $this->stokProdukDiLokasi($produk->id, $lokasiId),
|
||||
], 201);
|
||||
}
|
||||
|
||||
private function stokProdukDiLokasi(int $produkId, int $lokasiId): int
|
||||
{
|
||||
$in = (int) DB::table('pergerakan_stok')
|
||||
->where('produk_id', $produkId)
|
||||
->where('ke_lokasi_id', $lokasiId)
|
||||
->sum('jumlah');
|
||||
|
||||
$out = (int) DB::table('pergerakan_stok')
|
||||
->where('produk_id', $produkId)
|
||||
->where('dari_lokasi_id', $lokasiId)
|
||||
->sum('jumlah');
|
||||
|
||||
return $in - $out;
|
||||
}
|
||||
|
||||
private function generateTransferRef(): string
|
||||
{
|
||||
$stamp = now()->format('YmdHis');
|
||||
$rand = random_int(100, 999);
|
||||
return "TF-{$stamp}-{$rand}";
|
||||
}
|
||||
|
||||
public function transferHistory(Request $request): JsonResponse
|
||||
{
|
||||
$role = $request->user()?->role ?: 'admin';
|
||||
if ($role === 'kasir') {
|
||||
return response()->json(['message' => 'Akses ditolak.'], 403);
|
||||
}
|
||||
|
||||
$data = $request->validate([
|
||||
'from' => ['nullable', 'date'],
|
||||
'to' => ['nullable', 'date'],
|
||||
'q' => ['nullable', 'string', 'max:100'],
|
||||
'limit' => ['nullable', 'integer', 'min:1', 'max:200'],
|
||||
]);
|
||||
|
||||
$limit = (int)($data['limit'] ?? 50);
|
||||
|
||||
$query = PergerakanStok::query()
|
||||
->where('jenis', 'TRANSFER')
|
||||
->with([
|
||||
'produk:id,sku,nama,barcode',
|
||||
'dariLokasi:id,kode,nama',
|
||||
'keLokasi:id,kode,nama',
|
||||
])
|
||||
->orderByDesc('waktu');
|
||||
|
||||
if (!empty($data['from'])) {
|
||||
$query->where('waktu', '>=', $data['from'] . ' 00:00:00');
|
||||
}
|
||||
if (!empty($data['to'])) {
|
||||
$query->where('waktu', '<=', $data['to'] . ' 23:59:59');
|
||||
}
|
||||
if (!empty($data['q'])) {
|
||||
$s = trim($data['q']);
|
||||
$query->whereHas('produk', function ($q) use ($s) {
|
||||
$q->where('nama', 'like', "%{$s}%")
|
||||
->orWhere('barcode', 'like', "%{$s}%")
|
||||
->orWhere('sku', 'like', "%{$s}%");
|
||||
});
|
||||
}
|
||||
|
||||
$rows = $query->limit($limit)->get()->map(function ($m) {
|
||||
return [
|
||||
'id' => $m->id,
|
||||
'waktu' => $m->waktu,
|
||||
'produk' => $m->produk,
|
||||
'dari' => $m->dariLokasi,
|
||||
'ke' => $m->keLokasi,
|
||||
'jumlah_pcs' => (int)$m->jumlah,
|
||||
'unit' => $m->unit,
|
||||
'qty_input' => $m->qty_input,
|
||||
'multiplier' => $m->multiplier,
|
||||
'catatan' => $m->catatan,
|
||||
'referensi_tipe' => $m->referensi_tipe,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json(['data' => $rows]);
|
||||
}
|
||||
}
|
||||
439
app/Http/Controllers/Admin/TransaksiController.php
Normal file
439
app/Http/Controllers/Admin/TransaksiController.php
Normal file
@@ -0,0 +1,439 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Lokasi;
|
||||
use App\Models\Penjualan;
|
||||
use App\Models\PenjualanBarang;
|
||||
use App\Models\PergerakanStok;
|
||||
use App\Models\ProductUnit;
|
||||
use App\Models\Produk;
|
||||
use App\Models\Voucher;
|
||||
use App\Models\VoucherHistory;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class TransaksiController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'from' => ['nullable', 'date'],
|
||||
'to' => ['nullable', 'date'],
|
||||
'q' => ['nullable', 'string', 'max:100'],
|
||||
'limit' => ['nullable', 'integer', 'min:1', 'max:200'],
|
||||
]);
|
||||
|
||||
$limit = (int) ($data['limit'] ?? 50);
|
||||
|
||||
$query = Penjualan::query()
|
||||
->withCount('barang')
|
||||
->orderByDesc('waktu');
|
||||
|
||||
if (!empty($data['from'])) {
|
||||
$query->where('waktu', '>=', $data['from'] . ' 00:00:00');
|
||||
}
|
||||
if (!empty($data['to'])) {
|
||||
$query->where('waktu', '<=', $data['to'] . ' 23:59:59');
|
||||
}
|
||||
if (!empty($data['q'])) {
|
||||
$s = trim($data['q']);
|
||||
$query->where('nomor_penjualan', 'like', "%{$s}%");
|
||||
}
|
||||
|
||||
$rows = $query->limit($limit)->get()->map(function (Penjualan $p) {
|
||||
return [
|
||||
'id' => $p->id,
|
||||
'nomor_penjualan' => $p->nomor_penjualan,
|
||||
'waktu' => $p->waktu,
|
||||
'subtotal' => (int) $p->subtotal,
|
||||
'diskon' => (int) $p->diskon,
|
||||
'pajak' => (int) $p->pajak,
|
||||
'total' => (int) $p->total,
|
||||
'metode_pembayaran' => $p->metode_pembayaran,
|
||||
'status_pembayaran' => $p->status_pembayaran ?? 'LUNAS',
|
||||
'voucher_kode' => $p->voucher_kode,
|
||||
'voucher_amount' => (int) ($p->voucher_amount ?? ($p->voucher_kode ? $p->total : 0)),
|
||||
'jumlah_bayar' => (int) $p->jumlah_bayar,
|
||||
'kembalian' => (int) $p->kembalian,
|
||||
'sisa_piutang' => (int) ($p->sisa_piutang ?? 0),
|
||||
'items_count' => (int) ($p->barang_count ?? 0),
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json(['data' => $rows]);
|
||||
}
|
||||
|
||||
public function simpanTransaksi(Request $request): JsonResponse
|
||||
{
|
||||
$payload = $request->validate([
|
||||
'lokasi_id' => ['required', 'integer', 'exists:lokasi,id'],
|
||||
'items' => ['required', 'array', 'min:1'],
|
||||
'items.*.produk_id' => ['required', 'integer', 'exists:produk,id'],
|
||||
'items.*.qty' => ['required', 'integer', 'min:1'],
|
||||
'items.*.harga' => ['nullable', 'integer', 'min:0'],
|
||||
'diskon' => ['nullable', 'integer', 'min:0'],
|
||||
'pajak' => ['nullable', 'integer', 'min:0'],
|
||||
'metode_pembayaran' => ['required', 'string', 'max:50'],
|
||||
'jumlah_bayar' => ['required', 'integer', 'min:0'],
|
||||
'voucher_kode' => ['nullable', 'string', 'max:255'],
|
||||
'voucher_amount' => ['nullable', 'integer', 'min:0'],
|
||||
'vouchers' => ['nullable', 'array', 'max:3'],
|
||||
'vouchers.*.kode' => ['required_with:vouchers', 'string', 'max:255'],
|
||||
'vouchers.*.amount' => ['required_with:vouchers', 'integer', 'min:1'],
|
||||
'unit_piutang' => ['nullable', 'string', 'max:100'],
|
||||
]);
|
||||
|
||||
$lokasi = Lokasi::findOrFail((int) $payload['lokasi_id']);
|
||||
|
||||
$itemRows = collect($payload['items']);
|
||||
$produkIds = $itemRows->pluck('produk_id')->unique()->values();
|
||||
$produkMap = Produk::query()->whereIn('id', $produkIds)->get()->keyBy('id');
|
||||
|
||||
if ($produkMap->count() !== $produkIds->count()) {
|
||||
return response()->json(['message' => 'Produk tidak valid.'], 422);
|
||||
}
|
||||
|
||||
$baseUnits = ProductUnit::query()
|
||||
->whereIn('produk_id', $produkIds)
|
||||
->where('is_base', true)
|
||||
->get()
|
||||
->keyBy('produk_id');
|
||||
|
||||
$qtyByProduk = [];
|
||||
$computed = [];
|
||||
$subtotal = 0;
|
||||
|
||||
foreach ($itemRows as $row) {
|
||||
$pid = (int) $row['produk_id'];
|
||||
$qty = (int) $row['qty'];
|
||||
$produk = $produkMap->get($pid);
|
||||
|
||||
$harga = isset($row['harga']) ? (int) $row['harga'] : (int) $produk->harga_jual;
|
||||
$lineTotal = $harga * $qty;
|
||||
|
||||
$qtyByProduk[$pid] = ($qtyByProduk[$pid] ?? 0) + $qty;
|
||||
$subtotal += $lineTotal;
|
||||
|
||||
$computed[] = [
|
||||
'produk' => $produk,
|
||||
'qty' => $qty,
|
||||
'harga' => $harga,
|
||||
'total' => $lineTotal,
|
||||
];
|
||||
}
|
||||
|
||||
// cek stok per produk di lokasi
|
||||
foreach ($qtyByProduk as $pid => $qty) {
|
||||
$stok = $this->stokProdukDiLokasi($pid, $lokasi->id);
|
||||
if ($stok < $qty) {
|
||||
$nama = $produkMap->get($pid)?->nama ?? 'Produk';
|
||||
return response()->json([
|
||||
'message' => "Stok tidak cukup untuk {$nama}. Stok tersedia: {$stok}",
|
||||
], 422);
|
||||
}
|
||||
}
|
||||
|
||||
$diskon = (int) ($payload['diskon'] ?? 0);
|
||||
$pajak = (int) ($payload['pajak'] ?? 0);
|
||||
$total = max(0, $subtotal - $diskon + $pajak);
|
||||
$metode = strtoupper((string) $payload['metode_pembayaran']);
|
||||
$jumlahBayar = (int) $payload['jumlah_bayar'];
|
||||
$voucherKode = null;
|
||||
$voucherAmount = 0;
|
||||
$unitPiutang = null;
|
||||
$voucherList = [];
|
||||
|
||||
if (!empty($payload['vouchers']) && is_array($payload['vouchers'])) {
|
||||
$voucherList = collect($payload['vouchers'])
|
||||
->map(function ($v) {
|
||||
return [
|
||||
'kode' => trim((string) ($v['kode'] ?? '')),
|
||||
'amount' => (int) ($v['amount'] ?? 0),
|
||||
];
|
||||
})
|
||||
->filter(fn ($v) => $v['kode'] !== '' && $v['amount'] > 0)
|
||||
->values()
|
||||
->all();
|
||||
|
||||
$uniqueCodes = collect($voucherList)->pluck('kode')->unique();
|
||||
if ($uniqueCodes->count() !== count($voucherList)) {
|
||||
return response()->json([
|
||||
'message' => 'Kode voucher tidak boleh duplikat.',
|
||||
], 422);
|
||||
}
|
||||
}
|
||||
|
||||
if ($metode === 'VOUCHER' || $metode === 'VOUCHER_CASH') {
|
||||
if (!empty($voucherList)) {
|
||||
$voucherAmount = (int) collect($voucherList)->sum('amount');
|
||||
$voucherKode = $voucherList[0]['kode'] ?? null;
|
||||
} else {
|
||||
$voucherKode = trim((string) ($payload['voucher_kode'] ?? ''));
|
||||
if ($voucherKode === '') {
|
||||
return response()->json([
|
||||
'message' => 'Kode voucher wajib diisi.',
|
||||
], 422);
|
||||
}
|
||||
if ($metode === 'VOUCHER') {
|
||||
$voucherAmount = $total;
|
||||
$jumlahBayar = 0;
|
||||
} else {
|
||||
$voucherAmount = (int) ($payload['voucher_amount'] ?? 0);
|
||||
if ($voucherAmount <= 0) {
|
||||
return response()->json([
|
||||
'message' => 'Nominal voucher wajib diisi.',
|
||||
], 422);
|
||||
}
|
||||
if ($voucherAmount > $total) {
|
||||
return response()->json([
|
||||
'message' => 'Nominal voucher tidak boleh melebihi total.',
|
||||
], 422);
|
||||
}
|
||||
}
|
||||
$voucherList = [
|
||||
['kode' => $voucherKode, 'amount' => $voucherAmount],
|
||||
];
|
||||
}
|
||||
if ($voucherAmount <= 0) {
|
||||
return response()->json([
|
||||
'message' => 'Nominal voucher wajib diisi.',
|
||||
], 422);
|
||||
}
|
||||
if ($voucherAmount > $total) {
|
||||
return response()->json([
|
||||
'message' => 'Nominal voucher tidak boleh melebihi total.',
|
||||
], 422);
|
||||
}
|
||||
if ($metode === 'VOUCHER' && $voucherAmount < $total) {
|
||||
return response()->json([
|
||||
'message' => 'Saldo voucher tidak mencukupi untuk menutup total.',
|
||||
], 422);
|
||||
}
|
||||
}
|
||||
if ($metode === 'PIUTANG') {
|
||||
$unitPiutang = trim((string) ($payload['unit_piutang'] ?? ''));
|
||||
if ($unitPiutang === '') {
|
||||
return response()->json([
|
||||
'message' => 'Unit piutang wajib diisi.',
|
||||
], 422);
|
||||
}
|
||||
}
|
||||
|
||||
$remaining = max(0, $total - $voucherAmount);
|
||||
if ($metode !== 'PIUTANG' && $jumlahBayar < $remaining) {
|
||||
return response()->json([
|
||||
'message' => 'Jumlah bayar harus sama atau lebih besar dari sisa setelah voucher.',
|
||||
], 422);
|
||||
}
|
||||
|
||||
try {
|
||||
$penjualan = DB::transaction(function () use (
|
||||
$computed,
|
||||
$baseUnits,
|
||||
$lokasi,
|
||||
$subtotal,
|
||||
$diskon,
|
||||
$pajak,
|
||||
$total,
|
||||
$jumlahBayar,
|
||||
$payload,
|
||||
$request,
|
||||
$metode,
|
||||
$voucherKode,
|
||||
$voucherAmount,
|
||||
$voucherList,
|
||||
$unitPiutang
|
||||
) {
|
||||
$nomor = $this->generateNomor();
|
||||
$remaining = max(0, $total - $voucherAmount);
|
||||
$kembalian = max(0, $jumlahBayar - $remaining);
|
||||
$sisaPiutang = $metode === 'PIUTANG' ? max(0, $remaining - $jumlahBayar) : 0;
|
||||
$statusPembayaran = $sisaPiutang > 0 ? 'PIUTANG' : 'LUNAS';
|
||||
|
||||
$voucherUsageRows = [];
|
||||
if ($voucherAmount > 0) {
|
||||
foreach ($voucherList as $v) {
|
||||
$voucher = Voucher::query()
|
||||
->where('kode', $v['kode'])
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
if (!$voucher || !$voucher->aktif) {
|
||||
throw new \RuntimeException('VOUCHER_INVALID');
|
||||
}
|
||||
if ((int) $voucher->saldo < (int) $v['amount']) {
|
||||
throw new \RuntimeException('VOUCHER_SALDO');
|
||||
}
|
||||
$saldoBefore = (int) $voucher->saldo;
|
||||
$voucher->saldo = (int) $voucher->saldo - (int) $v['amount'];
|
||||
$voucher->save();
|
||||
$voucherUsageRows[] = [
|
||||
'voucher_id' => $voucher->id,
|
||||
'kode' => $voucher->kode,
|
||||
'tipe' => 'USE',
|
||||
'jumlah' => (int) $v['amount'],
|
||||
'saldo_before' => $saldoBefore,
|
||||
'saldo_after' => (int) $voucher->saldo,
|
||||
'penjualan_id' => null,
|
||||
'user_id' => $request->user()?->id,
|
||||
'catatan' => null,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$p = Penjualan::create([
|
||||
'user_id' => $request->user()?->id,
|
||||
'nomor_penjualan' => $nomor,
|
||||
'waktu' => now(),
|
||||
'subtotal' => $subtotal,
|
||||
'diskon' => $diskon,
|
||||
'pajak' => $pajak,
|
||||
'total' => $total,
|
||||
'metode_pembayaran' => $metode,
|
||||
'status_pembayaran' => $statusPembayaran,
|
||||
'voucher_kode' => $voucherKode,
|
||||
'voucher_amount' => $voucherAmount,
|
||||
'unit_piutang' => $metode === 'PIUTANG' ? $unitPiutang : null,
|
||||
'jumlah_bayar' => $jumlahBayar,
|
||||
'kembalian' => $kembalian,
|
||||
'sisa_piutang' => $sisaPiutang,
|
||||
]);
|
||||
|
||||
$barangRows = [];
|
||||
$movementRows = [];
|
||||
|
||||
foreach ($computed as $row) {
|
||||
/** @var \App\Models\Produk $produk */
|
||||
$produk = $row['produk'];
|
||||
$qty = (int) $row['qty'];
|
||||
$harga = (int) $row['harga'];
|
||||
$lineTotal = (int) $row['total'];
|
||||
|
||||
$barangRows[] = [
|
||||
'penjualan_id' => $p->id,
|
||||
'produk_id' => $produk->id,
|
||||
'jumlah' => $qty,
|
||||
'harga' => $harga,
|
||||
'total' => $lineTotal,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
];
|
||||
|
||||
$unit = $baseUnits->get($produk->id);
|
||||
$unitName = $unit?->nama_unit ?? 'PCS';
|
||||
$multiplier = (int) ($unit?->multiplier ?? 1);
|
||||
|
||||
$movementRows[] = [
|
||||
'waktu' => now(),
|
||||
'produk_id' => $produk->id,
|
||||
'dari_lokasi_id' => $lokasi->id,
|
||||
'ke_lokasi_id' => null,
|
||||
'jenis' => 'PENJUALAN',
|
||||
'jumlah' => $qty,
|
||||
'unit' => $unitName,
|
||||
'qty_input' => $qty,
|
||||
'multiplier' => $multiplier,
|
||||
'catatan' => "Penjualan {$nomor}",
|
||||
'referensi_tipe' => 'PENJUALAN',
|
||||
'referensi_id' => $p->id,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
];
|
||||
}
|
||||
|
||||
PenjualanBarang::insert($barangRows);
|
||||
PergerakanStok::insert($movementRows);
|
||||
|
||||
if ($voucherAmount > 0) {
|
||||
foreach ($voucherUsageRows as &$row) {
|
||||
$row['penjualan_id'] = $p->id;
|
||||
}
|
||||
unset($row);
|
||||
VoucherHistory::insert($voucherUsageRows);
|
||||
}
|
||||
|
||||
return $p;
|
||||
});
|
||||
} catch (\RuntimeException $e) {
|
||||
if ($e->getMessage() === 'VOUCHER_INVALID') {
|
||||
return response()->json([
|
||||
'message' => 'Voucher tidak valid atau tidak aktif.',
|
||||
], 422);
|
||||
}
|
||||
if ($e->getMessage() === 'VOUCHER_SALDO') {
|
||||
return response()->json([
|
||||
'message' => 'Saldo voucher tidak mencukupi.',
|
||||
], 422);
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'data' => $penjualan,
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function show(Penjualan $penjualan): JsonResponse
|
||||
{
|
||||
$penjualan->load(['barang.produk:id,nama,barcode']);
|
||||
|
||||
$items = $penjualan->barang->map(function (PenjualanBarang $b) {
|
||||
return [
|
||||
'produk_id' => $b->produk_id,
|
||||
'nama' => $b->produk?->nama,
|
||||
'barcode' => $b->produk?->barcode,
|
||||
'jumlah' => (int) $b->jumlah,
|
||||
'harga' => (int) $b->harga,
|
||||
'total' => (int) $b->total,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'id' => $penjualan->id,
|
||||
'nomor_penjualan' => $penjualan->nomor_penjualan,
|
||||
'waktu' => $penjualan->waktu,
|
||||
'subtotal' => (int) $penjualan->subtotal,
|
||||
'diskon' => (int) $penjualan->diskon,
|
||||
'pajak' => (int) $penjualan->pajak,
|
||||
'total' => (int) $penjualan->total,
|
||||
'metode_pembayaran' => $penjualan->metode_pembayaran,
|
||||
'status_pembayaran' => $penjualan->status_pembayaran ?? 'LUNAS',
|
||||
'voucher_kode' => $penjualan->voucher_kode,
|
||||
'voucher_amount' => (int) ($penjualan->voucher_amount ?? ($penjualan->voucher_kode ? $penjualan->total : 0)),
|
||||
'jumlah_bayar' => (int) $penjualan->jumlah_bayar,
|
||||
'kembalian' => (int) $penjualan->kembalian,
|
||||
'sisa_piutang' => (int) ($penjualan->sisa_piutang ?? 0),
|
||||
'items' => $items,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private function stokProdukDiLokasi(int $produkId, int $lokasiId): int
|
||||
{
|
||||
$in = (int) DB::table('pergerakan_stok')
|
||||
->where('produk_id', $produkId)
|
||||
->where('ke_lokasi_id', $lokasiId)
|
||||
->sum('jumlah');
|
||||
|
||||
$out = (int) DB::table('pergerakan_stok')
|
||||
->where('produk_id', $produkId)
|
||||
->where('dari_lokasi_id', $lokasiId)
|
||||
->sum('jumlah');
|
||||
|
||||
return $in - $out;
|
||||
}
|
||||
|
||||
private function generateNomor(): string
|
||||
{
|
||||
do {
|
||||
$nomor = 'PJ' . now()->format('ymdHis') . random_int(100, 999);
|
||||
} while (Penjualan::query()->where('nomor_penjualan', $nomor)->exists());
|
||||
|
||||
return $nomor;
|
||||
}
|
||||
}
|
||||
139
app/Http/Controllers/Admin/UserController.php
Normal file
139
app/Http/Controllers/Admin/UserController.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$limit = (int) $request->query('limit', 100);
|
||||
$limit = max(1, min(200, $limit));
|
||||
|
||||
$users = User::query()
|
||||
->with('pegawai:id,nama,nip')
|
||||
->orderByDesc('id')
|
||||
->limit($limit)
|
||||
->get(['id', 'name', 'email', 'role', 'nip', 'pegawai_id', 'created_at']);
|
||||
|
||||
return response()->json([
|
||||
'data' => $users->map(function (User $u) {
|
||||
return [
|
||||
'id' => $u->id,
|
||||
'name' => $u->name,
|
||||
'email' => $u->email,
|
||||
'role' => $u->role,
|
||||
'nip' => $u->nip,
|
||||
'pegawai_id' => $u->pegawai_id,
|
||||
'pegawai_nama' => $u->pegawai?->nama,
|
||||
'created_at' => $u->created_at,
|
||||
];
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'name' => ['nullable', 'string', 'max:100'],
|
||||
'email' => ['nullable', 'string', 'email', 'max:255', Rule::unique('users', 'email')],
|
||||
'password' => ['required', 'string', 'min:6'],
|
||||
'role' => ['nullable', 'string', Rule::in(['admin', 'kasir', 'gudang'])],
|
||||
'pegawai_id' => ['required', 'integer', 'exists:pegawai,id'],
|
||||
]);
|
||||
|
||||
$pegawai = \App\Models\Pegawai::find($data['pegawai_id']);
|
||||
if (!$pegawai) {
|
||||
return response()->json(['message' => 'Pegawai tidak ditemukan.'], 422);
|
||||
}
|
||||
|
||||
if (!$pegawai->nip) {
|
||||
return response()->json(['message' => 'Pegawai belum memiliki NIP.'], 422);
|
||||
}
|
||||
$nip = $pegawai->nip;
|
||||
if (User::query()->where('nip', $nip)->exists()) {
|
||||
return response()->json(['message' => 'NIP sudah digunakan.'], 422);
|
||||
}
|
||||
|
||||
$email = $data['email'] ?? null;
|
||||
if (!$email) {
|
||||
$email = $nip . '@pos.local';
|
||||
}
|
||||
|
||||
$user = User::create([
|
||||
'name' => $pegawai->nama ?? ($data['name'] ?? $nip),
|
||||
'email' => $email,
|
||||
'password' => Hash::make($data['password']),
|
||||
'role' => $data['role'] ?? User::ROLE_KASIR,
|
||||
'nip' => $nip,
|
||||
'pegawai_id' => $pegawai->id,
|
||||
]);
|
||||
|
||||
return response()->json(['data' => $user], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, User $user): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'name' => ['nullable', 'string', 'max:100'],
|
||||
'email' => ['nullable', 'string', 'email', 'max:255', Rule::unique('users', 'email')->ignore($user->id)],
|
||||
'password' => ['nullable', 'string', 'min:6'],
|
||||
'role' => ['nullable', 'string', Rule::in(['admin', 'kasir', 'gudang'])],
|
||||
'pegawai_id' => ['nullable', 'integer', 'exists:pegawai,id'],
|
||||
]);
|
||||
|
||||
$pegawai = null;
|
||||
if (!empty($data['pegawai_id'])) {
|
||||
$pegawai = \App\Models\Pegawai::find($data['pegawai_id']);
|
||||
}
|
||||
|
||||
$nip = $pegawai?->nip ?? $user->nip;
|
||||
if ($pegawai && !$pegawai->nip) {
|
||||
return response()->json(['message' => 'Pegawai belum memiliki NIP.'], 422);
|
||||
}
|
||||
if (!$nip) {
|
||||
return response()->json(['message' => 'NIP wajib diisi.'], 422);
|
||||
}
|
||||
if (User::query()->where('nip', $nip)->where('id', '!=', $user->id)->exists()) {
|
||||
return response()->json(['message' => 'NIP sudah digunakan.'], 422);
|
||||
}
|
||||
|
||||
$email = $data['email'] ?? $user->email;
|
||||
if (!$email) {
|
||||
$email = $nip . '@pos.local';
|
||||
}
|
||||
|
||||
$payload = [
|
||||
'name' => $pegawai?->nama ?? ($data['name'] ?? $user->name),
|
||||
'email' => $email,
|
||||
'role' => $data['role'] ?? $user->role ?? User::ROLE_KASIR,
|
||||
'nip' => $nip,
|
||||
'pegawai_id' => $pegawai?->id ?? null,
|
||||
];
|
||||
|
||||
if (!empty($data['password'])) {
|
||||
$payload['password'] = Hash::make($data['password']);
|
||||
}
|
||||
|
||||
$user->update($payload);
|
||||
|
||||
return response()->json(['data' => $user]);
|
||||
}
|
||||
|
||||
public function destroy(User $user): JsonResponse
|
||||
{
|
||||
if ($user->role === User::ROLE_ADMIN) {
|
||||
return response()->json(['message' => 'User admin tidak bisa dihapus.'], 422);
|
||||
}
|
||||
|
||||
$user->delete();
|
||||
|
||||
return response()->json(['message' => 'User berhasil dihapus.']);
|
||||
}
|
||||
}
|
||||
200
app/Http/Controllers/Admin/VoucherController.php
Normal file
200
app/Http/Controllers/Admin/VoucherController.php
Normal file
@@ -0,0 +1,200 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Voucher;
|
||||
use App\Models\VoucherHistory;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class VoucherController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'q' => ['nullable', 'string', 'max:100'],
|
||||
'limit' => ['nullable', 'integer', 'min:1', 'max:200'],
|
||||
]);
|
||||
|
||||
$limit = (int) ($data['limit'] ?? 100);
|
||||
|
||||
$query = Voucher::query()->with('pegawai:id,nama,nip')->orderByDesc('id');
|
||||
if (!empty($data['q'])) {
|
||||
$s = trim((string) $data['q']);
|
||||
$query->where('kode', 'like', "%{$s}%")
|
||||
->orWhere('catatan', 'like', "%{$s}%");
|
||||
}
|
||||
|
||||
$rows = $query->limit($limit)->get()->map(function (Voucher $v) {
|
||||
return [
|
||||
'id' => $v->id,
|
||||
'pegawai_id' => $v->pegawai_id,
|
||||
'pegawai_nama' => $v->pegawai?->nama,
|
||||
'pegawai_nip' => $v->pegawai?->nip,
|
||||
'kode' => $v->kode,
|
||||
'saldo' => (int) $v->saldo,
|
||||
'aktif' => (bool) $v->aktif,
|
||||
'catatan' => $v->catatan,
|
||||
'created_at' => $v->created_at,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json(['data' => $rows]);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'pegawai_id' => ['required', 'integer', 'exists:pegawai,id'],
|
||||
'kode' => ['nullable', 'string', 'max:255', 'unique:vouchers,kode'],
|
||||
'saldo' => ['nullable', 'integer', 'min:0'],
|
||||
'aktif' => ['nullable', 'boolean'],
|
||||
'catatan' => ['nullable', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$kode = trim((string) ($data['kode'] ?? ''));
|
||||
if ($kode === '') {
|
||||
$kode = $this->generateKode();
|
||||
}
|
||||
|
||||
$voucher = Voucher::create([
|
||||
'pegawai_id' => (int) $data['pegawai_id'],
|
||||
'kode' => $kode,
|
||||
'saldo' => (int) ($data['saldo'] ?? 0),
|
||||
'aktif' => isset($data['aktif']) ? (bool) $data['aktif'] : true,
|
||||
'catatan' => $data['catatan'] ?? null,
|
||||
'created_by' => $request->user()?->id,
|
||||
]);
|
||||
|
||||
return response()->json(['data' => $voucher->load('pegawai:id,nama,nip')], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, Voucher $voucher): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'saldo' => ['nullable', 'integer', 'min:0'],
|
||||
'add_saldo' => ['nullable', 'integer', 'min:0'],
|
||||
'aktif' => ['nullable', 'boolean'],
|
||||
'catatan' => ['nullable', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$updated = DB::transaction(function () use ($voucher, $data, $request) {
|
||||
$voucher = Voucher::query()->where('id', $voucher->id)->lockForUpdate()->firstOrFail();
|
||||
$saldoBefore = (int) $voucher->saldo;
|
||||
if (array_key_exists('add_saldo', $data) && $data['add_saldo'] !== null) {
|
||||
$voucher->saldo = (int) $voucher->saldo + (int) $data['add_saldo'];
|
||||
} elseif (array_key_exists('saldo', $data) && $data['saldo'] !== null) {
|
||||
$voucher->saldo = (int) $data['saldo'];
|
||||
}
|
||||
if (array_key_exists('aktif', $data)) {
|
||||
$voucher->aktif = (bool) $data['aktif'];
|
||||
}
|
||||
if (array_key_exists('catatan', $data)) {
|
||||
$voucher->catatan = $data['catatan'];
|
||||
}
|
||||
$voucher->save();
|
||||
if (array_key_exists('add_saldo', $data) && $data['add_saldo'] !== null) {
|
||||
VoucherHistory::create([
|
||||
'voucher_id' => $voucher->id,
|
||||
'kode' => $voucher->kode,
|
||||
'tipe' => 'TOPUP',
|
||||
'jumlah' => (int) $data['add_saldo'],
|
||||
'saldo_before' => $saldoBefore,
|
||||
'saldo_after' => (int) $voucher->saldo,
|
||||
'penjualan_id' => null,
|
||||
'user_id' => $request->user()?->id,
|
||||
'catatan' => 'Top up',
|
||||
]);
|
||||
}
|
||||
return $voucher->fresh()->load('pegawai:id,nama,nip');
|
||||
});
|
||||
|
||||
return response()->json(['data' => $updated]);
|
||||
}
|
||||
|
||||
public function history(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'from' => ['nullable', 'date'],
|
||||
'to' => ['nullable', 'date'],
|
||||
'q' => ['nullable', 'string', 'max:100'],
|
||||
'tipe' => ['nullable', 'string', 'max:20'],
|
||||
'limit' => ['nullable', 'integer', 'min:1', 'max:200'],
|
||||
]);
|
||||
|
||||
$limit = (int) ($data['limit'] ?? 100);
|
||||
|
||||
$query = DB::table('voucher_histories as h')
|
||||
->leftJoin('penjualan as p', 'p.id', '=', 'h.penjualan_id')
|
||||
->select([
|
||||
'h.id',
|
||||
'h.voucher_id',
|
||||
'h.kode',
|
||||
'h.tipe',
|
||||
'h.jumlah',
|
||||
'h.saldo_before',
|
||||
'h.saldo_after',
|
||||
'h.penjualan_id',
|
||||
'p.nomor_penjualan',
|
||||
'h.user_id',
|
||||
'h.catatan',
|
||||
'h.created_at',
|
||||
])
|
||||
->orderByDesc('h.id');
|
||||
|
||||
if (!empty($data['from'])) {
|
||||
$query->where('h.created_at', '>=', $data['from'] . ' 00:00:00');
|
||||
}
|
||||
if (!empty($data['to'])) {
|
||||
$query->where('h.created_at', '<=', $data['to'] . ' 23:59:59');
|
||||
}
|
||||
if (!empty($data['q'])) {
|
||||
$s = trim((string) $data['q']);
|
||||
$query->where('h.kode', 'like', "%{$s}%");
|
||||
}
|
||||
if (!empty($data['tipe'])) {
|
||||
$query->where('h.tipe', $data['tipe']);
|
||||
}
|
||||
|
||||
$rows = $query->limit($limit)->get();
|
||||
|
||||
return response()->json(['data' => $rows]);
|
||||
}
|
||||
|
||||
public function lookup(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'kode' => ['required', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$kode = trim((string) $data['kode']);
|
||||
if ($kode === '') {
|
||||
return response()->json(['message' => 'Kode voucher wajib diisi.'], 422);
|
||||
}
|
||||
|
||||
$voucher = Voucher::query()->where('kode', $kode)->first();
|
||||
if (!$voucher) {
|
||||
return response()->json(['message' => 'Voucher tidak ditemukan.'], 404);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'id' => $voucher->id,
|
||||
'kode' => $voucher->kode,
|
||||
'saldo' => (int) $voucher->saldo,
|
||||
'aktif' => (bool) $voucher->aktif,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private function generateKode(): string
|
||||
{
|
||||
do {
|
||||
$kode = 'VCH' . now()->format('ymd') . random_int(1000, 9999);
|
||||
} while (Voucher::query()->where('kode', $kode)->exists());
|
||||
|
||||
return $kode;
|
||||
}
|
||||
}
|
||||
49
app/Http/Controllers/Auth/AuthenticatedSessionController.php
Normal file
49
app/Http/Controllers/Auth/AuthenticatedSessionController.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Auth\LoginRequest;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class AuthenticatedSessionController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the login view.
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
return view('auth.login');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming authentication request.
|
||||
*/
|
||||
public function store(LoginRequest $request): RedirectResponse
|
||||
{
|
||||
$request->authenticate();
|
||||
|
||||
$request->session()->regenerate();
|
||||
|
||||
return redirect()->intended(RouteServiceProvider::HOME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy an authenticated session.
|
||||
*/
|
||||
public function destroy(Request $request): RedirectResponse
|
||||
{
|
||||
Auth::guard('web')->logout();
|
||||
|
||||
$request->session()->invalidate();
|
||||
|
||||
$request->session()->regenerateToken();
|
||||
|
||||
// Setelah logout balik ke login
|
||||
return redirect()->route('login');
|
||||
}
|
||||
}
|
||||
41
app/Http/Controllers/Auth/ConfirmablePasswordController.php
Normal file
41
app/Http/Controllers/Auth/ConfirmablePasswordController.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class ConfirmablePasswordController extends Controller
|
||||
{
|
||||
/**
|
||||
* Show the confirm password view.
|
||||
*/
|
||||
public function show(): View
|
||||
{
|
||||
return view('auth.confirm-password');
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm the user's password.
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
if (! Auth::guard('web')->validate([
|
||||
'email' => $request->user()->email,
|
||||
'password' => $request->password,
|
||||
])) {
|
||||
throw ValidationException::withMessages([
|
||||
'password' => __('auth.password'),
|
||||
]);
|
||||
}
|
||||
|
||||
$request->session()->put('auth.password_confirmed_at', time());
|
||||
|
||||
return redirect()->intended(RouteServiceProvider::HOME);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class EmailVerificationNotificationController extends Controller
|
||||
{
|
||||
/**
|
||||
* Send a new email verification notification.
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
if ($request->user()->hasVerifiedEmail()) {
|
||||
return redirect()->intended(RouteServiceProvider::HOME);
|
||||
}
|
||||
|
||||
$request->user()->sendEmailVerificationNotification();
|
||||
|
||||
return back()->with('status', 'verification-link-sent');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class EmailVerificationPromptController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the email verification prompt.
|
||||
*/
|
||||
public function __invoke(Request $request): RedirectResponse|View
|
||||
{
|
||||
return $request->user()->hasVerifiedEmail()
|
||||
? redirect()->intended(RouteServiceProvider::HOME)
|
||||
: view('auth.verify-email');
|
||||
}
|
||||
}
|
||||
61
app/Http/Controllers/Auth/NewPasswordController.php
Normal file
61
app/Http/Controllers/Auth/NewPasswordController.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Auth\Events\PasswordReset;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Password;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rules;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class NewPasswordController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the password reset view.
|
||||
*/
|
||||
public function create(Request $request): View
|
||||
{
|
||||
return view('auth.reset-password', ['request' => $request]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming new password request.
|
||||
*
|
||||
* @throws \Illuminate\Validation\ValidationException
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$request->validate([
|
||||
'token' => ['required'],
|
||||
'email' => ['required', 'email'],
|
||||
'password' => ['required', 'confirmed', Rules\Password::defaults()],
|
||||
]);
|
||||
|
||||
// Here we will attempt to reset the user's password. If it is successful we
|
||||
// will update the password on an actual user model and persist it to the
|
||||
// database. Otherwise we will parse the error and return the response.
|
||||
$status = Password::reset(
|
||||
$request->only('email', 'password', 'password_confirmation', 'token'),
|
||||
function ($user) use ($request) {
|
||||
$user->forceFill([
|
||||
'password' => Hash::make($request->password),
|
||||
'remember_token' => Str::random(60),
|
||||
])->save();
|
||||
|
||||
event(new PasswordReset($user));
|
||||
}
|
||||
);
|
||||
|
||||
// If the password was successfully reset, we will redirect the user back to
|
||||
// the application's home authenticated view. If there is an error we can
|
||||
// redirect them back to where they came from with their error message.
|
||||
return $status == Password::PASSWORD_RESET
|
||||
? redirect()->route('login')->with('status', __($status))
|
||||
: back()->withInput($request->only('email'))
|
||||
->withErrors(['email' => __($status)]);
|
||||
}
|
||||
}
|
||||
29
app/Http/Controllers/Auth/PasswordController.php
Normal file
29
app/Http/Controllers/Auth/PasswordController.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rules\Password;
|
||||
|
||||
class PasswordController extends Controller
|
||||
{
|
||||
/**
|
||||
* Update the user's password.
|
||||
*/
|
||||
public function update(Request $request): RedirectResponse
|
||||
{
|
||||
$validated = $request->validateWithBag('updatePassword', [
|
||||
'current_password' => ['required', 'current_password'],
|
||||
'password' => ['required', Password::defaults(), 'confirmed'],
|
||||
]);
|
||||
|
||||
$request->user()->update([
|
||||
'password' => Hash::make($validated['password']),
|
||||
]);
|
||||
|
||||
return back()->with('status', 'password-updated');
|
||||
}
|
||||
}
|
||||
44
app/Http/Controllers/Auth/PasswordResetLinkController.php
Normal file
44
app/Http/Controllers/Auth/PasswordResetLinkController.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Password;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class PasswordResetLinkController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the password reset link request view.
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
return view('auth.forgot-password');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming password reset link request.
|
||||
*
|
||||
* @throws \Illuminate\Validation\ValidationException
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$request->validate([
|
||||
'email' => ['required', 'email'],
|
||||
]);
|
||||
|
||||
// We will send the password reset link to this user. Once we have attempted
|
||||
// to send the link, we will examine the response then see the message we
|
||||
// need to show to the user. Finally, we'll send out a proper response.
|
||||
$status = Password::sendResetLink(
|
||||
$request->only('email')
|
||||
);
|
||||
|
||||
return $status == Password::RESET_LINK_SENT
|
||||
? back()->with('status', __($status))
|
||||
: back()->withInput($request->only('email'))
|
||||
->withErrors(['email' => __($status)]);
|
||||
}
|
||||
}
|
||||
51
app/Http/Controllers/Auth/RegisteredUserController.php
Normal file
51
app/Http/Controllers/Auth/RegisteredUserController.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rules;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class RegisteredUserController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the registration view.
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
return view('auth.register');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming registration request.
|
||||
*
|
||||
* @throws \Illuminate\Validation\ValidationException
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$request->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:'.User::class],
|
||||
'password' => ['required', 'confirmed', Rules\Password::defaults()],
|
||||
]);
|
||||
|
||||
$user = User::create([
|
||||
'name' => $request->name,
|
||||
'email' => $request->email,
|
||||
'password' => Hash::make($request->password),
|
||||
]);
|
||||
|
||||
event(new Registered($user));
|
||||
|
||||
Auth::login($user);
|
||||
|
||||
return redirect(RouteServiceProvider::HOME);
|
||||
}
|
||||
}
|
||||
28
app/Http/Controllers/Auth/VerifyEmailController.php
Normal file
28
app/Http/Controllers/Auth/VerifyEmailController.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Illuminate\Auth\Events\Verified;
|
||||
use Illuminate\Foundation\Auth\EmailVerificationRequest;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
|
||||
class VerifyEmailController extends Controller
|
||||
{
|
||||
/**
|
||||
* Mark the authenticated user's email address as verified.
|
||||
*/
|
||||
public function __invoke(EmailVerificationRequest $request): RedirectResponse
|
||||
{
|
||||
if ($request->user()->hasVerifiedEmail()) {
|
||||
return redirect()->intended(RouteServiceProvider::HOME.'?verified=1');
|
||||
}
|
||||
|
||||
if ($request->user()->markEmailAsVerified()) {
|
||||
event(new Verified($request->user()));
|
||||
}
|
||||
|
||||
return redirect()->intended(RouteServiceProvider::HOME.'?verified=1');
|
||||
}
|
||||
}
|
||||
12
app/Http/Controllers/Controller.php
Normal file
12
app/Http/Controllers/Controller.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
|
||||
class Controller extends BaseController
|
||||
{
|
||||
use AuthorizesRequests, ValidatesRequests;
|
||||
}
|
||||
60
app/Http/Controllers/ProfileController.php
Normal file
60
app/Http/Controllers/ProfileController.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\ProfileUpdateRequest;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Redirect;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class ProfileController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the user's profile form.
|
||||
*/
|
||||
public function edit(Request $request): View
|
||||
{
|
||||
return view('profile.edit', [
|
||||
'user' => $request->user(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the user's profile information.
|
||||
*/
|
||||
public function update(ProfileUpdateRequest $request): RedirectResponse
|
||||
{
|
||||
$request->user()->fill($request->validated());
|
||||
|
||||
if ($request->user()->isDirty('email')) {
|
||||
$request->user()->email_verified_at = null;
|
||||
}
|
||||
|
||||
$request->user()->save();
|
||||
|
||||
return Redirect::route('profile.edit')->with('status', 'profile-updated');
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the user's account.
|
||||
*/
|
||||
public function destroy(Request $request): RedirectResponse
|
||||
{
|
||||
$request->validateWithBag('userDeletion', [
|
||||
'password' => ['required', 'current_password'],
|
||||
]);
|
||||
|
||||
$user = $request->user();
|
||||
|
||||
Auth::logout();
|
||||
|
||||
$user->delete();
|
||||
|
||||
$request->session()->invalidate();
|
||||
$request->session()->regenerateToken();
|
||||
|
||||
return Redirect::to('/');
|
||||
}
|
||||
}
|
||||
69
app/Http/Kernel.php
Normal file
69
app/Http/Kernel.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http;
|
||||
|
||||
use Illuminate\Foundation\Http\Kernel as HttpKernel;
|
||||
|
||||
class Kernel extends HttpKernel
|
||||
{
|
||||
/**
|
||||
* The application's global HTTP middleware stack.
|
||||
*
|
||||
* These middleware are run during every request to your application.
|
||||
*
|
||||
* @var array<int, class-string|string>
|
||||
*/
|
||||
protected $middleware = [
|
||||
// \App\Http\Middleware\TrustHosts::class,
|
||||
\App\Http\Middleware\TrustProxies::class,
|
||||
\Illuminate\Http\Middleware\HandleCors::class,
|
||||
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
|
||||
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
|
||||
\App\Http\Middleware\TrimStrings::class,
|
||||
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* The application's route middleware groups.
|
||||
*
|
||||
* @var array<string, array<int, class-string|string>>
|
||||
*/
|
||||
protected $middlewareGroups = [
|
||||
'web' => [
|
||||
\App\Http\Middleware\EncryptCookies::class,
|
||||
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
|
||||
\Illuminate\Session\Middleware\StartSession::class,
|
||||
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
|
||||
\App\Http\Middleware\VerifyCsrfToken::class,
|
||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||
],
|
||||
|
||||
'api' => [
|
||||
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
|
||||
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
|
||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* The application's middleware aliases.
|
||||
*
|
||||
* Aliases may be used instead of class names to conveniently assign middleware to routes and groups.
|
||||
*
|
||||
* @var array<string, class-string|string>
|
||||
*/
|
||||
protected $middlewareAliases = [
|
||||
'auth' => \App\Http\Middleware\Authenticate::class,
|
||||
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
|
||||
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
|
||||
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
|
||||
'can' => \Illuminate\Auth\Middleware\Authorize::class,
|
||||
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
|
||||
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
|
||||
'precognitive' => \Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests::class,
|
||||
'role' => \App\Http\Middleware\RoleMiddleware::class,
|
||||
'signed' => \App\Http\Middleware\ValidateSignature::class,
|
||||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
||||
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
|
||||
];
|
||||
}
|
||||
17
app/Http/Middleware/Authenticate.php
Normal file
17
app/Http/Middleware/Authenticate.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Auth\Middleware\Authenticate as Middleware;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class Authenticate extends Middleware
|
||||
{
|
||||
/**
|
||||
* Get the path the user should be redirected to when they are not authenticated.
|
||||
*/
|
||||
protected function redirectTo(Request $request): ?string
|
||||
{
|
||||
return $request->expectsJson() ? null : '/admin/signin';
|
||||
}
|
||||
}
|
||||
17
app/Http/Middleware/EncryptCookies.php
Normal file
17
app/Http/Middleware/EncryptCookies.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;
|
||||
|
||||
class EncryptCookies extends Middleware
|
||||
{
|
||||
/**
|
||||
* The names of the cookies that should not be encrypted.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $except = [
|
||||
//
|
||||
];
|
||||
}
|
||||
17
app/Http/Middleware/PreventRequestsDuringMaintenance.php
Normal file
17
app/Http/Middleware/PreventRequestsDuringMaintenance.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance as Middleware;
|
||||
|
||||
class PreventRequestsDuringMaintenance extends Middleware
|
||||
{
|
||||
/**
|
||||
* The URIs that should be reachable while maintenance mode is enabled.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $except = [
|
||||
//
|
||||
];
|
||||
}
|
||||
30
app/Http/Middleware/RedirectIfAuthenticated.php
Normal file
30
app/Http/Middleware/RedirectIfAuthenticated.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class RedirectIfAuthenticated
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next, string ...$guards): Response
|
||||
{
|
||||
$guards = empty($guards) ? [null] : $guards;
|
||||
|
||||
foreach ($guards as $guard) {
|
||||
if (Auth::guard($guard)->check()) {
|
||||
return redirect(RouteServiceProvider::HOME);
|
||||
}
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
34
app/Http/Middleware/RoleMiddleware.php
Normal file
34
app/Http/Middleware/RoleMiddleware.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class RoleMiddleware
|
||||
{
|
||||
/**
|
||||
* Allow access for specific roles. Admin is always allowed.
|
||||
*
|
||||
* Usage: ->middleware('role:admin,kasir')
|
||||
*/
|
||||
public function handle(Request $request, Closure $next, ...$roles): Response
|
||||
{
|
||||
$user = $request->user();
|
||||
$role = $user?->role ?: 'admin';
|
||||
|
||||
if ($role === 'admin') {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
if (empty($roles) || !in_array($role, $roles, true)) {
|
||||
if ($request->expectsJson()) {
|
||||
return response()->json(['message' => 'Akses ditolak.'], 403);
|
||||
}
|
||||
abort(403, 'Akses ditolak.');
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
19
app/Http/Middleware/TrimStrings.php
Normal file
19
app/Http/Middleware/TrimStrings.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Foundation\Http\Middleware\TrimStrings as Middleware;
|
||||
|
||||
class TrimStrings extends Middleware
|
||||
{
|
||||
/**
|
||||
* The names of the attributes that should not be trimmed.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $except = [
|
||||
'current_password',
|
||||
'password',
|
||||
'password_confirmation',
|
||||
];
|
||||
}
|
||||
20
app/Http/Middleware/TrustHosts.php
Normal file
20
app/Http/Middleware/TrustHosts.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Http\Middleware\TrustHosts as Middleware;
|
||||
|
||||
class TrustHosts extends Middleware
|
||||
{
|
||||
/**
|
||||
* Get the host patterns that should be trusted.
|
||||
*
|
||||
* @return array<int, string|null>
|
||||
*/
|
||||
public function hosts(): array
|
||||
{
|
||||
return [
|
||||
$this->allSubdomainsOfApplicationUrl(),
|
||||
];
|
||||
}
|
||||
}
|
||||
28
app/Http/Middleware/TrustProxies.php
Normal file
28
app/Http/Middleware/TrustProxies.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Http\Middleware\TrustProxies as Middleware;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class TrustProxies extends Middleware
|
||||
{
|
||||
/**
|
||||
* The trusted proxies for this application.
|
||||
*
|
||||
* @var array<int, string>|string|null
|
||||
*/
|
||||
protected $proxies;
|
||||
|
||||
/**
|
||||
* The headers that should be used to detect proxies.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $headers =
|
||||
Request::HEADER_X_FORWARDED_FOR |
|
||||
Request::HEADER_X_FORWARDED_HOST |
|
||||
Request::HEADER_X_FORWARDED_PORT |
|
||||
Request::HEADER_X_FORWARDED_PROTO |
|
||||
Request::HEADER_X_FORWARDED_AWS_ELB;
|
||||
}
|
||||
22
app/Http/Middleware/ValidateSignature.php
Normal file
22
app/Http/Middleware/ValidateSignature.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Routing\Middleware\ValidateSignature as Middleware;
|
||||
|
||||
class ValidateSignature extends Middleware
|
||||
{
|
||||
/**
|
||||
* The names of the query string parameters that should be ignored.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $except = [
|
||||
// 'fbclid',
|
||||
// 'utm_campaign',
|
||||
// 'utm_content',
|
||||
// 'utm_medium',
|
||||
// 'utm_source',
|
||||
// 'utm_term',
|
||||
];
|
||||
}
|
||||
17
app/Http/Middleware/VerifyCsrfToken.php
Normal file
17
app/Http/Middleware/VerifyCsrfToken.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
|
||||
|
||||
class VerifyCsrfToken extends Middleware
|
||||
{
|
||||
/**
|
||||
* The URIs that should be excluded from CSRF verification.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $except = [
|
||||
//
|
||||
];
|
||||
}
|
||||
85
app/Http/Requests/Auth/LoginRequest.php
Normal file
85
app/Http/Requests/Auth/LoginRequest.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Auth;
|
||||
|
||||
use Illuminate\Auth\Events\Lockout;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class LoginRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'nip' => ['required', 'string'],
|
||||
'password' => ['required', 'string'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to authenticate the request's credentials.
|
||||
*
|
||||
* @throws \Illuminate\Validation\ValidationException
|
||||
*/
|
||||
public function authenticate(): void
|
||||
{
|
||||
$this->ensureIsNotRateLimited();
|
||||
|
||||
if (! Auth::attempt($this->only('nip', 'password'), $this->boolean('remember'))) {
|
||||
RateLimiter::hit($this->throttleKey());
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'nip' => trans('auth.failed'),
|
||||
]);
|
||||
}
|
||||
|
||||
RateLimiter::clear($this->throttleKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the login request is not rate limited.
|
||||
*
|
||||
* @throws \Illuminate\Validation\ValidationException
|
||||
*/
|
||||
public function ensureIsNotRateLimited(): void
|
||||
{
|
||||
if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
|
||||
return;
|
||||
}
|
||||
|
||||
event(new Lockout($this));
|
||||
|
||||
$seconds = RateLimiter::availableIn($this->throttleKey());
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'nip' => trans('auth.throttle', [
|
||||
'seconds' => $seconds,
|
||||
'minutes' => ceil($seconds / 60),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the rate limiting throttle key for the request.
|
||||
*/
|
||||
public function throttleKey(): string
|
||||
{
|
||||
return Str::transliterate(Str::lower($this->string('nip')).'|'.$this->ip());
|
||||
}
|
||||
}
|
||||
23
app/Http/Requests/ProfileUpdateRequest.php
Normal file
23
app/Http/Requests/ProfileUpdateRequest.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class ProfileUpdateRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', Rule::unique(User::class)->ignore($this->user()->id)],
|
||||
];
|
||||
}
|
||||
}
|
||||
16
app/Models/Distributor.php
Normal file
16
app/Models/Distributor.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Distributor extends Model
|
||||
{
|
||||
protected $table = 'distributors';
|
||||
|
||||
protected $fillable = [
|
||||
'nama',
|
||||
'alamat',
|
||||
'telepon',
|
||||
];
|
||||
}
|
||||
15
app/Models/Lokasi.php
Normal file
15
app/Models/Lokasi.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Lokasi extends Model
|
||||
{
|
||||
protected $table = 'lokasi';
|
||||
|
||||
protected $fillable = [
|
||||
'kode',
|
||||
'nama'
|
||||
];
|
||||
}
|
||||
25
app/Models/Pegawai.php
Normal file
25
app/Models/Pegawai.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Pegawai extends Model
|
||||
{
|
||||
protected $table = 'pegawai';
|
||||
|
||||
protected $fillable = [
|
||||
'nip',
|
||||
'nama',
|
||||
'jabatan',
|
||||
'telepon',
|
||||
'alamat',
|
||||
'tempat_lahir',
|
||||
'tanggal_lahir',
|
||||
'jenis_kelamin',
|
||||
'kota',
|
||||
'nik',
|
||||
'foto',
|
||||
'is_aktif',
|
||||
];
|
||||
}
|
||||
51
app/Models/PenerimaanBarang.php
Normal file
51
app/Models/PenerimaanBarang.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class PenerimaanBarang extends Model
|
||||
{
|
||||
protected $table = 'penerimaan_barang';
|
||||
|
||||
protected $fillable = [
|
||||
'nomor_penerimaan',
|
||||
'no_faktur',
|
||||
'tanggal_faktur',
|
||||
'tanggal_datang',
|
||||
'distributor_id',
|
||||
'lokasi_id',
|
||||
'user_id',
|
||||
'status',
|
||||
'catatan',
|
||||
'applied_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'tanggal_faktur' => 'date',
|
||||
'tanggal_datang' => 'date',
|
||||
'applied_at' => 'datetime',
|
||||
];
|
||||
|
||||
public function items(): HasMany
|
||||
{
|
||||
return $this->hasMany(PenerimaanBarangItem::class, 'penerimaan_id');
|
||||
}
|
||||
|
||||
public function distributor(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Distributor::class, 'distributor_id');
|
||||
}
|
||||
|
||||
public function lokasi(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Lokasi::class, 'lokasi_id');
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
}
|
||||
32
app/Models/PenerimaanBarangItem.php
Normal file
32
app/Models/PenerimaanBarangItem.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class PenerimaanBarangItem extends Model
|
||||
{
|
||||
protected $table = 'penerimaan_barang_items';
|
||||
|
||||
protected $fillable = [
|
||||
'penerimaan_id',
|
||||
'produk_id',
|
||||
'unit_id',
|
||||
'qty',
|
||||
'qty_input',
|
||||
'harga',
|
||||
'unit',
|
||||
'multiplier',
|
||||
];
|
||||
|
||||
public function penerimaan(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(PenerimaanBarang::class, 'penerimaan_id');
|
||||
}
|
||||
|
||||
public function produk(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Produk::class, 'produk_id');
|
||||
}
|
||||
}
|
||||
39
app/Models/Penjualan.php
Normal file
39
app/Models/Penjualan.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Models\User;
|
||||
|
||||
class Penjualan extends Model
|
||||
{
|
||||
protected $table = 'penjualan';
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'nomor_penjualan',
|
||||
'waktu',
|
||||
'subtotal',
|
||||
'diskon',
|
||||
'pajak',
|
||||
'total',
|
||||
'metode_pembayaran',
|
||||
'status_pembayaran',
|
||||
'voucher_kode',
|
||||
'voucher_amount',
|
||||
'unit_piutang',
|
||||
'jumlah_bayar',
|
||||
'kembalian',
|
||||
'sisa_piutang',
|
||||
];
|
||||
|
||||
public function barang()
|
||||
{
|
||||
return $this->hasMany(PenjualanBarang::class, 'penjualan_id');
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
}
|
||||
24
app/Models/PenjualanBarang.php
Normal file
24
app/Models/PenjualanBarang.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Models\Produk;
|
||||
|
||||
class PenjualanBarang extends Model
|
||||
{
|
||||
protected $table = 'penjualan_barang';
|
||||
|
||||
protected $fillable = [
|
||||
'penjualan_id',
|
||||
'produk_id',
|
||||
'jumlah',
|
||||
'harga',
|
||||
'total'
|
||||
];
|
||||
|
||||
public function produk()
|
||||
{
|
||||
return $this->belongsTo(Produk::class, 'produk_id');
|
||||
}
|
||||
}
|
||||
40
app/Models/PergerakanStok.php
Normal file
40
app/Models/PergerakanStok.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class PergerakanStok extends Model
|
||||
{
|
||||
protected $table = 'pergerakan_stok';
|
||||
|
||||
protected $fillable = [
|
||||
'waktu',
|
||||
'produk_id',
|
||||
'dari_lokasi_id',
|
||||
'ke_lokasi_id',
|
||||
'jenis',
|
||||
'jumlah',
|
||||
'unit',
|
||||
'qty_input',
|
||||
'multiplier',
|
||||
'catatan',
|
||||
'referensi_tipe',
|
||||
'referensi_id'
|
||||
];
|
||||
|
||||
public function produk()
|
||||
{
|
||||
return $this->belongsTo(\App\Models\Produk::class, 'produk_id');
|
||||
}
|
||||
|
||||
public function dariLokasi()
|
||||
{
|
||||
return $this->belongsTo(\App\Models\Lokasi::class, 'dari_lokasi_id');
|
||||
}
|
||||
|
||||
public function keLokasi()
|
||||
{
|
||||
return $this->belongsTo(\App\Models\Lokasi::class, 'ke_lokasi_id');
|
||||
}
|
||||
}
|
||||
20
app/Models/PiutangPayment.php
Normal file
20
app/Models/PiutangPayment.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class PiutangPayment extends Model
|
||||
{
|
||||
protected $table = 'piutang_payments';
|
||||
|
||||
protected $fillable = [
|
||||
'penjualan_id',
|
||||
'jumlah',
|
||||
'metode',
|
||||
'catatan',
|
||||
'user_id',
|
||||
'sisa_before',
|
||||
'sisa_after',
|
||||
];
|
||||
}
|
||||
22
app/Models/ProductUnit.php
Normal file
22
app/Models/ProductUnit.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ProductUnit extends Model
|
||||
{
|
||||
protected $table = 'produk_unit';
|
||||
|
||||
protected $fillable = [
|
||||
'produk_id',
|
||||
'nama_unit',
|
||||
'multiplier',
|
||||
'is_base',
|
||||
];
|
||||
|
||||
public function produk()
|
||||
{
|
||||
return $this->belongsTo(Produk::class);
|
||||
}
|
||||
}
|
||||
27
app/Models/Produk.php
Normal file
27
app/Models/Produk.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Models\ProductUnit;
|
||||
|
||||
class Produk extends Model
|
||||
{
|
||||
protected $table = 'produk';
|
||||
|
||||
protected $fillable = [
|
||||
'sku',
|
||||
'nama',
|
||||
'barcode',
|
||||
'harga_beli',
|
||||
'harga_jual',
|
||||
'is_aktif',
|
||||
];
|
||||
|
||||
public function units() {
|
||||
return $this->hasMany(ProductUnit::class, 'produk_id');
|
||||
}
|
||||
public function baseUnit() {
|
||||
return $this->hasOne(ProductUnit::class, 'produk_id')->where('is_base', true);
|
||||
}
|
||||
}
|
||||
15
app/Models/Setting.php
Normal file
15
app/Models/Setting.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Setting extends Model
|
||||
{
|
||||
protected $table = 'settings';
|
||||
|
||||
protected $fillable = [
|
||||
'key',
|
||||
'value',
|
||||
];
|
||||
}
|
||||
62
app/Models/User.php
Normal file
62
app/Models/User.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use HasApiTokens, HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
'role',
|
||||
'nip',
|
||||
'pegawai_id',
|
||||
];
|
||||
|
||||
public const ROLE_ADMIN = 'admin';
|
||||
public const ROLE_KASIR = 'kasir';
|
||||
public const ROLE_GUDANG = 'gudang';
|
||||
|
||||
public function isAdmin(): bool
|
||||
{
|
||||
return $this->role === self::ROLE_ADMIN;
|
||||
}
|
||||
|
||||
public function pegawai()
|
||||
{
|
||||
return $this->belongsTo(Pegawai::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast.
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected $casts = [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
25
app/Models/Voucher.php
Normal file
25
app/Models/Voucher.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Voucher extends Model
|
||||
{
|
||||
protected $table = 'vouchers';
|
||||
|
||||
protected $fillable = [
|
||||
'pegawai_id',
|
||||
'kode',
|
||||
'saldo',
|
||||
'aktif',
|
||||
'catatan',
|
||||
'created_by',
|
||||
];
|
||||
|
||||
public function pegawai(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Pegawai::class, 'pegawai_id');
|
||||
}
|
||||
}
|
||||
22
app/Models/VoucherHistory.php
Normal file
22
app/Models/VoucherHistory.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class VoucherHistory extends Model
|
||||
{
|
||||
protected $table = 'voucher_histories';
|
||||
|
||||
protected $fillable = [
|
||||
'voucher_id',
|
||||
'kode',
|
||||
'tipe',
|
||||
'jumlah',
|
||||
'saldo_before',
|
||||
'saldo_after',
|
||||
'penjualan_id',
|
||||
'user_id',
|
||||
'catatan',
|
||||
];
|
||||
}
|
||||
24
app/Providers/AppServiceProvider.php
Normal file
24
app/Providers/AppServiceProvider.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
26
app/Providers/AuthServiceProvider.php
Normal file
26
app/Providers/AuthServiceProvider.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
// use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
|
||||
|
||||
class AuthServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* The model to policy mappings for the application.
|
||||
*
|
||||
* @var array<class-string, class-string>
|
||||
*/
|
||||
protected $policies = [
|
||||
//
|
||||
];
|
||||
|
||||
/**
|
||||
* Register any authentication / authorization services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
19
app/Providers/BroadcastServiceProvider.php
Normal file
19
app/Providers/BroadcastServiceProvider.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Broadcast;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class BroadcastServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
Broadcast::routes();
|
||||
|
||||
require base_path('routes/channels.php');
|
||||
}
|
||||
}
|
||||
38
app/Providers/EventServiceProvider.php
Normal file
38
app/Providers/EventServiceProvider.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
|
||||
class EventServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* The event to listener mappings for the application.
|
||||
*
|
||||
* @var array<class-string, array<int, class-string>>
|
||||
*/
|
||||
protected $listen = [
|
||||
Registered::class => [
|
||||
SendEmailVerificationNotification::class,
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Register any events for your application.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if events and listeners should be automatically discovered.
|
||||
*/
|
||||
public function shouldDiscoverEvents(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
41
app/Providers/RouteServiceProvider.php
Normal file
41
app/Providers/RouteServiceProvider.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Cache\RateLimiting\Limit;
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
class RouteServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* The path to your application's "home" route.
|
||||
*
|
||||
* Typically, users are redirected here after authentication.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
// Setelah login arahkan ke halaman admin (React)
|
||||
public const HOME = '/admin';
|
||||
|
||||
/**
|
||||
* Define your route model bindings, pattern filters, and other route configuration.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
RateLimiter::for('api', function (Request $request) {
|
||||
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
|
||||
});
|
||||
|
||||
$this->routes(function () {
|
||||
Route::middleware('api')
|
||||
->prefix('api')
|
||||
->group(base_path('routes/api.php'));
|
||||
|
||||
Route::middleware('web')
|
||||
->group(base_path('routes/web.php'));
|
||||
});
|
||||
}
|
||||
}
|
||||
17
app/View/Components/AppLayout.php
Normal file
17
app/View/Components/AppLayout.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\View\Components;
|
||||
|
||||
use Illuminate\View\Component;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class AppLayout extends Component
|
||||
{
|
||||
/**
|
||||
* Get the view / contents that represents the component.
|
||||
*/
|
||||
public function render(): View
|
||||
{
|
||||
return view('layouts.app');
|
||||
}
|
||||
}
|
||||
17
app/View/Components/GuestLayout.php
Normal file
17
app/View/Components/GuestLayout.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\View\Components;
|
||||
|
||||
use Illuminate\View\Component;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class GuestLayout extends Component
|
||||
{
|
||||
/**
|
||||
* Get the view / contents that represents the component.
|
||||
*/
|
||||
public function render(): View
|
||||
{
|
||||
return view('layouts.guest');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user