| 1 |
from lxml import etree |
| 2 |
|
| 3 |
from odoo import models, Command, _ |
| 4 |
from odoo.tools import html2plaintext |
| 5 |
from odoo.addons.account_edi_ubl_cii.tools import Order |
| 6 |
from odoo.addons.account.tools import dict_to_xml |
| 7 |
from odoo.addons.account_edi_ubl_cii.models.account_edi_common import FloatFmt |
| 8 |
|
| 9 |
|
| 10 |
class PurchaseEdiXmlUbl_Bis3(models.AbstractModel): |
| 11 |
_name = 'purchase.edi.xml.ubl_bis3' |
| 12 |
_inherit = ['account.edi.xml.ubl_bis3'] |
| 13 |
_description = "Purchase UBL BIS Ordering 3.5" |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
def _export_order(self, purchase_order): |
| 20 |
vals = {'purchase_order': purchase_order} |
| 21 |
document_node = self._get_purchase_order_node(vals) |
| 22 |
xml_content = dict_to_xml(document_node, template=Order, nsmap=self._get_document_nsmap(vals)) |
| 23 |
return etree.tostring(xml_content, xml_declaration=True, encoding='UTF-8') |
| 24 |
|
| 25 |
def _setup_base_lines(self, vals): |
| 26 |
|
| 27 |
super()._setup_base_lines(vals) |
| 28 |
|
| 29 |
for base_line in vals['base_lines']: |
| 30 |
product = base_line['product_id'] |
| 31 |
partner = base_line['partner_id'] |
| 32 |
supplier_info = product.variant_seller_ids.filtered(lambda s: |
| 33 |
s.partner_id == partner |
| 34 |
and ( |
| 35 |
s.product_id == product |
| 36 |
or (not s.product_id and s.product_tmpl_id == product.product_tmpl_id) |
| 37 |
) |
| 38 |
and (s.product_code or s.product_name), |
| 39 |
)[:1] |
| 40 |
base_line['supplier_info'] = supplier_info |
| 41 |
|
| 42 |
def _get_purchase_order_node(self, vals): |
| 43 |
self._add_purchase_order_config_vals(vals) |
| 44 |
self._add_purchase_order_base_lines_vals(vals) |
| 45 |
self._add_purchase_order_currency_vals(vals) |
| 46 |
self._add_purchase_order_tax_grouping_function_vals(vals) |
| 47 |
self._setup_base_lines(vals) |
| 48 |
self._add_purchase_order_monetary_totals_vals(vals) |
| 49 |
|
| 50 |
document_node = {} |
| 51 |
self._add_purchase_order_header_nodes(document_node, vals) |
| 52 |
self._add_purchase_order_buyer_customer_party_nodes(document_node, vals) |
| 53 |
self._add_purchase_order_seller_supplier_party_nodes(document_node, vals) |
| 54 |
self._add_purchase_order_delivery_nodes(document_node, vals) |
| 55 |
self._add_purchase_order_payment_terms_nodes(document_node, vals) |
| 56 |
self._add_purchase_order_line_nodes(document_node, vals) |
| 57 |
self._add_purchase_order_allowance_charge_nodes(document_node, vals) |
| 58 |
self._add_purchase_order_tax_total_nodes(document_node, vals) |
| 59 |
self._add_purchase_order_monetary_total_nodes(document_node, vals) |
| 60 |
return document_node |
| 61 |
|
| 62 |
def _add_purchase_order_config_vals(self, vals): |
| 63 |
purchase_order = vals['purchase_order'] |
| 64 |
supplier = purchase_order.partner_id |
| 65 |
customer = purchase_order.company_id.partner_id.commercial_partner_id |
| 66 |
|
| 67 |
customer_delivery_address = customer.child_ids.filtered(lambda child: child.type == 'delivery') |
| 68 |
partner_shipping = ( |
| 69 |
purchase_order.dest_address_id |
| 70 |
or (customer_delivery_address and customer_delivery_address[0]) |
| 71 |
or customer |
| 72 |
) |
| 73 |
|
| 74 |
vals.update({ |
| 75 |
'document_type': 'order', |
| 76 |
|
| 77 |
'supplier': supplier, |
| 78 |
'customer': customer, |
| 79 |
'partner_shipping': partner_shipping, |
| 80 |
|
| 81 |
'company': purchase_order.company_id, |
| 82 |
'currency_id': purchase_order.currency_id, |
| 83 |
'company_currency_id': purchase_order.company_id.currency_id, |
| 84 |
|
| 85 |
'use_company_currency': False, |
| 86 |
'fixed_taxes_as_allowance_charges': True, |
| 87 |
}) |
| 88 |
|
| 89 |
def _add_purchase_order_base_lines_vals(self, vals): |
| 90 |
purchase_order = vals['purchase_order'] |
| 91 |
AccountTax = self.env['account.tax'] |
| 92 |
|
| 93 |
base_lines = [line._prepare_base_line_for_taxes_computation() for line in purchase_order.order_line.filtered(lambda line: not line.display_type)] |
| 94 |
AccountTax._add_tax_details_in_base_lines(base_lines, purchase_order.company_id) |
| 95 |
AccountTax._round_base_lines_tax_details(base_lines, purchase_order.company_id) |
| 96 |
|
| 97 |
vals['base_lines'] = base_lines |
| 98 |
|
| 99 |
def _add_purchase_order_currency_vals(self, vals): |
| 100 |
self._add_document_currency_vals(vals) |
| 101 |
|
| 102 |
def _add_purchase_order_tax_grouping_function_vals(self, vals): |
| 103 |
self._add_document_tax_grouping_function_vals(vals) |
| 104 |
|
| 105 |
def _add_purchase_order_monetary_totals_vals(self, vals): |
| 106 |
self._add_document_monetary_total_vals(vals) |
| 107 |
|
| 108 |
def _add_purchase_order_header_nodes(self, document_node, vals): |
| 109 |
purchase_order = vals['purchase_order'] |
| 110 |
document_node.update({ |
| 111 |
'cbc:CustomizationID': {'_text': 'urn:fdc:peppol.eu:poacc:trns:order:3'}, |
| 112 |
'cbc:ProfileID': {'_text': 'urn:fdc:peppol.eu:poacc:bis:ordering:3'}, |
| 113 |
'cbc:ID': {'_text': purchase_order.name}, |
| 114 |
'cbc:IssueDate': {'_text': purchase_order.create_date.date()}, |
| 115 |
'cbc:OrderTypeCode': {'_text': '105'}, |
| 116 |
'cbc:Note': {'_text': html2plaintext(purchase_order.note)} if purchase_order.note else None, |
| 117 |
'cbc:DocumentCurrencyCode': {'_text': vals['currency_name']}, |
| 118 |
'cac:QuotationDocumentReference': { |
| 119 |
'cbc:ID': {'_text': purchase_order.partner_ref} |
| 120 |
}, |
| 121 |
}) |
| 122 |
|
| 123 |
def _add_purchase_order_buyer_customer_party_nodes(self, document_node, vals): |
| 124 |
document_node['cac:BuyerCustomerParty'] = { |
| 125 |
'cac:Party': self._get_party_node({**vals, 'partner': vals['customer'], 'role': 'customer'}) |
| 126 |
} |
| 127 |
|
| 128 |
def _add_purchase_order_seller_supplier_party_nodes(self, document_node, vals): |
| 129 |
document_node['cac:SellerSupplierParty'] = { |
| 130 |
'cac:Party': self._get_party_node({**vals, 'partner': vals['supplier'], 'role': 'supplier'}) |
| 131 |
} |
| 132 |
|
| 133 |
def _add_purchase_order_delivery_nodes(self, document_node, vals): |
| 134 |
document_node['cac:Delivery'] = { |
| 135 |
'cac:DeliveryParty': self._get_party_node({**vals, 'partner': vals['partner_shipping'], 'role': 'delivery'}) |
| 136 |
} |
| 137 |
|
| 138 |
def _add_purchase_order_payment_terms_nodes(self, document_node, vals): |
| 139 |
purchase_order = vals['purchase_order'] |
| 140 |
if purchase_order.payment_term_id: |
| 141 |
document_node['cac:PaymentTerms'] = { |
| 142 |
'cbc:Note': {'_text': purchase_order.payment_term_id.name} |
| 143 |
} |
| 144 |
|
| 145 |
def _add_purchase_order_allowance_charge_nodes(self, document_node, vals): |
| 146 |
|
| 147 |
ubl_values = vals['_ubl_values'] |
| 148 |
document_node['cac:AllowanceCharge'] = [ |
| 149 |
self._ubl_get_allowance_charge_early_payment(vals, early_payment_values) |
| 150 |
for early_payment_values in ubl_values['allowance_charges_early_payment_currency'] |
| 151 |
] |
| 152 |
|
| 153 |
def _add_purchase_order_tax_total_nodes(self, document_node, vals): |
| 154 |
|
| 155 |
sub_vals = { |
| 156 |
**vals, |
| 157 |
'document_node': document_node, |
| 158 |
'currency': vals['currency_id'], |
| 159 |
} |
| 160 |
self._ubl_add_tax_totals_nodes(sub_vals) |
| 161 |
|
| 162 |
def _add_purchase_order_monetary_total_nodes(self, document_node, vals): |
| 163 |
ubl_values = vals['_ubl_values'] |
| 164 |
line_tag = self._get_tags_for_document_type(vals)['document_line'] |
| 165 |
|
| 166 |
line_extension_amount = sum( |
| 167 |
line_node['cac:LineItem']['cbc:LineExtensionAmount']['_text'] |
| 168 |
for line_node in document_node[line_tag] |
| 169 |
) |
| 170 |
tax_amount = sum( |
| 171 |
tax_total['cbc:TaxAmount']['_text'] |
| 172 |
for tax_total in document_node['cac:TaxTotal'] |
| 173 |
if tax_total['cbc:TaxAmount']['currencyID'] == vals['currency_id'].name |
| 174 |
) |
| 175 |
total_allowance = sum( |
| 176 |
allowance_charge['cbc:Amount']['_text'] |
| 177 |
for allowance_charge in document_node['cac:AllowanceCharge'] |
| 178 |
if allowance_charge['cbc:ChargeIndicator']['_text'] == 'false' |
| 179 |
) |
| 180 |
total_charge = sum( |
| 181 |
allowance_charge['cbc:Amount']['_text'] |
| 182 |
for allowance_charge in document_node['cac:AllowanceCharge'] |
| 183 |
if allowance_charge['cbc:ChargeIndicator']['_text'] == 'true' |
| 184 |
) |
| 185 |
payable_rounding_amount = ubl_values['payable_rounding_amount_currency'] |
| 186 |
|
| 187 |
document_node['cac:AnticipatedMonetaryTotal'] = { |
| 188 |
'cbc:LineExtensionAmount': { |
| 189 |
'_text': FloatFmt(line_extension_amount, min_dp=vals['currency_dp']), |
| 190 |
'currencyID': vals['currency_name'], |
| 191 |
}, |
| 192 |
'cbc:TaxExclusiveAmount': { |
| 193 |
'_text': FloatFmt(line_extension_amount, min_dp=vals['currency_dp']), |
| 194 |
'currencyID': vals['currency_name'], |
| 195 |
}, |
| 196 |
'cbc:TaxInclusiveAmount': { |
| 197 |
'_text': FloatFmt(line_extension_amount + tax_amount, min_dp=vals['currency_dp']), |
| 198 |
'currencyID': vals['currency_name'], |
| 199 |
}, |
| 200 |
'cbc:AllowanceTotalAmount': { |
| 201 |
'_text': FloatFmt(total_allowance, min_dp=vals['currency_dp']), |
| 202 |
'currencyID': vals['currency_name'], |
| 203 |
} if total_allowance else None, |
| 204 |
'cbc:ChargeTotalAmount': { |
| 205 |
'_text': FloatFmt(total_charge, min_dp=vals['currency_dp']), |
| 206 |
'currencyID': vals['currency_name'], |
| 207 |
} if total_charge else None, |
| 208 |
'cbc:PrepaidAmount': { |
| 209 |
'_text': FloatFmt(0.0, min_dp=vals['currency_dp']), |
| 210 |
'currencyID': vals['currency_name'], |
| 211 |
}, |
| 212 |
'cbc:PayableRoundingAmount': { |
| 213 |
'_text': FloatFmt(payable_rounding_amount, min_dp=vals['currency_dp']), |
| 214 |
'currencyID': vals['currency_name'], |
| 215 |
} if payable_rounding_amount else None, |
| 216 |
'cbc:PayableAmount': { |
| 217 |
'_text': FloatFmt(line_extension_amount + tax_amount, min_dp=vals['currency_dp']), |
| 218 |
'currencyID': vals['currency_name'], |
| 219 |
}, |
| 220 |
} |
| 221 |
|
| 222 |
def _add_purchase_order_line_nodes(self, document_node, vals): |
| 223 |
document_node['cac:OrderLine'] = order_line_nodes = [] |
| 224 |
|
| 225 |
line_idx = 1 |
| 226 |
for base_line in vals['base_lines']: |
| 227 |
line_vals = { |
| 228 |
**vals, |
| 229 |
'line_idx': line_idx, |
| 230 |
'base_line': base_line, |
| 231 |
} |
| 232 |
|
| 233 |
line_node = {} |
| 234 |
self._add_purchase_order_line_id_nodes(line_node, line_vals) |
| 235 |
self._add_purchase_order_line_allowance_charge_nodes(line_node, line_vals) |
| 236 |
self._add_purchase_order_line_amount_nodes(line_node, line_vals) |
| 237 |
self._add_purchase_order_line_item_nodes(line_node, line_vals) |
| 238 |
self._add_purchase_order_line_price_nodes(line_node, line_vals) |
| 239 |
|
| 240 |
order_line_nodes.append({ |
| 241 |
'cac:LineItem': line_node, |
| 242 |
}) |
| 243 |
line_idx += 1 |
| 244 |
|
| 245 |
def _add_purchase_order_line_id_nodes(self, line_node, vals): |
| 246 |
self._add_document_line_id_nodes(line_node, vals) |
| 247 |
|
| 248 |
def _add_purchase_order_line_amount_nodes(self, line_node, vals): |
| 249 |
|
| 250 |
sub_vals = { |
| 251 |
**vals, |
| 252 |
'line_node': line_node, |
| 253 |
'line_vals': { |
| 254 |
'base_line': vals['base_line'], |
| 255 |
}, |
| 256 |
} |
| 257 |
self._ubl_add_line_quantity_node(sub_vals) |
| 258 |
self._ubl_add_line_extension_amount_node(sub_vals) |
| 259 |
|
| 260 |
def _add_purchase_order_line_allowance_charge_nodes(self, line_node, vals): |
| 261 |
|
| 262 |
sub_vals = { |
| 263 |
**vals, |
| 264 |
'line_node': line_node, |
| 265 |
'line_vals': { |
| 266 |
'base_line': vals['base_line'], |
| 267 |
}, |
| 268 |
} |
| 269 |
self._ubl_add_line_allowance_charge_nodes(sub_vals) |
| 270 |
|
| 271 |
|
| 272 |
self._ubl_add_line_allowance_charge_nodes_for_discount(sub_vals) |
| 273 |
|
| 274 |
|
| 275 |
self._ubl_add_line_allowance_charge_nodes_for_recycling_contribution_taxes(sub_vals) |
| 276 |
|
| 277 |
|
| 278 |
self._ubl_add_line_allowance_charge_nodes_for_excise_taxes(sub_vals) |
| 279 |
|
| 280 |
def _ubl_add_line_item_name_description_nodes(self, vals): |
| 281 |
|
| 282 |
super()._ubl_add_line_item_name_description_nodes(vals) |
| 283 |
|
| 284 |
item_node = vals['item_node'] |
| 285 |
base_line = vals['line_vals']['base_line'] |
| 286 |
supplier_info = base_line['supplier_info'] |
| 287 |
if supplier_info.product_name: |
| 288 |
item_node['cbc:Name']['_text'] = supplier_info.product_name |
| 289 |
|
| 290 |
def _ubl_add_line_item_identification_nodes(self, vals): |
| 291 |
|
| 292 |
super()._ubl_add_line_item_identification_nodes(vals) |
| 293 |
|
| 294 |
item_node = vals['item_node'] |
| 295 |
base_line = vals['line_vals']['base_line'] |
| 296 |
supplier_info = base_line['supplier_info'] |
| 297 |
if supplier_info.product_code: |
| 298 |
item_node['cac:SellersItemIdentification'] = { |
| 299 |
'cbc:ID': {'_text': supplier_info.product_code}, |
| 300 |
} |
| 301 |
|
| 302 |
def _add_purchase_order_line_item_nodes(self, line_node, vals): |
| 303 |
|
| 304 |
sub_vals = { |
| 305 |
**vals, |
| 306 |
'line_node': line_node, |
| 307 |
'line_vals': { |
| 308 |
'base_line': vals['base_line'], |
| 309 |
}, |
| 310 |
} |
| 311 |
self._ubl_add_line_item_node(sub_vals) |
| 312 |
|
| 313 |
def _add_purchase_order_line_price_nodes(self, line_node, vals): |
| 314 |
|
| 315 |
base_line = vals['base_line'] |
| 316 |
ubl_values = base_line['_ubl_values'] |
| 317 |
|
| 318 |
line_node['cac:Price'] = { |
| 319 |
'cbc:PriceAmount': { |
| 320 |
'_text': FloatFmt(ubl_values['price_amount_currency'], min_dp=1, max_dp=6), |
| 321 |
'currencyID': vals['currency_name'], |
| 322 |
}, |
| 323 |
} |
| 324 |
|
| 325 |
def _ubl_get_line_allowance_charge_discount_node(self, vals, discount_values): |
| 326 |
|
| 327 |
discount_node = super()._ubl_get_line_allowance_charge_discount_node(vals, discount_values) |
| 328 |
discount_node['cbc:AllowanceChargeReason'] = None |
| 329 |
discount_node['cbc:MultiplierFactorNumeric'] = None |
| 330 |
discount_node['cbc:BaseAmount'] = None |
| 331 |
return discount_node |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
def _retrieve_order_vals(self, order, tree): |
| 338 |
""" Fill order details by extracting details from xml tree. |
| 339 |
param order: Order to fill details from xml tree. |
| 340 |
param tree: Xml tree to extract details. |
| 341 |
:return: list of logs to add warning and information about data from xml. |
| 342 |
""" |
| 343 |
order_vals, logs = super()._retrieve_order_vals(order, tree) |
| 344 |
partner, partner_logs = self._import_partner( |
| 345 |
order.company_id, |
| 346 |
**self._import_retrieve_partner_vals(tree, 'SellerSupplier'), |
| 347 |
) |
| 348 |
if partner: |
| 349 |
order_vals['partner_id'] = partner.id |
| 350 |
order_vals['partner_ref'] = tree.findtext('./{*}ID') |
| 351 |
order_vals['origin'] = tree.findtext('./{*}OriginatorDocumentReference/{*}ID') |
| 352 |
|
| 353 |
delivery_partner, delivery_logs = self._import_partner( |
| 354 |
order.company_id, |
| 355 |
**self._import_retrieve_partner_vals(tree, 'Delivery'), |
| 356 |
) |
| 357 |
if delivery_partner: |
| 358 |
order_vals['dest_address_id'] = delivery_partner.id |
| 359 |
|
| 360 |
allowance_charges_line_vals, allowance_charges_logs = self._import_document_allowance_charges(tree, order, 'purchase') |
| 361 |
lines_vals, line_logs = self._import_lines(order, tree, './{*}OrderLine/{*}LineItem', document_type='order', tax_type='purchase') |
| 362 |
|
| 363 |
for line in lines_vals: |
| 364 |
line['product_qty'] = line.pop('quantity') |
| 365 |
|
| 366 |
line.pop('deferred_start_date', False) |
| 367 |
line.pop('deferred_end_date', False) |
| 368 |
if not line.get('product_id'): |
| 369 |
line_logs.append(_("Could not retrieve the product named: %(name)s", name=line['name'])) |
| 370 |
lines_vals += allowance_charges_line_vals |
| 371 |
|
| 372 |
|
| 373 |
order_vals['order_line'] = [Command.create(line_vals) for line_vals in lines_vals] |
| 374 |
logs += partner_logs + delivery_logs + line_logs + allowance_charges_logs |
| 375 |
|
| 376 |
return order_vals, logs |