← Back to team overview

credativ team mailing list archive

lp:~kinner-vachhani/credativ-openerp/fix-tax-baseamount-differ-invoice into lp:credativ-openerp

 

Kinner Vachhani has proposed merging lp:~kinner-vachhani/credativ-openerp/fix-tax-baseamount-differ-invoice into lp:credativ-openerp.

Requested reviews:
  credativ (credativ)
Related bugs:
  Bug #986300 in OpenERP Addons: "Invoice tax base computing differ from untaxed amount"
  https://bugs.launchpad.net/openobject-addons/+bug/986300

For more details, see:
https://code.launchpad.net/~kinner-vachhani/credativ-openerp/fix-tax-baseamount-differ-invoice/+merge/167594

Invoice tax base computing differ from untaxed amount fixed
-- 
The attached diff has been truncated due to its size.
https://code.launchpad.net/~kinner-vachhani/credativ-openerp/fix-tax-baseamount-differ-invoice/+merge/167594
Your team credativ is requested to review the proposed merge of lp:~kinner-vachhani/credativ-openerp/fix-tax-baseamount-differ-invoice into lp:credativ-openerp.
=== added file '.bzrignore'
--- .bzrignore	1970-01-01 00:00:00 +0000
+++ .bzrignore	2013-06-05 16:26:39 +0000
@@ -0,0 +1,1 @@
+.*

=== added directory 'account'
=== added file 'account/__init__.py'
--- account/__init__.py	1970-01-01 00:00:00 +0000
+++ account/__init__.py	2013-06-05 16:26:39 +0000
@@ -0,0 +1,40 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
+#
+#    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/>.
+#
+##############################################################################
+
+import account
+import installer
+import project
+import partner
+import account_invoice
+import account_bank_statement
+import account_bank
+import account_cash_statement
+import account_move_line
+import account_analytic_line
+import account_financial_report
+import wizard
+import report
+import product
+import ir_sequence
+import company
+import res_currency
+import edi
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'account/__openerp__.py'
--- account/__openerp__.py	1970-01-01 00:00:00 +0000
+++ account/__openerp__.py	2013-06-05 16:26:39 +0000
@@ -0,0 +1,157 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
+#
+#    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/>.
+#
+##############################################################################
+{
+    "name" : "eInvoicing",
+    "version" : "1.1",
+    "author" : "OpenERP SA",
+    "category": 'Accounting & Finance',
+    'complexity': "easy",
+    "description": """
+Accounting and Financial Management.
+====================================
+
+Financial and accounting module that covers:
+--------------------------------------------
+General accountings
+Cost / Analytic accounting
+Third party accounting
+Taxes management
+Budgets
+Customer and Supplier Invoices
+Bank statements
+Reconciliation process by partner
+
+Creates a dashboard for accountants that includes:
+--------------------------------------------------
+* List of Customer Invoice to Approve
+* Company Analysis
+* Graph of Aged Receivables
+* Graph of Treasury
+
+The processes like maintaining of general ledger is done through the defined financial Journals (entry move line or
+grouping is maintained through journal) for a particular financial year and for preparation of vouchers there is a
+module named account_voucher.
+    """,
+    'website': 'http://www.openerp.com',
+    'images' : ['images/accounts.jpeg','images/bank_statement.jpeg','images/cash_register.jpeg','images/chart_of_accounts.jpeg','images/customer_invoice.jpeg','images/journal_entries.jpeg'],
+    'init_xml': [],
+    "depends" : ["base_setup", "product", "analytic", "process", "board", "edi"],
+    'update_xml': [
+        'security/account_security.xml',
+        'security/ir.model.access.csv',
+        'account_menuitem.xml',
+        'report/account_invoice_report_view.xml',
+        'report/account_entries_report_view.xml',
+        'report/account_treasury_report_view.xml',
+        'report/account_report_view.xml',
+        'report/account_analytic_entries_report_view.xml',
+        'wizard/account_move_bank_reconcile_view.xml',
+        'wizard/account_use_model_view.xml',
+        'account_installer.xml',
+        'wizard/account_period_close_view.xml',
+        'account_view.xml',
+        'account_report.xml',
+        'account_financial_report_data.xml',
+        'wizard/account_report_common_view.xml',
+        'wizard/account_invoice_refund_view.xml',
+        'wizard/account_fiscalyear_close_state.xml',
+        'wizard/account_chart_view.xml',
+        'wizard/account_tax_chart_view.xml',
+        'wizard/account_move_journal_view.xml',
+        'wizard/account_move_line_reconcile_select_view.xml',
+        'wizard/account_open_closed_fiscalyear_view.xml',
+        'wizard/account_move_line_unreconcile_select_view.xml',
+        'wizard/account_vat_view.xml',
+        'wizard/account_report_print_journal_view.xml',
+        'wizard/account_report_general_journal_view.xml',
+        'wizard/account_report_central_journal_view.xml',
+        'wizard/account_subscription_generate_view.xml',
+        'wizard/account_fiscalyear_close_view.xml',
+        'wizard/account_state_open_view.xml',
+        'wizard/account_journal_select_view.xml',
+        'wizard/account_change_currency_view.xml',
+        'wizard/account_validate_move_view.xml',
+        'wizard/account_unreconcile_view.xml',
+        'wizard/account_report_general_ledger_view.xml',
+        'wizard/account_invoice_state_view.xml',
+        'wizard/account_report_partner_balance_view.xml',
+        'wizard/account_report_account_balance_view.xml',
+        'wizard/account_report_aged_partner_balance_view.xml',
+        'wizard/account_report_partner_ledger_view.xml',
+        'wizard/account_reconcile_view.xml',
+        'wizard/account_reconcile_partner_process_view.xml',
+        'wizard/account_automatic_reconcile_view.xml',
+        'wizard/account_financial_report_view.xml',
+        'project/wizard/project_account_analytic_line_view.xml',
+        'account_end_fy.xml',
+        'account_invoice_view.xml',
+        'partner_view.xml',
+        'data/account_data.xml',
+        'data/data_account_type.xml',
+        'account_invoice_workflow.xml',
+        'project/project_view.xml',
+        'project/project_report.xml',
+        'project/wizard/account_analytic_balance_report_view.xml',
+        'project/wizard/account_analytic_cost_ledger_view.xml',
+        'project/wizard/account_analytic_inverted_balance_report.xml',
+        'project/wizard/account_analytic_journal_report_view.xml',
+        'project/wizard/account_analytic_cost_ledger_for_journal_report_view.xml',
+        'project/wizard/account_analytic_chart_view.xml',
+        'product_view.xml',
+        'account_assert_test.xml',
+        'process/statement_process.xml',
+        'process/customer_invoice_process.xml',
+        'process/supplier_invoice_process.xml',
+        'ir_sequence_view.xml',
+        'company_view.xml',
+        'board_account_view.xml',
+        "edi/invoice_action_data.xml",
+        "account_bank_view.xml",
+        "account_pre_install.yml"
+    ],
+    'demo_xml': [
+        'demo/account_demo.xml',
+        'project/project_demo.xml',
+        'project/analytic_account_demo.xml',
+        'demo/account_minimal.xml',
+        'demo/account_invoice_demo.xml',
+        #'account_unit_test.xml',
+    ],
+    'test': [
+        'test/account_customer_invoice.yml',
+        'test/account_supplier_invoice.yml',
+        'test/account_change_currency.yml',
+        'test/chart_of_account.yml',
+        'test/account_period_close.yml',
+        'test/account_use_model.yml',
+        'test/account_validate_account_move.yml',
+        'test/account_fiscalyear_close.yml',
+        'test/account_bank_statement.yml',
+        'test/account_cash_statement.yml',
+        'test/test_edi_invoice.yml',
+        'test/account_report.yml',
+        'test/account_fiscalyear_close_state.yml', #last test, as it will definitively close the demo fiscalyear
+    ],
+    'installable': True,
+    'auto_install': False,
+    'certificate': '0080331923549',
+}
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'account/account.py'
--- account/account.py	1970-01-01 00:00:00 +0000
+++ account/account.py	2013-06-05 16:26:39 +0000
@@ -0,0 +1,3520 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
+#
+#    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/>.
+#
+##############################################################################
+
+import time
+from datetime import datetime
+from dateutil.relativedelta import relativedelta
+from operator import itemgetter
+
+import netsvc
+import pooler
+from osv import fields, osv
+import decimal_precision as dp
+from tools.translate import _
+
+def check_cycle(self, cr, uid, ids, context=None):
+    """ climbs the ``self._table.parent_id`` chains for 100 levels or
+    until it can't find any more parent(s)
+
+    Returns true if it runs out of parents (no cycle), false if
+    it can recurse 100 times without ending all chains
+    """
+    level = 100
+    while len(ids):
+        cr.execute('SELECT DISTINCT parent_id '\
+                    'FROM '+self._table+' '\
+                    'WHERE id IN %s '\
+                    'AND parent_id IS NOT NULL',(tuple(ids),))
+        ids = map(itemgetter(0), cr.fetchall())
+        if not level:
+            return False
+        level -= 1
+    return True
+
+class account_payment_term(osv.osv):
+    _name = "account.payment.term"
+    _description = "Payment Term"
+    _columns = {
+        'name': fields.char('Payment Term', size=64, translate=True, required=True),
+        'active': fields.boolean('Active', help="If the active field is set to False, it will allow you to hide the payment term without removing it."),
+        'note': fields.text('Description', translate=True),
+        'line_ids': fields.one2many('account.payment.term.line', 'payment_id', 'Terms'),
+    }
+    _defaults = {
+        'active': 1,
+    }
+    _order = "name"
+
+    def compute(self, cr, uid, id, value, date_ref=False, context=None):
+        if not date_ref:
+            date_ref = datetime.now().strftime('%Y-%m-%d')
+        pt = self.browse(cr, uid, id, context=context)
+        amount = value
+        result = []
+        obj_precision = self.pool.get('decimal.precision')
+        for line in pt.line_ids:
+            prec = obj_precision.precision_get(cr, uid, 'Account')
+            if line.value == 'fixed':
+                amt = round(line.value_amount, prec)
+            elif line.value == 'procent':
+                amt = round(value * line.value_amount, prec)
+            elif line.value == 'balance':
+                amt = round(amount, prec)
+            if amt:
+                next_date = (datetime.strptime(date_ref, '%Y-%m-%d') + relativedelta(days=line.days))
+                if line.days2 < 0:
+                    next_first_date = next_date + relativedelta(day=1,months=1) #Getting 1st of next month
+                    next_date = next_first_date + relativedelta(days=line.days2)
+                if line.days2 > 0:
+                    next_date += relativedelta(day=line.days2, months=1)
+                result.append( (next_date.strftime('%Y-%m-%d'), amt) )
+                amount -= amt
+        return result
+
+account_payment_term()
+
+class account_payment_term_line(osv.osv):
+    _name = "account.payment.term.line"
+    _description = "Payment Term Line"
+    _columns = {
+        'name': fields.char('Line Name', size=32, required=True),
+        'sequence': fields.integer('Sequence', required=True, help="The sequence field is used to order the payment term lines from the lowest sequences to the higher ones"),
+        'value': fields.selection([('procent', 'Percent'),
+                                   ('balance', 'Balance'),
+                                   ('fixed', 'Fixed Amount')], 'Valuation',
+                                   required=True, help="""Select here the kind of valuation related to this payment term line. Note that you should have your last line with the type 'Balance' to ensure that the whole amount will be threated."""),
+
+        'value_amount': fields.float('Amount To Pay', digits_compute=dp.get_precision('Payment Term'), help="For percent enter a ratio between 0-1."),
+        'days': fields.integer('Number of Days', required=True, help="Number of days to add before computation of the day of month." \
+            "If Date=15/01, Number of Days=22, Day of Month=-1, then the due date is 28/02."),
+        'days2': fields.integer('Day of the Month', required=True, help="Day of the month, set -1 for the last day of the current month. If it's positive, it gives the day of the next month. Set 0 for net days (otherwise it's based on the beginning of the month)."),
+        'payment_id': fields.many2one('account.payment.term', 'Payment Term', required=True, select=True),
+    }
+    _defaults = {
+        'value': 'balance',
+        'sequence': 5,
+        'days2': 0,
+    }
+    _order = "sequence"
+
+    def _check_percent(self, cr, uid, ids, context=None):
+        obj = self.browse(cr, uid, ids[0], context=context)
+        if obj.value == 'procent' and ( obj.value_amount < 0.0 or obj.value_amount > 1.0):
+            return False
+        return True
+
+    _constraints = [
+        (_check_percent, 'Percentages for Payment Term Line must be between 0 and 1, Example: 0.02 for 2% ', ['value_amount']),
+    ]
+
+account_payment_term_line()
+
+class account_account_type(osv.osv):
+    _name = "account.account.type"
+    _description = "Account Type"
+
+    def _get_current_report_type(self, cr, uid, ids, name, arg, context=None):
+        obj_data = self.pool.get('ir.model.data')
+        obj_financial_report = self.pool.get('account.financial.report') 
+        res = {}
+        financial_report_ref = {
+            'asset': obj_financial_report.browse(cr, uid, obj_data.get_object_reference(cr, uid, 'account','account_financial_report_assets0')[1], context=context),
+            'liability': obj_financial_report.browse(cr, uid, obj_data.get_object_reference(cr, uid, 'account','account_financial_report_liability0')[1], context=context),
+            'income': obj_financial_report.browse(cr, uid, obj_data.get_object_reference(cr, uid, 'account','account_financial_report_income0')[1], context=context),
+            'expense': obj_financial_report.browse(cr, uid, obj_data.get_object_reference(cr, uid, 'account','account_financial_report_expense0')[1], context=context),
+        }
+        for record in self.browse(cr, uid, ids, context=context):
+            res[record.id] = 'none'
+            for key, financial_report in financial_report_ref.items():
+                list_ids = [x.id for x in financial_report.account_type_ids]
+                if record.id in list_ids:
+                    res[record.id] = key
+        return res
+
+    def _save_report_type(self, cr, uid, account_type_id, field_name, field_value, arg, context=None):
+        obj_data = self.pool.get('ir.model.data')
+        obj_financial_report = self.pool.get('account.financial.report') 
+        #unlink if it exists somewhere in the financial reports related to BS or PL
+        financial_report_ref = {
+            'asset': obj_financial_report.browse(cr, uid, obj_data.get_object_reference(cr, uid, 'account','account_financial_report_assets0')[1], context=context),
+            'liability': obj_financial_report.browse(cr, uid, obj_data.get_object_reference(cr, uid, 'account','account_financial_report_liability0')[1], context=context),
+            'income': obj_financial_report.browse(cr, uid, obj_data.get_object_reference(cr, uid, 'account','account_financial_report_income0')[1], context=context),
+            'expense': obj_financial_report.browse(cr, uid, obj_data.get_object_reference(cr, uid, 'account','account_financial_report_expense0')[1], context=context),
+        }
+        for key, financial_report in financial_report_ref.items():
+            list_ids = [x.id for x in financial_report.account_type_ids]
+            if account_type_id in list_ids:
+                obj_financial_report.write(cr, uid, [financial_report.id], {'account_type_ids': [(3, account_type_id)]})
+        #write it in the good place
+        if field_value != 'none':
+            return obj_financial_report.write(cr, uid, [financial_report_ref[field_value].id], {'account_type_ids': [(4, account_type_id)]})
+
+    _columns = {
+        'name': fields.char('Account Type', size=64, required=True, translate=True),
+        'code': fields.char('Code', size=32, required=True, select=True),
+        'close_method': fields.selection([('none', 'None'), ('balance', 'Balance'), ('detail', 'Detail'), ('unreconciled', 'Unreconciled')], 'Deferral Method', required=True, help="""Set here the method that will be used to generate the end of year journal entries for all the accounts of this type.
+
+ 'None' means that nothing will be done.
+ 'Balance' will generally be used for cash accounts.
+ 'Detail' will copy each existing journal item of the previous year, even the reconciled ones.
+ 'Unreconciled' will copy only the journal items that were unreconciled on the first day of the new fiscal year."""),
+        'report_type': fields.function(_get_current_report_type, fnct_inv=_save_report_type, type='selection', string='P&L / BS Category', 
+            selection= [('none','/'),
+                        ('income', _('Profit & Loss (Income account)')),
+                        ('expense', _('Profit & Loss (Expense account)')),
+                        ('asset', _('Balance Sheet (Asset account)')),
+                        ('liability', _('Balance Sheet (Liability account)'))], help="This field is used to generate legal reports: profit and loss, balance sheet.", required=True),
+        'note': fields.text('Description'),
+    }
+    _defaults = {
+        'close_method': 'none',
+        'report_type': 'none',
+    }
+    _order = "code"
+
+account_account_type()
+
+def _code_get(self, cr, uid, context=None):
+    acc_type_obj = self.pool.get('account.account.type')
+    ids = acc_type_obj.search(cr, uid, [])
+    res = acc_type_obj.read(cr, uid, ids, ['code', 'name'], context=context)
+    return [(r['code'], r['name']) for r in res]
+
+#----------------------------------------------------------
+# Accounts
+#----------------------------------------------------------
+
+class account_tax(osv.osv):
+    _name = 'account.tax'
+account_tax()
+
+class account_account(osv.osv):
+    _order = "parent_left"
+    _parent_order = "code"
+    _name = "account.account"
+    _description = "Account"
+    _parent_store = True
+    logger = netsvc.Logger()
+
+    def search(self, cr, uid, args, offset=0, limit=None, order=None,
+            context=None, count=False):
+        if context is None:
+            context = {}
+        pos = 0
+
+        while pos < len(args):
+
+            if args[pos][0] == 'code' and args[pos][1] in ('like', 'ilike') and args[pos][2]:
+                args[pos] = ('code', '=like', str(args[pos][2].replace('%', ''))+'%')
+            if args[pos][0] == 'journal_id':
+                if not args[pos][2]:
+                    del args[pos]
+                    continue
+                jour = self.pool.get('account.journal').browse(cr, uid, args[pos][2], context=context)
+                if (not (jour.account_control_ids or jour.type_control_ids)) or not args[pos][2]:
+                    args[pos] = ('type','not in',('consolidation','view'))
+                    continue
+                ids3 = map(lambda x: x.id, jour.type_control_ids)
+                ids1 = super(account_account, self).search(cr, uid, [('user_type', 'in', ids3)])
+                ids1 += map(lambda x: x.id, jour.account_control_ids)
+                args[pos] = ('id', 'in', ids1)
+            pos += 1
+
+        if context and context.has_key('consolidate_children'): #add consolidated children of accounts
+            ids = super(account_account, self).search(cr, uid, args, offset, limit,
+                order, context=context, count=count)
+            for consolidate_child in self.browse(cr, uid, context['account_id'], context=context).child_consol_ids:
+                ids.append(consolidate_child.id)
+            return ids
+
+        return super(account_account, self).search(cr, uid, args, offset, limit,
+                order, context=context, count=count)
+
+    def _get_children_and_consol(self, cr, uid, ids, context=None):
+        #this function search for all the children and all consolidated children (recursively) of the given account ids
+        ids2 = self.search(cr, uid, [('parent_id', 'child_of', ids)], context=context)
+        ids3 = []
+        for rec in self.browse(cr, uid, ids2, context=context):
+            for child in rec.child_consol_ids:
+                ids3.append(child.id)
+        if ids3:
+            ids3 = self._get_children_and_consol(cr, uid, ids3, context)
+        return ids2 + ids3
+
+    def __compute(self, cr, uid, ids, field_names, arg=None, context=None,
+                  query='', query_params=()):
+        """ compute the balance, debit and/or credit for the provided
+        account ids
+        Arguments:
+        `ids`: account ids
+        `field_names`: the fields to compute (a list of any of
+                       'balance', 'debit' and 'credit')
+        `arg`: unused fields.function stuff
+        `query`: additional query filter (as a string)
+        `query_params`: parameters for the provided query string
+                        (__compute will handle their escaping) as a
+                        tuple
+        """
+        mapping = {
+            'balance': "COALESCE(SUM(l.debit),0) - COALESCE(SUM(l.credit), 0) as balance",
+            'debit': "COALESCE(SUM(l.debit), 0) as debit",
+            'credit': "COALESCE(SUM(l.credit), 0) as credit",
+            # by convention, foreign_balance is 0 when the account has no secondary currency, because the amounts may be in different currencies
+            'foreign_balance': "(SELECT CASE WHEN currency_id IS NULL THEN 0 ELSE COALESCE(SUM(l.amount_currency), 0) END FROM account_account WHERE id IN (l.account_id)) as foreign_balance",
+        }
+        #get all the necessary accounts
+        children_and_consolidated = self._get_children_and_consol(cr, uid, ids, context=context)
+        #compute for each account the balance/debit/credit from the move lines
+        accounts = {}
+        res = {}
+        null_result = dict((fn, 0.0) for fn in field_names)
+        if children_and_consolidated:
+            aml_query = self.pool.get('account.move.line')._query_get(cr, uid, context=context)
+
+            wheres = [""]
+            if query.strip():
+                wheres.append(query.strip())
+            if aml_query.strip():
+                wheres.append(aml_query.strip())
+            filters = " AND ".join(wheres)
+            self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
+                                      'Filters: %s'%filters)
+            # IN might not work ideally in case there are too many
+            # children_and_consolidated, in that case join on a
+            # values() e.g.:
+            # SELECT l.account_id as id FROM account_move_line l
+            # INNER JOIN (VALUES (id1), (id2), (id3), ...) AS tmp (id)
+            # ON l.account_id = tmp.id
+            # or make _get_children_and_consol return a query and join on that
+            request = ("SELECT l.account_id as id, " +\
+                       ', '.join(mapping.values()) +
+                       " FROM account_move_line l" \
+                       " WHERE l.account_id IN %s " \
+                            + filters +
+                       " GROUP BY l.account_id")
+            params = (tuple(children_and_consolidated),) + query_params
+            cr.execute(request, params)
+            self.logger.notifyChannel('addons.'+self._name, netsvc.LOG_DEBUG,
+                                      'Status: %s'%cr.statusmessage)
+
+            for res in cr.dictfetchall():
+                accounts[res['id']] = res
+
+            # consolidate accounts with direct children
+            children_and_consolidated.reverse()
+            brs = list(self.browse(cr, uid, children_and_consolidated, context=context))
+            sums = {}
+            currency_obj = self.pool.get('res.currency')
+            while brs:
+                current = brs.pop(0)
+#                can_compute = True
+#                for child in current.child_id:
+#                    if child.id not in sums:
+#                        can_compute = False
+#                        try:
+#                            brs.insert(0, brs.pop(brs.index(child)))
+#                        except ValueError:
+#                            brs.insert(0, child)
+#                if can_compute:
+                for fn in field_names:
+                    sums.setdefault(current.id, {})[fn] = accounts.get(current.id, {}).get(fn, 0.0)
+                    for child in current.child_id:
+                        if child.company_id.currency_id.id == current.company_id.currency_id.id:
+                            sums[current.id][fn] += sums[child.id][fn]
+                        else:
+                            sums[current.id][fn] += currency_obj.compute(cr, uid, child.company_id.currency_id.id, current.company_id.currency_id.id, sums[child.id][fn], context=context)
+
+                # as we have to relay on values computed before this is calculated separately than previous fields
+                if current.currency_id and current.exchange_rate and \
+                            ('adjusted_balance' in field_names or 'unrealized_gain_loss' in field_names):
+                    # Computing Adjusted Balance and Unrealized Gains and losses
+                    # Adjusted Balance = Foreign Balance / Exchange Rate
+                    # Unrealized Gains and losses = Adjusted Balance - Balance
+                    adj_bal = sums[current.id].get('foreign_balance', 0.0) / current.exchange_rate
+                    sums[current.id].update({'adjusted_balance': adj_bal, 'unrealized_gain_loss': adj_bal - sums[current.id].get('balance', 0.0)})
+
+            for id in ids:
+                res[id] = sums.get(id, null_result)
+        else:
+            for id in ids:
+                res[id] = null_result
+        return res
+
+    def _get_company_currency(self, cr, uid, ids, field_name, arg, context=None):
+        result = {}
+        for rec in self.browse(cr, uid, ids, context=context):
+            result[rec.id] = (rec.company_id.currency_id.id,rec.company_id.currency_id.symbol)
+        return result
+
+    def _get_child_ids(self, cr, uid, ids, field_name, arg, context=None):
+        result = {}
+        for record in self.browse(cr, uid, ids, context=context):
+            if record.child_parent_ids:
+                result[record.id] = [x.id for x in record.child_parent_ids]
+            else:
+                result[record.id] = []
+
+            if record.child_consol_ids:
+                for acc in record.child_consol_ids:
+                    if acc.id not in result[record.id]:
+                        result[record.id].append(acc.id)
+
+        return result
+
+    def _get_level(self, cr, uid, ids, field_name, arg, context=None):
+        res = {}
+        for account in self.browse(cr, uid, ids, context=context):
+            #we may not know the level of the parent at the time of computation, so we
+            # can't simply do res[account.id] = account.parent_id.level + 1
+            level = 0
+            parent = account.parent_id
+            while parent:
+                level += 1
+                parent = parent.parent_id
+            res[account.id] = level
+        return res
+
+    def _set_credit_debit(self, cr, uid, account_id, name, value, arg, context=None):
+        if context.get('config_invisible', True):
+            return True
+
+        account = self.browse(cr, uid, account_id, context=context)
+        diff = value - getattr(account,name)
+        if not diff:
+            return True
+
+        journal_obj = self.pool.get('account.journal')
+        jids = journal_obj.search(cr, uid, [('type','=','situation'),('centralisation','=',1),('company_id','=',account.company_id.id)], context=context)
+        if not jids:
+            raise osv.except_osv(_('Error!'),_("You need an Opening journal with centralisation checked to set the initial balance!"))
+
+        period_obj = self.pool.get('account.period')
+        pids = period_obj.search(cr, uid, [('special','=',True),('company_id','=',account.company_id.id)], context=context)
+        if not pids:
+            raise osv.except_osv(_('Error!'),_("No opening/closing period defined, please create one to set the initial balance!"))
+
+        move_obj = self.pool.get('account.move.line')
+        move_id = move_obj.search(cr, uid, [
+            ('journal_id','=',jids[0]),
+            ('period_id','=',pids[0]),
+            ('account_id','=', account_id),
+            (name,'>', 0.0),
+            ('name','=', _('Opening Balance'))
+        ], context=context)
+        if move_id:
+            move = move_obj.browse(cr, uid, move_id[0], context=context)
+            move_obj.write(cr, uid, move_id[0], {
+                name: diff+getattr(move,name)
+            }, context=context)
+        else:
+            if diff<0.0:
+                raise osv.except_osv(_('Error!'),_("Unable to adapt the initial balance (negative value)!"))
+            nameinv = (name=='credit' and 'debit') or 'credit'
+            move_id = move_obj.create(cr, uid, {
+                'name': _('Opening Balance'),
+                'account_id': account_id,
+                'journal_id': jids[0],
+                'period_id': pids[0],
+                name: diff,
+                nameinv: 0.0
+            }, context=context)
+        return True
+
+    _columns = {
+        'name': fields.char('Name', size=256, required=True, select=True),
+        'currency_id': fields.many2one('res.currency', 'Secondary Currency', help="Forces all moves for this account to have this secondary currency."),
+        'code': fields.char('Code', size=64, required=True, select=1),
+        'type': fields.selection([
+            ('view', 'View'),
+            ('other', 'Regular'),
+            ('receivable', 'Receivable'),
+            ('payable', 'Payable'),
+            ('liquidity','Liquidity'),
+            ('consolidation', 'Consolidation'),
+            ('closed', 'Closed'),
+        ], 'Internal Type', required=True, help="The 'Internal Type' is used for features available on "\
+            "different types of accounts: view can not have journal items, consolidation are accounts that "\
+            "can have children accounts for multi-company consolidations, payable/receivable are for "\
+            "partners accounts (for debit/credit computations), closed for depreciated accounts."),
+        'user_type': fields.many2one('account.account.type', 'Account Type', required=True,
+            help="Account Type is used for information purpose, to generate "
+              "country-specific legal reports, and set the rules to close a fiscal year and generate opening entries."),
+        'financial_report_ids': fields.many2many('account.financial.report', 'account_account_financial_report', 'account_id', 'report_line_id', 'Financial Reports'),
+        'parent_id': fields.many2one('account.account', 'Parent', ondelete='cascade', domain=[('type','=','view')]),
+        'child_parent_ids': fields.one2many('account.account','parent_id','Children'),
+        'child_consol_ids': fields.many2many('account.account', 'account_account_consol_rel', 'child_id', 'parent_id', 'Consolidated Children'),
+        'child_id': fields.function(_get_child_ids, type='many2many', relation="account.account", string="Child Accounts"),
+        'balance': fields.function(__compute, digits_compute=dp.get_precision('Account'), string='Balance', multi='balance'),
+        'credit': fields.function(__compute, fnct_inv=_set_credit_debit, digits_compute=dp.get_precision('Account'), string='Credit', multi='balance'),
+        'debit': fields.function(__compute, fnct_inv=_set_credit_debit, digits_compute=dp.get_precision('Account'), string='Debit', multi='balance'),
+        'foreign_balance': fields.function(__compute, digits_compute=dp.get_precision('Account'), string='Foreign Balance', multi='balance',
+                                           help="Total amount (in Secondary currency) for transactions held in secondary currency for this account."),
+        'adjusted_balance': fields.function(__compute, digits_compute=dp.get_precision('Account'), string='Adjusted Balance', multi='balance',
+                                            help="Total amount (in Company currency) for transactions held in secondary currency for this account."),
+        'unrealized_gain_loss': fields.function(__compute, digits_compute=dp.get_precision('Account'), string='Unrealized Gain or Loss', multi='balance',
+                                                help="Value of Loss or Gain due to changes in exchange rate when doing multi-currency transactions."),
+        'reconcile': fields.boolean('Allow Reconciliation', help="Check this box if this account allows reconciliation of journal items."),
+        'exchange_rate': fields.related('currency_id', 'rate', type='float', string='Exchange Rate', digits=(12,6)),
+        'shortcut': fields.char('Shortcut', size=12),
+        'tax_ids': fields.many2many('account.tax', 'account_account_tax_default_rel',
+            'account_id', 'tax_id', 'Default Taxes'),
+        'note': fields.text('Note'),
+        'company_currency_id': fields.function(_get_company_currency, type='many2one', relation='res.currency', string='Company Currency'),
+        'company_id': fields.many2one('res.company', 'Company', required=True),
+        'active': fields.boolean('Active', select=2, help="If the active field is set to False, it will allow you to hide the account without removing it."),
+
+        'parent_left': fields.integer('Parent Left', select=1),
+        'parent_right': fields.integer('Parent Right', select=1),
+        'currency_mode': fields.selection([('current', 'At Date'), ('average', 'Average Rate')], 'Outgoing Currencies Rate',
+            help=
+            'This will select how the current currency rate for outgoing transactions is computed. '\
+            'In most countries the legal method is "average" but only a few software systems are able to '\
+            'manage this. So if you import from another software system you may have to use the rate at date. ' \
+            'Incoming transactions always use the rate at date.', \
+            required=True),
+        'level': fields.function(_get_level, string='Level', method=True, type='integer',
+             store={
+                    'account.account': (_get_children_and_consol, ['level', 'parent_id'], 10),
+                   }),
+    }
+
+    _defaults = {
+        'type': 'other',
+        'reconcile': False,
+        'active': True,
+        'currency_mode': 'current',
+        'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'account.account', context=c),
+    }
+
+    def _check_recursion(self, cr, uid, ids, context=None):
+        obj_self = self.browse(cr, uid, ids[0], context=context)
+        p_id = obj_self.parent_id and obj_self.parent_id.id
+        if (obj_self in obj_self.child_consol_ids) or (p_id and (p_id is obj_self.id)):
+            return False
+        while(ids):
+            cr.execute('SELECT DISTINCT child_id '\
+                       'FROM account_account_consol_rel '\
+                       'WHERE parent_id IN %s', (tuple(ids),))
+            child_ids = map(itemgetter(0), cr.fetchall())
+            c_ids = child_ids
+            if (p_id and (p_id in c_ids)) or (obj_self.id in c_ids):
+                return False
+            while len(c_ids):
+                s_ids = self.search(cr, uid, [('parent_id', 'in', c_ids)])
+                if p_id and (p_id in s_ids):
+                    return False
+                c_ids = s_ids
+            ids = child_ids
+        return True
+
+    def _check_type(self, cr, uid, ids, context=None):
+        if context is None:
+            context = {}
+        accounts = self.browse(cr, uid, ids, context=context)
+        for account in accounts:
+            if account.child_id and account.type not in ('view', 'consolidation'):
+                return False
+        return True
+
+    def _check_account_type(self, cr, uid, ids, context=None):
+        for account in self.browse(cr, uid, ids, context=context):
+            if account.type in ('receivable', 'payable') and account.user_type.close_method != 'unreconciled':
+                return False
+        return True
+
+    _constraints = [
+        (_check_recursion, 'Error ! You can not create recursive accounts.', ['parent_id']),
+        (_check_type, 'Configuration Error! \nYou can not define children to an account with internal type different of "View"! ', ['type']),
+        (_check_account_type, 'Configuration Error! \nYou can not select an account type with a deferral method different of "Unreconciled" for accounts with internal type "Payable/Receivable"! ', ['user_type','type']),
+    ]
+    _sql_constraints = [
+        ('code_company_uniq', 'unique (code,company_id)', 'The code of the account must be unique per company !')
+    ]
+    def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=100):
+        if not args:
+            args = []
+        args = args[:]
+        ids = []
+        try:
+            if name and str(name).startswith('partner:'):
+                part_id = int(name.split(':')[1])
+                part = self.pool.get('res.partner').browse(cr, user, part_id, context=context)
+                args += [('id', 'in', (part.property_account_payable.id, part.property_account_receivable.id))]
+                name = False
+            if name and str(name).startswith('type:'):
+                type = name.split(':')[1]
+                args += [('type', '=', type)]
+                name = False
+        except:
+            pass
+        if name:
+            ids = self.search(cr, user, [('code', '=like', name+"%")]+args, limit=limit)
+            if not ids:
+                ids = self.search(cr, user, [('shortcut', '=', name)]+ args, limit=limit)
+            if not ids:
+                ids = self.search(cr, user, [('name', operator, name)]+ args, limit=limit)
+            if not ids and len(name.split()) >= 2:
+                #Separating code and name of account for searching
+                operand1,operand2 = name.split(' ',1) #name can contain spaces e.g. OpenERP S.A.
+                ids = self.search(cr, user, [('code', operator, operand1), ('name', operator, operand2)]+ args, limit=limit)
+        else:
+            ids = self.search(cr, user, args, context=context, limit=limit)
+        return self.name_get(cr, user, ids, context=context)
+
+    def name_get(self, cr, uid, ids, context=None):
+        if not ids:
+            return []
+        reads = self.read(cr, uid, ids, ['name', 'code'], context=context)
+        res = []
+        for record in reads:
+            name = record['name']
+            if record['code']:
+                name = record['code'] + ' ' + name
+            res.append((record['id'], name))
+        return res
+
+    def copy(self, cr, uid, id, default={}, context=None, done_list=[], local=False):
+        account = self.browse(cr, uid, id, context=context)
+        new_child_ids = []
+        if not default:
+            default = {}
+        default = default.copy()
+        default['code'] = (account['code'] or '') + '(copy)'
+        if not local:
+            done_list = []
+        if account.id in done_list:
+            return False
+        done_list.append(account.id)
+        if account:
+            for child in account.child_id:
+                child_ids = self.copy(cr, uid, child.id, default, context=context, done_list=done_list, local=True)
+                if child_ids:
+                    new_child_ids.append(child_ids)
+            default['child_parent_ids'] = [(6, 0, new_child_ids)]
+        else:
+            default['child_parent_ids'] = False
+        return super(account_account, self).copy(cr, uid, id, default, context=context)
+
+    def _check_moves(self, cr, uid, ids, method, context=None):
+        line_obj = self.pool.get('account.move.line')
+        account_ids = self.search(cr, uid, [('id', 'child_of', ids)])
+
+        if line_obj.search(cr, uid, [('account_id', 'in', account_ids)]):
+            if method == 'write':
+                raise osv.except_osv(_('Error !'), _('You can not desactivate an account that contains some journal items.'))
+            elif method == 'unlink':
+                raise osv.except_osv(_('Error !'), _('You can not remove an account containing journal items.'))
+        #Checking whether the account is set as a property to any Partner or not
+        value = 'account.account,' + str(ids[0])
+        partner_prop_acc = self.pool.get('ir.property').search(cr, uid, [('value_reference','=',value)], context=context)
+        if partner_prop_acc:
+            raise osv.except_osv(_('Warning !'), _('You can not remove/desactivate an account which is set on a customer or supplier.'))
+        return True
+
+    def _check_allow_type_change(self, cr, uid, ids, new_type, context=None):
+        group1 = ['payable', 'receivable', 'other']
+        group2 = ['consolidation','view']
+        line_obj = self.pool.get('account.move.line')
+        for account in self.browse(cr, uid, ids, context=context):
+            old_type = account.type
+            account_ids = self.search(cr, uid, [('id', 'child_of', [account.id])])
+            if line_obj.search(cr, uid, [('account_id', 'in', account_ids)]):
+                #Check for 'Closed' type
+                if old_type == 'closed' and new_type !='closed':
+                    raise osv.except_osv(_('Warning !'), _("You cannot change the type of account from 'Closed' to any other type which contains journal items!"))
+                #Check for change From group1 to group2 and vice versa
+                if (old_type in group1 and new_type in group2) or (old_type in group2 and new_type in group1):
+                    raise osv.except_osv(_('Warning !'), _("You cannot change the type of account from '%s' to '%s' type as it contains journal items!") % (old_type,new_type,))
+        return True
+
+    def write(self, cr, uid, ids, vals, context=None):
+
+        if context is None:
+            context = {}
+        if not ids:
+            return True
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+
+        # Dont allow changing the company_id when account_move_line already exist
+        if 'company_id' in vals:
+            move_lines = self.pool.get('account.move.line').search(cr, uid, [('account_id', 'in', ids)])
+            if move_lines:
+                # Allow the write if the value is the same
+                for i in [i['company_id'][0] for i in self.read(cr,uid,ids,['company_id'])]:
+                    if vals['company_id']!=i:
+                        raise osv.except_osv(_('Warning !'), _('You cannot change the owner company of an account that already contains journal items.'))
+        if 'active' in vals and not vals['active']:
+            self._check_moves(cr, uid, ids, "write", context=context)
+        if 'type' in vals.keys():
+            self._check_allow_type_change(cr, uid, ids, vals['type'], context=context)
+        return super(account_account, self).write(cr, uid, ids, vals, context=context)
+
+    def unlink(self, cr, uid, ids, context=None):
+        self._check_moves(cr, uid, ids, "unlink", context=context)
+        return super(account_account, self).unlink(cr, uid, ids, context=context)
+
+account_account()
+
+class account_journal_view(osv.osv):
+    _name = "account.journal.view"
+    _description = "Journal View"
+    _columns = {
+        'name': fields.char('Journal View', size=64, required=True),
+        'columns_id': fields.one2many('account.journal.column', 'view_id', 'Columns')
+    }
+    _order = "name"
+
+account_journal_view()
+
+
+class account_journal_column(osv.osv):
+
+    def _col_get(self, cr, user, context=None):
+        result = []
+        cols = self.pool.get('account.move.line')._columns
+        for col in cols:
+            if col in ('period_id', 'journal_id'):
+                continue
+            result.append( (col, cols[col].string) )
+        result.sort()
+        return result
+
+    _name = "account.journal.column"
+    _description = "Journal Column"
+    _columns = {
+        'name': fields.char('Column Name', size=64, required=True),
+        'field': fields.selection(_col_get, 'Field Name', required=True, size=32),
+        'view_id': fields.many2one('account.journal.view', 'Journal View', select=True),
+        'sequence': fields.integer('Sequence', help="Gives the sequence order to journal column.", readonly=True),
+        'required': fields.boolean('Required'),
+        'readonly': fields.boolean('Readonly'),
+    }
+    _order = "view_id, sequence"
+
+account_journal_column()
+
+class account_journal(osv.osv):
+    _name = "account.journal"
+    _description = "Journal"
+    _columns = {
+        'name': fields.char('Journal Name', size=64, required=True),
+        'code': fields.char('Code', size=5, required=True, help="The code will be displayed on reports."),
+        'type': fields.selection([('sale', 'Sale'),('sale_refund','Sale Refund'), ('purchase', 'Purchase'), ('purchase_refund','Purchase Refund'), ('cash', 'Cash'), ('bank', 'Bank and Cheques'), ('general', 'General'), ('situation', 'Opening/Closing Situation')], 'Type', size=32, required=True,
+                                 help="Select 'Sale' for customer invoices journals."\
+                                 " Select 'Purchase' for supplier invoices journals."\
+                                 " Select 'Cash' or 'Bank' for journals that are used in customer or supplier payments."\
+                                 " Select 'General' for miscellaneous operations journals."\
+                                 " Select 'Opening/Closing Situation' for entries generated for new fiscal years."),
+        'type_control_ids': fields.many2many('account.account.type', 'account_journal_type_rel', 'journal_id','type_id', 'Type Controls', domain=[('code','<>','view'), ('code', '<>', 'closed')]),
+        'account_control_ids': fields.many2many('account.account', 'account_account_type_rel', 'journal_id','account_id', 'Account', domain=[('type','<>','view'), ('type', '<>', 'closed')]),
+        'view_id': fields.many2one('account.journal.view', 'Display Mode', required=True, help="Gives the view used when writing or browsing entries in this journal. The view tells OpenERP which fields should be visible, required or readonly and in which order. You can create your own view for a faster encoding in each journal."),
+        'default_credit_account_id': fields.many2one('account.account', 'Default Credit Account', domain="[('type','!=','view')]", help="It acts as a default account for credit amount"),
+        'default_debit_account_id': fields.many2one('account.account', 'Default Debit Account', domain="[('type','!=','view')]", help="It acts as a default account for debit amount"),
+        'centralisation': fields.boolean('Centralised counterpart', help="Check this box to determine that each entry of this journal won't create a new counterpart but will share the same counterpart. This is used in fiscal year closing."),
+        'update_posted': fields.boolean('Allow Cancelling Entries', help="Check this box if you want to allow the cancellation the entries related to this journal or of the invoice related to this journal"),
+        'group_invoice_lines': fields.boolean('Group Invoice Lines', help="If this box is checked, the system will try to group the accounting lines when generating them from invoices."),
+        'sequence_id': fields.many2one('ir.sequence', 'Entry Sequence', help="This field contains the informatin related to the numbering of the journal entries of this journal.", required=True),
+        'user_id': fields.many2one('res.users', 'User', help="The user responsible for this journal"),
+        'groups_id': fields.many2many('res.groups', 'account_journal_group_rel', 'journal_id', 'group_id', 'Groups'),
+        'currency': fields.many2one('res.currency', 'Currency', help='The currency used to enter statement'),
+        'entry_posted': fields.boolean('Skip \'Draft\' State for Manual Entries', help='Check this box if you don\'t want new journal entries to pass through the \'draft\' state and instead goes directly to the \'posted state\' without any manual validation. \nNote that journal entries that are automatically created by the system are always skipping that state.'),
+        'company_id': fields.many2one('res.company', 'Company', required=True, select=1, help="Company related to this journal"),
+        'allow_date':fields.boolean('Check Date in Period', help= 'If set to True then do not accept the entry if the entry date is not into the period dates'),
+    }
+
+    _defaults = {
+        'user_id': lambda self, cr, uid, context: uid,
+        'company_id': lambda self, cr, uid, c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.id,
+    }
+    _sql_constraints = [
+        ('code_company_uniq', 'unique (code, company_id)', 'The code of the journal must be unique per company !'),
+        ('name_company_uniq', 'unique (name, company_id)', 'The name of the journal must be unique per company !'),
+    ]
+
+    _order = 'code'
+
+    def _check_currency(self, cr, uid, ids, context=None):
+        for journal in self.browse(cr, uid, ids, context=context):
+            if journal.currency:
+                if journal.default_credit_account_id and not journal.default_credit_account_id.currency_id.id == journal.currency.id:
+                    return False
+                if journal.default_debit_account_id and not journal.default_debit_account_id.currency_id.id == journal.currency.id:
+                    return False
+        return True
+
+    _constraints = [
+        (_check_currency, 'Configuration error! The currency chosen should be shared by the default accounts too.', ['currency','default_debit_account_id','default_credit_account_id']),
+    ]
+
+    def copy(self, cr, uid, id, default={}, context=None, done_list=[], local=False):
+        journal = self.browse(cr, uid, id, context=context)
+        if not default:
+            default = {}
+        default = default.copy()
+        default['code'] = (journal['code'] or '') + '(copy)'
+        default['name'] = (journal['name'] or '') + '(copy)'
+        default['sequence_id'] = False
+        return super(account_journal, self).copy(cr, uid, id, default, context=context)
+
+    def write(self, cr, uid, ids, vals, context=None):
+        if context is None:
+            context = {}
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        for journal in self.browse(cr, uid, ids, context=context):
+            if 'company_id' in vals and journal.company_id.id != vals['company_id']:
+                move_lines = self.pool.get('account.move.line').search(cr, uid, [('journal_id', 'in', ids)])
+                if move_lines:
+                    raise osv.except_osv(_('Warning !'), _('You can not modify the company of this journal as its related record exist in journal items'))
+        return super(account_journal, self).write(cr, uid, ids, vals, context=context)
+
+    def create_sequence(self, cr, uid, vals, context=None):
+        """ Create new no_gap entry sequence for every new Joural
+        """
+        # in account.journal code is actually the prefix of the sequence
+        # whereas ir.sequence code is a key to lookup global sequences.
+        prefix = vals['code'].upper()
+
+        seq = {
+            'name': vals['name'],
+            'implementation':'no_gap',
+            'prefix': prefix + "/%(year)s/",
+            'padding': 4,
+            'number_increment': 1
+        }
+        if 'company_id' in vals:
+            seq['company_id'] = vals['company_id']
+        return self.pool.get('ir.sequence').create(cr, uid, seq)
+
+    def create(self, cr, uid, vals, context=None):
+        if not 'sequence_id' in vals or not vals['sequence_id']:
+            # if we have the right to create a journal, we should be able to
+            # create it's sequence.
+            vals.update({'sequence_id': self.create_sequence(cr, 1, vals, context)})
+        return super(account_journal, self).create(cr, uid, vals, context)
+
+    def name_get(self, cr, user, ids, context=None):
+        """
+        Returns a list of tupples containing id, name.
+        result format: {[(id, name), (id, name), ...]}
+
+        @param cr: A database cursor
+        @param user: ID of the user currently logged in
+        @param ids: list of ids for which name should be read
+        @param context: context arguments, like lang, time zone
+
+        @return: Returns a list of tupples containing id, name
+        """
+        result = self.browse(cr, user, ids, context=context)
+        res = []
+        for rs in result:
+            if rs.currency:
+                currency = rs.currency
+            else:
+                currency = rs.company_id.currency_id
+            name = "%s (%s)" % (rs.name, currency.name)
+            res += [(rs.id, name)]
+        return res
+
+    def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=100):
+        if not args:
+            args = []
+        if context is None:
+            context = {}
+        ids = []
+        if context.get('journal_type', False):
+            args += [('type','=',context.get('journal_type'))]
+        if name:
+            ids = self.search(cr, user, [('code', 'ilike', name)]+ args, limit=limit, context=context)
+        if not ids:
+            ids = self.search(cr, user, [('name', 'ilike', name)]+ args, limit=limit, context=context)#fix it ilike should be replace with operator
+
+        return self.name_get(cr, user, ids, context=context)
+
+    def onchange_type(self, cr, uid, ids, type, currency, context=None):
+        obj_data = self.pool.get('ir.model.data')
+        user_pool = self.pool.get('res.users')
+
+        type_map = {
+            'sale':'account_sp_journal_view',
+            'sale_refund':'account_sp_refund_journal_view',
+            'purchase':'account_sp_journal_view',
+            'purchase_refund':'account_sp_refund_journal_view',
+            'cash':'account_journal_bank_view',
+            'bank':'account_journal_bank_view',
+            'general':'account_journal_view',
+            'situation':'account_journal_view'
+        }
+
+        res = {}
+        view_id = type_map.get(type, 'account_journal_view')
+        user = user_pool.browse(cr, uid, uid)
+        if type in ('cash', 'bank') and currency and user.company_id.currency_id.id != currency:
+            view_id = 'account_journal_bank_view_multi'
+        data_id = obj_data.search(cr, uid, [('model','=','account.journal.view'), ('name','=',view_id)])
+        data = obj_data.browse(cr, uid, data_id[0], context=context)
+
+        res.update({
+            'centralisation':type == 'situation',
+            'view_id':data.res_id,
+        })
+        return {
+            'value':res
+        }
+
+account_journal()
+
+class account_fiscalyear(osv.osv):
+    _name = "account.fiscalyear"
+    _description = "Fiscal Year"
+    _columns = {
+        'name': fields.char('Fiscal Year', size=64, required=True),
+        'code': fields.char('Code', size=6, required=True),
+        'company_id': fields.many2one('res.company', 'Company', required=True),
+        'date_start': fields.date('Start Date', required=True),
+        'date_stop': fields.date('End Date', required=True),
+        'period_ids': fields.one2many('account.period', 'fiscalyear_id', 'Periods'),
+        'state': fields.selection([('draft','Open'), ('done','Closed')], 'State', readonly=True),
+    }
+    _defaults = {
+        'state': 'draft',
+        'company_id': lambda self,cr,uid,c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.id,
+    }
+    _order = "date_start, id"
+
+
+    def _check_duration(self, cr, uid, ids, context=None):
+        obj_fy = self.browse(cr, uid, ids[0], context=context)
+        if obj_fy.date_stop < obj_fy.date_start:
+            return False
+        return True
+
+    _constraints = [
+        (_check_duration, 'Error! The start date of the fiscal year must be before his end date.', ['date_start','date_stop'])
+    ]
+
+    def create_period3(self, cr, uid, ids, context=None):
+        return self.create_period(cr, uid, ids, context, 3)
+
+    def create_period(self, cr, uid, ids, context=None, interval=1):
+        period_obj = self.pool.get('account.period')
+        for fy in self.browse(cr, uid, ids, context=context):
+            ds = datetime.strptime(fy.date_start, '%Y-%m-%d')
+            period_obj.create(cr, uid, {
+                    'name':  "%s %s" % (_('Opening Period'), ds.strftime('%Y')),
+                    'code': ds.strftime('00/%Y'),
+                    'date_start': ds,
+                    'date_stop': ds,
+                    'special': True,
+                    'fiscalyear_id': fy.id,
+                })
+            while ds.strftime('%Y-%m-%d') < fy.date_stop:
+                de = ds + relativedelta(months=interval, days=-1)
+
+                if de.strftime('%Y-%m-%d') > fy.date_stop:
+                    de = datetime.strptime(fy.date_stop, '%Y-%m-%d')
+
+                period_obj.create(cr, uid, {
+                    'name': ds.strftime('%m/%Y'),
+                    'code': ds.strftime('%m/%Y'),
+                    'date_start': ds.strftime('%Y-%m-%d'),
+                    'date_stop': de.strftime('%Y-%m-%d'),
+                    'fiscalyear_id': fy.id,
+                })
+                ds = ds + relativedelta(months=interval)
+        return True
+
+    def find(self, cr, uid, dt=None, exception=True, context=None):
+        res = self.finds(cr, uid, dt, exception, context=context)
+        return res and res[0] or False
+
+    def finds(self, cr, uid, dt=None, exception=True, context=None):
+        if context is None: context = {}
+        if not dt:
+            dt = fields.date.context_today(self,cr,uid,context=context)
+        args = [('date_start', '<=' ,dt), ('date_stop', '>=', dt)]
+        if context.get('company_id', False):
+            company_id = context['company_id']
+        else:
+            company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
+        args.append(('company_id', '=', company_id))
+        ids = self.search(cr, uid, args, context=context)
+        if not ids:
+            if exception:
+                raise osv.except_osv(_('Error !'), _('No fiscal year defined for this date !\nPlease create one from the configuration of the accounting menu.'))
+            else:
+                return []
+        return ids
+
+    def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=80):
+        if args is None:
+            args = []
+        if context is None:
+            context = {}
+        ids = []
+        if name:
+            ids = self.search(cr, user, [('code', 'ilike', name)]+ args, limit=limit)
+        if not ids:
+            ids = self.search(cr, user, [('name', operator, name)]+ args, limit=limit)
+        return self.name_get(cr, user, ids, context=context)
+
+account_fiscalyear()
+
+class account_period(osv.osv):
+    _name = "account.period"
+    _description = "Account period"
+    _columns = {
+        'name': fields.char('Period Name', size=64, required=True),
+        'code': fields.char('Code', size=12),
+        'special': fields.boolean('Opening/Closing Period', size=12,
+            help="These periods can overlap."),
+        'date_start': fields.date('Start of Period', required=True, states={'done':[('readonly',True)]}),
+        'date_stop': fields.date('End of Period', required=True, states={'done':[('readonly',True)]}),
+        'fiscalyear_id': fields.many2one('account.fiscalyear', 'Fiscal Year', required=True, states={'done':[('readonly',True)]}, select=True),
+        'state': fields.selection([('draft','Open'), ('done','Closed')], 'State', readonly=True,
+                                  help='When monthly periods are created. The state is \'Draft\'. At the end of monthly period it is in \'Done\' state.'),
+        'company_id': fields.related('fiscalyear_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True)
+    }
+    _defaults = {
+        'state': 'draft',
+    }
+    _order = "date_start, special desc"
+    _sql_constraints = [
+        ('name_company_uniq', 'unique(name, company_id)', 'The name of the period must be unique per company!'),
+    ]
+
+    def _check_duration(self,cr,uid,ids,context=None):
+        obj_period = self.browse(cr, uid, ids[0], context=context)
+        if obj_period.date_stop < obj_period.date_start:
+            return False
+        return True
+
+    def _check_year_limit(self,cr,uid,ids,context=None):
+        for obj_period in self.browse(cr, uid, ids, context=context):
+            if obj_period.special:
+                continue
+
+            if obj_period.fiscalyear_id.date_stop < obj_period.date_stop or \
+               obj_period.fiscalyear_id.date_stop < obj_period.date_start or \
+               obj_period.fiscalyear_id.date_start > obj_period.date_start or \
+               obj_period.fiscalyear_id.date_start > obj_period.date_stop:
+                return False
+
+            pids = self.search(cr, uid, [('date_stop','>=',obj_period.date_start),('date_start','<=',obj_period.date_stop),('special','=',False),('id','<>',obj_period.id)])
+            for period in self.browse(cr, uid, pids):
+                if period.fiscalyear_id.company_id.id==obj_period.fiscalyear_id.company_id.id:
+                    return False
+        return True
+
+    _constraints = [
+        (_check_duration, 'Error ! The duration of the Period(s) is/are invalid. ', ['date_stop']),
+        (_check_year_limit, 'Invalid period ! Some periods overlap or the date period is not in the scope of the fiscal year. ', ['date_stop'])
+    ]
+
+    def next(self, cr, uid, period, step, context=None):
+        ids = self.search(cr, uid, [('date_start','>',period.date_start)])
+        if len(ids)>=step:
+            return ids[step-1]
+        return False
+
+    def find(self, cr, uid, dt=None, context=None):
+        if context is None: context = {}
+        if not dt:
+            dt = fields.date.context_today(self,cr,uid,context=context)
+#CHECKME: shouldn't we check the state of the period?
+        args = [('date_start', '<=' ,dt), ('date_stop', '>=', dt)]
+        if context.get('company_id', False):
+            args.append(('company_id', '=', context['company_id']))
+        else:
+            company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
+            args.append(('company_id', '=', company_id))
+        result = []
+        if context.get('account_period_prefer_normal'):
+            # look for non-special periods first, and fallback to all if no result is found
+            result = self.search(cr, uid, args + [('special', '=', False)], context=context)
+        if not result:
+            result = self.search(cr, uid, args, context=context)
+        if not result:
+            raise osv.except_osv(_('Error !'), _('No period defined for this date: %s !\nPlease create one.')%dt)
+        return result
+
+    def action_draft(self, cr, uid, ids, *args):
+        mode = 'draft'
+        for period in self.browse(cr, uid, ids):
+            if period.fiscalyear_id.state == 'done':
+                raise osv.except_osv(_('Warning !'), _('You can not re-open a period which belongs to closed fiscal year'))
+        cr.execute('update account_journal_period set state=%s where period_id in %s', (mode, tuple(ids),))
+        cr.execute('update account_period set state=%s where id in %s', (mode, tuple(ids),))
+        return True
+
+    def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=100):
+        if args is None:
+            args = []
+        if context is None:
+            context = {}
+        ids = []
+        if name:
+            ids = self.search(cr, user, [('code','ilike',name)]+ args, limit=limit)
+        if not ids:
+            ids = self.search(cr, user, [('name',operator,name)]+ args, limit=limit)
+        return self.name_get(cr, user, ids, context=context)
+
+    def write(self, cr, uid, ids, vals, context=None):
+        if 'company_id' in vals:
+            move_lines = self.pool.get('account.move.line').search(cr, uid, [('period_id', 'in', ids)])
+            if move_lines:
+                raise osv.except_osv(_('Warning !'), _('You can not modify company of this period as some journal items exists.'))
+        return super(account_period, self).write(cr, uid, ids, vals, context=context)
+
+    def build_ctx_periods(self, cr, uid, period_from_id, period_to_id):
+        if period_from_id == period_to_id:
+            return [period_from_id]
+        period_from = self.browse(cr, uid, period_from_id)
+        period_date_start = period_from.date_start
+        company1_id = period_from.company_id.id
+        period_to = self.browse(cr, uid, period_to_id)
+        period_date_stop = period_to.date_stop
+        company2_id = period_to.company_id.id
+        if company1_id != company2_id:
+            raise osv.except_osv(_('Error'), _('You should have chosen periods that belongs to the same company'))
+        if period_date_start > period_date_stop:
+            raise osv.except_osv(_('Error'), _('Start period should be smaller then End period'))
+        #for period from = january, we want to exclude the opening period (but it has same date_from, so we have to check if period_from is special or not to include that clause or not in the search).
+        if period_from.special:
+            return self.search(cr, uid, [('date_start', '>=', period_date_start), ('date_stop', '<=', period_date_stop), ('company_id', '=', company1_id)])
+        return self.search(cr, uid, [('date_start', '>=', period_date_start), ('date_stop', '<=', period_date_stop), ('company_id', '=', company1_id), ('special', '=', False)])
+
+account_period()
+
+class account_journal_period(osv.osv):
+    _name = "account.journal.period"
+    _description = "Journal Period"
+
+    def _icon_get(self, cr, uid, ids, field_name, arg=None, context=None):
+        result = {}.fromkeys(ids, 'STOCK_NEW')
+        for r in self.read(cr, uid, ids, ['state']):
+            result[r['id']] = {
+                'draft': 'STOCK_NEW',
+                'printed': 'STOCK_PRINT_PREVIEW',
+                'done': 'STOCK_DIALOG_AUTHENTICATION',
+            }.get(r['state'], 'STOCK_NEW')
+        return result
+
+    _columns = {
+        'name': fields.char('Journal-Period Name', size=64, required=True),
+        'journal_id': fields.many2one('account.journal', 'Journal', required=True, ondelete="cascade"),
+        'period_id': fields.many2one('account.period', 'Period', required=True, ondelete="cascade"),
+        'icon': fields.function(_icon_get, string='Icon', type='char', size=32),
+        'active': fields.boolean('Active', required=True, help="If the active field is set to False, it will allow you to hide the journal period without removing it."),
+        'state': fields.selection([('draft','Draft'), ('printed','Printed'), ('done','Done')], 'State', required=True, readonly=True,
+                                  help='When journal period is created. The state is \'Draft\'. If a report is printed it comes to \'Printed\' state. When all transactions are done, it comes in \'Done\' state.'),
+        'fiscalyear_id': fields.related('period_id', 'fiscalyear_id', string='Fiscal Year', type='many2one', relation='account.fiscalyear'),
+        'company_id': fields.related('journal_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True)
+    }
+
+    def _check(self, cr, uid, ids, context=None):
+        for obj in self.browse(cr, uid, ids, context=context):
+            cr.execute('select * from account_move_line where journal_id=%s and period_id=%s limit 1', (obj.journal_id.id, obj.period_id.id))
+            res = cr.fetchall()
+            if res:
+                raise osv.except_osv(_('Error !'), _('You can not modify/delete a journal with entries for this period !'))
+        return True
+
+    def write(self, cr, uid, ids, vals, context=None):
+        self._check(cr, uid, ids, context=context)
+        return super(account_journal_period, self).write(cr, uid, ids, vals, context=context)
+
+    def create(self, cr, uid, vals, context=None):
+        period_id = vals.get('period_id',False)
+        if period_id:
+            period = self.pool.get('account.period').browse(cr, uid, period_id, context=context)
+            vals['state']=period.state
+        return super(account_journal_period, self).create(cr, uid, vals, context)
+
+    def unlink(self, cr, uid, ids, context=None):
+        self._check(cr, uid, ids, context=context)
+        return super(account_journal_period, self).unlink(cr, uid, ids, context=context)
+
+    _defaults = {
+        'state': 'draft',
+        'active': True,
+    }
+    _order = "period_id"
+
+account_journal_period()
+
+class account_fiscalyear(osv.osv):
+    _inherit = "account.fiscalyear"
+    _description = "Fiscal Year"
+    _columns = {
+        'end_journal_period_id':fields.many2one('account.journal.period','End of Year Entries Journal', readonly=True),
+    }
+
+    def copy(self, cr, uid, id, default={}, context=None):
+        default.update({
+            'period_ids': [],
+            'end_journal_period_id': False
+        })
+        return super(account_fiscalyear, self).copy(cr, uid, id, default=default, context=context)
+
+account_fiscalyear()
+#----------------------------------------------------------
+# Entries
+#----------------------------------------------------------
+class account_move(osv.osv):
+    _name = "account.move"
+    _description = "Account Entry"
+    _order = 'id desc'
+
+    def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=80):
+        """
+        Returns a list of tupples containing id, name, as internally it is called {def name_get}
+        result format: {[(id, name), (id, name), ...]}
+
+        @param cr: A database cursor
+        @param user: ID of the user currently logged in
+        @param name: name to search
+        @param args: other arguments
+        @param operator: default operator is 'ilike', it can be changed
+        @param context: context arguments, like lang, time zone
+        @param limit: Returns first 'n' ids of complete result, default is 80.
+
+        @return: Returns a list of tuples containing id and name
+        """
+
+        if not args:
+          args = []
+        ids = []
+        if name:
+            ids += self.search(cr, user, [('name','ilike',name)]+args, limit=limit, context=context)
+
+        if not ids and name and type(name) == int:
+            ids += self.search(cr, user, [('id','=',name)]+args, limit=limit, context=context)
+
+        if not ids:
+            ids += self.search(cr, user, args, limit=limit, context=context)
+
+        return self.name_get(cr, user, ids, context=context)
+
+    def name_get(self, cursor, user, ids, context=None):
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        if not ids:
+            return []
+        res = []
+        data_move = self.pool.get('account.move').browse(cursor, user, ids, context=context)
+        for move in data_move:
+            if move.state=='draft':
+                name = '*' + str(move.id)
+            else:
+                name = move.name
+            res.append((move.id, name))
+        return res
+
+    def _get_period(self, cr, uid, context=None):
+        ctx = dict(context or {}, account_period_prefer_normal=True)
+        period_ids = self.pool.get('account.period').find(cr, uid, context=ctx)
+        return period_ids[0]
+
+    def _amount_compute(self, cr, uid, ids, name, args, context, where =''):
+        if not ids: return {}
+        cr.execute( 'SELECT move_id, SUM(debit) '\
+                    'FROM account_move_line '\
+                    'WHERE move_id IN %s '\
+                    'GROUP BY move_id', (tuple(ids),))
+        result = dict(cr.fetchall())
+        for id in ids:
+            result.setdefault(id, 0.0)
+        return result
+
+    def _search_amount(self, cr, uid, obj, name, args, context):
+        ids = set()
+        for cond in args:
+            amount = cond[2]
+            if isinstance(cond[2],(list,tuple)):
+                if cond[1] in ['in','not in']:
+                    amount = tuple(cond[2])
+                else:
+                    continue
+            else:
+                if cond[1] in ['=like', 'like', 'not like', 'ilike', 'not ilike', 'in', 'not in', 'child_of']:
+                    continue
+
+            cr.execute("select move_id from account_move_line group by move_id having sum(debit) %s %%s" % (cond[1]),(amount,))
+            res_ids = set(id[0] for id in cr.fetchall())
+            ids = ids and (ids & res_ids) or res_ids
+        if ids:
+            return [('id', 'in', tuple(ids))]
+        return [('id', '=', '0')]
+
+    _columns = {
+        'name': fields.char('Number', size=64, required=True),
+        'ref': fields.char('Reference', size=64),
+        'period_id': fields.many2one('account.period', 'Period', required=True, states={'posted':[('readonly',True)]}),
+        'journal_id': fields.many2one('account.journal', 'Journal', required=True, states={'posted':[('readonly',True)]}),
+        'state': fields.selection([('draft','Unposted'), ('posted','Posted')], 'State', required=True, readonly=True,
+            help='All manually created new journal entries are usually in the state \'Unposted\', but you can set the option to skip that state on the related journal. In that case, they will be behave as journal entries automatically created by the system on document validation (invoices, bank statements...) and will be created in \'Posted\' state.'),
+        'line_id': fields.one2many('account.move.line', 'move_id', 'Entries', states={'posted':[('readonly',True)]}),
+        'to_check': fields.boolean('To Review', help='Check this box if you are unsure of that journal entry and if you want to note it as \'to be reviewed\' by an accounting expert.'),
+        'partner_id': fields.related('line_id', 'partner_id', type="many2one", relation="res.partner", string="Partner", store=True),
+        'amount': fields.function(_amount_compute, string='Amount', digits_compute=dp.get_precision('Account'), type='float', fnct_search=_search_amount),
+        'date': fields.date('Date', required=True, states={'posted':[('readonly',True)]}, select=True),
+        'narration':fields.text('Internal Note'),
+        'company_id': fields.related('journal_id','company_id',type='many2one',relation='res.company',string='Company', store=True, readonly=True),
+        'balance': fields.float('balance', digits_compute=dp.get_precision('Account'), help="This is a field only used for internal purpose and shouldn't be displayed"),
+    }
+
+    _defaults = {
+        'name': '/',
+        'state': 'draft',
+        'period_id': _get_period,
+        'date': fields.date.context_today,
+        'company_id': lambda self, cr, uid, c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.id,
+    }
+
+    def _check_centralisation(self, cursor, user, ids, context=None):
+        for move in self.browse(cursor, user, ids, context=context):
+            if move.journal_id.centralisation:
+                move_ids = self.search(cursor, user, [
+                    ('period_id', '=', move.period_id.id),
+                    ('journal_id', '=', move.journal_id.id),
+                    ])
+                if len(move_ids) > 1:
+                    return False
+        return True
+
+    _constraints = [
+        (_check_centralisation,
+            'You can not create more than one move per period on centralized journal',
+            ['journal_id']),
+    ]
+
+    def post(self, cr, uid, ids, context=None):
+        if context is None:
+            context = {}
+        invoice = context.get('invoice', False)
+        valid_moves = self.validate(cr, uid, ids, context)
+
+        if not valid_moves:
+            raise osv.except_osv(_('Integrity Error !'), _('You can not validate a non-balanced entry !\nMake sure you have configured payment terms properly !\nThe latest payment term line should be of the type "Balance" !'))
+        obj_sequence = self.pool.get('ir.sequence')
+        for move in self.browse(cr, uid, valid_moves, context=context):
+            if move.name =='/':
+                new_name = False
+                journal = move.journal_id
+
+                if invoice and invoice.internal_number:
+                    new_name = invoice.internal_number
+                else:
+                    if journal.sequence_id:
+                        c = {'fiscalyear_id': move.period_id.fiscalyear_id.id}
+                        new_name = obj_sequence.next_by_id(cr, uid, journal.sequence_id.id, c)
+                    else:
+                        raise osv.except_osv(_('Error'), _('No sequence defined on the journal !'))
+
+                if new_name:
+                    self.write(cr, uid, [move.id], {'name':new_name})
+
+        cr.execute('UPDATE account_move '\
+                   'SET state=%s '\
+                   'WHERE id IN %s',
+                   ('posted', tuple(valid_moves),))
+        return True
+
+    def button_validate(self, cursor, user, ids, context=None):
+        for move in self.browse(cursor, user, ids, context=context):
+            # check that all accounts have the same topmost ancestor
+            top_common = None
+            for line in move.line_id:
+                account = line.account_id
+                top_account = account
+                while top_account.parent_id:
+                    top_account = top_account.parent_id
+                if not top_common:
+                    top_common = top_account
+                elif top_account.id != top_common.id:
+                    raise osv.except_osv(_('Error !'),
+                                         _('You cannot validate this journal entry because account "%s" does not belong to chart of accounts "%s"!') % (account.name, top_common.name))
+        return self.post(cursor, user, ids, context=context)
+
+    def button_cancel(self, cr, uid, ids, context=None):
+        for line in self.browse(cr, uid, ids, context=context):
+            if line.period_id.state == 'done':
+                raise osv.except_osv(_('Error !'), _('You can not modify a posted entry of closed periods'))
+            elif not line.journal_id.update_posted:
+                raise osv.except_osv(_('Error !'), _('You can not modify a posted entry of this journal !\nYou should set the journal to allow cancelling entries if you want to do that.'))
+        if ids:
+            cr.execute('UPDATE account_move '\
+                       'SET state=%s '\
+                       'WHERE id IN %s', ('draft', tuple(ids),))
+        return True
+
+    def onchange_line_id(self, cr, uid, ids, line_ids, context=None):
+        balance = 0.0
+        line_ids = [ line for line in line_ids if not (isinstance(line, (tuple, list)) and line and line[0] == 2) ]
+        line_ids = self.resolve_o2m_commands_to_record_dicts(cr, uid, 'line_id', line_ids, context=context)
+        for line in line_ids:
+            balance += (line['debit'] or 0.00)- (line['credit'] or 0.00)
+        return {'value': {'balance': balance}}
+
+    def write(self, cr, uid, ids, vals, context=None):
+        if context is None:
+            context = {}
+        c = context.copy()
+        c['novalidate'] = True
+        result = super(account_move, self).write(cr, uid, ids, vals, c)
+        self.validate(cr, uid, ids, context=context)
+        return result
+
+    #
+    # TODO: Check if period is closed !
+    #
+    def create(self, cr, uid, vals, context=None):
+        if context is None:
+            context = {}
+        if 'line_id' in vals and context.get('copy'):
+            for l in vals['line_id']:
+                if not l[0]:
+                    l[2].update({
+                        'reconcile_id':False,
+                        'reconcil_partial_id':False,
+                        'analytic_lines':False,
+                        'invoice':False,
+                        'ref':False,
+                        'balance':False,
+                        'account_tax_id':False,
+                    })
+
+            if 'journal_id' in vals and vals.get('journal_id', False):
+                for l in vals['line_id']:
+                    if not l[0]:
+                        l[2]['journal_id'] = vals['journal_id']
+                context['journal_id'] = vals['journal_id']
+            if 'period_id' in vals:
+                for l in vals['line_id']:
+                    if not l[0]:
+                        l[2]['period_id'] = vals['period_id']
+                context['period_id'] = vals['period_id']
+            else:
+                default_period = self._get_period(cr, uid, context)
+                for l in vals['line_id']:
+                    if not l[0]:
+                        l[2]['period_id'] = default_period
+                context['period_id'] = default_period
+
+        if 'line_id' in vals:
+            c = context.copy()
+            c['novalidate'] = True
+            result = super(account_move, self).create(cr, uid, vals, c)
+            self.validate(cr, uid, [result], context)
+        else:
+            result = super(account_move, self).create(cr, uid, vals, context)
+        return result
+
+    def copy(self, cr, uid, id, default={}, context=None):
+        if context is None:
+            context = {}
+        default.update({
+            'state':'draft',
+            'name':'/',
+        })
+        context.update({
+            'copy':True
+        })
+        return super(account_move, self).copy(cr, uid, id, default, context)
+
+    def unlink(self, cr, uid, ids, context=None, check=True):
+        if context is None:
+            context = {}
+        toremove = []
+        obj_move_line = self.pool.get('account.move.line')
+        for move in self.browse(cr, uid, ids, context=context):
+            if move['state'] != 'draft':
+                raise osv.except_osv(_('UserError'),
+                        _('You can not delete a posted journal entry "%s"!') % \
+                                move['name'])
+            line_ids = map(lambda x: x.id, move.line_id)
+            context['journal_id'] = move.journal_id.id
+            context['period_id'] = move.period_id.id
+            obj_move_line._update_check(cr, uid, line_ids, context)
+            obj_move_line.unlink(cr, uid, line_ids, context=context)
+            toremove.append(move.id)
+        result = super(account_move, self).unlink(cr, uid, toremove, context)
+        return result
+
+    def _compute_balance(self, cr, uid, id, context=None):
+        move = self.browse(cr, uid, id, context=context)
+        amount = 0
+        for line in move.line_id:
+            amount+= (line.debit - line.credit)
+        return amount
+
+    def _centralise(self, cr, uid, move, mode, context=None):
+        assert mode in ('debit', 'credit'), 'Invalid Mode' #to prevent sql injection
+        currency_obj = self.pool.get('res.currency')
+        if context is None:
+            context = {}
+
+        if mode=='credit':
+            account_id = move.journal_id.default_debit_account_id.id
+            mode2 = 'debit'
+            if not account_id:
+                raise osv.except_osv(_('UserError'),
+                        _('There is no default default debit account defined \n' \
+                                'on journal "%s"') % move.journal_id.name)
+        else:
+            account_id = move.journal_id.default_credit_account_id.id
+            mode2 = 'credit'
+            if not account_id:
+                raise osv.except_osv(_('UserError'),
+                        _('There is no default default credit account defined \n' \
+                                'on journal "%s"') % move.journal_id.name)
+
+        # find the first line of this move with the current mode
+        # or create it if it doesn't exist
+        cr.execute('select id from account_move_line where move_id=%s and centralisation=%s limit 1', (move.id, mode))
+        res = cr.fetchone()
+        if res:
+            line_id = res[0]
+        else:
+            context.update({'journal_id': move.journal_id.id, 'period_id': move.period_id.id})
+            line_id = self.pool.get('account.move.line').create(cr, uid, {
+                'name': _(mode.capitalize()+' Centralisation'),
+                'centralisation': mode,
+                'account_id': account_id,
+                'move_id': move.id,
+                'journal_id': move.journal_id.id,
+                'period_id': move.period_id.id,
+                'date': move.period_id.date_stop,
+                'debit': 0.0,
+                'credit': 0.0,
+            }, context)
+
+        # find the first line of this move with the other mode
+        # so that we can exclude it from our calculation
+        cr.execute('select id from account_move_line where move_id=%s and centralisation=%s limit 1', (move.id, mode2))
+        res = cr.fetchone()
+        if res:
+            line_id2 = res[0]
+        else:
+            line_id2 = 0
+
+        cr.execute('SELECT SUM(%s) FROM account_move_line WHERE move_id=%%s AND id!=%%s' % (mode,), (move.id, line_id2))
+        result = cr.fetchone()[0] or 0.0
+        cr.execute('update account_move_line set '+mode2+'=%s where id=%s', (result, line_id))
+
+        #adjust also the amount in currency if needed
+        cr.execute("select currency_id, sum(amount_currency) as amount_currency from account_move_line where move_id = %s and currency_id is not null group by currency_id", (move.id,))
+        for row in cr.dictfetchall():
+            currency_id = currency_obj.browse(cr, uid, row['currency_id'], context=context)
+            if not currency_obj.is_zero(cr, uid, currency_id, row['amount_currency']):
+                amount_currency = row['amount_currency'] * -1
+                account_id = amount_currency > 0 and move.journal_id.default_debit_account_id.id or move.journal_id.default_credit_account_id.id
+                cr.execute('select id from account_move_line where move_id=%s and centralisation=\'currency\' and currency_id = %slimit 1', (move.id, row['currency_id']))
+                res = cr.fetchone()
+                if res:
+                    cr.execute('update account_move_line set amount_currency=%s , account_id=%s where id=%s', (amount_currency, account_id, res[0]))
+                else:
+                    context.update({'journal_id': move.journal_id.id, 'period_id': move.period_id.id})
+                    line_id = self.pool.get('account.move.line').create(cr, uid, {
+                        'name': _('Currency Adjustment'),
+                        'centralisation': 'currency',
+                        'account_id': account_id,
+                        'move_id': move.id,
+                        'journal_id': move.journal_id.id,
+                        'period_id': move.period_id.id,
+                        'date': move.period_id.date_stop,
+                        'debit': 0.0,
+                        'credit': 0.0,
+                        'currency_id': row['currency_id'],
+                        'amount_currency': amount_currency,
+                    }, context)
+
+        return True
+
+    #
+    # Validate a balanced move. If it is a centralised journal, create a move.
+    #
+    def validate(self, cr, uid, ids, context=None):
+        if context and ('__last_update' in context):
+            del context['__last_update']
+
+        valid_moves = [] #Maintains a list of moves which can be responsible to create analytic entries
+        obj_analytic_line = self.pool.get('account.analytic.line')
+        obj_move_line = self.pool.get('account.move.line')
+        for move in self.browse(cr, uid, ids, context):
+            # Unlink old analytic lines on move_lines
+            for obj_line in move.line_id:
+                for obj in obj_line.analytic_lines:
+                    obj_analytic_line.unlink(cr,uid,obj.id)
+
+            journal = move.journal_id
+            amount = 0
+            line_ids = []
+            line_draft_ids = []
+            company_id = None
+            for line in move.line_id:
+                amount += line.debit - line.credit
+                line_ids.append(line.id)
+                if line.state=='draft':
+                    line_draft_ids.append(line.id)
+
+                if not company_id:
+                    company_id = line.account_id.company_id.id
+                if not company_id == line.account_id.company_id.id:
+                    raise osv.except_osv(_('Error'), _("Couldn't create move between different companies"))
+
+                if line.account_id.currency_id and line.currency_id:
+                    if line.account_id.currency_id.id != line.currency_id.id and (line.account_id.currency_id.id != line.account_id.company_id.currency_id.id):
+                        raise osv.except_osv(_('Error'), _("""Couldn't create move with currency different from the secondary currency of the account "%s - %s". Clear the secondary currency field of the account definition if you want to accept all currencies.""") % (line.account_id.code, line.account_id.name))
+
+            if abs(amount) < 10 ** -4:
+                # If the move is balanced
+                # Add to the list of valid moves
+                # (analytic lines will be created later for valid moves)
+                valid_moves.append(move)
+
+                # Check whether the move lines are confirmed
+
+                if not line_draft_ids:
+                    continue
+                # Update the move lines (set them as valid)
+
+                obj_move_line.write(cr, uid, line_draft_ids, {
+                    'state': 'valid'
+                }, context, check=False)
+
+                account = {}
+                account2 = {}
+
+                if journal.type in ('purchase','sale'):
+                    for line in move.line_id:
+                        code = amount = 0
+                        key = (line.account_id.id, line.tax_code_id.id)
+                        if key in account2:
+                            code = account2[key][0]
+                            amount = account2[key][1] * (line.debit + line.credit)
+                        elif line.account_id.id in account:
+                            code = account[line.account_id.id][0]
+                            amount = account[line.account_id.id][1] * (line.debit + line.credit)
+                        if (code or amount) and not (line.tax_code_id or line.tax_amount):
+                            obj_move_line.write(cr, uid, [line.id], {
+                                'tax_code_id': code,
+                                'tax_amount': amount
+                            }, context, check=False)
+            elif journal.centralisation:
+                # If the move is not balanced, it must be centralised...
+
+                # Add to the list of valid moves
+                # (analytic lines will be created later for valid moves)
+                valid_moves.append(move)
+
+                #
+                # Update the move lines (set them as valid)
+                #
+                self._centralise(cr, uid, move, 'debit', context=context)
+                self._centralise(cr, uid, move, 'credit', context=context)
+                obj_move_line.write(cr, uid, line_draft_ids, {
+                    'state': 'valid'
+                }, context, check=False)
+            else:
+                # We can't validate it (it's unbalanced)
+                # Setting the lines as draft
+                obj_move_line.write(cr, uid, line_ids, {
+                    'state': 'draft'
+                }, context, check=False)
+        # Create analytic lines for the valid moves
+        for record in valid_moves:
+            obj_move_line.create_analytic_lines(cr, uid, [line.id for line in record.line_id], context)
+
+        valid_moves = [move.id for move in valid_moves]
+        return len(valid_moves) > 0 and valid_moves or False
+
+account_move()
+
+class account_move_reconcile(osv.osv):
+    _name = "account.move.reconcile"
+    _description = "Account Reconciliation"
+    _columns = {
+        'name': fields.char('Name', size=64, required=True),
+        'type': fields.char('Type', size=16, required=True),
+        'line_id': fields.one2many('account.move.line', 'reconcile_id', 'Entry Lines'),
+        'line_partial_ids': fields.one2many('account.move.line', 'reconcile_partial_id', 'Partial Entry lines'),
+        'create_date': fields.date('Creation date', readonly=True),
+    }
+    _defaults = {
+        'name': lambda self,cr,uid,ctx=None: self.pool.get('ir.sequence').get(cr, uid, 'account.reconcile', context=ctx) or '/',
+    }
+
+    def reconcile_partial_check(self, cr, uid, ids, type='auto', context=None):
+        total = 0.0
+        for rec in self.browse(cr, uid, ids, context=context):
+            for line in rec.line_partial_ids:
+                if line.account_id.currency_id:
+                    total += line.amount_currency
+                else:
+                    total += (line.debit or 0.0) - (line.credit or 0.0)
+        if not total:
+            self.pool.get('account.move.line').write(cr, uid,
+                map(lambda x: x.id, rec.line_partial_ids),
+                {'reconcile_id': rec.id }
+            )
+        return True
+
+    def name_get(self, cr, uid, ids, context=None):
+        if not ids:
+            return []
+        result = []
+        for r in self.browse(cr, uid, ids, context=context):
+            total = reduce(lambda y,t: (t.debit or 0.0) - (t.credit or 0.0) + y, r.line_partial_ids, 0.0)
+            if total:
+                name = '%s (%.2f)' % (r.name, total)
+                result.append((r.id,name))
+            else:
+                result.append((r.id,r.name))
+        return result
+
+account_move_reconcile()
+
+#----------------------------------------------------------
+# Tax
+#----------------------------------------------------------
+"""
+a documenter
+child_depend: la taxe depend des taxes filles
+"""
+class account_tax_code(osv.osv):
+    """
+    A code for the tax object.
+
+    This code is used for some tax declarations.
+    """
+    def _sum(self, cr, uid, ids, names, args, context, where ='', where_params=()):
+        parent_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)]))
+        if context.get('based_on', 'invoices') == 'payments':
+            cr.execute('SELECT line.tax_code_id, sum(line.tax_amount), \
+                    sum(line.credit), sum(line.debit) \
+                    FROM account_move_line AS line, \
+                        account_move AS move \
+                        LEFT JOIN account_invoice invoice ON \
+                            (invoice.move_id = move.id) \
+                    WHERE line.tax_code_id IN %s '+where+' \
+                        AND move.id = line.move_id \
+                        AND ((invoice.state = \'paid\') \
+                            OR (invoice.id IS NULL)) \
+                            GROUP BY line.tax_code_id',
+                                (parent_ids,) + where_params)
+        else:
+            cr.execute('SELECT line.tax_code_id, sum(line.tax_amount), \
+                    sum(line.credit), sum(line.debit) \
+                    FROM account_move_line AS line, \
+                    account_move AS move \
+                    WHERE line.tax_code_id IN %s '+where+' \
+                    AND move.id = line.move_id \
+                    GROUP BY line.tax_code_id',
+                       (parent_ids,) + where_params)
+        res = dict((tax_code, [amount, credit, debit]) for tax_code, amount, credit, debit in cr.fetchall())
+        obj_precision = self.pool.get('decimal.precision')
+        res2 = {}
+        for record in self.browse(cr, uid, ids, context=context):
+            def _rec_get(record):
+                amount = res.get(record.id, [0.0, 0.0, 0.0])
+                for rec in record.child_ids:
+                    subamounts = _rec_get(rec)
+                    amount[0] += subamounts[0] * rec.sign
+                    amount[1] += subamounts[1] * rec.sign
+                    amount[2] += subamounts[2] * rec.sign
+                return amount
+            res2[record.id] = {}
+            records = _rec_get(record)
+            for name in names:
+                if name in ('sum', 'sum_period'):
+                    res2[record.id][name] = round(records[0], obj_precision.precision_get(cr, uid, 'Account'))
+                if name in ('sum_credit','sum_period_credit'):
+                    res2[record.id][name] = round(records[1], obj_precision.precision_get(cr, uid, 'Account'))
+                if name in ('sum_debit','sum_period_debit'):
+                    res2[record.id][name] = round(records[2], obj_precision.precision_get(cr, uid, 'Account'))
+        return res2
+
+    def _sum_year(self, cr, uid, ids, names, args, context=None):
+        if context is None:
+            context = {}
+        move_state = ('posted', )
+        if context.get('state', 'all') == 'all':
+            move_state = ('draft', 'posted', )
+        if context.get('fiscalyear_id', False):
+            fiscalyear_id = [context['fiscalyear_id']]
+        else:
+            fiscalyear_id = self.pool.get('account.fiscalyear').finds(cr, uid, exception=False)
+        where = ''
+        where_params = ()
+        if fiscalyear_id:
+            pids = []
+            for fy in fiscalyear_id:
+                pids += map(lambda x: str(x.id), self.pool.get('account.fiscalyear').browse(cr, uid, fy).period_ids)
+            if pids:
+                where = ' AND line.period_id IN %s AND move.state IN %s '
+                where_params = (tuple(pids), move_state)
+        return self._sum(cr, uid, ids, names, args, context,
+                where=where, where_params=where_params)
+
+    def _sum_period(self, cr, uid, ids, names, args, context):
+        if context is None:
+            context = {}
+        move_state = ('posted', )
+        if context.get('state', False) == 'all':
+            move_state = ('draft', 'posted', )
+        if context.get('period_id', False):
+            period_id = context['period_id']
+        else:
+            period_id = self.pool.get('account.period').find(cr, uid)
+            if not period_id:
+                return dict.fromkeys(ids, 0.0)
+            period_id = period_id[0]
+        return self._sum(cr, uid, ids, names, args, context,
+                where=' AND line.period_id=%s AND move.state IN %s', where_params=(period_id, move_state))
+
+    _name = 'account.tax.code'
+    _description = 'Tax Code'
+    _rec_name = 'code'
+    _columns = {
+        'name': fields.char('Tax Case Name', size=64, required=True, translate=True),
+        'code': fields.char('Case Code', size=64),
+        'info': fields.text('Description'),
+        'sum': fields.function(_sum_year, string="Year Sum", multi='_sum_year'),
+        'sum_credit': fields.function(_sum_year, string="Year Sum (Debit)", multi='_sum_year'),
+        'sum_debit': fields.function(_sum_year, string="Year Sum (Credit)", multi='_sum_year'),
+        'sum_period': fields.function(_sum_period, string="Period Sum", multi='_sum_period'),
+        'sum_period_credit': fields.function(_sum_period, string="Period Sum (Credit)", multi='_sum_period'),
+        'sum_period_debit': fields.function(_sum_period, string="Period Sum (Debit)", multi='_sum_period'),
+        'parent_id': fields.many2one('account.tax.code', 'Parent Code', select=True),
+        'child_ids': fields.one2many('account.tax.code', 'parent_id', 'Child Codes'),
+        'line_ids': fields.one2many('account.move.line', 'tax_code_id', 'Lines'),
+        'company_id': fields.many2one('res.company', 'Company', required=True),
+        'sign': fields.float('Coefficent for parent', required=True, help='You can specify here the coefficient that will be used when consolidating the amount of this case into its parent. For example, set 1/-1 if you want to add/substract it.'),
+        'notprintable':fields.boolean("Not Printable in Invoice", help="Check this box if you don't want any VAT related to this Tax Code to appear on invoices"),
+        'sequence': fields.integer('Sequence', help="Determine the display order in the report 'Accounting \ Reporting \ Generic Reporting \ Taxes \ Taxes Report'"),
+    }
+
+    def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=80):
+        if not args:
+            args = []
+        if context is None:
+            context = {}
+        ids = self.search(cr, user, ['|',('name',operator,name),('code',operator,name)] + args, limit=limit, context=context)
+        return self.name_get(cr, user, ids, context)
+
+    def name_get(self, cr, uid, ids, context=None):
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        if not ids:
+            return []
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        reads = self.read(cr, uid, ids, ['name','code'], context, load='_classic_write')
+        return [(x['id'], (x['code'] and (x['code'] + ' - ') or '') + x['name']) \
+                for x in reads]
+
+    def _default_company(self, cr, uid, context=None):
+        user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
+        if user.company_id:
+            return user.company_id.id
+        return self.pool.get('res.company').search(cr, uid, [('parent_id', '=', False)])[0]
+    _defaults = {
+        'company_id': _default_company,
+        'sign': 1.0,
+        'notprintable': False,
+    }
+
+    def copy(self, cr, uid, id, default=None, context=None):
+        if default is None:
+            default = {}
+        default = default.copy()
+        default.update({'line_ids': []})
+        return super(account_tax_code, self).copy(cr, uid, id, default, context)
+
+    _check_recursion = check_cycle
+    _constraints = [
+        (_check_recursion, 'Error ! You can not create recursive accounts.', ['parent_id'])
+    ]
+    _order = 'code'
+
+account_tax_code()
+
+class account_tax(osv.osv):
+    """
+    A tax object.
+
+    Type: percent, fixed, none, code
+        PERCENT: tax = price * amount
+        FIXED: tax = price + amount
+        NONE: no tax line
+        CODE: execute python code. localcontext = {'price_unit':pu, 'address':address_object}
+            return result in the context
+            Ex: result=round(price_unit*0.21,4)
+    """
+
+    def get_precision_tax():
+        def change_digit_tax(cr):
+            res = pooler.get_pool(cr.dbname).get('decimal.precision').precision_get(cr, 1, 'Account')
+            return (16, res+2)
+        return change_digit_tax
+
+    _name = 'account.tax'
+    _description = 'Tax'
+    _columns = {
+        'name': fields.char('Tax Name', size=64, required=True, translate=True, help="This name will be displayed on reports"),
+        'sequence': fields.integer('Sequence', required=True, help="The sequence field is used to order the tax lines from the lowest sequences to the higher ones. The order is important if you have a tax with several tax children. In this case, the evaluation order is important."),
+        'amount': fields.float('Amount', required=True, digits_compute=get_precision_tax(), help="For taxes of type percentage, enter % ratio between 0-1."),
+        'active': fields.boolean('Active', help="If the active field is set to False, it will allow you to hide the tax without removing it."),
+        'type': fields.selection( [('percent','Percentage'), ('fixed','Fixed Amount'), ('none','None'), ('code','Python Code'), ('balance','Balance')], 'Tax Type', required=True,
+            help="The computation method for the tax amount."),
+        'applicable_type': fields.selection( [('true','Always'), ('code','Given by Python Code')], 'Applicability', required=True,
+            help="If not applicable (computed through a Python code), the tax won't appear on the invoice."),
+        'domain':fields.char('Domain', size=32, help="This field is only used if you develop your own module allowing developers to create specific taxes in a custom domain."),
+        'account_collected_id':fields.many2one('account.account', 'Invoice Tax Account'),
+        'account_paid_id':fields.many2one('account.account', 'Refund Tax Account'),
+        'parent_id':fields.many2one('account.tax', 'Parent Tax Account', select=True),
+        'child_ids':fields.one2many('account.tax', 'parent_id', 'Child Tax Accounts'),
+        'child_depend':fields.boolean('Tax on Children', help="Set if the tax computation is based on the computation of child taxes rather than on the total amount."),
+        'python_compute':fields.text('Python Code'),
+        'python_compute_inv':fields.text('Python Code (reverse)'),
+        'python_applicable':fields.text('Python Code'),
+
+        #
+        # Fields used for the VAT declaration
+        #
+        'base_code_id': fields.many2one('account.tax.code', 'Account Base Code', help="Use this code for the VAT declaration."),
+        'tax_code_id': fields.many2one('account.tax.code', 'Account Tax Code', help="Use this code for the VAT declaration."),
+        'base_sign': fields.float('Base Code Sign', help="Usually 1 or -1."),
+        'tax_sign': fields.float('Tax Code Sign', help="Usually 1 or -1."),
+
+        # Same fields for refund invoices
+
+        'ref_base_code_id': fields.many2one('account.tax.code', 'Refund Base Code', help="Use this code for the VAT declaration."),
+        'ref_tax_code_id': fields.many2one('account.tax.code', 'Refund Tax Code', help="Use this code for the VAT declaration."),
+        'ref_base_sign': fields.float('Base Code Sign', help="Usually 1 or -1."),
+        'ref_tax_sign': fields.float('Tax Code Sign', help="Usually 1 or -1."),
+        'include_base_amount': fields.boolean('Included in base amount', help="Indicates if the amount of tax must be included in the base amount for the computation of the next taxes"),
+        'company_id': fields.many2one('res.company', 'Company', required=True),
+        'description': fields.char('Tax Code',size=32),
+        'price_include': fields.boolean('Tax Included in Price', help="Check this if the price you use on the product and invoices includes this tax."),
+        'type_tax_use': fields.selection([('sale','Sale'),('purchase','Purchase'),('all','All')], 'Tax Application', required=True)
+
+    }
+    _sql_constraints = [
+        ('name_company_uniq', 'unique(name, company_id)', 'Tax Name must be unique per company!'),
+    ]
+
+    def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=80):
+        """
+        Returns a list of tupples containing id, name, as internally it is called {def name_get}
+        result format: {[(id, name), (id, name), ...]}
+
+        @param cr: A database cursor
+        @param user: ID of the user currently logged in
+        @param name: name to search
+        @param args: other arguments
+        @param operator: default operator is 'ilike', it can be changed
+        @param context: context arguments, like lang, time zone
+        @param limit: Returns first 'n' ids of complete result, default is 80.
+
+        @return: Returns a list of tupples containing id and name
+        """
+        if not args:
+            args = []
+        if context is None:
+            context = {}
+        ids = []
+        if name:
+            ids = self.search(cr, user, [('description', '=', name)] + args, limit=limit, context=context)
+            if not ids:
+                ids = self.search(cr, user, [('name', operator, name)] + args, limit=limit, context=context)
+        else:
+            ids = self.search(cr, user, args, limit=limit, context=context or {})
+        return self.name_get(cr, user, ids, context=context)
+
+    def write(self, cr, uid, ids, vals, context=None):
+        if vals.get('type', False) and vals['type'] in ('none', 'code'):
+            vals.update({'amount': 0.0})
+        return super(account_tax, self).write(cr, uid, ids, vals, context=context)
+
+    def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
+        journal_pool = self.pool.get('account.journal')
+
+        if context and context.has_key('type'):
+            if context.get('type') in ('out_invoice','out_refund'):
+                args += [('type_tax_use','in',['sale','all'])]
+            elif context.get('type') in ('in_invoice','in_refund'):
+                args += [('type_tax_use','in',['purchase','all'])]
+
+        if context and context.has_key('journal_id'):
+            journal = journal_pool.browse(cr, uid, context.get('journal_id'))
+            if journal.type in ('sale', 'purchase'):
+                args += [('type_tax_use','in',[journal.type,'all'])]
+
+        return super(account_tax, self).search(cr, uid, args, offset, limit, order, context, count)
+
+    def name_get(self, cr, uid, ids, context=None):
+        if not ids:
+            return []
+        res = []
+        for record in self.read(cr, uid, ids, ['description','name'], context=context):
+            name = record['description'] and record['description'] or record['name']
+            res.append((record['id'],name ))
+        return res
+
+    def _default_company(self, cr, uid, context=None):
+        user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
+        if user.company_id:
+            return user.company_id.id
+        return self.pool.get('res.company').search(cr, uid, [('parent_id', '=', False)])[0]
+
+    _defaults = {
+        'python_compute': '''# price_unit\n# address: res.partner.address object or False\n# product: product.product object or None\n# partner: res.partner object or None\n\nresult = price_unit * 0.10''',
+        'python_compute_inv': '''# price_unit\n# address: res.partner.address object or False\n# product: product.product object or False\n\nresult = price_unit * 0.10''',
+        'applicable_type': 'true',
+        'type': 'percent',
+        'amount': 0,
+        'price_include': 0,
+        'active': 1,
+        'type_tax_use': 'all',
+        'sequence': 1,
+        'ref_tax_sign': 1,
+        'ref_base_sign': 1,
+        'tax_sign': 1,
+        'base_sign': 1,
+        'include_base_amount': False,
+        'company_id': _default_company,
+    }
+    _order = 'sequence'
+
+    def _applicable(self, cr, uid, taxes, price_unit, address_id=None, product=None, partner=None):
+        res = []
+        obj_partener_address = self.pool.get('res.partner.address')
+        for tax in taxes:
+            if tax.applicable_type=='code':
+                localdict = {'price_unit':price_unit, 'address':obj_partener_address.browse(cr, uid, address_id), 'product':product, 'partner':partner}
+                exec tax.python_applicable in localdict
+                if localdict.get('result', False):
+                    res.append(tax)
+            else:
+                res.append(tax)
+        return res
+
+    def _unit_compute(self, cr, uid, taxes, price_unit, address_id=None, product=None, partner=None, quantity=0):
+        taxes = self._applicable(cr, uid, taxes, price_unit, address_id, product, partner)
+        res = []
+        cur_price_unit=price_unit
+        obj_partener_address = self.pool.get('res.partner.address')
+        for tax in taxes:
+            # we compute the amount for the current tax object and append it to the result
+            data = {'id':tax.id,
+                    'name':tax.description and tax.description + " - " + tax.name or tax.name,
+                    'account_collected_id':tax.account_collected_id.id,
+                    'account_paid_id':tax.account_paid_id.id,
+                    'base_code_id': tax.base_code_id.id,
+                    'ref_base_code_id': tax.ref_base_code_id.id,
+                    'sequence': tax.sequence,
+                    'base_sign': tax.base_sign,
+                    'tax_sign': tax.tax_sign,
+                    'ref_base_sign': tax.ref_base_sign,
+                    'ref_tax_sign': tax.ref_tax_sign,
+                    'price_unit': cur_price_unit,
+                    'tax_code_id': tax.tax_code_id.id,
+                    'ref_tax_code_id': tax.ref_tax_code_id.id,
+            }
+            res.append(data)
+            if tax.type=='percent':
+                amount = cur_price_unit * tax.amount
+                data['amount'] = amount
+
+            elif tax.type=='fixed':
+                data['amount'] = tax.amount
+                data['tax_amount']=quantity
+               # data['amount'] = quantity
+            elif tax.type=='code':
+                address = address_id and obj_partener_address.browse(cr, uid, address_id) or None
+                localdict = {'price_unit':cur_price_unit, 'address':address, 'product':product, 'partner':partner}
+                exec tax.python_compute in localdict
+                amount = localdict['result']
+                data['amount'] = amount
+            elif tax.type=='balance':
+                data['amount'] = cur_price_unit - reduce(lambda x,y: y.get('amount',0.0)+x, res, 0.0)
+                data['balance'] = cur_price_unit
+
+            amount2 = data.get('amount', 0.0)
+            if tax.child_ids:
+                if tax.child_depend:
+                    latest = res.pop()
+                amount = amount2
+                child_tax = self._unit_compute(cr, uid, tax.child_ids, amount, address_id, product, partner, quantity)
+                res.extend(child_tax)
+                if tax.child_depend:
+                    for r in res:
+                        for name in ('base','ref_base'):
+                            if latest[name+'_code_id'] and latest[name+'_sign'] and not r[name+'_code_id']:
+                                r[name+'_code_id'] = latest[name+'_code_id']
+                                r[name+'_sign'] = latest[name+'_sign']
+                                r['price_unit'] = latest['price_unit']
+                                latest[name+'_code_id'] = False
+                        for name in ('tax','ref_tax'):
+                            if latest[name+'_code_id'] and latest[name+'_sign'] and not r[name+'_code_id']:
+                                r[name+'_code_id'] = latest[name+'_code_id']
+                                r[name+'_sign'] = latest[name+'_sign']
+                                r['amount'] = data['amount']
+                                latest[name+'_code_id'] = False
+            if tax.include_base_amount:
+                cur_price_unit+=amount2
+        return res
+
+    def compute_all(self, cr, uid, taxes, price_unit, quantity, address_id=None, product=None, partner=None, force_excluded=False):
+        """
+        :param force_excluded: boolean used to say that we don't want to consider the value of field price_include of
+            tax. It's used in encoding by line where you don't matter if you encoded a tax with that boolean to True or
+            False
+        RETURN: {
+                'total': 0.0,                # Total without taxes
+                'total_included: 0.0,        # Total with taxes
+                'taxes': []                  # List of taxes, see compute for the format
+            }
+        """
+        precision = self.pool.get('decimal.precision').precision_get(cr, uid, 'Account')
+        totalin = totalex = round(price_unit * quantity, precision)
+        tin = []
+        tex = []
+        for tax in taxes:
+            if not tax.price_include or force_excluded:
+                tex.append(tax)
+            else:
+                tin.append(tax)
+        tin = self.compute_inv(cr, uid, tin, price_unit, quantity, address_id=address_id, product=product, partner=partner)
+        for r in tin:
+            totalex -= r.get('amount', 0.0)
+        totlex_qty = 0.0
+        try:
+            totlex_qty = totalex/quantity
+        except:
+            pass
+        tex = self._compute(cr, uid, tex, totlex_qty, quantity, address_id=address_id, product=product, partner=partner)
+        for r in tex:
+            totalin += r.get('amount', 0.0)
+        return {
+            'total': totalex,
+            'total_included': totalin,
+            'taxes': tin + tex
+        }
+
+    def compute(self, cr, uid, taxes, price_unit, quantity, address_id=None, product=None, partner=None):
+        logger = netsvc.Logger()
+        logger.notifyChannel("warning", netsvc.LOG_WARNING,
+            "Deprecated, use compute_all(...)['taxes'] instead of compute(...) to manage prices with tax included")
+        return self._compute(cr, uid, taxes, price_unit, quantity, address_id, product, partner)
+
+    def _compute(self, cr, uid, taxes, price_unit, quantity, address_id=None, product=None, partner=None):
+        """
+        Compute tax values for given PRICE_UNIT, QUANTITY and a buyer/seller ADDRESS_ID.
+
+        RETURN:
+            [ tax ]
+            tax = {'name':'', 'amount':0.0, 'account_collected_id':1, 'account_paid_id':2}
+            one tax for each tax id in IDS and their children
+        """
+        res = self._unit_compute(cr, uid, taxes, price_unit, address_id, product, partner, quantity)
+        total = 0.0
+        precision_pool = self.pool.get('decimal.precision')
+        for r in res:
+            if r.get('balance',False):
+                r['amount'] = round(r.get('balance', 0.0) * quantity, precision_pool.precision_get(cr, uid, 'Account')) - total
+            else:
+                r['amount'] = round(r.get('amount', 0.0) * quantity, precision_pool.precision_get(cr, uid, 'Account'))
+                total += r['amount']
+        return res
+
+    def _unit_compute_inv(self, cr, uid, taxes, price_unit, address_id=None, product=None, partner=None):
+        taxes = self._applicable(cr, uid, taxes, price_unit, address_id, product, partner)
+        obj_partener_address = self.pool.get('res.partner.address')
+        res = []
+        taxes.reverse()
+        cur_price_unit = price_unit
+
+        tax_parent_tot = 0.0
+        for tax in taxes:
+            if (tax.type=='percent') and not tax.include_base_amount:
+                tax_parent_tot += tax.amount
+
+        for tax in taxes:
+            if (tax.type=='fixed') and not tax.include_base_amount:
+                cur_price_unit -= tax.amount
+
+        for tax in taxes:
+            if tax.type=='percent':
+                if tax.include_base_amount:
+                    amount = cur_price_unit - (cur_price_unit / (1 + tax.amount))
+                else:
+                    amount = (cur_price_unit / (1 + tax_parent_tot)) * tax.amount
+
+            elif tax.type=='fixed':
+                amount = tax.amount
+
+            elif tax.type=='code':
+                address = address_id and obj_partener_address.browse(cr, uid, address_id) or None
+                localdict = {'price_unit':cur_price_unit, 'address':address, 'product':product, 'partner':partner}
+                exec tax.python_compute_inv in localdict
+                amount = localdict['result']
+            elif tax.type=='balance':
+                amount = cur_price_unit - reduce(lambda x,y: y.get('amount',0.0)+x, res, 0.0)
+
+            if tax.include_base_amount:
+                cur_price_unit -= amount
+                todo = 0
+            else:
+                todo = 1
+            res.append({
+                'id': tax.id,
+                'todo': todo,
+                'name': tax.name,
+                'amount': amount,
+                'account_collected_id': tax.account_collected_id.id,
+                'account_paid_id': tax.account_paid_id.id,
+                'base_code_id': tax.base_code_id.id,
+                'ref_base_code_id': tax.ref_base_code_id.id,
+                'sequence': tax.sequence,
+                'base_sign': tax.base_sign,
+                'tax_sign': tax.tax_sign,
+                'ref_base_sign': tax.ref_base_sign,
+                'ref_tax_sign': tax.ref_tax_sign,
+                'price_unit': cur_price_unit,
+                'tax_code_id': tax.tax_code_id.id,
+                'ref_tax_code_id': tax.ref_tax_code_id.id,
+            })
+            if tax.child_ids:
+                if tax.child_depend:
+                    del res[-1]
+                    amount = price_unit
+
+            parent_tax = self._unit_compute_inv(cr, uid, tax.child_ids, amount, address_id, product, partner)
+            res.extend(parent_tax)
+
+        total = 0.0
+        for r in res:
+            if r['todo']:
+                total += r['amount']
+        for r in res:
+            r['price_unit'] -= total
+            r['todo'] = 0
+        return res
+
+    def compute_inv(self, cr, uid, taxes, price_unit, quantity, address_id=None, product=None, partner=None):
+        """
+        Compute tax values for given PRICE_UNIT, QUANTITY and a buyer/seller ADDRESS_ID.
+        Price Unit is a VAT included price
+
+        RETURN:
+            [ tax ]
+            tax = {'name':'', 'amount':0.0, 'account_collected_id':1, 'account_paid_id':2}
+            one tax for each tax id in IDS and their children
+        """
+        res = self._unit_compute_inv(cr, uid, taxes, price_unit, address_id, product, partner=None)
+        total = 0.0
+        obj_precision = self.pool.get('decimal.precision')
+        for r in res:
+            prec = obj_precision.precision_get(cr, uid, 'Account')
+            if r.get('balance',False):
+                r['amount'] = round(r['balance'] * quantity, prec) - total
+            else:
+                r['amount'] = round(r['amount'] * quantity, prec)
+                total += r['amount']
+        return res
+
+account_tax()
+
+# ---------------------------------------------------------
+# Account Entries Models
+# ---------------------------------------------------------
+
+class account_model(osv.osv):
+    _name = "account.model"
+    _description = "Account Model"
+    _columns = {
+        'name': fields.char('Model Name', size=64, required=True, help="This is a model for recurring accounting entries"),
+        'journal_id': fields.many2one('account.journal', 'Journal', required=True),
+        'company_id': fields.related('journal_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True),
+        'lines_id': fields.one2many('account.model.line', 'model_id', 'Model Entries'),
+        'legend': fields.text('Legend', readonly=True, size=100),
+    }
+
+    _defaults = {
+        'legend': lambda self, cr, uid, context:_('You can specify year, month and date in the name of the model using the following labels:\n\n%(year)s: To Specify Year \n%(month)s: To Specify Month \n%(date)s: Current Date\n\ne.g. My model on %(date)s'),
+    }
+    def generate(self, cr, uid, ids, datas={}, context=None):
+        move_ids = []
+        entry = {}
+        account_move_obj = self.pool.get('account.move')
+        account_move_line_obj = self.pool.get('account.move.line')
+        pt_obj = self.pool.get('account.payment.term')
+        period_obj = self.pool.get('account.period')
+
+        if context is None:
+            context = {}
+
+        if datas.get('date', False):
+            context.update({'date': datas['date']})
+
+        move_date = context.get('date', time.strftime('%Y-%m-%d'))
+        move_date = datetime.strptime(move_date,"%Y-%m-%d")
+        for model in self.browse(cr, uid, ids, context=context):
+            ctx = context.copy()
+            ctx.update({'company_id': model.company_id.id})
+            period_ids = period_obj.find(cr, uid, dt=context.get('date', False), context=ctx)
+            period_id = period_ids and period_ids[0] or False
+            ctx.update({'journal_id': model.journal_id.id,'period_id': period_id})
+            try:
+                entry['name'] = model.name%{'year': move_date.strftime('%Y'), 'month': move_date.strftime('%m'), 'date': move_date.strftime('%Y-%m')}
+            except:
+                raise osv.except_osv(_('Wrong model !'), _('You have a wrong expression "%(...)s" in your model !'))
+            move_id = account_move_obj.create(cr, uid, {
+                'ref': entry['name'],
+                'period_id': period_id,
+                'journal_id': model.journal_id.id,
+                'date': context.get('date', fields.date.context_today(self,cr,uid,context=context))
+            })
+            move_ids.append(move_id)
+            for line in model.lines_id:
+                analytic_account_id = False
+                if line.analytic_account_id:
+                    if not model.journal_id.analytic_journal_id:
+                        raise osv.except_osv(_('No Analytic Journal !'),_("You have to define an analytic journal on the '%s' journal!") % (model.journal_id.name,))
+                    analytic_account_id = line.analytic_account_id.id
+                val = {
+                    'move_id': move_id,
+                    'journal_id': model.journal_id.id,
+                    'period_id': period_id,
+                    'analytic_account_id': analytic_account_id
+                }
+
+                date_maturity = context.get('date',time.strftime('%Y-%m-%d'))
+                if line.date_maturity == 'partner':
+                    if not line.partner_id:
+                        raise osv.except_osv(_('Error !'), _("Maturity date of entry line generated by model line '%s' of model '%s' is based on partner payment term!" \
+                                                                "\nPlease define partner on it!")%(line.name, model.name))
+                    if line.partner_id.property_payment_term:
+                        payment_term_id = line.partner_id.property_payment_term.id
+                        pterm_list = pt_obj.compute(cr, uid, payment_term_id, value=1, date_ref=date_maturity)
+                        if pterm_list:
+                            pterm_list = [l[0] for l in pterm_list]
+                            pterm_list.sort()
+                            date_maturity = pterm_list[-1]
+
+                val.update({
+                    'name': line.name,
+                    'quantity': line.quantity,
+                    'debit': line.debit,
+                    'credit': line.credit,
+                    'account_id': line.account_id.id,
+                    'move_id': move_id,
+                    'partner_id': line.partner_id.id,
+                    'date': context.get('date', fields.date.context_today(self,cr,uid,context=context)),
+                    'date_maturity': date_maturity
+                })
+                account_move_line_obj.create(cr, uid, val, context=ctx)
+
+        return move_ids
+
+account_model()
+
+class account_model_line(osv.osv):
+    _name = "account.model.line"
+    _description = "Account Model Entries"
+    _columns = {
+        'name': fields.char('Name', size=64, required=True),
+        'sequence': fields.integer('Sequence', required=True, help="The sequence field is used to order the resources from lower sequences to higher ones."),
+        'quantity': fields.float('Quantity', digits_compute=dp.get_precision('Account'), help="The optional quantity on entries."),
+        'debit': fields.float('Debit', digits_compute=dp.get_precision('Account')),
+        'credit': fields.float('Credit', digits_compute=dp.get_precision('Account')),
+        'account_id': fields.many2one('account.account', 'Account', required=True, ondelete="cascade"),
+        'analytic_account_id': fields.many2one('account.analytic.account', 'Analytic Account', ondelete="cascade"),
+        'model_id': fields.many2one('account.model', 'Model', required=True, ondelete="cascade", select=True),
+        'amount_currency': fields.float('Amount Currency', help="The amount expressed in an optional other currency."),
+        'currency_id': fields.many2one('res.currency', 'Currency'),
+        'partner_id': fields.many2one('res.partner', 'Partner'),
+        'date_maturity': fields.selection([('today','Date of the day'), ('partner','Partner Payment Term')], 'Maturity Date', help="The maturity date of the generated entries for this model. You can choose between the creation date or the creation date of the entries plus the partner payment terms."),
+    }
+    _order = 'sequence'
+    _sql_constraints = [
+        ('credit_debit1', 'CHECK (credit*debit=0)',  'Wrong credit or debit value in model, they must be positive!'),
+        ('credit_debit2', 'CHECK (credit+debit>=0)', 'Wrong credit or debit value in model, they must be positive!'),
+    ]
+account_model_line()
+
+# ---------------------------------------------------------
+# Account Subscription
+# ---------------------------------------------------------
+
+
+class account_subscription(osv.osv):
+    _name = "account.subscription"
+    _description = "Account Subscription"
+    _columns = {
+        'name': fields.char('Name', size=64, required=True),
+        'ref': fields.char('Reference', size=16),
+        'model_id': fields.many2one('account.model', 'Model', required=True),
+        'date_start': fields.date('Start Date', required=True),
+        'period_total': fields.integer('Number of Periods', required=True),
+        'period_nbr': fields.integer('Period', required=True),
+        'period_type': fields.selection([('day','days'),('month','month'),('year','year')], 'Period Type', required=True),
+        'state': fields.selection([('draft','Draft'),('running','Running'),('done','Done')], 'State', required=True, readonly=True),
+        'lines_id': fields.one2many('account.subscription.line', 'subscription_id', 'Subscription Lines')
+    }
+    _defaults = {
+        'date_start': fields.date.context_today,
+        'period_type': 'month',
+        'period_total': 12,
+        'period_nbr': 1,
+        'state': 'draft',
+    }
+    def state_draft(self, cr, uid, ids, context=None):
+        self.write(cr, uid, ids, {'state':'draft'})
+        return False
+
+    def check(self, cr, uid, ids, context=None):
+        todone = []
+        for sub in self.browse(cr, uid, ids, context=context):
+            ok = True
+            for line in sub.lines_id:
+                if not line.move_id.id:
+                    ok = False
+                    break
+            if ok:
+                todone.append(sub.id)
+        if todone:
+            self.write(cr, uid, todone, {'state':'done'})
+        return False
+
+    def remove_line(self, cr, uid, ids, context=None):
+        toremove = []
+        for sub in self.browse(cr, uid, ids, context=context):
+            for line in sub.lines_id:
+                if not line.move_id.id:
+                    toremove.append(line.id)
+        if toremove:
+            self.pool.get('account.subscription.line').unlink(cr, uid, toremove)
+        self.write(cr, uid, ids, {'state':'draft'})
+        return False
+
+    def compute(self, cr, uid, ids, context=None):
+        for sub in self.browse(cr, uid, ids, context=context):
+            ds = sub.date_start
+            for i in range(sub.period_total):
+                self.pool.get('account.subscription.line').create(cr, uid, {
+                    'date': ds,
+                    'subscription_id': sub.id,
+                })
+                if sub.period_type=='day':
+                    ds = (datetime.strptime(ds, '%Y-%m-%d') + relativedelta(days=sub.period_nbr)).strftime('%Y-%m-%d')
+                if sub.period_type=='month':
+                    ds = (datetime.strptime(ds, '%Y-%m-%d') + relativedelta(months=sub.period_nbr)).strftime('%Y-%m-%d')
+                if sub.period_type=='year':
+                    ds = (datetime.strptime(ds, '%Y-%m-%d') + relativedelta(years=sub.period_nbr)).strftime('%Y-%m-%d')
+        self.write(cr, uid, ids, {'state':'running'})
+        return True
+
+account_subscription()
+
+class account_subscription_line(osv.osv):
+    _name = "account.subscription.line"
+    _description = "Account Subscription Line"
+    _columns = {
+        'subscription_id': fields.many2one('account.subscription', 'Subscription', required=True, select=True),
+        'date': fields.date('Date', required=True),
+        'move_id': fields.many2one('account.move', 'Entry'),
+    }
+
+    def move_create(self, cr, uid, ids, context=None):
+        tocheck = {}
+        all_moves = []
+        obj_model = self.pool.get('account.model')
+        for line in self.browse(cr, uid, ids, context=context):
+            datas = {
+                'date': line.date,
+            }
+            move_ids = obj_model.generate(cr, uid, [line.subscription_id.model_id.id], datas, context)
+            tocheck[line.subscription_id.id] = True
+            self.write(cr, uid, [line.id], {'move_id':move_ids[0]})
+            all_moves.extend(move_ids)
+        if tocheck:
+            self.pool.get('account.subscription').check(cr, uid, tocheck.keys(), context)
+        return all_moves
+
+    _rec_name = 'date'
+
+account_subscription_line()
+
+#  ---------------------------------------------------------------
+#   Account Templates: Account, Tax, Tax Code and chart. + Wizard
+#  ---------------------------------------------------------------
+
+class account_tax_template(osv.osv):
+    _name = 'account.tax.template'
+account_tax_template()
+
+class account_account_template(osv.osv):
+    _order = "code"
+    _name = "account.account.template"
+    _description ='Templates for Accounts'
+
+    _columns = {
+        'name': fields.char('Name', size=256, required=True, select=True),
+        'currency_id': fields.many2one('res.currency', 'Secondary Currency', help="Forces all moves for this account to have this secondary currency."),
+        'code': fields.char('Code', size=64, required=True, select=1),
+        'type': fields.selection([
+            ('receivable','Receivable'),
+            ('payable','Payable'),
+            ('view','View'),
+            ('consolidation','Consolidation'),
+            ('liquidity','Liquidity'),
+            ('other','Regular'),
+            ('closed','Closed'),
+            ], 'Internal Type', required=True,help="This type is used to differentiate types with "\
+            "special effects in OpenERP: view can not have entries, consolidation are accounts that "\
+            "can have children accounts for multi-company consolidations, payable/receivable are for "\
+            "partners accounts (for debit/credit computations), closed for depreciated accounts."),
+        'user_type': fields.many2one('account.account.type', 'Account Type', required=True,
+            help="These types are defined according to your country. The type contains more information "\
+            "about the account and its specificities."),
+        'financial_report_ids': fields.many2many('account.financial.report', 'account_template_financial_report', 'account_template_id', 'report_line_id', 'Financial Reports'),
+        'reconcile': fields.boolean('Allow Reconciliation', help="Check this option if you want the user to reconcile entries in this account."),
+        'shortcut': fields.char('Shortcut', size=12),
+        'note': fields.text('Note'),
+        'parent_id': fields.many2one('account.account.template', 'Parent Account Template', ondelete='cascade'),
+        'child_parent_ids':fields.one2many('account.account.template', 'parent_id', 'Children'),
+        'tax_ids': fields.many2many('account.tax.template', 'account_account_template_tax_rel', 'account_id', 'tax_id', 'Default Taxes'),
+        'nocreate': fields.boolean('Optional create', help="If checked, the new chart of accounts will not contain this by default."),
+        'chart_template_id': fields.many2one('account.chart.template', 'Chart Template', help="This optional field allow you to link an account template to a specific chart template that may differ from the one its root parent belongs to. This allow you to define chart templates that extend another and complete it with few new accounts (You don't need to define the whole structure that is common to both several times)."),
+    }
+
+    _defaults = {
+        'reconcile': False,
+        'type': 'view',
+        'nocreate': False,
+    }
+
+    def _check_type(self, cr, uid, ids, context=None):
+        if context is None:
+            context = {}
+        accounts = self.browse(cr, uid, ids, context=context)
+        for account in accounts:
+            if account.parent_id and account.parent_id.type != 'view':
+                return False
+        return True
+
+    _check_recursion = check_cycle
+    _constraints = [
+        (_check_recursion, 'Error ! You can not create recursive account templates.', ['parent_id']),
+        (_check_type, 'Configuration Error!\nYou can not define children to an account with internal type different of "View"! ', ['type']),
+
+    ]
+
+    def name_get(self, cr, uid, ids, context=None):
+        if not ids:
+            return []
+        reads = self.read(cr, uid, ids, ['name','code'], context=context)
+        res = []
+        for record in reads:
+            name = record['name']
+            if record['code']:
+                name = record['code']+' '+name
+            res.append((record['id'],name ))
+        return res
+
+    def generate_account(self, cr, uid, chart_template_id, tax_template_ref, acc_template_ref, code_digits, company_id, context=None):
+        """
+        This method for generating accounts from templates.
+
+        :param chart_template_id: id of the chart template chosen in the wizard
+        :param tax_template_ref: Taxes templates reference for write taxes_id in account_account.
+        :paramacc_template_ref: dictionary with the mappping between the account templates and the real accounts.
+        :param code_digits: number of digits got from wizard.multi.charts.accounts, this is use for account code.
+        :param company_id: company_id selected from wizard.multi.charts.accounts.
+        :returns: return acc_template_ref for reference purpose.
+        :rtype: dict
+        """
+        if context is None:
+            context = {}
+        obj_acc = self.pool.get('account.account')
+        company_name = self.pool.get('res.company').browse(cr, uid, company_id, context=context).name
+        template = self.pool.get('account.chart.template').browse(cr, uid, chart_template_id, context=context)
+        #deactivate the parent_store functionnality on account_account for rapidity purpose
+        ctx = context.copy()
+        ctx.update({'defer_parent_store_computation': True})
+        level_ref = {}
+        children_acc_criteria = [('chart_template_id','=', chart_template_id)]
+        if template.account_root_id.id:
+            children_acc_criteria = ['|'] + children_acc_criteria + ['&',('parent_id','child_of', [template.account_root_id.id]),('chart_template_id','=', False)]
+        children_acc_template = self.search(cr, uid, [('nocreate','!=',True)] + children_acc_criteria, order='id')
+        for account_template in self.browse(cr, uid, children_acc_template, context=context):
+            # skip the root of COA if it's not the main one
+            if (template.account_root_id.id == account_template.id) and template.parent_id:
+                continue
+            tax_ids = []
+            for tax in account_template.tax_ids:
+                tax_ids.append(tax_template_ref[tax.id])
+
+            code_main = account_template.code and len(account_template.code) or 0
+            code_acc = account_template.code or ''
+            if code_main > 0 and code_main <= code_digits and account_template.type != 'view':
+                code_acc = str(code_acc) + (str('0'*(code_digits-code_main)))
+            parent_id = account_template.parent_id and ((account_template.parent_id.id in acc_template_ref) and acc_template_ref[account_template.parent_id.id]) or False
+            #the level as to be given as well at the creation time, because of the defer_parent_store_computation in
+            #context. Indeed because of this, the parent_left and parent_right are not computed and thus the child_of
+            #operator does not return the expected values, with result of having the level field not computed at all.
+            if parent_id:
+                level = parent_id in level_ref and level_ref[parent_id] + 1 or obj_acc._get_level(cr, uid, [parent_id], 'level', None, context=context)[parent_id] + 1
+            else:
+                level = 0
+            vals={
+                'name': (template.account_root_id.id == account_template.id) and company_name or account_template.name,
+                'currency_id': account_template.currency_id and account_template.currency_id.id or False,
+                'code': code_acc,
+                'type': account_template.type,
+                'user_type': account_template.user_type and account_template.user_type.id or False,
+                'reconcile': account_template.reconcile,
+                'shortcut': account_template.shortcut,
+                'note': account_template.note,
+                'financial_report_ids': account_template.financial_report_ids and [(6,0,[x.id for x in account_template.financial_report_ids])] or False,
+                'parent_id': parent_id,
+                'tax_ids': [(6,0,tax_ids)],
+                'company_id': company_id,
+                'level': level,
+            }
+            new_account = obj_acc.create(cr, uid, vals, context=ctx)
+            acc_template_ref[account_template.id] = new_account
+            level_ref[new_account] = level
+
+        #reactivate the parent_store functionnality on account_account
+        obj_acc._parent_store_compute(cr)
+        return acc_template_ref
+
+account_account_template()
+
+class account_add_tmpl_wizard(osv.osv_memory):
+    """Add one more account from the template.
+
+    With the 'nocreate' option, some accounts may not be created. Use this to add them later."""
+    _name = 'account.addtmpl.wizard'
+
+    def _get_def_cparent(self, cr, uid, context=None):
+        acc_obj = self.pool.get('account.account')
+        tmpl_obj = self.pool.get('account.account.template')
+        tids = tmpl_obj.read(cr, uid, [context['tmpl_ids']], ['parent_id'])
+        if not tids or not tids[0]['parent_id']:
+            return False
+        ptids = tmpl_obj.read(cr, uid, [tids[0]['parent_id'][0]], ['code'])
+        res = None
+        if not ptids or not ptids[0]['code']:
+            raise osv.except_osv(_('Error !'), _('I can not locate a parent code for the template account!'))
+            res = acc_obj.search(cr, uid, [('code','=',ptids[0]['code'])])
+        return res and res[0] or False
+
+    _columns = {
+        'cparent_id':fields.many2one('account.account', 'Parent target', help="Creates an account with the selected template under this existing parent.", required=True),
+    }
+    _defaults = {
+        'cparent_id': _get_def_cparent,
+    }
+
+    def action_create(self,cr,uid,ids,context=None):
+        if context is None:
+            context = {}
+        acc_obj = self.pool.get('account.account')
+        tmpl_obj = self.pool.get('account.account.template')
+        data = self.read(cr, uid, ids)[0]
+        company_id = acc_obj.read(cr, uid, [data['cparent_id'][0]], ['company_id'])[0]['company_id'][0]
+        account_template = tmpl_obj.browse(cr, uid, context['tmpl_ids'])
+        vals = {
+            'name': account_template.name,
+            'currency_id': account_template.currency_id and account_template.currency_id.id or False,
+            'code': account_template.code,
+            'type': account_template.type,
+            'user_type': account_template.user_type and account_template.user_type.id or False,
+            'reconcile': account_template.reconcile,
+            'shortcut': account_template.shortcut,
+            'note': account_template.note,
+            'parent_id': data['cparent_id'][0],
+            'company_id': company_id,
+            }
+        acc_obj.create(cr, uid, vals)
+        return {'type':'state', 'state': 'end' }
+
+    def action_cancel(self, cr, uid, ids, context=None):
+        return { 'type': 'state', 'state': 'end' }
+
+account_add_tmpl_wizard()
+
+class account_tax_code_template(osv.osv):
+
+    _name = 'account.tax.code.template'
+    _description = 'Tax Code Template'
+    _order = 'code'
+    _rec_name = 'code'
+    _columns = {
+        'name': fields.char('Tax Case Name', size=64, required=True),
+        'code': fields.char('Case Code', size=64),
+        'info': fields.text('Description'),
+        'parent_id': fields.many2one('account.tax.code.template', 'Parent Code', select=True),
+        'child_ids': fields.one2many('account.tax.code.template', 'parent_id', 'Child Codes'),
+        'sign': fields.float('Sign For Parent', required=True),
+        'notprintable':fields.boolean("Not Printable in Invoice", help="Check this box if you don't want any VAT related to this Tax Code to appear on invoices"),
+    }
+
+    _defaults = {
+        'sign': 1.0,
+        'notprintable': False,
+    }
+
+    def generate_tax_code(self, cr, uid, tax_code_root_id, company_id, context=None):
+        '''
+        This function generates the tax codes from the templates of tax code that are children of the given one passed
+        in argument. Then it returns a dictionary with the mappping between the templates and the real objects.
+
+        :param tax_code_root_id: id of the root of all the tax code templates to process
+        :param company_id: id of the company the wizard is running for
+        :returns: dictionary with the mappping between the templates and the real objects.
+        :rtype: dict
+        '''
+        obj_tax_code_template = self.pool.get('account.tax.code.template')
+        obj_tax_code = self.pool.get('account.tax.code')
+        tax_code_template_ref = {}
+        company = self.pool.get('res.company').browse(cr, uid, company_id, context=context)
+
+        #find all the children of the tax_code_root_id
+        children_tax_code_template = tax_code_root_id and obj_tax_code_template.search(cr, uid, [('parent_id','child_of',[tax_code_root_id])], order='id') or []
+        for tax_code_template in obj_tax_code_template.browse(cr, uid, children_tax_code_template, context=context):
+            vals = {
+                'name': (tax_code_root_id == tax_code_template.id) and company.name or tax_code_template.name,
+                'code': tax_code_template.code,
+                'info': tax_code_template.info,
+                'parent_id': tax_code_template.parent_id and ((tax_code_template.parent_id.id in tax_code_template_ref) and tax_code_template_ref[tax_code_template.parent_id.id]) or False,
+                'company_id': company_id,
+                'sign': tax_code_template.sign,
+            }
+            #check if this tax code already exists
+            rec_list = obj_tax_code.search(cr, uid, [('name', '=', vals['name']),('code', '=', vals['code']),('company_id', '=', vals['company_id'])], context=context)
+            if not rec_list:
+                #if not yet, create it
+                new_tax_code = obj_tax_code.create(cr, uid, vals)
+                #recording the new tax code to do the mapping
+                tax_code_template_ref[tax_code_template.id] = new_tax_code
+        return tax_code_template_ref
+
+    def name_get(self, cr, uid, ids, context=None):
+        if not ids:
+            return []
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        reads = self.read(cr, uid, ids, ['name','code'], context, load='_classic_write')
+        return [(x['id'], (x['code'] and x['code'] + ' - ' or '') + x['name']) \
+                for x in reads]
+
+    _check_recursion = check_cycle
+    _constraints = [
+        (_check_recursion, 'Error ! You can not create recursive Tax Codes.', ['parent_id'])
+    ]
+    _order = 'code,name'
+account_tax_code_template()
+
+
+class account_chart_template(osv.osv):
+    _name="account.chart.template"
+    _description= "Templates for Account Chart"
+
+    _columns={
+        'name': fields.char('Name', size=64, required=True),
+        'parent_id': fields.many2one('account.chart.template', 'Parent Chart Template'),
+        'code_digits': fields.integer('# of Digits', required=True, help="No. of Digits to use for account code"), 
+        'visible': fields.boolean('Can be Visible?', help="Set this to False if you don't want this template to be used actively in the wizard that generate Chart of Accounts from templates, this is useful when you want to generate accounts of this template only when loading its child template."),
+        'complete_tax_set': fields.boolean('Complete Set of Taxes', help='This boolean helps you to choose if you want to propose to the user to encode the sale and purchase rates or choose from list of taxes. This last choice assumes that the set of tax defined on this template is complete'),
+        'account_root_id': fields.many2one('account.account.template', 'Root Account', domain=[('parent_id','=',False)]),
+        'tax_code_root_id': fields.many2one('account.tax.code.template', 'Root Tax Code', domain=[('parent_id','=',False)]),
+        'tax_template_ids': fields.one2many('account.tax.template', 'chart_template_id', 'Tax Template List', help='List of all the taxes that have to be installed by the wizard'),
+        'bank_account_view_id': fields.many2one('account.account.template', 'Bank Account'),
+        'property_account_receivable': fields.many2one('account.account.template', 'Receivable Account'),
+        'property_account_payable': fields.many2one('account.account.template', 'Payable Account'),
+        'property_account_expense_categ': fields.many2one('account.account.template', 'Expense Category Account'),
+        'property_account_income_categ': fields.many2one('account.account.template', 'Income Category Account'),
+        'property_account_expense': fields.many2one('account.account.template', 'Expense Account on Product Template'),
+        'property_account_income': fields.many2one('account.account.template', 'Income Account on Product Template'),
+        'property_reserve_and_surplus_account': fields.many2one('account.account.template', 'Reserve and Profit/Loss Account', domain=[('type', '=', 'payable')], help='This Account is used for transferring Profit/Loss(If It is Profit: Amount will be added, Loss: Amount will be deducted.), Which is calculated from Profilt & Loss Report'),
+        'property_account_income_opening': fields.many2one('account.account.template', 'Opening Entries Income Account'),
+        'property_account_expense_opening': fields.many2one('account.account.template', 'Opening Entries Expense Account'),
+    }
+
+    _defaults = {
+        'visible': True,
+        'code_digits': 6,
+        'complete_tax_set': True,
+    }
+
+account_chart_template()
+
+class account_tax_template(osv.osv):
+
+    _name = 'account.tax.template'
+    _description = 'Templates for Taxes'
+
+    _columns = {
+        'chart_template_id': fields.many2one('account.chart.template', 'Chart Template', required=True),
+        'name': fields.char('Tax Name', size=64, required=True),
+        'sequence': fields.integer('Sequence', required=True, help="The sequence field is used to order the taxes lines from lower sequences to higher ones. The order is important if you have a tax that has several tax children. In this case, the evaluation order is important."),
+        'amount': fields.float('Amount', required=True, digits=(14,4), help="For Tax Type percent enter % ratio between 0-1."),
+        'type': fields.selection( [('percent','Percent'), ('fixed','Fixed'), ('none','None'), ('code','Python Code'), ('balance','Balance')], 'Tax Type', required=True),
+        'applicable_type': fields.selection( [('true','True'), ('code','Python Code')], 'Applicable Type', required=True, help="If not applicable (computed through a Python code), the tax won't appear on the invoice."),
+        'domain':fields.char('Domain', size=32, help="This field is only used if you develop your own module allowing developers to create specific taxes in a custom domain."),
+        'account_collected_id':fields.many2one('account.account.template', 'Invoice Tax Account'),
+        'account_paid_id':fields.many2one('account.account.template', 'Refund Tax Account'),
+        'parent_id':fields.many2one('account.tax.template', 'Parent Tax Account', select=True),
+        'child_depend':fields.boolean('Tax on Children', help="Set if the tax computation is based on the computation of child taxes rather than on the total amount."),
+        'python_compute':fields.text('Python Code'),
+        'python_compute_inv':fields.text('Python Code (reverse)'),
+        'python_applicable':fields.text('Python Code'),
+
+        #
+        # Fields used for the VAT declaration
+        #
+        'base_code_id': fields.many2one('account.tax.code.template', 'Base Code', help="Use this code for the VAT declaration."),
+        'tax_code_id': fields.many2one('account.tax.code.template', 'Tax Code', help="Use this code for the VAT declaration."),
+        'base_sign': fields.float('Base Code Sign', help="Usually 1 or -1."),
+        'tax_sign': fields.float('Tax Code Sign', help="Usually 1 or -1."),
+
+        # Same fields for refund invoices
+
+        'ref_base_code_id': fields.many2one('account.tax.code.template', 'Refund Base Code', help="Use this code for the VAT declaration."),
+        'ref_tax_code_id': fields.many2one('account.tax.code.template', 'Refund Tax Code', help="Use this code for the VAT declaration."),
+        'ref_base_sign': fields.float('Base Code Sign', help="Usually 1 or -1."),
+        'ref_tax_sign': fields.float('Tax Code Sign', help="Usually 1 or -1."),
+        'include_base_amount': fields.boolean('Include in Base Amount', help="Set if the amount of tax must be included in the base amount before computing the next taxes."),
+        'description': fields.char('Internal Name', size=32),
+        'type_tax_use': fields.selection([('sale','Sale'),('purchase','Purchase'),('all','All')], 'Tax Use In', required=True,),
+        'price_include': fields.boolean('Tax Included in Price', help="Check this if the price you use on the product and invoices includes this tax."),
+    }
+
+    def name_get(self, cr, uid, ids, context=None):
+        if not ids:
+            return []
+        res = []
+        for record in self.read(cr, uid, ids, ['description','name'], context=context):
+            name = record['description'] and record['description'] or record['name']
+            res.append((record['id'],name ))
+        return res
+
+    def _default_company(self, cr, uid, context=None):
+        user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
+        if user.company_id:
+            return user.company_id.id
+        return self.pool.get('res.company').search(cr, uid, [('parent_id', '=', False)])[0]
+
+    _defaults = {
+        'python_compute': lambda *a: '''# price_unit\n# address: res.partner.address object or False\n# product: product.product object or None\n# partner: res.partner object or None\n\nresult = price_unit * 0.10''',
+        'python_compute_inv': lambda *a: '''# price_unit\n# address: res.partner.address object or False\n# product: product.product object or False\n\nresult = price_unit * 0.10''',
+        'applicable_type': 'true',
+        'type': 'percent',
+        'amount': 0,
+        'sequence': 1,
+        'ref_tax_sign': 1,
+        'ref_base_sign': 1,
+        'tax_sign': 1,
+        'base_sign': 1,
+        'include_base_amount': False,
+        'type_tax_use': 'all',
+        'price_include': 0,
+    }
+    _order = 'sequence'
+
+    def _generate_tax(self, cr, uid, tax_templates, tax_code_template_ref, company_id, context=None):
+        """
+        This method generate taxes from templates.
+
+        :param tax_templates: list of browse record of the tax templates to process
+        :param tax_code_template_ref: Taxcode templates reference.
+        :param company_id: id of the company the wizard is running for
+        :returns:
+            {
+            'tax_template_to_tax': mapping between tax template and the newly generated taxes corresponding,
+            'account_dict': dictionary containing a to-do list with all the accounts to assign on new taxes
+            }
+        """
+        if context is None:
+            context = {}
+        res = {}
+        todo_dict = {}
+        tax_template_to_tax = {}
+        for tax in tax_templates:
+            vals_tax = {
+                'name':tax.name,
+                'sequence': tax.sequence,
+                'amount': tax.amount,
+                'type': tax.type,
+                'applicable_type': tax.applicable_type,
+                'domain': tax.domain,
+                'parent_id': tax.parent_id and ((tax.parent_id.id in tax_template_to_tax) and tax_template_to_tax[tax.parent_id.id]) or False,
+                'child_depend': tax.child_depend,
+                'python_compute': tax.python_compute,
+                'python_compute_inv': tax.python_compute_inv,
+                'python_applicable': tax.python_applicable,
+                'base_code_id': tax.base_code_id and ((tax.base_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.base_code_id.id]) or False,
+                'tax_code_id': tax.tax_code_id and ((tax.tax_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.tax_code_id.id]) or False,
+                'base_sign': tax.base_sign,
+                'tax_sign': tax.tax_sign,
+                'ref_base_code_id': tax.ref_base_code_id and ((tax.ref_base_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.ref_base_code_id.id]) or False,
+                'ref_tax_code_id': tax.ref_tax_code_id and ((tax.ref_tax_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.ref_tax_code_id.id]) or False,
+                'ref_base_sign': tax.ref_base_sign,
+                'ref_tax_sign': tax.ref_tax_sign,
+                'include_base_amount': tax.include_base_amount,
+                'description': tax.description,
+                'company_id': company_id,
+                'type_tax_use': tax.type_tax_use,
+                'price_include': tax.price_include
+            }
+            new_tax = self.pool.get('account.tax').create(cr, uid, vals_tax)
+            tax_template_to_tax[tax.id] = new_tax
+            #as the accounts have not been created yet, we have to wait before filling these fields
+            todo_dict[new_tax] = {
+                'account_collected_id': tax.account_collected_id and tax.account_collected_id.id or False,
+                'account_paid_id': tax.account_paid_id and tax.account_paid_id.id or False,
+            }
+        res.update({'tax_template_to_tax': tax_template_to_tax, 'account_dict': todo_dict})
+        return res
+
+account_tax_template()
+
+# Fiscal Position Templates
+
+class account_fiscal_position_template(osv.osv):
+    _name = 'account.fiscal.position.template'
+    _description = 'Template for Fiscal Position'
+
+    _columns = {
+        'name': fields.char('Fiscal Position Template', size=64, required=True),
+        'chart_template_id': fields.many2one('account.chart.template', 'Chart Template', required=True),
+        'account_ids': fields.one2many('account.fiscal.position.account.template', 'position_id', 'Account Mapping'),
+        'tax_ids': fields.one2many('account.fiscal.position.tax.template', 'position_id', 'Tax Mapping'),
+        'note': fields.text('Notes', translate=True),
+    }
+
+    def generate_fiscal_position(self, cr, uid, chart_temp_id, tax_template_ref, acc_template_ref, company_id, context=None):
+        """
+        This method generate Fiscal Position, Fiscal Position Accounts and Fiscal Position Taxes from templates.
+
+        :param chart_temp_id: Chart Template Id.
+        :param taxes_ids: Taxes templates reference for generating account.fiscal.position.tax.
+        :param acc_template_ref: Account templates reference for generating account.fiscal.position.account.
+        :param company_id: company_id selected from wizard.multi.charts.accounts.
+        :returns: True
+        """
+        if context is None:
+            context = {}
+        obj_tax_fp = self.pool.get('account.fiscal.position.tax')
+        obj_ac_fp = self.pool.get('account.fiscal.position.account')
+        obj_fiscal_position = self.pool.get('account.fiscal.position')
+        fp_ids = self.search(cr, uid, [('chart_template_id', '=', chart_temp_id)])
+        for position in self.browse(cr, uid, fp_ids, context=context):
+            new_fp = obj_fiscal_position.create(cr, uid, {'company_id': company_id, 'name': position.name, 'note': position.note})
+            for tax in position.tax_ids:
+                obj_tax_fp.create(cr, uid, {
+                    'tax_src_id': tax_template_ref[tax.tax_src_id.id],
+                    'tax_dest_id': tax.tax_dest_id and tax_template_ref[tax.tax_dest_id.id] or False,
+                    'position_id': new_fp
+                })
+            for acc in position.account_ids:
+                obj_ac_fp.create(cr, uid, {
+                    'account_src_id': acc_template_ref[acc.account_src_id.id],
+                    'account_dest_id': acc_template_ref[acc.account_dest_id.id],
+                    'position_id': new_fp
+                })
+        return True
+
+account_fiscal_position_template()
+
+class account_fiscal_position_tax_template(osv.osv):
+    _name = 'account.fiscal.position.tax.template'
+    _description = 'Template Tax Fiscal Position'
+    _rec_name = 'position_id'
+
+    _columns = {
+        'position_id': fields.many2one('account.fiscal.position.template', 'Fiscal Position', required=True, ondelete='cascade'),
+        'tax_src_id': fields.many2one('account.tax.template', 'Tax Source', required=True),
+        'tax_dest_id': fields.many2one('account.tax.template', 'Replacement Tax')
+    }
+
+account_fiscal_position_tax_template()
+
+class account_fiscal_position_account_template(osv.osv):
+    _name = 'account.fiscal.position.account.template'
+    _description = 'Template Account Fiscal Mapping'
+    _rec_name = 'position_id'
+    _columns = {
+        'position_id': fields.many2one('account.fiscal.position.template', 'Fiscal Mapping', required=True, ondelete='cascade'),
+        'account_src_id': fields.many2one('account.account.template', 'Account Source', domain=[('type','<>','view')], required=True),
+        'account_dest_id': fields.many2one('account.account.template', 'Account Destination', domain=[('type','<>','view')], required=True)
+    }
+
+account_fiscal_position_account_template()
+
+# ---------------------------------------------------------
+# Account generation from template wizards
+# ---------------------------------------------------------
+
+class wizard_multi_charts_accounts(osv.osv_memory):
+    """
+    Create a new account chart for a company.
+    Wizards ask for:
+        * a company
+        * an account chart template
+        * a number of digits for formatting code of non-view accounts
+        * a list of bank accounts owned by the company
+    Then, the wizard:
+        * generates all accounts from the template and assigns them to the right company
+        * generates all taxes and tax codes, changing account assignations
+        * generates all accounting properties and assigns them correctly
+    """
+    _name='wizard.multi.charts.accounts'
+    _inherit = 'res.config'
+
+    _columns = {
+        'company_id':fields.many2one('res.company', 'Company', required=True),
+        'chart_template_id': fields.many2one('account.chart.template', 'Chart Template', required=True),
+        'bank_accounts_id': fields.one2many('account.bank.accounts.wizard', 'bank_account_id', 'Cash and Banks', required=True),
+        'code_digits':fields.integer('# of Digits', required=True, help="No. of Digits to use for account code"),
+        'seq_journal':fields.boolean('Separated Journal Sequences', help="Check this box if you want to use a different sequence for each created journal. Otherwise, all will use the same sequence."),
+        "sale_tax": fields.many2one("account.tax.template", "Default Sale Tax"),
+        "purchase_tax": fields.many2one("account.tax.template", "Default Purchase Tax"),
+        'sale_tax_rate': fields.float('Sales Tax(%)'),
+        'purchase_tax_rate': fields.float('Purchase Tax(%)'),
+        'complete_tax_set': fields.boolean('Complete Set of Taxes', help='This boolean helps you to choose if you want to propose to the user to encode the sales and purchase rates or use the usual m2o fields. This last choice assumes that the set of tax defined for the chosen template is complete'),
+    }
+    def onchange_tax_rate(self, cr, uid, ids, rate=False, context=None):
+        return {'value': {'purchase_tax_rate': rate or False}}
+
+    def onchange_chart_template_id(self, cr, uid, ids, chart_template_id=False, context=None):
+        res = {}
+        tax_templ_obj = self.pool.get('account.tax.template')
+        res['value'] = {'complete_tax_set': False, 'sale_tax': False, 'purchase_tax': False}
+        if chart_template_id:
+            data = self.pool.get('account.chart.template').browse(cr, uid, chart_template_id, context=context)
+            res['value'].update({'complete_tax_set': data.complete_tax_set})
+            if data.complete_tax_set:
+            # default tax is given by the lowest sequence. For same sequence we will take the latest created as it will be the case for tax created while isntalling the generic chart of account
+                sale_tax_ids = tax_templ_obj.search(cr, uid, [("chart_template_id"
+                                              , "=", chart_template_id), ('type_tax_use', 'in', ('sale','all'))], order="sequence, id desc")
+                purchase_tax_ids = tax_templ_obj.search(cr, uid, [("chart_template_id"
+                                              , "=", chart_template_id), ('type_tax_use', 'in', ('purchase','all'))], order="sequence, id desc")
+                res['value'].update({'sale_tax': sale_tax_ids and sale_tax_ids[0] or False, 'purchase_tax': purchase_tax_ids and purchase_tax_ids[0] or False})
+
+            if data.code_digits:
+               res['value'].update({'code_digits': data.code_digits})
+        return res
+
+    def default_get(self, cr, uid, fields, context=None):
+        res = super(wizard_multi_charts_accounts, self).default_get(cr, uid, fields, context=context) 
+        tax_templ_obj = self.pool.get('account.tax.template')
+
+        if 'bank_accounts_id' in fields:
+            res.update({'bank_accounts_id': [{'acc_name': _('Cash'), 'account_type': 'cash'},{'acc_name': _('Bank'), 'account_type': 'bank'}]})
+        if 'company_id' in fields:
+            res.update({'company_id': self.pool.get('res.users').browse(cr, uid, [uid], context=context)[0].company_id.id})
+        if 'seq_journal' in fields:
+            res.update({'seq_journal': True})
+
+        ids = self.pool.get('account.chart.template').search(cr, uid, [('visible', '=', True)], context=context)
+        if ids:
+            if 'chart_template_id' in fields:
+                res.update({'chart_template_id': ids[0]})
+            if 'sale_tax' in fields:
+                sale_tax_ids = tax_templ_obj.search(cr, uid, [("chart_template_id"
+                                              , "=", ids[0]), ('type_tax_use', 'in', ('sale','all'))], order="sequence")
+                res.update({'sale_tax': sale_tax_ids and sale_tax_ids[0] or False})
+            if 'purchase_tax' in fields:
+                purchase_tax_ids = tax_templ_obj.search(cr, uid, [("chart_template_id"
+                                          , "=", ids[0]), ('type_tax_use', 'in', ('purchase','all'))], order="sequence")
+                res.update({'purchase_tax': purchase_tax_ids and purchase_tax_ids[0] or False})
+        res.update({
+            'purchase_tax_rate': 15.0,
+            'sale_tax_rate': 15.0,
+        })
+        return res
+
+    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
+        res = super(wizard_multi_charts_accounts, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar,submenu=False)
+        cmp_select = []
+        acc_template_obj = self.pool.get('account.chart.template')
+        company_obj = self.pool.get('res.company')
+
+        company_ids = company_obj.search(cr, uid, [], context=context)
+        #display in the widget selection of companies, only the companies that haven't been configured yet (but don't care about the demo chart of accounts)
+        cr.execute("SELECT company_id FROM account_account WHERE active = 't' AND account_account.parent_id IS NULL AND name != %s", ("Chart For Automated Tests",))
+        configured_cmp = [r[0] for r in cr.fetchall()]
+        unconfigured_cmp = list(set(company_ids)-set(configured_cmp))
+        for field in res['fields']:
+            if field == 'company_id':
+                res['fields'][field]['domain'] = [('id','in',unconfigured_cmp)]
+                res['fields'][field]['selection'] = [('', '')]
+                if unconfigured_cmp:
+                    cmp_select = [(line.id, line.name) for line in company_obj.browse(cr, uid, unconfigured_cmp)]
+                    res['fields'][field]['selection'] = cmp_select
+        return res
+
+    def check_created_journals(self, cr, uid, vals_journal, company_id, context=None):
+        """
+        This method used for checking journals already created or not. If not then create new journal.
+        """
+        obj_journal = self.pool.get('account.journal')
+        rec_list = obj_journal.search(cr, uid, [('name','=', vals_journal['name']),('company_id', '=', company_id)], context=context)
+        if not rec_list:
+            obj_journal.create(cr, uid, vals_journal, context=context)
+        return True
+
+    def generate_journals(self, cr, uid, chart_template_id, acc_template_ref, company_id, context=None):
+        """
+        This method is used for creating journals.
+
+        :param chart_temp_id: Chart Template Id.
+        :param acc_template_ref: Account templates reference.
+        :param company_id: company_id selected from wizard.multi.charts.accounts.
+        :returns: True
+        """
+        journal_data = self._prepare_all_journals(cr, uid, chart_template_id, acc_template_ref, company_id, context=context)
+        for vals_journal in journal_data:
+            self.check_created_journals(cr, uid, vals_journal, company_id, context=context)
+        return True
+
+    def _prepare_all_journals(self, cr, uid, chart_template_id, acc_template_ref, company_id, context=None):
+        def _get_analytic_journal(journal_type):
+            # Get the analytic journal
+            data = False
+            if journal_type in ('sale', 'sale_refund'):
+                data = obj_data.get_object_reference(cr, uid, 'account', 'analytic_journal_sale') 
+            elif journal_type in ('purchase', 'purchase_refund'):
+                pass
+            elif journal_type == 'general':
+                pass
+            return data and data[1] or False
+
+        def _get_default_account(journal_type, type='debit'):
+            # Get the default accounts
+            default_account = False
+            if journal_type in ('sale', 'sale_refund'):
+                default_account = acc_template_ref.get(template.property_account_income_categ.id)
+            elif journal_type in ('purchase', 'purchase_refund'):
+                default_account = acc_template_ref.get(template.property_account_expense_categ.id)
+            elif journal_type == 'situation':
+                if type == 'debit':
+                    default_account = acc_template_ref.get(template.property_account_expense_opening.id)
+                else:
+                    default_account = acc_template_ref.get(template.property_account_income_opening.id)
+            return default_account
+
+        def _get_view_id(journal_type):
+            # Get the journal views
+            if journal_type in ('general', 'situation'):
+                data = obj_data.get_object_reference(cr, uid, 'account', 'account_journal_view')
+            elif journal_type in ('sale_refund', 'purchase_refund'):
+                data = obj_data.get_object_reference(cr, uid, 'account', 'account_sp_refund_journal_view') 
+            else:
+                data = obj_data.get_object_reference(cr, uid, 'account', 'account_sp_journal_view')
+            return data and data[1] or False
+
+        journal_names = {
+            'sale': _('Sales Journal'),
+            'purchase': _('Purchase Journal'),
+            'sale_refund': _('Sales Refund Journal'),
+            'purchase_refund': _('Purchase Refund Journal'),
+            'general': _('Miscellaneous Journal'),
+            'situation': _('Opening Entries Journal'),
+        }
+        journal_codes = {
+            'sale': _('SAJ'),
+            'purchase': _('EXJ'),
+            'sale_refund': _('SCNJ'),
+            'purchase_refund': _('ECNJ'),
+            'general': _('MISC'),
+            'situation': _('OPEJ'),
+        }
+
+        obj_data = self.pool.get('ir.model.data')
+        analytic_journal_obj = self.pool.get('account.analytic.journal')
+        template = self.pool.get('account.chart.template').browse(cr, uid, chart_template_id, context=context)
+
+        journal_data = []
+        for journal_type in ['sale', 'purchase', 'sale_refund', 'purchase_refund', 'general', 'situation']:
+            vals = {
+                'type': journal_type,
+                'name': journal_names[journal_type],
+                'code': journal_codes[journal_type],
+                'company_id': company_id,
+                'centralisation': journal_type == 'situation',
+                'view_id': _get_view_id(journal_type),
+                'analytic_journal_id': _get_analytic_journal(journal_type),
+                'default_credit_account_id': _get_default_account(journal_type, 'credit'),
+                'default_debit_account_id': _get_default_account(journal_type, 'debit'),
+            }
+            journal_data.append(vals)
+        return journal_data
+
+    def generate_properties(self, cr, uid, chart_template_id, acc_template_ref, company_id, context=None):
+        """
+        This method used for creating properties.
+
+        :param chart_template_id: id of the current chart template for which we need to create properties
+        :param acc_template_ref: Mapping between ids of account templates and real accounts created from them
+        :param company_id: company_id selected from wizard.multi.charts.accounts.
+        :returns: True
+        """
+        property_obj = self.pool.get('ir.property')
+        field_obj = self.pool.get('ir.model.fields')
+        todo_list = [
+            ('property_account_receivable','res.partner','account.account'),
+            ('property_account_payable','res.partner','account.account'),
+            ('property_account_expense_categ','product.category','account.account'),
+            ('property_account_income_categ','product.category','account.account'),
+            ('property_account_expense','product.template','account.account'),
+            ('property_account_income','product.template','account.account'),
+            ('property_reserve_and_surplus_account','res.company','account.account')
+        ]
+        template = self.pool.get('account.chart.template').browse(cr, uid, chart_template_id, context=context)
+        for record in todo_list:
+            account = getattr(template, record[0])
+            value = account and 'account.account,' + str(acc_template_ref[account.id]) or False
+            if value:
+                field = field_obj.search(cr, uid, [('name', '=', record[0]),('model', '=', record[1]),('relation', '=', record[2])], context=context)
+                vals = {
+                    'name': record[0],
+                    'company_id': company_id,
+                    'fields_id': field[0],
+                    'value': value,
+                }
+                property_ids = property_obj.search(cr, uid, [('name','=', record[0]),('company_id', '=', company_id)], context=context)
+                if property_ids:
+                    #the property exist: modify it
+                    property_obj.write(cr, uid, property_ids, vals, context=context)
+                else:
+                    #create the property
+                    property_obj.create(cr, uid, vals, context=context)
+        return True
+
+    def _install_template(self, cr, uid, template_id, company_id, code_digits=None, obj_wizard=None, acc_ref={}, taxes_ref={}, tax_code_ref={}, context=None):
+        '''
+        This function recursively loads the template objects and create the real objects from them.
+
+        :param template_id: id of the chart template to load
+        :param company_id: id of the company the wizard is running for
+        :param code_digits: integer that depicts the number of digits the accounts code should have in the COA
+        :param obj_wizard: the current wizard for generating the COA from the templates
+        :param acc_ref: Mapping between ids of account templates and real accounts created from them
+        :param taxes_ref: Mapping between ids of tax templates and real taxes created from them
+        :param tax_code_ref: Mapping between ids of tax code templates and real tax codes created from them
+        :returns: return a tuple with a dictionary containing
+            * the mapping between the account template ids and the ids of the real accounts that have been generated
+              from them, as first item,
+            * a similar dictionary for mapping the tax templates and taxes, as second item,
+            * a last identical containing the mapping of tax code templates and tax codes
+        :rtype: tuple(dict, dict, dict)
+        '''
+        template = self.pool.get('account.chart.template').browse(cr, uid, template_id, context=context)
+        if template.parent_id:
+            tmp1, tmp2, tmp3 = self._install_template(cr, uid, template.parent_id.id, company_id, code_digits=code_digits, acc_ref=acc_ref, taxes_ref=taxes_ref, tax_code_ref=tax_code_ref, context=context)
+            acc_ref.update(tmp1)
+            taxes_ref.update(tmp2)
+            tax_code_ref.update(tmp3)
+        tmp1, tmp2, tmp3 = self._load_template(cr, uid, template_id, company_id, code_digits=code_digits, obj_wizard=obj_wizard, account_ref=acc_ref, taxes_ref=taxes_ref, tax_code_ref=tax_code_ref, context=context)
+        acc_ref.update(tmp1)
+        taxes_ref.update(tmp2)
+        tax_code_ref.update(tmp3)
+        return acc_ref, taxes_ref, tax_code_ref
+
+    def _load_template(self, cr, uid, template_id, company_id, code_digits=None, obj_wizard=None, account_ref={}, taxes_ref={}, tax_code_ref={}, context=None):
+        '''
+        This function generates all the objects from the templates
+
+        :param template_id: id of the chart template to load
+        :param company_id: id of the company the wizard is running for
+        :param code_digits: integer that depicts the number of digits the accounts code should have in the COA
+        :param obj_wizard: the current wizard for generating the COA from the templates
+        :param acc_ref: Mapping between ids of account templates and real accounts created from them
+        :param taxes_ref: Mapping between ids of tax templates and real taxes created from them
+        :param tax_code_ref: Mapping between ids of tax code templates and real tax codes created from them
+        :returns: return a tuple with a dictionary containing
+            * the mapping between the account template ids and the ids of the real accounts that have been generated
+              from them, as first item,
+            * a similar dictionary for mapping the tax templates and taxes, as second item,
+            * a last identical containing the mapping of tax code templates and tax codes
+        :rtype: tuple(dict, dict, dict)
+        '''
+        template = self.pool.get('account.chart.template').browse(cr, uid, template_id, context=context)
+        obj_tax_code_template = self.pool.get('account.tax.code.template')
+        obj_acc_tax = self.pool.get('account.tax')
+        obj_tax_temp = self.pool.get('account.tax.template')
+        obj_acc_template = self.pool.get('account.account.template')
+        obj_fiscal_position_template = self.pool.get('account.fiscal.position.template')
+
+        # create all the tax code.
+        tax_code_ref.update(obj_tax_code_template.generate_tax_code(cr, uid, template.tax_code_root_id.id, company_id, context=context))
+
+        # Generate taxes from templates.
+        tax_templates = [x for x in template.tax_template_ids]
+        generated_tax_res = obj_tax_temp._generate_tax(cr, uid, tax_templates, tax_code_ref, company_id, context=context)
+        taxes_ref.update(generated_tax_res['tax_template_to_tax'])
+
+        # Generating Accounts from templates.
+        account_template_ref = obj_acc_template.generate_account(cr, uid, template_id, taxes_ref, account_ref, code_digits, company_id, context=context)
+        account_ref.update(account_template_ref)
+
+        # writing account values on tax after creation of accounts
+        for key,value in generated_tax_res['account_dict'].items():
+            if value['account_collected_id'] or value['account_paid_id']:
+                obj_acc_tax.write(cr, uid, [key], {
+                    'account_collected_id': account_ref.get(value['account_collected_id'], False),
+                    'account_paid_id': account_ref.get(value['account_paid_id'], False),
+                })
+
+        # Create Journals
+        self.generate_journals(cr, uid, template_id, account_ref, company_id, context=context)
+
+        # generate properties function
+        self.generate_properties(cr, uid, template_id, account_ref, company_id, context=context)
+
+        # Generate Fiscal Position , Fiscal Position Accounts and Fiscal Position Taxes from templates
+        obj_fiscal_position_template.generate_fiscal_position(cr, uid, template_id, taxes_ref, account_ref, company_id, context=context)
+
+        return account_ref, taxes_ref, tax_code_ref
+
+    def _create_tax_templates_from_rates(self, cr, uid, obj_wizard, company_id, context=None):
+        '''
+        This function checks if the chosen chart template is configured as containing a full set of taxes, and if
+        it's not the case, it creates the templates for account.tax.code and for account.account.tax objects accordingly
+        to the provided sale/purchase rates. Then it saves the new tax templates as default taxes to use for this chart
+        template.
+
+        :param obj_wizard: browse record of wizard to generate COA from templates
+        :param company_id: id of the company for wich the wizard is running
+        :return: True
+        '''
+        obj_tax_code_template = self.pool.get('account.tax.code.template')
+        obj_tax_temp = self.pool.get('account.tax.template')
+        chart_template = obj_wizard.chart_template_id
+        vals = {}
+        # get the ids of all the parents of the selected account chart template
+        current_chart_template = chart_template
+        all_parents = [current_chart_template.id]
+        while current_chart_template.parent_id:
+            current_chart_template = current_chart_template.parent_id
+            all_parents.append(current_chart_template.id)
+        # create tax templates and tax code templates from purchase_tax_rate and sale_tax_rate fields
+        if not chart_template.complete_tax_set:
+            value = obj_wizard.sale_tax_rate
+            ref_tax_ids = obj_tax_temp.search(cr, uid, [('type_tax_use','in', ('sale','all')), ('chart_template_id', 'in', all_parents)], context=context, order="sequence, id desc", limit=1)
+            obj_tax_temp.write(cr, uid, ref_tax_ids, {'amount': value/100.0, 'name': _('Tax %.2f%%') % value})
+            value = obj_wizard.purchase_tax_rate
+            ref_tax_ids = obj_tax_temp.search(cr, uid, [('type_tax_use','in', ('purchase','all')), ('chart_template_id', 'in', all_parents)], context=context, order="sequence, id desc", limit=1)
+            obj_tax_temp.write(cr, uid, ref_tax_ids, {'amount': value/100.0, 'name': _('Purchase Tax %.2f%%') % value})
+        return True
+
+    def execute(self, cr, uid, ids, context=None):
+        '''
+        This function is called at the confirmation of the wizard to generate the COA from the templates. It will read
+        all the provided information to create the accounts, the banks, the journals, the taxes, the tax codes, the
+        accounting properties... accordingly for the chosen company.
+        '''
+        ir_values_obj = self.pool.get('ir.values')
+        obj_wizard = self.browse(cr, uid, ids[0])
+        company_id = obj_wizard.company_id.id
+        # If the floats for sale/purchase rates have been filled, create templates from them
+        self._create_tax_templates_from_rates(cr, uid, obj_wizard, company_id, context=context)
+
+        # Install all the templates objects and generate the real objects
+        acc_template_ref, taxes_ref, tax_code_ref = self._install_template(cr, uid, obj_wizard.chart_template_id.id, company_id, code_digits=obj_wizard.code_digits, obj_wizard=obj_wizard, context=context)
+
+        # write values of default taxes for product
+        if obj_wizard.sale_tax and taxes_ref:
+            ir_values_obj.set(cr, 1, key='default', key2=False, name="taxes_id", company=company_id,
+                                models =[('product.product',False)], value=[taxes_ref[obj_wizard.sale_tax.id]])
+        if obj_wizard.purchase_tax and taxes_ref:
+            ir_values_obj.set(cr, 1, key='default', key2=False, name="supplier_taxes_id", company=company_id,
+                                models =[('product.product',False)], value=[taxes_ref[obj_wizard.purchase_tax.id]])
+
+        # Create Bank journals
+        self._create_bank_journals_from_o2m(cr, uid, obj_wizard, company_id, acc_template_ref, context=context)
+        action = {
+            'type': 'ir.actions.act_window',
+            'view_type': 'form',
+            'view_mode': 'form',
+            'res_model': 'board.board',
+            'view_id': self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account', 'board_account_form')[1],
+            'menu_id': self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account', 'menu_finance')[1]
+        }
+        return action
+
+    def _prepare_bank_journal(self, cr, uid, line, current_num, default_account_id, company_id, context=None):
+        '''
+        This function prepares the value to use for the creation of a bank journal created through the wizard of 
+        generating COA from templates.
+
+        :param line: dictionary containing the values encoded by the user related to his bank account
+        :param current_num: integer corresponding to a counter of the already created bank journals through this wizard.
+        :param default_account_id: id of the default debit.credit account created before for this journal.
+        :param company_id: id of the company for which the wizard is running
+        :return: mapping of field names and values
+        :rtype: dict
+        '''
+        obj_data = self.pool.get('ir.model.data')
+        obj_journal = self.pool.get('account.journal')
+        # Get the id of journal views
+        tmp = obj_data.get_object_reference(cr, uid, 'account', 'account_journal_bank_view_multi')
+        view_id_cur = tmp and tmp[1] or False
+        tmp = obj_data.get_object_reference(cr, uid, 'account', 'account_journal_bank_view')
+        view_id_cash = tmp and tmp[1] or False
+
+        # we need to loop again to find next number for journal code 
+        # because we can't rely on the value current_num as,
+        # its possible that we already have bank journals created (e.g. by the creation of res.partner.bank) 
+        # and the next number for account code might have been already used before for journal
+        for num in xrange(current_num, 100):
+            # journal_code has a maximal size of 5, hence we can enforce the boundary num < 100
+            journal_code = _('BNK')[:3] + str(num)
+            ids = obj_journal.search(cr, uid, [('code', '=', journal_code), ('company_id', '=', company_id)], context=context)
+            if not ids:
+                break
+        else:
+            raise osv.except_osv(_('Error'), _('Cannot generate an unused journal code.'))
+
+        vals = {
+                'name': line['acc_name'],
+                'code': journal_code,
+                'type': line['account_type'] == 'cash' and 'cash' or 'bank',
+                'company_id': company_id,
+                'analytic_journal_id': False,
+                'currency': False,
+                'default_credit_account_id': default_account_id,
+                'default_debit_account_id': default_account_id,
+        }
+        if line['currency_id']:
+            vals['view_id'] = view_id_cur
+            vals['currency'] = line['currency_id']
+        else:
+            vals['view_id'] = view_id_cash
+        return vals
+
+    def _prepare_bank_account(self, cr, uid, line, new_code, acc_template_ref, ref_acc_bank, company_id, context=None):
+        '''
+        This function prepares the value to use for the creation of the default debit and credit accounts of a
+        bank journal created through the wizard of generating COA from templates.
+
+        :param line: dictionary containing the values encoded by the user related to his bank account
+        :param new_code: integer corresponding to the next available number to use as account code
+        :param acc_template_ref: the dictionary containing the mapping between the ids of account templates and the ids
+            of the accounts that have been generated from them.
+        :param ref_acc_bank: browse record of the account template set as root of all bank accounts for the chosen
+            template
+        :param company_id: id of the company for which the wizard is running
+        :return: mapping of field names and values
+        :rtype: dict
+        '''
+        obj_data = self.pool.get('ir.model.data')
+
+        # Get the id of the user types fr-or cash and bank
+        tmp = obj_data.get_object_reference(cr, uid, 'account', 'data_account_type_cash')
+        cash_type = tmp and tmp[1] or False
+        tmp = obj_data.get_object_reference(cr, uid, 'account', 'data_account_type_bank')
+        bank_type = tmp and tmp[1] or False
+        return {
+                'name': line['acc_name'],
+                'currency_id': line['currency_id'],
+                'code': new_code,
+                'type': 'liquidity',
+                'user_type': line['account_type'] == 'cash' and cash_type or bank_type,
+                'parent_id': acc_template_ref[ref_acc_bank.id] or False,
+                'company_id': company_id,
+        }
+
+    def _create_bank_journals_from_o2m(self, cr, uid, obj_wizard, company_id, acc_template_ref, context=None):
+        '''
+        This function creates bank journals and its accounts for each line encoded in the field bank_accounts_id of the
+        wizard.
+
+        :param obj_wizard: the current wizard that generates the COA from the templates.
+        :param company_id: the id of the company for which the wizard is running.
+        :param acc_template_ref: the dictionary containing the mapping between the ids of account templates and the ids
+            of the accounts that have been generated from them.
+        :return: True
+        '''
+        obj_acc = self.pool.get('account.account')
+        obj_journal = self.pool.get('account.journal')
+        code_digits = obj_wizard.code_digits
+
+        # Build a list with all the data to process
+        journal_data = []
+        if obj_wizard.bank_accounts_id:
+            for acc in obj_wizard.bank_accounts_id:
+                vals = {
+                    'acc_name': acc.acc_name,
+                    'account_type': acc.account_type,
+                    'currency_id': acc.currency_id.id,
+                }
+                journal_data.append(vals)
+        ref_acc_bank = obj_wizard.chart_template_id.bank_account_view_id
+        if journal_data and not ref_acc_bank.code:
+            raise osv.except_osv(_('Configuration Error !'), _('The bank account defined on the selected chart of accounts hasn\'t a code.'))
+
+        current_num = 1
+        for line in journal_data:
+            # Seek the next available number for the account code
+            while True:
+                new_code = str(ref_acc_bank.code.ljust(code_digits-len(str(current_num)), '0')) + str(current_num)
+                ids = obj_acc.search(cr, uid, [('code', '=', new_code), ('company_id', '=', company_id)])
+                if not ids:
+                    break
+                else:
+                    current_num += 1
+            # Create the default debit/credit accounts for this bank journal
+            vals = self._prepare_bank_account(cr, uid, line, new_code, acc_template_ref, ref_acc_bank, company_id, context=context)
+            default_account_id  = obj_acc.create(cr, uid, vals, context=context)
+
+            #create the bank journal
+            vals_journal = self._prepare_bank_journal(cr, uid, line, current_num, default_account_id, company_id, context=context)
+            obj_journal.create(cr, uid, vals_journal)
+            current_num += 1
+        return True
+
+wizard_multi_charts_accounts()
+
+class account_bank_accounts_wizard(osv.osv_memory):
+    _name='account.bank.accounts.wizard'
+
+    _columns = {
+        'acc_name': fields.char('Account Name.', size=64, required=True),
+        'bank_account_id': fields.many2one('wizard.multi.charts.accounts', 'Bank Account', required=True),
+        'currency_id': fields.many2one('res.currency', 'Secondary Currency', help="Forces all moves for this account to have this secondary currency."),
+        'account_type': fields.selection([('cash','Cash'), ('check','Check'), ('bank','Bank')], 'Account Type', size=32),
+    }
+
+account_bank_accounts_wizard()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'account/account_analytic_line.py'
--- account/account_analytic_line.py	1970-01-01 00:00:00 +0000
+++ account/account_analytic_line.py	2013-06-05 16:26:39 +0000
@@ -0,0 +1,160 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
+#
+#    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/>.
+#
+##############################################################################
+
+from osv import fields
+from osv import osv
+from tools.translate import _
+
+class account_analytic_line(osv.osv):
+    _inherit = 'account.analytic.line'
+    _description = 'Analytic Line'
+    _columns = {
+        'product_uom_id': fields.many2one('product.uom', 'UoM'),
+        'product_id': fields.many2one('product.product', 'Product'),
+        'general_account_id': fields.many2one('account.account', 'General Account', required=True, ondelete='restrict'),
+        'move_id': fields.many2one('account.move.line', 'Move Line', ondelete='cascade', select=True),
+        'journal_id': fields.many2one('account.analytic.journal', 'Analytic Journal', required=True, ondelete='restrict', select=True),
+        'code': fields.char('Code', size=8),
+        'ref': fields.char('Ref.', size=64),
+        'currency_id': fields.related('move_id', 'currency_id', type='many2one', relation='res.currency', string='Account currency', store=True, help="The related account currency if not equal to the company one.", readonly=True),
+        'amount_currency': fields.related('move_id', 'amount_currency', type='float', string='Amount currency', store=True, help="The amount expressed in the related account currency if not equal to the company one.", readonly=True),
+    }
+
+    _defaults = {
+        'date': fields.date.context_today,
+        'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.analytic.line', context=c),
+    }
+    _order = 'date desc'
+
+    def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
+        if context is None:
+            context = {}
+        if context.get('from_date',False):
+            args.append(['date', '>=', context['from_date']])
+        if context.get('to_date',False):
+            args.append(['date','<=', context['to_date']])
+        return super(account_analytic_line, self).search(cr, uid, args, offset, limit,
+                order, context=context, count=count)
+
+    def _check_company(self, cr, uid, ids, context=None):
+        lines = self.browse(cr, uid, ids, context=context)
+        for l in lines:
+            if l.move_id and not l.account_id.company_id.id == l.move_id.account_id.company_id.id:
+                return False
+        return True
+
+    # Compute the cost based on the price type define into company
+    # property_valuation_price_type property
+    def on_change_unit_amount(self, cr, uid, id, prod_id, quantity, company_id,
+            unit=False, journal_id=False, context=None):
+        if context==None:
+            context={}
+        if not journal_id:
+            j_ids = self.pool.get('account.analytic.journal').search(cr, uid, [('type','=','purchase')])
+            journal_id = j_ids and j_ids[0] or False
+        if not journal_id or not prod_id:
+            return {}
+        product_obj = self.pool.get('product.product')
+        analytic_journal_obj =self.pool.get('account.analytic.journal')
+        product_price_type_obj = self.pool.get('product.price.type')
+        j_id = analytic_journal_obj.browse(cr, uid, journal_id, context=context)
+        prod = product_obj.browse(cr, uid, prod_id, context=context)
+        result = 0.0
+        if prod_id:
+            unit = prod.uom_id.id
+            if j_id.type == 'purchase':
+                unit = prod.uom_po_id.id
+        if j_id.type <> 'sale':
+            a = prod.product_tmpl_id.property_account_expense.id
+            if not a:
+                a = prod.categ_id.property_account_expense_categ.id
+            if not a:
+                raise osv.except_osv(_('Error !'),
+                        _('There is no expense account defined ' \
+                                'for this product: "%s" (id:%d)') % \
+                                (prod.name, prod.id,))
+        else:
+            a = prod.product_tmpl_id.property_account_income.id
+            if not a:
+                a = prod.categ_id.property_account_income_categ.id
+            if not a:
+                raise osv.except_osv(_('Error !'),
+                        _('There is no income account defined ' \
+                                'for this product: "%s" (id:%d)') % \
+                                (prod.name, prod_id,))
+
+        flag = False
+        # Compute based on pricetype
+        product_price_type_ids = product_price_type_obj.search(cr, uid, [('field','=','standard_price')], context=context)
+        pricetype = product_price_type_obj.browse(cr, uid, product_price_type_ids, context=context)[0]
+        if journal_id:
+            journal = analytic_journal_obj.browse(cr, uid, journal_id, context=context)
+            if journal.type == 'sale':
+                product_price_type_ids = product_price_type_obj.search(cr, uid, [('field','=','list_price')], context)
+                if product_price_type_ids:
+                    pricetype = product_price_type_obj.browse(cr, uid, product_price_type_ids, context=context)[0]
+        # Take the company currency as the reference one
+        if pricetype.field == 'list_price':
+            flag = True
+        ctx = context.copy()
+        if unit:
+            # price_get() will respect a 'uom' in its context, in order
+            # to return a default price for those units
+            ctx['uom'] = unit
+        amount_unit = prod.price_get(pricetype.field, context=ctx)[prod.id]
+        prec = self.pool.get('decimal.precision').precision_get(cr, uid, 'Account')
+        amount = amount_unit * quantity or 0.0
+        result = round(amount, prec)
+        if not flag:
+            result *= -1
+        return {'value': {
+            'amount': result,
+            'general_account_id': a,
+            'product_uom_id': unit
+            }
+        }
+
+    def view_header_get(self, cr, user, view_id, view_type, context=None):
+        if context is None:
+            context = {}
+        if context.get('account_id', False):
+            # account_id in context may also be pointing to an account.account.id
+            cr.execute('select name from account_analytic_account where id=%s', (context['account_id'],))
+            res = cr.fetchone()
+            if res:
+                res = _('Entries: ')+ (res[0] or '')
+            return res
+        return False
+
+account_analytic_line()
+
+class res_partner(osv.osv):
+    """ Inherits partner and adds contract information in the partner form """
+    _inherit = 'res.partner'
+
+    _columns = {
+        'contract_ids': fields.one2many('account.analytic.account', \
+                                                    'partner_id', 'Contracts', readonly=True),
+    }
+
+res_partner()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'account/account_assert_test.xml'
--- account/account_assert_test.xml	1970-01-01 00:00:00 +0000
+++ account/account_assert_test.xml	2013-06-05 16:26:39 +0000
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+    <data>
+        <assert model="account.move" search="[]" string="For all Journal Items, the state is valid implies that the sum of credits equals the sum of debits">
+            <test expr="not len(line_id) or line_id[0].state != 'valid' or (sum([l.debit - l.credit for l in line_id]) &lt;= 0.00001)"/>
+        </assert>
+    </data>
+</openerp>

=== added file 'account/account_bank.py'
--- account/account_bank.py	1970-01-01 00:00:00 +0000
+++ account/account_bank.py	2013-06-05 16:26:39 +0000
@@ -0,0 +1,112 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
+#
+#    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/>.
+#
+##############################################################################
+
+from tools.translate import _
+from osv import fields, osv
+
+class bank(osv.osv):
+    _inherit = "res.partner.bank"
+    _columns = {
+        'journal_id': fields.many2one('account.journal', 'Account Journal', help="This journal will be created automatically for this bank account when you save the record"),
+        'currency_id': fields.related('journal_id', 'currency', type="many2one", relation='res.currency', readonly=True,
+            string="Currency", help="Currency of the related account journal."),
+    }
+    def create(self, cr, uid, data, context={}):
+        result = super(bank, self).create(cr, uid, data, context=context)
+        self.post_write(cr, uid, [result], context=context)
+        return result
+
+    def write(self, cr, uid, ids, data, context={}):
+        result = super(bank, self).write(cr, uid, ids, data, context=context)
+        self.post_write(cr, uid, ids, context=context)
+        return result
+
+    def _prepare_name(self, bank):
+        "Return the name to use when creating a bank journal"
+        return (bank.bank_name or '') + ' ' + bank.acc_number
+
+    def post_write(self, cr, uid, ids, context={}):
+        if isinstance(ids, (int, long)):
+          ids = [ids]
+
+        obj_acc = self.pool.get('account.account')
+        obj_data = self.pool.get('ir.model.data')
+
+        for bank in self.browse(cr, uid, ids, context):
+            if bank.company_id and not bank.journal_id:
+                # Find the code and parent of the bank account to create
+                dig = 6
+                current_num = 1
+                ids = obj_acc.search(cr, uid, [('type','=','liquidity'), ('company_id', '=', bank.company_id.id)], context=context)
+                # No liquidity account exists, no template available
+                if not ids: continue
+
+                ref_acc_bank_temp = obj_acc.browse(cr, uid, ids[0], context=context)
+                ref_acc_bank = ref_acc_bank_temp.parent_id
+                while True:
+                    new_code = str(ref_acc_bank.code.ljust(dig-len(str(current_num)), '0')) + str(current_num)
+                    ids = obj_acc.search(cr, uid, [('code', '=', new_code), ('company_id', '=', bank.company_id.id)])
+                    if not ids:
+                        break
+                    current_num += 1
+                name = self._prepare_name(bank)
+                acc = {
+                    'name': name,
+                    'code': new_code,
+                    'type': 'liquidity',
+                    'user_type': ref_acc_bank_temp.user_type.id,
+                    'reconcile': False,
+                    'parent_id': ref_acc_bank.id,
+                    'company_id': bank.company_id.id,
+                }
+                acc_bank_id  = obj_acc.create(cr,uid,acc,context=context)
+
+                # Get the journal view id
+                data_id = obj_data.search(cr, uid, [('model','=','account.journal.view'), ('name','=','account_journal_bank_view')])
+                data = obj_data.browse(cr, uid, data_id[0], context=context)
+                view_id_cash = data.res_id
+
+                jour_obj = self.pool.get('account.journal')
+                new_code = 1
+                while True:
+                    code = _('BNK')+str(new_code)
+                    ids = jour_obj.search(cr, uid, [('code','=',code)], context=context)
+                    if not ids:
+                        break
+                    new_code += 1
+
+                #create the bank journal
+                vals_journal = {
+                    'name': name,
+                    'code': code,
+                    'type': 'bank',
+                    'company_id': bank.company_id.id,
+                    'analytic_journal_id': False,
+                    'default_credit_account_id': acc_bank_id,
+                    'default_debit_account_id': acc_bank_id,
+                    'view_id': view_id_cash
+                }
+                journal_id = jour_obj.create(cr, uid, vals_journal, context=context)
+
+                self.write(cr, uid, [bank.id], {'journal_id': journal_id}, context=context)
+        return True
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'account/account_bank_statement.py'
--- account/account_bank_statement.py	1970-01-01 00:00:00 +0000
+++ account/account_bank_statement.py	2013-06-05 16:26:39 +0000
@@ -0,0 +1,493 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
+#
+#    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/>.
+#
+##############################################################################
+
+import time
+
+from osv import fields, osv
+from tools.translate import _
+import decimal_precision as dp
+
+class account_bank_statement(osv.osv):
+
+    def create(self, cr, uid, vals, context=None):
+        seq = 0
+        if 'line_ids' in vals:
+            new_line_ids = []
+            for line in vals['line_ids']:
+                seq += 1
+                line[2]['sequence'] = seq
+        return super(account_bank_statement, self).create(cr, uid, vals, context=context)
+
+    def write(self, cr, uid, ids, vals, context=None):
+        res = super(account_bank_statement, self).write(cr, uid, ids, vals, context=context)
+        account_bank_statement_line_obj = self.pool.get('account.bank.statement.line')
+        for statement in self.browse(cr, uid, ids, context):
+            seq = 0
+            for line in statement.line_ids:
+                seq += 1
+                account_bank_statement_line_obj.write(cr, uid, [line.id], {'sequence': seq}, context=context)
+        return res
+
+    def _default_journal_id(self, cr, uid, context=None):
+        if context is None:
+            context = {}
+        journal_pool = self.pool.get('account.journal')
+        journal_type = context.get('journal_type', False)
+        journal_id = False
+        company_id = self.pool.get('res.company')._company_default_get(cr, uid, 'account.bank.statement',context=context)
+        if journal_type:
+            ids = journal_pool.search(cr, uid, [('type', '=', journal_type),('company_id','=',company_id)])
+            if ids:
+                journal_id = ids[0]
+        return journal_id
+
+    def _end_balance(self, cursor, user, ids, name, attr, context=None):
+        res_currency_obj = self.pool.get('res.currency')
+        res_users_obj = self.pool.get('res.users')
+        res = {}
+
+        company_currency_id = res_users_obj.browse(cursor, user, user,
+                context=context).company_id.currency_id.id
+
+        statements = self.browse(cursor, user, ids, context=context)
+        for statement in statements:
+            res[statement.id] = statement.balance_start
+            currency_id = statement.currency.id
+            for line in statement.move_line_ids:
+                if line.debit > 0:
+                    if line.account_id.id == \
+                            statement.journal_id.default_debit_account_id.id:
+                        res[statement.id] += res_currency_obj.compute(cursor,
+                                user, company_currency_id, currency_id,
+                                line.debit, context=context)
+                else:
+                    if line.account_id.id == \
+                            statement.journal_id.default_credit_account_id.id:
+                        res[statement.id] -= res_currency_obj.compute(cursor,
+                                user, company_currency_id, currency_id,
+                                line.credit, context=context)
+
+            if statement.state in ('draft', 'open'):
+                for line in statement.line_ids:
+                    res[statement.id] += line.amount
+        for r in res:
+            res[r] = round(res[r], 2)
+        return res
+
+    def _get_period(self, cr, uid, context=None):
+        periods = self.pool.get('account.period').find(cr, uid)
+        if periods:
+            return periods[0]
+        return False
+
+    def _currency(self, cursor, user, ids, name, args, context=None):
+        res = {}
+        res_currency_obj = self.pool.get('res.currency')
+        res_users_obj = self.pool.get('res.users')
+        default_currency = res_users_obj.browse(cursor, user,
+                user, context=context).company_id.currency_id
+        for statement in self.browse(cursor, user, ids, context=context):
+            currency = statement.journal_id.currency
+            if not currency:
+                currency = default_currency
+            res[statement.id] = currency.id
+        currency_names = {}
+        for currency_id, currency_name in res_currency_obj.name_get(cursor,
+                user, [x for x in res.values()], context=context):
+            currency_names[currency_id] = currency_name
+        for statement_id in res.keys():
+            currency_id = res[statement_id]
+            res[statement_id] = (currency_id, currency_names[currency_id])
+        return res
+
+    def _get_statement(self, cr, uid, ids, context=None):
+        result = {}
+        for line in self.pool.get('account.bank.statement.line').browse(cr, uid, ids, context=context):
+            result[line.statement_id.id] = True
+        return result.keys()
+
+    _order = "date desc, id desc"
+    _name = "account.bank.statement"
+    _description = "Bank Statement"
+    _columns = {
+        'name': fields.char('Name', size=64, required=True, states={'draft': [('readonly', False)]}, readonly=True, help='if you give the Name other then /, its created Accounting Entries Move will be with same name as statement name. This allows the statement entries to have the same references than the statement itself'), # readonly for account_cash_statement
+        'date': fields.date('Date', required=True, states={'confirm': [('readonly', True)]}, select=True),
+        'journal_id': fields.many2one('account.journal', 'Journal', required=True,
+            readonly=True, states={'draft':[('readonly',False)]}),
+        'period_id': fields.many2one('account.period', 'Period', required=True,
+            states={'confirm':[('readonly', True)]}),
+        'balance_start': fields.float('Starting Balance', digits_compute=dp.get_precision('Account'),
+            states={'confirm':[('readonly',True)]}),
+        'balance_end_real': fields.float('Ending Balance', digits_compute=dp.get_precision('Account'),
+            states={'confirm': [('readonly', True)]}),
+        'balance_end': fields.function(_end_balance,
+            store = {
+                'account.bank.statement': (lambda self, cr, uid, ids, c={}: ids, ['line_ids','move_line_ids'], 10),
+                'account.bank.statement.line': (_get_statement, ['amount'], 10),
+            },
+            string="Computed Balance", help='Balance as calculated based on Starting Balance and transaction lines'),
+        'company_id': fields.related('journal_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True),
+        'line_ids': fields.one2many('account.bank.statement.line',
+            'statement_id', 'Statement lines',
+            states={'confirm':[('readonly', True)]}),
+        'move_line_ids': fields.one2many('account.move.line', 'statement_id',
+            'Entry lines', states={'confirm':[('readonly',True)]}),
+        'state': fields.selection([('draft', 'New'),
+                                   ('open','Open'), # used by cash statements
+                                   ('confirm', 'Closed')],
+                                   'State', required=True, readonly="1",
+                                   help='When new statement is created the state will be \'Draft\'.\n'
+                                        'And after getting confirmation from the bank it will be in \'Confirmed\' state.'),
+        'currency': fields.function(_currency, string='Currency',
+            type='many2one', relation='res.currency'),
+        'account_id': fields.related('journal_id', 'default_debit_account_id', type='many2one', relation='account.account', string='Account used in this journal', readonly=True, help='used in statement reconciliation domain, but shouldn\'t be used elswhere.'),
+    }
+
+    _defaults = {
+        'name': "/",
+        'date': fields.date.context_today,
+        'state': 'draft',
+        'journal_id': _default_journal_id,
+        'period_id': _get_period,
+        'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.bank.statement',context=c),
+    }
+
+    def _check_company_id(self, cr, uid, ids, context=None):
+        for statement in self.browse(cr, uid, ids, context=context):
+            if statement.company_id.id != statement.period_id.company_id.id:
+                return False
+        return True
+
+    _constraints = [
+        (_check_company_id, 'The journal and period chosen have to belong to the same company.', ['journal_id','period_id']),
+    ]
+
+    def onchange_date(self, cr, uid, ids, date, company_id, context=None):
+        """
+            Find the correct period to use for the given date and company_id, return it and set it in the context
+        """
+        res = {}
+        period_pool = self.pool.get('account.period')
+
+        if context is None:
+            context = {}
+        ctx = context.copy()
+        ctx.update({'company_id': company_id})
+        pids = period_pool.find(cr, uid, dt=date, context=ctx)
+        if pids:
+            res.update({'period_id': pids[0]})
+            context.update({'period_id': pids[0]})
+
+        return {
+            'value':res,
+            'context':context,
+        }
+
+    def button_dummy(self, cr, uid, ids, context=None):
+        return self.write(cr, uid, ids, {}, context=context)
+
+    def create_move_from_st_line(self, cr, uid, st_line_id, company_currency_id, st_line_number, context=None):
+        if context is None:
+            context = {}
+        res_currency_obj = self.pool.get('res.currency')
+        account_move_obj = self.pool.get('account.move')
+        account_move_line_obj = self.pool.get('account.move.line')
+        account_bank_statement_line_obj = self.pool.get('account.bank.statement.line')
+        st_line = account_bank_statement_line_obj.browse(cr, uid, st_line_id, context=context)
+        st = st_line.statement_id
+
+        context.update({'date': st_line.date})
+
+        move_id = account_move_obj.create(cr, uid, {
+            'journal_id': st.journal_id.id,
+            'period_id': st.period_id.id,
+            'date': st_line.date,
+            'name': st_line_number,
+            'ref': st_line.ref,
+        }, context=context)
+        account_bank_statement_line_obj.write(cr, uid, [st_line.id], {
+            'move_ids': [(4, move_id, False)]
+        })
+
+        torec = []
+        if st_line.amount >= 0:
+            account_id = st.journal_id.default_credit_account_id.id
+        else:
+            account_id = st.journal_id.default_debit_account_id.id
+
+        acc_cur = ((st_line.amount<=0) and st.journal_id.default_debit_account_id) or st_line.account_id
+        context.update({
+                'res.currency.compute.account': acc_cur,
+            })
+        amount = res_currency_obj.compute(cr, uid, st.currency.id,
+                company_currency_id, st_line.amount, context=context)
+
+        val = {
+            'name': st_line.name,
+            'date': st_line.date,
+            'ref': st_line.ref,
+            'move_id': move_id,
+            'partner_id': ((st_line.partner_id) and st_line.partner_id.id) or False,
+            'account_id': (st_line.account_id) and st_line.account_id.id,
+            'credit': ((amount>0) and amount) or 0.0,
+            'debit': ((amount<0) and -amount) or 0.0,
+            'statement_id': st.id,
+            'journal_id': st.journal_id.id,
+            'period_id': st.period_id.id,
+            'currency_id': st.currency.id,
+            'analytic_account_id': st_line.analytic_account_id and st_line.analytic_account_id.id or False
+        }
+
+        if st.currency.id <> company_currency_id:
+            amount_cur = res_currency_obj.compute(cr, uid, company_currency_id,
+                        st.currency.id, amount, context=context)
+            val['amount_currency'] = -amount_cur
+
+        if st_line.account_id and st_line.account_id.currency_id and st_line.account_id.currency_id.id <> company_currency_id:
+            val['currency_id'] = st_line.account_id.currency_id.id
+            amount_cur = res_currency_obj.compute(cr, uid, company_currency_id,
+                    st_line.account_id.currency_id.id, amount, context=context)
+            val['amount_currency'] = -amount_cur
+
+        move_line_id = account_move_line_obj.create(cr, uid, val, context=context)
+        torec.append(move_line_id)
+
+        # Fill the secondary amount/currency
+        # if currency is not the same than the company
+        amount_currency = False
+        currency_id = False
+        if st.currency.id <> company_currency_id:
+            amount_currency = st_line.amount
+            currency_id = st.currency.id
+        account_move_line_obj.create(cr, uid, {
+            'name': st_line.name,
+            'date': st_line.date,
+            'ref': st_line.ref,
+            'move_id': move_id,
+            'partner_id': ((st_line.partner_id) and st_line.partner_id.id) or False,
+            'account_id': account_id,
+            'credit': ((amount < 0) and -amount) or 0.0,
+            'debit': ((amount > 0) and amount) or 0.0,
+            'statement_id': st.id,
+            'journal_id': st.journal_id.id,
+            'period_id': st.period_id.id,
+            'amount_currency': amount_currency,
+            'currency_id': currency_id,
+            }, context=context)
+
+        for line in account_move_line_obj.browse(cr, uid, [x.id for x in
+                account_move_obj.browse(cr, uid, move_id,
+                    context=context).line_id],
+                context=context):
+            if line.state <> 'valid':
+                raise osv.except_osv(_('Error !'),
+                        _('Journal item "%s" is not valid.') % line.name)
+
+        # Bank statements will not consider boolean on journal entry_posted
+        account_move_obj.post(cr, uid, [move_id], context=context)
+        return move_id
+
+    def get_next_st_line_number(self, cr, uid, st_number, st_line, context=None):
+        return st_number + '/' + str(st_line.sequence)
+
+    def balance_check(self, cr, uid, st_id, journal_type='bank', context=None):
+        st = self.browse(cr, uid, st_id, context=context)
+        if not ((abs((st.balance_end or 0.0) - st.balance_end_real) < 0.0001) or (abs((st.balance_end or 0.0) - st.balance_end_cash) < 0.0001)):
+            raise osv.except_osv(_('Error !'),
+                    _('The statement balance is incorrect !\nThe expected balance (%.2f) is different than the computed one. (%.2f)') % (st.balance_end_real, st.balance_end))
+        return True
+
+    def statement_close(self, cr, uid, ids, journal_type='bank', context=None):
+        return self.write(cr, uid, ids, {'state':'confirm'}, context=context)
+
+    def check_status_condition(self, cr, uid, state, journal_type='bank'):
+        return state in ('draft','open')
+
+    def button_confirm_bank(self, cr, uid, ids, context=None):
+        obj_seq = self.pool.get('ir.sequence')
+        if context is None:
+            context = {}
+
+        for st in self.browse(cr, uid, ids, context=context):
+            j_type = st.journal_id.type
+            company_currency_id = st.journal_id.company_id.currency_id.id
+            if not self.check_status_condition(cr, uid, st.state, journal_type=j_type):
+                continue
+
+            self.balance_check(cr, uid, st.id, journal_type=j_type, context=context)
+            if (not st.journal_id.default_credit_account_id) \
+                    or (not st.journal_id.default_debit_account_id):
+                raise osv.except_osv(_('Configuration Error !'),
+                        _('Please verify that an account is defined in the journal.'))
+
+            if not st.name == '/':
+                st_number = st.name
+            else:
+                c = {'fiscalyear_id': st.period_id.fiscalyear_id.id}
+                if st.journal_id.sequence_id:
+                    st_number = obj_seq.next_by_id(cr, uid, st.journal_id.sequence_id.id, context=c)
+                else:
+                    st_number = obj_seq.next_by_code(cr, uid, 'account.bank.statement', context=c)
+
+            for line in st.move_line_ids:
+                if line.state <> 'valid':
+                    raise osv.except_osv(_('Error !'),
+                            _('The account entries lines are not in valid state.'))
+            for st_line in st.line_ids:
+                if st_line.analytic_account_id:
+                    if not st.journal_id.analytic_journal_id:
+                        raise osv.except_osv(_('No Analytic Journal !'),_("You have to assign an analytic journal on the '%s' journal!") % (st.journal_id.name,))
+                if not st_line.amount:
+                    continue
+                st_line_number = self.get_next_st_line_number(cr, uid, st_number, st_line, context)
+                self.create_move_from_st_line(cr, uid, st_line.id, company_currency_id, st_line_number, context)
+
+            self.write(cr, uid, [st.id], {
+                    'name': st_number,
+                    'balance_end_real': st.balance_end
+            }, context=context)
+            self.log(cr, uid, st.id, _('Statement %s is confirmed, journal items are created.') % (st_number,))
+        return self.write(cr, uid, ids, {'state':'confirm'}, context=context)
+
+    def button_cancel(self, cr, uid, ids, context=None):
+        done = []
+        account_move_obj = self.pool.get('account.move')
+        for st in self.browse(cr, uid, ids, context=context):
+            if st.state=='draft':
+                continue
+            move_ids = []
+            for line in st.line_ids:
+                move_ids += [x.id for x in line.move_ids]
+            account_move_obj.button_cancel(cr, uid, move_ids, context=context)
+            account_move_obj.unlink(cr, uid, move_ids, context)
+            done.append(st.id)
+        return self.write(cr, uid, done, {'state':'draft'}, context=context)
+
+    def onchange_journal_id(self, cr, uid, statement_id, journal_id, context=None):
+        cr.execute('SELECT balance_end_real \
+                FROM account_bank_statement \
+                WHERE journal_id = %s AND NOT state = %s \
+                ORDER BY date DESC,id DESC LIMIT 1', (journal_id, 'draft'))
+        res = cr.fetchone()
+        balance_start = res and res[0] or 0.0
+        journal_data = self.pool.get('account.journal').read(cr, uid, journal_id, ['default_debit_account_id', 'company_id'], context=context)
+        account_id = journal_data['default_debit_account_id']
+        company_id = journal_data['company_id']
+        return {'value': {'balance_start': balance_start, 'account_id': account_id, 'company_id': company_id}}
+
+    def unlink(self, cr, uid, ids, context=None):
+        stat = self.read(cr, uid, ids, ['state'], context=context)
+        unlink_ids = []
+        for t in stat:
+            if t['state'] in ('draft'):
+                unlink_ids.append(t['id'])
+            else:
+                raise osv.except_osv(_('Invalid action !'), _('In order to delete a bank statement, you must first cancel it to delete related journal items.'))
+        osv.osv.unlink(self, cr, uid, unlink_ids, context=context)
+        return True
+
+    def copy(self, cr, uid, id, default=None, context=None):
+        if default is None:
+            default = {}
+        if context is None:
+            context = {}
+        default = default.copy()
+        default['move_line_ids'] = []
+        return super(account_bank_statement, self).copy(cr, uid, id, default, context=context)
+
+account_bank_statement()
+
+class account_bank_statement_line(osv.osv):
+
+    def onchange_partner_id(self, cr, uid, ids, partner_id, context=None):
+        obj_partner = self.pool.get('res.partner')
+        if context is None:
+            context = {}
+        if not partner_id:
+            return {}
+        part = obj_partner.browse(cr, uid, partner_id, context=context)
+        if not part.supplier and not part.customer:
+            type = 'general'
+        elif part.supplier and part.customer:
+            type = 'general'
+        else:
+            if part.supplier == True:
+                type = 'supplier'
+            if part.customer == True:
+                type = 'customer'
+        res_type = self.onchange_type(cr, uid, ids, partner_id=partner_id, type=type, context=context)
+        if res_type['value'] and res_type['value'].get('account_id', False):
+            return {'value': {'type': type, 'account_id': res_type['value']['account_id']}}
+        return {'value': {'type': type}}
+
+    def onchange_type(self, cr, uid, line_id, partner_id, type, context=None):
+        res = {'value': {}}
+        obj_partner = self.pool.get('res.partner')
+        if context is None:
+            context = {}
+        if not partner_id:
+            return res
+        account_id = False
+        line = self.browse(cr, uid, line_id, context=context)
+        if not line or (line and not line[0].account_id):
+            part = obj_partner.browse(cr, uid, partner_id, context=context)
+            if type == 'supplier':
+                account_id = part.property_account_payable.id
+            else:
+                account_id = part.property_account_receivable.id
+            res['value']['account_id'] = account_id
+        return res
+
+    _order = "statement_id desc, sequence"
+    _name = "account.bank.statement.line"
+    _description = "Bank Statement Line"
+    _columns = {
+        'name': fields.char('Communication', size=64, required=True),
+        'date': fields.date('Date', required=True),
+        'amount': fields.float('Amount', digits_compute=dp.get_precision('Account')),
+        'type': fields.selection([
+            ('supplier','Supplier'),
+            ('customer','Customer'),
+            ('general','General')
+            ], 'Type', required=True),
+        'partner_id': fields.many2one('res.partner', 'Partner'),
+        'account_id': fields.many2one('account.account','Account',
+            required=True),
+        'statement_id': fields.many2one('account.bank.statement', 'Statement',
+            select=True, required=True, ondelete='cascade'),
+        'analytic_account_id': fields.many2one('account.analytic.account', 'Analytic Account'),
+        'move_ids': fields.many2many('account.move',
+            'account_bank_statement_line_move_rel', 'statement_line_id','move_id',
+            'Moves'),
+        'ref': fields.char('Reference', size=32),
+        'note': fields.text('Notes'),
+        'sequence': fields.integer('Sequence', select=True, help="Gives the sequence order when displaying a list of bank statement lines."),
+        'company_id': fields.related('statement_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True),
+    }
+    _defaults = {
+        'name': lambda self,cr,uid,context={}: self.pool.get('ir.sequence').get(cr, uid, 'account.bank.statement.line'),
+        'date': lambda self,cr,uid,context={}: context.get('date', fields.date.context_today(self,cr,uid,context=context)),
+        'type': 'general',
+    }
+
+account_bank_statement_line()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'account/account_bank_view.xml'
--- account/account_bank_view.xml	1970-01-01 00:00:00 +0000
+++ account/account_bank_view.xml	2013-06-05 16:26:39 +0000
@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+<data>
+
+    <!--
+      Bank Accounts
+    -->
+
+    <record id="view_partner_bank_form_inherit" model="ir.ui.view">
+        <field name="name">Partner Bank Accounts - Journal</field>
+        <field name="model">res.partner.bank</field>
+        <field name="type">form</field>
+        <field name="inherit_id" ref="base.view_partner_bank_form"/>
+        <field name="arch" type="xml">
+            <group name="bank" position="after">
+                <group name="accounting" col="2" colspan="2" attrs="{'invisible': [('company_id','=', False)]}" groups="base.group_extended">
+                    <separator string="Accounting Information" colspan="2"/>
+                    <field name="journal_id"/>
+                    <field name="currency_id"/>
+                </group>
+            </group>
+        </field>
+    </record>
+
+
+    <record id="view_partner_bank_tree_add_currency" model="ir.ui.view">
+        <field name="name">Partner Bank Accounts - Add currency on tree</field>
+        <field name="model">res.partner.bank</field>
+        <field name="type">tree</field>
+        <field name="inherit_id" ref="base.view_partner_bank_tree"/>
+        <field name="arch" type="xml">
+            <field name="acc_number" position="after">
+                <field name="currency_id"/>
+            </field>
+        </field>
+    </record>
+
+
+    <record id="action_bank_tree" model="ir.actions.act_window">
+        <field name="name">Setup your Bank Accounts</field>
+        <field name="res_model">res.partner.bank</field>
+        <field name="view_type">form</field>
+        <field name="view_mode">tree,form</field>
+        <field name="context" eval="{'default_partner_id':ref('base.main_partner'), 'company_hide':False, 'default_company_id':ref('base.main_company'), 'search_default_my_bank':1}"/>
+        <field name="help">Configure your company's bank account and select those that must appear on the report footer. You can reorder banks in the list view. If you use the accounting application of OpenERP, journals and accounts will be created automatically based on these data.</field>
+    </record>
+    <menuitem
+        sequence="0"
+        parent="account.account_account_menu"
+        id="menu_action_bank_tree"
+        action="action_bank_tree"/>
+
+
+    <record id="account_configuration_bank_todo" model="ir.actions.todo">
+        <field name="action_id" ref="action_bank_tree"/>
+        <field name="category_id" ref="category_accounting_configuration"/>
+        <field name="sequence">4</field>
+    </record>
+
+
+</data>
+</openerp>

=== added file 'account/account_cash_statement.py'
--- account/account_cash_statement.py	1970-01-01 00:00:00 +0000
+++ account/account_cash_statement.py	2013-06-05 16:26:39 +0000
@@ -0,0 +1,346 @@
+# encoding: utf-8
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2008 PC Solutions (<http://pcsol.be>). All Rights Reserved
+#    $Id$
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU 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 General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+
+import time
+
+from osv import osv, fields
+from tools.translate import _
+import decimal_precision as dp
+
+class account_cashbox_line(osv.osv):
+
+    """ Cash Box Details """
+
+    _name = 'account.cashbox.line'
+    _description = 'CashBox Line'
+    _rec_name = 'number'
+
+    def _sub_total(self, cr, uid, ids, name, arg, context=None):
+
+        """ Calculates Sub total
+        @param name: Names of fields.
+        @param arg: User defined arguments
+        @return: Dictionary of values.
+        """
+        res = {}
+        for obj in self.browse(cr, uid, ids, context=context):
+            res[obj.id] = obj.pieces * obj.number
+        return res
+
+    def on_change_sub(self, cr, uid, ids, pieces, number, *a):
+
+        """ Calculates Sub total on change of number
+        @param pieces: Names of fields.
+        @param number:
+        """
+        sub = pieces * number
+        return {'value': {'subtotal': sub or 0.0}}
+
+    _columns = {
+        'pieces': fields.float('Values', digits_compute=dp.get_precision('Account')),
+        'number': fields.integer('Number'),
+        'subtotal': fields.function(_sub_total, string='Sub Total', type='float', digits_compute=dp.get_precision('Account')),
+        'starting_id': fields.many2one('account.bank.statement', ondelete='cascade'),
+        'ending_id': fields.many2one('account.bank.statement', ondelete='cascade'),
+     }
+
+account_cashbox_line()
+
+class account_cash_statement(osv.osv):
+
+    _inherit = 'account.bank.statement'
+
+    def _get_starting_balance(self, cr, uid, ids, context=None):
+
+        """ Find starting balance
+        @param name: Names of fields.
+        @param arg: User defined arguments
+        @return: Dictionary of values.
+        """
+        res = {}
+        for statement in self.browse(cr, uid, ids, context=context):
+            amount_total = 0.0
+
+            if statement.journal_id.type not in('cash'):
+                continue
+
+            for line in statement.starting_details_ids:
+                amount_total+= line.pieces * line.number
+            res[statement.id] = {
+                'balance_start': amount_total
+            }
+        return res
+
+    def _balance_end_cash(self, cr, uid, ids, name, arg, context=None):
+        """ Find ending balance  "
+        @param name: Names of fields.
+        @param arg: User defined arguments
+        @return: Dictionary of values.
+        """
+        res = {}
+        for statement in self.browse(cr, uid, ids, context=context):
+            amount_total = 0.0
+            for line in statement.ending_details_ids:
+                amount_total += line.pieces * line.number
+            res[statement.id] = amount_total
+        return res
+
+    def _get_sum_entry_encoding(self, cr, uid, ids, name, arg, context=None):
+
+        """ Find encoding total of statements "
+        @param name: Names of fields.
+        @param arg: User defined arguments
+        @return: Dictionary of values.
+        """
+        res2 = {}
+        for statement in self.browse(cr, uid, ids, context=context):
+            encoding_total=0.0
+            for line in statement.line_ids:
+               encoding_total += line.amount
+            res2[statement.id] = encoding_total
+        return res2
+
+    def _get_company(self, cr, uid, context=None):
+        user_pool = self.pool.get('res.users')
+        company_pool = self.pool.get('res.company')
+        user = user_pool.browse(cr, uid, uid, context=context)
+        company_id = user.company_id
+        if not company_id:
+            company_id = company_pool.search(cr, uid, [])
+        return company_id and company_id[0] or False
+
+    def _get_cash_open_box_lines(self, cr, uid, context=None):
+        res = []
+        curr = [1, 2, 5, 10, 20, 50, 100, 500]
+        for rs in curr:
+            dct = {
+                'pieces': rs,
+                'number': 0
+            }
+            res.append(dct)
+        journal_ids = self.pool.get('account.journal').search(cr, uid, [('type', '=', 'cash')], context=context)
+        if journal_ids:
+            results = self.search(cr, uid, [('journal_id', 'in', journal_ids),('state', '=', 'confirm')], context=context)
+            if results:
+                cash_st = self.browse(cr, uid, results, context=context)[0]
+                for cash_line in cash_st.ending_details_ids:
+                    for r in res:
+                        if cash_line.pieces == r['pieces']:
+                            r['number'] = cash_line.number
+        return res
+
+    def _get_default_cash_close_box_lines(self, cr, uid, context=None):
+        res = []
+        curr = [1, 2, 5, 10, 20, 50, 100, 500]
+        for rs in curr:
+            dct = {
+                'pieces': rs,
+                'number': 0
+            }
+            res.append(dct)
+        return res
+
+    def _get_cash_close_box_lines(self, cr, uid, context=None):
+        res = []
+        curr = [1, 2, 5, 10, 20, 50, 100, 500]
+        for rs in curr:
+            dct = {
+                'pieces': rs,
+                'number': 0
+            }
+            res.append((0, 0, dct))
+        return res
+
+    def _get_cash_open_close_box_lines(self, cr, uid, context=None):
+        res = {}
+        start_l = []
+        end_l = []
+        starting_details = self._get_cash_open_box_lines(cr, uid, context=context)
+        ending_details = self._get_default_cash_close_box_lines(cr, uid, context)
+        for start in starting_details:
+            start_l.append((0, 0, start))
+        for end in ending_details:
+            end_l.append((0, 0, end))
+        res['start'] = start_l
+        res['end'] = end_l
+        return res
+
+    def _get_statement(self, cr, uid, ids, context=None):
+        result = {}
+        for line in self.pool.get('account.bank.statement.line').browse(cr, uid, ids, context=context):
+            result[line.statement_id.id] = True
+        return result.keys()
+
+    _columns = {
+        'total_entry_encoding': fields.function(_get_sum_entry_encoding, string="Cash Transaction", help="Total cash transactions",
+            store = {
+                'account.bank.statement': (lambda self, cr, uid, ids, c={}: ids, ['line_ids','move_line_ids'], 10),
+                'account.bank.statement.line': (_get_statement, ['amount'], 10),
+            }),
+        'closing_date': fields.datetime("Closed On"),
+        'balance_end_cash': fields.function(_balance_end_cash, store=True, string='Closing Balance', help="Closing balance based on cashBox"),
+        'starting_details_ids': fields.one2many('account.cashbox.line', 'starting_id', string='Opening Cashbox'),
+        'ending_details_ids': fields.one2many('account.cashbox.line', 'ending_id', string='Closing Cashbox'),
+        'user_id': fields.many2one('res.users', 'Responsible', required=False),
+    }
+    _defaults = {
+        'state': 'draft',
+        'date': lambda self,cr,uid,context={}: context.get('date', time.strftime("%Y-%m-%d %H:%M:%S")),
+        'user_id': lambda self, cr, uid, context=None: uid,
+        'starting_details_ids': _get_cash_open_box_lines,
+        'ending_details_ids': _get_default_cash_close_box_lines
+     }
+
+    def create(self, cr, uid, vals, context=None):
+        if self.pool.get('account.journal').browse(cr, uid, vals['journal_id'], context=context).type == 'cash':
+            open_close = self._get_cash_open_close_box_lines(cr, uid, context)
+            if vals.get('starting_details_ids', False):
+                starting_details_ids = vals.get('starting_details_ids')
+                for start in starting_details_ids:
+                    dict_val = start[2] or {}
+                    for end in open_close['end']:
+                       if end[2]['pieces'] == dict_val.get('pieces', 0.0):
+                           end[2]['number'] += dict_val.get('number', 0.0)
+            vals.update({
+#                'ending_details_ids': open_close['start'],
+                'starting_details_ids': open_close['end']
+            })
+        else:
+            vals.update({
+                'ending_details_ids': False,
+                'starting_details_ids': False
+            })
+        res_id = super(account_cash_statement, self).create(cr, uid, vals, context=context)
+        self.write(cr, uid, [res_id], {})
+        return res_id
+
+    def write(self, cr, uid, ids, vals, context=None):
+        """
+        Update redord(s) comes in {ids}, with new value comes as {vals}
+        return True on success, False otherwise
+
+        @param cr: cursor to database
+        @param user: id of current user
+        @param ids: list of record ids to be update
+        @param vals: dict of new values to be set
+        @param context: context arguments, like lang, time zone
+
+        @return: True on success, False otherwise
+        """
+
+        super(account_cash_statement, self).write(cr, uid, ids, vals, context=context)
+        res = self._get_starting_balance(cr, uid, ids)
+        for rs in res:
+            super(account_cash_statement, self).write(cr, uid, [rs], res.get(rs))
+        return True
+
+    def onchange_journal_id(self, cr, uid, statement_id, journal_id, context=None):
+        """ Changes balance start and starting details if journal_id changes"
+        @param statement_id: Changed statement_id
+        @param journal_id: Changed journal_id
+        @return:  Dictionary of changed values
+        """
+        res = {}
+        balance_start = 0.0
+        if not journal_id:
+            res.update({
+                'balance_start': balance_start
+            })
+            return res
+        return super(account_cash_statement, self).onchange_journal_id(cr, uid, statement_id, journal_id, context=context)
+
+    def _equal_balance(self, cr, uid, cash_id, context=None):
+        statement = self.browse(cr, uid, cash_id, context=context)
+        self.write(cr, uid, [cash_id], {'balance_end_real': statement.balance_end})
+        statement.balance_end_real = statement.balance_end
+        if statement.balance_end != statement.balance_end_cash:
+            return False
+        return True
+
+    def _user_allow(self, cr, uid, statement_id, context=None):
+        return True
+
+    def button_open(self, cr, uid, ids, context=None):
+        """ Changes statement state to Running.
+        @return: True
+        """
+        obj_seq = self.pool.get('ir.sequence')
+        if context is None:
+            context = {}
+        statement_pool = self.pool.get('account.bank.statement')
+        for statement in statement_pool.browse(cr, uid, ids, context=context):
+            vals = {}
+            if not self._user_allow(cr, uid, statement.id, context=context):
+                raise osv.except_osv(_('Error !'), (_('User %s does not have rights to access %s journal !') % (statement.user_id.name, statement.journal_id.name)))
+
+            if statement.name and statement.name == '/':
+                c = {'fiscalyear_id': statement.period_id.fiscalyear_id.id}
+                if statement.journal_id.sequence_id:
+                    st_number = obj_seq.next_by_id(cr, uid, statement.journal_id.sequence_id.id, context=c)
+                else:
+                    st_number = obj_seq.next_by_code(cr, uid, 'account.cash.statement', context=c)
+                vals.update({
+                    'name': st_number
+                })
+
+            vals.update({
+                'state': 'open',
+            })
+            self.write(cr, uid, [statement.id], vals, context=context)
+        return True
+
+    def balance_check(self, cr, uid, cash_id, journal_type='bank', context=None):
+        if journal_type == 'bank':
+            return super(account_cash_statement, self).balance_check(cr, uid, cash_id, journal_type, context)
+        if not self._equal_balance(cr, uid, cash_id, context):
+            raise osv.except_osv(_('Error !'), _('The closing balance should be the same than the computed balance!'))
+        return True
+
+    def statement_close(self, cr, uid, ids, journal_type='bank', context=None):
+        if journal_type == 'bank':
+            return super(account_cash_statement, self).statement_close(cr, uid, ids, journal_type, context)
+        vals = {
+            'state':'confirm',
+            'closing_date': time.strftime("%Y-%m-%d %H:%M:%S")
+        }
+        return self.write(cr, uid, ids, vals, context=context)
+
+    def check_status_condition(self, cr, uid, state, journal_type='bank'):
+        if journal_type == 'bank':
+            return super(account_cash_statement, self).check_status_condition(cr, uid, state, journal_type)
+        return state=='open'
+
+    def button_confirm_cash(self, cr, uid, ids, context=None):
+        super(account_cash_statement, self).button_confirm_bank(cr, uid, ids, context=context)
+        return self.write(cr, uid, ids, {'closing_date': time.strftime("%Y-%m-%d %H:%M:%S")}, context=context)
+
+    def button_cancel(self, cr, uid, ids, context=None):
+        cash_box_line_pool = self.pool.get('account.cashbox.line')
+        super(account_cash_statement, self).button_cancel(cr, uid, ids, context=context)
+        for st in self.browse(cr, uid, ids, context):
+            for end in st.ending_details_ids:
+                cash_box_line_pool.write(cr, uid, [end.id], {'number': 0})
+        return True
+
+account_cash_statement()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'account/account_end_fy.xml'
--- account/account_end_fy.xml	1970-01-01 00:00:00 +0000
+++ account/account_end_fy.xml	2013-06-05 16:26:39 +0000
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+    <data>
+        
+        <record id="action_account_period_tree" model="ir.actions.act_window">
+            <field name="name">Close a Period</field>
+            <field name="res_model">account.period</field>
+            <field name="view_type">form</field>
+            <field name="view_id" ref="view_account_period_tree"/>
+            <field name="context">{'search_default_draft': 1}</field>
+            <field name="help">A period is a fiscal period of time during which accounting entries should be recorded for accounting related activities. Monthly period is the norm but depending on your countries or company needs, you could also have quarterly periods. Closing a period will make it impossible to record new accounting entries, all new entries should then be made on the following open period. Close a period when you do not want to record new entries and want to lock this period for tax related calculation.</field>
+        </record>
+        <menuitem
+            action="action_account_period_tree"
+            id="menu_action_account_period_close_tree"
+            parent="account.menu_account_end_year_treatments"
+            sequence="0" groups="base.group_extended"/>
+        
+    </data>
+</openerp>

=== added file 'account/account_financial_report.py'
--- account/account_financial_report.py	1970-01-01 00:00:00 +0000
+++ account/account_financial_report.py	2013-06-05 16:26:39 +0000
@@ -0,0 +1,135 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
+#
+#    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/>.
+#
+##############################################################################
+
+import time
+from datetime import datetime
+from dateutil.relativedelta import relativedelta
+from operator import itemgetter
+
+import netsvc
+import pooler
+from osv import fields, osv
+import decimal_precision as dp
+from tools.translate import _
+
+# ---------------------------------------------------------
+# Account Financial Report
+# ---------------------------------------------------------
+
+class account_financial_report(osv.osv):
+    _name = "account.financial.report"
+    _description = "Account Report"
+
+    def _get_level(self, cr, uid, ids, field_name, arg, context=None):
+        res = {}
+        for report in self.browse(cr, uid, ids, context=context):
+            level = 0
+            if report.parent_id:
+                level = report.parent_id.level + 1
+            res[report.id] = level
+        return res
+
+    def _get_children_by_order(self, cr, uid, ids, context=None):
+        res = []
+        for id in ids:
+            res.append(id)
+            ids2 = self.search(cr, uid, [('parent_id', '=', id)], order='sequence ASC', context=context)
+            res += self._get_children_by_order(cr, uid, ids2, context=context)
+        return res
+
+    def _get_balance(self, cr, uid, ids, field_names, args, context=None):
+        account_obj = self.pool.get('account.account')
+        res = {}
+        for report in self.browse(cr, uid, ids, context=context):
+            if report.id in res:
+                continue
+            res[report.id] = dict((fn, 0.0) for fn in field_names)
+            if report.type == 'accounts':
+                # it's the sum of the linked accounts
+                for a in report.account_ids:
+                    for field in field_names:
+                        res[report.id][field] += getattr(a, field)
+            elif report.type == 'account_type':
+                # it's the sum the leaf accounts with such an account type
+                report_types = [x.id for x in report.account_type_ids]
+                account_ids = account_obj.search(cr, uid, [('user_type','in', report_types), ('type','!=','view')], context=context)
+                for a in account_obj.browse(cr, uid, account_ids, context=context):
+                    for field in field_names:
+                        res[report.id][field] += getattr(a, field)
+            elif report.type == 'account_report' and report.account_report_id:
+                # it's the amount of the linked report
+                res2 = self._get_balance(cr, uid, [report.account_report_id.id], field_names, False, context=context)
+                for key, value in res2.items():
+                    for field in field_names:
+                        res[report.id][field] += value[field]
+            elif report.type == 'sum':
+                # it's the sum of the children of this account.report
+                res2 = self._get_balance(cr, uid, [rec.id for rec in report.children_ids], field_names, False, context=context)
+                for key, value in res2.items():
+                    for field in field_names:
+                        res[report.id][field] += value[field]
+        return res
+
+    _columns = {
+        'name': fields.char('Report Name', size=128, required=True, translate=True),
+        'parent_id': fields.many2one('account.financial.report', 'Parent'),
+        'children_ids':  fields.one2many('account.financial.report', 'parent_id', 'Account Report'),
+        'sequence': fields.integer('Sequence'),
+        'balance': fields.function(_get_balance, 'Balance', multi='balance'),
+        'debit': fields.function(_get_balance, 'Debit', multi='balance'),
+        'credit': fields.function(_get_balance, 'Credit', multi="balance"),
+        'level': fields.function(_get_level, string='Level', store=True, type='integer'),
+        'type': fields.selection([
+            ('sum','View'),
+            ('accounts','Accounts'),
+            ('account_type','Account Type'),
+            ('account_report','Report Value'),
+            ],'Type'),
+        'account_ids': fields.many2many('account.account', 'account_account_financial_report', 'report_line_id', 'account_id', 'Accounts'),
+        'account_report_id':  fields.many2one('account.financial.report', 'Report Value'),
+        'account_type_ids': fields.many2many('account.account.type', 'account_account_financial_report_type', 'report_id', 'account_type_id', 'Account Types'),
+        'sign': fields.selection([(-1, 'Reverse balance sign'), (1, 'Preserve balance sign')], 'Sign on Reports', required=True, help='For accounts that are typically more debited than credited and that you would like to print as negative amounts in your reports, you should reverse the sign of the balance; e.g.: Expense account. The same applies for accounts that are typically more credited than debited and that you would like to print as positive amounts in your reports; e.g.: Income account.'),
+        'display_detail': fields.selection([
+            ('no_detail','No detail'),
+            ('detail_flat','Display children flat'),
+            ('detail_with_hierarchy','Display children with hierarchy')
+            ], 'Display details'),
+        'style_overwrite': fields.selection([
+            (0, 'Automatic formatting'),
+            (1,'Main Title 1 (bold, underlined)'),
+            (2,'Title 2 (bold)'),
+            (3,'Title 3 (bold, smaller)'),
+            (4,'Normal Text'),
+            (5,'Italic Text (smaller)'),
+            (6,'Smallest Text'),
+            ],'Financial Report Style', help="You can set up here the format you want this record to be displayed. If you leave the automatic formatting, it will be computed based on the financial reports hierarchy (auto-computed field 'level')."),
+    }
+
+    _defaults = {
+        'type': 'sum',
+        'display_detail': 'detail_flat',
+        'sign': 1,
+        'style_overwrite': 0,
+    }
+
+account_financial_report()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'account/account_financial_report_data.xml'
--- account/account_financial_report_data.xml	1970-01-01 00:00:00 +0000
+++ account/account_financial_report_data.xml	2013-06-05 16:26:39 +0000
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+    <data noupdate="1">
+        <!-- 
+            Financial Reports
+        -->
+        <record id="account_financial_report_profitandloss0" model="account.financial.report">
+            <field name="name">Profit and Loss</field>
+            <field name="type">sum</field>
+        </record>
+        <record id="account_financial_report_income0" model="account.financial.report">
+            <field name="name">Income</field>
+            <field name="parent_id" ref="account_financial_report_profitandloss0"/>
+            <field name="display_detail">detail_with_hierarchy</field>
+            <field name="type">account_type</field>
+        </record>
+        <record id="account_financial_report_expense0" model="account.financial.report">
+            <field name="name">Expense</field>
+            <field name="parent_id" ref="account_financial_report_profitandloss0"/>
+            <field name="display_detail">detail_with_hierarchy</field>
+            <field name="type">account_type</field>
+        </record>
+
+        <record id="account_financial_report_balancesheet0" model="account.financial.report">
+            <field name="name">Balance Sheet</field>
+            <field name="type">sum</field>
+        </record>
+        <record id="account_financial_report_assets0" model="account.financial.report">
+            <field name="name">Assets</field>
+            <field name="parent_id" ref="account_financial_report_balancesheet0"/>
+            <field name="display_detail">detail_with_hierarchy</field>
+            <field name="type">account_type</field>
+        </record>
+        <record id="account_financial_report_liabilitysum0" model="account.financial.report">
+            <field name="name">Liability</field>
+            <field name="parent_id" ref="account_financial_report_balancesheet0"/>
+            <field name="display_detail">no_detail</field>
+            <field name="type">sum</field>
+        </record>
+        <record id="account_financial_report_liability0" model="account.financial.report">
+            <field name="name">Liability</field>
+            <field name="parent_id" ref="account_financial_report_liabilitysum0"/>
+            <field name="display_detail">detail_with_hierarchy</field>
+            <field name="type">account_type</field>
+        </record>
+        <record id="account_financial_report_profitloss_toreport0" model="account.financial.report">
+            <field name="name">Profit (Loss) to report</field>
+            <field name="parent_id" ref="account_financial_report_liabilitysum0"/>
+            <field name="display_detail">no_detail</field>
+            <field name="type">account_report</field>
+            <field name="account_report_id" ref="account_financial_report_profitandloss0"/>
+        </record>
+    </data>
+</openerp>
+

=== added file 'account/account_installer.xml'
--- account/account_installer.xml	1970-01-01 00:00:00 +0000
+++ account/account_installer.xml	2013-06-05 16:26:39 +0000
@@ -0,0 +1,109 @@
+<openerp>
+    <data>
+        <record id="view_account_configuration_installer" model="ir.ui.view">
+            <field name="name">account.installer.form</field>
+            <field name="model">account.installer</field>
+            <field name="type">form</field>
+            <field name="inherit_id" ref="base.res_config_installer"/>
+            <field name="arch" type="xml">
+            <data>
+                <form position="attributes">
+                    <attribute name="string">Accounting Application Configuration</attribute>
+                </form>
+                <separator string="title" position="attributes">
+                    <attribute name="string">Configure Your Chart of Accounts</attribute>
+                </separator>
+                <xpath expr="//label[@string='description']" position="attributes">
+                    <attribute name="string">The default Chart of Accounts is matching your country selection. If no certified Chart of Accounts exists for your specified country, a generic one can be installed and will be selected by default.</attribute>
+                </xpath>
+                <xpath expr="//button[@string='Install Modules']" position="attributes">
+                    <attribute name="string">Configure</attribute>
+                </xpath>
+                <xpath expr="//separator[@string=&quot;vsep&quot;]" position="attributes">
+                    <attribute name="rowspan">23</attribute>
+                    <attribute name="string"/>
+                </xpath>
+                <group colspan="8" position="inside">
+                    <group colspan="4" width="600">
+                            <field name="charts"/>
+                            <group colspan="4" groups="account.group_account_user">
+                                <separator col="4" colspan="4" string="Configure Fiscal Year"/>
+                                <field name="has_default_company" invisible="1" />
+                                <field name="company_id" colspan="4" widget="selection" attrs="{'invisible' : [('has_default_company', '=', True)]}"/><!-- we assume that this wizard will be run only by administrators and as this field may cause problem if hidden (because of the default company of the user removed from the selection because already configured), we simply choosed to remove the group "multi company" of it -->
+                                <field name="date_start" on_change="on_change_start_date(date_start)"/>
+                                <field name="date_stop"/>
+                                <field name="period" colspan="4"/>
+                            </group>
+                        </group>
+                    </group>
+                </data>
+            </field>
+        </record>
+
+        <record id="action_account_configuration_installer" model="ir.actions.act_window">
+            <field name="name">Install your Chart of Accounts</field>
+            <field name="type">ir.actions.act_window</field>
+            <field name="res_model">account.installer</field>
+            <field name="view_id" ref="view_account_configuration_installer"/>
+            <field name="view_type">form</field>
+            <field name="view_mode">form</field>
+            <field name="target">new</field>
+        </record>
+
+        <record id="category_accounting_configuration" model="ir.actions.todo.category">
+            <field name="name">Accounting</field>
+            <field name="sequence">5</field>
+        </record>
+
+        <record id="account_configuration_installer_todo" model="ir.actions.todo">
+            <field name="action_id" ref="action_account_configuration_installer"/>
+            <field name="category_id" ref="category_accounting_configuration"/>
+            <field name="sequence">3</field>
+            <field name="type">automatic</field>
+        </record>
+
+        <record id="action_view_financial_accounts_installer" model="ir.actions.act_window">
+            <field name="name">Review your Financial Accounts</field>
+            <field name="type">ir.actions.act_window</field>
+            <field name="res_model">account.account</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">tree,form</field>
+            <field name="context">{'config_invisible': False}</field>
+        </record>
+
+        <record id="view_financial_accounts_todo" model="ir.actions.todo">
+            <field name="action_id" ref="action_view_financial_accounts_installer" />
+            <field name="category_id" ref="category_accounting_configuration" />
+            <field name="groups_id" eval="[(6, 0, [ref('account.group_account_user')])]" />
+        </record>
+
+        <record id="action_review_financial_journals_installer" model="ir.actions.act_window">
+            <field name="name">Review your Financial Journals</field>
+            <field name="type">ir.actions.act_window</field>
+            <field name="res_model">account.journal</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">tree,form</field>
+            <field name="help">Setup your accounting journals. For bank accounts, it's better to use the 'Setup Your Bank Accounts' tool that will automatically create the accounts and journals for you.</field>
+        </record>
+
+        <record id="review_financial_journals_todo" model="ir.actions.todo">
+            <field name="action_id" ref="action_review_financial_journals_installer" />
+            <field name="category_id" ref="category_accounting_configuration" />
+            <field name="groups_id" eval="[(6, 0, [ref('account.group_account_user')])]" />
+        </record>
+        <record id="action_review_payment_terms_installer" model="ir.actions.act_window">
+            <field name="name">Review your Payment Terms</field>
+            <field name="type">ir.actions.act_window</field>
+            <field name="res_model">account.payment.term</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">tree,form</field>
+            <field name="help">Payment terms define the conditions to pay a customer or supplier invoice in one or several payments. Customers periodic reminders will use the payment terms for each letter. Each customer or supplier can be assigned to one of these payment terms.</field>
+        </record>
+
+        <record id="review_payment_terms_todo" model="ir.actions.todo">
+            <field name="action_id" ref="action_review_payment_terms_installer" />
+            <field name="category_id" ref="category_accounting_configuration" />
+            <field name="groups_id" eval="[(6, 0, [ref('account.group_account_user')])]" />
+        </record>
+    </data>
+</openerp>

=== added file 'account/account_invoice.py'
--- account/account_invoice.py	1970-01-01 00:00:00 +0000
+++ account/account_invoice.py	2013-06-05 16:26:39 +0000
@@ -0,0 +1,1675 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
+#
+#    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/>.
+#
+##############################################################################
+
+import time
+from lxml import etree
+import decimal_precision as dp
+
+import netsvc
+import pooler
+from osv import fields, osv, orm
+from tools.translate import _
+
+class account_invoice(osv.osv):
+    def _amount_all(self, cr, uid, ids, name, args, context=None):
+        res = {}
+        for invoice in self.browse(cr, uid, ids, context=context):
+            res[invoice.id] = {
+                'amount_untaxed': 0.0,
+                'amount_tax': 0.0,
+                'amount_total': 0.0
+            }
+            for line in invoice.invoice_line:
+                res[invoice.id]['amount_untaxed'] += line.price_subtotal
+            for line in invoice.tax_line:
+                res[invoice.id]['amount_tax'] += line.amount
+            res[invoice.id]['amount_total'] = res[invoice.id]['amount_tax'] + res[invoice.id]['amount_untaxed']
+        return res
+
+    def _get_journal(self, cr, uid, context=None):
+        if context is None:
+            context = {}
+        type_inv = context.get('type', 'out_invoice')
+        user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
+        company_id = context.get('company_id', user.company_id.id)
+        type2journal = {'out_invoice': 'sale', 'in_invoice': 'purchase', 'out_refund': 'sale_refund', 'in_refund': 'purchase_refund'}
+        journal_obj = self.pool.get('account.journal')
+        res = journal_obj.search(cr, uid, [('type', '=', type2journal.get(type_inv, 'sale')),
+                                            ('company_id', '=', company_id)],
+                                                limit=1)
+        return res and res[0] or False
+
+    def _get_currency(self, cr, uid, context=None):
+        res = False
+        journal_id = self._get_journal(cr, uid, context=context)
+        if journal_id:
+            journal = self.pool.get('account.journal').browse(cr, uid, journal_id, context=context)
+            res = journal.currency and journal.currency.id or journal.company_id.currency_id.id
+        return res
+
+    def _get_journal_analytic(self, cr, uid, type_inv, context=None):
+        type2journal = {'out_invoice': 'sale', 'in_invoice': 'purchase', 'out_refund': 'sale', 'in_refund': 'purchase'}
+        tt = type2journal.get(type_inv, 'sale')
+        result = self.pool.get('account.analytic.journal').search(cr, uid, [('type','=',tt)], context=context)
+        if not result:
+            raise osv.except_osv(_('No Analytic Journal !'),_("You must define an analytic journal of type '%s'!") % (tt,))
+        return result[0]
+
+    def _get_type(self, cr, uid, context=None):
+        if context is None:
+            context = {}
+        return context.get('type', 'out_invoice')
+
+    def _reconciled(self, cr, uid, ids, name, args, context=None):
+        res = {}
+        wf_service = netsvc.LocalService("workflow")
+        for inv in self.browse(cr, uid, ids, context=context):
+            res[inv.id] = self.test_paid(cr, uid, [inv.id])
+            if not res[inv.id] and inv.state == 'paid':
+                wf_service.trg_validate(uid, 'account.invoice', inv.id, 'open_test', cr)
+        return res
+
+    def _get_reference_type(self, cr, uid, context=None):
+        return [('none', _('Free Reference'))]
+
+    def _amount_residual(self, cr, uid, ids, name, args, context=None):
+        result = {}
+        for invoice in self.browse(cr, uid, ids, context=context):
+            checked_partial_rec_ids = []
+            result[invoice.id] = 0.0
+            if invoice.move_id:
+                for move_line in invoice.move_id.line_id:
+                    if move_line.account_id.type in ('receivable','payable'):
+                        if move_line.reconcile_partial_id:
+                            partial_reconcile_id = move_line.reconcile_partial_id.id
+                            if partial_reconcile_id in checked_partial_rec_ids:
+                                continue
+                            checked_partial_rec_ids.append(partial_reconcile_id)
+                        result[invoice.id] += move_line.amount_residual_currency
+        return result
+
+    # Give Journal Items related to the payment reconciled to this invoice
+    # Return ids of partial and total payments related to the selected invoices
+    def _get_lines(self, cr, uid, ids, name, arg, context=None):
+        res = {}
+        for invoice in self.browse(cr, uid, ids, context=context):
+            id = invoice.id
+            res[id] = []
+            if not invoice.move_id:
+                continue
+            data_lines = [x for x in invoice.move_id.line_id if x.account_id.id == invoice.account_id.id]
+            partial_ids = []
+            for line in data_lines:
+                ids_line = []
+                if line.reconcile_id:
+                    ids_line = line.reconcile_id.line_id
+                elif line.reconcile_partial_id:
+                    ids_line = line.reconcile_partial_id.line_partial_ids
+                l = map(lambda x: x.id, ids_line)
+                partial_ids.append(line.id)
+                res[id] =[x for x in l if x <> line.id and x not in partial_ids]
+        return res
+
+    def _get_invoice_line(self, cr, uid, ids, context=None):
+        result = {}
+        for line in self.pool.get('account.invoice.line').browse(cr, uid, ids, context=context):
+            result[line.invoice_id.id] = True
+        return result.keys()
+
+    def _get_invoice_tax(self, cr, uid, ids, context=None):
+        result = {}
+        for tax in self.pool.get('account.invoice.tax').browse(cr, uid, ids, context=context):
+            result[tax.invoice_id.id] = True
+        return result.keys()
+
+    def _compute_lines(self, cr, uid, ids, name, args, context=None):
+        result = {}
+        for invoice in self.browse(cr, uid, ids, context=context):
+            src = []
+            lines = []
+            if invoice.move_id:
+                for m in invoice.move_id.line_id:
+                    temp_lines = []
+                    if m.reconcile_id:
+                        temp_lines = map(lambda x: x.id, m.reconcile_id.line_id)
+                    elif m.reconcile_partial_id:
+                        temp_lines = map(lambda x: x.id, m.reconcile_partial_id.line_partial_ids)
+                    lines += [x for x in temp_lines if x not in lines]
+                    src.append(m.id)
+
+            lines = filter(lambda x: x not in src, lines)
+            result[invoice.id] = lines
+        return result
+
+    def _get_invoice_from_line(self, cr, uid, ids, context=None):
+        move = {}
+        for line in self.pool.get('account.move.line').browse(cr, uid, ids, context=context):
+            if line.reconcile_partial_id:
+                for line2 in line.reconcile_partial_id.line_partial_ids:
+                    move[line2.move_id.id] = True
+            if line.reconcile_id:
+                for line2 in line.reconcile_id.line_id:
+                    move[line2.move_id.id] = True
+        invoice_ids = []
+        if move:
+            invoice_ids = self.pool.get('account.invoice').search(cr, uid, [('move_id','in',move.keys())], context=context)
+        return invoice_ids
+
+    def _get_invoice_from_reconcile(self, cr, uid, ids, context=None):
+        move = {}
+        for r in self.pool.get('account.move.reconcile').browse(cr, uid, ids, context=context):
+            for line in r.line_partial_ids:
+                move[line.move_id.id] = True
+            for line in r.line_id:
+                move[line.move_id.id] = True
+
+        invoice_ids = []
+        if move:
+            invoice_ids = self.pool.get('account.invoice').search(cr, uid, [('move_id','in',move.keys())], context=context)
+        return invoice_ids
+
+    _name = "account.invoice"
+    _description = 'Invoice'
+    _order = "id desc"
+
+    _columns = {
+        'name': fields.char('Description', size=64, select=True, readonly=True, states={'draft':[('readonly',False)]}),
+        'origin': fields.char('Source Document', size=64, help="Reference of the document that produced this invoice.", readonly=True, states={'draft':[('readonly',False)]}),
+        'type': fields.selection([
+            ('out_invoice','Customer Invoice'),
+            ('in_invoice','Supplier Invoice'),
+            ('out_refund','Customer Refund'),
+            ('in_refund','Supplier Refund'),
+            ],'Type', readonly=True, select=True, change_default=True),
+
+        'number': fields.related('move_id','name', type='char', readonly=True, size=64, relation='account.move', store=True, string='Number'),
+        'internal_number': fields.char('Invoice Number', size=32, readonly=True, help="Unique number of the invoice, computed automatically when the invoice is created."),
+        'reference': fields.char('Invoice Reference', size=64, help="The partner reference of this invoice."),
+        'reference_type': fields.selection(_get_reference_type, 'Reference Type',
+            required=True, readonly=True, states={'draft':[('readonly',False)]}),
+        'comment': fields.text('Additional Information'),
+
+        'state': fields.selection([
+            ('draft','Draft'),
+            ('proforma','Pro-forma'),
+            ('proforma2','Pro-forma'),
+            ('open','Open'),
+            ('paid','Paid'),
+            ('cancel','Cancelled')
+            ],'State', select=True, readonly=True,
+            help=' * The \'Draft\' state is used when a user is encoding a new and unconfirmed Invoice. \
+            \n* The \'Pro-forma\' when invoice is in Pro-forma state,invoice does not have an invoice number. \
+            \n* The \'Open\' state is used when user create invoice,a invoice number is generated.Its in open state till user does not pay invoice. \
+            \n* The \'Paid\' state is set automatically when the invoice is paid. Its related journal entries may or may not be reconciled. \
+            \n* The \'Cancelled\' state is used when user cancel invoice.'),
+        'date_invoice': fields.date('Invoice Date', readonly=True, states={'draft':[('readonly',False)]}, select=True, help="Keep empty to use the current date"),
+        'date_due': fields.date('Due Date', states={'paid':[('readonly',True)], 'open':[('readonly',True)], 'close':[('readonly',True)]}, select=True,
+            help="If you use payment terms, the due date will be computed automatically at the generation "\
+                "of accounting entries. If you keep the payment term and the due date empty, it means direct payment. The payment term may compute several due dates, for example 50% now, 50% in one month."),
+        'partner_id': fields.many2one('res.partner', 'Partner', change_default=True, readonly=True, required=True, states={'draft':[('readonly',False)]}),
+        'address_contact_id': fields.many2one('res.partner.address', 'Contact Address', readonly=True, states={'draft':[('readonly',False)]}),
+        'address_invoice_id': fields.many2one('res.partner.address', 'Invoice Address', readonly=True, required=True, states={'draft':[('readonly',False)]}),
+        'payment_term': fields.many2one('account.payment.term', 'Payment Term',readonly=True, states={'draft':[('readonly',False)]},
+            help="If you use payment terms, the due date will be computed automatically at the generation "\
+                "of accounting entries. If you keep the payment term and the due date empty, it means direct payment. "\
+                "The payment term may compute several due dates, for example 50% now, 50% in one month."),
+        'period_id': fields.many2one('account.period', 'Force Period', domain=[('state','<>','done')], help="Keep empty to use the period of the validation(invoice) date.", readonly=True, states={'draft':[('readonly',False)]}),
+
+        'account_id': fields.many2one('account.account', 'Account', required=True, readonly=True, states={'draft':[('readonly',False)]}, help="The partner account used for this invoice."),
+        'invoice_line': fields.one2many('account.invoice.line', 'invoice_id', 'Invoice Lines', readonly=True, states={'draft':[('readonly',False)]}),
+        'tax_line': fields.one2many('account.invoice.tax', 'invoice_id', 'Tax Lines', readonly=True, states={'draft':[('readonly',False)]}),
+
+        'move_id': fields.many2one('account.move', 'Journal Entry', readonly=True, select=1, ondelete='restrict', help="Link to the automatically generated Journal Items."),
+        'amount_untaxed': fields.function(_amount_all, digits_compute=dp.get_precision('Account'), string='Untaxed',
+            store={
+                'account.invoice': (lambda self, cr, uid, ids, c={}: ids, ['invoice_line'], 20),
+                'account.invoice.tax': (_get_invoice_tax, None, 20),
+                'account.invoice.line': (_get_invoice_line, ['price_unit','invoice_line_tax_id','quantity','discount','invoice_id'], 20),
+            },
+            multi='all'),
+        'amount_tax': fields.function(_amount_all, digits_compute=dp.get_precision('Account'), string='Tax',
+            store={
+                'account.invoice': (lambda self, cr, uid, ids, c={}: ids, ['invoice_line'], 20),
+                'account.invoice.tax': (_get_invoice_tax, None, 20),
+                'account.invoice.line': (_get_invoice_line, ['price_unit','invoice_line_tax_id','quantity','discount','invoice_id'], 20),
+            },
+            multi='all'),
+        'amount_total': fields.function(_amount_all, digits_compute=dp.get_precision('Account'), string='Total',
+            store={
+                'account.invoice': (lambda self, cr, uid, ids, c={}: ids, ['invoice_line'], 20),
+                'account.invoice.tax': (_get_invoice_tax, None, 20),
+                'account.invoice.line': (_get_invoice_line, ['price_unit','invoice_line_tax_id','quantity','discount','invoice_id'], 20),
+            },
+            multi='all'),
+        'currency_id': fields.many2one('res.currency', 'Currency', required=True, readonly=True, states={'draft':[('readonly',False)]}),
+        'journal_id': fields.many2one('account.journal', 'Journal', required=True, readonly=True, states={'draft':[('readonly',False)]}),
+        'company_id': fields.many2one('res.company', 'Company', required=True, change_default=True, readonly=True, states={'draft':[('readonly',False)]}),
+        'check_total': fields.float('Verification Total', digits_compute=dp.get_precision('Account'), states={'open':[('readonly',True)],'close':[('readonly',True)]}),
+        'reconciled': fields.function(_reconciled, string='Paid/Reconciled', type='boolean',
+            store={
+                'account.invoice': (lambda self, cr, uid, ids, c={}: ids, None, 50), # Check if we can remove ?
+                'account.move.line': (_get_invoice_from_line, None, 50),
+                'account.move.reconcile': (_get_invoice_from_reconcile, None, 50),
+            }, help="It indicates that the invoice has been paid and the journal entry of the invoice has been reconciled with one or several journal entries of payment."),
+        'partner_bank_id': fields.many2one('res.partner.bank', 'Bank Account',
+            help='Bank Account Number to which the invoice will be paid. A Company bank account if this is a Customer Invoice or Supplier Refund, otherwise a Partner bank account number.', readonly=True, states={'draft':[('readonly',False)]}),
+        'move_lines':fields.function(_get_lines, type='many2many', relation='account.move.line', string='Entry Lines'),
+        'residual': fields.function(_amount_residual, digits_compute=dp.get_precision('Account'), string='Balance',
+            store={
+                'account.invoice': (lambda self, cr, uid, ids, c={}: ids, ['invoice_line','move_id'], 50),
+                'account.invoice.tax': (_get_invoice_tax, None, 50),
+                'account.invoice.line': (_get_invoice_line, ['price_unit','invoice_line_tax_id','quantity','discount','invoice_id'], 50),
+                'account.move.line': (_get_invoice_from_line, None, 50),
+                'account.move.reconcile': (_get_invoice_from_reconcile, None, 50),
+            },
+            help="Remaining amount due."),
+        'payment_ids': fields.function(_compute_lines, relation='account.move.line', type="many2many", string='Payments'),
+        'move_name': fields.char('Journal Entry', size=64, readonly=True, states={'draft':[('readonly',False)]}),
+        'user_id': fields.many2one('res.users', 'Salesman', readonly=True, states={'draft':[('readonly',False)]}),
+        'fiscal_position': fields.many2one('account.fiscal.position', 'Fiscal Position', readonly=True, states={'draft':[('readonly',False)]})
+    }
+    _defaults = {
+        'type': _get_type,
+        'state': 'draft',
+        'journal_id': _get_journal,
+        'currency_id': _get_currency,
+        'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.invoice', context=c),
+        'reference_type': 'none',
+        'check_total': 0.0,
+        'internal_number': False,
+        'user_id': lambda s, cr, u, c: u,
+    }
+    _sql_constraints = [
+        ('number_uniq', 'unique(number, company_id, journal_id, type)', 'Invoice Number must be unique per Company!'),
+    ]
+
+    def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):
+        journal_obj = self.pool.get('account.journal')
+        if context is None:
+            context = {}
+
+        if context.get('active_model', '') in ['res.partner'] and context.get('active_ids', False) and context['active_ids']:
+            partner = self.pool.get(context['active_model']).read(cr, uid, context['active_ids'], ['supplier','customer'])[0]
+            if not view_type:
+                view_id = self.pool.get('ir.ui.view').search(cr, uid, [('name', '=', 'account.invoice.tree')])
+                view_type = 'tree'
+            if view_type == 'form':
+                if partner['supplier'] and not partner['customer']:
+                    view_id = self.pool.get('ir.ui.view').search(cr,uid,[('name', '=', 'account.invoice.supplier.form')])
+                else:
+                    view_id = self.pool.get('ir.ui.view').search(cr,uid,[('name', '=', 'account.invoice.form')])
+        if view_id and isinstance(view_id, (list, tuple)):
+            view_id = view_id[0]
+        res = super(account_invoice,self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
+
+        type = context.get('journal_type', False)
+        for field in res['fields']:
+            if field == 'journal_id' and type:
+                journal_select = journal_obj._name_search(cr, uid, '', [('type', '=', type)], context=context, limit=None, name_get_uid=1)
+                res['fields'][field]['selection'] = journal_select
+
+        doc = etree.XML(res['arch'])
+        
+        if context.get('type', False):
+            for node in doc.xpath("//field[@name='partner_bank_id']"):
+                if context['type'] == 'in_refund':
+                    node.set('domain', "[('partner_id.ref_companies', 'in', [company_id])]")
+                elif context['type'] == 'out_refund':
+                    node.set('domain', "[('partner_id', '=', partner_id)]")
+            res['arch'] = etree.tostring(doc)
+                
+        if view_type == 'search':
+            if context.get('type', 'in_invoice') in ('out_invoice', 'out_refund'):
+                for node in doc.xpath("//group[@name='extended filter']"):
+                    doc.remove(node)
+            res['arch'] = etree.tostring(doc)
+
+        if view_type == 'tree':
+            partner_string = _('Customer')
+            if context.get('type', 'out_invoice') in ('in_invoice', 'in_refund'):
+                partner_string = _('Supplier')
+                for node in doc.xpath("//field[@name='reference']"):
+                    node.set('invisible', '0')
+            for node in doc.xpath("//field[@name='partner_id']"):
+                node.set('string', partner_string)
+            res['arch'] = etree.tostring(doc)
+        return res
+
+    def get_log_context(self, cr, uid, context=None):
+        if context is None:
+            context = {}
+        res = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account', 'invoice_form')
+        view_id = res and res[1] or False
+        context['view_id'] = view_id
+        return context
+
+    def create(self, cr, uid, vals, context=None):
+        if context is None:
+            context = {}
+        try:
+            res = super(account_invoice, self).create(cr, uid, vals, context)
+            for inv_id, name in self.name_get(cr, uid, [res], context=context):
+                ctx = context.copy()
+                if vals.get('type', 'in_invoice') in ('out_invoice', 'out_refund'):
+                    ctx = self.get_log_context(cr, uid, context=ctx)
+                message = _("Invoice '%s' is waiting for validation.") % name
+                self.log(cr, uid, inv_id, message, context=ctx)
+            return res
+        except Exception, e:
+            if '"journal_id" viol' in e.args[0]:
+                raise orm.except_orm(_('Configuration Error!'),
+                     _('There is no Accounting Journal of type Sale/Purchase defined!'))
+            else:
+                raise orm.except_orm(_('Unknown Error'), str(e))
+
+    def confirm_paid(self, cr, uid, ids, context=None):
+        if context is None:
+            context = {}
+        self.write(cr, uid, ids, {'state':'paid'}, context=context)
+        for inv_id, name in self.name_get(cr, uid, ids, context=context):
+            message = _("Invoice '%s' is paid.") % name
+            self.log(cr, uid, inv_id, message)
+        return True
+
+    def unlink(self, cr, uid, ids, context=None):
+        if context is None:
+            context = {}
+        invoices = self.read(cr, uid, ids, ['state','internal_number'], context=context)
+        unlink_ids = []
+        for t in invoices:
+            if t['state'] in ('draft', 'cancel') and t['internal_number']== False:
+                unlink_ids.append(t['id'])
+            else:
+                raise osv.except_osv(_('Invalid action !'), _('You can not delete an invoice which is open or paid. We suggest you to refund it instead.'))
+        osv.osv.unlink(self, cr, uid, unlink_ids, context=context)
+        return True
+
+    def onchange_partner_id(self, cr, uid, ids, type, partner_id,\
+            date_invoice=False, payment_term=False, partner_bank_id=False, company_id=False):
+        invoice_addr_id = False
+        contact_addr_id = False
+        partner_payment_term = False
+        acc_id = False
+        bank_id = False
+        fiscal_position = False
+
+        opt = [('uid', str(uid))]
+        if partner_id:
+
+            opt.insert(0, ('id', partner_id))
+            res = self.pool.get('res.partner').address_get(cr, uid, [partner_id], ['contact', 'invoice'])
+            contact_addr_id = res['contact']
+            invoice_addr_id = res['invoice']
+            p = self.pool.get('res.partner').browse(cr, uid, partner_id)
+            if company_id:
+                if p.property_account_receivable.company_id.id != company_id and p.property_account_payable.company_id.id != company_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)])
+                    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)])
+                    if not pay_pro_id:
+                        pay_pro_id = property_obj.search(cr,uid,[('name','=','property_account_payable'),('company_id','=',company_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 a chart of accounts for this company, you should create one.'))
+                    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 type in ('out_invoice', 'out_refund'):
+                acc_id = p.property_account_receivable.id
+            else:
+                acc_id = p.property_account_payable.id
+            fiscal_position = p.property_account_position and p.property_account_position.id or False
+            partner_payment_term = p.property_payment_term and p.property_payment_term.id or False
+            if p.bank_ids:
+                bank_id = p.bank_ids[0].id
+
+        result = {'value': {
+            'address_contact_id': contact_addr_id,
+            'address_invoice_id': invoice_addr_id,
+            'account_id': acc_id,
+            'payment_term': partner_payment_term,
+            'fiscal_position': fiscal_position
+            }
+        }
+
+        if type in ('in_invoice', 'in_refund'):
+            result['value']['partner_bank_id'] = bank_id
+
+        if payment_term != partner_payment_term:
+            if partner_payment_term:
+                to_update = self.onchange_payment_term_date_invoice(
+                    cr, uid, ids, partner_payment_term, date_invoice)
+                result['value'].update(to_update['value'])
+            else:
+                result['value']['date_due'] = False
+
+        if partner_bank_id != bank_id:
+            to_update = self.onchange_partner_bank(cr, uid, ids, bank_id)
+            result['value'].update(to_update['value'])
+        return result
+
+    def onchange_journal_id(self, cr, uid, ids, journal_id=False, context=None):
+        result = {}
+        if journal_id:
+            journal = self.pool.get('account.journal').browse(cr, uid, journal_id, context=context)
+            currency_id = journal.currency and journal.currency.id or journal.company_id.currency_id.id
+            result = {'value': {
+                    'currency_id': currency_id,
+                    }
+                }
+        return result
+
+    def onchange_payment_term_date_invoice(self, cr, uid, ids, payment_term_id, date_invoice):
+        if not payment_term_id:
+            return {}
+        res = {}
+        pt_obj = self.pool.get('account.payment.term')
+        if not date_invoice:
+            date_invoice = time.strftime('%Y-%m-%d')
+
+        pterm_list = pt_obj.compute(cr, uid, payment_term_id, value=1, date_ref=date_invoice)
+
+        if pterm_list:
+            pterm_list = [line[0] for line in pterm_list]
+            pterm_list.sort()
+            res = {'value':{'date_due': pterm_list[-1]}}
+        else:
+             raise osv.except_osv(_('Data Insufficient !'), _('The payment term of supplier does not have a payment term line!'))
+        return res
+
+    def onchange_invoice_line(self, cr, uid, ids, lines):
+        return {}
+
+    def onchange_partner_bank(self, cursor, user, ids, partner_bank_id=False):
+        return {'value': {}}
+
+    def onchange_company_id(self, cr, uid, ids, company_id, part_id, type, invoice_line, currency_id):
+        val = {}
+        dom = {}
+        obj_journal = self.pool.get('account.journal')
+        account_obj = self.pool.get('account.account')
+        inv_line_obj = self.pool.get('account.invoice.line')
+        if company_id and part_id and type:
+            acc_id = False
+            partner_obj = self.pool.get('res.partner').browse(cr,uid,part_id)
+            if partner_obj.property_account_payable and partner_obj.property_account_receivable:
+                if partner_obj.property_account_payable.company_id.id != company_id and partner_obj.property_account_receivable.company_id.id != company_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(part_id)+''),('company_id','=',company_id)])
+                    pay_pro_id = property_obj.search(cr, uid, [('name','=','property_account_payable'),('res_id','=','res.partner,'+str(part_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)])
+                    if not pay_pro_id:
+                        pay_pro_id = property_obj.search(cr, uid, [('name','=','property_account_payable'),('company_id','=',company_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 a chart of account, you should create one from the configuration of the accounting menu.'))
+                    if type in ('out_invoice', 'out_refund'):
+                        acc_id = rec_res_id
+                    else:
+                        acc_id = pay_res_id
+                    val= {'account_id': acc_id}
+            if ids:
+                if company_id:
+                    inv_obj = self.browse(cr,uid,ids)
+                    for line in inv_obj[0].invoice_line:
+                        if line.account_id:
+                            if line.account_id.company_id.id != company_id:
+                                result_id = account_obj.search(cr, uid, [('name','=',line.account_id.name),('company_id','=',company_id)])
+                                if not result_id:
+                                    raise osv.except_osv(_('Configuration Error !'),
+                                        _('Can not find a chart of account, you should create one from the configuration of the accounting menu.'))
+                                inv_line_obj.write(cr, uid, [line.id], {'account_id': result_id[-1]})
+            else:
+                if invoice_line:
+                    for inv_line in invoice_line:
+                        obj_l = account_obj.browse(cr, uid, inv_line[2]['account_id'])
+                        if obj_l.company_id.id != company_id:
+                            raise osv.except_osv(_('Configuration Error !'),
+                                _('Invoice line account company does not match with invoice company.'))
+                        else:
+                            continue
+        if company_id and type:
+            if type in ('out_invoice'):
+                journal_type = 'sale'
+            elif type in ('out_refund'):
+                journal_type = 'sale_refund'
+            elif type in ('in_refund'):
+                journal_type = 'purchase_refund'
+            else:
+                journal_type = 'purchase'
+            journal_ids = obj_journal.search(cr, uid, [('company_id','=',company_id), ('type', '=', journal_type)])
+            if journal_ids:
+                val['journal_id'] = journal_ids[0]
+            ir_values_obj = self.pool.get('ir.values')
+            res_journal_default = ir_values_obj.get(cr, uid, 'default', 'type=%s' % (type), ['account.invoice'])
+            for r in res_journal_default:
+                if r[1] == 'journal_id' and r[2] in journal_ids:
+                    val['journal_id'] = r[2]
+            if not val.get('journal_id', False):
+                raise osv.except_osv(_('Configuration Error !'), (_('Can\'t find any account journal of %s type for this company.\n\nYou can create one in the menu: \nConfiguration\Financial Accounting\Accounts\Journals.') % (journal_type)))
+            dom = {'journal_id':  [('id', 'in', journal_ids)]}
+        else:
+            journal_ids = obj_journal.search(cr, uid, [])
+
+        return {'value': val, 'domain': dom}
+
+    # go from canceled state to draft state
+    def action_cancel_draft(self, cr, uid, ids, *args):
+        self.write(cr, uid, ids, {'state':'draft'})
+        wf_service = netsvc.LocalService("workflow")
+        for inv_id in ids:
+            wf_service.trg_delete(uid, 'account.invoice', inv_id, cr)
+            wf_service.trg_create(uid, 'account.invoice', inv_id, cr)
+        return True
+
+    # Workflow stuff
+    #################
+
+    # return the ids of the move lines which has the same account than the invoice
+    # whose id is in ids
+    def move_line_id_payment_get(self, cr, uid, ids, *args):
+        if not ids: return []
+        result = self.move_line_id_payment_gets(cr, uid, ids, *args)
+        return result.get(ids[0], [])
+
+    def move_line_id_payment_gets(self, cr, uid, ids, *args):
+        res = {}
+        if not ids: return res
+        cr.execute('SELECT i.id, l.id '\
+                   'FROM account_move_line l '\
+                   'LEFT JOIN account_invoice i ON (i.move_id=l.move_id) '\
+                   'WHERE i.id IN %s '\
+                   'AND l.account_id=i.account_id',
+                   (tuple(ids),))
+        for r in cr.fetchall():
+            res.setdefault(r[0], [])
+            res[r[0]].append( r[1] )
+        return res
+
+    def copy(self, cr, uid, id, default=None, context=None):
+        default = default or {}
+        default.update({
+            'state':'draft',
+            'number':False,
+            'move_id':False,
+            'move_name':False,
+            'internal_number': False,
+            'period_id': False,
+        })
+        if 'date_invoice' not in default:
+            default.update({
+                'date_invoice':False
+            })
+        if 'date_due' not in default:
+            default.update({
+                'date_due':False
+            })
+        return super(account_invoice, self).copy(cr, uid, id, default, context)
+
+    def test_paid(self, cr, uid, ids, *args):
+        res = self.move_line_id_payment_get(cr, uid, ids)
+        if not res:
+            return False
+        ok = True
+        for id in res:
+            cr.execute('select reconcile_id from account_move_line where id=%s', (id,))
+            ok = ok and  bool(cr.fetchone()[0])
+        return ok
+
+    def button_reset_taxes(self, cr, uid, ids, context=None):
+        if context is None:
+            context = {}
+        ctx = context.copy()
+        ait_obj = self.pool.get('account.invoice.tax')
+        for id in ids:
+            cr.execute("DELETE FROM account_invoice_tax WHERE invoice_id=%s AND manual is False", (id,))
+            partner = self.browse(cr, uid, id, context=ctx).partner_id
+            if partner.lang:
+                ctx.update({'lang': partner.lang})
+            for taxe in ait_obj.compute(cr, uid, id, context=ctx).values():
+                ait_obj.create(cr, uid, taxe)
+        # Update the stored value (fields.function), so we write to trigger recompute
+        self.pool.get('account.invoice').write(cr, uid, ids, {'invoice_line':[]}, context=ctx)
+        return True
+
+    def button_compute(self, cr, uid, ids, context=None, set_total=False):
+        self.button_reset_taxes(cr, uid, ids, context)
+        for inv in self.browse(cr, uid, ids, context=context):
+            if set_total:
+                self.pool.get('account.invoice').write(cr, uid, [inv.id], {'check_total': inv.amount_total})
+        return True
+
+    def _convert_ref(self, cr, uid, ref):
+        return (ref or '').replace('/','')
+
+    def _get_analytic_lines(self, cr, uid, id, context=None):
+        if context is None:
+            context = {}
+        inv = self.browse(cr, uid, id)
+        cur_obj = self.pool.get('res.currency')
+
+        company_currency = inv.company_id.currency_id.id
+        if inv.type in ('out_invoice', 'in_refund'):
+            sign = 1
+        else:
+            sign = -1
+
+        iml = self.pool.get('account.invoice.line').move_line_get(cr, uid, inv.id, context=context)
+        for il in iml:
+            if il['account_analytic_id']:
+                if inv.type in ('in_invoice', 'in_refund'):
+                    ref = inv.reference
+                else:
+                    ref = self._convert_ref(cr, uid, inv.number)
+                if not inv.journal_id.analytic_journal_id:
+                    raise osv.except_osv(_('No Analytic Journal !'),_("You have to define an analytic journal on the '%s' journal!") % (inv.journal_id.name,))
+                il['analytic_lines'] = [(0,0, {
+                    'name': il['name'],
+                    'date': inv['date_invoice'],
+                    'account_id': il['account_analytic_id'],
+                    'unit_amount': il['quantity'],
+                    'amount': cur_obj.compute(cr, uid, inv.currency_id.id, company_currency, il['price'], context={'date': inv.date_invoice}) * sign,
+                    'product_id': il['product_id'],
+                    'product_uom_id': il['uos_id'],
+                    'general_account_id': il['account_id'],
+                    'journal_id': inv.journal_id.analytic_journal_id.id,
+                    'ref': ref,
+                })]
+        return iml
+
+    def action_date_assign(self, cr, uid, ids, *args):
+        for inv in self.browse(cr, uid, ids):
+            res = self.onchange_payment_term_date_invoice(cr, uid, inv.id, inv.payment_term.id, inv.date_invoice)
+            if res and res['value']:
+                self.write(cr, uid, [inv.id], res['value'])
+        return True
+
+    def finalize_invoice_move_lines(self, cr, uid, invoice_browse, move_lines):
+        """finalize_invoice_move_lines(cr, uid, invoice, move_lines) -> move_lines
+        Hook method to be overridden in additional modules to verify and possibly alter the
+        move lines to be created by an invoice, for special cases.
+        :param invoice_browse: browsable record of the invoice that is generating the move lines
+        :param move_lines: list of dictionaries with the account.move.lines (as for create())
+        :return: the (possibly updated) final move_lines to create for this invoice
+        """
+        return move_lines
+
+    def check_tax_lines(self, cr, uid, inv, compute_taxes, ait_obj):
+        if not inv.tax_line:
+            for tax in compute_taxes.values():
+                ait_obj.create(cr, uid, tax)
+        else:
+            tax_key = []
+            for tax in inv.tax_line:
+                if tax.manual:
+                    continue
+                key = (tax.tax_code_id.id, tax.base_code_id.id, tax.account_id.id)
+                tax_key.append(key)
+                if not key in compute_taxes:
+                    raise osv.except_osv(_('Warning !'), _('Global taxes defined, but they are not in invoice lines !'))
+                base = compute_taxes[key]['base']
+                if abs(base - tax.base) > inv.company_id.currency_id.rounding:
+                    raise osv.except_osv(_('Warning !'), _('Tax base different!\nClick on compute to update the tax base.'))
+            for key in compute_taxes:
+                if not key in tax_key:
+                    raise osv.except_osv(_('Warning !'), _('Taxes are missing!\nClick on compute button.'))
+
+    def compute_invoice_totals(self, cr, uid, inv, company_currency, ref, invoice_move_lines):
+        total = 0
+        total_currency = 0
+        cur_obj = self.pool.get('res.currency')
+        for i in invoice_move_lines:
+            if inv.currency_id.id != company_currency:
+                i['currency_id'] = inv.currency_id.id
+                i['amount_currency'] = i['price']
+                i['price'] = cur_obj.compute(cr, uid, inv.currency_id.id,
+                        company_currency, i['price'],
+                        context={'date': inv.date_invoice or time.strftime('%Y-%m-%d')})
+            else:
+                i['amount_currency'] = False
+                i['currency_id'] = False
+            i['ref'] = ref
+            if inv.type in ('out_invoice','in_refund'):
+                total += i['price']
+                total_currency += i['amount_currency'] or i['price']
+                i['price'] = - i['price']
+            else:
+                total -= i['price']
+                total_currency -= i['amount_currency'] or i['price']
+        return total, total_currency, invoice_move_lines
+
+    def inv_line_characteristic_hashcode(self, invoice, invoice_line):
+        """Overridable hashcode generation for invoice lines. Lines having the same hashcode
+        will be grouped together if the journal has the 'group line' option. Of course a module
+        can add fields to invoice lines that would need to be tested too before merging lines
+        or not."""
+        return "%s-%s-%s-%s-%s"%(
+            invoice_line['account_id'],
+            invoice_line.get('tax_code_id',"False"),
+            invoice_line.get('product_id',"False"),
+            invoice_line.get('analytic_account_id',"False"),
+            invoice_line.get('date_maturity',"False"))
+
+    def group_lines(self, cr, uid, iml, line, inv):
+        """Merge account move lines (and hence analytic lines) if invoice line hashcodes are equals"""
+        if inv.journal_id.group_invoice_lines:
+            line2 = {}
+            for x, y, l in line:
+                tmp = self.inv_line_characteristic_hashcode(inv, l)
+
+                if tmp in line2:
+                    am = line2[tmp]['debit'] - line2[tmp]['credit'] + (l['debit'] - l['credit'])
+                    line2[tmp]['debit'] = (am > 0) and am or 0.0
+                    line2[tmp]['credit'] = (am < 0) and -am or 0.0
+                    line2[tmp]['tax_amount'] += l['tax_amount']
+                    line2[tmp]['analytic_lines'] += l['analytic_lines']
+                    line2[tmp]['amount_currency'] += l['amount_currency']
+                    line2[tmp]['quantity'] += l['quantity']
+                else:
+                    line2[tmp] = l
+            line = []
+            for key, val in line2.items():
+                line.append((0,0,val))
+        return line
+
+    def action_move_create(self, cr, uid, ids, context=None):
+        """Creates invoice related analytics and financial move lines"""
+        ait_obj = self.pool.get('account.invoice.tax')
+        cur_obj = self.pool.get('res.currency')
+        period_obj = self.pool.get('account.period')
+        payment_term_obj = self.pool.get('account.payment.term')
+        journal_obj = self.pool.get('account.journal')
+        move_obj = self.pool.get('account.move')
+        if context is None:
+            context = {}
+        for inv in self.browse(cr, uid, ids, context=context):
+            if not inv.journal_id.sequence_id:
+                raise osv.except_osv(_('Error !'), _('Please define sequence on the journal related to this invoice.'))
+            if not inv.invoice_line:
+                raise osv.except_osv(_('No Invoice Lines !'), _('Please create some invoice lines.'))
+            if inv.move_id:
+                continue
+            
+            ctx = context.copy()
+            ctx.update({'lang': inv.partner_id.lang})
+            if not inv.date_invoice:
+                self.write(cr, uid, [inv.id], {'date_invoice': fields.date.context_today(self,cr,uid,context=context)}, context=ctx)
+            company_currency = inv.company_id.currency_id.id
+            # create the analytical lines
+            # one move line per invoice line
+            iml = self._get_analytic_lines(cr, uid, inv.id, context=ctx)
+            # check if taxes are all computed
+            compute_taxes = ait_obj.compute(cr, uid, inv.id, context=ctx)
+            self.check_tax_lines(cr, uid, inv, compute_taxes, ait_obj)
+
+            # I disabled the check_total feature
+            #if inv.type in ('in_invoice', 'in_refund') and abs(inv.check_total - inv.amount_total) >= (inv.currency_id.rounding/2.0):
+            #    raise osv.except_osv(_('Bad total !'), _('Please verify the price of the invoice !\nThe real total does not match the computed total.'))
+
+            if inv.payment_term:
+                total_fixed = total_percent = 0
+                for line in inv.payment_term.line_ids:
+                    if line.value == 'fixed':
+                        total_fixed += line.value_amount
+                    if line.value == 'procent':
+                        total_percent += line.value_amount
+                total_fixed = (total_fixed * 100) / (inv.amount_total or 1.0)
+                if (total_fixed + total_percent) > 100:
+                    raise osv.except_osv(_('Error !'), _("Can not create the invoice !\nThe related payment term is probably misconfigured as it gives a computed amount greater than the total invoiced amount. The latest line of your payment term must be of type 'balance' to avoid rounding issues."))
+
+            # one move line per tax line
+            iml += ait_obj.move_line_get(cr, uid, inv.id)
+
+            entry_type = ''
+            if inv.type in ('in_invoice', 'in_refund'):
+                ref = inv.reference
+                entry_type = 'journal_pur_voucher'
+                if inv.type == 'in_refund':
+                    entry_type = 'cont_voucher'
+            else:
+                ref = self._convert_ref(cr, uid, inv.number)
+                entry_type = 'journal_sale_vou'
+                if inv.type == 'out_refund':
+                    entry_type = 'cont_voucher'
+
+            diff_currency_p = inv.currency_id.id <> company_currency
+            # create one move line for the total and possibly adjust the other lines amount
+            total = 0
+            total_currency = 0
+            total, total_currency, iml = self.compute_invoice_totals(cr, uid, inv, company_currency, ref, iml)
+            acc_id = inv.account_id.id
+
+            name = inv['name'] or '/'
+            totlines = False
+            if inv.payment_term:
+                totlines = payment_term_obj.compute(cr,
+                        uid, inv.payment_term.id, total, inv.date_invoice or False, context=ctx)
+            if totlines:
+                res_amount_currency = total_currency
+                i = 0
+                ctx.update({'date': inv.date_invoice})
+                for t in totlines:
+                    if inv.currency_id.id != company_currency:
+                        amount_currency = cur_obj.compute(cr, uid, company_currency, inv.currency_id.id, t[1], context=ctx)
+                    else:
+                        amount_currency = False
+
+                    # last line add the diff
+                    res_amount_currency -= amount_currency or 0
+                    i += 1
+                    if i == len(totlines):
+                        amount_currency += res_amount_currency
+
+                    iml.append({
+                        'type': 'dest',
+                        'name': name,
+                        'price': t[1],
+                        'account_id': acc_id,
+                        'date_maturity': t[0],
+                        'amount_currency': diff_currency_p \
+                                and amount_currency or False,
+                        'currency_id': diff_currency_p \
+                                and inv.currency_id.id or False,
+                        'ref': ref,
+                    })
+            else:
+                iml.append({
+                    'type': 'dest',
+                    'name': name,
+                    'price': total,
+                    'account_id': acc_id,
+                    'date_maturity': inv.date_due or False,
+                    'amount_currency': diff_currency_p \
+                            and total_currency or False,
+                    'currency_id': diff_currency_p \
+                            and inv.currency_id.id or False,
+                    'ref': ref
+            })
+
+            date = inv.date_invoice or time.strftime('%Y-%m-%d')
+            part = inv.partner_id.id
+
+            line = map(lambda x:(0,0,self.line_get_convert(cr, uid, x, part, date, context=ctx)),iml)
+
+            line = self.group_lines(cr, uid, iml, line, inv)
+
+            journal_id = inv.journal_id.id
+            journal = journal_obj.browse(cr, uid, journal_id, context=ctx)
+            if journal.centralisation:
+                raise osv.except_osv(_('UserError'),
+                        _('You cannot create an invoice on a centralised journal. Uncheck the centralised counterpart box in the related journal from the configuration menu.'))
+
+            line = self.finalize_invoice_move_lines(cr, uid, inv, line)
+
+            move = {
+                'ref': inv.reference and inv.reference or inv.name,
+                'line_id': line,
+                'journal_id': journal_id,
+                'date': date,
+                'narration':inv.comment
+            }
+            period_id = inv.period_id and inv.period_id.id or False
+            ctx.update(company_id=inv.company_id.id,
+                       account_period_prefer_normal=True)
+            if not period_id:
+                period_ids = period_obj.find(cr, uid, inv.date_invoice, context=ctx)
+                period_id = period_ids and period_ids[0] or False
+            if period_id:
+                move['period_id'] = period_id
+                for i in line:
+                    i[2]['period_id'] = period_id
+
+            move_id = move_obj.create(cr, uid, move, context=ctx)
+            new_move_name = move_obj.browse(cr, uid, move_id, context=ctx).name
+            # make the invoice point to that move
+            self.write(cr, uid, [inv.id], {'move_id': move_id,'period_id':period_id, 'move_name':new_move_name}, context=ctx)
+            # Pass invoice in context in method post: used if you want to get the same
+            # account move reference when creating the same invoice after a cancelled one:
+            ctx.update({'invoice':inv})
+            move_obj.post(cr, uid, [move_id], context=ctx)
+        self._log_event(cr, uid, ids)
+        return True
+
+    def line_get_convert(self, cr, uid, x, part, date, context=None):
+        return {
+            'date_maturity': x.get('date_maturity', False),
+            'partner_id': part,
+            'name': x['name'][:64],
+            'date': date,
+            'debit': x['price']>0 and x['price'],
+            'credit': x['price']<0 and -x['price'],
+            'account_id': x['account_id'],
+            'analytic_lines': x.get('analytic_lines', []),
+            'amount_currency': x['price']>0 and abs(x.get('amount_currency', False)) or -abs(x.get('amount_currency', False)),
+            'currency_id': x.get('currency_id', False),
+            'tax_code_id': x.get('tax_code_id', False),
+            'tax_amount': x.get('tax_amount', False),
+            'ref': x.get('ref', False),
+            'quantity': x.get('quantity',1.00),
+            'product_id': x.get('product_id', False),
+            'product_uom_id': x.get('uos_id', False),
+            'analytic_account_id': x.get('account_analytic_id', False),
+        }
+
+    def action_number(self, cr, uid, ids, context=None):
+        if context is None:
+            context = {}
+        #TODO: not correct fix but required a frech values before reading it.
+        self.write(cr, uid, ids, {})
+
+        for obj_inv in self.browse(cr, uid, ids, context=context):
+            id = obj_inv.id
+            invtype = obj_inv.type
+            number = obj_inv.number
+            move_id = obj_inv.move_id and obj_inv.move_id.id or False
+            reference = obj_inv.reference or ''
+
+            self.write(cr, uid, ids, {'internal_number':number})
+
+            if invtype in ('in_invoice', 'in_refund'):
+                if not reference:
+                    ref = self._convert_ref(cr, uid, number)
+                else:
+                    ref = reference
+            else:
+                ref = self._convert_ref(cr, uid, number)
+
+            cr.execute('UPDATE account_move SET ref=%s ' \
+                    'WHERE id=%s AND (ref is null OR ref = \'\')',
+                    (ref, move_id))
+            cr.execute('UPDATE account_move_line SET ref=%s ' \
+                    'WHERE move_id=%s AND (ref is null OR ref = \'\')',
+                    (ref, move_id))
+            cr.execute('UPDATE account_analytic_line SET ref=%s ' \
+                    'FROM account_move_line ' \
+                    'WHERE account_move_line.move_id = %s ' \
+                        'AND account_analytic_line.move_id = account_move_line.id',
+                        (ref, move_id))
+
+            for inv_id, name in self.name_get(cr, uid, [id]):
+                ctx = context.copy()
+                if obj_inv.type in ('out_invoice', 'out_refund'):
+                    ctx = self.get_log_context(cr, uid, context=ctx)
+                message = _("Invoice  '%s' is validated.") % name
+                self.log(cr, uid, inv_id, message, context=ctx)
+        return True
+
+    def action_cancel(self, cr, uid, ids, *args):
+        context = {} # TODO: Use context from arguments
+        account_move_obj = self.pool.get('account.move')
+        invoices = self.read(cr, uid, ids, ['move_id', 'payment_ids'])
+        move_ids = [] # ones that we will need to remove
+        for i in invoices:
+            if i['move_id']:
+                move_ids.append(i['move_id'][0])
+            if i['payment_ids']:
+                account_move_line_obj = self.pool.get('account.move.line')
+                pay_ids = account_move_line_obj.browse(cr, uid, i['payment_ids'])
+                for move_line in pay_ids:
+                    if move_line.reconcile_partial_id and move_line.reconcile_partial_id.line_partial_ids:
+                        raise osv.except_osv(_('Error !'), _('You can not cancel an invoice which is partially paid! You need to unreconcile related payment entries first!'))
+
+        # First, set the invoices as cancelled and detach the move ids
+        self.write(cr, uid, ids, {'state':'cancel', 'move_id':False})
+        if move_ids:
+            # second, invalidate the move(s)
+            account_move_obj.button_cancel(cr, uid, move_ids, context=context)
+            # delete the move this invoice was pointing to
+            # Note that the corresponding move_lines and move_reconciles
+            # will be automatically deleted too
+            account_move_obj.unlink(cr, uid, move_ids, context=context)
+        self._log_event(cr, uid, ids, -1.0, 'Cancel Invoice')
+        return True
+
+    ###################
+
+    def list_distinct_taxes(self, cr, uid, ids):
+        invoices = self.browse(cr, uid, ids)
+        taxes = {}
+        for inv in invoices:
+            for tax in inv.tax_line:
+                if not tax['name'] in taxes:
+                    taxes[tax['name']] = {'name': tax['name']}
+        return taxes.values()
+
+    def _log_event(self, cr, uid, ids, factor=1.0, name='Open Invoice'):
+        #TODO: implement messages system
+        return True
+
+    def name_get(self, cr, uid, ids, context=None):
+        if not ids:
+            return []
+        types = {
+                'out_invoice': 'CI: ',
+                'in_invoice': 'SI: ',
+                'out_refund': 'OR: ',
+                'in_refund': 'SR: ',
+                }
+        return [(r['id'], (r['number']) or types[r['type']] + (r['name'] or '')) for r in self.read(cr, uid, ids, ['type', 'number', 'name'], context, load='_classic_write')]
+
+    def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=100):
+        if not args:
+            args = []
+        if context is None:
+            context = {}
+        ids = []
+        if name:
+            ids = self.search(cr, user, [('number','=',name)] + args, limit=limit, context=context)
+        if not ids:
+            ids = self.search(cr, user, [('name',operator,name)] + args, limit=limit, context=context)
+        return self.name_get(cr, user, ids, context)
+
+    def _refund_cleanup_lines(self, cr, uid, lines):
+        for line in lines:
+            del line['id']
+            del line['invoice_id']
+            for field in ('company_id', 'partner_id', 'account_id', 'product_id',
+                          'uos_id', 'account_analytic_id', 'tax_code_id', 'base_code_id'):
+                if line.get(field):
+                    line[field] = line[field][0]
+            if 'invoice_line_tax_id' in line:
+                line['invoice_line_tax_id'] = [(6,0, line.get('invoice_line_tax_id', [])) ]
+        return map(lambda x: (0,0,x), lines)
+
+    def refund(self, cr, uid, ids, date=None, period_id=None, description=None, journal_id=None):
+        invoices = self.read(cr, uid, ids, ['name', 'type', 'number', 'reference', 'comment', 'date_due', 'partner_id', 'address_contact_id', 'address_invoice_id', 'partner_contact', 'partner_insite', 'partner_ref', 'payment_term', 'account_id', 'currency_id', 'invoice_line', 'tax_line', 'journal_id', 'user_id', 'fiscal_position'])
+        obj_invoice_line = self.pool.get('account.invoice.line')
+        obj_invoice_tax = self.pool.get('account.invoice.tax')
+        obj_journal = self.pool.get('account.journal')
+        new_ids = []
+        for invoice in invoices:
+            del invoice['id']
+
+            type_dict = {
+                'out_invoice': 'out_refund', # Customer Invoice
+                'in_invoice': 'in_refund',   # Supplier Invoice
+                'out_refund': 'out_invoice', # Customer Refund
+                'in_refund': 'in_invoice',   # Supplier Refund
+            }
+
+            invoice_lines = obj_invoice_line.read(cr, uid, invoice['invoice_line'])
+            invoice_lines = self._refund_cleanup_lines(cr, uid, invoice_lines)
+
+            tax_lines = obj_invoice_tax.read(cr, uid, invoice['tax_line'])
+            tax_lines = filter(lambda l: l['manual'], tax_lines)
+            tax_lines = self._refund_cleanup_lines(cr, uid, tax_lines)
+            if journal_id:
+                refund_journal_ids = [journal_id]
+            elif invoice['type'] == 'in_invoice':
+                refund_journal_ids = obj_journal.search(cr, uid, [('type','=','purchase_refund')])
+            else:
+                refund_journal_ids = obj_journal.search(cr, uid, [('type','=','sale_refund')])
+
+            if not date:
+                date = time.strftime('%Y-%m-%d')
+            invoice.update({
+                'type': type_dict[invoice['type']],
+                'date_invoice': date,
+                'state': 'draft',
+                'number': False,
+                'invoice_line': invoice_lines,
+                'tax_line': tax_lines,
+                'journal_id': refund_journal_ids
+            })
+            if period_id:
+                invoice.update({
+                    'period_id': period_id,
+                })
+            if description:
+                invoice.update({
+                    'name': description,
+                })
+            # take the id part of the tuple returned for many2one fields
+            for field in ('address_contact_id', 'address_invoice_id', 'partner_id',
+                    'account_id', 'currency_id', 'payment_term', 'journal_id',
+                    'user_id', 'fiscal_position'):
+                invoice[field] = invoice[field] and invoice[field][0]
+            # create the new invoice
+            new_ids.append(self.create(cr, uid, invoice))
+
+        return new_ids
+
+    def pay_and_reconcile(self, cr, uid, ids, pay_amount, pay_account_id, period_id, pay_journal_id, writeoff_acc_id, writeoff_period_id, writeoff_journal_id, context=None, name=''):
+        if context is None:
+            context = {}
+        #TODO check if we can use different period for payment and the writeoff line
+        assert len(ids)==1, "Can only pay one invoice at a time"
+        invoice = self.browse(cr, uid, ids[0], context=context)
+        src_account_id = invoice.account_id.id
+        # Take the seq as name for move
+        types = {'out_invoice': -1, 'in_invoice': 1, 'out_refund': 1, 'in_refund': -1}
+        direction = types[invoice.type]
+        #take the choosen date
+        if 'date_p' in context and context['date_p']:
+            date=context['date_p']
+        else:
+            date=time.strftime('%Y-%m-%d')
+
+        # Take the amount in currency and the currency of the payment
+        if 'amount_currency' in context and context['amount_currency'] and 'currency_id' in context and context['currency_id']:
+            amount_currency = context['amount_currency']
+            currency_id = context['currency_id']
+        else:
+            amount_currency = False
+            currency_id = False
+
+        pay_journal = self.pool.get('account.journal').read(cr, uid, pay_journal_id, ['type'], context=context)
+        if invoice.type in ('in_invoice', 'out_invoice'):
+            if pay_journal['type'] == 'bank':
+                entry_type = 'bank_pay_voucher' # Bank payment
+            else:
+                entry_type = 'pay_voucher' # Cash payment
+        else:
+            entry_type = 'cont_voucher'
+        if invoice.type in ('in_invoice', 'in_refund'):
+            ref = invoice.reference
+        else:
+            ref = self._convert_ref(cr, uid, invoice.number)
+        # Pay attention to the sign for both debit/credit AND amount_currency
+        l1 = {
+            'debit': direction * pay_amount>0 and direction * pay_amount,
+            'credit': direction * pay_amount<0 and - direction * pay_amount,
+            'account_id': src_account_id,
+            'partner_id': invoice.partner_id.id,
+            'ref':ref,
+            'date': date,
+            'currency_id':currency_id,
+            'amount_currency':amount_currency and direction * amount_currency or 0.0,
+            'company_id': invoice.company_id.id,
+        }
+        l2 = {
+            'debit': direction * pay_amount<0 and - direction * pay_amount,
+            'credit': direction * pay_amount>0 and direction * pay_amount,
+            'account_id': pay_account_id,
+            'partner_id': invoice.partner_id.id,
+            'ref':ref,
+            'date': date,
+            'currency_id':currency_id,
+            'amount_currency':amount_currency and - direction * amount_currency or 0.0,
+            'company_id': invoice.company_id.id,
+        }
+
+        if not name:
+            name = invoice.invoice_line and invoice.invoice_line[0].name or invoice.number
+        l1['name'] = name
+        l2['name'] = name
+
+        lines = [(0, 0, l1), (0, 0, l2)]
+        move = {'ref': ref, 'line_id': lines, 'journal_id': pay_journal_id, 'period_id': period_id, 'date': date}
+        move_id = self.pool.get('account.move').create(cr, uid, move, context=context)
+
+        line_ids = []
+        total = 0.0
+        line = self.pool.get('account.move.line')
+        move_ids = [move_id,]
+        if invoice.move_id:
+            move_ids.append(invoice.move_id.id)
+        cr.execute('SELECT id FROM account_move_line '\
+                   'WHERE move_id IN %s',
+                   ((move_id, invoice.move_id.id),))
+        lines = line.browse(cr, uid, map(lambda x: x[0], cr.fetchall()) )
+        for l in lines+invoice.payment_ids:
+            if l.account_id.id == src_account_id:
+                line_ids.append(l.id)
+                total += (l.debit or 0.0) - (l.credit or 0.0)
+
+        inv_id, name = self.name_get(cr, uid, [invoice.id], context=context)[0]
+        if (not round(total,self.pool.get('decimal.precision').precision_get(cr, uid, 'Account'))) or writeoff_acc_id:
+            self.pool.get('account.move.line').reconcile(cr, uid, line_ids, 'manual', writeoff_acc_id, writeoff_period_id, writeoff_journal_id, context)
+        else:
+            code = invoice.currency_id.symbol
+            # TODO: use currency's formatting function
+            msg = _("Invoice '%s' is paid partially: %s%s of %s%s (%s%s remaining)") % \
+                    (name, pay_amount, code, invoice.amount_total, code, total, code)
+            self.log(cr, uid, inv_id,  msg)
+            self.pool.get('account.move.line').reconcile_partial(cr, uid, line_ids, 'manual', context)
+
+        # Update the stored value (fields.function), so we write to trigger recompute
+        self.pool.get('account.invoice').write(cr, uid, ids, {}, context=context)
+        return True
+
+account_invoice()
+
+class account_invoice_line(osv.osv):
+
+    def _amount_line(self, cr, uid, ids, prop, unknow_none, unknow_dict):
+        res = {}
+        tax_obj = self.pool.get('account.tax')
+        cur_obj = self.pool.get('res.currency')
+        for line in self.browse(cr, uid, ids):
+            price = line.price_unit * (1-(line.discount or 0.0)/100.0)
+            taxes = tax_obj.compute_all(cr, uid, line.invoice_line_tax_id, price, line.quantity, product=line.product_id, address_id=line.invoice_id.address_invoice_id, partner=line.invoice_id.partner_id)
+            res[line.id] = taxes['total']
+            if line.invoice_id:
+                cur = line.invoice_id.currency_id
+                res[line.id] = cur_obj.round(cr, uid, cur, res[line.id])
+        return res
+
+    def _price_unit_default(self, cr, uid, context=None):
+        if context is None:
+            context = {}
+        if context.get('check_total', False):
+            t = context['check_total']
+            for l in context.get('invoice_line', {}):
+                if isinstance(l, (list, tuple)) and len(l) >= 3 and l[2]:
+                    tax_obj = self.pool.get('account.tax')
+                    p = l[2].get('price_unit', 0) * (1-l[2].get('discount', 0)/100.0)
+                    t = t - (p * l[2].get('quantity'))
+                    taxes = l[2].get('invoice_line_tax_id')
+                    if len(taxes[0]) >= 3 and taxes[0][2]:
+                        taxes = tax_obj.browse(cr, uid, list(taxes[0][2]))
+                        for tax in tax_obj.compute_all(cr, uid, taxes, p,l[2].get('quantity'), context.get('address_invoice_id', False), l[2].get('product_id', False), context.get('partner_id', False))['taxes']:
+                            t = t - tax['amount']
+            return t
+        return 0
+
+    _name = "account.invoice.line"
+    _description = "Invoice Line"
+    _columns = {
+        'name': fields.char('Description', size=256, required=True),
+        'origin': fields.char('Origin', size=256, help="Reference of the document that produced this invoice."),
+        'invoice_id': fields.many2one('account.invoice', 'Invoice Reference', ondelete='cascade', select=True),
+        'uos_id': fields.many2one('product.uom', 'Unit of Measure', ondelete='set null'),
+        'product_id': fields.many2one('product.product', 'Product', ondelete='set null'),
+        'account_id': fields.many2one('account.account', 'Account', required=True, domain=[('type','<>','view'), ('type', '<>', 'closed')], help="The income or expense account related to the selected product."),
+        'price_unit': fields.float('Unit Price', required=True, digits_compute= dp.get_precision('Account')),
+        'price_subtotal': fields.function(_amount_line, string='Subtotal', type="float",
+            digits_compute= dp.get_precision('Account'), store=True),
+        'quantity': fields.float('Quantity', required=True),
+        'discount': fields.float('Discount (%)', digits_compute= dp.get_precision('Account')),
+        'invoice_line_tax_id': fields.many2many('account.tax', 'account_invoice_line_tax', 'invoice_line_id', 'tax_id', 'Taxes', domain=[('parent_id','=',False)]),
+        'note': fields.text('Notes'),
+        'account_analytic_id':  fields.many2one('account.analytic.account', 'Analytic Account'),
+        'company_id': fields.related('invoice_id','company_id',type='many2one',relation='res.company',string='Company', store=True, readonly=True),
+        'partner_id': fields.related('invoice_id','partner_id',type='many2one',relation='res.partner',string='Partner',store=True)
+    }
+    _defaults = {
+        'quantity': 1,
+        'discount': 0.0,
+        'price_unit': _price_unit_default,
+    }
+
+    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
+        if context is None:
+            context = {}
+        res = super(account_invoice_line,self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
+        if context.get('type', False):
+            doc = etree.XML(res['arch'])
+            for node in doc.xpath("//field[@name='product_id']"):
+                if context['type'] in ('in_invoice', 'in_refund'):
+                    node.set('domain', "[('purchase_ok', '=', True)]")
+                else:
+                    node.set('domain', "[('sale_ok', '=', True)]")
+            res['arch'] = etree.tostring(doc)
+        return res
+
+    def product_id_change(self, cr, uid, ids, product, uom, qty=0, name='', type='out_invoice', partner_id=False, fposition_id=False, price_unit=False, address_invoice_id=False, currency_id=False, context=None, company_id=None):
+        if context is None:
+            context = {}
+        company_id = company_id if company_id != None else context.get('company_id',False)
+        context = dict(context)
+        context.update({'company_id': company_id})
+        if not partner_id:
+            raise osv.except_osv(_('No Partner Defined !'),_("You must first select a partner !") )
+        if not product:
+            if type in ('in_invoice', 'in_refund'):
+                return {'value': {}, 'domain':{'product_uom':[]}}
+            else:
+                return {'value': {'price_unit': 0.0}, 'domain':{'product_uom':[]}}
+        part = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context)
+        fpos_obj = self.pool.get('account.fiscal.position')
+        fpos = fposition_id and fpos_obj.browse(cr, uid, fposition_id, context=context) or False
+
+        if part.lang:
+            context.update({'lang': part.lang})
+        result = {}
+        res = self.pool.get('product.product').browse(cr, uid, product, context=context)
+
+        if type in ('out_invoice','out_refund'):
+            a = res.product_tmpl_id.property_account_income.id
+            if not a:
+                a = res.categ_id.property_account_income_categ.id
+        else:
+            a = res.product_tmpl_id.property_account_expense.id
+            if not a:
+                a = res.categ_id.property_account_expense_categ.id
+
+        if context.get('account_id',False):
+            # this is set by onchange_account_id() to force the account choosen by the
+            # user - to get defaults taxes when product have no tax defined.
+            a = context['account_id']
+
+        a = fpos_obj.map_account(cr, uid, fpos, a)
+        if a:
+            result['account_id'] = a
+
+        if type in ('out_invoice', 'out_refund'):
+            taxes = res.taxes_id and res.taxes_id or (a and self.pool.get('account.account').browse(cr, uid, a, context=context).tax_ids or False)
+        else:
+            taxes = res.supplier_taxes_id and res.supplier_taxes_id or (a and self.pool.get('account.account').browse(cr, uid, a, context=context).tax_ids or False)
+        tax_id = fpos_obj.map_tax(cr, uid, fpos, taxes)
+
+        if type in ('in_invoice', 'in_refund'):
+            result.update( {'price_unit': price_unit or res.standard_price,'invoice_line_tax_id': tax_id} )
+        else:
+            result.update({'price_unit': res.list_price, 'invoice_line_tax_id': tax_id})
+        result['name'] = res.partner_ref
+
+        domain = {}
+        result['uos_id'] = res.uom_id.id or uom or False
+        result['note'] = res.description
+        if result['uos_id']:
+            res2 = res.uom_id.category_id.id
+            if res2:
+                domain = {'uos_id':[('category_id','=',res2 )]}
+
+        res_final = {'value':result, 'domain':domain}
+
+        if not company_id or not currency_id:
+            return res_final
+
+        company = self.pool.get('res.company').browse(cr, uid, company_id, context=context)
+        currency = self.pool.get('res.currency').browse(cr, uid, currency_id, context=context)
+
+        if company.currency_id.id != currency.id:
+            if type in ('in_invoice', 'in_refund'):
+                res_final['value']['price_unit'] = res.standard_price
+            new_price = res_final['value']['price_unit'] * currency.rate
+            res_final['value']['price_unit'] = new_price
+
+        if uom:
+            uom = self.pool.get('product.uom').browse(cr, uid, uom, context=context)
+            if res.uom_id.category_id.id == uom.category_id.id:
+                new_price = res_final['value']['price_unit'] * uom.factor_inv
+                res_final['value']['price_unit'] = new_price
+        return res_final
+
+    def uos_id_change(self, cr, uid, ids, product, uom, qty=0, name='', type='out_invoice', partner_id=False, fposition_id=False, price_unit=False, address_invoice_id=False, currency_id=False, context=None, company_id=None):
+        if context is None:
+            context = {}
+        company_id = company_id if company_id != None else context.get('company_id',False)
+        context = dict(context)
+        context.update({'company_id': company_id})
+        warning = {}
+        res = self.product_id_change(cr, uid, ids, product, uom, qty, name, type, partner_id, fposition_id, price_unit, address_invoice_id, currency_id, context=context)
+        if 'uos_id' in res['value']:
+            del res['value']['uos_id']
+        if not uom:
+            res['value']['price_unit'] = 0.0
+        if product and uom:
+            prod = self.pool.get('product.product').browse(cr, uid, product, context=context)
+            prod_uom = self.pool.get('product.uom').browse(cr, uid, uom, context=context)
+            if prod.uom_id.category_id.id != prod_uom.category_id.id:
+                 warning = {
+                    'title': _('Warning!'),
+                    'message': _('You selected an Unit of Measure which is not compatible with the product.')
+            }
+            return {'value': res['value'], 'warning': warning}
+        return res
+
+    def move_line_get(self, cr, uid, invoice_id, context=None):
+        res = []
+        tax_obj = self.pool.get('account.tax')
+        cur_obj = self.pool.get('res.currency')
+        if context is None:
+            context = {}
+        inv = self.pool.get('account.invoice').browse(cr, uid, invoice_id, context=context)
+        company_currency = inv.company_id.currency_id.id
+
+        for line in inv.invoice_line:
+            mres = self.move_line_get_item(cr, uid, line, context)
+            if not mres:
+                continue
+            res.append(mres)
+            tax_code_found= False
+            for tax in tax_obj.compute_all(cr, uid, line.invoice_line_tax_id,
+                    (line.price_unit * (1.0 - (line['discount'] or 0.0) / 100.0)),
+                    line.quantity, inv.address_invoice_id.id, line.product_id,
+                    inv.partner_id)['taxes']:
+
+                if inv.type in ('out_invoice', 'in_invoice'):
+                    tax_code_id = tax['base_code_id']
+                    tax_amount = line.price_subtotal * tax['base_sign']
+                else:
+                    tax_code_id = tax['ref_base_code_id']
+                    tax_amount = line.price_subtotal * tax['ref_base_sign']
+
+                if tax_code_found:
+                    if not tax_code_id:
+                        continue
+                    res.append(self.move_line_get_item(cr, uid, line, context))
+                    res[-1]['price'] = 0.0
+                    res[-1]['account_analytic_id'] = False
+                elif not tax_code_id:
+                    continue
+                tax_code_found = True
+
+                res[-1]['tax_code_id'] = tax_code_id
+                res[-1]['tax_amount'] = cur_obj.compute(cr, uid, inv.currency_id.id, company_currency, tax_amount, context={'date': inv.date_invoice})
+        return res
+
+    def move_line_get_item(self, cr, uid, line, context=None):
+        return {
+            'type':'src',
+            'name': line.name[:64],
+            'price_unit':line.price_unit,
+            'quantity':line.quantity,
+            'price':line.price_subtotal,
+            'account_id':line.account_id.id,
+            'product_id':line.product_id.id,
+            'uos_id':line.uos_id.id,
+            'account_analytic_id':line.account_analytic_id.id,
+            'taxes':line.invoice_line_tax_id,
+        }
+    #
+    # Set the tax field according to the account and the fiscal position
+    #
+    def onchange_account_id(self, cr, uid, ids, product_id, partner_id, inv_type, fposition_id, account_id):
+        if not account_id:
+            return {}
+        unique_tax_ids = []
+        fpos = fposition_id and self.pool.get('account.fiscal.position').browse(cr, uid, fposition_id) or False
+        account = self.pool.get('account.account').browse(cr, uid, account_id)
+        if not product_id:
+            taxes = account.tax_ids
+            unique_tax_ids = self.pool.get('account.fiscal.position').map_tax(cr, uid, fpos, taxes)
+        else:
+            # force user choosen account in context to allow product_id_change()
+            # to fallback to the this accounts in case product has no taxes defined.
+            context = {'account_id': account_id}
+            product_change_result = self.product_id_change(cr, uid, ids, product_id, False, type=inv_type,
+                partner_id=partner_id, fposition_id=fposition_id, context=context,
+                company_id=account.company_id.id)
+            if product_change_result and 'value' in product_change_result and 'invoice_line_tax_id' in product_change_result['value']:
+                unique_tax_ids = product_change_result['value']['invoice_line_tax_id']
+        return {'value':{'invoice_line_tax_id': unique_tax_ids}}
+
+account_invoice_line()
+
+class account_invoice_tax(osv.osv):
+    _name = "account.invoice.tax"
+    _description = "Invoice Tax"
+
+    def _count_factor(self, cr, uid, ids, name, args, context=None):
+        res = {}
+        for invoice_tax in self.browse(cr, uid, ids, context=context):
+            res[invoice_tax.id] = {
+                'factor_base': 1.0,
+                'factor_tax': 1.0,
+            }
+            if invoice_tax.amount <> 0.0:
+                factor_tax = invoice_tax.tax_amount / invoice_tax.amount
+                res[invoice_tax.id]['factor_tax'] = factor_tax
+
+            if invoice_tax.base <> 0.0:
+                factor_base = invoice_tax.base_amount / invoice_tax.base
+                res[invoice_tax.id]['factor_base'] = factor_base
+
+        return res
+
+    _columns = {
+        'invoice_id': fields.many2one('account.invoice', 'Invoice Line', ondelete='cascade', select=True),
+        'name': fields.char('Tax Description', size=64, required=True),
+        'account_id': fields.many2one('account.account', 'Tax Account', required=True, domain=[('type','<>','view'),('type','<>','income'), ('type', '<>', 'closed')]),
+        'base': fields.float('Base', digits_compute=dp.get_precision('Account')),
+        'amount': fields.float('Amount', digits_compute=dp.get_precision('Account')),
+        'manual': fields.boolean('Manual'),
+        'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of invoice tax."),
+        'base_code_id': fields.many2one('account.tax.code', 'Base Code', help="The account basis of the tax declaration."),
+        'base_amount': fields.float('Base Code Amount', digits_compute=dp.get_precision('Account')),
+        'tax_code_id': fields.many2one('account.tax.code', 'Tax Code', help="The tax basis of the tax declaration."),
+        'tax_amount': fields.float('Tax Code Amount', digits_compute=dp.get_precision('Account')),
+        'company_id': fields.related('account_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True),
+        'factor_base': fields.function(_count_factor, string='Multipication factor for Base code', type='float', multi="all"),
+        'factor_tax': fields.function(_count_factor, string='Multipication factor Tax code', type='float', multi="all")
+    }
+
+    def base_change(self, cr, uid, ids, base, currency_id=False, company_id=False, date_invoice=False):
+        cur_obj = self.pool.get('res.currency')
+        company_obj = self.pool.get('res.company')
+        company_currency = False
+        factor = 1
+        if ids:
+            factor = self.read(cr, uid, ids[0], ['factor_base'])['factor_base']
+        if company_id:
+            company_currency = company_obj.read(cr, uid, [company_id], ['currency_id'])[0]['currency_id'][0]
+        if currency_id and company_currency:
+            base = cur_obj.compute(cr, uid, currency_id, company_currency, base*factor, context={'date': date_invoice or time.strftime('%Y-%m-%d')}, round=False)
+        return {'value': {'base_amount':base}}
+
+    def amount_change(self, cr, uid, ids, amount, currency_id=False, company_id=False, date_invoice=False):
+        cur_obj = self.pool.get('res.currency')
+        company_obj = self.pool.get('res.company')
+        company_currency = False
+        factor = 1
+        if ids:
+            factor = self.read(cr, uid, ids[0], ['factor_tax'])['factor_tax']
+        if company_id:
+            company_currency = company_obj.read(cr, uid, [company_id], ['currency_id'])[0]['currency_id'][0]
+        if currency_id and company_currency:
+            amount = cur_obj.compute(cr, uid, currency_id, company_currency, amount*factor, context={'date': date_invoice or time.strftime('%Y-%m-%d')}, round=False)
+        return {'value': {'tax_amount': amount}}
+
+    _order = 'sequence'
+    _defaults = {
+        'manual': 1,
+        'base_amount': 0.0,
+        'tax_amount': 0.0,
+    }
+    def compute(self, cr, uid, invoice_id, context=None):
+        tax_grouped = {}
+        tax_obj = self.pool.get('account.tax')
+        cur_obj = self.pool.get('res.currency')
+        inv = self.pool.get('account.invoice').browse(cr, uid, invoice_id, context=context)
+        cur = inv.currency_id
+        company_currency = inv.company_id.currency_id.id
+
+        for line in inv.invoice_line:
+            for tax in tax_obj.compute_all(cr, uid, line.invoice_line_tax_id, (line.price_unit* (1-(line.discount or 0.0)/100.0)), line.quantity, inv.address_invoice_id.id, line.product_id, inv.partner_id)['taxes']:
+                val={}
+                val['invoice_id'] = inv.id
+                val['name'] = tax['name']
+                val['amount'] = tax['amount']
+                val['manual'] = False
+                val['sequence'] = tax['sequence']
+                #[Fix] Tax calculation rounding
+                val['base'] = cur_obj.round(cr, uid, cur, tax['price_unit'] * line['quantity'])
+                if inv.type in ('out_invoice','in_invoice'):
+                    val['base_code_id'] = tax['base_code_id']
+                    val['tax_code_id'] = tax['tax_code_id']
+                    val['base_amount'] = cur_obj.compute(cr, uid, inv.currency_id.id, company_currency, val['base'] * tax['base_sign'], context={'date': inv.date_invoice or time.strftime('%Y-%m-%d')}, round=False)
+                    val['tax_amount'] = cur_obj.compute(cr, uid, inv.currency_id.id, company_currency, val['amount'] * tax['tax_sign'], context={'date': inv.date_invoice or time.strftime('%Y-%m-%d')}, round=False)
+                    val['account_id'] = tax['account_collected_id'] or line.account_id.id
+                else:
+                    val['base_code_id'] = tax['ref_base_code_id']
+                    val['tax_code_id'] = tax['ref_tax_code_id']
+                    val['base_amount'] = cur_obj.compute(cr, uid, inv.currency_id.id, company_currency, val['base'] * tax['ref_base_sign'], context={'date': inv.date_invoice or time.strftime('%Y-%m-%d')}, round=False)
+                    val['tax_amount'] = cur_obj.compute(cr, uid, inv.currency_id.id, company_currency, val['amount'] * tax['ref_tax_sign'], context={'date': inv.date_invoice or time.strftime('%Y-%m-%d')}, round=False)
+                    val['account_id'] = tax['account_paid_id'] or line.account_id.id
+
+                key = (val['tax_code_id'], val['base_code_id'], val['account_id'])
+                if not key in tax_grouped:
+                    tax_grouped[key] = val
+                else:
+                    tax_grouped[key]['amount'] += val['amount']
+                    tax_grouped[key]['base'] += val['base']
+                    tax_grouped[key]['base_amount'] += val['base_amount']
+                    tax_grouped[key]['tax_amount'] += val['tax_amount']
+
+        for t in tax_grouped.values():
+            t['base'] = cur_obj.round(cr, uid, cur, t['base'])
+            t['amount'] = cur_obj.round(cr, uid, cur, t['amount'])
+            t['base_amount'] = cur_obj.round(cr, uid, cur, t['base_amount'])
+            t['tax_amount'] = cur_obj.round(cr, uid, cur, t['tax_amount'])
+        return tax_grouped
+
+    def move_line_get(self, cr, uid, invoice_id):
+        res = []
+        cr.execute('SELECT * FROM account_invoice_tax WHERE invoice_id=%s', (invoice_id,))
+        for t in cr.dictfetchall():
+            if not t['amount'] \
+                    and not t['tax_code_id'] \
+                    and not t['tax_amount']:
+                continue
+            res.append({
+                'type':'tax',
+                'name':t['name'],
+                'price_unit': t['amount'],
+                'quantity': 1,
+                'price': t['amount'] or 0.0,
+                'account_id': t['account_id'],
+                'tax_code_id': t['tax_code_id'],
+                'tax_amount': t['tax_amount']
+            })
+        return res
+
+account_invoice_tax()
+
+
+class res_partner(osv.osv):
+    """ Inherits partner and adds invoice information in the partner form """
+    _inherit = 'res.partner'
+    _columns = {
+        'invoice_ids': fields.one2many('account.invoice.line', 'partner_id', 'Invoices', readonly=True),
+    }
+
+    def copy(self, cr, uid, id, default=None, context=None):
+        default = default or {}
+        default.update({'invoice_ids' : []})
+        return super(res_partner, self).copy(cr, uid, id, default, context)
+
+res_partner()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'account/account_invoice_view.xml'
--- account/account_invoice_view.xml	1970-01-01 00:00:00 +0000
+++ account/account_invoice_view.xml	2013-06-05 16:26:39 +0000
@@ -0,0 +1,516 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+    <data>
+        <!--
+        Invoices
+        -->
+        <record id="view_invoice_line_calendar" model="ir.ui.view">
+            <field name="name">account.invoice.calendar</field>
+            <field name="model">account.invoice</field>
+            <field name="type">calendar</field>
+            <field name="arch" type="xml">
+                <calendar string="Invoices" color="journal_id" date_start="date_invoice">
+                    <field name="partner_id"/>
+                    <field name="amount_total"/>
+                </calendar>
+            </field>
+        </record>
+
+        <record model="ir.ui.view" id="view_invoice_graph">
+            <field name="name">account.invoice.graph</field>
+            <field name="model">account.invoice</field>
+            <field name="type">graph</field>
+            <field name="arch" type="xml">
+                <graph string="Invoices" type="bar">
+                    <field name="partner_id"/>
+                    <field name="amount_total" operator="+"/>
+                </graph>
+            </field>
+        </record>
+
+        <record id="view_invoice_line_tree" model="ir.ui.view">
+            <field name="name">account.invoice.line.tree</field>
+            <field name="model">account.invoice.line</field>
+            <field name="type">tree</field>
+            <field name="arch" type="xml">
+                <tree string="Invoice Line">
+                    <field name="name"/>
+                    <field name="account_id" groups="account.group_account_user"/>
+                    <field name="quantity"/>
+                    <field name="uos_id"/>
+                    <field name="price_unit"/>
+                    <field name="discount" groups="base.group_extended"/>
+                    <field name="price_subtotal"/>
+                </tree>
+            </field>
+        </record>
+
+        <record id="view_invoice_line_form" model="ir.ui.view">
+            <field name="name">account.invoice.line.form</field>
+            <field name="model">account.invoice.line</field>
+            <field name="type">form</field>
+            <field name="arch" type="xml">
+                <form string="Invoice Line">
+                    <field name="product_id" on_change="product_id_change(product_id, uos_id, quantity, name, parent.type, parent.partner_id, parent.fiscal_position, price_unit, parent.address_invoice_id, parent.currency_id, context, parent.company_id)"/>
+                    <field colspan="2" name="name"/>
+                    <label string="Quantity :" align="1.0"/>
+                    <group colspan="1" col="2">
+                        <field name="quantity" nolabel="1"/>
+                        <field name="uos_id" on_change="uos_id_change(product_id, uos_id, quantity, name, parent.type, parent.partner_id, parent.fiscal_position, price_unit, parent.address_invoice_id, parent.currency_id, context, parent.company_id)" nolabel="1"/>
+                    </group>
+                    <field name="price_unit"/>
+                    <field domain="[('company_id', '=', parent.company_id), ('journal_id', '=', parent.journal_id), ('type', '&lt;&gt;', 'view')]" name="account_id" on_change="onchange_account_id(product_id, parent.partner_id, parent.type, parent.fiscal_position,account_id)"/>
+                    <field name="discount" groups="base.group_extended"/>
+                    <field domain="[('type','&lt;&gt;','view'), ('company_id', '=', parent.company_id), ('parent_id', '!=', False)]" name="account_analytic_id" groups="analytic.group_analytic_accounting"/>
+                    <field name="company_id" groups="base.group_multi_company" readonly="1"/>
+                    <separator string="Notes" colspan="4"/>
+                    <field colspan="4" name="note" nolabel="1"/>
+                    <separator colspan="4" string="Taxes"/>
+                    <field colspan="4" name="invoice_line_tax_id" context="{'type':parent.type}" domain="[('parent_id','=',False),('company_id', '=', parent.company_id)]" nolabel="1"/>
+                </form>
+            </field>
+        </record>
+
+        <record id="view_invoice_tax_tree" model="ir.ui.view">
+            <field name="name">account.invoice.tax.tree</field>
+            <field name="model">account.invoice.tax</field>
+            <field name="type">tree</field>
+            <field name="arch" type="xml">
+                <tree string="Manual Invoice Taxes">
+                    <field name="sequence"/>
+                    <field name="manual"/>
+                    <field name="name"/>
+                    <field name="account_id" groups="base.group_account_user"/>
+                    <field name="base"/>
+                    <field name="amount"/>
+                </tree>
+            </field>
+        </record>
+
+        <record id="view_invoice_tax_form" model="ir.ui.view">
+            <field name="name">account.invoice.tax.form</field>
+            <field name="model">account.invoice.tax</field>
+            <field name="type">form</field>
+            <field name="arch" type="xml">
+                <form string="Manual Invoice Taxes">
+                    <field name="name"/>
+                    <field name="sequence"/>
+                    <field name="account_id"/>
+                    <field name="manual"/>
+                    <field name="amount"/>
+                    <field name="base" readonly="0"/>
+                    <separator colspan="4" string="Tax codes"/>
+                    <field name="base_code_id"/>
+                    <field name="base_amount"/>
+                    <field name="tax_code_id"/>
+                    <field name="tax_amount"/>
+                    <field name="factor_base" invisible="True"/>
+                    <field name="factor_tax" invisible="True"/>
+                </form>
+            </field>
+        </record>
+
+        <record id="invoice_tree" model="ir.ui.view">
+            <field name="name">account.invoice.tree</field>
+            <field name="model">account.invoice</field>
+            <field name="type">tree</field>
+            <field name="arch" type="xml">
+                <tree colors="blue:state == 'draft';black:state in ('proforma','proforma2','open');gray:state == 'cancel'" string="Invoice">
+                    <field name="date_invoice"/>
+                    <field name="number"/>
+                    <field name="partner_id" groups="base.group_user"/>
+                    <field name="reference" invisible="1"/>
+                    <field name="name" invisible="1"/>
+                    <field name="journal_id" invisible="1"/>
+                    <field name="period_id" invisible="1" groups="account.group_account_user"/>
+                    <field name="company_id" groups="base.group_multi_company" widget="selection"/>
+                    <field name="user_id"/>
+                    <field name="date_due"/>
+                    <field name="origin"/>
+                    <field name="currency_id"/>
+                    <field name="residual" sum="Residual Amount"/>
+                    <field name="amount_untaxed" sum="Untaxed Amount"/>
+                    <field name="amount_total" sum="Total Amount"/>
+                    <field name="state"/>
+
+                    <button name="invoice_open" states="draft,proforma2" string="Approve" icon="terp-camera_test"/>
+                </tree>
+            </field>
+        </record>
+
+        <record id="invoice_supplier_form" model="ir.ui.view">
+            <field name="name">account.invoice.supplier.form</field>
+            <field name="model">account.invoice</field>
+            <field name="type">form</field>
+            <field name="priority">2</field>
+            <field name="arch" type="xml">
+                <form string="Supplier Invoice">
+                    <group col="8" colspan="4">
+                        <field name="journal_id" on_change="onchange_journal_id(journal_id)" widget="selection"/>
+                        <field name="number" readonly="1"/>
+                        <field name="type" invisible="1"/>
+                        <field name="currency_id" width="50"/>
+                        <button name="%(action_account_change_currency)d" type="action" icon="terp-stock_effects-object-colorize" string="Change" attrs="{'invisible':[('state','!=','draft')]}" groups="account.group_account_user"/>
+                        <newline/>
+                        <field string="Supplier" name="partner_id" on_change="onchange_partner_id(type,partner_id,date_invoice,payment_term, partner_bank_id,company_id)" context="{'default_customer': 0, 'search_default_supplier': 1, 'default_supplier': 1}" options='{"quick_create": false}' domain="[('supplier', '=', True)]"/>
+                        <field domain="[('partner_id','=',partner_id)]" name="address_invoice_id" context="{'default_partner_id': partner_id}" options='{"quick_create": false}'/>
+                        <field name="fiscal_position" groups="base.group_extended" widget="selection"/>
+                        <newline/>
+                        <field name="date_invoice"/>
+                        <field name="period_id" domain="[('state', '=', 'draft')]" groups="account.group_account_user" widget="selection"/>
+                        <group colspan="2" col="1" groups="account.group_account_user">
+                            <label align="0.0" string="(keep empty to use the current period)"/>
+                        </group>
+                    </group>
+                    <notebook colspan="4">
+                        <page string="Invoice">
+                           <field domain="[('company_id', '=', company_id), ('type', '=', 'payable')]" name="account_id" groups="account.group_account_user"/>
+                            <field name="reference_type" nolabel="1" size="0"/>
+                            <field name="reference" nolabel="1"/>
+                            <field name="date_due"/>
+                            <field colspan="4" context="{'address_invoice_id': address_invoice_id, 'partner_id': partner_id, 'price_type': 'price_type' in dir() and price_type or False, 'type': type}" name="invoice_line" nolabel="1">
+                                <tree string="Invoice lines">
+                                    <field name="product_id" on_change="product_id_change(product_id, uos_id, quantity, name, parent.type, parent.partner_id, parent.fiscal_position, price_unit, parent.address_invoice_id, parent.currency_id, context, parent.company_id)"/>
+                                    <field domain="[('company_id', '=', parent.company_id), ('journal_id', '=', parent.journal_id), ('type', '&lt;&gt;', 'view')]" name="account_id" on_change="onchange_account_id(product_id,parent.partner_id,parent.type,parent.fiscal_position,account_id)"/>
+                                    <field name="invoice_line_tax_id" view_mode="2" context="{'type':parent.type}" domain="[('parent_id','=',False)]"/>
+                                    <field domain="[('type','&lt;&gt;','view'), ('company_id', '=', parent.company_id), ('parent_id', '!=', False)]" name="account_analytic_id" groups="analytic.group_analytic_accounting"/>
+                                    <field name="quantity"/>
+                                    <field name="price_unit"/>
+                                     <!-- Removed if subtotal is set -->
+                                    <field name="price_subtotal"/>
+                                    <field invisible="True" name="name"/>
+                                    <field invisible="True" name="uos_id"/>
+                                </tree>
+                            </field>
+                            <group col="1" colspan="2">
+                                <field name="tax_line" nolabel="1">
+                                    <tree editable="bottom" string="Taxes">
+                                        <field name="name"/>
+                                        <field name="account_id" groups="account.group_account_invoice"/>
+                                        <field name="base" on_change="base_change(base,parent.currency_id,parent.company_id,parent.date_invoice)" readonly="1"/>
+                                        <field name="amount" on_change="amount_change(amount,parent.currency_id,parent.company_id,parent.date_invoice)"/>
+
+                                        <field invisible="True" name="base_amount"/>
+                                        <field invisible="True" name="tax_amount"/>
+                                        <field name="factor_base" invisible="True"/>
+                                        <field name="factor_tax" invisible="True"/>
+                                    </tree>
+                                </field>
+                            </group>
+                            <group col="4" colspan="2">
+                                <button colspan="2" name="button_reset_taxes" states="draft" string="Compute Taxes" type="object" icon="terp-stock_format-scientific" help="This action will erase taxes"/>
+                                <field name="amount_untaxed"/>
+                                <label string="" colspan="2"/>
+                                <field name="amount_tax"/>
+                                <field name="reconciled"/>
+                                <field name="amount_total"/>
+                                <field name="state" widget="statusbar" statusbar_visible="draft,open,paid" statusbar_colors='{"proforma":"blue","proforma2":"blue"}'/>
+                                <field name="residual"/>
+                                <group col="6" colspan="4">
+                                    <button name="invoice_cancel" states="draft,proforma2,sale,open" string="Cancel" icon="gtk-cancel" groups="base.group_no_one"/>
+                                    <button name="action_cancel_draft" states="cancel" string="Set to Draft" type="object" icon="terp-stock_effects-object-colorize"/>
+                                    <button name="%(action_account_invoice_refund)d" type='action' string='Refund' states='open,paid' icon="gtk-execute"/>
+                                    <button name='%(action_account_state_open)d' type='action' string='Re-Open' groups="account.group_account_invoice" attrs="{'invisible':['|', ('state','&lt;&gt;','paid'), ('reconciled', '=', True)]}" icon="gtk-convert" help="This button only appears when the state of the invoice is 'paid' (showing that it has been fully reconciled) and auto-computed boolean 'reconciled' is False (depicting that it's not the case anymore). In other words, the invoice has been dereconciled and it does not fit anymore the 'paid' state. You should press this button to re-open it and let it continue its normal process after having resolved the eventual exceptions it may have created."/>
+                                    <button name="invoice_open" states="draft,proforma2" string="Approve" icon="terp-camera_test"/>
+                                </group>
+                            </group>
+                        </page>
+                        <page string="Other Info">
+                            <field domain="[('partner_id', '=', partner_id)]" name="partner_bank_id" on_change="onchange_partner_bank(partner_bank_id)"/>
+                            <field name="company_id" on_change="onchange_company_id(company_id,partner_id,type,invoice_line,currency_id)" widget="selection" groups="base.group_multi_company"/>
+                            <newline/>
+                            <field name="payment_term" widget="selection"/>
+                            <field name="name"/>
+                            <newline/>
+                            <field name="origin" groups="base.group_extended"/>
+                            <field domain="[('partner_id','=',partner_id)]" name="address_contact_id" groups="base.group_extended"/>
+                            <field name="user_id"/>
+                            <field name="move_id" groups="account.group_account_user"/>
+                            <separator colspan="4" string="Additional Information"/>
+                            <field colspan="4" name="comment" nolabel="1"/>
+                        </page>
+                        <page string="Payments" groups="base.group_extended">
+                            <field name="payment_ids" colspan="4" nolabel="1" >
+                                <tree string="Payments">
+                                    <field name="date" string="Payment Date"/>
+                                    <field name="move_id"/>
+                                    <field name="ref"/>
+                                    <field name="name" groups="base.group_extended"/>
+                                    <field name="journal_id"/>
+                                    <field name="debit"/>
+                                    <field name="credit"/>
+                                    <field name="amount_currency" groups="base.group_extended"/>
+                                    <field name="currency_id" groups="base.group_extended"/>
+                                </tree>
+                            </field>
+                        </page>
+                    </notebook>
+                </form>
+            </field>
+        </record>
+
+        <record id="invoice_form" model="ir.ui.view">
+            <field name="name">account.invoice.form</field>
+            <field name="model">account.invoice</field>
+            <field name="type">form</field>
+            <field name="arch" type="xml">
+                <form string="Invoice">
+                    <group colspan="4" col="8">
+                    <field name="journal_id" groups="base.group_user" on_change="onchange_journal_id(journal_id, context)" widget="selection"/>
+                    <field name="number"/>
+                    <field name="type" invisible="1"/>
+                    <field name="currency_id" width="50"/>
+                    <button name="%(action_account_change_currency)d" type="action" icon="terp-stock_effects-object-colorize" string="Change" attrs="{'invisible':[('state','!=','draft')]}" groups="account.group_account_user"/>
+                    <newline/>
+                    <field string="Customer" name="partner_id" on_change="onchange_partner_id(type,partner_id,date_invoice,payment_term, partner_bank_id,company_id)" groups="base.group_user" context="{'search_default_customer': 1}" options='{"quick_create": false}' domain="[('customer', '=', True)]"/>
+                    <field domain="[('partner_id','=',partner_id)]" name="address_invoice_id" context="{'default_partner_id': partner_id}" options='{"quick_create": false}'/>
+                    <field name="fiscal_position" groups="base.group_extended" widget="selection" options='{"quick_create": false}'/>
+                    <newline/>
+                    <field name="date_invoice"/>
+                    <field name="period_id" domain="[('state', '=', 'draft')]" groups="account.group_account_user" widget="selection"/>
+                    <field name="payment_term" widget="selection"/>
+                    <newline/>
+                    <field domain="[('company_id', '=', company_id),('type','=', 'receivable')]" name="account_id" groups="account.group_account_user"/>
+                    <field name="name" groups="base.group_extended"/>
+                    </group>
+                    <notebook colspan="4">
+                        <page string="Invoice">
+                            <field colspan="4" name="invoice_line" nolabel="1" widget="one2many_list" context="{'type': type}"/>
+                            <group col="1" colspan="2">
+                                <field name="tax_line" nolabel="1">
+                                    <tree editable="bottom" string="Taxes">
+                                        <field name="name"/>
+                                        <field name="account_id" groups="account.group_account_invoice"/>
+                                        <field name="base" on_change="base_change(base,parent.currency_id,parent.company_id,parent.date_invoice)" readonly="1"/>
+                                        <field name="amount" on_change="amount_change(amount,parent.currency_id,parent.company_id,parent.date_invoice)"/>
+                                        <field invisible="True" name="base_amount"/>
+                                        <field invisible="True" name="tax_amount"/>
+                                        <field name="factor_base" invisible="True"/>
+                                        <field name="factor_tax" invisible="True"/>
+                                    </tree>
+                                </field>
+                            </group>
+                            <group col="4" colspan="2">
+                                <group colspan="2" col="1">
+                                    <button name="button_reset_taxes" states="draft,proforma2" string="Compute Taxes" type="object" groups="base.group_user" icon="terp-stock_format-scientific" help="This action will erase taxes"/>
+                                </group>
+                                <field name="amount_untaxed"/>
+                                <label string="" colspan="2"/>
+                                <field name="amount_tax"/>
+                                <field name="reconciled"/>
+                                <field name="amount_total"/>
+                                <field name="state" widget="statusbar" statusbar_visible="draft,open,paid" statusbar_colors='{"proforma":"blue","proforma2":"blue"}'/>
+                                <field name="residual"/>
+                                <group col="8" colspan="4" groups="base.group_user">
+                                    <button name="invoice_cancel" states="draft,proforma2,sale,open" string="Cancel" icon="gtk-cancel" groups="base.group_no_one"/>
+                                    <button name="action_cancel_draft" states="cancel" string="Reset to Draft" type="object" icon="terp-stock_effects-object-colorize"/>
+                                    <button name='%(action_account_state_open)d' type='action' string='Re-Open' groups="account.group_account_invoice" attrs="{'invisible':['|', ('state','&lt;&gt;','paid'), ('reconciled', '=', True)]}" icon="gtk-convert" help="This button only appears when the state of the invoice is 'paid' (showing that it has been fully reconciled) and auto-computed boolean 'reconciled' is False (depicting that it's not the case anymore). In other words, the invoice has been dereconciled and it does not fit anymore the 'paid' state. You should press this button to re-open it and let it continue its normal process after having resolved the eventual exceptions it may have created."/>
+                                    <button name="%(action_account_invoice_refund)d" type='action' string='Refund' states='open,paid' icon="gtk-execute"/>
+                                    <button name="invoice_proforma2" states="draft" string="PRO-FORMA" icon="terp-gtk-media-pause" groups="account.group_account_user"/>
+                                    <button name="invoice_open" states="draft,proforma2" string="Validate" icon="gtk-go-forward"/>
+                                    <button name="%(account_invoices)d" string="Print Invoice" type="action" icon="gtk-print" states="open,paid,proforma,sale,proforma2"/>
+                                </group>
+                            </group>
+                        </page>
+                        <page string="Other Info">
+                            <field name="company_id" on_change="onchange_company_id(company_id,partner_id,type,invoice_line,currency_id)" widget="selection" groups="base.group_multi_company"/>
+                            <newline/>
+                            <field name="date_due"/>
+                            <field name="user_id"/>
+                            <newline/>
+                            <field domain="[('partner_id.ref_companies', 'in', [company_id])]" name="partner_bank_id"
+                                groups="base.group_extended"/>
+                            <field name="origin"/>
+                            <field colspan="4" domain="[('partner_id','=',partner_id)]" name="address_contact_id"
+                                groups="base.group_extended"/>
+                            <field name="move_id" groups="account.group_account_user"/>
+                            <separator colspan="4" string="Additional Information"/>
+                            <field colspan="4" name="comment" nolabel="1"/>
+                        </page>
+                        <page string="Payments">
+                            <field name="payment_ids" colspan="4" nolabel="1">
+                                <tree string="Payments">
+                                    <field name="date"/>
+                                    <field name="move_id"/>
+                                    <field name="ref"/>
+                                    <field name="name"/>
+                                    <field name="journal_id" groups="base.group_user"/>
+                                    <field name="debit"/>
+                                    <field name="credit"/>
+                                    <field name="amount_currency"/>
+                                    <field name="currency_id"/>
+                                </tree>
+                            </field>
+                        </page>
+                    </notebook>
+                </form>
+            </field>
+        </record>
+
+        <record id="view_account_invoice_filter" model="ir.ui.view">
+            <field name="name">account.invoice.select</field>
+            <field name="model">account.invoice</field>
+            <field name="type">search</field>
+            <field name="arch" type="xml">
+                <search string="Search Invoice">
+                   <group>
+                        <filter name="draft" icon="terp-document-new" string="Draft" domain="[('state','=','draft')]" help="Draft Invoices"/>
+                        <filter name="proforma" icon="terp-gtk-media-pause" string="Proforma" domain="[('state','=','proforma2')]" help="Proforma Invoices"/>
+                        <filter name="invoices" icon="terp-dolar" string="Invoices" domain="[('state','not in',['draft','cancel'])]" help="Proforma/Open/Paid Invoices"/>
+                        <separator orientation="vertical"/>
+                        <filter name="unpaid" icon="terp-dolar_ok!" string="Unpaid" domain="[('state','=','open')]" help="Unpaid Invoices"/>
+                        <separator orientation="vertical"/>
+                        <field name="number"
+                            string="Reference"
+                            filter_domain="['|', ('number','ilike',self),('origin','ilike',self)]"/>
+                        <field name="partner_id"/>
+                        <field name="user_id" widget="selection" string="Salesman">
+                            <filter domain="[('user_id','=',uid)]" help="My invoices" icon="terp-personal" />
+                        </field>
+                    </group>
+                    <newline/>
+                    <group>
+                        <field name="journal_id" widget="selection"/>
+                        <field name="period_id" string="Period"/>
+                    </group>
+                    <newline/>
+                    <group expand="0" string="Group By...">
+                        <filter string="Partner" icon="terp-partner" domain="[]" context="{'group_by':'partner_id'}"/>
+                        <filter string="Responsible" icon="terp-personal" domain="[]"  context="{'group_by':'user_id'}"/>
+                        <separator orientation="vertical"/>
+                        <filter string="Journal" icon="terp-folder-orange" domain="[]" context="{'group_by':'journal_id'}"/>
+                        <filter string="State" icon="terp-stock_effects-object-colorize" domain="[]"  context="{'group_by':'state'}"/>
+                        <separator orientation="vertical"/>
+                        <filter string="Period" icon="terp-go-month" domain="[]" context="{'group_by':'period_id'}"/>
+                        <filter string="Invoice Date" icon="terp-go-month" domain="[]"  context="{'group_by':'date_invoice'}"/>
+                        <filter string="Due Date" icon="terp-go-month" domain="[]"  context="{'group_by':'date_due'}"/>
+                    </group>
+               </search>
+            </field>
+        </record>
+
+        <record id="action_invoice_tree" model="ir.actions.act_window">
+            <field name="name">Invoices</field>
+            <field name="res_model">account.invoice</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">tree,form,calendar,graph</field>
+            <field name="view_id" ref="invoice_tree"/>
+            <field name="context">{'type':'out_invoice'}</field>
+            <field name="search_view_id" ref="view_account_invoice_filter"/>
+        </record>
+
+        <record id="action_invoice_tree_pending_invoice" model="ir.actions.act_window">
+            <field name="name">Pending Invoice</field>
+            <field name="res_model">account.invoice</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">tree,form,calendar,graph</field>
+            <field name="view_id" ref="invoice_tree"/>
+            <field name="context">{'type':'out_invoice'}</field>
+            <field name="domain">[('state','=','draft')]</field>
+           <!-- <field name="search_view_id" ref="view_account_invoice_filter"/>-->
+        </record>
+
+        <record id="action_invoice_tree_view1" model="ir.actions.act_window.view">
+            <field eval="1" name="sequence"/>
+            <field name="view_mode">tree</field>
+            <field name="act_window_id" ref="action_invoice_tree"/>
+        </record>
+
+        <record id="action_invoice_tree_view2" model="ir.actions.act_window.view">
+            <field eval="2" name="sequence"/>
+            <field name="view_mode">form</field>
+            <field name="view_id" ref="invoice_form"/>
+            <field name="act_window_id" ref="action_invoice_tree"/>
+        </record>
+
+        <record id="action_invoice_tree1" model="ir.actions.act_window">
+            <field name="name">Customer Invoices</field>
+            <field name="res_model">account.invoice</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">tree,form,calendar,graph</field>
+            <field eval="False" name="view_id"/>
+            <field name="domain">[('type','=','out_invoice')]</field>
+            <field name="context">{'default_type':'out_invoice', 'type':'out_invoice', 'journal_type': 'sale'}</field>
+            <field name="search_view_id" ref="view_account_invoice_filter"/>
+            <field name="help">With Customer Invoices you can create and manage sales invoices issued to your customers. OpenERP can also generate draft invoices automatically from sales orders or deliveries. You should only confirm them before sending them to your customers.</field>
+        </record>
+
+
+        <record id="action_invoice_tree1_view1" model="ir.actions.act_window.view">
+            <field eval="1" name="sequence"/>
+            <field name="view_mode">tree</field>
+            <field name="act_window_id" ref="action_invoice_tree1"/>
+        </record>
+
+        <record id="action_invoice_tree1_view2" model="ir.actions.act_window.view">
+            <field eval="2" name="sequence"/>
+            <field name="view_mode">form</field>
+            <field name="view_id" ref="invoice_form"/>
+            <field name="act_window_id" ref="action_invoice_tree1"/>
+        </record>
+
+        <menuitem action="action_invoice_tree1" id="menu_action_invoice_tree1" parent="menu_finance_receivables"/>
+
+        <record id="action_invoice_tree2" model="ir.actions.act_window">
+            <field name="name">Supplier Invoices</field>
+            <field name="res_model">account.invoice</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">tree,form,calendar,graph</field>
+            <field eval="False" name="view_id"/>
+            <field name="domain">[('type','=','in_invoice')]</field>
+            <field name="context">{'default_type': 'in_invoice', 'type': 'in_invoice', 'journal_type': 'purchase'}</field>
+            <field name="search_view_id" ref="view_account_invoice_filter"/>
+            <field name="help">With Supplier Invoices you can enter and manage invoices issued by your suppliers. OpenERP can also generate draft invoices automatically from purchase orders or receipts. This way, you can control the invoice from your supplier according to what you purchased or received.</field>
+        </record>
+        <menuitem action="action_invoice_tree2" id="menu_action_invoice_tree2" parent="menu_finance_payables"/>
+
+        <record id="action_invoice_tree3" model="ir.actions.act_window">
+            <field name="name">Customer Refunds</field>
+            <field name="res_model">account.invoice</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">tree,form,calendar,graph</field>
+            <field eval="False" name="view_id"/>
+            <field name="domain">[('type','=','out_refund')]</field>
+            <field name="context">{'default_type':'out_refund', 'type':'out_refund', 'journal_type': 'sale_refund'}</field>
+            <field name="search_view_id" ref="view_account_invoice_filter"/>
+            <field name="help">With Customer Refunds you can manage the credit notes for your customers. A refund is a document that credits an invoice completely or partially. You can easily generate refunds and reconcile them directly from the invoice form.</field>
+        </record>
+
+        <record id="action_invoice_tree3_view1" model="ir.actions.act_window.view">
+            <field eval="1" name="sequence"/>
+            <field name="view_mode">tree</field>
+            <field name="act_window_id" ref="action_invoice_tree3"/>
+        </record>
+
+        <record id="action_invoice_tree3_view2" model="ir.actions.act_window.view">
+            <field eval="2" name="sequence"/>
+            <field name="view_mode">form</field>
+            <field name="view_id" ref="invoice_form"/>
+            <field name="act_window_id" ref="action_invoice_tree3"/>
+        </record>
+        <menuitem action="action_invoice_tree3" id="menu_action_invoice_tree3" parent="menu_finance_receivables"/>
+
+        <record id="action_invoice_tree4" model="ir.actions.act_window">
+            <field name="name">Supplier Refunds</field>
+            <field name="res_model">account.invoice</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">tree,form,calendar,graph</field>
+            <field eval="False" name="view_id"/>
+            <field name="domain">[('type','=','in_refund')]</field>
+            <field name="context">{'default_type': 'in_refund', 'type': 'in_refund', 'journal_type': 'purchase_refund'}</field>
+            <field name="search_view_id" ref="view_account_invoice_filter"/>
+            <field name="help">With Supplier Refunds you can manage the credit notes you receive from your suppliers. A refund is a document that credits an invoice completely or partially. You can easily generate refunds and reconcile them directly from the invoice form.</field>
+        </record>
+        <menuitem action="action_invoice_tree4" id="menu_action_invoice_tree4" parent="menu_finance_payables"/>
+
+        <act_window context="{'search_default_partner_id':[active_id], 'default_partner_id': active_id}" id="act_res_partner_2_account_invoice_opened" name="Invoices" res_model="account.invoice" src_model="res.partner"/>
+
+        <act_window
+           id="act_account_journal_2_account_invoice_opened"
+           name="Unpaid Invoices"
+           context="{'search_default_journal_id': [active_id], 'search_default_unpaid':1, 'default_journal_id': active_id}"
+           res_model="account.invoice"
+           src_model="account.journal"/>
+
+    </data>
+</openerp>

=== added file 'account/account_invoice_workflow.xml'
--- account/account_invoice_workflow.xml	1970-01-01 00:00:00 +0000
+++ account/account_invoice_workflow.xml	2013-06-05 16:26:39 +0000
@@ -0,0 +1,115 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+    <data>
+
+        <record id="wkf" model="workflow">
+            <field name="name">account.invoice.basic</field>
+            <field name="osv">account.invoice</field>
+            <field name="on_create">True</field>
+        </record>
+
+        <record id="act_draft" model="workflow.activity">
+            <field name="wkf_id" ref="wkf"/>
+            <field name="flow_start">True</field>
+            <field name="name">draft</field>
+        </record>
+
+        <record id="act_proforma2" model="workflow.activity">
+            <field name="wkf_id" ref="wkf"/>
+            <field name="name">proforma2</field>
+            <field name="action">write({'state':'proforma2'})</field>
+            <field name="kind">function</field>
+        </record>
+
+        <record id="act_open" model="workflow.activity">
+            <field name="wkf_id" ref="wkf"/>
+            <field name="name">open</field>
+            <field name="action">action_date_assign()
+action_move_create()
+action_number()
+write({'state':'open'})</field>
+            <field name="kind">function</field>
+        </record>
+        <record model="workflow.activity" id="act_open_test">
+            <field name="wkf_id" ref="wkf"/>
+            <field name="name">re-open</field>
+            <field name="action">write({'state':'open'})</field>
+            <field name="kind">function</field>
+           </record>
+        <record id="act_paid" model="workflow.activity">
+            <field name="wkf_id" ref="wkf"/>
+            <field name="name">paid</field>
+            <!--<field name="flow_stop">True</field>-->
+            <field name="action">confirm_paid()</field>
+            <field name="kind">function</field>
+            <field name="signal_send">subflow.paid</field>
+        </record>
+        <record id="act_cancel" model="workflow.activity">
+            <field name="wkf_id" ref="wkf"/>
+            <field name="name">cancel</field>
+            <field name="flow_stop">True</field>
+            <field name="action">action_cancel()
+write({'state':'cancel'})</field>
+            <field name="kind">function</field>
+        </record>
+
+        <record id="t4" model="workflow.transition">
+            <field name="act_from" ref="act_draft"/>
+            <field name="act_to" ref="act_open"/>
+            <field name="signal">invoice_open</field>
+        </record>
+        <record id="t8" model="workflow.transition">
+            <field name="act_from" ref="act_open"/>
+            <field name="act_to" ref="act_paid"/>
+            <field name="trigger_model">account.move.line</field>
+            <field name="trigger_expr_id">move_line_id_payment_get()</field>
+            <field name="condition">test_paid()</field>
+        </record>
+        <record id="t9" model="workflow.transition">
+            <field name="act_from" ref="act_draft"/>
+            <field name="act_to" ref="act_cancel"/>
+            <field name="signal">invoice_cancel</field>
+        </record>
+        <record id="t12" model="workflow.transition">
+            <field name="act_from" ref="act_open"/>
+            <field name="act_to" ref="act_cancel"/>
+            <field name="signal">invoice_cancel</field>
+        </record>
+        <record id="t13" model="workflow.transition">
+            <field name="act_from" ref="act_paid"/>
+            <field name="act_to" ref="act_open_test"/>
+            <field name="signal">open_test</field>
+        </record>
+        <record id="t14" model="workflow.transition">
+            <field name="act_from" ref="act_open_test"/>
+            <field name="act_to" ref="act_cancel"/>
+            <field name="signal">invoice_cancel</field>
+        </record>
+
+        <record id="open_test_to_paid" model="workflow.transition">
+            <field name="act_from" ref="act_open_test"/>
+            <field name="act_to" ref="act_paid"/>
+            <field name="trigger_model">account.move.line</field>
+            <field name="trigger_expr_id">move_line_id_payment_get()</field>
+            <field name="condition">test_paid()</field>
+        </record>
+
+        <record id="draft_to_pro2" model="workflow.transition">
+            <field name="act_from" ref="account.act_draft"/>
+            <field name="act_to" ref="act_proforma2"/>
+            <field name="signal">invoice_proforma2</field>
+        </record>
+
+        <record id="pro2_to_open" model="workflow.transition">
+            <field name="act_from" ref="act_proforma2"/>
+            <field name="act_to" ref="account.act_open"/>
+            <field name="signal">invoice_open</field>
+        </record>
+
+        <record id="pro2_to_cancel" model="workflow.transition">
+            <field name="act_from" ref="act_proforma2"/>
+            <field name="act_to" ref="account.act_cancel"/>
+            <field name="signal">invoice_cancel</field>
+        </record>
+    </data>
+</openerp>

=== added file 'account/account_menuitem.xml'
--- account/account_menuitem.xml	1970-01-01 00:00:00 +0000
+++ account/account_menuitem.xml	2013-06-05 16:26:39 +0000
@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+    <data>
+
+        <menuitem icon="terp-account" id="menu_finance" name="Accounting" sequence="13"
+            groups="group_account_user,group_account_manager,group_account_invoice"
+            web_icon="images/accounting.png"
+            web_icon_hover="images/accounting-hover.png"/>
+        <menuitem id="menu_finance_receivables" name="Customers" parent="menu_finance" sequence="2"/>
+        <menuitem id="menu_finance_payables" name="Suppliers" parent="menu_finance" sequence="3"/>
+        <menuitem id="menu_finance_bank_and_cash" name="Bank and Cash" parent="menu_finance" sequence="4"
+            groups="group_account_user,group_account_manager"/>
+        <menuitem id="menu_finance_periodical_processing" name="Periodical Processing" parent="menu_finance" sequence="9" groups="group_account_user,group_account_manager"/>
+        <!-- This menu is used in account_code module -->
+        <menuitem id="menu_account_pp_statements" name="Statements" parent="menu_finance_periodical_processing" sequence="12"/>
+        <menuitem id="periodical_processing_journal_entries_validation" name="Draft Entries" parent="menu_finance_periodical_processing"/>
+        <menuitem id="periodical_processing_reconciliation" name="Reconciliation" parent="menu_finance_periodical_processing"/>
+        <menuitem id="periodical_processing_invoicing" name="Invoicing" parent="menu_finance_periodical_processing"/>
+        <menuitem id="menu_finance_charts" name="Charts" parent="menu_finance" groups="account.group_account_user" sequence="6"/>
+        <menuitem id="menu_finance_reporting" name="Reporting" parent="account.menu_finance" sequence="13"/>
+        <menuitem id="menu_finance_reporting_budgets" name="Budgets" parent="menu_finance_reporting" groups="group_account_user"/>
+        <menuitem id="menu_finance_legal_statement" name="Legal Reports" parent="menu_finance_reporting"/>
+        <menuitem id="menu_finance_management_belgian_reports" name="Belgian Reports" parent="menu_finance_reporting"/>
+        <menuitem id="menu_finance_configuration" name="Configuration" parent="menu_finance" sequence="14" groups="group_account_manager"/>
+        <menuitem id="menu_finance_accounting" name="Financial Accounting" parent="menu_finance_configuration"/>
+        <menuitem id="menu_analytic_accounting" name="Analytic Accounting" parent="menu_finance_configuration" groups="analytic.group_analytic_accounting"/>
+        <menuitem id="menu_analytic" parent="menu_analytic_accounting" name="Accounts" groups="analytic.group_analytic_accounting"/>
+        <menuitem id="menu_journals" sequence="9" name="Journals" parent="menu_finance_accounting" groups="group_account_manager"/>
+        <menuitem id="menu_configuration_misc" name="Miscellaneous" parent="menu_finance_configuration" sequence="30"/>
+        <menuitem id="base.menu_action_currency_form" parent="menu_configuration_misc" sequence="20"/>
+        <menuitem id="menu_finance_generic_reporting" name="Generic Reporting" parent="menu_finance_reporting" sequence="100"/>
+        <menuitem id="menu_finance_entries" name="Journal Entries" parent="menu_finance" sequence="5" groups="group_account_user,group_account_manager"/>
+        <menuitem id="menu_account_reports" name="Financial Reports" parent="menu_finance_accounting" sequence="18"/>
+
+        <menuitem id="account.menu_finance_recurrent_entries" name="Recurring Entries"
+            parent="menu_finance_periodical_processing" sequence="15"
+            groups="base.group_extended"/>
+
+        <menuitem id="menu_account_end_year_treatments"
+            name="End of Period" parent="menu_finance_periodical_processing"
+            sequence="25"/>
+        <menuitem id="menu_finance_periodical_processing_billing" name="Billing" parent="menu_finance_periodical_processing" sequence="35"/>
+
+        <menuitem id="menu_finance_statistic_report_statement" name="Statistic Reports" parent="menu_finance_reporting" sequence="300"/>
+        <menuitem id="next_id_22" name="Partners" parent="menu_finance_generic_reporting" sequence="1"/>
+        <menuitem id="menu_multi_currency" name="Multi-Currencies" parent="menu_finance_generic_reporting" sequence="10"/>
+        <menuitem
+            parent="account.menu_finance_legal_statement"
+            id="final_accounting_reports"
+            name="Accounting Reports"/>
+
+        <menuitem
+            parent="account.menu_finance_legal_statement"
+            id="menu_journals_report"
+            groups="group_account_user,group_account_manager"
+            name="Journals"/>
+
+    </data>
+</openerp>
+

=== added file 'account/account_move_line.py'
--- account/account_move_line.py	1970-01-01 00:00:00 +0000
+++ account/account_move_line.py	2013-06-05 16:26:39 +0000
@@ -0,0 +1,1413 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
+#
+#    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/>.
+#
+##############################################################################
+
+import time
+from datetime import datetime
+from operator import itemgetter
+
+from lxml import etree
+
+import netsvc
+from osv import fields, osv, orm
+from tools.translate import _
+import decimal_precision as dp
+import tools
+
+class account_move_line(osv.osv):
+    _name = "account.move.line"
+    _description = "Journal Items"
+
+    def _query_get(self, cr, uid, obj='l', context=None):
+        fiscalyear_obj = self.pool.get('account.fiscalyear')
+        fiscalperiod_obj = self.pool.get('account.period')
+        account_obj = self.pool.get('account.account')
+        fiscalyear_ids = []
+        if context is None:
+            context = {}
+        initial_bal = context.get('initial_bal', False)
+        company_clause = " "
+        if context.get('company_id', False):
+            company_clause = " AND " +obj+".company_id = %s" % context.get('company_id', False)
+        if not context.get('fiscalyear', False):
+            if context.get('all_fiscalyear', False):
+                #this option is needed by the aged balance report because otherwise, if we search only the draft ones, an open invoice of a closed fiscalyear won't be displayed
+                fiscalyear_ids = fiscalyear_obj.search(cr, uid, [])
+            else:
+                fiscalyear_ids = fiscalyear_obj.search(cr, uid, [('state', '=', 'draft')])
+        else:
+            #for initial balance as well as for normal query, we check only the selected FY because the best practice is to generate the FY opening entries
+            fiscalyear_ids = [context['fiscalyear']]
+
+        fiscalyear_clause = (','.join([str(x) for x in fiscalyear_ids])) or '0'
+        state = context.get('state', False)
+        where_move_state = ''
+        where_move_lines_by_date = ''
+
+        if context.get('date_from', False) and context.get('date_to', False):
+            if initial_bal:
+                where_move_lines_by_date = " AND " +obj+".move_id IN (SELECT id FROM account_move WHERE date < '" +context['date_from']+"')"
+            else:
+                where_move_lines_by_date = " AND " +obj+".move_id IN (SELECT id FROM account_move WHERE date >= '" +context['date_from']+"' AND date <= '"+context['date_to']+"')"
+
+        if state:
+            if state.lower() not in ['all']:
+                where_move_state= " AND "+obj+".move_id IN (SELECT id FROM account_move WHERE account_move.state = '"+state+"')"
+        if context.get('period_from', False) and context.get('period_to', False) and not context.get('periods', False):
+            if initial_bal:
+                period_company_id = fiscalperiod_obj.browse(cr, uid, context['period_from'], context=context).company_id.id
+                first_period = fiscalperiod_obj.search(cr, uid, [('company_id', '=', period_company_id)], order='date_start', limit=1)[0]
+                context['periods'] = fiscalperiod_obj.build_ctx_periods(cr, uid, first_period, context['period_from'])
+            else:
+                context['periods'] = fiscalperiod_obj.build_ctx_periods(cr, uid, context['period_from'], context['period_to'])
+        if context.get('periods', False):
+            if initial_bal:
+                query = obj+".state <> 'draft' AND "+obj+".period_id IN (SELECT id FROM account_period WHERE fiscalyear_id IN (%s)) %s %s" % (fiscalyear_clause, where_move_state, where_move_lines_by_date)
+                period_ids = fiscalperiod_obj.search(cr, uid, [('id', 'in', context['periods'])], order='date_start', limit=1)
+                if period_ids and period_ids[0]:
+                    first_period = fiscalperiod_obj.browse(cr, uid, period_ids[0], context=context)
+                    ids = ','.join([str(x) for x in context['periods']])
+                    query = obj+".state <> 'draft' AND "+obj+".period_id IN (SELECT id FROM account_period WHERE fiscalyear_id IN (%s) AND date_start <= '%s' AND id NOT IN (%s)) %s %s" % (fiscalyear_clause, first_period.date_start, ids, where_move_state, where_move_lines_by_date)
+            else:
+                ids = ','.join([str(x) for x in context['periods']])
+                query = obj+".state <> 'draft' AND "+obj+".period_id IN (SELECT id FROM account_period WHERE fiscalyear_id IN (%s) AND id IN (%s)) %s %s" % (fiscalyear_clause, ids, where_move_state, where_move_lines_by_date)
+        else:
+            query = obj+".state <> 'draft' AND "+obj+".period_id IN (SELECT id FROM account_period WHERE fiscalyear_id IN (%s)) %s %s" % (fiscalyear_clause, where_move_state, where_move_lines_by_date)
+
+        if initial_bal and not context.get('periods', False) and not where_move_lines_by_date:
+            #we didn't pass any filter in the context, and the initial balance can't be computed using only the fiscalyear otherwise entries will be summed twice
+            #so we have to invalidate this query
+            raise osv.except_osv(_('Warning !'),_("You haven't supplied enough argument to compute the initial balance, please select a period and journal in the context."))
+
+
+        if context.get('journal_ids', False):
+            query += ' AND '+obj+'.journal_id IN (%s)' % ','.join(map(str, context['journal_ids']))
+
+        if context.get('chart_account_id', False):
+            child_ids = account_obj._get_children_and_consol(cr, uid, [context['chart_account_id']], context=context)
+            query += ' AND '+obj+'.account_id IN (%s)' % ','.join(map(str, child_ids))
+
+        query += company_clause
+        return query
+
+    def _amount_residual(self, cr, uid, ids, field_names, args, context=None):
+        """
+           This function returns the residual amount on a receivable or payable account.move.line.
+           By default, it returns an amount in the currency of this journal entry (maybe different
+           of the company currency), but if you pass 'residual_in_company_currency' = True in the
+           context then the returned amount will be in company currency.
+        """
+        res = {}
+        if context is None:
+            context = {}
+        cur_obj = self.pool.get('res.currency')
+        for move_line in self.browse(cr, uid, ids, context=context):
+            res[move_line.id] = {
+                'amount_residual': 0.0,
+                'amount_residual_currency': 0.0,
+            }
+
+            if move_line.reconcile_id:
+                continue
+            if not move_line.account_id.type in ('payable', 'receivable'):
+                #this function does not suport to be used on move lines not related to payable or receivable accounts
+                continue
+
+            if move_line.currency_id:
+                move_line_total = move_line.amount_currency
+                sign = move_line.amount_currency < 0 and -1 or 1
+            else:
+                move_line_total = move_line.debit - move_line.credit
+                sign = (move_line.debit - move_line.credit) < 0 and -1 or 1
+            line_total_in_company_currency =  move_line.debit - move_line.credit
+            context_unreconciled = context.copy()
+            if move_line.reconcile_partial_id:
+                for payment_line in move_line.reconcile_partial_id.line_partial_ids:
+                    if payment_line.id == move_line.id:
+                        continue
+                    if payment_line.currency_id and move_line.currency_id and payment_line.currency_id.id == move_line.currency_id.id:
+                            move_line_total += payment_line.amount_currency
+                    else:
+                        if move_line.currency_id:
+                            context_unreconciled.update({'date': payment_line.date})
+                            amount_in_foreign_currency = cur_obj.compute(cr, uid, move_line.company_id.currency_id.id, move_line.currency_id.id, (payment_line.debit - payment_line.credit), round=False, context=context_unreconciled)
+                            move_line_total += amount_in_foreign_currency
+                        else:
+                            move_line_total += (payment_line.debit - payment_line.credit)
+                    line_total_in_company_currency += (payment_line.debit - payment_line.credit)
+
+            result = move_line_total
+            res[move_line.id]['amount_residual_currency'] =  sign * (move_line.currency_id and self.pool.get('res.currency').round(cr, uid, move_line.currency_id, result) or result)
+            res[move_line.id]['amount_residual'] = sign * line_total_in_company_currency
+        return res
+
+    def default_get(self, cr, uid, fields, context=None):
+        data = self._default_get(cr, uid, fields, context=context)
+        for f in data.keys():
+            if f not in fields:
+                del data[f]
+        return data
+
+    def create_analytic_lines(self, cr, uid, ids, context=None):
+        acc_ana_line_obj = self.pool.get('account.analytic.line')
+        for obj_line in self.browse(cr, uid, ids, context=context):
+            if obj_line.analytic_account_id:
+                if not obj_line.journal_id.analytic_journal_id:
+                    raise osv.except_osv(_('No Analytic Journal !'),_("You have to define an analytic journal on the '%s' journal!") % (obj_line.journal_id.name, ))
+                amt = (obj_line.credit or  0.0) - (obj_line.debit or 0.0)
+                vals_lines = {
+                    'name': obj_line.name,
+                    'date': obj_line.date,
+                    'account_id': obj_line.analytic_account_id.id,
+                    'unit_amount': obj_line.quantity,
+                    'product_id': obj_line.product_id and obj_line.product_id.id or False,
+                    'product_uom_id': obj_line.product_uom_id and obj_line.product_uom_id.id or False,
+                    'amount': amt,
+                    'general_account_id': obj_line.account_id.id,
+                    'journal_id': obj_line.journal_id.analytic_journal_id.id,
+                    'ref': obj_line.ref,
+                    'move_id': obj_line.id,
+                    'user_id': uid
+                }
+                acc_ana_line_obj.create(cr, uid, vals_lines)
+        return True
+
+    def _default_get_move_form_hook(self, cursor, user, data):
+        '''Called in the end of default_get method for manual entry in account_move form'''
+        if data.has_key('analytic_account_id'):
+            del(data['analytic_account_id'])
+        if data.has_key('account_tax_id'):
+            del(data['account_tax_id'])
+        return data
+
+    def convert_to_period(self, cr, uid, context=None):
+        if context is None:
+            context = {}
+        #check if the period_id changed in the context from client side
+        period_id = context.get('period_id')
+        if isinstance(period_id, basestring):
+            ids = self.pool.get('account.period').search(cr, uid, [('name', 'ilike', period_id)])
+            context['period_id'] = ids and ids[0] or False
+        return context
+
+    def _default_get(self, cr, uid, fields, context=None):
+        if context is None:
+            context = {}
+        if not context.get('journal_id', False) and context.get('search_default_journal_id', False):
+            context['journal_id'] = context.get('search_default_journal_id')
+        account_obj = self.pool.get('account.account')
+        period_obj = self.pool.get('account.period')
+        journal_obj = self.pool.get('account.journal')
+        move_obj = self.pool.get('account.move')
+        tax_obj = self.pool.get('account.tax')
+        fiscal_pos_obj = self.pool.get('account.fiscal.position')
+        partner_obj = self.pool.get('res.partner')
+        currency_obj = self.pool.get('res.currency')
+        context = self.convert_to_period(cr, uid, context)
+        # Compute simple values
+        data = super(account_move_line, self).default_get(cr, uid, fields, context=context)
+        # Starts: Manual entry from account.move form
+        if context.get('lines'):
+            total_new = context.get('balance', 0.00)
+            if context['journal']:
+                journal_data = journal_obj.browse(cr, uid, context['journal'], context=context)
+                if journal_data.type == 'purchase':
+                    if total_new > 0:
+                        account = journal_data.default_credit_account_id
+                    else:
+                        account = journal_data.default_debit_account_id
+                else:
+                    if total_new > 0:
+                        account = journal_data.default_credit_account_id
+                    else:
+                        account = journal_data.default_debit_account_id
+                if account and ((not fields) or ('debit' in fields) or ('credit' in fields)) and 'partner_id' in data and (data['partner_id']):
+                    part = partner_obj.browse(cr, uid, data['partner_id'], context=context)
+                    account = fiscal_pos_obj.map_account(cr, uid, part and part.property_account_position or False, account.id)
+                    account = account_obj.browse(cr, uid, account, context=context)
+                    data['account_id'] =  account.id
+
+            s = -total_new
+            data['debit'] = s > 0 and s or 0.0
+            data['credit'] = s < 0 and -s or 0.0
+            data = self._default_get_move_form_hook(cr, uid, data)
+            return data
+        # Ends: Manual entry from account.move form
+        if not 'move_id' in fields: #we are not in manual entry
+            return data
+        # Compute the current move
+        move_id = False
+        partner_id = False
+        if context.get('journal_id', False) and context.get('period_id', False):
+            if 'move_id' in fields:
+                cr.execute('SELECT move_id \
+                    FROM \
+                        account_move_line \
+                    WHERE \
+                        journal_id = %s and period_id = %s AND create_uid = %s AND state = %s \
+                    ORDER BY id DESC limit 1',
+                    (context['journal_id'], context['period_id'], uid, 'draft'))
+                res = cr.fetchone()
+                move_id = (res and res[0]) or False
+                if not move_id:
+                    return data
+                else:
+                    data['move_id'] = move_id
+            if 'date' in fields:
+                cr.execute('SELECT date \
+                    FROM \
+                        account_move_line \
+                    WHERE \
+                        journal_id = %s AND period_id = %s AND create_uid = %s \
+                    ORDER BY id DESC',
+                    (context['journal_id'], context['period_id'], uid))
+                res = cr.fetchone()
+                if res:
+                    data['date'] = res[0]
+                else:
+                    period = period_obj.browse(cr, uid, context['period_id'],
+                            context=context)
+                    data['date'] = period.date_start
+        if not move_id:
+            return data
+        total = 0
+        ref_id = False
+        move = move_obj.browse(cr, uid, move_id, context=context)
+        if 'name' in fields:
+            data.setdefault('name', move.line_id[-1].name)
+        acc1 = False
+        for l in move.line_id:
+            acc1 = l.account_id
+            partner_id = partner_id or l.partner_id.id
+            ref_id = ref_id or l.ref
+            total += (l.debit or 0.0) - (l.credit or 0.0)
+
+        if 'ref' in fields:
+            data['ref'] = ref_id
+        if 'partner_id' in fields:
+            data['partner_id'] = partner_id
+
+        if move.journal_id.type == 'purchase':
+            if total > 0:
+                account = move.journal_id.default_credit_account_id
+            else:
+                account = move.journal_id.default_debit_account_id
+        else:
+            if total > 0:
+                account = move.journal_id.default_credit_account_id
+            else:
+                account = move.journal_id.default_debit_account_id
+        part = partner_id and partner_obj.browse(cr, uid, partner_id) or False
+        # part = False is acceptable for fiscal position.
+        account = fiscal_pos_obj.map_account(cr, uid, part and part.property_account_position or False, account.id)
+        if account:
+            account = account_obj.browse(cr, uid, account, context=context)
+
+        if account and ((not fields) or ('debit' in fields) or ('credit' in fields)):
+            data['account_id'] = account.id
+            # Propose the price VAT excluded, the VAT will be added when confirming line
+            if account.tax_ids:
+                taxes = fiscal_pos_obj.map_tax(cr, uid, part and part.property_account_position or False, account.tax_ids)
+                tax = tax_obj.browse(cr, uid, taxes)
+                for t in tax_obj.compute_inv(cr, uid, tax, total, 1):
+                    total -= t['amount']
+
+        s = -total
+        data['debit'] = s > 0  and s or 0.0
+        data['credit'] = s < 0  and -s or 0.0
+
+        if account and account.currency_id:
+            data['currency_id'] = account.currency_id.id
+            acc = account
+            if s>0:
+                acc = acc1
+            compute_ctx = context.copy()
+            compute_ctx.update({
+                    'res.currency.compute.account': acc,
+                    'res.currency.compute.account_invert': True,
+                })
+            v = currency_obj.compute(cr, uid, account.company_id.currency_id.id, data['currency_id'], s, context=compute_ctx)
+            data['amount_currency'] = v
+        return data
+
+    def on_create_write(self, cr, uid, id, context=None):
+        if not id:
+            return []
+        ml = self.browse(cr, uid, id, context=context)
+        return map(lambda x: x.id, ml.move_id.line_id)
+
+    def _balance(self, cr, uid, ids, name, arg, context=None):
+        if context is None:
+            context = {}
+        c = context.copy()
+        c['initital_bal'] = True
+        sql = """SELECT l2.id, SUM(l1.debit-l1.credit)
+                    FROM account_move_line l1, account_move_line l2
+                    WHERE l2.account_id = l1.account_id
+                      AND l1.id <= l2.id
+                      AND l2.id IN %s AND """ + \
+                self._query_get(cr, uid, obj='l1', context=c) + \
+                " GROUP BY l2.id"
+
+        cr.execute(sql, [tuple(ids)])
+        return dict(cr.fetchall())
+
+    def _invoice(self, cursor, user, ids, name, arg, context=None):
+        invoice_obj = self.pool.get('account.invoice')
+        res = {}
+        for line_id in ids:
+            res[line_id] = False
+        cursor.execute('SELECT l.id, i.id ' \
+                        'FROM account_move_line l, account_invoice i ' \
+                        'WHERE l.move_id = i.move_id ' \
+                        'AND l.id IN %s',
+                        (tuple(ids),))
+        invoice_ids = []
+        for line_id, invoice_id in cursor.fetchall():
+            res[line_id] = invoice_id
+            invoice_ids.append(invoice_id)
+        invoice_names = {False: ''}
+        for invoice_id, name in invoice_obj.name_get(cursor, user, invoice_ids, context=context):
+            invoice_names[invoice_id] = name
+        for line_id in res.keys():
+            invoice_id = res[line_id]
+            res[line_id] = (invoice_id, invoice_names[invoice_id])
+        return res
+
+    def name_get(self, cr, uid, ids, context=None):
+        if not ids:
+            return []
+        result = []
+        for line in self.browse(cr, uid, ids, context=context):
+            if line.ref:
+                result.append((line.id, (line.move_id.name or '')+' ('+line.ref+')'))
+            else:
+                result.append((line.id, line.move_id.name))
+        return result
+
+    def _balance_search(self, cursor, user, obj, name, args, domain=None, context=None):
+        if context is None:
+            context = {}
+        if not args:
+            return []
+        where = ' AND '.join(map(lambda x: '(abs(sum(debit-credit))'+x[1]+str(x[2])+')',args))
+        cursor.execute('SELECT id, SUM(debit-credit) FROM account_move_line \
+                     GROUP BY id, debit, credit having '+where)
+        res = cursor.fetchall()
+        if not res:
+            return [('id', '=', '0')]
+        return [('id', 'in', [x[0] for x in res])]
+
+    def _invoice_search(self, cursor, user, obj, name, args, context=None):
+        if not args:
+            return []
+        invoice_obj = self.pool.get('account.invoice')
+        i = 0
+        while i < len(args):
+            fargs = args[i][0].split('.', 1)
+            if len(fargs) > 1:
+                args[i] = (fargs[0], 'in', invoice_obj.search(cursor, user,
+                    [(fargs[1], args[i][1], args[i][2])]))
+                i += 1
+                continue
+            if isinstance(args[i][2], basestring):
+                res_ids = invoice_obj.name_search(cursor, user, args[i][2], [],
+                        args[i][1])
+                args[i] = (args[i][0], 'in', [x[0] for x in res_ids])
+            i += 1
+        qu1, qu2 = [], []
+        for x in args:
+            if x[1] != 'in':
+                if (x[2] is False) and (x[1] == '='):
+                    qu1.append('(i.id IS NULL)')
+                elif (x[2] is False) and (x[1] == '<>' or x[1] == '!='):
+                    qu1.append('(i.id IS NOT NULL)')
+                else:
+                    qu1.append('(i.id %s %s)' % (x[1], '%s'))
+                    qu2.append(x[2])
+            elif x[1] == 'in':
+                if len(x[2]) > 0:
+                    qu1.append('(i.id IN (%s))' % (','.join(['%s'] * len(x[2]))))
+                    qu2 += x[2]
+                else:
+                    qu1.append(' (False)')
+        if qu1:
+            qu1 = ' AND' + ' AND'.join(qu1)
+        else:
+            qu1 = ''
+        cursor.execute('SELECT l.id ' \
+                'FROM account_move_line l, account_invoice i ' \
+                'WHERE l.move_id = i.move_id ' + qu1, qu2)
+        res = cursor.fetchall()
+        if not res:
+            return [('id', '=', '0')]
+        return [('id', 'in', [x[0] for x in res])]
+
+    def _get_move_lines(self, cr, uid, ids, context=None):
+        result = []
+        for move in self.pool.get('account.move').browse(cr, uid, ids, context=context):
+            for line in move.line_id:
+                result.append(line.id)
+        return result
+
+    _columns = {
+        'name': fields.char('Name', size=64, required=True),
+        'quantity': fields.float('Quantity', digits=(16,2), help="The optional quantity expressed by this line, eg: number of product sold. The quantity is not a legal requirement but is very useful for some reports."),
+        'product_uom_id': fields.many2one('product.uom', 'UoM'),
+        'product_id': fields.many2one('product.product', 'Product'),
+        'debit': fields.float('Debit', digits_compute=dp.get_precision('Account')),
+        'credit': fields.float('Credit', digits_compute=dp.get_precision('Account')),
+        'account_id': fields.many2one('account.account', 'Account', required=True, ondelete="cascade", domain=[('type','<>','view'), ('type', '<>', 'closed')], select=2),
+        'move_id': fields.many2one('account.move', 'Move', ondelete="cascade", help="The move of this entry line.", select=2, required=True),
+        'narration': fields.related('move_id','narration', type='text', relation='account.move', string='Internal Note'),
+        'ref': fields.related('move_id', 'ref', string='Reference', type='char', size=64, store=True),
+        'statement_id': fields.many2one('account.bank.statement', 'Statement', help="The bank statement used for bank reconciliation", select=1),
+        'reconcile_id': fields.many2one('account.move.reconcile', 'Reconcile', readonly=True, ondelete='set null', select=2),
+        'reconcile_partial_id': fields.many2one('account.move.reconcile', 'Partial Reconcile', readonly=True, ondelete='set null', select=2),
+        'amount_currency': fields.float('Amount Currency', help="The amount expressed in an optional other currency if it is a multi-currency entry.", digits_compute=dp.get_precision('Account')),
+        'amount_residual_currency': fields.function(_amount_residual, string='Residual Amount', multi="residual", help="The residual amount on a receivable or payable of a journal entry expressed in its currency (maybe different of the company currency)."),
+        'amount_residual': fields.function(_amount_residual, string='Residual Amount', multi="residual", help="The residual amount on a receivable or payable of a journal entry expressed in the company currency."),
+        'currency_id': fields.many2one('res.currency', 'Currency', help="The optional other currency if it is a multi-currency entry."),
+        'journal_id': fields.related('move_id', 'journal_id', string='Journal', type='many2one', relation='account.journal', required=True, select=True, readonly=True,
+                                store = {
+                                    'account.move': (_get_move_lines, ['journal_id'], 20)
+                                }),
+        'period_id': fields.related('move_id', 'period_id', string='Period', type='many2one', relation='account.period', required=True, select=True, readonly=True,
+                                store = {
+                                    'account.move': (_get_move_lines, ['period_id'], 20)
+                                }),
+        'blocked': fields.boolean('Litigation', help="You can check this box to mark this journal item as a litigation with the associated partner"),
+        'partner_id': fields.many2one('res.partner', 'Partner', select=1, ondelete='restrict'),
+        'date_maturity': fields.date('Due date', select=True ,help="This field is used for payable and receivable journal entries. You can put the limit date for the payment of this line."),
+        'date': fields.related('move_id','date', string='Effective date', type='date', required=True, select=True,
+                                store = {
+                                    'account.move': (_get_move_lines, ['date'], 20)
+                                }),
+        'date_created': fields.date('Creation date', select=True),
+        'analytic_lines': fields.one2many('account.analytic.line', 'move_id', 'Analytic lines'),
+        'centralisation': fields.selection([('normal','Normal'),('credit','Credit Centralisation'),('debit','Debit Centralisation'),('currency','Currency Adjustment')], 'Centralisation', size=8),
+        'balance': fields.function(_balance, fnct_search=_balance_search, string='Balance'),
+        'state': fields.selection([('draft','Unbalanced'), ('valid','Valid')], 'State', readonly=True,
+                                  help='When new move line is created the state will be \'Draft\'.\n* When all the payments are done it will be in \'Valid\' state.'),
+        'tax_code_id': fields.many2one('account.tax.code', 'Tax Account', help="The Account can either be a base tax code or a tax code account."),
+        'tax_amount': fields.float('Tax/Base Amount', digits_compute=dp.get_precision('Account'), select=True, help="If the Tax account is a tax code account, this field will contain the taxed amount.If the tax account is base tax code, "\
+                    "this field will contain the basic amount(without tax)."),
+        'invoice': fields.function(_invoice, string='Invoice',
+            type='many2one', relation='account.invoice', fnct_search=_invoice_search),
+        'account_tax_id':fields.many2one('account.tax', 'Tax'),
+        'analytic_account_id': fields.many2one('account.analytic.account', 'Analytic Account'),
+        'company_id': fields.related('account_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True)
+    }
+
+    def _get_date(self, cr, uid, context=None):
+        if context is None:
+            context or {}
+        period_obj = self.pool.get('account.period')
+        dt = time.strftime('%Y-%m-%d')
+        if ('journal_id' in context) and ('period_id' in context):
+            cr.execute('SELECT date FROM account_move_line ' \
+                    'WHERE journal_id = %s AND period_id = %s ' \
+                    'ORDER BY id DESC limit 1',
+                    (context['journal_id'], context['period_id']))
+            res = cr.fetchone()
+            if res:
+                dt = res[0]
+            else:
+                period = period_obj.browse(cr, uid, context['period_id'], context=context)
+                dt = period.date_start
+        return dt
+
+    def _get_currency(self, cr, uid, context=None):
+        if context is None:
+            context = {}
+        if not context.get('journal_id', False):
+            return False
+        cur = self.pool.get('account.journal').browse(cr, uid, context['journal_id']).currency
+        return cur and cur.id or False
+
+    _defaults = {
+        'blocked': False,
+        'centralisation': 'normal',
+        'date': _get_date,
+        'date_created': fields.date.context_today,
+        'state': 'draft',
+        'currency_id': _get_currency,
+        'journal_id': lambda self, cr, uid, c: c.get('journal_id', False),
+        'credit': 0.0,
+        'debit': 0.0,
+        'account_id': lambda self, cr, uid, c: c.get('account_id', False),
+        'period_id': lambda self, cr, uid, c: c.get('period_id', False),
+        'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.move.line', context=c)
+    }
+    _order = "date desc, id desc"
+    _sql_constraints = [
+        ('credit_debit1', 'CHECK (credit*debit=0)',  'Wrong credit or debit value in accounting entry !'),
+        ('credit_debit2', 'CHECK (credit+debit>=0)', 'Wrong credit or debit value in accounting entry !'),
+    ]
+
+    def _auto_init(self, cr, context=None):
+        super(account_move_line, self)._auto_init(cr, context=context)
+        cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'account_move_line_journal_id_period_id_index\'')
+        if not cr.fetchone():
+            cr.execute('CREATE INDEX account_move_line_journal_id_period_id_index ON account_move_line (journal_id, period_id)')
+
+    def _check_no_view(self, cr, uid, ids, context=None):
+        lines = self.browse(cr, uid, ids, context=context)
+        for l in lines:
+            if l.account_id.type == 'view':
+                raise osv.except_osv(_('Error :'), _('You can not create journal items on a "view" account %s %s') % (l.account_id.code, l.account_id.name))
+        return True
+
+    def _check_no_closed(self, cr, uid, ids, context=None):
+        lines = self.browse(cr, uid, ids, context=context)
+        for l in lines:
+            if l.account_id.type == 'closed':
+                raise osv.except_osv(_('Error :'), _('You can not create journal items on a closed account %s %s') % (l.account_id.code, l.account_id.name))
+        return True
+
+    def _check_company_id(self, cr, uid, ids, context=None):
+        lines = self.browse(cr, uid, ids, context=context)
+        for l in lines:
+            if l.company_id != l.account_id.company_id or l.company_id != l.period_id.company_id:
+                return False
+        return True
+
+    def _check_date(self, cr, uid, ids, context=None):
+        for l in self.browse(cr, uid, ids, context=context):
+            if l.journal_id.allow_date:
+                if not time.strptime(l.date[:10],'%Y-%m-%d') >= time.strptime(l.period_id.date_start, '%Y-%m-%d') or not time.strptime(l.date[:10], '%Y-%m-%d') <= time.strptime(l.period_id.date_stop, '%Y-%m-%d'):
+                    return False
+        return True
+
+    def _check_currency(self, cr, uid, ids, context=None):
+        for l in self.browse(cr, uid, ids, context=context):
+            if l.account_id.currency_id:
+                if not l.currency_id or not l.currency_id.id == l.account_id.currency_id.id:
+                    return False
+        return True
+
+    _constraints = [
+        (_check_no_view, 'You can not create journal items on an account of type view.', ['account_id']),
+        (_check_no_closed, 'You can not create journal items on closed account.', ['account_id']),
+        (_check_company_id, 'Company must be the same for its related account and period.', ['company_id']),
+        (_check_date, 'The date of your Journal Entry is not in the defined period! You should change the date or remove this constraint from the journal.', ['date']),
+        (_check_currency, 'The selected account of your Journal Entry forces to provide a secondary currency. You should remove the secondary currency on the account or select a multi-currency view on the journal.', ['currency_id']),
+    ]
+
+    #TODO: ONCHANGE_ACCOUNT_ID: set account_tax_id
+    def onchange_currency(self, cr, uid, ids, account_id, amount, currency_id, date=False, journal=False, context=None):
+        if context is None:
+            context = {}
+        account_obj = self.pool.get('account.account')
+        journal_obj = self.pool.get('account.journal')
+        currency_obj = self.pool.get('res.currency')
+        if (not currency_id) or (not account_id):
+            return {}
+        result = {}
+        acc = account_obj.browse(cr, uid, account_id, context=context)
+        if (amount>0) and journal:
+            x = journal_obj.browse(cr, uid, journal).default_credit_account_id
+            if x: acc = x
+        context.update({
+                'date': date,
+                'res.currency.compute.account': acc,
+            })
+        v = currency_obj.compute(cr, uid, currency_id, acc.company_id.currency_id.id, amount, context=context)
+        result['value'] = {
+            'debit': v > 0 and v or 0.0,
+            'credit': v < 0 and -v or 0.0
+        }
+        return result
+
+    def onchange_partner_id(self, cr, uid, ids, move_id, partner_id, account_id=None, debit=0, credit=0, date=False, journal=False):
+        partner_obj = self.pool.get('res.partner')
+        payment_term_obj = self.pool.get('account.payment.term')
+        journal_obj = self.pool.get('account.journal')
+        fiscal_pos_obj = self.pool.get('account.fiscal.position')
+        val = {}
+        val['date_maturity'] = False
+
+        if not partner_id:
+            return {'value':val}
+        if not date:
+            date = datetime.now().strftime('%Y-%m-%d')
+        part = partner_obj.browse(cr, uid, partner_id)
+
+        if part.property_payment_term:
+            res = payment_term_obj.compute(cr, uid, part.property_payment_term.id, 100, date)
+            if res:
+                val['date_maturity'] = res[0][0]
+        if not account_id:
+            id1 = part.property_account_payable.id
+            id2 =  part.property_account_receivable.id
+            if journal:
+                jt = journal_obj.browse(cr, uid, journal).type
+                if jt in ('sale', 'purchase_refund'):
+                    val['account_id'] = fiscal_pos_obj.map_account(cr, uid, part and part.property_account_position or False, id2)
+                elif jt in ('purchase', 'sale_refund'):
+                    val['account_id'] = fiscal_pos_obj.map_account(cr, uid, part and part.property_account_position or False, id1)
+                elif jt in ('general', 'bank', 'cash'):
+                    if part.customer:
+                        val['account_id'] = fiscal_pos_obj.map_account(cr, uid, part and part.property_account_position or False, id2)
+                    elif part.supplier:
+                        val['account_id'] = fiscal_pos_obj.map_account(cr, uid, part and part.property_account_position or False, id1)
+                if val.get('account_id', False):
+                    d = self.onchange_account_id(cr, uid, ids, val['account_id'])
+                    val.update(d['value'])
+        return {'value':val}
+
+    def onchange_account_id(self, cr, uid, ids, account_id=False, partner_id=False):
+        account_obj = self.pool.get('account.account')
+        partner_obj = self.pool.get('res.partner')
+        fiscal_pos_obj = self.pool.get('account.fiscal.position')
+        val = {}
+        if account_id:
+            res = account_obj.browse(cr, uid, account_id)
+            tax_ids = res.tax_ids
+            if tax_ids and partner_id:
+                part = partner_obj.browse(cr, uid, partner_id)
+                tax_id = fiscal_pos_obj.map_tax(cr, uid, part and part.property_account_position or False, tax_ids)[0]
+            else:
+                tax_id = tax_ids and tax_ids[0].id or False
+            val['account_tax_id'] = tax_id
+        return {'value': val}
+    #
+    # type: the type if reconciliation (no logic behind this field, for info)
+    #
+    # writeoff; entry generated for the difference between the lines
+    #
+    def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
+        if context is None:
+            context = {}
+        if context and context.get('periods', False):
+            args.append(('period_id', 'in', context['periods']))
+        elif context and context.get('fiscalyear', False):
+            periods = self.pool.get('account.fiscalyear').browse(cr, uid, context['fiscalyear'], context=context).period_ids
+            args.append(('period_id', 'in', [period.id for period in periods]))
+        if context and context.get('state', False):
+            if context['state'] == 'posted':
+                args.append(('move_id.state','=',context['state']))
+                        
+        partner_domain = self._partner_domain(cr, uid, offset, context=context)
+        if partner_domain is None:
+            return []
+        args.extend(partner_domain)
+        return super(account_move_line, self).search(cr, uid, args, offset, limit, order, context, count)
+
+    def read_group(self, cr, uid, domain, fields, groupby, offset=0,
+                   limit=None, context=None, orderby=False):
+        partner_domain = self._partner_domain(
+            cr, uid, offset, context=context)
+        if partner_domain is None:
+            # Always false domain, to return an empty groupby (no
+            # groups). Should create an empty groupby directly but no
+            # API and can't be arsed.
+            partner_domain = [('move_id', '=', False)]
+        domain.extend(partner_domain)
+        return super(account_move_line, self).read_group(
+            cr, uid, domain, fields, groupby,
+            offset, limit, context, orderby)
+
+    def _partner_domain(self, cr, uid, offset, context=None):
+        """ Returns a (complete) domain for the next partner with
+        unreconciled entries, to add to the existing domain.
+
+        May return an empty list if there is no filtering to be done
+        (``next_partner_only`` is not enabled).
+
+        Will return ``None`` if there is no partner with unreconciled
+        entries left, for the caller to handle as desired.
+        """
+        if context and context.get('next_partner_only'):
+            partner = context.get('partner_id', False) \
+                   or self.get_next_partner_only(cr, uid, offset, context)
+            if not partner:
+                return None
+            return [('partner_id', '=', partner[0])]
+        return []
+
+    def get_next_partner_only(self, cr, uid, offset=0, context=None):
+        cr.execute(
+             """
+             SELECT p.id
+             FROM res_partner p
+             RIGHT JOIN (
+                SELECT l.partner_id AS partner_id, SUM(l.debit) AS debit, SUM(l.credit) AS credit
+                FROM account_move_line l
+                LEFT JOIN account_account a ON (a.id = l.account_id)
+                    LEFT JOIN res_partner p ON (l.partner_id = p.id)
+                    WHERE a.reconcile IS TRUE
+                    AND l.reconcile_id IS NULL
+                    AND (p.last_reconciliation_date IS NULL OR l.date > p.last_reconciliation_date)
+                    AND l.state <> 'draft'
+                    GROUP BY l.partner_id
+                ) AS s ON (p.id = s.partner_id)
+                WHERE debit > 0 AND credit > 0
+                ORDER BY p.last_reconciliation_date LIMIT 1 OFFSET %s""", (offset, )
+            )
+        return cr.fetchone()
+
+    def reconcile_partial(self, cr, uid, ids, type='auto', context=None, writeoff_acc_id=False, writeoff_period_id=False, writeoff_journal_id=False):
+        move_rec_obj = self.pool.get('account.move.reconcile')
+        merges = []
+        unmerge = []
+        total = 0.0
+        merges_rec = []
+        company_list = []
+
+        for line in self.browse(cr, uid, ids, context=context):
+            if company_list and not line.company_id.id in company_list:
+                raise osv.except_osv(_('Warning !'), _('To reconcile the entries company should be the same for all entries'))
+            company_list.append(line.company_id.id)
+
+        for line in self.browse(cr, uid, ids, context=context):
+            if line.account_id.currency_id:
+                currency_id = line.account_id.currency_id
+            else:
+                currency_id = line.company_id.currency_id
+            if line.reconcile_id:
+                raise osv.except_osv(_('Warning'), _('Already Reconciled!'))
+            if line.reconcile_partial_id:
+                for line2 in line.reconcile_partial_id.line_partial_ids:
+                    if not line2.reconcile_id:
+                        if line2.id not in merges:
+                            merges.append(line2.id)
+                        if line2.account_id.currency_id:
+                            total += line2.amount_currency
+                        else:
+                            total += (line2.debit or 0.0) - (line2.credit or 0.0)
+                merges_rec.append(line.reconcile_partial_id.id)
+            else:
+                unmerge.append(line.id)
+                if line.account_id.currency_id:
+                    total += line.amount_currency
+                else:
+                    total += (line.debit or 0.0) - (line.credit or 0.0)
+        if self.pool.get('res.currency').is_zero(cr, uid, currency_id, total):
+            res = self.reconcile(cr, uid, merges+unmerge, context=context, writeoff_acc_id=writeoff_acc_id, writeoff_period_id=writeoff_period_id, writeoff_journal_id=writeoff_journal_id)
+            return res
+        r_id = move_rec_obj.create(cr, uid, {
+            'type': type,
+            'line_partial_ids': map(lambda x: (4,x,False), merges+unmerge)
+        })
+        move_rec_obj.reconcile_partial_check(cr, uid, [r_id] + merges_rec, context=context)
+        return True
+
+    def reconcile(self, cr, uid, ids, type='auto', writeoff_acc_id=False, writeoff_period_id=False, writeoff_journal_id=False, context=None):
+        account_obj = self.pool.get('account.account')
+        move_obj = self.pool.get('account.move')
+        move_rec_obj = self.pool.get('account.move.reconcile')
+        partner_obj = self.pool.get('res.partner')
+        currency_obj = self.pool.get('res.currency')
+        lines = self.browse(cr, uid, ids, context=context)
+        unrec_lines = filter(lambda x: not x['reconcile_id'], lines)
+        credit = debit = 0.0
+        currency = 0.0
+        account_id = False
+        partner_id = False
+        if context is None:
+            context = {}
+        company_list = []
+        for line in self.browse(cr, uid, ids, context=context):
+            if company_list and not line.company_id.id in company_list:
+                raise osv.except_osv(_('Warning !'), _('To reconcile the entries company should be the same for all entries'))
+            company_list.append(line.company_id.id)
+        for line in unrec_lines:
+            if line.state <> 'valid':
+                raise osv.except_osv(_('Error'),
+                        _('Entry "%s" is not valid !') % line.name)
+            credit += line['credit']
+            debit += line['debit']
+            currency += line['amount_currency'] or 0.0
+            account_id = line['account_id']['id']
+            partner_id = (line['partner_id'] and line['partner_id']['id']) or False
+        writeoff = debit - credit
+
+        # Ifdate_p in context => take this date
+        date = context.get('date_p') or time.strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
+
+        cr.execute('SELECT account_id, reconcile_id '\
+                   'FROM account_move_line '\
+                   'WHERE id IN %s '\
+                   'GROUP BY account_id,reconcile_id',
+                   (tuple(ids), ))
+        r = cr.fetchall()
+        #TODO: move this check to a constraint in the account_move_reconcile object
+        if not unrec_lines:
+            raise osv.except_osv(_('Error'), _('Entry is already reconciled'))
+        account = account_obj.browse(cr, uid, account_id, context=context)
+        if r[0][1] != None:
+            raise osv.except_osv(_('Error'), _('Some entries are already reconciled !'))
+
+        if context.get('fy_closing'):
+            # We don't want to generate any write-off when being called from the
+            # wizard used to close a fiscal year (and it doesn't give us any
+            # writeoff_acc_id).
+            pass
+        elif (not currency_obj.is_zero(cr, uid, account.company_id.currency_id, writeoff)) or \
+           (account.currency_id and (not currency_obj.is_zero(cr, uid, account.currency_id, currency))):
+            if not writeoff_acc_id:
+                raise osv.except_osv(_('Warning'), _('You have to provide an account for the write off/exchange difference entry !'))
+            if writeoff > 0:
+                debit = writeoff
+                credit = 0.0
+                self_credit = writeoff
+                self_debit = 0.0
+            else:
+                debit = 0.0
+                credit = -writeoff
+                self_credit = 0.0
+                self_debit = -writeoff
+            # If comment exist in context, take it
+            if 'comment' in context and context['comment']:
+                libelle = context['comment']
+            else:
+                libelle = _('Write-Off')
+
+            cur_obj = self.pool.get('res.currency')
+            cur_id = False
+            amount_currency_writeoff = 0.0
+            if context.get('company_currency_id',False) != context.get('currency_id',False):
+                cur_id = context.get('currency_id',False)
+                for line in unrec_lines:
+                    if line.currency_id and line.currency_id.id == context.get('currency_id',False):
+                        amount_currency_writeoff += line.amount_currency
+                    else:
+                        tmp_amount = cur_obj.compute(cr, uid, line.account_id.company_id.currency_id.id, context.get('currency_id',False), abs(line.debit-line.credit), context={'date': line.date})
+                        amount_currency_writeoff += (line.debit > 0) and tmp_amount or -tmp_amount
+
+            writeoff_lines = [
+                (0, 0, {
+                    'name': libelle,
+                    'debit': self_debit,
+                    'credit': self_credit,
+                    'account_id': account_id,
+                    'date': date,
+                    'partner_id': partner_id,
+                    'currency_id': cur_id or (account.currency_id.id or False),
+                    'amount_currency': amount_currency_writeoff and -1 * amount_currency_writeoff or (account.currency_id.id and -1 * currency or 0.0)
+                }),
+                (0, 0, {
+                    'name': libelle,
+                    'debit': debit,
+                    'credit': credit,
+                    'account_id': writeoff_acc_id,
+                    'analytic_account_id': context.get('analytic_id', False),
+                    'date': date,
+                    'partner_id': partner_id,
+                    'currency_id': cur_id or (account.currency_id.id or False),
+                    'amount_currency': amount_currency_writeoff and amount_currency_writeoff or (account.currency_id.id and currency or 0.0)
+                })
+            ]
+
+            writeoff_move_id = move_obj.create(cr, uid, {
+                'period_id': writeoff_period_id,
+                'journal_id': writeoff_journal_id,
+                'date':date,
+                'state': 'draft',
+                'line_id': writeoff_lines
+            })
+
+            writeoff_line_ids = self.search(cr, uid, [('move_id', '=', writeoff_move_id), ('account_id', '=', account_id)])
+            if account_id == writeoff_acc_id:
+                writeoff_line_ids = [writeoff_line_ids[1]]
+            ids += writeoff_line_ids
+
+        r_id = move_rec_obj.create(cr, uid, {
+            'type': type,
+            'line_id': map(lambda x: (4, x, False), ids),
+            'line_partial_ids': map(lambda x: (3, x, False), ids)
+        })
+        wf_service = netsvc.LocalService("workflow")
+        # the id of the move.reconcile is written in the move.line (self) by the create method above
+        # because of the way the line_id are defined: (4, x, False)
+        for id in ids:
+            wf_service.trg_trigger(uid, 'account.move.line', id, cr)
+
+        if lines and lines[0]:
+            partner_id = lines[0].partner_id and lines[0].partner_id.id or False
+            if partner_id and context and context.get('stop_reconcile', False):
+                partner_obj.write(cr, uid, [partner_id], {'last_reconciliation_date': time.strftime('%Y-%m-%d %H:%M:%S')})
+        return r_id
+
+    def view_header_get(self, cr, user, view_id, view_type, context=None):
+        if context is None:
+            context = {}
+        context = self.convert_to_period(cr, user, context=context)
+        if context.get('account_id', False):
+            cr.execute('SELECT code FROM account_account WHERE id = %s', (context['account_id'], ))
+            res = cr.fetchone()
+            if res:
+                res = _('Entries: ')+ (res[0] or '')
+            return res
+        if (not context.get('journal_id', False)) or (not context.get('period_id', False)):
+            return False
+        cr.execute('SELECT code FROM account_journal WHERE id = %s', (context['journal_id'], ))
+        j = cr.fetchone()[0] or ''
+        cr.execute('SELECT code FROM account_period WHERE id = %s', (context['period_id'], ))
+        p = cr.fetchone()[0] or ''
+        if j or p:
+            return j + (p and (':' + p) or '')
+        return False
+
+    def onchange_date(self, cr, user, ids, date, context=None):
+        """
+        Returns a dict that contains new values and context
+        @param cr: A database cursor
+        @param user: ID of the user currently logged in
+        @param date: latest value from user input for field date
+        @param args: other arguments
+        @param context: context arguments, like lang, time zone
+        @return: Returns a dict which contains new values, and context
+        """
+        res = {}
+        if context is None:
+            context = {}
+        period_pool = self.pool.get('account.period')
+        ctx = dict(context, account_period_prefer_normal=True)
+        pids = period_pool.find(cr, user, date, context=ctx)
+        if pids:
+            res.update({
+                'period_id':pids[0]
+            })
+            context.update({
+                'period_id':pids[0]
+            })
+        return {
+            'value':res,
+            'context':context,
+        }
+
+    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
+        journal_pool = self.pool.get('account.journal')
+        if context is None:
+            context = {}
+        result = super(account_move_line, self).fields_view_get(cr, uid, view_id, view_type, context=context, toolbar=toolbar, submenu=submenu)
+        if view_type != 'tree':
+            #Remove the toolbar from the form view
+            if view_type == 'form':
+                if result.get('toolbar', False):
+                    result['toolbar']['action'] = []
+            #Restrict the list of journal view in search view
+            if view_type == 'search' and result['fields'].get('journal_id', False):
+                result['fields']['journal_id']['selection'] = journal_pool.name_search(cr, uid, '', [], context=context)
+                ctx = context.copy()
+                #we add the refunds journal in the selection field of journal
+                if context.get('journal_type', False) == 'sale':
+                    ctx.update({'journal_type': 'sale_refund'})
+                    result['fields']['journal_id']['selection'] += journal_pool.name_search(cr, uid, '', [], context=ctx)
+                elif context.get('journal_type', False) == 'purchase':
+                    ctx.update({'journal_type': 'purchase_refund'})
+                    result['fields']['journal_id']['selection'] += journal_pool.name_search(cr, uid, '', [], context=ctx)
+            return result
+        if context.get('view_mode', False):
+            return result
+        fld = []
+        fields = {}
+        flds = []
+        title = _("Accounting Entries") #self.view_header_get(cr, uid, view_id, view_type, context)
+
+        ids = journal_pool.search(cr, uid, [])
+        journals = journal_pool.browse(cr, uid, ids, context=context)
+        all_journal = [None]
+        common_fields = {}
+        total = len(journals)
+        for journal in journals:
+            all_journal.append(journal.id)
+            for field in journal.view_id.columns_id:
+                if not field.field in fields:
+                    fields[field.field] = [journal.id]
+                    fld.append((field.field, field.sequence))
+                    flds.append(field.field)
+                    common_fields[field.field] = 1
+                else:
+                    fields.get(field.field).append(journal.id)
+                    common_fields[field.field] = common_fields[field.field] + 1
+        fld.append(('period_id', 3))
+        fld.append(('journal_id', 10))
+        flds.append('period_id')
+        flds.append('journal_id')
+        fields['period_id'] = all_journal
+        fields['journal_id'] = all_journal
+        fld = sorted(fld, key=itemgetter(1))
+        widths = {
+            'statement_id': 50,
+            'state': 60,
+            'tax_code_id': 50,
+            'move_id': 40,
+        }
+
+        document = etree.Element('tree', string=title, editable="top",
+                                 refresh="5", on_write="on_create_write",
+                                 colors="red:state=='draft';black:state=='valid'")
+        fields_get = self.fields_get(cr, uid, flds, context)
+        for field, _seq in fld:
+            if common_fields.get(field) == total:
+                fields.get(field).append(None)
+            # if field=='state':
+            #     state = 'colors="red:state==\'draft\'"'
+            f = etree.SubElement(document, 'field', name=field)
+
+            if field == 'debit':
+                f.set('sum', _("Total debit"))
+
+            elif field == 'credit':
+                f.set('sum', _("Total credit"))
+
+            elif field == 'move_id':
+                f.set('required', 'False')
+
+            elif field == 'account_tax_id':
+                f.set('domain', "[('parent_id', '=' ,False)]")
+                f.set('context', "{'journal_id': journal_id}")
+
+            elif field == 'account_id' and journal.id:
+                f.set('domain', "[('journal_id', '=', journal_id),('type','!=','view'), ('type','!=','closed')]")
+                f.set('on_change', 'onchange_account_id(account_id, partner_id)')
+
+            elif field == 'partner_id':
+                f.set('on_change', 'onchange_partner_id(move_id, partner_id, account_id, debit, credit, date, journal_id)')
+
+            elif field == 'journal_id':
+                f.set('context', "{'journal_id': journal_id}")
+
+            elif field == 'statement_id':
+                f.set('domain', "[('state', '!=', 'confirm'),('journal_id.type', '=', 'bank')]")
+                f.set('invisible', 'True')
+
+            elif field == 'date':
+                f.set('on_change', 'onchange_date(date)')
+
+            elif field == 'analytic_account_id':
+                # Currently it is not working due to being executed by superclass's fields_view_get
+                # f.set('groups', 'analytic.group_analytic_accounting')
+                pass
+
+            if field in ('amount_currency', 'currency_id'):
+                f.set('on_change', 'onchange_currency(account_id, amount_currency, currency_id, date, journal_id)')
+                f.set('attrs', "{'readonly': [('state', '=', 'valid')]}")
+
+            if field in widths:
+                f.set('width', str(widths[field]))
+
+            if field in ('journal_id',):
+                f.set("invisible", "context.get('journal_id', False)")
+            elif field in ('period_id',):
+                f.set("invisible", "context.get('period_id', False)")
+
+            orm.setup_modifiers(f, fields_get[field], context=context,
+                                in_tree_view=True)
+
+        result['arch'] = etree.tostring(document, pretty_print=True)
+        result['fields'] = fields_get
+        return result
+
+    def _check_moves(self, cr, uid, context=None):
+        # use the first move ever created for this journal and period
+        if context is None:
+            context = {}
+        cr.execute('SELECT id, state, name FROM account_move WHERE journal_id = %s AND period_id = %s ORDER BY id limit 1', (context['journal_id'],context['period_id']))
+        res = cr.fetchone()
+        if res:
+            if res[1] != 'draft':
+                raise osv.except_osv(_('UserError'),
+                       _('The account move (%s) for centralisation ' \
+                                'has been confirmed!') % res[2])
+        return res
+
+    def _remove_move_reconcile(self, cr, uid, move_ids=[], context=None):
+        # Function remove move rencocile ids related with moves
+        obj_move_line = self.pool.get('account.move.line')
+        obj_move_rec = self.pool.get('account.move.reconcile')
+        unlink_ids = []
+        if not move_ids:
+            return True
+        recs = obj_move_line.read(cr, uid, move_ids, ['reconcile_id', 'reconcile_partial_id'])
+        full_recs = filter(lambda x: x['reconcile_id'], recs)
+        rec_ids = [rec['reconcile_id'][0] for rec in full_recs]
+        part_recs = filter(lambda x: x['reconcile_partial_id'], recs)
+        part_rec_ids = [rec['reconcile_partial_id'][0] for rec in part_recs]
+        unlink_ids += rec_ids
+        unlink_ids += part_rec_ids
+        if unlink_ids:
+            obj_move_rec.unlink(cr, uid, unlink_ids)
+        return True
+
+    def unlink(self, cr, uid, ids, context=None, check=True):
+        if context is None:
+            context = {}
+        move_obj = self.pool.get('account.move')
+        self._update_check(cr, uid, ids, context)
+        result = False
+        move_ids = set()
+        for line in self.browse(cr, uid, ids, context=context):
+            move_ids.add(line.move_id.id)
+            context['journal_id'] = line.journal_id.id
+            context['period_id'] = line.period_id.id
+            result = super(account_move_line, self).unlink(cr, uid, [line.id], context=context)
+        move_ids = list(move_ids)
+        if check and move_ids:
+            move_obj.validate(cr, uid, move_ids, context=context)
+        return result
+
+    def write(self, cr, uid, ids, vals, context=None, check=True, update_check=True):
+        if context is None:
+            context={}
+        move_obj = self.pool.get('account.move')
+        account_obj = self.pool.get('account.account')
+        journal_obj = self.pool.get('account.journal')
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        if vals.get('account_tax_id', False):
+            raise osv.except_osv(_('Unable to change tax !'), _('You can not change the tax, you should remove and recreate lines !'))
+        if ('account_id' in vals) and not account_obj.read(cr, uid, vals['account_id'], ['active'])['active']:
+            raise osv.except_osv(_('Bad account!'), _('You can not use an inactive account!'))
+        if update_check:
+            if ('account_id' in vals) or ('journal_id' in vals) or ('period_id' in vals) or ('move_id' in vals) or ('debit' in vals) or ('credit' in vals) or ('date' in vals):
+                self._update_check(cr, uid, ids, context)
+
+        todo_date = None
+        if vals.get('date', False):
+            todo_date = vals['date']
+            del vals['date']
+
+        for line in self.browse(cr, uid, ids, context=context):
+            ctx = context.copy()
+            if ('journal_id' not in ctx):
+                if line.move_id:
+                   ctx['journal_id'] = line.move_id.journal_id.id
+                else:
+                    ctx['journal_id'] = line.journal_id.id
+            if ('period_id' not in ctx):
+                if line.move_id:
+                    ctx['period_id'] = line.move_id.period_id.id
+                else:
+                    ctx['period_id'] = line.period_id.id
+            #Check for centralisation
+            journal = journal_obj.browse(cr, uid, ctx['journal_id'], context=ctx)
+            if journal.centralisation:
+                self._check_moves(cr, uid, context=ctx)
+        result = super(account_move_line, self).write(cr, uid, ids, vals, context)
+        if check:
+            done = []
+            for line in self.browse(cr, uid, ids):
+                if line.move_id.id not in done:
+                    done.append(line.move_id.id)
+                    move_obj.validate(cr, uid, [line.move_id.id], context)
+                    if todo_date:
+                        move_obj.write(cr, uid, [line.move_id.id], {'date': todo_date}, context=context)
+        return result
+
+    def _update_journal_check(self, cr, uid, journal_id, period_id, context=None):
+        journal_obj = self.pool.get('account.journal')
+        period_obj = self.pool.get('account.period')
+        jour_period_obj = self.pool.get('account.journal.period')
+        cr.execute('SELECT state FROM account_journal_period WHERE journal_id = %s AND period_id = %s', (journal_id, period_id))
+        result = cr.fetchall()
+        for (state,) in result:
+            if state == 'done':
+                raise osv.except_osv(_('Error !'), _('You can not add/modify entries in a closed journal.'))
+        if not result:
+            journal = journal_obj.browse(cr, uid, journal_id, context=context)
+            period = period_obj.browse(cr, uid, period_id, context=context)
+            jour_period_obj.create(cr, uid, {
+                'name': (journal.code or journal.name)+':'+(period.name or ''),
+                'journal_id': journal.id,
+                'period_id': period.id
+            })
+        return True
+
+    def _update_check(self, cr, uid, ids, context=None):
+        done = {}
+        for line in self.browse(cr, uid, ids, context=context):
+            err_msg = _('Move name (id): %s (%s)') % (line.move_id.name, str(line.move_id.id))
+            if line.move_id.state <> 'draft' and (not line.journal_id.entry_posted):
+                raise osv.except_osv(_('Error !'), _('You can not do this modification on a confirmed entry! You can just change some non legal fields or you must unconfirm the journal entry first! \n%s') % err_msg)
+            if line.reconcile_id:
+                raise osv.except_osv(_('Error !'), _('You can not do this modification on a reconciled entry! You can just change some non legal fields or you must unreconcile first!\n%s') % err_msg)
+            t = (line.journal_id.id, line.period_id.id)
+            if t not in done:
+                self._update_journal_check(cr, uid, line.journal_id.id, line.period_id.id, context)
+                done[t] = True
+        return True
+
+    def create(self, cr, uid, vals, context=None, check=True):
+        account_obj = self.pool.get('account.account')
+        tax_obj = self.pool.get('account.tax')
+        move_obj = self.pool.get('account.move')
+        cur_obj = self.pool.get('res.currency')
+        journal_obj = self.pool.get('account.journal')
+        if context is None:
+            context = {}
+        if vals.get('move_id', False):
+            company_id = self.pool.get('account.move').read(cr, uid, vals['move_id'], ['company_id']).get('company_id', False)
+            if company_id:
+                vals['company_id'] = company_id[0]
+        if ('account_id' in vals) and not account_obj.read(cr, uid, vals['account_id'], ['active'])['active']:
+            raise osv.except_osv(_('Bad account!'), _('You can not use an inactive account!'))
+        if 'journal_id' in vals:
+            context['journal_id'] = vals['journal_id']
+        if 'period_id' in vals:
+            context['period_id'] = vals['period_id']
+        if ('journal_id' not in context) and ('move_id' in vals) and vals['move_id']:
+            m = move_obj.browse(cr, uid, vals['move_id'])
+            context['journal_id'] = m.journal_id.id
+            context['period_id'] = m.period_id.id
+        #we need to treat the case where a value is given in the context for period_id as a string
+        if 'period_id' not in context or not isinstance(context.get('period_id', ''), (int, long)):
+            period_candidate_ids = self.pool.get('account.period').name_search(cr, uid, name=context.get('period_id',''))
+            if len(period_candidate_ids) != 1:
+                raise osv.except_osv(_('Encoding error'), _('No period found or more than one period found for the given date.'))
+            context['period_id'] = period_candidate_ids[0][0]
+        if not context.get('journal_id', False) and context.get('search_default_journal_id', False):
+            context['journal_id'] = context.get('search_default_journal_id')            
+        self._update_journal_check(cr, uid, context['journal_id'], context['period_id'], context)
+        move_id = vals.get('move_id', False)
+        journal = journal_obj.browse(cr, uid, context['journal_id'], context=context)
+        if not move_id:
+            if journal.centralisation:
+                #Check for centralisation
+                res = self._check_moves(cr, uid, context)
+                if res:
+                    vals['move_id'] = res[0]
+            if not vals.get('move_id', False):
+                if journal.sequence_id:
+                    #name = self.pool.get('ir.sequence').next_by_id(cr, uid, journal.sequence_id.id)
+                    v = {
+                        'date': vals.get('date', time.strftime('%Y-%m-%d')),
+                        'period_id': context['period_id'],
+                        'journal_id': context['journal_id']
+                    }
+                    if vals.get('ref', ''):
+                        v.update({'ref': vals['ref']})
+                    move_id = move_obj.create(cr, uid, v, context)
+                    vals['move_id'] = move_id
+                else:
+                    raise osv.except_osv(_('No piece number !'), _('Can not create an automatic sequence for this piece!\nPut a sequence in the journal definition for automatic numbering or create a sequence manually for this piece.'))
+        ok = not (journal.type_control_ids or journal.account_control_ids)
+        if ('account_id' in vals):
+            account = account_obj.browse(cr, uid, vals['account_id'], context=context)
+            if journal.type_control_ids:
+                type = account.user_type
+                for t in journal.type_control_ids:
+                    if type.code == t.code:
+                        ok = True
+                        break
+            if journal.account_control_ids and not ok:
+                for a in journal.account_control_ids:
+                    if a.id == vals['account_id']:
+                        ok = True
+                        break
+            # Automatically convert in the account's secondary currency if there is one and
+            # the provided values were not already multi-currency
+            if account.currency_id and (vals.get('amount_currency', False) is False) and account.currency_id.id != account.company_id.currency_id.id:
+                vals['currency_id'] = account.currency_id.id
+                ctx = {}
+                if 'date' in vals:
+                    ctx['date'] = vals['date']
+                vals['amount_currency'] = cur_obj.compute(cr, uid, account.company_id.currency_id.id,
+                    account.currency_id.id, vals.get('debit', 0.0)-vals.get('credit', 0.0), context=ctx)
+        if not ok:
+            raise osv.except_osv(_('Bad account !'), _('You can not use this general account in this journal, check the tab \'Entry Controls\' on the related journal !'))
+
+        if vals.get('analytic_account_id',False):
+            if journal.analytic_journal_id:
+                vals['analytic_lines'] = [(0,0, {
+                        'name': vals['name'],
+                        'date': vals.get('date', time.strftime('%Y-%m-%d')),
+                        'account_id': vals.get('analytic_account_id', False),
+                        'unit_amount': vals.get('quantity', 1.0),
+                        'amount': vals.get('debit', 0.0) or vals.get('credit', 0.0),
+                        'general_account_id': vals.get('account_id', False),
+                        'journal_id': journal.analytic_journal_id.id,
+                        'ref': vals.get('ref', False),
+                        'user_id': uid
+            })]
+
+        result = super(account_move_line, self).create(cr, uid, vals, context=context)
+        # CREATE Taxes
+        if vals.get('account_tax_id', False):
+            tax_id = tax_obj.browse(cr, uid, vals['account_tax_id'])
+            total = vals['debit'] - vals['credit']
+            if journal.type in ('purchase_refund', 'sale_refund'):
+                base_code = 'ref_base_code_id'
+                tax_code = 'ref_tax_code_id'
+                account_id = 'account_paid_id'
+                base_sign = 'ref_base_sign'
+                tax_sign = 'ref_tax_sign'
+            else:
+                base_code = 'base_code_id'
+                tax_code = 'tax_code_id'
+                account_id = 'account_collected_id'
+                base_sign = 'base_sign'
+                tax_sign = 'tax_sign'
+            tmp_cnt = 0
+            for tax in tax_obj.compute_all(cr, uid, [tax_id], total, 1.00, force_excluded=True).get('taxes'):
+                #create the base movement
+                if tmp_cnt == 0:
+                    if tax[base_code]:
+                        tmp_cnt += 1
+                        self.write(cr, uid,[result], {
+                            'tax_code_id': tax[base_code],
+                            'tax_amount': tax[base_sign] * abs(total)
+                        })
+                else:
+                    data = {
+                        'move_id': vals['move_id'],
+                        'name': tools.ustr(vals['name'] or '') + ' ' + tools.ustr(tax['name'] or ''),
+                        'date': vals['date'],
+                        'partner_id': vals.get('partner_id',False),
+                        'ref': vals.get('ref',False),
+                        'account_tax_id': False,
+                        'tax_code_id': tax[base_code],
+                        'tax_amount': tax[base_sign] * abs(total),
+                        'account_id': vals['account_id'],
+                        'credit': 0.0,
+                        'debit': 0.0,
+                    }
+                    if data['tax_code_id']:
+                        self.create(cr, uid, data, context)
+                #create the VAT movement
+                data = {
+                    'move_id': vals['move_id'],
+                    'name': tools.ustr(vals['name'] or '') + ' ' + tools.ustr(tax['name'] or ''),
+                    'date': vals['date'],
+                    'partner_id': vals.get('partner_id',False),
+                    'ref': vals.get('ref',False),
+                    'account_tax_id': False,
+                    'tax_code_id': tax[tax_code],
+                    'tax_amount': tax[tax_sign] * abs(tax['amount']),
+                    'account_id': tax[account_id] or vals['account_id'],
+                    'credit': tax['amount']<0 and -tax['amount'] or 0.0,
+                    'debit': tax['amount']>0 and tax['amount'] or 0.0,
+                }
+                if data['tax_code_id']:
+                    self.create(cr, uid, data, context)
+            del vals['account_tax_id']
+
+        if check and ((not context.get('no_store_function')) or journal.entry_posted):
+            tmp = move_obj.validate(cr, uid, [vals['move_id']], context)
+            if journal.entry_posted and tmp:
+                move_obj.button_validate(cr,uid, [vals['move_id']], context)
+        return result
+
+account_move_line()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'account/account_pre_install.yml'
--- account/account_pre_install.yml	1970-01-01 00:00:00 +0000
+++ account/account_pre_install.yml	2013-06-05 16:26:39 +0000
@@ -0,0 +1,22 @@
+-
+  I configure automatically if the country is set on the company, mainly for online offers.
+-
+  !python {model: account.installer}: |
+    modules = self.pool.get('ir.module.module')
+    wizards = self.pool.get('ir.actions.todo')
+    wiz = wizards.browse(cr, uid, ref('account.account_configuration_installer_todo'))
+    part = self.pool.get('res.partner').browse(cr, uid, ref('base.main_partner'))
+    # if we know the country and the wizard has not yet been executed, we do it
+    if (part.country.id) and (wiz.state=='open'):
+        mod = 'l10n_'+part.country.code.lower()
+        ids = modules.search(cr, uid, [ ('name','=',mod) ], context=context)
+        if ids:
+            wizards.write(cr, uid, [ref('account.account_configuration_installer_todo')], {
+                'state': 'done'
+            })
+            wiz_id = self.create(cr, uid, {
+                'charts': mod
+            })
+            self.execute_simple(cr, uid, [wiz_id])
+            modules.state_update(cr, uid, ids,
+                'to install', ['uninstalled'], context=context)

=== added file 'account/account_report.xml'
--- account/account_report.xml	1970-01-01 00:00:00 +0000
+++ account/account_report.xml	2013-06-05 16:26:39 +0000
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+    <data>
+        <report auto="False" id="account_general_ledger" menu="False" model="account.account" name="account.general.ledger" rml="account/report/account_general_ledger.rml" string="General Ledger"/>
+        <report auto="False" id="account_financial_report" menu="False" model="account.financial.report" name="account.financial.report" rml="account/report/account_financial_report.rml" string="Financial Report"/>
+        <report auto="False" id="account_aged_partner_balance" menu="False" model="res.partner" name="account.aged_trial_balance" rml="account/report/account_aged_partner_balance.rml" string="Aged Partner Balance"/>
+        <report auto="False" id="account_general_ledger_landscape" menu="False" model="account.account" name="account.general.ledger_landscape" rml="account/report/account_general_ledger_landscape.rml" string="General Ledger"/>
+        <report auto="False" id="account_3rdparty_ledger" menu="False" model="res.partner" name="account.third_party_ledger" rml="account/report/account_partner_ledger.rml" string="Partner Ledger"/>
+        <report auto="False" id="account_3rdparty_ledger_other" menu="False" model="res.partner" name="account.third_party_ledger_other" rml="account/report/account_partner_ledger_other.rml" string="Partner Ledger"/>
+        <report auto="False" id="account_account_balance" menu="False" model="account.account" name="account.account.balance" rml="account/report/account_balance.rml" string="Trial Balance"/>
+        <report auto="False" id="account_3rdparty_account_balance" menu="False" model="account.account" name="account.partner.balance" rml="account/report/account_partner_balance.rml" string="Partner Balance"/>
+        <report auto="False" id="account_central_journal" model="account.journal.period" name="account.central.journal" rml="account/report/account_central_journal.rml" string="Central Journal" header="False"/>
+        <report auto="False" id="account_general_journal" model="account.journal.period" name="account.general.journal" rml="account/report/account_general_journal.rml" string="General Journal" header="False"/>
+        <report auto="False" id="account_journal" model="account.journal.period" name="account.journal.period.print" rml="account/report/account_journal.rml" string="Journal" header="False"/>
+        <report auto="False" id="account_journal_sale_purchase" model="account.journal.period" name="account.journal.period.print.sale.purchase" rml="account/report/account_journal_sale_purchase.rml" string="Sale/Purchase Journal" header="False"/>
+        <report auto="False" id="account_overdue" model="res.partner" name="account.overdue" rml="account/report/account_print_overdue.rml" string="Overdue Payments"/>
+        <report
+            auto="False"
+            id="account_invoices"
+            model="account.invoice"
+            name="account.invoice"  
+            rml="account/report/account_print_invoice.rml"
+            string="Invoices"
+            attachment="(object.state in ('open','paid')) and ('INV'+(object.number or '').replace('/','')+'.pdf')"
+            attachment_use="True"
+            usage="default"
+            multi="True"/>
+        <report id="account_transfers" model="account.transfer" name="account.transfer" string="Transfers" xml="account/report/transfer.xml" xsl="account/report/transfer.xsl"/>
+        <report auto="False" id="account_intracom" menu="False" model="account.move.line" name="account.intracom" string="IntraCom"/>
+
+        <report
+            auto="False"
+            id="account_vat_declaration"
+            menu="False"
+            model="account.tax.code"
+            name="account.vat.declaration"
+            rml="account/report/account_tax_report.rml"
+            string="Taxes Report"/>
+
+        <menuitem
+            id="menu_tax_report"
+            name="Taxes"
+            groups="group_account_user,group_account_manager"
+            parent="account.menu_finance_generic_reporting" sequence="3"/>
+
+    </data>
+</openerp>

=== added file 'account/account_unit_test.xml'
--- account/account_unit_test.xml	1970-01-01 00:00:00 +0000
+++ account/account_unit_test.xml	2013-06-05 16:26:39 +0000
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+    <data>
+        <!-- This file must be loaded  _after_  account_demo.xml ! -->
+        <record id="test_invoice_1" model="account.invoice">
+            <field name="currency_id" ref="base.EUR"/>
+            <field name="company_id" ref="base.main_company"/>
+            <field name="address_invoice_id" ref="base.res_partner_address_tang"/>
+            <field name="partner_id" ref="base.res_partner_asus"/>
+            <field name="journal_id" ref="account.sales_journal"/>
+            <field name="state">draft</field>
+            <field name="type">out_invoice</field>
+            <field name="account_id" ref="account.a_recv"/>
+            <field name="name">Test invoice 1</field>
+            <field name="address_contact_id" ref="base.res_partner_address_tang"/>
+        </record>
+        <record id="test_tax_line" model="account.invoice.tax">
+            <field name="name">Test Tax</field>
+            <field name="base">5.00</field>
+            <field name="amount">100.00</field>
+            <field name="account_id" ref="account.ova"/>
+            <field name="invoice_id" ref="test_invoice_1"/>
+        </record>
+        <record id="test_invoice_1_line_1" model="account.invoice.line">
+            <field name="name">Basic computer with Dvorak keyboard and left-handed mouse</field>
+            <field name="invoice_id" ref="test_invoice_1"/>
+            <field name="price_unit">250</field>
+            <field name="quantity">1</field>
+            <field name="account_id" ref="account.a_sale"/>
+        </record>
+        <record id="test_invoice_1_line_2" model="account.invoice.line">
+            <field name="name">Little server with raid 1 and 512ECC ram</field>
+            <field name="invoice_id" ref="test_invoice_1"/>
+            <field name="price_unit">800</field>
+            <field name="quantity">2</field>
+            <field name="account_id" ref="account.a_sale"/>
+        </record>
+
+        <assert id="test_invoice_1" model="account.invoice" string="The currency unit of Test invoice 1 is EUR">
+            <test expr="currency_id.symbol">€</test>
+        </assert>
+
+        <assert id="test_invoice_1" model="account.invoice" severity="error" string="The amount of Test invoice 1 is correct">
+            <test expr="sum([l.price_subtotal for l in invoice_line]) == 1850"/>
+            <test expr="sum([l.price_subtotal for l in invoice_line]) == amount_untaxed"/>
+        </assert>
+
+        <workflow action="invoice_open" model="account.invoice" ref="test_invoice_1"/>
+
+        <assert id="test_invoice_1" model="account.invoice" string="Test invoice 1 is now open">
+            <test expr="state">open</test>
+        </assert>
+        <function model="account.invoice" name="pay_and_reconcile">
+            <!-- ids = -->                    <value eval="[ref('test_invoice_1')]"/>
+            <!-- pay_amount = -->            <value eval="1850"/>
+            <!-- pay_account_id = -->        <value eval="ref('cash')"/>
+            <!-- period_id = -->            <value eval="ref('account.period_' + str(int(time.strftime('%m'))))"/>
+            <!-- pay_journal_id = -->        <value eval="ref('bank_journal')"/>
+            <!-- writeoff_acc_id = -->        <value eval="ref('cash')"/>
+            <!-- writeoff_period_id = -->    <value eval="ref('account.period_' + str(int(time.strftime('%m'))))"/>
+            <!-- writeoff_journal_id = -->    <value eval="ref('bank_journal')"/>
+            <!-- context = -->              <value eval="{}"/>
+            <!-- name = -->                 <value eval="str('Payment from ASUStek')"/>
+        </function>
+
+        <assert id="test_invoice_1" model="account.invoice" string="Test invoice 1 is now paid">
+            <test expr="state">paid</test>
+        </assert>
+    </data>
+</openerp>

=== added file 'account/account_view.xml'
--- account/account_view.xml	1970-01-01 00:00:00 +0000
+++ account/account_view.xml	2013-06-05 16:26:39 +0000
@@ -0,0 +1,2889 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+    <data>
+
+        <!--
+            Fiscal Year
+        -->
+
+        <record id="view_account_fiscalyear_form" model="ir.ui.view">
+            <field name="name">account.fiscalyear.form</field>
+            <field name="model">account.fiscalyear</field>
+            <field name="type">form</field>
+            <field name="arch" type="xml">
+                <form string="Fiscalyear">
+                    <group>
+                    <field name="name"/>
+                    <field name="code"/>
+                    <field name="date_start"/>
+                    <field name="date_stop"/>
+                    <field name="company_id" groups="base.group_multi_company"/>
+                    <field name="end_journal_period_id" groups="base.group_extended"/>
+                    </group>
+                    <separator colspan="4" string="Periods"/>
+                    <field colspan="4" name="period_ids" nolabel="1" widget="one2many_list">
+                        <form string="Period">
+                            <field name="name"/>
+                            <field name="code"/>
+                            <field name="date_start"/>
+                            <field name="date_stop"/>
+                            <field name="special"/>
+                        </form>
+                    </field>
+                    <separator colspan="4" string="States"/>
+                    <group>
+                        <field name="state" select="1" readonly="1"/>
+                        <button name="create_period" states="draft" string="Create Monthly Periods" type="object" icon="terp-document-new"/>
+                        <button name="create_period3" states="draft" string="Create 3 Months Periods" type="object" icon="terp-document-new"/>
+                    </group>
+                </form>
+            </field>
+        </record>
+        <record id="view_account_fiscalyear_tree" model="ir.ui.view">
+            <field name="name">account.fiscalyear.tree</field>
+            <field name="model">account.fiscalyear</field>
+            <field name="type">tree</field>
+            <field name="arch" type="xml">
+                <tree colors="blue:state == 'draft';gray:state == 'done' " string="Fiscalyear">
+                    <field name="code"/>
+                    <field name="name"/>
+                    <field name="company_id" groups="base.group_multi_company"/>
+                    <field name="state"/>
+                </tree>
+            </field>
+        </record>
+        <record id="view_account_fiscalyear_search" model="ir.ui.view">
+            <field name="name">account.fiscalyear.search</field>
+            <field name="model">account.fiscalyear</field>
+            <field name="type">search</field>
+            <field name="arch" type="xml">
+                <search string="Search Fiscalyear">
+                    <group>
+                        <filter string="Open" domain="[('state','=','draft')]" icon="terp-camera_test"/>
+                        <filter string="Closed" domain="[('state','=','done')]" icon="terp-dialog-close"/>
+                        <separator orientation="vertical"/>
+                        <field name="code"/>
+                        <field name="name"/>
+                        <field name="state"/>
+                    </group>
+                    <newline/>
+                    <group expand="0" string="Group By...">
+                        <filter string="State" context="{'group_by': 'state'}" icon="terp-stock_effects-object-colorize"/>
+                    </group>
+                </search>
+            </field>
+        </record>
+        <record id="action_account_fiscalyear_form" model="ir.actions.act_window">
+            <field name="name">Fiscal Years</field>
+            <field name="res_model">account.fiscalyear</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">tree,form</field>
+            <field name="help">Define your company's financial year according to your needs. A financial year is a period at the end of which a company's accounts are made up (usually 12 months). The financial year is usually referred to by the date in which it ends. For example, if a company's financial year ends November 30, 2011, then everything between December 1, 2010 and November 30, 2011  would be referred to as FY 2011. You are not obliged to follow the actual calendar year.</field>
+        </record>
+        <menuitem id="next_id_23" name="Periods" parent="account.menu_finance_accounting" sequence="8" />
+        <menuitem action="action_account_fiscalyear_form" id="menu_action_account_fiscalyear_form" parent="next_id_23"/>
+
+        <!--
+            Period
+        -->
+
+        <record id="view_account_period_form" model="ir.ui.view">
+            <field name="name">account.period.form</field>
+            <field name="model">account.period</field>
+            <field name="type">form</field>
+            <field name="arch" type="xml">
+                <form string="Period">
+                    <field name="name"/>
+                    <field name="code"/>
+                    <field name="date_start"/>
+                    <field name="date_stop"/>
+                    <field name="company_id" widget="selection" groups="base.group_multi_company"/>
+                    <field name="fiscalyear_id" widget="selection"/>
+                    <field name="special"/>
+                    <separator colspan="4" string="States"/>
+                    <field name="state"/>
+                    <button name="action_draft" states="done" string="Set to Draft" type="object" icon="gtk-convert" groups="account.group_account_manager"/>
+                </form>
+            </field>
+        </record>
+        <record id="view_account_period_tree" model="ir.ui.view">
+            <field name="name">account.period.tree</field>
+            <field name="model">account.period</field>
+            <field name="type">tree</field>
+            <field name="arch" type="xml">
+              <tree colors="blue:state == 'draft';gray:state == 'done' " string="Period">
+                    <field name="name"/>
+                    <field name="code"/>
+                    <field name="date_start"/>
+                    <field name="date_stop"/>
+                    <field name="special"/>
+                    <field name="company_id" groups="base.group_multi_company"/>
+                    <field name="state"/>
+                    <button name="action_draft" states="done" string="Set to Draft" type="object" icon="terp-document-new" groups="account.group_account_manager"/>
+                    <button name="%(action_account_period_close)d" states="draft" string="Close Period" type="action" icon="terp-camera_test"/>
+                </tree>
+            </field>
+        </record>
+
+        <record id="view_account_period_search" model="ir.ui.view">
+            <field name="name">account.period.search</field>
+            <field name="model">account.period</field>
+            <field name="type">search</field>
+            <field name="arch" type="xml">
+                <search string="Search Period">
+                    <group>
+                        <filter string="To Close" name="draft" domain="[('state','=','draft')]" icon="terp-dialog-close"/>
+                        <separator orientation="vertical"/>
+                        <field name="name"/>
+                        <field name="code"/>
+                    </group>
+                </search>
+            </field>
+        </record>
+
+        <record id="action_account_period_form" model="ir.actions.act_window">
+            <field name="name">Periods</field>
+            <field name="res_model">account.period</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">tree,form</field>
+            <field name="context">{'search_default_draft': 1}</field>
+            <field name="help">Here you can define a financial period, an interval of time in your company's financial year. An accounting period typically is a month or a quarter. It usually corresponds to the periods of the tax declaration. Create and manage periods from here and decide whether a period should be closed or left open depending on your company's activities over a specific period.</field>
+        </record>
+        <menuitem action="action_account_period_form" id="menu_action_account_period_form" parent="account.next_id_23"/>
+
+
+        <!--
+            Accounts
+        -->
+        <record id="view_account_form" model="ir.ui.view">
+            <field name="name">account.account.form</field>
+            <field name="model">account.account</field>
+            <field name="type">form</field>
+            <field name="arch" type="xml">
+                <form string="Account">
+                    <group col="6" colspan="4">
+                        <field name="name" select="1"/>
+                        <field name="code" select="1"/>
+                        <field name="company_id" widget="selection" groups="base.group_multi_company"/>
+                        <newline/>
+                        <field name="parent_id"/>
+                        <field name="type" select="1"/>
+                        <field name="user_type" select="1"/>
+                        <field name="active" groups="base.group_extended" />
+                        <newline/>
+                        <field name="debit" invisible="context.get('config_invisible', True)" attrs="{'readonly':[('type','=','view')]}"/>
+                        <field name="credit" invisible="context.get('config_invisible', True)" attrs="{'readonly':[('type','=','view')]}"/>
+                        <field name="balance" invisible="context.get('config_invisible', True)"/>
+                    </group>
+                    <notebook colspan="4">
+                        <page string="General Information">
+                            <newline/>
+                            <group col="2" colspan="2">
+                                <separator string="Currency" colspan="2"/>
+                                <field name="currency_id"/>
+                                <field name="currency_mode" attrs="{'readonly': [('currency_id','=',False)]}"/>
+                            </group>
+                            <group col="2" colspan="2">
+                                <separator string="Reconcile" colspan="2"/>
+                                <field name="reconcile"/>
+                            </group>
+                            <separator string="Default Taxes" colspan="4"/>
+                            <field colspan="4" name="tax_ids" nolabel="1" domain="[('parent_id','=',False)]"/>
+                            <separator string="Consolidated Children" colspan="4"/>
+                            <field name="child_consol_ids" colspan="4" nolabel="1" attrs="{'readonly':[('type','!=','consolidation')]}"/>
+                        </page>
+                        <page string="Notes">
+                            <field colspan="4" name="note" nolabel="1"/>
+                        </page>
+                    </notebook>
+                </form>
+            </field>
+        </record>
+
+        <record id="view_account_search" model="ir.ui.view">
+            <field name="name">account.account.search</field>
+            <field name="model">account.account</field>
+            <field name="type">search</field>
+            <field name="arch" type="xml">
+                <search string="Accounts">
+                    <group>
+                        <filter icon="terp-sale" string="Receivable Accounts" domain="[('type','=','receivable')]"/>
+                        <filter icon="terp-purchase" string="Payable Accounts" domain="[('type','=','payable')]"/>
+                        <separator orientation="vertical"/>
+                        <field name="code"/>
+                        <field name="name"/>
+                        <field name="user_type"/>
+                    </group>
+                    <newline/>
+                    <group expand="0" string="Group By...">
+                        <filter string="Parent Account" icon="terp-folder-orange" domain="" context="{'group_by':'parent_id'}"/>
+                        <separator orientation="vertical"/>
+                        <filter string="Account Type" icon="terp-stock_symbol-selection" domain="" context="{'group_by':'user_type'}"/>
+                        <filter string="Internal Type" icon="terp-stock_symbol-selection" domain="" context="{'group_by':'type'}"/>
+                    </group>
+                </search>
+            </field>
+        </record>
+
+        <record id="view_account_list" model="ir.ui.view">
+            <field name="name">account.account.list</field>
+            <field name="model">account.account</field>
+            <field name="type">tree</field>
+            <field name="field_parent">child_id</field>
+            <field name="arch" type="xml">
+                <tree colors="blue:type == 'view';black:type in ('other','receivable','payable','consolidation');gray:type == 'closed'" string="Chart of accounts" toolbar="1" >
+                    <field name="code"/>
+                    <field name="name"/>
+                    <field name="parent_id" invisible="1"/>
+                    <field name="user_type" invisible="1"/>
+                    <field name="debit"/>
+                    <field name="credit"/>
+                    <field name="balance"/>
+                    <field name="type"/>
+                    <field name="company_id" groups="base.group_multi_company"/>
+                    <field name="company_currency_id"/>
+                </tree>
+            </field>
+        </record>
+        <record id="view_treasory_graph" model="ir.ui.view">
+           <field name="name">account.treasury.graph</field>
+           <field name="model">account.account</field>
+           <field name="type">graph</field>
+           <field name="arch" type="xml">
+               <graph string="Treasury Analysis" type="bar">
+                  <field name="name"/>
+                  <field name="balance" operator="+"/>
+               </graph>
+         </field>
+      </record>
+        <record id="action_account_form" model="ir.actions.act_window">
+            <field name="name">Accounts</field>
+            <field name="res_model">account.account</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">tree,form,graph</field>
+            <field name="search_view_id" ref="view_account_search"/>
+            <field name="view_id" ref="view_account_list"/>
+            <field name="help">Create and manage the accounts you need to record journal entries. An account is part of a ledger allowing your company to register all kinds of debit and credit transactions. Companies present their annual accounts in two main parts: the balance sheet and the income statement (profit and loss account). The annual accounts of a company are required by law to disclose a certain amount of information. They have to be certified by an external auditor annually.</field>
+        </record>
+        <menuitem id="account_account_menu" name="Accounts" parent="menu_finance_accounting"/>
+        <menuitem action="action_account_form" id="menu_action_account_form" parent="account_account_menu"/>
+
+        <record id="view_account_tree" model="ir.ui.view">
+            <field name="name">account.account.tree</field>
+            <field name="model">account.account</field>
+            <field name="type">tree</field>
+            <field name="field_parent">child_id</field>
+            <field name="arch" type="xml">
+                <tree colors="blue:type == 'view';black:type in ('other','receivable','payable','consolidation');gray:type == 'closed'" string="Chart of accounts" toolbar="1" >
+                    <field name="code"/>
+                    <field name="name"/>
+                    <field name="debit"/>
+                    <field name="credit"/>
+                    <field name="balance"/>
+                    <field name="company_currency_id"/>
+                    <field name="company_id" groups="base.group_multi_company"/>
+                    <field name="type"/>
+                    <field name="parent_id" invisible="1"/>
+                </tree>
+            </field>
+        </record>
+        <record id="action_account_tree" model="ir.actions.act_window">
+            <field name="name">Chart of Accounts</field>
+            <field name="res_model">account.account</field>
+            <field name="view_type">tree</field>
+            <field name="view_id" ref="view_account_tree"/>
+            <field name="domain">[('parent_id','=',False)]</field>
+        </record>
+
+         <record id="view_account_gain_loss_tree" model="ir.ui.view">
+           <field name="name">Unrealized Gain or Loss</field>
+            <field name="model">account.account</field>
+            <field name="type">tree</field>
+            <field name="arch" type="xml">
+                <tree string="Unrealized Gains and losses">
+                    <field name="code"/>
+                    <field name="name"/>
+                    <field name="parent_id" invisible="1"/>
+                    <field name="user_type" invisible="1"/>
+                    <field name="type" invisible="1"/>
+                    <field name="currency_id"/>
+                    <field name="exchange_rate"/>
+                    <field name="foreign_balance"/>
+                    <field name="adjusted_balance"/>
+                    <field name="balance"/>
+                    <field name="unrealized_gain_loss"/>
+                </tree>
+            </field>
+        </record>
+
+          <record id="action_account_gain_loss" model="ir.actions.act_window">
+            <field name="name">Unrealized Gain or Loss</field>
+            <field name="res_model">account.account</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">tree</field>
+            <field name="view_id" ref="view_account_gain_loss_tree"/>
+            <field name="domain">[('currency_id','!=',False)]</field>
+            <field name="help">When doing multi-currency transactions, you may loose or gain some amount due to changes of exchange rate. This menu gives you a forecast of the Gain or Loss you'd realized if those transactions were ended today. Only for accounts having a secondary currency set.</field>
+        </record>
+
+        <menuitem
+            name="Unrealized Gain or Loss"
+            action="action_account_gain_loss"
+            groups="account.group_account_user"
+            id="menu_unrealized_gains_losses"
+            parent="account.menu_multi_currency"/>
+
+        <!--
+        Journal
+
+        Account Journal Columns
+        -->
+
+        <record id="view_journal_column_form" model="ir.ui.view">
+            <field name="name">account.journal.column.form</field>
+            <field name="model">account.journal.column</field>
+            <field name="type">form</field>
+            <field name="arch" type="xml">
+                <form string="Journal Column">
+                    <field colspan="4" name="name" select="1"/>
+                    <field name="field" select="1"/>
+                    <field name="sequence"/>
+                </form>
+            </field>
+        </record>
+        <record id="view_journal_column_tree" model="ir.ui.view">
+            <field name="name">account.journal.column.tree</field>
+            <field name="model">account.journal.column</field>
+            <field name="type">tree</field>
+            <field name="arch" type="xml">
+                <tree string="Journal Column">
+                    <field name="sequence"/>
+                    <field name="name"/>
+                </tree>
+            </field>
+        </record>
+
+        <record id="view_account_journal_view_search" model="ir.ui.view">
+            <field name="name">account.journal.view.search</field>
+            <field name="model">account.journal.view</field>
+            <field name="type">search</field>
+            <field name="arch" type="xml">
+                <search string="Journal View">
+                    <field name="name"/>
+                </search>
+            </field>
+        </record>
+        <record id="view_account_journal_view_tree" model="ir.ui.view">
+            <field name="name">account.journal.view.tree</field>
+            <field name="model">account.journal.view</field>
+            <field name="type">tree</field>
+            <field name="arch" type="xml">
+                <tree string="Journal View">
+                    <field name="name"/>
+                </tree>
+            </field>
+        </record>
+        <record id="view_account_journal_view_form" model="ir.ui.view">
+            <field name="name">account.journal.view.form</field>
+            <field name="model">account.journal.view</field>
+            <field name="type">form</field>
+            <field name="arch" type="xml">
+                <form string="Journal View">
+                    <field name="name"/>
+                    <field colspan="4" name="columns_id" nolabel="1" widget="one2many_list"/>
+                </form>
+            </field>
+        </record>
+        <record id="action_account_journal_view" model="ir.actions.act_window">
+            <field name="name">Journal Views</field>
+            <field name="res_model">account.journal.view</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">tree,form</field>
+            <field name="search_view_id" ref="view_account_journal_view_search"/>
+            <field name="help">Here you can customize an existing journal view or create a new view. Journal views determine the way you can record entries in your journal. Select the fields you want to appear in a journal and determine the sequence in which they will appear. Then you can create a new journal and link your view to it.</field>
+        </record>
+
+        <menuitem action="action_account_journal_view" id="menu_action_account_journal_view" parent="account.menu_journals" sequence="20"  groups="base.group_extended"/>
+
+        <!--
+    # Account Journal
+    -->
+
+        <record id="view_account_journal_tree" model="ir.ui.view">
+            <field name="name">account.journal.tree</field>
+            <field name="model">account.journal</field>
+            <field name="type">tree</field>
+            <field name="arch" type="xml">
+                <tree string="Account Journal">
+                    <field name="code"/>
+                    <field name="name"/>
+                    <field name="type"/>
+                    <field name="user_id"/>
+                    <field name="company_id" groups="base.group_multi_company"/>
+                </tree>
+            </field>
+        </record>
+        <record id="view_account_journal_search" model="ir.ui.view">
+            <field name="name">account.journal.search</field>
+            <field name="model">account.journal</field>
+            <field name="type">search</field>
+            <field name="arch" type="xml">
+                <search string="Search Account Journal">
+                    <group>
+                    <filter domain="['|', ('type', '=', 'sale'), ('type', '=', 'sale_refund')]" string="Sale" icon="terp-camera_test"/>
+                    <filter domain="['|', ('type', '=', 'purchase'), ('type', '=', 'purchase_refund')]" string="Purchase" icon="terp-purchase"/>
+                   <filter domain="['|', ('type', '=', 'cash'), ('type', '=', 'bank')]" string="Liquidity" icon="terp-dolar"/>
+                    <filter domain="['|', ('type', '=', 'general'), ('type', '=', 'situation')]" string="Others" icon="terp-stock"/>
+                    <separator orientation="vertical"/>
+                    <field name="code"/>
+                    <field name="name"/>
+                    <field name="user_id"/>
+                    </group>
+                    <newline/>
+                    <group expand="0" string="Group By...">
+                    <filter string="User" context="{'group_by':'user_id'}" icon="terp-personal"/>
+                    <separator orientation="vertical"/>
+                    <filter string="Type" context="{'group_by':'type'}" icon="terp-stock_symbol-selection"/>
+                    </group>
+                </search>
+            </field>
+        </record>
+        <record id="view_account_journal_form" model="ir.ui.view">
+            <field name="name">account.journal.form</field>
+            <field name="model">account.journal</field>
+            <field name="type">form</field>
+            <field name="arch" type="xml">
+                <form string="Account Journal">
+                    <group colspan="4" col="6">
+                        <field name="name" select="1"/>
+                        <field name="code" select="1"/>
+                        <field name="type" on_change="onchange_type(type, currency, context)"/>
+                    </group>
+                    <notebook colspan="4">
+                        <page string="General Information">
+                            <group col="2" colspan="2">
+                                <group colspan="2" col="2">
+                                    <separator string="Accounts" colspan="4"/>
+                                    <field name="default_debit_account_id" attrs="{'required':[('type','in', ('cash', 'bank'))]}" domain="[('type','&lt;&gt;','view'),('type','&lt;&gt;','consolidation')]"/>
+                                    <field name="default_credit_account_id" attrs="{'required':[('type','in',('cash', 'bank'))]}" domain="[('type','&lt;&gt;','view'),('type','&lt;&gt;','consolidation')]"/>
+                                </group>
+                                <group colspan="2" col="2">
+                                    <separator string="Journal View" colspan="4"/>
+                                    <field name="view_id" widget="selection"/>
+                                </group>
+                            </group>
+
+                            <group colspan="2" col="2">
+                                <separator string="Company" colspan="4"/>
+                                <field name="company_id" groups="base.group_multi_company"/>
+                                <field name="user_id" groups="base.group_extended"/>
+                                <field name="currency" groups="base.group_extended"/>
+                            </group>
+                            <group colspan="2" col="2" groups="base.group_extended">
+                                <separator string="Validations" colspan="4"/>
+                                <field name="allow_date"/>
+                            </group>
+                            <group colspan="2" col="2">
+                                <separator string="Other Configuration" colspan="4"/>
+                                <field name="centralisation" groups="base.group_extended"/>
+                                <field name="entry_posted"/>
+                            </group>
+                            <group colspan="2" col="2">
+                                <separator string="Invoicing Data" colspan="4"/>
+<!--                                <field name="invoice_sequence_id"/>-->
+                                <field name="group_invoice_lines"/>
+                            </group>
+                            <group colspan="2" col="2"> <!-- can't set the field as hidden for certain groups as it's required in the object and not in the view, and GTK doesn't handle that correctly -->
+                                <separator string="Sequence" colspan="4"/>
+                                <field name="sequence_id" required="0"/>
+                            </group>
+                        </page>
+                        <page string="Entry Controls" groups="base.group_extended">
+                            <separator colspan="4" string="Accounts Type Allowed (empty for no control)"/>
+                            <field colspan="4" name="type_control_ids" nolabel="1"/>
+                            <separator colspan="4" string="Accounts Allowed (empty for no control)"/>
+                            <field colspan="4" name="account_control_ids" nolabel="1"/>
+                        </page>
+                    </notebook>
+                </form>
+            </field>
+        </record>
+        <record id="action_account_journal_form" model="ir.actions.act_window">
+            <field name="name">Journals</field>
+            <field name="res_model">account.journal</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">tree,form</field>
+            <field name="help">Create and manage your company's journals from this menu. A journal is used to record transactions of all accounting data related to the day-to-day business of your company using double-entry bookkeeping system. Depending on the nature of its activities and the number of daily transactions, a company may keep several types of specialized journals such as a cash journal, purchase journal, sales journal...</field>
+        </record>
+        <menuitem action="action_account_journal_form" id="menu_action_account