Fix remittance import

This commit is contained in:
Yohann D'ANELLO 2020-07-26 23:59:04 +02:00
parent 03dc6f98c8
commit c19a0582bd
1 changed files with 37 additions and 22 deletions

View File

@ -67,6 +67,8 @@ class Command(ImportCommand):
def add_arguments(self, parser): def add_arguments(self, parser):
parser.add_argument('-b', '--buttons', action='store_true', help="import buttons") parser.add_argument('-b', '--buttons', action='store_true', help="import buttons")
parser.add_argument('-t', '--transactions', action='store', default=0, help="start id for transaction import") parser.add_argument('-t', '--transactions', action='store', default=0, help="start id for transaction import")
parser.add_argument('-n', '--nosave', action='store_true', default=True, help="Scan only transactions, "
"don't save them")
@timed @timed
def import_buttons(self, cur, chunk_size, import_buttons): def import_buttons(self, cur, chunk_size, import_buttons):
@ -169,7 +171,7 @@ class Command(ImportCommand):
@timed @timed
@transaction.atomic @transaction.atomic
def import_transaction(self, cur, chunk_size, idmin): def import_transaction(self, cur, chunk_size, idmin, save=True):
bulk_mgr = BulkCreateManager(chunk_size=chunk_size) bulk_mgr = BulkCreateManager(chunk_size=chunk_size)
cur.execute( cur.execute(
f"SELECT t.id, t.date AS transac_date, t.type, t.emetteur,\ f"SELECT t.id, t.date AS transac_date, t.type, t.emetteur,\
@ -184,10 +186,19 @@ class Command(ImportCommand):
pk_membership = 1 pk_membership = 1
pk_transaction = 1 pk_transaction = 1
for idx, row in enumerate(cur): for idx, row in enumerate(cur):
self.update_line(idx, n, row["description"]) if save or idx % chunk_size == 0:
self.update_line(idx, n, row["description"])
MAP_TRANSACTION[row["id"]] = pk_transaction MAP_TRANSACTION[row["id"]] = pk_transaction
if not save:
pk_transaction += 1
if row["valide"] and (row["type"] == "adhésion" or row["description"].lower() == "inscription"):
note = Note.objects.get(pk=self.MAP_IDBDE[row["emetteur"]])
if not isinstance(note, NoteClub):
pk_transaction += 1
continue
try: try:
date = make_aware(row["transac_date"]) date = make_aware(row["transac_date"])
except (pytz.NonExistentTimeError, pytz.AmbiguousTimeError): except (pytz.NonExistentTimeError, pytz.AmbiguousTimeError):
@ -217,7 +228,7 @@ class Command(ImportCommand):
if row["valide"] and (ttype == "adhésion" or row["description"].lower() == "inscription"): if row["valide"] and (ttype == "adhésion" or row["description"].lower() == "inscription"):
note = Note.objects.get(pk=obj_dict["source_id"]) note = Note.objects.get(pk=obj_dict["source_id"])
if isinstance(note, NoteClub): if isinstance(note, NoteClub):
child_transaction = None # don't bother register clubs child_transaction = None # don't bother register clubs
else: else:
user_id = note.user_id user_id = note.user_id
montant = obj_dict["amount"] montant = obj_dict["amount"]
@ -280,6 +291,20 @@ class Command(ImportCommand):
pk_transaction += 1 pk_transaction += 1
bulk_mgr.done() bulk_mgr.done()
@timed
def set_roles(self):
bulk_mgr = BulkCreateManager(chunk_size=10000)
bde_membership_ids = Membership.objects.filter(club__pk=BDE_PK).values_list('id', flat=True)
kfet_membership_ids = Membership.objects.filter(club__pk=KFET_PK).values_list('id', flat=True)
n = len(bde_membership_ids)
for idx, (m_bde_id, m_kfet_id) in enumerate(zip(bde_membership_ids, kfet_membership_ids)):
self.update_line(idx, n, str(idx))
bulk_mgr.add(
Membership.roles.through(membership_id=m_bde_id, role_id=BDE_ROLE_PK),
Membership.roles.through(membership_id=m_kfet_id, role_id=KFET_ROLE_PK),
)
bulk_mgr.done()
@timed @timed
@transaction.atomic @transaction.atomic
def import_remittances(self, cur, chunk_size): def import_remittances(self, cur, chunk_size):
@ -295,7 +320,7 @@ class Command(ImportCommand):
remittance_dict = { remittance_dict = {
"pk": pk_remittance, "pk": pk_remittance,
"date": make_aware(row["date"][:19]), "date": make_aware(row["date"]),
"remittance_type_id": 1, # Only Bank checks are supported in NK15 "remittance_type_id": 1, # Only Bank checks are supported in NK15
"comment": row["commentaire"], "comment": row["commentaire"],
"closed": row["close"], "closed": row["close"],
@ -318,7 +343,7 @@ class Command(ImportCommand):
if not row["idremise"]: if not row["idremise"]:
continue continue
tr = SpecialTransactionProxy.objects.get(transaction__id=MAP_TRANSACTION[row["idtransaction"]]) tr = SpecialTransactionProxy.objects.get_or_create(transaction_id=MAP_TRANSACTION[row["idtransaction"]])[0]
tr.remittance_id = MAP_REMITTANCE[row["idremise"]] tr.remittance_id = MAP_REMITTANCE[row["idremise"]]
tr.save() tr.save()
@ -326,21 +351,10 @@ class Command(ImportCommand):
tr.last_name = row["nom"] tr.last_name = row["nom"]
tr.first_name = row["prenom"] tr.first_name = row["prenom"]
tr.bank = row["banque"] tr.bank = row["banque"]
tr.save() try:
tr.save()
@timed except:
def set_roles(self): print("Failed to save row: " + str(row))
bulk_mgr = BulkCreateManager(chunk_size=10000)
bde_membership_ids = Membership.objects.filter(club__pk=BDE_PK).values_list('id', flat=True)
kfet_membership_ids = Membership.objects.filter(club__pk=KFET_PK).values_list('id', flat=True)
n = len(bde_membership_ids)
for idx, (m_bde_id, m_kfet_id) in enumerate(zip(bde_membership_ids, kfet_membership_ids)):
self.update_line(idx, n, str(idx))
bulk_mgr.add(
Membership.roles.through(membership_id=m_bde_id, role_id=BDE_ROLE_PK),
Membership.roles.through(membership_id=m_kfet_id, role_id=KFET_ROLE_PK),
)
bulk_mgr.done()
@timed @timed
def handle(self, *args, **kwargs): def handle(self, *args, **kwargs):
@ -353,7 +367,8 @@ class Command(ImportCommand):
if kwargs["map"]: if kwargs["map"]:
self.load_map(kwargs["map"]) self.load_map(kwargs["map"])
self.import_buttons(cur, kwargs["chunk"], kwargs["buttons"]) self.import_buttons(cur, kwargs["chunk"], kwargs["buttons"])
self.import_transaction(cur, kwargs["chunk"], kwargs["transactions"]) self.import_transaction(cur, kwargs["chunk"], kwargs["transactions"], not kwargs["nosave"])
self.set_roles() if not kwargs["nosave"]:
self.set_roles()
self.import_remittances(cur, kwargs["chunk"]) self.import_remittances(cur, kwargs["chunk"])
self.import_checks(cur) self.import_checks(cur)