Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
dwsproject
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
dws
dwsproject
Commits
d42fb772
Commit
d42fb772
authored
Sep 25, 2025
by
malihua
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
收支表保存提速
parent
d0c23ebe
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
16 deletions
+59
-16
roke_product_income_expense/controller/main.py
+59
-16
No files found.
roke_product_income_expense/controller/main.py
View file @
d42fb772
...
...
@@ -2,8 +2,11 @@ import datetime
import
pandas
as
pd
import
xlsxwriter
import
psycopg2
# Odoo 依赖 psycopg2 处理 PostgreSQL 交互
from
psycopg2
import
sql
# 用于安全拼接 SQL(防止 SQL 注入)
from
odoo
import
http
,
tools
from
odoo.http
import
content_disposition
,
request
from
odoo
import
models
,
fields
,
api
,
SUPERUSER_ID
,
_
import
os
import
io
from
jinja2
import
FileSystemLoader
,
Environment
...
...
@@ -29,24 +32,64 @@ class ProductIncomeExpenseIframe(http.Controller):
@http.route
(
"/roke/product/product_income_expense/create"
,
type
=
"json"
,
auth
=
'none'
,
cors
=
'*'
,
csrf
=
False
)
def
product_income_expense_create
(
self
):
_self
=
http
.
request
cr
=
_self
.
env
.
cr
# 获取 cursor
data_list
=
_self
.
jsonrequest
.
get
(
"data_list"
,
[])
for
v
in
data_list
:
data
=
{
"business_date"
:
v
.
get
(
"business_date"
,
False
),
"abstract"
:
v
.
get
(
"abstract"
,
False
),
"income"
:
v
.
get
(
"income"
,
False
),
"machinery_type"
:
v
.
get
(
"machinery_type"
,
"其他"
),
"expenditure"
:
v
.
get
(
"expenditure"
,
False
),
"customer"
:
v
.
get
(
"customer"
,
False
)
}
# 看是否有id,如果有的话就说明是更新,没有的话就说明是创建
if
v
.
get
(
"id"
,
False
):
expense_obj
=
_self
.
env
[
"roke.product.income.expense"
]
.
sudo
()
.
search
([(
"id"
,
"="
,
v
.
get
(
"id"
))])
if
not
expense_obj
:
return
{
"code"
:
1
,
"message"
:
"更新失败,没找到对应数据。"
}
expense_obj
.
write
(
data
)
try
:
# 获取模型环境
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
]
balance
=
round
(
latest_record
.
balance
+
float
(
data
.
get
(
"income"
))
-
float
(
data
.
get
(
"expenditure"
)),
2
)
else
:
_self
.
env
(
user
=
v
.
get
(
"user_id"
))[
"roke.product.income.expense"
]
.
create
(
data
)
# 如果没有找到任何记录
balance
=
round
(
0
+
data_list
.
get
(
"income"
)
-
data_list
.
get
(
"expenditure"
),
2
)
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"
):
# 更新
cr
.
execute
(
"""
UPDATE roke_product_income_expense
SET business_date =
%
s,
abstract =
%
s,
income =
%
s,
machinery_type =
%
s,
expenditure =
%
s,
customer =
%
s,
balance =
%
s,
WHERE id =
%
s
"""
,
(
data
[
"business_date"
],
data
[
"abstract"
],
data
[
"income"
],
data
[
"machinery_type"
],
data
[
"expenditure"
],
data
[
"customer"
],
data
[
"balance"
],
v
[
"id"
]
))
if
cr
.
rowcount
==
0
:
# 没有找到对应记录
return
{
"code"
:
1
,
"message"
:
"更新失败,没找到对应数据。"
}
else
:
# 创建
cr
.
execute
(
"""
INSERT INTO roke_product_income_expense
(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')
"""
,
(
data
[
"business_date"
],
data
[
"abstract"
],
data
[
"income"
],
data
[
"machinery_type"
],
data
[
"expenditure"
],
data
[
"customer"
],
data
[
"balance"
],
v
.
get
(
"user_id"
,
1
)
# 默认 user_id 没传的话用 1(管理员)
))
_self
.
env
.
cr
.
commit
()
# 提交事务,防止数据丢失
except
Exception
as
e
:
return
{
"code"
:
1
,
"message"
:
f
"操作失败:{str(e)}"
}
return
{
"code"
:
0
,
"message"
:
"操作成功!"
}
@http.route
(
"/roke/product/product_income_expense/get"
,
type
=
"json"
,
auth
=
'none'
,
cors
=
'*'
,
csrf
=
False
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment