# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details.

from datetime import date, timedelta

from odoo.addons.hr_commission.tests import test_commission


class TestCommissionPayslip(test_commission.TestCommission):

    def test_commission(self):
        super().test_commission()
        commission_code = 'COMMISSION'
        payslip = self.env['hr.payslip'].create({
            'name': 'test slip',
            'employee_id': self.employee.id,
            'date_from': date.today() - timedelta(days=1),
            'date_to': date.today() + timedelta(days=14),
        })
        payslip.onchange_employee()
        self.assertFalse(payslip.commission_payment_ids)

        # find unpaid commission payments from super().test_commission()
        commission_payments = self.env['hr.commission.payment'].search([
            ('employee_id', '=', self.employee.id),
        ])
        self.assertTrue(commission_payments)

        # press the button to pay it via payroll
        commission_payments.action_report_in_next_payslip()

        payslip.onchange_employee()
        # has attached commission payments
        self.assertTrue(payslip.commission_payment_ids)
        commission_input_lines = payslip.input_line_ids.filtered(lambda l: l.code == commission_code)
        self.assertTrue(commission_input_lines)
        self.assertEqual(sum(commission_input_lines.mapped('amount')),
                         sum(commission_payments.mapped('commission_amount')))
