summaryrefslogtreecommitdiff
path: root/db/schema/bidding.ts
blob: 1869963330319c2898b76125884b44e57d1d92d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
// schema.ts (입찰 시스템 전체 스키마 - 실무 요구사항 최종 반영)

import { 
  pgTable, 
  serial, 
  varchar, 
  text, 
  timestamp, 
  integer, 
  decimal, 
  boolean,
  pgEnum,
  date, 
} from 'drizzle-orm/pg-core'
import { Vendor, vendors } from './vendors'
import { projects } from './projects';
import { users } from './users';


// 입찰공고문 템플릿 (기존)
export const biddingNoticeTemplate = pgTable('bidding_notice_template', {
  id: serial('id').primaryKey(),
  biddingId: integer('bidding_id').references(() => biddings.id), // 연결된 입찰 ID (null이면 템플릿)
  type: varchar('type', { length: 50 }).notNull().default('standard'), // 입찰공고 타입
  title: varchar('title', { length: 200 }).notNull().default('입찰공고문'),
  content: text('content').notNull().default(''),
  isTemplate: boolean('is_template').default(false).notNull(), // 템플릿 여부
  createdAt: timestamp('created_at').defaultNow().notNull(),
  updatedAt: timestamp('updated_at').defaultNow().notNull(),
})

export type BiddingNoticeTemplate = typeof biddingNoticeTemplate.$inferSelect
export type NewBiddingNoticeTemplate = typeof biddingNoticeTemplate.$inferInsert

// 1. 입찰 상태 enum (실무 워크플로우 반영)
export const biddingStatusEnum = pgEnum('bidding_status', [
  'bidding_generated',        // 입찰생성
  'request_for_quotation',    // 사전견적요청
  'received_quotation',       // 사전견적접수
  'set_target_price',         // 내정가 산정
  'bidding_opened',           // 입찰공고
  'bidding_closed',           // 입찰마감
  'evaluation_of_bidding',    // 입찰평가중
  'bidding_disposal',         // 유찰
  'vendor_selected',          // 업체선정
  'bid_opening',              // 개찰
  'early_bid_opening',        // 조기개찰
  'rebidding',                // 재입찰
  'disposal_cancelled',       // 유찰취소
  'bid_closure',              // 폐찰
  'round_increase'            // 차수증가
])

// 2. 계약구분 enum 
export const contractTypeEnum = pgEnum('contract_type', [
  'unit_price',    // 단가계약
  'general',       // 일반계약
  'sale'          // 매각계약
])

// 3. 입찰유형 enum
export const biddingTypeEnum = pgEnum('bidding_type', [
  'equipment',        // 기자재
  'construction',     // 공사
  'service',         // 용역
  'lease',           // 임차
  'steel_stock',     // 형강스톡
  'piping',          // 배관
  'transport',       // 운송
  'waste',           // 폐기물
  'sale',            // 매각
  'other'            // 기타(직접입력)
])

// 4. 낙찰수 enum
export const awardCountEnum = pgEnum('award_count', [
  'single',    // 단수
  'multiple'   // 복수
])

// 5. 업체 초대/응답 상태 enum
export const invitationStatusEnum = pgEnum('invitation_status', [
  'pending',              // 초대 대기
  'pre_quote_sent',       // 사전견적 초대 발송
  'pre_quote_accepted',   // 사전견적 참여
  'pre_quote_declined',   // 사전견적 미참여
  'pre_quote_submitted',  // 사전견적제출완료
  'bidding_sent',         // 입찰 초대 발송
  'bidding_accepted',     // 입찰 참여
  'bidding_declined',     // 입찰 미참여
  'bidding_cancelled',    // 응찰 취소
  'bidding_submitted'     // 응찰 완료
])

// invitationStatus 라벨 맵핑
export const invitationStatusLabels: Record<string, string> = {
  pending: '초대 대기',
  pre_quote_sent: '사전견적 초대 발송',
  pre_quote_accepted: '사전견적 참여',
  pre_quote_declined: '사전견적 미참여',
  pre_quote_submitted: '사전견적제출완료',
  bidding_sent: '입찰 초대 발송',
  bidding_accepted: '입찰 참여',
  bidding_declined: '입찰 미참여',
  bidding_cancelled: '응찰 취소',
  bidding_submitted: '응찰 완료'
}

// 6. 문서 타입 enum
export const documentTypeEnum = pgEnum('document_type', [
  'notice',           // 입찰공고서
  'specification',    // 사양서
  'specification_meeting',    // 사양설명회
  'contract_draft',   // 계약서 초안
  'company_proposal', // 업체 제안서
  'financial_doc',    // 재무 관련 문서
  'technical_doc',    // 기술 관련 문서
  'certificate',      // 인증서류
  'pr_document',      // PR 문서
  'spec_document',    // SPEC 문서
  'evaluation_doc',   // 평가 관련 문서
  'bid_attachment',   // 입찰 첨부파일
  'other'            // 기타
])

// 7. 수량 단위 enum
export const quantityUnitEnum = pgEnum('quantity_unit', [
  'ea',     // 개
  'set',    // 세트
  'kg',     // 킬로그램
  'ton',    // 톤
  'm',      // 미터
  'm2',     // 제곱미터
  'm3',     // 세제곱미터
  'lot',    // 로트
  'other'   // 기타
])

// 8. 중량 단위 enum
export const weightUnitEnum = pgEnum('weight_unit', [
  'kg',     // 킬로그램
  'ton',    // 톤
  'lb',     // 파운드
  'g'       // 그램
])

