Subversion Repositories SmartDukaan

Rev

Rev 5443 | Rev 7410 | Go to most recent revision | Details | Compare with Previous | 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 {
6630 amar.kumar 83
        	Client purchaseClient = new PurchaseClient().getClient();
84
        	if(purchaseClient.getInvoice(invoiceNumber, Long.parseLong(supplierId))== null) {
85
	            Invoice invoice = new Invoice();
86
	            invoice.setInvoiceNumber(invoiceNumber);
87
	            invoice.setDate(sdf.parse(date).getTime());
88
	            invoice.setNumItems(Long.valueOf(numItems));
89
	            invoice.setReceivedFrom(receivedFrom);
90
	            invoice.setSupplierId(Long.valueOf(supplierId));
91
	            purchaseClient = new PurchaseClient().getClient();
92
	            purchaseClient.createInvoice(invoice);
93
        	}
5443 mandeep.dh 94
        } catch (Exception e) {
95
            logger.error("Could not create invoice", e);
96
        }
97
 
98
        return index();
99
    }
100
 
101
    /**
102
     * Utility method to convert a date to a readable format 
103
     */
104
    public String convertDate(Long date) {
105
        if (date == null || date == 0) {
106
            return "N/A";
107
        }
108
 
109
        return sdf.format(new Date(date));
110
    }
111
 
112
    public List<Invoice> getInvoices() {
113
        return invoices;
114
    }
115
 
116
    public void setInvoices(List<Invoice> invoices) {
117
        this.invoices = invoices;
118
    }
119
 
120
    public String getInvoiceNumber() {
121
        return invoiceNumber;
122
    }
123
 
124
    public void setInvoiceNumber(String invoiceNumber) {
125
        this.invoiceNumber = invoiceNumber;
126
    }
127
 
128
    public String getDate() {
129
        return date;
130
    }
131
 
132
    public void setDate(String date) {
133
        this.date = date;
134
    }
135
 
136
    public String getReceivedFrom() {
137
        return receivedFrom;
138
    }
139
 
140
    public void setReceivedFrom(String receivedFrom) {
141
        this.receivedFrom = receivedFrom;
142
    }
143
 
144
    public String getSupplierId() {
145
        return supplierId;
146
    }
147
 
148
    public void setSupplierId(String supplierId) {
149
        this.supplierId = supplierId;
150
    }
151
 
152
    public String getNumItems() {
153
        return numItems;
154
    }
155
 
156
    public void setNumItems(String numItems) {
157
        this.numItems = numItems;
158
    }
159
 
160
    public Map<Long, Supplier> getSuppliers() {
161
        return suppliers;
162
    }
163
 
164
    public void setSuppliers(Map<Long, Supplier> suppliers) {
165
        this.suppliers = suppliers;
166
    }
167
}