diff --git a/modules/purchase_trade/pricing.py b/modules/purchase_trade/pricing.py index c25174d..f397d3f 100755 --- a/modules/purchase_trade/pricing.py +++ b/modules/purchase_trade/pricing.py @@ -15,6 +15,7 @@ from sql.functions import CurrentTimestamp, DateTrunc from trytond.wizard import Button, StateTransition, StateView, Wizard from itertools import chain, groupby from operator import itemgetter +import calendar import datetime import logging import re @@ -162,10 +163,8 @@ class ImportPrices(Wizard): if prices: price = prices[0] elif create_missing_price_index: - price, = Price.create([{ - 'price_index': price_index, - 'price_desc': price_index, - }]) + price, = Price.create([ + cls._price_index_values(price_index)]) stats['created_indexes'].append( cls._result_line( row_number, price_index, None, @@ -210,6 +209,70 @@ class ImportPrices(Wizard): return stats + @classmethod + def _price_index_values(cls, price_index): + values = { + 'price_index': price_index, + 'price_desc': price_index, + 'price_curve_type': 'future', + } + references = [ + ('price_type', 'price.fixtype', [('name', '=', 'Market price')]), + ('price_currency', 'currency.currency', [('code', '=', 'USD')]), + ('price_calendar', 'price.calendar', [('name', '=', 'Argus EU')]), + ('price_unit', 'product.uom', [('name', '=', 'Mt')]), + ] + for field, model_name, domain in references: + record = cls._first_record(model_name, domain) + if record: + values[field] = record.id + + period = cls._period_from_price_index(price_index) + if period: + values['price_period'] = period.id + return values + + @classmethod + def _period_from_price_index(cls, price_index): + match = re.search(r'(?