Debian Codegraph @

addons/product_expiry/models/stock_quant.py

raw
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 from odoo import api, fields, models
5
6
7 class StockQuant(models.Model):
8 _inherit = 'stock.quant'
9
10 expiration_date = fields.Datetime(related='lot_id.expiration_date', store=True)
11 removal_date = fields.Datetime(related='lot_id.removal_date', store=True)
12 use_expiration_date = fields.Boolean(related='product_id.use_expiration_date')
13 available_quantity = fields.Float(help="On hand quantity which hasn't been reserved on a transfer and is still fresh, in the default unit of measure of the product")
14
15 def _get_gs1_barcode(self, gs1_quantity_rules_ai_by_uom=False):
16 barcode = super()._get_gs1_barcode(gs1_quantity_rules_ai_by_uom)
17 if self.use_expiration_date:
18 if self.lot_id.expiration_date:
19 barcode = '17' + self.lot_id.expiration_date.strftime('%y%m%d') + barcode
20 if self.lot_id.use_date:
21 barcode = '15' + self.lot_id.use_date.strftime('%y%m%d') + barcode
22 return barcode
23
24 @api.model
25 def _get_removal_strategy_order(self, removal_strategy):
26 if removal_strategy == 'fefo':
27 return 'removal_date, in_date, id'
28 return super()._get_removal_strategy_order(removal_strategy)
29
30 @api.depends('removal_date')
31 def _compute_available_quantity(self):
32 super()._compute_available_quantity()
33 current_date = fields.Datetime.now()
34 for quant in self:
35 if quant.use_expiration_date and quant.removal_date and quant.removal_date <= current_date:
36 quant.available_quantity = 0
37
38 def _set_view_context(self):
39 self_with_context = self
40 if self.env.context.get('default_product_id') and self.env['product.product'].browse(self.env.context.get('default_product_id')).use_expiration_date:
41 self_with_context = self.with_context(show_removal_date=True)
42 return super(StockQuant, self_with_context)._set_view_context()