Debian Codegraph @

addons/stock_landed_costs/models/account_move.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 api, fields, models
5
6
7 class AccountMove(models.Model):
8 _inherit = 'account.move'
9
10 landed_costs_ids = fields.One2many('stock.landed.cost', 'vendor_bill_id', string='Landed Costs')
11 landed_costs_visible = fields.Boolean(compute='_compute_landed_costs_visible')
12
13 @api.depends('line_ids', 'line_ids.is_landed_costs_line')
14 def _compute_landed_costs_visible(self):
15 for account_move in self:
16 if account_move.landed_costs_ids:
17 account_move.landed_costs_visible = False
18 else:
19 account_move.landed_costs_visible = any(line.is_landed_costs_line for line in account_move.line_ids)
20
21 def button_create_landed_costs(self):
22 """Create a `stock.landed.cost` record associated to the account move of `self`, each
23 `stock.landed.costs` lines mirroring the current `account.move.line` of self.
24 """
25 self.ensure_one()
26 landed_costs_lines = self.line_ids.filtered(lambda line: line.is_landed_costs_line)
27
28 sign = -1 if self.move_type in ['in_refund'] else 1
29 landed_costs = self.env['stock.landed.cost'].with_company(self.company_id).create({
30 'vendor_bill_id': self.id,
31 'cost_lines': [(0, 0, {
32 'product_id': l.product_id.id,
33 'name': l.product_id.name,
34 'account_id': l.product_id.product_tmpl_id.get_product_accounts()['stock_valuation'].id,
35 'price_unit': sign * l.currency_id._convert(l.price_subtotal, l.company_currency_id, l.company_id, self.invoice_date or fields.Date.context_today(l)),
36 'split_method': l.product_id.split_method_landed_cost or 'equal',
37 }) for l in landed_costs_lines],
38 })
39 action = self.env["ir.actions.actions"]._for_xml_id("stock_landed_costs.action_stock_landed_cost")
40 return dict(action, view_mode='form', res_id=landed_costs.id, views=[(False, 'form')])
41
42 def action_view_landed_costs(self):
43 self.ensure_one()
44
45 views = [(False, 'form')] if len(self.landed_costs_ids) == 1 else [
46 (self.env.ref('stock_landed_costs.view_stock_landed_cost_tree2').id, 'list'), (False, 'form'), (False, 'kanban')
47 ]
48 return self.landed_costs_ids.with_context(
49 default_vendor_bill_id=self.id
50 )._get_records_action(name=self.env._("Landed Costs"), views=views)
51
52 def _update_order_line_info(self, product_id, quantity, **kwargs):
53 price_unit = super()._update_order_line_info(product_id, quantity, **kwargs)
54 move_line = self.line_ids.filtered(lambda line: line.product_id.id == product_id)
55 if move_line:
56 move_line.is_landed_costs_line = move_line.product_id.landed_cost_ok
57 return price_unit
58
59
60 class AccountMoveLine(models.Model):
61 _inherit = 'account.move.line'
62
63 product_type = fields.Selection(related='product_id.type', readonly=True)
64 is_landed_costs_line = fields.Boolean()
65
66 @api.onchange('product_id')
67 def _onchange_product_id_landed_costs(self):
68 if self.product_id.landed_cost_ok:
69 self.is_landed_costs_line = True
70 else:
71 self.is_landed_costs_line = False
72
73 @api.onchange('is_landed_costs_line')
74 def _onchange_is_landed_costs_line(self):
75 if self.is_landed_costs_line and self.product_id and self.product_type != 'service':
76 self.is_landed_costs_line = False
77
78 def _eligible_for_stock_account(self):
79 return super()._eligible_for_stock_account() or (
80 self.product_id.type == "service"
81 and self.product_id.landed_cost_ok
82 and self.product_id.valuation == "real_time"
83 )