The goal is to be coherent with the user property. Actually, company_id and company_ids on the environment are no fields. Calling env.company_id returns a browse record, not an id.
801 lines
42 KiB
Python
801 lines
42 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from odoo import models, fields, api, _
|
|
from odoo.exceptions import UserError, ValidationError
|
|
from odoo.tools import float_compare
|
|
|
|
from itertools import groupby
|
|
|
|
MAP_INVOICE_TYPE_PARTNER_TYPE = {
|
|
'out_invoice': 'customer',
|
|
'out_refund': 'customer',
|
|
'in_invoice': 'supplier',
|
|
'in_refund': 'supplier',
|
|
}
|
|
# Since invoice amounts are unsigned, this is how we know if money comes in or goes out
|
|
MAP_INVOICE_TYPE_PAYMENT_SIGN = {
|
|
'out_invoice': 1,
|
|
'in_refund': -1,
|
|
'in_invoice': -1,
|
|
'out_refund': 1,
|
|
}
|
|
|
|
|
|
class account_payment_method(models.Model):
|
|
_name = "account.payment.method"
|
|
_description = "Payment Methods"
|
|
|
|
name = fields.Char(required=True, translate=True)
|
|
code = fields.Char(required=True) # For internal identification
|
|
payment_type = fields.Selection([('inbound', 'Inbound'), ('outbound', 'Outbound')], required=True)
|
|
|
|
|
|
class account_payment(models.Model):
|
|
_name = "account.payment"
|
|
_inherit = ['mail.thread', 'mail.activity.mixin']
|
|
_description = "Payments"
|
|
_order = "payment_date desc, name desc"
|
|
|
|
name = fields.Char(readonly=True, copy=False) # The name is attributed upon post()
|
|
payment_reference = fields.Char(copy=False, readonly=True, help="Reference of the document used to issue this payment. Eg. check number, file name, etc.")
|
|
move_name = fields.Char(string='Journal Entry Name', readonly=True,
|
|
default=False, copy=False,
|
|
help="Technical field holding the number given to the journal entry, automatically set when the statement line is reconciled then stored to set the same number again if the line is cancelled, set to draft and re-processed again.")
|
|
|
|
# Money flows from the journal_id's default_debit_account_id or default_credit_account_id to the destination_account_id
|
|
destination_account_id = fields.Many2one('account.account', compute='_compute_destination_account_id', readonly=True)
|
|
# For money transfer, money goes from journal_id to a transfer account, then from the transfer account to destination_journal_id
|
|
destination_journal_id = fields.Many2one('account.journal', string='Transfer To', domain=[('type', 'in', ('bank', 'cash'))], readonly=True, states={'draft': [('readonly', False)]})
|
|
|
|
invoice_ids = fields.Many2many('account.invoice', 'account_invoice_payment_rel', 'payment_id', 'invoice_id', string="Invoices", copy=False, readonly=True,
|
|
help="""Technical field containing the invoice for which the payment has been generated.
|
|
This does not especially correspond to the invoices reconciled with the payment,
|
|
as it can have been generated first, and reconciled later""")
|
|
reconciled_invoice_ids = fields.Many2many('account.invoice', string='Reconciled Invoices', compute='_compute_reconciled_invoice_ids', help="Invoices whose journal items have been reconciled with this payment's.")
|
|
has_invoices = fields.Boolean(compute="_compute_reconciled_invoice_ids", help="Technical field used for usability purposes")
|
|
|
|
move_line_ids = fields.One2many('account.move.line', 'payment_id', readonly=True, copy=False, ondelete='restrict')
|
|
move_reconciled = fields.Boolean(compute="_get_move_reconciled", readonly=True)
|
|
|
|
state = fields.Selection([('draft', 'Draft'), ('posted', 'Posted'), ('sent', 'Sent'), ('reconciled', 'Reconciled'), ('cancelled', 'Cancelled')], readonly=True, default='draft', copy=False, string="Status")
|
|
payment_type = fields.Selection([('outbound', 'Send Money'), ('inbound', 'Receive Money'), ('transfer', 'Internal Transfer')], string='Payment Type', required=True, readonly=True, states={'draft': [('readonly', False)]})
|
|
payment_method_id = fields.Many2one('account.payment.method', string='Payment Method Type', required=True, readonly=True, states={'draft': [('readonly', False)]}, oldname="payment_method",
|
|
help="Manual: Get paid by cash, check or any other method outside of Odoo.\n"\
|
|
"Electronic: Get paid automatically through a payment acquirer by requesting a transaction on a card saved by the customer when buying or subscribing online (payment token).\n"\
|
|
"Check: Pay bill by check and print it from Odoo.\n"\
|
|
"Batch Deposit: Encase several customer checks at once by generating a batch deposit to submit to your bank. When encoding the bank statement in Odoo, you are suggested to reconcile the transaction with the batch deposit.To enable batch deposit, module account_batch_payment must be installed.\n"\
|
|
"SEPA Credit Transfer: Pay bill from a SEPA Credit Transfer file you submit to your bank. To enable sepa credit transfer, module account_sepa must be installed ")
|
|
payment_method_code = fields.Char(related='payment_method_id.code',
|
|
help="Technical field used to adapt the interface to the payment type selected.", readonly=True)
|
|
|
|
partner_type = fields.Selection([('customer', 'Customer'), ('supplier', 'Vendor')], tracking=True, readonly=True, states={'draft': [('readonly', False)]})
|
|
partner_id = fields.Many2one('res.partner', string='Partner', tracking=True, readonly=True, states={'draft': [('readonly', False)]})
|
|
|
|
amount = fields.Monetary(string='Payment Amount', required=True, readonly=True, states={'draft': [('readonly', False)]}, tracking=True)
|
|
currency_id = fields.Many2one('res.currency', string='Currency', required=True, readonly=True, states={'draft': [('readonly', False)]}, default=lambda self: self.env.company.currency_id)
|
|
payment_date = fields.Date(string='Payment Date', default=fields.Date.context_today, required=True, readonly=True, states={'draft': [('readonly', False)]}, copy=False, tracking=True)
|
|
communication = fields.Char(string='Memo', readonly=True, states={'draft': [('readonly', False)]})
|
|
journal_id = fields.Many2one('account.journal', string='Payment Journal', required=True, readonly=True, states={'draft': [('readonly', False)]}, tracking=True, domain=[('type', 'in', ('bank', 'cash'))])
|
|
company_id = fields.Many2one('res.company', related='journal_id.company_id', string='Company', readonly=True)
|
|
|
|
hide_payment_method = fields.Boolean(compute='_compute_hide_payment_method',
|
|
help="Technical field used to hide the payment method if the"
|
|
"selected journal has only one available which is 'manual'")
|
|
|
|
payment_difference = fields.Monetary(compute='_compute_payment_difference', readonly=True)
|
|
payment_difference_handling = fields.Selection([('open', 'Keep open'), ('reconcile', 'Mark invoice as fully paid')], default='open', string="Payment Difference Handling", copy=False)
|
|
writeoff_account_id = fields.Many2one('account.account', string="Difference Account", domain=[('deprecated', '=', False)], copy=False)
|
|
writeoff_label = fields.Char(
|
|
string='Journal Item Label',
|
|
help='Change label of the counterpart that will hold the payment difference',
|
|
default='Write-Off')
|
|
partner_bank_account_id = fields.Many2one('res.partner.bank', string="Recipient Bank Account", readonly=True, states={'draft': [('readonly', False)]})
|
|
show_partner_bank_account = fields.Boolean(compute='_compute_show_partner_bank', help='Technical field used to know whether the field `partner_bank_account_id` needs to be displayed or not in the payments form views')
|
|
require_partner_bank_account = fields.Boolean(compute='_compute_show_partner_bank', help='Technical field used to know whether the field `partner_bank_account_id` needs to be required or not in the payments form views')
|
|
|
|
@api.model
|
|
def default_get(self, fields):
|
|
rec = super(account_payment, self).default_get(fields)
|
|
active_ids = self._context.get('active_ids') or self._context.get('active_id')
|
|
active_model = self._context.get('active_model')
|
|
|
|
# Check for selected invoices ids
|
|
if not active_ids or active_model != 'account.invoice':
|
|
return rec
|
|
|
|
invoices = self.env['account.invoice'].browse(active_ids)
|
|
|
|
# Check all invoices are open
|
|
if any(invoice.state != 'open' for invoice in invoices):
|
|
raise UserError(_("You can only register payments for open invoices"))
|
|
# Check if, in batch payments, there are not negative invoices and positive invoices
|
|
dtype = invoices[0].type
|
|
for inv in invoices[1:]:
|
|
if inv.type != dtype:
|
|
if ((dtype == 'in_refund' and inv.type == 'in_invoice') or
|
|
(dtype == 'in_invoice' and inv.type == 'in_refund')):
|
|
raise UserError(_("You cannot register payments for vendor bills and supplier refunds at the same time."))
|
|
if ((dtype == 'out_refund' and inv.type == 'out_invoice') or
|
|
(dtype == 'out_invoice' and inv.type == 'out_refund')):
|
|
raise UserError(_("You cannot register payments for customer invoices and credit notes at the same time."))
|
|
|
|
amount = self._compute_payment_amount(invoices, invoices[0].currency_id)
|
|
rec.update({
|
|
'currency_id': invoices[0].currency_id.id,
|
|
'amount': abs(amount),
|
|
'payment_type': 'inbound' if amount > 0 else 'outbound',
|
|
'partner_id': invoices[0].commercial_partner_id.id,
|
|
'partner_type': MAP_INVOICE_TYPE_PARTNER_TYPE[invoices[0].type],
|
|
'communication': invoices[0].reference or invoices[0].number,
|
|
'invoice_ids': [(6, 0, invoices.ids)],
|
|
})
|
|
return rec
|
|
|
|
@api.one
|
|
@api.constrains('amount')
|
|
def _check_amount(self):
|
|
if self.amount < 0:
|
|
raise ValidationError(_('The payment amount cannot be negative.'))
|
|
|
|
@api.model
|
|
def _get_method_codes_using_bank_account(self):
|
|
return []
|
|
|
|
@api.model
|
|
def _get_method_codes_needing_bank_account(self):
|
|
return []
|
|
|
|
@api.depends('payment_method_code')
|
|
def _compute_show_partner_bank(self):
|
|
""" Computes if the destination bank account must be displayed in the payment form view. By default, it
|
|
won't be displayed but some modules might change that, depending on the payment type."""
|
|
for payment in self:
|
|
payment.show_partner_bank_account = payment.payment_method_code in self._get_method_codes_using_bank_account()
|
|
payment.require_partner_bank_account = payment.payment_method_code in self._get_method_codes_needing_bank_account()
|
|
|
|
@api.multi
|
|
@api.depends('payment_type', 'journal_id')
|
|
def _compute_hide_payment_method(self):
|
|
for payment in self:
|
|
if not payment.journal_id or payment.journal_id.type not in ['bank', 'cash']:
|
|
payment.hide_payment_method = True
|
|
continue
|
|
journal_payment_methods = payment.payment_type == 'inbound'\
|
|
and payment.journal_id.inbound_payment_method_ids\
|
|
or payment.journal_id.outbound_payment_method_ids
|
|
payment.hide_payment_method = len(journal_payment_methods) == 1 and journal_payment_methods[0].code == 'manual'
|
|
|
|
@api.depends('invoice_ids', 'amount', 'payment_date', 'currency_id', 'payment_type')
|
|
def _compute_payment_difference(self):
|
|
for pay in self.filtered(lambda p: p.invoice_ids and p.state == 'draft'):
|
|
payment_amount = -pay.amount if pay.payment_type == 'outbound' else pay.amount
|
|
pay.payment_difference = pay._compute_payment_amount() - payment_amount
|
|
|
|
@api.onchange('journal_id')
|
|
def _onchange_journal(self):
|
|
if self.journal_id:
|
|
# Set default payment method (we consider the first to be the default one)
|
|
payment_methods = self.payment_type == 'inbound' and self.journal_id.inbound_payment_method_ids or self.journal_id.outbound_payment_method_ids
|
|
payment_methods_list = payment_methods.ids
|
|
|
|
default_payment_method_id = self.env.context.get('default_payment_method_id')
|
|
if default_payment_method_id:
|
|
# Ensure the domain will accept the provided default value
|
|
payment_methods_list.append(default_payment_method_id)
|
|
else:
|
|
self.payment_method_id = payment_methods and payment_methods[0] or False
|
|
|
|
# Set payment method domain (restrict to methods enabled for the journal and to selected payment type)
|
|
payment_type = self.payment_type in ('outbound', 'transfer') and 'outbound' or 'inbound'
|
|
|
|
domain = {'payment_method_id': [('payment_type', '=', payment_type), ('id', 'in', payment_methods_list)]}
|
|
|
|
if self.env.context.get('active_model') == 'account.invoice':
|
|
active_ids = self._context.get('active_ids')
|
|
invoices = self.env['account.invoice'].browse(active_ids)
|
|
self.amount = abs(self._compute_payment_amount(invoices))
|
|
|
|
return {'domain': domain}
|
|
return {}
|
|
|
|
@api.onchange('partner_id')
|
|
def _onchange_partner_id(self):
|
|
if self.invoice_ids and self.invoice_ids[0].partner_bank_id:
|
|
self.partner_bank_account_id = self.invoice_ids[0].partner_bank_id
|
|
elif self.partner_id != self.partner_bank_account_id.partner_id:
|
|
# This condition ensures we use the default value provided into
|
|
# context for partner_bank_account_id properly when provided with a
|
|
# default partner_id. Without it, the onchange recomputes the bank account
|
|
# uselessly and might assign a different value to it.
|
|
if self.partner_id and len(self.partner_id.bank_ids) > 0:
|
|
self.partner_bank_account_id = self.partner_id.bank_ids[0]
|
|
elif self.partner_id and len(self.partner_id.commercial_partner_id.bank_ids) > 0:
|
|
self.partner_bank_account_id = self.partner_id.commercial_partner_id.bank_ids[0]
|
|
else:
|
|
self.partner_bank_account_id = False
|
|
return {'domain': {'partner_bank_account_id': [('partner_id', 'in', [self.partner_id.id, self.partner_id.commercial_partner_id.id])]}}
|
|
|
|
@api.onchange('partner_type')
|
|
def _onchange_partner_type(self):
|
|
self.ensure_one()
|
|
# Set partner_id domain
|
|
if self.partner_type:
|
|
return {'domain': {'partner_id': [(self.partner_type, '=', True)]}}
|
|
|
|
@api.onchange('payment_type')
|
|
def _onchange_payment_type(self):
|
|
if not self.invoice_ids and not self.partner_type:
|
|
# Set default partner type for the payment type
|
|
if self.payment_type == 'inbound':
|
|
self.partner_type = 'customer'
|
|
elif self.payment_type == 'outbound':
|
|
self.partner_type = 'supplier'
|
|
elif self.payment_type not in ('inbound', 'outbound'):
|
|
self.partner_type = False
|
|
# Set payment method domain
|
|
res = self._onchange_journal()
|
|
if not res.get('domain', {}):
|
|
res['domain'] = {}
|
|
jrnl_filters = self._compute_journal_domain_and_types()
|
|
journal_types = jrnl_filters['journal_types']
|
|
journal_types.update(['bank', 'cash'])
|
|
res['domain']['journal_id'] = jrnl_filters['domain'] + [('type', 'in', list(journal_types))]
|
|
return res
|
|
|
|
def _compute_journal_domain_and_types(self):
|
|
journal_type = ['bank', 'cash']
|
|
domain = []
|
|
if self.invoice_ids:
|
|
domain.append(('company_id', '=', self.invoice_ids[0].company_id.id))
|
|
if self.currency_id.is_zero(self.amount) and self.has_invoices:
|
|
# In case of payment with 0 amount, allow to select a journal of type 'general' like
|
|
# 'Miscellaneous Operations' and set this journal by default.
|
|
journal_type = ['general']
|
|
self.payment_difference_handling = 'reconcile'
|
|
else:
|
|
if self.payment_type == 'inbound':
|
|
domain.append(('at_least_one_inbound', '=', True))
|
|
else:
|
|
domain.append(('at_least_one_outbound', '=', True))
|
|
return {'domain': domain, 'journal_types': set(journal_type)}
|
|
|
|
@api.onchange('amount', 'currency_id')
|
|
def _onchange_amount(self):
|
|
jrnl_filters = self._compute_journal_domain_and_types()
|
|
journal_types = jrnl_filters['journal_types']
|
|
domain_on_types = [('type', 'in', list(journal_types))]
|
|
if self.invoice_ids:
|
|
domain_on_types.append(('company_id', '=', self.invoice_ids[0].company_id.id))
|
|
if self.journal_id.type not in journal_types or (self.invoice_ids and self.journal_id.company_id != self.invoice_ids[0].company_id):
|
|
self.journal_id = self.env['account.journal'].search(domain_on_types, limit=1)
|
|
return {'domain': {'journal_id': jrnl_filters['domain'] + domain_on_types}}
|
|
|
|
@api.onchange('currency_id')
|
|
def _onchange_currency(self):
|
|
self.amount = abs(self._compute_payment_amount())
|
|
|
|
if self.journal_id: # TODO: only return if currency differ?
|
|
return
|
|
|
|
# Set by default the first liquidity journal having this currency if exists.
|
|
domain = [('type', 'in', ('bank', 'cash')), ('currency_id', '=', self.currency_id.id)]
|
|
if self.invoice_ids:
|
|
domain.append(('company_id', '=', self.invoice_ids[0].company_id.id))
|
|
journal = self.env['account.journal'].search(domain, limit=1)
|
|
if journal:
|
|
return {'value': {'journal_id': journal.id}}
|
|
|
|
@api.multi
|
|
def _compute_payment_amount(self, invoices=None, currency=None):
|
|
'''Compute the total amount for the payment wizard.
|
|
|
|
:param invoices: If not specified, pick all the invoices.
|
|
:param currency: If not specified, search a default currency on wizard/journal.
|
|
:return: The total amount to pay the invoices.
|
|
'''
|
|
|
|
# Get the payment invoices
|
|
if not invoices:
|
|
invoices = self.invoice_ids
|
|
|
|
# Get the payment currency
|
|
if not currency:
|
|
currency = self.currency_id or self.journal_id.currency_id or self.journal_id.company_id.currency_id
|
|
|
|
# Avoid currency rounding issues by summing the amounts according to the company_currency_id before
|
|
invoice_datas = invoices.read_group(
|
|
[('id', 'in', invoices.ids)],
|
|
['currency_id', 'type', 'residual_signed'],
|
|
['currency_id', 'type'], lazy=False)
|
|
total = 0.0
|
|
for invoice_data in invoice_datas:
|
|
amount_total = MAP_INVOICE_TYPE_PAYMENT_SIGN[invoice_data['type']] * invoice_data['residual_signed']
|
|
payment_currency = self.env['res.currency'].browse(invoice_data['currency_id'][0])
|
|
if payment_currency == currency:
|
|
total += amount_total
|
|
else:
|
|
total += payment_currency._convert(amount_total, currency, self.env.company, self.payment_date or fields.Date.today())
|
|
return total
|
|
|
|
@api.multi
|
|
def name_get(self):
|
|
return [(payment.id, payment.name or _('Draft Payment')) for payment in self]
|
|
|
|
@api.multi
|
|
@api.depends('move_line_ids.reconciled')
|
|
def _get_move_reconciled(self):
|
|
for payment in self:
|
|
rec = True
|
|
for aml in payment.move_line_ids.filtered(lambda x: x.account_id.reconcile):
|
|
if not aml.reconciled:
|
|
rec = False
|
|
break
|
|
payment.move_reconciled = rec
|
|
|
|
def open_payment_matching_screen(self):
|
|
# Open reconciliation view for customers/suppliers
|
|
move_line_id = False
|
|
for move_line in self.move_line_ids:
|
|
if move_line.account_id.reconcile:
|
|
move_line_id = move_line.id
|
|
break
|
|
if not self.partner_id:
|
|
raise UserError(_("Payments without a customer can't be matched"))
|
|
action_context = {'company_ids': [self.company_id.id], 'partner_ids': [self.partner_id.commercial_partner_id.id]}
|
|
if self.partner_type == 'customer':
|
|
action_context.update({'mode': 'customers'})
|
|
elif self.partner_type == 'supplier':
|
|
action_context.update({'mode': 'suppliers'})
|
|
if move_line_id:
|
|
action_context.update({'move_line_id': move_line_id})
|
|
return {
|
|
'type': 'ir.actions.client',
|
|
'tag': 'manual_reconciliation_view',
|
|
'context': action_context,
|
|
}
|
|
|
|
@api.one
|
|
@api.depends('invoice_ids', 'payment_type', 'partner_type', 'partner_id')
|
|
def _compute_destination_account_id(self):
|
|
if self.invoice_ids:
|
|
self.destination_account_id = self.invoice_ids[0].account_id.id
|
|
elif self.payment_type == 'transfer':
|
|
if not self.company_id.transfer_account_id.id:
|
|
raise UserError(_('There is no Transfer Account defined in the accounting settings. Please define one to be able to confirm this transfer.'))
|
|
self.destination_account_id = self.company_id.transfer_account_id.id
|
|
elif self.partner_id:
|
|
if self.partner_type == 'customer':
|
|
self.destination_account_id = self.partner_id.property_account_receivable_id.id
|
|
else:
|
|
self.destination_account_id = self.partner_id.property_account_payable_id.id
|
|
elif self.partner_type == 'customer':
|
|
default_account = self.env['ir.property'].get('property_account_receivable_id', 'res.partner')
|
|
self.destination_account_id = default_account.id
|
|
elif self.partner_type == 'supplier':
|
|
default_account = self.env['ir.property'].get('property_account_payable_id', 'res.partner')
|
|
self.destination_account_id = default_account.id
|
|
|
|
@api.depends('move_line_ids.matched_debit_ids', 'move_line_ids.matched_credit_ids')
|
|
def _compute_reconciled_invoice_ids(self):
|
|
for record in self:
|
|
record.reconciled_invoice_ids = (record.move_line_ids.mapped('matched_debit_ids.debit_move_id.invoice_id') |
|
|
record.move_line_ids.mapped('matched_credit_ids.credit_move_id.invoice_id'))
|
|
record.has_invoices = bool(record.reconciled_invoice_ids)
|
|
|
|
@api.multi
|
|
def action_register_payment(self):
|
|
active_ids = self.env.context.get('active_ids')
|
|
if not active_ids:
|
|
return ''
|
|
|
|
return {
|
|
'name': _('Register Payment'),
|
|
'res_model': len(active_ids) == 1 and 'account.payment' or 'account.payment.register',
|
|
'view_type': 'form',
|
|
'view_mode': 'form',
|
|
'view_id': len(active_ids) != 1 and self.env.ref('account.view_account_payment_form_multi').id or self.env.ref('account.view_account_payment_invoice_form').id,
|
|
'context': self.env.context,
|
|
'target': 'new',
|
|
'type': 'ir.actions.act_window',
|
|
}
|
|
|
|
@api.multi
|
|
def button_journal_entries(self):
|
|
return {
|
|
'name': _('Journal Items'),
|
|
'view_type': 'form',
|
|
'view_mode': 'tree,form',
|
|
'res_model': 'account.move.line',
|
|
'view_id': False,
|
|
'type': 'ir.actions.act_window',
|
|
'domain': [('payment_id', 'in', self.ids)],
|
|
}
|
|
|
|
@api.multi
|
|
def button_invoices(self):
|
|
if self.partner_type == 'supplier':
|
|
views = [(self.env.ref('account.invoice_supplier_tree').id, 'tree'), (self.env.ref('account.invoice_supplier_form').id, 'form')]
|
|
else:
|
|
views = [(self.env.ref('account.invoice_tree').id, 'tree'), (self.env.ref('account.invoice_form').id, 'form')]
|
|
return {
|
|
'name': _('Paid Invoices'),
|
|
'view_type': 'form',
|
|
'view_mode': 'tree,form',
|
|
'res_model': 'account.invoice',
|
|
'view_id': False,
|
|
'views': views,
|
|
'type': 'ir.actions.act_window',
|
|
'domain': [('id', 'in', [x.id for x in self.reconciled_invoice_ids])],
|
|
}
|
|
|
|
@api.multi
|
|
def unreconcile(self):
|
|
""" Set back the payments in 'posted' or 'sent' state, without deleting the journal entries.
|
|
Called when cancelling a bank statement line linked to a pre-registered payment.
|
|
"""
|
|
for payment in self:
|
|
if payment.payment_reference:
|
|
payment.write({'state': 'sent'})
|
|
else:
|
|
payment.write({'state': 'posted'})
|
|
|
|
@api.multi
|
|
def cancel(self):
|
|
for rec in self:
|
|
for move in rec.move_line_ids.mapped('move_id'):
|
|
if rec.reconciled_invoice_ids:
|
|
move.line_ids.remove_move_reconcile()
|
|
move.button_cancel()
|
|
move.unlink()
|
|
rec.state = 'cancelled'
|
|
|
|
@api.multi
|
|
def unlink(self):
|
|
if any(bool(rec.move_line_ids) for rec in self):
|
|
raise UserError(_("You cannot delete a payment that is already posted."))
|
|
if any(rec.move_name for rec in self):
|
|
raise UserError(_('It is not allowed to delete a payment that already created a journal entry since it would create a gap in the numbering. You should create the journal entry again and cancel it thanks to a regular revert.'))
|
|
return super(account_payment, self).unlink()
|
|
|
|
@api.multi
|
|
def post(self):
|
|
""" Create the journal items for the payment and update the payment's state to 'posted'.
|
|
A journal entry is created containing an item in the source liquidity account (selected journal's default_debit or default_credit)
|
|
and another in the destination reconcilable account (see _compute_destination_account_id).
|
|
If invoice_ids is not empty, there will be one reconcilable move line per invoice to reconcile with.
|
|
If the payment is a transfer, a second journal entry is created in the destination journal to receive money from the transfer account.
|
|
"""
|
|
for rec in self:
|
|
|
|
if rec.state != 'draft':
|
|
raise UserError(_("Only a draft payment can be posted."))
|
|
|
|
if any(inv.state != 'open' for inv in rec.invoice_ids):
|
|
raise ValidationError(_("The payment cannot be processed because the invoice is not open!"))
|
|
|
|
# keep the name in case of a payment reset to draft
|
|
if not rec.name:
|
|
# Use the right sequence to set the name
|
|
if rec.payment_type == 'transfer':
|
|
sequence_code = 'account.payment.transfer'
|
|
else:
|
|
if rec.partner_type == 'customer':
|
|
if rec.payment_type == 'inbound':
|
|
sequence_code = 'account.payment.customer.invoice'
|
|
if rec.payment_type == 'outbound':
|
|
sequence_code = 'account.payment.customer.refund'
|
|
if rec.partner_type == 'supplier':
|
|
if rec.payment_type == 'inbound':
|
|
sequence_code = 'account.payment.supplier.refund'
|
|
if rec.payment_type == 'outbound':
|
|
sequence_code = 'account.payment.supplier.invoice'
|
|
rec.name = self.env['ir.sequence'].with_context(ir_sequence_date=rec.payment_date).next_by_code(sequence_code)
|
|
if not rec.name and rec.payment_type != 'transfer':
|
|
raise UserError(_("You have to define a sequence for %s in your company.") % (sequence_code,))
|
|
|
|
# Create the journal entry
|
|
amount = rec.amount * (rec.payment_type in ('outbound', 'transfer') and 1 or -1)
|
|
move = rec._create_payment_entry(amount)
|
|
|
|
# In case of a transfer, the first journal entry created debited the source liquidity account and credited
|
|
# the transfer account. Now we debit the transfer account and credit the destination liquidity account.
|
|
if rec.payment_type == 'transfer':
|
|
transfer_credit_aml = move.line_ids.filtered(lambda r: r.account_id == rec.company_id.transfer_account_id)
|
|
transfer_debit_aml = rec._create_transfer_entry(amount)
|
|
(transfer_credit_aml + transfer_debit_aml).reconcile()
|
|
|
|
rec.write({'state': 'posted', 'move_name': move.name})
|
|
return True
|
|
|
|
@api.multi
|
|
def action_draft(self):
|
|
return self.write({'state': 'draft'})
|
|
|
|
def _create_payment_entry(self, amount):
|
|
""" Create a journal entry corresponding to a payment, if the payment references invoice(s) they are reconciled.
|
|
Return the journal entry.
|
|
"""
|
|
aml_obj = self.env['account.move.line'].with_context(check_move_validity=False)
|
|
debit, credit, amount_currency, currency_id = aml_obj.with_context(date=self.payment_date)._compute_amount_fields(amount, self.currency_id, self.company_id.currency_id)
|
|
|
|
move = self.env['account.move'].create(self._get_move_vals())
|
|
|
|
#Write line corresponding to invoice payment
|
|
counterpart_aml_dict = self._get_shared_move_line_vals(debit, credit, amount_currency, move.id, False)
|
|
counterpart_aml_dict.update(self._get_counterpart_move_line_vals(self.invoice_ids))
|
|
counterpart_aml_dict.update({'currency_id': currency_id})
|
|
counterpart_aml = aml_obj.create(counterpart_aml_dict)
|
|
|
|
#Reconcile with the invoices
|
|
if self.payment_difference_handling == 'reconcile' and self.payment_difference:
|
|
writeoff_line = self._get_shared_move_line_vals(0, 0, 0, move.id, False)
|
|
debit_wo, credit_wo, amount_currency_wo, currency_id = aml_obj.with_context(date=self.payment_date)._compute_amount_fields(self.payment_difference, self.currency_id, self.company_id.currency_id)
|
|
writeoff_line['name'] = self.writeoff_label
|
|
writeoff_line['account_id'] = self.writeoff_account_id.id
|
|
writeoff_line['debit'] = debit_wo
|
|
writeoff_line['credit'] = credit_wo
|
|
writeoff_line['amount_currency'] = amount_currency_wo
|
|
writeoff_line['currency_id'] = currency_id
|
|
writeoff_line = aml_obj.create(writeoff_line)
|
|
if counterpart_aml['debit'] or (writeoff_line['credit'] and not counterpart_aml['credit']):
|
|
counterpart_aml['debit'] += credit_wo - debit_wo
|
|
if counterpart_aml['credit'] or (writeoff_line['debit'] and not counterpart_aml['debit']):
|
|
counterpart_aml['credit'] += debit_wo - credit_wo
|
|
counterpart_aml['amount_currency'] -= amount_currency_wo
|
|
|
|
#Write counterpart lines
|
|
if not self.currency_id.is_zero(self.amount):
|
|
if not self.currency_id != self.company_id.currency_id:
|
|
amount_currency = 0
|
|
liquidity_aml_dict = self._get_shared_move_line_vals(credit, debit, -amount_currency, move.id, False)
|
|
liquidity_aml_dict.update(self._get_liquidity_move_line_vals(-amount))
|
|
aml_obj.create(liquidity_aml_dict)
|
|
|
|
#validate the payment
|
|
if not self.journal_id.post_at_bank_rec:
|
|
move.post()
|
|
|
|
#reconcile the invoice receivable/payable line(s) with the payment
|
|
if self.invoice_ids:
|
|
self.invoice_ids.register_payment(counterpart_aml)
|
|
|
|
return move
|
|
|
|
def _create_transfer_entry(self, amount):
|
|
""" Create the journal entry corresponding to the 'incoming money' part of an internal transfer, return the reconcilable move line
|
|
"""
|
|
aml_obj = self.env['account.move.line'].with_context(check_move_validity=False)
|
|
debit, credit, amount_currency, dummy = aml_obj.with_context(date=self.payment_date)._compute_amount_fields(amount, self.currency_id, self.company_id.currency_id)
|
|
amount_currency = self.destination_journal_id.currency_id and self.currency_id._convert(amount, self.destination_journal_id.currency_id, self.company_id, self.payment_date or fields.Date.today()) or 0
|
|
|
|
dst_move = self.env['account.move'].create(self._get_move_vals(self.destination_journal_id))
|
|
|
|
dst_liquidity_aml_dict = self._get_shared_move_line_vals(debit, credit, amount_currency, dst_move.id)
|
|
dst_liquidity_aml_dict.update({
|
|
'name': _('Transfer from %s') % self.journal_id.name,
|
|
'account_id': self.destination_journal_id.default_credit_account_id.id,
|
|
'currency_id': self.destination_journal_id.currency_id.id,
|
|
'journal_id': self.destination_journal_id.id})
|
|
aml_obj.create(dst_liquidity_aml_dict)
|
|
|
|
transfer_debit_aml_dict = self._get_shared_move_line_vals(credit, debit, 0, dst_move.id)
|
|
transfer_debit_aml_dict.update({
|
|
'name': self.name,
|
|
'account_id': self.company_id.transfer_account_id.id,
|
|
'journal_id': self.destination_journal_id.id})
|
|
if self.currency_id != self.company_id.currency_id:
|
|
transfer_debit_aml_dict.update({
|
|
'currency_id': self.currency_id.id,
|
|
'amount_currency': -self.amount,
|
|
})
|
|
transfer_debit_aml = aml_obj.create(transfer_debit_aml_dict)
|
|
if not self.destination_journal_id.post_at_bank_rec:
|
|
dst_move.post()
|
|
return transfer_debit_aml
|
|
|
|
def _get_move_vals(self, journal=None):
|
|
""" Return dict to create the payment move
|
|
"""
|
|
journal = journal or self.journal_id
|
|
move_vals = {
|
|
'date': self.payment_date,
|
|
'ref': self.communication or '',
|
|
'company_id': self.company_id.id,
|
|
'journal_id': journal.id,
|
|
}
|
|
if self.move_name:
|
|
move_vals['name'] = self.move_name
|
|
return move_vals
|
|
|
|
def _get_shared_move_line_vals(self, debit, credit, amount_currency, move_id, invoice_id=False):
|
|
""" Returns values common to both move lines (except for debit, credit and amount_currency which are reversed)
|
|
"""
|
|
return {
|
|
'partner_id': self.payment_type in ('inbound', 'outbound') and self.env['res.partner']._find_accounting_partner(self.partner_id).id or False,
|
|
'invoice_id': invoice_id and invoice_id.id or False,
|
|
'move_id': move_id,
|
|
'debit': debit,
|
|
'credit': credit,
|
|
'amount_currency': amount_currency or False,
|
|
'payment_id': self.id,
|
|
'journal_id': self.journal_id.id,
|
|
}
|
|
|
|
def _get_counterpart_move_line_vals(self, invoice=False):
|
|
if self.payment_type == 'transfer':
|
|
name = self.name
|
|
else:
|
|
name = ''
|
|
if self.partner_type == 'customer':
|
|
if self.payment_type == 'inbound':
|
|
name += _("Customer Payment")
|
|
elif self.payment_type == 'outbound':
|
|
name += _("Customer Credit Note")
|
|
elif self.partner_type == 'supplier':
|
|
if self.payment_type == 'inbound':
|
|
name += _("Vendor Credit Note")
|
|
elif self.payment_type == 'outbound':
|
|
name += _("Vendor Payment")
|
|
if invoice:
|
|
name += ': '
|
|
for inv in invoice:
|
|
if inv.move_id:
|
|
name += inv.number + ', '
|
|
name = name[:len(name)-2]
|
|
return {
|
|
'name': name,
|
|
'account_id': self.destination_account_id.id,
|
|
'currency_id': self.currency_id != self.company_id.currency_id and self.currency_id.id or False,
|
|
}
|
|
|
|
def _get_liquidity_move_line_vals(self, amount):
|
|
name = self.name
|
|
if self.payment_type == 'transfer':
|
|
name = _('Transfer to %s') % self.destination_journal_id.name
|
|
vals = {
|
|
'name': name,
|
|
'account_id': self.payment_type in ('outbound', 'transfer') and self.journal_id.default_debit_account_id.id or self.journal_id.default_credit_account_id.id,
|
|
'journal_id': self.journal_id.id,
|
|
'currency_id': self.currency_id != self.company_id.currency_id and self.currency_id.id or False,
|
|
}
|
|
|
|
# If the journal has a currency specified, the journal item need to be expressed in this currency
|
|
if self.journal_id.currency_id and self.currency_id != self.journal_id.currency_id:
|
|
amount = self.currency_id._convert(amount, self.journal_id.currency_id, self.company_id, self.payment_date or fields.Date.today())
|
|
debit, credit, amount_currency, dummy = self.env['account.move.line'].with_context(date=self.payment_date)._compute_amount_fields(amount, self.journal_id.currency_id, self.company_id.currency_id)
|
|
vals.update({
|
|
'amount_currency': amount_currency,
|
|
'currency_id': self.journal_id.currency_id.id,
|
|
})
|
|
|
|
return vals
|
|
|
|
def _get_invoice_payment_amount(self, inv):
|
|
"""
|
|
Computes the amount covered by the current payment in the given invoice.
|
|
|
|
:param inv: an invoice object
|
|
:returns: the amount covered by the payment in the invoice
|
|
"""
|
|
self.ensure_one()
|
|
return sum([
|
|
data['amount']
|
|
for data in inv._get_payments_vals()
|
|
if data['account_payment_id'] == self.id
|
|
])
|
|
|
|
|
|
class payment_register(models.TransientModel):
|
|
_name = 'account.payment.register'
|
|
_description = 'Register Payment'
|
|
|
|
payment_date = fields.Date(required=True, default=fields.Date.context_today)
|
|
journal_id = fields.Many2one('account.journal', required=True, domain=[('type', 'in', ('bank', 'cash'))])
|
|
payment_method_id = fields.Many2one('account.payment.method', string='Payment Method Type', required=True,
|
|
help="Manual: Get paid by cash, check or any other method outside of Odoo.\n"
|
|
"Electronic: Get paid automatically through a payment acquirer by requesting a transaction on a card saved by the customer when buying or subscribing online (payment token).\n"
|
|
"Check: Pay bill by check and print it from Odoo.\n"
|
|
"Batch Deposit: Encase several customer checks at once by generating a batch deposit to submit to your bank. When encoding the bank statement in Odoo, you are suggested to reconcile the transaction with the batch deposit.To enable batch deposit, module account_batch_payment must be installed.\n"
|
|
"SEPA Credit Transfer: Pay bill from a SEPA Credit Transfer file you submit to your bank. To enable sepa credit transfer, module account_sepa must be installed ")
|
|
invoice_ids = fields.Many2many('account.invoice', 'account_invoice_payment_rel_transient', 'payment_id', 'invoice_id', string="Invoices", copy=False, readonly=True)
|
|
|
|
@api.model
|
|
def default_get(self, fields):
|
|
rec = super(payment_register, self).default_get(fields)
|
|
active_ids = self._context.get('active_ids')
|
|
active_model = self._context.get('active_model')
|
|
invoices = self.env['account.invoice'].browse(active_ids)
|
|
|
|
# Check all invoices are open
|
|
if any(invoice.state != 'open' for invoice in invoices):
|
|
raise UserError(_("You can only register payments for open invoices"))
|
|
# Check all invoices are inbound or all invoices are outbound
|
|
outbound_list = [invoice.type in ('in_invoice', 'out_refund') for invoice in invoices]
|
|
first_outbound = invoices[0].type in ('in_invoice', 'out_refund')
|
|
if any(x != first_outbound for x in outbound_list):
|
|
raise UserError(_("You can only register at the same time for payment that are all inbound or all outbound"))
|
|
if 'invoice_ids' not in rec:
|
|
rec['invoice_ids'] = [(6, 0, invoices.ids)]
|
|
if 'journal_id' not in rec:
|
|
rec['journal_id'] = self.env['account.journal'].search([('company_id', '=', self.env.company.id), ('type', 'in', ('bank', 'cash'))], limit=1).id
|
|
if 'payment_method_id' not in rec:
|
|
if invoices[0].type in ('out_invoice', 'in_refund'):
|
|
domain = [('payment_type', '=', 'inbound')]
|
|
else:
|
|
domain = [('payment_type', '=', 'outbound')]
|
|
rec['payment_method_id'] = self.env['account.payment.method'].search(domain, limit=1).id
|
|
return rec
|
|
|
|
@api.onchange('journal_id', 'invoice_ids')
|
|
def _onchange_journal(self):
|
|
active_ids = self._context.get('active_ids')
|
|
invoices = self.env['account.invoice'].browse(active_ids)
|
|
if self.journal_id and invoices:
|
|
if invoices[0].type in ('out_invoice', 'in_refund'):
|
|
domain = [('payment_type', '=', 'inbound'), ('id', 'in', self.journal_id.inbound_payment_method_ids.ids)]
|
|
else:
|
|
domain = [('payment_type', '=', 'outbound'), ('id', 'in', self.journal_id.outbound_payment_method_ids.ids)]
|
|
|
|
return {'domain': {'payment_method_id': domain}}
|
|
return {}
|
|
|
|
@api.multi
|
|
def _prepare_payment_vals(self, invoice):
|
|
'''Create the payment values.
|
|
|
|
:param invoice: A single invoice/bill to pay.
|
|
:return: The payment values as a dictionary.
|
|
'''
|
|
amount = self.env['account.payment']._compute_payment_amount(invoices=invoice, currency=invoice.currency_id)
|
|
values = {
|
|
'journal_id': self.journal_id.id,
|
|
'payment_method_id': self.payment_method_id.id,
|
|
'payment_date': self.payment_date,
|
|
'communication': invoice.reference or invoice.number,
|
|
'invoice_ids': [(6, 0, invoice.ids)],
|
|
'payment_type': ('inbound' if amount > 0 else 'outbound'),
|
|
'amount': abs(amount),
|
|
'currency_id': invoice.currency_id.id,
|
|
'partner_id': invoice.commercial_partner_id.id,
|
|
'partner_type': MAP_INVOICE_TYPE_PARTNER_TYPE[invoice.type],
|
|
'partner_bank_account_id': invoice.partner_bank_id.id,
|
|
}
|
|
return values
|
|
|
|
@api.multi
|
|
def get_payments_vals(self):
|
|
'''Compute the values for payments.
|
|
|
|
:return: a list of payment values (dictionary).
|
|
'''
|
|
return [self._prepare_payment_vals(invoice) for invoice in self.invoice_ids]
|
|
|
|
@api.multi
|
|
def create_payments(self):
|
|
'''Create payments according to the invoices.
|
|
Having invoices with different commercial_partner_id or different type
|
|
(Vendor bills with customer invoices) leads to multiple payments.
|
|
In case of all the invoices are related to the same
|
|
commercial_partner_id and have the same type, only one payment will be
|
|
created.
|
|
|
|
:return: The ir.actions.act_window to show created payments.
|
|
'''
|
|
Payment = self.env['account.payment']
|
|
payments = Payment.create(self.get_payments_vals())
|
|
payments.post()
|
|
|
|
action_vals = {
|
|
'name': _('Payments'),
|
|
'domain': [('id', 'in', payments.ids), ('state', '=', 'posted')],
|
|
'view_type': 'form',
|
|
'res_model': 'account.payment',
|
|
'view_id': False,
|
|
'type': 'ir.actions.act_window',
|
|
}
|
|
if len(payments) == 1:
|
|
action_vals.update({'res_id': payments[0].id, 'view_mode': 'form'})
|
|
else:
|
|
action_vals['view_mode'] = 'tree,form'
|
|
return action_vals
|