修改状态为已收货时更新期初库存
This commit is contained in:
parent
2f87fda0de
commit
ac9c4d9b94
110
server/app.py
110
server/app.py
@ -6605,39 +6605,6 @@ def batch_update_purchase_demand_status():
|
||||
|
||||
existing_ids = [row['id'] for row in existing_records]
|
||||
|
||||
# 如果状态从"已下单"改为其他状态,恢复期初库存
|
||||
if status != 'ordered':
|
||||
for record in existing_records:
|
||||
if record['status'] == 'ordered':
|
||||
material_code = record['material_code']
|
||||
material_name = record['material_name']
|
||||
actual_purchase_qty = record['actual_purchase_qty']
|
||||
net_demand = record['net_demand']
|
||||
total_demand = record['total_demand']
|
||||
initial_stock = record['initial_stock']
|
||||
|
||||
# 根据实际采购数量计算之前的变化量
|
||||
if actual_purchase_qty > 0:
|
||||
# 如果实际采购不等于零,之前增加的库存量 = 实际采购 - 净需求
|
||||
added_stock = actual_purchase_qty - net_demand
|
||||
else:
|
||||
# 如果实际采购等于零,之前减少的库存量 = 期初库存 - 总需求
|
||||
added_stock = -(initial_stock - total_demand)
|
||||
|
||||
# 从期初库存表中减去之前的变化量(恢复原值)
|
||||
c.execute('SELECT stock_qty FROM initial_stock WHERE material_code=?', (material_code,))
|
||||
stock_record = c.fetchone()
|
||||
|
||||
if stock_record:
|
||||
current_stock = stock_record['stock_qty']
|
||||
restored_stock = current_stock - added_stock
|
||||
|
||||
# 只更新期初库存表
|
||||
c.execute('''UPDATE initial_stock
|
||||
SET stock_qty = ?, updated_at = ?
|
||||
WHERE material_code = ?''',
|
||||
(restored_stock, now, material_code))
|
||||
|
||||
# 构建更新语句
|
||||
updates = ['status=?', 'updated_at=?']
|
||||
params = [status, now]
|
||||
@ -6647,12 +6614,13 @@ def batch_update_purchase_demand_status():
|
||||
params.append(remark)
|
||||
|
||||
params.extend(existing_ids)
|
||||
placeholders = ','.join(['?' for _ in existing_ids])
|
||||
c.execute(f'UPDATE purchase_demand SET {", ".join(updates)} WHERE id IN ({placeholders})', params)
|
||||
|
||||
# 如果状态更新为已下单,更新期初库存(包括已经是已下单状态的情况)
|
||||
if status == 'ordered':
|
||||
# 如果状态更新为已收货,更新期初库存(包括已经是已收货状态的情况)
|
||||
if status == 'received':
|
||||
for record in existing_records:
|
||||
# 处理所有记录(包括已经是"已下单"状态的记录)
|
||||
# 处理所有记录(包括已经是"已收货"状态的记录)
|
||||
material_code = record['material_code']
|
||||
material_name = record['material_name']
|
||||
actual_purchase_qty = record['actual_purchase_qty']
|
||||
@ -6668,11 +6636,18 @@ def batch_update_purchase_demand_status():
|
||||
# 如果实际采购等于零,期初库存 = 期初库存 - 总需求
|
||||
new_initial_stock = initial_stock - total_demand
|
||||
|
||||
# 只更新期初库存表
|
||||
c.execute('''INSERT OR REPLACE INTO initial_stock
|
||||
(material_code, material_name, stock_qty, updated_at)
|
||||
VALUES (?, ?, ?, ?)''',
|
||||
(material_code, material_name, new_initial_stock, now))
|
||||
# 先尝试更新期初库存表,如果不存在则插入
|
||||
c.execute('''UPDATE initial_stock
|
||||
SET stock_qty = ?, material_name = ?, updated_at = ?
|
||||
WHERE material_code = ?''',
|
||||
(new_initial_stock, material_name, now, material_code))
|
||||
|
||||
# 如果没有更新任何记录,说明记录不存在,需要插入
|
||||
if c.rowcount == 0:
|
||||
c.execute('''INSERT INTO initial_stock
|
||||
(material_code, material_name, stock_qty, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?)''',
|
||||
(material_code, material_name, new_initial_stock, now, now))
|
||||
|
||||
count = c.rowcount
|
||||
conn.commit()
|
||||
@ -6954,43 +6929,12 @@ def update_purchase_demand(id):
|
||||
old_status = existing['status']
|
||||
new_status = data.get('status', old_status)
|
||||
|
||||
# 如果状态从"已下单"改为其他状态,恢复期初库存
|
||||
if 'status' in data and old_status == 'ordered' and new_status != 'ordered':
|
||||
material_code = existing['material_code']
|
||||
material_name = existing['material_name']
|
||||
actual_purchase_qty = existing['actual_purchase_qty']
|
||||
net_demand = existing['net_demand']
|
||||
total_demand = existing['total_demand']
|
||||
initial_stock = existing['initial_stock']
|
||||
|
||||
# 根据实际采购数量计算之前的变化量
|
||||
if actual_purchase_qty > 0:
|
||||
# 如果实际采购不等于零,之前增加的库存量 = 实际采购 - 净需求
|
||||
added_stock = actual_purchase_qty - net_demand
|
||||
else:
|
||||
# 如果实际采购等于零,之前减少的库存量 = 期初库存 - 总需求
|
||||
added_stock = -(initial_stock - total_demand)
|
||||
|
||||
# 从期初库存表中减去之前的变化量(恢复原值)
|
||||
c.execute('SELECT stock_qty FROM initial_stock WHERE material_code=?', (material_code,))
|
||||
stock_record = c.fetchone()
|
||||
|
||||
if stock_record:
|
||||
current_stock = stock_record['stock_qty']
|
||||
restored_stock = current_stock - added_stock
|
||||
|
||||
# 只更新期初库存表
|
||||
c.execute('''UPDATE initial_stock
|
||||
SET stock_qty = ?, updated_at = ?
|
||||
WHERE material_code = ?''',
|
||||
(restored_stock, get_beijing_time(), material_code))
|
||||
|
||||
# 执行更新
|
||||
update_values.append(id)
|
||||
c.execute(f'UPDATE purchase_demand SET {", ".join(update_fields)} WHERE id=?', update_values)
|
||||
|
||||
# 如果状态更新为已下单,更新期初库存(包括已经是已下单状态的情况)
|
||||
if 'status' in data and new_status == 'ordered':
|
||||
# 如果状态更新为已收货,更新期初库存(包括已经是已收货状态的情况)
|
||||
if 'status' in data and new_status == 'received':
|
||||
# 获取更新后的记录信息
|
||||
c.execute('SELECT material_code, material_name, actual_purchase_qty, net_demand, total_demand, initial_stock FROM purchase_demand WHERE id=?', (id,))
|
||||
record = c.fetchone()
|
||||
@ -7006,11 +6950,19 @@ def update_purchase_demand(id):
|
||||
# 如果实际采购等于零,期初库存 = 期初库存 - 总需求
|
||||
new_initial_stock = initial_stock - total_demand
|
||||
|
||||
# 只更新期初库存表
|
||||
c.execute('''INSERT OR REPLACE INTO initial_stock
|
||||
(material_code, material_name, stock_qty, updated_at)
|
||||
VALUES (?, ?, ?, ?)''',
|
||||
(material_code, material_name, new_initial_stock, get_beijing_time()))
|
||||
# 先尝试更新期初库存表,如果不存在则插入
|
||||
now_time = get_beijing_time()
|
||||
c.execute('''UPDATE initial_stock
|
||||
SET stock_qty = ?, material_name = ?, updated_at = ?
|
||||
WHERE material_code = ?''',
|
||||
(new_initial_stock, material_name, now_time, material_code))
|
||||
|
||||
# 如果没有更新任何记录,说明记录不存在,需要插入
|
||||
if c.rowcount == 0:
|
||||
c.execute('''INSERT INTO initial_stock
|
||||
(material_code, material_name, stock_qty, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?)''',
|
||||
(material_code, material_name, new_initial_stock, now_time, now_time))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user