| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 3 |
|
| 4 |
import datetime |
| 5 |
import dateutil.parser as dparser |
| 6 |
from re import findall as re_findall |
| 7 |
|
| 8 |
from odoo import api, fields, models |
| 9 |
from odoo.tools import get_lang |
| 10 |
|
| 11 |
|
| 12 |
class StockMove(models.Model): |
| 13 |
_inherit = "stock.move" |
| 14 |
|
| 15 |
use_expiration_date = fields.Boolean( |
| 16 |
string='Use Expiration Date', related='product_id.use_expiration_date') |
| 17 |
|
| 18 |
@api.model |
| 19 |
def action_generate_lot_line_vals(self, context_data, mode, first_lot, count, lot_text): |
| 20 |
vals_list = super().action_generate_lot_line_vals(context_data, mode, first_lot, count, lot_text) |
| 21 |
product = self.env['product.product'].browse(context_data.get('default_product_id')) |
| 22 |
picking = self.env['stock.picking'].browse(context_data.get('default_picking_id')) |
| 23 |
if product.use_expiration_date: |
| 24 |
from_date = picking.scheduled_date or fields.Datetime.today() |
| 25 |
expiration_date = from_date + datetime.timedelta(days=product.expiration_time) |
| 26 |
for vals in vals_list: |
| 27 |
vals['expiration_date'] = vals.get('expiration_date') or expiration_date |
| 28 |
return vals_list |
| 29 |
|
| 30 |
def _generate_serial_move_line_commands(self, field_data, location_dest_id=False, origin_move_line=None): |
| 31 |
"""Override to add a default `expiration_date` into the move lines values.""" |
| 32 |
move_lines_commands = super()._generate_serial_move_line_commands(field_data, location_dest_id, origin_move_line) |
| 33 |
if self.product_id.use_expiration_date: |
| 34 |
date = fields.Datetime.today() + datetime.timedelta(days=self.product_id.expiration_time) |
| 35 |
for move_line_command in move_lines_commands: |
| 36 |
move_line_vals = move_line_command[2] |
| 37 |
if 'expiration_date' not in move_line_vals: |
| 38 |
move_line_vals['expiration_date'] = date |
| 39 |
return move_lines_commands |
| 40 |
|
| 41 |
def _convert_string_into_field_data(self, string, options): |
| 42 |
res = super()._convert_string_into_field_data(string, options) |
| 43 |
if not res: |
| 44 |
try: |
| 45 |
datetime = dparser.parse(string, **options) |
| 46 |
if self and not self.use_expiration_date: |
| 47 |
# The datetime was correctly parsed but this move's product doesn't use expiration date. |
| 48 |
return "ignore" |
| 49 |
return {'expiration_date': datetime} |
| 50 |
except ValueError: |
| 51 |
pass |
| 52 |
return res |
| 53 |
|
| 54 |
def _get_formating_options(self, strings): |
| 55 |
options = super()._get_formating_options(strings) |
| 56 |
separators = "-/ " |
| 57 |
date_regex = f'[^{separators}]+' |
| 58 |
for string in strings: |
| 59 |
# Searches for a date. |
| 60 |
date_data = re_findall(date_regex, string) |
| 61 |
if len(date_data) < 2: # Not enough data. |
| 62 |
continue |
| 63 |
value_1, value_2 = date_data[:2] |
| 64 |
if re_findall('[a-zA-Z]', value_1): |
| 65 |
# Assumes the first value is the mounth (written in letters). Don't add any option |
| 66 |
# as mounth as the first date's value is the default behavior for `dateutil.parse`. |
| 67 |
break |
| 68 |
# Try to guess if the first data is the day or the year. |
| 69 |
if int(value_1) > 31: |
| 70 |
options['yearfirst'] = True |
| 71 |
break |
| 72 |
elif int(value_1) > 12 and (re_findall('[a-zA-Z]', value_2) or int(value_2) <= 12): |
| 73 |
options['dayfirst'] = True |
| 74 |
break |
| 75 |
else: # Too ambiguous, gets the option from the user's lang's date setting. |
| 76 |
user_lang_format = get_lang(self.env).date_format |
| 77 |
if re_findall('^%[mbB]', user_lang_format): # First parameter is for month. |
| 78 |
return options |
| 79 |
elif re_findall('^%[djaA]', user_lang_format): # First parameter is for day. |
| 80 |
options['dayfirst'] = True |
| 81 |
break |
| 82 |
elif re_findall('^%[yY]', user_lang_format): # First parameter is for year. |
| 83 |
options['yearfirst'] = True |
| 84 |
break |
| 85 |
return options |
| 86 |
|
| 87 |
def _update_reserved_quantity(self, need, location_id, lot_id=None, package_id=None, owner_id=None, strict=True): |
| 88 |
if self.product_id.use_expiration_date: |
| 89 |
return super(StockMove, self.with_context(with_expiration=self.date))._update_reserved_quantity(need, location_id, lot_id, package_id, owner_id, strict) |
| 90 |
return super()._update_reserved_quantity(need, location_id, lot_id, package_id, owner_id, strict) |
| 91 |
|
| 92 |
def _get_available_quantity(self, location_id, lot_id=None, package_id=None, owner_id=None, strict=False, allow_negative=False): |
| 93 |
if self.product_id.use_expiration_date: |
| 94 |
return super(StockMove, self.with_context(with_expiration=self.date))._get_available_quantity(location_id, lot_id, package_id, owner_id, strict, allow_negative) |
| 95 |
return super()._get_available_quantity(location_id, lot_id, package_id, owner_id, strict, allow_negative) |