diff --git a/src/pages/Inventory/Adjusts/index.tsx b/src/pages/Inventory/Adjusts/index.tsx index ce1879e..5378f8e 100644 --- a/src/pages/Inventory/Adjusts/index.tsx +++ b/src/pages/Inventory/Adjusts/index.tsx @@ -1,8 +1,9 @@ -import { PlusOutlined } from '@ant-design/icons'; -import { ProColumns, ProTable } from '@ant-design/pro-components'; -import { Button, Tag } from 'antd'; -import { listStockAdjusts } from '@/services/api'; -import type { StockAdjust } from '@/types/api'; +import { DeleteOutlined, PlusOutlined } from '@ant-design/icons'; +import { ActionType, ProColumns, ProTable } from '@ant-design/pro-components'; +import { Button, Col, Form, Input, Modal, Row, Select, Table, Tag, message } from 'antd'; +import { useEffect, useRef, useState } from 'react'; +import { createStockAdjust, listProducts, listStockAdjusts } from '@/services/api'; +import type { Product, StockAdjust } from '@/types/api'; const STATUS_MAP: Record = { 0: { text: '待审核', color: 'warning' }, @@ -10,6 +11,9 @@ const STATUS_MAP: Record = { 2: { text: '已驳回', color: 'error' }, }; +interface DetailRow { key: number; productId?: string; adjustQuantity?: string } +let rowKey = 0; + const columns: ProColumns[] = [ { title: '调整单号', dataIndex: 'adjustNo' }, { title: '调整日期', dataIndex: 'adjustDate', valueType: 'date', search: false }, @@ -28,20 +32,160 @@ const columns: ProColumns[] = [ { title: '操作', valueType: 'option', render: () => [查看] }, ]; -const AdjustsPage: React.FC = () => ( - - headerTitle="库存调整" - rowKey="adjustId" - columns={columns} - search={false} - request={async (params) => { - const res = await listStockAdjusts({ page: params.current, pageSize: params.pageSize }); - return { data: res?.data?.list || [], total: res?.data?.total || 0, success: res?.code === 0 }; - }} - toolBarRender={() => [ - , - ]} - /> -); +const AdjustsPage: React.FC = () => { + const actionRef = useRef(); + const [form] = Form.useForm(); + const [open, setOpen] = useState(false); + const [submitting, setSubmitting] = useState(false); + const [products, setProducts] = useState([]); + const [details, setDetails] = useState([{ key: rowKey++ }]); + + useEffect(() => { + listProducts({ page: 1, pageSize: 500 }).then((res) => { + if (res?.code === 0) setProducts(res.data?.list || []); + }); + }, []); + + const handleOpen = () => { + setDetails([{ key: rowKey++ }]); + setOpen(true); + }; + + const handleSubmit = async () => { + let values: { adjustDate: string; adjustReason?: string; remark?: string }; + try { + values = await form.validateFields(); + } catch { + return; + } + const validDetails = details.filter((r) => r.productId && r.adjustQuantity); + if (validDetails.length === 0) { + message.error('请至少添加一条调整明细'); + return; + } + setSubmitting(true); + try { + const res = await createStockAdjust({ + ...values, + details: validDetails.map((r) => ({ productId: r.productId!, adjustQuantity: r.adjustQuantity! })), + }); + if (res?.code !== 0) { message.error(res?.msg || '创建失败'); return; } + setOpen(false); + actionRef.current?.reload(); + message.success('调整单已创建'); + } catch { + message.error('创建失败'); + } finally { + setSubmitting(false); + } + }; + + const addRow = () => setDetails((prev) => [...prev, { key: rowKey++ }]); + const removeRow = (idx: number) => setDetails((prev) => prev.filter((_, i) => i !== idx)); + const updateRow = (idx: number, field: keyof DetailRow, value: string) => + setDetails((prev) => prev.map((r, i) => (i === idx ? { ...r, [field]: value } : r))); + + const today = new Date().toISOString().slice(0, 10); + + return ( + <> + + actionRef={actionRef} + headerTitle="库存调整" + rowKey="adjustId" + columns={columns} + search={false} + request={async (params) => { + const res = await listStockAdjusts({ page: params.current, pageSize: params.pageSize }); + return { data: res?.data?.list || [], total: res?.data?.total || 0, success: res?.code === 0 }; + }} + toolBarRender={() => [ + , + ]} + /> + + setOpen(false)} + confirmLoading={submitting} + width={680} + afterOpenChange={(o) => { if (!o) form.resetFields(); }} + > +
+ + + + + + + + + + + + + + + +
+ +
+ 调整明细 + +
+ ( + updateRow(idx, 'adjustQuantity', e.target.value)} + /> + ), + }, + { + title: '', + width: 40, + render: (_, __, idx) => ( + , - ]} - /> -); +const ChecksPage: React.FC = () => { + const actionRef = useRef(); + const [form] = Form.useForm(); + const [open, setOpen] = useState(false); + const [submitting, setSubmitting] = useState(false); + const [products, setProducts] = useState([]); + const [details, setDetails] = useState([]); + + useEffect(() => { + listProducts({ page: 1, pageSize: 500 }).then((res) => { + if (res?.code === 0) setProducts(res.data?.list || []); + }); + }, []); + + const handleOpen = () => { + setDetails([]); + setOpen(true); + }; + + const handleSubmit = async () => { + let values: { checkDate: string; checker?: string; remark?: string }; + try { + values = await form.validateFields(); + } catch { + return; + } + setSubmitting(true); + try { + const res = await createStockCheck({ + ...values, + details: details + .filter((r) => r.productId && r.actualQuantity) + .map((r) => ({ productId: r.productId!, actualQuantity: r.actualQuantity! })), + }); + if (res?.code !== 0) { message.error(res?.msg || '创建失败'); return; } + setOpen(false); + actionRef.current?.reload(); + message.success('盘点单已创建'); + } catch { + message.error('创建失败'); + } finally { + setSubmitting(false); + } + }; + + const addRow = () => setDetails((prev) => [...prev, { key: rowKey++ }]); + const removeRow = (idx: number) => setDetails((prev) => prev.filter((_, i) => i !== idx)); + const updateRow = (idx: number, field: keyof DetailRow, value: string) => + setDetails((prev) => prev.map((r, i) => (i === idx ? { ...r, [field]: value } : r))); + + const today = new Date().toISOString().slice(0, 10); + + return ( + <> + + actionRef={actionRef} + headerTitle="库存盘点" + rowKey="checkId" + columns={columns} + search={false} + request={async (params) => { + const res = await listStockChecks({ page: params.current, pageSize: params.pageSize }); + return { data: res?.data?.list || [], total: res?.data?.total || 0, success: res?.code === 0 }; + }} + toolBarRender={() => [ + , + ]} + /> + + setOpen(false)} + confirmLoading={submitting} + width={680} + afterOpenChange={(o) => { if (!o) form.resetFields(); }} + > +
+ +
+ + + + + + + + + + + + + + + +
+ 盘点明细(可选) + +
+
( + updateRow(idx, 'actualQuantity', e.target.value)} + /> + ), + }, + { + title: '', + width: 40, + render: (_, __, idx) => ( +