Debian Codegraph @

addons/product_expiry/models/production_lot.py

raw
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3 import datetime
4 from odoo import api, fields, models, SUPERUSER_ID, _
5
6
7 class StockLot(models.Model):
8 _inherit = 'stock.lot'
9
10 use_expiration_date = fields.Boolean(
11 string='Use Expiration Date', related='product_id.use_expiration_date')
12 expiration_date = fields.Datetime(
13 string='Expiration Date', compute='_compute_expiration_date', store=True, readonly=False,
14 help='This is the date on which the goods with this Serial Number may become dangerous and must not be consumed.')
15 use_date = fields.Datetime(string='Best before Date', compute='_compute_dates', store=True, readonly=False,
16 help='This is the date on which the goods with this Serial Number start deteriorating, without being dangerous yet.')
17 removal_date = fields.Datetime(string='Removal Date', compute='_compute_dates', store=True, readonly=False,
18 help='This is the date on which the goods with this Serial Number should be removed from the stock and not be counted in the Fresh On Hand Stock anymore. This date will be used in FEFO removal strategy.')
19 alert_date = fields.Datetime(string='Alert Date', compute='_compute_dates', store=True, readonly=False,
20 help='Date to determine the expired lots and serial numbers using the filter "Expiration Alerts".')
21 product_expiry_alert = fields.Boolean(compute='_compute_product_expiry_alert', help="The Expiration Date has been reached.")
22 product_expiry_reminded = fields.Boolean(string="Expiry has been reminded")
23
24 @api.depends('use_expiration_date', 'expiration_date', 'alert_date')
25 @api.depends_context('formatted_display_name')
26 def _compute_display_name(self):
27 lots_to_process_ids = []
28 for lot in self:
29 if lot.env.context.get('formatted_display_name') and lot.use_expiration_date and lot.expiration_date:
30 name = f"{lot.name}"
31 if fields.Datetime.now() >= lot.expiration_date:
32 name += self.env._("\t--Expired--")
33 elif lot.alert_date and fields.Datetime.now() >= lot.alert_date:
34 name += self.env._("\t--Expire on %(date)s--", date=fields.Datetime.to_string(lot.expiration_date))
35 lot.display_name = name
36 else:
37 lots_to_process_ids.append(lot.id)
38 if lots_to_process_ids:
39 super(StockLot, self.env['stock.lot'].browse(lots_to_process_ids))._compute_display_name()
40
41 @api.depends('expiration_date')
42 def _compute_product_expiry_alert(self):
43 current_date = fields.Datetime.now()
44 for lot in self:
45 if lot.expiration_date:
46 lot.product_expiry_alert = lot.expiration_date <= current_date
47 else:
48 lot.product_expiry_alert = False
49
50 @api.depends('product_id')
51 def _compute_expiration_date(self):
52 self.expiration_date = False
53 for lot in self:
54 if lot.product_id.use_expiration_date and not lot.expiration_date:
55 duration = lot.product_id.product_tmpl_id.expiration_time
56 lot.expiration_date = datetime.datetime.now() + datetime.timedelta(days=duration)
57
58 @api.depends('product_id', 'expiration_date')
59 def _compute_dates(self):
60 for lot in self:
61 if not lot.product_id.use_expiration_date:
62 lot.use_date = False
63 lot.removal_date = False
64 lot.alert_date = False
65 elif lot.expiration_date:
66 # when create
67 if lot.product_id != lot._origin.product_id or \
68 (not lot.use_date and not lot.removal_date and not lot.alert_date) or \
69 (lot.expiration_date and not lot._origin.expiration_date):
70 product_tmpl = lot.product_id.product_tmpl_id
71 lot.use_date = lot.expiration_date - datetime.timedelta(days=product_tmpl.use_time)
72 lot.removal_date = lot.expiration_date - datetime.timedelta(days=product_tmpl.removal_time)
73 lot.alert_date = lot.expiration_date - datetime.timedelta(days=product_tmpl.alert_time)
74 # when change
75 elif lot._origin.expiration_date:
76 time_delta = lot.expiration_date - lot._origin.expiration_date
77 lot.use_date = lot._origin.use_date and lot._origin.use_date + time_delta
78 lot.removal_date = lot._origin.removal_date and lot._origin.removal_date + time_delta
79 lot.alert_date = lot._origin.alert_date and lot._origin.alert_date + time_delta
80
81 @api.model
82 def _alert_date_exceeded(self):
83 """Log an activity on internally stored lots whose alert_date has been reached.
84
85 No further activity will be generated on lots whose alert_date
86 has already been reached (even if the alert_date is changed).
87 """
88 alert_lots = self.env['stock.lot'].search([
89 ('alert_date', '<=', fields.Date.today()),
90 ('product_expiry_reminded', '=', False)])
91
92 lot_stock_quants = self.env['stock.quant'].search([
93 ('lot_id', 'in', alert_lots.ids),
94 ('quantity', '>', 0),
95 ('location_id.usage', '=', 'internal')])
96 alert_lots = lot_stock_quants.mapped('lot_id')
97
98 for lot in alert_lots:
99 lot.activity_schedule(
100 'mail.mail_activity_data_todo',
101 user_id=lot.product_id.with_company(lot.company_id).responsible_id.id or lot.product_id.responsible_id.id or SUPERUSER_ID,
102 note=_("The alert date has been reached for this lot/serial number"),
103 summary=_("Alert Date Reached"),
104 )
105 alert_lots.write({
106 'product_expiry_reminded': True
107 })