Debian Codegraph @

addons/stock_account/models/account_move.py

raw symbols SCIP
1 from odoo import fields, models
2 from odoo.tools import float_is_zero
3
4
5 class AccountMove(models.Model):
6 _inherit = 'account.move'
7
8 stock_move_ids = fields.One2many('stock.move', 'account_move_id', string='Stock Move')
9
10 # -------------------------------------------------------------------------
11 # OVERRIDE METHODS
12 # -------------------------------------------------------------------------
13
14 def _get_lines_onchange_currency(self):
15 # OVERRIDE
16 return self.line_ids.filtered(lambda l: l.display_type != 'cogs')
17
18 def copy_data(self, default=None):
19 # Don't keep anglo-saxon lines when copying a journal entry.
20 vals_list = super().copy_data(default=default)
21
22 if not self.env.context.get('move_reverse_cancel'):
23 for vals in vals_list:
24 if 'line_ids' in vals:
25 vals['line_ids'] = [line_vals for line_vals in vals['line_ids']
26 if line_vals[0] != 0 or line_vals[2].get('display_type') != 'cogs']
27 return vals_list
28
29 def _post(self, soft=True):
30 # OVERRIDE
31
32 # Don't change anything on moves used to cancel another ones.
33 if self.env.context.get('move_reverse_cancel'):
34 return super()._post(soft)
35
36 # Create additional COGS lines for customer invoices.
37 self.env['account.move.line'].create(self._stock_account_prepare_realtime_out_lines_vals())
38
39 # Post entries.
40 res = super()._post(soft)
41
42 self.line_ids._get_stock_moves().filtered(lambda m: m.is_in)._set_value()
43
44 return res
45
46 def button_draft(self):
47 res = super().button_draft()
48
49 # Unlink the COGS lines generated during the 'post' method.
50 with self.env.protecting(self.env['account.move']._get_protected_vals({}, self)):
51 self.mapped('line_ids').filtered(lambda line: line.display_type == 'cogs').unlink()
52 return res
53
54 def button_cancel(self):
55 # OVERRIDE
56 res = super().button_cancel()
57
58 # Unlink the COGS lines generated during the 'post' method.
59 # In most cases it shouldn't be necessary since they should be unlinked with 'button_draft'.
60 # However, since it can be called in RPC, better be safe.
61 self.mapped('line_ids').filtered(lambda line: line.display_type == 'cogs').unlink()
62 return res
63
64 # -------------------------------------------------------------------------
65 # COGS METHODS
66 # -------------------------------------------------------------------------
67
68 def _stock_account_prepare_realtime_out_lines_vals(self):
69 ''' Prepare values used to create the journal items (account.move.line) corresponding to the Cost of Good Sold
70 lines (COGS) for customer invoices.
71
72 Example:
73
74 Buy a product having a cost of 9 being a storable product and having a perpetual valuation in FIFO.
75 Sell this product at a price of 10. The customer invoice's journal entries looks like:
76
77 Account | Debit | Credit
78 ---------------------------------------------------------------
79 200000 Product Sales | | 10.0
80 ---------------------------------------------------------------
81 101200 Account Receivable | 10.0 |
82 ---------------------------------------------------------------
83
84 This method computes values used to make two additional journal items:
85
86 ---------------------------------------------------------------
87 500000 COGS (stock variation) | 9.0 |
88 ---------------------------------------------------------------
89 110100 Stock Account | | 9.0
90 ---------------------------------------------------------------
91
92 Note: COGS are only generated for customer invoices except refund made to cancel an invoice.
93
94 :return: A list of Python dictionary to be passed to env['account.move.line'].create.
95 '''
96 lines_vals_list = []
97
98 price_unit_prec = self.env['decimal.precision'].precision_get('Product Price')
99 for move in self:
100
101 # Make the loop multi-company safe when accessing models like product.product
102 move = move.with_company(move.company_id)
103
104 if not move.is_sale_document(include_receipts=True):
105 continue
106
107 anglo_saxon_price_ctx = move._get_anglo_saxon_price_ctx()
108
109 for line in move.invoice_line_ids:
110 # Filter out lines being not eligible for COGS.
111 if not line._eligible_for_stock_account() or line.product_id.valuation != 'real_time':
112 continue
113 # Retrieve accounts needed to generate the COGS.
114 accounts = line.product_id.product_tmpl_id.get_product_accounts(fiscal_pos=move.fiscal_position_id)
115 stock_account = accounts['stock_valuation']
116 credit_expense_account = accounts['expense'] or move.journal_id.default_account_id
117 if not stock_account or not credit_expense_account:
118 continue
119
120 # Compute accounting fields.
121 sign = -1 if move.move_type == 'out_refund' else 1
122 price_unit = line.with_context(anglo_saxon_price_ctx)._get_cogs_value()
123 amount_currency = sign * line.product_uom_id._compute_quantity(line.quantity, line.product_id.uom_id) * price_unit
124
125 if move.currency_id.is_zero(amount_currency) or float_is_zero(price_unit, precision_digits=price_unit_prec):
126 continue
127
128 # Add interim account line.
129 lines_vals_list.append({
130 'name': line.name[:64] if line.name else '',
131 'move_id': move.id,
132 'partner_id': move.commercial_partner_id.id,
133 'product_id': line.product_id.id,
134 'product_uom_id': line.product_uom_id.id,
135 'quantity': line.quantity,
136 'price_unit': price_unit,
137 'amount_currency': -amount_currency,
138 'account_id': stock_account.id,
139 'display_type': 'cogs',
140 'tax_ids': [],
141 'cogs_origin_id': line.id,
142 })
143
144 # Add expense account line.
145 lines_vals_list.append({
146 'name': line.name[:64] if line.name else '',
147 'move_id': move.id,
148 'partner_id': move.commercial_partner_id.id,
149 'product_id': line.product_id.id,
150 'product_uom_id': line.product_uom_id.id,
151 'quantity': line.quantity,
152 'price_unit': -price_unit,
153 'amount_currency': amount_currency,
154 'account_id': credit_expense_account.id,
155 'analytic_distribution': line.analytic_distribution,
156 'display_type': 'cogs',
157 'tax_ids': [],
158 'cogs_origin_id': line.id,
159 })
160 return lines_vals_list
161
162 def _get_anglo_saxon_price_ctx(self):
163 """ To be overriden in modules overriding _get_cogs_value
164 to optimize computations that only depend on account.move and not account.move.line
165 """
166 return self.env.context
167
168 def _get_related_stock_moves(self):
169 return self.env['stock.move']
170
171 def _get_invoiced_lot_values(self):
172 return []