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);
}
Aggregations