summaryrefslogtreecommitdiff
path: root/lib/pq/pq-review-table-new/vendors-table-columns.tsx
blob: 0491f1dc718d65f6b9d2cba4c461e0bd2c84f57b (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
"use client"

import * as React from "react"
import { type DataTableRowAction } from "@/types/table"
import { type ColumnDef } from "@tanstack/react-table"
import { Ellipsis, Eye, PaperclipIcon, FileEdit } from "lucide-react"

import { formatDate } from "@/lib/utils"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { DataTableColumnHeader } from "@/components/data-table/data-table-column-header"
import { useRouter } from "next/navigation"

// PQ 제출 타입 정의
export interface PQSubmission {
  id: number
  pqNumber: string
  type: string
  status: string
  requesterName: string | null  // 요청자 이름
  createdAt: Date
  updatedAt: Date
  submittedAt: Date | null
  approvedAt: Date | null
  rejectedAt: Date | null
  rejectReason: string | null
  vendorId: number
  vendorName: string
  vendorCode: string
  taxId: string
  vendorStatus: string
  projectId: number | null
  projectName: string | null
  projectCode: string | null
  answerCount: number
  attachmentCount: number
  pqStatus: string
  pqTypeLabel: string
  investigation: {
    id: number
    investigationStatus: string
    requesterName: string | null  // 실사 요청자 이름
    evaluationType: "SITE_AUDIT" | "QM_SELF_AUDIT" | null
    qmManagerId: number | null
    qmManagerName: string | null // QM 담당자 이름
    qmManagerEmail: string | null // QM 담당자 이메일
    investigationAddress: string | null
    investigationMethod: string | null
    scheduledStartAt: Date | null
    scheduledEndAt: Date | null
    requestedAt: Date | null
    confirmedAt: Date | null
    completedAt: Date | null
    forecastedAt: Date | null
    evaluationScore: number | null
    evaluationResult: "APPROVED" | "SUPPLEMENT" | "REJECTED" | null
    investigationNotes: string | null
  } | null
  // 통합 상태를 위한 새 필드
  combinedStatus: {
    status: string
    label: string
    variant: "default" | "outline" | "secondary" | "destructive" | "success"
  }
}

type NextRouter = ReturnType<typeof useRouter>;

interface GetColumnsProps {
  setRowAction: React.Dispatch<React.SetStateAction<DataTableRowAction<PQSubmission> | null>>;
  router: NextRouter;
}

// 상태에 따른 Badge 변형 결정 함수
function getStatusBadge(status: string) {
  switch (status) {
    case "REQUESTED":
      return <Badge variant="outline">요청됨</Badge>
    case "IN_PROGRESS":
      return <Badge variant="secondary">진행 중</Badge>
    case "SUBMITTED":
      return <Badge>제출됨</Badge>
    case "APPROVED":
      return <Badge variant="success">승인됨</Badge>
    case "REJECTED":
      return <Badge variant="destructive">거부됨</Badge>
    default:
      return <Badge variant="outline">{status}</Badge>
  }
}

/**
 * tanstack table 컬럼 정의
 */
export function getColumns({ setRowAction, router }: GetColumnsProps): ColumnDef<PQSubmission>[] {
  // ----------------------------------------------------------------
  // 1) select 컬럼 (체크박스)
  // ----------------------------------------------------------------
  const selectColumn: ColumnDef<PQSubmission> = {
    id: "select",
    header: ({ table }) => (
      <Checkbox
        checked={
          table.getIsAllPageRowsSelected() ||
          (table.getIsSomePageRowsSelected() && "indeterminate")
        }
        onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
        aria-label="Select all"
        className="translate-y-0.5"
      />
    ),
    cell: ({ row }) => (
      <Checkbox
        checked={row.getIsSelected()}
        onCheckedChange={(value) => row.toggleSelected(!!value)}
        aria-label="Select row"
        className="translate-y-0.5"
      />
    ),
    size: 40,
    enableSorting: false,
    enableHiding: false,
  }

  // ----------------------------------------------------------------
  // 2) 일반 컬럼들
  // --------------------------
  // --------------------------------------

  const pqNoColumn: ColumnDef<PQSubmission> = {
    accessorKey: "pqNumber",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="PQ No." />
    ),
    cell: ({ row }) => (
      <div className="flex flex-col">
        <span className="font-medium">{row.getValue("pqNumber")}</span>
      </div>
    ),
  }

  // 협력업체 컬럼
  const vendorColumn: ColumnDef<PQSubmission> = {
    accessorKey: "vendorName",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="협력업체" />
    ),
    cell: ({ row }) => (
      <div className="flex flex-col">
        <span className="font-medium">{row.getValue("vendorName")}</span>
        <span className="text-xs text-muted-foreground">{row.original.vendorCode ? row.original.vendorCode : "-"}/{row.original.taxId}</span>
      </div>
    ),
  }

  // PQ 유형 컬럼
  const typeColumn: ColumnDef<PQSubmission> = {
    accessorKey: "type",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="PQ 유형" />
    ),
    cell: ({ row }) => {
      return (
        <div className="flex items-center">
          <Badge variant={row.original.type === "PROJECT" ? "default" : "outline"}>
            {row.original.pqTypeLabel}
          </Badge>
        </div>
      )
    },
    filterFn: (row, id, value) => {
      return value.includes(row.getValue(id))
    },
  }

  // 프로젝트 컬럼
  const projectColumn: ColumnDef<PQSubmission> = {
    accessorKey: "projectName",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="프로젝트" />
    ),
    cell: ({ row }) => {
      const projectName = row.original.projectName
      const projectCode = row.original.projectCode

      if (!projectName) {
        return <span className="text-muted-foreground">-</span>
      }

      return (
        <div className="flex flex-col">
          <span>{projectName}</span>
          {projectCode && (
            <span className="text-xs text-muted-foreground">{projectCode}</span>
          )}
        </div>
      )
    },
  }

  // 상태 컬럼
  const statusColumn: ColumnDef<PQSubmission> = {
    accessorKey: "combinedStatus",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="진행현황" />
    ),
    cell: ({ row }) => {
      const combinedStatus = getCombinedStatus(row.original);
      return <Badge variant={combinedStatus.variant}>{combinedStatus.label}</Badge>;
    },
    filterFn: (row, id, value) => {
      const combinedStatus = getCombinedStatus(row.original);
      return value.includes(combinedStatus.status);
    },
  };

  // PQ 상태와 실사 상태를 결합하는 헬퍼 함수
  function getCombinedStatus(submission: PQSubmission) {
    // PQ가 승인되지 않은 경우, PQ 상태를 우선 표시
    if (submission.status !== "APPROVED") {
      switch (submission.status) {
        case "REQUESTED":
          return { status: "PQ_REQUESTED", label: "PQ 요청됨", variant: "outline" as const };
        case "IN_PROGRESS":
          return { status: "PQ_IN_PROGRESS", label: "PQ 진행 중", variant: "secondary" as const };
        case "SUBMITTED":
          return { status: "PQ_SUBMITTED", label: "PQ 제출됨", variant: "default" as const };
        case "REJECTED":
          return { status: "PQ_REJECTED", label: "PQ 거부됨", variant: "destructive" as const };
        default:
          return { status: submission.status, label: submission.status, variant: "outline" as const };
      }
    }

    // PQ가 승인되었지만 실사가 없는 경우
    if (!submission.investigation) {
      return { status: "PQ_APPROVED", label: "PQ 승인됨", variant: "success" as const };
    }

    // PQ가 승인되고 실사가 있는 경우
    switch (submission.investigation.investigationStatus) {
      case "PLANNED":
        return { status: "INVESTIGATION_PLANNED", label: "실사 계획됨", variant: "outline" as const };
      case "IN_PROGRESS":
        return { status: "INVESTIGATION_IN_PROGRESS", label: "실사 진행 중", variant: "secondary" as const };
      case "COMPLETED":
        // 실사 완료 후 평가 결과에 따라 다른 상태 표시
        if (submission.investigation.evaluationResult) {
          switch (submission.investigation.evaluationResult) {
            case "APPROVED":
              return { status: "INVESTIGATION_APPROVED", label: "실사 승인", variant: "success" as const };
            case "SUPPLEMENT":
              return { status: "INVESTIGATION_SUPPLEMENT", label: "실사 보완필요", variant: "secondary" as const };
            case "REJECTED":
              return { status: "INVESTIGATION_REJECTED", label: "실사 불가", variant: "destructive" as const };
            default:
              return { status: "INVESTIGATION_COMPLETED", label: "실사 완료", variant: "default" as const };
          }
        }
        return { status: "INVESTIGATION_COMPLETED", label: "실사 완료", variant: "default" as const };
      case "CANCELED":
        return { status: "INVESTIGATION_CANCELED", label: "실사 취소됨", variant: "destructive" as const };
      default:
        return {
          status: `INVESTIGATION_${submission.investigation.investigationStatus}`,
          label: `실사 ${submission.investigation.investigationStatus}`,
          variant: "outline" as const
        };
    }
  }

  const evaluationTypeColumn: ColumnDef<PQSubmission> = {
    accessorKey: "evaluationType",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="평가 유형" />
    ),
    cell: ({ row }) => {
      const investigation = row.original.investigation;

      if (!investigation || !investigation.evaluationType) {
        return <span className="text-muted-foreground">-</span>;
      }

      switch (investigation.evaluationType) {
        case "SITE_AUDIT":
          return <Badge variant="outline">실사의뢰평가</Badge>;
        case "QM_SELF_AUDIT":
          return <Badge variant="secondary">QM자체평가</Badge>;
        default:
          return <span>{investigation.evaluationType}</span>;
      }
    },
    filterFn: (row, id, value) => {
      const investigation = row.original.investigation;
      if (!investigation || !investigation.evaluationType) return value.includes("null");
      return value.includes(investigation.evaluationType);
    },
  };


  const evaluationResultColumn: ColumnDef<PQSubmission> = {
    accessorKey: "evaluationResult",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="평가 결과" />
    ),
    cell: ({ row }) => {
      const investigation = row.original.investigation;

      if (!investigation || !investigation.evaluationResult) {
        return <span className="text-muted-foreground">-</span>;
      }

      switch (investigation.evaluationResult) {
        case "APPROVED":
          return <Badge variant="success">승인</Badge>;
        case "SUPPLEMENT":
          return <Badge variant="secondary">보완</Badge>;
        case "REJECTED":
          return <Badge variant="destructive">불가</Badge>;
        default:
          return <span>{investigation.evaluationResult}</span>;
      }
    },
    filterFn: (row, id, value) => {
      const investigation = row.original.investigation;
      if (!investigation || !investigation.evaluationResult) return value.includes("null");
      return value.includes(investigation.evaluationResult);
    },
  };

  // 답변 수 컬럼
  const answerCountColumn: ColumnDef<PQSubmission> = {
    accessorKey: "answerCount",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="답변 수" />
    ),
    cell: ({ row }) => {
      return (
        <div className="flex items-center gap-2">
          <span>{row.original.answerCount}</span>
        </div>
      )
    },
  }

  const investigationAddressColumn: ColumnDef<PQSubmission> = {
    accessorKey: "investigationAddress",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="실사 주소" />
    ),
    cell: ({ row }) => {
      const investigation = row.original.investigation;

      if (!investigation || !investigation.evaluationType) {
        return <span className="text-muted-foreground">-</span>;
      }

      return (
        <div className="flex items-center gap-2">
          <span>{investigation.investigationAddress}</span>
        </div>
      )
    },
  }

  const investigationNotesColumn: ColumnDef<PQSubmission> = {
    accessorKey: "investigationNotes",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="QM 의견" />
    ),
    cell: ({ row }) => {
      const investigation = row.original.investigation;

      if (!investigation || !investigation.investigationNotes) {
        return <span className="text-muted-foreground">-</span>;
      }

      return (
        <div className="flex items-center gap-2">
          <span>{investigation.investigationNotes}</span>
        </div>
      )
    },
  }


  const investigationRequestedAtColumn: ColumnDef<PQSubmission> = {
    accessorKey: "investigationRequestedAt",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="실사 의뢰일" />
    ),
    cell: ({ row }) => {
      const investigation = row.original.investigation;

      if (!investigation || !investigation.requestedAt) {
        return <span className="text-muted-foreground">-</span>;
      }
      const dateVal = investigation.requestedAt

      return (
        <div className="flex items-center gap-2">
          <span>{dateVal ? formatDate(dateVal, 'KR') : "-"}</span>
        </div>
      )
    },
  }


  const investigationForecastedAtColumn: ColumnDef<PQSubmission> = {
    accessorKey: "investigationForecastedAt",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="실사 예정일" />
    ),
    cell: ({ row }) => {
      const investigation = row.original.investigation;

      if (!investigation || !investigation.forecastedAt) {
        return <span className="text-muted-foreground">-</span>;
      }
      const dateVal = investigation.forecastedAt

      return (
        <div className="flex items-center gap-2">
          <span>{dateVal ? formatDate(dateVal, 'KR') : "-"}</span>
        </div>
      )
    },
  }

  const investigationConfirmedAtColumn: ColumnDef<PQSubmission> = {
    accessorKey: "investigationConfirmedAt",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="실사 확정일" />
    ),
    cell: ({ row }) => {
      const investigation = row.original.investigation;

      if (!investigation || !investigation.confirmedAt) {
        return <span className="text-muted-foreground">-</span>;
      }
      const dateVal = investigation.confirmedAt

      return (
        <div className="flex items-center gap-2">
          <span>{dateVal ? formatDate(dateVal, 'KR') : "-"}</span>
        </div>
      )
    },
  }

  const investigationCompletedAtColumn: ColumnDef<PQSubmission> = {
    accessorKey: "investigationCompletedAt",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="실제 실사일" />
    ),
    cell: ({ row }) => {
      const investigation = row.original.investigation;

      if (!investigation || !investigation.completedAt) {
        return <span className="text-muted-foreground">-</span>;
      }
      const dateVal = investigation.completedAt

      return (
        <div className="flex items-center gap-2">
          <span>{dateVal ? formatDate(dateVal, 'KR') : "-"}</span>
        </div>
      )
    },
  }

  // 제출일 컬럼
  const createdAtColumn: ColumnDef<PQSubmission> = {
    accessorKey: "createdAt",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="PQ 전송일" />
    ),
    cell: ({ row }) => {
      const dateVal = row.original.createdAt as Date
      return formatDate(dateVal, 'KR')
    },
  }

  // 제출일 컬럼
  const submittedAtColumn: ColumnDef<PQSubmission> = {
    accessorKey: "submittedAt",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="PQ 회신일" />
    ),
    cell: ({ row }) => {
      const dateVal = row.original.submittedAt as Date
      return dateVal ? formatDate(dateVal, 'KR') : "-"
    },
  }

  // 승인/거부일 컬럼
  const approvalDateColumn: ColumnDef<PQSubmission> = {
    accessorKey: "approvedAt",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="PQ 승인/거부일" />
    ),
    cell: ({ row }) => {
      if (row.original.approvedAt) {
        return <span className="text-green-600">{formatDate(row.original.approvedAt)}</span>
      }
      if (row.original.rejectedAt) {
        return <span className="text-red-600">{formatDate(row.original.rejectedAt)}</span>
      }
      return "-"
    },
  }

  // ----------------------------------------------------------------
  // 3) actions 컬럼 (Dropdown 메뉴)
  // ----------------------------------------------------------------
  const actionsColumn: ColumnDef<PQSubmission> = {
    id: "actions",
    enableHiding: false,
    cell: function Cell({ row }) {
      const pq = row.original
      const isSubmitted = pq.status === "SUBMITTED"
      const reviewUrl = `/evcp/pq_new/${pq.vendorId}/${pq.id}`

      return (
        <DropdownMenu>
          <DropdownMenuTrigger asChild>
            <Button
              aria-label="Open menu"
              variant="ghost"
              className="flex size-8 p-0 data-[state=open]:bg-muted"
            >
              <Ellipsis className="size-4" aria-hidden="true" />
            </Button>
          </DropdownMenuTrigger>
          <DropdownMenuContent align="end" className="w-40">
            <DropdownMenuItem
              onSelect={() => {
                router.push(reviewUrl);
              }}
            >
              {isSubmitted ? (
                <>
                  <FileEdit className="mr-2 h-4 w-4" />
                  검토
                </>
              ) : (
                <>
                  <Eye className="mr-2 h-4 w-4" />
                  보기
                </>
              )}
            </DropdownMenuItem>
          </DropdownMenuContent>
        </DropdownMenu>
      )
    },
    size: 40,
  }

  // 요청자 컬럼 추가
