From cebd1481ecb88e0f65d46e2217409bc16e9f5c38 Mon Sep 17 00:00:00 2001
From: Brett Smith <brettcsmith@brettcsmith.org>
Date: Thu, 26 Oct 2017 12:40:28 -0400
Subject: [PATCH] patreon: Import VAT withholdings.

---
 README.rst                         | 11 +++++++++++
 import2ledger/importers/patreon.py | 17 +++++++++++++----
 tests/data/PatreonVat.csv          |  5 +++++
 tests/data/imports.yml             | 28 ++++++++++++++++++++++++++++
 4 files changed, 57 insertions(+), 4 deletions(-)
 create mode 100644 tests/data/PatreonVat.csv

diff --git a/README.rst b/README.rst
index cbd5d14..5c94863 100644
--- a/README.rst
+++ b/README.rst
@@ -65,6 +65,17 @@ Patreon
 ``template patreon svcfees``
   Imports one expense transaction per month for that month's Patreon service fees.  Generated from Patreon's earnings report CSV.
 
+``template patreon vat``
+  Imports one transaction per country per month each time Patreon withheld VAT.  Generated from Patreon's VAT report CSV.
+
+  This template can use these variables:
+
+  ``country_name``
+    The full name of the country VAT was withheld for
+
+  ``country_code``
+    The two-letter ISO country code of the country VAT was withheld for
+
 Template variables
 ~~~~~~~~~~~~~~~~~~
 
diff --git a/import2ledger/importers/patreon.py b/import2ledger/importers/patreon.py
index 403c16d..d66a512 100644
--- a/import2ledger/importers/patreon.py
+++ b/import2ledger/importers/patreon.py
@@ -53,11 +53,14 @@ class IncomeImporter(ImporterBase):
 
 class FeeImporterBase(ImporterBase):
     def _read_row(self, row):
-        return {
-            'amount': row[self.AMOUNT_FIELD].lstrip('$'),
-            'date': util.strpdate(row['Month'], '%Y-%m'),
-            'payee': "Patreon",
+        retval = {
+            key.lower().replace(' ', '_'): row[key]
+            for key in self.NEEDED_FIELDS.difference([self.AMOUNT_FIELD, 'Month'])
         }
+        retval['amount'] = row[self.AMOUNT_FIELD].lstrip('$')
+        retval['date'] = util.strpdate(row['Month'], '%Y-%m')
+        retval['payee'] = "Patreon"
+        return retval
 
 
 class PatreonFeeImporter(FeeImporterBase):
@@ -70,3 +73,9 @@ class CardFeeImporter(FeeImporterBase):
     AMOUNT_FIELD = 'Processing Fees'
     NEEDED_FIELDS = frozenset(['Month', AMOUNT_FIELD])
     TEMPLATE_KEY = 'template patreon cardfees'
+
+
+class VATImporter(FeeImporterBase):
+    AMOUNT_FIELD = 'Vat Charged'
+    NEEDED_FIELDS = frozenset(['Country Code', 'Country Name', 'Month', AMOUNT_FIELD])
+    TEMPLATE_KEY = 'template patreon vat'
diff --git a/tests/data/PatreonVat.csv b/tests/data/PatreonVat.csv
new file mode 100644
index 0000000..b9aea3f
--- /dev/null
+++ b/tests/data/PatreonVat.csv
@@ -0,0 +1,5 @@
+Country Code,Country Name,Month,Vat Charged
+AT,Austria,2017-09,$2
+BE,Belgium,2017-09,$3.30
+BG,Bulgaria,2017-10,$0.40
+CZ,Czech Republic,2017-10,$6.05
diff --git a/tests/data/imports.yml b/tests/data/imports.yml
index ffc0041..427667e 100644
--- a/tests/data/imports.yml
+++ b/tests/data/imports.yml
@@ -33,3 +33,31 @@
       date: [2017, 10, 1]
       amount: "99.47"
       currency: USD
+
+- source: PatreonVat.csv
+  importer: patreon.VATImporter
+  expect:
+    - payee: Patreon
+      date: [2017, 9, 1]
+      amount: "2"
+      currency: USD
+      country_code: AT
+      country_name: Austria
+    - payee: Patreon
+      date: [2017, 9, 1]
+      amount: "3.30"
+      currency: USD
+      country_code: BE
+      country_name: Belgium
+    - payee: Patreon
+      date: [2017, 10, 1]
+      amount: "0.40"
+      currency: USD
+      country_code: BG
+      country_name: Bulgaria
+    - payee: Patreon
+      date: [2017, 10, 1]
+      amount: "6.05"
+      currency: USD
+      country_code: CZ
+      country_name: Czech Republic