Subversion Repositories SmartDukaan

Rev

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

Rev 30927 Rev 30957
Line 115... Line 115...
115
	private PurchaseRepository purchaseRepository;
115
	private PurchaseRepository purchaseRepository;
116
 
116
 
117
	@Autowired
117
	@Autowired
118
	private FofoOrderRepository fofoOrderRepository;
118
	private FofoOrderRepository fofoOrderRepository;
119
 
119
 
-
 
120
	private static final List<SchemeType> ACTIVATION_SCHEME_TYPES = Arrays.asList(SchemeType.ACTIVATION, SchemeType.SPECIAL_SUPPORT);
-
 
121
 
120
	@Override
122
	@Override
121
	public void saveScheme(int creatorId, CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
123
	public void saveScheme(int creatorId, CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
122
 
124
 
123
		this.validateCreateSchemeRequest(createSchemeRequest);
125
		this.validateCreateSchemeRequest(createSchemeRequest);
124
 
126
 
Line 286... Line 288...
286
		 * SchemeType.OUT) { this.processPreviousSales(scheme); }
288
		 * SchemeType.OUT) { this.processPreviousSales(scheme); }
287
		 */
289
		 */
288
	}
290
	}
289
 
291
 
290
	private void sendSchemeNotification(Scheme scheme) throws ProfitMandiBusinessException {
292
	private void sendSchemeNotification(Scheme scheme) throws ProfitMandiBusinessException {
291
		if (Arrays.asList(SchemeType.SPECIAL_SUPPORT, SchemeType.ACTIVATION).contains(scheme.getType())) {
293
		if (ACTIVATION_SCHEME_TYPES.contains(scheme.getType())) {
292
 
294
 
293
			SendNotificationModel sendNotificationModel = new SendNotificationModel();
295
			SendNotificationModel sendNotificationModel = new SendNotificationModel();
294
			List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(Collections.singleton(scheme.getId()));
296
			List<SchemeItem> schemeItems = schemeItemRepository.selectBySchemeIds(Collections.singleton(scheme.getId()));
295
			Set<Integer> catalogIds = schemeItems.stream().map(x -> x.getCatalogId()).collect(Collectors.toSet());
297
			Set<Integer> catalogIds = schemeItems.stream().map(x -> x.getCatalogId()).collect(Collectors.toSet());
296
			List<String> itemDescriptions = itemRepository.selectAllByCatalogIds(catalogIds).stream().filter(Utils.distinctByKey(Item::getCatalogItemId))
298
			List<String> itemDescriptions = itemRepository.selectAllByCatalogIds(catalogIds).stream().filter(Utils.distinctByKey(Item::getCatalogItemId))
Line 499... Line 501...
499
						sio.setStatus(SchemePayoutStatus.REJECTED);
501
						sio.setStatus(SchemePayoutStatus.REJECTED);
500
						sio.setStatusDescription("Partner Category upgraded to " + scheme.getPartnerType() + ", new entry for margin added");
502
						sio.setStatusDescription("Partner Category upgraded to " + scheme.getPartnerType() + ", new entry for margin added");
501
					}
503
					}
502
					actualCredit = amountToCredit - amountCredited;
504
					actualCredit = amountToCredit - amountCredited;
503
				} else {
505
				} else {
-
 
506
					return 0;            //Rejected Scheme for types INVESTMENT and ACTIVATION are considered rolledback only if the item billing is cancelled.
504
					return 0;
507
 
505
				}
508
				}
506
			} else {
509
			} else {
507
				actualCredit = amountToCredit;
510
				actualCredit = amountToCredit;
508
			}
511
			}
509
			LOGGER.info("Actual Credit ==== {}", actualCredit);
512
			LOGGER.info("Actual Credit ==== {}", actualCredit);
Line 522... Line 525...
522
				schemeInOut.setStatus(SchemePayoutStatus.PENDING);
525
				schemeInOut.setStatus(SchemePayoutStatus.PENDING);
523
				schemeInOut.setStatusDescription("Subject to investment days maintained");
526
				schemeInOut.setStatusDescription("Subject to investment days maintained");
524
				return 0;
527
				return 0;
