From 74da19fc6c1f7d8305eb363a637cb7c9974df7d9 Mon Sep 17 00:00:00 2001 From: "Torvald B. Bringsvor" Date: Mon, 1 Dec 2014 22:33:28 +0100 Subject: [PATCH] Rewritten for new api. Added myself to the readme file. --- account_reversal/README.rst | 1 + account_reversal/account_reversal.py | 118 +++++++++++---------------- 2 files changed, 47 insertions(+), 72 deletions(-) diff --git a/account_reversal/README.rst b/account_reversal/README.rst index 247a91af5..5dff2b400 100644 --- a/account_reversal/README.rst +++ b/account_reversal/README.rst @@ -17,3 +17,4 @@ Contributors * Alexis de Lattre (Akretion) * Guewen Baconnier (Camptocamp) * Nicolas Bessi (Camptocamp) + * Torvald Bringsvor (Bringsvor Consulting) diff --git a/account_reversal/account_reversal.py b/account_reversal/account_reversal.py index c062959f5..344f50565 100644 --- a/account_reversal/account_reversal.py +++ b/account_reversal/account_reversal.py @@ -23,28 +23,25 @@ # ############################################################################## -from openerp.osv import fields, orm +from openerp import fields, models -class account_move(orm.Model): +class account_move(models.Model): _inherit = "account.move" - _columns = { - 'to_be_reversed': fields.boolean( - 'To Be Reversed', - help='Check this box if your entry has to be' - 'reversed at the end of period.'), - 'reversal_id': fields.many2one( - 'account.move', - 'Reversal Entry', - ondelete='set null', - readonly=True), - } + to_be_reversed = fields.Boolean( + 'To Be Reversed', + help='Check this box if your entry has to be' + 'reversed at the end of period.') + reversal_id = fields.Many2one( + 'account.move', + 'Reversal Entry', + ondelete='set null', + readonly=True) - def _move_reversal(self, cr, uid, move, reversal_date, + def _move_reversal(self, reversal_date, reversal_period_id=False, reversal_journal_id=False, - move_prefix=False, move_line_prefix=False, - context=None): + move_prefix=False, move_line_prefix=False): """ Create the reversal of a move @@ -59,62 +56,49 @@ class account_move(orm.Model): :return: Returns the id of the created reversal move """ - if context is None: - context = {} - move_line_obj = self.pool.get('account.move.line') - period_obj = self.pool.get('account.period') - period_ctx = context.copy() - period_ctx['company_id'] = move.company_id.id - period_ctx['account_period_prefer_normal'] = True + period_obj = self.env['account.period'] if not reversal_period_id: - reversal_period_id = period_obj.find( - cr, uid, reversal_date, context=period_ctx)[0] + reversal_period_id = period_obj.with_context( + company_id=self.company_id.id, + account_period_prefer_normal=True).find(reversal_date)[0] if not reversal_journal_id: - reversal_journal_id = move.journal_id.id + reversal_journal_id = self.journal_id.id - reversal_ref = ''.join([x for x in [move_prefix, move.ref] if x]) - reversal_move_id = self.copy(cr, uid, move.id, - default={ - 'date': reversal_date, - 'period_id': reversal_period_id, - 'ref': reversal_ref, - 'journal_id': reversal_journal_id, - 'to_be_reversed': False, - }, - context=context) + reversal_ref = ''.join([x for x in [move_prefix, self.ref] if x]) + reversal_move = self.copy(default={ + 'date': reversal_date, + 'period_id': reversal_period_id.id, + 'ref': reversal_ref, + 'journal_id': reversal_journal_id, + 'to_be_reversed': False, + }) - self.write(cr, uid, [move.id], - {'reversal_id': reversal_move_id, - 'to_be_reversed': False}, - context=context) + self.write({ + 'reversal_id': reversal_move.id, + 'to_be_reversed': False, + }) - reversal_move = self.browse(cr, uid, reversal_move_id, context=context) for reversal_move_line in reversal_move.line_id: reversal_ml_name = ' '.join( [x for x in [move_line_prefix, reversal_move_line.name] if x] ) - move_line_obj.write( - cr, - uid, - [reversal_move_line.id], + reversal_move_line.write( {'debit': reversal_move_line.credit, 'credit': reversal_move_line.debit, 'amount_currency': reversal_move_line.amount_currency * -1, 'name': reversal_ml_name}, - context=context, check=True, update_check=True) - self.validate(cr, uid, [reversal_move_id], context=context) - return reversal_move_id + reversal_move.validate() + return reversal_move - def create_reversals(self, cr, uid, ids, reversal_date, - reversal_period_id=False, reversal_journal_id=False, - move_prefix=False, move_line_prefix=False, - context=None): + def create_reversals(self, reversal_date, reversal_period_id=False, + reversal_journal_id=False, + move_prefix=False, move_line_prefix=False): """ Create the reversal of one or multiple moves @@ -128,26 +112,16 @@ class account_move(orm.Model): :return: Returns a list of ids of the created reversal moves """ - if isinstance(ids, (int, long)): - ids = [ids] - reversed_move_ids = [] - for src_move in self.browse(cr, uid, ids, context=context): - if src_move.reversal_id: - continue # skip the reversal creation if already done + if self.reversal_id: + return - reversal_move_id = self._move_reversal( - cr, uid, - src_move, - reversal_date, - reversal_period_id=reversal_period_id, - reversal_journal_id=reversal_journal_id, - move_prefix=move_prefix, - move_line_prefix=move_line_prefix, - context=context - ) + reversal_move_id = self._move_reversal( + reversal_date, + reversal_period_id=reversal_period_id, + reversal_journal_id=reversal_journal_id, + move_prefix=move_prefix, + move_line_prefix=move_line_prefix + ) - if reversal_move_id: - reversed_move_ids.append(reversal_move_id) - - return reversed_move_ids + return reversal_move_id