377 lines
13 KiB
PHP
377 lines
13 KiB
PHP
<?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',
|
|
]);
|
|
}
|
|
}
|