Debian Codegraph @

addons/stock_account/report/stock_valuation_report.py

raw symbols SCIP
1 from collections import defaultdict
2
3 from odoo import _, api, fields, models
4
5
6 class StockValuationReport(models.AbstractModel):
7 _name = 'stock_account.stock.valuation.report'
8 _description = 'Stock Valuation'
9
10 @api.model
11 def get_report_values(self, date=False):
12 return {
13 'data': self._get_report_data(date=date),
14 'context': {},
15 }
16
17 @api.model
18 def _get_report_values(self, docids, data=None):
19 docs = []
20 doc = self._get_report_data()
21 docs.append(self._include_pdf_specifics(doc, data))
22 return {
23 'doc_ids': docids,
24 'doc_model': 'stock.valuation.report',
25 'docs': docs,
26 }
27
28 def _get_report_data(self, date=False, product_category=False, warehouse=False):
29 company = self.env.company
30 # Check if date is a string instance
31 if isinstance(date, str):
32 date = fields.Date.from_string(date)
33 if date == fields.Date.today():
34 date = False
35 if not date:
36 inventory_data = company.stock_value()
37 accounting_data = company.stock_accounting_value()
38 else:
39 inventory_data = company.stock_value(at_date=date)
40 accounting_data = company.stock_accounting_value(at_date=date)
41
42 accounts = inventory_data.keys() | accounting_data.keys()
43 account_ids = {acc.id for acc in accounts}
44
45 initial_balance = {
46 'label': _("Initial Balance"),
47 'value': 0,
48 'lines_by_account_id': defaultdict(lambda: {
49 'value': 0,
50 'accounts': [],
51 }),
52 }
53 ending_stock = {
54 'label': _("Ending Stock"),
55 'value': 0,
56 'lines_by_account_id': defaultdict(lambda: {
57 'value': 0,
58 'accounts': [],
59 }),
60 }
61
62 # Compute Opening Balance values and Ending Stock values.
63 for account in accounts:
64 opening_balance = accounting_data.get(account, 0)
65 ending_balance = inventory_data.get(account, 0)
66 account_ids.add(account.id)
67 if opening_balance:
68 initial_balance['value'] += opening_balance
69 initial_balance['lines_by_account_id'][account.id]['value'] += opening_balance
70 if ending_balance:
71 ending_stock['value'] += ending_balance
72 ending_stock['lines_by_account_id'][account.id]['value'] += ending_balance
73
74 # Get accounting data.
75 accounts_by_product = company._get_accounts_by_product()
76 location_valuation_vals = company._get_location_valuation_vals(
77 date, location_domain=[('usage', '=', 'inventory')],
78 )
79 stock_valuation_account_vals = company._get_stock_valuation_account_vals(
80 accounts_by_product, date, company._get_location_valuation_vals(date))
81
82 report_data = {
83 'company_id': company.id,
84 'currency_id': company.currency_id.id,
85 'ending_stock': ending_stock,
86 'initial_balance': initial_balance,
87 }
88
89 if self._must_include_inventory_loss():
90 # Compute Inventory Loss values.
91 inventory_loss = {
92 'label': _("Inventory Loss"),
93 'value': 0,
94 }
95 lines_by_account_id = defaultdict(lambda: {
96 'debit': 0,
97 'credit': 0,
98 })
99 for vals in location_valuation_vals:
100 account_ids.add(vals['account_id'])
101 inventory_loss['value'] -= vals['debit']
102 lines_by_account_id[vals['account_id']]['debit'] += vals['debit']
103 lines_by_account_id[vals['account_id']]['credit'] += vals['credit']
104 inventory_loss['lines'] = [{
105 'account_id': account_id,
106 'debit': vals['debit'],
107 'credit': vals['credit'],
108 } for (account_id, vals) in lines_by_account_id.items()]
109 report_data['inventory_loss'] = inventory_loss
110
111 # Compute Stock Variation values.
112 stock_variation = {
113 'label': _("Stock Variation"),
114 'value': 0,
115 }
116 lines_by_account_id = defaultdict(lambda: {
117 'debit': 0,
118 'credit': 0,
119 'lines': [],
120 })
121 for vals in stock_valuation_account_vals:
122 account_ids.add(vals['account_id'])
123 stock_variation['value'] += vals['debit']
124 lines_by_account_id[vals['account_id']]['debit'] += vals['debit']
125 lines_by_account_id[vals['account_id']]['credit'] += vals['credit']
126 stock_variation['lines'] = [{
127 'account_id': account_id,
128 'debit': vals['debit'],
129 'credit': vals['credit'],
130 } for (account_id, vals) in lines_by_account_id.items()]
131
132 accounts_read_data = self.env['account.account'].search_read(
133 [('id', 'in', account_ids)],
134 ['id', 'name', 'code', 'display_name']
135 )
136 report_data.update(
137 accounts_by_id={acc_data['id']: acc_data for acc_data in accounts_read_data},
138 stock_variation=stock_variation,
139 )
140 return report_data
141
142 def action_print_as_pdf(self):
143 return
144
145 def action_print_as_xlsx(self):
146 return
147
148 def _must_include_inventory_loss(self):
149 return bool(self.env['stock.location'].search_count([
150 ('usage', '=', 'inventory'),
151 ('valuation_account_id', '!=', False),
152 ], limit=1))