Subversion Repositories SmartDukaan

Rev

Rev 6630 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5443 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.inventory.controllers;
5
 
6
import in.shop2020.purchase.Invoice;
7
import in.shop2020.purchase.PurchaseService.Client;
8
import in.shop2020.purchase.Supplier;
9
import in.shop2020.thrift.clients.PurchaseClient;
10
 
11
import java.text.SimpleDateFormat;
12
import java.util.Calendar;
13
import java.util.Date;
14
import java.util.HashMap;
15
import java.util.List;
16
import java.util.Map;
17
 
18
import org.apache.commons.logging.Log;
19
import org.apache.commons.logging.LogFactory;
20
 
21
/**
22
 * @author mandeep
23
 * 
24
 */
25
public class InvoiceController extends BaseController {
26
    private static Log logger = LogFactory.getLog(InvoiceController.class);
27
 
28
    private List<Invoice> invoices;
29
    private String invoiceNumber;
30
    private String date;
31
    private String receivedFrom;
32
    private String supplierId;
33
    private String numItems;
34
    private SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
35
    private Map<Long, Supplier> suppliers = new HashMap<Long, Supplier>();
36
 
37
    /*
38
     * (non-Javadoc)
39
     * 
40
     * @see in.shop2020.inventory.controllers.BaseController#nindex()
41
     */
42
    @Override
43
    public String index() {
44
        try {
45
            Client purchaseClient = new PurchaseClient().getClient();
46
            invoices = purchaseClient.getInvoices(getYesterday().getTime());
47
            for (Supplier s : purchaseClient.getSuppliers()) {
48
                suppliers.put(s.getId(), s);
49
            }
50
        } catch (Exception e) {
51
            logger.error("Could not fetch invoices", e);
52
        }
53
 
54
        return super.index();
55
    }
56
 
57
    /* (non-Javadoc)
58
     * @see in.shop2020.inventory.controllers.BaseController#editNew()
59
     */
60
    @Override
61
    public String editNew() {
62
        try {
63
            Client purchaseClient = new PurchaseClient().getClient();
64
            for (Supplier s : purchaseClient.getSuppliers()) {
65
                suppliers.put(s.getId(), s);
66
            }
67
        } catch (Exception e) {
68
            logger.error("Could not fetch suppliers", e);
69
        }
70
 
71
        return super.editNew();
72
    }
73
 
74
    private Date getYesterday() {
75
        Calendar calendar = Calendar.getInstance();
76
        calendar.setTime(new Date());
77
        calendar.add(Calendar.DAY_OF_MONTH, -1);
78
        return calendar.getTime();
79
    }
80
 
81
    public String create() {
82
        try {
83
            Invoice invoice = new Invoice();
84
            invoice.setInvoiceNumber(invoiceNumber);
85
            invoice.setDate(sdf.parse(date).getTime());
86
            invoice.setNumItems(Long.valueOf(numItems));
87
            invoice.setReceivedFrom(receivedFrom);
88
            invoice.setSupplierId(Long.valueOf(supplierId));
89
            Client purchaseClient = new PurchaseClient().getClient();
90
            purchaseClient.createInvoice(invoice);
91
        } catch (Exception e) {
92
            logger.error("Could not create invoice", e);
93
        }
94
 
95
        return index();
96
    }
97
 
98
    /**
99
     * Utility method to convert a date to a readable format 
100
     */
101
    public String convertDate(Long date) {
102
        if (date == null || date == 0) {
103
            return "N/A";
104
        }
105
 
106
        return sdf.format(new Date(date));
107
    }
108
 
109
    public List<Invoice> getInvoices() {
110
        return invoices;
111
    }
112
 
113
    public void setInvoices(List<Invoice> invoices) {
114
        this.invoices = invoices;
115
    }
116
 
117
    public String getInvoiceNumber() {
118
        return invoiceNumber;
119
    }
120
 
121
    public void setInvoiceNumber(String invoiceNumber) {
122
        this.invoiceNumber = invoiceNumber;
123
    }
124
 
125
    public String getDate() {
126
        return date;
127
    }
128
 
129
    public void setDate(String date) {
130
        this.date = date;
131
    }
132
 
133
    public String getReceivedFrom() {
134
        return receivedFrom;
135
    }
136
 
137
    public void setReceivedFrom(String receivedFrom) {
138
        this.receivedFrom = receivedFrom;
139
    }
140
 
141
    public String getSupplierId() {
142
        return supplierId;
143
    }
144
 
145
    public void setSupplierId(String supplierId) {
146
        this.supplierId = supplierId;
147
    }
148
 
149
    public String getNumItems() {
150
        return numItems;
151
    }
152
 
153
    public void setNumItems(String numItems) {
154
        this.numItems = numItems;
155
    }
156
 
157
    public Map<Long, Supplier> getSuppliers() {
158
        return suppliers;
159
    }
160
 
161
    public void setSuppliers(Map<Long, Supplier> suppliers) {
162
        this.suppliers = suppliers;
163
    }
164
}