From e26ae4e800d05e3e3045972cb234847c4e11ef82 Mon Sep 17 00:00:00 2001 From: "Amit Parmar (OpenERP)" Date: Tue, 21 Jun 2011 18:59:25 +0530 Subject: [PATCH 1/9] [EDI] refactor code of specific import bzr revid: aar@tinyerp.com-20110621132925-95jijdbuml3st9bo --- addons/account/__openerp__.py | 6 ++++-- addons/account/wizard/__init__.py | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/addons/account/__openerp__.py b/addons/account/__openerp__.py index 039a6800a98..07174e4971f 100644 --- a/addons/account/__openerp__.py +++ b/addons/account/__openerp__.py @@ -121,7 +121,9 @@ module named account_voucher. 'company_view.xml', 'board_account_view.xml', "wizard/account_report_profit_loss_view.xml", - "wizard/account_report_balance_sheet_view.xml" + "wizard/account_report_balance_sheet_view.xml", + 'test/test_edi_invoice.yml', + #'wizard/edi_import_view.xml', ], 'demo_xml': [ 'account_demo.xml', @@ -143,7 +145,7 @@ module named account_voucher. 'test/account_bank_statement.yml', 'test/account_cash_statement.yml', 'test/account_report.yml', - 'test/test_edi_invoice.yml', + ], 'installable': True, diff --git a/addons/account/wizard/__init__.py b/addons/account/wizard/__init__.py index caed6fc3977..0d888f8e712 100644 --- a/addons/account/wizard/__init__.py +++ b/addons/account/wizard/__init__.py @@ -65,6 +65,7 @@ import account_change_currency import account_report_balance_sheet import account_report_profit_loss + # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: From df0bbd0650db82a82711e601128d9b2a4b6ebfe1 Mon Sep 17 00:00:00 2001 From: "Amit Parmar (OpenERP)" Date: Tue, 21 Jun 2011 19:00:26 +0530 Subject: [PATCH 2/9] [EDI] refactor code of specific import bzr revid: aar@tinyerp.com-20110621133026-ll3afhk9qhwx52is --- addons/account/edi_invoice.py | 287 +++++++++++++++++++++++ addons/account/test/test_edi_invoice.yml | 13 + 2 files changed, 300 insertions(+) create mode 100644 addons/account/edi_invoice.py create mode 100644 addons/account/test/test_edi_invoice.yml diff --git a/addons/account/edi_invoice.py b/addons/account/edi_invoice.py new file mode 100644 index 00000000000..956b0d9aa15 --- /dev/null +++ b/addons/account/edi_invoice.py @@ -0,0 +1,287 @@ + +from osv import fields, osv,orm +from base.ir import ir_edi + +class account_invoice(osv.osv,ir_edi.edi): + + _inherit = 'account.invoice' + #_name = 'edi.invoice' + + def edi_export(self, cr, uid, ids, edi_struct=None, context=None): + """Exports a supplier or customer invoice""" + rec_id = [ids[0].id] + + 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, + '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, + }, + } + # Get EDI doc based on this struct. The result will also contain + # all metadata fields and attachments. + + edi_doc = super(account_invoice,self).edi_export(cr, uid, ids, edi_struct, context) + + + for i, invoice in enumerate(self.browse(cr, uid, rec_id, context=context)): + # add specific data for import + inv_comp = invoice.company_id + + + comp_partner = inv_comp.partner_id + + comp_partner_addr = comp_partner.address + + for address in comp_partner_addr: + + + edi_doc[i].update({ + # Add company info and address + 'company_address': { + 'street': address.street, + 'street2': address.street2, + 'zip': address.zip, + 'city': address.city, + 'state_id': self.edi_m2o(cr, uid, address.state_id), + 'country_id': self.edi_m2o(cr, uid, address.country_id), + 'email': address.email, + 'phone': address.phone + }, + # Function fields are not included in normal export + #'company_logo': inv_comp.logo,#TODO + #'paid': inv_comp.paid, #TODO + }) + + return edi_doc + def edi_import(self, cr, uid, edi_document, context=None): + + """ During import, invoices will import the company that is provided in the invoice as + a new partner (e.g. supplier company for a customer invoice will be come a supplier + record for the new invoice. + Summary of tasks that need to be done: + - import company as a new partner, if type==in then supplier=1, else customer=1 + - partner_id field is modified to point to the new partner + - company_address data used to add address to new partner + - change type: out_invoice'<->'in_invoice','out_refund'<->'in_refund' + - reference: should contain the value of the 'internal_number' + - reference_type: 'none' + - internal number: reset to False, auto-generated + - journal_id: should be selected based on type: simply put the 'type' + in the context when calling create(), will be selected correctly + - #payment_term: if set, create a default one based on name... + - for invoice lines, the account_id value should be taken from the + product's default, i.e. from the default category, as it will not + be provided. + - for tax lines, we disconnect from the invoice.line, so all tax lines + will be of type 'manual', and default accounts should be picked based + on the tax config of the DB where it is imported. + """ + # generic implementation! + + partner = self.pool.get('res.partner') + partner_add = self.pool.get('res.partner.address') + model_data = self.pool.get('ir.model.data') + product_obj = self.pool.get('product.product') + product_categ = self.pool.get('product.category') + acc_invoice = self.pool.get('account.invoice') + company = self.pool.get('res.company') + tax_id = [] + account_id = [] + partner_id = None + company_id = None + if context is None: + context = {} + for field in edi_document.keys(): + + if field == 'type': + if len(edi_document['invoice_line']): + name = edi_document['invoice_line'][0]['product_id'][1] + else: + name = None + re_ids = product_obj.search(cr,uid,[('name','=',name)]) + if edi_document['type'] == 'out_invoice' or edi_document['type'] == 'out_refund': + if re_ids: + if product_obj.browse(cr,uid,re_ids)[0].property_account_expense: + account_id = product_obj.browse(cr,uid,re_ids)[0].property_account_expense + + else: + account_id = product_categ.browse(cr,uid,re_ids)[0].property_account_expense_categ + if product_obj.browse(cr,uid,re_ids)[0].taxes_id: + tax_id = product_obj.browse(cr, uid,re_ids)[0].taxes_id + if edi_document['type'] == 'out_refund': + edi_document['type'] = 'in_refund' + else: + edi_document['type'] = 'in_invoice' + + + elif edi_document['type'] == 'in_invoice' or edi_document['type'] == 'in_refund': + if re_ids: + if product_obj.browse(cr,uid,re_ids)[0].property_account_income: + account_id = product_obj.browse(cr,uid,re_ids)[0].property_account_income + else: + account_id = product_categ.browse(cr,uid,re_ids)[0].property_account_income_categ + if product_obj.browse(cr,uid,re_ids)[0].taxes_id: + tax_id = product_obj.browse(cr, uid,re_ids)[0].taxes_id + if edi_document['type'] == 'in_refund': + edi_document['type'] = 'out_refund' + else: + edi_document['type'] = 'out_invoice' + + if account_id: + name_ids = model_data.search(cr, uid, [('model','=',account_id._name),('res_id','=',account_id.id)]) + if name_ids: + xml_id = model_data.browse(cr, uid, name_ids)[0].name + db_uuid = ir_edi.safe_unique_id(account_id._name,account_id.id) + edi_document['invoice_line'][0]['account_id'] = [db_uuid+':'+xml_id,account_id.name] + + if tax_id: + name_ids = model_data.search(cr, uid, [('model','=',tax_id[0]._name),('res_id','=',tax_id[0].id)]) + + if name_ids: + xml_id = model_data.browse(cr, uid, name_ids)[0].name + db_uuid = ir_edi.safe_unique_id(tax_id[0]._name,tax_id[0].id) + edi_document['tax_line'][0]['account_id'] = [db_uuid+':'+xml_id,tax_id[0].name] + + else: + if len(edi_document['tax_line']): + edi_document['tax_line'][0]['manual'] = True + + res = {} + part = {} + comp = {} + partner_id = partner.search(cr,uid,[('name','=',edi_document['company_id'][1])]) + + if len(partner_id): + + browse_partner = partner.browse(cr,uid,partner_id[0]) + u_id = model_data.search(cr, uid, [('res_id','=',browse_partner.id),('model','=',browse_partner._name)]) + if len(u_id): + company_id = browse_partner.company_id + xml_obj = model_data.browse(cr,uid,u_id[0]) + uuid = ir_edi.safe_unique_id(browse_partner._name,browse_partner.id) + db_uuid = '%s:%s' % (uuid,xml_obj.name) + part.update({'partner_id':[db_uuid,browse_partner.name]}) + + else: + company_id = company.create(cr, uid, {'name':edi_document['company_id'][1]}) + add_id = partner_add.create(cr,uid,edi_document['company_address']) + res.update({'name': edi_document['company_id'][1],'supplier': True, 'partner_id': edi_document['partner_id'],'address':add_id}) + partner_id = partner.create(cr,uid,res) + + browse_partner = partner.browse(cr,uid,partner_id[0]) + + u_id = model_data.search(cr, uid, [('res_id','=',browse_partner.id),('model','=',browse_partner._name)]) + + + if len(u_id): + + xml_obj = model_data.browse(cr,uid,u_id[0]) + uuid = ir_edi.safe_unique_id(browse_partner._name,browse_partner.id) + db_uuid = '%s:%s' % (uuid,xml_obj.name) + part.update({'partner_id':[db_uuid,browse_partner.name]}) + + comp_id = partner.search(cr,uid,[('name','=',edi_document['partner_id'][1])]) + + if len(comp_id): + + browse_partner = partner.browse(cr,uid,comp_id[0]) + + browse_company = company.browse(cr,uid,browse_partner.company_id.id) + + u_id = u_id = model_data.search(cr, uid, [('res_id','=',browse_company.id),('model','=',browse_company._name)]) + if len(u_id): + + xml_obj = model_data.browse(cr,uid,u_id[0]) + uuid = ir_edi.safe_unique_id(browse_company._name,browse_company.id) + db_uuid = '%s:%s' % (uuid,xml_obj.name) + comp.update({'company_id':[db_uuid,browse_company.name]}) + + del edi_document['partner_id'] + del edi_document['company_id'] + edi_document.update(part) + edi_document.update(comp) + if len(partner_id): + + + + p = self.pool.get('res.partner').browse(cr, uid, partner_id[0]) + if company_id: + if p.property_account_receivable.company_id.id != company_id.id and p.property_account_payable.company_id.id != company_id.id: + property_obj = self.pool.get('ir.property') + + rec_pro_id = property_obj.search(cr,uid,[('name','=','property_account_receivable'),('res_id','=','res.partner,'+str(partner_id)+''),('company_id','=',company_id.id)]) + pay_pro_id = property_obj.search(cr,uid,[('name','=','property_account_payable'),('res_id','=','res.partner,'+str(partner_id)+''),('company_id','=',company_id)]) + if not rec_pro_id: + rec_pro_id = property_obj.search(cr,uid,[('name','=','property_account_receivable'),('company_id','=',company_id.id)]) + if not pay_pro_id: + pay_pro_id = property_obj.search(cr,uid,[('name','=','property_account_payable'),('company_id','=',company_id.id)]) + rec_line_data = property_obj.read(cr,uid,rec_pro_id,['name','value_reference','res_id']) + pay_line_data = property_obj.read(cr,uid,pay_pro_id,['name','value_reference','res_id']) + rec_res_id = rec_line_data and rec_line_data[0].get('value_reference',False) and int(rec_line_data[0]['value_reference'].split(',')[1]) or False + pay_res_id = pay_line_data and pay_line_data[0].get('value_reference',False) and int(pay_line_data[0]['value_reference'].split(',')[1]) or False + if not rec_res_id and not pay_res_id: + raise osv.except_osv(_('Configuration Error !'), + _('Can not find account chart for this company, Please Create account.')) + account_obj = self.pool.get('account.account') + rec_obj_acc = account_obj.browse(cr, uid, [rec_res_id]) + pay_obj_acc = account_obj.browse(cr, uid, [pay_res_id]) + p.property_account_receivable = rec_obj_acc[0] + p.property_account_payable = pay_obj_acc[0] + + if edi_document['type'] in ('out_invoice', 'out_refund'): + acc_obj = p.property_account_receivable + else: + acc_obj = p.property_account_payable + res_id = model_data.search(cr,uid,[('model','=',acc_obj._name),('res_id','=',acc_obj.id)]) + if len(res_id): + xml_obj = model_data.browse(cr, uid, res_id[0]) + uuid = ir_edi.safe_unique_id(acc_obj._name,acc_obj.id) + db_uuid = '%s:%s' % (uuid,xml_obj.name) + edi_document.update({'account_id':[db_uuid,acc_obj.name]}) + print "account_id",edi_document['account_id'] + edi_document.update({'reference':edi_document['internal_number'],'reference_type' : 'none'}) + edi_document['internal_number'] = False + context['type'] = edi_document['type'] + comp + del edi_document['company_address'] + + + return super(account_invoice,self).edi_import(cr, uid, edi_document) + +account_invoice() + +class account_invoice_line(osv.osv, ir_edi.edi): + _inherit='account.invoice.line' + +account_invoice_line() +class account_invoice_tax(osv.osv, ir_edi.edi): + _inherit = "account.invoice.tax" + +account_invoice_tax() diff --git a/addons/account/test/test_edi_invoice.yml b/addons/account/test/test_edi_invoice.yml new file mode 100644 index 00000000000..af23dd3e57a --- /dev/null +++ b/addons/account/test/test_edi_invoice.yml @@ -0,0 +1,13 @@ +- + Tesing of Export functionality +- + !python {model: ir.edi.document}: | + invoice_obj = self.pool.get('account.invoice') + invoice_ids = invoice_obj.search(cr, uid, []) + print "invoices>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>",invoice_ids + if invoice_ids: + invoices = invoice_obj.browse(cr, uid, invoice_ids) + tokens = self.export_edi(cr, uid, invoices) + for token in tokens: + document = self.get_document(cr, uid, token, context=context) + a = self.import_edi(cr, uid, edi_document = document) From 7f241bd2e7db48a791a65bc0ce83fd2d7cbf34ca Mon Sep 17 00:00:00 2001 From: "Amit Parmar (OpenERP)" Date: Wed, 22 Jun 2011 14:44:25 +0530 Subject: [PATCH 3/9] [EDI] refactor code of specific edi import and export bzr revid: aar@tinyerp.com-20110622091425-b1ldh2oduyglaoa1 --- addons/account/__openerp__.py | 13 ++++++------ addons/account/edi_invoice.py | 39 ++++------------------------------- 2 files changed, 10 insertions(+), 42 deletions(-) diff --git a/addons/account/__openerp__.py b/addons/account/__openerp__.py index 07174e4971f..b2996976c50 100644 --- a/addons/account/__openerp__.py +++ b/addons/account/__openerp__.py @@ -122,14 +122,13 @@ module named account_voucher. 'board_account_view.xml', "wizard/account_report_profit_loss_view.xml", "wizard/account_report_balance_sheet_view.xml", - 'test/test_edi_invoice.yml', - #'wizard/edi_import_view.xml', + ], 'demo_xml': [ - 'account_demo.xml', - 'project/project_demo.xml', - 'project/analytic_account_demo.xml', - 'demo/account_minimal.xml', + #'account_demo.xml', + #'project/project_demo.xml', + #'project/analytic_account_demo.xml', + #'demo/account_minimal.xml', # 'account_unit_test.xml', ], 'test': [ @@ -145,7 +144,7 @@ module named account_voucher. 'test/account_bank_statement.yml', 'test/account_cash_statement.yml', 'test/account_report.yml', - + 'test/test_edi_invoice.yml', ], 'installable': True, diff --git a/addons/account/edi_invoice.py b/addons/account/edi_invoice.py index 956b0d9aa15..56d0e52df95 100644 --- a/addons/account/edi_invoice.py +++ b/addons/account/edi_invoice.py @@ -47,21 +47,13 @@ class account_invoice(osv.osv,ir_edi.edi): # Get EDI doc based on this struct. The result will also contain # all metadata fields and attachments. - edi_doc = super(account_invoice,self).edi_export(cr, uid, ids, edi_struct, context) - - + edi_doc = super(account_invoice,self).edi_export(cr, uid, ids, edi_struct, context) for i, invoice in enumerate(self.browse(cr, uid, rec_id, context=context)): # add specific data for import inv_comp = invoice.company_id - - comp_partner = inv_comp.partner_id - comp_partner_addr = comp_partner.address - for address in comp_partner_addr: - - edi_doc[i].update({ # Add company info and address 'company_address': { @@ -119,7 +111,6 @@ class account_invoice(osv.osv,ir_edi.edi): if context is None: context = {} for field in edi_document.keys(): - if field == 'type': if len(edi_document['invoice_line']): name = edi_document['invoice_line'][0]['product_id'][1] @@ -130,7 +121,6 @@ class account_invoice(osv.osv,ir_edi.edi): if re_ids: if product_obj.browse(cr,uid,re_ids)[0].property_account_expense: account_id = product_obj.browse(cr,uid,re_ids)[0].property_account_expense - else: account_id = product_categ.browse(cr,uid,re_ids)[0].property_account_expense_categ if product_obj.browse(cr,uid,re_ids)[0].taxes_id: @@ -138,9 +128,7 @@ class account_invoice(osv.osv,ir_edi.edi): if edi_document['type'] == 'out_refund': edi_document['type'] = 'in_refund' else: - edi_document['type'] = 'in_invoice' - - + edi_document['type'] = 'in_invoice' elif edi_document['type'] == 'in_invoice' or edi_document['type'] == 'in_refund': if re_ids: if product_obj.browse(cr,uid,re_ids)[0].property_account_income: @@ -163,12 +151,10 @@ class account_invoice(osv.osv,ir_edi.edi): if tax_id: name_ids = model_data.search(cr, uid, [('model','=',tax_id[0]._name),('res_id','=',tax_id[0].id)]) - if name_ids: xml_id = model_data.browse(cr, uid, name_ids)[0].name db_uuid = ir_edi.safe_unique_id(tax_id[0]._name,tax_id[0].id) edi_document['tax_line'][0]['account_id'] = [db_uuid+':'+xml_id,tax_id[0].name] - else: if len(edi_document['tax_line']): edi_document['tax_line'][0]['manual'] = True @@ -177,9 +163,7 @@ class account_invoice(osv.osv,ir_edi.edi): part = {} comp = {} partner_id = partner.search(cr,uid,[('name','=',edi_document['company_id'][1])]) - if len(partner_id): - browse_partner = partner.browse(cr,uid,partner_id[0]) u_id = model_data.search(cr, uid, [('res_id','=',browse_partner.id),('model','=',browse_partner._name)]) if len(u_id): @@ -188,20 +172,14 @@ class account_invoice(osv.osv,ir_edi.edi): uuid = ir_edi.safe_unique_id(browse_partner._name,browse_partner.id) db_uuid = '%s:%s' % (uuid,xml_obj.name) part.update({'partner_id':[db_uuid,browse_partner.name]}) - else: company_id = company.create(cr, uid, {'name':edi_document['company_id'][1]}) add_id = partner_add.create(cr,uid,edi_document['company_address']) res.update({'name': edi_document['company_id'][1],'supplier': True, 'partner_id': edi_document['partner_id'],'address':add_id}) partner_id = partner.create(cr,uid,res) - browse_partner = partner.browse(cr,uid,partner_id[0]) - u_id = model_data.search(cr, uid, [('res_id','=',browse_partner.id),('model','=',browse_partner._name)]) - - if len(u_id): - xml_obj = model_data.browse(cr,uid,u_id[0]) uuid = ir_edi.safe_unique_id(browse_partner._name,browse_partner.id) db_uuid = '%s:%s' % (uuid,xml_obj.name) @@ -210,14 +188,10 @@ class account_invoice(osv.osv,ir_edi.edi): comp_id = partner.search(cr,uid,[('name','=',edi_document['partner_id'][1])]) if len(comp_id): - browse_partner = partner.browse(cr,uid,comp_id[0]) - browse_company = company.browse(cr,uid,browse_partner.company_id.id) - u_id = u_id = model_data.search(cr, uid, [('res_id','=',browse_company.id),('model','=',browse_company._name)]) if len(u_id): - xml_obj = model_data.browse(cr,uid,u_id[0]) uuid = ir_edi.safe_unique_id(browse_company._name,browse_company.id) db_uuid = '%s:%s' % (uuid,xml_obj.name) @@ -228,9 +202,6 @@ class account_invoice(osv.osv,ir_edi.edi): edi_document.update(part) edi_document.update(comp) if len(partner_id): - - - p = self.pool.get('res.partner').browse(cr, uid, partner_id[0]) if company_id: if p.property_account_receivable.company_id.id != company_id.id and p.property_account_payable.company_id.id != company_id.id: @@ -265,14 +236,12 @@ class account_invoice(osv.osv,ir_edi.edi): uuid = ir_edi.safe_unique_id(acc_obj._name,acc_obj.id) db_uuid = '%s:%s' % (uuid,xml_obj.name) edi_document.update({'account_id':[db_uuid,acc_obj.name]}) - print "account_id",edi_document['account_id'] + edi_document.update({'reference':edi_document['internal_number'],'reference_type' : 'none'}) edi_document['internal_number'] = False context['type'] = edi_document['type'] - comp + del edi_document['company_address'] - - return super(account_invoice,self).edi_import(cr, uid, edi_document) account_invoice() From 1d98e87dcca53ad9b89007d602734ec1b629821f Mon Sep 17 00:00:00 2001 From: "Amit Parmar (OpenERP)" Date: Wed, 22 Jun 2011 15:44:47 +0530 Subject: [PATCH 4/9] [EDI] revised code of specific import and export bzr revid: aar@tinyerp.com-20110622101447-qm824uxvcedyjgd4 --- addons/account/__openerp__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/addons/account/__openerp__.py b/addons/account/__openerp__.py index b2996976c50..651c426dc80 100644 --- a/addons/account/__openerp__.py +++ b/addons/account/__openerp__.py @@ -125,10 +125,10 @@ module named account_voucher. ], 'demo_xml': [ - #'account_demo.xml', - #'project/project_demo.xml', - #'project/analytic_account_demo.xml', - #'demo/account_minimal.xml', + 'account_demo.xml', + 'project/project_demo.xml', + 'project/analytic_account_demo.xml', + 'demo/account_minimal.xml', # 'account_unit_test.xml', ], 'test': [ From 77b2a20d81980235353b5e33f7b8897bfbe355b9 Mon Sep 17 00:00:00 2001 From: "Amit Parmar (OpenERP)" Date: Thu, 23 Jun 2011 19:12:07 +0530 Subject: [PATCH 5/9] [EDI]:customer invoice testing through yml bzr revid: aar@tinyerp.com-20110623134207-soeh9tgaknfhp6hf --- addons/account/__openerp__.py | 3 +- addons/account/test/test_edi_invoice.yml | 64 ++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/addons/account/__openerp__.py b/addons/account/__openerp__.py index 651c426dc80..1bb1b56a476 100644 --- a/addons/account/__openerp__.py +++ b/addons/account/__openerp__.py @@ -122,8 +122,7 @@ module named account_voucher. 'board_account_view.xml', "wizard/account_report_profit_loss_view.xml", "wizard/account_report_balance_sheet_view.xml", - - ], + ], 'demo_xml': [ 'account_demo.xml', 'project/project_demo.xml', diff --git a/addons/account/test/test_edi_invoice.yml b/addons/account/test/test_edi_invoice.yml index af23dd3e57a..9d624489c8c 100644 --- a/addons/account/test/test_edi_invoice.yml +++ b/addons/account/test/test_edi_invoice.yml @@ -1,13 +1,71 @@ - - Tesing of Export functionality + create Company +- + !record {model: res.company, id: res_company_test}: + name: Thomson pvt. ltd. + partner_id: 1 + rml_header: 1 + rml_header2: 1 + rml_header3: 1 + currency_id: 1 + +- + create customer +- + !record {model: res.partner, id: res_partner_test}: + name: Junjun wala + supplier: False + company_id: res_company_test + address: + - partner_id: res_partner_test + type: default + name: ivan + street: m g road + street2: line 28 + city: paris + +- + Creating a customer invoice record +- + !record {model: account.invoice, id: account_invoice_test}: + amount_tax: 0.0 + amount_total: 83.0 + amount_untaxed: 83.0 + currency_id: base.EUR + date_invoice: '2011-06-22' + invoice_line: + name: '[CPU1] Processor AMD Athlon XP 1800+' + partner_id: res_partner_test + price_subtotal: 75.0 + price_unit: 75.0 + product_id: product.product_product_cpu1 + quantity: 1.0 + journal_id: 'False' + name: Nothing + partner_id: res_partner_test + payment_term: account.account_payment_term + reference_type: none + +- + Tesing of Export functionality and Import Functionality - !python {model: ir.edi.document}: | + invoice_obj = self.pool.get('account.invoice') - invoice_ids = invoice_obj.search(cr, uid, []) - print "invoices>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>",invoice_ids + invoice_ids = invoice_obj.search(cr, uid, [partner_id]) if invoice_ids: invoices = invoice_obj.browse(cr, uid, invoice_ids) tokens = self.export_edi(cr, uid, invoices) for token in tokens: document = self.get_document(cr, uid, token, context=context) a = self.import_edi(cr, uid, edi_document = document) + +- + Check after import of document customer become supplier or not +- + !python {model: account.invoice}: | + ids=self.search(cr, uid, [('partner_id','=',base.main_company),('company_id','=',res_company_test),('type','=','in_invoice')]) + if not ids: + raise AssertionError("Invoice is not imported") + + \ No newline at end of file From 66356f01af42f48df61064ae1fc5ff1f0453de92 Mon Sep 17 00:00:00 2001 From: "Amit Parmar (OpenERP)" Date: Fri, 24 Jun 2011 18:41:17 +0530 Subject: [PATCH 6/9] [EDI] Final Tesing Ymal bzr revid: aar@tinyerp.com-20110624131117-x517otc1yc1f2r97 --- addons/account/__openerp__.py | 1 + addons/account/edi_invoice.py | 60 ++++++++++++++++--- addons/account/test/test_edi_invoice.yml | 76 +++++++++++------------- 3 files changed, 86 insertions(+), 51 deletions(-) diff --git a/addons/account/__openerp__.py b/addons/account/__openerp__.py index 1bb1b56a476..44537ad638a 100644 --- a/addons/account/__openerp__.py +++ b/addons/account/__openerp__.py @@ -122,6 +122,7 @@ module named account_voucher. 'board_account_view.xml', "wizard/account_report_profit_loss_view.xml", "wizard/account_report_balance_sheet_view.xml", + ], 'demo_xml': [ 'account_demo.xml', diff --git a/addons/account/edi_invoice.py b/addons/account/edi_invoice.py index 56d0e52df95..189604e6306 100644 --- a/addons/account/edi_invoice.py +++ b/addons/account/edi_invoice.py @@ -1,6 +1,7 @@ -from osv import fields, osv,orm +from osv import fields, osv, orm from base.ir import ir_edi +from tools.translate import _ class account_invoice(osv.osv,ir_edi.edi): @@ -96,6 +97,7 @@ class account_invoice(osv.osv,ir_edi.edi): on the tax config of the DB where it is imported. """ # generic implementation! + "" partner = self.pool.get('res.partner') partner_add = self.pool.get('res.partner.address') @@ -104,6 +106,7 @@ class account_invoice(osv.osv,ir_edi.edi): product_categ = self.pool.get('product.category') acc_invoice = self.pool.get('account.invoice') company = self.pool.get('res.company') + country = self.pool.get('res.country') tax_id = [] account_id = [] partner_id = None @@ -168,16 +171,47 @@ class account_invoice(osv.osv,ir_edi.edi): u_id = model_data.search(cr, uid, [('res_id','=',browse_partner.id),('model','=',browse_partner._name)]) if len(u_id): company_id = browse_partner.company_id + xml_obj = model_data.browse(cr,uid,u_id[0]) uuid = ir_edi.safe_unique_id(browse_partner._name,browse_partner.id) db_uuid = '%s:%s' % (uuid,xml_obj.name) part.update({'partner_id':[db_uuid,browse_partner.name]}) + else: + + company_address = {} company_id = company.create(cr, uid, {'name':edi_document['company_id'][1]}) - add_id = partner_add.create(cr,uid,edi_document['company_address']) - res.update({'name': edi_document['company_id'][1],'supplier': True, 'partner_id': edi_document['partner_id'],'address':add_id}) - partner_id = partner.create(cr,uid,res) + + for key in edi_document['company_address'].keys(): + if type(edi_document['company_address'][key]).__name__ == 'list': + if edi_document['company_address'][key][1] is not None: + country_id = country.search(cr ,uid,[('name','=',edi_document['company_address'][key][1])]) + + if len(country_id): + company_address.update({key : country_id[0]}) + + else: + if isinstance(edi_document['company_address'][key][1],unicode): + country_name = str(edi_document['company_address'][key][1]) + country_code = country_name[:2].upper() + country_id = country.create(cr, uid, {'code': country_code,name: country_name}) + company_address.update({key : country_id[0]}) + else: + company_address.update({key : edi_document['company_address'][key][1]}) + else: + company_address.update({key:edi_document['company_address'][key]}) + + add_id = [] + partner_id = [] + + add_id = partner_add.create(cr,uid,company_address) + + res.update({'name': edi_document['company_id'][1],'supplier': True,'address': [unicode(add_id)], 'company_id': unicode(company_id),'country' : country_id}) + + partner_id.append(partner.create(cr,uid,{'name': edi_document['company_id'][1],'supplier': True,'address': unicode(add_id), 'company_id': unicode(company_id),'country' : country_id})) + browse_partner = partner.browse(cr,uid,partner_id[0]) + company_id = browse_partner.company_id u_id = model_data.search(cr, uid, [('res_id','=',browse_partner.id),('model','=',browse_partner._name)]) if len(u_id): xml_obj = model_data.browse(cr,uid,u_id[0]) @@ -200,15 +234,21 @@ class account_invoice(osv.osv,ir_edi.edi): del edi_document['partner_id'] del edi_document['company_id'] edi_document.update(part) - edi_document.update(comp) + edi_document.update(comp) + + if len(partner_id): p = self.pool.get('res.partner').browse(cr, uid, partner_id[0]) + + partner_id = int(partner_id[0]) + if company_id: if p.property_account_receivable.company_id.id != company_id.id and p.property_account_payable.company_id.id != company_id.id: property_obj = self.pool.get('ir.property') rec_pro_id = property_obj.search(cr,uid,[('name','=','property_account_receivable'),('res_id','=','res.partner,'+str(partner_id)+''),('company_id','=',company_id.id)]) - pay_pro_id = property_obj.search(cr,uid,[('name','=','property_account_payable'),('res_id','=','res.partner,'+str(partner_id)+''),('company_id','=',company_id)]) + pay_pro_id = property_obj.search(cr,uid,[('name','=','property_account_payable'),('res_id','=','res.partner,'+str(partner_id)+''),('company_id','=',company_id.id)]) + if not rec_pro_id: rec_pro_id = property_obj.search(cr,uid,[('name','=','property_account_receivable'),('company_id','=',company_id.id)]) if not pay_pro_id: @@ -218,8 +258,8 @@ class account_invoice(osv.osv,ir_edi.edi): rec_res_id = rec_line_data and rec_line_data[0].get('value_reference',False) and int(rec_line_data[0]['value_reference'].split(',')[1]) or False pay_res_id = pay_line_data and pay_line_data[0].get('value_reference',False) and int(pay_line_data[0]['value_reference'].split(',')[1]) or False if not rec_res_id and not pay_res_id: - raise osv.except_osv(_('Configuration Error !'), - _('Can not find account chart for this company, Please Create account.')) + raise osv.except_osv(_('Configuration Error !'), _('Can not find account chart for this company, Please Create account.')) + account_obj = self.pool.get('account.account') rec_obj_acc = account_obj.browse(cr, uid, [rec_res_id]) pay_obj_acc = account_obj.browse(cr, uid, [pay_res_id]) @@ -230,7 +270,9 @@ class account_invoice(osv.osv,ir_edi.edi): acc_obj = p.property_account_receivable else: acc_obj = p.property_account_payable + res_id = model_data.search(cr,uid,[('model','=',acc_obj._name),('res_id','=',acc_obj.id)]) + if len(res_id): xml_obj = model_data.browse(cr, uid, res_id[0]) uuid = ir_edi.safe_unique_id(acc_obj._name,acc_obj.id) @@ -240,7 +282,7 @@ class account_invoice(osv.osv,ir_edi.edi): edi_document.update({'reference':edi_document['internal_number'],'reference_type' : 'none'}) edi_document['internal_number'] = False context['type'] = edi_document['type'] - + del edi_document['company_address'] return super(account_invoice,self).edi_import(cr, uid, edi_document) diff --git a/addons/account/test/test_edi_invoice.yml b/addons/account/test/test_edi_invoice.yml index 9d624489c8c..ec0dead338c 100644 --- a/addons/account/test/test_edi_invoice.yml +++ b/addons/account/test/test_edi_invoice.yml @@ -1,7 +1,7 @@ - create Company - - !record {model: res.company, id: res_company_test}: + !record {model: res.company, id: res_company_test11}: name: Thomson pvt. ltd. partner_id: 1 rml_header: 1 @@ -10,62 +10,54 @@ currency_id: 1 - - create customer + create partner - - !record {model: res.partner, id: res_partner_test}: + !record {model: res.partner, id: res_partner_test20}: name: Junjun wala supplier: False - company_id: res_company_test - address: - - partner_id: res_partner_test - type: default - name: ivan - street: m g road - street2: line 28 - city: paris + company_id: res_company_test11 -- - Creating a customer invoice record -- - !record {model: account.invoice, id: account_invoice_test}: - amount_tax: 0.0 - amount_total: 83.0 - amount_untaxed: 83.0 +- + create customer invoice +- + !record {model: account.invoice, id: customer_invoice_test}: + journal_id: 1 + partner_id: res_partner_test20 currency_id: base.EUR + address_invoice_id: base.res_partner_address_11 + company_id: base.main_company + account_id: 1 date_invoice: '2011-06-22' - invoice_line: - name: '[CPU1] Processor AMD Athlon XP 1800+' - partner_id: res_partner_test - price_subtotal: 75.0 - price_unit: 75.0 - product_id: product.product_product_cpu1 - quantity: 1.0 - journal_id: 'False' name: Nothing - partner_id: res_partner_test - payment_term: account.account_payment_term - reference_type: none - + type: 'out_invoice' + invoice_line: + - product_id: product.product_product_pc1 + partner_id: res_partner_test20 + uos_id: 1 + quantity: 1.0 + price_unit: 10.0 + name: 'basic pc' + account_id: 1 + - Tesing of Export functionality and Import Functionality - !python {model: ir.edi.document}: | invoice_obj = self.pool.get('account.invoice') - invoice_ids = invoice_obj.search(cr, uid, [partner_id]) - if invoice_ids: - invoices = invoice_obj.browse(cr, uid, invoice_ids) - tokens = self.export_edi(cr, uid, invoices) - for token in tokens: - document = self.get_document(cr, uid, token, context=context) - a = self.import_edi(cr, uid, edi_document = document) - + #invoice_ids = invoice_obj.search(cr, uid, ['account.customer_invoice_test']) + #if invoice_ids: + invoices = invoice_obj.browse(cr, uid, [ref("customer_invoice_test")]) + tokens = self.export_edi(cr, uid, invoices) + for token in tokens: + document = self.get_document(cr, uid, token, context=context) + a = self.import_edi(cr, uid, edi_document = document) + - - Check after import of document customer become supplier or not + Check the customer invoice is exported or not - !python {model: account.invoice}: | - ids=self.search(cr, uid, [('partner_id','=',base.main_company),('company_id','=',res_company_test),('type','=','in_invoice')]) + ids=self.search(cr, uid, [('partner_id','=',ref("res_partner_test20")),('company_id','=',ref("res_company_test11")),('type','=','out_invoice')]) if not ids: raise AssertionError("Invoice is not imported") - - \ No newline at end of file + From b2f73c0010ae84da417906a28300120b3571eec6 Mon Sep 17 00:00:00 2001 From: "Harry (OpenERP)" Date: Mon, 27 Jun 2011 14:02:28 +0530 Subject: [PATCH 7/9] [REVIEW+IMP] account, EDI: clean export_edi method of account.invoice bzr revid: hmo@tinyerp.com-20110627083228-cmiuptih2y8kjlqd --- addons/account/__openerp__.py | 5 +- addons/account/edi_invoice.py | 78 ++++++++++++++---------- addons/account/test/test_edi_invoice.yml | 1 + 3 files changed, 49 insertions(+), 35 deletions(-) diff --git a/addons/account/__openerp__.py b/addons/account/__openerp__.py index 44537ad638a..5ec724ed462 100644 --- a/addons/account/__openerp__.py +++ b/addons/account/__openerp__.py @@ -143,10 +143,9 @@ module named account_voucher. 'test/account_fiscalyear_close.yml', 'test/account_bank_statement.yml', 'test/account_cash_statement.yml', - 'test/account_report.yml', 'test/test_edi_invoice.yml', - - ], + 'test/account_report.yml', + ], 'installable': True, 'active': False, 'certificate': '0080331923549', diff --git a/addons/account/edi_invoice.py b/addons/account/edi_invoice.py index 7020a8115ad..ba3675a15f4 100644 --- a/addons/account/edi_invoice.py +++ b/addons/account/edi_invoice.py @@ -23,13 +23,11 @@ from osv import fields, osv, orm from base.ir import ir_edi from tools.translate import _ -class account_invoice(osv.osv,ir_edi.edi): +class account_invoice(osv.osv, ir_edi.edi): _inherit = 'account.invoice' - def edi_export(self, cr, uid, ids, edi_struct=None, context=None): + def edi_export(self, cr, uid, records, edi_struct=None, context=None): """Exports a supplier or customer invoice""" - rec_id = [ids[0].id] - edi_struct = { 'name': True, 'origin': True, @@ -63,35 +61,51 @@ class account_invoice(osv.osv,ir_edi.edi): 'tax_amount': True, }, } - # Get EDI doc based on this struct. The result will also contain - # all metadata fields and attachments. - - edi_doc = super(account_invoice,self).edi_export(cr, uid, ids, edi_struct, context) - for i, invoice in enumerate(self.browse(cr, uid, rec_id, context=context)): - # add specific data for import - inv_comp = invoice.company_id - comp_partner = inv_comp.partner_id - comp_partner_addr = comp_partner.address - for address in comp_partner_addr: - edi_doc[i].update({ - # Add company info and address - 'company_address': { - 'street': address.street, - 'street2': address.street2, - 'zip': address.zip, - 'city': address.city, - 'state_id': self.edi_m2o(cr, uid, address.state_id), - 'country_id': self.edi_m2o(cr, uid, address.country_id), - 'email': address.email, - 'phone': address.phone - }, - # Function fields are not included in normal export + partner_pool = self.pool.get('res.partner') + partner_address_pool = self.pool.get('res.partner.address') + company_address_dict = { + 'street': True, + 'street2': True, + 'zip': True, + 'city': True, + 'state_id': True, + 'country_id': True, + 'email': True, + 'phone': True, + + } + edi_doc_list = [] + for invoice in records: + # Get EDI doc based on struct. The result will also contain all metadata fields and attachments. + edi_doc = super(account_invoice,self).edi_export(cr, uid, [invoice], edi_struct, context) + if not edi_doc: + continue + edi_doc = edi_doc[0] - #'company_logo': inv_comp.logo,#TODO - #'paid': inv_comp.paid, #TODO - }) - - return edi_doc + # Add company info and address + res = partner_pool.address_get(cr, uid, [invoice.company_id.partner_id.id], ['contact', 'invoice']) + contact_addr_id = res['contact'] + invoice_addr_id = res['invoice'] + + address = partner_address_pool.browse(cr, uid, invoice_addr_id, context=context) + edi_company_address_dict = {} + for key, value in company_address_dict.items(): + if not value: + continue + address_rec = getattr(address, key) + if not address_rec: + continue + if key.endswith('_id'): + address_rec = self.edi_m2o(cr, uid, address_rec, context=context) + edi_company_address_dict[key] = address_rec + + edi_doc.update({ + 'company_address': edi_company_address_dict, + #'company_logo': inv_comp.logo,#TODO + #'paid': inv_comp.paid, #TODO + }) + edi_doc_list.append(edi_doc) + return edi_doc_list def edi_import(self, cr, uid, edi_document, context=None): diff --git a/addons/account/test/test_edi_invoice.yml b/addons/account/test/test_edi_invoice.yml index 75c39d8aaf0..f66932f0464 100644 --- a/addons/account/test/test_edi_invoice.yml +++ b/addons/account/test/test_edi_invoice.yml @@ -48,6 +48,7 @@ tokens = self.export_edi(cr, uid, invoices) for token in tokens: document = self.get_document(cr, uid, token, context=context) + print document a = self.import_edi(cr, uid, edi_document = document) - From 3154cb96520b6a279e95f5cde74b5005cee472ab Mon Sep 17 00:00:00 2001 From: "Harry (OpenERP)" Date: Mon, 27 Jun 2011 17:56:16 +0530 Subject: [PATCH 8/9] [REF] account, edi: refactor code of edi_import method bzr revid: hmo@tinyerp.com-20110627122616-15e6y17d29bbricb --- addons/account/edi_invoice.py | 295 +++++++++-------------- addons/account/test/test_edi_invoice.yml | 5 +- 2 files changed, 119 insertions(+), 181 deletions(-) diff --git a/addons/account/edi_invoice.py b/addons/account/edi_invoice.py index ba3675a15f4..5d6d92df584 100644 --- a/addons/account/edi_invoice.py +++ b/addons/account/edi_invoice.py @@ -131,192 +131,131 @@ class account_invoice(osv.osv, ir_edi.edi): on the tax config of the DB where it is imported. """ - partner = self.pool.get('res.partner') - partner_add = self.pool.get('res.partner.address') - model_data = self.pool.get('ir.model.data') - product_obj = self.pool.get('product.product') - product_categ = self.pool.get('product.category') - acc_invoice = self.pool.get('account.invoice') - company = self.pool.get('res.company') - country = self.pool.get('res.country') + partner_pool = self.pool.get('res.partner') + partner_address_pool = self.pool.get('res.partner.address') + model_data_pool = self.pool.get('ir.model.data') + product_pool = self.pool.get('product.product') + product_categ_pool = self.pool.get('product.category') + company_pool = self.pool.get('res.company') + country_pool = self.pool.get('res.country') + state_pool = self.pool.get('res.country.state') + account_journal_pool = self.pool.get('account.journal') + invoice_line_pool = self.pool.get('account.invoice.line') + account_pool = self.pool.get('account.account') tax_id = [] account_id = [] partner_id = None company_id = None if context is None: context = {} - for field in edi_document.keys(): - if field == 'type': - if len(edi_document['invoice_line']): - name = edi_document['invoice_line'][0]['product_id'][1] - else: - name = None - re_ids = product_obj.search(cr,uid,[('name','=',name)]) - if edi_document['type'] == 'out_invoice' or edi_document['type'] == 'out_refund': - if re_ids: - if product_obj.browse(cr,uid,re_ids)[0].property_account_expense: - account_id = product_obj.browse(cr,uid,re_ids)[0].property_account_expense - else: - account_id = product_categ.browse(cr,uid,re_ids)[0].property_account_expense_categ - if product_obj.browse(cr,uid,re_ids)[0].taxes_id: - tax_id = product_obj.browse(cr, uid,re_ids)[0].taxes_id - if edi_document['type'] == 'out_refund': - edi_document['type'] = 'in_refund' - else: - edi_document['type'] = 'in_invoice' - elif edi_document['type'] == 'in_invoice' or edi_document['type'] == 'in_refund': - if re_ids: - if product_obj.browse(cr,uid,re_ids)[0].property_account_income: - account_id = product_obj.browse(cr,uid,re_ids)[0].property_account_income - else: - account_id = product_categ.browse(cr,uid,re_ids)[0].property_account_income_categ - if product_obj.browse(cr,uid,re_ids)[0].taxes_id: - tax_id = product_obj.browse(cr, uid,re_ids)[0].taxes_id - if edi_document['type'] == 'in_refund': - edi_document['type'] = 'out_refund' - else: - edi_document['type'] = 'out_invoice' - - if account_id: - name_ids = model_data.search(cr, uid, [('model','=',account_id._name),('res_id','=',account_id.id)]) - if name_ids: - xml_id = model_data.browse(cr, uid, name_ids)[0].name - db_uuid = ir_edi.safe_unique_id(account_id._name,account_id.id) - edi_document['invoice_line'][0]['account_id'] = [db_uuid+':'+xml_id,account_id.name] - - if tax_id: - name_ids = model_data.search(cr, uid, [('model','=',tax_id[0]._name),('res_id','=',tax_id[0].id)]) - if name_ids: - xml_id = model_data.browse(cr, uid, name_ids)[0].name - db_uuid = ir_edi.safe_unique_id(tax_id[0]._name,tax_id[0].id) - edi_document['tax_line'][0]['account_id'] = [db_uuid+':'+xml_id,tax_id[0].name] - else: - if len(edi_document['tax_line']): - edi_document['tax_line'][0]['manual'] = True - - res = {} - part = {} - comp = {} - partner_id = partner.search(cr,uid,[('name','=',edi_document['company_id'][1])]) - if len(partner_id): - browse_partner = partner.browse(cr,uid,partner_id[0]) - u_id = model_data.search(cr, uid, [('res_id','=',browse_partner.id),('model','=',browse_partner._name)]) - if len(u_id): - company_id = browse_partner.company_id - - xml_obj = model_data.browse(cr,uid,u_id[0]) - uuid = ir_edi.safe_unique_id(browse_partner._name,browse_partner.id) - db_uuid = '%s:%s' % (uuid,xml_obj.name) - part.update({'partner_id':[db_uuid,browse_partner.name]}) - - else: - - company_address = {} - company_id = company.create(cr, uid, {'name':edi_document['company_id'][1]}) - - for key in edi_document['company_address'].keys(): - if type(edi_document['company_address'][key]).__name__ == 'list': - if edi_document['company_address'][key][1] is not None: - country_id = country.search(cr ,uid,[('name','=',edi_document['company_address'][key][1])]) - - if len(country_id): - company_address.update({key : country_id[0]}) - - else: - if isinstance(edi_document['company_address'][key][1],unicode): - country_name = str(edi_document['company_address'][key][1]) - country_code = country_name[:2].upper() - country_id = country.create(cr, uid, {'code': country_code,name: country_name}) - company_address.update({key : country_id[0]}) - else: - company_address.update({key : edi_document['company_address'][key][1]}) - else: - company_address.update({key:edi_document['company_address'][key]}) - - add_id = [] - partner_id = [] - - add_id = partner_add.create(cr,uid,company_address) - - res.update({'name': edi_document['company_id'][1],'supplier': True,'address': [unicode(add_id)], 'company_id': unicode(company_id),'country' : country_id}) - - partner_id.append(partner.create(cr,uid,{'name': edi_document['company_id'][1],'supplier': True,'address': unicode(add_id), 'company_id': unicode(company_id),'country' : country_id})) - - browse_partner = partner.browse(cr,uid,partner_id[0]) - company_id = browse_partner.company_id - u_id = model_data.search(cr, uid, [('res_id','=',browse_partner.id),('model','=',browse_partner._name)]) - if len(u_id): - xml_obj = model_data.browse(cr,uid,u_id[0]) - uuid = ir_edi.safe_unique_id(browse_partner._name,browse_partner.id) - db_uuid = '%s:%s' % (uuid,xml_obj.name) - part.update({'partner_id':[db_uuid,browse_partner.name]}) - - comp_id = partner.search(cr,uid,[('name','=',edi_document['partner_id'][1])]) - - if len(comp_id): - browse_partner = partner.browse(cr,uid,comp_id[0]) - browse_company = company.browse(cr,uid,browse_partner.company_id.id) - u_id = u_id = model_data.search(cr, uid, [('res_id','=',browse_company.id),('model','=',browse_company._name)]) - if len(u_id): - xml_obj = model_data.browse(cr,uid,u_id[0]) - uuid = ir_edi.safe_unique_id(browse_company._name,browse_company.id) - db_uuid = '%s:%s' % (uuid,xml_obj.name) - comp.update({'company_id':[db_uuid,browse_company.name]}) - - del edi_document['partner_id'] - del edi_document['company_id'] - edi_document.update(part) - edi_document.update(comp) - - - if len(partner_id): - p = self.pool.get('res.partner').browse(cr, uid, partner_id[0]) - - partner_id = int(partner_id[0]) - - if company_id: - if p.property_account_receivable.company_id.id != company_id.id and p.property_account_payable.company_id.id != company_id.id: - property_obj = self.pool.get('ir.property') - - rec_pro_id = property_obj.search(cr,uid,[('name','=','property_account_receivable'),('res_id','=','res.partner,'+str(partner_id)+''),('company_id','=',company_id.id)]) - pay_pro_id = property_obj.search(cr,uid,[('name','=','property_account_payable'),('res_id','=','res.partner,'+str(partner_id)+''),('company_id','=',company_id.id)]) - - if not rec_pro_id: - rec_pro_id = property_obj.search(cr,uid,[('name','=','property_account_receivable'),('company_id','=',company_id.id)]) - if not pay_pro_id: - pay_pro_id = property_obj.search(cr,uid,[('name','=','property_account_payable'),('company_id','=',company_id.id)]) - rec_line_data = property_obj.read(cr,uid,rec_pro_id,['name','value_reference','res_id']) - pay_line_data = property_obj.read(cr,uid,pay_pro_id,['name','value_reference','res_id']) - rec_res_id = rec_line_data and rec_line_data[0].get('value_reference',False) and int(rec_line_data[0]['value_reference'].split(',')[1]) or False - pay_res_id = pay_line_data and pay_line_data[0].get('value_reference',False) and int(pay_line_data[0]['value_reference'].split(',')[1]) or False - if not rec_res_id and not pay_res_id: - raise osv.except_osv(_('Configuration Error !'), _('Can not find account chart for this company, Please Create account.')) - - account_obj = self.pool.get('account.account') - rec_obj_acc = account_obj.browse(cr, uid, [rec_res_id]) - pay_obj_acc = account_obj.browse(cr, uid, [pay_res_id]) - p.property_account_receivable = rec_obj_acc[0] - p.property_account_payable = pay_obj_acc[0] - if edi_document['type'] in ('out_invoice', 'out_refund'): - acc_obj = p.property_account_receivable - else: - acc_obj = p.property_account_payable - - res_id = model_data.search(cr,uid,[('model','=',acc_obj._name),('res_id','=',acc_obj.id)]) - - if len(res_id): - xml_obj = model_data.browse(cr, uid, res_id[0]) - uuid = ir_edi.safe_unique_id(acc_obj._name,acc_obj.id) - db_uuid = '%s:%s' % (uuid,xml_obj.name) - edi_document.update({'account_id':[db_uuid,acc_obj.name]}) - - edi_document.update({'reference':edi_document['internal_number'],'reference_type' : 'none'}) - edi_document['internal_number'] = False - context['type'] = edi_document['type'] - + + # import company as a new partner, if type==in then supplier=1, else customer=1 + # partner_id field is modified to point to the new partner + # company_address data used to add address to new partner + edi_company_address = edi_document['company_address'] + edi_partner_id = edi_document['partner_id'] + company_name = edi_document['company_id'][1] + invoice_type = edi_document['type'] + state_id = edi_company_address.get('state_id', False) + state_name = state_id and state_id[1] + country_id = edi_company_address.get('country_id', False) + country_name = country_id and country_id[1] + + country_id = country_name and self.edi_import_relation(cr, uid, 'res.country', country_name, context=context) or False + state_id = state_name and self.edi_import_relation(cr, uid, 'res.country.state', state_name, + values={'country_id': country_id, 'code': state_name}, context=context) or False + address_value = { + 'street': edi_company_address.get('street', False), + 'street2': edi_company_address.get('street2', False), + 'zip': edi_company_address.get('zip', False), + 'city': edi_company_address.get('city', False), + 'state_id': state_id, + 'country_id': country_id, + 'email': edi_company_address.get('email', False), + 'phone': edi_company_address.get('phone', False), + + } + + + partner_value = {'name': company_name} + if invoice_type in ('out_invoice', 'in_refund'): + partner_value.update({'customer': True, 'supplier': False}) + if invoice_type in ('in_invoice', 'out_refund'): + partner_value.update({'customer': False, 'supplier': True}) + + partner_id = partner_pool.create(cr, uid, partner_value, context=context) + address_value.update({'partner_id': partner_id}) + address_id = partner_address_pool.create(cr, uid, address_value, context=context) + + partner = partner_pool.browse(cr, uid, partner_id, context=context) + edi_document['partner_id'] = self.edi_m2o(cr, uid, partner, context=context) + + # change type: out_invoice'<->'in_invoice','out_refund'<->'in_refund' + invoice_type = invoice_type.startswith('in_') and invoice_type.replace('in_','out_') or invoice_type.replace('out_','in_') + edi_document['type'] = invoice_type + + # Set Account + if invoice_type in ('out_invoice', 'out_refund'): + invoice_account = partner.property_account_receivable + else: + invoice_account = partner.property_account_payable + edi_document['account_id'] = invoice_account and self.edi_m2o(cr, uid, invoice_account, context=context) or False + + # reference: should contain the value of the 'internal_number' + edi_document['reference'] = edi_document.get('internal_number', False) + # reference_type: 'none' + edi_document['reference_type'] = 'none' + + # internal number: reset to False, auto-generated + edi_document['internal_number'] = False + + # company should set by default so delete company data from edi Document del edi_document['company_address'] - return super(account_invoice,self).edi_import(cr, uid, edi_document) + del edi_document['company_id'] + + # journal_id: should be selected based on type: simply put the 'type' in the context when calling create(), will be selected correctly + journal_context = context.copy() + journal_context.update({'type':invoice_type}) + journal_id = self._get_journal(cr, uid, context=journal_context) + journal = False + if journal_id: + journal = account_journal_pool.browse(cr, uid, journal_id, context=context) + edi_document['journal_id'] = journal and self.edi_m2o(cr, uid, journal, context=context) or False + + # for invoice lines, the account_id value should be taken from the product's default, i.e. from the default category, as it will not be provided. + for edi_invoice_line in edi_document.get('invoice_line', []): + product_id = edi_invoice_line.get('product_id', False) + account = False + if product_id: + product_name = product_id and product_id[1] + product_id = self.edi_import_relation(cr, uid, 'product.product', product_name, context=context) + product = product_pool.browse(cr, uid, product_id, context=context) + + if invoice_type in ('out_invoice','out_refund'): + account = product.product_tmpl_id.property_account_income + if not account: + account = product.categ_id.property_account_income_categ + else: + account = product.product_tmpl_id.property_account_expense + if not account: + account = product.categ_id.property_account_expense_categ + + # TODO: add effect of fiscal position + # account = fpos_obj.map_account(cr, uid, fiscal_position_id, account.id) + edi_invoice_line['account_id'] = account and self.edi_m2o(cr, uid, account, context=context) or False + + # for tax lines, we disconnect from the invoice.line, so all tax lines will be of type 'manual', and default accounts should be picked based + # on the tax config of the DB where it is imported. + for edi_tax_line in edi_document.get('tax_line', []): + account_ids = account_pool.search(cr, uid, [('type','<>','view'),('type','<>','income'), ('type', '<>', 'closed')]) + if account_ids: + edi_tax_line['account_id'] = account_ids[0] #TODO should select account of output VAT for Customer Invoice and Input VAT for Supplier Invoice + edi_tax_line['manual'] = True + + # TODO :=> payment_term: if set, create a default one based on name... + print edi_document + return super(account_invoice,self).edi_import(cr, uid, edi_document, context=context) account_invoice() diff --git a/addons/account/test/test_edi_invoice.yml b/addons/account/test/test_edi_invoice.yml index f66932f0464..18e5a8fb9e9 100644 --- a/addons/account/test/test_edi_invoice.yml +++ b/addons/account/test/test_edi_invoice.yml @@ -28,7 +28,7 @@ company_id: base.main_company account_id: 1 date_invoice: '2011-06-22' - name: Nothing + name: selling product type: 'out_invoice' invoice_line: - product_id: product.product_product_pc1 @@ -48,14 +48,13 @@ tokens = self.export_edi(cr, uid, invoices) for token in tokens: document = self.get_document(cr, uid, token, context=context) - print document a = self.import_edi(cr, uid, edi_document = document) - Check the customer invoice is exported or not - !python {model: account.invoice}: | - ids=self.search(cr, uid, [('partner_id','=',ref("res_partner_test20")),('company_id','=',ref("res_company_test11")),('type','=','out_invoice')]) + ids=self.search(cr, uid, [('partner_id','=',ref("res_partner_test20")),('type','=','out_invoice')]) if not ids: raise AssertionError("Invoice is not imported") From 99624f6b0efe76186d1c51e26d733938c087660d Mon Sep 17 00:00:00 2001 From: "Harry (OpenERP)" Date: Mon, 27 Jun 2011 17:57:32 +0530 Subject: [PATCH 9/9] [REF] account, edi: remove print statement bzr revid: hmo@tinyerp.com-20110627122732-s2yswh7mtyaumjz9 --- addons/account/edi_invoice.py | 1 - 1 file changed, 1 deletion(-) diff --git a/addons/account/edi_invoice.py b/addons/account/edi_invoice.py index 5d6d92df584..e37bd220d35 100644 --- a/addons/account/edi_invoice.py +++ b/addons/account/edi_invoice.py @@ -254,7 +254,6 @@ class account_invoice(osv.osv, ir_edi.edi): edi_tax_line['manual'] = True # TODO :=> payment_term: if set, create a default one based on name... - print edi_document return super(account_invoice,self).edi_import(cr, uid, edi_document, context=context) account_invoice()