Debian Codegraph @

addons/point_of_sale/wizard/pos_payment.py

raw
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 from odoo.tools import float_is_zero
6 from odoo.exceptions import UserError
7
8
9 class PosMakePayment(models.TransientModel):
10 _name = 'pos.make.payment'
11 _description = 'Point of Sale Make Payment Wizard'
12
13 def _default_config(self):
14 active_id = self.env.context.get('active_id')
15 if active_id:
16 return self.env['pos.order'].browse(active_id).session_id.config_id
17 return False
18
19 def _default_amount(self):
20 active_id = self.env.context.get('active_id')
21 if active_id:
22 order = self.env['pos.order'].browse(active_id)
23 amount_total = order.amount_total
24 # If we refund the entire order, we refund what was paid originally, else we refund the value of the items returned
25 if float_is_zero(order.refunded_order_id.amount_total + order.amount_total, precision_rounding=order.currency_id.rounding):
26 amount_total = -order.refunded_order_id.amount_paid
27 return amount_total - order.amount_paid
28 return False
29
30 def _default_payment_method(self):
31 active_id = self.env.context.get('active_id')
32 if active_id:
33 order_id = self.env['pos.order'].browse(active_id)
34 return order_id.session_id.payment_method_ids.sorted(lambda pm: pm.is_cash_count, reverse=True)[:1]
35 return False
36
37 config_id = fields.Many2one('pos.config', string='Point of Sale Configuration', required=True, default=_default_config)
38 amount = fields.Float(digits=0, required=True, default=_default_amount)
39 payment_method_id = fields.Many2one('pos.payment.method', string='Payment Method', required=True, default=_default_payment_method)
40 payment_name = fields.Char(string='Payment Reference')
41 payment_date = fields.Datetime(string='Payment Date', required=True, default=lambda self: fields.Datetime.now())
42
43 def check(self):
44 """Check the order:
45 if the order is not paid: continue payment,
46 if the order is paid print ticket.
47 """
48 self.ensure_one()
49
50 order = self.env['pos.order'].browse(self.env.context.get('active_id', False))
51 if self.payment_method_id.split_transactions and not order.partner_id:
52 raise UserError(_(
53 "Customer is required for %s payment method.",
54 self.payment_method_id.name
55 ))
56
57 currency = order.currency_id
58
59 init_data = self.read()[0]
60 payment_method = self.env['pos.payment.method'].browse(init_data['payment_method_id'][0])
61 if not float_is_zero(init_data['amount'], precision_rounding=currency.rounding):
62 order.add_payment({
63 'pos_order_id': order.id,
64 'amount': order._get_rounded_amount(init_data['amount'], payment_method.is_cash_count or not self.config_id.only_round_cash_method),
65 'name': init_data['payment_name'],
66 'payment_method_id': init_data['payment_method_id'][0],
67 })
68
69 if order.state == 'draft' and order._is_pos_order_paid():
70 order._process_saved_order(False)
71 if order.state in {'paid', 'done'}:
72 order._send_order()
73 order.config_id.notify_synchronisation(order.config_id.current_session_id.id, 0)
74 return {'type': 'ir.actions.act_window_close'}
75
76 return self.launch_payment()
77
78 def launch_payment(self):
79 return {
80 'name': _('Payment'),
81 'view_mode': 'form',
82 'res_model': 'pos.make.payment',
83 'view_id': False,
84 'target': 'new',
85 'views': False,
86 'type': 'ir.actions.act_window',
87 'context': self.env.context,
88 }