Skip to content

Commit 3308a49

Browse files
committed
✨ ERP:增加金额计算的四舍五入
1 parent 1c98dea commit 3308a49

File tree

3 files changed

+185
-34
lines changed

3 files changed

+185
-34
lines changed

src/utils/index.ts

Lines changed: 127 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,56 @@ export const copyValueToTarget = (target: any, source: any) => {
190190
Object.assign(target, newObj)
191191
}
192192

193+
/**
194+
* 获取链接的参数值
195+
* @param key 参数键名
196+
* @param urlStr 链接地址,默认为当前浏览器的地址
197+
*/
198+
export const getUrlValue = (key: string, urlStr: string = location.href): string => {
199+
if (!urlStr || !key) return ''
200+
const url = new URL(decodeURIComponent(urlStr))
201+
return url.searchParams.get(key) ?? ''
202+
}
203+
204+
/**
205+
* 获取链接的参数值(值类型)
206+
* @param key 参数键名
207+
* @param urlStr 链接地址,默认为当前浏览器的地址
208+
*/
209+
export const getUrlNumberValue = (key: string, urlStr: string = location.href): number => {
210+
return toNumber(getUrlValue(key, urlStr))
211+
}
212+
213+
/**
214+
* 构建排序字段
215+
* @param prop 字段名称
216+
* @param order 顺序
217+
*/
218+
export const buildSortingField = ({ prop, order }) => {
219+
return { field: prop, order: order === 'ascending' ? 'asc' : 'desc' }
220+
}
221+
222+
// ========== NumberUtils 数字方法 ==========
223+
224+
/**
225+
* 数组求和
226+
*
227+
* @param values 数字数组
228+
* @return 求和结果,默认为 0
229+
*/
230+
export const getSumValue = (values: number[]): number => {
231+
return values.reduce((prev, curr) => {
232+
const value = Number(curr)
233+
if (!Number.isNaN(value)) {
234+
return prev + curr
235+
} else {
236+
return prev
237+
}
238+
}, 0)
239+
}
240+
241+
// ========== 通用金额方法 ==========
242+
193243
/**
194244
* 将一个整数转换为分数保留两位小数
195245
* @param num
@@ -206,6 +256,7 @@ export const formatToFraction = (num: number | string | undefined): string => {
206256
*
207257
* @param num 整数
208258
*/
259+
// TODO @芋艿:看看怎么融合掉
209260
export const floatToFixed2 = (num: number | string | undefined): string => {
210261
let str = '0.00'
211262
if (typeof num === 'undefined') {
@@ -232,6 +283,7 @@ export const floatToFixed2 = (num: number | string | undefined): string => {
232283
* 将一个分数转换为整数
233284
* @param num
234285
*/
286+
// TODO @芋艿:看看怎么融合掉
235287
export const convertToInteger = (num: number | string | undefined): number => {
236288
if (typeof num === 'undefined') return 0
237289
const parsedNumber = typeof num === 'string' ? parseFloat(num) : num
@@ -266,31 +318,89 @@ export const calculateRelativeRate = (value?: number, reference?: number) => {
266318
return ((100 * ((value || 0) - reference)) / reference).toFixed(0)
267319
}
268320

321+
// ========== ERP 专属方法 ==========
322+
323+
const ERP_COUNT_DIGIT = 3
324+
const ERP_PRICE_DIGIT = 2
325+
269326
/**
270-
* 获取链接的参数值
271-
* @param key 参数键名
272-
* @param urlStr 链接地址,默认为当前浏览器的地址
327+
* 【ERP】格式化 Input 数字
328+
*
329+
* 例如说:库存数量
330+
*
331+
* @param num 数量
332+
* @return 格式化后的数量
273333
*/
274-
export const getUrlValue = (key: string, urlStr: string = location.href): string => {
275-
if (!urlStr || !key) return ''
276-
const url = new URL(decodeURIComponent(urlStr))
277-
return url.searchParams.get(key) ?? ''
334+
export const erpNumberFormatter = (num: number | string | undefined, digit: number) => {
335+
if (num === null) {
336+
return ''
337+
}
338+
if (typeof num === 'string') {
339+
num = parseFloat(num)
340+
}
341+
// 如果非 number,则直接返回空串
342+
if (isNaN(num)) {
343+
return ''
344+
}
345+
return num.toFixed(digit)
278346
}
279347

280348
/**
281-
* 获取链接的参数值(值类型)
282-
* @param key 参数键名
283-
* @param urlStr 链接地址,默认为当前浏览器的地址
349+
* 【ERP】格式化数量,保留三位小数
350+
*
351+
* 例如说:库存数量
352+
*
353+
* @param num 数量
354+
* @return 格式化后的数量
284355
*/
285-
export const getUrlNumberValue = (key: string, urlStr: string = location.href): number => {
286-
return toNumber(getUrlValue(key, urlStr))
356+
export const erpCountInputFormatter = (num: number | string | undefined) => {
357+
return erpNumberFormatter(num, ERP_COUNT_DIGIT)
287358
}
288359

360+
// noinspection JSCommentMatchesSignature
289361
/**
290-
* 构建排序字段
291-
* @param prop 字段名称
292-
* @param order 顺序
362+
* 【ERP】格式化数量,保留三位小数
363+
*
364+
* @param cellValue 数量
365+
* @return 格式化后的数量
293366
*/
294-
export const buildSortingField = ({ prop, order }) => {
295-
return { field: prop, order: order === 'ascending' ? 'asc' : 'desc' }
367+
export const erpCountTableColumnFormatter = (_, __, cellValue: any, ___) => {
368+
return erpNumberFormatter(cellValue, ERP_COUNT_DIGIT)
369+
}
370+
371+
/**
372+
* 【ERP】格式化金额,保留二位小数
373+
*
374+
* 例如说:库存数量
375+
*
376+
* @param num 数量
377+
* @return 格式化后的数量
378+
*/
379+
export const erpPriceInputFormatter = (num: number | string | undefined) => {
380+
return erpNumberFormatter(num, ERP_PRICE_DIGIT)
381+
}
382+
383+
// noinspection JSCommentMatchesSignature
384+
/**
385+
* 【ERP】格式化金额,保留二位小数
386+
*
387+
* @param cellValue 数量
388+
* @return 格式化后的数量
389+
*/
390+
export const erpPriceTableColumnFormatter = (_, __, cellValue: any, ___) => {
391+
return erpNumberFormatter(cellValue, ERP_PRICE_DIGIT)
392+
}
393+
394+
/**
395+
* 【ERP】价格计算,四舍五入保留两位小数
396+
*
397+
* @param price 价格
398+
* @param count 数量
399+
* @return 总价格。如果有任一为空,则返回 undefined
400+
*/
401+
export const erpPriceMultiply = (price: number, count: number) => {
402+
if (price == null || count == null) {
403+
return undefined
404+
}
405+
return parseFloat((price * count).toFixed(ERP_PRICE_DIGIT))
296406
}

src/views/erp/stock/in/components/StockInItemForm.vue

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
:inline-message="true"
99
:disabled="disabled"
1010
>
11-
<el-table :data="formData" show-summary class="-mt-10px">
11+
<el-table :data="formData" show-summary :summary-method="getSummaries" class="-mt-10px">
1212
<el-table-column label="序号" type="index" align="center" width="60" />
1313
<el-table-column label="仓库名称" min-width="125">
1414
<template #default="{ row, $index }">
@@ -55,7 +55,7 @@
5555
<el-table-column label="库存" min-width="100">
5656
<template #default="{ row }">
5757
<el-form-item class="mb-0px!">
58-
<el-input disabled v-model="row.stockCount" />
58+
<el-input disabled v-model="row.stockCount" :formatter="erpCountInputFormatter" />
5959
</el-form-item>
6060
</template>
6161
</el-table-column>
@@ -73,13 +73,14 @@
7373
</el-form-item>
7474
</template>
7575
</el-table-column>
76-
<el-table-column label="数量" prop="count" fixed="right" min-width="120">
76+
<el-table-column label="数量" prop="count" fixed="right" min-width="140">
7777
<template #default="{ row, $index }">
7878
<el-form-item :prop="`${$index}.count`" :rules="formRules.count" class="mb-0px!">
7979
<el-input-number
8080
v-model="row.count"
8181
controls-position="right"
82-
:min="1"
82+
:min="0.001"
83+
:precision="3"
8384
class="!w-100%"
8485
/>
8586
</el-form-item>
@@ -92,7 +93,13 @@
9293
:rules="formRules.productPrice"
9394
class="mb-0px!"
9495
>
95-
<el-input-number v-model="row.productPrice" controls-position="right" :min="1" />
96+
<el-input-number
97+
v-model="row.productPrice"
98+
controls-position="right"
99+
:min="0.01"
100+
:precision="2"
101+
class="!w-100%"
102+
/>
96103
</el-form-item>
97104
</template>
98105
</el-table-column>
@@ -103,7 +110,7 @@
103110
:rules="formRules.totalPrice"
104111
class="mb-0px!"
105112
>
106-
<el-input disabled v-model="row.totalPrice" />
113+
<el-input disabled v-model="row.totalPrice" :formatter="erpPriceInputFormatter" />
107114
</el-form-item>
108115
</template>
109116
</el-table-column>
@@ -129,6 +136,13 @@
129136
import { ProductApi, ProductVO } from '@/api/erp/product/product'
130137
import { WarehouseApi, WarehouseVO } from '@/api/erp/stock/warehouse'
131138
import { StockApi } from '@/api/erp/stock/stock'
139+
import {
140+
erpCountInputFormatter,
141+
erpPriceInputFormatter,
142+
erpPriceMultiply,
143+
getSumValue
144+
} from '@/utils'
145+
import { fenToYuanFormat } from '@/utils/formatter'
132146
133147
const props = defineProps<{
134148
items: undefined
@@ -166,17 +180,33 @@ watch(
166180
}
167181
// 循环处理
168182
val.forEach((item) => {
169-
// TODO 芋艿:后面处理下相乘问题;包括后端的;
170-
if (item.productPrice && item.count) {
171-
item.totalPrice = item.productPrice * item.count
172-
} else {
173-
item.totalPrice = undefined
174-
}
183+
item.totalPrice = erpPriceMultiply(item.productPrice, item.count)
175184
})
176185
},
177186
{ deep: true }
178187
)
179188
189+
/** 合计 */
190+
const getSummaries = (param: SummaryMethodProps) => {
191+
const { columns, data } = param
192+
const sums: string[] = []
193+
columns.forEach((column, index) => {
194+
if (index === 0) {
195+
sums[index] = '合计'
196+
return
197+
}
198+
if (['count', 'totalPrice'].includes(column.property)) {
199+
const sum = getSumValue(data.map((item) => Number(item[column.property])))
200+
sums[index] =
201+
column.property === 'count' ? erpCountInputFormatter(sum) : erpPriceInputFormatter(sum)
202+
} else {
203+
sums[index] = ''
204+
}
205+
})
206+
207+
return sums
208+
}
209+
180210
/** 新增按钮操作 */
181211
const handleAdd = () => {
182212
const row = {

src/views/erp/stock/in/index.vue

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,22 @@
157157
label="入库时间"
158158
align="center"
159159
prop="inTime"
160-
:formatter="dateFormatter"
161-
width="180px"
160+
:formatter="dateFormatter2"
161+
width="120px"
162162
/>
163163
<el-table-column label="创建人" align="center" prop="creatorName" />
164-
<el-table-column label="数量" align="center" prop="totalCount" />
165-
<el-table-column label="金额" align="center" prop="totalPrice" />
164+
<el-table-column
165+
label="数量"
166+
align="center"
167+
prop="totalCount"
168+
:formatter="erpCountTableColumnFormatter"
169+
/>
170+
<el-table-column
171+
label="金额"
172+
align="center"
173+
prop="totalPrice"
174+
:formatter="erpPriceTableColumnFormatter"
175+
/>
166176
<el-table-column label="状态" align="center" fixed="right" width="90" prop="status">
167177
<template #default="scope">
168178
<dict-tag :type="DICT_TYPE.ERP_AUDIT_STATUS" :value="scope.row.status" />
@@ -229,7 +239,7 @@
229239

230240
<script setup lang="ts">
231241
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
232-
import { dateFormatter } from '@/utils/formatTime'
242+
import { dateFormatter, dateFormatter2 } from '@/utils/formatTime'
233243
import download from '@/utils/download'
234244
import { StockInApi, StockInVO } from '@/api/erp/stock/in'
235245
import StockInForm from './StockInForm.vue'
@@ -239,6 +249,7 @@ import { SupplierApi, SupplierVO } from '@/api/erp/purchase/supplier'
239249
import { UserVO } from '@/api/system/user'
240250
import * as UserApi from '@/api/system/user'
241251
import * as BusinessApi from '@/api/crm/business'
252+
import { erpCountTableColumnFormatter, erpPriceTableColumnFormatter } from '@/utils'
242253
243254
/** ERP 其它入库单 列表 */
244255
defineOptions({ name: 'ErpStockIn' })

0 commit comments

Comments
 (0)