From 296746ce6e6e2c7a1dc24848d66b5d554f379122 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Fri, 13 May 2022 17:25:05 +0200 Subject: [PATCH] stock_valuation_xlsx: restore past cost price support, using stock valuation layers Don't replace the native menu entry any more. Code refactoring between the 2 wizards Improve multi-company support --- stock_valuation_xlsx/__manifest__.py | 2 + .../wizard/stock_valuation_xlsx.py | 61 ++++++++++-------- .../wizard/stock_valuation_xlsx_view.xml | 11 ++-- .../wizard/stock_variation_xlsx.py | 62 ++----------------- .../wizard/stock_variation_xlsx_view.xml | 4 +- 5 files changed, 49 insertions(+), 91 deletions(-) diff --git a/stock_valuation_xlsx/__manifest__.py b/stock_valuation_xlsx/__manifest__.py index 5771885..7c11092 100644 --- a/stock_valuation_xlsx/__manifest__.py +++ b/stock_valuation_xlsx/__manifest__.py @@ -13,6 +13,8 @@ Stock Valuation XLSX ==================== +This module is designed to work with *Cost Method* = **Average Cost (AVCO)**. + This module generate nice XLSX stock valuation reports either: * from a physical inventory, diff --git a/stock_valuation_xlsx/wizard/stock_valuation_xlsx.py b/stock_valuation_xlsx/wizard/stock_valuation_xlsx.py index edd9dd7..98a3a39 100644 --- a/stock_valuation_xlsx/wizard/stock_valuation_xlsx.py +++ b/stock_valuation_xlsx/wizard/stock_valuation_xlsx.py @@ -137,33 +137,40 @@ class StockValuationXlsx(models.TransientModel): logger.debug('depreciation_rules=%s', rules) return rules - def compute_product_data( - self, company_id, in_stock_product_ids, standard_price_past_date=False): - self.ensure_one() - logger.debug('Start compute_product_data') - ppo = self.env['product.product'] + @api.model + def compute_product_data(self, company_id, filter_product_ids, standard_price_dict): + # standard_price_dict is a dictionnary with: + # keys = the keys that we expect in the result dict + # values : a datetime object (for past date) or False (False means PRESENT) + logger.debug( + 'Start compute_product_data standard_price_dict=%s', standard_price_dict) + ppo = self.env['product.product'].with_company(company_id) + svlo = self.env['stock.valuation.layer'] fields_list = self._prepare_product_fields() - # if not standard_price_past_date: # TODO - if True: + # Do we need the present date? + if not all(standard_price_dict.values()): fields_list.append('standard_price') - products = ppo.search_read([('id', 'in', in_stock_product_ids)], fields_list) + products = ppo.search_read([('id', 'in', filter_product_ids)], fields_list) product_id2data = {} for p in products: logger.debug('p=%d', p['id']) - if standard_price_past_date: - # No more product.price.history on v14 - # We are supposed to use stock.valuation.layer.revaluation - # TODO migrate to stock.valuation.layer.revaluation - #history = ppho.search_read([ - # ('company_id', '=', company_id), - # ('product_id', '=', p['id']), - # ('datetime', '<=', standard_price_past_date)], - # ['cost'], order='datetime desc, id desc', limit=1) - #standard_price = history and history[0]['cost'] or 0.0 - standard_price = p['standard_price'] # TODO remove this tmp stuff - else: - standard_price = p['standard_price'] - product_id2data[p['id']] = {'standard_price': standard_price} + product_id2data[p['id']] = {} + for std_price_field_name, std_price_date in standard_price_dict.items(): + if not std_price_date: # present + product_id2data[p['id']][std_price_field_name] = p['standard_price'] + else: + layer_rg = svlo.read_group( + [ + ('product_id', '=', p['id']), + ('company_id', '=', company_id), + ('create_date', '<=', std_price_date), + ], + ['value', 'quantity'], + []) + standard_price = 0 + if layer_rg and layer_rg[0]['quantity']: + standard_price = layer_rg[0]['value'] / layer_rg[0]['quantity'] + product_id2data[p['id']][std_price_field_name] = standard_price for pfield in fields_list: if pfield.endswith('_id'): product_id2data[p['id']][pfield] = p[pfield][0] @@ -381,9 +388,13 @@ class StockValuationXlsx(models.TransientModel): elif self.source == 'inventory': past_date = self.inventory_id.date data, in_stock_products = self.compute_data_from_inventory(product_ids, prec_qty) - standard_price_past_date = past_date - if not (self.source == 'stock' and self.stock_date_type == 'present') and self.standard_price_date == 'present': + if self.source == 'stock' and self.stock_date_type == 'present': standard_price_past_date = False + else: # field standard_price_date is shown on screen + if self.standard_price_date == 'present': + standard_price_past_date = False + else: + standard_price_past_date = past_date depreciation_rules = [] if apply_depreciation: depreciation_rules = self._prepare_expiry_depreciation_rules(company_id, past_date) @@ -394,7 +405,7 @@ class StockValuationXlsx(models.TransientModel): in_stock_product_ids = list(in_stock_products.keys()) product_id2data = self.compute_product_data( company_id, in_stock_product_ids, - standard_price_past_date=standard_price_past_date) + {'standard_price': standard_price_past_date}) data_res = self.group_result(data, split_by_lot, split_by_location) categ_id2name = self.product_categ_id2name(self.categ_ids) uom_id2name = self.uom_id2name() diff --git a/stock_valuation_xlsx/wizard/stock_valuation_xlsx_view.xml b/stock_valuation_xlsx/wizard/stock_valuation_xlsx_view.xml index 7d9cf4c..55f892e 100644 --- a/stock_valuation_xlsx/wizard/stock_valuation_xlsx_view.xml +++ b/stock_valuation_xlsx/wizard/stock_valuation_xlsx_view.xml @@ -41,17 +41,14 @@ - Stock Valuation XLSX + Inventory Valuation XLSX stock.valuation.xlsx form new - - - - Stock Valuation XLSX - 0 - + + diff --git a/stock_valuation_xlsx/wizard/stock_variation_xlsx.py b/stock_valuation_xlsx/wizard/stock_variation_xlsx.py index fa63fca..03c2515 100644 --- a/stock_valuation_xlsx/wizard/stock_variation_xlsx.py +++ b/stock_valuation_xlsx/wizard/stock_variation_xlsx.py @@ -106,59 +106,6 @@ class StockVariationXlsx(models.TransientModel): products = self.env['product.product'].search(domain) return products.ids - def _prepare_product_fields(self): - return ['uom_id', 'name', 'default_code', 'categ_id'] - - def compute_product_data( - self, company_id, filter_product_ids, - standard_price_start_date=False, standard_price_end_date=False): - self.ensure_one() - logger.debug('Start compute_product_data') - ppo = self.env['product.product'] - fields_list = self._prepare_product_fields() - # if not standard_price_start_date or not standard_price_end_date: # TODO - if True: - fields_list.append('standard_price') - products = ppo.search_read([('id', 'in', filter_product_ids)], fields_list) - product_id2data = {} - for p in products: - logger.debug('p=%d', p['id']) - if standard_price_start_date: - # No more product.price.history on v14 - # We are supposed to use stock.valuation.layer.revaluation - # TODO migrate to stock.valuation.layer.revaluation - #history = ppho.search_read([ - # ('company_id', '=', company_id), - # ('product_id', '=', p['id']), - # ('datetime', '<=', standard_price_start_date)], - # ['cost'], order='datetime desc, id desc', limit=1) - #start_standard_price = history and history[0]['cost'] or 0.0 - start_standard_price = p['standard_price'] # TODO remove this tmp stuff - else: - start_standard_price = p['standard_price'] - if standard_price_end_date: - #history = ppho.search_read([ - # ('company_id', '=', company_id), - # ('product_id', '=', p['id']), - # ('datetime', '<=', standard_price_end_date)], - # ['cost'], order='datetime desc, id desc', limit=1) - #end_standard_price = history and history[0]['cost'] or 0.0 - end_standard_price = p['standard_price'] # TODO remove this tmp stuff - else: - end_standard_price = p['standard_price'] - - product_id2data[p['id']] = { - 'start_standard_price': start_standard_price, - 'end_standard_price': end_standard_price, - } - for pfield in fields_list: - if pfield.endswith('_id'): - product_id2data[p['id']][pfield] = p[pfield][0] - else: - product_id2data[p['id']][pfield] = p[pfield] - logger.debug('End compute_product_data') - return product_id2data - def compute_data_from_stock(self, product_ids, prec_qty, start_date, end_date_type, end_date, company_id): self.ensure_one() logger.debug('Start compute_data_from_stock past_date=%s end_date_type=%s, end_date=%s', start_date, end_date_type, end_date) @@ -273,12 +220,13 @@ class StockVariationXlsx(models.TransientModel): standard_price_start_date = standard_price_end_date = False if self.standard_price_start_date_type == 'start': standard_price_start_date = self.start_date - if self.standard_price_end_date_type == 'end': + if self.standard_price_end_date_type == 'end' and self.end_date_type == 'past': standard_price_end_date = self.end_date - product_id2data = self.compute_product_data( - company_id, list(product_data.keys()), - standard_price_start_date, standard_price_end_date) + product_id2data = svxo.compute_product_data( + company_id, list(product_data.keys()), { + 'start_standard_price': standard_price_start_date, + 'end_standard_price': standard_price_end_date}) categ_id2name = svxo.product_categ_id2name(self.categ_ids) uom_id2name = svxo.uom_id2name() res = self.stringify_and_sort_result( diff --git a/stock_valuation_xlsx/wizard/stock_variation_xlsx_view.xml b/stock_valuation_xlsx/wizard/stock_variation_xlsx_view.xml index 9ac60aa..923bdf2 100644 --- a/stock_valuation_xlsx/wizard/stock_variation_xlsx_view.xml +++ b/stock_valuation_xlsx/wizard/stock_variation_xlsx_view.xml @@ -43,13 +43,13 @@ - Stock Variation XLSX + Inventory Variation XLSX stock.variation.xlsx form new - +