diff --git a/__manifest__.py b/__manifest__.py index 55503856593281476731400f611be984791942c5..e01de0f59c055dc6932beb589f4b38d5d002c358 100755 --- a/__manifest__.py +++ b/__manifest__.py @@ -12,10 +12,8 @@ 'fumoc_partner', ], 'data': [ - "security/ir.model.access.csv", # datas # views - 'views/fumoc_lot_prefix_dlc.xml', 'views/product_views.xml', 'views/res_config_settings.xml', 'views/sale_order.xml', diff --git a/models/__init__.py b/models/__init__.py index 5f0e12d26fe86835b95af3add3d99643edf0408f..1b456f61267a9a6dff635a46cef812dbb2ce07dd 100644 --- a/models/__init__.py +++ b/models/__init__.py @@ -2,7 +2,6 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from . import account_move -from . import fumoc_lot_prefix_dlc from . import product from . import res_company from . import res_config_settings diff --git a/models/fumoc_lot_prefix_dlc.py b/models/fumoc_lot_prefix_dlc.py deleted file mode 100644 index 2e28a3b6aeac97bce6fd006d9db6ce23603217b4..0000000000000000000000000000000000000000 --- a/models/fumoc_lot_prefix_dlc.py +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright 2021 Le Filament (<http://www.le-filament.com>) -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - -from odoo import fields, models - - -class FumocLotPrefixDLC(models.Model): - _name = 'fumoc.lot.prefix.dlc' - _description = 'Correspondance préfix des lots et DLC' - - name = fields.Char('Descrition') - prefix = fields.Char('Préfixe', required=True) - dlc = fields.Integer('DLC') - - _sql_constraints = [ - ('unique_prefix', 'unique(prefix)', - 'Le préfixe doit être unique') - ] diff --git a/models/stock_production_lot.py b/models/stock_production_lot.py index def2a5de84221be3be7118a118d9accd1588ef0a..d51e07a1e750a4e9d850397ab5e54d7039287376 100644 --- a/models/stock_production_lot.py +++ b/models/stock_production_lot.py @@ -3,7 +3,7 @@ import datetime import re -from odoo import models, api +from odoo import models, fields, api from odoo.exceptions import UserError @@ -13,53 +13,50 @@ class FumocStockProductionLot(models.Model): # ------------------------------------------------------ # Business method # ------------------------------------------------------ - def compute_expiration_date(self): - """ - Check if prefix is known in conf + if suffix is readable - -> compute expiration date and other dates if needed - """ - if not self.env.user.company_id.year_reference_lot: - raise UserError('L\'année de référence pour les lots n\'a pas été ' - 'configurée !') - prefix = re.findall('([a-zA-Z ]*)\d*.*', self.name)[0] - existing_prefix = self.env['fumoc.lot.prefix.dlc'].search([ - ('prefix', '=', prefix) - ]) - suffix = re.findall('([\d]+)\D*', self.name)[-1] - if len(suffix) >= 4: - readable_suffix = suffix[-4:] - else: - readable_suffix = False - if existing_prefix and readable_suffix: - dlc = existing_prefix.dlc - year_ref = self.env.user.company_id.year_reference_lot - year_index = int(readable_suffix[:1]) - year = year_ref + year_index - quantieme = int(readable_suffix[1:]) - production_date = datetime.datetime(year, 1, 1) + \ - datetime.timedelta(days=quantieme-1) - expiration_date = production_date + datetime.timedelta(days=dlc) - time_delta = expiration_date - self.expiration_date - vals = self._get_date_values(time_delta) - vals['expiration_date'] = expiration_date - self.update(vals) + def _get_dates(self, product_id=None, lotname=None): + """Replaces existing method.""" + mapped_fields = { + 'expiration_date': 'expiration_time', + 'use_date': 'use_time', + 'removal_date': 'removal_time', + 'alert_date': 'alert_time' + } + res = dict.fromkeys(mapped_fields, False) + product = self.env['product.product'].browse(product_id) or self.product_id + lot_name = lotname or self.name + if product and lot_name: + if not self.env.user.company_id.year_reference_lot: + raise UserError('L\'année de référence pour les lots n\'a pas été ' + 'configurée !') + suffix = re.findall('([\d]+)\D*', lot_name)[-1] + if len(suffix) >= 4: + readable_suffix = suffix[-4:] + year_ref = self.env.user.company_id.year_reference_lot + year_index = int(readable_suffix[:1]) + year = year_ref + year_index + quantieme = int(readable_suffix[1:]) + production_date = datetime.datetime(year, 1, 1) + \ + datetime.timedelta(days=quantieme-1) + for field in mapped_fields: + duration = getattr(product, mapped_fields[field]) + if duration: + date = production_date + datetime.timedelta(days=duration) + res[field] = fields.Datetime.to_string(date) + return res + + # Assign dates according to products data + @api.model_create_multi + def create(self, vals_list): + for vals in vals_list: + dates = self._get_dates(vals.get('product_id') or self.env.context.get('default_product_id'), vals.get('name') or self.env.context.get('default_name')) + for d in dates: + if not vals.get(d): + vals[d] = dates[d] + return super().create(vals_list) - # ------------------------------------------------------ - # Onchange - # ------------------------------------------------------ @api.onchange('name') def _onchange_name(self): - if self.create_date: - self.compute_expiration_date() + dates_dict = self._get_dates() + for field, value in dates_dict.items(): + setattr(self, field, value) - # ------------------------------------------------------ - # Override parent - # ------------------------------------------------------ - @api.model_create_multi - def create(self, vals_list): - """ - Override create to call compute_expiration_date on created lot - """ - res = super(FumocStockProductionLot, self).create(vals_list) - res.compute_expiration_date() - return res diff --git a/security/ir.model.access.csv b/security/ir.model.access.csv deleted file mode 100755 index 87b8d85c573dd78885a5aa4db19ffe60e1a3bfd3..0000000000000000000000000000000000000000 --- a/security/ir.model.access.csv +++ /dev/null @@ -1,3 +0,0 @@ -id,name,model_id/id,group_id/id,perm_read,perm_write,perm_create,perm_unlink -"access_fumoc_lot_prefix_dlc_group_partner_manager","fumoc_lot_prefix_dlc group_partner_manager","model_fumoc_lot_prefix_dlc","stock.group_stock_manager",1,1,1,1 -"access_fumoc_lot_prefix_dlc_group_user","fumoc_lot_prefix_dlc group_user","model_fumoc_lot_prefix_dlc","base.group_user",1,0,0,0 diff --git a/views/fumoc_lot_prefix_dlc.xml b/views/fumoc_lot_prefix_dlc.xml deleted file mode 100644 index 1f97681eb0d8d1d28bc289ed2b35f6c146a3d724..0000000000000000000000000000000000000000 --- a/views/fumoc_lot_prefix_dlc.xml +++ /dev/null @@ -1,28 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<odoo> - <data> - - <record id="action_fumoc_lot_prefix_dlc" model="ir.actions.act_window"> - <field name="name">Lots/DLC</field> - <field name="type">ir.actions.act_window</field> - <field name="res_model">fumoc.lot.prefix.dlc</field> - <field name="view_mode">tree</field> - </record> - - <record id="fumoc_lot_prefix_dlc_view_tree" model="ir.ui.view"> - <field name="name">fumoc_lot_prefix_dlc_view_tree</field> - <field name="model">fumoc.lot.prefix.dlc</field> - <field name="arch" type="xml"> - <tree editable="top" string="Lots / DLC"> - <field name="name"/> - <field name="prefix"/> - <field name="dlc"/> - </tree> - </field> - </record> - - <menuitem id="menu_fumoc_lot_prefix_dlc" name="Lots / DLC" parent="stock.menu_stock_config_settings" - sequence="10" action="action_fumoc_lot_prefix_dlc"/> - - </data> -</odoo>