Debian Codegraph @

addons/purchase_stock/models/account_invoice.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 from odoo import fields, models, api
5 from odoo.tools import float_compare, float_is_zero
6 from odoo.tools.misc import unique
7
8
9 class AccountMove(models.Model):
10 _inherit = 'account.move'
11
12 def _stock_account_prepare_anglo_saxon_in_lines_vals(self):
13 ''' Prepare values used to create the journal items (account.move.line) corresponding to the price difference
14 lines for vendor bills. It only concerns the quantities that have been delivered before the bill
15 Example:
16 Buy a product having a cost of 9 and a supplier price of 10 and being a storable product and having a perpetual
17 valuation in FIFO. Deliver the product and then post the bill. The vendor bill's journal entries looks like:
18
19 Account | Debit | Credit
20 ---------------------------------------------------------------
21 101120 Stock Account | 10.0 |
22 ---------------------------------------------------------------
23 101100 Account Payable | | 10.0
24 ---------------------------------------------------------------
25 This method computes values used to make two additional journal items:
26 ---------------------------------------------------------------
27 101120 Stock Account | | 1.0
28 ---------------------------------------------------------------
29 xxxxxx Expenses | 1.0 |
30 ---------------------------------------------------------------
31 :return: A list of Python dictionary to be passed to env['account.move.line'].create.
32 '''
33 lines_vals_list = []
34 price_unit_prec = self.env['decimal.precision'].precision_get('Product Price')
35
36 for move in self:
37 if move.move_type not in ('in_invoice', 'in_refund', 'in_receipt') or not move.company_id.anglo_saxon_accounting:
38 continue
39
40 move = move.with_company(move.company_id)
41 for line in move.invoice_line_ids:
42 # Filter out lines being not eligible for price difference.
43 # Moreover, this function is used for standard cost method only.
44 if not line._eligible_for_stock_account() or line.product_id.cost_method != 'standard':
45 continue
46
47 # Retrieve accounts needed to generate the price difference.
48
49 debit_pdiff_account = False
50 if line.product_id.cost_method == 'standard':
51 debit_pdiff_account = line.product_id.categ_id.property_price_difference_account_id
52 debit_pdiff_account = move.fiscal_position_id.map_account(debit_pdiff_account)
53 else:
54 debit_pdiff_account = line.product_id.product_tmpl_id.get_product_accounts(fiscal_pos=move.fiscal_position_id)['expense']
55 if not debit_pdiff_account:
56 continue
57
58 price_unit_val_dif, relevant_qty = line._get_price_unit_val_dif_and_relevant_qty()
59 price_subtotal = relevant_qty * price_unit_val_dif
60
61 # We consider there is a price difference if the subtotal is not zero. In case a
62 # discount has been applied, we can't round the price unit anymore, and hence we
63 # can't compare them.
64 if (
65 not move.currency_id.is_zero(price_subtotal)
66 and float_compare(line["price_unit"], line.price_unit, precision_digits=price_unit_prec) == 0
67 ):
68
69 # Add price difference account line.
70 vals = {
71 'name': line.name[:64],
72 'move_id': move.id,
73 'partner_id': line.partner_id.id or move.commercial_partner_id.id,
74 'product_id': line.product_id.id,
75 'product_uom_id': line.product_uom_id.id,
76 'quantity': relevant_qty,
77 'balance': line.currency_id._convert(
78 relevant_qty * price_unit_val_dif,
79 line.company_currency_id,
80 line.company_id, fields.Date.today(),
81 ),
82 'account_id': debit_pdiff_account.id,
83 'analytic_distribution': line.analytic_distribution,
84 'display_type': 'cogs',
85 }
86 lines_vals_list.append(vals)
87
88 # Correct the amount of the current line.
89 vals = {
90 'name': line.name[:64],
91 'move_id': move.id,
92 'partner_id': line.partner_id.id or move.commercial_partner_id.id,
93 'product_id': line.product_id.id,
94 'product_uom_id': line.product_uom_id.id,
95 'quantity': relevant_qty,
96 'balance': line.currency_id._convert(
97 relevant_qty * -price_unit_val_dif,
98 line.company_currency_id,
99 line.company_id, fields.Date.today(),
100 ),
101 'account_id': line.account_id.id,
102 'analytic_distribution': line.analytic_distribution,
103 'display_type': 'cogs',
104 }
105 lines_vals_list.append(vals)
106 return lines_vals_list
107
108 def button_draft(self):
109 return super().button_draft()
110
111 def _post(self, soft=True):
112 if not self.env.context.get('move_reverse_cancel'):
113 self.env['account.move.line'].create(self._stock_account_prepare_anglo_saxon_in_lines_vals())
114
115 return super()._post(soft)
116
117 def _stock_account_get_last_step_stock_moves(self):
118 """ Overridden from stock_account.
119 Returns the stock moves associated to this invoice."""
120 rslt = super()._stock_account_get_last_step_stock_moves()
121 for invoice in self.filtered(lambda x: x.move_type == 'in_invoice'):
122 rslt += invoice.mapped('invoice_line_ids.purchase_line_id.move_ids').filtered(lambda x: x.state == 'done' and x.location_id.usage == 'supplier')
123 for invoice in self.filtered(lambda x: x.move_type == 'in_refund'):
124 rslt += invoice.mapped('invoice_line_ids.purchase_line_id.move_ids').filtered(lambda x: x.state == 'done' and x.location_dest_id.usage == 'supplier')
125 return rslt
126
127 @api.depends('purchase_id')
128 def _compute_incoterm_location(self):
129 super()._compute_incoterm_location()
130 for move in self:
131 purchase_locations = move.line_ids.purchase_line_id.order_id.mapped('incoterm_location')
132 incoterm_res = next((incoterm for incoterm in purchase_locations if incoterm), False)
133 # if multiple purchase order we take an incoterm that is not false
134 if incoterm_res:
135 move.incoterm_location = incoterm_res