forked from hbrunn/edi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add module base_phone_business_document_import
Add unitests in base_business_document_import Small code enhancements/simplifications
- Loading branch information
1 parent
b311308
commit acd75e7
Showing
15 changed files
with
458 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,17 @@ class BusinessDocumentImport(models.AbstractModel): | |
_name = 'business.document.import' | ||
_description = 'Common methods to import business documents' | ||
|
||
@api.model | ||
def _strip_cleanup_dict(self, match_dict): | ||
if match_dict: | ||
for key, value in match_dict.iteritems(): | ||
if value and isinstance(value, (str, unicode)): | ||
match_dict[key] = value.strip() | ||
if match_dict.get('country_code'): | ||
match_dict['country_code'] = match_dict['country_code'].upper() | ||
if match_dict.get('state_code'): | ||
match_dict['state_code'] = match_dict['state_code'].upper() | ||
|
||
@api.model | ||
def _match_partner( | ||
self, partner_dict, chatter_msg, partner_type='supplier'): | ||
|
@@ -29,9 +40,14 @@ def _match_partner( | |
'email': '[email protected]', | ||
'name': 'Akretion France', | ||
'ref': 'C1242', | ||
'phone': '01.41.98.12.42', | ||
'fax': '01.41.98.12.43', | ||
} | ||
The keys 'phone' and 'fax' are used by the module | ||
base_phone_business_document_import | ||
""" | ||
rpo = self.env['res.partner'] | ||
self._strip_cleanup_dict(partner_dict) | ||
if partner_dict.get('recordset'): | ||
return partner_dict['recordset'] | ||
if partner_dict.get('id'): | ||
|
@@ -69,6 +85,11 @@ def _match_partner( | |
'|', | ||
('state_id', '=', False), | ||
('state_id', '=', states[0].id)] | ||
# Hook to plug alternative matching methods | ||
partner = self._hook_match_partner( | ||
partner_dict, chatter_msg, domain, partner_type_label) | ||
if partner: | ||
return partner | ||
if partner_dict.get('vat'): | ||
vat = partner_dict['vat'].replace(' ', '').upper() | ||
# use base_vat_sanitized | ||
|
@@ -79,7 +100,7 @@ def _match_partner( | |
if partners: | ||
return partners[0] | ||
else: | ||
raise UserError(_( | ||
chatter_msg.append(_( | ||
"The analysis of the business document returned '%s' as " | ||
"%s VAT number. But there are no %s " | ||
"with this VAT number in Odoo.") | ||
|
@@ -139,14 +160,34 @@ def _match_partner( | |
partner_dict.get('ref'), | ||
partner_dict.get('name'))) | ||
|
||
@api.model | ||
def _hook_match_partner( | ||
self, partner_dict, chatter_msg, domain, partner_type_label): | ||
return False | ||
|
||
@api.model | ||
def _match_shipping_partner(self, shipping_dict, partner, chatter_msg): | ||
"""Example: | ||
shipping_dict = { | ||
'partner': { | ||
'email': '[email protected]', | ||
'name': 'Akretion France', | ||
}, | ||
'address': { | ||
'zip': '69100', | ||
'country_code': 'FR', | ||
}, | ||
} | ||
The partner argument is a bit special: it is a fallback in case | ||
shipping_dict['partner'] = {} | ||
""" | ||
rpo = self.env['res.partner'] | ||
if shipping_dict.get('partner'): | ||
partner = self._match_partner( | ||
shipping_dict['partner'], chatter_msg, partner_type=False) | ||
domain = [('parent_id', '=', partner.id)] | ||
address_dict = shipping_dict['address'] | ||
self._strip_cleanup_dict(address_dict) | ||
country = False | ||
if address_dict.get('country_code'): | ||
countries = self.env['res.country'].search([ | ||
|
@@ -173,7 +214,7 @@ def _match_shipping_partner(self, shipping_dict, partner, chatter_msg): | |
('state_id', '=', states[0].id)] | ||
if address_dict.get('zip'): | ||
domain.append(('zip', '=', address_dict['zip'])) | ||
# TODO: sanitize ZIP ? | ||
# sanitize ZIP ? | ||
partners = rpo.search(domain + [('type', '=', 'delivery')]) | ||
if partners: | ||
partner = partners[0] | ||
|
@@ -184,14 +225,15 @@ def _match_shipping_partner(self, shipping_dict, partner, chatter_msg): | |
return partner | ||
|
||
@api.model | ||
def _match_product(self, product_dict, chatter_msg, partner=False): | ||
def _match_product(self, product_dict, chatter_msg, seller=False): | ||
"""Example: | ||
product_dict = { | ||
'ean13': '5449000054227', | ||
'code': 'COCA1L', | ||
} | ||
""" | ||
ppo = self.env['product.product'] | ||
self._strip_cleanup_dict(product_dict) | ||
if product_dict.get('recordset'): | ||
return product_dict['recordset'] | ||
if product_dict.get('id'): | ||
|
@@ -210,9 +252,9 @@ def _match_product(self, product_dict, chatter_msg, partner=False): | |
return products[0] | ||
# WARNING: Won't work for multi-variant products | ||
# because product.supplierinfo is attached to product template | ||
if partner: | ||
if seller: | ||
sinfo = self.env['product.supplierinfo'].search([ | ||
('name', '=', partner.id), | ||
('name', '=', seller.id), | ||
('product_code', '=', product_dict['code']), | ||
]) | ||
if ( | ||
|
@@ -230,7 +272,7 @@ def _match_product(self, product_dict, chatter_msg, partner=False): | |
"Supplier: %s\n") % ( | ||
product_dict.get('ean13'), | ||
product_dict.get('code'), | ||
partner and partner.name or 'None')) | ||
seller and seller.name or 'None')) | ||
|
||
@api.model | ||
def _match_currency(self, currency_dict, chatter_msg): | ||
|
@@ -244,6 +286,7 @@ def _match_currency(self, currency_dict, chatter_msg): | |
if not currency_dict: | ||
currency_dict = {} | ||
rco = self.env['res.currency'] | ||
self._strip_cleanup_dict(currency_dict) | ||
if currency_dict.get('recordset'): | ||
return currency_dict['recordset'] | ||
if currency_dict.get('id'): | ||
|
@@ -262,13 +305,14 @@ def _match_currency(self, currency_dict, chatter_msg): | |
if currency_dict.get('symbol'): | ||
currencies = rco.search( | ||
[('symbol', '=', currency_dict['symbol'])]) | ||
if currencies: | ||
if len(currencies) == 1: | ||
return currencies[0] | ||
else: | ||
raise UserError(_( | ||
chatter_msg.append(_( | ||
"The analysis of the business document returned '%s' as " | ||
"the currency symbol. But there are no currency " | ||
"with that symbol in Odoo.") % currency_dict['symbol']) | ||
"the currency symbol. But there are none or several " | ||
"currencies with that symbol in Odoo.") | ||
% currency_dict['symbol']) | ||
if currency_dict.get('iso_or_symbol'): | ||
currencies = rco.search([ | ||
'|', | ||
|
@@ -283,7 +327,7 @@ def _match_currency(self, currency_dict, chatter_msg): | |
"currency with the symbol nor ISO code in Odoo.") | ||
% currency_dict['iso_or_symbol']) | ||
if currency_dict.get('country_code'): | ||
country_code = currency_dict['country_code'].upper() | ||
country_code = currency_dict['country_code'] | ||
countries = self.env['res.country'].search([ | ||
('code', '=', country_code)]) | ||
if countries: | ||
|
@@ -320,6 +364,7 @@ def _match_uom(self, uom_dict, chatter_msg, product=False): | |
puo = self.env['product.uom'] | ||
if not uom_dict: | ||
uom_dict = {} | ||
self._strip_cleanup_dict(uom_dict) | ||
if uom_dict.get('recordset'): | ||
return uom_dict['recordset'] | ||
if uom_dict.get('id'): | ||
|
@@ -382,6 +427,7 @@ def _match_tax( | |
With l10n_fr, it will return 20% VAT tax. | ||
""" | ||
ato = self.env['account.tax'] | ||
self._strip_cleanup_dict(tax_dict) | ||
if tax_dict.get('recordset'): | ||
return tax_dict['recordset'] | ||
if tax_dict.get('id'): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from . import test_business_document_import |
Oops, something went wrong.