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
| '************************************************************************************************************************
'********************************** SCRIPT VBS *****************************************************
'********************************** AJUSTEMENT DE STOCK PROD *****************************************************
'************************************************************************************************************************
'P_502_3.5_20130123_11400795.xml
'************************************************************************************************************************
'************************************************************************************************************************
StartTime = Timer()
'Variables Importantes ** A modifier selon souhait
Dossier = "C:\Users\moike01\Desktop\Test_P&N" 'Dossier contenant les fichiers XML production
FicScan = "C:\Users\moike01\Desktop\2\historique.log" 'Fichier historique des scans fichiers
ConFich = "P:\Detection Ajustement Stock\FichierConfiguration.txt" 'Fichier de Configuration
FichLog = "C:\Users\moike01\Desktop\2\Log.csv" 'Fichier Log Écart de Stock recensé
AdEmail = "kevin.moine@genzyme.com"
FileNum = "P:\Detection Ajustement Stock\NumerosAjustStock.txt" 'Fichier stockage Numéros de Mobiles
Test = "P:\Detection Ajustement Stock\TachesEnCours.txt" 'Fichier vide servant à empêcher nouvelle éxécution de la tâche si en cours
'************************************************************************************************************************
'On Error Resume Next
Path_ListeXML = Dossier & "\ListeXML.txt" 'Fichier intermédiaire du script. Présent dans Dossier
Set fso = CreateObject("Scripting.FileSystemObject" )
'----------------------------------------------------------------------------------------------------------------------------------
'VÉRIFICATION DE L'EXISTENCE DU FICHIER POUR SAVOIR SI TACHE DEJA EN COURS
If Not fso.fileExists(Test) Then
Set Tache = fso.CreateTextFile(Test,True)
Tache.Close()
'MsgBox("Lancement du script")
'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
Set fso = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set history = fso.OpenTextFile(FicScan, ForReading, True)
history_data = history.ReadAll
history.Close
Set XMLFile = fso.OpenTextFile(Path_ListeXML, ForReading, True)
Set TempFile = fso.OpenTextFile(Dossier & "\Temp.txt", ForWriting, True)
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
XMLFile.Close
fso.DeleteFile(Path_ListeXML)
TempFile.Close
fso.MoveFile Dossier & "\Temp.txt" , Path_ListeXML
'-------------------------------------------------------------------------------------------------------------------------------
'PARCOURS DES XML REFERENCES DANS LE FICHIER DE STOCKAGE
Set Fichier = fso.OpenTextFile(Path_ListeXML,1,True)
Set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false" ' permet de charger entièrement le document en mémoire avant le traitement
Set objRegex = new RegExp
objRegex.Pattern = "(.w*)*\_3.7_(.w*)*\.xml"
objRegex.IgnoreCase = True
Set oFl = fso.GetFile(Path_ListeXML)
If (oFl.Size <> "0") Then
Do While Fichier.AtEndOfStream <> True
'MsgBox("Parcours des fichiers non déjà scannés")
Fichier_Line = Fichier.ReadLine
xmlDoc.load(fso.BuildPath(Dossier, Fichier_Line))
If (objRegex.Test(Fichier_Line) = True) Then
ArboXML = "/soapenv:Envelope/soapenv:Body/issueInventory/inventoryIssue"
AjustType = "-"
Else
ArboXML = "/soapenv:Envelope/soapenv:Body/receiveInventory/inventoryReceipt"
AjustType = "+"
End If
Set oElement = xmlDoc.documentElement
' Si le fichier n'est pas vide
If Not oElement Is Nothing Then
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
'** 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
'-------------------------------------------------------------------------------------------------------
'RECHERCHE VALEURS MATIÈRE A PARTIR DU FICHIER DE CONFIGURATION
Set FileConfig = fso.OpenTextFile(ConFich,ForReading,True)
FileConfig_data = FileConfig.ReadAll
FileConfig.Close
arrLines = Split(FileConfig_data,vbCrLf)
'** Recherche Seuil d'alerte **
Set a = New RegExp
a.Pattern = "seuil_" & codeMatiereXML
a.IgnoreCase = True
a.Global = False 'Renvoyer seulement la première occurrence
'Set occurrence = a.Execute(FileConfig_data)
If (a.Test(FileConfig_data) = False) Then
'** Seuil non trouvé **
'** Corps du mail **
strBody = "<body>"
strBody = strBody & "<img src='cid:x.jpg' height=103 width=175>"
strBody = strBody & "<table border=1 >"
strBody = strBody & "<tr><td align=center colspan=2 bgcolor= #00008B><font color=#ffffff>x</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 & "</table>"
strBody = strBody & "</body>"
'MsgBox("Matière non défini")
'** Appel fonction Mail **
Call SendMail(AdEmail, strBody, "Rapport Script")
'MsgBox("Mail envoyé")
'** Envoi SMS **
Contenu = "Matière%20non%20présente%20dans%20le%20fichier%20de%20configuration:%20" & codeMatiereXML
Call SendSMS(Contenu)
'MsgBox("SMS envoyé")
Else
'** Seuil trouvé **
'** Parcours Fichier de Configuration **
For i = 0 To UBound(arrLines)
Set seuilOK = a.Execute(arrLines(i))
If (seuilOK.count<>0) Then
'** Récupération Seuil Matière Autorisé **
tab = Split(arrLines(i),"=")
seuilAjustement = tab(1)
sAjust = Replace(seuilAjustement,".",",")
seuilConf = Csng(sAjust)
qteXML = Csng(qXML)
'** Cas Écart de stock **
If (qteXML > seuilConf) Then
'MsgBox("Écart de stock dans le fichier " & Fichier_Line)
'** Récupération de l'intitulé matière **
tab1 = Split(arrLines(i+1),"=")
descM = tab1(1)
descMatiere = Replace(descM,".",",")
'** Récupération de la taille de l'unité de stock **
tab2 = Split(arrLines(i+2),"=")
qStd = tab2(1)
quantiteStd = Replace(qStd,".",",")
'** Récuperation unité de poids **
tab3 = Split(arrLines(i+3),"=")
unitMatiere = tab3(1)
'** Corps du mail **
strBody = "<body>"
strBody = strBody & "<img src='cid:x.jpg' height=103 width=175>"
strBody = strBody & "<table border=1 >"
strBody = strBody & "<tr><td align=center colspan=2 bgcolor= #00008B><font color=#ffffff>xx</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 & "(" & descMatiere & ")" & "</td> </tr>"
strBody = strBody & "<tr><td bgcolor=#D3D3D3>Quantite ajustee :</td> <td>" & qteXML & unitMatiere & " (Seuil d'alerte : " & seuilConf & unitMatiere & ")</td></tr>"
strBody = strBody & "<tr><td bgcolor=#778899>Quantite standard U.Stk :</td> <td>" & quantiteStd & unitMatiere & "</td></tr>"
strBody = strBody & "<tr><td bgcolor=#D3D3D3>Zone :</td> <td>" & locationXML & "</td></tr>"
strBody = strBody & "</table>"
strBody = strBody & "</body>"
'** Appel fonction Mail **
Call SendMail(AdEmail, strBody, "Rapport Script")
'MsgBox("Mail envoyé")
'** Envoi SMS **
Contenu = "Écart%20de%20stock%20détecté%20" & AjustType & "%20*Matiere%2FLot%20:" & codeMatiereXML & "%2F" & lotXML & "%28" & descMatiere & "%29*Qte%20ajustée%20:%20" & qteXML & unitMatiere & "%28Seuil Alerte%20:%20" & seuilConf & unitMatiere & "%29*Qte Std%20:%20" & quantiteStd & unitMatiere
Call SendSMS(Contenu)
'MsgBox("SMS envoyé")
'** HISTORISATION => Écriture dans fichier Log.csv **
Set corps = fso.OpenTextFile(FichLog,ForAppending)
Message = Fichier_Line & ";" & codeMatiereXML & ";" & lotXML & ";" & descMatiere & ";" & qteXML & ";" & locationXML
corps.WriteLine Message
corps.Close
End If
End If
Next
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
fso.DeleteFile(Test)
Else
MsgBox("Tâches déjà en cours")
Wscript.Quit
End If
'-------------------------------------------------------------------------------------------------------------------------------
'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(strTo, strBody, strTitre)
Set objMail = CreateObject("CDO.Message")
Set objConfig = CreateObject("CDO.configuration")
Set objFields = objConfig.Fields
strComputer = "IP_Mail"
'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
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")= "IP_Mail"
.Item("http://schemas.microsoft.com/cdo/configuration/SMTPServerPort")= 25
.Update
End With
With objMail
Set .Configuration = objConfig
.To = strTo
.Cc = strCc
.Bcc = strBcc
.AddAttachment("C:\Users\moike01\Desktop\Genzyme_logo.jpg")
.From = "Script.AjustementStock@genzyme.com"
.Subject = strTitre
.HTMLBody = strBody
.Send
End With
End If
Next
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 = "IP_SMS"
'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
'Solution déboguage => Écriture Heure passage boucle
'Set fDebug = fso.OpenTextFile("P:\Detection Ajustement Stock\Debug.txt",ForAppending,True)
'fDebug.WriteLine Now
'fDebug.Close
IE.Navigate "http://IP_SMS/source/send_sms.php?from=admin&lunghezza=1836&nc=&nphone=" & fTel_Line & "&remLen1=1820&send=send&testo=" & Contenu
Wscript.Sleep 100
Loop
End If
Next
fTel.Close
IE.Quit
End Function
Private Sub ListeFichierXML(FolderPath)
Set Folder = fso.Getfolder(FolderPath)
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
Set OutPut = fso.OpenTextFile(Path_ListeXML,2,True) '8 si on veut pas écrasé
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
EndTime = Timer()
MsgBox("Seconds to 2 decimal places: " & FormatNumber(EndTime - StartTime, 2))
'MsgBox("Fin du traitement") |
Partager