I have maintenace and invoice app i want when create maintenance request automatically create invoice for this customer in community
Se marcó esta pregunta
Hi,
To automatically create an invoice when a maintenance request is created in Odoo Community Edition, you can extend the maintenance.request model to include a customer (partner_id) field and override the create() method to generate an invoice. The invoice is created using the account.move model with a basic invoice line. Below is a sample implementation:
from odoo import models, fields, api
class MaintenanceRequest(models.Model):
_inherit = 'maintenance.request'
partner_id = fields.Many2one('res.partner', string="Customer", required=True)
@api.model
def create(self, vals):
record = super().create(vals)
record._create_invoice()
return record
def _create_invoice(self):
income_account = self.env['account.account'].search([('user_type_id.type', '=', 'income')], limit=1)
self.env['account.move'].create({
'move_type': 'out_invoice',
'partner_id': self.partner_id.id,
'invoice_date': fields.Date.today(),
'invoice_line_ids': [(0, 0, {
'name': f"Maintenance Service - {self.name}",
'quantity': 1,
'price_unit': 100.0,
'account_id': income_account.id,
})],
})
Hope it helps
thank you work just i change the user_type_id and fields to dynamic
from odoo import models, fields, api
class MaintenanceRequest(models.Model):
_inherit = 'maintenance.request'
maintenance_amount = fields.Monetary(string="Maintenance Amount")
currency_id = fields.Many2one('res.currency', string="Currency", default=lambda self: self.env.company.currency_id)
partner_id = fields.Many2one('res.partner', string="Customer", required=True)
maintenance_amount = fields.Float(string="Maintenance Amount", required=True)
maintenance_quantity = fields.Float(string="Maintenance Quantity", required=True, default=1.0)
@api.model
def create(self, vals):
record = super().create(vals)
record._create_invoice()
return record
def _create_invoice(self):
income_account = self.env['account.account'].search([('account_type', '=', 'income')], limit=1)
if not income_account:
raise ValueError("No income account found! Please configure an income account in Chart of Accounts.")
self.env['account.move'].create({
'move_type': 'out_invoice',
'partner_id': self.partner_id.id,
'invoice_date': fields.Date.today(),
'invoice_line_ids': [(0, 0, {
'name': f"Maintenance Service - {self.name}",
'quantity': self.maintenance_quantity,
'price_unit': self.maintenance_amount,
'account_id': income_account.id,
})],
})
¿Le interesa esta conversación? ¡Participe en ella!
Cree una cuenta para poder utilizar funciones exclusivas e interactuar con la comunidad.
Registrarse