Skip to content

Commit

Permalink
Merge pull request #731 from rufener/client-search
Browse files Browse the repository at this point in the history
Uniformization of client-search within a new component
  • Loading branch information
rufener authored Jun 12, 2023
2 parents 5e8ca88 + c2f609a commit f02c23e
Show file tree
Hide file tree
Showing 16 changed files with 617 additions and 426 deletions.
3 changes: 3 additions & 0 deletions back/infolica/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ def includeme(config):
config.add_route('client_by_id', '/infolica/api/clients/{id}')
config.add_route('recherche_clients', '/infolica/api/recherche_clients')
config.add_route('recherche_clients_s', '/infolica/api/recherche_clients/')
config.add_route('search_clients_by_term', '/infolica/api/search_clients_by_term')
config.add_route('client_moral_personnes_by_client_id', '/infolica/api/client_moral_personnes/{client_id}')
config.add_route('client_moral_personnes', '/infolica/api/client_moral_personnes')
config.add_route('client_moral_personnes_s', '/infolica/api/client_moral_personnes/')
config.add_route('search_clients_aggregated_by_term', '/infolica/api/search_clients_aggregated_by_term')
config.add_route('search_client_aggregated_by_id', '/infolica/api/search_client_aggregated/{id}')
#Affaires
config.add_route('affaires', '/infolica/api/affaires')
config.add_route('affaires_s', '/infolica/api/affaires/')
Expand Down
143 changes: 109 additions & 34 deletions back/infolica/views/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,64 @@
import json
from datetime import datetime
from sqlalchemy import cast, or_, String
import re


def _set_client_aggregated_name(client, sep=', '):
nom_ = sep.join(filter(None, [
client.entreprise,
' '.join(filter(None, [client.titre, client.prenom, client.nom])),
client.co,
client.adresse,
' '.join(filter(None, [client.npa, client.localite])),
'SAP: ' + (client.no_sap if client.no_sap is not None else '-'),
'BDP/BDEE: ' + (client.no_bdp_bdee if client.no_bdp_bdee is not None else '-')
]))

if client.sortie is not None:
nom_ = '(ancien client) ' + nom_

return nom_


def _multipleAttributesClientSearch(request, searchTerm, old_clients=False, search_limit='default'):
if search_limit == 'default':
search_limit = int(request.registry.settings['search_limit'])

searchTerms = re.split(r'\s|\,|\:|\-|SAP|BDP/BDEE', searchTerm.strip())
searchTerms = list(filter(None, searchTerms))

query = request.dbsession.query(Client)
if not old_clients:
query = query.filter(Client.sortie == None)

if len(searchTerms) > 0:
for term in searchTerms:
term = '%' + str(term) + '%'
query = query.filter(
or_(
Client.entreprise.ilike(term),
Client.titre.ilike(term),
Client.nom.ilike(term),
Client.prenom.ilike(term),
Client.co.ilike(term),
Client.adresse.ilike(term),
# cast(Client.npa, String).ilike(term),
# Client.localite.ilike(term),
# cast(Client.case_postale, String).ilike(term),
# cast(Client.tel_fixe, String).ilike(term),
# cast(Client.fax, String).ilike(term),
# cast(Client.tel_portable, String).ilike(term),
# Client.mail.ilike(term),
cast(Client.no_sap, String).ilike(term),
cast(Client.no_bdp_bdee, String).ilike(term),
# cast(Client.no_access, String).ilike(term),
)
)

results = query.limit(search_limit).all()

return results


@view_config(route_name='types_clients', request_method='GET', renderer='json')
Expand Down Expand Up @@ -57,6 +115,28 @@ def client_by_id_view(request):
return Utils.serialize_one(query)


@view_config(route_name='search_client_aggregated_by_id', request_method='GET', renderer='json')
def client_aggregated_by_id_view(request):
"""
Return client aggregated by id
"""
# Check connected
if not check_connected(request):
raise exc.HTTPForbidden()

id = request.matchdict['id']
result = request.dbsession.query(Client).filter(
Client.id == id).first()

client = {
'id': result.id,
'nom': _set_client_aggregated_name(result),
'type_client': result.type_client
}

return client


@view_config(route_name='recherche_clients', request_method='POST', renderer='json')
@view_config(route_name='recherche_clients_s', request_method='POST', renderer='json')
def clients_search_view(request):
Expand Down Expand Up @@ -94,53 +174,48 @@ def clients_search_view(request):
return Utils.serialize_many(query)


@view_config(route_name='recherche_clients', request_method='GET', renderer='json')
@view_config(route_name='search_clients_by_term', request_method='GET', renderer='json')
def clients_search_by_term_view(request):
"""
Search clients
Search clients by term
"""
# Check connected
if not check_connected(request):
raise exc.HTTPForbidden()

settings = request.registry.settings
search_limit = int(settings['search_limit'])
searchTerm = request.params["searchTerm"] if "searchTerm" in request.params else None
old_clients = request.params['old_clients'] == 'true' if 'old_clients' in request.params else False

searchTerms = searchTerm.strip().split(" ")
clients = _multipleAttributesClientSearch(request, searchTerm, old_clients=old_clients)

