com.redhat.rhtracking.web.controller.InvoiceController.java Source code

Java tutorial

Introduction

Here is the source code for com.redhat.rhtracking.web.controller.InvoiceController.java

Source

/*
 * Copyright (C) 2015 Nuuptech
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.redhat.rhtracking.web.controller;

import com.redhat.rhtracking.core.services.DeliveryMatrixService;
import com.redhat.rhtracking.core.services.InvoiceService;
import com.redhat.rhtracking.events.EventStatus;
import static com.redhat.rhtracking.web.controller.Utils.getFlashMessagesList;
import static com.redhat.rhtracking.web.controller.Utils.getMessagesList;
import com.redhat.rhtracking.web.domain.InvoiceInfo;
import com.redhat.rhtracking.web.domain.OneString;
import java.math.BigDecimal;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 *
 * @author marco-g8
 */
@Controller
public class InvoiceController {

    private static final Logger logger = Logger.getLogger(InvoiceController.class);

    @Autowired
    private DeliveryMatrixService deliveryMatrixService;

    @Autowired
    private InvoiceService invoiceService;

    @RequestMapping(value = "/invoice/add", method = RequestMethod.GET)
    public String saveView(@RequestParam(required = true) long matrix,
            @ModelAttribute("invoiceInfo") InvoiceInfo invoiceInfo, Model model) {

        List<String> messages = getMessagesList(model);
        model.addAttribute("matrix", matrix);

        Map<String, Object> request = new HashMap<>();
        request.put("deliveryMatrixId", matrix);
        Map<String, Object> opportunity = deliveryMatrixService.findOpportunityBy(request);

        if ((EventStatus) opportunity.get("status") != EventStatus.SUCCESS) {
            messages.add("warning::Ocurrio un error al obtener la matriz de delivery");
            return "redirect:/";
        }

        invoiceInfo.setOpportunity((String) opportunity.get("identifier"));
        invoiceInfo.setDeliveryMatrix(matrix);

        Map<String, Object> deliveryMatrix = (Map<String, Object>) opportunity.get("deliveryMatrix");
        model.addAttribute("salesOrder", deliveryMatrix.get("salesOrder"));

        Map<String, Object> hours = deliveryMatrixService.findOpportunityHours(opportunity);

        model.addAttribute("platform", false);
        model.addAttribute("middleware", false);
        model.addAttribute("workshop", false);

        for (Map<String, Object> h : (List<Map<String, Object>>) hours.get("list")) {
            String type = (String) h.get("hourType");
            switch (type) {
            case "PLATFORM":
                model.addAttribute("platformHours", (int) h.get("hours") - (int) h.get("hoursToBill"));
                invoiceInfo.setPlatformPrice((BigDecimal) h.get("unitPrice"));
                model.addAttribute("platform", true);
                break;

            case "MIDDLEWARE":
                model.addAttribute("middlewareHours", (int) h.get("hours") - (int) h.get("hoursToBill"));
                invoiceInfo.setMiddlewarePrice((BigDecimal) h.get("unitPrice"));
                model.addAttribute("middleware", true);
                break;

            case "WORKSHOP":
                model.addAttribute("workshopHours", (int) h.get("hours") - (int) h.get("hoursToBill"));
                invoiceInfo.setWorkshopPrice((BigDecimal) h.get("unitPrice"));
                model.addAttribute("workshop", true);
                break;

            default:
            }
        }
        model.addAttribute("hours", hours.get("list"));

        return "/invoice/add";
    }

