| 1 |
from odoo import api, fields, models, _ |
| 2 |
from odoo.exceptions import UserError, ValidationError |
| 3 |
|
| 4 |
|
| 5 |
class PosPaymentMethod(models.Model): |
| 6 |
_name = 'pos.payment.method' |
| 7 |
_description = "Point of Sale Payment Methods" |
| 8 |
_order = "sequence, id" |
| 9 |
_inherit = ['pos.load.mixin'] |
| 10 |
|
| 11 |
def _get_payment_terminal_selection(self): |
| 12 |
return [] |
| 13 |
|
| 14 |
def _get_payment_method_type(self): |
| 15 |
selection = [('none', self.env._("None required")), ('terminal', self.env._("Terminal"))] |
| 16 |
if self.env['res.partner.bank'].get_available_qr_methods_in_sequence(): |
| 17 |
selection.append(('qr_code', self.env._("Bank App (QR Code)"))) |
| 18 |
return selection |
| 19 |
|
| 20 |
name = fields.Char(string="Method", required=True, translate=True, help='Defines the name of the payment method that will be displayed in the Point of Sale when the payments are selected.') |
| 21 |
sequence = fields.Integer(copy=False) |
| 22 |
outstanding_account_id = fields.Many2one('account.account', |
| 23 |
string='Outstanding Account', |
| 24 |
ondelete='restrict', |
| 25 |
help='Account used as outstanding account when creating accounting payment records for bank payments.') |
| 26 |
receivable_account_id = fields.Many2one('account.account', |
| 27 |
string='Intermediary Account', |
| 28 |
ondelete='restrict', |
| 29 |
domain=[('reconcile', '=', True), ('account_type', '=', 'asset_receivable')], |
| 30 |
help="Leave empty to use the default account from the company setting.\n" |
| 31 |
"Overrides the company's receivable account (for Point of Sale) used in the journal entries.") |
| 32 |
is_cash_count = fields.Boolean(string='Cash', compute="_compute_is_cash_count", store=True) |
| 33 |
journal_id = fields.Many2one('account.journal', |
| 34 |
string='Journal', |
| 35 |
domain=['|', '&', ('type', '=', 'cash'), ('pos_payment_method_ids', '=', False), ('type', '=', 'bank')], |
| 36 |
ondelete='restrict', |
| 37 |
index='btree_not_null', |
| 38 |
check_company=True, |
| 39 |
help='Leave empty to use the receivable account of customer.\n' |
| 40 |
'Defines the journal where to book the accumulated payments (or individual payment if Identify Customer is true) after closing the session.\n' |
| 41 |
'For cash journal, we directly write to the default account in the journal via statement lines.\n' |
| 42 |
'For bank journal, we write to the outstanding account specified in this payment method.\n' |
| 43 |
'Only cash and bank journals are allowed.') |
| 44 |
split_transactions = fields.Boolean( |
| 45 |
string='Identify Customer', |
| 46 |
default=False, |
| 47 |
help='Forces to set a customer when using this payment method and splits the journal entries for each customer. It could slow down the closing process.') |
| 48 |
open_session_ids = fields.Many2many('pos.session', string='Pos Sessions', compute='_compute_open_session_ids', help='Open PoS sessions that are using this payment method.') |
| 49 |
config_ids = fields.Many2many('pos.config', string='Point of Sale') |
| 50 |
company_id = fields.Many2one('res.company', string='Company', default=lambda self: self.env.company) |
| 51 |
default_pos_receivable_account_name = fields.Char(related="company_id.account_default_pos_receivable_account_id.display_name", string="Default Receivable Account Name") |
| 52 |
use_payment_terminal = fields.Selection(selection=lambda self: self._get_payment_terminal_selection(), string='Use a Payment Terminal', help='Record payments with a terminal on this journal.') |
| 53 |
# used to hide use_payment_terminal when no payment interfaces are installed |
| 54 |
hide_use_payment_terminal = fields.Boolean(compute='_compute_hide_use_payment_terminal') |
| 55 |
active = fields.Boolean(default=True) |
| 56 |
type = fields.Selection(selection=[('cash', 'Cash'), ('bank', 'Bank'), ('pay_later', 'Customer Account')], compute="_compute_type") |
| 57 |
image = fields.Image("Image", max_width=50, max_height=50) |
| 58 |
payment_method_type = fields.Selection(selection=lambda self: self._get_payment_method_type(), string="Integration", default='none', required=True) |
| 59 |
default_qr = fields.Char(compute='_compute_qr') |
| 60 |
qr_code_method = fields.Selection( |
| 61 |
string='QR Code Format', copy=False, |
| 62 |
selection=lambda self: self.env['res.partner.bank'].get_available_qr_methods_in_sequence(), |
| 63 |
help='Type of QR-code to be generated for this payment method.', |
| 64 |
) |
| 65 |
hide_qr_code_method = fields.Boolean(compute='_compute_hide_qr_code_method') |
| 66 |
|
| 67 |
@api.model |
| 68 |
def get_provider_status(self, modules_list): |
| 69 |
return { |
| 70 |
'state': self.env['ir.module.module'].search_read([('name', 'in', modules_list)], ['name', 'state']), |
| 71 |
} |
| 72 |
|
| 73 |
@api.model |
| 74 |
def _load_pos_data_domain(self, data, config): |
| 75 |
return ['|', ('active', '=', False), ('active', '=', True)] |
| 76 |
|
| 77 |
@api.model |
| 78 |
def _load_pos_data_fields(self, config): |
| 79 |
return ['id', 'name', 'is_cash_count', 'use_payment_terminal', 'split_transactions', 'type', 'image', 'sequence', 'payment_method_type', 'default_qr'] |
| 80 |
|
| 81 |
@api.depends('type', 'payment_method_type') |
| 82 |
def _compute_hide_use_payment_terminal(self): |
| 83 |
no_terminals = not bool(self._fields['use_payment_terminal'].selection(self)) |
| 84 |
for payment_method in self: |
| 85 |
payment_method.hide_use_payment_terminal = no_terminals or payment_method.type in ('cash', 'pay_later') or payment_method.payment_method_type != 'terminal' |
| 86 |
|
| 87 |
@api.depends('payment_method_type') |
| 88 |
def _compute_hide_qr_code_method(self): |
| 89 |
for payment_method in self: |
| 90 |
payment_method.hide_qr_code_method = payment_method.payment_method_type != 'qr_code' or len(self.env['res.partner.bank'].get_available_qr_methods_in_sequence()) == 1 |
| 91 |
|
| 92 |
@api.onchange('payment_method_type') |
| 93 |
def _onchange_payment_method_type(self): |
| 94 |
# We don't display the field if there is only one option and cannot set a default on it |
| 95 |
if self.payment_method_type == 'none': |
| 96 |
self.use_payment_terminal = False |
| 97 |
|
| 98 |
selection_options = self.env['res.partner.bank'].get_available_qr_methods_in_sequence() |
| 99 |
if len(selection_options) == 1: |
| 100 |
self.qr_code_method = selection_options[0][0] |
| 101 |
|
| 102 |
@api.onchange('use_payment_terminal') |
| 103 |
def _onchange_use_payment_terminal(self): |
| 104 |
"""Used by inheriting model to unset the value of the field related to the unselected payment terminal.""" |
| 105 |
pass |
| 106 |
|
| 107 |
@api.depends('config_ids') |
| 108 |
def _compute_open_session_ids(self): |
| 109 |
for payment_method in self: |
| 110 |
payment_method.open_session_ids = self.env['pos.session'].search([('config_id', 'in', payment_method.config_ids.ids), ('state', '!=', 'closed')]) |
| 111 |
|
| 112 |
@api.depends('journal_id', 'split_transactions') |
| 113 |
def _compute_type(self): |
| 114 |
for pm in self: |
| 115 |
if pm.journal_id.type in {'cash', 'bank'}: |
| 116 |
pm.type = pm.journal_id.type |
| 117 |
else: |
| 118 |
pm.type = 'pay_later' |
| 119 |
|
| 120 |
@api.onchange('journal_id') |
| 121 |
def _onchange_journal_id(self): |
| 122 |
for pm in self: |
| 123 |
if pm.journal_id and pm.journal_id.type not in ['cash', 'bank']: |
| 124 |
raise UserError(_("Only journals of type 'Cash' or 'Bank' could be used with payment methods.")) |
| 125 |
if pm.journal_id and pm.journal_id.type == 'bank': |
| 126 |
chart_template = self.with_context(allowed_company_ids=self.env.company.root_id.ids).env['account.chart.template'] |
| 127 |
pm.outstanding_account_id = chart_template.ref('account_journal_payment_debit_account_id', raise_if_not_found=False) or self.company_id.transfer_account_id |
| 128 |
if self.is_cash_count: |
| 129 |
self.use_payment_terminal = False |
| 130 |
|
| 131 |
@api.depends('type') |
| 132 |
def _compute_is_cash_count(self): |
| 133 |
for pm in self: |
| 134 |
pm.is_cash_count = pm.type == 'cash' |
| 135 |
|
| 136 |
def _is_write_forbidden(self, fields): |
| 137 |
whitelisted_fields = {'sequence'} |
| 138 |
return bool(fields - whitelisted_fields and self.open_session_ids) |
| 139 |
|
| 140 |
@api.model_create_multi |
| 141 |
def create(self, vals_list): |
| 142 |
for vals in vals_list: |
| 143 |
if vals.get('payment_method_type', False): |
| 144 |
self._force_payment_method_type_values(vals, vals['payment_method_type']) |
| 145 |
return super().create(vals_list) |
| 146 |
|
| 147 |
def write(self, vals): |
| 148 |
if self._is_write_forbidden(set(vals.keys())): |
| 149 |
raise UserError(_('Please close and validate the following open PoS Sessions before modifying this payment method.\n' |
| 150 |
'Open sessions: %s', (' '.join(self.open_session_ids.mapped('name')),))) |
| 151 |
|
| 152 |
if 'payment_method_type' in vals: |
| 153 |
self._force_payment_method_type_values(vals, vals['payment_method_type']) |
| 154 |
return super().write(vals) |
| 155 |
|
| 156 |
pmt_terminal = self.filtered(lambda pm: pm.payment_method_type == 'terminal') |
| 157 |
pmt_qr = self.filtered(lambda pm: pm.payment_method_type == 'qr_code') |
| 158 |
not_pmt = self - pmt_terminal - pmt_qr |
| 159 |
|
| 160 |
res = True |
| 161 |
forced_vals = vals.copy() |
| 162 |
if pmt_terminal: |
| 163 |
self._force_payment_method_type_values(forced_vals, 'terminal', True) |
| 164 |
res = super(PosPaymentMethod, pmt_terminal).write(forced_vals) and res |
| 165 |
if pmt_qr: |
| 166 |
self._force_payment_method_type_values(forced_vals, 'qr_code', True) |
| 167 |
res = super(PosPaymentMethod, pmt_qr).write(forced_vals) and res |
| 168 |
if not_pmt: |
| 169 |
res = super(PosPaymentMethod, not_pmt).write(vals) and res |
| 170 |
|
| 171 |
return res |
| 172 |
|
| 173 |
@staticmethod |
| 174 |
def _force_payment_method_type_values(vals, payment_method_type, if_present=False): |
| 175 |
if payment_method_type == 'terminal': |
| 176 |
disabled_fields_name = ['qr_code_method'] |
| 177 |
elif payment_method_type == 'qr_code': |
| 178 |
disabled_fields_name = ['use_payment_terminal'] |
| 179 |
else: |
| 180 |
disabled_fields_name = ['use_payment_terminal', 'qr_code_method'] |
| 181 |
if if_present: |
| 182 |
for name in disabled_fields_name: |
| 183 |
if name in vals: |
| 184 |
vals[name] = False |
| 185 |
else: |
| 186 |
for name in disabled_fields_name: |
| 187 |
vals[name] = False |
| 188 |
|
| 189 |
def copy_data(self, default=None): |
| 190 |
default = dict(default or {}, config_ids=[(5, 0, 0)]) |
| 191 |
vals_list = super().copy_data(default=default) |
| 192 |
|
| 193 |
for pm, vals in zip(self, vals_list): |
| 194 |
if pm.journal_id and pm.journal_id.type == 'cash': |
| 195 |
if ('journal_id' in default and default['journal_id'] == pm.journal_id.id) or ('journal_id' not in default): |
| 196 |
vals['journal_id'] = False |
| 197 |
return vals_list |
| 198 |
|
| 199 |
@api.constrains('payment_method_type', 'journal_id', 'qr_code_method') |
| 200 |
def _check_payment_method(self): |
| 201 |
for rec in self: |
| 202 |
if rec.payment_method_type == "qr_code": |
| 203 |
if (rec.journal_id.type != 'bank' or not rec.journal_id.bank_account_id): |
| 204 |
raise ValidationError(_("At least one bank account must be defined on the journal to allow registering QR code payments with Bank apps.")) |
| 205 |
if not rec.qr_code_method: |
| 206 |
raise ValidationError(_("You must select a QR-code method to generate QR-codes for this payment method.")) |
| 207 |
error_msg = self.journal_id.bank_account_id._get_error_messages_for_qr(self.qr_code_method, False, rec.company_id.currency_id) |
| 208 |
if error_msg: |
| 209 |
raise ValidationError(error_msg) |
| 210 |
|
| 211 |
@api.depends('payment_method_type', 'journal_id') |
| 212 |
def _compute_qr(self): |
| 213 |
for pm in self: |
| 214 |
if pm.payment_method_type != "qr_code": |
| 215 |
pm.default_qr = False |
| 216 |
continue |
| 217 |
try: |
| 218 |
# Generate QR without amount that can then be used when the POS is offline |
| 219 |
pm.default_qr = pm.get_qr_code(False, '', '', pm.company_id.currency_id.id, False) |
| 220 |
except UserError: |
| 221 |
pm.default_qr = False |
| 222 |
|
| 223 |
def get_qr_code(self, amount, free_communication, structured_communication, currency, debtor_partner): |
| 224 |
""" Generates and returns a QR-code |
| 225 |
""" |
| 226 |
self.ensure_one() |
| 227 |
if self.payment_method_type != "qr_code" or not self.qr_code_method: |
| 228 |
raise UserError(_("This payment method is not configured to generate QR codes.")) |
| 229 |
payment_bank = self.journal_id.bank_account_id |
| 230 |
debtor_partner = self.env['res.partner'].browse(debtor_partner) |
| 231 |
currency = self.env['res.currency'].browse(currency) |
| 232 |
|
| 233 |
return payment_bank.with_context(is_online_qr=True).build_qr_code_base64( |
| 234 |
float(amount), free_communication, structured_communication, currency, debtor_partner, self.qr_code_method, silent_errors=False) |