Subversion Repositories SmartDukaan

Rev

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

Rev 26082 Rev 26131
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
3
import java.io.Serializable;
3
import java.io.Serializable;
4
import java.time.LocalDate;
4
import java.time.LocalDate;
5
import java.time.LocalDateTime;
5
import java.time.LocalDateTime;
-
 
6
import java.time.LocalTime;
6
import java.util.ArrayList;
7
import java.util.ArrayList;
7
import java.util.Arrays;
8
import java.util.Arrays;
8
import java.util.Collections;
9
import java.util.Collections;
9
import java.util.Comparator;
10
import java.util.Comparator;
10
import java.util.HashMap;
11
import java.util.HashMap;
Line 569... Line 570...
569
			model.addAttribute("planningMonth", LocalDate.now().plusMonths(1).getMonth());
570
			model.addAttribute("planningMonth", LocalDate.now().plusMonths(1).getMonth());
570
		} else if (LocalDate.now().isAfter(LocalDate.now().withDayOfMonth(8))
571
		} else if (LocalDate.now().isAfter(LocalDate.now().withDayOfMonth(8))
571
				&& (LocalDate.now().isBefore(LocalDate.now().withDayOfMonth(26)))) {
572
				&& (LocalDate.now().isBefore(LocalDate.now().withDayOfMonth(26)))) {
572
			currentMonthDate = LocalDate.now().withDayOfMonth(1);
573
			currentMonthDate = LocalDate.now().withDayOfMonth(1);
573
			monthlyPlanned = monthlyPlannedRepository.selectByFofoIdYearMonth(fofoId, currentMonthDate);
574
			monthlyPlanned = monthlyPlannedRepository.selectByFofoIdYearMonth(fofoId, currentMonthDate);
-
 
575
			model.addAttribute("planningMonth", currentMonthDate.getMonth());
-
 
576
			model.addAttribute("freezed", true);
574
			LOGGER.info("monthlyPlanned1" + monthlyPlanned);
577
			LOGGER.info("monthlyPlanned1" + monthlyPlanned);
575
		} else if (LocalDate.now().isBefore(LocalDate.now().withDayOfMonth(8))) {
578
		} else if (LocalDate.now().isBefore(LocalDate.now().withDayOfMonth(8))) {
576
			currentMonthDate = LocalDate.now().withDayOfMonth(1);
579
			currentMonthDate = LocalDate.now().withDayOfMonth(1);
577
			monthlyPlanned = monthlyPlannedRepository.selectByFofoIdYearMonth(fofoId, currentMonthDate);
580
			monthlyPlanned = monthlyPlannedRepository.selectByFofoIdYearMonth(fofoId, currentMonthDate);
578
			LOGGER.info("monthlyPlanned3" + monthlyPlanned);
581
			LOGGER.info("monthlyPlanned3" + monthlyPlanned);
Line 595... Line 598...
595
 
598
 
596
		Map<Integer, Integer> last15daysMap = itemRepository
599
		Map<Integer, Integer> last15daysMap = itemRepository
597
				.selectItems(FofoOrderItem.class, "id", "itemId", equalsMap, notEqualsMap, equalsJoinMap,
600
				.selectItems(FofoOrderItem.class, "id", "itemId", equalsMap, notEqualsMap, equalsJoinMap,
598
						notEqualsJoinMap, "quantity")
601
						notEqualsJoinMap, "quantity")
599
				.stream().collect(Collectors.toMap(x -> x.getCatalogId(), x -> (int) x.getAggregateValue()));
602
				.stream().collect(Collectors.toMap(x -> x.getCatalogId(), x -> (int) x.getAggregateValue()));
600
		LOGGER.info("last15daysMap");
-
 
-
 
603
 
601
		LOGGER.info("last15daysMap" + last15daysMap);
604
		LOGGER.info("last15daysMap" + last15daysMap);
602
 
605
 
603
		Map<Integer, TagListing> taglistingMap = tagListings.stream()
606
		Map<Integer, TagListing> taglistingMap = tagListings.stream()
604
				.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
607
				.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
605
		List<Item> items = itemRepository.selectByIds(taglistingMap.keySet());
608
		List<Item> items = itemRepository.selectByIds(taglistingMap.keySet());
Line 694... Line 697...
694
				Comparator.reverseOrder());