// 8. 입찰 메인 테이블 (실무 요구사항 반영)
export const biddings = pgTable('biddings', {
  id: serial('id').primaryKey(),
  biddingNumber: varchar('bidding_number', { length: 50 }).unique().notNull(), // 입찰 No.
  originalBiddingNumber: varchar('original_bidding_number', { length: 50 }), // 원입찰번호
  revision: integer('revision').default(0), // Rev.
  //견적에서 넘어온 레코드인지, 자체생산인지, 디폴트는 자체생산, notnull
  biddingSourceType: varchar('bidding_source_type', { length: 20 }).notNull().default('manual'),
  // 기본 정보
  projectName: varchar('project_name', { length: 300 }), // 프로젝트명
  itemName: varchar('item_name', { length: 300 }), // 품목명
  title: varchar('title', { length: 300 }).notNull(), // 입찰명
  description: text('description'),
  // 입찰공고 내용은 별도 biddingNotices 테이블에서 관리됨
  
  // 계약 정보
  contractType: contractTypeEnum('contract_type').notNull(), // 계약구분
  biddingType: biddingTypeEnum('bidding_type').notNull(), // 입찰유형
  awardCount: awardCountEnum('award_count').default('single'), // 낙찰수
  // contractPeriod: varchar('contract_period', { length: 100 }), // 계약기간
  //시작일 (기본값: 현재 날짜)
  contractStartDate: date('contract_start_date').defaultNow(),
  //종료일 (기본값: 시작일로부터 1년 후)
  contractEndDate: date('contract_end_date'),
  
  // 일정 관리
  preQuoteDate: date('pre_quote_date'), // 사전견적일
  biddingRegistrationDate: date('bidding_registration_date'), // 입찰등록일
  submissionStartDate: timestamp('submission_start_date'), // 입찰서제출기간 시작
  submissionEndDate: timestamp('submission_end_date'), // 입찰서제출기간 끝
  evaluationDate: timestamp('evaluation_date'),
  
  // 사양설명회
  hasSpecificationMeeting: boolean('has_specification_meeting').default(false),
  
  // 예산 및 가격 정보
  currency: varchar('currency', { length: 3 }).default('KRW'), // 통화
  budget: decimal('budget', { precision: 15, scale: 2 }), // 예산
  targetPrice: decimal('target_price', { precision: 15, scale: 2 }), // 내정가
  targetPriceCalculationCriteria: text('target_price_calculation_criteria'), // 내정가 산정 기준
  finalBidPrice: decimal('final_bid_price', { precision: 15, scale: 2 }), // 최종입찰가
  
  // PR 정보
  prNumber: varchar('pr_number', { length: 50 }), // PR No.
  hasPrDocument: boolean('has_pr_document').default(false), // PR 문서 여부
  
  // 상태 및 설정
  status: biddingStatusEnum('status').default('bidding_generated').notNull(),
  isPublic: boolean('is_public').default(false), // 공개 입찰 여부
  isUrgent: boolean('is_urgent').default(false), // 긴급여부

  // 구매조직 정보
  purchasingOrganization: varchar('purchasing_organization', { length: 100 }), // 구매조직

  // 담당자 정보 (개선된 구조)
  bidPicId: integer('bid_pic_id').references(() => users.id), // 입찰담당자 ID
  bidPicName: varchar('bid_pic_name', { length: 100 }), // 입찰담당자 이름
  bidPicCode: varchar('bid_pic_code', { length: 50 }), // 입찰담당자 코드
  supplyPicId: integer('supply_pic_id').references(() => users.id), // 조달담당자 ID
  supplyPicName: varchar('supply_pic_name', { length: 100 }), // 조달담당자 이름
  supplyPicCode: varchar('supply_pic_code', { length: 50 }), // 조달담당자 코드

  // 기존 담당자 정보 제거됨 (user FK로 대체)
  
  // 메타 정보
  remarks: text('remarks'), // 비고
  createdBy: varchar('created_by', { length: 100 }), // 생성자
  createdAt: timestamp('created_at').defaultNow().notNull(),
  updatedAt: timestamp('updated_at').defaultNow().notNull(), // 최종수정일
  updatedBy: varchar('updated_by', { length: 100 }), // 최종수정자
  ANFNR: varchar({length: 50}).unique(), // 원본 ANFNR 추적을 위한 Bidding/RFQ Number (ECC), onConflict target이므로 unique 처리
})

// 9. 사양설명회 정보 테이블
export const specificationMeetings = pgTable('specification_meetings', {
  id: serial('id').primaryKey(),
  biddingId: integer('bidding_id').references(() => biddings.id).notNull(),
  
  meetingDate: timestamp('meeting_date').notNull(),
  meetingTime: varchar('meeting_time', { length: 20 }),
  location: varchar('location', { length: 500 }),
  address: text('address'),
  contactPerson: varchar('contact_person', { length: 100 }),
  contactPhone: varchar('contact_phone', { length: 20 }),
  contactEmail: varchar('contact_email', { length: 100 }),
  
  agenda: text('agenda'), // 회의 안건
  materials: text('materials'), // 준비물
  notes: text('notes'), // 특이사항
  isRequired: boolean('is_required').default(false), // 필수 참석 여부
  
  createdAt: timestamp('created_at').defaultNow().notNull(),
  updatedAt: timestamp('updated_at').defaultNow().notNull(),
})

// 10. PR 문서 테이블
export const prDocuments = pgTable('pr_documents', {
  id: serial('id').primaryKey(),
  biddingId: integer('bidding_id').references(() => biddings.id).notNull(),
  
  documentName: varchar('document_name', { length: 300 }).notNull(), // 문서명
  fileName: varchar('file_name', { length: 500 }).notNull(),
  originalFileName: varchar('original_file_name', { length: 500 }).notNull(),
  fileSize: integer('file_size'), // bytes
  mimeType: varchar('mime_type', { length: 100 }),
  filePath: varchar('file_path', { length: 1000 }).notNull(), // 실제 파일 경로
  
  registeredAt: timestamp('registered_at').defaultNow().notNull(), // 등재일
  registeredBy: varchar('registered_by', { length: 100 }).notNull(), // 등재자
  
  description: text('description'),
  version: varchar('version', { length: 20 }),
  
  createdAt: timestamp('created_at').defaultNow().notNull(),
  updatedAt: timestamp('updated_at').defaultNow().notNull(),
})

