feat: add trans tab

This commit is contained in:
iridiumR 2023-05-28 12:07:41 +08:00
parent 6536cb9126
commit db96a5057b
No known key found for this signature in database
GPG key ID: 49735733EB1A32C8

View file

@ -1,6 +1,7 @@
from PyQt6.QtWidgets import *
from ._tab import TabPage
from ._pg import PostgresTab
from PyQt6.QtGui import QDoubleValidator
import json
class TransTab(TabPage):
@ -34,7 +35,118 @@ class TransTab(TabPage):
self.setLayout(transLayout)
def onTransAddClicked(self):
pass
# 创建添加数据对话框
self.sDialog = QDialog(self)
self.sDialog.setWindowTitle('Add New Transaction')
typeLable = QLabel("Type:")
typeLine = QComboBox()
typeLine.addItem("Expense")
typeLine.addItem("Income")
typeLine.currentIndexChanged.connect(self.onDialogTypeChanged)
categoryLabel = QLabel("Category:")
categoryLine = QComboBox()
accountLabel = QLabel("Account:")
accountLine = QComboBox()
amountLabel = QLabel("Amount:")
amountLine = QLineEdit()
amountLine.setText("0.00")
# 只允许输入两位小数
amountLine.setValidator(QDoubleValidator(0.00, 999999999.99, 2))
descriptionLabel = QLabel("Description:")
descriptionLine = QLineEdit()
button1 = QPushButton("Confirm")
button2 = QPushButton("Abort")
buttonLayout = QHBoxLayout()
buttonLayout.addWidget(button1)
buttonLayout.addWidget(button2)
button1.clicked.connect(self.sDialog.accept)
button2.clicked.connect(self.sDialog.reject)
self.dialogLayout = QFormLayout()
self.dialogLayout.addRow(typeLable, typeLine)
self.dialogLayout.addRow(categoryLabel, categoryLine)
self.dialogLayout.addRow(accountLabel, accountLine)
self.dialogLayout.addRow(amountLabel, amountLine)
self.dialogLayout.addRow(descriptionLabel, descriptionLine)
self.dialogLayout.addRow(buttonLayout)
self.dialogLayout.setSpacing(12)
self.dialogLayout.setContentsMargins(15, 15, 15, 15)
self.sDialog.setMinimumWidth(400)
self.sDialog.setLayout(self.dialogLayout)
# 初始化数据
self.onDialogTypeChanged(1)
# 显示添加数据对话框
if self.sDialog.exec() == QDialog.DialogCode.Accepted:
try:
amount = float(amountLine.text())
# 获取输入数据
type = typeLine.currentText()
if (type == "Income"):
type = "in"
else:
type = "out"
amount = -amount
category = self.categoryData[categoryLine.currentIndex()][0]
account = self.accountData[accountLine.currentIndex()][0]
description = descriptionLine.text()
# JSONB插入数据
data = {"description": description}
data = json.dumps(data)
print("insert data:", data)
self.pg.execute(
"INSERT INTO transaction (c_id, a_id, amount, meta) VALUES (%s,%s,%s,%s)", (category, account, amount, data,))
self.pg.execute("UPDATE account SET balance = balance + (%s::NUMERIC)::MONEY WHERE a_id = %s", (amount, account,))
# 刷新表格
self.selected()
except Exception as e:
# 处理插入数据错误
print(e)
QMessageBox.critical(self, 'Error', str(e))
def onTransModifyClicked(self):
pass
pass
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")
else:
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])
# 填充账户
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])