Commit 9b8a20a0 by malihua

收入支出表接口换成SQL

parent 85b49815
......@@ -52,43 +52,78 @@ class ProductIncomeExpenseIframe(http.Controller):
@http.route("/roke/product/product_income_expense/get", type="json", auth='none', cors='*', csrf=False)
def product_income_expense_get_list(self):
_self = http.request
limit = _self.jsonrequest.get("limit", 20)
page = _self.jsonrequest.get("page", 1)
limit = int(_self.jsonrequest.get("limit", 20))
page = int(_self.jsonrequest.get("page", 1))
start_date = _self.jsonrequest.get("start_date", "")
end_date = _self.jsonrequest.get("end_date", "")
type_str = _self.jsonrequest.get("type_str", False) # income收入/expenditure支出
machinery_type = _self.jsonrequest.get("machinery_type", False)
customer = _self.jsonrequest.get("customer", False)
abstract = _self.jsonrequest.get("abstract", False)
domain = []
# 拼接 SQL 条件
where_clauses = ["1=1"]
params = []
if start_date and end_date:
domain.append(("business_date", ">=", start_date))
domain.append(("business_date", "<=", end_date))
if type_str:
domain.append((type_str, ">", 0))
where_clauses.append("business_date >= %s")
where_clauses.append("business_date <= %s")
params.extend([start_date, end_date])
if type_str: # income 或 expenditure
where_clauses.append(f"{type_str} > 0")
if machinery_type:
domain.append(("machinery_type", "=", machinery_type))
where_clauses.append("machinery_type = %s")
params.append(machinery_type)
if customer:
domain.append(("customer", "ilike", customer))
where_clauses.append("customer ILIKE %s")
params.append("%%%s%%" % customer)
if abstract:
domain.append(("abstract", "ilike", abstract))
data_list = _self.env["roke.product.income.expense"].sudo().search(domain, limit=limit,
offset=(page - 1) * limit,
order="business_date desc, create_date desc")
count = _self.env["roke.product.income.expense"].sudo().search_count(domain)
where_clauses.append("abstract ILIKE %s")
params.append("%%%s%%" % abstract)
where_sql = " AND ".join(where_clauses)
# 查询数据
offset = (page - 1) * limit
sql = f"""
SELECT id, business_date, abstract, customer, income, expenditure, balance, machinery_type, create_uid, create_date
FROM roke_product_income_expense
WHERE {where_sql}
ORDER BY business_date DESC, create_date DESC
LIMIT %s OFFSET %s
"""
params.extend([limit, offset])
_self.env.cr.execute(sql, params)
rows = _self.env.cr.dictfetchall()
# 查询总数
sql_count = f"SELECT COUNT(*) FROM roke_product_income_expense WHERE {where_sql}"
_self.env.cr.execute(sql_count, params[:-2]) # 去掉 limit 和 offset
count = _self.env.cr.fetchone()[0]
data = []
for v in data_list:
for v in rows:
business_date = v["business_date"].strftime('%Y-%m-%d') if v["business_date"] else ""
create_date = (v["create_date"] + datetime.timedelta(hours=8)).strftime('%Y-%m-%d %H:%M') if v[
"create_date"] else ""
user_name = _self.env['res.users'].sudo().browse(v["create_uid"]).name or ""
data.append({
"id": v.id,
"business_date": v.business_date and v.business_date.strftime('%Y-%m-%d'),
"abstract": v.abstract or "",
"customer": v.customer or "",
"income": round(v.income, 2) or 0,
"machinery_type": v.machinery_type or "其他",
"expenditure": round(v.expenditure) or 0,
"balance": round(v.balance, 2) or 0,
"user_name": v.create_uid.name or "",
"create_date": (v.create_date + datetime.timedelta(hours=8)).strftime('%Y-%m-%d %H:%M'),
"id": v["id"],
"business_date": business_date,
"abstract": v["abstract"] or "",
"customer": v["customer"] or "",
"income": round(v["income"] or 0, 2),
"machinery_type": v["machinery_type"] or "其他",
"expenditure": round(v["expenditure"] or 0, 2),
"balance": round(v["balance"] or 0, 2),
"user_name": user_name,
"create_date": create_date,
})
return {"code": 0, "message": "获取成功!", "data": data, "count": count}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment