Skip to content

Commit

Permalink
chore: update transaction create logic
Browse files Browse the repository at this point in the history
chore: change file name in ledger creation export
  • Loading branch information
MustafaMunir123 committed Dec 3, 2023
1 parent 2895c8c commit a365a08
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
4 changes: 2 additions & 2 deletions apps/transactions/api/v1/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def order_ledger_columns(dataframe):
column_order = ["entry_no", "date", "narration", "currency", "debit_amount", "credit_amount", "balance"]
return dataframe[column_order]

def export_ledger(self, data) -> None:
def export_ledger(self, data, account) -> None:
transactions_dataframe = pd.DataFrame(data["transactions"])
transactions_dataframe.pop("title")
transactions_dataframe = self.order_ledger_columns(transactions_dataframe)
Expand Down Expand Up @@ -99,7 +99,7 @@ def export_ledger(self, data) -> None:
row_number = self.append_to_worksheet(worksheet, general_dataframe, 1)
self.append_to_worksheet(worksheet, transactions_dataframe, row_number)

workbook.save(f"{self.download_path}/ledger.xlsx")
workbook.save(f"{self.download_path}/{account.title}.xlsx")

@staticmethod
def append_to_worksheet(worksheet, dataframe, row_number, index=False):
Expand Down
49 changes: 33 additions & 16 deletions apps/transactions/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,34 @@ def get_serializer():

def post(self, request):
try:
complete_records = []
for record in request.data["transactions"]:
to_account = record.pop("to_account", None)
from_account = record.pop("from_account", None)
to_account_instance = Account.objects.get(id=to_account)
from_account_instance = Account.objects.get(id=from_account)
serializer = self.get_serializer()
serializer = serializer(data=record)
serializer.is_valid(raise_exception=True)
serializer.save(to_account=to_account_instance, from_account=from_account_instance)
serializer.validated_data["to_account"] = to_account_instance.title
serializer.validated_data["from_account"] = from_account_instance.title
serializer.validated_data["entry_no"] = serializer.data["entry_no"]
complete_records.append(serializer.validated_data)
return success_response(data=complete_records, status=status.HTTP_200_OK)
entry_no = request.data.get("entry_no", None)
if not entry_no:
return success_response(success=False, status=status.HTTP_400_BAD_REQUEST, data="entry_no not provided")

transaction = Transaction.objects.filter(entry_no=entry_no)
if transaction and transaction[0]:
transaction.update(**request.data)
return success_response(status=status.HTTP_200_OK, data=f"transaction with id {entry_no} updated.")

last_transaction = Transaction.objects.last()
last_entry = last_transaction.entry_no
if entry_no != last_entry + 1:
return success_response(
status=status.HTTP_400_BAD_REQUEST, success=False, data="invalid transaction id"
)

to_account = request.data.pop("to_account", None)
from_account = request.data.pop("from_account", None)
to_account_instance = Account.objects.get(id=to_account)
from_account_instance = Account.objects.get(id=from_account)
serializer = self.get_serializer()
serializer = serializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save(to_account=to_account_instance, from_account=from_account_instance)
serializer.validated_data["to_account"] = to_account_instance.title
serializer.validated_data["from_account"] = from_account_instance.title
serializer.validated_data["entry_no"] = serializer.data["entry_no"]
return success_response(data=serializer.validated_data, status=status.HTTP_200_OK)
except Exception as ex:
raise ex

Expand Down Expand Up @@ -178,8 +191,12 @@ class ExportAPIView(APIView):
@staticmethod
def export_ledger(request, pk=None):
try:
account = Account.objects.filter(id=pk)
if not account:
return success_response(status=status.HTTP_400_BAD_REQUEST, data="account does not exists")

data = LedgerAPIView().get(request=request, pk=pk).data
ExportServices().export_ledger(data=data["data"])
ExportServices().export_ledger(data=data["data"], account=account[0])
return success_response(data="File Created", status=status.HTTP_200_OK)
except Exception as ex:
raise ex
Expand Down
Binary file modified db.sqlite3
Binary file not shown.

0 comments on commit a365a08

Please sign in to comment.