- 부가세관리: 신고기간 1P/1C/2P/2C 형식, 세금구분(과세/영세/면세), 카드 공제분 매입 반영, 라벨 변경 - 매출관리: 작성일자/승인번호 라벨, 구분(과세/영세/면세) 추가 - 미지급금: 결제예정일/거래일자 라벨, 청구서번호 숨김, 매입세금계산서 발행여부 체크박스 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
184 lines
6.0 KiB
PHP
184 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Finance;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Finance\Payable;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class PayableController extends Controller
|
|
{
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$tenantId = session('selected_tenant_id', 1);
|
|
|
|
$query = Payable::forTenant($tenantId);
|
|
|
|
if ($search = $request->input('search')) {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('vendor_name', 'like', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
if ($status = $request->input('status')) {
|
|
if ($status !== 'all') {
|
|
$query->where('status', $status);
|
|
}
|
|
}
|
|
|
|
if ($category = $request->input('category')) {
|
|
if ($category !== 'all') {
|
|
$query->where('category', $category);
|
|
}
|
|
}
|
|
|
|
$payables = $query->orderBy('created_at', 'desc')
|
|
->get()
|
|
->map(function ($item) {
|
|
return [
|
|
'id' => $item->id,
|
|
'vendorName' => $item->vendor_name,
|
|
'invoiceNo' => $item->invoice_no,
|
|
'issueDate' => $item->issue_date?->format('Y-m-d'),
|
|
'dueDate' => $item->due_date?->format('Y-m-d'),
|
|
'category' => $item->category,
|
|
'amount' => $item->amount,
|
|
'paidAmount' => $item->paid_amount,
|
|
'status' => $item->status,
|
|
'description' => $item->description,
|
|
'memo' => $item->memo,
|
|
'taxInvoiceIssued' => (bool) $item->tax_invoice_issued,
|
|
];
|
|
});
|
|
|
|
$all = Payable::forTenant($tenantId)->get();
|
|
|
|
$totalAmount = $all->sum('amount');
|
|
$totalPaid = $all->sum('paid_amount');
|
|
$overdueAmount = $all->where('status', 'overdue')->sum(function ($item) {
|
|
return $item->amount - $item->paid_amount;
|
|
});
|
|
|
|
$stats = [
|
|
'totalAmount' => $totalAmount,
|
|
'totalPaid' => $totalPaid,
|
|
'totalUnpaid' => $totalAmount - $totalPaid,
|
|
'overdueAmount' => $overdueAmount,
|
|
'count' => $all->count(),
|
|
];
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $payables,
|
|
'stats' => $stats,
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'vendorName' => 'required|string|max:100',
|
|
'invoiceNo' => 'nullable|string|max:50',
|
|
'amount' => 'required|integer|min:0',
|
|
]);
|
|
|
|
$tenantId = session('selected_tenant_id', 1);
|
|
|
|
Payable::create([
|
|
'tenant_id' => $tenantId,
|
|
'vendor_name' => $request->input('vendorName'),
|
|
'invoice_no' => $request->input('invoiceNo'),
|
|
'issue_date' => $request->input('issueDate'),
|
|
'due_date' => $request->input('dueDate'),
|
|
'category' => $request->input('category', '사무용품'),
|
|
'amount' => $request->input('amount', 0),
|
|
'paid_amount' => 0,
|
|
'status' => 'unpaid',
|
|
'description' => $request->input('description'),
|
|
'memo' => $request->input('memo'),
|
|
'tax_invoice_issued' => $request->boolean('taxInvoiceIssued', false),
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '미지급금이 등록되었습니다.',
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, int $id): JsonResponse
|
|
{
|
|
$tenantId = session('selected_tenant_id', 1);
|
|
$payable = Payable::forTenant($tenantId)->findOrFail($id);
|
|
|
|
$request->validate([
|
|
'vendorName' => 'required|string|max:100',
|
|
'invoiceNo' => 'nullable|string|max:50',
|
|
'amount' => 'required|integer|min:0',
|
|
]);
|
|
|
|
$payable->update([
|
|
'vendor_name' => $request->input('vendorName'),
|
|
'invoice_no' => $request->input('invoiceNo'),
|
|
'issue_date' => $request->input('issueDate'),
|
|
'due_date' => $request->input('dueDate'),
|
|
'category' => $request->input('category'),
|
|
'amount' => $request->input('amount'),
|
|
'status' => $request->input('status', $payable->status),
|
|
'description' => $request->input('description'),
|
|
'memo' => $request->input('memo'),
|
|
'tax_invoice_issued' => $request->boolean('taxInvoiceIssued', $payable->tax_invoice_issued),
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '미지급금이 수정되었습니다.',
|
|
]);
|
|
}
|
|
|
|
public function pay(Request $request, int $id): JsonResponse
|
|
{
|
|
$tenantId = session('selected_tenant_id', 1);
|
|
$payable = Payable::forTenant($tenantId)->findOrFail($id);
|
|
|
|
$request->validate([
|
|
'payAmount' => 'required|integer|min:1',
|
|
]);
|
|
|
|
$payAmount = $request->input('payAmount');
|
|
$remaining = $payable->amount - $payable->paid_amount;
|
|
|
|
if ($payAmount > $remaining) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '지급액이 잔액을 초과합니다.',
|
|
], 422);
|
|
}
|
|
|
|
$newPaid = $payable->paid_amount + $payAmount;
|
|
$newStatus = $newPaid >= $payable->amount ? 'paid' : 'partial';
|
|
|
|
$payable->update([
|
|
'paid_amount' => $newPaid,
|
|
'status' => $newStatus,
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '지급 처리되었습니다.',
|
|
]);
|
|
}
|
|
|
|
public function destroy(int $id): JsonResponse
|
|
{
|
|
$tenantId = session('selected_tenant_id', 1);
|
|
$payable = Payable::forTenant($tenantId)->findOrFail($id);
|
|
$payable->delete();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '미지급금이 삭제되었습니다.',
|
|
]);
|
|
}
|
|
}
|