@@ -9,6 +9,7 @@ title: 'Line items grouping: Alternative Python solution'
9
9
Consider using the following simple Python code (as a [ serverless function] ( ../rossum-formulas/serverless-functions.md ) ) that replaces the whole functionality of this extension (no need for any webhook):
10
10
11
11
``` py
12
+ from collections import defaultdict
12
13
from rossum_python import RossumPython, is_empty, default_to, is_set
13
14
14
15
@@ -24,31 +25,30 @@ def rossum_hook_request_handler(payload):
24
25
# Reset the target table:
25
26
x.field.tax_details_export = []
26
27
27
- vat_rate_groups = {}
28
+ vat_rate_groups = defaultdict(lambda : {
29
+ ' tax_detail_base_export' : [],
30
+ ' tax_detail_tax_export' : [],
31
+ ' tax_detail_total_export' : [],
32
+ ' tax_detail_description_export' : None
33
+ })
28
34
29
35
for row in x.field.tax_details:
30
- vat_rate = row.tax_detail_rate_normalized.attr.value
31
-
32
- if vat_rate not in vat_rate_groups:
33
- vat_rate_groups[vat_rate] = {
34
- ' tax_detail_base_export' : [],
35
- ' tax_detail_tax_export' : [],
36
- ' tax_detail_total_export' : [],
37
- ' tax_detail_description_export' : row.tax_detail_description
38
- }
39
-
40
- vat_rate_groups[vat_rate][' tax_detail_base_export' ].append(row.tax_detail_base_normalized)
41
- vat_rate_groups[vat_rate][' tax_detail_tax_export' ].append(row.tax_detail_tax_normalized)
42
- vat_rate_groups[vat_rate][' tax_detail_total_export' ].append(row.tax_detail_total_normalized)
43
-
44
- for rate, values in vat_rate_groups.items():
45
- x.field.tax_details_export.append({
36
+ group = vat_rate_groups[row.tax_detail_rate_normalized.attr.value]
37
+ group[' tax_detail_base_export' ].append(row.tax_detail_base_normalized)
38
+ group[' tax_detail_tax_export' ].append(row.tax_detail_tax_normalized)
39
+ group[' tax_detail_total_export' ].append(row.tax_detail_total_normalized)
40
+ group[' tax_detail_description_export' ] = row.tax_detail_description
41
+
42
+ x.field.tax_details_export = [
43
+ {
46
44
' tax_detail_rate_export' : rate,
47
45
' tax_detail_base_export' : sum_values(values[' tax_detail_base_export' ]),
48
46
' tax_detail_tax_export' : sum_values(values[' tax_detail_tax_export' ]),
49
47
' tax_detail_total_export' : sum_values(values[' tax_detail_total_export' ]),
50
48
' tax_detail_description_export' : values[' tax_detail_description_export' ],
51
- })
49
+ }
50
+ for rate, values in vat_rate_groups.items()
51
+ ]
52
52
53
53
return x.hook_response()
54
54
```
0 commit comments