Commit bf729c8f by wangkangjie

Merge branch 'master' of https://git.rokedata.com/dws/dwsproject

parents 64d5dd66 cdd07d60
...@@ -11,6 +11,7 @@ import os ...@@ -11,6 +11,7 @@ import os
import io import io
from jinja2 import FileSystemLoader, Environment from jinja2 import FileSystemLoader, Environment
import logging import logging
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) BASE_DIR = os.path.dirname(os.path.dirname(__file__))
...@@ -37,32 +38,26 @@ class ProductIncomeExpenseIframe(http.Controller): ...@@ -37,32 +38,26 @@ class ProductIncomeExpenseIframe(http.Controller):
try: try:
# 获取模型环境 # 获取模型环境
model = _self.env(user=SUPERUSER_ID)["roke.product.income.expense"] model = _self.env(user=SUPERUSER_ID)["roke.product.income.expense"]
# 查找最新创建的一条数据(按创建时间降序,取第一条)
latest_record = model.search([], order="business_date desc,create_date desc", limit=1)
if latest_record:
data = data_list[0]
expenditure = data.get("expenditure")
if expenditure in ['', None, False]:
data.update({"expenditure": 0})
income = data.get("income")
if income in ['', None, False]:
data.update({"income": 0})
balance = round(latest_record.balance + float(data.get("income")) - float(data.get("expenditure")), 2)
else:
# 如果没有找到任何记录
balance = round(0 + data_list.get("income") - data_list.get("expenditure"), 2)
for v in data_list: for v in data_list:
data = {
"business_date": v.get("business_date"),
"abstract": v.get("abstract"),
"income": v.get("income"),
"machinery_type": v.get("machinery_type", "其他"),
"expenditure": v.get("expenditure"),
"customer": v.get("customer"),
"balance": balance,
}
if v.get("id"): # 更新 if v.get("id"): # 更新
# 获取当前要更新的记录(旧数据)
current_record = model.browse(int(v.get("id")))
if not current_record:
return {"code": 1, "message": f"更新失败,ID为{v.get('id')}的记录不存在"}
# 提取旧数据(用于还原历史影响)
old_income = float(current_record.income or 0)
old_expenditure = float(current_record.expenditure or 0)
old_diff = old_income - old_expenditure # 旧的收支差额
# 提取更新后的值
new_income = float(v.get("income", 0) or 0)
new_expenditure = float(v.get("expenditure", 0) or 0)
new_diff = new_income - new_expenditure # 新的收支差额
# 计算差额变化
diff_change = new_diff - old_diff # 新旧差额的差值
# 当前的一条数据
latest_record = model.search([('id', '=', int(v.get("id")))], order="business_date desc,create_date desc", limit=1)
balance = round(latest_record.balance + diff_change, 2);
cr.execute(""" cr.execute("""
UPDATE roke_product_income_expense UPDATE roke_product_income_expense
SET business_date = %s, SET business_date = %s,
...@@ -74,23 +69,33 @@ class ProductIncomeExpenseIframe(http.Controller): ...@@ -74,23 +69,33 @@ class ProductIncomeExpenseIframe(http.Controller):
balance = %s balance = %s
WHERE id = %s WHERE id = %s
""", ( """, (
data["business_date"], data["abstract"], data["income"], v.get("business_date"), v.get("abstract"), v.get("income"),
data["machinery_type"], data["expenditure"], data["customer"],data["balance"], v.get("machinery_type"), v.get("expenditure"), v.get("customer"), balance,
v["id"] v.get("id")
)) ))
if cr.rowcount == 0: # 没有找到对应记录 if cr.rowcount == 0: # 没有找到对应记录
return {"code": 1, "message": "更新失败,没找到对应数据。"} return {"code": 1, "message": "更新失败,没找到对应数据。"}
else: # 创建 else: # 创建
# 支出
expenditure = float(v.get("expenditure", 0) or 0)
# 收入
income = float(v.get("income", 0) or 0)
# 最新一条数据
latest_record = model.search([], order="business_date desc,create_date desc", limit=1)
# 计算结余
balance = round(latest_record.balance + (income - expenditure), 2)
cr.execute(""" cr.execute("""
INSERT INTO roke_product_income_expense INSERT INTO roke_product_income_expense
(business_date, abstract, income, machinery_type, expenditure, customer, balance, create_uid, create_date) (business_date, abstract, income, machinery_type, expenditure, customer, balance, create_uid, create_date)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, NOW() - INTERVAL '8 hours') VALUES (%s, %s, %s, %s, %s, %s, %s, %s, NOW() - INTERVAL '8 hours')
""", ( """, (
data["business_date"], data["abstract"], data["income"], v.get("business_date"), v.get("abstract"), v.get("income"),
data["machinery_type"], data["expenditure"], data["customer"], v.get("machinery_type"), v.get("expenditure"), v.get("customer"),
data["balance"], balance,
v.get("user_id", 1) # 默认 user_id 没传的话用 1(管理员) v.get("user_id", 1) # 默认 user_id 没传的话用 1(管理员)
)) ))
_self.env.cr.commit() # 提交事务,防止数据丢失 _self.env.cr.commit() # 提交事务,防止数据丢失
......
...@@ -176,6 +176,7 @@ class ResMesBigScreen(http.Controller): ...@@ -176,6 +176,7 @@ class ResMesBigScreen(http.Controller):
jsonrequest = http.request.jsonrequest jsonrequest = http.request.jsonrequest
domain = [] domain = []
abnormal_alarm_id = jsonrequest.get('abnormal_alarm_id', False) abnormal_alarm_id = jsonrequest.get('abnormal_alarm_id', False)
plant_id = http.request.jsonrequest.get('plant_id') #车间ID参数
#表单id #表单id
if abnormal_alarm_id: if abnormal_alarm_id:
domain.append(('id', '=', abnormal_alarm_id)) domain.append(('id', '=', abnormal_alarm_id))
...@@ -183,6 +184,9 @@ class ResMesBigScreen(http.Controller): ...@@ -183,6 +184,9 @@ class ResMesBigScreen(http.Controller):
#异常类型id #异常类型id
if abnormal_id: if abnormal_id:
domain.append(('abnormal_id', '=', abnormal_id)) domain.append(('abnormal_id', '=', abnormal_id))
# 车间 id
if plant_id:
domain.append(('equipment_id.plant_id', '=', plant_id))
page_size = int(http.request.jsonrequest.get('page_size', 20)) page_size = int(http.request.jsonrequest.get('page_size', 20))
page_no = int(http.request.jsonrequest.get('page_no', 1)) page_no = int(http.request.jsonrequest.get('page_no', 1))
abnormal_alarm_ids = http.request.env(user=SUPERUSER_ID)['roke.abnormal.alarm'].search(domain, limit=page_size, offset=(page_no - 1) * page_size, order="originating_time desc") abnormal_alarm_ids = http.request.env(user=SUPERUSER_ID)['roke.abnormal.alarm'].search(domain, limit=page_size, offset=(page_no - 1) * page_size, order="originating_time desc")
...@@ -212,7 +216,11 @@ class ResMesBigScreen(http.Controller): ...@@ -212,7 +216,11 @@ class ResMesBigScreen(http.Controller):
try: try:
url = "https://dws-platform.xbg.rokeris.com/dev-api/public/device_efficiency/series_utilization_rate_top_5" url = "https://dws-platform.xbg.rokeris.com/dev-api/public/device_efficiency/series_utilization_rate_top_5"
date = fields.Date.today().strftime("%Y-%m-%d") date = fields.Date.today().strftime("%Y-%m-%d")
eq_ids = http.request.env["roke.mes.equipment"].sudo().search([('code',"!=",'')]) plant_id = http.request.jsonrequest.get('plant_id') #车间ID参数
domain = [('code', "!=", '')]
if plant_id:
domain.append(('plant_id', '=', plant_id))
eq_ids = http.request.env["roke.mes.equipment"].sudo().search(domain)
query_data = [{"device_name": eq_id.name,"device_code": eq_id.code} for eq_id in eq_ids] query_data = [{"device_name": eq_id.name,"device_code": eq_id.code} for eq_id in eq_ids]
res = requests.post(url, data=json.dumps({ res = requests.post(url, data=json.dumps({
"device_code_list": query_data, "device_code_list": query_data,
......
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