[FIX] Fix transactionID module import and completion

[IMP] Usability and filter
  [DOC] Little typo
(lp:c2c-financial-addons/6.1 rev 66)
This commit is contained in:
Joël Grand-Guillaume
2012-06-22 17:45:50 +02:00
parent 00c42b3ab7
commit 00cd824d35
11 changed files with 146 additions and 61 deletions

View File

@@ -20,3 +20,4 @@
##############################################################################
import statement
import partner

View File

@@ -24,6 +24,7 @@ logger = netsvc.Logger()
from openerp.osv.orm import Model, fields
from openerp.osv import fields, osv
from operator import itemgetter, attrgetter
import datetime
class ErrorTooManyPartner(Exception):
"""
@@ -146,7 +147,7 @@ class AccountStatementCompletionRule(Model):
inv = inv_obj.browse(cursor, uid, inv_id[0])
res['partner_id'] = inv.partner_id.id
elif inv_id and len(inv_id) > 1:
raise ErrorTooManyPartner(_('Line named "%s" was matched by more than one partner.')%(st_line.name,st_line.id))
raise ErrorTooManyPartner(_('Line named "%s" (Ref:%s) was matched by more than one partner.')%(st_line.name,st_line.ref))
st_vals = st_obj.get_values_for_line(cursor, uid, profile_id = st_line.statement_id.profile_id.id,
partner_id = res.get('partner_id',False), line_type = st_line.type, amount = st_line.amount, context = context)
res.update(st_vals)
@@ -178,7 +179,7 @@ class AccountStatementCompletionRule(Model):
so = so_obj.browse(cursor, uid, so_id[0])
res['partner_id'] = so.partner_id.id
elif so_id and len(so_id) > 1:
raise ErrorTooManyPartner(_('Line named "%s" was matched by more than one partner.')%(st_line.name,st_line.id))
raise ErrorTooManyPartner(_('Line named "%s" (Ref:%s) was matched by more than one partner.')%(st_line.name,st_line.ref))
st_vals = st_obj.get_values_for_line(cursor, uid, profile_id = st_line.statement_id.profile_id.id,
partner_id = res.get('partner_id',False), line_type = st_line.type, amount = st_line.amount, context = context)
res.update(st_vals)
@@ -209,13 +210,13 @@ class AccountStatementCompletionRule(Model):
compt = 0
if st_line:
ids = partner_obj.search(cursor, uid, [['bank_statement_label', '!=', False]], context=context)
for partner in self.browse(cursor, uid, ids, context=context):
for partner in partner_obj.browse(cursor, uid, ids, context=context):
for partner_label in partner.bank_statement_label.split(';'):
if partner_label in st_line.label:
compt += 1
res['partner_id'] = partner.id
if compt > 1:
raise ErrorTooManyPartner(_('Line named "%s" was matched by more than one partner.')%(st_line.name,st_line.id))
raise ErrorTooManyPartner(_('Line named "%s" (Ref:%s) was matched by more than one partner.')%(st_line.name,st_line.ref))
if res:
st_vals = st_obj.get_values_for_line(cursor, uid, profile_id = st_line.statement_id.profile_id.id,
partner_id = res.get('partner_id',False), line_type = st_line.type, amount = st_line.amount, context = context)
@@ -243,12 +244,13 @@ class AccountStatementCompletionRule(Model):
st_obj = self.pool.get('account.bank.statement.line')
st_line = st_obj.browse(cursor,uid,line_id)
if st_line:
sql = "SELECT id FROM res_partner WHERE name ~* '.*%s.*'"
cursor.execute(sql, (st_line.label,))
sql = "SELECT id FROM res_partner WHERE name ~* %s"
pattern = ".*%s.*" % st_line.label
cursor.execute(sql, (pattern,))
result = cursor.fetchall()
if len(result) > 1:
raise ErrorTooManyPartner(_('Line named "%s" was matched by more than one partner.')%(st_line.name,st_line.id))
for id in result:
raise ErrorTooManyPartner(_('Line named "%s" (Ref:%s) was matched by more than one partner.')%(st_line.name,st_line.ref))
for id in result[0]:
res['partner_id'] = id
if res:
st_vals = st_obj.get_values_for_line(cursor, uid, profile_id = st_line.statement_id.profile_id.id,
@@ -290,12 +292,9 @@ class AccountStatementLine(Model):
the profile.. We will ignore line for which already_completed is ticked.
:return:
A dict of value that can be passed directly to the write method of
the statement line or {}
{'partner_id': value,
'account_id' : value,
...}
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.profil')
st_obj = self.pool.get('account.bank.statement.line')
@@ -312,7 +311,7 @@ class AccountStatementLine(Model):
# Merge the result
res[line.id].update(vals)
except ErrorTooManyPartner, exc:
msg = "Line ID %s had following error: %s" % (line.id, str(exc))
msg = "Line ID %s had following error: %s" % (line.id, exc.value)
errors_stack.append(msg)
if errors_stack:
msg = u"\n".join(errors_stack)
@@ -326,36 +325,64 @@ class AccountBankSatement(Model):
"""
_inherit = "account.bank.statement"
_columns = {
'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.
: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
"""
error_log = ""
user_name = self.pool.get('res.users').read(cr, uid, uid, ['name'])['name']
log = self.read(cr, uid, stat_id, ['completion_logs'], context=context)['completion_logs']
log_line = log and log.split("\n") or []
completion_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
if error_msg:
error_log = error_msg
log_line[0:0] = [completion_date + ' : '
+ _("Bank Statement ID %s has %s lines completed by %s") %(stat_id, number_imported, user_name)
+ "\n" + error_log + "-------------" + "\n"]
log = "\n".join(log_line)
self.write(cr, uid, [stat_id], {'completion_logs' : log}, context=context)
logger.notifyChannel('Bank Statement Completion', netsvc.LOG_INFO,
"Bank Statement ID %s has %s lines completed"%(stat_id, number_imported))
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 !
"""
# TODO: Test the errors system, we should be able to complete all line that
# passed, and raise an error for all other at once..
if not context:
context={}
stat_line_obj = self.pool.get('account.bank.statement.line')
errors_msg=False
msg = ""
compl_lines = 0
for stat in self.browse(cr, uid, ids, context=context):
ctx = context.copy()
line_ids = map(lambda x:x.id, stat.line_ids)
try:
res = stat_line_obj.get_line_values_from_rules(cr, uid, line_ids, context=ctx)
except ErrorTooManyPartner, exc:
errors_msg = str(exc)
for id in line_ids:
vals = res.get(id, False)
if vals:
for line in stat.line_ids:
res = {}
try:
res = stat_line_obj.get_line_values_from_rules(cr, uid, [line.id], context=ctx)
if res:
compl_lines += 1
except ErrorTooManyPartner, exc:
msg += exc.value + "\n"
except Exception, exc:
msg += exc.value + "\n"
# vals = res and res.keys() or False
if res:
vals = res[line.id]
vals['already_completed'] = True
stat_line_obj.write(cr, uid, id, vals, context=ctx)
# cr.commit()
# TOTEST: I don't know if this is working...
if errors_msg:
# raise osv.except_osv(_('Error'), errors_msg)
warning = {
'title': _('Error!'),
'message' : errors_msg,
}
return {'warning': warning}
stat_line_obj.write(cr, uid, line.id, vals, context=ctx)
self.write_completion_log(cr, uid, stat.id, msg, compl_lines, context=context)
return True

View File

@@ -27,6 +27,12 @@
<button name="button_auto_completion" string="Auto Completion" states='draft,open' type="object" colspan="1"/>
</xpath>
<xpath expr="/form/notebook/page[@string='Journal Entries']" position="after">
<page string="Completion Logs" attrs="{'invisible':[('completion_logs','=',False)]}">
<field name="completion_logs" colspan="4" nolabel="1" attrs="{'invisible':[('completion_logs','=',False)]}"/>
</page>
</xpath>
</data>
</field>
</record>

View File

@@ -110,6 +110,11 @@ class CreditPartnerStatementImporter(osv.osv_memory):
ftype.replace('.',''),
context=context
)
# We should return here the profile for which we executed the import
return {'type': 'ir.actions.act_window_close'}
return {
'domain': "[('id','in', ["+','.join(map(str,[sid]))+"])]",
'name': 'Imported Bank Statement',
'view_type': 'form',
'view_mode': 'tree,form',
'res_model': 'account.bank.statement',
'type': 'ir.actions.act_window',
}

