Subversion Repositories SmartDukaan

Rev

Rev 7410 | Rev 13996 | 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;
5443 mandeep.dh 37
    private SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
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
        	}
6630 amar.kumar 91
        	Client purchaseClient = new PurchaseClient().getClient();
7410 amar.kumar 92
       	if(!purchaseClient.isInvoiceReceived(invoiceNumber, Long.parseLong(supplierId))) {
6630 amar.kumar 93
	            Invoice invoice = new Invoice();
94
	            invoice.setInvoiceNumber(invoiceNumber);
95
	            invoice.setDate(sdf.parse(date).getTime());
96
	            invoice.setNumItems(Long.valueOf(numItems));
97
	            invoice.setReceivedFrom(receivedFrom);
98
	            invoice.setSupplierId(Long.valueOf(supplierId));
7410 amar.kumar 99
	            invoice.setWarehouseId(warehouseId);
11219 manish.sha 100
	            invoice.setInvoiceDate(sdf.parse(invoiceDate).getTime());
6630 amar.kumar 101
	            purchaseClient = new PurchaseClient().getClient();
102
	            purchaseClient.createInvoice(invoice);
7410 amar.kumar 103
        	} else {
104
        		addActionError("Duplicate invoice " + invoiceNumber + " for supplierId " + supplierId);
105
        		return EDIT_NEW;
6630 amar.kumar 106
        	}
5443 mandeep.dh 107
        } catch (Exception e) {
108
            logger.error("Could not create invoice", e);
109
        }
110
 
111
        return index();
112
    }
113
 
114
    /**
115
     * Utility method to convert a date to a readable format 
116
     */
117
    public String convertDate(Long date) {
118
        if (date == null || date == 0) {
119
            return "N/A";
120
        }
121
 
122
        return sdf.format(new Date(date));
123
    }
124
 
125
    public List<Invoice> getInvoices() {
126
        return invoices;
127
    }
128
 
129
    public void setInvoices(List<Invoice> invoices) {
130
        this.invoices = invoices;
131
    }
132
 
133
    public String getInvoiceNumber() {
134
        return invoiceNumber;
135
    }
136
 
137
    public void setInvoiceNumber(String invoiceNumber) {
138
        this.invoiceNumber = invoiceNumber;
139
    }
140
 
141
    public String getDate() {
142
        return date;
143
    }
144
 
145
    public void setDate(String date) {
146
        this.date = date;
147
    }
148
 
149
    public String getReceivedFrom() {
150
        return receivedFrom;
151
    }
152
 
153
    public void setReceivedFrom(String receivedFrom) {
154
        this.receivedFrom = receivedFrom;
155
    }
156
 
157
    public String getSupplierId() {
158
        return supplierId;
159
    }
160
 
161
    public void setSupplierId(String supplierId) {
162
        this.supplierId = supplierId;
163
    }
164
 
165
    public String getNumItems() {
166
        return numItems;
167
    }
168
 
169
    public void setNumItems(String numItems) {
170
        this.numItems = numItems;
171
    }
172
 
173
    public Map<Long, Supplier> getSuppliers() {
174
        return suppliers;
175
    }
176
 
177
    public void setSuppliers(Map<Long, Supplier> suppliers) {
178
        this.suppliers = suppliers;
179
    }
7410 amar.kumar 180
 
181
	public Long getWarehouseId() {
182
		return warehouseId;
183
	}
184
 
185
	public void setWarehouseId(Long warehouseId) {
186
		this.warehouseId = warehouseId;
187
	}
11219 manish.sha 188
 
189
	public String getInvoiceDate() {
190
		return invoiceDate;
191
	}
192
 
193
	public void setInvoiceDate(String invoiceDate) {
194
		this.invoiceDate = invoiceDate;
195
	}
196
 
5443 mandeep.dh 197
}