@@ -9,6 +9,7 @@ title: 'Line items grouping: Alternative Python solution'
99Consider 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):
1010
1111``` py
12+ from collections import defaultdict
1213from rossum_python import RossumPython, is_empty, default_to, is_set
1314
1415
@@ -24,31 +25,30 @@ def rossum_hook_request_handler(payload):
2425 # Reset the target table:
2526 x.field.tax_details_export = []
2627
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+ })
2834
2935 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+ {
4644 ' tax_detail_rate_export' : rate,
4745 ' tax_detail_base_export' : sum_values(values[' tax_detail_base_export' ]),
4846 ' tax_detail_tax_export' : sum_values(values[' tax_detail_tax_export' ]),
4947 ' tax_detail_total_export' : sum_values(values[' tax_detail_total_export' ]),
5048 ' tax_detail_description_export' : values[' tax_detail_description_export' ],
51- })
49+ }
50+ for rate, values in vat_rate_groups.items()
51+ ]
5252
5353 return x.hook_response()
5454```
0 commit comments