Browse Source

Oct 08: [FIX] Bug Fixed 'automatic_invoice_and_post'

14.0
RisvanaCybro 7 months ago
parent
commit
74b12c9b68
  1. 70
      automatic_invoice_and_post/models/stock_picking.py

70
automatic_invoice_and_post/models/stock_picking.py

@ -2,7 +2,6 @@
###############################################################################
#
# Cybrosys Technologies Pvt. Ltd.
#
# Copyright (C) 2023-TODAY Cybrosys Technologies(<https://www.cybrosys.com>)
# Author: Afra K (odoo@cybrosys.com)
#
@ -21,51 +20,36 @@
###############################################################################
from odoo import models
class StockPicking(models.Model):
"""
This class inherits from the `stock.picking` model in Odoo and adds
functionality to automatically create and post invoices when a delivery is
validated.
"""
_inherit = 'stock.picking'
def button_validate(self):
"""
This function is called when a delivery is validated. It checks whether
an invoice should be created and posted automatically based on
configuration settings. If an invoice should be created, it calls
the `_create_invoices` function on the sale associated with the
delivery, creates the invoice, and posts it. If the `auto_send_invoice`
configuration setting is enabled, it sends an email to the customer with
the invoice attached.
:return: result of the parent class's button_validate function
"""
res = super().button_validate()
auto_validate_invoice = self.env[
'ir.config_parameter'].sudo().get_param(
'automatic_invoice_and_post.is_create_invoice_delivery_validate')
auto_send_invoice = self.env['ir.config_parameter'].sudo().get_param(
'automatic_invoice_and_post.is_auto_send_invoice')
if auto_validate_invoice:
if any(rec.product_id.invoice_policy == 'delivery' for rec in
self.move_lines) or not self.sale_id.invoice_ids:
# Call the _create_invoices function on the associated sale
# to create the invoice
invoice_created = self.sale_id._create_invoices(
self.sale_id) if self.sale_id else False
# Post the created invoice
if invoice_created:
invoice_created.action_post()
# If automatic invoice sending is enabled and the customer
# has an email address,send the invoice to the customer
if auto_send_invoice and invoice_created.partner_id.email:
template = self.env.ref(
'account.email_template_edi_invoice').sudo()
template.send_mail(invoice_created.id,
force_send=True,
email_values={
'email_to': invoice_created.partner_id.email
})
auto_validate_invoice = self.env['ir.config_parameter'].sudo().get_param('automatic_invoice_and_post.is_create_invoice_delivery_validate')
auto_send_invoice = self.env['ir.config_parameter'].sudo().get_param('automatic_invoice_and_post.is_auto_send_invoice')
if auto_validate_invoice and self.sale_id:
try:
# Check for order lines with delivered quantities or backorders
if self.all_order_lines_matched() or self.has_backorders_with_deliveries():
invoice = self.sale_id._create_invoices(self.sale_id)
invoice.action_post() # Post the created invoice
self.sale_id.invoice_ids += invoice
# Send invoice if enabled
if auto_send_invoice and invoice.partner_id.email:
template = self.env.ref('account.email_template_edi_invoice').sudo()
template.send_mail(invoice.id, force_send=True, email_values={'email_to': invoice.partner_id.email})
except Exception:
pass # Optionally handle the exception as needed
return res
def all_order_lines_matched(self):
""" Check if all order lines have matching ordered and delivered quantities. """
return all(line.product_uom_qty == line.qty_delivered for line in self.sale_id.order_line)
def has_backorders_with_deliveries(self):
""" Check if there are delivered items that also have backorders. """
return any(line.qty_delivered > 0 and line.product_uom_qty > line.qty_delivered for line in self.sale_id.order_line)

Loading…
Cancel
Save