Debian Codegraph @

addons/product_expiry/models/stock_picking.py

raw
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3 import datetime
4
5 from odoo import models, _
6
7
8 class StockPicking(models.Model):
9 _inherit = "stock.picking"
10
11 def _pre_action_done_hook(self):
12 res = super()._pre_action_done_hook()
13 # We use the 'skip_expired' context key to avoid to make the check when
14 # user did already confirmed the wizard about expired lots.
15 if res is True and not self.env.context.get('skip_expired'):
16 pickings_to_warn_expired = self._check_expired_lots()
17 if pickings_to_warn_expired:
18 return pickings_to_warn_expired._action_generate_expired_wizard()
19 return res
20
21 def _check_expired_lots(self):
22 expired_pickings = self.move_line_ids.filtered(lambda ml: ml.lot_id.product_expiry_alert or (ml.removal_date and ml.removal_date <= datetime.datetime.now())).picking_id
23 return expired_pickings
24
25 def _action_generate_expired_wizard(self):
26 expired_lot_ids = self.move_line_ids.filtered(lambda ml: ml.lot_id.product_expiry_alert or (ml.removal_date and ml.removal_date <= datetime.datetime.now())).lot_id.ids
27 view_id = self.env.ref('product_expiry.confirm_expiry_view').id
28 context = dict(self.env.context)
29
30 context.update({
31 'default_picking_ids': [(6, 0, self.ids)],
32 'default_lot_ids': [(6, 0, expired_lot_ids)],
33 })
34 return {
35 'name': _('Confirmation'),
36 'type': 'ir.actions.act_window',
37 'res_model': 'expiry.picking.confirmation',
38 'view_mode': 'form',
39 'views': [(view_id, 'form')],
40 'view_id': view_id,
41 'target': 'new',
42 'context': context,
43 }