From 11993483495f765b590d49d2c5ec1d776d8e17ad Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Fri, 10 Jul 2015 09:46:32 +0200 Subject: [PATCH] [IMP] use v8 api more often --- .../model/account_payment.py | 39 +++--- .../model/payment_line.py | 87 +++++------- account_direct_debit/models/payment_line.py | 132 ++++++++---------- 3 files changed, 108 insertions(+), 150 deletions(-) diff --git a/account_banking_payment_transfer/model/account_payment.py b/account_banking_payment_transfer/model/account_payment.py index b23221c3e..043211bd6 100644 --- a/account_banking_payment_transfer/model/account_payment.py +++ b/account_banking_payment_transfer/model/account_payment.py @@ -91,7 +91,8 @@ class PaymentOrder(models.Model): .Integer(string='Partial Reconciles Counter', compute='get_partial_reconcile_count') - def action_rejected(self, cr, uid, ids, context=None): + @api.multi + def action_rejected(self): return True @api.multi @@ -133,13 +134,8 @@ class PaymentOrder(models.Model): def test_undo_done(self): return not self.test_done() - @api.model + @api.multi def _prepare_transfer_move(self): - # TODO question : can I use self.mode.xxx in an @api.model ?? - # It works, but I'm not sure we are supposed to do that ! - # I didn't want to use api.one to avoid having to - # do self._prepare_transfer_move()[0] in action_sent - # I prefer to just have to do self._prepare_transfer_move() vals = { 'journal_id': self.mode.transfer_journal_id.id, 'ref': '%s %s' % ( @@ -147,7 +143,7 @@ class PaymentOrder(models.Model): } return vals - @api.model + @api.multi def _prepare_move_line_transfer_account( self, amount, move, payment_lines, labels): if len(payment_lines) == 1: @@ -170,37 +166,35 @@ class PaymentOrder(models.Model): } return vals - @api.model - def _prepare_move_line_partner_account(self, line, move, labels, - payment_order_type): + @api.multi + def _prepare_move_line_partner_account(self, line, move, labels): if line.move_line_id: account_id = line.move_line_id.account_id.id else: - if payment_order_type == 'debit': + if self.payment_order_type == 'debit': account_id = line.partner_id.property_account_receivable.id else: account_id = line.partner_id.property_account_payable.id vals = { 'name': _('%s line %s') % ( - labels[payment_order_type], line.name), + labels[self.payment_order_type], line.name), 'move_id': move.id, 'partner_id': line.partner_id.id, 'account_id': account_id, - 'credit': (payment_order_type == 'debit' and + 'credit': (self.payment_order_type == 'debit' and line.amount or 0.0), - 'debit': (payment_order_type == 'payment' and + 'debit': (self.payment_order_type == 'payment' and line.amount or 0.0), } return vals - @api.model + @api.multi def action_sent_no_move_line_hook(self, pay_line): """This function is designed to be inherited""" return - @api.model - def _create_move_line_partner_account(self, line, move, labels, - order_type): + @api.multi + def _create_move_line_partner_account(self, line, move, labels): """This method is designed to be inherited in a custom module""" # TODO: take multicurrency into account @@ -208,14 +202,14 @@ class PaymentOrder(models.Model): # create the payment/debit counterpart move line # on the partner account partner_ml_vals = self._prepare_move_line_partner_account( - line, move, labels, order_type) + line, move, labels) partner_move_line = aml_obj.create(partner_ml_vals) # register the payment/debit move line # on the payment line and call reconciliation on it line.write({'transit_move_line_id': partner_move_line.id}) - @api.model + @api.multi def _reconcile_payment_lines(self, payment_lines): for line in payment_lines: if line.move_line_id: @@ -263,8 +257,7 @@ class PaymentOrder(models.Model): total_amount = 0 for line in lines: total_amount += line.amount - self._create_move_line_partner_account( - line, move, labels, self.payment_order_type) + self._create_move_line_partner_account(line, move, labels) # create the payment/debit move line on the transfer account trf_ml_vals = self._prepare_move_line_transfer_account( total_amount, move, lines, labels) diff --git a/account_banking_payment_transfer/model/payment_line.py b/account_banking_payment_transfer/model/payment_line.py index bffb6c6d0..514f427da 100644 --- a/account_banking_payment_transfer/model/payment_line.py +++ b/account_banking_payment_transfer/model/payment_line.py @@ -22,13 +22,11 @@ # along with this program. If not, see . # ############################################################################## - -from openerp.osv import orm, fields -from openerp import workflow, api +from openerp import models, fields, workflow, api, exceptions from openerp.tools.translate import _ -class PaymentLine(orm.Model): +class PaymentLine(models.Model): ''' Add some fields; make destination bank account mandatory, as it makes no sense to send payments into thin air. @@ -37,51 +35,37 @@ class PaymentLine(orm.Model): ''' _inherit = 'payment.line' - def _get_transfer_move_line(self, cr, uid, ids, name, arg, context=None): + @api.multi + def _get_transfer_move_line(self): res = {} - for order_line in self.browse(cr, uid, ids, context=context): + for order_line in self: if order_line.transit_move_line_id: order_type = order_line.order_id.payment_order_type trf_lines = order_line.transit_move_line_id.move_id.line_id for move_line in trf_lines: if order_type == 'debit' and move_line.debit > 0: - res[order_line.id] = move_line.id + self.transfer_move_line_id = move_line elif order_type == 'payment' and move_line.credit > 0: - res[order_line.id] = move_line.id - else: - res[order_line.id] = False + self.transfer_move_line_id = move_line return res - _columns = { - 'msg': fields.char('Message', size=255, required=False, readonly=True), - 'date_done': fields.date( - 'Date Confirmed', select=True, readonly=True), - 'transit_move_line_id': fields.many2one( - 'account.move.line', 'Transfer move line', - readonly=True, - help="Move line through which the payment/debit order " - "pays the invoice", - ), - 'transfer_move_line_id': fields.function( - _get_transfer_move_line, - type='many2one', - relation='account.move.line', - string='Transfer move line counterpart', - readonly=True, - help="Counterpart move line on the transfer account", - ), - } - - _defaults = { - 'msg': '', - } + msg = fields.Char('Message', required=False, readonly=True, default='') + date_done = fields.Date('Date Confirmed', select=True, readonly=True) + transit_move_line_id = fields.Many2one( + 'account.move.line', string='Transfer move line', readonly=True, + help="Move line through which the payment/debit order " + "pays the invoice") + transfer_move_line_id = fields.Many2one( + 'account.move.line', compute='_get_transfer_move_line', + string='Transfer move line counterpart', + help="Counterpart move line on the transfer account") """ Hooks for processing direct debit orders, such as implemented in account_direct_debit module. """ - def get_storno_account_id(self, cr, uid, payment_line_id, amount, - currency_id, context=None): + @api.multi + def get_storno_account_id(self, amount, currency_id): """ Hook for verifying a match of the payment line with the amount. Return the account associated with the storno. @@ -95,8 +79,8 @@ class PaymentLine(orm.Model): return False - def debit_storno(self, cr, uid, payment_line_id, amount, - currency_id, storno_retry=True, context=None): + @api.multi + def debit_storno(self, amount, currency_id, storno_retry=True): """ Hook for handling a canceled item of a direct debit order. Presumably called from a bank statement import routine. @@ -116,8 +100,8 @@ class PaymentLine(orm.Model): return False - @api.cr_uid_id_context - def debit_reconcile(self, cr, uid, payment_line_id, context=None): + @api.one + def debit_reconcile(self): """ Reconcile a debit order's payment line with the the move line that it is based on. Called from payment_order.action_sent(). @@ -128,40 +112,35 @@ class PaymentLine(orm.Model): :param payment_line_id: the single id of the canceled payment line """ - if isinstance(payment_line_id, (list, tuple)): - payment_line_id = payment_line_id[0] - move_line_obj = self.pool.get('account.move.line') - payment_line = self.browse(cr, uid, payment_line_id, context=context) - - transit_move_line = payment_line.transit_move_line_id - torec_move_line = payment_line.move_line_id + transit_move_line = self.transit_move_line_id + torec_move_line = self.move_line_id if (not transit_move_line or not torec_move_line): - raise orm.except_orm( + raise exceptions.except_orm( _('Can not reconcile'), - _('No move line for line %s') % payment_line.name + _('No move line for line %s') % self.name ) if torec_move_line.reconcile_id: - raise orm.except_orm( + raise exceptions.except_orm( _('Error'), _('Move line %s has already been reconciled') % torec_move_line.name ) if (transit_move_line.reconcile_id or transit_move_line.reconcile_partial_id): - raise orm.except_orm( + raise exceptions.except_orm( _('Error'), _('Move line %s has already been reconciled') % transit_move_line.name ) line_ids = [transit_move_line.id, torec_move_line.id] - move_line_obj.reconcile_partial(cr, uid, line_ids, type='auto', - context=context) + self.env['account.move.line'].browse(line_ids).reconcile_partial( + type='auto') # If a bank transaction of a storno was first confirmed # and now canceled (the invoice is now in state 'debit_denied' if torec_move_line.invoice: workflow.trg_validate( - uid, 'account.invoice', torec_move_line.invoice.id, - 'undo_debit_denied', cr) + self.env.uid, 'account.invoice', torec_move_line.invoice.id, + 'undo_debit_denied', self.env.cr) diff --git a/account_direct_debit/models/payment_line.py b/account_direct_debit/models/payment_line.py index 655263ab5..0da003002 100644 --- a/account_direct_debit/models/payment_line.py +++ b/account_direct_debit/models/payment_line.py @@ -1,14 +1,12 @@ # -*- coding: utf-8 -*- -from openerp.osv import orm, fields -from openerp import netsvc -from openerp.tools.translate import _ +from openerp import api, exceptions, models, fields, _, workflow -class PaymentLine(orm.Model): +class PaymentLine(models.Model): _inherit = 'payment.line' - def debit_storno(self, cr, uid, payment_line_id, amount, - currency, storno_retry=True, context=None): + @api.multi + def debit_storno(self, amount, currency, storno_retry=True): """The processing of a storno is triggered by a debit transfer on one of the company's bank accounts. This method offers to re-reconcile the original debit @@ -27,12 +25,13 @@ class PaymentLine(orm.Model): :return: an incomplete reconcile for the caller to fill :rtype: database id of an account.move.reconcile resource. """ - reconcile_obj = self.pool.get('account.move.reconcile') - line = self.browse(cr, uid, payment_line_id) - reconcile_id = False + self.ensure_one() + reconcile_obj = self.env['account.move.reconcile'] + line = self + reconcile = reconcile_obj.browse([]) if (line.transit_move_line_id and not line.storno and - self.pool.get('res.currency').is_zero( - cr, uid, currency, ( + self.env['res.currency'].is_zero( + currency, ( (line.transit_move_line_id.credit or 0.0) - (line.transit_move_line_id.debit or 0.0) + amount))): # Two different cases, full and partial @@ -42,66 +41,60 @@ class PaymentLine(orm.Model): # we should not need to take partial into account on the side of # the transit_move_line. if line.transit_move_line_id.reconcile_partial_id: - reconcile_id = \ - line.transit_move_line_id.reconcile_partial_id.id + reconcile_partial = \ + line.transit_move_line_id.reconcile_partial_id reconcile = line.transit_move_line_id.reconcile_id if len(reconcile.line_partial_ids) == 2: # reuse the simple reconcile for the storno transfer - reconcile_obj.write( - cr, uid, reconcile_id, - {'line_id': [(6, 0, line.transit_move_line_id.id)], - 'line_partial_ids': [(6, 0, [])]}, context=context) + reconcile_partial.write({ + 'line_id': [(6, 0, line.transit_move_line_id.id)], + 'line_partial_ids': [(6, 0, [])] + }) else: # split up the original reconcile in a partial one # and a new one for reconciling the storno transfer - reconcile = { + reconcile_partial.write({ 'line_partial_ids': [(3, line.transit_move_line_id.id)] - } - reconcile_obj.write( - cr, uid, reconcile_id, reconcile, context=context) - reconcile_id = reconcile_obj.create( - cr, uid, - {'type': 'auto', - 'line_id': [(6, 0, line.transit_move_line_id.id)]}, - context=context) + }) + reconcile = reconcile_obj.create({ + 'type': 'auto', + 'line_id': [(6, 0, line.transit_move_line_id.id)] + }) elif line.transit_move_line_id.reconcile_id: - reconcile_id = line.transit_move_line_id.reconcile_id.id + reconcile = line.transit_move_line_id.reconcile_id if len(line.transit_move_line_id.reconcile_id.line_id) == 2: # reuse the simple reconcile for the storno transfer - reconcile_obj.write( - cr, uid, reconcile_id, - {'line_id': [(6, 0, [line.transit_move_line_id.id])]}, - context=context) + reconcile.write({ + 'line_id': [(6, 0, [line.transit_move_line_id.id])], + }) else: # split up the original reconcile in a partial one # and a new one for reconciling the storno transfer reconcile = line.transit_move_line_id.reconcile_id partial_ids = [x.id for x in reconcile.line_id if x.id != line.transit_move_line_id.id] - reconcile_obj.write( - cr, uid, reconcile_id, - {'line_partial_ids': [(6, 0, partial_ids)], - 'line_id': [(6, 0, [])]}, context=context) - reconcile_id = reconcile_obj.create( - cr, uid, - {'type': 'auto', - 'line_id': [(6, 0, line.transit_move_line_id.id)]}, - context=context) + reconcile.write({ + 'line_partial_ids': [(6, 0, partial_ids)], + 'line_id': [(6, 0, [])], + }) + reconcile = reconcile_obj.create({ + 'type': 'auto', + 'line_id': [(6, 0, line.transit_move_line_id.id)] + }) # mark the payment line for storno processed - if reconcile_id: - self.write(cr, uid, [payment_line_id], - {'storno': True}, context=context) + if reconcile: + self.write({'storno': True}) # put forth the invoice workflow if line.move_line_id.invoice: activity = (storno_retry and 'open_test' or 'invoice_debit_denied') - netsvc.LocalService("workflow").trg_validate( - uid, 'account.invoice', line.move_line_id.invoice.id, - activity, cr) - return reconcile_id + workflow.trg_validate( + self.env.uid, 'account.invoice', + line.move_line_id.invoice.id, activity, self.env.cr) + return reconcile.id - def get_storno_account_id( - self, cr, uid, payment_line_id, amount, currency, context=None): + @api.multi + def get_storno_account_id(self, amount, currency): """Check the match of the arguments, and return the account associated with the storno. Used in account_banking interactive mode @@ -112,34 +105,27 @@ class PaymentLine(orm.Model): :return: an account if there is a full match, False otherwise :rtype: database id of an account.account resource. """ - line = self.browse(cr, uid, payment_line_id) + self.ensure_one() account_id = False - if (line.transit_move_line_id and not line.storno and - self.pool.get('res.currency').is_zero( - cr, uid, currency, ( - (line.transit_move_line_id.credit or 0.0) - - (line.transit_move_line_id.debit or 0.0) + amount))): - account_id = line.transit_move_line_id.account_id.id + if (self.transit_move_line_id and not self.storno and + self.env['res.currency'].is_zero( + currency, ( + (self.transit_move_line_id.credit or 0.0) - + (self.transit_move_line_id.debit or 0.0) + amount))): + account_id = self.transit_move_line_id.account_id.id return account_id - def debit_reconcile(self, cr, uid, payment_line_id, context=None): + @api.one + def debit_reconcile(self): """Raise if a payment line is passed for which storno is True.""" - if isinstance(payment_line_id, (list, tuple)): - payment_line_id = payment_line_id[0] - payment_line_vals = self.read( - cr, uid, payment_line_id, ['storno', 'name'], context=context) - if payment_line_vals['storno']: - raise orm.except_orm( + if self.storno: + raise exceptions.except_orm( _('Can not reconcile'), _('Cancelation of payment line \'%s\' has already been ' - 'processed') % payment_line_vals['name']) - return super(PaymentLine, self).debit_reconcile( - cr, uid, payment_line_id, context=context) + 'processed') % self.name) + return super(PaymentLine, self).debit_reconcile() - _columns = { - 'storno': fields.boolean( - 'Storno', - readonly=True, - help=("If this is true, the debit order has been canceled " - "by the bank or by the customer")), - } + storno = fields.Boolean( + 'Storno', readonly=True, + help="If this is true, the debit order has been canceled by the bank " + "or by the customer")