Merge branch 'main' of g.8f.al:ir/budget

This commit is contained in:
zky2020 2023-06-05 15:43:41 +08:00
commit 152255428a
4 changed files with 73 additions and 39 deletions

View file

@ -88,11 +88,11 @@ class AccountTab(TabPage):
# 构造查询语句
a_id = self.rows[index][0]
print(a_id)
self.pg.execute(
"SELECT meta->>'name', replace(substring(balance::text, 2), ',', ''), meta->'description' FROM account WHERE a_id=%s", (a_id,))
data = self.pg.fetchall()
print(data)
oldBalance = float(data[0][1])
@ -149,7 +149,6 @@ class AccountTab(TabPage):
# JSONB插入数据
data = {"name": accountNameLine.text(), "balance": balance, "description": description}
data = json.dumps(data)
print("insert data:",data)
self.pg.execute("UPDATE account SET meta=%s WHERE a_id=%s", (data, a_id))
@ -217,7 +216,6 @@ class AccountTab(TabPage):
# JSONB插入数据
data = {"name": accountNameLine.text(), "description": description}
data = json.dumps(data)
print("insert data:",data)
self.pg.execute("INSERT INTO account (balance, meta) VALUES (%s, %s) RETURNING a_id", (balance, data,))
a_id = self.pg.fetchall()[0][0]
@ -244,7 +242,7 @@ class AccountTab(TabPage):
try:
self.pg.execute("SELECT a_id, meta ->> 'name' FROM account ORDER BY a_id")
self.rows = self.pg.cur.fetchall()
print(self.rows)
for row in self.rows:
data = row[1]
self.accountComboBox.addItem(data)
@ -259,7 +257,6 @@ class AccountTab(TabPage):
self.pg.execute("SELECT c_id, meta ->> 'name' FROM category ORDER BY c_id")
self.categoryData = self.pg.fetchall()
print(self.accountData)
# 初始化表格
self.accountTable.clear()

View file

@ -16,6 +16,7 @@ class CategoryTab(TabPage):
def initUI(self):
# 创建连接信息控件
self.categoryComboBox = QComboBox()
self.categoryComboBox.currentIndexChanged.connect(self.onCategoryIndexChanged)
self.categoryModifyButton = QPushButton('Modify')
self.categoryModifyButton.clicked.connect(self.onCategoryModifyClicked)
@ -57,11 +58,11 @@ class CategoryTab(TabPage):
# 构造查询语句
c_id = self.rows[index][0]
print(c_id)
self.pg.execute(
"SELECT meta->>'name', meta->>'type', meta->'description' FROM category WHERE c_id=%s", (c_id,))
data = self.pg.fetchall()
print(data)
# 绘制界面并填充数据
label1 = QLabel("Category Name:")
@ -122,7 +123,7 @@ class CategoryTab(TabPage):
data = {"name": line1.text(), "type": type,
"description": description}
data = json.dumps(data)
print("insert data:", data)
self.pg.execute("UPDATE category SET meta=%s WHERE c_id=%s",(data,c_id,))
@ -187,7 +188,7 @@ class CategoryTab(TabPage):
data = {"name": line1.text(), "type": type,
"description": description}
data = json.dumps(data)
print("insert data:", data)
self.pg.execute(
"INSERT INTO category (meta) VALUES (%s)", (data,))
@ -208,7 +209,7 @@ class CategoryTab(TabPage):
self.pg.execute(
"SELECT c_id, meta ->> 'name' FROM category ORDER BY c_id")
self.rows = self.pg.cur.fetchall()
print(self.rows)
for row in self.rows:
data = row[1]
self.categoryComboBox.addItem(data)
@ -216,35 +217,32 @@ class CategoryTab(TabPage):
print(e)
QMessageBox.critical(self, 'Error', str(e))
# self.pg.execute("SELECT c_id, meta ->> 'name' FROM category ORDER BY c_id")
# self.categoryData = self.pg.fetchall()
self.pg.execute("SELECT t_id, a_id, c_id, s_id, time, translate(amount::varchar,'$',''), meta->>'description' FROM transaction ORDER BY time DESC")
def onCategoryIndexChanged(self, index):
# 获取comboBox中的c_id
c_id = self.rows[index][0]
self.pg.execute("SELECT t_id, a_id, c_id, s_id, time, translate(amount::varchar,'$',''),\
meta->>'description' FROM transaction \
WHERE c_id = %s ORDER BY time DESC", (c_id,))
self.transData = self.pg.fetchall()
self.pg.execute("SELECT a_id, meta ->> 'name' FROM account ORDER BY a_id")
self.accountData = self.pg.fetchall()
self.pg.execute("SELECT c_id, meta ->> 'name' FROM category ORDER BY c_id")
self.categoryData = self.pg.fetchall()
print(self.rows)
# 初始化表格
self.categoryTable.clear()
self.categoryTable.setRowCount(len(self.transData))
self.categoryTable.setColumnCount(3) # 类别名称 类别 描述
self.categoryTable.setHorizontalHeaderLabels(['Category Name', 'Category Type','Description'])
self.categoryTable.setColumnCount(4) # 时间 账户 金额 描述
self.categoryTable.setHorizontalHeaderLabels(['Time', 'Account', 'Amount', 'Description'])
# 设置自动调整列宽
self.categoryTable.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch)
# 填充表格
for i in range(len(self.transData)):
# self.categoryTable.setItem(i, 2, QTableWidgetItem(self.accountData[self.transData[i][1]-1][1]))
if self.transData[i][2] is None:
self.categoryTable.setItem(i, 0, QTableWidgetItem("None"))
else:
self.categoryTable.setItem(i, 0, QTableWidgetItem(data))
# self.categoryTable.setItem(i, 0, QTableWidgetItem(self.accountData[self.transData[i][2]-1][1]))
# self.categoryTable.setItem(i, 0, QTableWidgetItem(data[0][0]))
self.categoryTable.setItem(i, 1, QTableWidgetItem(str(self.transData[i][5])))
self.categoryTable.setItem(i, 2, QTableWidgetItem(self.transData[i][6]))
self.categoryTable.setItem(i, 0, QTableWidgetItem(self.transData[i][4].strftime("%Y-%m-%d %H:%M:%S")))
self.categoryTable.setItem(i, 1, QTableWidgetItem(self.accountData[self.transData[i][1]-1][1]))
self.categoryTable.setItem(i, 2, QTableWidgetItem(str(self.transData[i][5])))
self.categoryTable.setItem(i, 3, QTableWidgetItem(self.transData[i][6]))

