Skip to content

Commit

Permalink
Revert changes to computation of _immediately_usable_qty
Browse files Browse the repository at this point in the history
Commit 6c16913 changed the way we compute the immediately_usable_qty: instead of using the virtual stock, we used the sum of quants without reservations. But a quant may actually be reserved and still be available (for example it may be reserved for an internal move).
Fixes OCA#79

Remove loop and use correct decorator

Restore the features of stock_available_immediately

The previous fix restored stock_available but then there was no way to exclude the incomming moves from the count. This belongs in stock_available_immediately, restoring it cleanly.
This commit also takes care to respect the distinction between templates and variants, so it should fix OCA#73 too.

Restore the qty avail. to promise on variant treeview

PEP8
  • Loading branch information
Lionel Sausin authored and marielejeune committed Oct 4, 2022
1 parent ed2fd08 commit 4757bd9
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 153 deletions.
3 changes: 1 addition & 2 deletions stock_available/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@
#
##############################################################################

from . import product
from . import res_config
from . import models
7 changes: 4 additions & 3 deletions stock_available/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
'depends': ['stock'],
'license': 'AGPL-3',
'data': [
'product_view.xml',
'res_config_view.xml',
]
'views/product_template_view.xml',
'views/product_product_view.xml',
'views/res_config_view.xml',
],
}
23 changes: 23 additions & 0 deletions stock_available/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This module is copyright (C) 2014 Numérigraphe SARL. All Rights Reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from . import product_template
from . import product_product
from . import res_config
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,29 @@
from openerp.addons import decimal_precision as dp


class ProductTemplate(models.Model):
class ProductProduct(models.Model):
"""Add a field for the stock available to promise.
Useful implementations need to be installed through the Settings menu or by
installing one of the modules stock_available_*
"""
_inherit = 'product.template'
_inherit = 'product.product'

# immediately usable quantity caluculated with the quant method
@api.multi
@api.one
@api.depends('virtual_available')
def _immediately_usable_qty(self):
stock_location_obj = self.env['stock.location']
internal_locations = stock_location_obj.search([
('usage', '=', 'internal')])
sublocations = self.env['stock.location']
for location in internal_locations:
sublocations += stock_location_obj.search(
[('id', 'child_of', location.id)])
for product_template in self:
products = self.env['product.product'].search([
('product_tmpl_id', '=', product_template.id)])
quant_obj = self.env['stock.quant']
quants = quant_obj.search([
('location_id', 'in', sublocations.ids),
('product_id', 'in', products.ids),
('reservation_id', '=', False)])
availability = 0
if quants:
for quant in quants:
availability += quant.qty
product_template.immediately_usable_qty = availability
"""No-op implementation of the stock available to promise.
By default, available to promise = forecasted quantity.
Must be overridden by another module that actually implement
computations."""
self.immediately_usable_qty = self.virtual_available

immediately_usable_qty = fields.Float(
digits=dp.get_precision('Product Unit of Measure'),
compute='_immediately_usable_qty',
string='Available to promise (quant calculation)',
string='Available to promise',
help="Stock for this Product that can be safely proposed "
"for sale to Customers.\n"
"The definition of this value can be configured to suit "
"your needs , this number is obtained by using the new odoo 8 "
"quants, so it gives us the actual current quants minus reserved"
"quants")
"your needs")
42 changes: 42 additions & 0 deletions stock_available/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This module is copyright (C) 2014 Numérigraphe SARL. All Rights Reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import models, fields, api
from openerp.addons import decimal_precision as dp


class ProductTemplate(models.Model):
_inherit = 'product.template'

@api.one
@api.depends('virtual_available')
def _immediately_usable_qty(self):
"""Compute the quantity using all the variants"""
self.immediately_usable_qty = sum(
[v.immediately_usable_qty for v in self.product_variant_ids])

immediately_usable_qty = fields.Float(
digits=dp.get_precision('Product Unit of Measure'),
compute='_immediately_usable_qty',
string='Available to promise',
help="Stock for this Product that can be safely proposed "
"for sale to Customers.\n"
"The definition of this value can be configured to suit "
"your needs")
File renamed without changes.
120 changes: 0 additions & 120 deletions stock_available/tests/test_stock_available.py

This file was deleted.

20 changes: 20 additions & 0 deletions stock_available/views/product_product_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record model="ir.ui.view" id="view_stock_available_tree_variant">
<field name="name">Quantity available to promise (variant tree)</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="stock.view_stock_product_tree"/>
<field name="arch" type="xml">
<data>
<tree position="attributes">
<attribute name="colors">red:immediately_usable_qty&lt;0;blue:immediately_usable_qty&gt;=0 and state in ('draft', 'end', 'obsolete');black:immediately_usable_qty&gt;=0 and state not in ('draft', 'end', 'obsolete')</attribute>
</tree>
<field name="virtual_available" position="after">
<field name="immediately_usable_qty" />
</field>
</data>
</field>
</record>
</data>
</openerp>
File renamed without changes.
File renamed without changes.

0 comments on commit 4757bd9

Please sign in to comment.