const requesterColumn: ColumnDef<PQSubmission> = {
  accessorKey: "requesterName",
  header: ({ column }) => (
    <DataTableColumnHeader column={column} title="PQ/실사 요청자" />
  ),
  cell: ({ row }) => {
    // PQ 요청자와 실사 요청자를 모두 표시
    const pqRequesterName = row.original.requesterName;
    const investigationRequesterName = row.original.investigation?.requesterName;
    
    // 상태에 따라 적절한 요청자 표시
    const status = getCombinedStatus(row.original).status;
    
    if (status.startsWith('INVESTIGATION_') && investigationRequesterName) {
      return <span>{investigationRequesterName}</span>;
    }
    
    return pqRequesterName 
      ? <span>{pqRequesterName}</span> 
      : <span className="text-muted-foreground">-</span>;
  },
};
const qmManagerColumn: ColumnDef<PQSubmission> = {
  accessorKey: "qmManager",
  header: ({ column }) => (
    <DataTableColumnHeader column={column} title="QM 담당자" />
  ),
  cell: ({ row }) => {
    const investigation = row.original.investigation;
    
    if (!investigation || !investigation.qmManagerName) {
      return <span className="text-muted-foreground">-</span>;
    }
    
    return (
      <div className="flex flex-col">
        <span>{investigation.qmManagerName}</span>
        {investigation.qmManagerEmail && (
          <span className="text-xs text-muted-foreground">{investigation.qmManagerEmail}</span>
        )}
      </div>
    );
  },
};


  // ----------------------------------------------------------------
  // 4) 최종 컬럼 배열
  // ----------------------------------------------------------------
  return [
    selectColumn,
    statusColumn,       // 통합된 진행현황 컬럼
    pqNoColumn,
    vendorColumn,
    investigationAddressColumn,
    typeColumn,
    projectColumn,
    createdAtColumn,
    submittedAtColumn,
    approvalDateColumn,
    answerCountColumn,
    evaluationTypeColumn,  // 평가 유형 컬럼
    investigationForecastedAtColumn,
    investigationRequestedAtColumn,
    investigationConfirmedAtColumn,
    investigationCompletedAtColumn,
    evaluationResultColumn, // 평가 결과 컬럼
    requesterColumn,
    qmManagerColumn,
    investigationNotesColumn,
    actionsColumn,
  ];
}