Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REM] *: remove calls to api.one and adapt code #29511

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions addons/account/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,25 +667,25 @@ def _inverse_refund_seq_number_next(self):
sequence = journal.refund_sequence_id._get_current_sequence()
sequence.number_next = journal.refund_sequence_number_next

@api.one
@api.constrains('currency_id', 'default_credit_account_id', 'default_debit_account_id')
def _check_currency(self):
if self.currency_id:
if self.default_credit_account_id and not self.default_credit_account_id.currency_id.id == self.currency_id.id:
raise ValidationError(_('The currency of the journal should be the same than the default credit account.'))
if self.default_debit_account_id and not self.default_debit_account_id.currency_id.id == self.currency_id.id:
raise ValidationError(_('The currency of the journal should be the same than the default debit account.'))
for journal in self:
if journal.currency_id:
if journal.default_credit_account_id and not journal.default_credit_account_id.currency_id.id == journal.currency_id.id:
raise ValidationError(_('The currency of the journal should be the same than the default credit account.'))
if journal.default_debit_account_id and not journal.default_debit_account_id.currency_id.id == journal.currency_id.id:
raise ValidationError(_('The currency of the journal should be the same than the default debit account.'))

@api.one
@api.constrains('type', 'bank_account_id')
def _check_bank_account(self):
if self.type == 'bank' and self.bank_account_id:
if self.bank_account_id.company_id != self.company_id:
raise ValidationError(_('The bank account of a bank journal must belong to the same company (%s).') % self.company_id.name)
# A bank account can belong to a customer/supplier, in which case their partner_id is the customer/supplier.
# Or they are part of a bank journal and their partner_id must be the company's partner_id.
if self.bank_account_id.partner_id != self.company_id.partner_id:
raise ValidationError(_('The holder of a journal\'s bank account must be the company (%s).') % self.company_id.name)
for journal in self:
if journal.type == 'bank' and journal.bank_account_id:
if journal.bank_account_id.company_id != journal.company_id:
raise ValidationError(_('The bank account of a bank journal must belong to the same company (%s).') % journal.company_id.name)
# A bank account can belong to a customer/supplier, in which case their partner_id is the customer/supplier.
# Or they are part of a bank journal and their partner_id must be the company's partner_id.
if journal.bank_account_id.partner_id != journal.company_id.partner_id:
raise ValidationError(_('The holder of a journal\'s bank account must be the company (%s).') % journal.company_id.name)

@api.onchange('default_debit_account_id')
def onchange_debit_account_id(self):
Expand Down Expand Up @@ -980,11 +980,11 @@ class ResPartnerBank(models.Model):
journal_id = fields.One2many('account.journal', 'bank_account_id', domain=[('type', '=', 'bank')], string='Account Journal', readonly=True,
help="The accounting journal corresponding to this bank account.")

@api.one
@api.constrains('journal_id')
def _check_journal_id(self):
if len(self.journal_id) > 1:
raise ValidationError(_('A bank account can belong to only one journal.'))
for bank in self:
if len(bank.journal_id) > 1:
raise ValidationError(_('A bank account can belong to only one journal.'))


#----------------------------------------------------------
Expand Down Expand Up @@ -1111,13 +1111,13 @@ def _validate_repartition_lines(self):
raise ValidationError(_("Invoice and credit note repartitions should match (same percentages, in the same order)."))
index += 1

@api.one
@api.constrains('children_tax_ids', 'type_tax_use')
def _check_children_scope(self):
if not self._check_m2m_recursion('children_tax_ids'):
raise ValidationError(_("Recursion found for tax '%s'.") % (self.name,))
if not all(child.type_tax_use in ('none', self.type_tax_use) for child in self.children_tax_ids):
raise ValidationError(_('The application scope of taxes in a group must be either the same as the group or left empty.'))
for tax in self:
if not tax._check_m2m_recursion('children_tax_ids'):
raise ValidationError(_("Recursion found for tax '%s'.") % (tax.name,))
if not all(child.type_tax_use in ('none', tax.type_tax_use) for child in tax.children_tax_ids):
raise ValidationError(_('The application scope of taxes in a group must be either the same as the group or left empty.'))

