View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.ctv.bo.controller.demandes.intervention;
35  
36  import static fr.paris.lutece.plugins.ctv.constant.Document.JUSTIFICATIFS_CONVENTION;
37  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.CIVILITES;
38  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_DEMANDEUR;
39  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.KEY_ETAPE;
40  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.MAX_FILE_SIZE;
41  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TEMPLATE_DEMANDEUR_INTERVENTION;
42  
43  import java.util.Map;
44  
45  import javax.servlet.http.HttpServletRequest;
46  
47  import org.apache.commons.beanutils.ConvertUtils;
48  
49  import com.mysql.jdbc.StringUtils;
50  
51  import fr.paris.lutece.plugins.ctv.bo.DemandeWriting;
52  import fr.paris.lutece.plugins.ctv.bo.EntiteDemande;
53  import fr.paris.lutece.plugins.ctv.bo.EntiteDemande.ProfilEntite;
54  import fr.paris.lutece.plugins.ctv.bo.controller.demandes.AbstractCtvJspBean;
55  import fr.paris.lutece.plugins.ctv.business.IEntiteDemandeService;
56  import fr.paris.lutece.plugins.ctv.constant.Document;
57  import fr.paris.lutece.plugins.ctv.constant.StatutTaxationDemande;
58  import fr.paris.lutece.plugins.ctv.converter.EnumConverter;
59  import fr.paris.lutece.plugins.ctv.handler.CtvUploadDemandeDocumentHandler;
60  import fr.paris.lutece.plugins.ctv.service.FormulaireDemandeInterventionServiceImpl;
61  import fr.paris.lutece.plugins.ctv.service.IFormulaire;
62  import fr.paris.lutece.portal.service.datastore.DatastoreService;
63  import fr.paris.lutece.portal.service.spring.SpringContextService;
64  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
65  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
66  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
67  
68  @Controller( controllerPath = "jsp/admin/plugins/ctv/gestionDemandes/intervention/etapes", controllerJsp = "demandeur.jsp", right = "TABLEAU_DE_BORD" )
69  
70  public class DemandeurJspBean extends AbstractCtvJspBean
71  {
72  
73      private static final long serialVersionUID = 6185806539165628650L;
74  
75      private IEntiteDemandeService entiteDemandeService = SpringContextService.getBean( "entiteDemandeService" );
76  
77      protected IFormulaire formulaireInterventionService = FormulaireDemandeInterventionServiceImpl.getInstance( );
78  
79      private static final String EXONERATION_DE_TAXE = "exonerationDeTaxe";
80      protected CtvUploadDemandeDocumentHandler ctvDemandeUploadHandler = SpringContextService
81              .getBean( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER );
82  
83      @View( value = ETAPE_DEMANDEUR, defaultView = true )
84      public String getIdentification( HttpServletRequest request )
85      {
86  
87          Map<String, Object> model = getModel( );
88          initModel( model );
89          int numDemande = formulaireInterventionService.getNumDemande( request );
90          fillUserPossibleActions( request, model, numDemande );
91          formulaireInterventionService.alimentationModel( request, model );
92          model.put( KEY_ETAPE, "demandeur" );
93          model.put( "controlForm", true );
94          model.put( Document.JUSTIFICATIFS_CONVENTION + "_listUploadedFiles", ctvDemandeUploadHandler.getListUploadedFiles(Document.JUSTIFICATIFS_CONVENTION, request.getSession( ) ) );
95          model.put( MAX_FILE_SIZE, ctvDemandeUploadHandler.getMaxFileSize( ) );
96          return getPage( "", TEMPLATE_DEMANDEUR_INTERVENTION, model );
97      }
98  
99      @Action( ACTION_NEXT )
100     public String next( HttpServletRequest request )
101     {
102         updateDemandeur( request );
103         updateDemande( request );
104         return redirect( request, "chantier.jsp" );
105     }
106 
107     @Action( ACTION_SAVE )
108     public String save( HttpServletRequest request )
109     {
110         updateDemandeur( request );
111         updateDemande( request );
112         // Récupération de la redirection
113         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
114         if ( StringUtils.isNullOrEmpty( nextStepPath ) )
115         {
116             return redirect( request, "../demandeIntervention.jsp" );
117         }
118         return redirect( request, nextStepPath );
119     }
120 
121     @Action( ACTION_COMPLETE_LATER )
122     public String completeLater( HttpServletRequest request )
123     {
124         updateDemandeur( request );
125         updateDemande( request );
126         formulaireInterventionService.removeFilesFromSession( request );
127         return redirect( request, getAbsolutePath( request, PATH_TABLEAU_DE_BORD ) );
128     }
129 
130     @Action( ACTION_ABANDON )
131     public String abandon( HttpServletRequest request )
132     {
133         formulaireInterventionService.removeFilesFromSession( request );
134         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
135         if ( StringUtils.isNullOrEmpty( nextStepPath ) )
136         {
137             return redirect( request, "../demandeIntervention.jsp" );
138         }
139         return redirect( request, nextStepPath );
140     }
141 
142     private void initModel( Map<String, Object> model )
143     {
144         model.put( CIVILITES, EntiteDemande.Civilite.values( ) );
145         model.put( EXONERATION_DE_TAXE, DemandeWriting.ExonerationTaxe.values( ) );
146         model.put( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER, ctvDemandeUploadHandler );
147         model.put( "nonTaxable", DatastoreService.getDataValue( "parametre.numeric.flux.savoi2.siret.non.taxable", "" ) );
148     }
149 
150     private void updateDemandeur( HttpServletRequest request )
151     {
152         int numDemande = formulaireInterventionService.getNumDemande( request );
153         ConvertUtils.register( new EnumConverter( ), EntiteDemande.Civilite.class );
154         EntiteDemande demandeur = new EntiteDemande( );
155         populate( demandeur, request );
156         demandeur.setProfilEntite( ProfilEntite.PROFESSIONNEL );
157         entiteDemandeService.updateDemandeur( numDemande, demandeur );
158         fillLastUser( numDemande, request );
159     }
160 
161     private void updateDemande( HttpServletRequest request )
162     {
163         ConvertUtils.register( new EnumConverter( ), DemandeWriting.ExonerationTaxe.class );
164         DemandeWriting demande = new DemandeWriting();
165         populate(demande, request);
166         int numDemande = formulaireInterventionService.getNumDemande( request );
167         String demandeurSiret = entiteDemandeService.getDemandeurByNumDemande(numDemande).getSirenSiret();
168         String numSiretNonTaxables = DatastoreService.getDataValue("parametre.numeric.flux.savoi2.siret.non.taxable", "");
169         boolean isExoForIntervention = checkExonerationIntervention();
170 
171         if (demandeurSiret.isEmpty() || ( demandeurSiret == null ) || !numSiretNonTaxables.contains(demandeurSiret)) {
172 
173             if (!isExoForIntervention) {
174                 demande.setDemandeConvention(false);
175                 demande.setDemandeTaxation(StatutTaxationDemande.A_TAXER);
176             } else {
177                 if (( (demande.getExonerationTaxe().getExoneration()) != null )
178                         && "Oui".equals(demande.getExonerationTaxe().getExoneration())) {
179                     demande.setDemandeConvention(true);
180                     demande.setDemandeTaxation(StatutTaxationDemande.EXONERATION_A_VERIFIER);
181                 } else {
182                     demande.setDemandeConvention(false);
183                     demande.setDemandeTaxation(StatutTaxationDemande.A_TAXER);
184                 }
185             }
186             //cas spécifique des numéros siret non taxables
187         } else {
188             demande.setDemandeConvention(false);
189             demande.setDemandeTaxation(StatutTaxationDemande.NON_TAXABLE);
190         }
191 
192         demandeService.updateDemandeConvention( numDemande, demande.isDemandeConvention( ) );
193         demandeService.updateDemandeTaxation( numDemande, demande.getDemandeTaxation( ) );
194         ctvDemandeUploadHandler.storeFilesOnDisk( request, JUSTIFICATIFS_CONVENTION );
195     }
196 
197     private boolean checkExonerationIntervention()
198     {
199         String paramExoIntervention = (DatastoreService.getDataValue( "parametre.numeric.flux.savoi2.acces.bloc.exoneration.intervention", "Non" )).trim( ).toUpperCase( );
200         return ("OUI".equals( paramExoIntervention ) || "TRUE".equals( paramExoIntervention ) || "1".equals( paramExoIntervention ));
201     }
202 
203 }