525
			} else {
528
			} else {
526
				schemeInOut.setStatus(SchemePayoutStatus.CREDITED);
529
				schemeInOut.setStatus(SchemePayoutStatus.CREDITED);
-
 
530
				schemeInOut.setCreditTimestamp(LocalDateTime.now());
527
				if (scheme.getType().equals(SchemeType.IN)) {
531
				if (scheme.getType().equals(SchemeType.IN)) {
528
					schemeInOut.setStatusDescription("Credited for GRN of IMEI#" + inventoryItem.getSerialNumber());
532
					schemeInOut.setStatusDescription("Credited for GRN of IMEI#" + inventoryItem.getSerialNumber());
529
				} else if (SchemeService.OUT_SCHEME_TYPES.contains(scheme.getType())) {
533
				} else if (SchemeService.OUT_SCHEME_TYPES.contains(scheme.getType())) {
530
					schemeInOut.setStatusDescription("Credited for sale of IMEI#" + inventoryItem.getSerialNumber());
534
					schemeInOut.setStatusDescription("Credited for sale of IMEI#" + inventoryItem.getSerialNumber());
531
				}
535
				}
532
				schemeInOut.setCreditTimestamp(LocalDateTime.now());
-
 
533
			}
536
			}
534
		}
537
		}
535
		return actualCredit;
538
		return actualCredit;
536
	}
539
	}
537
 
540
 
