From e7c5370e6bd05aa21a404ca54ff17d20dddb0a2c Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Fri, 5 Oct 2018 17:31:52 +0200 Subject: [PATCH] Improvements according to review --- account_asset_management/__manifest__.py | 6 +- .../models/account_asset.py | 125 +++++++++-------- .../models/account_asset_line.py | 48 ++++--- .../models/account_asset_profile.py | 18 +-- .../models/account_invoice.py | 5 +- .../models/account_move.py | 129 +++++++++--------- .../wizard/account_asset_compute.py | 2 +- .../wizard/account_asset_remove.py | 28 ++-- 8 files changed, 188 insertions(+), 173 deletions(-) diff --git a/account_asset_management/__manifest__.py b/account_asset_management/__manifest__.py index fe4be7127..a59c4cac1 100644 --- a/account_asset_management/__manifest__.py +++ b/account_asset_management/__manifest__.py @@ -10,9 +10,8 @@ ], 'conflicts': ['account_asset'], 'author': "Noviat,Odoo Community Association (OCA)", - 'website': 'http://www.noviat.com', + 'website': 'https://github.com/OCA/account-financial-tools', 'category': 'Accounting & Finance', - 'sequence': 32, 'data': [ 'security/account_asset_security.xml', 'security/ir.model.access.csv', @@ -28,7 +27,4 @@ 'views/account_move_line.xml', 'views/menuitem.xml', ], - 'auto_install': False, - 'installable': True, - 'application': True, } diff --git a/account_asset_management/models/account_asset.py b/account_asset_management/models/account_asset.py index f5d31990d..fefb0816d 100644 --- a/account_asset_management/models/account_asset.py +++ b/account_asset_management/models/account_asset.py @@ -83,7 +83,9 @@ class AccountAsset(models.Model): string='Parent Asset', readonly=True, states={'draft': [('readonly', False)]}, domain=[('type', '=', 'view')], - ondelete='restrict') + ondelete='restrict', + index=True, + ) parent_left = fields.Integer(index=True) parent_right = fields.Integer(index=True) child_ids = fields.One2many( @@ -234,12 +236,15 @@ class AccountAsset(models.Model): lambda l: l.type in ('depreciate', 'remove') and (l.init_entry or l.move_check)) value_depreciated = sum([l.amount for l in lines]) - asset.value_residual = \ - asset.depreciation_base - value_depreciated - asset.value_depreciated = value_depreciated + residual = asset.depreciation_base - value_depreciated + depreciated = value_depreciated else: - asset.value_residual = 0.0 - asset.value_depreciated = 0.0 + residual = 0.0 + depreciated = 0.0 + asset.update({ + 'value_residual': residual, + 'value_depreciated': depreciated + }) @api.multi @api.constrains('parent_id') @@ -279,9 +284,10 @@ class AccountAsset(models.Model): dl_create_line = self.depreciation_line_ids.filtered( lambda r: r.type == 'create') if dl_create_line: - dl_create_line.write({ + dl_create_line.update({ 'amount': self.depreciation_base, - 'line_date': self.date_start}) + 'line_date': self.date_start + }) @api.onchange('profile_id') def _onchange_profile_id(self): @@ -290,16 +296,18 @@ class AccountAsset(models.Model): raise UserError( _("You cannot change the profile of an asset " "with accounting entries.")) - if self.profile_id: - profile = self.profile_id - self.parent_id = profile.parent_id - self.method = profile.method - self.method_number = profile.method_number - self.method_time = profile.method_time - self.method_period = profile.method_period - self.method_progress_factor = profile.method_progress_factor - self.prorata = profile.prorata - self.account_analytic_id = profile.account_analytic_id + profile = self.profile_id + if profile: + self.update({ + 'parent_id': profile.parent_id, + 'method': profile.method, + 'method_number': profile.method_number, + 'method_time': profile.method_time, + 'method_period': profile.method_period, + 'method_progress_factor': profile.method_progress_factor, + 'prorata': profile.prorata, + 'account_analytic_id': profile.account_analytic_id, + }) @api.onchange('method_time') def _onchange_method_time(self): @@ -309,10 +317,12 @@ class AccountAsset(models.Model): @api.onchange('type') def _onchange_type(self): if self.type == 'view': - self.date_start = False - self.profile_id = False - self.purchase_value = False - self.salvage_value = False + self.update({ + 'date_start': False, + 'profile_id': False, + 'purchase_value': False, + 'salvage_value': False, + }) if self.depreciation_line_ids: self.depreciation_line_ids.unlink() @@ -323,7 +333,7 @@ class AccountAsset(models.Model): if vals.get('type') == 'view': vals['date_start'] = False asset = super(AccountAsset, self).create(vals) - if self._context.get('create_asset_from_move_line'): + if self.env.context.get('create_asset_from_move_line'): # Trigger compute of depreciation_base asset.salvage_value = 0.0 if asset.type == 'normal': @@ -335,20 +345,19 @@ class AccountAsset(models.Model): if vals.get('method_time'): if vals['method_time'] != 'year' and not vals.get('prorata'): vals['prorata'] = True - super(AccountAsset, self).write(vals) + res = super(AccountAsset, self).write(vals) for asset in self: asset_type = vals.get('type') or asset.type if asset_type == 'view' or \ - self._context.get('asset_validate_from_write'): + self.env.context.get('asset_validate_from_write'): continue asset._create_first_asset_line() if asset.profile_id.open_asset and \ - self._context.get('create_asset_from_move_line'): + self.env.context.get('create_asset_from_move_line'): asset.compute_depreciation_board() # extra context to avoid recursion - ctx = dict(self._context, asset_validate_from_write=True) - asset.with_context(ctx).validate() - return True + asset.with_context(asset_validate_from_write=True).validate() + return res def _create_first_asset_line(self): self.ensure_one() @@ -364,8 +373,8 @@ class AccountAsset(models.Model): 'type': 'create', } asset_line = asset_line_obj.create(asset_line_vals) - if self._context.get('create_asset_from_move_line'): - asset_line.move_id = self._context['move_id'] + if self.env.context.get('create_asset_from_move_line'): + asset_line.move_id = self.env.context['move_id'] @api.multi def unlink(self): @@ -379,9 +388,9 @@ class AccountAsset(models.Model): _("You cannot delete an asset that contains " "posted depreciation lines.")) # update accounting entries linked to lines of type 'create' - ctx = dict(self._context, allow_asset_removal=True, - from_parent_object=True) - amls = self.with_context(ctx).mapped('account_move_line_ids') + amls = self.with_context( + allow_asset_removal=True, from_parent_object=True + ).mapped('account_move_line_ids') amls.write({'asset_id': False}) return super(AccountAsset, self).unlink() @@ -422,7 +431,7 @@ class AccountAsset(models.Model): @api.multi def remove(self): self.ensure_one() - ctx = dict(self._context, active_ids=self.ids, active_id=self.id) + ctx = dict(self.env.context, active_ids=self.ids, active_id=self.id) early_removal = False if self.method in ['linear-limit', 'degr-limit']: @@ -441,7 +450,6 @@ class AccountAsset(models.Model): 'target': 'new', 'type': 'ir.actions.act_window', 'context': ctx, - 'nodestroy': True, } @api.multi @@ -461,13 +469,17 @@ class AccountAsset(models.Model): 'res_model': 'account.move', 'view_id': False, 'type': 'ir.actions.act_window', - 'context': self._context, - 'nodestroy': True, + 'context': self.env.context, 'domain': [('id', 'in', am_ids)], } @api.multi def compute_depreciation_board(self): + + def group_lines(x, y): + y.update({'amount': x['amount'] + y['amount']}) + return y + line_obj = self.env['account.asset.line'] digits = self.env['decimal.precision'].precision_get('Account') @@ -498,8 +510,8 @@ class AccountAsset(models.Model): continue # group lines prior to depreciation start period - depreciation_start_date = datetime.strptime( - asset.date_start, '%Y-%m-%d') + depreciation_start_date = fields.Datetime.from_string( + asset.date_start) lines = table[0]['lines'] lines1 = [] lines2 = [] @@ -512,9 +524,6 @@ class AccountAsset(models.Model): else: lines2.append(line) if lines1: - def group_lines(x, y): - y.update({'amount': x['amount'] + y['amount']}) - return y lines1 = [reduce(group_lines, lines1)] lines1[0]['depreciated_value'] = 0.0 table[0]['lines'] = lines1 + lines2 @@ -523,8 +532,8 @@ class AccountAsset(models.Model): # recompute in case of deviation depreciated_value_posted = depreciated_value = 0.0 if posted_lines: - last_depreciation_date = datetime.strptime( - last_line.line_date, '%Y-%m-%d') + last_depreciation_date = fields.Datetime.from_string( + last_line.line_date) last_date_in_table = table[-1]['lines'][-1]['date'] if last_date_in_table <= last_depreciation_date: raise UserError( @@ -616,8 +625,8 @@ class AccountAsset(models.Model): - years: duration in calendar years, considering also leap years """ fy = self.env['date.range'].browse(fy_id) - fy_date_start = datetime.strptime(fy.date_start, '%Y-%m-%d') - fy_date_stop = datetime.strptime(fy.date_end, '%Y-%m-%d') + fy_date_start = fields.Datetime.from_string(fy.date_start) + fy_date_stop = fields.Datetime.from_string(fy.date_end) days = (fy_date_stop - fy_date_start).days + 1 months = (fy_date_stop.year - fy_date_start.year) * 12 \ + (fy_date_stop.month - fy_date_start.month) + 1 @@ -655,8 +664,8 @@ class AccountAsset(models.Model): fy_id = entry['fy_id'] if self.prorata: if firstyear: - depreciation_date_start = datetime.strptime( - self.date_start, '%Y-%m-%d') + depreciation_date_start = fields.Date.from_string( + self.date_start) fy_date_stop = entry['date_stop'] first_fy_asset_days = \ (fy_date_stop - depreciation_date_start).days + 1 @@ -689,10 +698,10 @@ class AccountAsset(models.Model): if the fiscal year starts in the middle of a month. """ if self.prorata: - depreciation_start_date = datetime.strptime( - self.date_start, '%Y-%m-%d') + depreciation_start_date = fields.Datetime.from_string( + self.date_start) else: - fy_date_start = datetime.strptime(fy.date_start, '%Y-%m-%d') + fy_date_start = fields.Datetime.from_string(fy.date_start) depreciation_start_date = datetime( fy_date_start.year, fy_date_start.month, 1) return depreciation_start_date @@ -717,8 +726,8 @@ class AccountAsset(models.Model): depreciation_stop_date = depreciation_start_date + \ relativedelta(years=self.method_number, days=-1) elif self.method_time == 'end': - depreciation_stop_date = datetime.strptime( - self.method_end, '%Y-%m-%d') + depreciation_stop_date = fields.Date.from_string( + self.method_end) return depreciation_stop_date def _get_first_period_amount(self, table, entry, depreciation_start_date, @@ -812,7 +821,7 @@ class AccountAsset(models.Model): depreciation_stop_date, line_dates): digits = self.env['decimal.precision'].precision_get('Account') - asset_sign = self.depreciation_base >= 0 and 1 or -1 + asset_sign = 1 if self.depreciation_base >= 0 else -1 i_max = len(table) - 1 remaining_value = self.depreciation_base depreciated_value = 0.0 @@ -1041,7 +1050,7 @@ class AccountAsset(models.Model): @api.multi def _compute_entries(self, date_end, check_triggers=False): - # To DO : add ir_cron job calling this method to + # TODO : add ir_cron job calling this method to # generate periodical accounting entries result = [] error_log = '' @@ -1063,9 +1072,9 @@ class AccountAsset(models.Model): order='line_date') for depreciation in depreciations: try: - with self._cr.savepoint(): + with self.env.cr.savepoint(): result += depreciation.create_move() - except: + except Exception: e = exc_info()[0] tb = ''.join(format_exception(*exc_info())) asset_ref = depreciation.asset_id.code and '%s (ref: %s)' \ diff --git a/account_asset_management/models/account_asset_line.py b/account_asset_management/models/account_asset_line.py index 10e062974..0512cfb9d 100644 --- a/account_asset_management/models/account_asset_line.py +++ b/account_asset_management/models/account_asset_line.py @@ -28,10 +28,14 @@ class AccountAssetLine(models.Model): ('close', 'Close'), ('removed', 'Removed')], related='asset_id.state', - string='State of Asset') + string='State of Asset', + readonly=True, + ) depreciation_base = fields.Float( related='asset_id.depreciation_base', - string='Depreciation Base') + string='Depreciation Base', + readonly=True, + ) amount = fields.Float( string='Amount', digits=dp.get_precision('Account'), required=True) @@ -68,9 +72,9 @@ class AccountAssetLine(models.Model): @api.multi def _compute_values(self): dlines = self - if self._context.get('no_compute_asset_line_ids'): + if self.env.context.get('no_compute_asset_line_ids'): # skip compute for lines in unlink - exclude_ids = self._context['no_compute_asset_line_ids'] + exclude_ids = self.env.context['no_compute_asset_line_ids'] dlines = dlines.filtered(lambda l: l.id not in exclude_ids) dlines = self.filtered(lambda l: l.type == 'depreciate') dlines = dlines.sorted(key=lambda l: l.line_date) @@ -106,20 +110,21 @@ class AccountAssetLine(models.Model): for dl in self: if vals.get('line_date'): if isinstance(vals['line_date'], datetime.date): - vals['line_date'] = vals['line_date'].strftime('%Y-%m-%d') + vals['line_date'] = fields.Date.to_string( + vals['line_date']) line_date = vals.get('line_date') or dl.line_date asset_lines = dl.asset_id.depreciation_line_ids if list(vals.keys()) == ['move_id'] and not vals['move_id']: # allow to remove an accounting entry via the # 'Delete Move' button on the depreciation lines. - if not self._context.get('unlink_from_asset'): + if not self.env.context.get('unlink_from_asset'): raise UserError(_( "You are not allowed to remove an accounting entry " "linked to an asset." "\nYou should remove such entries from the asset.")) elif list(vals.keys()) == ['asset_id']: continue - elif dl.move_id and not self._context.get( + elif dl.move_id and not self.env.context.get( 'allow_asset_line_update'): raise UserError(_( "You cannot change a depreciation line " @@ -165,13 +170,12 @@ class AccountAssetLine(models.Model): "You cannot delete a depreciation line with " "an associated accounting entry.")) previous = dl.previous_id - next = dl.asset_id.depreciation_line_ids.filtered( + next_line = dl.asset_id.depreciation_line_ids.filtered( lambda l: l.previous_id == dl and l not in self) - if next: - next.previous_id = previous - ctx = dict(self._context, no_compute_asset_line_ids=self.ids) - return super( - AccountAssetLine, self.with_context(ctx)).unlink() + if next_line: + next_line.previous_id = previous + return super(AccountAssetLine, self.with_context( + no_compute_asset_line_ids=self.ids)).unlink() def _setup_move_data(self, depreciation_date): asset = self.asset_id @@ -212,8 +216,9 @@ class AccountAssetLine(models.Model): @api.multi def create_move(self): created_move_ids = [] - asset_ids = [] - ctx = dict(self._context, allow_asset=True, check_move_validity=False) + asset_ids = set() + ctx = dict(self.env.context, + allow_asset=True, check_move_validity=False) for line in self: asset = line.asset_id depreciation_date = line.line_date @@ -228,13 +233,13 @@ class AccountAssetLine(models.Model): depreciation_date, exp_acc, 'expense', move) self.env['account.move.line'].with_context(ctx).create(aml_e_vals) move.post() - write_ctx = dict(self._context, allow_asset_line_update=True) - line.with_context(write_ctx).write({'move_id': move.id}) + line.with_context(allow_asset_line_update=True).write({ + 'move_id': move.id + }) created_move_ids.append(move.id) - asset_ids.append(asset.id) + asset_ids.add(asset.id) # we re-evaluate the assets to determine if we can close them - for asset in self.env['account.asset'].browse( - list(set(asset_ids))): + for asset in self.env['account.asset'].browse(list(asset_ids)): if asset.company_id.currency_id.is_zero(asset.value_residual): asset.state = 'close' return created_move_ids @@ -249,8 +254,7 @@ class AccountAssetLine(models.Model): 'res_model': 'account.move', 'view_id': False, 'type': 'ir.actions.act_window', - 'context': self._context, - 'nodestroy': True, + 'context': self.env.context, 'domain': [('id', '=', self.move_id.id)], } diff --git a/account_asset_management/models/account_asset_profile.py b/account_asset_management/models/account_asset_profile.py index e0a0d4e23..705a61438 100644 --- a/account_asset_management/models/account_asset_profile.py +++ b/account_asset_management/models/account_asset_profile.py @@ -10,7 +10,7 @@ class AccountAssetProfile(models.Model): _description = 'Asset profile' _order = 'name' - name = fields.Char(string='Name', size=64, required=True, index=1) + name = fields.Char(string='Name', size=64, required=True, index=True) note = fields.Text() account_analytic_id = fields.Many2one( comodel_name='account.analytic.account', @@ -170,11 +170,11 @@ class AccountAssetProfile(models.Model): if vals.get('method_time'): if vals['method_time'] != 'year' and not vals.get('prorata'): vals['prorata'] = True - super(AccountAssetProfile, self).write(vals) - for profile in self: - acc_id = vals.get('account_asset_id') - if acc_id: - account = self.env['account.account'].browse(acc_id) - if not account.asset_profile_id: - account.write({'asset_profile_id': profile.id}) - return True + res = super(AccountAssetProfile, self).write(vals) + # TODO last profile in self is defined as default on the related + # account. must be improved. + account = self.env['account.account'].browse( + vals.get('account_asset_id')) + if self and account and not account.asset_profile_id: + account.write({'asset_profile_id': self[-1].id}) + return res diff --git a/account_asset_management/models/account_invoice.py b/account_asset_management/models/account_invoice.py index 666811072..da71d1bc3 100644 --- a/account_asset_management/models/account_invoice.py +++ b/account_asset_management/models/account_invoice.py @@ -11,8 +11,7 @@ class AccountInvoice(models.Model): @api.multi def finalize_invoice_move_lines(self, move_lines): - move_lines = super(AccountInvoice, self) \ - .finalize_invoice_move_lines(move_lines) + move_lines = super().finalize_invoice_move_lines(move_lines) new_lines = [] for line_tuple in move_lines: line = line_tuple[2] @@ -73,7 +72,7 @@ class AccountInvoice(models.Model): @api.multi def action_move_create(self): - res = super(AccountInvoice, self).action_move_create() + res = super().action_move_create() for inv in self: move = inv.move_id assets = [aml.asset_id for aml in diff --git a/account_asset_management/models/account_move.py b/account_asset_management/models/account_move.py index 75cca26fa..99d2522e2 100644 --- a/account_asset_management/models/account_move.py +++ b/account_asset_management/models/account_move.py @@ -22,30 +22,29 @@ class AccountMove(models.Model): _inherit = 'account.move' @api.multi - def unlink(self, **kwargs): - for move in self: - deprs = self.env['account.asset.line'].search( - [('move_id', '=', move.id), - ('type', 'in', ['depreciate', 'remove'])]) - if deprs and not self._context.get('unlink_from_asset'): - raise UserError( - _("You are not allowed to remove an accounting entry " - "linked to an asset." - "\nYou should remove such entries from the asset.")) - # trigger store function - deprs.write({'move_id': False}) - return super(AccountMove, self).unlink(**kwargs) + def unlink(self): + # for move in self: + deprs = self.env['account.asset.line'].search( + [('move_id', 'in', self.ids), + ('type', 'in', ['depreciate', 'remove'])]) + if deprs and not self.env.context.get('unlink_from_asset'): + raise UserError( + _("You are not allowed to remove an accounting entry " + "linked to an asset." + "\nYou should remove such entries from the asset.")) + # trigger store function + deprs.write({'move_id': False}) + return super(AccountMove, self).unlink() @api.multi def write(self, vals): if set(vals).intersection(FIELDS_AFFECTS_ASSET_MOVE): - for move in self: - deprs = self.env['account.asset.line'].search( - [('move_id', '=', move.id), ('type', '=', 'depreciate')]) - if deprs: - raise UserError( - _("You cannot change an accounting entry " - "linked to an asset depreciation line.")) + deprs = self.env['account.asset.line'].search( + [('move_id', 'in', self.ids), ('type', '=', 'depreciate')]) + if deprs: + raise UserError( + _("You cannot change an accounting entry " + "linked to an asset depreciation line.")) return super(AccountMove, self).write(vals) @@ -64,8 +63,8 @@ class AccountMoveLine(models.Model): self.asset_profile_id = self.account_id.asset_profile_id @api.model - def create(self, vals, **kwargs): - if vals.get('asset_id') and not self._context.get('allow_asset'): + def create(self, vals): + if vals.get('asset_id') and not self.env.context.get('allow_asset'): raise UserError( _("You are not allowed to link " "an accounting entry to an asset." @@ -82,70 +81,78 @@ class AccountMoveLine(models.Model): 'partner_id': vals['partner_id'], 'date_start': move.date, } - if self._context.get('company_id'): - temp_vals['company_id'] = self._context['company_id'] + if self.env.context.get('company_id'): + temp_vals['company_id'] = self.env.context['company_id'] temp_asset = asset_obj.new(temp_vals) temp_asset._onchange_profile_id() asset_vals = temp_asset._convert_to_write(temp_asset._cache) self._get_asset_analytic_values(vals, asset_vals) - ctx = dict(self._context, create_asset_from_move_line=True, - move_id=vals['move_id']) asset = asset_obj.with_context( - ctx).create(asset_vals) + create_asset_from_move_line=True, + move_id=vals['move_id']).create(asset_vals) vals['asset_id'] = asset.id - return super(AccountMoveLine, self).create(vals, **kwargs) + return super(AccountMoveLine, self).create(vals) @api.multi - def write(self, vals, **kwargs): - for aml in self: - if aml.asset_id: - if set(vals).intersection(FIELDS_AFFECTS_ASSET_MOVE_LINE): - if not (self.env.context.get('allow_asset_removal') and - list(vals.keys()) == ['asset_id']): - raise UserError( - _("You cannot change an accounting item " - "linked to an asset depreciation line.")) + def _prepare_asset_create(self, vals): + self.ensure_one() + debit = 'debit' in vals and vals.get('debit', 0.0) or self.debit + credit = 'credit' in vals and \ + vals.get('credit', 0.0) or self.credit + depreciation_base = debit - credit + partner_id = 'partner' in vals and \ + vals.get('partner', False) or self.partner_id.id + date_start = 'date' in vals and \ + vals.get('date', False) or self.date + return { + 'name': vals.get('name') or self.name, + 'profile_id': vals['asset_profile_id'], + 'purchase_value': depreciation_base, + 'partner_id': partner_id, + 'date_start': date_start, + 'company_id': vals.get('company_id') or self.company_id.id, + } + + @api.multi + def write(self, vals): + if ( + self.mapped('asset_id') and + set(vals).intersection(FIELDS_AFFECTS_ASSET_MOVE_LINE) and + not ( + self.env.context.get('allow_asset_removal') and + list(vals.keys()) == ['asset_id']) + ): + raise UserError( + _("You cannot change an accounting item " + "linked to an asset depreciation line.")) if vals.get('asset_id'): raise UserError( _("You are not allowed to link " "an accounting entry to an asset." "\nYou should generate such entries from the asset.")) if vals.get('asset_profile_id'): - assert len(self.ids) == 1, \ - 'This option should only be used for a single id at a time.' + if len(self) == 1: + raise AssertionError(_( + 'This option should only be used for a single id at a ' + 'time.')) asset_obj = self.env['account.asset'] for aml in self: if vals['asset_profile_id'] == aml.asset_profile_id.id: continue # create asset - debit = 'debit' in vals and vals.get('debit', 0.0) or aml.debit - credit = 'credit' in vals and \ - vals.get('credit', 0.0) or aml.credit - depreciation_base = debit - credit - partner_id = 'partner' in vals and \ - vals.get('partner', False) or aml.partner_id.id - date_start = 'date' in vals and \ - vals.get('date', False) or aml.date - asset_vals = { - 'name': vals.get('name') or aml.name, - 'profile_id': vals['asset_profile_id'], - 'purchase_value': depreciation_base, - 'partner_id': partner_id, - 'date_start': date_start, - 'company_id': vals.get('company_id') or aml.company_id.id, - } + asset_vals = aml._prepare_asset_create(vals) self._play_onchange_profile_id(asset_vals) self._get_asset_analytic_values(vals, asset_vals) - ctx = dict(self._context, create_asset_from_move_line=True, - move_id=aml.move_id.id) - asset = asset_obj.with_context(ctx).create(asset_vals) + asset = asset_obj.with_context( + create_asset_from_move_line=True, + move_id=aml.move_id.id).create(asset_vals) vals['asset_id'] = asset.id - return super(AccountMoveLine, self).write(vals, **kwargs) + return super(AccountMoveLine, self).write(vals) @api.model def _get_asset_analytic_values(self, vals, asset_vals): - asset_vals['account_analytic_id'] = \ - vals.get('analytic_account_id', False) + asset_vals['account_analytic_id'] = vals.get( + 'analytic_account_id', False) @api.model def _play_onchange_profile_id(self, vals): diff --git a/account_asset_management/wizard/account_asset_compute.py b/account_asset_management/wizard/account_asset_compute.py index 9d766a928..2d9b090ef 100644 --- a/account_asset_management/wizard/account_asset_compute.py +++ b/account_asset_management/wizard/account_asset_compute.py @@ -53,7 +53,7 @@ class AccountAssetCompute(models.TransientModel): @api.multi def view_asset_moves(self): self.ensure_one() - domain = [('id', 'in', self._context.get('asset_move_ids', []))] + domain = [('id', 'in', self.env.context.get('asset_move_ids', []))] return { 'name': _('Created Asset Moves'), 'view_type': 'form', diff --git a/account_asset_management/wizard/account_asset_remove.py b/account_asset_management/wizard/account_asset_remove.py index 38b504f18..fde524c80 100644 --- a/account_asset_management/wizard/account_asset_remove.py +++ b/account_asset_management/wizard/account_asset_remove.py @@ -6,7 +6,7 @@ from datetime import datetime import logging from odoo import api, fields, models, _ -from odoo.exceptions import UserError +from odoo.exceptions import UserError, ValidationError _logger = logging.getLogger(__name__) @@ -15,10 +15,6 @@ class AccountAssetRemove(models.TransientModel): _name = 'account.asset.remove' _description = 'Remove Asset' - _sql_constraints = [( - 'sale_value', 'CHECK (sale_value>=0)', - 'The Sale Value must be positive!')] - date_remove = fields.Date( string='Asset Removal Date', required=True, default=fields.Date.today, @@ -61,6 +57,11 @@ class AccountAssetRemove(models.TransientModel): "the 'Plus-Value Account' or 'Min-Value Account' ") note = fields.Text('Notes') + @api.constrains('sale_value') + def _check_sale_value(self): + if self.sale_value < 0: + raise ValidationError(_('The Sale Value must be positive!')) + @api.model def _default_sale_value(self): return self._get_sale()['sale_value'] @@ -70,7 +71,7 @@ class AccountAssetRemove(models.TransientModel): return self._get_sale()['account_sale_id'] def _get_sale(self): - asset_id = self._context.get('active_id') + asset_id = self.env.context.get('active_id') sale_value = 0.0 account_sale_id = False inv_lines = self.env['account.invoice.line'].search( @@ -89,19 +90,19 @@ class AccountAssetRemove(models.TransientModel): @api.model def _default_account_plus_value_id(self): - asset_id = self._context.get('active_id') + asset_id = self.env.context.get('active_id') asset = self.env['account.asset'].browse(asset_id) return asset.profile_id.account_plus_value_id @api.model def _default_account_min_value_id(self): - asset_id = self._context.get('active_id') + asset_id = self.env.context.get('active_id') asset = self.env['account.asset'].browse(asset_id) return asset.profile_id.account_min_value_id @api.model def _default_account_residual_value_id(self): - asset_id = self._context.get('active_id') + asset_id = self.env.context.get('active_id') asset = self.env['account.asset'].browse(asset_id) return asset.profile_id.account_residual_value_id @@ -115,7 +116,7 @@ class AccountAssetRemove(models.TransientModel): @api.model def _get_posting_regime(self): asset_obj = self.env['account.asset'] - asset = asset_obj.browse(self._context.get('active_id')) + asset = asset_obj.browse(self.env.context.get('active_id')) country = asset and asset.company_id.country_id.code or False if country in self._residual_value_regime_countries(): return 'residual_value' @@ -130,12 +131,12 @@ class AccountAssetRemove(models.TransientModel): self.ensure_one() asset_line_obj = self.env['account.asset.line'] - asset_id = self._context.get('active_id') + asset_id = self.env.context.get('active_id') asset = self.env['account.asset'].browse(asset_id) asset_ref = asset.code and '%s (ref: %s)' \ % (asset.name, asset.code) or asset.name - if self._context.get('early_removal'): + if self.env.context.get('early_removal'): residual_value = self._prepare_early_removal(asset) else: residual_value = asset.value_residual @@ -195,8 +196,7 @@ class AccountAssetRemove(models.TransientModel): 'res_model': 'account.move', 'view_id': False, 'type': 'ir.actions.act_window', - 'context': self._context, - 'nodestroy': True, + 'context': self.env.context, 'domain': [('id', '=', move.id)], }