| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 3 |
# Copyright (C) 2004-2008 PC Solutions (<http://pcsol.be>). All Rights Reserved |
| 4 |
from odoo import fields, models, api, _ |
| 5 |
from odoo.exceptions import ValidationError |
| 6 |
from odoo.addons.base.models.ir_model import MODULE_UNINSTALL_FLAG |
| 7 |
|
| 8 |
|
| 9 |
class AccountJournal(models.Model): |
| 10 |
_inherit = 'account.journal' |
| 11 |
|
| 12 |
pos_payment_method_ids = fields.One2many('pos.payment.method', 'journal_id', string='Point of Sale Payment Methods') |
| 13 |
|
| 14 |
@api.constrains('type') |
| 15 |
def _check_type(self): |
| 16 |
methods = self.env['pos.payment.method'].sudo().search([("journal_id", "in", self.ids)]) |
| 17 |
if methods: |
| 18 |
raise ValidationError(_("This journal is associated with a payment method. You cannot modify its type")) |
| 19 |
|
| 20 |
def _check_no_active_payments(self): |
| 21 |
linked_payment_methods = self.env['pos.payment.method'].sudo().search([('journal_id', 'in', self.ids)], limit=1) |
| 22 |
if linked_payment_methods: |
| 23 |
raise ValidationError(_("You can not archive this journal because it is set on the following payment method : %s.", linked_payment_methods.name)) |
| 24 |
|
| 25 |
@api.ondelete(at_uninstall=False) |
| 26 |
def _unlink_journal_except_with_active_payments(self): |
| 27 |
for journal in self: |
| 28 |
journal._check_no_active_payments() |
| 29 |
|
| 30 |
@api.ondelete(at_uninstall=True) |
| 31 |
def _unlink_journal_cascade_pos_payment_methods(self): |
| 32 |
if self.env.context.get(MODULE_UNINSTALL_FLAG): # only cascade when switching CoA |
| 33 |
self.pos_payment_method_ids.unlink() |
| 34 |
self.env['pos.config'].search([('journal_id', 'in', self.ids)]).unlink() |
| 35 |
|
| 36 |
def action_archive(self): |
| 37 |
self._check_no_active_payments() |
| 38 |
return super().action_archive() |
| 39 |
|
| 40 |
def _get_journal_inbound_outstanding_payment_accounts(self): |
| 41 |
res = super()._get_journal_inbound_outstanding_payment_accounts() |
| 42 |
account_ids = set(res.ids) |
| 43 |
for payment_method in self.sudo().pos_payment_method_ids: |
| 44 |
account_ids.add(payment_method.outstanding_account_id.id) |
| 45 |
return self.env['account.account'].browse(account_ids) |
| 46 |
|
| 47 |
@api.model |
| 48 |
def _ensure_company_account_journal(self): |
| 49 |
journal = self.search([ |
| 50 |
('code', '=', 'POSS'), |
| 51 |
('company_id', '=', self.env.company.id), |
| 52 |
], limit=1) |
| 53 |
if not journal: |
| 54 |
journal = self.create({ |
| 55 |
'name': _('Point of Sale'), |
| 56 |
'code': 'POSS', |
| 57 |
'type': 'general', |
| 58 |
'company_id': self.env.company.id, |
| 59 |
}) |
| 60 |
return journal |