Ai config
This commit is contained in:
+39
-35
@@ -1,4 +1,5 @@
|
||||
const { query } = require('../config/database')
|
||||
const { query, transaction } = require('../config/database')
|
||||
const { paginate } = require('../utils/pagination')
|
||||
|
||||
async function getPurchases(ctx) {
|
||||
let sql = `
|
||||
@@ -14,11 +15,11 @@ async function getPurchases(ctx) {
|
||||
}
|
||||
|
||||
sql += ' ORDER BY p.created_at DESC'
|
||||
const purchases = await query(sql, params)
|
||||
const result = await paginate(query, sql, params, ctx.query.page, ctx.query.pageSize)
|
||||
|
||||
ctx.body = {
|
||||
code: 200,
|
||||
data: purchases
|
||||
...result
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,26 +70,28 @@ async function createPurchase(ctx) {
|
||||
total += (item.purchase_price || 0) * (item.quantity || 0)
|
||||
}
|
||||
|
||||
const purchaseResult = await query(
|
||||
'INSERT INTO purchases (supplier_id, supplier_name, total, remarks) VALUES (?, ?, ?, ?)',
|
||||
[supplier_id, supplier.name, total, remarks || '']
|
||||
)
|
||||
|
||||
const purchaseId = purchaseResult.insertId
|
||||
|
||||
for (const item of items) {
|
||||
const goods = await query('SELECT name FROM goods WHERE id = ?', [item.goods_id])
|
||||
const goodsName = goods.length > 0 ? goods[0].name : ''
|
||||
await query(
|
||||
'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]
|
||||
const result = await transaction(async (conn) => {
|
||||
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
|
||||
|
||||
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 : ''
|
||||
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]
|
||||
)
|
||||
}
|
||||
return purchaseId
|
||||
})
|
||||
|
||||
ctx.body = {
|
||||
code: 200,
|
||||
message: '采购单创建成功',
|
||||
data: { id: purchaseId }
|
||||
data: { id: result }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,26 +112,27 @@ async function inboundPurchase(ctx) {
|
||||
|
||||
const items = await query('SELECT * FROM purchase_items WHERE purchase_id = ?', [id])
|
||||
|
||||
for (const item of items) {
|
||||
const existing = await query('SELECT * FROM stock WHERE goods_id = ?', [item.goods_id])
|
||||
if (existing.length > 0) {
|
||||
await query(
|
||||
'UPDATE stock SET quantity = quantity + ? WHERE goods_id = ?',
|
||||
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) {
|
||||
await conn.execute(
|
||||
'UPDATE stock SET quantity = quantity + ? WHERE goods_id = ?',
|
||||
[item.quantity, item.goods_id]
|
||||
)
|
||||
} else {
|
||||
await conn.execute(
|
||||
'INSERT INTO stock (goods_id, quantity, warehouse) VALUES (?, ?, ?)',
|
||||
[item.goods_id, item.quantity, '默认仓库']
|
||||
)
|
||||
}
|
||||
await conn.execute(
|
||||
'UPDATE goods SET stock = stock + ? WHERE id = ?',
|
||||
[item.quantity, item.goods_id]
|
||||
)
|
||||
} else {
|
||||
await query(
|
||||
'INSERT INTO stock (goods_id, quantity, warehouse) VALUES (?, ?, ?)',
|
||||
[item.goods_id, item.quantity, '默认仓库']
|
||||
)
|
||||
}
|
||||
await query(
|
||||
'UPDATE goods SET stock = stock + ? WHERE id = ?',
|
||||
[item.quantity, item.goods_id]
|
||||
)
|
||||
}
|
||||
|
||||
await query('UPDATE purchases SET status = 1 WHERE id = ?', [id])
|
||||
await conn.execute('UPDATE purchases SET status = 1 WHERE id = ?', [id])
|
||||
})
|
||||
|
||||
ctx.body = {
|
||||
code: 200,
|
||||
|
||||
Reference in New Issue
Block a user