Browse Source

Merge branch 'master' of https://code.ffdn.org/franciliens.net/fcn-toolbox

root 6 years ago
parent
commit
c723b4006f
3 changed files with 27 additions and 6 deletions
  1. 11 2
      fcn-cotisation
  2. 4 1
      fcn-dolibarr
  3. 12 3
      fcntoolbox/dolibarr.py

+ 11 - 2
fcn-cotisation

@@ -12,6 +12,9 @@ import smtplib
 from email.mime.text import MIMEText
 from email.parser import Parser
 
+# local imports
+import fcntoolbox.dolibarr as dolibarr
+
 parser = argparse.ArgumentParser()
 parser.add_argument("selection", type=str,
                     choices=["send-reminders", "report"])
@@ -31,6 +34,8 @@ s = smtplib.SMTP('localhost')
 
 conn = psycopg2.connect(database=confdoli['database'], 
   user=confdoli['user'], password=confdoli['password'])
+doli = dolibarr.Instance(conn)
+accounts = list(doli.get_bank_accounts(fields=['iban_prefix', 'bank']))
 
 parsedate = lambda x : datetime.datetime.strptime(x, "%Y-%m-%d %H:%M:%S")
 
@@ -79,8 +84,12 @@ def sendReminders(reminder):
   
   for adherent in adherents:
     adherent['rowid'] = int(adherent['rowid'])
-#    adherent['datefin'] = parsedate(adherent['datefin'])
-    rawemail = remindFormat.format(**adherent)
+    fdict = adherent.copy()
+    if len(accounts) > 0:
+      account = accounts[0]
+      fdict['bank_account_iban'] = account[0]
+      fdict['bank_account_bank'] = account[1]
+    rawemail = remindFormat.format(**fdict)
     parsedemail = Parser().parsestr(rawemail)
     body = parsedemail.get_payload()
     msg = MIMEText(body)

+ 4 - 1
fcn-dolibarr

@@ -8,7 +8,7 @@ import fcntoolbox.dolibarr as dolibarr
 parser = argparse.ArgumentParser()
 
 parser.add_argument("selection", type=str,
-                    choices=["get-adherents", "get-subscribers"])
+                    choices=["get-adherents", "get-subscribers", "get-bank-accounts"])
 parser.add_argument("-c", "--config", type=str,
                     default="/etc/fcntoolbox/config.ini",
                     help="specify a configuration file")
@@ -32,3 +32,6 @@ if args.selection == "get-adherents":
 elif args.selection == "get-subscribers":
   subscribers = doli.get_subscriber(args.product, ['email'])
   print("\n".join(map(lambda x: x[0], subscribers)))
+elif args.selection == "get-bank-accounts":
+  accounts = doli.get_bank_accounts(fields=['iban_prefix'])
+  print("\n".join(map(lambda x: x[0], accounts)))

+ 12 - 3
fcntoolbox/dolibarr.py

@@ -2,7 +2,8 @@
 # Style Guide for Python Code https://www.python.org/dev/peps/pep-0008/
 # Docstring Conventions https://www.python.org/dev/peps/pep-0257/
 
-import psycopg2.sql as sql # >= 2.7
+#import psycopg2.sql as sql # >= 2.7
+from . import sql
 
 class Instance:
   def __init__(self, conn):
@@ -27,7 +28,7 @@ class Instance:
     sqlfields = sql.SQL(',').join(sqlfields)
     sqljoin = sql.SQL('LEFT OUTER JOIN llx_adherent_extrafields AS adhx ON (adh.rowid = adhx.fk_object)')
     sqlquery = sql.SQL("SELECT {0} FROM llx_adherent AS adh {1} WHERE {2}").format(sqlfields, sqljoin, sqlcond)
-    self._cur.execute(sqlquery)
+    self._cur.execute(sqlquery.as_string(self._cur))
     return self._cur
   
   def get_adherent_count(self, activeonly=True):
@@ -58,5 +59,13 @@ class Instance:
     FROM llx_societe, llx_contrat, llx_contratdet, llx_product
     WHERE {1}
     ORDER BY llx_product.ref, llx_contrat.rowid ASC""").format(sqlfields, sqlcond)
-    self._cur.execute(sqlquery)
+    self._cur.execute(sqlquery.as_string(self._cur))
+    return self._cur
+
+  def get_bank_accounts(self, fields=None):
+    # Prepare query
+    if fields is None:
+      fields = ['bank', 'proprio', 'iban_prefix']
+    sqlquery = sql.SQL('SELECT {0} FROM llx_bank_account WHERE courant = 1 AND clos = 0'.format(','.join(fields)))
+    self._cur.execute(sqlquery.as_string(self._cur))
     return self._cur