'decimal:2', 'original_deposit' => 'decimal:2', 'original_withdraw' => 'decimal:2', 'sort_order' => 'integer', ]; /** * 테넌트 관계 */ public function tenant(): BelongsTo { return $this->belongsTo(Tenant::class); } /** * 테넌트별 분개 내역 조회 (기간별) * 고유키를 기준으로 그룹핑하여 반환 */ public static function getByDateRange(int $tenantId, string $startDate, string $endDate): array { $splits = self::where('tenant_id', $tenantId) ->whereBetween('trans_date', [$startDate, $endDate]) ->orderBy('original_unique_key') ->orderBy('sort_order') ->get(); // 고유키별로 그룹핑 $grouped = []; foreach ($splits as $split) { $key = $split->original_unique_key; if (! isset($grouped[$key])) { $grouped[$key] = []; } $grouped[$key][] = $split; } return $grouped; } /** * 특정 거래의 분개 내역 조회 */ public static function getByUniqueKey(int $tenantId, string $uniqueKey): \Illuminate\Database\Eloquent\Collection { return self::where('tenant_id', $tenantId) ->where('original_unique_key', $uniqueKey) ->orderBy('sort_order') ->get(); } /** * 특정 거래의 분개 내역 저장 (기존 분개 삭제 후 재생성) */ public static function saveSplits(int $tenantId, string $uniqueKey, array $originalData, array $splits): void { // 기존 분개 삭제 self::where('tenant_id', $tenantId) ->where('original_unique_key', $uniqueKey) ->delete(); // 새 분개 저장 foreach ($splits as $index => $split) { self::create([ 'tenant_id' => $tenantId, 'original_unique_key' => $uniqueKey, 'split_amount' => $split['amount'] ?? 0, 'account_code' => $split['accountCode'] ?? null, 'account_name' => $split['accountName'] ?? null, 'description' => $split['description'] ?? null, 'memo' => $split['memo'] ?? null, 'sort_order' => $index, 'bank_account_num' => $originalData['bankAccountNum'] ?? '', 'trans_dt' => $originalData['transDt'] ?? '', 'trans_date' => $originalData['transDate'] ?? '', 'original_deposit' => $originalData['originalDeposit'] ?? 0, 'original_withdraw' => $originalData['originalWithdraw'] ?? 0, 'summary' => $originalData['summary'] ?? '', ]); } } /** * 분개 내역 삭제 (원본으로 복원) */ public static function deleteSplits(int $tenantId, string $uniqueKey): int { return self::where('tenant_id', $tenantId) ->where('original_unique_key', $uniqueKey) ->delete(); } }