From f52949fd4efb8fde266ce8facd95dd757eb7fe2f Mon Sep 17 00:00:00 2001 From: Jordi Ballester Date: Tue, 30 May 2017 16:50:47 +0200 Subject: [PATCH] [IMP] stock_demand_estimate: replace demand estimate periods with date range --- stock_demand_estimate/README.rst | 9 ++- stock_demand_estimate/__openerp__.py | 4 +- stock_demand_estimate/models/__init__.py | 2 +- stock_demand_estimate/models/date_range.py | 22 +++++ .../models/stock_demand_estimate.py | 27 ++++--- .../models/stock_demand_estimate_period.py | 49 ----------- .../security/ir.model.access.csv | 2 - .../security/stock_security.xml | 24 ++---- .../stock_demand_estimate_period_view.xml | 49 ----------- .../views/stock_demand_estimate_view.xml | 4 +- .../wizards/stock_demand_estimate_wizard.py | 81 +++++++++++-------- .../stock_demand_estimate_wizard_view.xml | 14 ++-- 12 files changed, 111 insertions(+), 176 deletions(-) create mode 100644 stock_demand_estimate/models/date_range.py delete mode 100644 stock_demand_estimate/models/stock_demand_estimate_period.py delete mode 100644 stock_demand_estimate/views/stock_demand_estimate_period_view.xml diff --git a/stock_demand_estimate/README.rst b/stock_demand_estimate/README.rst index d3ca4b1c8..2fba92c0f 100644 --- a/stock_demand_estimate/README.rst +++ b/stock_demand_estimate/README.rst @@ -14,9 +14,12 @@ The module does not provide in itself any specific usage of the estimates. Installation ============ -This module relies on the OCA module '2D matrix for x2many fields', and can -be downloaded from: -* Github: https://github.com/OCA/web/tree/8.0/web_widget_x2many_2d_matrix +This module relies on: + +* The OCA module '2D matrix for x2many fields', and can be downloaded from + Github: https://github.com/OCA/web/tree/9.0/web_widget_x2many_2d_matrix +* The OCA module 'Date Range', and can be downloaded from + Github: https://github.com/OCA/server-tools/tree/9.0/date_range Usage diff --git a/stock_demand_estimate/__openerp__.py b/stock_demand_estimate/__openerp__.py index 3d838b25f..d2b165a19 100644 --- a/stock_demand_estimate/__openerp__.py +++ b/stock_demand_estimate/__openerp__.py @@ -11,11 +11,11 @@ "website": "https://www.odoo-community.org", "category": "Warehouse Management", "depends": ["stock", - "web_widget_x2many_2d_matrix" + "web_widget_x2many_2d_matrix", + "date_range" ], "data": ["security/ir.model.access.csv", "security/stock_security.xml", - "views/stock_demand_estimate_period_view.xml", "views/stock_demand_estimate_view.xml", "wizards/stock_demand_estimate_wizard_view.xml", ], diff --git a/stock_demand_estimate/models/__init__.py b/stock_demand_estimate/models/__init__.py index 7e93e257a..55c1477c2 100644 --- a/stock_demand_estimate/models/__init__.py +++ b/stock_demand_estimate/models/__init__.py @@ -3,5 +3,5 @@ # (http://www.eficent.com) # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). -from . import stock_demand_estimate_period from . import stock_demand_estimate +from . import date_range diff --git a/stock_demand_estimate/models/date_range.py b/stock_demand_estimate/models/date_range.py new file mode 100644 index 000000000..34321dc34 --- /dev/null +++ b/stock_demand_estimate/models/date_range.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# © 2016 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp import api, fields, models +from openerp.tools.translate import _ +from openerp.exceptions import ValidationError + + +class DateRange(models.Model): + _inherit = "date.range" + + @api.multi + @api.depends('date_start', 'date_end') + def _compute_days(self): + for rec in self: + if rec.date_start and rec.date_end: + rec.days = (fields.Date.from_string(rec.date_start) - + fields.Date.from_string(rec.date_end)).days + 1 + + days = fields.Float(string="Days between dates", + compute='_compute_days', store=True, readonly=True) diff --git a/stock_demand_estimate/models/stock_demand_estimate.py b/stock_demand_estimate/models/stock_demand_estimate.py index d4dc48091..8afcd8770 100644 --- a/stock_demand_estimate/models/stock_demand_estimate.py +++ b/stock_demand_estimate/models/stock_demand_estimate.py @@ -31,10 +31,10 @@ class StockDemandEstimate(models.Model): @api.multi def _compute_daily_qty(self): for rec in self: - rec.daily_qty = rec.product_qty / rec.period_id.days + rec.daily_qty = rec.product_qty / rec.date_range_id.days - period_id = fields.Many2one( - comodel_name="stock.demand.estimate.period", + date_range_id = fields.Many2one( + comodel_name="date.range", string="Estimating Period", required=True) product_id = fields.Many2one(comodel_name="product.product", @@ -62,21 +62,26 @@ class StockDemandEstimate(models.Model): def name_get(self): res = [] for rec in self: - name = "%s - %s - %s" % (rec.period_id.name, rec.product_id.name, + name = "%s - %s - %s" % (rec.date_range_id.name, rec.product_id.name, rec.location_id.name) res.append((rec.id, name)) return res @api.model - def get_quantity_by_date_range(self, date_from, date_to): + def get_quantity_by_date_range(self, date_start, date_end): # Check if the dates overlap with the period - period_date_from = fields.Date.from_string(self.period_id.date_from) - period_date_to = fields.Date.from_string(self.period_id.date_to) + period_date_start = fields.Date.from_string( + self.date_range_id.date_start) + period_date_end = fields.Date.from_string( + self.date_range_id.date_end) - if date_from <= period_date_to and period_date_from <= date_to: - overlap_date_from = max(period_date_from, date_from) - overlap_date_to = min(period_date_to, date_to) - days = (abs(overlap_date_to-overlap_date_from)).days + 1 + # We need only the periods that overlap + # the dates introduced by the user. + if (date_start <= period_date_start <= date_end + or date_start <= period_date_end <= date_end): + overlap_date_start = max(period_date_start, date_start) + overlap_date_end = min(period_date_end, date_end) + days = (abs(overlap_date_end-overlap_date_start)).days + 1 return days * self.daily_qty return 0.0 diff --git a/stock_demand_estimate/models/stock_demand_estimate_period.py b/stock_demand_estimate/models/stock_demand_estimate_period.py deleted file mode 100644 index cb62e6285..000000000 --- a/stock_demand_estimate/models/stock_demand_estimate_period.py +++ /dev/null @@ -1,49 +0,0 @@ -# -*- coding: utf-8 -*- -# © 2016 Eficent Business and IT Consulting Services S.L. -# (http://www.eficent.com) -# © 2016 Aleph Objects, Inc. (https://www.alephobjects.com/) -# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). - -from openerp import api, fields, models, _ -from openerp.exceptions import Warning as UserError - - -class StockDemandEstimatePeriod(models.Model): - _name = 'stock.demand.estimate.period' - _description = 'Stock Demand Estimate Period' - _order = 'date_from' - - @api.multi - @api.depends('date_from', 'date_to') - def _compute_days(self): - for rec in self: - if rec.date_from and rec.date_to: - rec.days = (fields.Date.from_string(rec.date_to) - - fields.Date.from_string(rec.date_from)).days + 1 - - name = fields.Char(string="Name", required=True) - date_from = fields.Date(string="Date From", required=True) - date_to = fields.Date(string="Date To", required=True) - days = fields.Float(string="Days between dates", - compute='_compute_days', store=True, readonly=True) - - estimate_ids = fields.One2many( - comodel_name="stock.demand.estimate", - inverse_name="period_id") - - company_id = fields.Many2one( - comodel_name='res.company', string='Company', required=True, - default=lambda self: self.env['res.company']._company_default_get( - 'stock.demand.estimate.period')) - - @api.multi - @api.constrains('name', 'date_from', 'date_to') - def _check_period(self): - for period in self: - self.env.cr.execute('SELECT id, date_from, date_to \ - FROM stock_demand_estimate_period \ - WHERE (date_from <= %s and %s <= date_to) \ - AND id <> %s', (period.date_to, period.date_from, period.id)) - res = self.env.cr.fetchall() - if res: - raise UserError(_('Two periods cannot overlap.')) diff --git a/stock_demand_estimate/security/ir.model.access.csv b/stock_demand_estimate/security/ir.model.access.csv index 715896e64..a250f8113 100644 --- a/stock_demand_estimate/security/ir.model.access.csv +++ b/stock_demand_estimate/security/ir.model.access.csv @@ -1,5 +1,3 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink access_stock_demand_estimate,stock.orderpoint.demand.estimate,model_stock_demand_estimate,stock.group_stock_user,1,0,0,0 access_stock_demand_estimate_system,stock.orderpoint.demand.estimate system,model_stock_demand_estimate,stock.group_stock_manager,1,1,1,1 -access_stock_demand_estimate_period,stock.orderpoint.demand.estimate.period,model_stock_demand_estimate_period,stock.group_stock_user,1,0,0,0 -access_stock_demand_estimate_period_system,stock.orderpoint.demand.estimate.period system,model_stock_demand_estimate_period,stock.group_stock_manager,1,1,1,1 diff --git a/stock_demand_estimate/security/stock_security.xml b/stock_demand_estimate/security/stock_security.xml index c447a085c..7dc10eb89 100644 --- a/stock_demand_estimate/security/stock_security.xml +++ b/stock_demand_estimate/security/stock_security.xml @@ -1,22 +1,14 @@ - + - - Stock demand estimate multi-company - - - ['|',('company_id','=',False),('company_id','child_of',[user.company_id.id])] - + + Stock demand estimate multi-company + + + ['|',('company_id','=',False),('company_id','child_of',[user.company_id.id])] + - - Stock demand estimate multi-company - - - ['|',('company_id','=',False),('company_id','child_of',[user.company_id.id])] - - - + diff --git a/stock_demand_estimate/views/stock_demand_estimate_period_view.xml b/stock_demand_estimate/views/stock_demand_estimate_period_view.xml deleted file mode 100644 index d32933cee..000000000 --- a/stock_demand_estimate/views/stock_demand_estimate_period_view.xml +++ /dev/null @@ -1,49 +0,0 @@ - - - - - - stock.demand.estimate.period.tree - stock.demand.estimate.period - - - - - - - - - - - - stock.demand.estimate.period.search - stock.demand.estimate.period - - - - - - - - - - - Stock Demand Estimate Periods - ir.actions.act_window - stock.demand.estimate.period - form - tree,form - - - - - - - diff --git a/stock_demand_estimate/views/stock_demand_estimate_view.xml b/stock_demand_estimate/views/stock_demand_estimate_view.xml index 2b2f0f981..343ba25a2 100644 --- a/stock_demand_estimate/views/stock_demand_estimate_view.xml +++ b/stock_demand_estimate/views/stock_demand_estimate_view.xml @@ -8,7 +8,7 @@ stock.demand.estimate - + @@ -25,7 +25,7 @@ stock.demand.estimate - + diff --git a/stock_demand_estimate/wizards/stock_demand_estimate_wizard.py b/stock_demand_estimate/wizards/stock_demand_estimate_wizard.py index 8d97c95d2..dc4c8f405 100644 --- a/stock_demand_estimate/wizards/stock_demand_estimate_wizard.py +++ b/stock_demand_estimate/wizards/stock_demand_estimate_wizard.py @@ -8,21 +8,16 @@ from openerp import api, fields, models, _ import openerp.addons.decimal_precision as dp from openerp.exceptions import Warning as UserError -_PERIOD_SELECTION = [ - ('monthly', 'Monthly'), - ('weekly', 'Weekly') -] - class StockDemandEstimateSheet(models.TransientModel): _name = 'stock.demand.estimate.sheet' _description = 'Stock Demand Estimate Sheet' - def _default_date_from(self): - return self.env.context.get('date_from', False) + def _default_date_start(self): + return self.env.context.get('date_start', False) - def _default_date_to(self): - return self.env.context.get('date_to', False) + def _default_date_end(self): + return self.env.context.get('date_end', False) def _default_location_id(self): location_id = self.env.context.get('location_id', False) @@ -32,13 +27,24 @@ class StockDemandEstimateSheet(models.TransientModel): return False def _default_estimate_ids(self): - date_from = self.env.context.get('date_from', False) - date_to = self.env.context.get('date_to', False) - location_id = self.env.context.get('location_id', False) - product_ids = self.env.context.get('product_ids', False) - domain = [('date_from', '>=', date_from), - ('date_to', '<=', date_to)] - periods = self.env['stock.demand.estimate.period'].search( + date_start = self.env.context.get('default_date_start', False) + date_end = self.env.context.get('default_date_end', False) + date_range_type_id = self.env.context.get('default_date_range_type_id', + False) + location_id = self.env.context.get('default_location_id', False) + product_ids = self.env.context.get('default_product_ids', False) + domain = [('type_id', '=', date_range_type_id), '|', '&', + ('date_start', '>=', date_start), + ('date_start', '<=', date_end), + '&', + ('date_end', '>=', date_start), + ('date_end', '<=', date_end)] + periods = self.env['date.range'].search( + domain) + domain = [('type_id', '=', date_range_type_id), + ('date_start', '<=', date_start), + ('date_end', '>=', date_start)] + periods |= self.env['date.range'].search( domain) products = self.env['product.product'].browse(product_ids) @@ -52,13 +58,13 @@ class StockDemandEstimateSheet(models.TransientModel): for period in periods: estimates = self.env['stock.demand.estimate'].search( [('product_id', '=', product.id), - ('period_id', '=', period.id), + ('date_range_id', '=', period.id), ('location_id', '=', location_id)]) if estimates: lines.append((0, 0, { 'value_x': period.name, 'value_y': name_y, - 'period_id': period.id, + 'date_range_id': period.id, 'product_id': product.id, 'product_uom': estimates[0].product_uom.id, 'location_id': location_id, @@ -69,7 +75,7 @@ class StockDemandEstimateSheet(models.TransientModel): lines.append((0, 0, { 'value_x': period.name, 'value_y': name_y, - 'period_id': period.id, + 'date_range_id': period.id, 'product_id': product.id, 'product_uom': product.uom_id.id, 'location_id': location_id, @@ -77,13 +83,13 @@ class StockDemandEstimateSheet(models.TransientModel): })) return lines - date_from = fields.Date(string="Date From", readonly=True, - default=_default_date_from) - date_to = fields.Date(string="Date From", readonly=True, - default=_default_date_to) + date_start = fields.Date(string="Date From", readonly=True) + date_end = fields.Date(string="Date From", readonly=True) + date_range_type_id = fields.Many2one(string='Date Range Type', + comodel_name='date.range.type', + readonly=True) location_id = fields.Many2one(comodel_name="stock.location", - string="Location", readonly=True, - default=_default_location_id) + string="Location", readonly=True) line_ids = fields.Many2many( string="Estimates", comodel_name='stock.demand.estimate.sheet.line', @@ -93,7 +99,7 @@ class StockDemandEstimateSheet(models.TransientModel): @api.model def _prepare_estimate_data(self, line): return { - 'period_id': line.period_id.id, + 'date_range_id': line.date_range_id.id, 'product_id': line.product_id.id, 'location_id': line.location_id.id, 'product_uom_qty': line.product_uom_qty, @@ -129,7 +135,7 @@ class StockDemandEstimateSheetLine(models.TransientModel): _description = 'Stock Demand Estimate Sheet Line' estimate_id = fields.Many2one(comodel_name='stock.demand.estimate') - period_id = fields.Many2one( + date_range_id = fields.Many2one( comodel_name='stock.demand.estimate.period', string='Period') location_id = fields.Many2one(comodel_name='stock.location', @@ -146,8 +152,11 @@ class DemandEstimateWizard(models.TransientModel): _name = 'stock.demand.estimate.wizard' _description = 'Stock Demand Estimate Wizard' - date_from = fields.Date(string="Date From", required=True) - date_to = fields.Date(string="Date To", required=True) + date_start = fields.Date(string="Date From", required=True) + date_end = fields.Date(string="Date To", required=True) + date_range_type_id = fields.Many2one(string='Date Range Type', + comodel_name='date.range.type', + required=True) location_id = fields.Many2one(comodel_name="stock.location", string="Location", required=True) product_ids = fields.Many2many( @@ -158,8 +167,9 @@ class DemandEstimateWizard(models.TransientModel): def _prepare_demand_estimate_sheet(self): self.ensure_one() return { - 'date_from': self.date_from, - 'date_to': self.date_to, + 'date_start': self.date_start, + 'date_end': self.date_end, + 'date_range_type_id': self.date_range_type_id.id, 'location_id': self.location_id.id, } @@ -170,10 +180,11 @@ class DemandEstimateWizard(models.TransientModel): raise UserError(_('You must select at lease one product.')) context = { - 'date_from': self.date_from, - 'date_to': self.date_to, - 'location_id': self.location_id.id, - 'product_ids': self.product_ids.ids + 'default_date_start': self.date_start, + 'default_date_end': self.date_end, + 'default_date_range_type_id': self.date_range_type_id.id, + 'default_location_id': self.location_id.id, + 'default_product_ids': self.product_ids.ids } res = { 'context': context, diff --git a/stock_demand_estimate/wizards/stock_demand_estimate_wizard_view.xml b/stock_demand_estimate/wizards/stock_demand_estimate_wizard_view.xml index af5926770..5753a5a3b 100644 --- a/stock_demand_estimate/wizards/stock_demand_estimate_wizard_view.xml +++ b/stock_demand_estimate/wizards/stock_demand_estimate_wizard_view.xml @@ -10,11 +10,12 @@
- -
+ + @@ -47,10 +48,11 @@ - -
+