更新完善页面

This commit is contained in:
董海洋
2026-06-03 14:15:55 +08:00
parent 4b7ae9c933
commit 1675662537
57 changed files with 7625 additions and 883 deletions
+37 -9
View File
@@ -51,9 +51,9 @@ async function getPurchaseById(ctx) {
}
async function createPurchase(ctx) {
const { supplier_id, items, remarks } = ctx.request.body
const { supplier_id, items, remarks } = ctx.request.body || {}
if (!supplier_id || !items || items.length === 0) {
if (!supplier_id || !Array.isArray(items) || items.length === 0) {
ctx.body = { code: 400, message: '请选择供应商和采购商品' }
return
}
@@ -67,19 +67,25 @@ async function createPurchase(ctx) {
let total = 0
for (const item of items) {
total += (item.purchase_price || 0) * (item.quantity || 0)
const qty = parseInt(item.quantity) || 0
const price = parseFloat(item.purchase_price) || 0
if (qty <= 0 || price < 0) {
ctx.body = { code: 400, message: '数量/单价不合法' }
return
}
total += price * qty
}
const result = await transaction(async (conn) => {
const purchaseResult = await conn.execute(
const [purchaseResult] = await conn.execute(
'INSERT INTO purchases (supplier_id, supplier_name, total, remarks) VALUES (?, ?, ?, ?)',
[supplier_id, supplier.name, total, remarks || '']
)
const purchaseId = purchaseResult[0].insertId
const purchaseId = purchaseResult.insertId
for (const item of items) {
const goods = await conn.execute('SELECT name FROM goods WHERE id = ?', [item.goods_id])
const goodsName = goods[0].length > 0 ? goods[0][0].name : ''
const [goods] = await conn.execute('SELECT name FROM goods WHERE id = ?', [item.goods_id])
const goodsName = goods.length > 0 ? goods[0].name : ''
await conn.execute(
'INSERT INTO purchase_items (purchase_id, goods_id, goods_name, quantity, purchase_price) VALUES (?, ?, ?, ?, ?)',
[purchaseId, item.goods_id, goodsName, item.quantity || 0, item.purchase_price || 0]
@@ -96,6 +102,12 @@ async function createPurchase(ctx) {
}
async function inboundPurchase(ctx) {
const operator = ctx.state.user
if (!operator) {
ctx.status = 401
ctx.body = { code: 401, message: '未登录' }
return
}
const id = parseInt(ctx.params.id)
const purchases = await query('SELECT * FROM purchases WHERE id = ?', [id])
@@ -111,11 +123,20 @@ async function inboundPurchase(ctx) {
}
const items = await query('SELECT * FROM purchase_items WHERE purchase_id = ?', [id])
if (items.length === 0) {
ctx.body = { code: 400, message: '采购单无明细' }
return
}
await transaction(async (conn) => {
for (const item of items) {
const existing = await conn.execute('SELECT * FROM stock WHERE goods_id = ?', [item.goods_id])
if (existing[0].length > 0) {
const [goods] = await conn.execute('SELECT id, stock FROM goods WHERE id = ? FOR UPDATE', [item.goods_id])
if (goods.length === 0) {
throw new Error(`商品 ${item.goods_id} 不存在,无法入库`)
}
const [stockRows] = await conn.execute('SELECT quantity FROM stock WHERE goods_id = ? FOR UPDATE', [item.goods_id])
if (stockRows.length > 0) {
await conn.execute(
'UPDATE stock SET quantity = quantity + ? WHERE goods_id = ?',
[item.quantity, item.goods_id]
@@ -130,6 +151,13 @@ async function inboundPurchase(ctx) {
'UPDATE goods SET stock = stock + ? WHERE id = ?',
[item.quantity, item.goods_id]
)
try {
await conn.execute(
'INSERT INTO stock_logs (goods_id, change_type, delta, quantity_after, operator_id, remark) VALUES (?, ?, ?, ?, ?, ?)',
[item.goods_id, 'purchase', item.quantity, (stockRows[0]?.quantity || 0) + item.quantity, operator.id, `采购入库 #${id}`]
)
} catch {}
}
await conn.execute('UPDATE purchases SET status = 1 WHERE id = ?', [id])
})