'decimal:2', 'split_supply_amount' => 'decimal:2', 'split_tax' => 'decimal:2', 'original_amount' => '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('use_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) { $supplyAmount = $split['supplyAmount'] ?? null; $tax = $split['tax'] ?? null; $splitAmount = ($supplyAmount !== null && $tax !== null) ? (float) $supplyAmount + (float) $tax : ($split['amount'] ?? 0); self::create([ 'tenant_id' => $tenantId, 'original_unique_key' => $uniqueKey, 'split_amount' => $splitAmount, 'split_supply_amount' => $supplyAmount, 'split_tax' => $tax, 'account_code' => $split['accountCode'] ?? null, 'account_name' => $split['accountName'] ?? null, 'deduction_type' => $split['deductionType'] ?? null, 'evidence_name' => $split['evidenceName'] ?? null, 'description' => $split['description'] ?? null, 'memo' => $split['memo'] ?? null, 'sort_order' => $index, 'card_num' => $originalData['cardNum'] ?? '', 'use_dt' => $originalData['useDt'] ?? '', 'use_date' => $originalData['useDate'] ?? '', 'approval_num' => $originalData['approvalNum'] ?? '', 'original_amount' => $originalData['originalAmount'] ?? 0, 'merchant_name' => $originalData['merchantName'] ?? '', ]); } } /** * 분개 내역 삭제 (원본으로 복원) */ public static function deleteSplits(int $tenantId, string $uniqueKey): int { return self::where('tenant_id', $tenantId) ->where('original_unique_key', $uniqueKey) ->delete(); } }