This question has been flagged

Hi,

I  need to override the default_get(self, default_fields) method in order to include a newly created state in the following warning in that function. How can I do that? Thanks in advance

if not invoices or any(invoice.state != 'posted' for invoice in invoices):
raise UserError(_("You can only register payments for open invoices"))

Avatar
Discard
Best Answer

Hi,

Try the following

from odoo import models, fields, api, _
from odoo.addons.account.models.account_payment import account_payment


class AccountPayment(models.Model):
_inherit = 'account.payment'


@api.model
def default_get(self, default_fields):
# You can write your modified lines of code here
rec = super(account_payment, self).default_get(default_fields)
return rec


account_payment.default_get = default_get

Regards

Avatar
Discard
Best Answer

Hello, I had the same problem yet this solution is not working 

here is my code 

I can't get to the print inside the if active_model == 'sale.order' 


class AccountPaymentNumidoo(models.Model):
_inherit = "account.payment"
# order_line_ids = fields.One2many('sale.order.line', 'payment_id', readonly=True, copy=False, ondelete='restrict')
order_ids = fields.Many2many('sale.order', 'account_order_payment_rel', 'payment_id', 'order_id',
string="Invoices", copy=False, readonly=True,
help="""Technical field containing the invoice for which the payment has been generated.
This does not especially correspond to the invoices reconciled with the payment,
as it can have been generated first, and reconciled later""")
mode_payment = fields.Selection([('especes', 'Espèces'),
('par_cheque', 'Par chèque'),
('virement_bancaire', 'Virement Bancaire'),
('versement', 'Versement Bancaire'),
('traite', 'Traite')])
@api.model
def default_get(self, default_fields):
rec = super(account_payment, self).default_get(default_fields)
active_ids = self._context.get('active_ids') or self._context.get('active_id')
active_model = self._context.get('active_model')
print(active_model)
# Check for selected invoices ids
if not active_ids or active_model != 'account.move' or active_model != 'sale.order':
return rec
if active_model == 'account.move':
invoices = self.env['account.move'].browse(active_ids).filtered(lambda move: move.is_invoice(include_receipts=True))
# Check all invoices are open
if not invoices or any(invoice.state != 'posted' for invoice in invoices):
raise UserError(_("You can only register payments for open invoices"))
# Check if, in batch payments, there are not negative invoices and positive invoices
dtype = invoices[0].type
for inv in invoices[1:]:
if inv.type != dtype:
if ((dtype == 'in_refund' and inv.type == 'in_invoice') or
(dtype == 'in_invoice' and inv.type == 'in_refund')):
raise UserError(
_("You cannot register payments for vendor bills and supplier refunds at the same time."))
if ((dtype == 'out_refund' and inv.type == 'out_invoice') or
(dtype == 'out_invoice' and inv.type == 'out_refund')):
raise UserError(
_("You cannot register payments for customer invoices and credit notes at the same time."))

amount = self._compute_payment_amount(invoices, invoices[0].currency_id, invoices[0].journal_id,
rec.get('payment_date') or fields.Date.today())
rec.update({
'currency_id': invoices[0].currency_id.id,
'amount': abs(amount),
'payment_type': 'inbound' if amount > 0 else 'outbound',
'partner_id': invoices[0].commercial_partner_id.id,
'partner_type': MAP_INVOICE_TYPE_PARTNER_TYPE[invoices[0].type],
'communication': invoices[0].invoice_payment_ref or invoices[0].ref or invoices[0].name,
'invoice_ids': [(6, 0, invoices.ids)],
})
else:
if active_model == 'sale.order':
print("i aaaaaaaaaaaa ma sale order")
orders = self.env['sale.order'].browse(active_ids).filtered(lambda move: move.is_sale_document(include_receipts=True))
amount=0
rec.update({
'currency_id': orders[0].currency_id.id,
'amount': abs(amount),
'payment_type': 'inbound',
'partner_id': orders[0].commercial_partner_id.id,
#'partner_type': MAP_INVOICE_TYPE_PARTNER_TYPE[invoices[0].type],
'communication': orders[0].order_payment_ref or orders[0].ref or orders[0].name,
'order_ids': [(6, 0, order.ids)],
})
return rec
account_payment.default_get = default_get
Avatar
Discard
Best Answer

Hello Stephen
Please check below example, I think it's helpful for you.

Ex.
@api.model
    def default_get(self, default_fields):
        rec = super(Order, self).default_get(default_fields)
        active_model = self.env.context.get('active_model', False)
        active_id = self.env.context.get('active_id', False)
        if active_model and active_id and active_model == 'sale.order':
            invoices = self.env['sale.order'].browse(active_id).invoice_ids
            if not invoices or any(invoice.state != 'posted' for invoice in invoices):
                raise UserError(_("You can only register payments for open invoices"))

Avatar
Discard
Best Answer

@api.model
def default_get(self, default_fields):
    res = super(AccountPayment, self).default_get(default_fields)
    active_ids = self._context.get('active_ids')
    invoices = self.env['account.invoice'].browse(active_ids)
    communication = ' '.join([ref for ref in invoices.mapped('reference') if ref]),
    res.update({
       'communication': communication,
    })
    return res

Avatar
Discard