// 11. PR 아이템 테이블 (상세 응찰 정보)
export const prItemsForBidding = pgTable('pr_items_for_bidding', {
  id: serial('id').primaryKey(),
  biddingId: integer('bidding_id').references(() => biddings.id).notNull(),
  
  // 기본 정보
  itemNumber: varchar('item_number', { length: 50 }), // 아이템 번호
  projectId: integer('project_id').references(() => projects.id), // 프로젝트 ID (새로 추가)
  projectInfo: varchar('project_info', { length: 300 }), // 프로젝트 정보 (기존, 추후 제거 가능)
  itemInfo: varchar('item_info', { length: 300 }), // 품목정보
  shi: varchar('shi', { length: 100 }), // SHI
  // 자재 그룹 정보 (새로 추가)
  materialGroupNumber: varchar('material_group_number', { length: 100 }), // 자재그룹번호
  materialGroupInfo: varchar('material_group_info', { length: 300 }), // 자재그룹정보
  // 자재 정보 (새로 추가)
  materialNumber: varchar('material_number', { length: 100 }), // 자재번호
  materialInfo: varchar('material_info', { length: 500 }), // 자재정보
  
  // 납품 일정
  requestedDeliveryDate: date('requested_delivery_date'), // 납품요청일
  
  // 가격 정보
  annualUnitPrice: decimal('annual_unit_price', { precision: 15, scale: 2 }), // 연간단가
  currency: varchar('currency', { length: 3 }).default('KRW'),
  
  // 수량 및 중량
  quantity: decimal('quantity', { precision: 10, scale: 2 }), // 수량
  quantityUnit: varchar('quantity_unit', { length: 50 }),  // 수량단위 (구매단위)
  totalWeight: decimal('total_weight', { precision: 10, scale: 2 }), // 총 중량
  weightUnit: varchar('weight_unit', { length: 50 }),  // 중량단위 (자재순중량)
  
  // 가격 단위 추가
  priceUnit: varchar('price_unit', { length: 50 }), // 가격단위
  purchaseUnit: varchar('purchase_unit', { length: 50 }), // 구매단위
  materialWeight: decimal('material_weight', { precision: 10, scale: 2 }), // 자재순중량

  // WBS 정보
  wbsCode: varchar('wbs_code', { length: 100 }), // WBS 코드
  wbsName: varchar('wbs_name', { length: 300 }), // WBS 명칭

  // Cost Center 정보
  costCenterCode: varchar('cost_center_code', { length: 100 }), // 코스트센터 코드
  costCenterName: varchar('cost_center_name', { length: 300 }), // 코스트센터 명칭

  // GL Account 정보
  glAccountCode: varchar('gl_account_code', { length: 100 }), // GL 계정 코드
  glAccountName: varchar('gl_account_name', { length: 300 }), // GL 계정 명칭

  // 내정 정보 (새로 추가)
  targetUnitPrice: decimal('target_unit_price', { precision: 15, scale: 2 }), // 내정단가
  targetAmount: decimal('target_amount', { precision: 15, scale: 2 }), // 내정금액
  targetCurrency: varchar('target_currency', { length: 3 }).default('KRW'), // 내정통화

  // 예산 정보 (새로 추가)
  budgetAmount: decimal('budget_amount', { precision: 15, scale: 2 }), // 예산금액
  budgetCurrency: varchar('budget_currency', { length: 3 }).default('KRW'), // 예산통화

  // 실적 정보 (새로 추가)
  actualAmount: decimal('actual_amount', { precision: 15, scale: 2 }), // 실적금액
  actualCurrency: varchar('actual_currency', { length: 3 }).default('KRW'), // 실적통화
  

  prNumber: varchar('pr_number', { length: 50 }), // PR번호
  
  // SPEC 파일 정보
  hasSpecDocument: boolean('has_spec_document').default(false),
  
  createdAt: timestamp('created_at').defaultNow().notNull(),
  updatedAt: timestamp('updated_at').defaultNow().notNull(),
})

// 12. 입찰 조건 테이블 (SHI 구매자가 제시하는 조건들)
export const biddingConditions = pgTable('bidding_conditions', {
  id: serial('id').primaryKey(),
  biddingId: integer('bidding_id').references(() => biddings.id).notNull(),
  
  // 지급조건
  paymentTerms: text('payment_terms'), // 지급조건 옵션들 
  
  // 세금
  taxConditions: text('tax_conditions'), // Tax 옵션들
  
  // 계약 및 납기
  contractDeliveryDate: date('contract_delivery_date'), // 계약납기일
  isPriceAdjustmentApplicable: boolean('is_price_adjustment_applicable'), // 연동제적용 여부
  
  // 무역조건
  incoterms: text('incoterms'), // Incoterms 옵션들
  incotermsOption: text('incoterms_option'), // Incoterms 옵션 (추가)
  shippingPort: varchar('shipping_port', { length: 200 }), // 선적지
  destinationPort: varchar('destination_port', { length: 200 }), // 하역지
  
  // 기타
  sparePartOptions: text('spare_part_options'), // Spare part 옵션들
  
  createdAt: timestamp('created_at').defaultNow().notNull(),
  updatedAt: timestamp('updated_at').defaultNow().notNull(),
})

