Subversion Repositories SmartDukaan

Rev

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