mirror of
https://github.com/brmlab/brmbar.git
synced 2025-06-08 13:24:01 +02:00
#9: stored function for sell transaction
This commit is contained in:
parent
aded7a5769
commit
cef95c4313
3 changed files with 175 additions and 17 deletions
|
@ -40,18 +40,20 @@ class Currency:
|
|||
""" Return tuple ($buy, $sell) of rates of $self in relation to $other (brmbar.Currency):
|
||||
$buy is the price of $self in means of $other when buying it (into brmbar)
|
||||
$sell is the price of $self in means of $other when selling it (from brmbar) """
|
||||
|
||||
res = self.db.execute_and_fetch("SELECT rate, rate_dir FROM exchange_rates WHERE target = %s AND source = %s AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1", [self.id, other.id])
|
||||
# buy rate
|
||||
res = self.db.execute_and_fetch("SELECT public.find_buy_rate(%s, %s)",[self.id, other.id])
|
||||
if res is None:
|
||||
raise NameError("Something fishy in find_buy_rate.");
|
||||
buy = res[0]
|
||||
if buy < 0:
|
||||
raise NameError("Currency.rate(): Unknown conversion " + other.name() + " to " + self.name())
|
||||
buy_rate, buy_rate_dir = res
|
||||
buy = buy_rate if buy_rate_dir == "target_to_source" else 1/buy_rate
|
||||
|
||||
res = self.db.execute_and_fetch("SELECT rate, rate_dir FROM exchange_rates WHERE target = %s AND source = %s AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1", [other.id, self.id])
|
||||
# sell rate
|
||||
res = self.db.execute_and_fetch("SELECT public.find_sell_rate(%s, %s)",[self.id, other.id])
|
||||
if res is None:
|
||||
raise NameError("Something fishy in find_sell_rate.");
|
||||
sell = res[0]
|
||||
if sell < 0:
|
||||
raise NameError("Currency.rate(): Unknown conversion " + self.name() + " to " + other.name())
|
||||
sell_rate, sell_rate_dir = res
|
||||
sell = sell_rate if sell_rate_dir == "source_to_target" else 1/sell_rate
|
||||
|
||||
return (buy, sell)
|
||||
|
||||
|
|
|
@ -25,18 +25,25 @@ class Shop:
|
|||
deficit = Account.load(db, name = "BrmBar Deficit"))
|
||||
|
||||
def sell(self, item, user, amount = 1):
|
||||
# Sale: Currency conversion from item currency to shop currency
|
||||
(buy, sell) = item.currency.rates(self.currency)
|
||||
cost = amount * sell
|
||||
profit = amount * (sell - buy)
|
||||
# Call the stored procedure for the sale
|
||||
cost = self.db.execute_and_fetch(
|
||||
"SELECT public.sell_item(%s, %s, %s, %s, %s)",
|
||||
[item.id, amount, user.id, self.currency.id, f"BrmBar sale of {amount}x {item.name} to {user.name}"]
|
||||
)[0]#[0]
|
||||
|
||||
transaction = self._transaction(responsible = user, description = "BrmBar sale of {}x {} to {}".format(amount, item.name, user.name))
|
||||
item.credit(transaction, amount, user.name)
|
||||
user.debit(transaction, cost, item.name) # debit (increase) on a _debt_ account
|
||||
self.profits.debit(transaction, profit, "Margin on " + item.name)
|
||||
self.db.commit()
|
||||
|
||||
return cost
|
||||
# Sale: Currency conversion from item currency to shop currency
|
||||
#(buy, sell) = item.currency.rates(self.currency)
|
||||
#cost = amount * sell
|
||||
#profit = amount * (sell - buy)
|
||||
|
||||
#transaction = self._transaction(responsible = user, description = "BrmBar sale of {}x {} to {}".format(amount, item.name, user.name))
|
||||
#item.credit(transaction, amount, user.name)
|
||||
#user.debit(transaction, cost, item.name) # debit (increase) on a _debt_ account
|
||||
#self.profits.debit(transaction, profit, "Margin on " + item.name)
|
||||
#self.db.commit()
|
||||
#return cost
|
||||
|
||||
def sell_for_cash(self, item, amount = 1):
|
||||
# Sale: Currency conversion from item currency to shop currency
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue