Subversion Repositories SmartDukaan

Rev

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