697
				Comparator.reverseOrder());
695
		LOGGER.info("monthlyPlanned" + monthlyPlanned);
698
		LOGGER.info("monthlyPlanned" + monthlyPlanned);
696
		model.addAttribute("catalogTagListings",
699
		model.addAttribute("catalogTagListings",
697
				catalogModelList.stream().sorted(firstCmp).collect(Collectors.toList()));
700
				catalogModelList.stream().sorted(firstCmp).collect(Collectors.toList()));
698
		model.addAttribute("isAdmin", roleManager.isAdmin(roleIds));
701
		model.addAttribute("isAdmin", roleManager.isAdmin(roleIds));
699
		model.addAttribute("brandStockPrices", this.getBrandStockPrices(loginDetails.getFofoId()));
702
		model.addAttribute("brandStockPrices",this.getBrandStockPrices(loginDetails.getFofoId()));
-
 
703
		model.addAttribute("previousPlanningMonth", LocalDate.now().minusMonths(1).getMonth());
-
 
704
		model.addAttribute("currentPlanningMonth", LocalDate.now().getMonth());
-
 
705
 
-
 
706
		model.addAttribute("customRetailers", customRetailers);
-
 
707
		model.addAttribute("totalAmount", totalAmount);
-
 
708
		model.addAttribute("monthlyPlanned", monthlyPlanned);
-
 
709
		model.addAttribute("totalPcs", totalPcs);
-
 
710
		return "open-indent";
-
 
711
 
-
 
712
	}
-
 
713
 
-
 
714
	@RequestMapping(value = "/indent/loadIndentPreviousMonth")
-
 
