minSellPrice.js 5.15 KB
import { isMarketingGoods } from './marketingFlag'

export function isBelowMinSellPrice(goods, value) {
  if (!goods || goods.validMinSellPrice !== 'Y' || goods.minSellPrice == null || goods.minSellPrice === '') {
    return false
  }
  return parseFloat(value) < parseFloat(goods.minSellPrice)
}

export function getGoodsDisplayName(goods) {
  if (!goods) return ''
  return goods.goodsAlias || goods.upgradeGoodsName || goods.goodsInfo?.goodsName || goods.baseGoodsName || ''
}

// 活动价:活动商品加入订单时商品列表 sellPrice 的快照(oldPrice),不随用户改价变化
export function getGoodsActivityPrice(goods, orderItem) {
  if (!goods || !isMarketingGoods(goods, orderItem)) return undefined
  if (goods.oldPrice == null || goods.oldPrice === '') return undefined
  return goods.oldPrice
}

function buildSingleApprovalItem(item, goods, number) {
  if (isMarketingGoods(goods, item)) return null
  const sellPrice = normalizeGoodsPrice(goods.sellPrice)
  if (!isBelowMinSellPrice(goods, sellPrice)) return null
  return {
    type: 'single',
    name: getGoodsDisplayName(goods),
    sellPrice,
    activityPrice: getGoodsActivityPrice(goods, item),
    number: number || '1',
    sourceItem: item
  }
}

function getPackageDisplayTotal(item, orderTypeCode) {
  const orderType = String(orderTypeCode || '')
  let total = 0
  item.packageGoods.goodsList.forEach(goods => {
    const sellPrice = parseFloat(goods.sellPrice || 0)
    if (orderType === '1' || orderType === '4') {
      total += sellPrice
    } else if (goods.finalPricePerUnit != null) {
      total += parseFloat(goods.finalPricePerUnit || 0)
    } else {
      total += sellPrice
    }
  })
  return total
}

function buildPackageApprovalItem(item, orderTypeCode) {
  const children = (item.packageGoods.goodsList || [])
    .map(goods => {
      if (isMarketingGoods(goods, item)) return null
      const sellPrice = normalizeGoodsPrice(goods.sellPrice)
      if (!isBelowMinSellPrice(goods, sellPrice)) return null
      return {
        name: getGoodsDisplayName(goods),
        sellPrice,
        activityPrice: getGoodsActivityPrice(goods, item),
        number: item.packageGoods.number || '1',
        sourceGoods: goods
      }
    })
    .filter(Boolean)

  if (!children.length) return null

  return {
    type: 'package',
    name: item.packageGoods.packageAlias || item.packageGoods.packageName || '',
    sellPrice: getPackageDisplayTotal(item, orderTypeCode),
    number: item.packageGoods.number || '1',
    children,
    sourceItem: item
  }
}

export function collectBelowMinSellPriceItems(orderObj) {
  if (!orderObj) return []

  const productFields = ['frontGear', 'frontWindow', 'rearGear', 'rearWindow', 'sunRoof', 'package', 'skuGoods']
  const result = []

  productFields.forEach(field => {
    const list = orderObj[field]
    if (!Array.isArray(list)) return

    list.forEach(item => {
      if (item.goodsFlag == 1 && item.goods) {
        const approvalItem = buildSingleApprovalItem(item, item.goods, item.goods.number)
        if (approvalItem) result.push(approvalItem)
        return
      }

      if (item.goodsFlag == 2) {
        if (item.multipleSpecificationsGoods?.goods) {
          const approvalItem = buildSingleApprovalItem(
            item,
            item.multipleSpecificationsGoods.goods,
            item.multipleSpecificationsGoods.number
          )
          if (approvalItem) result.push(approvalItem)
          return
        }
        if (item.goods) {
          const approvalItem = buildSingleApprovalItem(item, item.goods, item.goods.number)
          if (approvalItem) result.push(approvalItem)
        }
        return
      }

      if (item.goodsFlag == 3 && item.packageGoods) {
        const approvalItem = buildPackageApprovalItem(item, orderObj.orderTypeCode)
        if (approvalItem) result.push(approvalItem)
      }
    })
  })

  return result
}

export function hasBelowMinSellPriceGoods(orderObj) {
  return collectBelowMinSellPriceItems(orderObj).length > 0
}

export function shouldUseMinPriceApproval(orderTypeCode) {
  return String(orderTypeCode || '') === '1'
}

export function applyLegacyMinSellPriceClamp(goods, value) {
  let price = normalizeGoodsPrice(value)
  if (isBelowMinSellPrice(goods, price)) {
    uni.showToast({
      title: '单价不可低于商品最低价',
      icon: 'none'
    })
    price = String(goods.minSellPrice)
  }
  return price
}

export function normalizeGoodsPrice(value) {
  let price = value
  if (!price || String(price).trim() === '') {
    price = '0'
  }
  price = String(price).replace(/[^\d.]/g, '')
  if (price.split('.').length > 2) {
    price = price.slice(0, price.lastIndexOf('.')) + price.slice(price.lastIndexOf('.') + 1)
  }
  if (price.includes('.')) {
    const parts = price.split('.')
    if (parts[1].length > 2) {
      parts[1] = parts[1].substring(0, 2)
      price = parts.join('.')
    }
  }
  if (parseFloat(price) > 100000) {
    price = '100000'
  }
  if (parseFloat(price) < 0 || parseFloat(price) == 0) {
    price = '0'
  }
  return price
}

export function getSkuGoodsPriceTarget(item) {
  if (item?.multipleSpecificationsGoods?.goods) {
    return item.multipleSpecificationsGoods.goods
  }
  return item?.goods || null
}