Blame view

src/views/project/order/FormDetail/ProfitFormPanel.vue 4.96 KB
sanmu authored
1
2
3
4
<template>
  <BasicForm @register="registerForm" />
</template>
<script lang="ts">
sanmu authored
5
  import { computed, defineComponent, reactive, ref, nextTick } from 'vue';
sanmu authored
6
  import { BasicForm, useForm } from '/@/components/Form/index';
sanmu authored
7
  import { FIELDS_PROFIT_INFO } from '../tableData';
sanmu authored
8
  import { getProfitDisable } from '/@/utils/project';
sanmu authored
9
  import { debounce, get } from 'lodash-es';
sanmu authored
10
11
  import { useOrderInfo } from '/@/hooks/component/order';
  import { useOrderStoreWithOut } from '/@/store/modules/order';
sanmu authored
12
13
  import message from '/@/views/form-design/utils/message';
  import { getOrderProfitRate } from '/@/api/project/order';
sanmu authored
14
15
16
17
18
19
20
21
22
23
24

  export default defineComponent({
    components: { BasicForm },

    props: {
      onGoCheckDetail: {
        type: Function,
      },
      id: {
        type: String,
      },
sanmu authored
25
26
27
      profitFormData: {
        type: Object,
      },
sanmu authored
28
29
30
31
      orderCount: {
        type: Number,
        default: 0,
      },
sanmu authored
32
33
34
    },
    emits: ['success'],
    setup(props, { emit }) {
sanmu authored
35
      let fields = ref({});
sanmu authored
36
37
38
      const orderStore = useOrderStoreWithOut();

      const { exchangeRate } = useOrderInfo(orderStore);
sanmu authored
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
      const updateProfitRate = debounce(async (field) => {
        const values = getFieldsValue();
        const {
          profitType,
          packetPrice,
          exchangeRate = 0,
          customerPrice,
          customerRmbPrice,
          productionDepartmentPrice,
          profitRate,
        } = values;
        if (customerPrice && exchangeRate && packetPrice && customerRmbPrice) {
          const customerTotalPrice = (customerPrice * (props.orderCount || 0)).toFixed(2); // 客户总价,美元
          const productionDepartmentTotalPrice = (
            productionDepartmentPrice * (props.orderCount || 0)
          ).toFixed(2); // 生成科总价¥
          const packetTotalPrice = (packetPrice * (props.orderCount || 0)).toFixed(2); // 包装费用总价
          if (
            customerPrice &&
            customerRmbPrice &&
            productionDepartmentPrice &&
            // 只修改单价和部门才会计算利润率
            ['customerPrice', 'customerRmbPrice', 'productionDepartmentPrice'].includes(field)
          ) {
            const res = await getOrderProfitRate({
              profitType: profitType || '0',
              packetTotalPrice,
              productionDepartmentTotalPrice,
              customerTotalPrice,
              exchangeRate,
            });
            if (profitRate !== (res * 100).toFixed(2)) {
              setFieldsValue({ profitRate: (res * 100).toFixed(2) + '%' });
            }
          }
        }
      }, 300);
sanmu authored
76
77

      const schemas = computed(() => {
sanmu authored
78
79
80
81
        const options = {
          exchangeRate,
        };
sanmu authored
82
83
84
        return FIELDS_PROFIT_INFO.map((item) => {
          return {
            ...item,
sanmu authored
85
            field: `${item.field}`,
sanmu authored
86
87
            componentProps: {
              ...item.componentProps,
88
              ...(item.component === 'Select' && { showSearch: true }),
sanmu authored
89
90
91
92
93
94
95
96
              ...(item.component === 'Select' &&
                !item.componentProps?.options?.length && { options: options[item.field] }),
              disabled: getProfitDisable(
                item.field,
                get(fields.value, `${item.field}`),
                props.id,
                get(props.profitFormData, `${item.field}`),
              ),
sanmu authored
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
              onChange: async (val) => {
                const values = getFieldsValue();
                const { exchangeRate = 0, customerPrice, customerRmbPrice } = values || {};
                // 修改的客户单价
                if (item.field === 'customerPrice' && val !== customerPrice) {
                  if (exchangeRate !== 0) {
                    await setFieldsValue({ customerRmbPrice: val * exchangeRate });
                  } else {
                    message.error('汇率等于0,美元人民币之间无法转换');
                  }
                }
                // 值不相等才变化,不然无限循环了
                if (item.field === 'customerRmbPrice' && val !== customerRmbPrice) {
                  if (exchangeRate !== 0) {
                    await setFieldsValue({ customerPrice: val / exchangeRate });
                  } else {
                    message.error('汇率等于0,美元人民币之间无法转换');
                  }
                }
                nextTick(() => {
                  updateProfitRate(item.field);
                });
              },
sanmu authored
120
121
122
123
124
125
126
127
            },
            colProps: {
              span: 24,
            },
          };
        });
      });
sanmu authored
128
      const [registerForm, { setFieldsValue, getFieldsValue, resetFields, validate }] = useForm({
sanmu authored
129
130
        labelWidth: 120,
        schemas,
sanmu authored
131
        layout: 'vertical',
sanmu authored
132
133
134
135
136
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
sanmu authored
137
138
139
140
141
142
143
144
145
146

      return {
        fields,
        schemas,
        validate,
        registerForm,
        getFieldsValue,
        setFieldsValue,
        resetFields,
      };
sanmu authored
147
148
149
    },
  });
</script>