feat:일일자금일보 기간별 보고서 기능 추가 (바로빌 계좌내역 기반)

This commit is contained in:
김보곤
2026-02-06 08:45:11 +09:00
parent fb44727633
commit 33ad29ea01
3 changed files with 335 additions and 470 deletions

View File

@@ -5,8 +5,10 @@
use App\Http\Controllers\Controller;
use App\Models\Finance\DailyFundTransaction;
use App\Models\Finance\DailyFundMemo;
use App\Models\Barobill\BankTransaction as BarobillBankTransaction;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class DailyFundController extends Controller
{
@@ -157,4 +159,116 @@ public function saveMemo(Request $request): JsonResponse
],
]);
}
/**
* 기간별 자금일보 (바로빌 계좌 거래내역 기반)
*/
public function periodReport(Request $request): JsonResponse
{
$tenantId = session('selected_tenant_id', 1);
$startDate = $request->input('start_date', now()->subMonth()->format('Ymd'));
$endDate = $request->input('end_date', now()->format('Ymd'));
// YYYYMMDD 형식으로 변환
$startDateYmd = str_replace('-', '', $startDate);
$endDateYmd = str_replace('-', '', $endDate);
// 기간 내 거래내역 조회
$transactions = BarobillBankTransaction::where('tenant_id', $tenantId)
->whereBetween('trans_date', [$startDateYmd, $endDateYmd])
->orderBy('trans_date', 'desc')
->orderBy('trans_time', 'desc')
->get();
// 일별로 그룹핑
$dailyData = [];
$accountBalances = []; // 계좌별 최신 잔액 추적
foreach ($transactions as $tx) {
$date = $tx->trans_date;
$accountNum = $tx->bank_account_num;
if (!isset($dailyData[$date])) {
$dailyData[$date] = [
'date' => $date,
'dateFormatted' => $this->formatDateKorean($date),
'accounts' => [],
'deposits' => [],
'withdrawals' => [],
'totalDeposit' => 0,
'totalWithdraw' => 0,
];
}
// 계좌별 데이터 집계
if (!isset($dailyData[$date]['accounts'][$accountNum])) {
$dailyData[$date]['accounts'][$accountNum] = [
'bankName' => $tx->bank_name,
'accountNum' => $accountNum,
'deposit' => 0,
'withdraw' => 0,
'balance' => $tx->balance,
];
}
// 입출금 내역 추가
if ($tx->deposit > 0) {
$dailyData[$date]['deposits'][] = [
'time' => $tx->trans_time,
'bankName' => $tx->bank_name,
'summary' => $tx->summary,
'cast' => $tx->cast,
'amount' => $tx->deposit,
'balance' => $tx->balance,
];
$dailyData[$date]['accounts'][$accountNum]['deposit'] += $tx->deposit;
$dailyData[$date]['totalDeposit'] += $tx->deposit;
}
if ($tx->withdraw > 0) {
$dailyData[$date]['withdrawals'][] = [
'time' => $tx->trans_time,
'bankName' => $tx->bank_name,
'summary' => $tx->summary,
'cast' => $tx->cast,
'amount' => $tx->withdraw,
'balance' => $tx->balance,
];
$dailyData[$date]['accounts'][$accountNum]['withdraw'] += $tx->withdraw;
$dailyData[$date]['totalWithdraw'] += $tx->withdraw;
}
// 해당 일자의 최신 잔액 업데이트
$dailyData[$date]['accounts'][$accountNum]['balance'] = $tx->balance;
}
// accounts를 배열로 변환
foreach ($dailyData as $date => &$data) {
$data['accounts'] = array_values($data['accounts']);
}
// 날짜 내림차순 정렬 (최신 일자가 위)
krsort($dailyData);
return response()->json([
'success' => true,
'data' => [
'startDate' => $startDate,
'endDate' => $endDate,
'dailyReports' => array_values($dailyData),
],
]);
}
private function formatDateKorean(string $dateYmd): string
{
$year = substr($dateYmd, 0, 4);
$month = (int) substr($dateYmd, 4, 2);
$day = (int) substr($dateYmd, 6, 2);
$date = \Carbon\Carbon::createFromFormat('Ymd', $dateYmd);
$dayOfWeek = ['일', '월', '화', '수', '목', '금', '토'][$date->dayOfWeek];
return "{$year}{$month}{$day}{$dayOfWeek}요일";
}
}