Blame view

src/pages/Instalment/components/read/read.tsx 10.3 KB
PurelzMgnead authored
1
import {
PurelzMgnead authored
2
3
4
5
6
7
8
9
10
11
12
13
  postOrderErpOrderStagesPayWaySaveOrUpdate,
  postOrderErpOrderStagesSaveOrUpdate,
  postOrderErpOrderStagesSearch,
  postOrderErpOrderStagesUpload,
} from '@/services';
import {
  ModalForm,
  ProCard,
  ProForm,
  ProFormDatePicker,
  ProFormText,
  ProFormTextArea,
PurelzMgnead authored
14
} from '@ant-design/pro-components';
PurelzMgnead authored
15
import { Form, message } from 'antd';
PurelzMgnead authored
16
import { RcFile } from 'antd/es/upload';
PurelzMgnead authored
17
18
19
import { useEffect, useState } from 'react';
import PayWayDetail from './readPayWay';
import ProductDetail from './readProduct';
PurelzMgnead authored
20
21

const waitTime = (time: number = 100) => {
PurelzMgnead authored
22
23
24
25
26
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
PurelzMgnead authored
27
28
29
};

export default ({ currentContract }) => {
PurelzMgnead authored
30
31
32
33
34
35
  const [form] = Form.useForm<{ name: string; company: string }>();
  const [contextBody, setContextBody] = useState({});
  const [, setEditProductBody] = useState([]);
  const [total, setTotal] = useState(0);
  const [payWayBody, setPayWayBody] = useState([]);
  const [otherBody, setOtherBody] = useState([]);
PurelzMgnead authored
36
PurelzMgnead authored
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  type OrderStagesWithListItem = {
    //文件编号
    id: number;
    //合同编号
    contract: string;
    //供应商名称
    vendor: string;
    //签合同日期
    dateRange: Date;
    //终端名称
    terminal: string;
    orderStagesDeviceVoList: orderStagesDevice[];
    //合同总金额
    totalPrice: number;
    //付款方式
    payWay: string;
    //附件
    annex: string;
    //备注
    remark: string;
  };
PurelzMgnead authored
58
PurelzMgnead authored
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  type orderStagesDevice = {
    //设备id
    dId: number;
    //设备名称
    deviceName: string;
    //设备型号
    deviceModel: string;
    //数量
    count: number;
    //单价
    unitPrice: number;
    //总价
    price: number;
  };
PurelzMgnead authored
73
PurelzMgnead authored
74
75
76
77
  async function refresh() {
    const res = await postOrderErpOrderStagesSearch({
      data: { contract: currentContract },
    });
PurelzMgnead authored
78
    const context = res?.data?.data[0];
PurelzMgnead authored
79
PurelzMgnead authored
80
    if (context?.contract !== null) {
PurelzMgnead authored
81
      setContextBody(context);
PurelzMgnead authored
82
83
      setTotal(context?.totalPrice);
      form.setFieldValue('totalPrice', context?.totalPrice);
PurelzMgnead authored
84
    }
PurelzMgnead authored
85
  }
PurelzMgnead authored
86
PurelzMgnead authored
87
88
89
  function setSave(value) {
    setOtherBody(value);
  }
PurelzMgnead authored
90
PurelzMgnead authored
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
  useEffect(() => {
    setContextBody({ ...contextBody, totalPrice: total });
    form.setFieldValue('totalPrice', total);
  }, [total]);

  const handleInputChange = (value: string, no: number, priceNow?: number) => {
    let totalPay = 0;
    const payValue: string[] = value.split('/');
    let body:
      | ((prevState: never[]) => never[])
      | { proportion: string; payPrice: number }[] = [];
    if (no === 1) {
      if (payValue.length !== 4) {
        message.warning('比例个数总和不为4个!');
      } else {
        payValue.forEach((item) => {
          totalPay += Number(item);
        });
      }
      if (totalPay !== 100) {
        message.warning('比例总和不为100!');
      } else {
        message.success('输入有效!');
        const price = total;
        payValue.forEach((item) => {
          body.push({
            proportion: item + '%',
            payPrice: (Number(item) * price) / 100,
          });
        });
        setPayWayBody(body);
      }
    } else {
      payValue.forEach((item) => {
        totalPay += Number(item);
      });
      payValue.forEach((item) => {
        body.push({
          proportion: item + '%',
          payPrice: (Number(item) * priceNow) / 100,
        });
      });
      setPayWayBody(body);
PurelzMgnead authored
134
    }
PurelzMgnead authored
135
  };
PurelzMgnead authored
136
PurelzMgnead authored
137
138
139
140
  async function getBody() {
    const res = await postOrderErpOrderStagesSearch({
      data: { contract: currentContract },
    });
PurelzMgnead authored
141
    const context = res.data.data[0];
PurelzMgnead authored
142
PurelzMgnead authored
143
    if (context?.contract !== null) {
PurelzMgnead authored
144
      setContextBody(context);
PurelzMgnead authored
145
146
      setTotal(context?.totalPrice);
      form.setFieldValue('totalPrice', context?.totalPrice);
PurelzMgnead authored
147
    }
PurelzMgnead authored
148
    handleInputChange(context?.payWay, 0, context?.totalPrice);
PurelzMgnead authored
149
  }
PurelzMgnead authored
150
PurelzMgnead authored
151
152
153
  useEffect(() => {
    getBody();
  }, []);
PurelzMgnead authored
154
PurelzMgnead authored
155
156
157
158
159
160
161
162
  function getEditProductBody(value) {
    setEditProductBody(value);
    let price = 0;
    value.map((obj) => (price += obj.count * obj.unitPrice));
    setTotal(price);
    setContextBody({ ...contextBody, orderStagesDeviceVoList: value });
    handleInputChange(contextBody.payWay, 0, price);
  }
PurelzMgnead authored
163
PurelzMgnead authored
164
165
  return (
    <ModalForm<OrderStagesWithListItem>
166
      title="查看"
PurelzMgnead authored
167
168
169
170
171
172
      trigger={<a onClick={refresh}>查看</a>}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
      }}
173
174
175
176
177
178
179
180
      submitter={{
        resetButtonProps: {},
        submitButtonProps: {
          style: {
            display: 'none',
          },
        },
      }}
PurelzMgnead authored
181
182
183
184
185
      submitTimeout={2000}
      onFinish={async (values) => {
        let remakeValue = [];
        // 创建一个用于存储所有异步操作的Promise数组
        const promises = [];
PurelzMgnead authored
186
PurelzMgnead authored
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
        otherBody.forEach((item) => {
          let remakeItem = {
            ossId: item.ossId,
            number: item.id,
            dataRange: item.payDate,
            fileName: item.fileName,
          };
          if (
            typeof item.fileUrl === 'object' &&
            item.fileUrl instanceof File
          ) {
            const formData = new FormData();
            formData.append('file', item.fileUrl as RcFile);
            const uploadPromise = async () => {
              const res = await postOrderErpOrderStagesUpload({
                data: formData,
                headers: {
                  'Content-Type':
                    'multipart/form-data; boundary=----WebKitFormBoundarynl6gT1BKdPWIejNq',
                },
              });
              if (res.data) {
                remakeItem.fileUrl = res.data;
              }
            };
            promises.push(uploadPromise());
          }
          remakeValue.push(remakeItem);
        });
PurelzMgnead authored
216
PurelzMgnead authored
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
        // 使用Promise.all等待所有异步操作完成后再执行保存操作
        Promise.all(promises).then(async () => {
          await postOrderErpOrderStagesPayWaySaveOrUpdate({
            data: remakeValue,
          });
        });
        const formData = new FormData();
        // let toSendEdit={...values,orderStagesDeviceVoList:contextBody.orderStagesDeviceVoList};
        let toSendEdit = {
          id: values.id || contextBody.id,
          contract: values.contract || contextBody.contract,
          vendor: values.vendor || contextBody.vendor,
          dateRange: values.dateRange || contextBody.dateRange,
          terminal: values.terminal || contextBody.terminal,
          orderStagesDeviceDoList:
            values.orderStagesDeviceVoList ||
            contextBody.orderStagesDeviceVoList,
          totalPrice: values.totalPrice || contextBody.totalPrice,
          payWay: values.payWay || contextBody.payWay,
          annex: contextBody.annex,
          remark: values.remark || contextBody.remark,
        };
        if (values.annex) {
          formData.append('file', values.annex[0].originFileObj as RcFile);
          const res = await postOrderErpOrderStagesUpload({
            data: formData,
            headers: {
              'Content-Type':
                'multipart/form-data; boundary=----WebKitFormBoundarynl6gT1BKdPWIejNq',
            },
          });
          if (res.data) {
            toSendEdit.annex = res.data;
          }
        }
        const isSaveOrUpdate = await postOrderErpOrderStagesSaveOrUpdate({
          data: { ...toSendEdit },
        });
        if (isSaveOrUpdate) {
          getBody();
        }
        await waitTime(2000);
        message.success('提交成功');
        return true;
      }}
    >
      <ProCard title="基本信息" headerBordered bordered>
        <ProForm.Group>
          <ProFormText
            width="md"
            name="vendor"
            label="供应商名称"
            placeholder="请输入"
PurelzMgnead authored
270
            initialValue={contextBody?.vendor}
PurelzMgnead authored
271
272
            readonly
          />
PurelzMgnead authored
273
PurelzMgnead authored
274
275
276
277
278
          <ProFormText
            width="md"
            name="terminal"
            label="终端名称"
            placeholder="请输入"
PurelzMgnead authored
279
            initialValue={contextBody?.terminal}
PurelzMgnead authored
280
281
            readonly
          />
PurelzMgnead authored
282
PurelzMgnead authored
283
284
285
286
287
288
289
290
          <ProFormDatePicker
            name="dateRange"
            width="md"
            label="签合同日期"
            placeholder="请选择日期"
            fieldProps={{
              format: (value) => value.format('YYYY-MM-DD'),
            }}
PurelzMgnead authored
291
            initialValue={contextBody?.dateRange}
PurelzMgnead authored
292
293
            readonly
          />
PurelzMgnead authored
294
PurelzMgnead authored
295
296
297
298
299
          <ProFormText
            width="md"
            name="payWay"
            label="付款比例"
            placeholder="请输入"
PurelzMgnead authored
300
            initialValue={contextBody?.payWay}
PurelzMgnead authored
301
302
303
304
305
            readonly
            onBlur={(e) => {
              handleInputChange(e.target.value, 1);
            }}
          />
PurelzMgnead authored
306
PurelzMgnead authored
307
308
309
310
311
          <ProFormText
            width="md"
            name="contract"
            label="合同编号"
            placeholder="请输入"
PurelzMgnead authored
312
            initialValue={contextBody?.contract}
PurelzMgnead authored
313
314
            readonly
          />
PurelzMgnead authored
315
PurelzMgnead authored
316
          {/* <ProFormText
PurelzMgnead authored
317
318
319
320
321
322
323
324
                        width="md"
                        name="annex"
                        label="合同附件"
                        placeholder="请输入"
                        initialValue={contextBody.fileUrl}
                        readonly
                    /> */}
PurelzMgnead authored
325
326
327
328
329
330
331
332
333
          <ProFormText
            width="md"
            name="totalPrice"
            label="合同金额"
            placeholder="请输入"
            disabled
            readonly
            // rules={[{ required: true, message: '此项为必填项' }]}
            // value={contextBody.totalPrice}
PurelzMgnead authored
334
            initialValue={contextBody?.totalPrice}
PurelzMgnead authored
335
336
337
338
339
340
341
342
343
344
          />
        </ProForm.Group>
      </ProCard>
      <ProCard
        title="产品明细"
        style={{ marginTop: 10 }}
        headerBordered
        bordered
      >
        <ProductDetail
PurelzMgnead authored
345
          productBody={contextBody?.orderStagesDeviceVoList}
PurelzMgnead authored
346
347
348
          EditProductBody={getEditProductBody}
        ></ProductDetail>
      </ProCard>
PurelzMgnead authored
349
PurelzMgnead authored
350
351
352
353
354
355
356
357
      <ProCard
        title="付款信息"
        style={{ marginTop: 10 }}
        headerBordered
        bordered
      >
        <PayWayDetail
          payBody={payWayBody}
PurelzMgnead authored
358
          thisId={contextBody?.id}
PurelzMgnead authored
359
360
361
          currtSave={setSave}
        ></PayWayDetail>
      </ProCard>
PurelzMgnead authored
362
PurelzMgnead authored
363
364
365
366
      <ProCard style={{ marginTop: 10 }} headerBordered bordered>
        <ProFormTextArea
          label="备注"
          name="remark"
PurelzMgnead authored
367
          initialValue={contextBody?.remark}
PurelzMgnead authored
368
369
370
371
372
373
          readonly
        />
      </ProCard>
    </ModalForm>
  );
};