Debian Codegraph @

addons/product_expiry/models/stock_move_line.py

raw
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 import datetime
5
6 from odoo import api, fields, models
7 from odoo.tools.sql import column_exists, create_column
8
9
10 class StockMoveLine(models.Model):
11 _inherit = "stock.move.line"
12
13 expiration_date = fields.Datetime(
14 string='Expiration Date', compute='_compute_expiration_date', store=True,
15 help='This is the date on which the goods with this Serial Number may'
16 ' become dangerous and must not be consumed.')
17 removal_date = fields.Datetime(string='Removal Date', compute='_compute_removal_date', store=True)
18 is_expired = fields.Boolean(related='lot_id.product_expiry_alert')
19 use_expiration_date = fields.Boolean(
20 string='Use Expiration Date', related='product_id.use_expiration_date')
21
22 def _auto_init(self):
23 """ Create column for 'expiration_date' here to avoid MemoryError when letting
24 the ORM compute it after module installation. Since both 'lot_id.expiration_date'
25 and 'product_id.use_expiration_date' are new fields introduced in this module,
26 there is no need for an UPDATE statement here.
27 """
28 if not column_exists(self.env.cr, "stock_move_line", "expiration_date"):
29 create_column(self.env.cr, "stock_move_line", "expiration_date", "timestamp")
30 if not column_exists(self.env.cr, "stock_move_line", "removal_date"):
31 create_column(self.env.cr, "stock_move_line", "removal_date", "timestamp")
32 return super()._auto_init()
33
34 @api.depends('product_id', 'lot_id.expiration_date', 'picking_id.scheduled_date')
35 def _compute_expiration_date(self):
36 for move_line in self:
37 if move_line.lot_id.expiration_date:
38 move_line.expiration_date = move_line.lot_id.expiration_date
39 elif move_line.picking_type_use_create_lots:
40 if move_line.product_id.use_expiration_date:
41 if not move_line.expiration_date:
42 from_date = move_line.picking_id.scheduled_date or fields.Datetime.today()
43 move_line.expiration_date = from_date + datetime.timedelta(days=move_line.product_id.expiration_time)
44 else:
45 move_line.expiration_date = False
46
47 @api.depends('product_id', 'expiration_date', 'lot_id.removal_date')
48 def _compute_removal_date(self):
49 for move_line in self:
50 if move_line.lot_id.removal_date:
51 move_line.removal_date = move_line.lot_id.removal_date
52 elif move_line.picking_type_use_create_lots:
53 if move_line.product_id.use_expiration_date and move_line.expiration_date:
54 move_line.removal_date = move_line.expiration_date - datetime.timedelta(days=move_line.product_id.removal_time)
55 else:
56 move_line.removal_date = False
57
58 def _prepare_new_lot_vals(self):
59 vals = super()._prepare_new_lot_vals()
60 if self.expiration_date:
61 vals['expiration_date'] = self.expiration_date
62 return vals