diff --git a/stock_demand_estimate/models/date_range.py b/stock_demand_estimate/models/date_range.py index 34321dc34..1f9b227d9 100644 --- a/stock_demand_estimate/models/date_range.py +++ b/stock_demand_estimate/models/date_range.py @@ -3,8 +3,6 @@ # 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): diff --git a/stock_demand_estimate/models/stock_demand_estimate.py b/stock_demand_estimate/models/stock_demand_estimate.py index 8afcd8770..e788ebddc 100644 --- a/stock_demand_estimate/models/stock_demand_estimate.py +++ b/stock_demand_estimate/models/stock_demand_estimate.py @@ -62,8 +62,8 @@ class StockDemandEstimate(models.Model): def name_get(self): res = [] for rec in self: - name = "%s - %s - %s" % (rec.date_range_id.name, rec.product_id.name, - rec.location_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 @@ -77,11 +77,12 @@ class StockDemandEstimate(models.Model): # 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): + 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/tests/test_stock_demand_estimate.py b/stock_demand_estimate/tests/test_stock_demand_estimate.py index b8d3b4c01..1f3336a92 100644 --- a/stock_demand_estimate/tests/test_stock_demand_estimate.py +++ b/stock_demand_estimate/tests/test_stock_demand_estimate.py @@ -4,6 +4,7 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). from openerp.tests import common from dateutil.rrule import MONTHLY +from openerp.exceptions import ValidationError class TestStockDemandEstimate(common.TransactionCase): @@ -78,13 +79,12 @@ class TestStockDemandEstimate(common.TransactionCase): sheets = self.env['stock.demand.estimate.sheet'].search([]) for sheet in sheets: - self.assertEquals(len(sheet.line_ids), 12, 'There should be 12 ' - 'lines.') - self.assertEquals(sheet.date_start, '1943-01-01', 'The date start ' - 'should be 1943-01-01') - self.assertEquals(sheet.date_end, '1943-12-31', 'The date end ' - 'should be ' - '1943-12-31') + self.assertEquals(len(sheet.line_ids), 12, + 'There should be 12 lines.') + self.assertEquals(sheet.date_start, '1943-01-01', + 'The date start should be 1943-01-01') + self.assertEquals(sheet.date_end, '1943-12-31', + 'The date end should be 1943-12-31') self.assertEquals(sheet.location_id.id, self.location.id, 'Wrong location') self.assertEquals(sheet.product_ids.ids, [self.product1.id], @@ -120,5 +120,16 @@ class TestStockDemandEstimate(common.TransactionCase): sheets = self.env['stock.demand.estimate.sheet'].search([]) for sheet in sheets: for line in sheet.line_ids: - self.assertEquals(line.product_uom_qty,1, + self.assertEquals(line.product_uom_qty, 1, 'The quantity should be 1') + + def test_invalid_dates(self): + + wiz = self.env['stock.demand.estimate.wizard'] + with self.assertRaises(ValidationError): + wiz.create({ + 'date_start': '1943-12-31', + 'date_end': '1943-01-01', + 'location_id': self.location.id, + 'date_range_type_id': self.drt_monthly.id, + 'product_ids': [(6, 0, [self.product1.id])]}) diff --git a/stock_demand_estimate/wizards/stock_demand_estimate_wizard.py b/stock_demand_estimate/wizards/stock_demand_estimate_wizard.py index b7f00a60e..67b8ae22e 100644 --- a/stock_demand_estimate/wizards/stock_demand_estimate_wizard.py +++ b/stock_demand_estimate/wizards/stock_demand_estimate_wizard.py @@ -6,33 +6,20 @@ from openerp import api, fields, models, _ import openerp.addons.decimal_precision as dp -from openerp.exceptions import Warning as UserError +from openerp.exceptions import ValidationError class StockDemandEstimateSheet(models.TransientModel): _name = 'stock.demand.estimate.sheet' _description = 'Stock Demand Estimate Sheet' - def _default_date_start(self): - return self.env.context.get('date_start', 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) - if location_id: - return self.env['stock.location'].browse(location_id) - else: - return False - def _default_estimate_ids(self): 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) + product_ids = self.env.context.get('product_ids', False) domain = [('type_id', '=', date_range_type_id), '|', '&', ('date_start', '>=', date_start), ('date_start', '<=', date_end), @@ -163,6 +150,13 @@ class DemandEstimateWizard(models.TransientModel): comodel_name="product.product", string="Products") + @api.one + @api.constrains('date_start', 'date_end') + def _check_start_end_dates(self): + if self.date_start > self.date_end: + raise ValidationError(_( + 'The start date cannot be later than the end date.')) + @api.multi def _prepare_demand_estimate_sheet(self): self.ensure_one() @@ -184,7 +178,7 @@ class DemandEstimateWizard(models.TransientModel): '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 + '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 5753a5a3b..64a046ba3 100644 --- a/stock_demand_estimate/wizards/stock_demand_estimate_wizard_view.xml +++ b/stock_demand_estimate/wizards/stock_demand_estimate_wizard_view.xml @@ -20,7 +20,8 @@
- +