From 70a9eece8bbee2ee859d51751519d34ce1acb07e Mon Sep 17 00:00:00 2001 From: jiseob park Date: Sun, 11 Aug 2024 23:10:23 +0900 Subject: [PATCH] =?UTF-8?q?PurchaseOrderForm=20=EA=B8=B0=EB=B3=B8=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/PurchaseOrderForm/index.tsx | 116 +++++++++++++++++++++ src/route/MainRoutes/index.tsx | 2 + src/service/api.ts | 17 ++- src/types/item.ts | 12 +++ src/types/purchaseOrder.ts | 11 ++ src/types/purchaseOrderItem.ts | 11 ++ src/types/supplier.ts | 10 ++ 7 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 src/components/PurchaseOrderForm/index.tsx create mode 100644 src/types/item.ts create mode 100644 src/types/purchaseOrder.ts create mode 100644 src/types/purchaseOrderItem.ts create mode 100644 src/types/supplier.ts diff --git a/src/components/PurchaseOrderForm/index.tsx b/src/components/PurchaseOrderForm/index.tsx new file mode 100644 index 00000000..23e0dc82 --- /dev/null +++ b/src/components/PurchaseOrderForm/index.tsx @@ -0,0 +1,116 @@ +import { createPurchaseOrder, getAllItems, getAllSuppliers } from "@/service/api"; +import { Button, Card, DatePicker, Form, FormLayout, LegacyStack, Page, Range, Select, TextContainer, TextField } from "@shopify/polaris"; +import React, { useState, useEffect } from "react"; + +const PurchaseOrderForm = () => { + + const [supplierId, setSupplierId] = useState(''); + const [dueDate, setDueDate] = useState(new Date()); + const [memo, setMemo] = useState(''); + const [items, setItems] = useState([{ itemId: '', quantity: '' }]); + const [availableItems, setAvailableItems] = useState<{ label: string, value: string }[]>([]); + const [suppliers, setSuppliers] = useState<{ label: string, value: string }[]>([]); + + useEffect(() => { + const fetchSuppliersAndItems = async () => { + const suppliersResponse = await getAllSuppliers(); + setSuppliers(suppliersResponse.map(supplier => ({ + label: supplier.supplierName, + value: supplier.id.toString() + }))); + + const itemsResponse = await getAllItems(); + setAvailableItems(itemsResponse.map(item => ({ + label: item.name, + value: item.id.toString() + }))); + }; + + fetchSuppliersAndItems(); + }, []); + + const handleItemChange = (index: number, value: string, name: string) => { + const newItems = [...items]; + newItems[index] = { ...newItems[index], [name]: value }; + setItems(newItems); + } + const handleSubmit = async () => { + interface PurchaseOrderItem { + item: { id: number }; + quantity: number; + } + + const purchaseOrder = { + orderNumber: '', // Add the orderNumber property + orderDate: new Date(), + dueDate: new Date(dueDate), + status: '', // Add the status property + // items: items.map((item): PurchaseOrderItem => ({ + // item: { id: parseInt(item.itemId, 10) }, + // quantity: parseInt(item.quantity, 10) + // })), + // supplier: { id: parseInt(supplierId, 10) }, + }; + await createPurchaseOrder(purchaseOrder); + }; + + const handleAddItem = () => setItems([...items, { itemId: '', quantity: '' }]); + + const handleDateChange = (date: Range) => { + setDueDate(date.start); + }; + + return ( + + +
+ + handleItemChange(index, value, 'itemId')} + /> + handleItemChange(index, value, 'quantity')} + /> + + ))} + + + + +
+
+
+ ); +}; + +export default PurchaseOrderForm; diff --git a/src/route/MainRoutes/index.tsx b/src/route/MainRoutes/index.tsx index 13788849..b46622cc 100644 --- a/src/route/MainRoutes/index.tsx +++ b/src/route/MainRoutes/index.tsx @@ -11,6 +11,7 @@ import InsertProduct from '../InsertProduct'; import Product from '../Product'; import SellerDashboard from '../../components/SellerDashboard'; import InventoryManagement from '../InventoryManagement'; +import PurchaseOrderForm from '@/components/PurchaseOrderForm'; const MainRoutes = () => { console.log("hihi mainroutes") @@ -27,6 +28,7 @@ const MainRoutes = () => { } /> } /> } /> + } /> ) }; diff --git a/src/service/api.ts b/src/service/api.ts index 3cc86140..bd53302e 100644 --- a/src/service/api.ts +++ b/src/service/api.ts @@ -1,6 +1,9 @@ import { ExpectedReceipt } from "@/types/expectedReceipt"; -import { Receipt } from "@/types/Receipt"; +import { PurchaseOrder } from "@/types/purchaseOrder"; +import { Receipt } from "../types/receipt"; +import { Supplier } from "@/types/supplier"; import axios from "axios"; +import { Item } from "@/types/item"; const api = axios.create({ baseURL: process.env.BASE_URL || 'http://localhost:8080/api' @@ -29,4 +32,16 @@ export const getReceipts = async (): Promise => { return handleApiCall(api.get('/receipts')); } +//purchase order + +export const createPurchaseOrder = async (purchaseOrder: PurchaseOrder) => { + handleApiCall(api.post('/purchase-orders', purchaseOrder)); +}; +export const getAllSuppliers = async (): Promise => { + return handleApiCall(api.get('/suppliers')); +}; +export const getAllItems = async (): Promise => { + return await handleApiCall(api.get('/items')); +}; + export default api; \ No newline at end of file diff --git a/src/types/item.ts b/src/types/item.ts new file mode 100644 index 00000000..baebae28 --- /dev/null +++ b/src/types/item.ts @@ -0,0 +1,12 @@ +export interface Item { + id: number; + name: string; + specification: string; + unit: string; + category: string; + barcode: string; + image: string; + price: string; + stockAlertLevel?: number; + // inventories: Inventory[]; +} \ No newline at end of file diff --git a/src/types/purchaseOrder.ts b/src/types/purchaseOrder.ts new file mode 100644 index 00000000..b7349107 --- /dev/null +++ b/src/types/purchaseOrder.ts @@ -0,0 +1,11 @@ +import { PurchaseOrderItem } from "./purchaseOrderItem"; +import { Supplier } from "./supplier"; + +export interface PurchaseOrder { + orderNumber: string; + orderDate: Date; // LocalDate -> string + dueDate: Date; // LocalDate -> string + status: string; + items?: PurchaseOrderItem[]; // 1개의 구매 주문에는 여러개의 purchaseOrderItem이 있을 수 있다. + supplier?: Supplier; // 여러개의 구매 주문은 1개의 공급자에게 발주될 수 있다. +} \ No newline at end of file diff --git a/src/types/purchaseOrderItem.ts b/src/types/purchaseOrderItem.ts new file mode 100644 index 00000000..243aff3d --- /dev/null +++ b/src/types/purchaseOrderItem.ts @@ -0,0 +1,11 @@ +import { Item } from "./item"; +import { PurchaseOrder } from "./purchaseOrder"; + +export interface PurchaseOrderItem { + id: number; + productCode: number; + quantity: number; + unitPrice: number; + purchaseOrder: PurchaseOrder; + item: Item; +} \ No newline at end of file diff --git a/src/types/supplier.ts b/src/types/supplier.ts new file mode 100644 index 00000000..1e9fb954 --- /dev/null +++ b/src/types/supplier.ts @@ -0,0 +1,10 @@ +import { PurchaseOrder } from "./purchaseOrder"; + +export interface Supplier { + id: number; + supplierName: string; + contactInfo: string; + address: String; + supplierType: string; // Change to Enum + purchaseOrder: PurchaseOrder[]; +} \ No newline at end of file