ApplyForInvoicingModal.tsx
11.2 KB
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
import { RESPONSE_CODE } from '@/constants/enum';
import { postServiceOrderApplyInvoicing } from '@/services';
import { FloatAdd, enumToSelect, getAliYunOSSFileNameFromUrl } from '@/utils';
import {
ModalForm,
ProFormMoney,
ProFormSelect,
ProFormText,
ProFormTextArea,
ProFormUploadDragger,
} from '@ant-design/pro-components';
import { Form, message } from 'antd';
import { useEffect, useState } from 'react';
import { PAYEE_OPTIONS } from '../constant';
// 定义选项类型
interface SelectOption {
label: string;
value: string;
}
export default ({ setCheckVisible, isEdit, subOrders, onClose }) => {
const [isUrgent, setIsUrgent] = useState('');
const [receivingCompanyOptions, setReceivingCompanyOptions] = useState<
SelectOption[]
>([]);
let ids = subOrders?.map((item) => {
return item.id;
});
// 定义返回类型接口
interface MainOrderData {
value: string | number;
totalPayment: number;
invoiceIssuedAmount: number;
availableAmount: number;
}
// 获取唯一的主订单ID及其相关金额信息
const getUniqueMainOrderIds = (): MainOrderData[] => {
const mainOrderIds = subOrders?.map((item: any) => item.mainOrderId);
const uniqueIds = [...new Set(mainOrderIds)].filter(Boolean);
return uniqueIds.map((id) => {
// 获取该主订单下所有子订单
const orderSubOrders = subOrders.filter(
(item: any) => item.mainOrderId === id,
);
// 计算该主订单的总金额
let totalPayment = 0;
orderSubOrders.forEach((item: any) => {
totalPayment = FloatAdd(totalPayment, item.totalPayment || 0);
});
// 计算已开票金额(如果有的话)
let invoiceIssuedAmount = 0;
orderSubOrders.forEach((item: any) => {
invoiceIssuedAmount = FloatAdd(
invoiceIssuedAmount,
item.invoiceIssuedAmount || 0,
);
});
// 计算可开票金额
const availableAmount = Math.max(0, totalPayment - invoiceIssuedAmount);
return {
value: String(id),
totalPayment,
invoiceIssuedAmount,
availableAmount,
};
});
};
let mainIdSet = new Set();
subOrders?.forEach((item: { mainOrderId: unknown }) => {
mainIdSet.add(item.mainOrderId);
});
let mainIds = Array.from(mainIdSet).join(',');
let newListAnnex = [];
//回显,子订单可以编辑备注跟附件
if (isEdit) {
newListAnnex = subOrders.afterAnnexList?.map((path) => {
let i = 0;
return {
uid: i++,
name: getAliYunOSSFileNameFromUrl(path),
status: 'uploaded',
url: path,
response: { data: [path] },
};
});
subOrders.filePaths = newListAnnex;
}
const [form] = Form.useForm<{
applyInvoicingNotes: string;
filePaths: any;
subIds: any[];
afterInvoicingUpdate: boolean;
receivingCompany: string;
isUrgent: boolean;
deadline: string;
}>();
useEffect(() => {
//显示拼接的主订单id
form.setFieldValue('applyInvoicingNotes', mainIds);
// 设置每个主订单的可开票金额初始值
const mainOrders = getUniqueMainOrderIds();
mainOrders.forEach((order) => {
form.setFieldValue(
`invoiceAvailableAmount_${order.value}`,
order.availableAmount,
);
});
// 转换收款单位选项并保存
const options = enumToSelect(PAYEE_OPTIONS);
setReceivingCompanyOptions(options);
}, []);
return (
<ModalForm<{
applyInvoicingNotes: string;
filePaths: any;
subIds: any[];
afterInvoicingUpdate: boolean;
}>
width={500}
open
title={isEdit ? '修改信息' : '申请开票'}
initialValues={subOrders}
form={form}
autoFocusFirstInput
modalProps={{
okText: '确认',
cancelText: '取消',
destroyOnClose: true,
onCancel: () => {
setCheckVisible(false);
},
}}
submitter={{
render: (props, defaultDoms) => {
return defaultDoms;
},
}}
submitTimeout={2000}
onFinish={async (values) => {
values.subIds = ids;
//附件处理
values.filePaths = values.filePaths?.map((item) => {
return { url: item.response.data[0] };
});
if (isEdit) {
values.afterInvoicingUpdate = true;
} else {
values.afterInvoicingUpdate = false;
}
// 收集主订单可开票金额
const invoiceOrderAmounts = getUniqueMainOrderIds().map((item) => ({
orderId: Number(item.value), // 确保为 number 类型
availableAmount: values[`invoiceAvailableAmount_${item.value}`],
}));
// 校验所有主订单可开票金额之和等于price字段
const price = values.price;
let sumAvailable = 0;
invoiceOrderAmounts.forEach((item) => {
sumAvailable = FloatAdd(sumAvailable, item.availableAmount || 0);
});
if (Math.abs(sumAvailable - price) > 0.01) {
message.error(
`所有主订单可开票金额之和(${sumAvailable})必须等于开票金额(${price})`,
);
return;
}
// 获取receivingCompany对应的名称
const selectedOption = receivingCompanyOptions.find(
(option) => option.value === values.receivingCompany,
);
const receivingCompanyName = selectedOption ? selectedOption.label : '';
// 添加到请求数据中
const reqData = {
...values,
receivingCompanyName, // 添加收款单位名称
mainOrderIds: mainIds, // 使用已有的主订单ID字符串
invoiceOrderAmounts,
};
const res = await postServiceOrderApplyInvoicing({ data: reqData });
if (res.result === RESPONSE_CODE.SUCCESS) {
message.success(res.message);
onClose();
}
}}
onOpenChange={setCheckVisible}
>
{/* 主订单金额表格,任何情况都显示 */}
<div style={{ marginBottom: 24 }}>
<div
style={{
display: 'flex',
fontWeight: 'bold',
marginBottom: 8,
padding: '8px 0',
borderBottom: '1px solid #f0f0f0',
}}
>
<div style={{ flex: 25 }}>订单号</div>
<div style={{ flex: 18, textAlign: 'right' }}>订单金额</div>
<div style={{ flex: 18, textAlign: 'right' }}>已开票金额</div>
<div style={{ flex: 39, textAlign: 'right' }}>可开票金额</div>
</div>
{getUniqueMainOrderIds().map((item, index) => {
const maxAvailable = Math.max(
0,
item.totalPayment - item.invoiceIssuedAmount,
);
return (
<div
key={index}
style={{
display: 'flex',
marginBottom: 8,
padding: '8px 0',
borderBottom: '1px solid #f0f0f0',
}}
>
<div style={{ flex: 25 }}>{item.value}</div>
<div style={{ flex: 18, textAlign: 'right' }}>
¥ {item.totalPayment.toFixed(2)}
</div>
<div style={{ flex: 18, textAlign: 'right' }}>
¥ {item.invoiceIssuedAmount.toFixed(2)}
</div>
<div style={{ flex: 39, textAlign: 'right' }}>
<ProFormMoney
name={`invoiceAvailableAmount_${item.value}`}
locale="zh-CN"
fieldProps={{
precision: 2,
style: { width: '70%' },
}}
initialValue={item.availableAmount}
rules={[
{ required: true, message: '请填写可开票金额!' },
{
validator: (_, value) => {
if (value > maxAvailable) {
return Promise.reject(
`可开票金额不能超过${maxAvailable.toFixed(2)}`,
);
} else if (value === 0) {
return Promise.reject(`可开票金额不能为0`);
}
return Promise.resolve();
},
},
]}
/>
</div>
</div>
);
})}
</div>
<div className="mb-1">
如果需要合并订单,请将需要合并的订单id写在备注中,id之间用英文逗号隔开。
</div>
<ProFormTextArea
width="lg"
name="applyInvoicingNotes"
key="applyInvoicingNotes"
placeholder="请输入备注"
onMetaChange={(val) => {
console.log(val);
}}
proFieldProps={{
onchange: () => {
message.info('change');
},
}}
/>
<ProFormText
width="lg"
name="purchaser"
label="抬头名称"
key="purchaser"
placeholder="请输入抬头名称"
rules={[{ required: true, message: '抬头名称必填' }]}
/>
<ProFormSelect
placeholder="选择收款单位"
name="receivingCompany"
width="lg"
key="receivingCompany"
label={
<div>
<span>开票收款单位</span>
<span className="pl-2 text-xs text-gray-400">
财务开票将依据这个字段,选择对应的公司开票(若对[收款单位]没有要求,请任意选择一个)
</span>
</div>
}
options={receivingCompanyOptions}
rules={[{ required: true, message: '开票收款单位必填' }]}
/>
<ProFormSelect
placeholder="选择是否加急"
name="isUrgent"
width="lg"
key="isUrgent"
label="是否加急"
options={[
{ label: '是', value: 'true' },
{ label: '否', value: 'false' },
]}
rules={[{ required: true, message: '是否加急必填' }]}
onChange={(val: any) => {
setIsUrgent(val);
}}
/>
{/* <ProFormDatePicker
key="deadline"
label="期望开票时间"
name="deadline"
rules={[{ required: isUrgent === 'true', message: '期望开票时间必填' }]}
hidden={isUrgent !== 'true'}
/> */}
<ProFormTextArea
key="invoicingUrgentCause"
label="加急开票原因"
name="invoicingUrgentCause"
rules={[{ required: isUrgent === 'true', message: '加急开票原因' }]}
hidden={isUrgent !== 'true'}
/>
<ProFormUploadDragger
key="2"
label={
<div>
<span>开票明细确认表</span>
<span className="pl-2 text-xs text-gray-400">
如果开票信息有变更,如开票内容跟下单内容不一致、下单抬头和付款抬头不一致,请上传开票明细确认表。
</span>
</div>
}
name="filePaths"
action="/api/service/order/fileProcess"
fieldProps={{
headers: { Authorization: localStorage.getItem('token') },
}}
/>
</ModalForm>
);
};