Skip to content

Commit

Permalink
Merge ffbe165 into d66519a
Browse files Browse the repository at this point in the history
  • Loading branch information
felipepaloschi committed Sep 12, 2018
2 parents d66519a + ffbe165 commit 1dfad18
Showing 1 changed file with 73 additions and 2 deletions.
75 changes: 73 additions & 2 deletions br_account_payment/models/account_payment.py
Expand Up @@ -2,7 +2,7 @@
# © 2016 Alessandro Fernandes Martini, Trustcode
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, fields, models
from odoo import api, fields, models, _


class AccountPayment(models.Model):
Expand All @@ -23,7 +23,70 @@ def default_get(self, fields):

def _create_payment_entry(self, amount):
self = self.with_context(move_line_to_reconcile=self.move_line_id)
return super(AccountPayment, self)._create_payment_entry(amount)
if self.invoice_ids:
return super(AccountPayment, self)._create_payment_entry(amount)

aml_obj = self.env['account.move.line'].with_context(
check_move_validity=False)
counterpart_debit, counterpart_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())

counterpart_aml_dict = self._get_shared_move_line_vals(
counterpart_debit, counterpart_credit, amount_currency, move.id)
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)

if self.payment_difference_handling == 'reconcile'\
and self.payment_difference:
writeoff_line = self._get_shared_move_line_vals(
0, 0, 0, move.id)
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)[2:]

if counterpart_debit > 0:
debit_wo = abs(self.payment_difference)
credit_wo = 0.0
amount_currency_wo = abs(amount_currency_wo)
else:
debit_wo = 0.0
credit_wo = abs(self.payment_difference)
amount_currency_wo = -abs(amount_currency_wo)

writeoff_line.update({
'name': _('Counterpart'),
'account_id': self.writeoff_account_id.id,
'debit': debit_wo,
'credit': credit_wo,
'amount_currency': amount_currency_wo,
'currency_id': currency_id
})

writeoff_line = aml_obj.create(writeoff_line)

if counterpart_aml['debit']:
counterpart_aml['debit'] += credit_wo - debit_wo
if counterpart_aml['credit']:
counterpart_aml['credit'] += debit_wo - credit_wo
counterpart_aml['amount_currency'] -= amount_currency_wo

if not self.currency_id != self.company_id.currency_id:
amount_currency = 0
liquidity_aml_dict = self._get_shared_move_line_vals(
counterpart_credit, counterpart_debit, -amount_currency, move.id)
liquidity_aml_dict.update(self._get_liquidity_move_line_vals(-amount))
aml_obj.create(liquidity_aml_dict)

(self.move_line_id + counterpart_aml).reconcile()

move.post()
return move

@api.depends('partner_id', 'partner_type')
def _compute_open_moves(self):
Expand Down Expand Up @@ -52,3 +115,11 @@ def action_view_receivable_payable(self):
action['context'] = {'search_default_partner_id': self.partner_id.id}

return action

def _compute_payment_difference(self):
super(AccountPayment, self)._compute_payment_difference()
if len(self.invoice_ids) == 0:
move_line = self.env['account.move.line'].browse(
self.env.context.get('active_id'))
diff = (move_line.debit or move_line.credit) - self.amount
self.payment_difference = diff if diff < 0 else 0

0 comments on commit 1dfad18

Please sign in to comment.