Skip to content

Commit

Permalink
[FIX] account_cutoff_accrual_sale: SO invoiced on order
Browse files Browse the repository at this point in the history
Properly compute accruals when the invoice policy is on order.
For service, consider it should be invoiced once confirmed.
For stock, make accruals if the invoice and delivered quantity are not
on the same period.
  • Loading branch information
jbaudoux committed Feb 12, 2025
1 parent 1ea46a4 commit 5c23391
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions account_cutoff_accrual_sale/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging

from odoo import api, fields, models
from odoo.osv import expression

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -36,14 +37,48 @@ def _get_cutoff_accrual_partner(self):
def _get_cutoff_accrual_product_qty(self):
return self.product_uom_qty

@api.model
def _get_cutoff_accrual_lines_domain(self, cutoff):
domain = super()._get_cutoff_accrual_lines_domain(cutoff)
# The line could be invoiceable but not the order (see delivery
# module).
domain.append(("invoice_status", "=", "to invoice"))
domain.append(("order_id.invoice_status", "=", "to invoice"))
domain = expression.AND(
(
domain,
(
("state", "in", ("sale", "done")),
("display_type", "=", False),
),
)
)
return domain

@api.model
def _get_cutoff_accrual_lines_query(self, cutoff):
query = super()._get_cutoff_accrual_lines_query(cutoff)
self.flush_model(
["qty_delivered_method", "qty_delivered", "qty_invoiced", "qty_to_invoice"]
)
# The delivery line could be invoiceable but not the order (see
# delivery module). So check also the SO invoice status.
so_alias = query.join(
self._table, "order_id", self.order_id._table, "id", "order_id"
)
self.order_id.flush_model(["invoice_status"])
# For stock products, we always consider the delivered quantity as it
# impacts the stock valuation.
# Otherwise, we consider the invoice policy by checking the
# qty_to_invoice.
query.add_where(
f"""
CASE
WHEN "{self._table}".qty_delivered_method = 'stock_move'
THEN "{self._table}".qty_delivered != "{self._table}".qty_invoiced
ELSE "{self._table}".qty_to_invoice != 0
AND "{so_alias}".invoice_status = 'to invoice'
END
"""
)
return query

def _prepare_cutoff_accrual_line(self, cutoff):
res = super()._prepare_cutoff_accrual_line(cutoff)
if not res:
Expand Down Expand Up @@ -96,6 +131,8 @@ def _get_cutoff_accrual_delivered_service_quantity(self, cutoff):
if self.create_date >= cutoff_nextday:
# A line added after the cutoff cannot be delivered in the past
return 0
# In case of service, we consider what should be invoiced and this is
# given by the invoice policy.
if self.product_id.invoice_policy == "order":
return self.product_uom_qty
return self.qty_delivered
Expand All @@ -106,6 +143,6 @@ def _get_cutoff_accrual_delivered_stock_quantity(self, cutoff):
if self.create_date >= cutoff_nextday:
# A line added after the cutoff cannot be delivered in the past
return 0
if self.product_id.invoice_policy == "order":
return self.product_uom_qty
# In case of stock, we always consider what is delivered as this
# impacted the stock valuation.
return self.qty_delivered

0 comments on commit 5c23391

Please sign in to comment.