// 13. 입찰 참여 업체 테이블2
export const biddingCompanies = pgTable('bidding_companies', {
  id: serial('id').primaryKey(),
  biddingId: integer('bidding_id').references(() => biddings.id).notNull(),
  companyId: integer('company_id').references(() => vendors.id).notNull(),
  
  // 초대 및 응답 상태
  invitationStatus: invitationStatusEnum('invitation_status').notNull(),
  invitedAt: timestamp('invited_at'),
  respondedAt: timestamp('responded_at'),
  
  // 사전견적 정보
  preQuoteAmount: decimal('pre_quote_amount', { precision: 15, scale: 2 }),
  preQuoteSubmittedAt: timestamp('pre_quote_submitted_at'),
  preQuoteDeadline: timestamp('pre_quote_deadline'), // 사전견적 마감일
  isPreQuoteSelected: boolean('is_pre_quote_selected').default(false), // 본입찰 대상 선정 여부
  isPreQuoteParticipated: boolean('is_pre_quote_participated'), // 사전견적 참여 여부
  
  // 본입찰 정보  
  isBiddingInvited: boolean('is_bidding_invited').default(false), // 본입찰 초대 여부
  isBiddingParticipated: boolean('is_bidding_participated'),//본입찰 참여 여부
  finalQuoteAmount: decimal('final_quote_amount', { precision: 15, scale: 2 }),
  finalQuoteSubmittedAt: timestamp('final_quote_submitted_at'),
  isFinalSubmission: boolean('is_final_submission').default(false), // 최종제출 여부
  isWinner: boolean('is_winner'), // 낙찰 여부
  isAttendingMeeting: boolean('is_attending_meeting'), // 사양설명회 참석 여부
  awardRatio: decimal('award_ratio', { precision: 5, scale: 2 }), // 발주비율
  //연동제 적용요건 문의 여부
  isPriceAdjustmentApplicableQuestion: boolean('is_price_adjustment_applicable_question').default(false), // 연동제 적용요건 문의 여부

  // 기타
  notes: text('notes'), // 특이사항
  contactPerson: varchar('contact_person', { length: 100 }), // 업체 담당자
  contactEmail: varchar('contact_email', { length: 100 }),
  contactPhone: varchar('contact_phone', { length: 20 }),
  
  createdAt: timestamp('created_at').defaultNow().notNull(),
  updatedAt: timestamp('updated_at').defaultNow().notNull(),
})

// 13-1. 입찰 참여 업체 담당자 테이블
export const biddingCompaniesContacts = pgTable('bidding_companies_contacts', {
  id: serial('id').primaryKey(),
  biddingId: integer('bidding_id').references(() => biddings.id).notNull(),
  vendorId: integer('vendor_id').references(() => vendors.id).notNull(),
  contactName: varchar('contact_name', { length: 255 }).notNull(),
  contactEmail: varchar('contact_email', { length: 255 }).notNull(),
  contactNumber: varchar('contact_number', { length: 50 }),
  createdAt: timestamp('created_at').defaultNow().notNull(),
  updatedAt: timestamp('updated_at').defaultNow().notNull(),
})

// 14. 업체별 PR 아이템 응찰 정보
export const companyPrItemBids = pgTable('company_pr_item_bids', {
  id: serial('id').primaryKey(),
  biddingCompanyId: integer('bidding_company_id').references(() => biddingCompanies.id).notNull(),
  prItemId: integer('pr_item_id').references(() => prItemsForBidding.id).notNull(),
  
  // 업체 응찰 정보
  proposedDeliveryDate: date('proposed_delivery_date'), // 납품예정일
  bidUnitPrice: decimal('bid_unit_price', { precision: 15, scale: 2 }), // 입찰단가
  bidAmount: decimal('bid_amount', { precision: 15, scale: 2 }), // 입찰금액
  currency: varchar('currency', { length: 3 }).default('KRW'),
  
  // 기술 정보
  technicalSpecification: text('technical_specification'), // 기술 사양
  
  // 제출 정보
  isPreQuote: boolean('is_pre_quote').default(false), // 사전견적 여부
  submittedAt: timestamp('submitted_at').defaultNow().notNull(),
  
  createdAt: timestamp('created_at').defaultNow().notNull(),
  updatedAt: timestamp('updated_at').defaultNow().notNull(),
})

// 15. 업체별 입찰 조건 응답
export const companyConditionResponses = pgTable('company_condition_responses', {
  id: serial('id').primaryKey(),
  biddingCompanyId: integer('bidding_company_id').references(() => biddingCompanies.id).notNull(),
  
  // 각 조건에 대한 응답 (JSON 형태로 저장)
  paymentTermsResponse: varchar('payment_terms_response', { length: 200 }), // 선택된 지급조건
  taxConditionsResponse: varchar('tax_conditions_response', { length: 200 }), // 선택된 Tax 조건
  
  // 계약 및 납기 응답
  proposedContractDeliveryDate: date('proposed_contract_delivery_date'), // 제안 계약납기일
  priceAdjustmentResponse: boolean('price_adjustment_response'), // 연동제적용 응답
  isInitialResponse: boolean('is_initial_response'), // 초도여부 응답
  
  // 무역조건 응답
  incotermsResponse: varchar('incoterms_response', { length: 100 }), // 선택된 Incoterms
  proposedShippingPort: varchar('proposed_shipping_port', { length: 200 }), // 제안 선적지
  proposedDestinationPort: varchar('proposed_destination_port', { length: 200 }), // 제안 하역지
  
  // 기타 응답
  sparePartResponse: varchar('spare_part_response', { length: 200 }), // Spare part 응답
  
  // 추가 제안사항
  additionalProposals: text('additional_proposals'), // 추가 제안사항
  
  // 제출 정보
  isPreQuote: boolean('is_pre_quote').default(false), // 사전견적 여부
  submittedAt: timestamp('submitted_at').defaultNow().notNull(),
  
  createdAt: timestamp('created_at').defaultNow().notNull(),
  updatedAt: timestamp('updated_at').defaultNow().notNull(),
})

