no.dusken.barweb.admin.InvoiceController.java Source code

Java tutorial

Introduction

Here is the source code for no.dusken.barweb.admin.InvoiceController.java

Source

/*
 Copyright 2009 - 2010 Under Dusken
    
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
    
 http://www.apache.org/licenses/LICENSE-2.0
    
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
package no.dusken.barweb.admin;

import no.dusken.barweb.model.BarPerson;
import no.dusken.barweb.model.Gjeng;
import no.dusken.barweb.model.Invoice;
import no.dusken.barweb.model.Transaksjon;
import no.dusken.barweb.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

/**
 * @author Marvin B. Lillehaug <lillehau@underdusken.no>
 */
@Controller
public class InvoiceController {

    private GjengService gjengService;

    private TransaksjonService transaksjonService;

    private InvoiceService invoiceService;

    private PersonInvoiceService personInvoiceService;

    private InvoiceTransaksjonService invoiceTransaksjonService;

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

    @RequestMapping(value = "/invoices/setPaid.do", method = RequestMethod.POST)
    public String handleInvoicePaid(@RequestParam("invoiceID") Long invoiceID, Model model) {
        Invoice invoice = invoiceService.findOne(invoiceID);
        markInvoiceAsPaid(invoice);
        model.addAttribute("invoice", invoice);
        return "no/dusken/barweb/admin/invoice/paid";
    }

    @RequestMapping(value = "/invoices/invoiceForGjeng.do", method = RequestMethod.GET)
    public String generateInvoiceGjeng(@RequestParam("gjengID") Long gjengID) {
        Gjeng g = gjengService.findOne(gjengID);
        List<Transaksjon> transaksjons = transaksjonService.getByGjengAndInvoiceIsNull(g);
        Invoice invoice = new Invoice();

        invoice.setGjeng(g);
        invoice.setPaid(false);
        invoice.setComment("");
        logger.info("Generated invoice for {}", g.getName());
        invoice = invoiceService.save(invoice);
        invoice.setTransactions(transaksjons);
        invoice = invoiceService.save(invoice);

        // set invoice in transakjons so that we don't bill the same transaksjon twice.
        // refreshing the invoice object because it gets changed when setting the invoice in the transaction.
        // if we don't update, OptimisticLockException is thrown complaining that the invoice object is changed.
        for (Transaksjon t : transaksjons) {
            invoice = invoiceService.findOne(invoice.getId());
            t.setInvoice(invoice);
            transaksjonService.save(t);
        }

        logger.info("Generated invoice for " + g.getName());

        return "redirect:/admin/editInvoice.do?ID=" + invoice.getId();
    }

    @RequestMapping(value = "/invoices/delete.do", method = RequestMethod.POST)
    public String deleteInvoice(@RequestParam("invoiceID") Long invoiceID, Model model) {
        Invoice invoice = invoiceService.findOne(invoiceID);
        logger.info("deleting invoice with id: " + invoice.getId() + " for gjeng: " + invoice.getGjeng().getName());
        invoiceTransaksjonService.deleteInvoiceUpdateTransaksjons(invoice, invoice.getTransactions());
        model.addAttribute("entity", invoice);
        return "no/dusken/barweb/common/deleted";
    }

    @RequestMapping(value = "/invoices/getPdf.do", method = RequestMethod.POST)
    public String generateInvoicePdf(@RequestParam("invoiceID") Long invoiceID, Model model) {
        Invoice invoice = invoiceService.findOne(invoiceID);
        model.addAttribute("invoice", invoice);
        model.addAttribute("persons", getPersonOwes(invoice.getTransactions()));
        model.addAttribute("gjeng", invoice.getGjeng());
        return "invoiceview";
    }

