Repository: jonaswitt/stripe-datev-exporter

Issue:
AssertionError when downloading data
First of all, thanks for the awesome tool!

I can download the data for all months of the previous year 2022 except for November. For this single month I get the following error:

```
(venv) porcupine:stripe-datev-exporter tom$ python stripe-datev-cli.py download 2022 11
Retrieving data between 2022-11-01 and 2022-11-30
Retrieved 6 invoice(s), total -66.94 EUR
Retrieved 2 charge(s), total 21.42 EUR
Wrote    6 invoices      to out/overview/overview-2022-11.csv
Wrote    6 revenue items to out/monthly_recognition/monthly_recognition-2022-11.csv
Traceback (most recent call last):
  File "/Users/tom/Development/stripe-datev-exporter/stripe-datev-cli.py", line 249, in <module>
    StripeDatevCli().run(sys.argv)
  File "/Users/tom/Development/stripe-datev-exporter/stripe-datev-cli.py", line 49, in run
    getattr(self, args.command)(argv[2:])
  File "/Users/tom/Development/stripe-datev-exporter/stripe-datev-cli.py", line 105, in download
    records += stripe_datev.invoices.createAccountingRecords(revenue_item)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tom/Development/stripe-datev-exporter/stripe_datev/invoices.py", line 287, in createAccountingRecords
    months = recognition.split_months(recognition_start, recognition_end, [amount_with_tax])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tom/Development/stripe-datev-exporter/stripe_datev/recognition.py", line 46, in split_months
    assert amount == sum(month["amounts"][idx] for month in months)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError
(venv) porcupine:stripe-datev-exporter tom$ 
```

I am happy to provide more info in order to debug the problem, should that be necessary.



Comments:

Patch:
diff --git a/stripe_datev/recognition.py b/stripe_datev/recognition.py
index a4c1d83..a8852f7 100644
--- a/stripe_datev/recognition.py
+++ b/stripe_datev/recognition.py
@@ -39,7 +39,7 @@ def split_months(start, end, amounts):
 
   months[-1]["amounts"] = [month_amount + remaining_amounts[idx] for idx, month_amount in enumerate(months[-1]["amounts"])]
 
-  if not any(amount > 0 for amount in months[-1]["amounts"]):
+  if not any(amount != 0 for amount in months[-1]["amounts"]):
     months = months[:-1]
 
   for idx, amount in enumerate(amounts):


Test patch:
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/test_recognition.py b/tests/test_recognition.py
new file mode 100644
index 0000000..8daea9e
--- /dev/null
+++ b/tests/test_recognition.py
@@ -0,0 +1,18 @@
+from stripe_datev import config, recognition
+import unittest
+import decimal
+import datetime
+
+class RecognitionTest(unittest.TestCase):
+
+    def test_split_months_simple(self):
+        months = recognition.split_months(config.accounting_tz.localize(datetime.datetime(2020, 4, 1)), config.accounting_tz.localize(datetime.datetime(2020, 6, 30, 23, 59, 59)), [decimal.Decimal('100')])
+
+        self.assertEqual(len(months), 3)
+        self.assertEqual(months[0]["amounts"], [decimal.Decimal('32.97')])
+
+    def test_split_months_negative(self):
+        months = recognition.split_months(datetime.datetime(2022, 11, 15, 17, 3, 22, tzinfo=config.accounting_tz), datetime.datetime(2023, 8, 16, 11, 52, 12, tzinfo=config.accounting_tz), [decimal.Decimal('-14.11')])
+
+        self.assertEqual(len(months), 10)
+        self.assertEqual(months[-1]["amounts"], [decimal.Decimal('-0.78')])