agrhum_traitement.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. '''
  2. '''
  3. from core.pde import AgrhumDb , FraisDeplacement, HeureSupp
  4. import logging
  5. import sys
  6. import json
  7. from datetime import datetime
  8. from path import Path # @UnusedImport
  9. from core import logconf
  10. from core.model import Sql
  11. #logger = logging.getLogger("ctrl2analytique")
  12. #logconf.start("ctrl2analytique", logging.DEBUG)
  13. # # POUR TESTER, d�commenter les lignes suivantes
  14. # > Lancer le script /resources/test_ctrl2analytique.py pour reinitialiser les donn�es de la base de test
  15. ##-----------------------------------------------
  16. AgrhumDb._path = Path(r"\\h2o\local\4-transversal\BDD\mdb_test\BDD_ParcRH.mdb")
  17. #CommunDb._path = Path(r"\\h2o\local\4-transversal\BDD\mdb_test\Commun_Data.mdb")
  18. #logger.handlers = [h for h in logger.handlers if (type(h) == logging.StreamHandler)]
  19. #logger.warning("<<<<<<<<<<<<<< Mode TEST >>>>>>>>>>>>>>>>>")
  20. ##-----------------------------------------------
  21. print ("La recherche du python de l'amazonie commence ici, pas encore l'anaconda :")
  22. agrhum_db = AgrhumDb(autocommit=False)
  23. CodeAgent = "T9"
  24. MoisRH = 12
  25. AnneeRH = 2017
  26. #Verification dans la table tbl_suiviRH suivi si la ligne agent mois annnée existe
  27. #Verfification si etat != de Importé ou si valide = True : demande confirmation pour retraitement
  28. #Si retraitement on suprime des tables cibles tbl_formHS et tbl_formDep
  29. #Parcours de tbl_importRH transfo donnée et écriture dans tbl_formdep
  30. #Parcours de tbl_importRH transfo donnée et écriture dans tbl_formHS
  31. def main():
  32. suivi = agrhum_db.first("select * from tbl_suiviRH where codeagent ='{}' and moisrh = {} and anneerh = {}".format(CodeAgent, MoisRH,AnneeRH))
  33. if not suivi:
  34. print("suivi manquant")
  35. return
  36. #if input("Voulez vous retarite ces donnees(o/n)") != "o" :
  37. # print("Annulation")
  38. # return
  39. agrhum_db.execute("delete * from tbl_formDep where codeagent ='{}' and moisrh = {} and anneerh = {}".format(CodeAgent, MoisRH,AnneeRH))
  40. agrhum_db.execute("delete * from tbl_formHS where codeagent ='{}' and moisrh = {} and anneerh = {}".format(CodeAgent, MoisRH,AnneeRH))
  41. data = agrhum_db.read_all("select * from tbl_importrh where codeagent ='{}' and Month([daterh]) = {} and Year([daterh]) = {} order by DateRH".format(CodeAgent, MoisRH,AnneeRH))
  42. sql = Sql.format("""SELECT
  43. tbl_baremes.NomBareme,
  44. tbl_baremes.BorneInf,
  45. tbl_baremes.BorneSup,
  46. tbl_baremes.Valeur,
  47. tbl_PeriodeBareme.DateInf,
  48. tbl_PeriodeBareme.DateSup
  49. FROM tbl_baremes
  50. INNER JOIN tbl_PeriodeBareme ON (tbl_baremes.PeriodeValidite = tbl_PeriodeBareme.CodePeriode) AND (tbl_baremes.NomBareme = tbl_PeriodeBareme.NomBareme)
  51. WHERE tbl_baremes.NomBareme = 'Heures de route'
  52. AND tbl_PeriodeBareme.DateInf <= {:date}
  53. AND (tbl_PeriodeBareme.DateSup is null or tbl_PeriodeBareme.DateSup > {:date}) """, datetime(AnneeRH,MoisRH,1), datetime(AnneeRH,MoisRH,1))
  54. baremes = agrhum_db.read_all(sql)
  55. index = {}
  56. indexHS ={}
  57. for el in data :
  58. if not el.DateRH.day in index :
  59. fraisdep = FraisDeplacement()
  60. fraisdep.IDSuivi = suivi.IDSuivi
  61. fraisdep.AnneeRH = AnneeRH
  62. fraisdep.CodeAgent = CodeAgent
  63. fraisdep.MoisRH = MoisRH
  64. fraisdep.JourRH = el.DateRH.day
  65. fraisdep.Depart = el.Depart
  66. # mettre a jour le codedepart
  67. fraisdep.Itineraire = " - ".join([loc for loc in (fraisdep.Depart, el.Localisation) if loc])
  68. fraisdep.Distance1_perso = int(el.DistanceTranche1) if el.VehiculePersoTranche1 == "True" else 0
  69. fraisdep.Distance2_perso = int(el.DistanceTranche2) if el.VehiculePersoTranche2 == "True" else 0
  70. fraisdep.Distance2_service = int(el.DistanceTranche2) if not el.VehiculePersoTranche2 == "True" else 0
  71. fraisdep.Repas = int(el.Repas)
  72. index[el.DateRH.day] = fraisdep
  73. else :
  74. index[el.DateRH.day].Itineraire += " - " + el.Localisation
  75. index[el.DateRH.day].Distance1_perso += int(el.DistanceTranche1) if el.VehiculePersoTranche1 == "True" else 0
  76. index[el.DateRH.day].Distance2_perso += int(el.DistanceTranche2) if el.VehiculePersoTranche2 == "True" else 0
  77. index[el.DateRH.day].Distance2_service += int(el.DistanceTranche2) if not el.VehiculePersoTranche2 == "True" else 0
  78. index[el.DateRH.day].Repas += int(el.Repas)
  79. for fraisdep in index.values() :
  80. distance2 = (fraisdep.Distance2_perso + fraisdep.Distance2_service)
  81. fraisdep.HeuresDep = next((bareme.Valeur for bareme in baremes if bareme.BorneInf <= distance2 and bareme.BorneSup > distance2))
  82. fraisdep.HeuresDepNuit = next((bareme.Valeur for bareme in baremes if bareme.BorneInf <= fraisdep.Distance1_perso and bareme.BorneSup > fraisdep.Distance1_perso))
  83. sqlexec = Sql.format("""INSERT INTO tbl_FormDep ( IDSuivi,
  84. CodeAgent,
  85. JourRH,
  86. MoisRH,
  87. AnneeRH,
  88. Depart,
  89. Itineraire,
  90. Distance2_perso,
  91. Distance2_service,
  92. Distance1_perso,
  93. HeuresDep,
  94. HeuresDepNuit,
  95. Repas,
  96. Remarque,
  97. Valide,
  98. CreePar,
  99. CreeLe )
  100. VALUES ({fraisdep.IDSuivi},
  101. {fraisdep.CodeAgent:text},
  102. {fraisdep.JourRH},
  103. {fraisdep.MoisRH},
  104. {fraisdep.AnneeRH},
  105. {fraisdep.Depart:text},
  106. {fraisdep.Itineraire:text},
  107. {fraisdep.Distance2_perso},
  108. {fraisdep.Distance2_service},
  109. {fraisdep.Distance1_perso},
  110. {fraisdep.HeuresDep},
  111. {fraisdep.HeuresDepNuit},
  112. {fraisdep.Repas},
  113. {fraisdep.Remarque:text},
  114. {fraisdep.Valide},
  115. {fraisdep.CreePar:text},
  116. {fraisdep.CreeLe:date})""",fraisdep = fraisdep)
  117. agrhum_db.execute(sqlexec)
  118. indexHS ={}
  119. for el in data :
  120. if not el.DateRH.day in indexHS :
  121. hs = HeureSupp()
  122. hs.IDSuivi = suivi.IDSuivi
  123. hs.AnneeRH = AnneeRH
  124. hs.CodeAgent = CodeAgent
  125. hs.MoisRH = MoisRH
  126. hs.JourRH = el.DateRH.day
  127. hs.HeureSup1 = el.HeureSup1
  128. hs.HeuresDep = index[el.DateRH.day].HeuresDep
  129. hs.HeuresDepNuit = index[el.DateRH.day].HeuresDepNuit
  130. hs.HeureSupNuit = float( el.HeureSup2) + hs.HeuresDepNuit
  131. hs.HeureSupDim = el.HeureSupDimanche
  132. hs.HS_VHCanal = float(el.HeureSup1) + float(el.HeureSup2) + float(el.HeureSupDimanche) if el.strCategorieInterventionId == "07" else 0.0
  133. hs.HS_Chantier = float(el.HeureSup1) + float(el.HeureSup2) + float(el.HeureSupDimanche) if el.strCategorieInterventionId != "07" else 0.0
  134. indexHS[el.DateRH.day] = hs
  135. else :
  136. indexHS[el.DateRH.day].HeureSup1 += float( el.HeureSup1)
  137. indexHS[el.DateRH.day].HeureSupNuit += float( el.HeureSup2)
  138. indexHS[el.DateRH.day].HeureSupDim += float( el.HeureSupDimanche)
  139. indexHS[el.DateRH.day].HS_VHCanal += float(el.HeureSup1) + float(el.HeureSup2) + float(el.HeureSupDimanche) if el.strCategorieInterventionId == "07" else 0.0
  140. indexHS[el.DateRH.day].HS_Chantier += float(el.HeureSup1) + float(el.HeureSup2) + float(el.HeureSupDimanche) if el.strCategorieInterventionId != "07" else 0.0
  141. totalhs = 0.0
  142. totalhsmois = 0.0
  143. for hs in indexHS.values() :
  144. totalhs = hs.HeureSup1 + hs.HeureSupNuit + hs.HeureSupDim
  145. if totalhs + totalhsmois <= 14 :
  146. hs.HeureSup1Inf14 = totalhs
  147. elif totalhsmois >14 :
  148. hs.HeureSup1Sup14 = totalhs
  149. elif totalhs + totalhsmois > 14 :
  150. hs.HeureSup1Inf14 = 14.0 - totalhsmois
  151. hs.HeureSup1Sup14 = totalhs + totalhsmois -14.0
  152. totalhsmois += totalhs
  153. sqlexechs = Sql.format("""INSERT INTO tbl_FormHS ( IDSuivi, CodeAgent, JourRH, MoisRH, AnneeRH, HeureSup1, HeuresDep, HeuresDepNuit, [HeureSup1<=14], [HeureSup1>14], HeureSupNuit, HeureSupDim, HS_VHCanal, HS_Chantier, Valide, CreePar, CreeLe )
  154. VALUES ({hs.IDSuivi}, {hs.CodeAgent:text}, {hs.JourRH}, {hs.MoisRH}, {hs.AnneeRH}, {hs.HeureSup1}, {hs.HeuresDep}, {hs.HeuresDepNuit}, {hs.HeureSup1Inf14}, {hs.HeureSup1Sup14}, {hs.HeureSupNuit}, {hs.HeureSupDim}, {hs.HS_VHCanal}, {hs.HS_Chantier}, {hs.Valide}, {hs.CreePar:text}, {hs.CreeLe:date})
  155. """, hs = hs)
  156. agrhum_db.execute(sqlexechs)
  157. with open(Path(r"%temp%\test.json").expandvars(), "w+") as f:
  158. f.write(str(index))
  159. agrhum_db.commit()
  160. if __name__ == "__main__":
  161. main()