return Utils.serialize_many(clients)

query = request.dbsession.query(Client)
if not old_clients:
query = query.filter(Client.sortie == None)

if len(searchTerms) > 0:
for term in searchTerms:
term = '%' + str(term) + '%'
query = query.filter(
or_(
Client.entreprise.ilike(term),
Client.titre.ilike(term),
Client.nom.ilike(term),
Client.prenom.ilike(term),
Client.co.ilike(term),
Client.adresse.ilike(term),
cast(Client.npa, String).ilike(term),
Client.localite.ilike(term),
cast(Client.case_postale, String).ilike(term),
cast(Client.tel_fixe, String).ilike(term),
cast(Client.fax, String).ilike(term),
cast(Client.tel_portable, String).ilike(term),
Client.mail.ilike(term),
cast(Client.no_sap, String).ilike(term),
cast(Client.no_bdp_bdee, String).ilike(term),
cast(Client.no_access, String).ilike(term),
)
)

query = query.limit(search_limit).all()
@view_config(route_name='search_clients_aggregated_by_term', request_method='GET', renderer='json')
def clients_aggregated_search_by_term_view(request):
"""
Search clients aggregated
"""
# Check connected
if not check_connected(request):
raise exc.HTTPForbidden()

searchTerm = request.params["searchTerm"] if "searchTerm" in request.params else None
old_clients = request.params['old_clients'] == 'true' if 'old_clients' in request.params else False

query = _multipleAttributesClientSearch(request, searchTerm, old_clients=old_clients)

liste_clients = []
for client in query:
nom_ = _set_client_aggregated_name(client)

liste_clients.append({
'id': client.id,
'nom': nom_,
'type_client': client.type_client
})

return Utils.serialize_many(query)
return liste_clients


@view_config(route_name='clients', request_method='POST', renderer='json')
Expand Down
3 changes: 3 additions & 0 deletions front/.env
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ VUE_APP_LOGOUT_ENDPOINT = "/logout"

#Clients
VUE_APP_CLIENTS_ENDPOINT = "/clients"
VUE_APP_CLIENT_AGGREGATED_ENDPOINT = "/search_clients_aggregated_by_term"
VUE_APP_CLIENT_AGGREGATED_BY_ID_ENDPOINT = "/search_client_aggregated"
VUE_APP_SEARCH_CLIENTS_ENDPOINT = "/recherche_clients"
VUE_APP_SEARCH_CLIENTS_BY_TERM_ENDPOINT = "/search_clients_by_term"
VUE_APP_SEARCH_TYPES_CLIENTS_ENDPOINT = "/types_clients"
VUE_APP_CLIENT_MORAL_PERSONNES_ENDPOINT = "/client_moral_personnes"

Expand Down
39 changes: 9 additions & 30 deletions front/src/components/Affaires/Affaires.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import {
getTypesAffaires,
getEtapesAffaire,
getOperateurs,
stringifyAutocomplete,
stringifyAutocomplete2,
checkPermission,
getClientsByTerm,
setClientsAdresse_
} from "@/services/helper";
import {handleException} from '@/services/exceptionsHandler';
import ClientSearch from "@/components/Utils/ClientSearch/ClientSearch.vue";
const moment = require('moment');
export default {
name: "Affaires",
components: {
ClientSearch
},
props: {},
data: () => ({
affaires: [],
Expand All @@ -34,7 +35,7 @@ export default {
nom: null,
cadastre: "",
type: "",
client: null,
client_id: null,
operateur: null,
dateFrom: null,
dateTo: null,
Expand Down Expand Up @@ -88,29 +89,6 @@ export default {
});
},
/**
* searchClient
*/
async searchClient(searchTerm) {
let conditions = {
'searchTerm': searchTerm,
'old_clients': true
};
getClientsByTerm(conditions)
.then(response => {
if (response && response.data) {
let tmp = setClientsAdresse_(response.data);
tmp.forEach(x => {
if (x.sortie) {
x.adresse_ = "(ancien client) " + x.adresse_;
}
});
this.clients = stringifyAutocomplete(tmp, "adresse_");
}
}).catch(err => handleException(err, this));
},
/**
* init operateurs liste
*/
Expand Down Expand Up @@ -144,12 +122,13 @@ export default {
this.search.nom = null;
this.search.cadastre = "";
this.search.type = "";
this.search.client = null;
this.search.operateur = null;
this.search.dateFrom = null;
this.search.dateTo = null;
this.search.etape = null;
this.search.limitNbResults = true;
this.search.client_id = null;
this.$root.$emit('resetSearchClientTerm');
},
/*
Expand Down Expand Up @@ -178,8 +157,8 @@ export default {
formData.append("type_affaire", this.search.type);
}
if (this.search.client && this.search.client.id !== null) {
formData.append("client_id", this.search.client.id);
if (this.search.client_id) {
formData.append("client_id", this.search.client_id);
}
if (this.search.operateur && this.search.operateur.id !== null) {
Expand Down
Loading

0 comments on commit f02c23e

Please sign in to comment.