Subversion Repositories SmartDukaan

Rev

Rev 7410 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.inventory.controllers;

import in.shop2020.purchase.Invoice;
import in.shop2020.purchase.PurchaseService.Client;
import in.shop2020.purchase.Supplier;
import in.shop2020.thrift.clients.PurchaseClient;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author mandeep
 * 
 */
public class InvoiceController extends BaseController {
    private static Log logger = LogFactory.getLog(InvoiceController.class);

    private List<Invoice> invoices;
    private String invoiceNumber;
    private String date;
    private String receivedFrom;
    private String supplierId;
    private String numItems;
    private SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
    private Map<Long, Supplier> suppliers = new HashMap<Long, Supplier>();

    /*
     * (non-Javadoc)
     * 
     * @see in.shop2020.inventory.controllers.BaseController#nindex()
     */
    @Override
    public String index() {
        try {
            Client purchaseClient = new PurchaseClient().getClient();
            invoices = purchaseClient.getInvoices(getYesterday().getTime());
            for (Supplier s : purchaseClient.getSuppliers()) {
                suppliers.put(s.getId(), s);
            }
        } catch (Exception e) {
            logger.error("Could not fetch invoices", e);
        }

        return super.index();
    }

    /* (non-Javadoc)
     * @see in.shop2020.inventory.controllers.BaseController#editNew()
     */
    @Override
    public String editNew() {
        try {
            Client purchaseClient = new PurchaseClient().getClient();
            for (Supplier s : purchaseClient.getSuppliers()) {
                suppliers.put(s.getId(), s);
            }
        } catch (Exception e) {
            logger.error("Could not fetch suppliers", e);
        }

        return super.editNew();
    }

    private Date getYesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar.getTime();
    }

    public String create() {
        try {
            Invoice invoice = new Invoice();
            invoice.setInvoiceNumber(invoiceNumber);
            invoice.setDate(sdf.parse(date).getTime());
            invoice.setNumItems(Long.valueOf(numItems));
            invoice.setReceivedFrom(receivedFrom);
            invoice.setSupplierId(Long.valueOf(supplierId));
            Client purchaseClient = new PurchaseClient().getClient();
            purchaseClient.createInvoice(invoice);
        } catch (Exception e) {
            logger.error("Could not create invoice", e);
        }

        return index();
    }

    /**
     * Utility method to convert a date to a readable format 
     */
    public String convertDate(Long date) {
        if (date == null || date == 0) {
            return "N/A";
        }

        return sdf.format(new Date(date));
    }

    public List<Invoice> getInvoices() {
        return invoices;
    }

    public void setInvoices(List<Invoice> invoices) {
        this.invoices = invoices;
    }

    public String getInvoiceNumber() {
        return invoiceNumber;
    }

    public void setInvoiceNumber(String invoiceNumber) {
        this.invoiceNumber = invoiceNumber;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getReceivedFrom() {
        return receivedFrom;
    }

    public void setReceivedFrom(String receivedFrom) {
        this.receivedFrom = receivedFrom;
    }

    public String getSupplierId() {
        return supplierId;
    }

    public void setSupplierId(String supplierId) {
        this.supplierId = supplierId;
    }

    public String getNumItems() {
        return numItems;
    }

    public void setNumItems(String numItems) {
        this.numItems = numItems;
    }

    public Map<Long, Supplier> getSuppliers() {
        return suppliers;
    }

    public void setSuppliers(Map<Long, Supplier> suppliers) {
        this.suppliers = suppliers;
    }
}