Skip to content

Commit 3d763b4

Browse files
committed
Merge PR #945 into 16.0
Signed-off-by simahawk
2 parents 9cd45f5 + 20850b0 commit 3d763b4

File tree

6 files changed

+81
-51
lines changed

6 files changed

+81
-51
lines changed

despatch_advice_import/wizard/despatch_advice_import.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ def process_document(self):
135135
)
136136
self.process_data(parsed_order_document)
137137

138-
def _collect_lines_by_id(self, lines_doc):
138+
def _collect_lines_by_id(self, lines_doc, key="order_line_id"):
139139
lines_by_id = {}
140140
for line in lines_doc:
141-
line_id = int(line["order_line_id"])
141+
line_id = int(line[key])
142142
if line_id in lines_by_id:
143143
lines_by_id[line_id]["qty"] += line["qty"]
144144
lines_by_id[line_id]["backorder_qty"] += line["backorder_qty"]

despatch_advice_import_ubl/tests/files/despatch_advice_2.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
66
xmlns:xs="http://www.w3.org/2001/XMLSchema"
77
>
8-
<cbc:ID>0810805774</cbc:ID>
8+
<cbc:ID>{picking_name}</cbc:ID>
99
<cbc:IssueDate>2020-11-16</cbc:IssueDate>
1010
<cbc:DespatchAdviceTypeCode>scheduled</cbc:DespatchAdviceTypeCode>
1111
<cac:OrderReference>

despatch_advice_import_ubl/tests/files/despatch_advice_tmpl.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
66
xmlns:xs="http://www.w3.org/2001/XMLSchema"
77
>
8-
<cbc:ID>0810805774</cbc:ID>
8+
<cbc:ID>{picking_name}</cbc:ID>
99
<cbc:IssueDate>2020-11-16</cbc:IssueDate>
1010
<cbc:DespatchAdviceTypeCode>scheduled</cbc:DespatchAdviceTypeCode>
1111
<cac:OrderReference>

despatch_advice_import_ubl/tests/test_despatch_advice_import.py

