@@ -31,100 +31,123 @@ def parse_ubl_despatch_advice(self, xml_root):
31
31
else :
32
32
main_xmlns = ns .pop ("DespatchAdvice" )
33
33
ns ["main" ] = main_xmlns
34
- date_xpath = xml_root .xpath ("/main:DespatchAdvice/cbc:IssueDate" , namespaces = ns )
35
- estimated_delivery_date_xpath = xml_root .xpath (
34
+ date_el = xml_root .xpath ("/main:DespatchAdvice/cbc:IssueDate" , namespaces = ns )
35
+ estimated_delivery_date_el = xml_root .xpath (
36
36
"/main:DespatchAdvice/cac:Shipment/cac:Delivery/"
37
37
"cac:EstimatedDeliveryPeriod/cbc:EndDate" ,
38
38
namespaces = ns ,
39
39
)
40
- order_reference_xpath = xml_root .xpath (
40
+ order_id_el = xml_root .xpath ("/main:DespatchAdvice/cbc:ID" , namespaces = ns )
41
+ order_reference_el = xml_root .xpath (
41
42
"/main:DespatchAdvice/cac:OrderReference/cbc:ID" , namespaces = ns
42
43
)
43
44
44
- despatch_advice_type_code_xpath = xml_root .xpath (
45
+ despatch_advice_type_code_el = xml_root .xpath (
45
46
"/main:DespatchAdvice/cbc:DespatchAdviceTypeCode" , namespaces = ns
46
47
)
47
48
48
- supplier_xpath = xml_root .xpath (
49
+ supplier_el = xml_root .xpath (
49
50
"/main:DespatchAdvice/cac:DespatchSupplierParty/cac:Party" , namespaces = ns
50
51
)
51
52
# We only take the "official references" for supplier_dict
52
- supplier_dict = self .ubl_parse_party (supplier_xpath [0 ], ns )
53
+ supplier_dict = self .ubl_parse_party (supplier_el [0 ], ns )
53
54
supplier_dict = {
54
55
"vat" : supplier_dict .get ("vat" ),
55
56
}
56
- customer_xpath = xml_root .xpath (
57
+ customer_el = xml_root .xpath (
57
58
"/main:DespatchAdvice/cac:DeliveryCustomerParty/cac:Party" , namespaces = ns
58
59
)
59
- customer_dict = self .ubl_parse_party (customer_xpath [0 ], ns )
60
+ customer_dict = self .ubl_parse_party (customer_el [0 ], ns )
60
61
61
62
customer_dict = {"vat" : customer_dict .get ("vat" )}
62
- lines_xpath = xml_root .xpath (
63
+ lines_el = xml_root .xpath (
63
64
"/main:DespatchAdvice/cac:DespatchLine" , namespaces = ns
64
65
)
65
66
res_lines = []
66
- for line in lines_xpath :
67
+ for line in lines_el :
67
68
res_lines .append (self .parse_ubl_despatch_advice_line (line , ns ))
68
69
res = {
69
- "ref" : order_reference_xpath [0 ].text if order_reference_xpath else "" ,
70
+ "id" : order_id_el [0 ].text if order_id_el else "" ,
71
+ "ref" : order_reference_el [0 ].text if order_reference_el else "" ,
70
72
"supplier" : supplier_dict ,
71
73
"company" : customer_dict ,
72
- "despatch_advice_type_code" : despatch_advice_type_code_xpath [0 ].text
73
- if len (despatch_advice_type_code_xpath ) > 0
74
- else "" ,
75
- "date" : len (date_xpath ) and date_xpath [0 ].text ,
76
- "estimated_delivery_date" : len (estimated_delivery_date_xpath )
77
- and estimated_delivery_date_xpath [0 ].text ,
74
+ "despatch_advice_type_code" : (
75
+ despatch_advice_type_code_el [0 ].text
76
+ if len (despatch_advice_type_code_el ) > 0
77
+ else ""
78
+ ),
79
+ "date" : len (date_el ) and date_el [0 ].text ,
80
+ "estimated_delivery_date" : len (estimated_delivery_date_el )
81
+ and estimated_delivery_date_el [0 ].text ,
78
82
"lines" : res_lines ,
79
83
}
80
84
return res
81
85
82
86
@api .model
83
87
def parse_ubl_despatch_advice_line (self , line , ns ):
84
- line_id_xpath = line .xpath ("cbc:ID" , namespaces = ns )
85
- qty_xpath = line .xpath ("cbc:DeliveredQuantity" , namespaces = ns )
86
- qty = float (qty_xpath [0 ].text )
87
- backorder_qty_xpath = line .xpath ("cbc:OutstandingQuantity" , namespaces = ns )
88
+ line_id_el = line .xpath ("cbc:ID" , namespaces = ns )
89
+ qty_el = line .xpath ("cbc:DeliveredQuantity" , namespaces = ns )
90
+ qty = float (qty_el [0 ].text )
91
+ backorder_qty_el = line .xpath ("cbc:OutstandingQuantity" , namespaces = ns )
88
92
backorder_qty = None
89
- if backorder_qty_xpath and len (backorder_qty_xpath ):
90
- backorder_qty = float (backorder_qty_xpath [0 ].text )
93
+ if backorder_qty_el and len (backorder_qty_el ):
94
+ backorder_qty = float (backorder_qty_el [0 ].text )
91
95
else :
92
96
backorder_qty = 0
93
97
94
- product_ref_xpath = line .xpath (
98
+ product_ref_el = line .xpath (
95
99
"cac:Item/cac:SellersItemIdentification/cbc:ID" , namespaces = ns
96
100
)
97
101
98
- if len (product_ref_xpath ) == 0 :
99
- product_ref_xpath = line .xpath (
102
+ if len (product_ref_el ) == 0 :
103
+ product_ref_el = line .xpath (
100
104
"cac:Item/cac:BuyersItemIdentification/cbc:ID" , namespaces = ns
101
105
)
102
106
103
- product_lot_xpath = line .xpath (
107
+ product_lot_el = line .xpath (
104
108
"cac:Item/cac:ItemInstance/cac:LotIdentification/cbc:LotNumberID" ,
105
109
namespaces = ns ,
106
110
)
107
- order_reference_xpath = line .xpath (
111
+ order_reference_el = line .xpath (
108
112
"cac:OrderLineReference/cac:OrderReference/cbc:ID" , namespaces = ns
109
113
)
110
114
111
- order_line_id_xpath = line .xpath (
115
+ order_line_id_el = line .xpath (
112
116
"cac:OrderLineReference/cbc:LineID" , namespaces = ns
113
117
)
114
118
115
- if not order_line_id_xpath :
119
+ if not order_line_id_el :
116
120
raise UserError (_ ("Missing line ID in the Despatch Advice." ))
117
121
118
122
res_line = {
119
- "line_id" : line_id_xpath [0 ].text ,
120
- "order_line_id" : order_line_id_xpath [0 ].text ,
121
- "ref" : order_reference_xpath [0 ].text if order_reference_xpath else "" ,
123
+ "line_id" : line_id_el [0 ].text ,
124
+ "order_line_id" : order_line_id_el [0 ].text ,
125
+ "ref" : order_reference_el [0 ].text if order_reference_el else "" ,
122
126
"qty" : qty ,
123
- "product_ref" : product_ref_xpath [0 ].text ,
124
- "product_lot" : product_lot_xpath [0 ].text if product_lot_xpath else "" ,
125
- "uom" : {"unece_code" : qty_xpath [0 ].attrib .get ("unitCode" )},
127
+ "product_ref" : product_ref_el [0 ].text ,
128
+ "product_lot" : product_lot_el [0 ].text if product_lot_el else "" ,
129
+ "uom" : {"unece_code" : qty_el [0 ].attrib .get ("unitCode" )},
126
130
"backorder_qty" : backorder_qty ,
127
131
}
132
+
133
+ package_id_el = line .xpath (
134
+ "cac:Shipment/cac:TransportHandlingUnit/cac:ActualPackage/cbc:ID" ,
135
+ namespaces = ns ,
136
+ )
137
+ package_type_el = line .xpath (
138
+ "cac:Shipment/cac:TransportHandlingUnit/cbc:TransportHandlingUnitTypeCode" ,
139
+ namespaces = ns ,
140
+ )
141
+ package_weight_el = line .xpath (
142
+ "cac:Shipment/cac:GrossWeightMeasure/cbc:Measure" , namespaces = ns
143
+ )
144
+ if package_id_el or package_type_el :
145
+ res_line ["package" ] = {
146
+ "name" : package_id_el [0 ].text if package_id_el else "" ,
147
+ "type" : package_type_el [0 ].text if package_type_el else "" ,
148
+ "weight" : package_weight_el [0 ].text if package_weight_el else "" ,
149
+ }
150
+
128
151
defaults = self .env .context .get ("despatch_advice_import__default_vals" , {}).get (
129
152
"lines" , {}
130
153
)
@@ -133,21 +156,23 @@ def parse_ubl_despatch_advice_line(self, line, ns):
133
156
134
157
@api .model
135
158
def ubl_parse_party (self , party_node , ns ):
136
- partner_name_xpath = party_node .xpath ("cac:PartyName/cbc:Name" , namespaces = ns )
137
- if not partner_name_xpath :
138
- partner_name_xpath = party_node .xpath (
159
+ partner_name_el = party_node .xpath ("cac:PartyName/cbc:Name" , namespaces = ns )
160
+ if not partner_name_el :
161
+ partner_name_el = party_node .xpath (
139
162
"cac:PartyLegalEntity/cbc:RegistrationName" , namespaces = ns
140
163
)
141
164
142
- vat_xpath = party_node .xpath ("cac:PartyIdentification/cbc:ID" , namespaces = ns )
165
+ vat_el = party_node .xpath ("cac:PartyIdentification/cbc:ID" , namespaces = ns )
143
166
partner_dict = {
144
- "vat" : vat_xpath [0 ].text
145
- if vat_xpath and vat_xpath [0 ].attrib .get ("schemeName" ).upper ()
146
- else False ,
147
- "name" : partner_name_xpath [0 ].text ,
167
+ "vat" : (
168
+ vat_el [0 ].text
169
+ if vat_el and vat_el [0 ].attrib .get ("schemeName" ).upper ()
170
+ else False
171
+ ),
172
+ "name" : partner_name_el [0 ].text ,
148
173
}
149
- address_xpath = party_node .xpath ("cac:PostalAddress" , namespaces = ns )
150
- if address_xpath :
151
- address_dict = self .ubl_parse_address (address_xpath [0 ], ns )
174
+ address_el = party_node .xpath ("cac:PostalAddress" , namespaces = ns )
175
+ if address_el :
176
+ address_dict = self .ubl_parse_address (address_el [0 ], ns )
152
177
partner_dict .update (address_dict )
153
178
return partner_dict
0 commit comments