From dd3c3f62443a7ff6276d97c13cfa2a1d97f6ab8a Mon Sep 17 00:00:00 2001 From: iridiumR Date: Mon, 5 Jun 2023 18:32:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=BD=AC=E8=B4=A6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/budget/_account.py | 73 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/src/budget/_account.py b/src/budget/_account.py index 64ef24f..b119dde 100644 --- a/src/budget/_account.py +++ b/src/budget/_account.py @@ -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))