diff --git a/purchase_suggest/wizard/purchase_suggest.py b/purchase_suggest/wizard/purchase_suggest.py index 065df36..24753b1 100644 --- a/purchase_suggest/wizard/purchase_suggest.py +++ b/purchase_suggest/wizard/purchase_suggest.py @@ -51,6 +51,16 @@ class PurchaseSuggestGenerate(models.TransientModel): order='id desc', limit=1) # I cannot filter on 'date_order' because it is not a stored field porderline_id = porderlines and porderlines[0].id or False + future_qty = qty_dict['virtual_available'] + qty_dict['draft_po_qty'] + if float_compare( + qty_dict['max_qty'], qty_dict['min_qty'], + precision_rounding=qty_dict['product'].uom_id.rounding) == 1: + # order to go up to qty_max + qty_to_order = qty_dict['max_qty'] - future_qty + else: + # order to go up to qty_min + qty_to_order = qty_dict['min_qty'] - future_qty + sline = { 'company_id': qty_dict['orderpoint'] and qty_dict['orderpoint'].company_id.id, @@ -64,7 +74,9 @@ class PurchaseSuggestGenerate(models.TransientModel): qty_dict['orderpoint'] and qty_dict['orderpoint'].id, 'location_id': self.location_id.id, 'min_qty': qty_dict['min_qty'], + 'max_qty': qty_dict['max_qty'], 'last_po_line_id': porderline_id, + 'qty_to_order': qty_to_order, } return sline @@ -98,6 +110,7 @@ class PurchaseSuggestGenerate(models.TransientModel): if op.product_id.id not in products: products[op.product_id.id] = { 'min_qty': op.product_min_qty, + 'max_qty': op.product_max_qty, 'draft_po_qty': 0.0, # This value is set later on 'orderpoint': op, 'product': op.product_id @@ -237,6 +250,10 @@ class PurchaseSuggest(models.TransientModel): string="Min Quantity", readonly=True, digits=dp.get_precision('Product Unit of Measure'), help="in the unit of measure for the product") + max_qty = fields.Float( + string="Max Quantity", readonly=True, + digits=dp.get_precision('Product Unit of Measure'), + help="in the unit of measure for the product") qty_to_order = fields.Float( string='Quantity to Order', digits=dp.get_precision('Product Unit of Measure'), diff --git a/purchase_suggest/wizard/purchase_suggest_view.xml b/purchase_suggest/wizard/purchase_suggest_view.xml index 1074425..7d81dbc 100644 --- a/purchase_suggest/wizard/purchase_suggest_view.xml +++ b/purchase_suggest/wizard/purchase_suggest_view.xml @@ -59,7 +59,8 @@ - + + diff --git a/purchase_suggest_min_qty_on_product/product.py b/purchase_suggest_min_qty_on_product/product.py index cd3228a..7ececd0 100644 --- a/purchase_suggest_min_qty_on_product/product.py +++ b/purchase_suggest_min_qty_on_product/product.py @@ -20,8 +20,10 @@ # ############################################################################## -from openerp import models, fields +from openerp import models, fields, api, _ import openerp.addons.decimal_precision as dp +from openerp.tools import float_compare, float_is_zero +from openerp.exceptions import ValidationError class ProductProduct(models.Model): @@ -34,3 +36,28 @@ class ProductProduct(models.Model): help="If the forecast quantity is lower than the value of this field, " "Odoo will suggest to re-order this product. This field is in the " "unit of measure of the product.") + max_qty = fields.Float( + string=u'Maximum Quantity', track_visibility='onchange', + digits=dp.get_precision('Product Unit of Measure'), + company_dependent=True, + help="If the forecast quantity is lower than the value of the minimum " + " quantity, Odoo will suggest to re-order this product to go up to " + "the maximum quantity. This field is in the unit of measure of the " + "product.") + + @api.constrains('min_qty', 'max_qty') + def check_min_max_qty(self): + precision = self.env['decimal.precision'].precision_get( + 'Product Unit of Measure') + for product in self: + if ( + not float_is_zero( + product.max_qty, precision_digits=precision) and + float_compare( + product.max_qty, product.min_qty, + precision_digits=precision) != 1): + raise ValidationError(_( + "On product '%s', the maximum quantity (%s) is lower " + "than the minimum quantity (%s).") % ( + product.name_get()[0][1], + product.max_qty, product.min_qty)) diff --git a/purchase_suggest_min_qty_on_product/product_view.xml b/purchase_suggest_min_qty_on_product/product_view.xml index 5959f4e..825a357 100644 --- a/purchase_suggest_min_qty_on_product/product_view.xml +++ b/purchase_suggest_min_qty_on_product/product_view.xml @@ -29,6 +29,7 @@ + diff --git a/purchase_suggest_min_qty_on_product/wizard/purchase_suggest.py b/purchase_suggest_min_qty_on_product/wizard/purchase_suggest.py index 44fd95e..2ec072a 100644 --- a/purchase_suggest_min_qty_on_product/wizard/purchase_suggest.py +++ b/purchase_suggest_min_qty_on_product/wizard/purchase_suggest.py @@ -44,7 +44,8 @@ class PurchaseSuggestionGenerate(models.TransientModel): @api.model def generate_products_dict(self): - '''inherit the native method to use min_qty on product.product''' + '''inherit the native method to use min_qty/max_qty on + product.product''' ppo = self.env['product.product'] products = {} product_domain = self._prepare_product_domain() @@ -56,6 +57,7 @@ class PurchaseSuggestionGenerate(models.TransientModel): # So we remove "if product.z_stock_min > 0" products[product.id] = { 'min_qty': product.min_qty, + 'max_qty': product.max_qty, 'draft_po_qty': 0.0, # This value is set later on 'orderpoint': False, 'product': product,