diff --git a/stock_cycle_count/models/stock_cycle_count.py b/stock_cycle_count/models/stock_cycle_count.py index 89220e116..cdc332e6e 100644 --- a/stock_cycle_count/models/stock_cycle_count.py +++ b/stock_cycle_count/models/stock_cycle_count.py @@ -57,7 +57,7 @@ class StockCycleCount(models.Model): company_id = fields.Many2one( comodel_name='res.company', string='Company', required=True, - default='_default_company', + default=_default_company, readonly=True, ) diff --git a/stock_cycle_count/models/stock_cycle_count_rule.py b/stock_cycle_count/models/stock_cycle_count_rule.py index 8ddebf202..5ed28d01d 100644 --- a/stock_cycle_count/models/stock_cycle_count_rule.py +++ b/stock_cycle_count/models/stock_cycle_count_rule.py @@ -45,7 +45,7 @@ class StockCycleCountRule(models.Model): 'warehouse.') ) - @api.onchange('rule_type') + @api.depends('rule_type') def _compute_rule_description(self): if self.rule_type == 'periodic': self.rule_description = _('Ensures that at least a defined number ' @@ -153,16 +153,16 @@ class StockCycleCountRule(models.Model): def _compute_rule_periodic(self, locs): cycle_counts = [] for loc in locs: - last_inventories = self.env['stock.inventory'].search([ + latest_inventory_date = self.env['stock.inventory'].search([ ('location_id', '=', loc.id), - ('state', 'in', ['confirm', 'done', 'draft'])]).mapped('date') - if last_inventories: - latest_inventory = sorted(last_inventories, reverse=True)[0] + ('state', 'in', ['confirm', 'done', 'draft'])], + order="date desc", limit=1).date + if latest_inventory_date: try: period = self.periodic_count_period / \ self.periodic_qty_per_period next_date = datetime.strptime( - latest_inventory, + latest_inventory_date, DEFAULT_SERVER_DATETIME_FORMAT) + timedelta( days=period) if next_date < datetime.today(): diff --git a/stock_cycle_count/models/stock_move.py b/stock_cycle_count/models/stock_move.py index 184f427d8..87b83371e 100644 --- a/stock_cycle_count/models/stock_move.py +++ b/stock_cycle_count/models/stock_move.py @@ -12,6 +12,5 @@ class StockMove(models.Model): @api.multi def action_done(self): super(StockMove, self).action_done() - for move in self: - move.location_id.check_zero_confirmation() + self.mapped("location_id").check_zero_confirmation() return True diff --git a/stock_cycle_count/models/stock_warehouse.py b/stock_cycle_count/models/stock_warehouse.py index 884d7c261..1b777d217 100644 --- a/stock_cycle_count/models/stock_warehouse.py +++ b/stock_cycle_count/models/stock_warehouse.py @@ -66,6 +66,16 @@ class StockWarehouse(models.Model): ('rule_type', '!=', 'zero'), ('warehouse_ids', 'in', self.ids)]) return rules + @api.model + def _prepare_cycle_count(self, cycle_count_proposed): + return { + 'date_deadline': cycle_count_proposed['date'], + 'location_id': cycle_count_proposed['location'].id, + 'cycle_count_rule_id': cycle_count_proposed[ + 'rule_type'].id, + 'state': 'draft' + } + @api.multi def action_compute_cycle_count_rules(self): """ Apply the rule in all the sublocations of a given warehouse(s) and @@ -110,13 +120,9 @@ class StockWarehouse(models.Model): DEFAULT_SERVER_DATETIME_FORMAT) - datetime.today() if not existing_cycle_counts and \ delta.days < rec.cycle_count_planning_horizon: - self.env['stock.cycle.count'].create({ - 'date_deadline': cycle_count_proposed['date'], - 'location_id': cycle_count_proposed['location'].id, - 'cycle_count_rule_id': cycle_count_proposed[ - 'rule_type'].id, - 'state': 'draft' - }) + cc_vals = self._prepare_cycle_count( + cycle_count_proposed) + self.env['stock.cycle.count'].create(cc_vals) @api.model def cron_cycle_count(self):