소스 검색

CHG ctrl2analytique - Refonte du controle des données

olivier.massot 7 년 전
부모
커밋
58bab4eeb3
1개의 변경된 파일100개의 추가작업 그리고 193개의 파일을 삭제
  1. 100 193
      ctrl2analytique.py

+ 100 - 193
ctrl2analytique.py

@@ -15,7 +15,7 @@ import sys
 from path import Path  # @UnusedImport
 
 from core import logconf
-from core.pde import ControlesDb, AnalytiqueDb, mk_workdir, CommunDb, Affaire, \
+from core.pde import ControlesDb, AnalytiqueDb, CommunDb, Affaire, \
     Interv, Tarification
 from core.sqlformatter import SqlFormatter
 
@@ -351,200 +351,107 @@ def main():
     interventions = interventions_cc + interventions_ce + interventions_ci
 
     # ########## CONTROLE ET CORRECTION DES DONNEES ##########
+    # En cas d'erreurs, on liste celles-ci et on annule l'import.
 
-    errors = -1
-
-    while errors:
-        errors = 0
-        to_remove = []
-
-        for affaire in affaires:
-
-            prefix = "Affaire {}: ".format(affaire.strLiaisonControle)
-
-            logger.info("Contrôle de %s: %s", prefix, affaire)
-
-            if not affaire.strMOId:
-                logging.error(prefix + "MO manquant")
-                errors += 1
-                if not no_prompt:
-                    affaire.strMOId = input("Saisir la nouvelle valeur:")
-            else:
-                if not commun_db.exists(Sql.format("SELECT [lngTiersId] FROM tblTiers WHERE [lngTiersId]={}", affaire.strMOId)):
-                    logging.error(prefix + "Le MO %s n'existe pas dans tblTiers", affaire.strMOId)
-                    errors += 1
-                    if not no_prompt:
-                        affaire.strMOId = input("Saisir la nouvelle valeur:")
-
-            if not affaire.strMOeId:
-                logging.error(prefix + "MOe manquant")
-                errors += 1
-                if not no_prompt:
-                    affaire.strMOeId = input("Saisir la nouvelle valeur:")
-            else:
-                if not commun_db.exists(Sql.format("SELECT [lngTiersId] FROM tblTiers WHERE [lngTiersId]={}", affaire.strMOeId)):
-                    logging.error(prefix + "Le MOe %s n'existe pas dans tblTiers", affaire.strMOeId)
-                    errors += 1
-                    if not no_prompt:
-                        affaire.strMOeId = input("Saisir la nouvelle valeur:")
-
-            if not affaire.strEntrepriseId:
-                logging.error(prefix + "Entreprise manquante")
-                errors += 1
-                if not no_prompt:
-                    affaire.strEntrepriseId = input("Saisir la nouvelle valeur:")
-            else:
-                if not commun_db.exists(Sql.format("SELECT [lngTiersId] FROM tblTiers WHERE [lngTiersId]={}", affaire.strEntrepriseId)):
-                    logging.error(prefix + "L'entreprise %s n'existe pas dans tblTiers", affaire.strEntrepriseId)
-                    errors += 1
-                    if not no_prompt:
-                        affaire.strEntrepriseId = input("Saisir la nouvelle valeur:")
-
-            if not affaire.strCommneId:
-                logging.error(prefix + "Commune manquante")
-                errors += 1
-                if not no_prompt:
-                    affaire.strCommneId = input("Saisir la nouvelle valeur:")
-            else:
-                if not commun_db.exists(Sql.format("SELECT [lngTiersId] FROM tblTiers WHERE [lngTiersId]={}", affaire.strCommneId)):
-                    logging.error(prefix + "La commune %s n'existe pas dans tblTiers", affaire.strCommneId)
-                    errors += 1
-                    if not no_prompt:
-                        affaire.strCommneId = input("Saisir la nouvelle valeur:")
-
-            if not affaire.strTypeId:
-                logging.error(prefix + "Type d'affaire manquant")
-                errors += 1
-                if not no_prompt:
-                    affaire.strTypeId = input("Saisir la nouvelle valeur:")
-
-            if not affaire.dtmCommande:
-                logging.error(prefix + "Date de commande manquante")
-                errors += 1
-                if not no_prompt:
-                    affaire.strTypeId = datetime.strptime(input("Saisir la nouvelle valeur (au format jj/mm/aaaa):"), "%d/%m/%Y")
-
-            if affaire.blnMarche == True:
-                if not affaire.intDevisId:
-                    logging.error(prefix + "Numéro de devis manquant")
-                    errors += 1
-                    if not no_prompt:
-                        affaire.intDevisId = input("Saisir la nouvelle valeur:")
-
-            if analytique_db.exists(Sql.format("SELECT dblAffaireId FROM tbl_Affaires WHERE [strLiaisonControle]={:text}", affaire.strLiaisonControle)):
-                logging.error(prefix + "Une affaire portant ce code existe déjà: %s", affaire.strLiaisonControle)
-                errors += 1
-                if not no_prompt:
-                    if input("Tapez 'n' pour retirer cette affaire de la liste des affaires à créer:") == 'n':
-                        to_remove.append(affaire.strLiaisonControle)
-
-        if to_remove:
-            affaires = [affaire for affaire in affaires if not affaire.strLiaisonControle in to_remove]
-
-
-        for interv in interventions:
-
-            prefix = "Intervention {}: ".format(interv.strTest)
-            logger.info("Contrôle de %s: %s", prefix, interv)
-
-            if not interv.strEquipeId:
-                logging.error(prefix + "Equipe manquante")
-                errors += 1
-                if not no_prompt:
-                    interv.strEquipeId = input("Saisir la nouvelle valeur:")
-            else:
-                if not analytique_db.exists(Sql.format("SELECT [strEquipesId] FROM tbl_Equipe WHERE [strEquipesId]={:text}", interv.strEquipeId)):
-                    logging.error(prefix + "L'equipe %s n'existe pas dans tbl_Equipe", interv.strEquipeId)
-                    errors += 1
-                    if not no_prompt:
-                        interv.strEquipeId = input("Saisir la nouvelle valeur:")
-
-            if not interv.strEnginId:
-                logging.error(prefix + "Engin manquant")
-                errors += 1
-                if not no_prompt:
-                    interv.strEnginId = input("Saisir la nouvelle valeur:")
-            else:
-                if not analytique_db.exists(Sql.format("SELECT [strEnginId] FROM tbl_Engin WHERE [strEnginId]={:text}", interv.strEnginId)):
-                    logging.error(prefix + "L'engin %s n'existe pas dans tbl_Engin", interv.strEnginId)
-                    errors += 1
-                    if not no_prompt:
-                        interv.strEnginId = input("Saisir la nouvelle valeur:")
-
-            if not interv.strRapportId:
-                logging.error(prefix + "Rapport manquant")
-                errors += 1
-                if not no_prompt:
-                    interv.strRapportId = input("Saisir la nouvelle valeur:")
-
-            if not interv.strCatégorieInterventionId:
-                logging.error(prefix + "Catégorie de l'intervention manquante")
-                errors += 1
-                if not no_prompt:
-                    interv.strCatégorieInterventionId = input("Saisir la nouvelle valeur:")
-            else:
-                if not analytique_db.exists(Sql.format("SELECT [strCategorieInterventioinId] FROM tbl_CategorieIntervention WHERE [strCategorieInterventioinId]={:text}", interv.strCatégorieInterventionId)):
-                    logging.error(prefix + "La catégorie d'intervention %s n'existe pas dans tbl_CategorieIntervention", interv.strCatégorieInterventionId)
-                    errors += 1
-                    if not no_prompt:
-                        interv.strCatégorieInterventionId = input("Saisir la nouvelle valeur:")
-
-            if not interv.strTypeInterventionId:
-                logging.error(prefix + "Type d'intervention manquant")
-                errors += 1
-                if not no_prompt:
-                    interv.strTypeInterventionId = input("Saisir la nouvelle valeur:")
-            else:
-                if not analytique_db.exists(Sql.format("SELECT [strTypeInterventionId] FROM tbl_TypeIntervention WHERE [strTypeInterventionId]={:text}", interv.strTypeInterventionId)):
-                    logging.error(prefix + "Le type d'intervention %s n'existe pas dans tbl_TypeIntervention", interv.strTypeInterventionId)
-                    errors += 1
-                    if not no_prompt:
-                        interv.strTypeInterventionId = input("Saisir la nouvelle valeur:")
-
-            if not interv.dblquantite:
-                logging.error(prefix + "Quantité nulle")
-                errors += 1
-                if not no_prompt:
-                    interv.dblquantite = input("Saisir la nouvelle valeur:")
-
-            if not interv.strunite:
-                logging.error(prefix + "Unité non renseignée")
-                errors += 1
-                if not no_prompt:
-                    interv.strunite = input("Saisir la nouvelle valeur:")
-            else:
-                if not commun_db.exists(Sql.format("SELECT [strUniteCourt] FROM tblUnite WHERE [strUniteCourt]={:text}", interv.strunite)):
-                    logging.error(prefix + "L'unité %s n'existe pas dans tblUnite", interv.strunite)
-                    errors += 1
-                    if not no_prompt:
-                        interv.strunite = input("Saisir la nouvelle valeur:")
-
-            if not interv.dtmIntervention:
-                logging.error(prefix + "Erreur : date d'intervention")
-                errors += 1
-                if not no_prompt:
-                    interv.dtmIntervention = datetime.strptime(input("Saisir la nouvelle valeur (au format jj/mm/aaaa):"), "%d/%m/%Y")
-
-            if not interv.dtmDureeIntervention or interv.dtmDureeIntervention == date_zero:
-                logging.error(prefix + "Durée d'intervention nulle")
-                errors += 1
-                if not no_prompt:
-                    try:
-                        reponse = input("Saisir la nouvelle valeur (en heures):")
-                        interv.dtmDureeIntervention = date_zero + timedelta(hours=int(reponse))
-                    except ValueError:
-                        logger.error("Valeur corrigée invalide ('%s')", reponse)
-
-        # *** 6- Interruption pour corection manuelle des données (si nécessaire)
-        if errors:
-            logging.error("<!> %s erreurs ont été détectées dans les données à importer. <!>", errors)
-            if no_prompt:
-                logger.info("# Annulation de l'import")
-                sys.exit(1)
+    errors = []
+    logger.info("# Contrôle des données...")
+    for affaire in affaires:
+
+        prefix = "Affaire {}: ".format(affaire.strLiaisonControle)
+        if not affaire.strMOId:
+            errors.append(prefix + "MO manquant")
+        else:
+            if not commun_db.exists(Sql.format("SELECT [lngTiersId] FROM tblTiers WHERE [lngTiersId]={}", affaire.strMOId)):
+                errors.append(prefix + "Le MO {} n'existe pas dans tblTiers".format(affaire.strMOId))
+
+        if not affaire.strMOeId:
+            errors.append(prefix + "MOe manquant")
+        else:
+            if not commun_db.exists(Sql.format("SELECT [lngTiersId] FROM tblTiers WHERE [lngTiersId]={}", affaire.strMOeId)):
+                errors.append(prefix + "Le MOe {} n'existe pas dans tblTiers".format(affaire.strMOeId))
+
+        if not affaire.strEntrepriseId:
+            errors.append(prefix + "Entreprise manquante")
+        else:
+            if not commun_db.exists(Sql.format("SELECT [lngTiersId] FROM tblTiers WHERE [lngTiersId]={}", affaire.strEntrepriseId)):
+                errors.append(prefix + "L'entreprise {} n'existe pas dans tblTiers".format(affaire.strEntrepriseId))
+
+        if not affaire.strCommneId:
+            errors.append(prefix + "Commune manquante")
+        else:
+            if not commun_db.exists(Sql.format("SELECT [lngTiersId] FROM tblTiers WHERE [lngTiersId]={}", affaire.strCommneId)):
+                errors.append(prefix + "La commune {} n'existe pas dans tblTiers".format(affaire.strCommneId))
+
+        if not affaire.strTypeId:
+            errors.append(prefix + "Type d'affaire manquant")
+
+        if not affaire.dtmCommande:
+            errors.append(prefix + "Date de commande manquante")
+
+        if affaire.blnMarche == True:
+            if not affaire.intDevisId:
+                errors.append(prefix + "Numéro de devis manquant")
+
+        if analytique_db.exists(Sql.format("SELECT dblAffaireId FROM tbl_Affaires WHERE [strLiaisonControle]='{}'", affaire.strLiaisonControle)):
+            errors.append(prefix + "Une affaire portant ce code existe déjà: {}".format(affaire.strLiaisonControle))
+
+    for interv in interventions:
+
+        prefix = "Intervention {}: ".format(interv.strTest)
+
+        if not interv.strEquipeId:
+            errors.append(prefix + "Equipe manquante")
+        else:
+            if not analytique_db.exists(Sql.format("SELECT [strEquipesId] FROM tbl_Equipe WHERE [strEquipesId]={:text}", interv.strEquipeId)):
+                errors.append(prefix + "L'equipe {} n'existe pas dans tbl_Equipe".format(interv.strEquipeId))
+
+        if not interv.strEnginId:
+            errors.append(prefix + "Engin manquant")
+        else:
+            if not analytique_db.exists(Sql.format("SELECT [strEnginId] FROM tbl_Engin WHERE [strEnginId]={:text}", interv.strEnginId)):
+                errors.append(prefix + "L'engin {} n'existe pas dans tbl_Engin".format(interv.strEnginId))
+
+        if not interv.strRapportId:
+            errors.append(prefix + "Rapport manquant")
+
+        if not interv.strCatégorieInterventionId:
+            errors.append(prefix + "Catégorie de l'intervention manquante")
+        else:
+            if not analytique_db.exists(Sql.format("SELECT [strCategorieInterventioinId] FROM tbl_CategorieIntervention WHERE [strCategorieInterventioinId]={:text}", interv.strCatégorieInterventionId)):
+                errors.append(prefix + "La catégorie d'intervention {} n'existe pas dans tbl_CategorieIntervention".format(interv.strCatégorieInterventionId))
+
+        if not interv.strTypeInterventionId:
+            errors.append(prefix + "Type d'intervention manquant")
+        else:
+            if not analytique_db.exists(Sql.format("SELECT [strTypeInterventionId] FROM tbl_TypeIntervention WHERE [strTypeInterventionId]={:text}", interv.strTypeInterventionId)):
+                errors.append(prefix + "Le type d'intervention {} n'existe pas dans tbl_TypeIntervention".format(interv.strTypeInterventionId))
+
+        if not interv.dblquantite:
+            errors.append(prefix + "Quantité nulle")
+
+        if not interv.strunite:
+            errors.append(prefix + "Unité non renseignée")
         else:
-            logging.info("Aucune erreur n'a été détectée dans les données.")
-            if not no_prompt:
-                input("Presser Entrée pour continuer...")
+            if not commun_db.exists(Sql.format("SELECT [strUniteCourt] FROM tblUnite WHERE [strUniteCourt]={:text}", interv.strunite)):
+                errors.append(prefix + "L'unité {} n'existe pas dans tblUnite".format(interv.strunite))
+
+        if not interv.dtmIntervention:
+            errors.append(prefix + "Erreur : date d'intervention")
+
+        if not interv.dtmDureeIntervention or interv.dtmDureeIntervention == date_zero:
+            errors.append(prefix + "Durée d'intervention nulle")
+
+    # *** 6- Interruption pour corection manuelle des données (si nécessaire)
+    if errors:
+        logging.error("<!> Des erreurs ont été détectées dans les données à importer. <!>")
+        for msg in errors:
+            logging.error(msg)
+        logger.info("# Annulation de l'import")
+        return
+    else:
+        logging.info("Aucune erreur n'a été détectée dans les données.")
+        if not no_prompt:
+            input("Appuyez sur Entée pour continuer...")
 
 
     # ########## MISE A JOUR DE LA BASE DE DONNEES ANALYTIQUE ##########