From 7550c5c6f49a9837e6036d5d631bcd30d1cfedda Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Thu, 6 Oct 2011 13:36:51 +0200 Subject: [PATCH] [IMP] account: various fixes/review in EDI + auto-email bzr revid: odo@openerp.com-20111006113651-3otsv1n2cwc56xk1 --- addons/account/edi/invoice.py | 95 ++++++++++++---------- addons/account/edi/invoice_action_data.xml | 28 ++++--- addons/account/test/test_edi_invoice.yml | 23 ++++-- 3 files changed, 84 insertions(+), 62 deletions(-) diff --git a/addons/account/edi/invoice.py b/addons/account/edi/invoice.py index a650f0b4537..817bf583516 100644 --- a/addons/account/edi/invoice.py +++ b/addons/account/edi/invoice.py @@ -21,53 +21,59 @@ from osv import fields, osv, orm from edi import EDIMixin -from tools.translate import _ + +INVOICE_LINE_EDI_STRUCT = { + 'name': True, + 'origin': True, + 'uos_id': True, + 'product_id': True, + 'price_unit': True, + 'quantity': True, + 'discount': True, + 'note': True, +} + +INVOICE_TAX_LINE_EDI_STRUCT = { + 'name': True, + 'base': True, + 'amount': True, + 'manual': True, + 'sequence': True, + 'base_amount': True, + 'tax_amount': True, +} + +INVOICE_EDI_STRUCT = { + 'name': True, + 'origin': True, + 'company_id': True, # -> to be changed into partner + 'type': True, # -> reversed at import + 'internal_number': True, # -> reference at import + 'comment': True, + 'date_invoice': True, + 'date_due': True, + 'partner_id': True, + 'payment_term': True, + 'currency_id': True, # TODO: should perhaps include sample rate + rounding + 'invoice_line': INVOICE_LINE_EDI_STRUCT, + 'tax_line': INVOICE_TAX_LINE_EDI_STRUCT, +} class account_invoice(osv.osv, EDIMixin): _inherit = 'account.invoice' def edi_export(self, cr, uid, records, edi_struct=None, context=None): """Exports a supplier or customer invoice""" - edi_struct = { - 'name': True, - 'origin': True, - 'company_id': True, # -> to be changed into partner - 'type': True, # -> reversed at import - 'internal_number': True, # -> reference at import - 'comment': True, - 'date_invoice': True, - 'date_due': True, - 'partner_id': True, - 'address_invoice_id': True, #only one address needed - 'payment_term': True, - 'currency_id': True, # TODO: should perhaps include sample rate + rounding - 'invoice_line': { - 'name': True, - 'origin': True, - 'uos_id': True, - 'product_id': True, - 'price_unit': True, - 'quantity': True, - 'discount': True, - 'note': True, - }, - 'tax_line': { - 'name': True, - 'base': True, - 'amount': True, - 'manual': True, - 'sequence': True, - 'base_amount': True, - 'tax_amount': True, - }, - } + edi_struct = dict(edi_struct or INVOICE_EDI_STRUCT) res_company = self.pool.get('res.company') + res_partner_address = self.pool.get('res.partner.address') edi_doc_list = [] for invoice in records: edi_doc = super(account_invoice,self).edi_export(cr, uid, [invoice], edi_struct, context)[0] edi_doc.update({ 'company_address': res_company.edi_export_address(cr, uid, invoice.company_id, context=context), 'company_paypal_account': invoice.company_id.paypal_account, + 'partner_address': res_partner_address.edi_export(cr, uid, [invoice.address_invoice_id], context=context)[0], #'company_logo': #TODO }) edi_doc_list.append(edi_doc) @@ -101,33 +107,36 @@ class account_invoice(osv.osv, EDIMixin): return account def _edi_import_company(self, cr, uid, edi_document, context=None): + # TODO: for multi-company setups, we currently import the document in the + # user's current company, but we should perhaps foresee a way to select + # the desired company among the user's allowed companies + self._edi_requires_attributes(('company_id','company_address','type'), edi_document) res_partner_address = self.pool.get('res.partner.address') res_partner = self.pool.get('res.partner') # imported company = new partner - company_id, company_name = edi_document['company_id'] - partner_id = self.edi_import_relation(cr, uid, 'res.partner', company_name, - company_id, context=context) + src_company_id, src_company_name = edi_document.pop('company_id') + partner_id = self.edi_import_relation(cr, uid, 'res.partner', src_company_name, + src_company_id, context=context) invoice_type = edi_document['type'] partner_value = {} if invoice_type in ('out_invoice', 'out_refund'): partner_value.update({'customer': True}) if invoice_type in ('in_invoice', 'in_refund'): partner_value.update({'supplier': True}) - partner_id = res_partner.write(cr, uid, [partner_id], partner_value, context=context) + res_partner.write(cr, uid, [partner_id], partner_value, context=context) # imported company_address = new partner address - address_info = edi_document['company_address'] - address_info['partner_id'] = (company_id, company_name) + address_info = edi_document.pop('company_address') + address_info['partner_id'] = (src_company_id, src_company_name) address_info['type'] = 'invoice' address_id = res_partner_address.edi_import(cr, uid, address_info, context=context) # modify edi_document to refer to new partner - del edi_document['company_id'] - del edi_document['company_address'] partner_address = res_partner_address.browse(cr, uid, address_id, context=context) - edi_document['partner_id'] = (company_id, company_name) + edi_document['partner_id'] = (src_company_id, src_company_name) + edi_document.pop('partner_address', False) # ignored edi_document['address_invoice_id'] = self.edi_m2o(cr, uid, partner_address, context=context) return partner_id diff --git a/addons/account/edi/invoice_action_data.xml b/addons/account/edi/invoice_action_data.xml index 32ad7e4e8ff..1152d0bada5 100644 --- a/addons/account/edi/invoice_action_data.xml +++ b/addons/account/edi/invoice_action_data.xml @@ -12,7 +12,7 @@ try: tmpl = self.pool.get('ir.model.data').get_object(cr, uid, 'account', 'email_template_edi_invoice') edi_token = self.pool.get('edi.document').export_edi(cr, uid, [object], context = context)[0] - context.update(edi_web_invoice_url_view='%s/web/view_edi?db=%s&token=%s' %(web_root_url, cr.dbname, edi_token)) + context.update(edi_web_url_view='%s/web/view_edi?db=%s&token=%s' %(web_root_url, cr.dbname, edi_token)) self.pool.get('email.template').send_mail(cr, uid,tmpl.id,object.id,context=context) except: pass @@ -22,7 +22,7 @@ except: ir.actions.server True - EDI Document - Invoice + Auto-email confirmed invoices @@ -51,9 +51,11 @@ except:

  REFERENCES
  Invoice number: ${object.number}
-   Invoice amount: ${object.amount_total} ${object.currency_id.name}
-   Invoice date: ${object.date_invoice or 'n/a'}
-   Order reference: ${object.origin or 'n/a'}
+   Invoice total: ${object.amount_total} ${object.currency_id.name}
+   Invoice date: ${object.date_invoice}
+ % if object.origin: +   Order reference: ${object.origin}
+ % endif   Your contact: ${object.user_id.name}

@@ -61,7 +63,7 @@ except: You can view the invoice document, download it and pay online using the following link:

View Invoice + href="${ctx.get('edi_web_url_view') or ''}">View Invoice % if object.company_id.paypal_account:
@@ -113,18 +115,20 @@ except: Hello${object.address_invoice_id.name and ' ' or ''}${object.address_invoice_id.name or ''}, A new invoice is available for ${object.partner_id.name}: - | Invoice Number: *${object.number}* - | Amount: *${object.amount_total} ${object.currency_id.name}* - | Invoice date: ${object.date_invoice or 'n/a'} - | Order reference: ${object.origin or 'n/a'} + | Invoice number: *${object.number}* + | Invoice total: *${object.amount_total} ${object.currency_id.name}* + | Invoice date: ${object.date_invoice} + % if object.origin: + | Order reference: ${object.origin} + % endif | Your contact: ${object.user_id.name} ${object.user_id.user_email and '<%s>'%(object.user_id.user_email) or ''} You can view the invoice document, download it and pay online using the following link: - ${ctx.get('edi_web_invoice_url_view') or 'n/a'} + ${ctx.get('edi_web_url_view') or 'n/a'} % if object.company_id.paypal_account: It is also possible to directly pay with Paypal: - ${"https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=%s&item_name=OpenERP%%20Invoice%%20%s&invoice=%s&amount=%s&currency_code=%s&button_subtype=services&no_note=1&bn=OpenERP_Invoice_PayNow_%s"%(object.company_id.paypal_account, object.number and object.number.replace('/','%2f') or '', object.number and object.number.replace('/','%2f') or '', object.amount_total, object.currency_id.name, object.currency_id.name)} + ${"https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=%s&item_name=OpenERP%%20Invoice%%20%s&invoice=%s&amount=%s¤cy_code=%s&button_subtype=services&no_note=1&bn=OpenERP_Invoice_PayNow_%s"%(object.company_id.paypal_account, object.number and object.number.replace('/','%2f') or '', object.number and object.number.replace('/','%2f') or '', object.amount_total, object.currency_id.name, object.currency_id.name)} % endif If you have any question, do not hesitate to contact us. diff --git a/addons/account/test/test_edi_invoice.yml b/addons/account/test/test_edi_invoice.yml index 62d57988c9e..23c29b6ac97 100644 --- a/addons/account/test/test_edi_invoice.yml +++ b/addons/account/test/test_edi_invoice.yml @@ -66,8 +66,17 @@ }, "company_id": ["account:b22acf7a-ddcd-11e0-a4db-701a04e25543.res_company_test11", "Thomson pvt. ltd."], "currency_id": ["base:b22acf7a-ddcd-11e0-a4db-701a04e25543.EUR", "EUR (€)"], - "address_invoice_id": ["base:b22acf7a-ddcd-11e0-a4db-701a04e25543.res_partner_address_11", "Sebastien LANGE, France, Alencon, 1 place de l'\u00c9glise"], - "partner_id": ["account:b22acf7a-ddcd-11e0-a4db-701a04e25543.res_partner_test20", "Junjun wala"], + "partner_id": ["account:b22acf7a-ddcd-11e0-a4db-701a04e25543.res_partner_test20", "Junjun wala"], + "partner_address": { + "__id": "base:5af1272e-dd26-11e0-b65e-701a04e25543.res_partner_address_7wdsjasdjh", + "__module": "base", + "__model": "res.partner.address", + "phone": "(+32).81.81.37.00", + "street": "Chaussee de Namur 40", + "city": "Gerompont", + "zip": "1367", + "country_id": ["base:5af1272e-dd26-11e0-b65e-701a04e25543.be", "Belgium"], + }, "date_invoice": "2011-06-22", "name": "sample invoice", "tax_line": [{ @@ -111,18 +120,18 @@ assert invoice_new.type == 'in_invoice', "Invoice type was not set properly" assert len(invoice_new.invoice_line) == 2, "invoice lines are not same" for inv_line in invoice_new.invoice_line: - if inv_line.name == 'basic pc': + if inv_line.name == 'Basic PC': assert inv_line.uos_id.name == "PCE" , "uom is not same" assert inv_line.price_unit == 10 , "price unit is not same" assert inv_line.quantity == 1 , "product qty is not same" assert inv_line.price_subtotal == 10, "price sub total is not same" - elif inv_line.name == 'medium pc': + elif inv_line.name == 'Medium PC': assert inv_line.uos_id.name == "PCE" , "uom is not same" assert inv_line.price_unit == 100 , "price unit is not same" assert inv_line.quantity == 5 , "product qty is not same" assert inv_line.price_subtotal == 500, "price sub total is not same" else: - assert 'wrong product imported in invoice lines' + raise AssertionError('unknown invoice line: %s' % inv_line) for inv_tax in invoice_new.tax_line: - assert inv_tax.manual, "for tax line manual is not set to True" - assert inv_tax.account_id, "for tax_line default accounts is not picked based on the tax config of the DB where it is imported." + assert inv_tax.manual, "tax line not set to manual" + assert inv_tax.account_id, "missing tax line account"