@api.multi
@api.returns('self', lambda value: value.id)
Expand Down
41 changes: 22 additions & 19 deletions addons/account/models/account_bank_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class AccountCashboxLine(models.Model):
_rec_name = 'coin_value'
_order = 'coin_value'

@api.one
@api.depends('coin_value', 'number')
def _sub_total(self):
""" Calculates Sub total"""
self.subtotal = self.coin_value * self.number
for line in self:
line.subtotal = line.coin_value * line.number

coin_value = fields.Float(string='Coin/Bill Value', required=True, digits=0)
number = fields.Integer(string='Number of Coins/Bills', help='Opening Unit Numbers')
Expand Down Expand Up @@ -71,27 +71,30 @@ def validate(self):

class AccountBankStatement(models.Model):

@api.one
@api.depends('line_ids', 'balance_start', 'line_ids.amount', 'balance_end_real')
def _end_balance(self):
self.total_entry_encoding = sum([line.amount for line in self.line_ids])
self.balance_end = self.balance_start + self.total_entry_encoding
self.difference = self.balance_end_real - self.balance_end
for statement in self:
statement.total_entry_encoding = sum([line.amount for line in statement.line_ids])
statement.balance_end = statement.balance_start + statement.total_entry_encoding
statement.difference = statement.balance_end_real - statement.balance_end

@api.multi
def _is_difference_zero(self):
for bank_stmt in self:
bank_stmt.is_difference_zero = float_is_zero(bank_stmt.difference, precision_digits=bank_stmt.currency_id.decimal_places)

@api.one
@api.depends('journal_id')
def _compute_currency(self):
self.currency_id = self.journal_id.currency_id or self.company_id.currency_id
for statement in self:
statement.currency_id = statement.journal_id.currency_id or statement.company_id.currency_id

@api.one
@api.depends('line_ids.journal_entry_ids')
def _check_lines_reconciled(self):
self.all_lines_reconciled = all([line.journal_entry_ids.ids or line.account_id.id for line in self.line_ids if not self.currency_id.is_zero(line.amount)])
for statement in self:
statement.all_lines_reconciled = all([
line.journal_entry_ids.ids or line.account_id.id for line in statement.line_ids
if not statement.currency_id.is_zero(line.amount)
])

@api.depends('move_line_ids')
def _get_move_line_count(self):
Expand Down Expand Up @@ -322,20 +325,20 @@ class AccountBankStatementLine(models.Model):
default=False, copy=False,
help="Technical field holding the number given to the journal entry, automatically set when the statement line is reconciled then stored to set the same number again if the line is cancelled, set to draft and re-processed again.")

@api.one
@api.constrains('amount')
def _check_amount(self):
# Allow to enter bank statement line with an amount of 0,
# so that user can enter/import the exact bank statement they have received from their bank in Odoo
currency = self.currency_id or self.journal_currency_id
if self.journal_id.type != 'bank' and currency.is_zero(self.amount):
raise ValidationError(_('The amount of a cash transaction cannot be 0.'))
for line in self:
# Allow to enter bank statement line with an amount of 0,
# so that user can enter/import the exact bank statement they have received from their bank in Odoo
currency = line.currency_id or line.journal_currency_id
if line.journal_id.type != 'bank' and currency.is_zero(line.amount):
raise ValidationError(_('The amount of a cash transaction cannot be 0.'))

@api.one
@api.constrains('amount', 'amount_currency')
def _check_amount_currency(self):
if self.amount_currency != 0 and self.amount == 0:
raise ValidationError(_('If "Amount Currency" is specified, then "Amount" must be as well.'))
for line in self:
if line.amount_currency != 0 and line.amount == 0:
raise ValidationError(_('If "Amount Currency" is specified, then "Amount" must be as well.'))

@api.constrains('currency_id', 'journal_id')
def _check_currency_id(self):
Expand Down
14 changes: 7 additions & 7 deletions addons/account/models/account_journal_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
class account_journal(models.Model):
_inherit = "account.journal"

@api.one
def _kanban_dashboard(self):
self.kanban_dashboard = json.dumps(self.get_journal_dashboard_datas())
for journal in self:
journal.kanban_dashboard = json.dumps(journal.get_journal_dashboard_datas())

@api.one
def _kanban_dashboard_graph(self):
if (self.type in ['sale', 'purchase']):
self.kanban_dashboard_graph = json.dumps(self.get_bar_graph_datas())
elif (self.type in ['cash', 'bank']):
self.kanban_dashboard_graph = json.dumps(self.get_line_graph_datas())
for journal in self:
if (journal.type in ['sale', 'purchase']):
journal.kanban_dashboard_graph = json.dumps(journal.get_bar_graph_datas())
elif (journal.type in ['cash', 'bank']):
journal.kanban_dashboard_graph = json.dumps(journal.get_line_graph_datas())

def _get_json_activity_data(self):
for journal in self:
Expand Down
1 change: 0 additions & 1 deletion addons/account/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -3407,7 +3407,6 @@ def create_analytic_lines(self):
values_list = lines_to_create_analytic_entries._prepare_analytic_line()
self.env['account.analytic.line'].create(values_list)

@api.multi
def _prepare_analytic_line(self):
""" Prepare the values used to create() an account.analytic.line upon validation of an account.move.line having
an analytic account. This method is intended to be extended in other modules.
Expand Down
41 changes: 21 additions & 20 deletions addons/account/models/account_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,27 +361,28 @@ def open_payment_matching_screen(self):
'context': action_context,
}

@api.one
@api.depends('invoice_ids', 'payment_type', 'partner_type', 'partner_id')
def _compute_destination_account_id(self):
if self.invoice_ids:
self.destination_account_id = self.invoice_ids[0].mapped('line_ids.account_id')\
.filtered(lambda account: account.user_type_id.type in ('receivable', 'payable'))[0]
elif self.payment_type == 'transfer':
if not self.company_id.transfer_account_id.id:
raise UserError(_('There is no Transfer Account defined in the accounting settings. Please define one to be able to confirm this transfer.'))
self.destination_account_id = self.company_id.transfer_account_id.id
elif self.partner_id:
if self.partner_type == 'customer':
self.destination_account_id = self.partner_id.property_account_receivable_id.id
else:
self.destination_account_id = self.partner_id.property_account_payable_id.id
elif self.partner_type == 'customer':
default_account = self.env['ir.property'].get('property_account_receivable_id', 'res.partner')
self.destination_account_id = default_account.id
elif self.partner_type == 'supplier':
default_account = self.env['ir.property'].get('property_account_payable_id', 'res.partner')
self.destination_account_id = default_account.id
for payment in self:
if payment.invoice_ids:
payment.destination_account_id = payment.invoice_ids[0].mapped(
'line_ids.account_id').filtered(
lambda account: account.user_type_id.type in ('receivable', 'payable'))[0]
elif payment.payment_type == 'transfer':
if not payment.company_id.transfer_account_id.id:
raise UserError(_('There is no Transfer Account defined in the accounting settings. Please define one to be able to confirm this transfer.'))
payment.destination_account_id = payment.company_id.transfer_account_id.id
elif payment.partner_id:
if payment.partner_type == 'customer':
payment.destination_account_id = payment.partner_id.property_account_receivable_id.id
else:
payment.destination_account_id = payment.partner_id.property_account_payable_id.id
elif payment.partner_type == 'customer':
default_account = self.env['ir.property'].get('property_account_receivable_id', 'res.partner')
payment.destination_account_id = default_account.id
elif payment.partner_type == 'supplier':
default_account = self.env['ir.property'].get('property_account_payable_id', 'res.partner')
payment.destination_account_id = default_account.id

@api.depends('move_line_ids.matched_debit_ids', 'move_line_ids.matched_credit_ids')
def _compute_reconciled_invoice_ids(self):
Expand Down Expand Up @@ -449,7 +450,7 @@ def cancel(self):
move.line_ids.remove_move_reconcile()
move.button_cancel()
move.unlink()
rec.state = 'cancelled'
payment.state = 'cancelled'

@api.multi
def unlink(self):
Expand Down
1 change: 0 additions & 1 deletion addons/account/models/chart_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ def _prepare_transfer_account_template(self):
'chart_template_id': self.id,
}

@api.one
def try_loading_for_current_company(self):
""" Installs this chart of accounts for the current company if not chart
of accounts had been created for it yet.
Expand Down
92 changes: 47 additions & 45 deletions addons/account/models/partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ class AccountFiscalPosition(models.Model):
# To be used in hiding the 'Federal States' field('attrs' in view side) when selected 'Country' has 0 states.
states_count = fields.Integer(compute='_compute_states_count')

