<commit_before>'use strict';

angular.module('ddsApp').controller('FoyerCaptureLogementModalCtrl', function($scope, $http, $modalInstance, logementTypes, locationTypes, loyerLabels, situation) {
    if (!situation.logement) {
        situation.logement = {};
    }
    if (!situation.logement.adresse) {
        situation.logement.adresse = {};
    }
    var logement = $scope.logement = situation.logement;
    $scope.logementTypes = logementTypes;
    $scope.locationTypes = locationTypes;
    $scope.loyerLabels = loyerLabels;

    var membreFamilleProprietaireCaptured = function() {
        return 'payant' === logement.type && angular.isDefined(logement.membreFamilleProprietaire);
    };

    $scope.captureMembreFamilleProprietaire = function() {
        if ('payant' === logement.type) {
            return true;
        } else if ('locataire' === logement.type) {
            return angular.isDefined(logement.colocation);
        }

        return false;
    };

    $scope.captureLocationType = function() {
        return 'locataire' === logement.type && angular.isDefined(logement.membreFamilleProprietaire);
    };

    $scope.captureLoyer = function() {
        if ('gratuit' === logement.type) {
            return false;
        }

        return _.any([
            true === logement.primoAccedant,
            angular.isDefined(logement.locationType),
            membreFamilleProprietaireCaptured()
        ]);
    };

    $scope.captureCodePostal = function() {
        return _.any([
            angular.isDefined(logement.primoAccedant),
            angular.isDefined(logement.locationType),
            'gratuit' === logement.type,
            membreFamilleProprietaireCaptured()
        ]);
    };

    $scope.changeLogementType = function() {
        ['colocation', 'locationType', 'membreFamilleProprietaire', 'primoAccedant', 'loyer'].forEach(function(field) {
            delete logement[field];
        });
        delete logement.adresse.codePostal;
        $scope.selectedCity = null;
        $scope.cities = [];
    };

    $scope.updateCities = function() {
        $scope.retrievingCities = false;
        $scope.cities = [];

        var codePostal = logement.adresse.codePostal;
        if (!codePostal || 5 !== codePostal.length) {
            $scope.unknownCodePostal = true;
            return;
        }

        $scope.retrievingCities = true;
        var baseApi = '//public.opendatasoft.com/api/records/1.0/search?dataset=correspondance-code-insee-code-postal&format=jsonp&callback=JSON_CALLBACK&rows=1000&q=';
        $http.jsonp(baseApi + codePostal).then(function(result) {
            var records = result.data.records;
            if (!records.length) {
                $scope.unknownCodePostal = true;
            } else {
                $scope.unknownCodePostal = false;
                $scope.cities = [];
                records.forEach(function(record) {
                    var field = 'nom_comm';
                    var nomCommune = record.fields[field];
                    field = 'insee_com';
                    var codeInsee = record.fields[field];
                    $scope.cities.push({nom: nomCommune, codeInsee: codeInsee});
                });<commit_msg>Utilise l'API pour les codes communes<commit_after>'use strict';

angular.module('ddsApp').controller('FoyerCaptureLogementModalCtrl', function($scope, $http, $modalInstance, logementTypes, locationTypes, loyerLabels, situation) {
    if (!situation.logement) {
        situation.logement = {};
    }
    if (!situation.logement.adresse) {
        situation.logement.adresse = {};
    }
    var logement = $scope.logement = situation.logement;
    $scope.logementTypes = logementTypes;
    $scope.locationTypes = locationTypes;
    $scope.loyerLabels = loyerLabels;

    var membreFamilleProprietaireCaptured = function() {
        return 'payant' === logement.type && angular.isDefined(logement.membreFamilleProprietaire);
    };

    $scope.captureMembreFamilleProprietaire = function() {
        if ('payant' === logement.type) {
            return true;
        } else if ('locataire' === logement.type) {
            return angular.isDefined(logement.colocation);
        }

        return false;
    };

    $scope.captureLocationType = function() {
        return 'locataire' === logement.type && angular.isDefined(logement.membreFamilleProprietaire);
    };

    $scope.captureLoyer = function() {
        if ('gratuit' === logement.type) {
            return false;
        }

        return _.any([
            true === logement.primoAccedant,
            angular.isDefined(logement.locationType),
            membreFamilleProprietaireCaptured()
        ]);
    };

    $scope.captureCodePostal = function() {
        return _.any([
            angular.isDefined(logement.primoAccedant),
            angular.isDefined(logement.locationType),
            'gratuit' === logement.type,
            membreFamilleProprietaireCaptured()
        ]);
    };

    $scope.changeLogementType = function() {
        ['colocation', 'locationType', 'membreFamilleProprietaire', 'primoAccedant', 'loyer'].forEach(function(field) {
            delete logement[field];
        });
        delete logement.adresse.codePostal;
        $scope.selectedCity = null;
        $scope.cities = [];
    };

    $scope.updateCities = function() {
        $scope.retrievingCities = false;
        $scope.cities = [];

        var codePostal = logement.adresse.codePostal;
        if (!codePostal || 5 !== codePostal.length) {
            $scope.unknownCodePostal = true;
            return;
        }

        $scope.retrievingCities = true;
        $http.get('/api/outils/communes', { params: { codePostal: codePostal } }).then(function(result) {
            var records = result.data.records;
            if (!records.length) {
                $scope.unknownCodePostal = true;
            } else {
                $scope.unknownCodePostal = false;
                $scope.cities = [];
                records.forEach(function(record) {
                    var field = 'nom_comm';
                    var nomCommune = record.fields[field];
                    field = 'insee_com';
                    var codeInsee = record.fields[field];
                    $scope.cities.push({nom: nomCommune, codeInsee: codeInsee});
                });
{'fraction_non_alphanumeric': 0.1243498817966903, 'fraction_numerical': 0.001260835303388495, 'mean_word_length': 2.2460358056265983, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5928950', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1838, 'n_words': 283}