123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- import { times, plus, minus, divide } from 'number-precision'
- export function formatPrice(value, needSymbol = true) {
-
- if (value === 0) {
- return needSymbol ? '$0.00' : '0.00'
- } else {
- return needSymbol ? `$${value}` : `${value}`
- }
- }
- function multiply(value, ratio = 100) {
- return parseFloat((value * ratio).toPrecision(12))
- }
- export function round(number, ratio = 100) {
- return Math.round(multiply(number, ratio)) / ratio
- }
- export function transformNumber(value, ratio = 1) {
- const v = Number(value)
- if (Number.isNaN(v)) {
- return value
- } else {
-
- return divide(Math.trunc(times(times(v, ratio), 100)), 100)
- }
- }
- export const getUnit = function (buyNum, index, attributeList, basePriceList) {
-
- const candidate = Object.entries(attributeList).filter(
- item => buyNum >= item[1]
- )
- let key = 'website_qty1'
- if (candidate.length) {
- key = candidate.pop()[0]
- }
- return transformNumber(basePriceList[index][key], buyNum)
- }
- export const getSetup = function (buyNum, form, additionList) {
- const sum = Object.entries(form).reduce((total, current) => {
- let s = 0
-
- if (/\d+/.test(current[0]) && current[1].enable) {
- const temp = current[1].colorForm.filter(
- i => i.id === current[1].printService
- )
- let colorNumber = 1
- if (temp.length) {
- colorNumber = temp[0].colorNumber
- }
- const decoration = current[1].decorationList.filter(
- i => i.id === current[1].printService
- )
- let setup = 0
- if (decoration.length) {
- setup = plus(
- Number(decoration[0].website_setup),
- times(Number(decoration[0].supplier_setup), colorNumber - 1)
- )
- }
- s = plus(s, setup)
- } else if (current[1].length) {
-
- const addition = additionList[current[0]].filter(addition =>
- current[1].includes(addition.id)
- )
- if (addition.length) {
- const temp = addition.reduce((t, c) => {
- let value = Number(c.website_setup)
- if ([5, 6].includes(c.website_setup_id)) {
-
- value = 0
- }
- return plus(t, Number.isNaN(value) ? 0 : value)
- }, 0)
- s = plus(s, temp)
- }
- }
- total = plus(total, s)
- return total
- }, 0)
- return transformNumber(sum)
- }
- export const getPrint = function (buyNum, form, attributeList) {
-
- const candidate = Object.entries(attributeList).filter(
- item => buyNum >= item[1]
- )
- const key = `website_qty${candidate.length}`
- const key2 = `supplier_qty${candidate.length}`
-
- const result = Object.entries(form).reduce((total, current) => {
- if (total === 'POA') {
- return total
- }
- let sum = 0
- if (/\d+/.test(current[0]) && current[1].enable) {
- const temp = current[1].colorForm.filter(
- i => i.id === current[1].printService
- )
- let colorNumber = 1
- if (temp.length) {
- colorNumber = temp[0].colorNumber
- }
- const decoration = current[1].decorationList.filter(
- i => i.id === current[1].printService
- )
-
- const p1 = transformNumber(decoration[0][key])
- if (p1 === 999 || p1 === 111 || typeof p1 !== 'number') {
- return 'POA'
- }
-
- const p2 = transformNumber(decoration[0][key2])
- if (p2 === 999 || p2 === 111 || typeof p2 !== 'number') {
- return 'POA'
- }
- let price = 0
- if (decoration.length) {
- price = plus(p1, times(p2, colorNumber - 1))
- }
- sum = plus(sum, price)
- }
- total = plus(total, sum)
- return total
- }, 0)
- return transformNumber(result, buyNum)
- }
- export const getAddon = function (buyNum, form, attributeList, additionList) {
-
- const candidate = Object.entries(attributeList).filter(
- item => buyNum >= item[1]
- )
- const key = `website_qty${candidate.length}`
- const result = Object.entries(form).reduce((total, current) => {
- if (total === 'POA') {
- return total
- }
- let sum = 0
- if (!/\d+/.test(current[0]) && current[0] !== 'packaging') {
- sum = additionList[current[0]]
- .filter(item => current[1].includes(item.id))
- .reduce((t, c) => {
- if (t === 'POA') {
- return t
- }
- let temp = transformNumber(c[key])
- if (temp === 999 || temp === 111 || typeof temp !== 'number') {
- temp = 0
- return 'POA'
- }
- t = plus(t, temp)
- return t
- }, 0)
- }
- return plus(total, sum)
- }, 0)
- return transformNumber(result, buyNum)
- }
- export const getPackaging = function (
- buyNum,
- form,
- attributeList,
- additionList
- ) {
-
- const candidate = Object.entries(attributeList).filter(
- item => buyNum >= item[1]
- )
- const key = `website_qty${candidate.length}`
- const result = Object.entries(form).reduce((total, current) => {
- if (total === 'POA') {
- return total
- }
- let sum = 0
- if (!/\d+/.test(current[0]) && current[0] === 'packaging') {
- sum = additionList[current[0]]
- .filter(item => current[1].includes(item.id))
- .reduce((t, c) => {
- if (t === 'POA') {
- return t
- }
- let temp = transformNumber(c[key])
- if (temp === 999 || temp === 111 || typeof temp !== 'number') {
- temp = 0
- return 'POA'
- }
- t = plus(t, temp)
- return t
- }, 0)
- }
- return plus(total, sum)
- }, 0)
- return transformNumber(result, buyNum)
- }
- export const getFright = function (buyNum, config, freight, weight, ratio = 1) {
-
- const totalWeight = Math.ceil(times(+weight.unit_w_local, buyNum))
- const expressFactor = plus(
- 1,
- divide(plus(+config.express_freight, +config.fuel), 100)
- )
- const AAEFactor = plus(
- 1,
- divide(plus(+config.bag_freight, +config.fuel), 100)
- )
- let frightCost = 0
- if (freight.type === 1) {
- if (totalWeight > 20) {
- const a1 = minus(totalWeight, 20)
- const a2 = times(a1, +freight.basic)
- const a3 = plus(+freight.pickup, a2)
- frightCost = times(a3, expressFactor)
- } else {
- frightCost = times(+freight.pickup, expressFactor)
- }
- } else if (freight.type === 2) {
- const a1 = times(totalWeight, +freight.basic)
- const a2 = plus(+freight.pickup, a1)
- frightCost = times(a2, AAEFactor)
- } else {
- frightCost = 0
- }
- return transformNumber(frightCost, ratio)
- }
|