# -*- coding: utf-8 -*-
# © 2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo.tests.common import TransactionCase
from odoo.tools import float_compare


class TestDirectDebit(TransactionCase):

    def test_direct_debit(self):
        precision = self.env['decimal.precision'].precision_get('Account')
        donation = self.env.ref('donation_direct_debit.donation6')
        # It is important to have
        # journal_id.default_debit_account_id.reconcile = True
        # to get a value on move_line.amount_residual
        # By pass a constraint because we can't change to reconcile=True
        # when there are already some moves in the account
        self.cr.execute(
            'UPDATE account_account set reconcile=true where id=%s',
            (donation.journal_id.default_debit_account_id.id, ))
        donation.validate()
        self.assertEquals(donation.state, 'done')
        self.assertTrue(donation.move_id)
        mline = False
        for line in donation.move_id.line_ids:
            if line.account_id == donation.journal_id.default_debit_account_id:
                self.assertEquals(donation.mandate_id, line.mandate_id)
                self.assertEquals(
                    donation.payment_mode_id, line.payment_mode_id)
                mline = line
                break
        paylines = self.env['account.payment.line'].search(
            [('move_line_id', '=', mline.id)])
        self.assertEquals(len(paylines), 1)
        payline = paylines[0]
        self.assertEquals(payline.partner_id, donation.commercial_partner_id)
        self.assertFalse(float_compare(
            payline.amount_currency, 150, precision_digits=precision))
        self.assertEquals(payline.currency_id, donation.currency_id)
        self.assertEquals(payline.communication, 'Don prelev SEPA')
        self.assertEquals(
            payline.partner_bank_id, donation.mandate_id.partner_bank_id)
        self.assertEquals(payline.mandate_id, donation.mandate_id)
        self.assertEquals(payline.order_id.state, 'draft')

@@ -25,16 +25,21 @@
 # %C2%A9 2016
+-2018
  Akretio
@@ -751,24 +751,57 @@
     
-self.cr.execu
+dd_account = self.env%5B'account.account'%5D.crea
 te(
+%7B
 %0A   
@@ -814,128 +814,559 @@
    '
-UPDATE account_account set reconcile=true where id=%25s',%0A            (donation.journal_id.default_debit_account_id.id, ))
+code': '511DDTEST',%0A            'name': 'Donations via direct debit',%0A            'reconcile': True,%0A            'user_type_id':%0A            self.env.ref('account.data_account_type_current_assets').id,%0A            %7D)%0A        dd_journal = self.env%5B'account.journal'%5D.create(%7B%0A            'name': 'Donations via Direct debit',%0A            'code': 'DONDD',%0A            'type': 'bank',%0A            'default_credit_account_id': dd_account.id,%0A            'default_debit_account_id': dd_account.id,%0A            %7D)%0A        donation.journal_id = dd_journal.id
 %0A   

improve tests