71 lines
2.1 KiB
Plaintext
71 lines
2.1 KiB
Plaintext
|
|
ns = params_kw.copy()
|
|
record_id = ns.get('id', '')
|
|
remark = ns.get('remark', '')
|
|
payment_method = ns.get('payment_method', '')
|
|
|
|
if not record_id:
|
|
return json.dumps({'success': False, 'error': '缺少记录ID'}, ensure_ascii=False)
|
|
|
|
sc_dbname = get_module_dbname('supplychain')
|
|
acc_dbname = get_module_dbname('accounting')
|
|
userorgid = await get_userorgid()
|
|
|
|
# 查询supplychain_accounting记录
|
|
db = DBPools()
|
|
async with db.sqlorContext(sc_dbname) as sor:
|
|
recs = await sor.sqlExe("""
|
|
SELECT sa.*, sp.supplier_name
|
|
FROM supplychain_accounting sa
|
|
LEFT JOIN suppliers sp ON sa.supplier_id = sp.id COLLATE utf8mb4_unicode_ci
|
|
WHERE sa.id = ${record_id}$
|
|
""", {'record_id': record_id})
|
|
|
|
if not recs:
|
|
return json.dumps({'success': False, 'error': '记录不存在'}, ensure_ascii=False)
|
|
|
|
rec = recs[0]
|
|
|
|
# 供应商结算: supply_amount是我方应付给供应商的金额
|
|
from accounting.settle import SettleAccounting
|
|
settle_log = {
|
|
'accounting_orgid': userorgid,
|
|
'providerid': rec.supplier_id or '',
|
|
'sale_mode': '1',
|
|
'settle_date': str(rec.sale_date or curDateString()),
|
|
'settle_amt': float(rec.supply_amount or 0),
|
|
'business_op': '供应商结算',
|
|
'remark': remark
|
|
}
|
|
|
|
try:
|
|
async with db.sqlorContext(acc_dbname) as acc_sor:
|
|
sa = SettleAccounting(settle_log)
|
|
await sa.accounting(acc_sor)
|
|
|
|
# 更新sales_ledger
|
|
async with db.sqlorContext(sc_dbname) as sc_sor:
|
|
await sc_sor.sqlExe("""
|
|
UPDATE sales_ledger
|
|
SET settlement_status = '1', updated_at = NOW()
|
|
WHERE productid = ${productid}$
|
|
AND supplier_id = ${supplier_id}$
|
|
AND sale_date = ${sale_date}$
|
|
""", {
|
|
'productid': rec.productid,
|
|
'supplier_id': rec.supplier_id,
|
|
'sale_date': str(rec.sale_date)
|
|
})
|
|
|
|
debug(f'provider_settlement: settled {record_id}, amt={rec.supply_amount}')
|
|
return json.dumps({
|
|
'success': True,
|
|
'message': f'结算成功,金额: {rec.supply_amount}',
|
|
'settle_id': sa.settleid,
|
|
'bill_id': sa.billid
|
|
}, ensure_ascii=False, default=str)
|
|
|
|
except Exception as e:
|
|
debug(f'provider_settlement error: {format_exc()}')
|
|
return json.dumps({'success': False, 'error': f'结算失败: {str(e)}'}, ensure_ascii=False)
|