mirror of
https://github.com/OCA/account-reconcile.git
synced 2025-01-20 12:27:39 +02:00
425 lines
17 KiB
Python
425 lines
17 KiB
Python
# -*- coding: utf-8 -*-
|
|
##############################################################################
|
|
#
|
|
# Copyright 2012-2013 Camptocamp SA (Guewen Baconnier)
|
|
# Copyright (C) 2010 Sébastien Beau
|
|
# Copyright 2015 Camptocamp SA (Damien Crier)
|
|
#
|
|
# 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 datetime import datetime
|
|
from openerp import models, api, fields, _
|
|
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
|
|
from openerp.exceptions import except_orm
|
|
from openerp import sql_db
|
|
|
|
# from openerp import pooler
|
|
|
|
import logging
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class EasyReconcileOptions(models.AbstractModel):
|
|
"""Options of a reconciliation profile
|
|
|
|
Columns shared by the configuration of methods
|
|
and by the reconciliation wizards.
|
|
This allows decoupling of the methods and the
|
|
wizards and allows to launch the wizards alone
|
|
"""
|
|
|
|
_name = 'easy.reconcile.options'
|
|
|
|
@api.model
|
|
def _get_rec_base_date(self):
|
|
return [
|
|
('end_period_last_credit', 'End of period of most recent credit'),
|
|
('newest', 'Most recent move line'),
|
|
('actual', 'Today'),
|
|
('end_period', 'End of period of most recent move line'),
|
|
('newest_credit', 'Date of most recent credit'),
|
|
('newest_debit', 'Date of most recent debit')
|
|
]
|
|
|
|
write_off = fields.Float('Write off allowed', default=0.)
|
|
account_lost_id = fields.Many2one('account.account',
|
|
string="Account Lost")
|
|
account_profit_id = fields.Many2one('account.account',
|
|
string="Account Profit")
|
|
journal_id = fields.Many2one('account.journal',
|
|
string="Journal")
|
|
date_base_on = fields.Selection('_get_rec_base_date',
|
|
required=True,
|
|
string='Date of reconciliation',
|
|
default='end_period_last_credit')
|
|
filter = fields.Char(string='Filter', size=128)
|
|
analytic_account_id = fields.Many2one('account.analytic.account',
|
|
string='Analytic_account',
|
|
help="Analytic account"
|
|
"for the write-off")
|
|
income_exchange_account_id = fields.Many2one('account.account',
|
|
string='Gain Exchange'
|
|
'Rate Account')
|
|
expense_exchange_account_id = fields.Many2one('account.account',
|
|
string='Loss Exchange'
|
|
'Rate Account')
|
|
|
|
|
|
class AccountEasyReconcileMethod(models.Model):
|
|
_name = 'account.easy.reconcile.method'
|
|
_description = 'reconcile method for account_easy_reconcile'
|
|
_inherit = 'easy.reconcile.options'
|
|
_order = 'sequence'
|
|
|
|
@api.model
|
|
def _get_all_rec_method(self):
|
|
return [
|
|
('easy.reconcile.simple.name', 'Simple. Amount and Name'),
|
|
('easy.reconcile.simple.partner', 'Simple. Amount and Partner'),
|
|
('easy.reconcile.simple.reference',
|
|
'Simple. Amount and Reference'),
|
|
]
|
|
|
|
@api.model
|
|
def _get_rec_method(self):
|
|
return self._get_all_rec_method()
|
|
|
|
name = fields.Selection('_get_rec_method', string='Type', required=True)
|
|
sequence = fields.Integer(string='Sequence',
|
|
default=1,
|
|
required=True,
|
|
help="The sequence field is used to order "
|
|
"the reconcile method"
|
|
)
|
|
task_id = fields.Many2one('account.easy.reconcile',
|
|
string='Task',
|
|
required=True,
|
|
ondelete='cascade'
|
|
)
|
|
company_id = fields.Many2one('res.company',
|
|
string='Company',
|
|
related="task_id.company_id",
|
|
store=True,
|
|
readonly=True
|
|
)
|
|
|
|
# def init(self, cr):
|
|
# """ Migration stuff
|
|
#
|
|
# Name is not anymore methods names but the name
|
|
# of the model which does the reconciliation
|
|
# """
|
|
# cr.execute("""
|
|
# UPDATE account_easy_reconcile_method
|
|
# SET name = 'easy.reconcile.simple.partner'
|
|
# WHERE name = 'action_rec_auto_partner'
|
|
# """)
|
|
# cr.execute("""
|
|
# UPDATE account_easy_reconcile_method
|
|
# SET name = 'easy.reconcile.simple.name'
|
|
# WHERE name = 'action_rec_auto_name'
|
|
# """)
|
|
|
|
|
|
class AccountEasyReconcile(models.Model):
|
|
|
|
_name = 'account.easy.reconcile'
|
|
_inherit = ['mail.thread']
|
|
_description = 'account easy reconcile'
|
|
|
|
@api.one
|
|
@api.depends('account')
|
|
def _get_total_unrec(self):
|
|
obj_move_line = self.env['account.move.line']
|
|
self.unreconciled_count = len(obj_move_line.search(
|
|
[('account_id', '=', self.account.id),
|
|
('reconcile_id', '=', False),
|
|
('reconcile_partial_id', '=', False)],
|
|
))
|
|
|
|
@api.one
|
|
@api.depends('account')
|
|
def _get_partial_rec(self):
|
|
obj_move_line = self.env['account.move.line']
|
|
self.reconciled_partial_count = len(obj_move_line.search(
|
|
[('account_id', '=', self.account.id),
|
|
('reconcile_id', '=', False),
|
|
('reconcile_partial_id', '!=', False)],
|
|
))
|
|
|
|
@api.one
|
|
@api.depends('history_ids')
|
|
def _last_history(self):
|
|
# do a search() for retrieving the latest history line,
|
|
# as a read() will badly split the list of ids with 'date desc'
|
|
# and return the wrong result.
|
|
history_obj = self.env['easy.reconcile.history']
|
|
last_history_rs = history_obj.search(
|
|
[('easy_reconcile_id', '=', self.id)],
|
|
limit=1, order='date desc'
|
|
)
|
|
self.last_history = last_history_rs[0] if last_history_rs else False
|
|
|
|
name = fields.Char(string='Name', size=32, required=True)
|
|
account = fields.Many2one('account.account',
|
|
string='Account',
|
|
required=True,
|
|
)
|
|
reconcile_method = fields.One2many('account.easy.reconcile.method',
|
|
'task_id',
|
|
string='Method'
|
|
)
|
|
unreconciled_count = fields.Integer(string='Unreconciled Items',
|
|
compute='_get_total_unrec'
|
|
)
|
|
reconciled_partial_count = fields.Integer(
|
|
string='Partially Reconciled Items',
|
|
compute='_get_partial_rec'
|
|
)
|
|
history_ids = fields.One2many('easy.reconcile.history',
|
|
'easy_reconcile_id',
|
|
string='History',
|
|
readonly=True
|
|
)
|
|
last_history = fields.Many2one('easy.reconcile.history',
|
|
string='readonly=True',
|
|
compute='_last_history',
|
|
readonly=True
|
|
)
|
|
company_id = fields.Many2one('res.company', string='Company')
|
|
|
|
@api.model
|
|
def _prepare_run_transient(self, rec_method):
|
|
return {'account_id': rec_method.task_id.account.id,
|
|
'write_off': rec_method.write_off,
|
|
'account_lost_id': (rec_method.account_lost_id and
|
|
rec_method.account_lost_id.id),
|
|
'account_profit_id': (rec_method.account_profit_id and
|
|
rec_method.account_profit_id.id),
|
|
'analytic_account_id': (rec_method.analytic_account_id and
|
|
rec_method.analytic_account_id.id),
|
|
'income_exchange_account_id':
|
|
(rec_method.income_exchange_account_id and
|
|
rec_method.income_exchange_account_id.id),
|
|
'expense_exchange_account_id':
|
|
(rec_method.income_exchange_account_id and
|
|
rec_method.income_exchange_account_id.id),
|
|
'journal_id': (rec_method.journal_id and
|
|
rec_method.journal_id.id),
|
|
'date_base_on': rec_method.date_base_on,
|
|
'filter': rec_method.filter}
|
|
|
|
@api.multi
|
|
def run_reconcile(self):
|
|
def find_reconcile_ids(fieldname, move_line_ids):
|
|
if not move_line_ids:
|
|
return []
|
|
sql = ("SELECT DISTINCT " + fieldname +
|
|
" FROM account_move_line "
|
|
" WHERE id in %s "
|
|
" AND " + fieldname + " IS NOT NULL")
|
|
self.env.cr.execute(sql, (tuple(move_line_ids),))
|
|
res = self.env.cr.fetchall()
|
|
return [row[0] for row in res]
|
|
|
|
# we use a new cursor to be able to commit the reconciliation
|
|
# often. We have to create it here and not later to avoid problems
|
|
# where the new cursor sees the lines as reconciles but the old one
|
|
# does not.
|
|
|
|
for rec in self:
|
|
ctx = self.env.context.copy()
|
|
ctx['commit_every'] = (
|
|
rec.account.company_id.reconciliation_commit_every
|
|
)
|
|
if ctx['commit_every']:
|
|
new_cr = sql_db.db_connect(self.env.cr.dbname).cursor()
|
|
else:
|
|
new_cr = self.env.cr
|
|
|
|
uid, context = self.env.uid, self.env.context
|
|
with api.Environment.manage():
|
|
self.env = api.Environment(new_cr, uid, context)
|
|
|
|
try:
|
|
all_ml_rec_ids = []
|
|
all_ml_partial_ids = []
|
|
|
|
for method in rec.reconcile_method:
|
|
rec_model = self.env[method.name]
|
|
auto_rec_id = rec_model.create(
|
|
self._prepare_run_transient(method)
|
|
)
|
|
|
|
ml_rec_ids, ml_partial_ids = (
|
|
auto_rec_id.automatic_reconcile()
|
|
)
|
|
|
|
all_ml_rec_ids += ml_rec_ids
|
|
all_ml_partial_ids += ml_partial_ids
|
|
|
|
reconcile_ids = find_reconcile_ids(
|
|
'reconcile_id',
|
|
all_ml_rec_ids
|
|
)
|
|
partial_ids = find_reconcile_ids(
|
|
'reconcile_partial_id',
|
|
all_ml_partial_ids
|
|
)
|
|
|
|
self.env['easy.reconcile.history'].create(
|
|
{
|
|
'easy_reconcile_id': rec.id,
|
|
'date': fields.datetime.now(),
|
|
'reconcile_ids': [
|
|
(4, rid) for rid in reconcile_ids
|
|
],
|
|
'reconcile_partial_ids': [
|
|
(4, rid) for rid in partial_ids
|
|
],
|
|
})
|
|
except Exception as e:
|
|
# In case of error, we log it in the mail thread, log the
|
|
# stack trace and create an empty history line; otherwise,
|
|
# the cron will just loop on this reconcile task.
|
|
_logger.exception(
|
|
"The reconcile task %s had an exception: %s",
|
|
rec.name, e.message
|
|
)
|
|
message = "There was an error during reconciliation : %s" \
|
|
% e.message
|
|
rec.message_post(body=message)
|
|
self.env['easy.reconcile.history'].create(
|
|
{
|
|
'easy_reconcile_id': rec.id,
|
|
'date': fields.datetime.now(),
|
|
'reconcile_ids': [],
|
|
'reconcile_partial_ids': [],
|
|
}
|
|
)
|
|
finally:
|
|
if ctx['commit_every']:
|
|
new_cr.commit()
|
|
new_cr.close()
|
|
|
|
# self.env.cr.close()
|
|
|
|
return True
|
|
|
|
@api.model
|
|
def _no_history(self, rec):
|
|
""" Raise an `orm.except_orm` error, supposed to
|
|
be called when there is no history on the reconciliation
|
|
task.
|
|
"""
|
|
raise except_orm(
|
|
_('Error'),
|
|
_('There is no history of reconciled '
|
|
'items on the task: %s.') % rec.name)
|
|
|
|
@api.model
|
|
def _open_move_line_list(self, move_line_ids, name):
|
|
return {
|
|
'name': name,
|
|
'view_mode': 'tree,form',
|
|
'view_id': False,
|
|
'view_type': 'form',
|
|
'res_model': 'account.move.line',
|
|
'type': 'ir.actions.act_window',
|
|
'nodestroy': True,
|
|
'target': 'current',
|
|
'domain': unicode([('id', 'in', move_line_ids)]),
|
|
}
|
|
|
|
@api.multi
|
|
def open_unreconcile(self):
|
|
""" Open the view of move line with the unreconciled move lines"""
|
|
self.ensure_one()
|
|
obj_move_line = self.env['account.move.line']
|
|
line_ids = obj_move_line.search(
|
|
[('account_id', '=', self.account.id),
|
|
('reconcile_id', '=', False),
|
|
('reconcile_partial_id', '=', False)])
|
|
name = _('Unreconciled items')
|
|
return self._open_move_line_list(line_ids and line_ids.ids or [], name)
|
|
|
|
@api.multi
|
|
def open_partial_reconcile(self):
|
|
""" Open the view of move line with the unreconciled move lines"""
|
|
self.ensure_one()
|
|
obj_move_line = self.env['account.move.line']
|
|
line_ids = obj_move_line.search(
|
|
[('account_id', '=', self.account.id),
|
|
('reconcile_id', '=', False),
|
|
('reconcile_partial_id', '!=', False)])
|
|
name = _('Partial reconciled items')
|
|
return self._open_move_line_list(line_ids and line_ids.ids or [], name)
|
|
|
|
@api.model
|
|
def last_history_reconcile(self, rec_id):
|
|
""" Get the last history record for this reconciliation profile
|
|
and return the action which opens move lines reconciled
|
|
"""
|
|
if isinstance(rec_id, (tuple, list)):
|
|
assert len(rec_id) == 1, \
|
|
"Only 1 id expected"
|
|
rec_id = rec_id[0]
|
|
rec = self.browse(rec_id)
|
|
if not rec.last_history:
|
|
self._no_history(rec)
|
|
return rec.last_history.open_reconcile()
|
|
|
|
@api.model
|
|
def last_history_partial(self, rec_id):
|
|
""" Get the last history record for this reconciliation profile
|
|
and return the action which opens move lines reconciled
|
|
"""
|
|
if isinstance(rec_id, (tuple, list)):
|
|
assert len(rec_id) == 1, \
|
|
"Only 1 id expected"
|
|
rec_id = rec_id[0]
|
|
rec = self.browse(rec_id)
|
|
if not rec.last_history:
|
|
self._no_history(rec)
|
|
return rec.last_history.open_partial()
|
|
|
|
@api.model
|
|
def run_scheduler(self, run_all=None):
|
|
""" Launch the reconcile with the oldest run
|
|
This function is mostly here to be used with cron task
|
|
|
|
:param run_all: if set it will ingore lookup and launch
|
|
all reconciliation
|
|
:returns: True in case of success or raises an exception
|
|
|
|
"""
|
|
def _get_date(reconcile):
|
|
if reconcile.last_history.date:
|
|
return datetime.strptime(reconcile.last_history.date,
|
|
DEFAULT_SERVER_DATETIME_FORMAT)
|
|
else:
|
|
return datetime.min
|
|
|
|
reconciles = self.search([])
|
|
assert reconciles.ids, "No easy reconcile available"
|
|
if run_all:
|
|
reconciles.run_reconcile()
|
|
return True
|
|
reconciles.sorted(key=_get_date)
|
|
older = reconciles[0]
|
|
older.run_reconcile()
|
|
return True
|