Subversion Repositories SmartDukaan

Rev

Rev 14053 | Rev 20153 | 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;
7410 amar.kumar 20
import org.apache.shiro.SecurityUtils;
5443 mandeep.dh 21
 
22
/**
23
 * @author mandeep
24
 * 
25
 */
26
public class InvoiceController extends BaseController {
27
    private static Log logger = LogFactory.getLog(InvoiceController.class);
28
 
29
    private List<Invoice> invoices;
30
    private String invoiceNumber;
31
    private String date;
32
    private String receivedFrom;
33
    private String supplierId;
34
    private String numItems;
7410 amar.kumar 35
    private Long warehouseId;
11219 manish.sha 36
    private String invoiceDate;
20151 amit.gupta 37
    private SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
5443 mandeep.dh 38
    private Map<Long, Supplier> suppliers = new HashMap<Long, Supplier>();
39
 
40
    /*
41
     * (non-Javadoc)
42
     * 
43
     * @see in.shop2020.inventory.controllers.BaseController#nindex()
44
     */
45
    @Override
46
    public String index() {
47
        try {
48
            Client purchaseClient = new PurchaseClient().getClient();
49
            invoices = purchaseClient.getInvoices(getYesterday().getTime());
50
            for (Supplier s : purchaseClient.getSuppliers()) {
51
                suppliers.put(s.getId(), s);
52
            }
53
        } catch (Exception e) {
54
            logger.error("Could not fetch invoices", e);
55
        }
56
 
57
        return super.index();
58
    }
59
 
60
    /* (non-Javadoc)
61
     * @see in.shop2020.inventory.controllers.BaseController#editNew()
62
     */
63
    @Override
64
    public String editNew() {
65
        try {
66
            Client purchaseClient = new PurchaseClient().getClient();
67
            for (Supplier s : purchaseClient.getSuppliers()) {
68
                suppliers.put(s.getId(), s);
69
            }
70
        } catch (Exception e) {
71
            logger.error("Could not fetch suppliers", e);
72
        }
73
 
74
        return super.editNew();
75
    }
76
 
77
    private Date getYesterday() {
78
        Calendar calendar = Calendar.getInstance();
79
        calendar.setTime(new Date());
80
        calendar.add(Calendar.DAY_OF_MONTH, -1);
81
        return calendar.getTime();
82
    }
83
 
84
    public String create() {
85
        try {
7410 amar.kumar 86
        	if(!isAutorizedToAccessWarehouse(warehouseId)){
87
        		logger.error("Unauthorized Access for WarehouseId " + warehouseId + " by " + SecurityUtils.getSubject().getPrincipal().toString());
88
        		addActionError("Unauthorized Access for WarehouseId " + warehouseId);
89
        		return EDIT_NEW;
90
        	}
20151 amit.gupta 91
        	long currentTime = new Date().getTime();
13996 manish.sha 92
        	if(currentTime !=sdf.parse(date).getTime()){
93
        		logger.error("Invoice Receive Date Must be equal to Today's Date");
94
        		addActionError("Invoice Receive Date Must be equal to Today's Date");
95
        		return EDIT_NEW;
14053 manish.sha 96
        	}
6630 amar.kumar 97
        	Client purchaseClient = new PurchaseClient().getClient();
7410 amar.kumar 98
       	if(!purchaseClient.isInvoiceReceived(invoiceNumber, Long.parseLong(supplierId))) {
6630 amar.kumar 99
	            Invoice invoice = new Invoice();
100
	            invoice.setInvoiceNumber(invoiceNumber);
101
	            invoice.setDate(sdf.parse(date).getTime());
102
	            invoice.setNumItems(Long.valueOf(numItems));
103
	            invoice.setReceivedFrom(receivedFrom);
104
	            invoice.setSupplierId(Long.valueOf(supplierId));
7410 amar.kumar 105
	            invoice.setWarehouseId(warehouseId);
11219 manish.sha 106
	            invoice.setInvoiceDate(sdf.parse(invoiceDate).getTime());
6630 amar.kumar 107
	            purchaseClient = new PurchaseClient().getClient();
108
	            purchaseClient.createInvoice(invoice);
7410 amar.kumar 109
        	} else {
110
        		addActionError("Duplicate invoice " + invoiceNumber + " for supplierId " + supplierId);
111
        		return EDIT_NEW;
6630 amar.kumar 112
        	}
5443 mandeep.dh 113
        } catch (Exception e) {
114
            logger.error("Could not create invoice", e);
115
        }
116
 
117
        return index();
118
    }
119
 
120
    /**
121
     * Utility method to convert a date to a readable format 
122
     */
123
    public String convertDate(Long date) {
124
        if (date == null || date == 0) {
125
            return "N/A";
126
        }
127
 
128
        return sdf.format(new Date(date));
129
    }
130
 
131
    public List<Invoice> getInvoices() {
132
        return invoices;
133
    }
134
 
135
    public void setInvoices(List<Invoice> invoices) {
136
        this.invoices = invoices;
137
    }
138
 
139
    public String getInvoiceNumber() {
140
        return invoiceNumber;
141
    }
142
 
143
    public void setInvoiceNumber(String invoiceNumber) {
144
        this.invoiceNumber = invoiceNumber;
145
    }
146
 
147
    public String getDate() {
148
        return date;
149
    }
150
 
151
    public void setDate(String date) {
152
        this.date = date;
153
    }
154
 
155
    public String getReceivedFrom() {
156
        return receivedFrom;
157
    }
158
 
159
    public void setReceivedFrom(String receivedFrom) {
160
        this.receivedFrom = receivedFrom;
161
    }
162
 
163
    public String getSupplierId() {
164
        return supplierId;
165
    }
166
 
167
    public void setSupplierId(String supplierId) {
168
        this.supplierId = supplierId;
169
    }
170
 
171
    public String getNumItems() {
172
        return numItems;
173
    }
174
 
175
    public void setNumItems(String numItems) {
176
        this.numItems = numItems;
177
    }
178
 
179
    public Map<Long, Supplier> getSuppliers() {
180
        return suppliers;
181
    }
182
 
183
    public void setSuppliers(Map<Long, Supplier> suppliers) {
184
        this.suppliers = suppliers;
185
    }
7410 amar.kumar 186
 
187
	public Long getWarehouseId() {
188
		return warehouseId;
189
	}
190
 
191
	public void setWarehouseId(Long warehouseId) {
192
		this.warehouseId = warehouseId;
193
	}
11219 manish.sha 194
 
195
	public String getInvoiceDate() {
196
		return invoiceDate;
197
	}
198
 
199
	public void setInvoiceDate(String invoiceDate) {
200
		this.invoiceDate = invoiceDate;
201
	}
202
 
5443 mandeep.dh 203
}