Debian Codegraph @

addons/point_of_sale/models/account_move.py

raw
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
6
7 class AccountMove(models.Model):
8 _name = 'account.move'
9 _inherit = ['account.move', 'pos.load.mixin']
10
11 pos_order_ids = fields.One2many('pos.order', 'account_move')
12 pos_payment_ids = fields.One2many('pos.payment', 'account_move_id')
13 pos_refunded_invoice_ids = fields.Many2many('account.move', 'refunded_invoices', 'refund_account_move', 'original_account_move')
14 reversed_pos_order_id = fields.Many2one('pos.order', string="Reversed POS Order",
15 index='btree_not_null',
16 help="The pos order that was reverted after closing the session to create an invoice for it.")
17 pos_session_ids = fields.One2many("pos.session", "move_id", "POS Sessions")
18 pos_order_count = fields.Integer(compute="_compute_origin_pos_count", string='POS Order Count')
19
20 @api.depends('pos_order_ids')
21 def _compute_origin_pos_count(self):
22 for move in self:
23 move.pos_order_count = len(move.sudo().pos_order_ids)
24
25 @api.depends('tax_cash_basis_created_move_ids', 'pos_session_ids')
26 def _compute_always_tax_exigible(self):
27 super()._compute_always_tax_exigible()
28 # The pos closing move does not create caba entries (anymore); we set the tax values directly on the closing move.
29 # (But there may still be old closing moves that used caba entries from previous versions.)
30 for move in self:
31 if move.always_tax_exigible or move.tax_cash_basis_created_move_ids:
32 continue
33 if move.pos_session_ids:
34 move.always_tax_exigible = True
35
36 def _stock_account_get_last_step_stock_moves(self):
37 stock_moves = super(AccountMove, self)._stock_account_get_last_step_stock_moves()
38 for invoice in self.filtered(lambda x: x.move_type == 'out_invoice'):
39 stock_moves += invoice.sudo().mapped('pos_order_ids.picking_ids.move_ids').filtered(lambda x: x.state == 'done' and x.location_dest_id.usage == 'customer')
40 for invoice in self.filtered(lambda x: x.move_type == 'out_refund'):
41 stock_moves += invoice.sudo().mapped('pos_order_ids.picking_ids.move_ids').filtered(lambda x: x.state == 'done' and x.location_id.usage == 'customer')
42 return stock_moves
43
44
45 def _get_invoiced_lot_values(self):
46 self.ensure_one()
47
48 lot_values = super(AccountMove, self)._get_invoiced_lot_values()
49
50 if self.state == 'draft':
51 return lot_values
52
53 # user may not have access to POS orders, but it's ok if they have
54 # access to the invoice
55 for order in self.sudo().pos_order_ids:
56 for line in order.lines:
57 lots = line.pack_lot_ids or False
58 if lots:
59 for lot in lots:
60 lot_values.append({
61 'product_name': lot.product_id.name,
62 'quantity': line.qty if lot.product_id.tracking == 'lot' else 1.0,
63 'uom_name': line.product_uom_id.name,
64 'lot_name': lot.lot_name,
65 'pos_lot_id': lot.id,
66 })
67
68 return lot_values
69
70 def _compute_payments_widget_reconciled_info(self):
71 """Add pos_payment_name field in the reconciled vals to be able to show the payment method in the invoice."""
72 super()._compute_payments_widget_reconciled_info()
73 for move in self:
74 if move.invoice_payments_widget:
75 if move.state == 'posted' and move.is_invoice(include_receipts=True):
76 reconciled_partials = move._get_all_reconciled_invoice_partials()
77 for i, reconciled_partial in enumerate(reconciled_partials):
78 counterpart_line = reconciled_partial['aml']
79 pos_payment = counterpart_line.move_id.sudo().pos_payment_ids[:1]
80 move.invoice_payments_widget['content'][i].update({
81 'pos_payment_name': pos_payment.payment_method_id.name,
82 })
83
84 def _compute_amount(self):
85 super()._compute_amount()
86 for move in self:
87 if move.move_type == 'entry' and move.reversed_pos_order_id:
88 move.amount_total_signed = move.amount_total_signed * -1
89
90 def _compute_tax_totals(self):
91 return super(AccountMove, self.with_context(linked_to_pos=bool(self.sudo().pos_order_ids)))._compute_tax_totals()
92
93 def _compute_is_storno(self):
94 # EXTENDS 'account'
95 super()._compute_is_storno()
96 for move in self:
97 move.is_storno = move.is_storno or (
98 move.company_id.account_storno and move.reversed_pos_order_id
99 )
100
101 def action_view_source_pos_orders(self):
102 self.ensure_one()
103 action = self.env['ir.actions.act_window']._for_xml_id('point_of_sale.action_pos_pos_form')
104
105 if len(self.pos_order_ids) == 1:
106 action['views'] = [(self.env.ref('point_of_sale.view_pos_pos_form', False).id, 'form')]
107 action['res_id'] = self.pos_order_ids.id
108 else:
109 action['domain'] = [('id', 'in', self.pos_order_ids.ids)]
110 return action
111
112 def button_draft(self):
113 if self.sudo().pos_order_ids.filtered(lambda o: o.session_id.state != 'closed'):
114 self.env.user._bus_send("simple_notification", {
115 'type': 'danger',
116 'message': _("You can't reset this invoice to draft because the POS session is still open. Please close the ongoing session first, then try again."),
117 'sticky': True,
118 })
119 return False
120 return super().button_draft()
121
122 @api.model
123 def _load_pos_data_fields(self, config):
124 result = super()._load_pos_data_fields(config)
125 return result or ['id', 'name']
126
127 @api.model
128 def _load_pos_data_domain(self, data, config):
129 return False
130
131 class AccountMoveLine(models.Model):
132 _inherit = 'account.move.line'
133
134 def _get_cogs_value(self):
135 self.ensure_one()
136 if not self.product_id:
137 return self.price_unit
138 price_unit = super()._get_cogs_value()
139 sudo_order = self.move_id.sudo().pos_order_ids
140 if sudo_order:
141 price_unit = sudo_order._get_pos_anglo_saxon_price_unit(self.product_id, self.move_id.partner_id.id, self.quantity)
142 return price_unit
143
144 def _compute_name(self):
145 amls = self.filtered(lambda l: not l.move_id.pos_session_ids)
146 super(AccountMoveLine, amls)._compute_name()