    @RequestMapping(value = "/invoice/add", method = RequestMethod.POST)
    public String save(@Valid @ModelAttribute("invoiceInfo") InvoiceInfo invoiceInfo, BindingResult result,
            RedirectAttributes redirectAttributes, Model model, Principal principal) {
        List<String> messages = getFlashMessagesList(model, redirectAttributes);
        if (result.hasErrors()) {
            messages.add("warning::Errors found");
            messages.add("info::" + result.getAllErrors());
            redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.register", result);
            redirectAttributes.addFlashAttribute("invoiceInfo", invoiceInfo);

            redirectAttributes.addAttribute("matrix", invoiceInfo.getDeliveryMatrix());
            return "redirect:/invoice/add";
        }

        Map<String, Object> request = new HashMap<>();

        request.put("invoiceRequest", invoiceInfo.getRequestNumber().replace(" ", ""));
        String invoiceNumber = invoiceInfo.getInvoiceNumber();
        invoiceNumber = invoiceNumber.replace(" ", "");
        if (!invoiceNumber.equals(""))
            request.put("invoiceNumber", invoiceNumber);

        request.put("platformQuantity", invoiceInfo.getPlatformHours());
        request.put("platformPrice", invoiceInfo.getPlatformPrice());
        request.put("middlewareQuantity", invoiceInfo.getMiddlewareHours());
        request.put("middlewarePrice", invoiceInfo.getMiddlewarePrice());
        request.put("workshopQuantity", invoiceInfo.getWorkshopHours());
        request.put("workshopPrice", invoiceInfo.getWorkshopPrice());

        request.put("date", invoiceInfo.getDate());
        request.put("deliveryMatrixId", invoiceInfo.getDeliveryMatrix());

        Map<String, Object> saveInvoice = invoiceService.saveInvoice(request);
        if ((EventStatus) saveInvoice.get("status") != EventStatus.SUCCESS) {
            messages.add("warning::Errrores encontrados");
            messages.add("info::" + saveInvoice);
            redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.register", result);
            redirectAttributes.addFlashAttribute("invoiceInfo", invoiceInfo);

            redirectAttributes.addAttribute("matrix", invoiceInfo.getDeliveryMatrix());
            return "redirect:/invoice/add";
        }

        return "redirect:/opportunity/show?id=" + invoiceInfo.getOpportunity();
    }

    @RequestMapping(value = "/invoice/update", method = RequestMethod.POST)
    public String updateInvoiceNumber(@RequestParam(required = true) long invoice,
            @Valid @ModelAttribute("oneString") OneString oneString, BindingResult result,
            RedirectAttributes redirectAttributes, Model model) {
        List<String> messages = getFlashMessagesList(model, redirectAttributes);
        if (result.hasErrors()) {
            messages.add("warning::Error ");
            return "redirect:/invoice/" + invoice + "/details";
        }
        Map<String, Object> request = new HashMap<>();
        request.put("id", invoice);
        request.put("invoiceNumber", oneString.getValue());
        Map<String, Object> response = invoiceService.updateInvoice(request);
        if ((EventStatus) response.get("status") != EventStatus.SUCCESS) {
            messages.add("error::Ocurrio un error al actualizar la factura");
        }
        return "redirect:/invoice/" + invoice + "/details";
    }

    @RequestMapping(value = "/invoice/", method = RequestMethod.GET)
    public String list(@RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "20") int size, Model model) {
        Map<String, Object> request = new HashMap<>();
        request.put("pageNumber", page - 1);
        request.put("pageSize", size > 10 ? size : 20);
        Map<String, Object> listAllConsultantsPaged = invoiceService.listAllInvoices(request);//paged

        if ((EventStatus) listAllConsultantsPaged.get("status") == EventStatus.SUCCESS) {
            model.addAttribute("pageNumber", page - 1);
            model.addAllAttributes(listAllConsultantsPaged);
        } else
            getMessagesList(model).add("error::Ocurrio un error al obtener al procesar la solicitud");
        return "/invoice/list";
    }

    @RequestMapping(value = "/invoice/{id}/details", method = RequestMethod.GET)
    public String show(@PathVariable long id, RedirectAttributes redirectAttributes, Model model) {
        List<String> messages = getFlashMessagesList(model, redirectAttributes);

        Map<String, Object> request = new HashMap<>();
        request.put("id", id);
        Map<String, Object> response = invoiceService.findInvoice(request);

        if ((EventStatus) response.get("status") != EventStatus.SUCCESS) {
            messages.add("error::Factura desconocida");
            return "redirect:/invoice/";
        }
        messages.add("info::" + response);
        model.addAllAttributes(response);

        return "/invoice/details";
    }

    @ModelAttribute("invoiceInfo")
    public InvoiceInfo invoiceInfo() {
        return new InvoiceInfo();
    }

    @ModelAttribute("oneString")
    public OneString getOneString() {
        return new OneString();
    }

}