浏览代码

Erster Entwurf IFT-Import

robert 2 年之前
父节点
当前提交
2e669658b5

+ 10 - 0
tools/database/config/IFT/exec/IFT_Actuals.sql

@@ -0,0 +1,10 @@
+DELETE T1 FROM [dbo].[IFT_Actuals] T1 
+INNER JOIN [temp].[IFT_Actuals] T2 
+ON T1.[Document Number] = T2.[Document Number] 
+AND T1.[Number of Line Item] = T2.[Number of Line Item]
+AND T1.[Posting Date] = T2.[Posting Date]
+GO
+
+INSERT INTO [dbo].[IFT_Actuals]
+SELECT T1.*, LEFT(T1.[Account Number GL], 7) FROM [temp].[IFT_Actuals] T1
+GO

+ 6 - 0
tools/database/config/IFT/exec/IFT_Budget.sql

@@ -0,0 +1,6 @@
+TRUNCATE TABLE [dbo].[IFT_Budget]
+GO
+
+INSERT INTO [dbo].[IFT_Budget]
+SELECT T1.* FROM [temp].[IFT_Budget] T1
+GO

+ 6 - 0
tools/database/config/IFT/exec/IFT_Commitment.sql

@@ -0,0 +1,6 @@
+TRUNCATE TABLE [dbo].[IFT_Commitment]
+GO
+
+INSERT INTO [dbo].[IFT_Commitment]
+SELECT T1.* FROM [temp].[IFT_Commitment] T1
+GO

+ 9 - 0
tools/database/config/IFT/exec/IFT_Controlling_Documents.sql

@@ -0,0 +1,9 @@
+DELETE T1 FROM [dbo].[IFT_Controlling_Documents] T1  
+INNER JOIN [temp].[IFT_Controlling_Documents] T2 
+ON T1.[Document Number] = T2.[Document Number] 
+AND T1.[posting line] = T2.[posting line]
+GO
+
+INSERT INTO [dbo].[IFT_Controlling_Documents]
+SELECT T1.* FROM [temp].[IFT_Controlling_Documents] T1
+GO

+ 6 - 0
tools/database/config/IFT/exec/IFT_Plan.sql

@@ -0,0 +1,6 @@
+TRUNCATE TABLE [dbo].[IFT_Plan]
+GO
+
+INSERT INTO [dbo].[IFT_Plan]
+SELECT T1.* FROM [temp].[IFT_Plan] T1
+GO

+ 14 - 0
tools/database/config/IFT/exec/Truncate_Temp.sql

@@ -0,0 +1,14 @@
+TRUNCATE TABLE [temp].[IFT_Actuals]
+GO
+
+TRUNCATE TABLE [temp].[IFT_Budget]
+GO
+
+TRUNCATE TABLE [temp].[IFT_Commitment]
+GO
+
+TRUNCATE TABLE [temp].[IFT_Controlling_Documents]
+GO
+
+TRUNCATE TABLE [temp].[IFT_Plan]
+GO

+ 28 - 0
tools/database/gebos_convert.py

@@ -0,0 +1,28 @@
+import csv
+from pathlib import Path
+
+
+def convert_dir(path):
+    source_path = Path(path)
+    target_path = source_path.parent.joinpath('staging')
+
+    for source_file in source_path.glob('*.csv'):
+        print(source_file.name)
+        target_file = target_path / source_file.name
+        convert_file(source_file, target_file)
+
+
+def convert_file(source, target):
+    with open(source, 'r', encoding='utf-8') as frh:
+        with open(target, 'w', encoding='latin-1') as fwh:
+            csv_reader = csv.reader(frh, delimiter=';')
+            csv_writer = csv.writer(fwh, delimiter='\t')
+            csv_writer.writerows(csv_reader)
+
+
+def main():
+    convert_dir('E:\\GEBOS\\data')
+
+
+if __name__ == '__main__':
+    main()

+ 26 - 0
tools/database/ift_backup.py

@@ -0,0 +1,26 @@
+import os
+from pathlib import Path
+
+
+def ift_backup(base_dir=None):
+    if base_dir is None:
+        base_dir = '/home/robert/projekte/python/BMW_Wien/IFT'
+
+    source_path = Path(base_dir) / 'prod'
+    target_path = base_dir + '/archive'
+
+    for source_file in source_path.glob('*.txt'):
+        # print(source_file)
+        file_temp = source_file.name[:-4]
+        if file_temp.count('_') == 2:
+            prefix, filetype, timestamp = file_temp.split('_')
+            year = timestamp[:4]
+            month = timestamp[4:6]
+            target = f"{target_path}/{filetype}/{year}/{year}-{month}/{source_file.name}"
+            os.makedirs(Path(target).parent, exist_ok=True)
+            print(target)
+            # source_file.rename(target)
+
+
+if __name__ == '__main__':
+    ift_backup()

+ 1 - 1
tools/database/ift_convert.py

@@ -11,7 +11,7 @@ class IFTConverter:
                 '2': self.import_config(base_dir + 'actuals_item.txt')
             },
             '_B_': {
-                '010': self.import_config(base_dir + 'budget_line.txt')                
+                '010': self.import_config(base_dir + 'budget_line.txt')
             },
             '_C_': {
                 '010': self.import_config(base_dir + 'commitment_line.txt')

+ 47 - 0
tools/database/ift_import.py

@@ -0,0 +1,47 @@
+from pathlib import Path
+import subprocess
+
+cfg = {
+    'schema': 'temp',
+    'scripts_dir': 'C:/GlobalCube/Tasks/scripts',
+    'query_dir': 'C:/GlobalCube/System/NAVISION/SQL/exec',
+}
+target_tables = {
+    'A': 'IFT_Actuals',
+    'B': 'IFT_Budget',
+    'C': 'IFT_Commitment',
+    'K': 'IFT_Controlling_Documents',
+    'P': 'IFT_Plan'
+}
+
+
+def task(args):
+    print(args)
+    # return subprocess.Popen(args, stdout=subprocess.DEVNULL).wait()
+    return 0
+
+
+def ift_import(csv_file, target_table):
+    bulkcopy = str(Path(cfg['scripts_dir']) / 'bulkcopy_import.bat')
+    sqlquery = str(Path(cfg['scripts_dir']) / 'sqlquery.bat')
+    sqlexec = str(Path(cfg['scripts_dir']) / 'sqlexec.bat')
+    query_file = str(Path(cfg['query_dir']) / (target_table + '.sql'))
+
+    task(f"{sqlquery} \"TRUNCATE TABLE [{cfg['schema']}].[{target_table}]\" ")
+    task(f"{bulkcopy} [{cfg['schema']}].[{target_table}] \"{csv_file}\" ")
+    task(f"{sqlexec} {query_file}")
+
+
+def import_dir(base_dir=None):
+    if base_dir is None:
+        base_dir = '/home/robert/projekte/python/BMW_Wien/IFT/prod_csv'
+
+    for csv_file in Path(base_dir).glob('*.csv'):
+        if csv_file.name.count('_') == 2:
+            _, filetype, _ = csv_file.name.split('_')
+            ift_import(str(csv_file), target_tables[filetype])
+            # csv_file.unlink()
+
+
+if __name__ == '__main__':
+    import_dir()