diff --git a/back/infolica/routes.py b/back/infolica/routes.py index 5806e617..e104ceb3 100644 --- a/back/infolica/routes.py +++ b/back/infolica/routes.py @@ -98,6 +98,7 @@ def includeme(config): config.add_route('numeros_relations','/infolica/api/numeros_relations') config.add_route('numeros_relations_s','/infolica/api/numeros_relations/') config.add_route('numeros_relation_by_affaire_id','/infolica/api/affaire_numeros_relations/{id}') + config.add_route('new_ddp','/infolica/api/new_ddp') # config.add_route('numeros_relations_by_numeroBase','/infolica/api/numeros_relations_by_numeroBase_id') # config.add_route('numeros_relations_by_numeroBase_s','/infolica/api/numeros_relations_by_numeroBase_id/') config.add_route('numeros_differes','/infolica/api/numeros_differes') diff --git a/back/infolica/scripts/utils.py b/back/infolica/scripts/utils.py index 5e33e280..d7b14c03 100644 --- a/back/infolica/scripts/utils.py +++ b/back/infolica/scripts/utils.py @@ -317,7 +317,7 @@ def check_unread_preavis_remarks(cls, request, affaire_id, service_id=None): PreavisRemarque.lu_operateur_id == None, ) - if not service_id is None: + if service_id is not None: preavis_remarques = preavis_remarques.filter(Preavis.service_id == service_id) preavis_remarques = preavis_remarques.all() diff --git a/back/infolica/views/facture_emolument.py b/back/infolica/views/facture_emolument.py index 34a7eda3..861a3433 100644 --- a/back/infolica/views/facture_emolument.py +++ b/back/infolica/views/facture_emolument.py @@ -1,20 +1,18 @@ # -*- coding: utf-8 -*-- -from sqlalchemy.sql.elements import and_ -from pyramid.view import view_config -import pyramid.httpexceptions as exc -from pyramid.response import Response -from sqlalchemy import or_, func - +import datetime +import json +import math +import pyramid.httpexceptions as exc +import requests from infolica.models.constant import Constant -from infolica.models.models import Affaire, Facture, Numero, TableauEmoluments, VAffaire, VNumerosAffaires -from infolica.models.models import EmolumentAffaire, Emolument, EmolumentAffaireRepartition, FactureParametres -from infolica.scripts.utils import Utils +from infolica.models.models import Affaire, AffaireNumero, Cadastre, Emolument, EmolumentAffaire, EmolumentAffaireRepartition, Facture, FactureParametres, Numero, TableauEmoluments, VAffaire, VNumerosAffaires from infolica.scripts.authentication import check_connected -import json -import datetime -import requests -import math +from infolica.scripts.utils import Utils +from pyramid.response import Response +from pyramid.view import view_config +from sqlalchemy import func, or_ +from sqlalchemy.sql.elements import and_ ########################################################### # EMOLUMENTS @@ -25,7 +23,35 @@ def _round_nearest(x, a=0.05): return round(round(x / a) * a, -int(math.floor(math.log10(a)))) -@view_config(route_name='facture_parametres', request_method='GET', renderer='json') +def _numeros_emoluments_affaire(request, affaire_id): + affaire_numero_type_ancien_id = int(request.registry.settings["affaire_numero_type_ancien_id"]) + + num_aff = ( + request.dbsession.query(Numero.id, Numero.numero, Cadastre.nom) + .join(Cadastre, Cadastre.id == Numero.cadastre_id) + .join(AffaireNumero, AffaireNumero.numero_id == Numero.id) + .filter(AffaireNumero.affaire_id == affaire_id, AffaireNumero.type_id == affaire_numero_type_ancien_id) + .order_by(Cadastre.nom, Numero.numero) + .all() + ) + + num_emol = request.dbsession.query(EmolumentAffaire).filter(EmolumentAffaire.affaire_id == affaire_id).all() + + numeros = [] + for num_aff_i in num_aff: + numero_i = {"numero_id": num_aff_i.id, "numero": num_aff_i.numero, "cadastre": num_aff_i.nom, "emolument_affaire_id": None} + + for ne in num_emol: + if num_aff_i.id in ne.numeros_id: + numero_i["emolument_affaire_id"] = ne.id + break + + numeros.append(numero_i) + + return numeros + + +@view_config(route_name="facture_parametres", request_method="GET", renderer="json") def tableau_facture_parametres_view(request): """ Return actual parameters and values @@ -33,27 +59,29 @@ def tableau_facture_parametres_view(request): # Check connected if not check_connected(request): raise exc.HTTPForbidden() - + today = datetime.date.today() - results = request.dbsession.query(FactureParametres).filter( - FactureParametres.valable_de <= today, - or_( - FactureParametres.valable_a >= today, - FactureParametres.valable_a == None, + results = ( + request.dbsession.query(FactureParametres) + .filter( + FactureParametres.valable_de <= today, + or_( + FactureParametres.valable_a >= today, + FactureParametres.valable_a == None, + ), ) - ).all() + .all() + ) params = {} for result in results: - if not result.nom in params.keys(): + if result.nom not in params.keys(): params[result.nom] = result.valeur - return { 'facture_parametres': params } + return {"facture_parametres": params} - - -@view_config(route_name='emolument_affaire', request_method='GET', renderer='json') +@view_config(route_name="emolument_affaire", request_method="GET", renderer="json") def emolument_affaire_view(request): """ Return emolument_affaire @@ -62,123 +90,96 @@ def emolument_affaire_view(request): if not check_connected(request): raise exc.HTTPForbidden() - affaire_id = request.params['affaire_id'] if 'affaire_id' in request.params else None + affaire_id = request.params["affaire_id"] if "affaire_id" in request.params else None query = request.dbsession.query(EmolumentAffaire) - - if not affaire_id is None: + + if affaire_id is not None: query = query.filter(EmolumentAffaire.affaire_id == affaire_id) - + emolument_affaire = query.all() - - + # Récupérer les données des bâtiments result = [] for emolument_affaire_i in emolument_affaire: - query_bat = request.dbsession.query( - Emolument.batiment, - Emolument.batiment_f - ).filter( - Emolument.emolument_affaire_id == emolument_affaire_i.id - ).filter( - Emolument.batiment > 0 - ).group_by( - Emolument.batiment, - Emolument.batiment_f - ).order_by( - Emolument.batiment.asc() # Really important with respect to implementation of loading form_detail_batiment in front !! - ).all() - + query_bat = ( + request.dbsession.query(Emolument.batiment, Emolument.batiment_f) + .filter(Emolument.emolument_affaire_id == emolument_affaire_i.id) + .filter(Emolument.batiment > 0) + .group_by(Emolument.batiment, Emolument.batiment_f) + .order_by( + Emolument.batiment.asc() # Really important with respect to implementation of loading form_detail_batiment in front !! + ) + .all() + ) + batiment_f = [y for _, y in query_bat] numeros = [] numeros_id = [] if emolument_affaire_i.numeros_id and len(emolument_affaire_i.numeros_id) > 0: numeros_id = emolument_affaire_i.numeros_id - numeros = Utils.serialize_many(request.dbsession.query(VNumerosAffaires).filter( - and_( - VNumerosAffaires.numero_id.in_(tuple(emolument_affaire_i.numeros_id)), - VNumerosAffaires.affaire_id == emolument_affaire_i.affaire_id - ) - ).all()) - + numeros = Utils.serialize_many(request.dbsession.query(VNumerosAffaires).filter(and_(VNumerosAffaires.numero_id.in_(tuple(emolument_affaire_i.numeros_id)), VNumerosAffaires.affaire_id == emolument_affaire_i.affaire_id)).all()) + # Récupérer les liens sur la facture (répartition des montants) - facture_repartition = Utils.serialize_many( - request.dbsession.query(EmolumentAffaireRepartition).filter( - EmolumentAffaireRepartition.emolument_affaire_id == emolument_affaire_i.id - ).all() - ) + facture_repartition = Utils.serialize_many(request.dbsession.query(EmolumentAffaireRepartition).filter(EmolumentAffaireRepartition.emolument_affaire_id == emolument_affaire_i.id).all()) result.append( Utils._params( - id = emolument_affaire_i.id, - affaire_id = emolument_affaire_i.affaire_id, - pente_pc = emolument_affaire_i.pente_pc, - diff_visibilite_pc = emolument_affaire_i.diff_visibilite_pc, - trafic_pc = emolument_affaire_i.trafic_pc, - zi = emolument_affaire_i.zi , - indice_application = emolument_affaire_i.indice_application, - tva_pc = emolument_affaire_i.tva_pc, - remarque = emolument_affaire_i.remarque, - nb_batiments = len(batiment_f), - batiment_f = batiment_f, - numeros_id = numeros_id, - numeros = numeros, - facture_type_id = emolument_affaire_i.facture_type_id, - utilise = emolument_affaire_i.utilise, - facture_repartition = facture_repartition + id=emolument_affaire_i.id, + affaire_id=emolument_affaire_i.affaire_id, + pente_pc=emolument_affaire_i.pente_pc, + diff_visibilite_pc=emolument_affaire_i.diff_visibilite_pc, + trafic_pc=emolument_affaire_i.trafic_pc, + zi=emolument_affaire_i.zi, + indice_application=emolument_affaire_i.indice_application, + tva_pc=emolument_affaire_i.tva_pc, + remarque=emolument_affaire_i.remarque, + nb_batiments=len(batiment_f), + batiment_f=batiment_f, + numeros_id=numeros_id, + numeros=numeros, + facture_type_id=emolument_affaire_i.facture_type_id, + utilise=emolument_affaire_i.utilise, + facture_repartition=facture_repartition, ) ) - + return result -@view_config(route_name='emolument', request_method='GET', renderer='json') +@view_config(route_name="emolument", request_method="GET", renderer="json") def emolument_view(request): """ - Return emoluments of emoluments_affaire + Return emoluments of emoluments_affaire """ if not check_connected(request): raise exc.HTTPForbidden() today = datetime.date.today() - emolument_affaire_id = request.params['emolument_affaire_id'] if 'emolument_affaire_id' in request.params else None - emoluments_divers_tarifhoraire_id = int(request.registry.settings['emoluments_divers_tarifhoraire_id']) + emolument_affaire_id = request.params["emolument_affaire_id"] if "emolument_affaire_id" in request.params else None + emoluments_divers_tarifhoraire_id = int(request.registry.settings["emoluments_divers_tarifhoraire_id"]) - emol_affaire = request.dbsession.query(EmolumentAffaire).filter( - EmolumentAffaire.id==emolument_affaire_id - ).first() + emol_affaire = request.dbsession.query(EmolumentAffaire).filter(EmolumentAffaire.id == emolument_affaire_id).first() form_general = Utils.serialize_one(emol_affaire) if emol_affaire.utilise is True: today_last = today fact = request.dbsession.query(Facture).join(EmolumentAffaireRepartition).filter(EmolumentAffaireRepartition.emolument_affaire_id == emol_affaire.id).first() - today = fact.date + today = getattr(fact, "date", None) if today is None: today = request.dbsession.query(Affaire.date_envoi).filter(Affaire.id == emol_affaire.affaire_id).scalar() if today is None: today = today_last - nb_batiments = request.dbsession.query(func.max(Emolument.batiment)).filter( - Emolument.emolument_affaire_id==emolument_affaire_id - ).scalar() - if nb_batiments is None: nb_batiments = 0 + nb_batiments = request.dbsession.query(func.max(Emolument.batiment)).filter(Emolument.emolument_affaire_id == emolument_affaire_id).scalar() + if nb_batiments is None: + nb_batiments = 0 - - existing_emoluments_query = request.dbsession.query(Emolument).filter( - Emolument.emolument_affaire_id==emolument_affaire_id - ) + existing_emoluments_query = request.dbsession.query(Emolument).filter(Emolument.emolument_affaire_id == emolument_affaire_id) # emoluments - table = request.dbsession.query( - TableauEmoluments - ).filter( - TableauEmoluments.date_entree <= today, - or_( - TableauEmoluments.date_sortie > today, - TableauEmoluments.date_sortie == None - ) - ).order_by(TableauEmoluments.categorie_id, TableauEmoluments.sous_categorie_id, TableauEmoluments.ordre).all() + table = request.dbsession.query(TableauEmoluments).filter(TableauEmoluments.date_entree <= today, or_(TableauEmoluments.date_sortie > today, TableauEmoluments.date_sortie == None)).order_by(TableauEmoluments.categorie_id, TableauEmoluments.sous_categorie_id, TableauEmoluments.ordre).all() emoluments = [] divers_tarifhoraire = [] @@ -187,16 +188,16 @@ def emolument_view(request): last_sous_categorie_id = "" categorie = [] sous_categorie = [] - batiments_f = [0]*nb_batiments + batiments_f = [0] * nb_batiments for position in table: if position.id == emoluments_divers_tarifhoraire_id: - existing_emoluments = existing_emoluments_query.filter(Emolument.tableau_emolument_id==int(emoluments_divers_tarifhoraire_id)).all() + existing_emoluments = existing_emoluments_query.filter(Emolument.tableau_emolument_id == int(emoluments_divers_tarifhoraire_id)).all() for ee in existing_emoluments: position_ = { - 'nom': ee.position, - 'nombre': ee.nombre, - 'montant': ee.prix_unitaire, - 'prix': ee.montant, + "nom": ee.position, + "nombre": ee.nombre, + "montant": ee.prix_unitaire, + "prix": ee.montant, } divers_tarifhoraire.append(position_) continue @@ -213,16 +214,16 @@ def emolument_view(request): categorie = [] position_ = Utils.serialize_one(position) - position_['nombre'] = [0]*(nb_batiments +1) - position_['prix'] = [0]*(nb_batiments +1) - + position_["nombre"] = [0] * (nb_batiments + 1) + position_["prix"] = [0] * (nb_batiments + 1) + # check if emolument already exists un database - existing_emoluments = existing_emoluments_query.filter(Emolument.tableau_emolument_id==int(position_['id'])).all() + existing_emoluments = existing_emoluments_query.filter(Emolument.tableau_emolument_id == int(position_["id"])).all() for ee in existing_emoluments: - position_['nombre'][ee.batiment] = ee.nombre - position_['prix'][ee.batiment] = ee.montant - if ee.batiment > 0 and batiments_f[ee.batiment-1] == 0: - batiments_f[ee.batiment-1] = ee.batiment_f + position_["nombre"][ee.batiment] = ee.nombre + position_["prix"][ee.batiment] = ee.montant + if ee.batiment > 0 and batiments_f[ee.batiment - 1] == 0: + batiments_f[ee.batiment - 1] = ee.batiment_f sous_categorie.append(position_) @@ -232,61 +233,59 @@ def emolument_view(request): categorie.append(sous_categorie) emoluments.append(categorie) - form_general['batiment_f'] = batiments_f - form_general['nb_batiments'] = len(batiments_f) + form_general["batiment_f"] = batiments_f + form_general["nb_batiments"] = len(batiments_f) - return {'emoluments': json.dumps(emoluments), 'form_general': json.dumps(form_general), 'divers_tarifhoraire': json.dumps(divers_tarifhoraire)} + numeros = _numeros_emoluments_affaire(request, form_general["affaire_id"]) + return {"emoluments": json.dumps(emoluments), "form_general": json.dumps(form_general), "divers_tarifhoraire": json.dumps(divers_tarifhoraire), "numeros": json.dumps(numeros)} -@view_config(route_name='emolument', request_method='POST', renderer='json') +@view_config(route_name="emolument", request_method="POST", renderer="json") def emolument_new_view(request): """ Add new emolument """ # Check authorization - if not Utils.has_permission(request, request.registry.settings['affaire_facture_edition']): + if not Utils.has_permission(request, request.registry.settings["affaire_facture_edition"]): raise exc.HTTPForbidden() - emoluments_divers_tarifhoraire_id = int(request.registry.settings['emoluments_divers_tarifhoraire_id']) + emoluments_divers_tarifhoraire_id = int(request.registry.settings["emoluments_divers_tarifhoraire_id"]) - form_general = json.loads(request.params['form_general']) if 'form_general' in request.params else None - emoluments = json.loads(request.params['emoluments']) if 'emoluments' in request.params else None - divers_tarifhoraire = json.loads(request.params['divers_tarifhoraire']) if 'divers_tarifhoraire' in request.params else None + form_general = json.loads(request.params["form_general"]) if "form_general" in request.params else None + emoluments = json.loads(request.params["emoluments"]) if "emoluments" in request.params else None + divers_tarifhoraire = json.loads(request.params["divers_tarifhoraire"]) if "divers_tarifhoraire" in request.params else None # save form_general - if form_general['id'] is None: + if form_general["id"] is None: emol_affaire = EmolumentAffaire() emol_affaire = Utils.set_model_record(emol_affaire, form_general) request.dbsession.add(emol_affaire) request.dbsession.flush() else: - emol_affaire = request.dbsession.query(EmolumentAffaire).filter(EmolumentAffaire.id==form_general['id']).first() + emol_affaire = request.dbsession.query(EmolumentAffaire).filter(EmolumentAffaire.id == form_general["id"]).first() emol_affaire = Utils.set_model_record(emol_affaire, form_general) request.dbsession.flush() - # save form_general - existing_emoluments = request.dbsession.query(Emolument).filter( - Emolument.emolument_affaire_id==emol_affaire.id - ).all() + existing_emoluments = request.dbsession.query(Emolument).filter(Emolument.emolument_affaire_id == emol_affaire.id).all() for emol in existing_emoluments: request.dbsession.delete(emol) for category in emoluments: for scategory in category: for position in scategory: - for i in range(len(position['nombre'])): - if float(position['nombre'][i] or 0) > 0 or float(position['prix'][i] or 0) > 0: + for i in range(len(position["nombre"])): + if float(position["nombre"][i] or 0) > 0 or float(position["prix"][i] or 0) > 0: params = Utils._params( emolument_affaire_id=emol_affaire.id, - tableau_emolument_id=int(position['id']), - position=position['nom'], - prix_unitaire=_round_nearest(float(position['montant']) or 0), - nombre=float(position['nombre'][i] or '0'), + tableau_emolument_id=int(position["id"]), + position=position["nom"], + prix_unitaire=_round_nearest(float(position["montant"]) or 0), + nombre=float(position["nombre"][i] or "0"), batiment=i, - batiment_f=1 if i == 0 else float(form_general['batiment_f'][i-1]), - montant=_round_nearest(float(position['prix'][i]) or 0) + batiment_f=1 if i == 0 else float(form_general["batiment_f"][i - 1]), + montant=_round_nearest(float(position["prix"][i]) or 0), ) # save emolument @@ -295,20 +294,11 @@ def emolument_new_view(request): request.dbsession.add(record) - # save divers tarif horaire for dth in divers_tarifhoraire: - if dth['nom'] is not None and dth['nombre'] is not None and dth['montant'] is not None and \ - dth['nom'] != '' and dth['nombre'] != '' and dth['montant'] != '': + if dth["nom"] is not None and dth["nombre"] is not None and dth["montant"] is not None and dth["nom"] != "" and dth["nombre"] != "" and dth["montant"] != "": params = Utils._params( - emolument_affaire_id=emol_affaire.id, - tableau_emolument_id=emoluments_divers_tarifhoraire_id, - position=dth['nom'], - prix_unitaire=_round_nearest(float(dth['montant']) or 0), - nombre=float(dth['nombre']), - batiment=0, - batiment_f=1, - montant=_round_nearest(float(dth['prix']) or 0) + emolument_affaire_id=emol_affaire.id, tableau_emolument_id=emoluments_divers_tarifhoraire_id, position=dth["nom"], prix_unitaire=_round_nearest(float(dth["montant"]) or 0), nombre=float(dth["nombre"]), batiment=0, batiment_f=1, montant=_round_nearest(float(dth["prix"]) or 0) ) # save emolument @@ -317,53 +307,55 @@ def emolument_new_view(request): request.dbsession.add(record) - return {'emolument_affaire_id': emol_affaire.id} + return {"emolument_affaire_id": emol_affaire.id} -@view_config(route_name='emolument', request_method='DELETE', renderer='json') +@view_config(route_name="emolument", request_method="DELETE", renderer="json") def emolument_delete_view(request): """ Delete emolument """ # Check authorization - if not Utils.has_permission(request, request.registry.settings['affaire_facture_edition']): + if not Utils.has_permission(request, request.registry.settings["affaire_facture_edition"]): raise exc.HTTPForbidden() - emolument_affaire_id = request.params['emolument_affaire_id'] if 'emolument_affaire_id' in request.params else None + emolument_affaire_id = request.params["emolument_affaire_id"] if "emolument_affaire_id" in request.params else None # get emolument affaire - emol_affaire = request.dbsession.query(EmolumentAffaire).filter(EmolumentAffaire.id==emolument_affaire_id).first() + emol_affaire = request.dbsession.query(EmolumentAffaire).filter(EmolumentAffaire.id == emolument_affaire_id).first() # delete linked emoluments - existing_emoluments = request.dbsession.query(Emolument).filter( - Emolument.emolument_affaire_id==emol_affaire.id - ).all() + existing_emoluments = request.dbsession.query(Emolument).filter(Emolument.emolument_affaire_id == emol_affaire.id).all() for emol in existing_emoluments: request.dbsession.delete(emol) + # get emolument_affaire_repartition and delete them if exist + ear = request.dbsession.query(EmolumentAffaireRepartition).filter(EmolumentAffaireRepartition.emolument_affaire_id == emolument_affaire_id).all() + for ear_i in ear: + request.dbsession.delete(ear_i) + + request.dbsession.flush() + # finally delete emolument affaire request.dbsession.delete(emol_affaire) return Utils.get_data_save_response(Constant.SUCCESS_DELETE.format(EmolumentAffaire.__tablename__)) - -@view_config(route_name='emolument_affaire_freeze', request_method='PUT', renderer='json') +@view_config(route_name="emolument_affaire_freeze", request_method="PUT", renderer="json") def update_emolument_affaire_freeze_view(request): """ Freeze emolument_affaire """ # Check authorization - if not Utils.has_permission(request, request.registry.settings['affaire_facture_edition']): + if not Utils.has_permission(request, request.registry.settings["affaire_facture_edition"]): raise exc.HTTPForbidden() params = request.params - - record_id = params['emolument_affaire_id'] if 'emolument_affaire_id' in params else None - record = request.dbsession.query(EmolumentAffaire).filter( - EmolumentAffaire.id == record_id - ).first() + record_id = params["emolument_affaire_id"] if "emolument_affaire_id" in params else None + + record = request.dbsession.query(EmolumentAffaire).filter(EmolumentAffaire.id == record_id).first() record = Utils.set_model_record(record, params) @@ -374,7 +366,8 @@ def update_emolument_affaire_freeze_view(request): ### EMOLUMENT AFFAIRE REPARTITION ### ####################################### -@view_config(route_name='emolument_affaire_repartiton', request_method='GET', renderer='json') + +@view_config(route_name="emolument_affaire_repartiton", request_method="GET", renderer="json") def emolument_affaire_repartiton_view(request): """ get emolument_affaire_repartiton @@ -384,127 +377,115 @@ def emolument_affaire_repartiton_view(request): raise exc.HTTPForbidden() records = request.dbsession.query(EmolumentAffaireRepartition) - + if "emolument_affaire_id" in request.params: records = records.filter(EmolumentAffaireRepartition.emolument_affaire_id == request.params["emolument_affaire_id"]) - + if "facture_id" in request.params: records = records.filter(EmolumentAffaireRepartition.facture_id == request.params["facture_id"]) - + records = records.all() - if len(records)>0: + if len(records) > 0: return Utils.serialize_many(records) else: return [] -@view_config(route_name='emolument_affaire_repartiton', request_method='POST', renderer='json') +@view_config(route_name="emolument_affaire_repartiton", request_method="POST", renderer="json") def emolument_affaire_repartiton_new_view(request): """ Add new emolument_affaire_repartiton """ # Check authorization - if not Utils.has_permission(request, request.registry.settings['affaire_facture_edition']): + if not Utils.has_permission(request, request.registry.settings["affaire_facture_edition"]): raise exc.HTTPForbidden() emolument_affaire_id = request.params["emolument_affaire_id"] if "emolument_affaire_id" in request.params else None emolument_facture_repartition = json.loads(request.params["emolument_facture_repartition"]) if "emolument_facture_repartition" in request.params else None # get records of current emolument_affaire_id - emolumentAffaireRepartition = request.dbsession.query(EmolumentAffaireRepartition).filter( - EmolumentAffaireRepartition.emolument_affaire_id == emolument_affaire_id - ).all() - + emolumentAffaireRepartition = request.dbsession.query(EmolumentAffaireRepartition).filter(EmolumentAffaireRepartition.emolument_affaire_id == emolument_affaire_id).all() # iterate through emolument_facture_repartition for efr_i in emolument_facture_repartition: # test if efr_i exists already in db record = None for idx, eaf_i in enumerate(emolumentAffaireRepartition): - if eaf_i.facture_id == efr_i['id']: + if eaf_i.facture_id == efr_i["id"]: # La relation existe déjà, la modifier record = emolumentAffaireRepartition.pop(idx) break - - if not record is None: - if float(record.repartition) != float(efr_i['emolument_repartition']): - if float(efr_i['emolument_repartition']) == 0: + + if record is not None: + if float(record.repartition) != float(efr_i["emolument_repartition"]): + if float(efr_i["emolument_repartition"]) == 0: # supprimer l'entrée car la répartition est nulle request.dbsession.delete(record) else: # enregistrer la nouvelle répartition - params = Utils._params( - facture_id = efr_i['id'], - emolument_affaire_id = emolument_affaire_id, - repartition = efr_i['emolument_repartition'] - ) + params = Utils._params(facture_id=efr_i["id"], emolument_affaire_id=emolument_affaire_id, repartition=efr_i["emolument_repartition"]) record = Utils.set_model_record(record, params) - + else: # Créer l'entrée inexistante et si la répartition est non nulle - if float(efr_i['emolument_repartition']) > 0: + if float(efr_i["emolument_repartition"]) > 0: record = EmolumentAffaireRepartition() - params = Utils._params( - facture_id = efr_i['id'], - emolument_affaire_id = emolument_affaire_id, - repartition = efr_i['emolument_repartition'] - ) + params = Utils._params(facture_id=efr_i["id"], emolument_affaire_id=emolument_affaire_id, repartition=efr_i["emolument_repartition"]) record = Utils.set_model_record(record, params) request.dbsession.add(record) # remove items in db not posted for item in emolumentAffaireRepartition: - request.dbsession.delete(item) + request.dbsession.delete(item) return Utils.get_data_save_response(Constant.SUCCESS_SAVE.format(EmolumentAffaireRepartition.__tablename__)) -@view_config(route_name='emolument_affaire_repartiton', request_method='DELETE', renderer='json') +@view_config(route_name="emolument_affaire_repartiton", request_method="DELETE", renderer="json") def emolument_affaire_repartiton_delete_view(request): """ Delete emolument_affaire_repartiton by emolument_affaire_id """ # Check authorization - if not Utils.has_permission(request, request.registry.settings['affaire_facture_edition']): + if not Utils.has_permission(request, request.registry.settings["affaire_facture_edition"]): raise exc.HTTPForbidden() emolument_affaire_id = request.params["emolument_affaire_id"] if "emolument_affaire_id" in request.params else None - records = request.dbsession.query(EmolumentAffaireRepartition).filter( - EmolumentAffaireRepartition.emolument_affaire_id == emolument_affaire_id - ).all() + records = request.dbsession.query(EmolumentAffaireRepartition).filter(EmolumentAffaireRepartition.emolument_affaire_id == emolument_affaire_id).all() for record in records: request.dbsession.delete(record) - -@view_config(route_name='tableau_emoluments_new', request_method='GET', renderer='json') +@view_config(route_name="tableau_emoluments_new", request_method="GET", renderer="json") def tableau_emoluments_new_view(request): """ Get tableau of emoluments """ # Check authorization - if not Utils.has_permission(request, request.registry.settings['affaire_facture_edition']): + if not Utils.has_permission(request, request.registry.settings["affaire_facture_edition"]): raise exc.HTTPForbidden() + affaire_id = request.params["affaire_id"] if "affaire_id" in request.params else None + today = datetime.date.today() - emoluments_divers_tarifhoraire_id = int(request.registry.settings['emoluments_divers_tarifhoraire_id']) + emoluments_divers_tarifhoraire_id = int(request.registry.settings["emoluments_divers_tarifhoraire_id"]) # get active emoluments - table = request.dbsession.query( - TableauEmoluments - ).filter( - TableauEmoluments.date_entree <= today, - or_( - TableauEmoluments.date_sortie > today, - TableauEmoluments.date_sortie == None - ), - TableauEmoluments.id != emoluments_divers_tarifhoraire_id, # dont take into account diver-tarif-horaire which is generated by the front of the application - ).order_by(TableauEmoluments.categorie_id, TableauEmoluments.sous_categorie_id, TableauEmoluments.ordre).all() + table = ( + request.dbsession.query(TableauEmoluments) + .filter( + TableauEmoluments.date_entree <= today, + or_(TableauEmoluments.date_sortie > today, TableauEmoluments.date_sortie == None), + TableauEmoluments.id != emoluments_divers_tarifhoraire_id, # dont take into account diver-tarif-horaire which is generated by the front of the application + ) + .order_by(TableauEmoluments.categorie_id, TableauEmoluments.sous_categorie_id, TableauEmoluments.ordre) + .all() + ) result = [] c = 0 @@ -530,11 +511,12 @@ def tableau_emoluments_new_view(request): categorie.append(sous_categorie) result.append(categorie) - return result + numeros = _numeros_emoluments_affaire(request, affaire_id) + return {"emoluments": result, "numeros": numeros} -@view_config(route_name='export_emoluments_pdf', request_method='POST') +@view_config(route_name="export_emoluments_pdf", request_method="POST") def export_emoluments_pdf_view(request): """ Create PDF of emoluments @@ -546,21 +528,15 @@ def export_emoluments_pdf_view(request): now = datetime.datetime.now() # get request params - tableau_emoluments_id = request.params['tableau_emoluments_id'] if 'tableau_emoluments_id' in request.params else None - affaire_id = request.params['affaire_id'] if 'affaire_id' in request.params else None - tableau_emoluments_html = request.params['tableau_emoluments_html'] if 'tableau_emoluments_html' in request.params else None - tableau_recapitulatif_html = request.params['tableau_recapitulatif_html'] if 'tableau_recapitulatif_html' in request.params else None + tableau_emoluments_id = request.params["tableau_emoluments_id"] if "tableau_emoluments_id" in request.params else None + affaire_id = request.params["affaire_id"] if "affaire_id" in request.params else None + tableau_emoluments_html = request.params["tableau_emoluments_html"] if "tableau_emoluments_html" in request.params else None + tableau_recapitulatif_html = request.params["tableau_recapitulatif_html"] if "tableau_recapitulatif_html" in request.params else None # get facture_id - factures = request.dbsession.query( - Facture - ).join( - EmolumentAffaireRepartition - ).filter( - EmolumentAffaireRepartition.emolument_affaire_id == tableau_emoluments_id - ).all() - - #get affaire + factures = request.dbsession.query(Facture).join(EmolumentAffaireRepartition).filter(EmolumentAffaireRepartition.emolument_affaire_id == tableau_emoluments_id).all() + + # get affaire affaire = request.dbsession.query(VAffaire).filter(VAffaire.id == affaire_id).first() # get bf_emolument @@ -569,13 +545,10 @@ def export_emoluments_pdf_view(request): if numeros_id is not None: emolument_bf_html = [] for num_id in numeros_id: - emolument_bf_html.append( - str(request.dbsession.query(Numero).filter(Numero.id == num_id).first().numero) - ) + emolument_bf_html.append(str(request.dbsession.query(Numero).filter(Numero.id == num_id).first().numero)) emolument_bf_html = ", ".join(emolument_bf_html) - d = {"now": now.strftime("%d.%m.%Y, %H:%M:%S")} header_str = "
" @@ -650,12 +623,12 @@ def export_emoluments_pdf_view(request): header_str += "Affaire n° " + str(affaire_id) + " sur le cadastre: " + affaire.cadastre + "
" - + # numéros de BF s'ils sont rattachés if emolument_bf_html is not None and emolument_bf_html != "": header_str += "Bien(s)-fonds n° " + emolument_bf_html + "
" header_str += "Emolument n° " + str(tableau_emoluments_id) + "
" - + # edit facture_html if len(factures) == 0: factures_html = "Aucune facture rattachée à l'émolument" @@ -668,20 +641,33 @@ def export_emoluments_pdf_view(request): if factures_html == "" or factures_html == []: factures_html = "La facture n'a pas encore été envoyée" - header_str += 'Facture(s): ' + factures_html + "
" + header_str += "Facture(s): " + factures_html + "
" + tableau_emoluments_html = header_str + tableau_emoluments_html + tableau_emoluments_html += 'Récapitulatif
' + tableau_emoluments_html += tableau_recapitulatif_html + "" + + filename = "Tableau_émoluments_" + str(tableau_emoluments_id) + "_Affaire_" + str(affaire_id) + ".pdf" + result = requests.post(request.registry.settings["weasyprint_baseurl"] + filename, data=tableau_emoluments_html) + + response = Response(result.content) + params = response.content_type_params + params["filename"] = filename + response.content_type = "application/pdf" + response.content_type_params = params + return response tableau_emoluments_html = header_str + tableau_emoluments_html tableau_emoluments_html += 'Récapitulatif
' tableau_emoluments_html += tableau_recapitulatif_html + "