View File

@@ -91,6 +91,7 @@
<field name="type">search</field>
<field name="arch" type="xml">
<xpath expr="/search/group/field[@name='name']" position="before">
<field name="id"/>
<field name="profile_id"/>
<field name="credit_partner_id"/>
<separator orientation="vertical"/>
@@ -109,6 +110,9 @@
<field name="inherit_id" ref="account.view_bank_statement_tree"/>
<field name="type">tree</field>
<field name="arch" type="xml">
<xpath expr="/tree/field[@name='name']" position="before">
<field name="id"/>
</xpath>
<xpath expr="/tree/field[@name='name']" position="after">
<field name="profile_id"/>
</xpath>
@@ -132,7 +136,8 @@
</xpath>
<!-- Add a new group before the first one with name, profil and date -->
<xpath expr="/form/group[@col='7']" position="before">
<group col="6" colspan="4">
<group col="8" colspan="4">
<field name="id" select="1" readonly="1"/>
<field name="name" select="1"/>
<field name="profile_id" select="1" required="1" on_change="onchange_imp_config_id(profile_id)" widget="selection"/>
<field name="date" select="1" on_change="onchange_date(date, company_id)"/>

View File

@@ -4,9 +4,8 @@
<record id="bank_statement_completion_rule_4" model="account.statement.completion.rule">
<field name="name">Match from line reference (based on transaction ID)</field>
<field name="sequence">40</field>
<field name="sequence">30</field>
<field name="function_to_call">get_from_transaction_id_and_so</field>
</field>
</record>
</data>