View file

@ -6,6 +6,7 @@ from PyQt6.QtGui import QDoubleValidator
import datetime
import json
class StatTab(TabPage):
def __init__(self, parent):
super().__init__()
@ -25,10 +26,11 @@ class StatTab(TabPage):
# 饼状图
self.chart = QChart()
self.chart.setTitle('Month Statistics')
self.chart.setAnimationOptions(QChart.AnimationOption.SeriesAnimations)
chartLayout = QVBoxLayout()
chartLayout.addWidget(QChartView(self.chart))
# 关闭图例
self.chart.legend().setVisible(False)
leftLayout = QVBoxLayout()
leftLayout.addLayout(buttonLayout)
@ -48,6 +50,9 @@ class StatTab(TabPage):
statLayout = QHBoxLayout()
statLayout.addLayout(leftLayout)
statLayout.addLayout(calenderLayout)
# 设置左右空间占比为21
statLayout.setStretchFactor(leftLayout, 2)
statLayout.setStretchFactor(calenderLayout, 1)
self.setLayout(statLayout)
def onOutButtonClicked(self):
@ -59,26 +64,26 @@ class StatTab(TabPage):
self.selected()
def onCalendarClicked(self, date):
pass
def onCalendarPageChanged(self, year, month):
# 合计分类的交易量
# 清除饼状图
self.chart.removeAllSeries()
# 构建日期查询
startTime = datetime.datetime(date.year(), date.month(), date.day(), 0, 0, 0)
endTime = datetime.datetime(date.year(), date.month(), date.day(), 23, 59, 59)
# 查询交易
self.pg.execute("SELECT c_id, sum(amount)::numeric::float8 FROM transaction WHERE c_id IS NOT NULL and \
time BETWEEN %s AND %s GROUP BY c_id ORDER BY c_id", \
(datetime.date(year, month, 1), datetime.date(year, month+1, 1),))
time BETWEEN %s AND %s GROUP BY c_id ORDER BY c_id", \
(startTime, endTime,))
self.statData = self.pg.fetchall()
print(self.statData)
# 获取此所有交易类别
# 获取此所有交易类别
self.pg.execute("SELECT c_id, meta->>'type', meta->>'name' FROM category \
WHERE c_id in (SELECT c_id FROM transaction WHERE time between %s and %s GROUP BY c_id) \
ORDER BY c_id", \
(datetime.date(year, month, 1), datetime.date(year, month+1, 1),))
(startTime, endTime,))
self.categoryData = self.pg.fetchall()
print(self.categoryData)
# 填充饼状图
self.chart.setTitle('Day Statistics')
self.chart.removeAllSeries()
series = QPieSeries()
@ -87,6 +92,41 @@ class StatTab(TabPage):
series.append(self.categoryData[i][2], self.statData[i][1])
self.chart.addSeries(series)
# 在饼状图内显示数额
for slice in series.slices():
slice.setLabelVisible(True)
slice.setLabel(slice.label()+" "+"{:.2f}%".format(100 * slice.percentage()))
def onCalendarPageChanged(self, year, month):
# 合计分类的交易量
self.pg.execute("SELECT c_id, sum(amount)::numeric::float8 FROM transaction WHERE c_id IS NOT NULL and \
time BETWEEN %s AND %s GROUP BY c_id ORDER BY c_id", \
(datetime.date(year, month, 1), datetime.date(year, month+1, 1),))
self.statData = self.pg.fetchall()
# 获取此月所有交易类别
self.pg.execute("SELECT c_id, meta->>'type', meta->>'name' FROM category \
WHERE c_id in (SELECT c_id FROM transaction WHERE time between %s and %s GROUP BY c_id) \
ORDER BY c_id", \
(datetime.date(year, month, 1), datetime.date(year, month+1, 1),))
self.categoryData = self.pg.fetchall()
# 填充饼状图
self.chart.setTitle('Month Statistics')
self.chart.removeAllSeries()
series = QPieSeries()
for i in range(len(self.statData)):
if (self.categoryData[i][1] == self.mode):
series.append(self.categoryData[i][2], self.statData[i][1])
self.chart.addSeries(series)
# 在饼状图内显示数额
for slice in series.slices():
slice.setLabelVisible(True)
slice.setLabel(slice.label()+" "+"{:.2f}%".format(100 * slice.percentage()))
def selected(self):
# 刷新图表
self.onCalendarPageChanged(self.calendar.yearShown(), self.calendar.monthShown())

