ProfitFormPanel.vue
4.96 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
<template>
<BasicForm @register="registerForm" />
</template>
<script lang="ts">
import { computed, defineComponent, reactive, ref, nextTick } from 'vue';
import { BasicForm, useForm } from '/@/components/Form/index';
import { FIELDS_PROFIT_INFO } from '../tableData';
import { getProfitDisable } from '/@/utils/project';
import { debounce, get } from 'lodash-es';
import { useOrderInfo } from '/@/hooks/component/order';
import { useOrderStoreWithOut } from '/@/store/modules/order';
import message from '/@/views/form-design/utils/message';
import { getOrderProfitRate } from '/@/api/project/order';
export default defineComponent({
components: { BasicForm },
props: {
onGoCheckDetail: {
type: Function,
},
id: {
type: String,
},
profitFormData: {
type: Object,
},
orderCount: {
type: Number,
default: 0,
},
},
emits: ['success'],
setup(props, { emit }) {
let fields = ref({});
const orderStore = useOrderStoreWithOut();
const { exchangeRate } = useOrderInfo(orderStore);
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);
const schemas = computed(() => {
const options = {
exchangeRate,
};
return FIELDS_PROFIT_INFO.map((item) => {
return {
...item,
field: `${item.field}`,
componentProps: {
...item.componentProps,
...(item.component === 'Select' && { showSearch: true }),
...(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}`),
),
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);
});
},
},
colProps: {
span: 24,
},
};
});
});
const [registerForm, { setFieldsValue, getFieldsValue, resetFields, validate }] = useForm({
labelWidth: 120,
schemas,
layout: 'vertical',
showActionButtonGroup: false,
actionColOptions: {
span: 24,
},
});
return {
fields,
schemas,
validate,
registerForm,
getFieldsValue,
setFieldsValue,
resetFields,
};
},
});
</script>