Skip to content

Commit

Permalink
[10.0][IMP] stock_cycle_count:
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
LoisRForgeFlow committed Mar 26, 2018
1 parent 1062159 commit d5a9498
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion stock_cycle_count/models/stock_cycle_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _default_company(self):
company_id = fields.Many2one(
comodel_name='res.company', string='Company',
required=True,
default='_default_company',
default=_default_company,
readonly=True,
)

Expand Down
12 changes: 6 additions & 6 deletions stock_cycle_count/models/stock_cycle_count_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _check_zero_rule(self):
'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 '
Expand Down Expand Up @@ -153,16 +153,16 @@ def _propose_cycle_count(self, date, location):
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():
Expand Down
3 changes: 1 addition & 2 deletions stock_cycle_count/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 13 additions & 7 deletions stock_cycle_count/models/stock_warehouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ def _cycle_count_rules_to_compute(self):
('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
Expand Down Expand Up @@ -110,13 +120,9 @@ def action_compute_cycle_count_rules(self):
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):
Expand Down

0 comments on commit d5a9498

Please sign in to comment.