mirror of
https://github.com/OCA/account-reconcile.git
synced 2025-01-20 12:27:39 +02:00
Merge remote-tracking branch 'upstream/7.0' into 7.0-improve-acc-number-completion
Conflicts: account_statement_bankaccount_completion/statement.py account_statement_bankaccount_completion/tests/test_bankaccount_completion.py account_statement_base_completion/tests/test_base_completion.py account_statement_regex_account_completion/tests/test_regex_account_completion.py
This commit is contained in:
@@ -19,59 +19,67 @@
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
{'name': "Bank statement base completion",
|
||||
'version': '1.0.3',
|
||||
'author': 'Camptocamp',
|
||||
'maintainer': 'Camptocamp',
|
||||
'category': 'Finance',
|
||||
'complexity': 'normal',
|
||||
'depends': ['account_statement_ext'],
|
||||
'description': """
|
||||
The goal of this module is to improve the basic bank statement, help dealing with huge volume of
|
||||
reconciliation by providing basic rules to identify the partner of a bank statement line.
|
||||
Each bank statement profile can have its own rules to be applied according to a sequence order.
|
||||
{
|
||||
'name': "Bank statement base completion",
|
||||
'version': '1.0.3',
|
||||
'author': 'Camptocamp',
|
||||
'maintainer': 'Camptocamp',
|
||||
'category': 'Finance',
|
||||
'complexity': 'normal',
|
||||
'depends': ['account_statement_ext'],
|
||||
'description': """
|
||||
The goal of this module is to improve the basic bank statement, help dealing
|
||||
with huge volume of reconciliation by providing basic rules to identify the
|
||||
partner of a bank statement line.
|
||||
Each bank statement profile can have its own rules to be applied according to a
|
||||
sequence order.
|
||||
|
||||
Some basic rules are provided in this module:
|
||||
|
||||
1) Match from statement line label (based on partner field 'Bank Statement Label')
|
||||
1) Match from statement line label (based on partner field 'Bank Statement
|
||||
Label')
|
||||
2) Match from statement line label (based on partner name)
|
||||
3) Match from statement line reference (based on Invoice number)
|
||||
|
||||
You can easily override this module and add your own rules in your own one. The basic rules only
|
||||
fill in the partner, but you can use them to fill in any value of the line (in the future, we will
|
||||
add a rule to automatically match and reconcile the line).
|
||||
You can easily override this module and add your own rules in your own one. The
|
||||
basic rules only fill in the partner, but you can use them to fill in any
|
||||
value of the line (in the future, we will add a rule to automatically match and
|
||||
reconcile the line).
|
||||
|
||||
It adds as well a label on the bank statement line (on which the pre-define rules can match) and
|
||||
a char field on the partner called 'Bank Statement Label'. Using the pre-define rules, you will be
|
||||
able to match various labels for a partner.
|
||||
It adds as well a label on the bank statement line (on which the pre-define
|
||||
rules can match) and a char field on the partner called 'Bank Statement Label'.
|
||||
Using the pre-define rules, you will be able to match various labels for a
|
||||
partner.
|
||||
|
||||
The reference of the line is always used by the reconciliation process. We're supposed to copy
|
||||
there (or write manually) the matching string. This can be: the order Number or an invoice number,
|
||||
or anything that will be found in the invoice accounting entry part to make the match.
|
||||
The reference of the line is always used by the reconciliation process. We're
|
||||
supposed to copy there (or write manually) the matching string. This can be:
|
||||
the order Number or an invoice number, or anything that will be found in the
|
||||
invoice accounting entry part to make the match.
|
||||
|
||||
You can use it with our account_advanced_reconcile module to automatize the reconciliation process.
|
||||
You can use it with our account_advanced_reconcile module to automatize the
|
||||
reconciliation process.
|
||||
|
||||
|
||||
TODO: The rules that look for invoices to find out the partner should take back the payable / receivable
|
||||
account from there directly instead of retrieving it from partner properties !
|
||||
|
||||
""",
|
||||
'website': 'http://www.camptocamp.com',
|
||||
'data': [
|
||||
'statement_view.xml',
|
||||
'partner_view.xml',
|
||||
'data.xml',
|
||||
'security/ir.model.access.csv',
|
||||
],
|
||||
'demo': [],
|
||||
'test': [
|
||||
'test/partner.yml',
|
||||
'test/invoice.yml',
|
||||
'test/supplier_invoice.yml',
|
||||
'test/completion_test.yml'
|
||||
],
|
||||
'installable': True,
|
||||
'images': [],
|
||||
'auto_install': False,
|
||||
'license': 'AGPL-3',
|
||||
TODO: The rules that look for invoices to find out the partner should take back
|
||||
the payable / receivable account from there directly instead of retrieving it
|
||||
from partner properties !
|
||||
""",
|
||||
'website': 'http://www.camptocamp.com',
|
||||
'data': [
|
||||
'statement_view.xml',
|
||||
'partner_view.xml',
|
||||
'data.xml',
|
||||
'security/ir.model.access.csv',
|
||||
],
|
||||
'demo': [],
|
||||
'test': [
|
||||
'test/partner.yml',
|
||||
'test/invoice.yml',
|
||||
'test/supplier_invoice.yml',
|
||||
'test/completion_test.yml'
|
||||
],
|
||||
'installable': True,
|
||||
'images': [],
|
||||
'auto_install': False,
|
||||
'license': 'AGPL-3',
|
||||
}
|
||||
|
||||
@@ -1,38 +1,39 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#################################################################################
|
||||
# #
|
||||
##########################################################################
|
||||
#
|
||||
# Copyright (C) 2011 Akretion & Camptocamp
|
||||
# Author : Sébastien BEAU, Joel Grand-Guillaume #
|
||||
# #
|
||||
# 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/>. #
|
||||
# #
|
||||
#################################################################################
|
||||
# Author : Sébastien BEAU, Joel Grand-Guillaume
|
||||
#
|
||||
# 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 openerp.osv.orm import Model
|
||||
from openerp.osv import fields
|
||||
from openerp.osv import orm, fields
|
||||
|
||||
|
||||
class res_partner(Model):
|
||||
"""
|
||||
Add a bank label on the partner so that we can use it to match
|
||||
class ResPartner(orm.Model):
|
||||
"""Add a bank label on the partner so that we can use it to match
|
||||
this partner when we found this in a statement line.
|
||||
"""
|
||||
_inherit = 'res.partner'
|
||||
|
||||
_columns = {
|
||||
'bank_statement_label': fields.char('Bank Statement Label', size=100,
|
||||
help="Enter the various label found on your bank statement separated by a ; If "
|
||||
"one of this label is include in the bank statement line, the partner will be automatically "
|
||||
"filled (as long as you use this method/rules in your statement profile)."),
|
||||
}
|
||||
'bank_statement_label': fields.char(
|
||||
'Bank Statement Label', size=100,
|
||||
help="Enter the various label found on your bank statement "
|
||||
"separated by a ; If one of this label is include in the bank "
|
||||
"statement line, the partner will be automatically filled (as "
|
||||
"long as you use this method/rules in your statement "
|
||||
"profile)."),
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
<record id="bk_view_partner_form" model="ir.ui.view">
|
||||
<field name="name">account_bank_statement_import.view.partner.form</field>
|
||||
<field name="model">res.partner</field>
|
||||
<field name="type">form</field>
|
||||
<field name="priority">20</field>
|
||||
<field name="inherit_id" ref="account.view_partner_property_form"/>
|
||||
<field name="arch" type="xml">
|
||||
@@ -17,6 +16,6 @@
|
||||
</field>
|
||||
</record>
|
||||
|
||||
|
||||
|
||||
</data>
|
||||
</openerp>
|
||||
|
||||
@@ -31,7 +31,7 @@ import psycopg2
|
||||
from collections import defaultdict
|
||||
import re
|
||||
from openerp.tools.translate import _
|
||||
from openerp.osv import osv, orm, fields
|
||||
from openerp.osv import orm, fields
|
||||
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
|
||||
from operator import attrgetter
|
||||
|
||||
@@ -40,10 +40,10 @@ _logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ErrorTooManyPartner(Exception):
|
||||
""" New Exception definition that is raised when more than one partner is
|
||||
matched by the completion rule.
|
||||
"""
|
||||
New Exception definition that is raised when more than one partner is matched by
|
||||
the completion rule.
|
||||
"""
|
||||
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
@@ -55,19 +55,18 @@ class ErrorTooManyPartner(Exception):
|
||||
|
||||
|
||||
class AccountStatementProfil(orm.Model):
|
||||
"""Extend the class to add rules per profile that will match at least the
|
||||
partner, but it could also be used to match other values as well.
|
||||
"""
|
||||
Extend the class to add rules per profile that will match at least the partner,
|
||||
but it could also be used to match other values as well.
|
||||
"""
|
||||
|
||||
_inherit = "account.statement.profile"
|
||||
|
||||
_columns = {
|
||||
# @Akretion: For now, we don't implement this features, but this would probably be there:
|
||||
# 'auto_completion': fields.text('Auto Completion'),
|
||||
# 'transferts_account_id':fields.many2one('account.account', 'Transferts Account'),
|
||||
# => You can implement it in a module easily, we design it with your needs in mind
|
||||
# as well!
|
||||
# @Akretion: For now, we don't implement this features, but this would
|
||||
# probably be there: 'auto_completion': fields.text('Auto Completion'),
|
||||
# 'transferts_account_id':fields.many2one('account.account',
|
||||
# 'Transferts Account'),
|
||||
# => You can implement it in a module easily, we design it with your
|
||||
# needs in mind as well!
|
||||
|
||||
'rule_ids': fields.many2many(
|
||||
'account.statement.completion.rule',
|
||||
@@ -84,8 +83,7 @@ class AccountStatementProfil(orm.Model):
|
||||
return sorted(prof.rule_ids, key=attrgetter('sequence'))
|
||||
|
||||
def _find_values_from_rules(self, cr, uid, calls, line, context=None):
|
||||
"""
|
||||
This method will execute all related rules, in their sequence order,
|
||||
"""This method will execute all related rules, in their sequence order,
|
||||
to retrieve all the values returned by the first rules that will match.
|
||||
:param calls: list of lookup function name available in rules
|
||||
:param dict line: read of the concerned account.bank.statement.line
|
||||
@@ -97,12 +95,10 @@ class AccountStatementProfil(orm.Model):
|
||||
|
||||
...}
|
||||
"""
|
||||
if context is None:
|
||||
context = {}
|
||||
if not calls:
|
||||
calls = self._get_rules(cr, uid, line['profile_id'], context=context)
|
||||
calls = self._get_rules(
|
||||
cr, uid, line['profile_id'], context=context)
|
||||
rule_obj = self.pool.get('account.statement.completion.rule')
|
||||
|
||||
for call in calls:
|
||||
method_to_call = getattr(rule_obj, call.function_to_call)
|
||||
if len(inspect.getargspec(method_to_call).args) == 6:
|
||||
@@ -116,35 +112,37 @@ class AccountStatementProfil(orm.Model):
|
||||
|
||||
|
||||
class AccountStatementCompletionRule(orm.Model):
|
||||
"""
|
||||
This will represent all the completion method that we can have to
|
||||
fullfill the bank statement lines. You'll be able to extend them in you own module
|
||||
and choose those to apply for every statement profile.
|
||||
"""This will represent all the completion method that we can have to
|
||||
fullfill the bank statement lines. You'll be able to extend them in you own
|
||||
module and choose those to apply for every statement profile.
|
||||
The goal of a rule is to fullfill at least the partner of the line, but
|
||||
if possible also the reference because we'll use it in the reconciliation
|
||||
process. The reference should contain the invoice number or the SO number
|
||||
or any reference that will be matched by the invoice accounting move.
|
||||
"""
|
||||
|
||||
_name = "account.statement.completion.rule"
|
||||
_order = "sequence asc"
|
||||
|
||||
def _get_functions(self, cr, uid, context=None):
|
||||
"""
|
||||
List of available methods for rules. Override this to add you own.
|
||||
"""
|
||||
"""List of available methods for rules. Override this to add you own."""
|
||||
return [
|
||||
('get_from_ref_and_invoice', 'From line reference (based on customer invoice number)'),
|
||||
('get_from_ref_and_supplier_invoice', 'From line reference (based on supplier invoice number)'),
|
||||
('get_from_label_and_partner_field', 'From line label (based on partner field)'),
|
||||
('get_from_label_and_partner_name', 'From line label (based on partner name)')]
|
||||
('get_from_ref_and_invoice',
|
||||
'From line reference (based on customer invoice number)'),
|
||||
('get_from_ref_and_supplier_invoice',
|
||||
'From line reference (based on supplier invoice number)'),
|
||||
('get_from_label_and_partner_field',
|
||||
'From line label (based on partner field)'),
|
||||
('get_from_label_and_partner_name',
|
||||
'From line label (based on partner name)')
|
||||
]
|
||||
|
||||
def __get_functions(self, cr, uid, context=None):
|
||||
""" Call method which can be inherited """
|
||||
return self._get_functions(cr, uid, context=context)
|
||||
|
||||
_columns = {
|
||||
'sequence': fields.integer('Sequence', help="Lower means parsed first."),
|
||||
'sequence': fields.integer('Sequence',
|
||||
help="Lower means parsed first."),
|
||||
'name': fields.char('Name', size=128),
|
||||
'profile_ids': fields.many2many(
|
||||
'account.statement.profile',
|
||||
@@ -163,8 +161,9 @@ class AccountStatementCompletionRule(orm.Model):
|
||||
type_domain = ('out_invoice', 'out_refund')
|
||||
number_field = 'number'
|
||||
else:
|
||||
raise osv.except_osv(_('System error'),
|
||||
_('Invalid invoice type for completion: %') % inv_type)
|
||||
raise orm.except_orm(
|
||||
_('System error'),
|
||||
_('Invalid invoice type for completion: %') % inv_type)
|
||||
|
||||
inv_id = inv_obj.search(cr, uid,
|
||||
[(number_field, '=', st_line['ref'].strip()),
|
||||
@@ -174,17 +173,19 @@ class AccountStatementCompletionRule(orm.Model):
|
||||
if len(inv_id) == 1:
|
||||
inv = inv_obj.browse(cr, uid, inv_id[0], context=context)
|
||||
else:
|
||||
raise ErrorTooManyPartner(_('Line named "%s" (Ref:%s) was matched by more '
|
||||
'than one partner while looking on %s invoices') %
|
||||
(st_line['name'], st_line['ref'], inv_type))
|
||||
raise ErrorTooManyPartner(
|
||||
_('Line named "%s" (Ref:%s) was matched by more than one '
|
||||
'partner while looking on %s invoices') %
|
||||
(st_line['name'], st_line['ref'], inv_type))
|
||||
return inv
|
||||
return False
|
||||
|
||||
def _from_invoice(self, cr, uid, line, inv_type, context):
|
||||
"""Populate statement line values"""
|
||||
if not inv_type in ('supplier', 'customer'):
|
||||
raise osv.except_osv(_('System error'),
|
||||
_('Invalid invoice type for completion: %') % inv_type)
|
||||
if inv_type not in ('supplier', 'customer'):
|
||||
raise orm.except_orm(_('System error'),
|
||||
_('Invalid invoice type for completion: %') %
|
||||
inv_type)
|
||||
res = {}
|
||||
inv = self._find_invoice(cr, uid, line, inv_type, context=context)
|
||||
if inv:
|
||||
@@ -195,7 +196,6 @@ class AccountStatementCompletionRule(orm.Model):
|
||||
partner_id = inv.commercial_partner_id.id
|
||||
else:
|
||||
partner_id = inv.partner_id.id
|
||||
|
||||
res = {'partner_id': partner_id,
|
||||
'account_id': inv.account_id.id,
|
||||
'type': inv_type}
|
||||
@@ -206,10 +206,10 @@ class AccountStatementCompletionRule(orm.Model):
|
||||
|
||||
# Should be private but data are initialised with no update XML
|
||||
def get_from_ref_and_supplier_invoice(self, cr, uid, line, context=None):
|
||||
"""
|
||||
Match the partner based on the invoice supplier invoice number and the reference of the statement
|
||||
line. Then, call the generic get_values_for_line method to complete other values.
|
||||
If more than one partner matched, raise the ErrorTooManyPartner error.
|
||||
"""Match the partner based on the invoice supplier invoice number and
|
||||
the reference of the statement line. Then, call the generic
|
||||
get_values_for_line method to complete other values. If more than one
|
||||
partner matched, raise the ErrorTooManyPartner error.
|
||||
|
||||
:param dict line: read of the concerned account.bank.statement.line
|
||||
:return:
|
||||
@@ -224,10 +224,10 @@ class AccountStatementCompletionRule(orm.Model):
|
||||
|
||||
# Should be private but data are initialised with no update XML
|
||||
def get_from_ref_and_invoice(self, cr, uid, line, context=None):
|
||||
"""
|
||||
Match the partner based on the invoice number and the reference of the statement
|
||||
line. Then, call the generic get_values_for_line method to complete other values.
|
||||
If more than one partner matched, raise the ErrorTooManyPartner error.
|
||||
"""Match the partner based on the invoice number and the reference of
|
||||
the statement line. Then, call the generic get_values_for_line method to
|
||||
complete other values. If more than one partner matched, raise the
|
||||
ErrorTooManyPartner error.
|
||||
|
||||
:param dict line: read of the concerned account.bank.statement.line
|
||||
:return:
|
||||
@@ -257,7 +257,7 @@ class AccountStatementCompletionRule(orm.Model):
|
||||
|
||||
...}
|
||||
"""
|
||||
partner_obj = self.pool.get('res.partner')
|
||||
partner_obj = self.pool['res.partner']
|
||||
st_obj = self.pool.get('account.bank.statement.line')
|
||||
res = {}
|
||||
# As we have to iterate on each partner for each line,
|
||||
@@ -267,12 +267,15 @@ class AccountStatementCompletionRule(orm.Model):
|
||||
# but this option is not really maintanable
|
||||
if not context.get('label_memoizer'):
|
||||
context['label_memoizer'] = defaultdict(list)
|
||||
partner_ids = partner_obj.search(cr,
|
||||
uid,
|
||||
[('bank_statement_label', '!=', False)])
|
||||
partner_ids = partner_obj.search(
|
||||
cr, uid, [('bank_statement_label', '!=', False)],
|
||||
context=context)
|
||||
line_ids = context.get('line_ids', [])
|
||||
for partner in partner_obj.browse(cr, uid, partner_ids, context=context):
|
||||
vals = '|'.join(re.escape(x.strip()) for x in partner.bank_statement_label.split(';'))
|
||||
for partner in partner_obj.browse(cr, uid, partner_ids,
|
||||
context=context):
|
||||
vals = '|'.join(
|
||||
re.escape(x.strip())
|
||||
for x in partner.bank_statement_label.split(';'))
|
||||
or_regex = ".*%s.*" % vals
|
||||
sql = ("SELECT id from account_bank_statement_line"
|
||||
" WHERE id in %s"
|
||||
@@ -281,32 +284,29 @@ class AccountStatementCompletionRule(orm.Model):
|
||||
pairs = cr.fetchall()
|
||||
for pair in pairs:
|
||||
context['label_memoizer'][pair[0]].append(partner)
|
||||
|
||||
if st_line['id'] in context['label_memoizer']:
|
||||
found_partner = context['label_memoizer'][st_line['id']]
|
||||
if len(found_partner) > 1:
|
||||
msg = (_('Line named "%s" (Ref:%s) was matched by '
|
||||
'more than one partner while looking on partner label: %s') %
|
||||
(st_line['name'], st_line['ref'], ','.join([x.name for x in found_partner])))
|
||||
msg = (_('Line named "%s" (Ref:%s) was matched by more than '
|
||||
'one partner while looking on partner label: %s') %
|
||||
(st_line['name'], st_line['ref'],
|
||||
','.join([x.name for x in found_partner])))
|
||||
raise ErrorTooManyPartner(msg)
|
||||
res['partner_id'] = found_partner[0].id
|
||||
st_vals = st_obj.get_values_for_line(cr,
|
||||
uid,
|
||||
profile_id=st_line['profile_id'],
|
||||
master_account_id=st_line['master_account_id'],
|
||||
partner_id=found_partner[0].id,
|
||||
line_type=False,
|
||||
amount=st_line['amount'] if st_line['amount'] else 0.0,
|
||||
context=context)
|
||||
st_vals = st_obj.get_values_for_line(
|
||||
cr, uid, profile_id=st_line['profile_id'],
|
||||
master_account_id=st_line['master_account_id'],
|
||||
partner_id=found_partner[0].id, line_type=False,
|
||||
amount=st_line['amount'] if st_line['amount'] else 0.0,
|
||||
context=context)
|
||||
res.update(st_vals)
|
||||
return res
|
||||
|
||||
def get_from_label_and_partner_name(self, cr, uid, st_line, context=None):
|
||||
"""
|
||||
Match the partner based on the label field of the statement line
|
||||
and the name of the partner.
|
||||
Then, call the generic get_values_for_line method to complete other values.
|
||||
If more than one partner matched, raise the ErrorTooManyPartner error.
|
||||
"""Match the partner based on the label field of the statement line
|
||||
and the name of the partner. Then, call the generic get_values_for_line
|
||||
method to complete other values. If more than one partner matched, raise
|
||||
the ErrorTooManyPartner error.
|
||||
|
||||
:param dict st_line: read of the concerned account.bank.statement.line
|
||||
:return:
|
||||
@@ -320,7 +320,8 @@ class AccountStatementCompletionRule(orm.Model):
|
||||
res = {}
|
||||
# We memoize allowed partner
|
||||
if not context.get('partner_memoizer'):
|
||||
context['partner_memoizer'] = tuple(self.pool['res.partner'].search(cr, uid, []))
|
||||
context['partner_memoizer'] = tuple(
|
||||
self.pool['res.partner'].search(cr, uid, []))
|
||||
if not context['partner_memoizer']:
|
||||
return res
|
||||
st_obj = self.pool.get('account.bank.statement.line')
|
||||
@@ -328,33 +329,41 @@ class AccountStatementCompletionRule(orm.Model):
|
||||
# example: 'John J. Doe (No 1)' is escaped to 'John J\. Doe \(No 1\)'
|
||||
# See http://stackoverflow.com/a/400316/1504003 for a list of
|
||||
# chars to escape. Postgres is POSIX-ARE, compatible with
|
||||
# POSIX-ERE excepted that '\' must be escaped inside brackets according to:
|
||||
# http://www.postgresql.org/docs/9.0/static/functions-matching.html
|
||||
# POSIX-ERE excepted that '\' must be escaped inside brackets according
|
||||
# to:
|
||||
# http://www.postgresql.org/docs/9.0/static/functions-matching.html
|
||||
# in chapter 9.7.3.6. Limits and Compatibility
|
||||
sql = """SELECT id FROM (
|
||||
SELECT id, regexp_matches(%s, regexp_replace(name,'([\.\^\$\*\+\?\(\)\[\{\\\|])', %s, 'g'), 'i') AS name_match FROM res_partner
|
||||
WHERE id IN %s) AS res_patner_matcher
|
||||
WHERE name_match IS NOT NULL"""
|
||||
cr.execute(sql, (st_line['name'], r"\\\1", context['partner_memoizer']))
|
||||
sql = """
|
||||
SELECT id FROM (
|
||||
SELECT id,
|
||||
regexp_matches(%s,
|
||||
regexp_replace(name,'([\.\^\$\*\+\?\(\)\[\{\\\|])', %s,
|
||||
'g'), 'i') AS name_match
|
||||
FROM res_partner
|
||||
WHERE id IN %s)
|
||||
AS res_patner_matcher
|
||||
WHERE name_match IS NOT NULL"""
|
||||
cr.execute(
|
||||
sql, (st_line['name'], r"\\\1", context['partner_memoizer']))
|
||||
result = cr.fetchall()
|
||||
if not result:
|
||||
return res
|
||||
if len(result) > 1:
|
||||
raise ErrorTooManyPartner(_('Line named "%s" (Ref:%s) was matched by more '
|
||||
'than one partner while looking on partner by name') %
|
||||
(st_line['name'], st_line['ref']))
|
||||
raise ErrorTooManyPartner(
|
||||
_('Line named "%s" (Ref:%s) was matched by more than one '
|
||||
'partner while looking on partner by name') %
|
||||
(st_line['name'], st_line['ref']))
|
||||
res['partner_id'] = result[0][0]
|
||||
st_vals = st_obj.get_values_for_line(cr,
|
||||
uid,
|
||||
profile_id=st_line['profile_id'],
|
||||
master_account_id=st_line['master_account_id'],
|
||||
partner_id=res['partner_id'],
|
||||
line_type=False,
|
||||
amount=st_line['amount'] if st_line['amount'] else 0.0,
|
||||
context=context)
|
||||
st_vals = st_obj.get_values_for_line(
|
||||
cr, uid, profile_id=st_line['profile_id'],
|
||||
master_account_id=st_line['master_account_id'],
|
||||
partner_id=res['partner_id'], line_type=False,
|
||||
amount=st_line['amount'] if st_line['amount'] else 0.0,
|
||||
context=context)
|
||||
res.update(st_vals)
|
||||
return res
|
||||
|
||||
|
||||
class AccountStatement(orm.Model):
|
||||
_inherit = "account.bank.statement"
|
||||
|
||||
@@ -364,24 +373,26 @@ class AccountStatement(orm.Model):
|
||||
line_without_account = line_obj.search(cr, uid, [
|
||||
['statement_id', '=', stat_id],
|
||||
['account_id', '=', False],
|
||||
], context=context)
|
||||
], context=context)
|
||||
if line_without_account:
|
||||
stat = self.browse(cr, uid, stat_id, context=context)
|
||||
raise orm.except_orm(_('User error'),
|
||||
_('You should fill all account on the line of the'
|
||||
' statement %s')%stat.name)
|
||||
raise orm.except_orm(
|
||||
_('User error'),
|
||||
_('You should fill all account on the line of the'
|
||||
' statement %s') % stat.name)
|
||||
return super(AccountStatement, self).button_confirm_bank(
|
||||
cr, uid, ids, context=context)
|
||||
cr, uid, ids, context=context)
|
||||
|
||||
|
||||
class AccountStatementLine(orm.Model):
|
||||
"""
|
||||
Add sparse field on the statement line to allow to store all the
|
||||
bank infos that are given by a bank/office. You can then add you own in your
|
||||
module. The idea here is to store all bank/office infos in the additionnal_bank_fields
|
||||
serialized field when importing the file. If many values, add a tab in the bank
|
||||
statement line to store your specific one. Have a look in account_statement_base_import
|
||||
module to see how we've done it.
|
||||
module. The idea here is to store all bank/office infos in the
|
||||
additionnal_bank_fields serialized field when importing the file. If many
|
||||
values, add a tab in the bank statement line to store your specific one.
|
||||
Have a look in account_statement_base_import module to see how we've done
|
||||
it.
|
||||
"""
|
||||
_inherit = "account.bank.statement.line"
|
||||
_order = "already_completed desc, date asc"
|
||||
@@ -411,38 +422,42 @@ class AccountStatementLine(orm.Model):
|
||||
}
|
||||
|
||||
def _get_line_values_from_rules(self, cr, uid, line, rules, context=None):
|
||||
"""
|
||||
We'll try to find out the values related to the line based on rules setted on
|
||||
the profile.. We will ignore line for which already_completed is ticked.
|
||||
"""We'll try to find out the values related to the line based on rules
|
||||
setted on the profile.. We will ignore line for which already_completed
|
||||
is ticked.
|
||||
|
||||
:return:
|
||||
A dict of dict value that can be passed directly to the write method of
|
||||
the statement line or {}. The first dict has statement line ID as a key:
|
||||
{117009: {'partner_id': 100997, 'account_id': 489L}}
|
||||
A dict of dict value that can be passed directly to the write
|
||||
method of the statement line or {}. The first dict has statement
|
||||
line ID as a key: {117009: {'partner_id': 100997,
|
||||
'account_id': 489L}}
|
||||
"""
|
||||
profile_obj = self.pool.get('account.statement.profile')
|
||||
profile_obj = self.pool['account.statement.profile']
|
||||
if line.get('already_completed'):
|
||||
return {}
|
||||
# Ask the rule
|
||||
vals = profile_obj._find_values_from_rules(cr, uid, rules, line, context)
|
||||
vals = profile_obj._find_values_from_rules(
|
||||
cr, uid, rules, line, context)
|
||||
if vals:
|
||||
vals['id'] = line['id']
|
||||
return vals
|
||||
return {}
|
||||
|
||||
def _get_available_columns(self, statement_store, include_serializable=False):
|
||||
def _get_available_columns(self, statement_store,
|
||||
include_serializable=False):
|
||||
"""Return writeable by SQL columns"""
|
||||
statement_line_obj = self.pool['account.bank.statement.line']
|
||||
model_cols = statement_line_obj._columns
|
||||
avail = [k for k, col in model_cols.iteritems() if not hasattr(col, '_fnct')]
|
||||
avail = [
|
||||
k for k, col in model_cols.iteritems() if not hasattr(col, '_fnct')]
|
||||
keys = [k for k in statement_store[0].keys() if k in avail]
|
||||
# add sparse fields..
|
||||
if include_serializable:
|
||||
for k, col in model_cols.iteritems():
|
||||
if k in statement_store[0].keys() and \
|
||||
isinstance(col, fields.sparse) and \
|
||||
col.serialization_field not in keys and \
|
||||
col._type == 'char':
|
||||
if k in statement_store[0].keys() and \
|
||||
isinstance(col, fields.sparse) and \
|
||||
col.serialization_field not in keys and \
|
||||
col._type == 'char':
|
||||
keys.append(col.serialization_field)
|
||||
keys.sort()
|
||||
return keys
|
||||
@@ -472,7 +487,9 @@ class AccountStatementLine(orm.Model):
|
||||
"""
|
||||
statement_line_obj = self.pool['account.bank.statement.line']
|
||||
model_cols = statement_line_obj._columns
|
||||
sparse_fields = dict([(k, col) for k, col in model_cols.iteritems() if isinstance(col, fields.sparse) and col._type == 'char'])
|
||||
sparse_fields = dict(
|
||||
[(k, col) for k, col in model_cols.iteritems() if isinstance(
|
||||
col, fields.sparse) and col._type == 'char'])
|
||||
values = []
|
||||
for statement in statement_store:
|
||||
to_json_k = set()
|
||||
@@ -480,7 +497,8 @@ class AccountStatementLine(orm.Model):
|
||||
for k, col in sparse_fields.iteritems():
|
||||
if k in st_copy:
|
||||
to_json_k.add(col.serialization_field)
|
||||
serialized = st_copy.setdefault(col.serialization_field, {})
|
||||
serialized = st_copy.setdefault(
|
||||
col.serialization_field, {})
|
||||
serialized[k] = st_copy[k]
|
||||
for k in to_json_k:
|
||||
st_copy[k] = simplejson.dumps(st_copy[k])
|
||||
@@ -493,16 +511,21 @@ class AccountStatementLine(orm.Model):
|
||||
does not exist"""
|
||||
statement_line_obj = self.pool['account.bank.statement.line']
|
||||
statement_line_obj.check_access_rule(cr, uid, [], 'create')
|
||||
statement_line_obj.check_access_rights(cr, uid, 'create', raise_exception=True)
|
||||
cols = self._get_available_columns(statement_store, include_serializable=True)
|
||||
statement_line_obj.check_access_rights(
|
||||
cr, uid, 'create', raise_exception=True)
|
||||
cols = self._get_available_columns(
|
||||
statement_store, include_serializable=True)
|
||||
statement_store = self._prepare_manyinsert(statement_store, cols)
|
||||
tmp_vals = (', '.join(cols), ', '.join(['%%(%s)s' % i for i in cols]))
|
||||
sql = "INSERT INTO account_bank_statement_line (%s) VALUES (%s);" % tmp_vals
|
||||
sql = "INSERT INTO account_bank_statement_line (%s) " \
|
||||
"VALUES (%s);" % tmp_vals
|
||||
try:
|
||||
cr.executemany(sql, tuple(self._serialize_sparse_fields(cols, statement_store)))
|
||||
cr.executemany(
|
||||
sql, tuple(self._serialize_sparse_fields(cols,
|
||||
statement_store)))
|
||||
except psycopg2.Error as sql_err:
|
||||
cr.rollback()
|
||||
raise osv.except_osv(_("ORM bypass error"),
|
||||
raise orm.except_orm(_("ORM bypass error"),
|
||||
sql_err.pgerror)
|
||||
|
||||
def _update_line(self, cr, uid, vals, context=None):
|
||||
@@ -516,18 +539,18 @@ class AccountStatementLine(orm.Model):
|
||||
cols = self._get_available_columns([vals])
|
||||
vals = self._prepare_insert(vals, cols)
|
||||
tmp_vals = (', '.join(['%s = %%(%s)s' % (i, i) for i in cols]))
|
||||
sql = "UPDATE account_bank_statement_line SET %s where id = %%(id)s;" % tmp_vals
|
||||
sql = "UPDATE account_bank_statement_line " \
|
||||
"SET %s where id = %%(id)s;" % tmp_vals
|
||||
try:
|
||||
cr.execute(sql, vals)
|
||||
except psycopg2.Error as sql_err:
|
||||
cr.rollback()
|
||||
raise osv.except_osv(_("ORM bypass error"),
|
||||
raise orm.except_orm(_("ORM bypass error"),
|
||||
sql_err.pgerror)
|
||||
|
||||
|
||||
class AccountBankStatement(orm.Model):
|
||||
"""
|
||||
We add a basic button and stuff to support the auto-completion
|
||||
"""We add a basic button and stuff to support the auto-completion
|
||||
of the bank statement once line have been imported or manually fullfill.
|
||||
"""
|
||||
_inherit = "account.bank.statement"
|
||||
@@ -536,44 +559,42 @@ class AccountBankStatement(orm.Model):
|
||||
'completion_logs': fields.text('Completion Log', readonly=True),
|
||||
}
|
||||
|
||||
def write_completion_log(self, cr, uid, stat_id, error_msg, number_imported, context=None):
|
||||
"""
|
||||
Write the log in the completion_logs field of the bank statement to let the user
|
||||
know what have been done. This is an append mode, so we don't overwrite what
|
||||
already recoded.
|
||||
def write_completion_log(self, cr, uid, stat_id, error_msg,
|
||||
number_imported, context=None):
|
||||
"""Write the log in the completion_logs field of the bank statement to
|
||||
let the user know what have been done. This is an append mode, so we
|
||||
don't overwrite what already recoded.
|
||||
|
||||
:param int/long stat_id: ID of the account.bank.statement
|
||||
:param char error_msg: Message to add
|
||||
:number_imported int/long: Number of lines that have been completed
|
||||
:return True
|
||||
"""
|
||||
user_name = self.pool.get('res.users').read(cr, uid, uid,
|
||||
['name'], context=context)['name']
|
||||
user_name = self.pool.get('res.users').read(
|
||||
cr, uid, uid, ['name'], context=context)['name']
|
||||
statement = self.browse(cr, uid, stat_id, context=context)
|
||||
number_line = len(statement.line_ids)
|
||||
|
||||
log = self.read(cr, uid, stat_id, ['completion_logs'],
|
||||
context=context)['completion_logs']
|
||||
log = log if log else ""
|
||||
|
||||
completion_date = datetime.datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)
|
||||
message = (_("%s Bank Statement ID %s has %s/%s lines completed by %s \n%s\n%s\n") %
|
||||
(completion_date, stat_id, number_imported, number_line, user_name,
|
||||
error_msg, log))
|
||||
self.write(cr, uid, [stat_id], {'completion_logs': message}, context=context)
|
||||
completion_date = datetime.datetime.now().strftime(
|
||||
DEFAULT_SERVER_DATETIME_FORMAT)
|
||||
message = (_("%s Bank Statement ID %s has %s/%s lines completed by "
|
||||
"%s \n%s\n%s\n") % (completion_date, stat_id,
|
||||
number_imported, number_line,
|
||||
user_name, error_msg, log))
|
||||
self.write(
|
||||
cr, uid, [stat_id], {'completion_logs': message}, context=context)
|
||||
|
||||
body = (_('Statement ID %s auto-completed for %s/%s lines completed') %
|
||||
(stat_id, number_imported, number_line)),
|
||||
self.message_post(cr, uid,
|
||||
[stat_id],
|
||||
body=body,
|
||||
context=context)
|
||||
self.message_post(cr, uid, [stat_id], body=body, context=context)
|
||||
return True
|
||||
|
||||
def button_auto_completion(self, cr, uid, ids, context=None):
|
||||
"""
|
||||
Complete line with values given by rules and tic the already_completed
|
||||
checkbox so we won't compute them again unless the user untick them!
|
||||
"""Complete line with values given by rules and tic the
|
||||
already_completed checkbox so we won't compute them again unless the
|
||||
user untick them!
|
||||
"""
|
||||
if context is None:
|
||||
context = {}
|
||||
@@ -581,24 +602,27 @@ class AccountBankStatement(orm.Model):
|
||||
profile_obj = self.pool.get('account.statement.profile')
|
||||
compl_lines = 0
|
||||
stat_line_obj.check_access_rule(cr, uid, [], 'create')
|
||||
stat_line_obj.check_access_rights(cr, uid, 'create', raise_exception=True)
|
||||
stat_line_obj.check_access_rights(
|
||||
cr, uid, 'create', raise_exception=True)
|
||||
for stat in self.browse(cr, uid, ids, context=context):
|
||||
msg_lines = []
|
||||
ctx = context.copy()
|
||||
ctx['line_ids'] = tuple((x.id for x in stat.line_ids))
|
||||
b_profile = stat.profile_id
|
||||
rules = profile_obj._get_rules(cr, uid, b_profile, context=context)
|
||||
profile_id = b_profile.id # Only for perfo even it gains almost nothing
|
||||
# Only for perfo even it gains almost nothing
|
||||
profile_id = b_profile.id
|
||||
master_account_id = b_profile.receivable_account_id
|
||||
master_account_id = master_account_id.id if master_account_id else False
|
||||
master_account_id = master_account_id.id if \
|
||||
master_account_id else False
|
||||
res = False
|
||||
for line in stat_line_obj.read(cr, uid, ctx['line_ids']):
|
||||
try:
|
||||
# performance trick
|
||||
line['master_account_id'] = master_account_id
|
||||
line['profile_id'] = profile_id
|
||||
res = stat_line_obj._get_line_values_from_rules(cr, uid, line,
|
||||
rules, context=ctx)
|
||||
res = stat_line_obj._get_line_values_from_rules(
|
||||
cr, uid, line, rules, context=ctx)
|
||||
if res:
|
||||
compl_lines += 1
|
||||
except ErrorTooManyPartner, exc:
|
||||
@@ -606,17 +630,20 @@ class AccountBankStatement(orm.Model):
|
||||
except Exception, exc:
|
||||
msg_lines.append(repr(exc))
|
||||
error_type, error_value, trbk = sys.exc_info()
|
||||
st = "Error: %s\nDescription: %s\nTraceback:" % (error_type.__name__, error_value)
|
||||
st = "Error: %s\nDescription: %s\nTraceback:" % (
|
||||
error_type.__name__, error_value)
|
||||
st += ''.join(traceback.format_tb(trbk, 30))
|
||||
_logger.error(st)
|
||||
if res:
|
||||
# stat_line_obj.write(cr, uid, [line.id], vals, context=ctx)
|
||||
try:
|
||||
stat_line_obj._update_line(cr, uid, res, context=context)
|
||||
stat_line_obj._update_line(
|
||||
cr, uid, res, context=context)
|
||||
except Exception as exc:
|
||||
msg_lines.append(repr(exc))
|
||||
error_type, error_value, trbk = sys.exc_info()
|
||||
st = "Error: %s\nDescription: %s\nTraceback:" % (error_type.__name__, error_value)
|
||||
st = "Error: %s\nDescription: %s\nTraceback:" % (
|
||||
error_type.__name__, error_value)
|
||||
st += ''.join(traceback.format_tb(trbk, 30))
|
||||
_logger.error(st)
|
||||
# we can commit as it is not needed to be atomic
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
<field name="model">account.bank.statement</field>
|
||||
<field name="inherit_id" ref="account.view_bank_statement_form" />
|
||||
<field eval="16" name="priority"/>
|
||||
<field name="type">form</field>
|
||||
<field name="arch" type="xml">
|
||||
<data>
|
||||
<xpath expr="/form/sheet/notebook/page/field[@name='line_ids']/form/group/field[@name='sequence']" position="after">
|
||||
@@ -37,7 +36,6 @@
|
||||
<field name="name">account_bank_statement_import_base.bank_statement.auto_cmpl</field>
|
||||
<field name="model">account.bank.statement</field>
|
||||
<field name="inherit_id" ref="account.view_bank_statement_form" />
|
||||
<field name="type">form</field>
|
||||
<field name="arch" type="xml">
|
||||
<data>
|
||||
<xpath expr="/form/sheet/notebook/page/field[@name='line_ids']/tree/field[@name='amount']" position="after">
|
||||
@@ -51,7 +49,6 @@
|
||||
<field name="name">account.statement.profile.view</field>
|
||||
<field name="model">account.statement.profile</field>
|
||||
<field name="inherit_id" ref="account_statement_ext.statement_importer_view_form"/>
|
||||
<field name="type">form</field>
|
||||
<field name="arch" type="xml">
|
||||
<field name="bank_statement_prefix" position="after">
|
||||
<separator colspan="4" string="Auto-Completion Rules"/>
|
||||
@@ -59,12 +56,11 @@
|
||||
</field>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
|
||||
|
||||
|
||||
<record id="statement_st_completion_rule_view_form" model="ir.ui.view">
|
||||
<field name="name">account.statement.completion.rule.view</field>
|
||||
<field name="model">account.statement.completion.rule</field>
|
||||
<field name="type">form</field>
|
||||
<field name="arch" type="xml">
|
||||
<form string="Statement Completion Rule">
|
||||
<field name="sequence"/>
|
||||
@@ -79,7 +75,6 @@
|
||||
<record id="statement_st_completion_rule_view_tree" model="ir.ui.view">
|
||||
<field name="name">account.statement.completion.rule.view</field>
|
||||
<field name="model">account.statement.completion.rule</field>
|
||||
<field name="type">tree</field>
|
||||
<field name="arch" type="xml">
|
||||
<tree string="Statement Completion Rule">
|
||||
<field name="sequence"/>
|
||||
@@ -97,8 +92,8 @@
|
||||
</record>
|
||||
|
||||
<menuitem string="Statement Completion Rule" action="action_st_completion_rule_tree"
|
||||
id="menu_action_st_completion_rule_tree_menu" parent="account.menu_configuration_misc"
|
||||
id="menu_action_st_completion_rule_tree_menu" parent="account.menu_configuration_misc"
|
||||
sequence="30"/>
|
||||
|
||||
|
||||
</data>
|
||||
</openerp>
|
||||
|
||||
Reference in New Issue
Block a user