96 lines
4.6 KiB
Python
96 lines
4.6 KiB
Python
#!/usr/bin/env python3
|
|
"""按 dashboard.py 同款逻辑验证卡片3/4成本计算是否使用最新库存日期 + 特殊成本价"""
|
|
import sys, os
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
from config import DB_CONFIG
|
|
import pymysql
|
|
from services.inventory_calc import resolve_cost_price
|
|
|
|
DB = dict(DB_CONFIG)
|
|
DB['cursorclass'] = pymysql.cursors.DictCursor
|
|
conn = pymysql.connect(**DB)
|
|
cur = conn.cursor()
|
|
|
|
# 1. 最新库存日期
|
|
cur.execute("SELECT MAX(data_date) AS d FROM inventory_detail")
|
|
latest_date = cur.fetchone()['d']
|
|
print("最新库存日期:", latest_date)
|
|
assert str(latest_date)[:10] == '2026-08-06', f"最新日期不是2026-08-06: {latest_date}"
|
|
|
|
# 2. 特殊成本价在 resolve_cost_price 中生效检查
|
|
codes = ['POZ-FM-0184-TM-SM', 'POZ-XFS-9014-TM-MD', 'POZ-FM-9742-TM-MD', 'POZ-MM-0580-TM-MD',
|
|
'POZ-SKS-3374-LD', 'POZ-SKS-3367-LD', 'POZ-SKS-3350-LD', 'POZ-SKS-3343-LD']
|
|
for c in codes:
|
|
print(f" {c} -> resolve_cost_price = {resolve_cost_price(c, 999)}")
|
|
|
|
# 3. 按 dashboard 逻辑计算卡片3(总成本)/卡片4(成品成本)
|
|
cur.execute("""
|
|
SELECT od.product_code, ANY_VALUE(od.cost_price) AS pr
|
|
FROM (
|
|
SELECT product_code, MAX(data_date) AS max_date
|
|
FROM inventory_detail
|
|
GROUP BY product_code
|
|
) latest
|
|
JOIN inventory_detail od ON od.product_code = latest.product_code AND od.data_date = latest.max_date
|
|
GROUP BY od.product_code
|
|
""")
|
|
_cost_map = {r['product_code']: resolve_cost_price(r['product_code'], float(r['pr']) if r['pr'] else 0) for r in cur.fetchall()}
|
|
|
|
cur.execute("""
|
|
SELECT id.product_code,
|
|
SUM(id.available_qty) AS available_qty,
|
|
SUM(id.production_in_transit) AS production_in_transit,
|
|
SUM(id.purchase_in_transit) AS purchase_in_transit,
|
|
CASE WHEN p.product_type = '成品' AND p.deleted_at IS NULL THEN 1 ELSE 0 END AS is_finished,
|
|
CASE WHEN p.product_type = '包材' AND p.deleted_at IS NULL
|
|
AND p.product_name NOT LIKE %s THEN 1 ELSE 0 END AS is_packaging
|
|
FROM inventory_detail id
|
|
JOIN products p ON id.product_code = p.product_code
|
|
WHERE id.data_date = %s
|
|
AND ((p.product_type = '成品' AND p.deleted_at IS NULL)
|
|
OR (p.product_type = '包材' AND p.product_name NOT LIKE %s AND p.deleted_at IS NULL))
|
|
GROUP BY id.product_code
|
|
""", ('%内料%', latest_date, '%内料%'))
|
|
rows = cur.fetchall()
|
|
|
|
cur.execute("""
|
|
SELECT b.product_code,
|
|
SUM(COALESCE(b.quantity, 0) * COALESCE(m.cost_price, 0)) AS inner_cost
|
|
FROM bom b
|
|
LEFT JOIN products m ON m.product_code = b.material_code
|
|
WHERE (b.material_name LIKE %s OR b.material_code LIKE %s)
|
|
GROUP BY b.product_code
|
|
""", ('%内料%', '%-NL'))
|
|
_inner_cost_map = {r['product_code']: float(r['inner_cost'] or 0) for r in cur.fetchall()}
|
|
|
|
finished_inventory_cost = packaging_inventory_cost = 0.0
|
|
finished_production_transit = finished_purchase_transit = packaging_purchase_transit = 0.0
|
|
for r in rows:
|
|
price = _cost_map.get(r['product_code'], 0)
|
|
if r['is_finished']:
|
|
finished_inventory_cost += float(r['available_qty'] or 0) * price
|
|
finished_production_transit += float(r['production_in_transit'] or 0) * _inner_cost_map.get(r['product_code'], 0)
|
|
finished_purchase_transit += float(r['purchase_in_transit'] or 0) * price
|
|
elif r['is_packaging']:
|
|
packaging_inventory_cost += float(r['available_qty'] or 0) * price
|
|
packaging_purchase_transit += float(r['purchase_in_transit'] or 0) * price
|
|
|
|
finished_inventory_cost = round(finished_inventory_cost, 2)
|
|
finished_production_transit = round(finished_production_transit, 2)
|
|
finished_purchase_transit = round(finished_purchase_transit, 2)
|
|
packaging_inventory_cost = round(packaging_inventory_cost, 2)
|
|
packaging_purchase_transit = round(packaging_purchase_transit, 2)
|
|
finished_in_transit_cost = round(finished_production_transit + finished_purchase_transit, 2)
|
|
finished_cost = round(finished_inventory_cost + finished_in_transit_cost, 2)
|
|
packaging_in_transit_cost = round(packaging_purchase_transit, 2)
|
|
packaging_cost = round(packaging_inventory_cost + packaging_in_transit_cost, 2)
|
|
total_cost = round(finished_cost + packaging_cost, 2)
|
|
|
|
print("\n=== 卡片3(总成本) / 卡片4(成品成本) ===")
|
|
print("成品库存成本:", finished_inventory_cost)
|
|
print("成品在途成本:", finished_in_transit_cost, "(生产在途", finished_production_transit, "+ 采购在途", finished_purchase_transit, ")")
|
|
print("成品成本(卡片4):", finished_cost)
|
|
print("包材成本:", packaging_cost, "(库存", packaging_inventory_cost, "+ 在途", packaging_in_transit_cost, ")")
|
|
print("总成本(卡片3):", total_cost)
|
|
conn.close()
|