Debian Codegraph @

addons/product_expiry/wizard/confirm_expiry.py

raw
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3 from datetime import datetime
4
5 from odoo import api, fields, models, _
6
7
8 class ExpiryPickingConfirmation(models.TransientModel):
9 _name = 'expiry.picking.confirmation'
10 _description = 'Confirm Expiry'
11
12 lot_ids = fields.Many2many('stock.lot', readonly=True, required=True)
13 picking_ids = fields.Many2many('stock.picking', readonly=True)
14 description = fields.Char('Description', compute='_compute_descriptive_fields')
15 show_lots = fields.Boolean('Show Lots', compute='_compute_descriptive_fields')
16
17 @api.depends('lot_ids')
18 def _compute_descriptive_fields(self):
19 # Shows expired lots only if we are more than one expired lot.
20 self.show_lots = len(self.lot_ids) > 1
21 if self.show_lots:
22 # For multiple expired lots, they are listed in the wizard view.
23 self.description = _(
24 "You are going to deliver some product expired lots."
25 "\nDo you confirm you want to proceed?"
26 )
27 else:
28 # For one expired lot, its name is written in the wizard message.
29 self.description = _(
30 "You are going to deliver the product %(product_name)s, %(lot_name)s which is expired or should at least be removed from stock."
31 "\nDo you confirm you want to proceed?",
32 product_name=self.lot_ids.product_id.display_name,
33 lot_name=self.lot_ids.name
34 )
35
36 def process(self):
37 picking_to_validate = self.env.context.get('button_validate_picking_ids')
38 if picking_to_validate:
39 picking_to_validate = self.env['stock.picking'].browse(picking_to_validate)
40 ctx = dict(self.env.context, skip_expired=True)
41 ctx.pop('default_lot_ids')
42 return picking_to_validate.with_context(ctx).button_validate()
43 return True
44
45 def process_no_expired(self):
46 """ Remove the expired mls and confirm the picking. """
47 pickings_to_validate = self.env['stock.picking'].browse(self.env.context.get('button_validate_picking_ids'))
48 self.picking_ids.move_line_ids.filtered(
49 lambda ml: ml.use_expiration_date and ml.removal_date and ml.removal_date < datetime.now()
50 ).unlink()
51 return pickings_to_validate.button_validate()