Skip to content

Commit fed8cf5

Browse files
Use booking date when creating cashctrl transactions
1 parent a588edf commit fed8cf5

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

django/finance/accounting/cashctrl.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ def _create_collective_transaction(self, transaction):
9797
split_items_o.append({"accountId": cct_account, credit_type: amount_str})
9898

9999
payload = dict(
100-
dateAdded=datetime.date.today().strftime("%Y-%m-%d"),
100+
dateAdded=self.cct_book_ref.get_date(getattr(transaction, "date", None)).strftime(
101+
"%Y-%m-%d"
102+
),
101103
title=getattr(transaction, "description", ""),
102104
items=json.dumps(split_items_o),
103105
)
@@ -133,11 +135,13 @@ def _create_simple_transaction(self, transaction):
133135
if isinstance(transaction.splits[1].amount, float)
134136
else str(transaction.splits[1].amount)
135137
)
138+
date_added = self.cct_book_ref.get_date(getattr(transaction, "date", None)).strftime(
139+
"%Y-%m-%d"
140+
)
136141
attributes = (
137142
f"amount={amount_str}&creditId={cct_account_credit}&debitId={cct_account_debit}"
138143
f"&title={urllib.parse.quote_plus(getattr(transaction, 'description', ''))}"
139-
f"&dateAdded={datetime.datetime.now()}"
140-
f"&notes={urllib.parse.quote_plus('Added through API')}"
144+
f"&dateAdded={date_added}&notes={urllib.parse.quote_plus('Added through API')}"
141145
)
142146

143147
# Call create endpoint

django/finance/tests/test_accounting_cct.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
from unittest.mock import ANY, Mock, call, patch
23

34
from django.conf import settings
@@ -247,7 +248,44 @@ def test_add_transaction(self, mock_post, mock_get):
247248
)
248249
called_url = mock_post.call_args[0][0]
249250
assert (
250-
f"{self.cohiva_test_endpoint}journal/create.json?amount=100.00&creditId=1237&debitId=1477&title=Test+CashCtrl+add_transaction&dateAdded="
251+
f"{self.cohiva_test_endpoint}journal/create.json?amount=100.00&creditId=1237&debitId=1477&title=Test+CashCtrl+add_transaction&dateAdded=2026-01-01"
252+
in called_url
253+
)
254+
255+
@patch("finance.accounting.cashctrl.requests.get")
256+
@patch("finance.accounting.cashctrl.requests.post")
257+
def test_add_transaction_without_date(self, mock_post, mock_get):
258+
messages = []
259+
with AccountingManager(messages) as book:
260+
# configure fake responses
261+
mock_get.return_value.raise_for_status.side_effect = None
262+
mock_get.side_effect = self.fetch_account_responses
263+
264+
mock_post.return_value.json.return_value = {
265+
"success": True,
266+
"message": "Buchung gespeichert",
267+
"insertId": 700,
268+
}
269+
mock_post.return_value.raise_for_status.side_effect = None
270+
271+
transaction_id = book.add_transaction(
272+
100.00,
273+
self.account1,
274+
self.account2,
275+
None,
276+
"Test CashCtrl add_transaction",
277+
autosave=False,
278+
)
279+
self.assertTrue(transaction_id.startswith("cct_"))
280+
self.assertEqual("cct_0_700", transaction_id)
281+
book.save()
282+
283+
# verify that the API was called
284+
today = datetime.date.today().strftime("%Y-%m-%d")
285+
called_url = mock_post.call_args[0][0]
286+
assert (
287+
f"{self.cohiva_test_endpoint}journal/create.json?amount=100.00&creditId=1237"
288+
f"&debitId=1477&title=Test+CashCtrl+add_transaction&dateAdded={today}"
251289
in called_url
252290
)
253291

0 commit comments

Comments
 (0)