Browse Source

Unmatch some payment containing specific keywords

Alexandre Aubin 7 years ago
parent
commit
b2435565b2
1 changed files with 32 additions and 6 deletions
  1. 32 6
      coin/billing/management/commands/import_payments_from_csv.py

+ 32 - 6
coin/billing/management/commands/import_payments_from_csv.py

@@ -26,6 +26,9 @@ DELIMITER=str(';')
 DATE_FORMAT="%d/%m/%Y"
 # The default regex used to match the label of a payment with a member ID
 ID_REGEX=r"(?i)(\b|_)ID[\s\-\_\/]*(\d+)(\b|_)"
+# If the label of the payment contains one of these, the payment won't be
+# matched to a member when importing it.
+KEYWORDS_TO_NOTMATCH=[ "DON", "MECENAT", "REM CHQ" ]
 
 
 
@@ -79,14 +82,9 @@ should run this command with --commit if you agree with the dry-run."""
 
         payments = self.convertCSVToDicts(self.cleanCSV(self.loadCSV(options["filename"])))
 
-        # Dummy payments for test
-        #payments = []
-        #payments.append({ "date": "2017-03-02", "label":"foo ID 43 zob", "amount":30.0})
-        #payments.append({ "date": "2017-03-14", "label":"foo JohnDoe zob", "amount":30.0})
-        #payments.append({ "date": "2017-04-03", "label":"foo John Doe zob", "amount":30.0})
-
         payments = self.tryToMatchPaymentWithMembers(payments)
         newPayments = self.filterAlreadyKnownPayments(payments)
+        newPayments = self.unmatchPaymentWithKeywords(newPayments)
 
         numberOfAlreadyKnownPayments = len(payments)-len(newPayments)
         numberOfNewPayments = len(newPayments)
@@ -263,6 +261,34 @@ should run this command with --commit if you agree with the dry-run."""
         return payments
 
 
+    def unmatchPaymentWithKeywords(self, payments):
+
+        matchers = {}
+        for keyword in KEYWORDS_TO_NOTMATCH:
+            matchers[keyword] = re.compile(r"(?i)(\b|_|-)"+re.escape(keyword)+r"(\b|_|-)")
+
+        for i, payment in enumerate(payments):
+
+            # If no match found, don't filter anyway
+            if payment["memberMatched"] == None:
+                continue
+
+            for keyword, matcher in matchers.items():
+                matches = matcher.findall(payment["label"])
+
+                # If not found, try next
+                if len(matches) == 0:
+                    continue
+
+                print "Ignoring possible match for payment '%s' because " \
+                      "it contains the keyword %s"                        \
+                      % (payment["label"], keyword)
+                payments[i]["memberMatched"] = None
+
+                break
+
+        return payments
+
     def filterAlreadyKnownPayments(self, payments):
 
         newPayments = []