mirror of
https://github.com/brmlab/brmbar.git
synced 2025-06-10 06:13:59 +02:00
#19: stored function for "consolidation" transaction
This commit is contained in:
parent
aad79dafa6
commit
96027ca66a
3 changed files with 124 additions and 17 deletions
|
@ -46,11 +46,16 @@ class Account:
|
|||
return cls(db, name = name, id = id, currency = currency, acctype = acctype)
|
||||
|
||||
def balance(self):
|
||||
debit = self.db.execute_and_fetch("SELECT SUM(amount) FROM transaction_splits WHERE account = %s AND side = %s", [self.id, 'debit'])
|
||||
debit = debit[0] or 0
|
||||
credit = self.db.execute_and_fetch("SELECT SUM(amount) FROM transaction_splits WHERE account = %s AND side = %s", [self.id, 'credit'])
|
||||
credit = credit[0] or 0
|
||||
return debit - credit
|
||||
bal = self.db.execute_and_fetch(
|
||||
"SELECT public.compute_account_balance(%s)",
|
||||
[self.id]
|
||||
)[0]
|
||||
return bal
|
||||
#debit = self.db.execute_and_fetch("SELECT SUM(amount) FROM transaction_splits WHERE account = %s AND side = %s", [self.id, 'debit'])
|
||||
#debit = debit[0] or 0
|
||||
#credit = self.db.execute_and_fetch("SELECT SUM(amount) FROM transaction_splits WHERE account = %s AND side = %s", [self.id, 'credit'])
|
||||
#credit = credit[0] or 0
|
||||
#return debit - credit
|
||||
|
||||
def balance_str(self):
|
||||
return self.currency.str(self.balance())
|
||||
|
|
|
@ -248,18 +248,23 @@ class Shop:
|
|||
return False
|
||||
|
||||
def consolidate(self):
|
||||
transaction = self._transaction(description = "BrmBar inventory consolidation")
|
||||
|
||||
excess_balance = self.excess.balance()
|
||||
if excess_balance != 0:
|
||||
print("Excess balance {} debited to profit".format(-excess_balance))
|
||||
self.excess.debit(transaction, -excess_balance, "Excess balance added to profit.")
|
||||
self.profits.debit(transaction, -excess_balance, "Excess balance added to profit.")
|
||||
deficit_balance = self.deficit.balance()
|
||||
if deficit_balance != 0:
|
||||
print("Deficit balance {} credited to profit".format(deficit_balance))
|
||||
self.deficit.credit(transaction, deficit_balance, "Deficit balance removed from profit.")
|
||||
self.profits.credit(transaction, deficit_balance, "Deficit balance removed from profit.")
|
||||
msg = self.db.execute_and_fetch(
|
||||
"SELECT public.make_consolidate_transaction(%s, %s, %s)",
|
||||
[self.excess.id, self.deficit.id, self.profits.id]
|
||||
)[0]
|
||||
#transaction = self._transaction(description = "BrmBar inventory consolidation")
|
||||
#excess_balance = self.excess.balance()
|
||||
#if excess_balance != 0:
|
||||
# print("Excess balance {} debited to profit".format(-excess_balance))
|
||||
# self.excess.debit(transaction, -excess_balance, "Excess balance added to profit.")
|
||||
# self.profits.debit(transaction, -excess_balance, "Excess balance added to profit.")
|
||||
#deficit_balance = self.deficit.balance()
|
||||
#if deficit_balance != 0:
|
||||
# print("Deficit balance {} credited to profit".format(deficit_balance))
|
||||
# self.deficit.credit(transaction, deficit_balance, "Deficit balance removed from profit.")
|
||||
# self.profits.credit(transaction, deficit_balance, "Deficit balance removed from profit.")
|
||||
if msg != None:
|
||||
print(msg)
|
||||
self.db.commit()
|
||||
|
||||
def undo(self, oldtid):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue