Subversion Repositories SmartDukaan

Rev

Rev 3936 | Rev 4657 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3936 Rev 4598
Line 157... Line 157...
157
            Map<Date, Long> registerCountMap = new HashMap<Date, Long>();
157
            Map<Date, Long> registerCountMap = new HashMap<Date, Long>();
158
            Map<Date, Double> saleQuantityMap = new HashMap<Date, Double>();
158
            Map<Date, Double> saleQuantityMap = new HashMap<Date, Double>();
159
            Map<Date, Double> saleAmountMap = new HashMap<Date, Double>();
159
            Map<Date, Double> saleAmountMap = new HashMap<Date, Double>();
160
            
160
            
161
            Map<Date, Map<String, Double>> productQuantityMap = new HashMap<Date, Map<String, Double>>();
161
            Map<Date, Map<String, Double>> productQuantityMap = new HashMap<Date, Map<String, Double>>();
-
 
162
            Map<Date, Map<Long, String>> dateTxnProductMap = new HashMap<Date, Map<Long, String>>();
162
            Map<Date, Map<String, Double>> productAmountMap = new HashMap<Date, Map<String,Double>>();
163
            Map<Date, Map<String, Double>> productAmountMap = new HashMap<Date, Map<String,Double>>();
163
            
164
            
164
            for (Affiliate aff : affiliates) {
165
            for (Affiliate aff : affiliates) {
165
                Set<Long> Payments = new HashSet<Long>(); // To deal with multiple refreshes on pay-success page.
166
                Set<Long> Payments = new HashSet<Long>(); // To deal with multiple refreshes on pay-success page.
166
                for (TrackLog tracklog : userClient.getTrackLogsByAffiliate(aff.getId(), startDate.getTime(), endDate.getTime())) {
167
                for (TrackLog tracklog : userClient.getTrackLogsByAffiliate(aff.getId(), startDate.getTime(), endDate.getTime())) {
Line 208... Line 209...
208
                                    
209
                                    
209
                                    String productName = item.getBrand() + " "
210
                                    String productName = item.getBrand() + " "
210
                                            + item.getModel_name() + " "
211
                                            + item.getModel_name() + " "
211
                                            + item.getModel_number();
212
                                            + item.getModel_number();
212
                                    productName = productName.replace("null", "").replaceAll("  ", " ").trim();
213
                                    productName = productName.replace("null", "").replaceAll("  ", " ").trim();
-
 
214
                                    
213
                                    if (productQuantityMap.containsKey(date)) {
215
                                    if (productQuantityMap.containsKey(date)) {
214
                                        Map<String, Double> pQuantityMap = productQuantityMap.get(date);
216
                                        Map<String, Double> pQuantityMap = productQuantityMap.get(date);
215
                                        if (pQuantityMap.containsKey(productName)) {
217
                                        if (pQuantityMap.containsKey(productName)) {
216
                                            Double quantity = pQuantityMap.get(productName);
218
                                            Double quantity = pQuantityMap.get(productName);
217
                                            quantity += item.getQuantity();
219
                                            quantity += item.getQuantity();
Line 225... Line 227...
225
                                        Map<String, Double> pQuantityMap = new HashMap<String, Double>();
227
                                        Map<String, Double> pQuantityMap = new HashMap<String, Double>();
226
                                        pQuantityMap.put(productName, item.getQuantity());
228
                                        pQuantityMap.put(productName, item.getQuantity());
227
                                        productQuantityMap.put(date, pQuantityMap);
229
                                        productQuantityMap.put(date, pQuantityMap);
228
                                    }
230
                                    }
229
                                    
231
                                    
-
 
232
                                    if (dateTxnProductMap.containsKey(date)) {
-
 
233
                                        Map<Long, String> txnProductMap = dateTxnProductMap.get(date);
-
 
234
                                        txnProductMap.put(order.getTransactionId(), productName);
-
 
235
                                    }
-
 
236
                                    else {
-
 
237
                                        Map<Long, String> txnProductMap = new HashMap<Long, String>();
-
 
238
                                        txnProductMap.put(order.getTransactionId(), productName);
-
 
239
                                        dateTxnProductMap.put(date, txnProductMap);
-
 
240
                                    }
-
 
241
                                    
230
                                    if (productAmountMap.containsKey(date)) {
242
                                    if (productAmountMap.containsKey(date)) {
231
                                        Map<String, Double> pAmountMap = productAmountMap.get(date);
243
                                        Map<String, Double> pAmountMap = productAmountMap.get(date);
232
                                        if (pAmountMap.containsKey(productName)) {
244
                                        if (pAmountMap.containsKey(productName)) {
233
                                            Double amount = pAmountMap.get(productName);
245
                                            Double amount = pAmountMap.get(productName);
234
                                            amount += item.getTotal_price();
246
                                            amount += item.getTotal_price();
Line 255... Line 267...
255
            
267
            
256
            response.setHeader("Content-disposition", "inline; filename=" + mAffName + "-affiliate-report" + ".xls");
268
            response.setHeader("Content-disposition", "inline; filename=" + mAffName + "-affiliate-report" + ".xls");
257
            
269
            
258
            ServletOutputStream sos;
270
            ServletOutputStream sos;
259
            try {
271
            try {
260
                ByteArrayOutputStream baos = getSpreadSheetData(mAffName, dates, registerCountMap, saleQuantityMap, saleAmountMap, productQuantityMap, productAmountMap);
272
                ByteArrayOutputStream baos = getSpreadSheetData(mAffName,
-
 
273
                                                                   dates,
-
 
274
                                                                   registerCountMap, 
-
 
275
                                                                   saleQuantityMap, 
-
 
276
                                                                   saleAmountMap, 
-
 
277
                                                                   productQuantityMap, 
-
 
278
                                                                   dateTxnProductMap, 
-
 
279
                                                                   productAmountMap);
261
                sos = response.getOutputStream();
280
                sos = response.getOutputStream();
262
                baos.writeTo(sos);
281
                baos.writeTo(sos);
263
                sos.flush();
282
                sos.flush();
264
            } catch (IOException e) {
283
            } catch (IOException e) {
265
                log.error("Unable to stream the affiliates report", e);
284
                log.error("Unable to stream the affiliates report", e);
Line 281... Line 300...
281
    // Prepares the XLS worksheet object and fills in the data with proper formatting
300
    // Prepares the XLS worksheet object and fills in the data with proper formatting
282
	private ByteArrayOutputStream getSpreadSheetData(String mAffName, 
301
	private ByteArrayOutputStream getSpreadSheetData(String mAffName, 
283
	        SortedSet<Date> dates,
302
	        SortedSet<Date> dates,
284
            Map<Date, Long> registerCountMap,
303
            Map<Date, Long> registerCountMap,
285
            Map<Date, Double> saleQuantityMap,
304
            Map<Date, Double> saleQuantityMap,
286
            Map<Date, Double> saleAmountMap, 
305
            Map<Date, Double> saleAmountMap,
287
            Map<Date, Map<String, Double>> productQuantityMap,
306
            Map<Date, Map<String, Double>> productQuantityMap,
-
 
307
            Map<Date, Map<Long, String>> dateTxnProductMap,
288
            Map<Date, Map<String, Double>> productAmountMap)
308
            Map<Date, Map<String, Double>> productAmountMap)
289
	{
309
	{
290
	    ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
310
	    ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
291
 
311
 
292
		Workbook wb = new HSSFWorkbook();
312
		Workbook wb = new HSSFWorkbook();
Line 299... Line 319...
299
	    CreationHelper createHelper = wb.getCreationHelper();
319
	    CreationHelper createHelper = wb.getCreationHelper();
300
	    CellStyle dateCellStyle = wb.createCellStyle();
320
	    CellStyle dateCellStyle = wb.createCellStyle();
301
	    dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("DD/MM/YYYY"));
321
	    dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("DD/MM/YYYY"));
302
	    
322
	    
303
	    createSummarySheet(mAffName, dates, registerCountMap, saleQuantityMap, saleAmountMap, wb, style, dateCellStyle);
323
	    createSummarySheet(mAffName, dates, registerCountMap, saleQuantityMap, saleAmountMap, wb, style, dateCellStyle);
304
	    createSaleDetailSheet(mAffName, dates, productQuantityMap, productAmountMap, wb, style, dateCellStyle);
324
	    createSaleDetailSheet(mAffName, dates, dateTxnProductMap, productQuantityMap, productAmountMap, wb, style, dateCellStyle);
305
	        	
325
	        	
306
		// Write the workbook to the output stream
326
		// Write the workbook to the output stream
307
		try {
327
		try {
308
			wb.write(baosXLS);
328
			wb.write(baosXLS);
309
			baosXLS.close();
329
			baosXLS.close();
Line 312... Line 332...
312
		}		
332
		}		
313
		return baosXLS;
333
		return baosXLS;
314
	}
334
	}
315
	
335
	
316
    private void createSaleDetailSheet(String mAffName, SortedSet<Date> dates,
336
    private void createSaleDetailSheet(String mAffName, SortedSet<Date> dates,
-
 
337
            Map<Date, Map<Long, String>> dateTxnProductMap,
317
            Map<Date, Map<String, Double>> productQuantityMap,
338
            Map<Date, Map<String, Double>> productQuantityMap,
318
            Map<Date, Map<String, Double>> productAmountMap, Workbook wb,
339
            Map<Date, Map<String, Double>> productAmountMap, Workbook wb,
319
            CellStyle style, CellStyle dateCellStyle) {
340
            CellStyle style, CellStyle dateCellStyle) {
320
        // Product Sales SHEET
341
        // Product Sales SHEET
321
        Sheet affSheet = wb.createSheet("Product Sales Report");
342
        Sheet affSheet = wb.createSheet("Product Sales Report");
Line 345... Line 366...
345
        affSheet.createRow(affSerialNo ++);affSheet.createRow(affSerialNo ++);
366
        affSheet.createRow(affSerialNo ++);affSheet.createRow(affSerialNo ++);
346
        
367
        
347
        Row affHeaderRow = affSheet.createRow(affSerialNo++);
368
        Row affHeaderRow = affSheet.createRow(affSerialNo++);
348
        affHeaderRow.createCell(0).setCellValue("Date");
369
        affHeaderRow.createCell(0).setCellValue("Date");
349
        affHeaderRow.createCell(1).setCellValue("ProductName");
370
        affHeaderRow.createCell(1).setCellValue("ProductName");
350
        affHeaderRow.createCell(2).setCellValue("Sales Count");
371
        affHeaderRow.createCell(2).setCellValue("Transaction Id");
351
        affHeaderRow.createCell(3).setCellValue("Amount");
372
        affHeaderRow.createCell(3).setCellValue("Amount");
352
        for (int i=0; i<4 ;i++) {
373
        for (int i=0; i<4 ;i++) {
353
            affHeaderRow.getCell(i).setCellStyle(style);
374
            affHeaderRow.getCell(i).setCellStyle(style);
354
        }
375
        }
355
        
376
        
356
        Double totalQuantity = 0d;
377
        Double totalQuantity = 0d;
357
        Double totalAmount = 0d;
378
        Double totalAmount = 0d;
358
        for(Date date : dates) {
379
        for(Date date : dates) {
359
            if (!productQuantityMap.containsKey(date)) {
380
            if (!dateTxnProductMap.containsKey(date)) {
360
                continue;
381
                continue;
361
            }
382
            }
-
 
383
            //HashSet<String> tmpProductSet = new HashSet<String>();
-
 
384
            String prodName = null;
-
 
385
            Row commContentRow = null;
362
            for (Entry<String, Double> quantityEntry : productQuantityMap.get(date).entrySet()) {
386
            for (Entry<Long, String> txnEntry : dateTxnProductMap.get(date).entrySet()) {
363
                affSerialNo++;
387
                affSerialNo++;
364
                String prodName = quantityEntry.getKey();
388
                prodName = txnEntry.getValue();
365
                Double quantity = quantityEntry.getValue();
389
                Long txn = txnEntry.getKey();
366
                Double amount = productAmountMap.get(date).get(prodName);
-
 
367
                totalQuantity += quantity;
-
 
368
                totalAmount += amount;
-
 
369
                Row commContentRow = affSheet.createRow(affSerialNo);
390
                commContentRow = affSheet.createRow(affSerialNo);
370
                commContentRow.createCell(0).setCellValue(date);
391
                commContentRow.createCell(0).setCellValue(date);
371
                commContentRow.getCell(0).setCellStyle(dateCellStyle);
392
                commContentRow.getCell(0).setCellStyle(dateCellStyle);
372
                commContentRow.createCell(1).setCellValue(prodName);
393
                commContentRow.createCell(1).setCellValue(prodName);
373
                commContentRow.createCell(2).setCellValue(quantity);
394
                commContentRow.createCell(2).setCellValue(txn);
-
 
395
                Double amount = productAmountMap.get(date).get(prodName);
-
 
396
                amount = amount/(productQuantityMap.get(date).get(prodName));
-
 
397
                totalAmount += amount;
374
                commContentRow.createCell(3).setCellValue(amount);
398
                commContentRow.createCell(3).setCellValue(amount);
375
                
399
                totalQuantity++;
376
            }
400
            }
377
        }
401
        }
378
        affSerialNo+=2;
402
        affSerialNo+=2;
379
        Row commContentRow = affSheet.createRow(affSerialNo);
403
        Row commContentRow = affSheet.createRow(affSerialNo);
380
        commContentRow.createCell(0).setCellValue("Total");
404
        commContentRow.createCell(0).setCellValue("Total");