8
0
2024-01-24 16:42:38 +01:00

509 lines
21 KiB
Plaintext

' VB Script Document
'
' Export Script for WinLine documents.
' Configuration has to be done in the additional fields in the "WinLine Mandantenstamm"
'
' Digital Data
' Ludwig-Rinn-Straße 16
' 35452 Heuchelheim
' Tel.: 0641 / 202360
' E-Mail: info(at)didalog.de
'
' Version Number: 1.0.0.0
' Version Date: 23.04.2019
On Error Resume Next
'#-----------------------------------------------------------------------------------------------------#
'############################################ set variables ############################################
'#-----------------------------------------------------------------------------------------------------#
DIM DateTime, SplitChar, ReplaceSpecialChar, DocExtension, DocVersion, DocVersionSeparator, DocType, MandatorShortName, DocAccountNumber, DocAANGExportPath, DocAABExportPath, DocALSExportPath, DocAREExportPath
DIM FileSystemObject, Shell, DocAANGConvention, DocAABConvention, DocALSConvention, DocAREConvention, DocAANGNumber, DocAABNumber, DocALSNumber, DocARENumber, DocTargetPath, DocTargetFileName, FullPath
SET FileSystemObject = CreateObject("Scripting.FileSystemObject")
SET Shell = CreateObject("WScript.Shell")
'Standard / default variables.
DateTime = now
SplitChar = "|"
ReplaceSpecialChar = " "
DocExtension = ".pdf"
DocExportCheck = "true"
DocExportCheckTry = 0
DocExportCheckMaxTrys = 10
DocVersion = 1
DocVersionSeparator = "~"
DocType = Value (25,139) 'WinLine Belegstufe
RecordNumber = Value (25,22) 'WinLine Laufnummer
PostingType = Value (357,6) 'WinLine Buchungsart (Debitorisch = 1 / Kreditorisch = 2)
MandatorShortName = TRIM(CSTR(Value (1,200))) 'WinLine Mandantenkürzel (individual Feld)
DocAccountNumber = TRIM(CSTR(Value (0,30))) 'WinLine Kontonummer (Debitor / Kreditor)
DocAANGNumber = TRIM(CSTR(Value (0,34))) 'WinLine Angebotsnummer
DocAABNumber = TRIM(CSTR(Value (0,35))) 'WinLine Auftragsnummer
DocALSNumber = TRIM(CSTR(Value (0,36))) 'WinLine Lieferscheinnummer
DocARENumber = TRIM(CSTR(Value (0,37))) 'WinLine Rechnungsnummer
DocAANFNumber = TRIM(CSTR(Value (0,34))) 'WinLine Anfragennummer
DocABENumber = TRIM(CSTR(Value (0,35))) 'WinLine Bestellnummer
DocAANGExportPath = TRIM(CSTR(Split(Value (1,201),SplitChar)(0))) 'WinLine Exportpfad für Angebote (individual Feld)
DocAABExportPath = TRIM(CSTR(Split(Value (1,202),SplitChar)(0))) 'WinLine Exportpfad für Aufträge (individual Feld)
DocALSExportPath = TRIM(CSTR(Split(Value (1,203),SplitChar)(0))) 'WinLine Exportpfad für Lieferscheine (individual Feld)
DocAREExportPath = TRIM(CSTR(Split(Value (1,204),SplitChar)(0))) 'WinLine Exportpfad für Rechnungen (individual Feld)
DocAANFExportPath = TRIM(CSTR(Split(Value (1,205),SplitChar)(0))) 'WinLine Exportpfad für Anfragen (individual Feld)
DocABEExportPath = TRIM(CSTR(Split(Value (1,206),SplitChar)(0))) 'WinLine Exportpfad für Bestellungen (individual Feld)
DocAANGConvention = TRIM(CSTR(Split(Value (1,201),SplitChar)(1))) 'WinLine Benennungsschema für Angebote (individual Feld)
DocAABConvention = TRIM(CSTR(Split(Value (1,202),SplitChar)(1))) 'WinLine Benennungsschema für Aufträge (individual Feld)
DocALSConvention = TRIM(CSTR(Split(Value (1,203),SplitChar)(1))) 'WinLine Benennungsschema für Lieferscheine (individual Feld)
DocAREConvention = TRIM(CSTR(Split(Value (1,204),SplitChar)(1))) 'WinLine Benennungsschema für Rechnungen (individual Feld)
DocAANFConvention = TRIM(CSTR(Split(Value (1,205),SplitChar)(1))) 'WinLine Benennungsschema für Anfragen (individual Feld)
DocABEConvention = TRIM(CSTR(Split(Value (1,206),SplitChar)(1))) 'WinLine Benennungsschema für Bestellungen (individual Feld)
'Costumer variables - dont forget to add the replace function in the lower preparing part.
'VarName = mesoField 'Description
'#-----------------------------------------------------------------------------------------------------#
'########################################### preparing part ############################################
'#-----------------------------------------------------------------------------------------------------#
'Retrive Document Type (1 = Angebot/Anfrage). Depending on it, the Targetpath and Targetfilename.
IF (DocType = 1) THEN
'If document is for a debtor.
IF (PostingType = 1) THEN
'If ExportPath has content
IF NOT (IsEmpty(DocAANGExportPath) Or IsNull(DocAANGExportPath) Or DocAANGExportPath = "") THEN
DocTargetPath = DocAANGExportPath
'If ExportPath has no content, fallback on the general WinLine network path.
ELSE
DocTargetPath = "%NETWORK%"
END IF
'If Convention has content.
IF NOT (IsEmpty(DocAANGConvention) Or IsNull(DocAANGConvention) Or DocAANGConvention = "") THEN
DocTargetFileName = DocAANGConvention
'If Convention AND ExportPath had both no content.
ELSEIF (DocTargetPath = "%NETWORK%") THEN
DocTargetFileName = Nothing
'If ExportPath has content, but Convention hasnt.
ELSE
MSGBOX("Ungültige Export Dateinamens Einstellung für Ausgangsangebote. FailSafe ist AANG_" & DocAccountNumber & ((year(DateTime)*100 + month(DateTime))*100 + day(DateTime))*10000 + hour(DateTime)*100 + minute(DateTime) & ".pdf" & "!")
DocTargetFileName = "AANG_" & DocAccountNumber & ((year(DateTime)*100 + month(DateTime))*100 + day(DateTime))*10000 + hour(DateTime)*100 + minute(DateTime)
END IF
'If document is for a creditor.
ELSEIF (PostingType = 2) THEN
'If ExportPath has content
IF NOT (IsEmpty(DocAANFExportPath) Or IsNull(DocAANFExportPath) Or DocAANFExportPath = "") THEN
DocTargetPath = DocAANFExportPath
'If ExportPath has no content, fallback on the general WinLine network path.
ELSE
DocTargetPath = "%NETWORK%"
END IF
'If Convention has content.
IF NOT (IsEmpty(DocAANFConvention) Or IsNull(DocAANFConvention) Or DocAANFConvention = "") THEN
DocTargetFileName = DocAANFConvention
'If Convention AND ExportPath had both no content.
ELSEIF (DocTargetPath = "%NETWORK%") THEN
DocTargetFileName = Nothing
'If ExportPath has content, but Convention hasnt.
ELSE
MSGBOX("Ungültige Export Dateinamens Einstellung für Ausgangsanfragen. FailSafe ist AANF_" & DocAccountNumber & ((year(DateTime)*100 + month(DateTime))*100 + day(DateTime))*10000 + hour(DateTime)*100 + minute(DateTime) & ".pdf" & "!")
DocTargetFileName = "AANF_" & DocAccountNumber & ((year(DateTime)*100 + month(DateTime))*100 + day(DateTime))*10000 + hour(DateTime)*100 + minute(DateTime)
END IF
'If document is not for a debtor or creditor.
ELSE
MSGBOX("Undefinierte Export Einstellung! Abbruch des Exports.")
END IF
'Retrive Document Type (2 = Auftrag/Bestellung). Depending on it, the Targetpath and Targetfilename.
ELSEIF (DocType = 2) THEN
'If document is for a debtor.
IF (PostingType = 1) THEN
'If ExportPath has content
IF NOT (IsEmpty(DocAABExportPath) Or IsNull(DocAABExportPath) Or DocAABExportPath = "") THEN
DocTargetPath = DocAABExportPath
'If ExportPath has no content, try to exit. But exit wont work, so fallback on the general WinLine network path.
ELSE
DocTargetPath = "%NETWORK%"
END IF
'If Convention has content.
IF NOT (IsEmpty(DocAABConvention) Or IsNull(DocAABConvention) Or DocAABConvention = "") THEN
DocTargetFileName = DocAABConvention
'If Convention AND ExportPath had both no content.
ELSEIF (DocTargetPath = "%NETWORK%") THEN
DocTargetFileName = Nothing
'If ExportPath has content, but Convention hasnt.
ELSE
MSGBOX("Ungültige Export Dateinamens Einstellung für Ausgangsauftragsbestätgrechnungen. FailSafe ist AAB_" & DocAccountNumber & ((year(DateTime)*100 + month(DateTime))*100 + day(DateTime))*10000 + hour(DateTime)*100 + minute(DateTime) & ".pdf" & "!")
DocTargetFileName = "AAB_" & DocAccountNumber & ((year(DateTime)*100 + month(DateTime))*100 + day(DateTime))*10000 + hour(DateTime)*100 + minute(DateTime)
END IF
'If document is for a creditor.
ELSEIF (PostingType = 2) THEN
'If ExportPath has content
IF NOT (IsEmpty(DocABEExportPath) Or IsNull(DocABEExportPath) Or DocABEExportPath = "") THEN
DocTargetPath = DocABEExportPath
'If ExportPath has no content, fallback on the general WinLine network path.
ELSE
DocTargetPath = "%NETWORK%"
END IF
'If Convention has content.
IF NOT (IsEmpty(DocABEConvention) Or IsNull(DocABEConvention) Or DocABEConvention = "") THEN
DocTargetFileName = DocABEConvention
'If Convention AND ExportPath had both no content.
ELSEIF (DocTargetPath = "%NETWORK%") THEN
DocTargetFileName = Nothing
'If ExportPath has content, but Convention hasnt.
ELSE
MSGBOX("Ungültige Export Dateinamens Einstellung für Ausgangsbestellungen. FailSafe ist ABE_" & DocAccountNumber & ((year(DateTime)*100 + month(DateTime))*100 + day(DateTime))*10000 + hour(DateTime)*100 + minute(DateTime) & ".pdf" & "!")
DocTargetFileName = "ABE_" & DocAccountNumber & ((year(DateTime)*100 + month(DateTime))*100 + day(DateTime))*10000 + hour(DateTime)*100 + minute(DateTime)
END IF
'If document is not for a debtor or creditor.
ELSE
MSGBOX("Undefinierte Export Einstellung! Abbruch des Exports.")
END IF
'Retrive Document Type (3 = Lieferschein). Depending on it, the Targetpath and Targetfilename.
ELSEIF (DocType = 3) THEN
'If document is for a debtor.
IF (PostingType = 1) THEN
'If ExportPath has content
IF NOT (IsEmpty(DocALSExportPath) Or IsNull(DocALSExportPath) Or DocALSExportPath = "") THEN
DocTargetPath = DocALSExportPath
'If ExportPath has no content, try to exit. But exit wont work, so fallback on the general WinLine network path.
ELSE
DocTargetPath = "%NETWORK%"
END IF
'If Convention has content.
IF NOT (IsEmpty(DocALSConvention) Or IsNull(DocALSConvention) Or DocALSConvention = "") THEN
DocTargetFileName = DocALSConvention
'If Convention AND ExportPath had both no content.
ELSEIF (DocTargetPath = "%NETWORK%") THEN
DocTargetFileName = Nothing
'If ExportPath has content, but Convention hasnt.
ELSE
MSGBOX("Ungültige Export Dateinamens Einstellung für Ausgangslieferscheine. FailSafe ist ALS_" & DocAccountNumber & ((year(DateTime)*100 + month(DateTime))*100 + day(DateTime))*10000 + hour(DateTime)*100 + minute(DateTime) & ".pdf" & "!")
DocTargetFileName = "ALS_" & DocAccountNumber & ((year(DateTime)*100 + month(DateTime))*100 + day(DateTime))*10000 + hour(DateTime)*100 + minute(DateTime)
END IF
'If document is for a creditor.
ELSEIF (PostingType = 2) THEN
'########################################### Currently not implemented ###########################################
'If document is not for a debtor or creditor.
ELSE
MSGBOX("Undefinierte Export Einstellung! Abbruch des Exports.")
END IF
'Retrive Document Type (4 = Rechnung). Depending on it, the Targetpath and Targetfilename.
ELSEIF (DocType = 4) THEN
'If document is for a debtor.
IF (PostingType = 1) THEN
'If ExportPath has content
IF NOT (IsEmpty(DocAREExportPath) Or IsNull(DocAREExportPath) Or DocAREExportPath = "") THEN
DocTargetPath = DocAREExportPath
'If ExportPath has no content, try to exit. But exit wont work, so fallback on the general WinLine network path.
ELSE
DocTargetPath = "%NETWORK%"
END IF
'If Convention has content.
IF NOT (IsEmpty(DocAREConvention) Or IsNull(DocAREConvention) Or DocAREConvention = "") THEN
DocTargetFileName = DocAREConvention
'If Convention AND ExportPath had both no content.
ELSEIF (DocTargetPath = "%NETWORK%") THEN
DocTargetFileName = Nothing
'If ExportPath has content, but Convention hasnt.
ELSE
MSGBOX("Ungültige Export Dateinamens Einstellung für Ausgangsrechnungen. FailSafe ist ARE_" & DocAccountNumber & ((year(DateTime)*100 + month(DateTime))*100 + day(DateTime))*10000 + hour(DateTime)*100 + minute(DateTime) & ".pdf" & "!")
DocTargetFileName = "ARE_" & DocAccountNumber & ((year(DateTime)*100 + month(DateTime))*100 + day(DateTime))*10000 + hour(DateTime)*100 + minute(DateTime)
END IF
'If document is for a creditor.
ELSEIF (PostingType = 2) THEN
'########################################### Currently not implemented ###########################################
'If document is not for a debtor or creditor.
ELSE
MSGBOX("Undefinierte Export Einstellung! Abbruch des Exports.")
END IF
'If DocType was unknown.
ELSE
MSGBOX("Undefinierte Belegstufe! Abbruch des Exports.")
END IF
'Replace PlaceHolder in DocTargetPath from Configuration, including invalid Characters.
IF (DocTargetPath <> "") THEN
DocTargetPath = Replace(DocTargetPath,"%Mandantenkürzel%",MandatorShortName)
DocTargetPath = Replace(DocTargetPath,"%Jahr%",(Year(date)))
DocTargetPath = Replace(DocTargetPath,"%Monat%",(month(date)))
DocTargetPath = Replace(DocTargetPath,"%Monatsname%",MonthName((month(date))))
DocTargetPath = Replace(DocTargetPath,"%Tag%",(day(date)))
DocTargetPath = Replace(DocTargetPath,"%KontoNr%",DocAccountNumber)
DocTargetPath = Replace(DocTargetPath,"%Laufnummer%",RecordNumber)
DocTargetPath = Replace(Replace(Replace(Replace(Replace(Replace(DocTargetPath,"/",ReplaceSpecialChar),"*",ReplaceSpecialChar),"?",ReplaceSpecialChar),"<",ReplaceSpecialChar),">",ReplaceSpecialChar),"|",ReplaceSpecialChar)
END IF
'Replace PlaceHolder in DocTargetFileName from Configuration, including invalid Characters.
IF (DocTargetFileName <> "") THEN
DocTargetFileName = Replace(DocTargetFileName,"%Mandantenkürzel%",MandatorShortName)
DocTargetFileName = Replace(DocTargetFileName,"%AngebotsNr%",DocAANGNumber)
DocTargetFileName = Replace(DocTargetFileName,"%AuftragsNr%",DocAABNumber)
DocTargetFileName = Replace(DocTargetFileName,"%LieferscheinNr%",DocALSNumber)
DocTargetFileName = Replace(DocTargetFileName,"%RechnungsNr%",DocARENumber)
DocTargetFileName = Replace(DocTargetFileName,"%AnfragenNr%",DocAANFNumber)
DocTargetFileName = Replace(DocTargetFileName,"%BestellNr%",DocABENumber)
DocTargetFileName = Replace(DocTargetFileName,"%Jahr%",(Year(date)))
DocTargetFileName = Replace(DocTargetFileName,"%Monat%",(month(date)))
DocTargetFileName = Replace(DocTargetFileName,"%Monatsname%",MonthName((month(date))))
DocTargetFileName = Replace(DocTargetFileName,"%Tag%",(day(date)))
DocTargetFileName = Replace(DocTargetFileName,"%KontoNr%",DocAccountNumber)
DocTargetFileName = Replace(DocTargetFileName,"%Laufnummer%",RecordNumber)
DocTargetFileName = Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(DocTargetFileName,"\",ReplaceSpecialChar),"/",ReplaceSpecialChar),":",ReplaceSpecialChar),"*",ReplaceSpecialChar),"?",ReplaceSpecialChar),"<",ReplaceSpecialChar),">",ReplaceSpecialChar),"|",ReplaceSpecialChar)
END IF
'#-----------------------------------------------------------------------------------------------------#
'############################################# main part ###############################################
'#-----------------------------------------------------------------------------------------------------#
'Check if all necessary export parameters are set.
IF ((DocTargetPath <> "") AND (DocTargetFileName <> "") AND (DocExtension <> "")) THEN
'Check if destination folder / folder strukture exists. If not, try to create.
IF NOT FileSystemObject.FolderExists(DocTargetPath) THEN
strDir = FileSystemObject.GetAbsolutePathName(DocTargetPath)
arrDirs = Split( strDir, "\" )
If Left( strDir, 2 ) = "\\" THEN
strDirBuild = "\\" & arrDirs(2) & "\" & arrDirs(3) & "\"
idxFirst = 4
Else
strDirBuild = arrDirs(0) & "\"
idxFirst = 1
End If
For idx = idxFirst to Ubound( arrDirs )
strDirBuild = FileSystemObject.BuildPath( strDirBuild, arrDirs(idx) )
If Not FileSystemObject.FolderExists( strDirBuild ) THEN
FileSystemObject.CreateFolder strDirBuild
End if
Next
END IF
'If DocTargetPath exists, export file - including version tagging.
IF (FileSystemObject.FolderExists(DocTargetPath)) THEN
FullPath = DocTargetPath & "\" & DocTargetFileName & DocExtension
IF (FileSystemObject.FileExists(FullPath)) THEN
DO
DocVersion = DocVersion + 1
FullPath = DocTargetPath & "\" & DocTargetFileName & DocVersionSeparator & DocVersion & DocExtension
LOOP UNTIL (FileSystemObject.FileExists(FullPath) = False)
END IF
ExportOutput FullPath, 5, 0
'Optional check if file was exported successfully.
IF (DocExportCheck = "true") THEN
CheckExportScriptPath = FileSystemObject.FolderExists(Shell.ExpandEnvironmentStrings("%TEMP%") & "\" & "Digital_Data")
CheckExportScript = "CheckExport-" & DocTargetFileName & ((year(DateTime)*100 + month(DateTime))*100 + day(DateTime))*10000 + hour(DateTime)*100 + minute(DateTime) & ".vbs"
IF NOT (CheckExportScriptPath) THEN
FileSystemObject.CreateFolder(CheckExportScriptPath)
END IF
IF (FileSystemObject.FolderExists(CheckExportScriptPath)) THEN
CheckExportFile = CheckExportScriptPath & "\" & CheckExportScript
Set CheckExportObject = FileSystemObject.CreateTextFile(CheckExportFile,True)
CheckExportObject.Write "DIM FileSystemObject, Fullpath, DocExportCheckTry, DocExportCheckMaxTrys" & vbCrLf
CheckExportObject.Write "SET FileSystemObject = CreateObject(\"Scripting.FileSystemObject\")" & vbCrLf
CheckExportObject.Write "" & vbCrLf
CheckExportObject.Write "FullPath = Wscript.Arguments(0)" & vbCrLf
CheckExportObject.Write "DocExportCheckTry = Wscript.Arguments(1)" & vbCrLf
CheckExportObject.Write "DocExportCheckMaxTrys = Wscript.Arguments(2)" & vbCrLf
CheckExportObject.Write "" & vbCrLf
CheckExportObject.Write "DO" & vbCrLf
CheckExportObject.Write "" & vbCrLf
CheckExportObject.Write "DocExportCheckTry = DocExportCheckTry + 1" & vbCrLf
CheckExportObject.Write "WScript.Sleep 3000 'Wait 3 Seconds" & vbCrLf
CheckExportObject.Write "" & vbCrLf
CheckExportObject.Write "LOOP UNTIL ((FileSystemObject.FileExists(FullPath) = True) OR ((DocExportCheckTry >= DocExportCheckMaxTrys) = True))" & vbCrLf
CheckExportObject.Write "" & vbCrLf
CheckExportObject.Write "IF NOT (FileSystemObject.FileExists(FullPath)) THEN" & vbCrLf
CheckExportObject.Write "" & vbCrLf
CheckExportObject.Write 'MSGBOX("ACHTUNG: Der Export ist fehlgeschlagen! Bitte wenden Sie sich an Ihren Administrator!")' & vbCrLf
CheckExportObject.Write "" & vbCrLf
CheckExportObject.Write "END IF" & vbCrLf
CheckExportObject.Write "" & vbCrLf
CheckExportObject.Write "FileSystemObject = Nothing" & vbCrLf
CheckExportObject.Write "Fullpath = Nothing" & vbCrLf
CheckExportObject.Write "DocExportCheckTry = Nothing" & vbCrLf
CheckExportObject.Write "DocExportCheckMaxTrys = Nothing" & vbCrLf
CheckExportObject.Close
ELSE
MSGBOX("ACHTUNG: Das CheckExportScript konnte nicht erstellt werden!")
END IF
END IF
ELSE
MSGBOX("ACHTUNG: Zielpfad für Export konnte nicht erstellt werden! Export wird abgebrochen.")
END IF
ELSE
'Missing Value in DocTargetPath, DocTargetFileName or DocExtension
WScript.Quit(1)
END IF
'#-----------------------------------------------------------------------------------------------------#
'########################################### finishing part ############################################
'#-----------------------------------------------------------------------------------------------------#
FileSystemObject = Nothing
Shell = Nothing
DateTime = Nothing
SplitChar = Nothing
ReplaceSpecialChar = Nothing
DocExtension = Nothing
DocExportCheck = Nothing
DocExportCheckTry = Nothing
DocExportCheckMaxTrys = Nothing
DocType = Nothing
DocVersion = Nothing
DocVersionSeparator = Nothing
MandatorShortName = Nothing
DocAccountNumber = Nothing
DocAANGExportPath = Nothing
DocAABExportPath = Nothing
DocALSExportPath = Nothing
DocAREExportPath = Nothing
DocAANFExportPath = Nothing
DocABExportPath = Nothing
DocAANGConvention = Nothing
DocAABConvention = Nothing
DocALSConvention = Nothing
DocAREConvention = Nothing
DocAANFConvention = Nothing
DocABEConvention = Nothing
DocAANGNumber = Nothing
DocAABNumber = Nothing
DocALSNumber = Nothing
DocARENumber = Nothing
DocAANFNumber = Nothing
DocABENumber = Nothing
DocTargetPath = Nothing
DocTargetFileName = Nothing
FullPath = Nothing
Result = ""