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
c7b10f38
Commit
c7b10f38
authored
Sep 25, 2025
by
马丽华
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'mlh_jzjx_project' into 'master'
收支表保存提速 See merge request
!29
parents
9802149c
d42fb772
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
12 deletions
+55
-12
roke_product_income_expense/controller/main.py
+55
-12
No files found.
roke_product_income_expense/controller/main.py
View file @
c7b10f38
...
@@ -2,8 +2,11 @@ import datetime
...
@@ -2,8 +2,11 @@ import datetime
import
pandas
as
pd
import
pandas
as
pd
import
xlsxwriter
import
xlsxwriter
import
psycopg2
# Odoo 依赖 psycopg2 处理 PostgreSQL 交互
from
psycopg2
import
sql
# 用于安全拼接 SQL(防止 SQL 注入)
from
odoo
import
http
,
tools
from
odoo
import
http
,
tools
from
odoo.http
import
content_disposition
,
request
from
odoo.http
import
content_disposition
,
request
from
odoo
import
models
,
fields
,
api
,
SUPERUSER_ID
,
_
import
os
import
os
import
io
import
io
from
jinja2
import
FileSystemLoader
,
Environment
from
jinja2
import
FileSystemLoader
,
Environment
...
@@ -29,24 +32,64 @@ class ProductIncomeExpenseIframe(http.Controller):
...
@@ -29,24 +32,64 @@ class ProductIncomeExpenseIframe(http.Controller):
@http.route
(
"/roke/product/product_income_expense/create"
,
type
=
"json"
,
auth
=
'none'
,
cors
=
'*'
,
csrf
=
False
)
@http.route
(
"/roke/product/product_income_expense/create"
,
type
=
"json"
,
auth
=
'none'
,
cors
=
'*'
,
csrf
=
False
)
def
product_income_expense_create
(
self
):
def
product_income_expense_create
(
self
):
_self
=
http
.
request
_self
=
http
.
request
cr
=
_self
.
env
.
cr
# 获取 cursor
data_list
=
_self
.
jsonrequest
.
get
(
"data_list"
,
[])
data_list
=
_self
.
jsonrequest
.
get
(
"data_list"
,
[])
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
:
# 如果没有找到任何记录
balance
=
round
(
0
+
data_list
.
get
(
"income"
)
-
data_list
.
get
(
"expenditure"
),
2
)
for
v
in
data_list
:
for
v
in
data_list
:
data
=
{
data
=
{
"business_date"
:
v
.
get
(
"business_date"
,
False
),
"business_date"
:
v
.
get
(
"business_date"
),
"abstract"
:
v
.
get
(
"abstract"
,
False
),
"abstract"
:
v
.
get
(
"abstract"
),
"income"
:
v
.
get
(
"income"
,
False
),
"income"
:
v
.
get
(
"income"
),
"machinery_type"
:
v
.
get
(
"machinery_type"
,
"其他"
),
"machinery_type"
:
v
.
get
(
"machinery_type"
,
"其他"
),
"expenditure"
:
v
.
get
(
"expenditure"
,
False
),
"expenditure"
:
v
.
get
(
"expenditure"
),
"customer"
:
v
.
get
(
"customer"
,
False
)
"customer"
:
v
.
get
(
"customer"
),
"balance"
:
balance
,
}
}
# 看是否有id,如果有的话就说明是更新,没有的话就说明是创建
if
v
.
get
(
"id"
,
False
):
if
v
.
get
(
"id"
):
# 更新
expense_obj
=
_self
.
env
[
"roke.product.income.expense"
]
.
sudo
()
.
search
([(
"id"
,
"="
,
v
.
get
(
"id"
))])
cr
.
execute
(
"""
if
not
expense_obj
:
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"
:
"更新失败,没找到对应数据。"
}
return
{
"code"
:
1
,
"message"
:
"更新失败,没找到对应数据。"
}
expense_obj
.
write
(
data
)
else
:
else
:
# 创建
_self
.
env
(
user
=
v
.
get
(
"user_id"
))[
"roke.product.income.expense"
]
.
create
(
data
)
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"
:
"操作成功!"
}
return
{
"code"
:
0
,
"message"
:
"操作成功!"
}
@http.route
(
"/roke/product/product_income_expense/get"
,
type
=
"json"
,
auth
=
'none'
,
cors
=
'*'
,
csrf
=
False
)
@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