// 16. 입찰 관련 문서 테이블 (강화)
export const biddingDocuments = pgTable('bidding_documents', {
  id: serial('id').primaryKey(),
  biddingId: integer('bidding_id').references(() => biddings.id).notNull(),
  companyId: integer('company_id').references(() => vendors.id), // null이면 발주처 문서
  prItemId: integer('pr_item_id').references(() => prItemsForBidding.id), // SPEC 문서인 경우 연결
  specificationMeetingId: integer('specification_meeting_id').references(() => specificationMeetings.id), // 사양설명회 문서인 경우 연결
  
  documentType: documentTypeEnum('document_type').notNull(),
  fileName: varchar('file_name', { length: 500 }).notNull(),
  originalFileName: varchar('original_file_name', { length: 500 }).notNull(),
  fileSize: integer('file_size'), // bytes
  mimeType: varchar('mime_type', { length: 100 }),
  filePath: varchar('file_path', { length: 1000 }).notNull(), // 실제 파일 경로
  
  title: varchar('title', { length: 300 }),
  description: text('description'),
  
  // 접근 권한
  isPublic: boolean('is_public').default(false), // 모든 참여업체 열람 가능
  isRequired: boolean('is_required').default(false), // 필수 제출 문서
  
  uploadedBy: varchar('uploaded_by', { length: 100 }),
  uploadedAt: timestamp('uploaded_at').defaultNow().notNull(),
})
// 17. 업체 선정 결과 테이블 (평가 점수 대신 선정 사유)
export const vendorSelectionResults = pgTable('vendor_selection_results', {
  id: serial('id').primaryKey(),
  biddingId: integer('bidding_id').references(() => biddings.id).notNull(),
  selectedCompanyId: integer('selected_company_id').references(() => vendors.id).notNull(),
  
  // 선정 사유 및 결과
  selectionReason: text('selection_reason').notNull(), // 선정 사유
  evaluationSummary: text('evaluation_summary'), // 평가 요약
  
  // 선정 결과 관련 첨부파일들
  hasResultDocuments: boolean('has_result_documents').default(false),
  
  // 선정 정보
  selectedAt: timestamp('selected_at').defaultNow().notNull(),
  selectedBy: varchar('selected_by', { length: 100 }).notNull(),
  
  // 기타
  remarks: text('remarks'),
  
  createdAt: timestamp('created_at').defaultNow().notNull(),
  updatedAt: timestamp('updated_at').defaultNow().notNull(),
})

// 19. 하도급대금 등 연동표 테이블
export const priceAdjustmentForms = pgTable('price_adjustment_forms', {
  id: serial('id').primaryKey(),
  
  // companyConditionResponses 테이블과 외래 키로 연결
  companyConditionResponsesId: integer('company_condition_responses_id')
    .notNull()
    .references(() => companyConditionResponses.id),
  
  // 품목등의 명칭
  itemName: varchar('item_name', { length: 255 }),

  // 조정대금 반영시점
  adjustmentReflectionPoint: varchar('adjustment_reflection_point', { length: 255 }),
  
  // 연동대상 주요 원재료
  majorApplicableRawMaterial: text('major_applicable_raw_material'),
  
  // 하도급대금등 연동 산식
  adjustmentFormula: text('adjustment_formula'),

  // 원재료 가격 기준지표
  rawMaterialPriceIndex: text('raw_material_price_index'),
  
  // 기준시점 및 비교시점
  referenceDate: date('reference_date'), // 기준시점
  comparisonDate: date('comparison_date'), // 비교시점

  // 연동 비율
  adjustmentRatio: decimal('adjustment_ratio', { precision: 5, scale: 2 }), // 소수점 2자리까지
  
  // 기타 사항
  notes: text('notes'),

  // 조정요건
  adjustmentConditions: text('adjustment_conditions'),
  
  // 연동 미적용 주요 원재료
  majorNonApplicableRawMaterial: text('major_non_applicable_raw_material'),
  
  // 조정주기
  adjustmentPeriod: varchar('adjustment_period', { length: 100 }),

  // 수탁기업(협력사) 작성자
  contractorWriter: varchar('contractor_writer', { length: 100 }),

  // 조정일
  adjustmentDate: date('adjustment_date'),
  
  // 연동 미적용 사유
  nonApplicableReason: text('non_applicable_reason'),
  
  // 메타 정보
  createdAt: timestamp('created_at').defaultNow().notNull(),
  updatedAt: timestamp('updated_at').defaultNow().notNull(),
});

// 타입 정의
export type Bidding = typeof biddings.$inferSelect & {
  // 추가 필드들 (쿼리 결과에 포함되므로 타입에도 추가)
  purchasingOrganization?: string | null
  bidPicId?: number | null
  bidPicName?: string | null
  supplyPicId?: number | null
  supplyPicName?: string | null
}

export type NewBidding = typeof biddings.$inferInsert

export type SpecificationMeeting = typeof specificationMeetings.$inferSelect
export type NewSpecificationMeeting = typeof specificationMeetings.$inferInsert

export type PrDocument = typeof prDocuments.$inferSelect
export type NewPrDocument = typeof prDocuments.$inferInsert

export type PrItem = typeof prItemsForBidding.$inferSelect
export type NewPrItem = typeof prItemsForBidding.$inferInsert

export type BiddingConditions = typeof biddingConditions.$inferSelect
export type NewBiddingConditions = typeof biddingConditions.$inferInsert

export type BiddingCompany = typeof biddingCompanies.$inferSelect
export type NewBiddingCompany = typeof biddingCompanies.$inferInsert

export type BiddingCompanyContact = typeof biddingCompaniesContacts.$inferSelect
export type NewBiddingCompanyContact = typeof biddingCompaniesContacts.$inferInsert

export type CompanyPrItemBid = typeof companyPrItemBids.$inferSelect
export type NewCompanyPrItemBid = typeof companyPrItemBids.$inferInsert

export type CompanyConditionResponse = typeof companyConditionResponses.$inferSelect
export type NewCompanyConditionResponse = typeof companyConditionResponses.$inferInsert

export type BiddingDocument = typeof biddingDocuments.$inferSelect
export type NewBiddingDocument = typeof biddingDocuments.$inferInsert

export type VendorSelectionResult = typeof vendorSelectionResults.$inferSelect
export type NewVendorSelectionResult = typeof vendorSelectionResults.$inferInsert

export type PriceAdjustmentForm = typeof priceAdjustmentForms.$inferSelect
export type NewPriceAdjustmentForm = typeof priceAdjustmentForms.$inferInsert

// 조인 타입 정의 (자주 사용될 것들)
export type BiddingWithDetails = Bidding & {
  specificationMeeting?: SpecificationMeeting
  prDocuments: PrDocument[]
  prItemsForBidding: (PrItem & {
    specDocuments: BiddingDocument[]
  })[]
  biddingConditions?: BiddingConditions
  companies: (BiddingCompany & {
    company: Vendor
    prItemBids: CompanyPrItemBid[]
    conditionResponses: CompanyConditionResponse[]
    documents: BiddingDocument[]
  })[]
  documents: BiddingDocument[]
  selectionResult?: VendorSelectionResult
}

export type BiddingListItem = Bidding & {
  // 전체 참여 현황
  participantExpected: number // 초대업체 수
  participationRate: number // 참여율 (입찰 기준)
  
  // 사전견적 참여 현황
  preQuotePending: number // 사전견적 초대 대기/발송
  preQuoteAccepted: number // 사전견적 참여
  preQuoteDeclined: number // 사전견적 미참여
  preQuoteSubmitted: number // 사전견적 제출완료
  
  // 입찰 참여 현황
  biddingPending: number // 입찰 초대 발송
  biddingAccepted: number // 입찰 참여
  biddingDeclined: number // 입찰 미참여
  biddingCancelled: number // 응찰 취소
  biddingSubmitted: number // 응찰 완료
  
  // 호환성을 위한 기존 필드 (deprecated)
  participantParticipated: number // 참여
  participantDeclined: number // 포기
  participantPending: number // 대기
  participantAccepted: number // 수락
  
  participantStats: {
    expected: number    // 참여예정
    participated: number // 참여
    declined: number    // 포기
  }
  specificationMeeting?: SpecificationMeeting
  prDocuments: PrDocument[]
}

// 상태별 한글 매핑
export const biddingStatusLabels = {
  bidding_generated: '입찰생성',
  request_for_quotation: '사전견적요청', 
  received_quotation: '사전견적접수',
  set_target_price: '내정가 산정',
  bidding_opened: '입찰공고',
  bidding_closed: '입찰마감',
  evaluation_of_bidding: '입찰평가중',
  bidding_disposal: '유찰',
  vendor_selected: '업체선정',
  bid_opening: '개찰',
  early_bid_opening: '조기개찰',
  rebidding: '재입찰',
  disposal_cancelled: '유찰취소',
  bid_closure: '폐찰',
  round_increase: '차수증가'
} as const

export const contractTypeLabels = {
  unit_price: '단가계약',
  general: '일반계약',
  sale: '매각계약'
} as const

export const biddingNoticeTypeLabels = {
  standard: '표준',
  facility: '시설재',
  unit_price: '단가계약'
} as const

export const biddingTypeLabels = {
  equipment: '기자재',
  construction: '공사',
  service: '용역',
  lease: '임차',
  steel_stock: '형강스톡',
  piping: '배관',
  transport: '운송',
  waste: '폐기물',
  sale: '매각',
  other: '기타(직접입력)'
} as const

export const awardCountLabels = {
  single: '단수',
  multiple: '복수'
} as const

export const quantityUnitLabels = {
  ea: '개',
  set: '세트',
  kg: 'kg',
  ton: 'ton',
  m: 'm',
  m2: 'm²',
  m3: 'm³',
  lot: 'lot',
  other: '기타'
} as const

export const weightUnitLabels = {
  kg: 'kg',
  ton: 'ton',
  lb: 'lb',
  g: 'g'
} as const


// export const biddingListView = pgView('bidding_list_view').as((qb) =>
//   qb
//     .select({
//       // ═══════════════════════════════════════════════════════════════
//       // 기본 입찰 정보
//       // ═══════════════════════════════════════════════════════════════
//       id: biddings.id,
//       biddingNumber: biddings.biddingNumber,
//       revision: biddings.revision,
//       projectName: biddings.projectName,
//       itemName: biddings.itemName,
//       title: biddings.title,
//       description: biddings.description,
//       biddingSourceType: biddings.biddingSourceType,
//       isUrgent: biddings.isUrgent,
      
//       // ═══════════════════════════════════════════════════════════════
//       // 계약 정보
//       // ═══════════════════════════════════════════════════════════════
//       contractType: biddings.contractType,
//       biddingType: biddings.biddingType,
//       awardCount: biddings.awardCount,
//       contractStartDate: biddings.contractStartDate,
//       contractEndDate: biddings.contractEndDate,
      
//       // ═══════════════════════════════════════════════════════════════
//       // 일정 관리
//       // ═══════════════════════════════════════════════════════════════
//       preQuoteDate: biddings.preQuoteDate,
//       biddingRegistrationDate: biddings.biddingRegistrationDate,
//       submissionStartDate: biddings.submissionStartDate,
//       submissionEndDate: biddings.submissionEndDate,
//       evaluationDate: biddings.evaluationDate,
      
//       // ═══════════════════════════════════════════════════════════════
//       // 회의 및 문서
//       // ═══════════════════════════════════════════════════════════════
//       hasSpecificationMeeting: biddings.hasSpecificationMeeting,
//       hasPrDocument: biddings.hasPrDocument,
//       prNumber: biddings.prNumber,
      
//       // ═══════════════════════════════════════════════════════════════
//       // 가격 정보
//       // ═══════════════════════════════════════════════════════════════
//       currency: biddings.currency,
//       budget: biddings.budget,
//       targetPrice: biddings.targetPrice,
//       finalBidPrice: biddings.finalBidPrice,
      
//       // ═══════════════════════════════════════════════════════════════
//       // 상태 및 담당자
//       // ═══════════════════════════════════════════════════════════════
//       status: biddings.status,
//       isPublic: biddings.isPublic,
//       managerName: biddings.managerName,
//       managerEmail: biddings.managerEmail,
//       managerPhone: biddings.managerPhone,
      
//       // ═══════════════════════════════════════════════════════════════
//       // 메타 정보
//       // ═══════════════════════════════════════════════════════════════
//       remarks: biddings.remarks,
//       createdBy: biddings.createdBy,
//       createdAt: biddings.createdAt,
//       updatedAt: biddings.updatedAt,
//       updatedBy: biddings.updatedBy,
      
//       // ═══════════════════════════════════════════════════════════════
//       // 사양설명회 상세 정보
//       // ═══════════════════════════════════════════════════════════════
//       hasSpecificationMeetingDetails: sql<boolean>`${specificationMeetings.id} IS NOT NULL`.as('has_specification_meeting_details'),
//       meetingDate: specificationMeetings.meetingDate,
//       meetingLocation: specificationMeetings.location,
//       meetingContactPerson: specificationMeetings.contactPerson,
//       meetingIsRequired: specificationMeetings.isRequired,
      
//       // ═══════════════════════════════════════════════════════════════
//       // PR 문서 집계
//       // ═══════════════════════════════════════════════════════════════
//       prDocumentCount: sql<number>`
//         COALESCE((
//           SELECT count(*) 
//           FROM pr_documents 
//           WHERE bidding_id = ${biddings.id}
//         ), 0)
//       `.as('pr_document_count'),
      
//       // PR 문서 목록 (최대 5개 문서명)
//       prDocumentNames: sql<string[]>`
//         (
//           SELECT array_agg(document_name ORDER BY registered_at DESC)
//           FROM pr_documents 
//           WHERE bidding_id = ${biddings.id}
//           LIMIT 5
//         )
//       `.as('pr_document_names'),
      
//       // ═══════════════════════════════════════════════════════════════
//       // 참여 현황 집계 (핵심)
//       // ═══════════════════════════════════════════════════════════════
//       participantExpected: sql<number>`
//         COALESCE((
//           SELECT count(*) 
//           FROM bidding_companies 
//           WHERE bidding_id = ${biddings.id}
//         ), 0)
//       `.as('participant_expected'),
      
//       participantParticipated: sql<number>`
//         COALESCE((
//           SELECT count(*) 
//           FROM bidding_companies 
//           WHERE bidding_id = ${biddings.id} 
//           AND invitation_status = 'submitted'
//         ), 0)
//       `.as('participant_participated'),
      
//       participantDeclined: sql<number>`
//         COALESCE((
//           SELECT count(*) 
//           FROM bidding_companies 
//           WHERE bidding_id = ${biddings.id} 
//           AND invitation_status = 'declined'
//         ), 0)
//       `.as('participant_declined'),
      
//       participantPending: sql<number>`
//         COALESCE((
//           SELECT count(*) 
//           FROM bidding_companies 
//           WHERE bidding_id = ${biddings.id} 
//           AND invitation_status IN ('pending', 'sent')
//         ), 0)
//       `.as('participant_pending'),
      
//       participantAccepted: sql<number>`
//         COALESCE((
//           SELECT count(*) 
//           FROM bidding_companies 
//           WHERE bidding_id = ${biddings.id} 
//           AND invitation_status = 'accepted'
//         ), 0)
//       `.as('participant_accepted'),
      
//       // ═══════════════════════════════════════════════════════════════
//       // 참여율 계산
//       // ═══════════════════════════════════════════════════════════════
//       participationRate: sql<number>`
//         CASE 
//           WHEN (
//             SELECT count(*) 
//             FROM bidding_companies 
//             WHERE bidding_id = ${biddings.id}
//           ) > 0 
//           THEN ROUND(
//             (
//               SELECT count(*)::decimal 
//               FROM bidding_companies 
//               WHERE bidding_id = ${biddings.id} 
//               AND invitation_status = 'submitted'
//             ) / (
//               SELECT count(*)::decimal 
//               FROM bidding_companies 
//               WHERE bidding_id = ${biddings.id}
//             ) * 100, 1
//           )
//           ELSE 0 
//         END
//       `.as('participation_rate'),
      
//       // ═══════════════════════════════════════════════════════════════
//       // 견적 금액 통계
//       // ═══════════════════════════════════════════════════════════════
//       avgPreQuoteAmount: sql<number>`
//         (
//           SELECT AVG(pre_quote_amount) 
//           FROM bidding_companies 
//           WHERE bidding_id = ${biddings.id} 
//           AND pre_quote_amount IS NOT NULL
//         )
//       `.as('avg_pre_quote_amount'),
      
//       minPreQuoteAmount: sql<number>`
//         (
//           SELECT MIN(pre_quote_amount) 
//           FROM bidding_companies 
//           WHERE bidding_id = ${biddings.id} 
//           AND pre_quote_amount IS NOT NULL
//         )
//       `.as('min_pre_quote_amount'),
      
//       maxPreQuoteAmount: sql<number>`
//         (
//           SELECT MAX(pre_quote_amount) 
//           FROM bidding_companies 
//           WHERE bidding_id = ${biddings.id} 
//           AND pre_quote_amount IS NOT NULL
//         )
//       `.as('max_pre_quote_amount'),
      
//       avgFinalQuoteAmount: sql<number>`
//         (
//           SELECT AVG(final_quote_amount) 
//           FROM bidding_companies 
//           WHERE bidding_id = ${biddings.id} 
//           AND final_quote_amount IS NOT NULL
//         )
//       `.as('avg_final_quote_amount'),
      
//       minFinalQuoteAmount: sql<number>`
//         (
//           SELECT MIN(final_quote_amount) 
//           FROM bidding_companies 
//           WHERE bidding_id = ${biddings.id} 
//           AND final_quote_amount IS NOT NULL
//         )
//       `.as('min_final_quote_amount'),
      
//       maxFinalQuoteAmount: sql<number>`
//         (
//           SELECT MAX(final_quote_amount) 
//           FROM bidding_companies 
//           WHERE bidding_id = ${biddings.id} 
//           AND final_quote_amount IS NOT NULL
//         )
//       `.as('max_final_quote_amount'),
      