+4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def test_xml_convert_to_internal_data_01(self):
8787
All the fields are filled into the internal data structure.
8888
"""
8989
xml_content = self.despatch_advice_xml1.decode("utf-8").format(
90+
picking_name="0810805774",
9091
order_id=self.purchase_order.name,
9192
line_1_id=self.line1.id,
9293
line_1_qty=self.line1.product_qty,
@@ -110,6 +111,7 @@ def test_xml_convert_to_internal_data_01(self):
110111
"date": "2020-11-16",
111112
"despatch_advice_type_code": "delivery",
112113
"estimated_delivery_date": "2020-11-17",
114+
"id": "0810805774",
113115
"lines": [
114116
{
115117
"backorder_qty": 12.0,
@@ -149,6 +151,7 @@ def test_xml_convert_to_internal_data_02(self):
149151
All the fields are filled into the internal data structure.
150152
"""
151153
xml_content = self.despatch_advice_xml2.decode("utf-8").format(
154+
picking_name="0810805774",
152155
line_1_id=self.line1.id,
153156
line_1_order_id=self.purchase_order.name,
154157
line_1_qty=self.line1.product_qty,
@@ -172,6 +175,7 @@ def test_xml_convert_to_internal_data_02(self):
172175
"date": "2020-11-16",
173176
"despatch_advice_type_code": "scheduled",
174177
"estimated_delivery_date": "2020-11-17",
178+
"id": "0810805774",
175179
"lines": [
176180
{
177181
"backorder_qty": 12.0,

despatch_advice_import_ubl/tests/test_despatch_advice_mix.py

+1
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ def test_despatch_advice_import(self):
705705
self.assertTrue(po_moves)
706706

707707
xml_content2 = self.despatch_advice_xml2.decode("utf-8").format(
708+
picking_name="0810805774",
708709
order_id=self.purchase_order.name,
709710
line_3_id=self.line3.id,
710711
line_6_id=self.line6.id,

despatch_advice_import_ubl/wizard/despatch_advice_import.py

+72-47
Original file line numberDiff line numberDiff line change
@@ -31,100 +31,123 @@ def parse_ubl_despatch_advice(self, xml_root):
3131
else:
3232
main_xmlns = ns.pop("DespatchAdvice")
3333
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(
3636
"/main:DespatchAdvice/cac:Shipment/cac:Delivery/"
3737
"cac:EstimatedDeliveryPeriod/cbc:EndDate",
3838
namespaces=ns,
3939
)
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(
4142
"/main:DespatchAdvice/cac:OrderReference/cbc:ID", namespaces=ns
4243
)
4344

44-
despatch_advice_type_code_xpath = xml_root.xpath(
45+
despatch_advice_type_code_el = xml_root.xpath(
4546
"/main:DespatchAdvice/cbc:DespatchAdviceTypeCode", namespaces=ns
4647
)
4748

48-
supplier_xpath = xml_root.xpath(
49+
supplier_el = xml_root.xpath(
4950
"/main:DespatchAdvice/cac:DespatchSupplierParty/cac:Party", namespaces=ns
5051
)
5152
# 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)
5354
supplier_dict = {
5455
"vat": supplier_dict.get("vat"),
5556
}
56-
customer_xpath = xml_root.xpath(
57+
customer_el = xml_root.xpath(
5758
"/main:DespatchAdvice/cac:DeliveryCustomerParty/cac:Party", namespaces=ns
5859
)
59-
customer_dict = self.ubl_parse_party(customer_xpath[0], ns)
60+
customer_dict = self.ubl_parse_party(customer_el[0], ns)
6061

6162
customer_dict = {"vat": customer_dict.get("vat")}
62-
lines_xpath = xml_root.xpath(
63+
lines_el = xml_root.xpath(
6364
"/main:DespatchAdvice/cac:DespatchLine", namespaces=ns
6465
)
6566
res_lines = []
66-
for line in lines_xpath:
67+
for line in lines_el:
6768
res_lines.append(self.parse_ubl_despatch_advice_line(line, ns))
6869
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 "",
7072
"supplier": supplier_dict,
7173
"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,
7882
"lines": res_lines,
7983
}
8084
return res
8185

8286
@api.model
8387
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)
8892
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)
9195
else:
9296
backorder_qty = 0
9397

94-
product_ref_xpath = line.xpath(
98+
product_ref_el = line.xpath(
9599
"cac:Item/cac:SellersItemIdentification/cbc:ID", namespaces=ns
96100
)
97101

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(
100104
"cac:Item/cac:BuyersItemIdentification/cbc:ID", namespaces=ns
101105
)
102106

103-
product_lot_xpath = line.xpath(
107+
product_lot_el = line.xpath(
104108
"cac:Item/cac:ItemInstance/cac:LotIdentification/cbc:LotNumberID",
105109
namespaces=ns,
106110
)
107-
order_reference_xpath = line.xpath(
111+
order_reference_el = line.xpath(
108112
"cac:OrderLineReference/cac:OrderReference/cbc:ID", namespaces=ns
109113
)
110114

111-
order_line_id_xpath = line.xpath(
115+
order_line_id_el = line.xpath(
112116
"cac:OrderLineReference/cbc:LineID", namespaces=ns
113117
)
114118

115-
if not order_line_id_xpath:
119+
if not order_line_id_el:
116120
raise UserError(_("Missing line ID in the Despatch Advice."))
117121

118122
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 "",
122126
"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")},
126130
"backorder_qty": backorder_qty,
127131
}
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+
128151
defaults = self.env.context.get("despatch_advice_import__default_vals", {}).get(
129152
"lines", {}
130153
)
@@ -133,21 +156,23 @@ def parse_ubl_despatch_advice_line(self, line, ns):
133156

134157
@api.model
135158
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(
139162
"cac:PartyLegalEntity/cbc:RegistrationName", namespaces=ns
140163
)
141164

142-
vat_xpath = party_node.xpath("cac:PartyIdentification/cbc:ID", namespaces=ns)
165+
vat_el = party_node.xpath("cac:PartyIdentification/cbc:ID", namespaces=ns)
143166
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,
148173
}
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)
152177
partner_dict.update(address_dict)
153178
return partner_dict

0 commit comments

Comments
 (0)