李红攀:V2.6.969,销售出库管理

This commit is contained in:
2026-06-22 17:09:31 +08:00
parent d0ce2f2b55
commit 617d293dc3
7 changed files with 1710 additions and 2 deletions

View File

@@ -0,0 +1,643 @@
<template>
<view class="yd-page-container">
<wd-navbar
:title="pageTitle"
left-arrow
placeholder
safe-area-inset-top
fixed
@click-left="handleBack"
/>
<view class="pb-180rpx">
<wd-form ref="formRef" :model="formData" :rules="formRules">
<wd-cell-group title="基本信息" border>
<wd-cell title="出库单号" :value="formData.no || '保存后自动生成'" />
<wd-cell title="客户" :value="formData.customerName || '-'" />
<wd-cell
title="关联订单"
:value="formData.orderNo || '点击选择'"
is-link
@click="handlePickOrder"
/>
<wd-cell title="出库时间" title-width="180rpx" prop="outTime" center>
<wd-datetime-picker
v-model="formData.outTime"
type="date"
label=""
placeholder="请选择出库时间"
/>
</wd-cell>
<wd-picker
v-model="formData.saleUserId"
label="销售人员"
label-width="180rpx"
:columns="userColumns"
placeholder="请选择销售人员"
clearable
/>
<wd-picker
v-model="formData.accountId"
label="结算账户"
label-width="180rpx"
:columns="accountColumns"
placeholder="请选择结算账户"
clearable
/>
<wd-input
v-model="formData.discountPercent"
label="优惠率(%)"
label-width="180rpx"
type="digit"
placeholder="请输入优惠率"
clearable
/>
<wd-input
v-model="formData.otherPrice"
label="其他费用"
label-width="180rpx"
type="digit"
placeholder="请输入其他费用"
clearable
/>
<wd-textarea
v-model="formData.remark"
label="备注"
label-width="180rpx"
placeholder="请输入备注"
:maxlength="200"
show-word-limit
clearable
/>
</wd-cell-group>
<wd-cell-group title="出库明细" border>
<view
v-for="(item, index) in formData.items"
:key="item.id || `${item.productId}-${index}`"
class="mx-24rpx mb-20rpx rounded-12rpx bg-[#f9f9f9] p-24rpx"
>
<view class="mb-12rpx text-28rpx text-[#333] font-semibold">
{{ item.productName || `产品${index + 1}` }}
</view>
<view class="mb-12rpx flex justify-between text-24rpx text-[#999]">
<text>产品编码</text>
<text class="text-[#333]">{{ item.productBarCode || '-' }}</text>
</view>
<view class="mb-12rpx flex justify-between text-24rpx text-[#999]">
<text>单位</text>
<text class="text-[#333]">{{ item.productUnitName || '-' }}</text>
</view>
<view class="mb-12rpx flex justify-between text-24rpx text-[#999]">
<text>订单可出</text>
<text class="text-[#333]">{{ formatCount(getOrderAvailableCount(item)) }}</text>
</view>
<view class="mb-12rpx flex justify-between text-24rpx text-[#999]">
<text>可用库存</text>
<text class="text-[#333]">{{ formatCount(item.stockCount) }}</text>
</view>
<wd-picker
v-model="item.warehouseId"
class="mb-12rpx"
label="仓库"
label-width="120rpx"
:columns="warehouseColumns"
placeholder="请选择仓库"
clearable
@confirm="(e: any) => onWarehouseConfirm(e, index)"
/>
<wd-input
v-model="item.count"
class="mb-12rpx"
label="数量"
label-width="120rpx"
type="digit"
placeholder="请输入数量"
clearable
/>
<wd-input
v-model="item.productPrice"
class="mb-12rpx"
label="单价"
label-width="120rpx"
type="digit"
placeholder="请输入单价"
clearable
/>
<wd-input
v-model="item.taxPercent"
class="mb-12rpx"
label="税率(%)"
label-width="120rpx"
type="digit"
placeholder="请输入税率"
clearable
/>
<wd-input
v-model="item.batchNo"
class="mb-12rpx"
label="批次号"
label-width="120rpx"
placeholder="请输入批次号"
clearable
/>
<wd-input
v-model="item.remark"
label="备注"
label-width="120rpx"
placeholder="请输入备注"
clearable
/>
<view class="mt-12rpx flex justify-between text-26rpx">
<text class="text-[#999]">金额</text>
<text class="text-[#409eff] font-semibold">{{ formatPrice(calculateItemTotal(item)) }}</text>
</view>
</view>
<view v-if="!formData.items?.length" class="py-40rpx text-center text-[#999]">
请先选择可出库销售订单
</view>
</wd-cell-group>
<wd-cell-group title="汇总信息" border>
<wd-cell title="总数量" :value="formatCount(totalCount)" />
<wd-cell title="商品金额" :value="`¥${formatPrice(totalProductPrice)}`" />
<wd-cell title="税额合计" :value="`¥${formatPrice(totalTaxPrice)}`" />
<wd-cell title="应收金额" :value="`¥${formatPrice(totalPrice)}`" />
</wd-cell-group>
</wd-form>
</view>
<view class="yd-detail-footer">
<wd-button type="primary" block :loading="formLoading" @click="handleSubmit">
保存
</wd-button>
</view>
<wd-popup v-model="orderPickerVisible" position="right" custom-style="width: 100%; height: 100%;">
<view class="yd-page-container">
<wd-navbar
title="选择销售订单"
left-arrow
placeholder
safe-area-inset-top
fixed
@click-left="orderPickerVisible = false"
/>
<wd-search
v-model="orderQueryParams.no"
placeholder="搜索订单单号"
@search="getOrderList"
@clear="getOrderList"
/>
<view class="px-24rpx pb-180rpx">
<view
v-for="item in orderList"
:key="item.id"
class="mb-20rpx overflow-hidden rounded-12rpx bg-white shadow-sm"
:class="{ 'border-2 border-[#1890ff]': selectedOrderId === item.id }"
@click="handleSelectOrder(item)"
>
<view class="p-24rpx">
<view class="mb-12rpx flex items-center justify-between">
<view class="text-30rpx text-[#333] font-semibold">
{{ item.no }}
</view>
<wd-icon
v-if="selectedOrderId === item.id"
name="check"
color="#1890ff"
size="40rpx"
/>
</view>
<view class="mb-8rpx flex items-center justify-between text-26rpx text-[#666]">
<text class="text-[#999]">客户</text>
<text>{{ item.customerName || '-' }}</text>
</view>
<view class="mb-8rpx flex items-center justify-between text-26rpx text-[#666]">
<text class="text-[#999]">产品</text>
<text class="line-clamp-1 ml-16rpx max-w-[420rpx] text-right">{{ item.productNames || '-' }}</text>
</view>
<view class="mt-12rpx flex justify-around border-t border-[#f0f0f0] pt-12rpx">
<view class="text-center">
<view class="text-28rpx text-[#333] font-semibold">
{{ formatCount(item.totalCount) }}
</view>
<view class="text-22rpx text-[#999]">
总数量
</view>
</view>
<view class="text-center">
<view class="text-28rpx text-[#52c41a] font-semibold">
{{ formatCount(item.outCount) }}
</view>
<view class="text-22rpx text-[#999]">
已出库
</view>
</view>
<view class="text-center">
<view class="text-28rpx text-[#409eff] font-semibold">
{{ formatPrice(item.totalPrice) }}
</view>
<view class="text-22rpx text-[#999]">
金额
</view>
</view>
</view>
</view>
</view>
<view v-if="orderList.length === 0 && orderLoadMoreState !== 'loading'" class="py-100rpx text-center">
<wd-status-tip image="content" tip="暂无可出库订单" />
</view>
<wd-loadmore
v-if="orderList.length > 0"
:state="orderLoadMoreState"
@reload="loadMoreOrders"
/>
</view>
<view class="yd-detail-footer">
<wd-button type="primary" block :disabled="!selectedOrderId" @click="confirmSelectOrder">
确认选择
</wd-button>
</view>
</view>
</wd-popup>
</view>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'wot-design-uni/components/wd-form/types'
import type { SaleOrder, SaleOrderItem } from '@/api/erp/sale-order'
import type { SaleOut, SaleOutItem } from '@/api/erp/sale-out'
import type { LoadMoreState } from '@/http/types'
import { computed, onMounted, ref } from 'vue'
import { useToast } from 'wot-design-uni'
import { getAccountSimpleList } from '@/api/erp/account'
import { getApprovalRecordListByBiz } from '@/api/erp/approval-record'
import { getSaleOrder, getSaleOrderPage } from '@/api/erp/sale-order'
import { createSaleOut, getSaleOut, updateSaleOut } from '@/api/erp/sale-out'
import { getWarehouseSimpleList } from '@/api/erp/warehouse'
import { getSimpleUserList } from '@/api/system/user'
import { navigateBackPlus } from '@/utils'
const props = defineProps<{
id?: number | string
}>()
definePage({
style: {
navigationBarTitleText: '',
navigationStyle: 'custom',
},
})
const toast = useToast()
const formLoading = ref(false)
const formRef = ref<FormInstance>()
const warehouseList = ref<{ id?: number, name?: string }[]>([])
const userList = ref<{ id?: number, nickname?: string }[]>([])
const accountList = ref<{ id?: number, name?: string }[]>([])
const orderPickerVisible = ref(false)
const orderList = ref<SaleOrder[]>([])
const selectedOrderId = ref<number>()
const orderLoadMoreState = ref<LoadMoreState>('loading')
const orderTotal = ref(0)
const formData = ref<SaleOut>({
id: undefined,
no: undefined,
customerId: undefined,
customerName: undefined,
accountId: undefined,
saleUserId: undefined,
outTime: Date.now(),
orderId: undefined,
orderNo: undefined,
discountPercent: 0,
otherPrice: 0,
remark: undefined,
items: [],
})
const orderQueryParams = ref({
pageNo: 1,
pageSize: 10,
no: undefined as string | undefined,
outEnable: true,
})
const isEdit = computed(() => !!props.id)
const pageTitle = computed(() => (isEdit.value ? '编辑销售出库' : '新增销售出库'))
const formRules = {
outTime: [{ required: true, message: '请选择出库时间' }],
}
const warehouseColumns = computed(() => warehouseList.value.map(item => ({ label: item.name, value: item.id })))
const userColumns = computed(() => userList.value.map(item => ({ label: item.nickname, value: item.id })))
const accountColumns = computed(() => accountList.value.map(item => ({ label: item.name, value: item.id })))
const totalCount = computed(() => {
return (formData.value.items || []).reduce((sum, item) => sum + Number(item.count || 0), 0)
})
const totalProductPrice = computed(() => {
return (formData.value.items || []).reduce((sum, item) => {
const count = Number(item.count || 0)
const price = Number(item.productPrice || 0)
return sum + count * price
}, 0)
})
const totalTaxPrice = computed(() => {
return (formData.value.items || []).reduce((sum, item) => {
const subtotal = Number(item.count || 0) * Number(item.productPrice || 0)
const taxPercent = Number(item.taxPercent || 0)
return sum + subtotal * taxPercent / 100
}, 0)
})
const totalPrice = computed(() => {
const discountPercent = Number(formData.value.discountPercent || 0)
const discountPrice = totalProductPrice.value * discountPercent / 100
return totalProductPrice.value + totalTaxPrice.value - discountPrice + Number(formData.value.otherPrice || 0)
})
function handleBack() {
navigateBackPlus('/pages-erp/sale-out/index')
}
function formatCount(value?: number) {
if (value === undefined || value === null)
return '0.00'
return Number(value).toFixed(2)
}
function formatPrice(value?: number) {
if (value === undefined || value === null)
return '0.00'
return Number(value).toFixed(2)
}
function calculateItemTotal(item: SaleOutItem) {
const count = Number(item.count || 0)
const price = Number(item.productPrice || 0)
const taxPercent = Number(item.taxPercent || 0)
const subtotal = count * price
return subtotal + subtotal * taxPercent / 100
}
function getOrderAvailableCount(item: SaleOutItem) {
const totalCount = Number(item.totalCount || 0)
const outCount = Number(item.outCount || 0)
return Math.max(totalCount - outCount, 0)
}
function onWarehouseConfirm({ value }: any, index: number) {
const current = formData.value.items?.[index]
if (!current)
return
current.warehouseId = value?.[0]
const warehouse = warehouseList.value.find(item => item.id === current.warehouseId)
current.warehouseName = warehouse?.name
}
async function loadOptions() {
const [warehouses, users, accounts] = await Promise.allSettled([
getWarehouseSimpleList(),
getSimpleUserList(),
getAccountSimpleList(),
])
warehouseList.value = warehouses.status === 'fulfilled' ? (warehouses.value || []) : []
userList.value = users.status === 'fulfilled' ? (users.value || []) : []
accountList.value = accounts.status === 'fulfilled' ? (accounts.value || []) : []
}
async function ensureEditable() {
if (!formData.value.id)
return true
if (formData.value.status === 20) {
toast.warning('已审核单据不可编辑')
setTimeout(() => handleBack(), 300)
return false
}
try {
const approvalList = await getApprovalRecordListByBiz(formData.value.id, 'erp_sale_out')
if (approvalList.length > 0) {
toast.warning('已有审批记录的单据不可编辑')
setTimeout(() => handleBack(), 300)
return false
}
} catch {
// ignore approval query failure here
}
return true
}
async function getDetail() {
if (!props.id)
return
try {
toast.loading('加载中...')
formData.value = await getSaleOut(Number(props.id))
const editable = await ensureEditable()
if (!editable)
return
} finally {
toast.close()
}
}
function handlePickOrder() {
if (isEdit.value && formData.value.orderId) {
toast.warning('编辑模式不支持更换关联订单')
return
}
orderPickerVisible.value = true
orderQueryParams.value.pageNo = 1
orderList.value = []
selectedOrderId.value = formData.value.orderId
getOrderList()
}
async function getOrderList() {
orderLoadMoreState.value = 'loading'
try {
const data = await getSaleOrderPage({ ...orderQueryParams.value })
orderList.value = orderQueryParams.value.pageNo === 1 ? data.list : [...orderList.value, ...data.list]
orderTotal.value = data.total
orderLoadMoreState.value = orderList.value.length >= orderTotal.value ? 'finished' : 'loading'
} catch {
orderQueryParams.value.pageNo = orderQueryParams.value.pageNo > 1 ? orderQueryParams.value.pageNo - 1 : 1
orderLoadMoreState.value = 'error'
}
}
function loadMoreOrders() {
if (orderLoadMoreState.value === 'finished')
return
orderQueryParams.value.pageNo++
getOrderList()
}
function handleSelectOrder(item: SaleOrder) {
selectedOrderId.value = item.id
}
function mapSaleOrderItemToSaleOutItem(item: SaleOrderItem, defaultWarehouseId?: number, defaultWarehouseName?: string): SaleOutItem {
const totalCount = Number(item.totalCount ?? item.count ?? 0)
const outCount = Number(item.outCount || 0)
const availableCount = Math.max(totalCount - outCount, 0)
return {
id: undefined,
orderItemId: item.id,
warehouseId: defaultWarehouseId,
warehouseName: defaultWarehouseName,
productId: item.productId,
productName: item.productName,
productBarCode: item.productBarCode,
productUnitId: item.productUnitId,
productUnitName: item.productUnitName,
productPrice: item.productPrice ?? item.unitPrice,
count: availableCount,
totalCount,
totalPrice: item.totalPrice,
taxPercent: item.taxPercent,
taxPrice: item.taxPrice,
batchNo: undefined,
remark: item.remark,
stockCount: item.stockCount,
totalProductPrice: item.totalProductPrice,
outCount,
}
}
async function confirmSelectOrder() {
if (!selectedOrderId.value)
return
try {
toast.loading('加载订单详情...')
const orderDetail = await getSaleOrder(selectedOrderId.value)
const defaultWarehouse = warehouseList.value[0]
const items = (orderDetail.items || [])
.map(item => mapSaleOrderItemToSaleOutItem(item, defaultWarehouse?.id, defaultWarehouse?.name))
.filter(item => Number(item.count || 0) > 0)
if (items.length === 0) {
toast.warning('该订单没有可出库明细')
return
}
formData.value = {
...formData.value,
customerId: orderDetail.customerId,
customerName: orderDetail.customerName,
accountId: orderDetail.accountId,
saleUserId: orderDetail.saleUserId,
orderId: orderDetail.id,
orderNo: orderDetail.no,
discountPercent: orderDetail.discountPercent || 0,
remark: formData.value.remark || orderDetail.remark,
items,
}
orderPickerVisible.value = false
} finally {
toast.close()
}
}
function buildSubmitData() {
return {
id: formData.value.id,
accountId: formData.value.accountId,
saleUserId: formData.value.saleUserId,
outTime: formData.value.outTime,
orderId: formData.value.orderId,
deliveryNoticeId: (formData.value as any).deliveryNoticeId,
discountPercent: Number(formData.value.discountPercent || 0),
otherPrice: Number(formData.value.otherPrice || 0),
fileUrl: formData.value.fileUrl,
remark: formData.value.remark,
items: (formData.value.items || []).map(item => ({
id: item.id,
orderItemId: item.orderItemId,
warehouseId: item.warehouseId,
productId: item.productId,
productUnitId: item.productUnitId,
productPrice: Number(item.productPrice || 0),
count: Number(item.count || 0),
taxPercent: Number(item.taxPercent || 0),
batchNo: item.batchNo,
remark: item.remark,
stockUnitPrice: item.stockUnitPrice,
stockValue: item.stockValue,
grossProfit: item.grossProfit,
})),
}
}
async function handleSubmit() {
const { valid } = await formRef.value!.validate()
if (!valid)
return
if (!formData.value.orderId) {
toast.warning('请先选择关联订单')
return
}
if (!formData.value.items || formData.value.items.length === 0) {
toast.warning('请至少保留一条出库明细')
return
}
for (let i = 0; i < (formData.value.items || []).length; i++) {
const item = formData.value.items![i]
if (!item.orderItemId || !item.productId || !item.productUnitId) {
toast.warning(`${i + 1} 项明细缺少关联字段`)
return
}
if (!item.warehouseId) {
toast.warning(`${i + 1} 项请选择仓库`)
return
}
if (!item.count || Number(item.count) <= 0) {
toast.warning(`${i + 1} 项数量必须大于 0`)
return
}
if (Number(item.count) > getOrderAvailableCount(item)) {
toast.warning(`${i + 1} 项数量不能超过订单可出数量`)
return
}
}
formLoading.value = true
try {
const submitData = buildSubmitData() as SaleOut
if (isEdit.value) {
await updateSaleOut(submitData)
toast.success('保存成功')
} else {
await createSaleOut(submitData)
toast.success('新增成功')
}
setTimeout(() => {
handleBack()
}, 500)
} finally {
formLoading.value = false
}
}
onMounted(async () => {
await loadOptions()
await getDetail()
})
</script>
<style lang="scss" scoped>
</style>