spring
7 天以前 f24ec48c98a485af26321f7f4b74fe1611162f5a
src/pages/inventoryManagement/stockManagement/subtract.vue
@@ -14,53 +14,26 @@
        </view>
        <view class="form-row">
          <text class="form-label">单位</text>
          <up-input v-model="form.unit" disabled />
          <up-radio-group v-model="form.unit" class="unit-radio-group">
            <up-radio
              v-for="opt in unitOptions"
              :key="opt.value"
              :label="opt.label"
              :name="opt.value"
            ></up-radio>
          </up-radio-group>
        </view>
      </view>
      <view class="form-section">
        <view class="section-title">出库/过磅信息</view>
        <view class="section-title">出库信息</view>
        <view class="form-row">
          <text class="form-label">车牌号</text>
          <up-input v-model="form.licensePlateNo" placeholder="请输入车牌号" />
        </view>
        <view class="form-row">
          <text class="form-label">毛重(吨)</text>
          <text class="form-label">出库数量</text>
          <up-input
            v-model="form.grossWeight"
            v-model="form.stockOutNum"
            type="number"
            placeholder="请输入毛重"
            :placeholder="'最大' + maxOutQuantity"
          />
        </view>
        <view class="form-row">
          <text class="form-label">皮重(吨)</text>
          <up-input
            v-model="form.tareWeight"
            type="number"
            placeholder="请输入皮重"
          />
        </view>
        <view class="form-row">
          <text class="form-label">净重(吨)</text>
          <up-input
            v-model="form.netWeight"
            type="number"
            disabled
            placeholder="自动计算"
          />
        </view>
        <view class="form-row">
          <text class="form-label">过磅日期</text>
          <view class="selector-trigger" @click="openWeighingDatePicker">
            <text class="selector-text" :class="{ placeholder: !form.weighingDate }">
              {{ form.weighingDate || '请选择过磅日期' }}
            </text>
            <up-icon name="calendar" size="16" color="#999"></up-icon>
          </view>
        </view>
        <view class="form-row">
          <text class="form-label">过磅员</text>
          <up-input v-model="form.weighingOperator" placeholder="请输入过磅员" />
        </view>
        <view class="form-row">
          <text class="form-label">备注</text>
@@ -72,24 +45,12 @@
    <view class="bottom-bar">
      <view class="btn-submit" @click="handleSubmit">出库</view>
    </view>
    <!-- 过磅日期选择器 -->
    <up-popup :show="showWeighingDatePicker" mode="bottom" @close="showWeighingDatePicker = false">
      <up-datetime-picker
        :show="true"
        v-model="weighingDateValue"
        mode="datetime"
        @confirm="onWeighingDateConfirm"
        @cancel="showWeighingDatePicker = false"
      />
    </up-popup>
  </view>
</template>
<script setup>
import { ref, reactive, watch, computed } from 'vue'
import { ref, reactive, computed } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import dayjs from 'dayjs'
import PageHeader from '@/components/PageHeader.vue'
import { subtractStockInventory } from '@/api/inventoryManagement/stockInventory.js'
@@ -102,7 +63,7 @@
  parentName: '',
  productName: '',
  model: '',
  unit: '',
  unit: '吨',
  qualitity: undefined,
  lockedQuantity: undefined,
  unLockedQuantity: undefined,
