HirePurchaseUploadPayBillModal.tsx
11.1 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
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
import { RESPONSE_CODE } from '@/constants/enum';
import {
postServiceOrderFileProcess,
postServiceOrderHirePurchase,
} from '@/services';
import { transImageFile } from '@/utils';
import { PlusOutlined } from '@ant-design/icons';
import { Button, Form, Input, Modal, Radio, Upload, message } from 'antd';
import { RcFile } from 'antd/lib/upload';
import { UploadFile, UploadProps } from 'antd/lib/upload/interface';
import { cloneDeep } from 'lodash';
import { useEffect, useRef, useState } from 'react';
import { COMFIR_RECEIPT_IMAGES_NUMBER } from '../constant';
interface HirePurchaseUploadPayBillModalProps {
visible: boolean;
onCancel: () => void;
onOk: () => void;
orderAmount?: number;
paidAmount?: number;
record?: any;
subOrders?: any[];
}
const HirePurchaseUploadPayBillModal: React.FC<
HirePurchaseUploadPayBillModalProps
> = ({
visible,
onCancel,
onOk,
orderAmount = 100000.0,
paidAmount = 0,
record,
subOrders = [],
}) => {
// 订单总金额
const totalPayment = record?.totalPayment || orderAmount;
// 已回款金额
const installedMoney = record?.installmentMoneyAudit || paidAmount;
// 待回款金额
const remainingMoney = totalPayment - installedMoney;
const [form] = Form.useForm();
const [fileList, setFileList] = useState<UploadFile[]>([]);
const [paymentType, setPaymentType] = useState<string>('INSTALLMENT');
const [previewOpen, setPreviewOpen] = useState(false);
const [previewImage, setPreviewImage] = useState('');
const [previewTitle, setPreviewTitle] = useState('');
const fileListObj = useRef<UploadFile[]>([]);
const getBase64 = (file: RcFile): Promise<string> =>
new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result as string);
reader.onerror = (error) => reject(error);
});
const handleCancel = () => setPreviewOpen(false);
const uploadButton = (
<div>
<PlusOutlined />
<div style={{ marginTop: 8 }}>上传凭证</div>
</div>
);
const handleTypeChange = (e: any) => {
const newType = e.target.value;
setPaymentType(newType);
// 如果选择全部回款,自动填入待回款金额
if (newType === 'FULL') {
form.setFieldsValue({
amount: remainingMoney.toFixed(2),
});
}
};
// 验证回款金额不能超过待回款金额
const validateAmount = (_: any, value: string) => {
if (!value) return Promise.reject('请输入回款金额');
// Check if the value is a valid number
if (isNaN(value)) return Promise.reject('请输入有效的数字');
// Check if the value has more than 2 decimal places
const decimalCount = (value.match(/\.\d+/) || [''])[0].length - 1;
if (decimalCount > 2) {
return Promise.reject('最多只能输入两位小数');
}
const amount = parseFloat(value);
if (amount <= 0) return Promise.reject('回款金额必须大于0');
if (amount > remainingMoney)
return Promise.reject(
`回款金额不能超过待回款金额 ${remainingMoney.toFixed(2)}元`,
);
return Promise.resolve();
};
const handleBeforeUpload = (file: any) => {
setFileList([...fileList, file]);
return false;
};
/** 粘贴快捷键的回调 */
const onPaste = async (e: any) => {
/** 获取剪切板的数据clipboardData */
let clipboardData = e.clipboardData,
i = 0,
items,
item,
types;
/** 为空判断 */
if (clipboardData) {
items = clipboardData.items;
if (!items) {
message.info('您的剪贴板中没有照片');
return;
}
item = items[0];
types = clipboardData.types || [];
/** 遍历剪切板的数据 */
for (; i < types.length; i++) {
if (types[i] === 'Files') {
item = items[i];
break;
}
}
/** 判断文件是否为图片 */
if (item && item.kind === 'file' && item.type.match(/^image\//i)) {
const imgItem = item.getAsFile();
const newFileList = cloneDeep(fileListObj.current);
let filteredArray = newFileList.filter(
(obj) => obj.status !== 'removed',
); //过滤掉状态为已删除的照片
const listItem = {
...imgItem,
status: 'done',
url: await getBase64(imgItem),
originFileObj: imgItem,
};
if (filteredArray.length >= COMFIR_RECEIPT_IMAGES_NUMBER) {
message.info('上传凭证数量不能超过3');
return;
}
fileListObj.current = filteredArray;
filteredArray.push(listItem);
setFileList(filteredArray);
return;
}
}
message.info('您的剪贴板中没有照片');
};
const handleChange: UploadProps['onChange'] = ({ fileList: newFileList }) => {
fileListObj.current = newFileList;
setFileList(newFileList);
};
const handlePreview = async (file: UploadFile) => {
if (!file.url && !file.preview) {
file.preview = await getBase64(file.originFileObj as RcFile);
}
setPreviewImage(file.url || (file.preview as string));
setPreviewOpen(true);
setPreviewTitle(
file.name ||
file.originFileObj?.name ||
file.url!.substring(file.url!.lastIndexOf('/') + 1),
);
};
const props: UploadProps = {
onRemove: (file) => {
const index = fileList.indexOf(file);
const newFileList = fileList.slice();
newFileList.splice(index, 1);
setFileList(newFileList);
},
beforeUpload: handleBeforeUpload,
listType: 'picture-card',
onPreview: handlePreview,
fileList,
onChange: handleChange,
accept: 'image/png, image/jpeg, image/png',
name: 'files',
headers: { Authorization: localStorage.getItem('token') },
};
useEffect(() => {
document.addEventListener('paste', onPaste);
return () => {
document.removeEventListener('paste', onPaste);
};
}, []);
const handleOk = async () => {
try {
const values = await form.validateFields();
if (fileList.length <= 0) {
message.error('请上传至少一张凭证');
return;
}
message.open({
type: 'loading',
content: '正在上传凭证...',
duration: 0,
});
// 附件处理
let formData = new FormData();
for (let file of fileList) {
if (file.originFileObj) {
formData.append('files', file.originFileObj as RcFile);
} else {
// 有url的话取url(源文件),没url取thumbUrl。有url的时候thumbUrl是略缩图
if (file?.url === undefined || file?.url === null) {
formData.append(
'files',
transImageFile(file?.thumbUrl),
file?.originFileObj?.name,
);
} else {
formData.append(
'files',
transImageFile(file?.url),
file?.originFileObj?.name,
);
}
}
}
let res = await postServiceOrderFileProcess({
data: formData,
});
message.destroy();
if (res.result === RESPONSE_CODE.SUCCESS) {
let fileUrls = res?.data?.map((item) => {
return { url: item };
});
// 分期付款提交
const installmentMoney = values.amount;
const installmentComment = values.remarks;
// 获取子订单IDs
const subOrderIds =
subOrders?.map((item: any) => {
return item.id;
}) || [];
const data = await postServiceOrderHirePurchase({
data: {
subOrderIds: subOrderIds,
filePaths: fileUrls,
installmentMoney: installmentMoney,
installmentComment: installmentComment,
},
});
if (data.result === RESPONSE_CODE.SUCCESS) {
message.success(data.message || '提交成功');
onOk();
} else {
message.error(data.message || '提交失败');
}
} else {
message.error(res.message || '上传失败');
}
} catch (error) {
console.error('Validate Failed:', error);
message.error('提交失败');
}
};
return (
<>
<Modal
title="回款"
open={visible}
onCancel={onCancel}
footer={[
<Button key="cancel" onClick={onCancel}>
取消
</Button>,
<Button key="submit" type="primary" onClick={handleOk}>
确认
</Button>,
]}
width={500}
>
<Form form={form} layout="vertical">
<div style={{ marginBottom: 16 }}>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
marginBottom: 8,
}}
>
<span>订单总金额:</span>
<span>{totalPayment.toFixed(2)}元</span>
</div>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
marginBottom: 8,
}}
>
<span>已回款金额:</span>
<span>{installedMoney.toFixed(2)}元</span>
</div>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
marginBottom: 8,
}}
>
<span>待回款金额:</span>
<span>{remainingMoney.toFixed(2)}元</span>
</div>
</div>
<Form.Item
label="回款类型"
name="paymentType"
initialValue={paymentType}
>
<Radio.Group onChange={handleTypeChange}>
<Radio value="INSTALLMENT">分期回款</Radio>
<Radio value="FULL">全部回款</Radio>
</Radio.Group>
</Form.Item>
<Form.Item
label="回款金额"
name="amount"
rules={[
{ required: true, message: '请输入回款金额' },
{ validator: validateAmount },
]}
>
<Input
placeholder="请输入回款金额"
suffix="元"
disabled={paymentType === 'FULL'}
/>
</Form.Item>
<div className="pb-4 text-xs decoration-gray-50">可复制照片粘贴</div>
<Form.Item
label="附件凭证"
name="attachments"
rules={[{ required: true, message: '请上传回款凭证' }]}
>
<Upload {...props}>
{fileList.length < COMFIR_RECEIPT_IMAGES_NUMBER
? uploadButton
: ''}
</Upload>
</Form.Item>
<Form.Item label="备注" name="remarks">
<Input.TextArea rows={4} placeholder="请输入备注信息" />
</Form.Item>
</Form>
</Modal>
<Modal
open={previewOpen}
title={previewTitle}
footer={null}
onCancel={handleCancel}
>
<img alt="图片预览" style={{ width: '100%' }} src={previewImage} />
</Modal>
</>
);
};
export default HirePurchaseUploadPayBillModal;