715
	public String previousMonthLoadOpenIndent(HttpServletRequest request, Model model,
-
 
716
			@RequestParam(required = false, defaultValue = "0") int fofoId,
-
 
717
			@RequestParam(defaultValue = "TEN_LAC") CounterSize counterSize) throws Exception {
-
 
718
 
-
 
719
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
-
 
720
		Set<Integer> roleIds = loginDetails.getRoleIds();
-
 
721
		LOGGER.info("Counter size is {}", counterSize);
-
 
722
		LOGGER.info("Fofo Id is {}", fofoId);
-
 
723
		boolean isAdmin = roleManager.isAdmin(roleIds);
-
 
724
 
-
 
725
		List<String> brands = mongoClient.getMongoBrands(fofoId, null, 3).stream().map(x -> (String) x.get("name"))
-
 
726
				.collect(Collectors.toList());
-
 
727
 
-
 
728
		Map<String, Object> equalsMap = new HashMap<>();
-
 
729
		equalsMap.put("categoryId", 10006);
-
 
730
		equalsMap.put("brand", brands);
-
 
731
		Map<String, List<?>> notEqualsMap = new HashMap<>();
-
 
732
 
-
 
733
		Map<String, List<?>> notEqualsJoinMap = new HashMap<>();
-
 
734
		Map<Integer, Integer> currentStockMap;
-
 
735
 
-
 
736
		if (!isAdmin && fofoId == 0) {
-
 
737
			fofoId = cookiesProcessor.getCookiesObject(request).getFofoId();
-
 
738
			Map<String, Object> equalsStockJoinMap = new HashMap<>();
-
 
739
			equalsStockJoinMap.put("fofoId", fofoId);
-
 
740
			currentStockMap = itemRepository
-
 
741
					.selectItems(CurrentInventorySnapshot.class, "id", "itemId", equalsMap, notEqualsMap,
-
 
742
							equalsStockJoinMap, notEqualsJoinMap, "availability")
-
 
743
					.stream().collect(Collectors.toMap(x -> x.getCatalogId(), x -> (int) x.getAggregateValue()));
-
 
744
			LOGGER.info("currentStock");
-
 
745
		} else {
-
 
746
			if (fofoId == 0) {
-
 
747
 
-
 
748
				Map<String, Object> equalsStockJoinMap = new HashMap<>();
-
 
749
				equalsStockJoinMap.put("fofoId", fofoId);
-
 
750
 
-
 
751
				currentStockMap = itemRepository
-
 
752
						.selectItems(CurrentInventorySnapshot.class, "id", "itemId", equalsMap, notEqualsMap,
-
 
753
								equalsStockJoinMap, notEqualsJoinMap, "availability")
-
 
754
						.stream().collect(Collectors.toMap(x -> x.getCatalogId(), x -> (int) x.getAggregateValue()));
-
 
755
 
-
 
756
			} else {
-
 
757
 
-
 
758
				Map<String, Object> equalsStockJoinMap = new HashMap<>();
-
 
759
				equalsStockJoinMap.put("fofoId", fofoId);
-
 
760
 
-
 
761
				currentStockMap = itemRepository
-
 
762
						.selectItems(CurrentInventorySnapshot.class, "id", "itemId", equalsMap, notEqualsMap,
-
 
763
								equalsStockJoinMap, notEqualsJoinMap, "availability")
-
 
764
						.stream().collect(Collectors.toMap(x -> x.getCatalogId(), x -> (int) x.getAggregateValue()));
-
 
765
 
-
 
766
			}
-
 
767
		}
-
 
768
 
-
 
769
		LOGGER.info("currentStock" + currentStockMap);
-
 
770
 
-
 
771
		Map<Integer, Integer> itemsInTransit = null;
-
 
772
		List<TagListing> tagListings = tagListingRepository.selectAll(true);
-
 
773
		if (!isAdmin) {
-
 
774
			tagListings = new ArrayList<>(tagListings);
-
 
775
			List<Order> inTransitOrders = orderRepository.selectPendingGrnOrder(fofoId, validOrderStatusList);
-
 
776
			inTransitOrders = this.filterValidOrders(inTransitOrders);
-
 
777
			itemsInTransit = inTransitOrders.stream().collect(Collectors.groupingBy(x -> x.getLineItem().getItemId(),
-
 
778
					Collectors.summingInt(x -> x.getLineItem().getQuantity())));
-
 
779
		} else {
-
 
780
			itemsInTransit = new HashMap<>();
-
 
781
		}
-
 
782
 
-
 
783
		int totalPcs = 0;
-
 
784
 
-
 
785
		float totalAmount = 0;
-
 
786
		Map<Integer, CatalogListingModel> catalogListingMap = new HashMap<>();
-
 
787
 
-
 
788
		Map<Integer, FocusedModel> focusedModelMap = focusedModelRepository.selectAll().stream()
-
 
789
				.collect(Collectors.toMap(FocusedModel::getCatalogId, Function.identity()));
-
 
790
 
-
 
791
		LocalDate currentMonthDate = LocalDate.now().minusMonths(1).withDayOfMonth(1);
-
 
792
		MonthlyPlanned monthlyPlanned = null;
-
 
793
		monthlyPlanned = monthlyPlannedRepository.selectByFofoIdYearMonth(fofoId, currentMonthDate);
-
 
794
		LOGGER.info("monthlyPlanned2" + monthlyPlanned);
-
 
795
		model.addAttribute("planningMonth", LocalDate.now().minusMonths(1).getMonth());
-
 
796
		model.addAttribute("previousPlanningMonth", LocalDate.now().minusMonths(1).getMonth());
-
 
797
		model.addAttribute("currentPlanningMonth", LocalDate.now().getMonth());
-
 
798
 
-
 
799
		Map<Integer, Integer> plannedDetailMap = null;
-
 
800
		if (monthlyPlanned != null) {
-
 
801
			plannedDetailMap = plannedDetailRepository.selectByPlannedId(monthlyPlanned.getId()).stream()
-
 
802
					.collect(Collectors.toMap(x -> x.getCatalogId(), x -> x.getQuantity()));
-
 
803
 
-
 
804
		}
-
 
805
 
-
 
806
		LOGGER.info("plannedDetailMap" + plannedDetailMap);
-
 
807
		LocalDateTime curDate = LocalDate.now().atStartOfDay();
-
 
808
		List<Integer> fofoOrderIds = fofoOrderRepository
-
 
809
				.selectByFofoIdBetweenCreatedTimeStamp(loginDetails.getFofoId(),
-
 
810
						curDate.withDayOfMonth(1).minusMonths(1), curDate.with(LocalTime.MAX).minusMonths(1))
-
 
811
				.stream().map(x -> x.getId()).collect(Collectors.toList());
-
 
812
		LOGGER.info("fofoOrderIds" + fofoOrderIds);
-
 
813
		Map<String, Object> equalsJoinMap = new HashMap<>();
-
 
814
		equalsJoinMap.put("orderId", fofoOrderIds);
-
 
815
 
-
 
816
		Map<Integer, Integer> lastMonthSaleMap = itemRepository
-
 
817
				.selectItems(FofoOrderItem.class, "id", "itemId", equalsMap, notEqualsMap, equalsJoinMap,
-
 
818
						notEqualsJoinMap, "quantity")
-
 
819
				.stream().collect(Collectors.toMap(x -> x.getCatalogId(), x -> (int) x.getAggregateValue()));
-
 
820
 
-
 
821
		LOGGER.info("lastMonthSaleMap" + lastMonthSaleMap);
-
 
822
 
-
 
823
		Map<Integer, TagListing> taglistingMap = tagListings.stream()
-
 
824
				.collect(Collectors.toMap(x -> x.getItemId(), x -> x));
-
 
825
		List<Item> items = itemRepository.selectByIds(taglistingMap.keySet());
-
 
826
		Map<Integer, List<Item>> catalogIdItemMap = items.stream().collect(
-
 
827
				Collectors.groupingBy(x -> x.getCatalogItemId(), Collectors.mapping(y -> y, Collectors.toList())));
-
 
828
		LOGGER.info("catalogIdItemMap");
-
 
829
 
-
 
830
		Map<String, Object> equalsItemJoinMap = new HashMap<>();
-
 
831
		equalsItemJoinMap.put("active", 1);
-
 
832
		List<CatalogIdAggregateValue> tagListingCatalogIds = itemRepository.selectItems(TagListing.class, "id",
-
 
833
				"itemId", equalsMap, notEqualsMap, equalsItemJoinMap, notEqualsJoinMap, "tagId");
-
 
834
		LOGGER.info("tagListingCatalogIds");
-
 
835
		for (CatalogIdAggregateValue catalogIdAggregateValue : tagListingCatalogIds) {
-
 
836
			int catalogId = catalogIdAggregateValue.getCatalogId();
-
 
837
			Item item = catalogIdItemMap.get(catalogId).get(0);
-
 
838
			TagListing tagListing = taglistingMap.get(item.getId());
-
 
839
			CatalogListingModel catalogListingModel = catalogListingMap.get(catalogId);
-
 
840
			if (!catalogListingMap.containsKey(catalogId)) {
-
 
841
				catalogListingModel = new CatalogListingModel();
-
 
842
				catalogListingModel.setCatalogId(catalogId);
-
 
843
				catalogListingModel.setDp(tagListing.getSellingPrice());
-
 
844
				catalogListingModel.setMop(tagListing.getMop());
-
 
845
				catalogListingModel.setItemDescription(item.getItemDescriptionNoColor());
-
 
846
				if (plannedDetailMap != null) {
-
 
847
 
-
 
848
					Integer quantity = plannedDetailMap.get(catalogId);
-
 
849
					if (quantity != null) {
-
 
850
						catalogListingModel.setAllocatedQuantity(quantity);
-
 
851
					}
-
 
852
				}
-
 
853
 
-
 
854
				if (lastMonthSaleMap != null) {
-
 
855
					Integer lastMonthSale = lastMonthSaleMap.get(catalogId);
-
 
856
 
-
 
857
					if (lastMonthSale != null) {
-
 
858
						catalogListingModel.setLastMonthSaleMap(lastMonthSale);
-
 
859
					} else {
-
 
860
						catalogListingModel.setLastMonthSaleMap(0);
-
 
861
					}
-
 
862
				} else {
-
 
863
					catalogListingModel.setLast15DaysSale(0);
-
 
864
				}
-
 
865
 
-
 
866
				catalogListingModel.setBrand(item.getBrand());
-
 
867
				if (item.getCategoryId() == 10006) {
-
 
868
					catalogListingModel.setCategoryId(item.getCategoryId());
-
 
869
				}
-
 
870
 
-
 
871
				FocusedModel fm = focusedModelMap.get(catalogId);
-
 
872
				if (fm != null) {
-
 
873
					catalogListingModel.setRecommendedQty(fm.getRecommendedQty());
-
 
874
					catalogListingModel.setMinimumQty(fm.getMinimumQty());
-
 
875
 
-
 
876
				} else {
-
 
877
					catalogListingModel.setRecommendedQty(0);
-
 
878
					catalogListingModel.setMinimumQty(0);
-
 
879
				}
-
 
880
 
-
 
881
				catalogListingMap.put(catalogId, catalogListingModel);
-
 
882
 
-
 
883
			}
-
 
884
			int itemAvailability = 0;
-
 
885
			if (currentStockMap != null) {
-
 
886
				Integer qty = currentStockMap.get(catalogId);
-
 
887
				itemAvailability = qty == null ? 0 : qty;
-
 
888
				catalogListingModel.setStockInHand(itemAvailability);
-
 
889
			} else {
-
 
890
				catalogListingModel.setStockInHand(0);
-
 
891
			}
-
 
892
			Integer inTransitQuantity = itemsInTransit.get(item.getId());
-
 
893
			int inTransitQty = (inTransitQuantity == null ? 0 : inTransitQuantity);
-
 
894
			catalogListingModel.setInTransitQuantity(catalogListingModel.getInTransitQuantity() + inTransitQty);
-
 
895
 
-
 
896
		}
-
 
897
 
-
 
898
		Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers();
-
 
899
		if (fofoId > 0) {
-
 
900
			CustomRetailer customRetailer = customRetailersMap.get(fofoId);
-
 
901
			model.addAttribute("retailerName",
-
 
902
					customRetailer.getBusinessName() + "-" + customRetailer.getAddress().getCity());
-
 
903
			FofoStore fs = fofoStoreRepository.selectByRetailerId(fofoId);
-
 
904
			model.addAttribute("retailerId", customRetailer.getPartnerId());
-
 
905
			model.addAttribute("counterSize", fs.getCounterSize().toString());
-
 
906
		} else {
-
 
907
			model.addAttribute("counterSize", counterSize.toString());
-
 
908
		}
-
 
909
		String customRetailers = JSONObject.valueToString(customRetailersMap.values());
-
 
910
 
-
 
911
		List<CatalogListingModel> catalogModelList = new ArrayList<>(catalogListingMap.values());
-
 
912
 
-
 
913
		Comparator<CatalogListingModel> firstCmp = Comparator.comparing(CatalogListingModel::getMinimumQty,
-
 
914
				Comparator.reverseOrder());
-
 
915
		LOGGER.info("monthlyPlanned" + monthlyPlanned);
-
 
916
		model.addAttribute("catalogTagListings",
-
 
917
				catalogModelList.stream().sorted(firstCmp).collect(Collectors.toList()));
-
 
918
		model.addAttribute("isAdmin", roleManager.isAdmin(roleIds));
-
 
919
		model.addAttribute("previousMonth", true);
-
 
920
		model.addAttribute("freezed", true);
-
 
921
		model.addAttribute("brandStockPrices",this.getBrandStockPrices(loginDetails.getFofoId()));
700
		model.addAttribute("customRetailers", customRetailers);
922
		model.addAttribute("customRetailers", customRetailers);
701
		model.addAttribute("totalAmount", totalAmount);
923
		model.addAttribute("totalAmount", totalAmount);
702
		model.addAttribute("monthlyPlanned", monthlyPlanned);
924
		model.addAttribute("monthlyPlanned", monthlyPlanned);
703
		model.addAttribute("totalPcs", totalPcs);
925
		model.addAttribute("totalPcs", totalPcs);
704
		return "open-indent";
926
		return "open-indent";