#!/usr/bin/env python3
"""10 下单：付款即开票。先预检（dry_run=true，不发支付宝），再真实下单。
用法：python3 10_order_create.py [--goods cabbage|corn]"""
import sys
from kl_agri import *

goods = "corn" if "corn" in sys.argv else "cabbage"
g = GOODS[goods]
body = {"tax_no": TAX_NO, "company_clerk_id": state_get("company_clerk_id"), "company_supplier_id": state_get("supplier_id"),
        "outer_order_id": new_no("PO"), "memo": "农产品收购",
        "order_item_list": [{"item_name": g["item_name"], "tax_code": g["tax_code"], "item_unit": g["item_unit"],
                             "item_num": "100", "item_unit_amount": "0.80"}]}
pre = dict(body, dry_run=True)
run("10_order_create--dryrun", "order/create", pre, notes=["预检：参数按官方文档校验通过，返回将要发给支付宝的报文，不下单、不扣费。"])
name = "10_order_create" + ("" if goods == "cabbage" else "--corn")
resp, ok_ = run(name, "order/create", body, notes=["测试环境不扣服务费（fee=0.00）；正式环境按订单金额 × 费率受理成功才扣。"])
if ok_:
    d = resp["data"]
    state_set(order_id=d["order_id"], outer_order_id=body["outer_order_id"])
    print("下一步：", d["next_action"]["hint"], d["next_action"]["url"])
