| 1 |
from collections import defaultdict |
| 2 |
|
| 3 |
from odoo import _, models |
| 4 |
|
| 5 |
|
| 6 |
class StockValuationReport(models.AbstractModel): |
| 7 |
_inherit = 'stock_account.stock.valuation.report' |
| 8 |
|
| 9 |
def _get_report_data(self, date=False, product_category=False, warehouse=False): |
| 10 |
report_data = super()._get_report_data(date=date, product_category=product_category, warehouse=warehouse) |
| 11 |
if not self._must_include_cost_of_production(): |
| 12 |
return report_data |
| 13 |
production_locations_valuation_vals = self.env.company._get_location_valuation_vals( |
| 14 |
location_domain=[('usage', '=', 'production')] |
| 15 |
) |
| 16 |
cost_of_production = { |
| 17 |
'label': _("Cost of Production"), |
| 18 |
'value': 0, |
| 19 |
} |
| 20 |
lines_by_account_id = defaultdict(lambda: { |
| 21 |
'debit': 0, |
| 22 |
'credit': 0, |
| 23 |
'lines': [], |
| 24 |
}) |
| 25 |
for vals in production_locations_valuation_vals: |
| 26 |
account = self.env['account.account'].browse(vals['account_id']) |
| 27 |
if account: |
| 28 |
account_vals = account.read(['name', 'code', 'display_name'])[0] |
| 29 |
report_data['accounts_by_id'][account.id] = account_vals |
| 30 |
cost_of_production['value'] -= vals['debit'] |
| 31 |
lines_by_account_id[account.id]['debit'] += vals['debit'] |
| 32 |
lines_by_account_id[account.id]['credit'] += vals['credit'] |
| 33 |
lines_by_account_id[account.id]['lines'].append(vals) |
| 34 |
cost_of_production['lines'] = [{ |
| 35 |
'account_id': account_id, |
| 36 |
'debit': vals['debit'], |
| 37 |
'credit': vals['credit'], |
| 38 |
} for (account_id, vals) in lines_by_account_id.items()] |
| 39 |
report_data['cost_of_production'] = cost_of_production |
| 40 |
return report_data |
| 41 |
|
| 42 |
def _must_include_cost_of_production(self): |
| 43 |
return bool(self.env['stock.location'].search_count([ |
| 44 |
('usage', '=', 'production'), |
| 45 |
('valuation_account_id', '!=', False), |
| 46 |
], limit=1)) |