# barbacompta - invoicing for dummies
# Copyright (C) 2022 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import pytest

from eo_gestion.eo_facture.models import Contrat


class TestLoggedIn:
    @pytest.fixture
    def app(self, app):
        # login
        response = app.get('/').follow()
        response.form['username'] = 'admin'
        response.form['password'] = 'admin'
        response.form.submit()
        return app

    def test_contrat_recurrent_no_debut(self, app):
        response = app.get('/eo_facture/contrat/add/')
        response.form['client'].force_value('1')
        response.form['intitule'] = 'Contrat 1'
        response.form['periodicite'] = 'annuelle'
        response = response.form.submit('_continue')
        assert len(response.pyquery('.errorlist'))

    def test_contrat_recurrent_debut_changed(self, app):
        response = app.get('/eo_facture/contrat/add/')
        response.form['client'].force_value('1')
        response.form['intitule'] = 'Contrat 1'
        response.form['periodicite'] = 'annuelle'
        response.form['periodicite_debut'] = '2018-12-01'
        response = response.form.submit('_continue').follow()
        response.forms['contrat_form']['periodicite_debut'] = '2018-12-02'
        response = response.forms['contrat_form'].submit('_continue').follow()

        # Créer la facture de première échéance
        response = app.get('/')
        assert 'facturer-echeance' in response.form.action
        response = response.form.submit().follow()

    def test_avoir_sur_contrat_recurrent(self, app):
        response = app.get('/eo_facture/contrat/add/')
        response.form['client'].force_value('1')
        response.form['intitule'] = 'Contrat 1'
        response.form['periodicite'] = 'annuelle'
        response.form['periodicite_debut'] = '2018-12-01'
        response = response.form.submit('_continue').follow()

        # Créer la facture de première échéance
        response = app.get('/')
        assert 'facturer-echeance' in response.form.action
        response = response.form.submit().follow()
        response.form['lignes-0-intitule'] = 'Bricole 1'
        response.form['lignes-0-prix_unitaire_ht'] = '200'
        response = response.form.submit('_continue').follow()
        assert response.html.find('label', {'for': 'id_numero_d_echeance'})

        contrat = Contrat.objects.get(intitule='Contrat 1')
        facture = contrat.factures.get()
        assert facture.lignes.get().intitule == 'Bricole 1'

        # Créer un avoir sur la première échéance
        assert '/eo_facture/facture/%s/cancel/' % facture.id in response.text
        response = app.get('/eo_facture/facture/%s/cancel/' % facture.id).follow()
        assert not response.html.find('label', {'for': 'id_numero_d_echeance'})
        response = response.form.submit('_save').follow()

        assert contrat.factures.count() == 2
        facture_avoir = facture.factures_avoir.get()
        assert facture_avoir.intitule == 'AVOIR POUR LA FACTURE Contrat 1 du 01/12/2018 au 01/12/2019'