View File

@@ -31,8 +31,9 @@ class AccountStatementCompletionRule(Model):
_inherit = "account.statement.completion.rule"
def _get_functions(self):
res = super (self,AccountStatementCompletionRule)._get_functions()
def _get_functions(self, cr, uid, context=None):
res = super (AccountStatementCompletionRule, self)._get_functions(
cr, uid, context=context)
res.append(('get_from_transaction_id_and_so', 'From line reference (based on SO transaction ID'))
return res
@@ -58,16 +59,16 @@ class AccountStatementCompletionRule(Model):
res = {}
if st_line:
so_obj = self.pool.get('sale.order')
so_id = so_obj.search(cursor, uid, [('transaction_id', '=', st_line.transaction_id)])
so_id = so_obj.search(cr, uid, [('transaction_id', '=', st_line.transaction_id)])
if so_id and len(so_id) == 1:
so = so_obj.browse(cursor, uid, so_id[0])
so = so_obj.browse(cr, uid, so_id[0])
res['partner_id'] = so.partner_id.id
res['ref'] = so.name
elif so_id and len(so_id) > 1:
raise ErrorTooManyPartner(_('Line named "%s" was matched by more than one partner.')%(st_line.name,st_line.id))
raise ErrorTooManyPartner(_('Line named "%s" (Ref:%s) was matched by more than one partner.')%(st_line.name,st_line.ref))
if so_id:
st_vals = st_obj.get_values_for_line(cr, uid, profile_id = st_line.statement_id.profile_id.id,
partner_id = res.get('partner_id',False), line_type = st_line.type, st_line.amount, context)
partner_id = res.get('partner_id',False), line_type = st_line.type, amount = st_line.amount, context=context)
res.update(st_vals)
return res

View File

@@ -6,7 +6,7 @@
<field name="name">account_bank_statement_import_base.bank_statement.view_form</field>
<field name="model">account.bank.statement</field>
<field name="inherit_id" ref="account.view_bank_statement_form" />
<field eval="16" name="priority"/>
<field eval="20" name="priority"/>
<field name="type">form</field>
<field name="arch" type="xml">
<data>

View File

@@ -19,3 +19,4 @@
#
##############################################################################
import parser
import statement

View File

@@ -50,15 +50,15 @@ class TransactionIDFileParser(FileParser):
@classmethod
def parser_for(cls, parser_name):
"""
Used by the new_bank_statement_parser class factory. Return true if
the providen name is generic_csvxls_transaction
"""
"""
Used by the new_bank_statement_parser class factory. Return true if
the providen name is generic_csvxls_transaction
"""
return parser_name == 'generic_csvxls_transaction'
def get_st_line_vals(self, line, *args, **kwargs):
"""T
his method must return a dict of vals that can be passed to create
"""
This method must return a dict of vals that can be passed to create
method of statement line in order to record it. It is the responsibility
of every parser to give this dict of vals, so each one can implement his
own way of recording the lines.
@@ -90,13 +90,10 @@ class TransactionIDFileParser(FileParser):
"""
Compute the commission from value of each line
"""
res = super(GenericFileParser, self)._post(*args, **kwargs)
res = super(TransactionIDFileParser, self)._post(*args, **kwargs)
val = 0.0
for row in self.result_row_list:
val += row.get('commission_amount',0.0)
self.commission_global_amount = val
return res

View File

@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Joel Grand-Guillaume
# Copyright 2011-2012 Camptocamp SA
#
# 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, fields
from openerp.osv import fields, osv
class AccountStatementProfil(Model):
_inherit = "account.statement.profil"
def get_import_type_selection(self, cr, uid, context=None):
"""
Has to be inherited to add parser
"""
res = super(AccountStatementProfil, self).get_import_type_selection(cr, uid, context=context)
res.append(('generic_csvxls_transaction','Generic .csv/.xls based on SO transaction ID'))
return res
_columns = {
'import_type': fields.selection(get_import_type_selection, 'Type of import', required=True,
help = "Choose here the method by which you want to import bank statement for this profil."),
}