2011-06-27 06:49:49 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
2011-10-05 00:34:09 +00:00
|
|
|
# OpenERP, Open Source Business Applications
|
|
|
|
# Copyright (c) 2011 OpenERP S.A. <http://openerp.com>
|
2011-06-27 06:49:49 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
2011-06-21 13:30:26 +00:00
|
|
|
|
2011-06-24 13:11:17 +00:00
|
|
|
from osv import fields, osv, orm
|
2011-10-05 00:34:09 +00:00
|
|
|
from edi import EDIMixin
|
2011-10-06 11:36:51 +00:00
|
|
|
|
|
|
|
INVOICE_LINE_EDI_STRUCT = {
|
|
|
|
'name': True,
|
|
|
|
'origin': True,
|
|
|
|
'uos_id': True,
|
|
|
|
'product_id': True,
|
|
|
|
'price_unit': True,
|
|
|
|
'quantity': True,
|
|
|
|
'discount': True,
|
|
|
|
'note': True,
|
2011-10-26 00:01:10 +00:00
|
|
|
|
|
|
|
# fields used for web preview only - discarded on import
|
|
|
|
'price_subtotal': True,
|
2011-10-06 11:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2011-10-24 14:44:07 +00:00
|
|
|
#custom: currency_id
|
2011-10-06 11:36:51 +00:00
|
|
|
'invoice_line': INVOICE_LINE_EDI_STRUCT,
|
|
|
|
'tax_line': INVOICE_TAX_LINE_EDI_STRUCT,
|
2011-10-26 00:01:10 +00:00
|
|
|
|
|
|
|
# fields used for web preview only - discarded on import
|
|
|
|
#custom: 'partner_ref'
|
|
|
|
'amount_total': True,
|
|
|
|
'amount_untaxed': True,
|
|
|
|
'amount_tax': True,
|
2011-10-06 11:36:51 +00:00
|
|
|
}
|
2011-06-21 13:30:26 +00:00
|
|
|
|
2011-10-05 00:34:09 +00:00
|
|
|
class account_invoice(osv.osv, EDIMixin):
|
2011-06-21 13:30:26 +00:00
|
|
|
_inherit = 'account.invoice'
|
|
|
|
|
2011-06-27 08:32:28 +00:00
|
|
|
def edi_export(self, cr, uid, records, edi_struct=None, context=None):
|
2011-06-21 13:30:26 +00:00
|
|
|
"""Exports a supplier or customer invoice"""
|
2011-10-06 11:36:51 +00:00
|
|
|
edi_struct = dict(edi_struct or INVOICE_EDI_STRUCT)
|
2011-10-05 00:34:09 +00:00
|
|
|
res_company = self.pool.get('res.company')
|
2012-03-05 06:48:45 +00:00
|
|
|
res_partner_address = self.pool.get('res.partner')
|
2011-06-27 08:32:28 +00:00
|
|
|
edi_doc_list = []
|
|
|
|
for invoice in records:
|
2011-10-10 14:38:06 +00:00
|
|
|
# generate the main report
|
|
|
|
self._edi_generate_report_attachment(cr, uid, invoice, context=context)
|
2011-10-05 00:34:09 +00:00
|
|
|
edi_doc = super(account_invoice,self).edi_export(cr, uid, [invoice], edi_struct, context)[0]
|
2011-06-27 08:32:28 +00:00
|
|
|
edi_doc.update({
|
2011-10-05 00:34:09 +00:00
|
|
|
'company_address': res_company.edi_export_address(cr, uid, invoice.company_id, context=context),
|
2011-10-05 14:16:52 +00:00
|
|
|
'company_paypal_account': invoice.company_id.paypal_account,
|
2012-03-05 10:16:05 +00:00
|
|
|
'partner_address': res_partner_address.edi_export(cr, uid, [invoice.partner_id], context=context)[0],
|
2011-10-24 14:44:07 +00:00
|
|
|
|
2011-10-26 00:01:10 +00:00
|
|
|
'currency': self.pool.get('res.currency').edi_export(cr, uid, [invoice.currency_id], context=context)[0],
|
|
|
|
'partner_ref': invoice.reference or False,
|
2011-06-27 08:32:28 +00:00
|
|
|
})
|
|
|
|
edi_doc_list.append(edi_doc)
|
|
|
|
return edi_doc_list
|
2011-06-27 06:49:49 +00:00
|
|
|
|
2011-10-05 00:34:09 +00:00
|
|
|
def _edi_tax_account(self, cr, uid, invoice_type='out_invoice', context=None):
|
|
|
|
#TODO/FIXME: should select proper Tax Account
|
2011-09-08 09:43:29 +00:00
|
|
|
account_pool = self.pool.get('account.account')
|
|
|
|
account_ids = account_pool.search(cr, uid, [('type','<>','view'),('type','<>','income'), ('type', '<>', 'closed')])
|
|
|
|
tax_account = False
|
|
|
|
if account_ids:
|
|
|
|
tax_account = account_pool.browse(cr, uid, account_ids[0])
|
|
|
|
return tax_account
|
|
|
|
|
2011-10-05 00:34:09 +00:00
|
|
|
def _edi_invoice_account(self, cr, uid, partner_id, invoice_type, context=None):
|
2011-06-27 12:26:16 +00:00
|
|
|
partner_pool = self.pool.get('res.partner')
|
2011-09-08 09:43:29 +00:00
|
|
|
partner = partner_pool.browse(cr, uid, partner_id, context=context)
|
|
|
|
if invoice_type in ('out_invoice', 'out_refund'):
|
|
|
|
invoice_account = partner.property_account_receivable
|
|
|
|
else:
|
|
|
|
invoice_account = partner.property_account_payable
|
|
|
|
return invoice_account
|
|
|
|
|
2011-10-05 00:34:09 +00:00
|
|
|
def _edi_product_account(self, cr, uid, product_id, invoice_type, context=None):
|
2011-06-27 12:26:16 +00:00
|
|
|
product_pool = self.pool.get('product.product')
|
2011-09-08 09:43:29 +00:00
|
|
|
product = product_pool.browse(cr, uid, product_id, context=context)
|
|
|
|
if invoice_type in ('out_invoice','out_refund'):
|
2011-10-05 00:34:09 +00:00
|
|
|
account = product.property_account_income or product.categ_id.property_account_income_categ
|
2011-09-08 09:43:29 +00:00
|
|
|
else:
|
2011-10-05 00:34:09 +00:00
|
|
|
account = product.property_account_expense or product.categ_id.property_account_expense_categ
|
2011-09-08 09:43:29 +00:00
|
|
|
return account
|
|
|
|
|
2011-10-05 00:34:09 +00:00
|
|
|
def _edi_import_company(self, cr, uid, edi_document, context=None):
|
2011-10-06 11:36:51 +00:00
|
|
|
# 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
|
|
|
|
|
2011-10-05 00:34:09 +00:00
|
|
|
self._edi_requires_attributes(('company_id','company_address','type'), edi_document)
|
|
|
|
res_partner = self.pool.get('res.partner')
|
2011-09-13 12:59:23 +00:00
|
|
|
|
2011-10-05 00:34:09 +00:00
|
|
|
# imported company = new partner
|
2011-10-06 11:36:51 +00:00
|
|
|
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)
|
2011-06-27 12:26:16 +00:00
|
|
|
invoice_type = edi_document['type']
|
2011-09-13 12:59:23 +00:00
|
|
|
partner_value = {}
|
2011-10-05 00:34:09 +00:00
|
|
|
if invoice_type in ('out_invoice', 'out_refund'):
|
2011-09-13 12:59:23 +00:00
|
|
|
partner_value.update({'customer': True})
|
2011-10-05 00:34:09 +00:00
|
|
|
if invoice_type in ('in_invoice', 'in_refund'):
|
2011-09-13 12:59:23 +00:00
|
|
|
partner_value.update({'supplier': True})
|
2011-10-06 11:36:51 +00:00
|
|
|
res_partner.write(cr, uid, [partner_id], partner_value, context=context)
|
2011-06-27 12:26:16 +00:00
|
|
|
|
2011-10-05 00:34:09 +00:00
|
|
|
# imported company_address = new partner address
|
2011-10-06 11:36:51 +00:00
|
|
|
address_info = edi_document.pop('company_address')
|
2012-03-06 10:07:43 +00:00
|
|
|
address_info['parent_id'] = (src_company_id, src_company_name)
|
2011-10-05 00:34:09 +00:00
|
|
|
address_info['type'] = 'invoice'
|
2012-03-05 06:48:45 +00:00
|
|
|
address_id = res_partner.edi_import(cr, uid, address_info, context=context)
|
2011-10-05 00:34:09 +00:00
|
|
|
|
|
|
|
# modify edi_document to refer to new partner
|
2012-03-05 06:48:45 +00:00
|
|
|
partner_address = res_partner.browse(cr, uid, address_id, context=context)
|
2011-10-06 11:36:51 +00:00
|
|
|
edi_document['partner_id'] = (src_company_id, src_company_name)
|
|
|
|
edi_document.pop('partner_address', False) # ignored
|
2012-03-05 10:16:05 +00:00
|
|
|
#edi_document['address_contact_id'] = self.edi_m2o(cr, uid, partner_address, context=context)
|
2011-10-05 00:34:09 +00:00
|
|
|
|
2011-09-13 12:59:23 +00:00
|
|
|
return partner_id
|
2011-10-05 00:34:09 +00:00
|
|
|
|
2011-09-08 09:43:29 +00:00
|
|
|
|
|
|
|
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
|
2011-10-05 00:34:09 +00:00
|
|
|
- journal_id: should be selected based on type: simply put the 'type'
|
2011-09-08 09:43:29 +00:00
|
|
|
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
|
2011-10-05 00:34:09 +00:00
|
|
|
on the tax config of the DB where it is imported.
|
2011-09-08 09:43:29 +00:00
|
|
|
"""
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
2011-10-24 14:44:07 +00:00
|
|
|
self._edi_requires_attributes(('company_id','company_address','type','invoice_line','currency'), edi_document)
|
|
|
|
|
|
|
|
# extract currency info
|
|
|
|
res_currency = self.pool.get('res.currency')
|
|
|
|
currency_info = edi_document.pop('currency')
|
|
|
|
currency_id = res_currency.edi_import(cr, uid, currency_info, context=context)
|
|
|
|
currency = res_currency.browse(cr, uid, currency_id)
|
|
|
|
edi_document['currency_id'] = self.edi_m2o(cr, uid, currency, context=context)
|
2011-09-08 09:43:29 +00:00
|
|
|
|
2011-06-27 12:26:16 +00:00
|
|
|
# change type: out_invoice'<->'in_invoice','out_refund'<->'in_refund'
|
2011-09-08 09:43:29 +00:00
|
|
|
invoice_type = edi_document['type']
|
2011-06-27 12:26:16 +00:00
|
|
|
invoice_type = invoice_type.startswith('in_') and invoice_type.replace('in_','out_') or invoice_type.replace('out_','in_')
|
|
|
|
edi_document['type'] = invoice_type
|
|
|
|
|
2011-10-05 00:34:09 +00:00
|
|
|
#import company as a new partner
|
|
|
|
partner_id = self._edi_import_company(cr, uid, edi_document, context=context)
|
|
|
|
|
2011-06-27 12:26:16 +00:00
|
|
|
# Set Account
|
2011-10-05 00:34:09 +00:00
|
|
|
invoice_account = self._edi_invoice_account(cr, uid, partner_id, invoice_type, context=context)
|
2011-06-27 12:26:16 +00:00
|
|
|
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
|
|
|
|
|
2011-10-26 00:01:10 +00:00
|
|
|
# discard web preview fields, if present
|
|
|
|
edi_document.pop('partner_ref', None)
|
|
|
|
|
2011-06-27 12:26:16 +00:00
|
|
|
# journal_id: should be selected based on type: simply put the 'type' in the context when calling create(), will be selected correctly
|
2011-10-05 00:34:09 +00:00
|
|
|
context.update(type=invoice_type)
|
2011-09-08 09:43:29 +00:00
|
|
|
|
2011-06-27 12:26:16 +00:00
|
|
|
# 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.
|
2011-10-05 00:34:09 +00:00
|
|
|
for edi_invoice_line in edi_document['invoice_line']:
|
|
|
|
product_info = edi_invoice_line['product_id']
|
|
|
|
product_id = self.edi_import_relation(cr, uid, 'product.product', product_info[1],
|
|
|
|
product_info[0], context=context)
|
|
|
|
account = self._edi_product_account(cr, uid, product_id, invoice_type, context=context)
|
|
|
|
# TODO: could be improved with fiscal positions perhaps
|
2011-06-27 12:26:16 +00:00
|
|
|
# account = fpos_obj.map_account(cr, uid, fiscal_position_id, account.id)
|
2011-10-05 00:34:09 +00:00
|
|
|
edi_invoice_line['account_id'] = self.edi_m2o(cr, uid, account, context=context) if account else False
|
2011-06-28 13:43:26 +00:00
|
|
|
|
2011-10-26 00:01:10 +00:00
|
|
|
# discard web preview fields, if present
|
|
|
|
edi_invoice_line.pop('price_subtotal', None)
|
|
|
|
|
2011-06-27 12:26:16 +00:00
|
|
|
# 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.
|
2011-10-05 00:34:09 +00:00
|
|
|
tax_account = self._edi_tax_account(cr, uid, context=context)
|
|
|
|
tax_account_info = self.edi_m2o(cr, uid, tax_account, context=context)
|
2011-06-27 12:26:16 +00:00
|
|
|
for edi_tax_line in edi_document.get('tax_line', []):
|
2011-10-05 00:34:09 +00:00
|
|
|
edi_tax_line['account_id'] = tax_account_info
|
2011-06-27 12:26:16 +00:00
|
|
|
edi_tax_line['manual'] = True
|
|
|
|
|
|
|
|
return super(account_invoice,self).edi_import(cr, uid, edi_document, context=context)
|
2011-06-21 13:30:26 +00:00
|
|
|
|
2011-11-05 02:32:10 +00:00
|
|
|
|
|
|
|
def _edi_record_display_action(self, cr, uid, id, context=None):
|
|
|
|
"""Returns an appropriate action definition dict for displaying
|
|
|
|
the record with ID ``rec_id``.
|
|
|
|
|
|
|
|
:param int id: database ID of record to display
|
|
|
|
:return: action definition dict
|
|
|
|
"""
|
|
|
|
action = super(account_invoice,self)._edi_record_display_action(cr, uid, id, context=context)
|
|
|
|
try:
|
|
|
|
invoice = self.browse(cr, uid, id, context=context)
|
|
|
|
if 'out_' in invoice.type:
|
|
|
|
view_ext_id = 'invoice_form'
|
|
|
|
journal_type = 'sale'
|
|
|
|
else:
|
|
|
|
view_ext_id = 'invoice_supplier_form'
|
|
|
|
journal_type = 'purchase'
|
|
|
|
ctx = "{'type': '%s', 'journal_type': '%s'}" % (invoice.type, journal_type)
|
|
|
|
action.update(context=ctx)
|
|
|
|
view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account', view_ext_id)[1]
|
|
|
|
action.update(views=[(view_id,'form'), (False, 'tree')])
|
|
|
|
except ValueError:
|
|
|
|
# ignore if views are missing
|
|
|
|
pass
|
|
|
|
return action
|
|
|
|
|
|
|
|
|
2011-10-05 00:34:09 +00:00
|
|
|
class account_invoice_line(osv.osv, EDIMixin):
|
2011-06-21 13:30:26 +00:00
|
|
|
_inherit='account.invoice.line'
|
|
|
|
|
2011-10-05 00:34:09 +00:00
|
|
|
class account_invoice_tax(osv.osv, EDIMixin):
|
2011-06-21 13:30:26 +00:00
|
|
|
_inherit = "account.invoice.tax"
|
|
|
|
|
2011-09-13 12:59:23 +00:00
|
|
|
|
2011-11-22 08:51:38 +00:00
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|