feat: 转账功能

This commit is contained in:
iridiumR 2023-06-05 18:32:11 +08:00
parent c5c1a1751d
commit dd3c3f6244
No known key found for this signature in database
GPG key ID: 49735733EB1A32C8

View file

@ -21,8 +21,10 @@ class AccountTab(TabPage):
# 默认宽度
self.accountBalanceLine.setFixedWidth(150)
self.accountModifyButton = QPushButton('Modify')
self.accountTransferButton = QPushButton('Transfer')
self.accountAddButton = QPushButton('Add')
self.accountModifyButton.clicked.connect(self.onAccountModifyClicked)
self.accountTransferButton.clicked.connect(self.onAccountTransferClicked)
self.accountAddButton.clicked.connect(self.onAccountAddClicked)
# 创建控件布局
@ -30,6 +32,7 @@ class AccountTab(TabPage):
topLayout.addWidget(self.accountComboBox)
topLayout.addWidget(self.accountBalanceLine)
topLayout.addWidget(self.accountModifyButton)
topLayout.addWidget(self.accountTransferButton)
topLayout.addWidget(self.accountAddButton)
# 创建表格
@ -252,3 +255,73 @@ class AccountTab(TabPage):
self.accountTable.setItem(i, 2, QTableWidgetItem(str(self.transData[i][5])))
self.accountTable.setItem(i, 3, QTableWidgetItem(self.transData[i][6]))
def onAccountTransferClicked(self):
# 创建添加数据对话框
self.aDialog = QDialog(self)
self.aDialog.setWindowTitle('Transfer')
srcLabel = QLabel("Source Account:")
srcComboBox = QComboBox()
for row in self.rows:
data = row[1]
srcComboBox.addItem(data)
dstLabel = QLabel("Destination Account:")
dstComboBox = QComboBox()
for row in self.rows:
data = row[1]
dstComboBox.addItem(data)
amountLabel = QLabel("Amount:")
amountLine = QLineEdit()
amountLine.setText("0.00")
# 只允许输入两位小数
amountLine.setValidator(QDoubleValidator(0.00, 999999999.99, 2))
# 按钮
button1 = QPushButton("Confirm")
button2 = QPushButton("Abort")
buttonLayout = QHBoxLayout()
buttonLayout.addWidget(button1)
buttonLayout.addWidget(button2)
button1.clicked.connect(self.aDialog.accept)
button2.clicked.connect(self.aDialog.reject)
layout = QFormLayout()
layout.addRow(srcLabel,srcComboBox)
layout.addRow(dstLabel,dstComboBox)
layout.addRow(amountLabel,amountLine)
layout.addRow(buttonLayout)
layout.setSpacing(12)
layout.setContentsMargins(15, 15, 15, 15)
self.aDialog.setMinimumWidth(400)
self.aDialog.setLayout(layout)
if self.aDialog.exec() == QDialog.DialogCode.Accepted:
try:
# 获取账户c_id
src_id = self.rows[srcComboBox.currentIndex()][0]
dst_id = self.rows[dstComboBox.currentIndex()][0]
amount = float(amountLine.text())
if (amount <= 0):
raise Exception('Amount must be greater than zero')
if (src_id == dst_id):
raise Exception('Source and destination account cannot be the same')
# 插入数据
data = {"type":"transfer", "description": "Transfer"}
data = json.dumps(data)
self.pg.execute(
"INSERT INTO transaction (time, a_id, s_id, amount, meta)\
VALUES (now(), %s, %s, %s, %s)", (dst_id, src_id, amount, data,))
# 源账户减少
self.pg.execute(
"UPDATE account SET balance = balance - (%s::NUMERIC)::MONEY WHERE a_id = %s", (amount, src_id,))
# 目标账户增加
self.pg.execute(
"UPDATE account SET balance = balance +(%s::NUMERIC)::MONEY WHERE a_id = %s", (amount, dst_id,))
# 刷新表格
self.selected()
except Exception as e:
print(e)
QMessageBox.critical(self, 'Error', str(e))