Skip to content
5 changes: 4 additions & 1 deletion sale_pricelist_from_commitment_date/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
{
"name": "Sale Pricelist From Commitment Date",
"summary": "Use sale order commitment date to compute line price from pricelist",
"version": "16.0.1.0.1",
"version": "16.0.1.1.0",
"category": "Sale",
"website": "https://github.com/OCA/sale-workflow",
"author": "Camptocamp, Odoo Community Association (OCA)",
"license": "AGPL-3",
"depends": ["sale"],
"data": [
"views/product_pricelist.xml",
],
"installable": True,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2024 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

import logging

from odoo import SUPERUSER_ID, api

_logger = logging.getLogger(__name__)


def migrate(cr, version):
# Price was based on commitment/delivery date by default before, so enable
# the new option on all pricelists.
if not version:
return
_logger.info("Enable price based on delivery date for all pricelists...")
env = api.Environment(cr, SUPERUSER_ID, {})
pricelists = env["product.pricelist"].search([])
pricelists.write({"price_based_on_delivery_date": True})
1 change: 1 addition & 0 deletions sale_pricelist_from_commitment_date/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

from . import product_pricelist
from . import product_pricelist_item
from . import sale_order
from . import sale_order_line
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Copyright 2021 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models
from odoo import fields, models


class ProductPricelist(models.Model):

_inherit = "product.pricelist"

price_based_on_delivery_date = fields.Boolean()

def _get_product_rule(self, product, quantity, uom=None, date=False, **kwargs):
force_pricelist_date = self.env.context.get("force_pricelist_date")
if force_pricelist_date:
Expand Down
16 changes: 16 additions & 0 deletions sale_pricelist_from_commitment_date/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2024 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from odoo import models


class SaleOrder(models.Model):
_inherit = "sale.order"

def _get_pricelist_date(self):
if self.env.context.get("force_pricelist_date"):
return self.env.context["force_pricelist_date"]
if self.pricelist_id.price_based_on_delivery_date:
return self.commitment_date or self.expected_date
else:
return False
34 changes: 21 additions & 13 deletions sale_pricelist_from_commitment_date/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,40 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, models
from odoo.tools import groupby


class SaleOrderLine(models.Model):

_inherit = "sale.order.line"

@api.depends(
"product_id", "product_uom", "product_uom_qty", "order_id.commitment_date"
"product_id",
"product_uom",
"product_uom_qty",
"order_id.commitment_date",
"order_id.pricelist_id",
)
def _compute_price_unit(self):
for line in self:
date = self.env.context.get(
"force_pricelist_date", line.order_id.commitment_date
for order, lines in groupby(self, key=lambda line: line.order_id):
date = order._get_pricelist_date()
lines_ = (
self.browse().concat(*lines).with_context(force_pricelist_date=date)
)
line = line.with_context(force_pricelist_date=date)
super(SaleOrderLine, line)._compute_price_unit()
super(SaleOrderLine, lines_)._compute_price_unit()
return True

@api.depends(
"product_id", "product_uom", "product_uom_qty", "order_id.commitment_date"
"product_id",
"product_uom",
"product_uom_qty",
"order_id.commitment_date",
"order_id.pricelist_id",
)
def _compute_pricelist_item_id(self):
for line in self:
date = self.env.context.get(
"force_pricelist_date", line.order_id.commitment_date
for order, lines in groupby(self, key=lambda line: line.order_id):
date = order._get_pricelist_date()
lines_ = (
self.browse().concat(*lines).with_context(force_pricelist_date=date)
)
line = line.with_context(force_pricelist_date=date)
super(SaleOrderLine, line)._compute_pricelist_item_id()
super(SaleOrderLine, lines_)._compute_pricelist_item_id()
return True
1 change: 1 addition & 0 deletions sale_pricelist_from_commitment_date/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* rename module to `sale_price_from_delivery_date` for next migration
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def _create_price_list(cls, name):
"active": True,
"currency_id": cls.env.ref("base.USD").id,
"company_id": cls.env.user.company_id.id,
"price_based_on_delivery_date": True,
}
)

Expand Down Expand Up @@ -119,7 +120,8 @@ def test_00_pricelist(self):
sale.date_order = "2020-03-08"
# No change with changing order date
self.assertEqual(order_line.price_unit, 30)
# Remove commitment date, will match on date_order
# Disable price based on delivery date, will match on date_order
sale.pricelist_id.price_based_on_delivery_date = False
sale.commitment_date = False
self.assertEqual(order_line.price_unit, 10)
# Remove the order date, will match on default price
Expand Down
18 changes: 18 additions & 0 deletions sale_pricelist_from_commitment_date/views/product_pricelist.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2024 Camptocamp SA
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>

<record id="product_pricelist_view" model="ir.ui.view">
<field name="name">product.pricelist.form</field>
<field name="model">product.pricelist</field>
<field name="inherit_id" ref="product.product_pricelist_view" />
<field name="arch" type="xml">
<xpath expr="//field[@name='company_id']" position="after">
<field name="price_based_on_delivery_date" />
</xpath>

</field>
</record>

</odoo>
Loading