View file

@ -107,7 +107,7 @@ class TransTab(TabPage):
# JSONB插入数据
data = {"type":type, "description": description}
data = json.dumps(data)
print("insert data:", data)
self.pg.execute(
"INSERT INTO transaction (c_id, a_id, time, amount, meta) VALUES (%s,%s,now(), %s,%s)", (category, account, amount, data,))
@ -181,7 +181,7 @@ class TransTab(TabPage):
if num != amountLine.text():
amount = float(amountLine.text())
amount = amount - float(num)
print(amount)
self.pg.execute("UPDATE account SET balance = balance + (%s::NUMERIC)::MONEY WHERE a_id = %s",\
(amount, self.transData[self.rows[0].row()][1],))
@ -216,7 +216,7 @@ class TransTab(TabPage):
def onDialogTypeChanged(self, index):
# 获取当前选择的类型
type = self.dialogLayout.itemAt(1).widget().currentText()
print(type)
if(type=="Income"):
self.pg.execute(
"SELECT c_id, meta->>'name' FROM category WHERE meta->>'type'='in' ORDER BY c_id")
@ -224,7 +224,7 @@ class TransTab(TabPage):
self.pg.execute(
"SELECT c_id, meta->>'name' FROM category WHERE meta->>'type'='out' ORDER BY c_id")
self.categoryData = self.pg.fetchall()
print(self.categoryData)
self.dialogLayout.itemAt(3).widget().clear()
for i in range(len(self.categoryData)):
self.dialogLayout.itemAt(3).widget().addItem(self.categoryData[i][1])
@ -232,7 +232,7 @@ class TransTab(TabPage):
# 填充账户
self.pg.execute("SELECT a_id, meta ->> 'name' FROM account ORDER BY a_id")
self.accountData = self.pg.fetchall()
print(self.accountData)
self.dialogLayout.itemAt(5).widget().clear()
for i in range(len(self.accountData)):
self.dialogLayout.itemAt(5).widget().addItem(self.accountData[i][1])
@ -245,7 +245,6 @@ class TransTab(TabPage):
self.pg.execute("SELECT c_id, meta ->> 'name' FROM category ORDER BY c_id")
self.categoryData = self.pg.fetchall()
print(self.transData)
# 初始化表格
self.transTable.clear()