Skip to content

Commit

Permalink
Fix merge conflict, fix none checks
Browse files Browse the repository at this point in the history
  • Loading branch information
BerglundDaniel committed Dec 6, 2023
2 parents b94d98d + da283f1 commit 64c9a83
Show file tree
Hide file tree
Showing 18 changed files with 1,368 additions and 765 deletions.
2 changes: 1 addition & 1 deletion accounting/export_transactions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ year=$1
target="transactions_$1.tsv"
echo "Exporting year $year to $target"
set -e
docker compose exec db2 bash -c "mysql -uroot --batch --default-character-set=utf8 --raw -p\${MYSQL_ROOT_PASSWORD} makeradmin -e \"select webshop_transactions.created_at, webshop_transactions.id, webshop_transaction_contents.amount, webshop_products.name from webshop_transactions LEFT JOIN membership_members ON webshop_transactions.member_id=membership_members.member_id LEFT JOIN webshop_transaction_contents ON webshop_transactions.id=webshop_transaction_contents.transaction_id LEFT JOIN webshop_products ON webshop_products.id=webshop_transaction_contents.product_id WHERE webshop_transactions.status='completed' AND webshop_transactions.created_at>='$year-01-01' AND webshop_transactions.created_at<='$year-12-31 23:59:59' ORDER BY webshop_transactions.created_at;\"" | grep -v "mysql: \[Warning\] Using a password" > $target
docker compose exec db2 bash -c "mysql -uroot --batch --default-character-set=utf8 --raw -p\${MYSQL_ROOT_PASSWORD} makeradmin -e \"select webshop_transactions.created_at, webshop_transactions.id, webshop_transaction_contents.amount, webshop_products.name, webshop_transaction_accounts.account, webshop_transaction_cost_centers.cost_center, webshop_product_accounting.debits, webshop_product_accounting.credits from webshop_transactions LEFT JOIN membership_members ON webshop_transactions.member_id=membership_members.member_id LEFT JOIN webshop_transaction_contents ON webshop_transactions.id=webshop_transaction_contents.transaction_id LEFT JOIN webshop_products ON webshop_products.id=webshop_transaction_contents.product_id LEFT JOIN webshop_product_accounting ON webshop_product_accounting.product_id=webshop_products.id LEFT JOIN webshop_transaction_accounts ON webshop_transaction_accounts.id=webshop_product_accounting.account_id LEFT JOIN webshop_transaction_cost_centers ON webshop_transaction_cost_centers.id=webshop_product_accounting.cost_center_id WHERE webshop_transactions.status='completed' AND webshop_transactions.created_at>='$year-01-01' AND webshop_transactions.created_at<='$year-12-31 23:59:59' ORDER BY webshop_transactions.created_at;\"" | grep -v "mysql: \[Warning\] Using a password" > $target
70 changes: 44 additions & 26 deletions accounting/sie_export/make_bookings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
from typing import Dict, Tuple
from datetime import datetime
from argparse import ArgumentParser

from accounting import MonthlyTransactions


