[10.0][IMP] stock_cycle_count:

* simplify fetching latest inventory date
* use api.depends where needed
* add hook for cycle count creation
* take advantage of api.multi on check_zero_confirmation method
This commit is contained in:
Lois Rilo
2018-03-23 16:28:51 +01:00
committed by Lois Rilo
parent f0d840f729
commit e821d9c55d
4 changed files with 21 additions and 16 deletions

View File

@@ -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,
)

View File

@@ -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():

View File

@@ -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

View File

@@ -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):