#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Receivable create API""" import json, uuid, time result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid request', 'type': 'error'}} try: contract_id = params_kw.get('contract_id', '').strip() customer_id = params_kw.get('customer_id', '').strip() receivable_amount = params_kw.get('receivable_amount', '0').strip() due_date = params_kw.get('due_date', '').strip() if not due_date: result['options'] = {'title': 'Error', 'message': '到期日不能为空', 'type': 'error'} else: dbname = get_module_dbname('financial_management') new_id = str(uuid.uuid4()).replace('-', '') now = time.strftime('%Y-%m-%d %H:%M:%S') async with DBPools().sqlorContext(dbname) as sor: await sor.sqlExe("""INSERT INTO receivables (id, order_id, contract_id, customer_id, receivable_amount, received_amount, due_date, status, description, org_id, created_at, updated_at) VALUES (${id}$, ${order_id}$, ${contract_id}$, ${customer_id}$, ${receivable_amount}$, ${received_amount}$, ${due_date}$, ${status}$, ${description}$, ${org_id}$, ${created_at}$, ${updated_at}$)""", { 'id': new_id, 'order_id': params_kw.get('order_id', '').strip(), 'contract_id': contract_id, 'customer_id': customer_id, 'receivable_amount': float(receivable_amount) if receivable_amount else 0, 'received_amount': 0, 'due_date': due_date, 'status': params_kw.get('status', 'pending').strip(), 'description': params_kw.get('description', '').strip(), 'org_id': params_kw.get('org_id', '').strip(), 'created_at': now, 'updated_at': now }) result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '应收账款创建成功', 'type': 'success'}} except Exception as e: result['options'] = {'title': 'Error', 'message': f'创建失败: {str(e)}', 'type': 'error'} return json.dumps(result, ensure_ascii=False)