summaryrefslogtreecommitdiff
path: root/lib/itb/table/purchase-request-columns.tsx
blob: e6489c186cf157e35e8d854d629d13386ddd6029 (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
// components/purchase-requests/purchase-request-columns.tsx
"use client";

import { type ColumnDef } from "@tanstack/react-table";
import { Checkbox } from "@/components/ui/checkbox";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import {
    Eye,
    Edit,
    Trash2,
    CheckCircle,
    XCircle,
    FileText,
    Package,
    Send,
    Clock
} from "lucide-react";
import {
    DropdownMenu,
    DropdownMenuContent,
    DropdownMenuItem,
    DropdownMenuLabel,
    DropdownMenuSeparator,
    DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { MoreHorizontal } from "lucide-react";
import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header";
import { format } from "date-fns";
import { ko } from "date-fns/locale";
import type { DataTableRowAction } from "@/types/table";
import type { PurchaseRequestView } from "@/db/schema";

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

const statusConfig = {
    "작성중": {
        label: "작성중",
        variant: "secondary" as const,
        icon: Edit,
        color: "text-gray-500"
    },
    "요청완료": {
        label: "요청완료",
        variant: "default" as const,
        icon: Send,
        color: "text-blue-500"
    },
    "검토중": {
        label: "검토중",
        variant: "warning" as const,
        icon: Clock,
        color: "text-yellow-500"
    },
    "승인": {
        label: "승인",
        variant: "success" as const,
        icon: CheckCircle,
        color: "text-green-500"
    },
    "반려": {
        label: "반려",
        variant: "destructive" as const,
        icon: XCircle,
        color: "text-red-500"
    },
    "RFQ생성완료": {
        label: "RFQ생성완료",
        variant: "outline" as const,
        icon: FileText,
        color: "text-purple-500"
    },
};

export function getPurchaseRequestColumns({
    setRowAction
}: GetColumnsProps): ColumnDef<PurchaseRequestView>[] {
    return [
        {
            id: "select",
            header: ({ table }) => (
                <Checkbox
                    checked={
                        table.getIsAllPageRowsSelected() ||
                        (table.getIsSomePageRowsSelected() && "indeterminate")
                    }
                    onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
                    aria-label="Select all"
                />
            ),
            cell: ({ row }) => (
                <Checkbox
                    checked={row.getIsSelected()}
                    onCheckedChange={(value) => row.toggleSelected(!!value)}
                    aria-label="Select row"
                />
            ),
            enableSorting: false,
            enableHiding: false,
            size: 40,
        },
        {
            accessorKey: "requestCode",
            header: ({ column }) => (
                <DataTableColumnHeaderSimple column={column} title="요청번호" />
            ),
            cell: ({ row }) => (
                <Button
                    variant="ghost"
                    className="h-auto p-0 font-mono font-medium text-primary hover:underline"
                    onClick={() => setRowAction({ row, type: "view" })}
                >
                    {row.getValue("requestCode")}
                </Button>
            ),
            size: 130,
        },
        {
            accessorKey: "status",
            header: ({ column }) => (
                <DataTableColumnHeaderSimple column={column} title="상태" />
            ),
            cell: ({ row }) => {
                const status = row.getValue("status") as keyof typeof statusConfig;
                const config = statusConfig[status];
                const Icon = config?.icon;

                return (
                    <Badge variant={config?.variant} className="font-medium">
                        {Icon && <Icon className={`mr-1 h-3 w-3 ${config.color}`} />}
                        {config?.label || status}
                    </Badge>
                );
            },
            filterFn: (row, id, value) => {
                return value.includes(row.getValue(id));
            },
            size: 120,
        },
        {
            accessorKey: "requestTitle",
            header: ({ column }) => (
                <DataTableColumnHeaderSimple column={column} title="요청제목" />
            ),
            cell: ({ row }) => (
                <div className="max-w-[300px]">
                    <div className="truncate font-medium" title={row.getValue("requestTitle")}>
                        {row.getValue("requestTitle")}
                    </div>
                    {row.original.requestDescription && (
                        <div className="truncate text-xs text-muted-foreground mt-0.5"
                            title={row.original.requestDescription}>
                            {row.original.requestDescription}
                        </div>
                    )}
                </div>
            ),
            maxSize: 300,
        },
        {
            accessorKey: "projectName",
            header: ({ column }) => (
                <DataTableColumnHeaderSimple column={column} title="프로젝트" />
            ),
            cell: ({ row }) => (
                <div className="flex flex-col space-y-0.5">
                    {row.original.projectCode && (
                        <span className="font-mono text-xs text-muted-foreground">
                            {row.original.projectCode}
                        </span>
                    )}
                    <span className="truncate text-sm" title={row.original.projectName}>
                        {row.original.projectName || "-"}
                    </span>
                </div>
            ),
            size: 180,
        },
        {
            accessorKey: "packageName",
            header: ({ column }) => (
                <DataTableColumnHeaderSimple column={column} title="패키지" />
            ),
            cell: ({ row }) => (
                <div className="flex flex-col space-y-0.5">
                    {row.original.packageNo && (
                        <span className="font-mono text-xs text-muted-foreground">
                            {row.original.packageNo}
                        </span>
                    )}
                    <span className="truncate text-sm" title={row.original.packageName}>
                        {row.original.packageName || "-"}
                    </span>
                </div>
            ),
            size: 150,
        },
        {
            id: "attachments",
            header: "첨부",
            cell: ({ row }) => {
                const count = Number(row.original.attachmentCount) || 0;
                return count > 0 ? (
                    <Button
                        variant="ghost"
                        size="sm"
                        className="h-8 px-2"
                        onClick={() => setRowAction({ row, type: "attachments" })}
                    >
                        <FileText className="h-4 w-4 mr-1" />
                        {count}
                    </Button>
                ) : (
                    <span className="text-muted-foreground text-sm">-</span>
                );
            },
            size: 70,
        },
        {
            id: "items",
            header: "품목",
            cell: ({ row }) => {
                const count = row.original.itemCount || 0;
                const totalQuantity = row.original.totalQuantity;
                return count > 0 ? (
                    <Button
                        variant="ghost"
                        size="sm"
                        className="h-8 px-2"
                        onClick={() => setRowAction({ row, type: "items" })}
                    >
                        <Package className="h-4 w-4 mr-1" />
                        <div className="flex flex-col items-start">
                            <span>{count}종</span>
                            {totalQuantity && (
                                <span className="text-xs text-muted-foreground">
                                    총 {totalQuantity}개
                                </span>
                            )}
                        </div>
                    </Button>
                ) : (
                    <span className="text-muted-foreground text-sm">-</span>
                );
            },
            size: 90,
        },
        {
            accessorKey: "engPicName",
            header: ({ column }) => (
                <DataTableColumnHeaderSimple column={column} title="설계담당" />
            ),
            cell: ({ row }) => (
                <div className="text-sm">
                    {row.getValue("engPicName") || "-"}
                </div>
            ),
            size: 100,
        },
        {
            accessorKey: "purchasePicName",
            header: ({ column }) => (
                <DataTableColumnHeaderSimple column={column} title="구매담당" />
            ),
            cell: ({ row }) => (
                <div className="text-sm">
                    {row.getValue("purchasePicName") || "-"}
                </div>
            ),
            size: 100,
        },
        {
            accessorKey: "estimatedBudget",
            header: ({ column }) => (
                <DataTableColumnHeaderSimple column={column} title="예상예산" />
            ),
            cell: ({ row }) => (
                <div className="text-sm font-mono">
                    {row.getValue("estimatedBudget") || "-"}
                </div>
            ),
            size: 100,
        },
        {
            accessorKey: "requestedDeliveryDate",
            header: ({ column }) => (
                <DataTableColumnHeaderSimple column={column} title="희망납기" />
            ),
            cell: ({ row }) => {
                const date = row.getValue("requestedDeliveryDate") as Date | null;
                return (
                    <div className="text-sm">
                        {date ? format(new Date(date), "yyyy-MM-dd", { locale: ko }) : "-"}
                    </div>
                );
            },
            size: 100,
        },
        {
            accessorKey: "createdAt",
            header: ({ column }) => (
                <DataTableColumnHeaderSimple column={column} title="등록일" />
            ),
            cell: ({ row }) => {
                const date = row.getValue("createdAt") as Date;
                return (
                    <div className="text-sm text-muted-foreground">
                        {date ? format(new Date(date), "yyyy-MM-dd", { locale: ko }) : "-"}
                    </div>
                );
            },
            size: 100,
        },
        {
            id: "actions",
            header: "작업",
            cell: ({ row }) => {
                const status = row.original.status;

                return (
                    <DropdownMenu>
                        <DropdownMenuTrigger asChild>
                            <Button variant="ghost" className="h-8 w-8 p-0">
                                <span className="sr-only">메뉴 열기</span>
                                <MoreHorizontal className="h-4 w-4" />
                            </Button>
                        </DropdownMenuTrigger>
                        <DropdownMenuContent align="end">
                            <DropdownMenuLabel>작업</DropdownMenuLabel>
                            <DropdownMenuItem
                                onClick={() => setRowAction({ row, type: "view" })}
                            >
                                <Eye className="mr-2 h-4 w-4" />
                                상세보기
                            </DropdownMenuItem>
                            <DropdownMenuSeparator />



                            <DropdownMenuItem
                                onClick={() => setRowAction({ row, type: "update" })}
                            >
                                <Edit className="mr-2 h-4 w-4" />
                                수정
                            </DropdownMenuItem>
                            <DropdownMenuItem
                                onClick={() => setRowAction({ row, type: "delete" })}
                                className="text-destructive"
                            >
                                <Trash2 className="mr-2 h-4 w-4" />
                                삭제
                            </DropdownMenuItem>




{status ==="작성중" && 
<>
                            <DropdownMenuSeparator />
                            <DropdownMenuItem
                                onClick={() => setRowAction({ row, type: "createRfq" })}
                                className="text-primary"
                            >
                                <FileText className="mr-2 h-4 w-4" />
                                RFQ 생성
                            </DropdownMenuItem>
                            </>
                        }

                        </DropdownMenuContent>
                    </DropdownMenu>

                );
            },
            size: 80,
        },
    ];
}