45 lines
2.1 KiB
Python
45 lines
2.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
from odoo import models, fields, api, _
|
|
|
|
|
|
class AccountPaymentRegister(models.TransientModel):
|
|
_inherit = "account.payment.register"
|
|
|
|
# Add expense account field to payment register wizard
|
|
expense_account_id = fields.Many2one(
|
|
'account.account',
|
|
string='Expense Account',
|
|
domain="[('account_type', 'not in', ('asset_receivable', 'liability_payable'))]",
|
|
help="Account used for expense instead of the default payable/receivable account"
|
|
)
|
|
|
|
@api.depends('journal_id', 'partner_type', 'payment_type', 'expense_account_id')
|
|
def _compute_destination_account_id(self):
|
|
''' Override to use expense account if defined '''
|
|
super()._compute_destination_account_id()
|
|
for pay in self:
|
|
# If we have an expense account, use it instead of the default payable/receivable account
|
|
if pay.expense_account_id and ((pay.payment_type == 'outbound' and pay.partner_type == 'supplier') or
|
|
(pay.payment_type == 'inbound' and pay.partner_type == 'customer')):
|
|
pay.destination_account_id = pay.expense_account_id
|
|
|
|
def _create_payment_vals_from_wizard(self, batch_result):
|
|
''' Override to use expense account if defined '''
|
|
payment_vals = super()._create_payment_vals_from_wizard(batch_result)
|
|
|
|
# If we have an expense account, replace the destination account
|
|
if self.expense_account_id and len(payment_vals.get('line_ids', [])) >= 2:
|
|
# The second line is typically the counterpart line (payable/receivable)
|
|
payment_vals['line_ids'][1]['account_id'] = self.expense_account_id.id
|
|
|
|
return payment_vals
|
|
|
|
def _create_payment_vals_from_batch(self, batch_result):
|
|
''' Override to use expense account if defined '''
|
|
payment_vals = super()._create_payment_vals_from_batch(batch_result)
|
|
|
|
# If we have an expense account, replace the destination account
|
|
if self.expense_account_id:
|
|
payment_vals['destination_account_id'] = self.expense_account_id.id
|
|
|
|
return payment_vals |