Selaa lähdekoodia

Marketing kann einzeln eingespielt werden, Form9

robert 2 vuotta sitten
vanhempi
commit
27d2712619
5 muutettua tiedostoa jossa 91 lisäystä ja 29 poistoa
  1. 1 0
      .gitignore
  2. 3 1
      config/reisacher.json
  3. BIN
      data/Marketingplanung_AHR_2023_V1.xls
  4. 9 3
      webservice/file_io.py
  5. 78 25
      webservice/planner_load.py

+ 1 - 0
.gitignore

@@ -1,2 +1,3 @@
 save/
 __pycache__
+.~lock.*

+ 3 - 1
config/reisacher.json

@@ -22,7 +22,9 @@
             "4",
             "5",
             "6",
-            "7"
+            "7",
+            "8",
+            "9"
         ],
         "filter": {
             "department": [

BIN
data/Marketingplanung_AHR_2023_V1.xls


+ 9 - 3
webservice/file_io.py

@@ -20,6 +20,7 @@ planner_dir = script_dir.parent.joinpath('export')
 config_dir = script_dir.parent.joinpath('config')
 # save_dir = Path('C:\\Projekte\\Python\\Planung\\save')
 user_info = None
+timestamp_keywords = ('accounts', 'plan', 'marketing')
 
 
 @bp.route('/login/', methods=['POST'])
@@ -66,16 +67,21 @@ def load(year, version, timestamp):
 
     if timestamp == 'current' or not Path(file).exists():
         file_list = list_dict()
-        timestamp = file_list['tree'][year][version][-1]
-        file = full_filename(year, version, timestamp)
+        timestamp2 = file_list['tree'][year][version][-1]
+        file = full_filename(year, version, timestamp2)
 
     print(file)
-    time.sleep(2)
+
     with open(file, 'r') as frh:
         structure = json.loads(frh.read())
         if 'options' not in structure[0]:
             p_load = PlannerLoad(planner_dir)
             structure = p_load.convert_file(structure)
+        elif timestamp in timestamp_keywords:
+            p_load = PlannerLoad(planner_dir)
+            p_load.load_values(year)
+            p_load.set_structure(structure)
+            structure = p_load.update_values(timestamp)
 
         return Response(
             response=json.dumps(structure),

+ 78 - 25
webservice/planner_load.py

@@ -8,6 +8,7 @@ class PlannerLoad:
     account_info: list = None
     plan_values: list = None
     marketing_values: list = None
+    new_structure: list
     structure: list
     config = {
         'department': [1, 2, 3, 10, 30, 40, 50, 55, 81, 82],
@@ -20,6 +21,10 @@ class PlannerLoad:
         self.base_dir = Path(base_dir)
 
     def new_file(self, year: str):
+        self.load_values(year)
+        return self.convert_file(self.new_structure)
+
+    def load_values(self, year: str):
         with open(self.base_dir.joinpath(f'accounts_{year}.json'), 'r') as frh:
             self.account_values = json.load(frh)['values']
         with open(self.base_dir.joinpath(f'planning_{year}.json'), 'r') as frh:
@@ -29,7 +34,10 @@ class PlannerLoad:
         with open(self.base_dir.joinpath('gcstruct.json'), 'r') as frh:
             gcstruct = json.load(frh)
         self.account_info = dict([(a['Konto_Nr'], a) for a in gcstruct['accounts']])
-        return self.convert_file(gcstruct['flat']['Struktur_FB'])
+        self.new_structure = gcstruct['flat']['Struktur_FB']
+
+    def set_structure(self, structure: list):
+        self.structure = structure
 
     def convert_file(self, structure_source):
         self.structure = []
@@ -86,35 +94,80 @@ class PlannerLoad:
         return self.structure
 
     def get_values2(self, s):
-        a_values = {}
-        m_values = {}
-        if s['accounts'] and s['values2'] is None:
-            a_values = self.account_values.get(s['accounts'][0], dict())
-            m_values = self.marketing_values.get(s['accounts'][0], dict())
-        p_values = {}
-        if self.plan_values is not None:
-            p_values = self.plan_values.get(s['id'], dict())
+        if 'values2' not in s or len(s['values2'].keys()) == 0:
+            s['values2'] = dict([(str(d), [0] * 30) for d in self.config['department']])
+        else:
+            for d in s['values2'].keys():
+                if len(s['values2'][d]) < 30:
+                    s['values2'][d] = [s['values2'][d][i] if 0 <= i < len(s['values2'][d]) else 0
+                                       for i in self.config['translation']]            
+        
+        self.update_account_values(s)
+        self.update_plan_values(s)
+        self.update_marketing_values(s)
+    
+    def update_values(self, value_type):
+        v_types = {
+            'accounts': self.update_account_values,
+            'plan': self.update_plan_values,
+            'marketing': self.update_marketing_values
+        }
+
+        parents = []
+        for s in self.structure:
+            if v_types[value_type](s):
+                parents.append(s['parents'][0])
+        if value_type == 'marketing':
+            for p_id in set(parents):
+                parent = self.get_structure_by_id(p_id)
+                parent['form'] = '9'
+                for d in parent['options'].keys():
+                    parent['options'][d]['status'] = '0'
+        return self.structure
 
-        values = dict([(str(d), [0] * 30) for d in self.config['department']])
-        for d in values.keys():
-            if s['values2'] is not None and d in s['values2']:
-                if len(s['values2'][d]) == 30:
-                    values[d] = s['values2'][d]
-                else:
-                    values[d] = [s['values2'][d][i] if 0 <= i < len(s['values2'][d]) else 0
-                                 for i in self.config['translation']]
-            if d in p_values:
-                for i, v in enumerate(p_values[d], 28):
-                    values[d][i] = v
+    def get_structure_by_id(self, id):
+        for s in self.structure:
+            if s['id'] == id:
+                return s
+        return None
+
+    def update_account_values(self, s):
+        if len(s['accounts']) == 0:
+            return False
+        a_values = self.account_values.get(s['accounts'][0], dict())
+
+        for d in s['values2'].keys():
             if d in a_values:
                 for i, v in enumerate(a_values[d], 20):
-                    values[d][i] = v
+                    s['values2'][d][i] = v
+        return s['values2']
+
+    def update_plan_values(self, s):
+        if self.plan_values is None:
+            return False
+        p_values = self.plan_values.get(s['id'], dict())
+
+        for d in s['values2'].keys():
+            if d in p_values:
+                for i, v in enumerate(p_values[d], 28):
+                    s['values2'][d][i] = v
+        return True
+
+    def update_marketing_values(self, s):
+        if len(s['accounts']) == 0:
+            return False
+        m_values = self.marketing_values.get(s['accounts'][0], dict())
+        if not m_values:
+            return False
+
+        for d in s['values2'].keys():
             if d in m_values:
                 for i, v in enumerate(m_values[d], 1):
-                    values[d][i] = v
-                values[d][0] = sum(m_values[d])
-                values[d][14] = sum(m_values[d])
-        return values
+                    s['values2'][d][i] = v
+                s['values2'][d][0] = sum(m_values[d])
+                s['values2'][d][14] = sum(m_values[d])
+                s['options'][d]['seasonal'] = False
+        return True
 
     def convert_values2(self, s):
         a_values = {}