HEADER_TEMPLATE = '''
HEADER_TEMPLATE = """
#FLAGGA 0
#PROGRAM "MakerAdmin Booking" 1.0
Expand All @@ -18,7 +19,8 @@
#KPTYP EUBAS97
#VALUTA SEK
#DIM 1 "Kostnadställe"
'''
"""


def date_format(dt: datetime) -> str:
return dt.strftime("%Y%m%d")
Expand All @@ -29,37 +31,34 @@ def get_header(signer: str, financial_year: int):
date=date_format(datetime.now()),
signer=signer,
date_start=date_format(datetime(financial_year, 1, 1)),
date_end=date_format(datetime(financial_year, 12, 31))
date_end=date_format(datetime(financial_year, 12, 31)),
)


def transaction_string(account, cost_center,sum, date, description) -> str:
def transaction_string(account, cost_center, sum, date, description) -> str:
return f'#TRANS {account} {{"1" "{cost_center}"}} {sum} {date} "{description}"'

def main():

def main():
parser = ArgumentParser()
parser.add_argument("--signer", help="Who generated the file", required=True),
parser.add_argument("--financial-year", type=int, required=True,
help="The financial year of the bookings"),
parser.add_argument("--financial-year", type=int, required=True, help="The financial year of the bookings"),
parser.add_argument("--output", "-o", default=None, help="The output file where to save the export"),
parser.add_argument("--monthly-csv", required=True, help="csv for monthly transactions"),
args = parser.parse_args()

monthly_transactions = MonthlyTransactions.parse_csv(args.monthly_csv)


args = parser.parse_args()

monthly_transactions = MonthlyTransactions.parse_csv(args.monthly_csv)

transaction_by_period = defaultdict(list)
for transaction in monthly_transactions:
transaction_by_period[transaction.period].append(transaction)

file_str: str = ""
file_str += get_header(args.signer, args.financial_year) + "\n"

verfication_number = 1
for period, transactions in transaction_by_period.items():
period_date = datetime.strptime(period, '%Y-%m')
period_date = datetime.strptime(period, "%Y-%m")
date_str = date_format(period_date)
month = period_date.strftime("%B")
year = period_date.strftime("%Y")
Expand All @@ -68,20 +67,39 @@ def main():

for transaction in transactions:
description = f"{transaction.period}"
amount = transaction.amount_debit if(transaction.amount_debit==0) else -transaction.amount_credit

file_str+= transaction_string(transaction.account,transaction.cost_center,transaction.amount_credit-transaction.amount_debit,date_str,description)+"\n"

file_str+= transaction_string(transaction.account,transaction.cost_center,transaction.amount_credit-transaction.amount_debit,date_str,description)+"\n"

verfication_number+=1
file_str += '}\n'
amount = transaction.amount_debit if (transaction.amount_debit == 0) else -transaction.amount_credit

file_str += (
transaction_string(
transaction.account,
transaction.cost_center,
transaction.amount_credit - transaction.amount_debit,
date_str,
description,
)
+ "\n"
)

file_str += (
transaction_string(
transaction.account,
transaction.cost_center,
transaction.amount_credit - transaction.amount_debit,
date_str,
description,
)
+ "\n"
)

verfication_number += 1
file_str += "}\n"

if args.output:
with open(args.output, mode="w",encoding="cp437") as f:
f.write(file_str)
with open(args.output, mode="w", encoding="cp437") as f:
f.write(file_str)
else:
print(file_str)
print(file_str)


if __name__ == "__main__":
main()
main()
55 changes: 27 additions & 28 deletions accounting/transactions_monthly.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,37 @@
output = f"monthly_transactions_summary_{year}.tsv"

monthly_totals = defaultdict(float)
monthly_transactions = defaultdict(int)
account_totals = defaultdict(lambda: defaultdict(float))
cost_center_totals = defaultdict(lambda: defaultdict(float))
debit_totals = defaultdict(lambda: defaultdict(float))
credit_totals = defaultdict(lambda: defaultdict(float))

with open(inputs, 'r') as file:
reader = csv.DictReader(file, delimiter='\t')
with open(inputs, "r") as file:
reader = csv.DictReader(file, delimiter="\t")
for row in reader:
date = datetime.strptime(row['created_at'], '%Y-%m-%d %H:%M:%S')
year_month = date.strftime('%Y-%m')
amount = float(row['amount'])
date = datetime.strptime(row["created_at"], "%Y-%m-%d %H:%M:%S")
year_month = date.strftime("%Y-%m")
amount = float(row["amount"])
monthly_totals[year_month] += amount
monthly_transactions[year_month] += 1
account = row['account']
cost_center = row['cost_center']
account_totals[year_month][account] += amount
cost_center_totals[year_month][cost_center] += amount

with open(output, 'w', newline='') as file:
writer = csv.writer(file, delimiter='\t')
headers = ['Period', 'AmountDebit','AmountCredit', 'Account', 'CostCenter']
account = row["account"]
debits_fraction = float(row["debits"])
credits_fraction = float(row["credits"])
cost_center = row["cost_center"]
debit_totals[year_month][(account, cost_center)] += amount * debits_fraction
credit_totals[year_month][(account, cost_center)] += amount * credits_fraction

with open(output, "w", newline="") as file:
writer = csv.writer(file, delimiter="\t")
headers = ["Period", "AmountDebit", "AmountCredit", "Account", "CostCenter"]
writer.writerow(headers)

for year_month in sorted(monthly_totals.keys()):
for account,cost_center in zip(account_totals[year_month],cost_center_totals[year_month]):
writer.writerow([
year_month,
account_totals[year_month][account],
cost_center_totals[year_month][cost_center],
account,
cost_center
])
for acc_cost_tuple in debit_totals[year_month].keys():
writer.writerow(
[
year_month,
debit_totals[year_month][acc_cost_tuple],
credit_totals[year_month][acc_cost_tuple],
acc_cost_tuple[0],
acc_cost_tuple[1],
]
)
print(f"Data Compiled to {output}")



Loading

0 comments on commit 64c9a83

Please sign in to comment.