diff --git a/scripts/create_table.sql b/scripts/create_table.sql index e9c3aa3..62981f1 100644 --- a/scripts/create_table.sql +++ b/scripts/create_table.sql @@ -28,14 +28,15 @@ CREATE TABLE category ( -- 流水表 CREATE TABLE transaction ( t_id SERIAL PRIMARY KEY, -- 流水号 - c_id INTEGER REFERENCES category(c_id), -- 关联类别号 - a_id INTEGER REFERENCES account(a_id), -- 关联账户号 - s_id INTEGER REFERENCES category(c_id), -- 源账户号 + a_id INTEGER REFERENCES account(a_id) NOT NULL, -- 关联账户号 + c_id INTEGER REFERENCES category(c_id), -- 关联类别号 + s_id INTEGER REFERENCES category(c_id), -- 源账户号 + time TIMESTAMP, amount MONEY, meta JSONB -- 元数据 ); -- {"discription":"something",(可选) --- "type":"in"/"out"/"transfer", +-- "type":"in"/"out"/"transfer"/"init"/"modify", -- "reimburse":{ (报销) -- "finish": true, diff --git a/src/budget/_account.py b/src/budget/_account.py index 5e29524..12395be 100644 --- a/src/budget/_account.py +++ b/src/budget/_account.py @@ -86,19 +86,19 @@ class AccountTab(TabPage): print(data) # 绘制界面并填充数据 - label1 = QLabel("Account Name:") - line1 = QLineEdit() - line1.setText(data[0][0]) + accountNameLabel = QLabel("Account Name:") + accountNameLine = QLineEdit() + accountNameLine.setText(data[0][0]) - label2 = QLabel("Account Balance:") - line2 = QLineEdit() - line2.setText(f'{float(data[0][1]):.2f}') + balanceLabel = QLabel("Account Balance:") + balanceLine = QLineEdit() + balanceLine.setText(f'{float(data[0][1]):.2f}') # 只允许输入两位小数 - line2.setValidator(QDoubleValidator(0.00, 999999999.99, 2)) + balanceLine.setValidator(QDoubleValidator(0.00, 999999999.99, 2)) - label3 = QLabel("Description:") - line3 = QLineEdit() - line3.setText(data[0][2]) + descriptionLabel = QLabel("Description:") + descriptionLine = QLineEdit() + descriptionLine.setText(data[0][2]) button1 = QPushButton("Confirm") button3 = QPushButton("Delete") @@ -114,9 +114,9 @@ class AccountTab(TabPage): button2.clicked.connect(self.aDialog.reject) layout = QFormLayout() - layout.addRow(label1,line1) - layout.addRow(label2,line2) - layout.addRow(label3,line3) + layout.addRow(accountNameLabel,accountNameLine) + layout.addRow(balanceLabel,balanceLine) + layout.addRow(descriptionLabel,descriptionLine) layout.addRow(buttonLayout) layout.setSpacing(12) layout.setContentsMargins(15, 15, 15, 15) @@ -127,15 +127,15 @@ class AccountTab(TabPage): if self.aDialog.exec() == QDialog.DialogCode.Accepted: try: # 获取输入数据 - name = line1.text() - balance = float(line2.text()) + name = accountNameLine.text() + balance = float(balanceLine.text()) - description = line3.text() + description = descriptionLine.text() if (name == ''): raise Exception('Account name cannot be empty') # JSONB插入数据 - data = {"name": line1.text(), "balance": balance, "description": description} + data = {"name": accountNameLine.text(), "balance": balance, "description": description} data = json.dumps(data) print("insert data:",data) @@ -154,15 +154,15 @@ class AccountTab(TabPage): self.aDialog = QDialog(self) self.aDialog.setWindowTitle('Add New Account') - label1 = QLabel("Account Name:") - line1 = QLineEdit() - label2 = QLabel("Account Balance:") - line2 = QLineEdit() - line2.setText("0.00") + accountNameLabel = QLabel("Account Name:") + accountNameLine = QLineEdit() + balanceLabel = QLabel("Account Balance:") + balanceLine = QLineEdit() + balanceLine.setText("0.00") # 只允许输入两位小数 - line2.setValidator(QDoubleValidator(0.00, 999999999.99, 2)) - label3 = QLabel("Description:") - line3 = QLineEdit() + balanceLine.setValidator(QDoubleValidator(0.00, 999999999.99, 2)) + descriptionLabel = QLabel("Description:") + descriptionLine = QLineEdit() button1 = QPushButton("Confirm") button2 = QPushButton("Abort") @@ -174,9 +174,9 @@ class AccountTab(TabPage): button2.clicked.connect(self.aDialog.reject) layout = QFormLayout() - layout.addRow(label1,line1) - layout.addRow(label2,line2) - layout.addRow(label3,line3) + layout.addRow(accountNameLabel,accountNameLine) + layout.addRow(balanceLabel,balanceLine) + layout.addRow(descriptionLabel,descriptionLine) layout.addRow(buttonLayout) layout.setSpacing(12) layout.setContentsMargins(15, 15, 15, 15) @@ -187,19 +187,26 @@ class AccountTab(TabPage): if self.aDialog.exec() == QDialog.DialogCode.Accepted: try: # 获取输入数据 - name = line1.text() - balance = float(line2.text()) + name = accountNameLine.text() + balance = float(balanceLine.text()) - description = line3.text() + description = descriptionLine.text() if (name == ''): raise Exception('Account name cannot be empty') # JSONB插入数据 - data = {"name": line1.text(), "description": description} + 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)", (balance, data,)) + self.pg.execute("INSERT INTO account (balance, meta) VALUES (%s, %s) RETURNING a_id", (balance, data,)) + a_id = self.pg.fetchall()[0][0] + + # 添加账户时,在transaction表中添加一条记录 + data = {"type":"init", "description": "Initial Balance"} + data = json.dumps(data) + self.pg.execute( + "INSERT INTO transaction (a_id, time, amount, meta) VALUES (%s, now(), %s,%s)", (a_id, balance, data,)) # 刷新表格 self.selected() diff --git a/src/budget/_pg.py b/src/budget/_pg.py index 75cf7d2..88899a8 100644 --- a/src/budget/_pg.py +++ b/src/budget/_pg.py @@ -42,7 +42,7 @@ class PostgresTab(TabPage): def connectToDatabase(self): try: - self.conn = psycopg2.connect(database=self.dbName, user=self.user, password=self.password, host=self.host, port=self.port) + self.conn = psycopg2.connect(database=self.dbName, user=self.user, password=self.password, host=self.host, port=self.port, timezone="Asia/Shanghai") self.cur = self.conn.cursor() except Exception as e: print(e)