Search in sources :

Example 1 with EsupPaperCutService

use of org.esupportail.papercut.services.EsupPaperCutService in project esup-papercut by EsupPortail.

the class EsupPapercutPortletController method renderView.

@RequestMapping
public ModelAndView renderView(RenderRequest request, RenderResponse response) {
    ModelMap model = new ModelMap();
    model.put("htmlHeader", request.getPreferences().getValue("htmlHeader", ""));
    model.put("htmlFooter", request.getPreferences().getValue("htmlFooter", ""));
    model.put("payboxMontantMin", request.getPreferences().getValue("payboxMontantMin", "0.5"));
    model.put("payboxMontantMax", request.getPreferences().getValue("payboxMontantMax", "5.0"));
    model.put("payboxMontantStep", request.getPreferences().getValue("payboxMontantStep", "0.5"));
    model.put("payboxMontantDefaut", request.getPreferences().getValue("payboxMontantDefaut", "2.0"));
    double papercutSheetCost = Double.parseDouble(request.getPreferences().getValue("papercutSheetCost", "-1"));
    double papercutColorSheetCost = Double.parseDouble(request.getPreferences().getValue("papercutColorSheetCost", "-1"));
    String paperCutContext = request.getPreferences().getValue(PREF_PAPERCUT_CONTEXT, null);
    EsupPaperCutService esupPaperCutService = esupPaperCutServices.get(paperCutContext);
    String uid = getUid(request);
    String userMail = getUserMail(request);
    // check if the user can make a transaction
    int transactionNbMax = Integer.parseInt(request.getPreferences().getValue("transactionNbMax", "-1"));
    BigDecimal transactionMontantMax = new BigDecimal(request.getPreferences().getValue("transactionMontantMax", "-1"));
    boolean canMakeTransaction = true;
    // constraints via transactionNbMax
    if (transactionNbMax > -1) {
        long nbTransactionsNotArchived = PayboxPapercutTransactionLog.countFindPayboxPapercutTransactionLogsByUidEqualsAndPaperCutContextEqualsAndArchived(uid, paperCutContext, false);
        if (transactionNbMax <= nbTransactionsNotArchived) {
            canMakeTransaction = false;
        }
    }
    BigDecimal payboxMontantMin = new BigDecimal(request.getPreferences().getValue("payboxMontantMin", "0.5"));
    BigDecimal payboxMontantMax = new BigDecimal(request.getPreferences().getValue("payboxMontantMax", "5.0"));
    BigDecimal payboxMontantStep = new BigDecimal(request.getPreferences().getValue("payboxMontantStep", "0.5"));
    BigDecimal payboxMontantDefaut = new BigDecimal(request.getPreferences().getValue("payboxMontantDefaut", "2.0"));
    // constraints on the slider via transactionMontantMax
    if (canMakeTransaction && transactionMontantMax.intValue() > -1) {
        List<PayboxPapercutTransactionLog> transactionsNotArchived = PayboxPapercutTransactionLog.findPayboxPapercutTransactionLogsByUidEqualsAndPaperCutContextEqualsAndArchived(uid, paperCutContext, false).getResultList();
        BigDecimal montantTotalTransactionsNotArchived = new BigDecimal("0");
        for (PayboxPapercutTransactionLog txLog : transactionsNotArchived) {
            montantTotalTransactionsNotArchived = montantTotalTransactionsNotArchived.add(new BigDecimal(txLog.getMontant()));
        }
        transactionMontantMax = transactionMontantMax.multiply(new BigDecimal("100")).subtract(montantTotalTransactionsNotArchived);
        if (transactionMontantMax.doubleValue() < payboxMontantMax.doubleValue() * 100) {
            payboxMontantMax = transactionMontantMax.divide(payboxMontantStep).multiply(payboxMontantStep);
            payboxMontantMax = payboxMontantMax.divide(new BigDecimal("100"));
            if (payboxMontantDefaut.compareTo(payboxMontantMax) == 1) {
                payboxMontantDefaut = payboxMontantMax;
            }
            if (payboxMontantMax.compareTo(payboxMontantMin) == -1) {
                canMakeTransaction = false;
            }
            model.put("payboxMontantMax", payboxMontantMax.doubleValue());
            model.put("payboxMontantDefaut", payboxMontantDefaut.doubleValue());
        }
    }
    // generation de l'ensemble des payboxForm :  payboxMontantMin -> payboxMontantMax par pas de payboxMontantStep
    String portletContextPath = ((RenderResponse) response).createRenderURL().toString();
    Map<Integer, PayBoxForm> payboxForms = new HashMap<Integer, PayBoxForm>();
    for (BigDecimal montant = payboxMontantMin; montant.compareTo(payboxMontantMax) <= 0; montant = montant.add(payboxMontantStep)) {
        PayBoxForm payBoxForm = esupPaperCutService.getPayBoxForm(uid, userMail, montant.doubleValue(), paperCutContext, portletContextPath);
        if (papercutSheetCost > 0) {
            int nbSheets = (int) (montant.doubleValue() / papercutSheetCost);
            payBoxForm.setNbSheets(nbSheets);
        }
        if (papercutColorSheetCost > 0) {
            int nbColorSheets = (int) (montant.doubleValue() / papercutColorSheetCost);
            payBoxForm.setNbColorSheets(nbColorSheets);
        }
        payboxForms.put(montant.multiply(new BigDecimal(100)).intValue(), payBoxForm);
    }
    Map<Integer, PayBoxForm> sortedMap = new TreeMap<Integer, PayBoxForm>(payboxForms);
    model.put("payboxForms", sortedMap);
    model.put("payboxMontantDefautCents", payboxMontantDefaut.multiply(new BigDecimal(100)).intValue());
    model.put("canMakeTransaction", canMakeTransaction);
    UserPapercutInfos userPapercutInfos = esupPaperCutService.getUserPapercutInfos(uid);
    model.put("userPapercutInfos", userPapercutInfos);
    boolean isAdmin = isAdmin(request);
    boolean isManager = isManager(request);
    model.put("isAdmin", isAdmin);
    model.put("isManager", isManager);
    model.put("active", "home");
    return new ModelAndView(getViewName(request, "index"), model);
}
Also used : HashMap(java.util.HashMap) ModelMap(org.springframework.ui.ModelMap) ModelAndView(org.springframework.web.portlet.ModelAndView) TreeMap(java.util.TreeMap) PayboxPapercutTransactionLog(org.esupportail.papercut.domain.PayboxPapercutTransactionLog) BigDecimal(java.math.BigDecimal) EsupPaperCutService(org.esupportail.papercut.services.EsupPaperCutService) PayBoxForm(org.esupportail.papercut.domain.PayBoxForm) UserPapercutInfos(org.esupportail.papercut.domain.UserPapercutInfos) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

BigDecimal (java.math.BigDecimal)1 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1 PayBoxForm (org.esupportail.papercut.domain.PayBoxForm)1 PayboxPapercutTransactionLog (org.esupportail.papercut.domain.PayboxPapercutTransactionLog)1 UserPapercutInfos (org.esupportail.papercut.domain.UserPapercutInfos)1 EsupPaperCutService (org.esupportail.papercut.services.EsupPaperCutService)1 ModelMap (org.springframework.ui.ModelMap)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ModelAndView (org.springframework.web.portlet.ModelAndView)1