@@ -113,24 +74,50 @@
  createTime: '',
  updateTime: '',
  version: undefined,
  // 出库/过磅信息
  licensePlateNo: '',
  grossWeight: '',
  tareWeight: '',
  netWeight: '',
  weighingDate: '',
  weighingOperator: '',
  // 出库信息
  stockOutNum: '',
  remark: ''
})
const type = ref('0') // 固定合格库存
const showWeighingDatePicker = ref(false)
const weighingDateValue = ref(Date.now())
const unitOptions = [
  { label: '吨', value: '吨' },
  { label: '公斤', value: '公斤' }
]
const maxAllowedNetWeight = computed(() => {
  const v = form.unLockedQuantity ?? form.qualitity
  const n = Number(v)
  return !isNaN(n) ? n : 0
// 记录的原始单位,用于吨/公斤换算校验“最大可出库数量”
const recordUnit = ref('')
const normalizeUnit = (u) => {
  if (!u) return ''
  const s = String(u).trim()
  if (s === '吨' || s === 't' || s === 'ton' || s === 'tonne') return '吨'
  if (
    s === '公斤' ||
    s === 'kg' ||
    s === 'kilogram' ||
    s === '千克' ||
    s === 'kilograms'
  )
    return '公斤'
  return s
}
const convertByUnit = (value, fromUnit, toUnit) => {
  if (value === '' || value === undefined || value === null) return 0
  const num = Number(value)
  if (Number.isNaN(num)) return 0
  if (fromUnit === toUnit) return num
  // 默认只处理吨<->公斤;其他单位直接原样返回
  if (fromUnit === '吨' && toUnit === '公斤') return num * 1000
  if (fromUnit === '公斤' && toUnit === '吨') return num / 1000
  return num
}
const maxOutQuantity = computed(() => {
  const baseMax = Number(form.unLockedQuantity ?? 0)
  if (!recordUnit.value || !form.unit) return baseMax
  return convertByUnit(baseMax, recordUnit.value, form.unit)
})
onLoad((options) => {
@@ -142,6 +129,9 @@
      const item = payload && payload.item ? payload.item : payload
      // 将列表记录的完整字段拷贝到表单中,保持与 PC 端 Subtract.vue 一致
      Object.assign(form, item)
      const normalizedUnit = normalizeUnit(form.unit)
      form.unit = normalizedUnit === '吨' || normalizedUnit === '公斤' ? normalizedUnit : '吨'
      recordUnit.value = form.unit
      uni.removeStorageSync('stockSubtractRecord')
    } catch (e) {
      uni.removeStorageSync('stockSubtractRecord')
@@ -149,49 +139,26 @@
  }
})
// 净重 = 毛重 - 皮重
const computeNetWeight = () => {
  const gross = Number(form.grossWeight)
  const tare = Number(form.tareWeight)
  if (!isNaN(gross) && !isNaN(tare)) {
    const net = Number((gross - tare).toFixed(2))
    form.netWeight = net > 0 ? net : 0
  } else {
    form.netWeight = ''
  }
}
watch(
  () => [form.grossWeight, form.tareWeight],
  () => {
    computeNetWeight()
  }
)
const openWeighingDatePicker = () => {
  weighingDateValue.value = form.weighingDate
    ? dayjs(form.weighingDate, 'YYYY-MM-DD HH:mm:ss').valueOf()
    : Date.now()
  showWeighingDatePicker.value = true
}
const onWeighingDateConfirm = (e) => {
  const ts = e?.value ?? weighingDateValue.value
  form.weighingDate = dayjs(ts).format('YYYY-MM-DD HH:mm:ss')
  showWeighingDatePicker.value = false
}
const handleSubmit = () => {
  if (!form.id) {
    uni.showToast({ title: '记录信息缺失,无法出库', icon: 'none' })
    return
  }
  const net = Number(form.netWeight)
  if (!isNaN(net) && net > 0 && maxAllowedNetWeight.value > 0 && net > maxAllowedNetWeight.value) {
    uni.showToast({ title: `净重不能大于可用库存 ${maxAllowedNetWeight.value}`, icon: 'none' })
  const normalizedUnit = normalizeUnit(form.unit)
  form.unit = normalizedUnit === '吨' || normalizedUnit === '公斤' ? normalizedUnit : '吨'
  const outNum = Number(form.stockOutNum)
  const max = Number(maxOutQuantity.value ?? 0)
  if (!outNum || outNum <= 0 || outNum > max) {
    uni.showToast({ title: `请输入 1~${max} 之间的数量`, icon: 'none' })
    return
  }
  const payload = { ...form }
  const payload = {
    id: form.id,
    stockOutNum: outNum,
    remark: form.remark,
    unit: form.unit
  }
  subtractStockInventory(payload)
    .then(() => {
      uni.showToast({ title: '出库成功', icon: 'success' })
@@ -271,5 +238,11 @@
  align-items: center;
  justify-content: center;
}
.unit-radio-group {
  display: flex;
  gap: 24rpx;
  align-items: center;
  flex-wrap: wrap;
}
</style>