|
|
@@ -15,6 +15,7 @@
|
|
|
'''
|
|
|
import logging
|
|
|
import re
|
|
|
+from subprocess import call
|
|
|
import sys
|
|
|
|
|
|
from path import Path # @UnusedImport
|
|
|
@@ -150,8 +151,7 @@ for data in ws:
|
|
|
facture.autocorrection()
|
|
|
|
|
|
# Ajoute les données au format CSV au fichier d'import
|
|
|
- with open(importfile, 'a') as f:
|
|
|
- f.write(facture.to_csv())
|
|
|
+ facture.dump_to_csv(importfile)
|
|
|
|
|
|
|
|
|
|
|
|
@@ -163,18 +163,16 @@ while errors:
|
|
|
logger.info("Contrôle des données")
|
|
|
|
|
|
# Parcourt les lignes du fichier d'import, et teste la validité de chacune.
|
|
|
- with open(importfile) as f:
|
|
|
- next(f) # saute la première ligne
|
|
|
-
|
|
|
- for line in f:
|
|
|
- facture = Facture.from_csv(line)
|
|
|
-
|
|
|
- if not facture.is_valid():
|
|
|
+ for facture in Facture.load_csv(importfile):
|
|
|
+ if not facture.is_valid():
|
|
|
errors += 1
|
|
|
|
|
|
if errors:
|
|
|
logger.error("<!> Une ou plusieurs erreurs ont été détectées, voir le fichier de log pour plus d'information <!>")
|
|
|
- logger.info("Veuillez corriger les données du fichier %s", importfile)
|
|
|
+ if no_prompt:
|
|
|
+ logger.info("Veuillez executer le script manuellement: %s", Path(__file__).abspath())
|
|
|
+ else:
|
|
|
+ logger.info("Veuillez corriger les données du fichier %s", importfile)
|
|
|
|
|
|
# En cas d'erreur(s), deux possibilités:
|
|
|
# - Le script a été lancé en mode sans interruption avec l'option '-n', on interrompt le script.
|
|
|
@@ -182,114 +180,113 @@ while errors:
|
|
|
if no_prompt:
|
|
|
sys.exit(errors)
|
|
|
else:
|
|
|
+ try:
|
|
|
+ call(["start", importfile])
|
|
|
+ except:
|
|
|
+ logger.error("Erreur au lancement du fichier %s", importfile)
|
|
|
input("Presser une touche pour continuer...")
|
|
|
|
|
|
-
|
|
|
logger.info("Les données sont valides.")
|
|
|
|
|
|
# 3- Si toutes les données sont valides, parcourt les lignes du fichier import.csv et les insère dans la table tbl_Facture.
|
|
|
logger.info("Mise à jour des tables de %s", AnalytiqueDb._path)
|
|
|
|
|
|
-with open(importfile) as f:
|
|
|
- next(f) # saute la première ligne
|
|
|
-
|
|
|
- for line in f:
|
|
|
- facture = Facture.from_csv(line)
|
|
|
-
|
|
|
- logger.info("* Facture %s/%s/%s: traitement", facture.numExBudget, facture.numMandat, facture.numLiqMandat)
|
|
|
- # NB: les données ne sont committées qu'aprés l'exécution de toutes les requêtes suivantes
|
|
|
-
|
|
|
- logger.info("> mise à jour de tbl_Factures")
|
|
|
-
|
|
|
- # Insère les données dans la table tbl_Factures
|
|
|
- sql = """INSERT INTO tbl_Factures ( intExercice, strLiquidation, intLiquidationLigne, strEngagement,
|
|
|
- strEnveloppe, strService, strTiers, strTiersLibelle, strMotsClefs,
|
|
|
- dtmDeb, intOperation, strNomenclature0, strAXE, strCentreCout,
|
|
|
- strObjet, dblMontantTotal, dblMontantTVA, strORIGINE_DONNEES
|
|
|
- )
|
|
|
- VALUES ({intExercice}, '{strLiquidation}', {intLiquidationLigne}, '{strEngagement}',
|
|
|
- '{strEnveloppe}', '{strService}', '{strTiers}', '{strTiersLibelle}', '{strMotsClefs}',
|
|
|
- #{dtmDeb}#, {intOperation}, '{strNomenclature0}', '{strAxe}', '{strCentreCout}',
|
|
|
- '{strObjet}', {dblMontantTotal}, {dblMontantTVA}, '{strORIGINE_DONNEES}')
|
|
|
- """.format(
|
|
|
- intExercice=facture.numExBudget,
|
|
|
- strLiquidation=facture.numLiqMandat,
|
|
|
- intLiquidationLigne=facture.numLigneMandat,
|
|
|
- strEngagement=facture.numMandat,
|
|
|
- strEnveloppe=facture.numEnv,
|
|
|
- strService='7710',
|
|
|
- strTiers=facture.numTiers,
|
|
|
- strTiersLibelle=facture.libRai,
|
|
|
- strMotsClefs=AnalytiqueDb.nz(facture.refIntMandat),
|
|
|
- dtmDeb=AnalytiqueDb.format_date(facture.dateDepDelai),
|
|
|
- intOperation=AnalytiqueDb.nz(facture.codePeriode, "Null"),
|
|
|
- strNomenclature0=facture.typeNomencMarche,
|
|
|
- strAxe=facture.codeAxe,
|
|
|
- strCentreCout=facture.codeCout,
|
|
|
- strObjet=AnalytiqueDb.format_date(facture.dateMandat, out_format="%d/%m/%Y"),
|
|
|
- dblMontantTVA=facture.mntTvaMandat,
|
|
|
- dblMontantTotal=facture.mntVent,
|
|
|
- strORIGINE_DONNEES='ASTRE'
|
|
|
- )
|
|
|
+for facture in Facture.load_csv(importfile):
|
|
|
+
|
|
|
+ logger.info("* Facture %s/%s/%s: traitement", facture.numExBudget, facture.numMandat, facture.numLiqMandat)
|
|
|
+ # NB: les données ne sont committées qu'aprés l'exécution de toutes les requêtes suivantes
|
|
|
+
|
|
|
+ logger.info("> mise à jour de tbl_Factures")
|
|
|
+
|
|
|
+ # Insère les données dans la table tbl_Factures
|
|
|
+ sql = """INSERT INTO tbl_Factures ( intExercice, strLiquidation, intLiquidationLigne, strEngagement,
|
|
|
+ strEnveloppe, strService, strTiers, strTiersLibelle, strMotsClefs,
|
|
|
+ dtmDeb, intOperation, strNomenclature0, strAXE, strCentreCout,
|
|
|
+ strObjet, dblMontantTotal, dblMontantTVA, strORIGINE_DONNEES
|
|
|
+ )
|
|
|
+ VALUES ({intExercice}, '{strLiquidation}', {intLiquidationLigne}, '{strEngagement}',
|
|
|
+ '{strEnveloppe}', '{strService}', '{strTiers}', '{strTiersLibelle}', '{strMotsClefs}',
|
|
|
+ #{dtmDeb}#, {intOperation}, '{strNomenclature0}', '{strAxe}', '{strCentreCout}',
|
|
|
+ '{strObjet}', {dblMontantTotal}, {dblMontantTVA}, '{strORIGINE_DONNEES}')
|
|
|
+ """.format(
|
|
|
+ intExercice=facture.numExBudget,
|
|
|
+ strLiquidation=facture.numLiqMandat,
|
|
|
+ intLiquidationLigne=facture.numLigneMandat,
|
|
|
+ strEngagement=facture.numMandat,
|
|
|
+ strEnveloppe=facture.numEnv,
|
|
|
+ strService='7710',
|
|
|
+ strTiers=facture.numTiers,
|
|
|
+ strTiersLibelle=facture.libRai,
|
|
|
+ strMotsClefs=AnalytiqueDb.nz(facture.refIntMandat),
|
|
|
+ dtmDeb=AnalytiqueDb.format_date(facture.dateDepDelai),
|
|
|
+ intOperation=AnalytiqueDb.nz(facture.codePeriode, "Null"),
|
|
|
+ strNomenclature0=facture.typeNomencMarche,
|
|
|
+ strAxe=facture.codeAxe,
|
|
|
+ strCentreCout=facture.codeCout,
|
|
|
+ strObjet=AnalytiqueDb.format_date(facture.dateMandat, out_format="%d/%m/%Y"),
|
|
|
+ dblMontantTVA=facture.mntTvaMandat,
|
|
|
+ dblMontantTotal=facture.mntVent,
|
|
|
+ strORIGINE_DONNEES='ASTRE'
|
|
|
+ )
|
|
|
+ logger.debug("> %s", sql)
|
|
|
+ analytique_db.execute(sql)
|
|
|
+
|
|
|
+ facture.factureId = analytique_db.first("SELECT TOP 1 dblFactureId FROM tbl_Factures ORDER BY dblFactureId DESC").dblFactureId
|
|
|
+
|
|
|
+
|
|
|
+ if facture.codeAxe == "ENGIN":
|
|
|
+ # La ligne concerne un engin: insère les données dans la table tbl_Facture_Engin
|
|
|
+ logger.info("> mise à jour de tbl_Facture_Engin")
|
|
|
+
|
|
|
+ materiel = analytique_db.first("SELECT intlMaterielID FROM tbl_Materiel WHERE [txtMateriel]='{}'".format(facture.codeCout))
|
|
|
+ materielId = materiel.intlMaterielID if materiel else '859'
|
|
|
+ logger.debug("retrieve intlMaterielID: %s", materielId)
|
|
|
+
|
|
|
+ sql = """INSERT INTO tbl_Facture_Engin ( intlMaterielID, txtMateriel, dblFactureId, strLibelle, dblMontant, strType )
|
|
|
+ VALUES ({}, '{}', {}, '{}', {}, '{}')
|
|
|
+ """.format(materielId,
|
|
|
+ facture.codeCout,
|
|
|
+ facture.factureId,
|
|
|
+ AnalytiqueDb.nz(facture.libCout),
|
|
|
+ facture.mntVent,
|
|
|
+ facture.libRai
|
|
|
+ )
|
|
|
logger.debug("> %s", sql)
|
|
|
analytique_db.execute(sql)
|
|
|
|
|
|
- facture.factureId = analytique_db.first("SELECT TOP 1 dblFactureId FROM tbl_Factures ORDER BY dblFactureId DESC").dblFactureId
|
|
|
-
|
|
|
-
|
|
|
- if facture.codeAxe == "ENGIN":
|
|
|
- # La ligne concerne un engin: insère les données dans la table tbl_Facture_Engin
|
|
|
- logger.info("> mise à jour de tbl_Facture_Engin")
|
|
|
-
|
|
|
- materiel = analytique_db.first("SELECT intlMaterielID FROM tbl_Materiel WHERE [txtMateriel]='{}'".format(facture.codeCout))
|
|
|
- materielId = materiel.intlMaterielID if materiel else '859'
|
|
|
- logger.debug("retrieve intlMaterielID: %s", materielId)
|
|
|
-
|
|
|
- sql = """INSERT INTO tbl_Facture_Engin ( intlMaterielID, txtMateriel, dblFactureId, strLibelle, dblMontant, strType )
|
|
|
- VALUES ({}, '{}', {}, '{}', {}, '{}')
|
|
|
- """.format(materielId,
|
|
|
- facture.codeCout,
|
|
|
- facture.factureId,
|
|
|
- AnalytiqueDb.nz(facture.libCout),
|
|
|
- facture.mntVent,
|
|
|
- facture.libRai
|
|
|
- )
|
|
|
- logger.debug("> %s", sql)
|
|
|
- analytique_db.execute(sql)
|
|
|
-
|
|
|
- elif facture.codeAxe == "AFFAI":
|
|
|
- # La ligne concerne une affaire: insère les données dans la table tbl_Facture_Affaire
|
|
|
- logger.info("> mise à jour de tbl_Facture_Affaire")
|
|
|
-
|
|
|
- sql = """INSERT INTO tbl_Facture_Affaire ( strAffaireId, dblFactureId, strLibelle, dblMontant, strType )
|
|
|
- VALUES ('{}', {}, '{}', {}, '{}')
|
|
|
- """.format(facture.codeCout,
|
|
|
- facture.factureId,
|
|
|
- facture.libRai ,
|
|
|
- facture.mntVent,
|
|
|
- AnalytiqueDb.nz(facture.libCout),
|
|
|
- )
|
|
|
- logger.debug("> %s", sql)
|
|
|
- analytique_db.execute(sql)
|
|
|
-
|
|
|
-
|
|
|
- logger.info("> mise à jour de tbl_Mandatement")
|
|
|
-
|
|
|
- # Insère les données dans la table tbl_Mandatement
|
|
|
- sql = """INSERT INTO tbl_Mandatement ( dblFacture, strNumMandat, dtmMandat, strBordereau )
|
|
|
- VALUES ({}, '{}', #{}#, '{}')
|
|
|
- """.format(facture.factureId,
|
|
|
- facture.numMandat,
|
|
|
- AnalytiqueDb.format_date(facture.dateMandat),
|
|
|
- facture.numBj
|
|
|
- )
|
|
|
+ elif facture.codeAxe == "AFFAI":
|
|
|
+ # La ligne concerne une affaire: insère les données dans la table tbl_Facture_Affaire
|
|
|
+ logger.info("> mise à jour de tbl_Facture_Affaire")
|
|
|
+
|
|
|
+ sql = """INSERT INTO tbl_Facture_Affaire ( strAffaireId, dblFactureId, strLibelle, dblMontant, strType )
|
|
|
+ VALUES ('{}', {}, '{}', {}, '{}')
|
|
|
+ """.format(facture.codeCout,
|
|
|
+ facture.factureId,
|
|
|
+ facture.libRai ,
|
|
|
+ facture.mntVent,
|
|
|
+ AnalytiqueDb.nz(facture.libCout),
|
|
|
+ )
|
|
|
logger.debug("> %s", sql)
|
|
|
analytique_db.execute(sql)
|
|
|
|
|
|
- # Commit les insertions dans la base
|
|
|
- analytique_db.commit()
|
|
|
|
|
|
- logger.info("Facture %s : ok", facture.factureId)
|
|
|
+ logger.info("> mise à jour de tbl_Mandatement")
|
|
|
+
|
|
|
+ # Insère les données dans la table tbl_Mandatement
|
|
|
+ sql = """INSERT INTO tbl_Mandatement ( dblFacture, strNumMandat, dtmMandat, strBordereau )
|
|
|
+ VALUES ({}, '{}', #{}#, '{}')
|
|
|
+ """.format(facture.factureId,
|
|
|
+ facture.numMandat,
|
|
|
+ AnalytiqueDb.format_date(facture.dateMandat),
|
|
|
+ facture.numBj
|
|
|
+ )
|
|
|
+ logger.debug("> %s", sql)
|
|
|
+ analytique_db.execute(sql)
|
|
|
+
|
|
|
+ # Commit les insertions dans la base
|
|
|
+ analytique_db.commit()
|
|
|
+
|
|
|
+ logger.info("Facture %s : ok", facture.factureId)
|
|
|
|
|
|
|
|
|
|