PayApprovalDialog.tsx
9.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
import { useEffect, useState } from 'react';
import { useMutation } from '@tanstack/react-query';
import type { DefaultOptionType } from 'antd/es/cascader';
import { UButton, UCascader, UForm, UInput, UModal, USelect, UText, useUMessage } from '@uj/ui';
import { withdrawApi, type ApprovalRow } from '@/api';
import { EMPTY, errorMessage } from '@/features/shared/format';
import {
loadApprovalChannels,
loadApprovalEntities,
loadApprovalLevies,
useChannelAdmins,
type ApprovalLevy,
} from '@/features/shared/options';
export interface PayApprovalDialogProps {
open: boolean;
/** 老系统把整行记录交给弹窗当初始信息(批次号、积分总额、付款总额)。 */
row?: ApprovalRow;
onClose: () => void;
onSuccess: () => void;
}
const levyKey = (channel: string, entity: string) => `${channel}|${entity}`;
/** 按路径把新一层子节点塞进级联树(老系统的 lazyLoad resolve 等价动作)。 */
function withChildren(
nodes: DefaultOptionType[],
path: string[],
children: DefaultOptionType[],
): DefaultOptionType[] {
const [head, ...rest] = path;
return nodes.map((node) =>
String(node.value) !== head
? node
: rest.length
? { ...node, children: withChildren(node.children ?? [], rest, children) }
: { ...node, children },
);
}
/**
* 付款审批弹窗(提交 OA 付款审批)。
*
* 付款渠道是「渠道 → 付款主体 → 代征主体」三层,逐层展开时才发下一层的请求,
* 与老系统的懒加载级联一致;选中代征主体后按它的税点重算付款总额
* (付款总额 = 积分总额 + 积分总额 × 税点,老系统 calculateAmount 同一条式子)。
* 验证码发给所选渠道的管理员,默认选中第一个。
*/
export function PayApprovalDialog({ open, row, onClose, onSuccess }: PayApprovalDialogProps) {
const message = useUMessage();
const [options, setOptions] = useState<DefaultOptionType[]>([]);
const [levies, setLevies] = useState<Record<string, ApprovalLevy[]>>({});
const [path, setPath] = useState<string[]>([]);
const [tax, setTax] = useState('');
const [phone, setPhone] = useState('');
const [captcha, setCaptcha] = useState('');
const [explain, setExplain] = useState('');
const [remark, setRemark] = useState('');
const [seconds, setSeconds] = useState(0);
const channel = path[0] ?? '';
const admins = useChannelAdmins(channel || undefined, open && !!channel);
useEffect(() => {
if (!open || options.length) return;
void loadApprovalChannels().then((list) =>
setOptions(list.map((item) => ({ ...item, isLeaf: false }))),
);
}, [open, options.length]);
useEffect(() => {
const admin = admins.admins[0];
if (admin) setPhone(String(admin.adminPhone ?? admin.adminId ?? ''));
}, [admins.admins]);
useEffect(() => {
if (seconds <= 0) return;
const timer = window.setInterval(
() => setSeconds((value) => (value <= 1 ? 0 : value - 1)),
1000,
);
return () => window.clearInterval(timer);
}, [seconds]);
const loadNode = async (selected: DefaultOptionType[]) => {
const at = selected.map((node) => String(node.value));
if (at.length === 1) {
const entities = await loadApprovalEntities(at[0]);
setOptions((current) =>
withChildren(
current,
at,
entities.map((item) => ({ ...item, isLeaf: false })),
),
);
return;
}
if (at.length === 2) {
const items = await loadApprovalLevies(at[0], at[1]);
setLevies((current) => ({ ...current, [levyKey(at[0], at[1])]: items }));
setOptions((current) =>
withChildren(
current,
at,
items.map((item) => ({ value: item.value, label: item.label, isLeaf: true })),
),
);
}
};
const pointTotal = Number(row?.pointTotal);
const taxRate = Number(tax);
// 未选到代征主体之前,付款总额沿用批次记录里的数(老系统 recordInfo.paymentTotal)。
const paymentTotal =
tax !== '' && Number.isFinite(pointTotal) && Number.isFinite(taxRate)
? (pointTotal + (pointTotal * taxRate) / 100).toFixed(2)
: row?.paymentTotal === undefined || row?.paymentTotal === ''
? EMPTY
: String(row.paymentTotal);
const sms = useMutation({
mutationFn: () => withdrawApi.sendAdminSms({ channel, phone }),
onSuccess: () => {
void message.success('短信验证码发送成功');
setSeconds(60);
},
onError: (error) => void message.error(errorMessage(error)),
});
const submit = useMutation({
mutationFn: () => {
if (!path[2]) throw new Error('请选择付款渠道');
if (!phone) throw new Error('请选择管理员');
if (!captcha) throw new Error('请输入验证码');
if (!explain) throw new Error('请输入发票及其他说明');
const admin = admins.admins.find(
(item) => String(item.adminPhone ?? item.adminId ?? '') === phone,
);
return withdrawApi.applyApprovalOa({
id: row?.id,
phone,
paymentSubjectId: path[1],
// 接口字段名叫 paymentChannelId,存的其实是代征主体 id(老系统就这么写的)。
paymentChannelId: path[2],
captcha,
explain,
remark,
approver: String(admin?.adminName ?? ''),
});
},
onSuccess: () => {
void message.success('付款审批成功');
onSuccess();
},
onError: (error) => void message.error(errorMessage(error)),
});
const close = () => {
setOptions([]);
setLevies({});
setPath([]);
setTax('');
setPhone('');
setCaptcha('');
setExplain('');
setRemark('');
setSeconds(0);
onClose();
};
return (
<UModal
title="付款审批"
open={open}
okText="确定"
confirmLoading={submit.isPending}
onCancel={close}
onOk={() => submit.mutate()}
>
<UForm layout="horizontal" labelCol={{ span: 6 }} wrapperCol={{ span: 18 }}>
<UForm.Item label="付款渠道" required>
<UCascader
value={path}
options={options}
loadData={loadNode}
placeholder="请选择"
showSearch
allowClear
style={{ width: '100%' }}
onChange={(value) => {
const next = (value as unknown[]).map(String);
setPath(next);
setTax(
next.length === 3
? (levies[levyKey(next[0], next[1])]?.find((item) => item.value === next[2])
?.tax ?? '')
: '',
);
setCaptcha('');
setSeconds(0);
}}
/>
</UForm.Item>
<UForm.Item label="批次号">
<UText>{row?.batchNo ?? EMPTY}</UText>
</UForm.Item>
<UForm.Item label="积分总额">
<UText>{row?.pointTotal ?? EMPTY}</UText>
</UForm.Item>
<UForm.Item label="付款总额/元">
<UText strong>{paymentTotal}</UText>
</UForm.Item>
<UForm.Item label="税点">
<UText>{tax === '' ? EMPTY : `${tax}%`}</UText>
</UForm.Item>
<UForm.Item label="管理员" required>
<USelect
value={phone || undefined}
placeholder="请选择管理员"
style={{ width: '100%' }}
loading={admins.isLoading}
options={admins.admins.map((item) => ({
value: String(item.adminPhone ?? item.adminId ?? ''),
label: `${item.adminName ?? ''} ${item.adminPhone ?? ''}`.trim(),
}))}
onChange={(value) => {
setPhone(String(value ?? ''));
setCaptcha('');
setSeconds(0);
}}
/>
</UForm.Item>
<UForm.Item label="验证码" required>
<div className="uj-points-captcha">
<UInput
value={captcha}
placeholder="请输入"
maxLength={6}
onChange={(event) => setCaptcha(event.target.value)}
/>
<UButton
type="link"
disabled={seconds > 0 || !channel || !phone}
loading={sms.isPending}
onClick={() => sms.mutate()}
>
{seconds > 0 ? `${seconds}s` : '获取验证码'}
</UButton>
</div>
</UForm.Item>
<UForm.Item label="发票及其他说明" required>
<UInput.TextArea
rows={3}
maxLength={200}
showCount
value={explain}
placeholder="说明本次付款是否已升票、历次付款有无欠票及相应欠票金额,及其他需要补充说明的情况。如无则输入“无”。"
onChange={(event) => setExplain(event.target.value)}
/>
</UForm.Item>
<UForm.Item label="备注">
<UInput.TextArea
rows={3}
maxLength={50}
showCount
value={remark}
placeholder="请输入备注"
onChange={(event) => setRemark(event.target.value)}
/>
</UForm.Item>
</UForm>
</UModal>
);
}