| 1901 |
vikas |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
| 2340 |
vikas |
3 |
import in.shop2020.model.v1.order.LineItem;
|
|
|
4 |
import in.shop2020.model.v1.order.Order;
|
| 2980 |
vikas |
5 |
import in.shop2020.model.v1.order.OrderStatus;
|
| 1901 |
vikas |
6 |
import in.shop2020.model.v1.user.Affiliate;
|
|
|
7 |
import in.shop2020.model.v1.user.MasterAffiliate;
|
|
|
8 |
import in.shop2020.model.v1.user.TrackLog;
|
|
|
9 |
import in.shop2020.model.v1.user.UserContextService.Client;
|
| 2340 |
vikas |
10 |
import in.shop2020.payments.Payment;
|
| 3125 |
rajveer |
11 |
import in.shop2020.thrift.clients.PaymentClient;
|
|
|
12 |
import in.shop2020.thrift.clients.TransactionClient;
|
|
|
13 |
import in.shop2020.thrift.clients.UserClient;
|
| 1901 |
vikas |
14 |
|
|
|
15 |
import java.io.ByteArrayOutputStream;
|
|
|
16 |
import java.io.IOException;
|
| 2501 |
vikas |
17 |
import java.util.ArrayList;
|
| 1901 |
vikas |
18 |
import java.util.Date;
|
|
|
19 |
import java.util.HashMap;
|
| 2393 |
vikas |
20 |
import java.util.HashSet;
|
| 1901 |
vikas |
21 |
import java.util.LinkedList;
|
|
|
22 |
import java.util.List;
|
|
|
23 |
import java.util.Map;
|
| 2393 |
vikas |
24 |
import java.util.Set;
|
| 1901 |
vikas |
25 |
|
|
|
26 |
import javax.servlet.ServletOutputStream;
|
|
|
27 |
import javax.servlet.http.HttpServletRequest;
|
|
|
28 |
import javax.servlet.http.HttpServletResponse;
|
|
|
29 |
|
|
|
30 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
31 |
import org.apache.poi.ss.usermodel.Cell;
|
|
|
32 |
import org.apache.poi.ss.usermodel.CellStyle;
|
|
|
33 |
import org.apache.poi.ss.usermodel.CreationHelper;
|
|
|
34 |
import org.apache.poi.ss.usermodel.Font;
|
|
|
35 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
36 |
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
37 |
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
38 |
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
39 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
40 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
41 |
|
| 2904 |
chandransh |
42 |
/**
|
|
|
43 |
* Provides reports about successful registrations and orders place through an
|
|
|
44 |
* affiliate.
|
|
|
45 |
*
|
|
|
46 |
* @author Vikas Malik
|
|
|
47 |
*
|
|
|
48 |
*/
|
| 1901 |
vikas |
49 |
public class AffiliateController implements ServletResponseAware, ServletRequestAware{
|
|
|
50 |
|
|
|
51 |
private HttpServletResponse response;
|
|
|
52 |
private HttpServletRequest request;
|
|
|
53 |
|
|
|
54 |
private String errorMsg = "";
|
|
|
55 |
private List<MasterAffiliate> masterAffiliates;
|
|
|
56 |
|
|
|
57 |
@Override
|
|
|
58 |
public void setServletResponse(HttpServletResponse res) {
|
|
|
59 |
this.response = res;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
@Override
|
|
|
63 |
public void setServletRequest(HttpServletRequest req) {
|
|
|
64 |
this.request = req;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
public String index() {
|
|
|
68 |
|
|
|
69 |
try {
|
| 3125 |
rajveer |
70 |
UserClient userContextServiceClient = new UserClient();
|
| 1901 |
vikas |
71 |
Client userClient = userContextServiceClient.getClient();
|
|
|
72 |
masterAffiliates = userClient.getAllMasterAffiliates();
|
|
|
73 |
} catch (Exception e) {
|
|
|
74 |
e.printStackTrace();
|
|
|
75 |
}
|
|
|
76 |
return "report";
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
public String create() {
|
|
|
80 |
try {
|
|
|
81 |
long mAfId = Long.parseLong(request.getParameter("masterAffiliate"));
|
| 3125 |
rajveer |
82 |
UserClient userContextServiceClient = new UserClient();
|
|
|
83 |
PaymentClient paymentServiceClient = new PaymentClient();
|
|
|
84 |
TransactionClient transactionServiceClient = new TransactionClient();
|
| 2340 |
vikas |
85 |
|
| 1901 |
vikas |
86 |
Client userClient = userContextServiceClient.getClient();
|
| 2340 |
vikas |
87 |
in.shop2020.payments.PaymentService.Client paymentClient = paymentServiceClient.getClient();
|
|
|
88 |
|
| 2393 |
vikas |
89 |
Set<Long> Payments = new HashSet<Long>();
|
| 2501 |
vikas |
90 |
List<Map<String, String>> contentRows = new LinkedList<Map<String,String>>();
|
|
|
91 |
List<Affiliate> affiliates;
|
|
|
92 |
if (mAfId == -1) {
|
|
|
93 |
affiliates = new ArrayList<Affiliate>();
|
|
|
94 |
for(MasterAffiliate mAffiliate : userClient.getAllMasterAffiliates()) {
|
|
|
95 |
affiliates.addAll(userClient.getAffiliatesByMasterAffiliate(mAffiliate.getId()));
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
else {
|
|
|
99 |
affiliates = userClient.getAffiliatesByMasterAffiliate(mAfId);
|
|
|
100 |
}
|
| 2980 |
vikas |
101 |
|
| 2501 |
vikas |
102 |
for (Affiliate aff : affiliates) {
|
|
|
103 |
MasterAffiliate mAffiliate = userClient.getMasterAffiliateById(aff.getMasterAffiliateId());
|
| 2000 |
vikas |
104 |
for (TrackLog tracklog : userClient.getTrackLogsByAffiliate(aff.getId())) {
|
| 2980 |
vikas |
105 |
boolean orderCompleted = false;
|
| 2000 |
vikas |
106 |
Map<String, String> contentRow = new HashMap<String, String>();
|
|
|
107 |
contentRow.put("mAffiliate", mAffiliate.getName());
|
|
|
108 |
contentRow.put("affiliate", aff.getName());
|
|
|
109 |
contentRow.put("affiliateUrl", aff.getUrl());
|
|
|
110 |
contentRow.put("date",String.valueOf(tracklog.getAddedOn()));
|
|
|
111 |
contentRow.put("event", tracklog.getEvent());
|
|
|
112 |
contentRow.put("url", tracklog.getUrl());
|
| 2345 |
vikas |
113 |
if (tracklog.getEvent().equals("payment success")) {
|
| 2340 |
vikas |
114 |
try {
|
| 2345 |
vikas |
115 |
long paymentId = Long.parseLong(tracklog.getData().replaceAll("\\(*\\)", ""));
|
| 2393 |
vikas |
116 |
if (Payments.contains(paymentId)) {
|
|
|
117 |
continue;
|
|
|
118 |
}
|
|
|
119 |
Payments.add(paymentId);
|
| 2340 |
vikas |
120 |
Payment payment = paymentClient
|
|
|
121 |
.getPayment(paymentId);
|
|
|
122 |
List<Order> orders = transactionServiceClient
|
|
|
123 |
.getClient().getOrdersForTransaction(
|
|
|
124 |
payment.getMerchantTxnId(),
|
|
|
125 |
payment.getUserId());
|
|
|
126 |
contentRow.put("amount",
|
|
|
127 |
Double.toString(payment.getAmount()));
|
| 2345 |
vikas |
128 |
String itemString = "";
|
| 2340 |
vikas |
129 |
for (Order order : orders) {
|
| 2980 |
vikas |
130 |
if (order.getStatus() == OrderStatus.DELIVERY_SUCCESS) {
|
|
|
131 |
orderCompleted = true;
|
|
|
132 |
}
|
| 2340 |
vikas |
133 |
List<LineItem> items = order.getLineitems();
|
|
|
134 |
for (LineItem item : items) {
|
|
|
135 |
itemString += item.getBrand() + " "
|
|
|
136 |
+ item.getModel_name() + " "
|
|
|
137 |
+ item.getModel_number() + "("
|
|
|
138 |
+ item.getQuantity() + ", "
|
|
|
139 |
+ item.getTotal_price() + "), ";
|
|
|
140 |
}
|
| 2345 |
vikas |
141 |
contentRow.put("items", itemString.replaceAll("null", ""));
|
| 2340 |
vikas |
142 |
}
|
|
|
143 |
} catch (Exception e) {
|
|
|
144 |
e.printStackTrace();
|
|
|
145 |
}
|
|
|
146 |
}
|
| 2000 |
vikas |
147 |
contentRow.put("data", tracklog.getData());
|
| 2980 |
vikas |
148 |
if (orderCompleted) {
|
|
|
149 |
contentRows.add(contentRow);
|
|
|
150 |
}
|
| 1901 |
vikas |
151 |
}
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
// Preparing XLS file for output
|
|
|
155 |
response.setContentType("application/vnd.ms-excel");
|
|
|
156 |
|
| 2058 |
vikas |
157 |
response.setHeader("Content-disposition", "inline; filename=affiliate-report" + ".xls");
|
| 1901 |
vikas |
158 |
|
|
|
159 |
ServletOutputStream sos;
|
|
|
160 |
try {
|
| 2501 |
vikas |
161 |
ByteArrayOutputStream baos = getSpreadSheetData(contentRows);
|
| 1901 |
vikas |
162 |
sos = response.getOutputStream();
|
|
|
163 |
baos.writeTo(sos);
|
|
|
164 |
sos.flush();
|
|
|
165 |
} catch (IOException e) {
|
|
|
166 |
errorMsg = "Failed to write to response.";
|
|
|
167 |
e.printStackTrace();
|
|
|
168 |
}
|
|
|
169 |
} catch (Exception e) {
|
|
|
170 |
errorMsg = e.getMessage();
|
|
|
171 |
e.printStackTrace();
|
|
|
172 |
}
|
|
|
173 |
return null;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
// Prepares the XLS worksheet object and fills in the data with proper formatting
|
| 2501 |
vikas |
177 |
private ByteArrayOutputStream getSpreadSheetData(List<Map<String, String>> contentRows)
|
| 1901 |
vikas |
178 |
{
|
|
|
179 |
ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
|
|
|
180 |
|
|
|
181 |
Workbook wb = new HSSFWorkbook();
|
|
|
182 |
|
|
|
183 |
Font font = wb.createFont();
|
|
|
184 |
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
|
|
185 |
CellStyle style = wb.createCellStyle();
|
|
|
186 |
style.setFont(font);
|
|
|
187 |
|
|
|
188 |
CreationHelper createHelper = wb.getCreationHelper();
|
|
|
189 |
CellStyle dateCellStyle = wb.createCellStyle();
|
|
|
190 |
dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("DD/MM/YYYY HH:MM"));
|
|
|
191 |
|
| 2501 |
vikas |
192 |
createAffiliateSheet(contentRows, wb, style, dateCellStyle);
|
| 1901 |
vikas |
193 |
|
|
|
194 |
// Write the workbook to the output stream
|
|
|
195 |
try {
|
|
|
196 |
wb.write(baosXLS);
|
|
|
197 |
baosXLS.close();
|
|
|
198 |
} catch (IOException e) {
|
|
|
199 |
e.printStackTrace();
|
|
|
200 |
}
|
|
|
201 |
return baosXLS;
|
|
|
202 |
}
|
|
|
203 |
|
| 2501 |
vikas |
204 |
private void createAffiliateSheet(List<Map<String, String>> contentRows, Workbook wb, CellStyle style, CellStyle dateCellStyle)
|
| 1901 |
vikas |
205 |
{
|
|
|
206 |
// Affiliate SHEET
|
|
|
207 |
Sheet affSheet = wb.createSheet("Affiliates Report");
|
|
|
208 |
short affSerialNo = 0;
|
|
|
209 |
|
|
|
210 |
Row affTitleRow = affSheet.createRow(affSerialNo ++);
|
|
|
211 |
Cell affTitleCell = affTitleRow.createCell(0);
|
| 2501 |
vikas |
212 |
affTitleCell.setCellValue("Affiliates Report");
|
| 1901 |
vikas |
213 |
affTitleCell.setCellStyle(style);
|
|
|
214 |
|
|
|
215 |
affSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
|
|
|
216 |
|
|
|
217 |
affSheet.createRow(affSerialNo ++);
|
|
|
218 |
|
|
|
219 |
Row affHeaderRow = affSheet.createRow(affSerialNo++);
|
|
|
220 |
affHeaderRow.createCell(0).setCellValue("Affiliate");
|
|
|
221 |
affHeaderRow.createCell(1).setCellValue("Affiliate Url");
|
|
|
222 |
affHeaderRow.createCell(2).setCellValue("Date");
|
|
|
223 |
affHeaderRow.createCell(3).setCellValue("Event");
|
|
|
224 |
affHeaderRow.createCell(4).setCellValue("Url");
|
|
|
225 |
affHeaderRow.createCell(5).setCellValue("Data");
|
| 2340 |
vikas |
226 |
affHeaderRow.createCell(6).setCellValue("Items");
|
|
|
227 |
affHeaderRow.createCell(7).setCellValue("Amount");
|
|
|
228 |
for (int i=0; i<8 ;i++) {
|
| 1901 |
vikas |
229 |
affHeaderRow.getCell(i).setCellStyle(style);
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
for( Map<String, String> contentRow : contentRows) {
|
|
|
233 |
affSerialNo++;
|
|
|
234 |
Row commContentRow = affSheet.createRow(affSerialNo);
|
|
|
235 |
|
|
|
236 |
commContentRow.createCell(0).setCellValue(contentRow.get("affiliate"));
|
|
|
237 |
commContentRow.createCell(1).setCellValue(contentRow.get("affiliateUrl"));
|
|
|
238 |
commContentRow.createCell(2).setCellValue(new Date(Long.parseLong(contentRow.get("date"))));
|
|
|
239 |
commContentRow.getCell(2).setCellStyle(dateCellStyle);
|
|
|
240 |
commContentRow.createCell(3).setCellValue(contentRow.get("event"));
|
|
|
241 |
commContentRow.createCell(4).setCellValue(contentRow.get("url"));
|
|
|
242 |
commContentRow.createCell(5).setCellValue(contentRow.get("data"));
|
| 2340 |
vikas |
243 |
if (contentRow.containsKey("items")) {
|
|
|
244 |
commContentRow.createCell(6).setCellValue(contentRow.get("items"));
|
|
|
245 |
}
|
|
|
246 |
if (contentRow.containsKey("amount")) {
|
|
|
247 |
commContentRow.createCell(7).setCellValue(contentRow.get("amount"));
|
|
|
248 |
}
|
| 1901 |
vikas |
249 |
}
|
| 2340 |
vikas |
250 |
for (int i = 0; i<8; i++) {
|
| 1901 |
vikas |
251 |
affSheet.autoSizeColumn(i);
|
|
|
252 |
}
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
public String getErrorMsg() {
|
|
|
256 |
return errorMsg;
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
public List<MasterAffiliate> getMasterAffiliates() {
|
|
|
260 |
return masterAffiliates;
|
|
|
261 |
}
|
| 2340 |
vikas |
262 |
}
|