Line 539... Line 542...
539
	private float getAmount(InventoryItem inventoryItem, Scheme scheme) throws ProfitMandiBusinessException {
542
	private float getAmount(InventoryItem inventoryItem, Scheme scheme) throws ProfitMandiBusinessException {
540
		float amount = 0;
543
		float amount = 0;
541
		float dpForCalc = 0;
544
		float dpForCalc = 0;
542
		float taxableSellingPrice = 0;
545
		float taxableSellingPrice = 0;
543
 
546
 
544
		float totalTaxRate = stateGstRateRepository.getTotalTaxRate(inventoryItem.getItemId());
547
		//float totalTaxRate = stateGstRateRepository.getTotalTaxRate(inventoryItem.getItemId());
545
		if (scheme.getAmountType() == AmountType.PERCENTAGE) {
548
		if (scheme.getAmountType().equals(AmountType.PERCENTAGE)) {
546
			if (scheme.getType().equals(SchemeType.IN)) {
549
			if (scheme.getType().equals(SchemeType.IN)) {
547
				dpForCalc = inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount();
550
				dpForCalc = inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount();
548
			} else {
551
			} else {
549
				try {
552
				try {
550
					dpForCalc = Math.min(inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount(),
553
					dpForCalc = Math.min(inventoryItem.getUnitPrice() - inventoryItem.getPriceDropAmount(),
Line 552... Line 555...
552
				} catch (Exception e) {
555
				} catch (Exception e) {
553
					LOGGER.info("Could not find tag Listing entry in {}", inventoryItem.getItemId());
556
					LOGGER.info("Could not find tag Listing entry in {}", inventoryItem.getItemId());
554
					e.printStackTrace();
557
					e.printStackTrace();
555
				}
558
				}
556
			}
559
			}
-
 
560
			//TODO:Should be calculated on unit price
557
			taxableSellingPrice = dpForCalc / (1 + totalTaxRate / 100);
561
			//taxableSellingPrice = dpForCalc / (1 + totalTaxRate / 100);
558
			amount = taxableSellingPrice * scheme.getAmount() / 100;
562
			//amount = taxableSellingPrice * scheme.getAmount() / 100;
-
 
563
			amount = dpForCalc * scheme.getAmount() / 100;
559
			System.out.println(String.format("%d\t%s\t%d\t%d\t%s\t%s\t%s\t%s\t%f\t%f\t%f\t%f", inventoryItem.getId(),
564
			System.out.println(String.format("%d\t%s\t%d\t%d\t%s\t%s\t%s\t%s\t%f\t%f\t%f\t%f", inventoryItem.getId(),
560
					inventoryItem.getSerialNumber(), inventoryItem.getItemId(), scheme.getId(), scheme.getName(),
565
					inventoryItem.getSerialNumber(), inventoryItem.getItemId(), scheme.getId(), scheme.getName(),
561
					scheme.getType(), scheme.getAmountType(), scheme.getPartnerType(), dpForCalc, taxableSellingPrice,
566
					scheme.getType(), scheme.getAmountType(), scheme.getPartnerType(), dpForCalc, taxableSellingPrice,
562
					scheme.getAmount(), amount));
567
					scheme.getAmount(), amount));
563
		} else {
568
		} else if (scheme.getType().equals(SchemeType.IN)) {
564
			amount = scheme.getAmount();
569
			amount = scheme.getAmount();
565
			if (scheme.isBasePlusGstCalc()) {
-
 
566
				amount = this.getReversalAmount(scheme, inventoryItem);
-
 
567
			}
-
 
568
			System.out.println(String.format("%d\t%s\t%d\t%d\t%s\t%s\t%s\t%s\t%f\t%f\t%d\t%f", inventoryItem.getId(),
-
 
569
					inventoryItem.getSerialNumber(), inventoryItem.getItemId(), scheme.getId(), scheme.getName(),
-
 
570
					scheme.getType(), scheme.getAmountType(), scheme.getPartnerType(), dpForCalc, taxableSellingPrice,
-
 
571
					0, amount));
-
 
572
		}
570
		}
573
 
-
 
574
		return amount;
571
		return amount;
575
	}
572
	}
576
 
573
 
577
	//Specifically for SchemeReversal
-
 
578
	private float getReversalAmount(Scheme scheme, InventoryItem inventoryItem) {
-
 
579
		float amount = scheme.getAmount();
-
 
580
		if (scheme.isBasePlusGstCalc()) {
-
 
581
			amount = (float) (amount / 1.18);
-
 
582
		}
-
 
583
		List<SchemeInOut> sios = schemeInOutRepository.selectAll(inventoryItem.getId());
-
 
584
		sios = sios.stream().filter(x -> x.getRolledBackTimestamp() == null).collect(Collectors.toList());
-
 
585
		List<Integer> schemeIds = sios.stream().map(x -> x.getSchemeId()).collect(Collectors.toList());
-
 
586
		List<Scheme> schemes = schemeRepository.selectBySchemeIds(schemeIds);
-
 
587
		float totalPaidPercentage = schemes.stream().filter(x -> x.getAmountType().equals(AmountType.PERCENTAGE)).map(x -> x.getAmount()).collect(Collectors.summingDouble(x -> x)).floatValue();
-
 
588
		return totalPaidPercentage;
-
 
589
	}
-
 
590
 
-
 
591
	@Override
574
	@Override
592
	public float processSchemeOut(int fofoOrderId, int retailerId) throws ProfitMandiBusinessException {
575
	public float processSchemeOut(int fofoOrderId, int retailerId) throws ProfitMandiBusinessException {
593
		float totalCashback = 0;
576
		float totalCashback = 0;
594
		FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(retailerId, fofoOrderId);
577
		FofoOrder fofoOrder = fofoOrderRepository.selectByFofoIdAndOrderId(retailerId, fofoOrderId);
595
		// Process only if order is not cancelled
578
		// Process only if order is not cancelled
Line 624... Line 607...
624
			}
607
			}
625
 
608
 
626
			int count = 0;
609
			int count = 0;
627
 
610
 
628
			List<SchemeType> allOutSchemeTypes = new ArrayList<>();
611
			List<SchemeType> allOutSchemeTypes = new ArrayList<>();
629
			allOutSchemeTypes.addAll(Arrays.asList(SchemeType.ACTIVATION, SchemeType.INVESTMENT));
612
			allOutSchemeTypes.addAll(Arrays.asList(SchemeType.ACTIVATION, SchemeType.INVESTMENT, SchemeType.SPECIAL_SUPPORT));
630
			allOutSchemeTypes.addAll(OUT_SCHEME_TYPES);
613
			allOutSchemeTypes.addAll(OUT_SCHEME_TYPES);
631
			allOutSchemeTypes.add(SchemeType.SPECIAL_SUPPORT);
-
 
632
			List<Scheme> allActiveSchemes = schemeRepository.selectActiveAll(allOutSchemeTypes, partnerType,
614
			List<Scheme> allActiveSchemes = schemeRepository.selectActiveAll(allOutSchemeTypes, partnerType,
633
					fofoOrder.getCreateTimestamp(), false);
615
					fofoOrder.getCreateTimestamp(), false);
634
 
616
 
635
			for (InventoryItem inventoryItem : inventoryItems) {
617
			for (InventoryItem inventoryItem : inventoryItems) {
636
				float itemCashback = 0;
618
				float itemCashback = 0;
637
				Set<Integer> schemeIds = new HashSet<>(
619
				Set<Integer> schemeIds = new HashSet<>(
638
						schemeItemRepository.selectSchemeIdByCatalogId(inventoryItem.getItem().getCatalogItemId()));
620
						schemeItemRepository.selectSchemeIdByCatalogId(inventoryItem.getItem().getCatalogItemId()));
639
				List<Scheme> itemActiveSchemes = allActiveSchemes.stream().filter(x -> schemeIds.contains(x.getId()))
621
				List<Scheme> itemActiveSchemes = allActiveSchemes.stream().filter(x -> schemeIds.contains(x.getId()))
640
						.collect(Collectors.toList());
622
						.collect(Collectors.toList());
641
				List<Scheme> supportSchemes = itemActiveSchemes.stream().filter(x -> x.getType().equals(SchemeType.SPECIAL_SUPPORT)).collect(Collectors.toList());
623
				List<Scheme> supportSchemes = itemActiveSchemes.stream().filter(x -> Arrays.asList(SchemeType.SPECIAL_SUPPORT, SchemeType.ACTIVATION).contains(x.getType())).collect(Collectors.toList());
642
				itemActiveSchemes = itemActiveSchemes.stream().filter(x -> !x.getType().equals(SchemeType.SPECIAL_SUPPORT)).collect(Collectors.toList());
624
				itemActiveSchemes = itemActiveSchemes.stream().filter(x -> !(x.getType().equals(SchemeType.SPECIAL_SUPPORT))).collect(Collectors.toList());
643
				for (Scheme scheme : itemActiveSchemes) {
625
				for (Scheme scheme : itemActiveSchemes) {
644
					LOGGER.info("Scheme ==== {}", scheme);
626
					LOGGER.info("Scheme ==== {}", scheme);
645
					itemCashback += this.createSchemeInOut(scheme, inventoryItem);
627
					itemCashback += this.createSchemeInOut(scheme, inventoryItem);
646
				}
628
				}
647
				if (supportSchemes.size() > 0) {
629
				if (supportSchemes.size() > 0) {
Line 663... Line 645...
663
		}
645
		}
664
		return totalCashback;
646
		return totalCashback;
665
	}
647
	}
666
 
648
 
667
	@Override
649
	@Override
-
 
650
	//Tax rate has been passed to 0 to ensure no tax deduction
668
	public float getSpecialSupportAmount(float supportAmount, PartnerType partnerType, LocalDate onDate, int catalogId) throws ProfitMandiBusinessException {
651
	public float getSpecialSupportAmount(float supportAmount, PartnerType partnerType, LocalDate onDate,
-
 
652
										 int catalogId) throws ProfitMandiBusinessException {
669
		int itemId = itemRepository.selectAllByCatalogItemId(catalogId).stream().findAny().get().getId();
653
		//int itemId = itemRepository.selectAllByCatalogItemId(catalogId).stream().findAny().get().getId();
670
		float totalTaxRate = stateGstRateRepository.getTotalTaxRate(itemId);
654
		//float totalTaxRate = stateGstRateRepository.getTotalTaxRate(itemId);
671
		return this.getSpecialSupportAmount(supportAmount, partnerType, onDate, catalogId, totalTaxRate);
655
		return this.getSpecialSupportAmount(supportAmount, partnerType, onDate, catalogId, 0);
672
	}
656
	}
673
 
657
 
674
	@Override
658
	@Override
675
	public float getSpecialSupportAmount(float supportAmount, PartnerType partnerType, LocalDate onDate, int catalogId, float taxRate) throws ProfitMandiBusinessException {
659
	public float getSpecialSupportAmount(float supportAmount, PartnerType partnerType, LocalDate onDate,
-
 
660
										 int catalogId, float taxRate) throws ProfitMandiBusinessException {
676
		float totalMargin = this.selectPercentageScheme(partnerType, onDate, catalogId, false, 0, 0).stream().collect(Collectors.summingDouble(x -> x.getAmount())).floatValue();
661
		float totalMargin = this.selectPercentageScheme(partnerType, onDate, catalogId, false, 0, 0).stream().collect(Collectors.summingDouble(x -> x.getAmount())).floatValue();
677
		float amountToCredit = supportAmount * (1 - (totalMargin / (100 + taxRate)));
662
		float amountToCredit = supportAmount * (1 - (totalMargin / (100 + taxRate)));
678
		return amountToCredit;
663
		return amountToCredit;
679
	}
664
	}
680
 
665
 
-
 
666
	private void processSpecialSupport(FofoOrder fofoOrder, List<Scheme> supportSchemes, InventoryItem
681
	private void processSpecialSupport(FofoOrder fofoOrder, List<Scheme> supportSchemes, InventoryItem inventoryItem, PartnerType partnerType, LocalDateTime saleDate) throws ProfitMandiBusinessException {
667
			inventoryItem, PartnerType partnerType, LocalDateTime saleDate) throws ProfitMandiBusinessException {
682
		int catalogId = inventoryItem.getItem().getCatalogItemId();
668
		int catalogId = inventoryItem.getItem().getCatalogItemId();
683
		float totalMargin = this.selectPercentageScheme(partnerType, saleDate.toLocalDate(), catalogId, false, 0, 0).stream().collect(Collectors.summingDouble(x -> x.getAmount())).floatValue();
669
		float totalMargin = this.selectPercentageScheme(partnerType, saleDate.toLocalDate(), catalogId, false, 0, 0).stream().collect(Collectors.summingDouble(x -> x.getAmount())).floatValue();
684
		LOGGER.info("total percentage margin - {}", totalMargin);
670
		LOGGER.info("total percentage margin - {}", totalMargin);
685
		float totalTaxRate = stateGstRateRepository.getTotalTaxRate(inventoryItem.getItemId());
-
 
686
		for (Scheme scheme : supportSchemes) {
671
		for (Scheme scheme : supportSchemes) {
687
			/*if (scheme.isBasePlusGstCalc()) {
-
 
688
			}*/
-
 
689
			float amountToCredit = scheme.getAmount() * (1 - (totalMargin / (100 + totalTaxRate)));
672
			float amountToCredit = scheme.getAmount() * (1 - (totalMargin / 100));
690
			List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByScheme(scheme.getId(), inventoryItem.getId());
673
			List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByScheme(scheme.getId(), inventoryItem.getId());
691
			SchemeInOut schemeInOut = schemeInOuts.stream().filter(x -> x.getRolledBackTimestamp() == null).findFirst().orElse(null);
674
			SchemeInOut schemeInOut = schemeInOuts.stream().filter(x -> x.getRolledBackTimestamp() == null).findFirst().orElse(null);
692
			if (schemeInOut == null) {
675
			if (schemeInOut == null) {
693
				schemeInOut = new SchemeInOut();
676
				schemeInOut = new SchemeInOut();
694
				schemeInOut.setSchemeId(scheme.getId());
677
				schemeInOut.setSchemeId(scheme.getId());
Line 822... Line 805...
822
				if (scheme.getAmountType().equals(AmountType.FIXED)) {
805
				if (scheme.getAmountType().equals(AmountType.FIXED)) {
823
					continue;
806
					continue;
824
				}
807
				}
825
				if (scheme.getType().equals(SchemeType.IN) && schemeInOut.getRolledBackTimestamp() == null) {
808
				if (scheme.getType().equals(SchemeType.IN) && schemeInOut.getRolledBackTimestamp() == null) {
826
					float newAmount = getAmount(ii, scheme);
809
					float newAmount = getAmount(ii, scheme);
827
					if (schemeInOut.getAmount() - newAmount >= 0.01f) {
810
					if (Math.abs(schemeInOut.getAmount() - newAmount) >= 0.01f) {
828
						schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
811
						schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
829
 
812
 
830
						SchemeInOut sioNew = new SchemeInOut();
813
						SchemeInOut sioNew = new SchemeInOut();
831
						sioNew.setAmount(newAmount);
814
						sioNew.setAmount(newAmount);
832
						sioNew.setStatus(schemeInOut.getStatus());
815
						sioNew.setStatus(schemeInOut.getStatus());
833
						sioNew.setStatusDescription(schemeInOut.getStatusDescription());
816
						sioNew.setStatusDescription(schemeInOut.getStatusDescription());
834
						sioNew.setInventoryItemId(schemeInOut.getInventoryItemId());
817
						sioNew.setInventoryItemId(schemeInOut.getInventoryItemId());
835
						sioNew.setSchemeId(schemeInOut.getSchemeId());
818
						sioNew.setSchemeId(schemeInOut.getSchemeId());
-
 
819
						sioNew.setCreditTimestamp(LocalDateTime.now());
836
						schemeInOutRepository.persist(sioNew);
820
						schemeInOutRepository.persist(sioNew);
837
 
821
 
838
						schemeInOut.setStatus(SchemePayoutStatus.REJECTED);
822
						schemeInOut.setStatus(SchemePayoutStatus.REJECTED);
839
						schemeInOut.setStatusDescription("Change in margins due to price drop");
823
						schemeInOut.setStatusDescription("Change in margins due to price drop");
840
						// IF not credited then dont consider any credit/debit for that sio entry
824
						// IF not credited then dont consider any credit/debit for that sio entry