Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import uuid
OMISE_FEE = 0.0365
THAILAND_VAT = 0.07
class Transaction:
def __init__(self, balance, direction, amount, description):
self.balance = balance
self.direction = direction
self.amount = amount
self.description = description
def match(self, balance, direction):
return self.balance == balance and self.direction == direction
def __str__(self):
return "[ {:>11s} ] {:<6s} {:>7,.2f} - {}".format(
self.balance,
self.direction,
self.amount,
self.description,
)
class Charge:
def __init__(self, merchant, customer, amount):
self.id = 'chrg_' + uuid.uuid4().hex[-6:]
self.merchant = merchant
self.customer = customer
self.amount = amount
def bookkeep(self):
total = self.amount
fee = round(total * OMISE_FEE, 2)
fee_vat = round(fee * THAILAND_VAT, 2)
net = total - fee - fee_vat
return [
Transaction(self.customer, 'credit', total, 'customer charge'),
Transaction(self.id, 'debit', total, 'customer charge'),
Transaction(self.id, 'credit', fee, 'processing fee'),
Transaction('Omise', 'debit', fee, 'processing fee'),
Transaction(self.id, 'credit', fee_vat, 'VAT on processing fee'),
Transaction('Omise', 'debit', fee_vat, 'VAT on processing fee'),
Transaction(self.id, 'credit', net, 'merchant net'),
Transaction(self.merchant, 'debit', net, 'merchant net'),
]
def __str__(self):
return '[{}] {} - {} {:>7,.2f}'.format(
self.merchant,
self.id,
self.customer,
self.amount,
)
charges = [
Charge('Burger King', 'John', 199.88),
Charge('Burger King', 'John', 99.89),
Charge('Burger King', 'John', 259.99),
]
transactions = []
for chrg in charges:
transactions.extend(chrg.bookkeep())
debits = sum(tx.amount for tx in transactions if tx.match('John', 'debit'))
credits = sum(tx.amount for tx in transactions if tx.match('John', 'credit'))
john_total = debits - credits
debits = sum(tx.amount for tx in transactions if tx.match('Omise', 'debit'))
credits = sum(tx.amount for tx in transactions if tx.match('Omise', 'credit'))
omise_total = debits - credits
debits = sum(tx.amount for tx in transactions if tx.match('Burger King', 'debit'))
credits = sum(tx.amount for tx in transactions if tx.match('Burger King', 'credit'))
bking_total = debits - credits
print("John has %.2f in balance" % (john_total))
print("Omise has %.2f in balance" % (omise_total))
print("Burger King has %.2f in balance" % (bking_total))
print("\nCharges:")
for charge in charges:
print(charge)
print("\nTransactions:")
for tx in transactions:
print(tx)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment