This question has been flagged
1 Reply
4749 Views

I have created an invoice from code which is posted now i want to register its payment to paid on a button click from my code. What should I do to pay the invoice from python code?

Avatar
Discard
Best Answer

Hi,

You can create a payment by directly calling the create method of the account.payment model and passing the invoice_id in the context. See this example,

Payment = self.env['account.payment'].with_context(default_invoice_ids=[(4, inv1.id, False)])
payment = Payment.create({
'payment_date': time.strftime('%Y') + '-07-15',
'payment_method_id': self.inbound_payment_method.id,
'payment_type': 'inbound',
'partner_type': 'customer',
'partner_id': inv1.partner_id.id,
'amount': 314.07,
'journal_id': self.bank_journal_euro.id,
'company_id': company.id,
'currency_id': self.currency_euro_id,
'payment_difference_handling': 'reconcile',
'writeoff_account_id': self.diff_income_account.id,
})
payment.post()


Thanks

Avatar
Discard
Author

Thanks. Let me try this

Author

File "/home/syedhamza/custom/programming/enviornments/odoo/odoo13/src/odoo/odoo/addons/base/models/res_currency.py", line 192, in _convert

assert company, "convert amount from unknown company"

AssertionError: convert amount from unknown company

i m receiving this error here's my code:

def create_token_fees(self):

token = self.env.ref('real_estate.token_money')

settings = self.env['res.config.settings'].search([])

company = self.env.user.company_id.id

prod = [(0, 0, {

'product_id': token.id,

'name': token.name,

'account_id': token.property_account_income_id.id or False,

'quantity': 1,

'price_unit': self.token_fees

})]

invoice = self.env['account.move'].create({

# 'name': rec.property_registration_id.name,

# 'user_id': rec.manager_id.id,

'partner_id': settings.token_partner_id.id,

'type': 'out_invoice',

'journal_id': settings.account_journal_id.id,

'crm_id': self.crm_id.id,

'token_partner': self.crm_id.contact_name,

'invoice_date': fields.Date.today(),

'invoice_line_ids': prod

})

print("Token Fee")

invoice.action_post()

Payment = self.env['account.payment'].with_context(default_invoice_ids=[(4, invoice.id, False)])

payment = Payment.create({

'payment_date': fields.Date.today(),

# 'payment_method_id': self.inbound_payment_method.id,

'payment_type': 'inbound',

'partner_type': 'customer',

'partner_id': settings.token_partner_id.id,

'amount': 314.07,

' journal_id ': settings.account_journal_id.id,

'company_id': company.id,

'currency_id': company.currency_id.id,

'payment_difference_handling': 'reconcile',

# 'writeoff_account_id': self.diff_income_account.id,

})

payment.post()