| Line 41... |
Line 41... |
| 41 |
import org.springframework.transaction.annotation.Transactional;
|
41 |
import org.springframework.transaction.annotation.Transactional;
|
| 42 |
import org.springframework.ui.Model;
|
42 |
import org.springframework.ui.Model;
|
| 43 |
import org.springframework.web.bind.annotation.*;
|
43 |
import org.springframework.web.bind.annotation.*;
|
| 44 |
import org.springframework.web.multipart.MultipartFile;
|
44 |
import org.springframework.web.multipart.MultipartFile;
|
| 45 |
|
45 |
|
| - |
|
46 |
import org.apache.poi.ss.usermodel.CellStyle;
|
| - |
|
47 |
import org.apache.poi.ss.usermodel.Font;
|
| - |
|
48 |
import org.apache.poi.xssf.usermodel.XSSFRow;
|
| - |
|
49 |
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
| - |
|
50 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
| - |
|
51 |
|
| 46 |
import javax.mail.MessagingException;
|
52 |
import javax.mail.MessagingException;
|
| 47 |
import javax.servlet.http.HttpServletRequest;
|
53 |
import javax.servlet.http.HttpServletRequest;
|
| - |
|
54 |
import java.io.ByteArrayInputStream;
|
| - |
|
55 |
import java.io.ByteArrayOutputStream;
|
| 48 |
import java.io.IOException;
|
56 |
import java.io.IOException;
|
| 49 |
import java.time.LocalDate;
|
57 |
import java.time.LocalDate;
|
| 50 |
import java.time.LocalDateTime;
|
58 |
import java.time.LocalDateTime;
|
| 51 |
import java.util.*;
|
59 |
import java.util.*;
|
| 52 |
import java.util.stream.Collectors;
|
60 |
import java.util.stream.Collectors;
|
| Line 399... |
Line 407... |
| 399 |
LOGGER.info("partnersItemDescription" + partnersItemDescription);
|
407 |
LOGGER.info("partnersItemDescription" + partnersItemDescription);
|
| 400 |
|
408 |
|
| 401 |
model.addAttribute("response1", mvcResponseSender.createResponseString(partnersItemDescription));
|
409 |
model.addAttribute("response1", mvcResponseSender.createResponseString(partnersItemDescription));
|
| 402 |
return "response";
|
410 |
return "response";
|
| 403 |
}
|
411 |
}
|
| - |
|
412 |
|
| - |
|
413 |
@RequestMapping(value = "/downloadVendorPricingChangesReport", method = RequestMethod.GET)
|
| - |
|
414 |
public ResponseEntity<?> downloadVendorPricingChangesReport(
|
| - |
|
415 |
@RequestParam(name = "startDate") LocalDate startDate,
|
| - |
|
416 |
@RequestParam(name = "endDate") LocalDate endDate) throws Exception {
|
| - |
|
417 |
|
| - |
|
418 |
Map<Integer, Supplier> supplierMap = supplierRepository.selectAll().stream()
|
| - |
|
419 |
.filter(s -> s.isStatus() && !s.isInternal())
|
| - |
|
420 |
.collect(Collectors.toMap(Supplier::getId, s -> s));
|
| - |
|
421 |
|
| - |
|
422 |
List<VendorPriceCircularModel> allBaseline = vendorCatalogPricingLogRepository.getVendorPricesOnDate(0, startDate);
|
| - |
|
423 |
Map<Integer, List<VendorPriceCircularModel>> baselineByVendor = allBaseline.stream()
|
| - |
|
424 |
.filter(p -> supplierMap.containsKey(p.getVendorId()))
|
| - |
|
425 |
.collect(Collectors.groupingBy(VendorPriceCircularModel::getVendorId));
|
| - |
|
426 |
|
| - |
|
427 |
List<VendorPriceCircularModel> allChanges = vendorCatalogPricingLogRepository.selectApprovedInDateRange(startDate, endDate);
|
| - |
|
428 |
Map<Integer, List<VendorPriceCircularModel>> changesByVendor = allChanges.stream()
|
| - |
|
429 |
.collect(Collectors.groupingBy(VendorPriceCircularModel::getVendorId));
|
| - |
|
430 |
|
| - |
|
431 |
Set<Integer> allCatalogIds = new HashSet<>();
|
| - |
|
432 |
allBaseline.stream().filter(p -> supplierMap.containsKey(p.getVendorId())).forEach(p -> allCatalogIds.add(p.getCatalogId()));
|
| - |
|
433 |
allChanges.forEach(p -> allCatalogIds.add(p.getCatalogId()));
|
| - |
|
434 |
|
| - |
|
435 |
Map<Integer, List<Item>> itemMap = new HashMap<>();
|
| - |
|
436 |
if (!allCatalogIds.isEmpty()) {
|
| - |
|
437 |
itemMap = itemRepository.selectAllByCatalogIds(allCatalogIds).stream()
|
| - |
|
438 |
.collect(Collectors.groupingBy(Item::getCatalogItemId));
|
| - |
|
439 |
}
|
| - |
|
440 |
|
| - |
|
441 |
ByteArrayOutputStream bos = buildVendorPricingChangesExcel(supplierMap, baselineByVendor, changesByVendor, itemMap);
|
| - |
|
442 |
|
| - |
|
443 |
HttpHeaders headers = new HttpHeaders();
|
| - |
|
444 |
headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
| - |
|
445 |
headers.set("Content-disposition", "attachment; filename=vendor-pricing-changes-" + startDate + "-to-" + endDate + ".xlsx");
|
| - |
|
446 |
headers.setContentLength(bos.toByteArray().length);
|
| - |
|
447 |
|
| - |
|
448 |
InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(bos.toByteArray()));
|
| - |
|
449 |
return new ResponseEntity<>(resource, headers, org.springframework.http.HttpStatus.OK);
|
| - |
|
450 |
}
|
| - |
|
451 |
|
| - |
|
452 |
private ByteArrayOutputStream buildVendorPricingChangesExcel(
|
| - |
|
453 |
Map<Integer, Supplier> supplierMap,
|
| - |
|
454 |
Map<Integer, List<VendorPriceCircularModel>> baselineByVendor,
|
| - |
|
455 |
Map<Integer, List<VendorPriceCircularModel>> changesByVendor,
|
| - |
|
456 |
Map<Integer, List<Item>> itemMap) throws Exception {
|
| - |
|
457 |
|
| - |
|
458 |
XSSFWorkbook workbook = new XSSFWorkbook();
|
| - |
|
459 |
XSSFSheet sheet = workbook.createSheet("Pricing Changes");
|
| - |
|
460 |
|
| - |
|
461 |
CellStyle headerStyle = workbook.createCellStyle();
|
| - |
|
462 |
Font boldFont = workbook.createFont();
|
| - |
|
463 |
boldFont.setBold(true);
|
| - |
|
464 |
headerStyle.setFont(boldFont);
|
| - |
|
465 |
|
| - |
|
466 |
int rowNum = 0;
|
| - |
|
467 |
|
| - |
|
468 |
XSSFRow colRow = sheet.createRow(rowNum++);
|
| - |
|
469 |
String[] columns = {"Vendor", "Effective Date", "Model", "Catalog Id", "TP", "DP", "MOP"};
|
| - |
|
470 |
for (int i = 0; i < columns.length; i++) {
|
| - |
|
471 |
colRow.createCell(i).setCellValue(columns[i]);
|
| - |
|
472 |
colRow.getCell(i).setCellStyle(headerStyle);
|
| - |
|
473 |
}
|
| - |
|
474 |
|
| - |
|
475 |
List<Map.Entry<Integer, Supplier>> sortedVendors = supplierMap.entrySet().stream()
|
| - |
|
476 |
.sorted(Comparator.comparing(e -> e.getValue().getName()))
|
| - |
|
477 |
.collect(Collectors.toList());
|
| - |
|
478 |
|
| - |
|
479 |
for (Map.Entry<Integer, Supplier> vendorEntry : sortedVendors) {
|
| - |
|
480 |
int vendorId = vendorEntry.getKey();
|
| - |
|
481 |
String vendorName = vendorEntry.getValue().getName();
|
| - |
|
482 |
|
| - |
|
483 |
List<VendorPriceCircularModel> baseline = baselineByVendor.getOrDefault(vendorId, Collections.emptyList());
|
| - |
|
484 |
List<VendorPriceCircularModel> changes = changesByVendor.getOrDefault(vendorId, Collections.emptyList());
|
| - |
|
485 |
|
| - |
|
486 |
if (baseline.isEmpty() && changes.isEmpty()) {
|
| - |
|
487 |
continue;
|
| - |
|
488 |
}
|
| - |
|
489 |
|
| - |
|
490 |
Map<Integer, Float> tpTracker = new HashMap<>();
|
| - |
|
491 |
|
| - |
|
492 |
for (VendorPriceCircularModel bp : baseline) {
|
| - |
|
493 |
XSSFRow dataRow = sheet.createRow(rowNum++);
|
| - |
|
494 |
dataRow.createCell(0).setCellValue(vendorName);
|
| - |
|
495 |
dataRow.createCell(1).setCellValue(FormattingUtils.formatDate(bp.getEffectedOn()));
|
| - |
|
496 |
dataRow.createCell(2).setCellValue(getModelName(itemMap, bp.getCatalogId()));
|
| - |
|
497 |
dataRow.createCell(3).setCellValue(bp.getCatalogId());
|
| - |
|
498 |
dataRow.createCell(4).setCellValue(bp.getTransferPrice());
|
| - |
|
499 |
dataRow.createCell(5).setCellValue(bp.getDealerPrice());
|
| - |
|
500 |
dataRow.createCell(6).setCellValue(bp.getMop());
|
| - |
|
501 |
tpTracker.put(bp.getCatalogId(), bp.getTransferPrice());
|
| - |
|
502 |
}
|
| - |
|
503 |
|
| - |
|
504 |
for (VendorPriceCircularModel cp : changes) {
|
| - |
|
505 |
Float lastTp = tpTracker.get(cp.getCatalogId());
|
| - |
|
506 |
if (lastTp == null || Float.compare(lastTp, cp.getTransferPrice()) != 0) {
|
| - |
|
507 |
XSSFRow dataRow = sheet.createRow(rowNum++);
|
| - |
|
508 |
dataRow.createCell(0).setCellValue(vendorName);
|
| - |
|
509 |
dataRow.createCell(1).setCellValue(FormattingUtils.formatDate(cp.getEffectedOn()));
|
| - |
|
510 |
dataRow.createCell(2).setCellValue(getModelName(itemMap, cp.getCatalogId()));
|
| - |
|
511 |
dataRow.createCell(3).setCellValue(cp.getCatalogId());
|
| - |
|
512 |
dataRow.createCell(4).setCellValue(cp.getTransferPrice());
|
| - |
|
513 |
dataRow.createCell(5).setCellValue(cp.getDealerPrice());
|
| - |
|
514 |
dataRow.createCell(6).setCellValue(cp.getMop());
|
| - |
|
515 |
tpTracker.put(cp.getCatalogId(), cp.getTransferPrice());
|
| - |
|
516 |
}
|
| - |
|
517 |
}
|
| - |
|
518 |
}
|
| - |
|
519 |
|
| - |
|
520 |
for (int i = 0; i < 7; i++) {
|
| - |
|
521 |
sheet.autoSizeColumn(i);
|
| - |
|
522 |
}
|
| - |
|
523 |
|
| - |
|
524 |
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
| - |
|
525 |
workbook.write(bos);
|
| - |
|
526 |
workbook.close();
|
| - |
|
527 |
return bos;
|
| - |
|
528 |
}
|
| - |
|
529 |
|
| - |
|
530 |
private String getModelName(Map<Integer, List<Item>> itemMap, int catalogId) {
|
| - |
|
531 |
List<Item> items = itemMap.get(catalogId);
|
| - |
|
532 |
if (items != null && !items.isEmpty()) {
|
| - |
|
533 |
return items.get(0).getItemDescriptionNoColor();
|
| - |
|
534 |
}
|
| - |
|
535 |
return "Unknown (" + catalogId + ")";
|
| - |
|
536 |
}
|
| 404 |
}
|
537 |
}
|