@api.one
def _compute_states_count(self):
self.states_count = len(self.country_id.state_ids)
for position in self:
position.states_count = len(position.country_id.state_ids)

@api.one
@api.constrains('zip_from', 'zip_to')
def _check_zip(self):
if self.zip_from > self.zip_to:
raise ValidationError(_('Invalid "Zip Range", please configure it properly.'))
return True
result = []
for position in self:
if position.zip_from > position.zip_to:
raise ValidationError(_('Invalid "Zip Range", please configure it properly.'))
result.append(True)
return result

@api.model # noqa
def map_tax(self, taxes, product=None, partner=None):
Expand Down Expand Up @@ -323,53 +325,53 @@ def get_followup_lines_domain(self, date, overdue_only=False, only_unblocked=Fal
domain += overdue_domain
return domain

@api.one
def _compute_has_unreconciled_entries(self):
# Avoid useless work if has_unreconciled_entries is not relevant for this partner
if not self.active or not self.is_company and self.parent_id:
return
self.env.cr.execute(
""" SELECT 1 FROM(
SELECT
p.last_time_entries_checked AS last_time_entries_checked,
MAX(l.write_date) AS max_date
FROM
account_move_line l
RIGHT JOIN account_account a ON (a.id = l.account_id)
RIGHT JOIN res_partner p ON (l.partner_id = p.id)
WHERE
p.id = %s
AND EXISTS (
SELECT 1
FROM account_move_line l
WHERE l.account_id = a.id
AND l.partner_id = p.id
AND l.amount_residual > 0
)
AND EXISTS (
SELECT 1
FROM account_move_line l
WHERE l.account_id = a.id
AND l.partner_id = p.id
AND l.amount_residual < 0
)
GROUP BY p.last_time_entries_checked
) as s
WHERE (last_time_entries_checked IS NULL OR max_date > last_time_entries_checked)
""", (self.id,))
self.has_unreconciled_entries = self.env.cr.rowcount == 1
for partner in self:
# Avoid useless work if has_unreconciled_entries is not relevant for this partner
if not partner.active or not partner.is_company and partner.parent_id:
continue
self.env.cr.execute(
""" SELECT 1 FROM(
SELECT
p.last_time_entries_checked AS last_time_entries_checked,
MAX(l.write_date) AS max_date
FROM
account_move_line l
RIGHT JOIN account_account a ON (a.id = l.account_id)
RIGHT JOIN res_partner p ON (l.partner_id = p.id)
WHERE
p.id = %s
AND EXISTS (
SELECT 1
FROM account_move_line l
WHERE l.account_id = a.id
AND l.partner_id = p.id
AND l.amount_residual > 0
)
AND EXISTS (
SELECT 1
FROM account_move_line l
WHERE l.account_id = a.id
AND l.partner_id = p.id
AND l.amount_residual < 0
)
GROUP BY p.last_time_entries_checked
) as s
WHERE (last_time_entries_checked IS NULL OR max_date > last_time_entries_checked)
""", (partner.id,))
partner.has_unreconciled_entries = self.env.cr.rowcount == 1

@api.multi
def mark_as_reconciled(self):
self.env['account.partial.reconcile'].check_access_rights('write')
return self.sudo().with_context(company_id=self.env.company.id).write({'last_time_entries_checked': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)})

@api.one
def _get_company_currency(self):
if self.company_id:
self.currency_id = self.sudo().company_id.currency_id
else:
self.currency_id = self.env.company.currency_id
for partner in self:
if partner.company_id:
partner.currency_id = partner.sudo().company_id.currency_id
else:
partner.currency_id = self.env.company.currency_id

credit = fields.Monetary(compute='_credit_debit_get', search=_credit_search,
string='Total Receivable', help="Total amount this customer owes you.")
Expand Down
Loading