//       // ═══════════════════════════════════════════════════════════════
//       // 선정 및 낙찰 정보
//       // ═══════════════════════════════════════════════════════════════
//       selectedForFinalBidCount: sql<number>`
//         COALESCE((
//           SELECT count(*) 
//           FROM bidding_companies 
//           WHERE bidding_id = ${biddings.id} 
//           AND is_pre_quote_selected = true
//         ), 0)
//       `.as('selected_for_final_bid_count'),
      
//       winnerCount: sql<number>`
//         COALESCE((
//           SELECT count(*) 
//           FROM bidding_companies 
//           WHERE bidding_id = ${biddings.id} 
//           AND is_winner = true
//         ), 0)
//       `.as('winner_count'),
      
//       // 낙찰 업체명 목록
//       winnerCompanyNames: sql<string[]>`
//         (
//           SELECT array_agg(v.vendor_name ORDER BY v.vendor_name)
//           FROM bidding_companies bc
//           JOIN vendors v ON bc.company_id = v.id
//           WHERE bc.bidding_id = ${biddings.id} 
//           AND bc.is_winner = true
//         )
//       `.as('winner_company_names'),
      
//       // ═══════════════════════════════════════════════════════════════
//       // 일정 상태 계산
//       // ═══════════════════════════════════════════════════════════════
//       submissionStatus: sql<string>`
//         CASE 
//           WHEN ${biddings.submissionStartDate} IS NULL OR ${biddings.submissionEndDate} IS NULL 
//           THEN 'not_scheduled'
//           WHEN NOW() < ${biddings.submissionStartDate} 
//           THEN 'scheduled'
//           WHEN NOW() BETWEEN ${biddings.submissionStartDate} AND ${biddings.submissionEndDate} 
//           THEN 'active'
//           WHEN NOW() > ${biddings.submissionEndDate} 
//           THEN 'closed'
//           ELSE 'unknown'
//         END
//       `.as('submission_status'),
      
//       // 마감까지 남은 일수
//       daysUntilDeadline: sql<number>`
//         CASE 
//           WHEN ${biddings.submissionEndDate} IS NOT NULL 
//           AND NOW() < ${biddings.submissionEndDate} 
//           THEN EXTRACT(DAYS FROM (${biddings.submissionEndDate} - NOW()))::integer
//           ELSE NULL 
//         END
//       `.as('days_until_deadline'),
      
//       // 시작까지 남은 일수
//       daysUntilStart: sql<number>`
//         CASE 
//           WHEN ${biddings.submissionStartDate} IS NOT NULL 
//           AND NOW() < ${biddings.submissionStartDate} 
//           THEN EXTRACT(DAYS FROM (${biddings.submissionStartDate} - NOW()))::integer
//           ELSE NULL 
//         END
//       `.as('days_until_start'),
      
//       // ═══════════════════════════════════════════════════════════════
//       // 추가 유용한 계산 필드들
//       // ═══════════════════════════════════════════════════════════════
      
//       // 예산 대비 최저 견적 비율
//       budgetEfficiencyRate: sql<number>`
//         CASE 
//           WHEN ${biddings.budget} IS NOT NULL AND ${biddings.budget} > 0
//           AND (
//             SELECT MIN(final_quote_amount) 
//             FROM bidding_companies 
//             WHERE bidding_id = ${biddings.id} 
//             AND final_quote_amount IS NOT NULL
//           ) IS NOT NULL
//           THEN ROUND(
//             (
//               SELECT MIN(final_quote_amount) 
//               FROM bidding_companies 
//               WHERE bidding_id = ${biddings.id} 
//               AND final_quote_amount IS NOT NULL
//             ) / ${biddings.budget} * 100, 1
//           )
//           ELSE NULL 
//         END
//       `.as('budget_efficiency_rate'),
      
//       // 내정가 대비 최저 견적 비율
//       targetPriceEfficiencyRate: sql<number>`
//         CASE 
//           WHEN ${biddings.targetPrice} IS NOT NULL AND ${biddings.targetPrice} > 0
//           AND (
//             SELECT MIN(final_quote_amount) 
//             FROM bidding_companies 
//             WHERE bidding_id = ${biddings.id} 
//             AND final_quote_amount IS NOT NULL
//           ) IS NOT NULL
//           THEN ROUND(
//             (
//               SELECT MIN(final_quote_amount) 
//               FROM bidding_companies 
//               WHERE bidding_id = ${biddings.id} 
//               AND final_quote_amount IS NOT NULL
//             ) / ${biddings.targetPrice} * 100, 1
//           )
//           ELSE NULL 
//         END
//       `.as('target_price_efficiency_rate'),
      
//       // 입찰 진행 단계 점수 (0-100)
//       progressScore: sql<number>`
//         CASE ${biddings.status}
//           WHEN 'bidding_generated' THEN 10
//           WHEN 'request_for_quotation' THEN 20
//           WHEN 'received_quotation' THEN 40
//           WHEN 'set_target_price' THEN 60
//           WHEN 'bidding_opened' THEN 70
//           WHEN 'bidding_closed' THEN 80
//           WHEN 'evaluation_of_bidding' THEN 90
//           WHEN 'vendor_selected' THEN 100
//           WHEN 'bidding_disposal' THEN 0
//           ELSE 0
//         END
//       `.as('progress_score'),
      
//       // 마지막 활동일 (가장 최근 업체 응답일)
//       lastActivityDate: sql<Date>`
//         GREATEST(
//           ${biddings.updatedAt},
//           COALESCE((
//             SELECT MAX(updated_at) 
//             FROM bidding_companies 
//             WHERE bidding_id = ${biddings.id}
//           ), ${biddings.updatedAt})
//         )
//       `.as('last_activity_date'),
//     })
//     .from(biddings)
//     .leftJoin(
//       specificationMeetings, 
//       sql`${biddings.id} = ${specificationMeetings.biddingId}`
//     )
// )

// export type BiddingListView = typeof biddingListView.$inferSelect