nc.noumea.mairie.appock.viewmodel.EditStockReferentAchatViewModel.java Source code

Java tutorial

Introduction

Here is the source code for nc.noumea.mairie.appock.viewmodel.EditStockReferentAchatViewModel.java

Source

package nc.noumea.mairie.appock.viewmodel;

/*-
 * #%L
 * Logiciel de Gestion des approvisionnements et des stocks des fournitures administratives de la Mairie de Nouma
 * %%
 * Copyright (C) 2017 Mairie de Nouma, Nouvelle-Caldonie
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import nc.noumea.mairie.appock.dto.EntreeSortieStock;
import nc.noumea.mairie.appock.entity.*;
import nc.noumea.mairie.appock.enums.TypeMouvementStock;
import nc.noumea.mairie.appock.services.AuthHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.*;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zkplus.spring.DelegatingVariableResolver;
import org.zkoss.zul.Messagebox;

import javax.mail.MessagingException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@VariableResolver(DelegatingVariableResolver.class)
public class EditStockReferentAchatViewModel extends EditStockViewModel implements Serializable {
    private static final long serialVersionUID = 1L;

    @WireVariable
    AuthHelper authHelper;

    @Override
    @Init(superclass = true)
    public void init() {
        super.init();
    }

    @Command
    public void creeSortieStockReferentAchat() {
        if (getSelectedService() == null) {
            Messagebox.show("Vous devez choisir un service dans la liste des services ci-dessus", "Erreur",
                    Messagebox.OK, Messagebox.ERROR);
            return;
        }
        super.creeSortieStock("ReferentAchat");
    }

    @Command
    public void ajoutArticleStock() {
        Catalogue catalogue = catalogueService.findActif();
        if (catalogue == null) {
            Messagebox.show("Il n'existe pas de catalogue actif  afficher", "Erreur", Messagebox.OK,
                    Messagebox.ERROR);
            return;
        }

        if (getSelectedService() == null) {
            Messagebox.show("Vous devez choisir un service dans la liste des services ci-dessus", "Erreur",
                    Messagebox.OK, Messagebox.ERROR);
            return;
        }

        Map<String, Object> args = new HashMap<>();
        args.put("stock", entity);
        args.put("catalogue", catalogue);
        Executions.createComponents("/layout/editStockReferentAchatAjoutArticlePopup.zul", null, args);
    }

    @GlobalCommand
    public void ajouteArticleAuStock(@BindingParam("selectedArticleCatalogue") ArticleCatalogue articleCatalogue,
            @BindingParam("stock") Stock stock, @BindingParam("quantiteRenseigne") Integer quantiteRenseigne) {

        if (articleCatalogue == null || stock == null) {
            return;
        }

        for (ArticleStock articleStock : stock.getListeArticleStock()) {
            if (articleStock.getArticleCatalogue().getReference().equals(articleCatalogue.getReference())) {
                Messagebox.show("Article dj prsent dans le stock", "Erreur", Messagebox.OK, Messagebox.ERROR);
                return;
            }
        }

        stockService.creeArticleStockEtMouvement(articleCatalogue, quantiteRenseigne, stock, null);

        refreshListeArticleStock();
        rechargeOngletMonStock();
        BindUtils.postNotifyChange(null, null, this, "*");
    }

    @Command
    public void creeEntreeStock() {
        if (getSelectedService() == null) {
            Messagebox.show("Vous devez choisir un service dans la liste des services ci-dessus", "Erreur",
                    Messagebox.OK, Messagebox.ERROR);
            return;
        }
        if (CollectionUtils.isEmpty(getSelectedListeArticleStock())) {
            Messagebox.show(
                    "Vous devez slectionner un article dans la liste avant de pouvoir effectuer une entre en stock",
                    "Erreur", Messagebox.OK, Messagebox.ERROR);
            return;
        }

        Map<String, Object> args = new HashMap<>();
        args.put("selectedListeArticleStock", getSelectedListeArticleStock());
        Executions.createComponents("/layout/createEntreeStockPopup.zul", null, args);
    }

    @GlobalCommand
    public void creeNouvelleEntreeStock(@BindingParam("selectedArticleStock") ArticleStock articleStock,
            @BindingParam("quantiteRenseigne") Integer quantiteRenseigne,
            @BindingParam("observation") String observation) {
        creeEntreeSortieGeneric(articleStock, quantiteRenseigne, observation, TypeMouvementStock.ENTREE);
    }

    @GlobalCommand
    public void creeMultipleNouvelleEntreeStock(
            @BindingParam("listeEntreeStock") List<EntreeSortieStock> listeEntreeStock) {
        creeEntreeSortieGeneric(listeEntreeStock, TypeMouvementStock.ENTREE);
    }

    @GlobalCommand
    public void creeNouvelleSortieStockReferentAchat(
            @BindingParam("selectedArticleStock") ArticleStock articleStock,
            @BindingParam("quantiteRenseigne") Integer quantiteRenseigne,
            @BindingParam("observation") String observation) {
        super.creeNouvelleSortieStock(articleStock, quantiteRenseigne, observation);
    }

    @GlobalCommand
    public void creeMultipleNouvelleSortieStockReferentAchat(
            @BindingParam("listeSortieStock") List<EntreeSortieStock> listeSortieStock) {
        super.creeMultipleNouvelleSortieStock(listeSortieStock);
    }

    @Command
    public void demandeInventaireService() throws UnsupportedEncodingException, MessagingException {
        if (entity == null || getSelectedService() == null) {
            return;
        }

        Service service = serviceService.findOne(getSelectedService().getId());
        Messagebox.show(
                "Voulez-vous vraiment demander au service '" + service.getLibellePoleDirectionService()
                        + "' de raliser un inventaire ?",
                "Confirmation", new Messagebox.Button[] { Messagebox.Button.YES, Messagebox.Button.NO },
                Messagebox.QUESTION, evt -> {
                    if (evt.getName().equals("onYes")) {
                        entity.setInventaireDemande(true);
                        entity.setInventaireDemandeUser(authHelper.getCurrentUser().getNomComplet());
                        entity.setDateDerniereDemandeInventaire(LocalDateTime.now());
                        stockService.save(entity);
                        mailService.sendMailDemandeInventaire(getSelectedService());

                        refreshListeArticleStock();
                        rechargeOngletMonStock();
                        BindUtils.postNotifyChange(null, null, this, "*");
                        showNotificationStandard("Demande d'inventaire envoye au service");
                    }
                });
    }

    @Command
    public void demandeInventaireTousService() {
        Messagebox.show("Voulez-vous vraiment demander  tous les services de raliser un inventaire ?",
                "Suppression", new Messagebox.Button[] { Messagebox.Button.YES, Messagebox.Button.NO },
                Messagebox.QUESTION, evt -> {
                    if (evt.getName().equals("onYes")) {
                        for (Service service : serviceService.findAllActif()) {
                            if (service.getStock() == null) {
                                service.setStock(new Stock());
                                service = serviceService.save(service);
                            }

                            Stock stock = stockService.findOne(service.getStock().getId());
                            stock.setInventaireDemande(true);
                            stock.setInventaireDemandeUser(authHelper.getCurrentUser().getNomComplet());
                            stock.setDateDerniereDemandeInventaire(LocalDateTime.now());
                            stockService.save(stock);
                            mailService.sendMailDemandeInventaire(service);

                            refreshListeArticleStock();
                            rechargeOngletMonStock();
                            BindUtils.postNotifyChange(null, null, this, "*");
                            showNotificationStandard("Demande d'inventaire envoye  tous les services");
                        }
                    }
                });
    }

    @Command
    @NotifyChange("*")
    public void recherche() {
        refreshListeArticleStock();
    }

    @Command
    @NotifyChange("*")
    public void videRecherche() {
        setTexteRecherche(null);
        refreshListeArticleStock();
    }
}