1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
| '************************************************************************************************************************
'********************************** SCRIPT VBS *****************************************************
'********************************** AJUSTEMENT DE STOCK PROD *****************************************************
'************************************************************************************************************************
'P_502_3.5_20130123_11400795.xml
'************************************************************************************************************************
'************************************************************************************************************************
'Variables Importantes ** A modifier selon souhait
Dossier = "" 'Dossier contenant les fichiers XML production
FicScan = "" 'Fichier historique des scans fichiers
ConFich = "" 'Fichier de Configuration
FichLog = "" 'Fichier Log Écart de Stock recensé
FileNum = "" 'Fichier stockage Numéros de Mobiles
FileMail = "" 'Fichier stockage Adresses Mails
'************************************************************************************************************************
'StartTime = Timer()
lastModif = 0
Compteur = 0
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Dim WshShell, WshProcessEnv
Dim value() 'Tableau de Matières
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshProcessEnv = WshShell.Environment("System")
'Créer/Initialiser une variable d'environnement
WshProcessEnv("ArretScriptAjustStock") = "1"
Path_ListeXML = "" 'Fichier intermédiaire du script. Présent dans Dossier
'Création d'un conteneur de type Dictionnaire
Set dico = CreateObject("Scripting.Dictionary")
'Création du modèle d'objet permettant de manipuler le système de fichiers Windows
Set fso = CreateObject("Scripting.FileSystemObject")
'Création d'une classe Matière pour rassembler les attributs
Class Matiere
Dim codeM,seuilM,descriptionM,quantiteM,uniteM
End Class
'********************************************************************************************************
'** Recherche Seuil d'alerte **
Set a = New RegExp
a.Pattern = "seuil_\d*" 'renvoit seuil_12010 par exemple
a.IgnoreCase = True
a.Global = True 'Toutes les occurences
Set f = fso.GetFile(ConFich)
'********************************************************************************************************
'Traitement
'Conditions d'arrêt du script
Do While (WshProcessEnv("ArretScriptAjustStock")="1")
'Scan du fichier de Configuration la première fois et ensuite à chaque changement de la date de dernière modification
If (DateDiff("s", f.DateLastModified, lastModif) <> 0) Or (lastModif=0) Then
'MsgBox("Date modifiée")
'Mise à jour de la variable de test
lastModif = f.DateLastModified
'** Ouverture en lecture du fichier de configuration et Récupération de son contenu **
Set FileConfig = fso.OpenTextFile(ConFich,ForReading,True)
FileConfig_data = FileConfig.ReadAll
FileConfig.Close
'Tableau contenant chaque ligne du fichier de configuration
arrLines = Split(FileConfig_data,vbCrLf)
'Parcours du contenu du fichier de Configuration
For i = 0 To UBound(arrLines)
'Recherche de seuils
Set seuilOK = a.Execute(arrLines(i))
'Si seuil_ présent au moins une fois
If (seuilOK.count<>0) Then
'** Création d'une nouvelle matière **
'Augmentation du tableau
Redim value(i) 'Un tableau dynamique doit être initialisé la première fois !
Set value(p) = new Matiere
'** Récupération du code Matière (codeM et clé du dictionnaire) **
tableau = Split(arrLines(i),"_")
valeur = tableau(1)
tableau1 = Split(valeur," ")
Code = tableau1(0)
'MsgBox(Vartype(Code))
value(p).codeM = Code
'** Récupération Seuil Matière Autorisé **
tabl = Split(arrLines(i),"=")
seuilAjustement = tabl(1)
tabl1 = Split(seuilAjustement," ")
seAjust=tabl1(1)
sAjust = Replace(seAjust,".",",")
seuilConf = Csng(sAjust)
'MsgBox(seuilConf)
value(p).seuilM = seuilConf
'** Récupération de l'intitulé matière **
table = Split(arrLines(i+1),"=")
descM = table(1)
table1 = Split(descM," ")
descMa=table1(1)
descMatiere = Replace(descMa,".",",")
value(p).descriptionM = descmatiere
'** Récupération de la taille de l'unité de stock **
tablea = Split(arrLines(i+2),"=")
qStd = tablea(1)
tablea1 = Split(qStd," ")
descMat=tablea1(1)
quantiteStd = Replace(descMat,".",",")
value(p).quantiteM = quantiteStd
'** Récuperation unité de poids **
tab3 = Split(arrLines(i+3),"=")
uniMatiere = tab3(1)
tab3b = Split(uniMatiere," ")
unitMatiere=tab3b(1)
value(p).uniteM = unitMatiere
'Ajout au dictionnaire si le code matière n'existe pas déjà
If Not dico.exists(value(p).codeM) Then
dico.add value(p).codeM, value(p)
End If
'Incrémentation du compteur du tableau
p=p+1
End If
Next
Else
'MsgBox("Pas de Modification du fichier")
p=0
End If
'EndTime = Timer()
'MsgBox("Seconds to 2 decimal places: " & FormatNumber(EndTime - StartTime, 2))
'** Vérification de l'existence du dossier **
If (fso.FolderExists(Dossier) = False) Then
Erreur = MsgBox("Le dossier n'existe pas !" )
Wscript.Quit
Else
'MsgBox("Le Dossier " & Dossier & " existe !")
End If
'------------------------------------------------------------------------------------------------------------------------------
'CRÉATION FICHIER DE STOCKAGE DES FICHIERS XML NON SCANNÉS
If Not fso.fileExists(Path_ListeXML) Then
Set OutPut = fso.CreateTextFile(Path_ListeXML,True)
OutPut.Close
Set OutPut = Nothing
'MsgBox("Création du fichier de liste des fichiers XML")
Else
'MsgBox("Fichier listant les fichiers xml avec _3.7_ et _3.9_ non scannés déjà existant.")
End If
'-------------------------------------------------------------------------------------------------------------------------------
'RECHERCHE DES FICHIERS CONTENANT _3.7_ et _3.9_
ListeFichierXML(Dossier)
'------------------------------------------------------------------------------------------------------------------------------
'GESTION DU FICHIER HISTORIQUE
RajoutHistorique()
'-------------------------------------------------------------------------------------------------------------------------------
'PARCOURS DES XML REFERENCES DANS LE FICHIER DE STOCKAGE
'Ouverture du fichier listant les fichiers 3.7 et 3.9
Set Fichier = fso.OpenTextFile(Path_ListeXML,ForReading,True)
'Création d'un objet permettant d'ouvrir, lire et créer un fichier XML
Set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false" ' permet de charger entièrement le document en mémoire avant le traitement
'Motif de test
Set objRegex = new RegExp
objRegex.Pattern = "(.w*)*\_3.7_(.w*)*\.xml"
objRegex.IgnoreCase = True
Set oFl = fso.GetFile(Path_ListeXML)
'Vérification de la taille du fichier listant les fichiers XML
If (oFl.Size <> "0") Then
Do While Fichier.AtEndOfStream <> True
'MsgBox("Parcours des fichiers non déjà scannés")
'Lecture de la ligne et ouverture du fichier XML correspondant
Fichier_Line = Fichier.ReadLine
xmlDoc.load(fso.BuildPath(Dossier, Fichier_Line))
'Test avec le motif pour spécifier l'arborescence XML relative
If (objRegex.Test(Fichier_Line) = True) Then
ArboXML = "/soapenv:Envelope/soapenv:Body/issueInventory/inventoryIssue"
AjustType = "moins"
Else
ArboXML = "/soapenv:Envelope/soapenv:Body/receiveInventory/inventoryReceipt"
AjustType = "plus"
End If
Set oElement = xmlDoc.documentElement
'Vérification Taille fichier XML
If Not oElement Is Nothing Then
'Sélection de la balise rmks
Set XmlAttrib = oElement.getElementsByTagName("rmks")
For Each x In XmlAttrib
'---------------------------------------------------------------------------------------------------------------
'RECHERCHE DE WEIGHT ADJ
If x.Text="WEIGHT ADJ" Then
'MsgBox("Le fichier " & Fichier_Line & " contient WEIGHT ADJ")
'-----------------------------------------------------------------------------------------------------------
'RECHERCHE DES VALEURS CLÉS FICHIERS XML
For Each inventory In xmlDoc.selectNodes(ArboXML)
'MsgBox("Récupération des valeurs du fichier XML")
'** Code de la Matière **
codeMatiereXML = inventory.selectSingleNode("ptPart").Text
'Msgbox(VarType(codeMatiereXML))
'** Quantité XML **
quantiteXML = inventory.selectSingleNode("lotserialQty").Text
qXML = Replace(quantiteXML,".",",")
'** Zone **
locationXML = inventory.selectSingleNode("location").Text
'** Numéro de lot **
lotXML = inventory.selectSingleNode("lotserial").Text
'---------------------------------------------------------------------------------------------------
'Vérification de l'existence du code matière avec le dictionnaire
If Not dico.exists(codeMatiereXML) Then
'MsgBox("non trouvée")
'** Matière non trouvée **
'** Corps du mail **
strBody = "<body>"
strBody = strBody & "<table border=1 >"
strBody = strBody & "<tr><td align=center colspan=2 bgcolor= #00008B><font color=#ffffff>Genzyme Lyon </font></td></tr>"
strBody = strBody & "<tr><td align=center colspan=2><b>Alerte MES</b> </td></tr>"
strBody = strBody & "<tr><td align=center colspan=2><b>Code Matière</b></td></tr>"
strBody = strBody & "<tr><td align=center colspan=2>" & Now & "</td></tr>"
strBody = strBody & "<tr><td bgcolor=#778899>Problème</td> <td>Matiere non définie dans les valeurs d'ajustement : " & codeMatiereXML & "</td> </tr>"
strBody = strBody & "<tr><td>Nom du fichier concerné</td></td> " & Fichier_Line & "</td></tr>"
strBody = strBody & "</table>"
strBody = strBody & "</body>"
'MsgBox("Matière non défini")
'** Appel fonction Mail **
Call SendMail(strBody, "Rapport Script")
'MsgBox("Mail envoyé")
'** Envoi SMS **
Contenu = "Matière%20non%20présente%20dans%20le%20fichier%20de%20configuration:%20" & codeMatiereXML & "%20Fichier%20" & Fichier_Line
Call SendSMS(Contenu)
'MsgBox("SMS envoyé")
Set corps = fso.OpenTextFile(FichLog,ForAppending)
Message = Fichier_Line & ";" & codeMatiereXML & ";N/A;N/A;N/A;N/A;Matière non definie" & codeMatiereXML
corps.WriteLine Message
corps.Close
Else
'MsgBox("trouvée")
'Récupération de l'objet avec la clé code matière
Set var=dico.item(codeMatiereXML)
'** Matiere trouvée **
'** Cas Écart de stock **
If (Csng(qXML) > Csng(var.seuilM)) Then
'MsgBox("Écart de stock dans le fichier " & Fichier_Line)
'** Corps du mail **
strBody = "<body>"
strBody = strBody & "<table border=1 >"
strBody = strBody & "<tr><td align=center colspan=2 bgcolor= #00008B><font color=#ffffff>Genzyme Lyon </font></td></tr>"
strBody = strBody & "<tr><td align=center colspan=2><b>Alerte MES</b> </td></tr>"
strBody = strBody & "<tr><td align=center colspan=2><b>Écart de stock detecte " & AjustType & "</b></td></tr>"
strBody = strBody & "<tr><td align=center colspan=2>" & Now & "</td></tr>"
strBody = strBody & "<tr><td bgcolor=#778899>Matiere/Lot :</td> <td>" & codeMatiereXML & "/" & lotXML & "(" & var.descriptionM & ")" & "</td> </tr>"
strBody = strBody & "<tr><td bgcolor=#D3D3D3>Quantite ajustee :</td> <td>" & qXML & var.uniteM & " (Seuil d'alerte : " & var.seuilM & unitMatiere & ")</td></tr>"
strBody = strBody & "<tr><td bgcolor=#778899>Quantite standard U.Stk :</td> <td>" & var.quantiteM & var.uniteM & "</td></tr>"
strBody = strBody & "<tr><td bgcolor=#D3D3D3>Zone :</td> <td>" & locationXML & "</td></tr>"
strBody = strBody & "<tr><td>Nom du fichier concerné</td></td> " & Fichier_Line & "</td></tr>"
strBody = strBody & "</table>"
strBody = strBody & "</body>"
'** Appel fonction Mail **
Call SendMail(strBody, "Rapport Script")
'MsgBox("Mail envoyé")
'** Envoi SMS **
Contenu = "Écart%20de%20stock%20détecté%20" & AjustType & "%20*Matiere%2FLot%20:" & codeMatiereXML & "%2F" & lotXML & "%28" & var.descriptionM & "%29*Qte%20ajustée%20:%20" & qXML & var.uniteM & "%28Seuil%20Alerte%20:%20" & var.seuilM & var.uniteM & "%29*Qte%20Std%20:%20" & var.quantiteM & var.uniteM & "%20Fichier%20" & Fichier_Line
Call SendSMS(Contenu)
'MsgBox("SMS envoyé")
'**** Débogage SMS *****
Set Test = fso.OpenTextFile("",ForWriting,True)
Donnees ="Écart de stock détecté " & AjustType & " *Matiere/Lot :" & codeMatiereXML & "/" & lotXML & "(" & var.descriptionM & ")*Qte ajustée : " & qXML & var.uniteM & "(Seuil Alerte : " & var.seuilM & var.uniteM & ")*Qte Std : " & var.quantiteM & var.uniteM
Test.WriteLine Donnees
Test.Close
'***********************
'** HISTORISATION => Écriture dans fichier Log.csv **
Set corps = fso.OpenTextFile(FichLog,ForAppending)
Message = Fichier_Line & ";" & codeMatiereXML & ";" & lotXML & ";" & var.descriptionM & ";" & qXML & ";" & locationXML
corps.WriteLine Message
corps.Close
End If
End If
Next
Else
'MsgBox("Le Fichier ne contient pas WEIGHT ADJ")
End If
Next
Else
'MsgBox("Fichier XML vide")
End If
Loop
Else
'MsgBox("Tous les fichiers ont déjà été traités précedemment")
End If
Wscript.Sleep 3000
Loop
'-------------------------------------------------------------------------------------------------------------------------------
'LIBÉRATION DES OBJETS
Set Fichier = Nothing
Set xmlDoc = Nothing
Set oElement = Nothing
Set XmlAttrib = Nothing
Set ws= Nothing
Set IE = Nothing
Set strbody = Nothing
Set XMLFile = Nothing
Set TempFile = Nothing
Set history = Nothing
Set corps = Nothing
Set tab = Nothing
Set tab1 = Nothing
Set tab2 = Nothing
Set tab3 = Nothing
Set a = Nothing
Set occurrence = Nothing
Set arrlines = Nothing
Set FileConfig = Nothing
Set OutPut= Nothing
Set Folder = Nothing
Set objRegex = Nothing
Set fso = Nothing
Set seuilOK = Nothing
'-----------------------------------------------------------------------------------------------------------------------------------
'FONCTION ENVOI MAIL
Function SendMail(strBody, strTitre)
Set objMail = CreateObject("CDO.Message")
Set objConfig = CreateObject("CDO.configuration")
Set objFields = objConfig.Fields
strComputer = ""
Set fMail = fso.OpenTextFile(FileMail,ForReading,True)
'Msgbox("Ping de : " & strComputer)
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}!\\").ExecQuery("select * from Win32_PingStatus where address = '" & strComputer & "'")
For Each objStatus in objPing
If objStatus.Statuscode = 0 Then
Do While fMail.AtEndOfStream <> True
fMail_Line = fMail.ReadLine
'MsgBox(fMail_Line)
With objFields
.Item("http://schemas.microsoft.com/cdo/configuration/SendUsing")= 2 'Définit le type d'envoi en SMTP
'Serveur Mail Interne
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")= ""
.Item("http://schemas.microsoft.com/cdo/configuration/SMTPServerPort")= 25
.Update
End With
With objMail
Set .Configuration = objConfig
.To = fMail_Line
.Cc = strCc
.Bcc = strBcc
'.AddAttachment("")
.From = "Script.AjustementStock@genzyme.com"
.Subject = strTitre
.HTMLBody = strBody
.Send
End With
Loop
End If
Next
fMail.Close
End Function
'-----------------------------------------------------------------------------------------------------------------------------------
'FONCTION ENVOI SMS
Function SendSMS(Contenu)
Set IE = Wscript.CreateObject("InternetExplorer.Application")
IE.Visible = False
Set fTel = fso.OpenTextFile(FileNum,ForReading,True)
strComputer = "10.81.76.15"
'Msgbox("Ping de : " & strComputer)
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}!\\").ExecQuery("select * from Win32_PingStatus where address = '" & strComputer & "'")
For Each objStatus in objPing
If objStatus.Statuscode = 0 Then
Do While fTel.AtEndOfStream <> True
fTel_Line = fTel.ReadLine
IE.Navigate "http://10.81.76.15/source/send_sms.php?from=admin&lunghezza=1836&nc=&nphone=" & fTel_Line & "&remLen1=1820&send=send&testo=" & Contenu
Wscript.Sleep 500
Loop
End If
Next
fTel.Close
IE.Quit
End Function
'----------------------------------------------------------------------------------------------------------------------------------
'FONCTION LISTANT XML _3.7_ ET _3.9_
Private Sub ListeFichierXML(FolderPath)
'On capte le dossier passé en paramètre
Set Folder = fso.Getfolder(FolderPath)
' Création des deux motifs pour trouver les titres contenant 3.7 et 3.9
Set objRegex = new RegExp
objRegex.Pattern = "(.w*)*\_3.7_(.w*)*\.xml"
objRegex.IgnoreCase = True
Set Reg = new RegExp
Reg.Pattern = "(.w*)*\_3.9_(.w*)*\.xml"
Reg.IgnoreCase = True
'Ouverture du fichier pour stocker les correspondances avec les motifs
Set OutPut = fso.OpenTextFile(Path_ListeXML,ForWriting,True) '8 si on veut pas écrasé
'Test des fichiers dans le dossier
For Each Fichier In Folder.Files
If (fso.GetExtensionName(Fichier.Path) = "xml") Then
If (objRegex.Test(Fichier.Name) = True) Or (Reg.Test(Fichier.Name) = True) Then
OutPut.WriteLine (Fichier.Name)
End If
End If
Next
OutPut.Close
End Sub
'----------------------------------------------------------------------------------------------------------------------------------
'FONCTION RAJOUT NOUVEAUX FICHIERS A L'HISTORIQUE
Function RajoutHistorique()
'Ouverture et récupération des valeurs du fichiers historique
Set history = fso.OpenTextFile(FicScan, ForReading, True)
history_data = history.ReadAll
history.Close
'Ouverture du fichier listant les bons fichiers XML et d'un fichier temporaire
Set XMLFile = fso.OpenTextFile(Path_ListeXML, ForReading, True)
Set TempFile = fso.OpenTextFile("C:\Script\Temp.txt", ForWriting, True)
'Comparaison des nouveaux fichiers avec la base et rajout si non présent
Do Until XMLFile.AtEndOfStream
XMLFile_Line = XMLFile.ReadLine
If InStr(history_data, XMLFile_Line) = 0 Then
Set history = fso.OpenTextFile(FicScan, ForAppending, True)
history.Write XMLFile_Line & vbCRLF
history.Close
TempFile.Write XMLFile_Line & vbCRLF
'MsgBox("Mise à jour du fichier historique.log")
Else
'MsgBox("Le Fichier " & XMLFile_Line & " a déjà été scanné auparavant")
End If
Loop
'Fermeture du fichier
XMLFile.Close
fso.DeleteFile(Path_ListeXML)
TempFile.Close
fso.MoveFile "" , Path_ListeXML
End Function
'MsgBox("Fin du traitement")
'EndTime = Timer()
'MsgBox("Seconds to 2 decimal places: " & FormatNumber(EndTime - StartTime, 2)) |
Partager