    @RequestMapping(value = "/invoices/list.do", method = RequestMethod.GET)
    public String listInvoices(@RequestParam(value = "gjengID", defaultValue = "0") Long gjengID, Model m) {
        List<Invoice> invoices;
        if (gjengID != 0) {
            Gjeng gjeng = gjengService.findOne(gjengID);
            invoices = invoiceService.getByGjengOrderByTimeCreatedDesc(gjeng);
            m.addAttribute("gjeng", gjeng);
        } else {
            invoices = invoiceService.getByHidden(false);

            Comparator<Invoice> c = new Comparator<Invoice>() {
                public int compare(Invoice i1, Invoice i2) {
                    Gjeng g1 = i1.getGjeng();
                    Gjeng g2 = i2.getGjeng();
                    if (g1 == null || g2 == null) {
                        if (g1 == null && g2 != null)
                            return -1;
                        if (g1 != null && g2 == null)
                            return 1;
                        else
                            return 0;
                    }
                    String g1name = g1.getName();
                    String g2name = g2.getName();

                    return g1name.compareTo(g2name);
                }
            };
            Collections.sort(invoices, c);

        }
        m.addAttribute("invoices", invoices);
        return "no/dusken/barweb/admin/invoice/list";
    }

    @RequestMapping(value = "/invoices/hide.do", method = RequestMethod.POST)
    public String hideInvoice(@RequestParam("invoiceID") Long invoiceID) {
        Invoice i = invoiceService.findOne(invoiceID);
        i.setHidden(true);
        invoiceService.save(i);
        return "redirect:/admin/invoices/list.do";
    }

    private Invoice markInvoiceAsPaid(Invoice invoice) {
        if (!invoice.getPaid()) {
            Map<BarPerson, Integer> personMap = getPersonOwes(invoice.getTransactions());
            List<BarPerson> barPersons = new LinkedList<BarPerson>();
            for (Map.Entry<BarPerson, Integer> p : personMap.entrySet()) {
                BarPerson key = p.getKey();
                key.setMoney(key.getMoney() + p.getValue());
            }
            invoice.setPaid(true);
            logger.info("Marking invoice with id: " + invoice.getId() + " as paid.");
            personInvoiceService.savePersonsAndInvoice(barPersons, invoice);
        }
        return invoice;
    }

    /**
     * @param transaksjons on invoice
     * @return a map with person as key and how much the transaksjons given sum for each BarPerson.
     */
    private Map<BarPerson, Integer> getPersonOwes(List<Transaksjon> transaksjons) {

        Map<BarPerson, Integer> map = new HashMap<BarPerson, Integer>();
        for (Transaksjon t : transaksjons) {
            BarPerson p = t.getBarPerson();
            Integer sum = map.get(p);
            if (sum == null) {
                map.put(p, t.getSum());
            } else {
                map.put(p, sum + t.getSum());
            }
        }

        SortedMap<BarPerson, Integer> sortedMap = new TreeMap<BarPerson, Integer>(new ValueComparator(map));
        sortedMap.putAll(map);

        return sortedMap;

    }

    /**
     * class for sorting personList for invoices.
     * Used in getPersonOwes to sort the list that show up on the invoice.
     */
    static class ValueComparator implements Comparator<BarPerson> {

        Map<BarPerson, Integer> map;

        ValueComparator(Map<BarPerson, Integer> map) {
            this.map = map;
        }

        @Override
        public int compare(BarPerson o1, BarPerson o2) {
            return o1.getFullName().compareTo(o2.getFullName());
        }
    }

    @Autowired
    public void setGjengService(GjengService gjengService) {
        this.gjengService = gjengService;
    }

    @Autowired
    public void setTransaksjonService(TransaksjonService transaksjonService) {
        this.transaksjonService = transaksjonService;
    }

    @Autowired
    public void setInvoiceService(InvoiceService invoiceService) {
        this.invoiceService = invoiceService;
    }

    @Autowired
    public void setPersonInvoiceService(PersonInvoiceService personInvoiceService) {
        this.personInvoiceService = personInvoiceService;
    }

    @Autowired
    public void setInvoiceTransaksjonService(InvoiceTransaksjonService invoiceTransaksjonService) {
        this.invoiceTransaksjonService = invoiceTransaksjonService;
    }

}