Subversion Repositories SmartDukaan

Rev

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

Rev 25055 Rev 25069
Line 1... Line 1...
1
package com.spice.profitmandi.service.scheme;
1
package com.spice.profitmandi.service.scheme;
2
 
2
 
-
 
3
import java.time.LocalDate;
3
import java.time.LocalDateTime;
4
import java.time.LocalDateTime;
-
 
5
import java.time.LocalTime;
4
import java.util.ArrayList;
6
import java.util.ArrayList;
5
import java.util.HashMap;
7
import java.util.HashMap;
6
import java.util.HashSet;
8
import java.util.HashSet;
7
import java.util.List;
9
import java.util.List;
8
import java.util.Map;
10
import java.util.Map;
Line 470... Line 472...
470
		}
472
		}
471
		return completedInventoryItems;
473
		return completedInventoryItems;
472
	}
474
	}
473
 
475
 
474
	@Override
476
	@Override
475
	public void expireSchemeById(int schemeId) throws ProfitMandiBusinessException {
477
	public void expireSchemeById(int schemeId, LocalDateTime expiryTime) throws ProfitMandiBusinessException {
476
		Scheme scheme = schemeRepository.selectById(schemeId);
478
		Scheme scheme = schemeRepository.selectById(schemeId);
477
		if (scheme.getActiveTimestamp() == null) {
479
		if (scheme==null || scheme.getActiveTimestamp() == null) {
478
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
480
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ACTIVE_TIMESTAMP, scheme.getActiveTimestamp(),
479
					"SCHM_1007");
481
					"SCHM_1007");
480
		}
482
		}
481
		if (scheme.getExpireTimestamp() != null) {
483
		if (scheme.getExpireTimestamp() != null) {
482
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
484
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EXPIRE_TIMESTAMP, scheme.getExpireTimestamp(),
483
					"SCHM_1008");
485
					"SCHM_1008");
484
		}
486
		}
485
		scheme.setExpireTimestamp(LocalDateTime.now());
487
		scheme.setExpireTimestamp(LocalDateTime.now());
-
 
488
		scheme.setEndDateTime(expiryTime);
486
		schemeRepository.persist(scheme);
489
		schemeRepository.persist(scheme);
487
	}
490
	}
488
 
491
 
489
	private Map<Integer, Scheme> toSchemeIdSchemeMap(List<Scheme> schemes) {
492
	private Map<Integer, Scheme> toSchemeIdSchemeMap(List<Scheme> schemes) {
490
		Map<Integer, Scheme> schemeIdSchemeMap = new HashMap<>();
493
		Map<Integer, Scheme> schemeIdSchemeMap = new HashMap<>();
Line 550... Line 553...
550
	public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
553
	public void processSchemeIn(int purchaseId, int retailerId) throws ProfitMandiBusinessException {
551
		LOGGER.info("Trying to process SchemeIn with purchaseId [{}] and retailerId [{}]", purchaseId, retailerId);
554
		LOGGER.info("Trying to process SchemeIn with purchaseId [{}] and retailerId [{}]", purchaseId, retailerId);
552
		Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
555
		Purchase purchase = purchaseRepository.selectByIdAndFofoId(purchaseId, retailerId);
553
		LOGGER.info("purchase is completed = {}", (purchase.getCompleteTimestamp() != null));
556
		LOGGER.info("purchase is completed = {}", (purchase.getCompleteTimestamp() != null));
554
		if (purchase.getCompleteTimestamp() != null) {
557
		if (purchase.getCompleteTimestamp() != null) {
555
			/*
-
 
556
			 * List<Order> orders =
-
 
557
			 * orderRepository.selectByAirwayBillOrInvoiceNumber(purchase.
-
 
558
			 * getPurchaseReference(), retailerId);
-
 
559
			 */
-
 
560
			/*
-
 
561
			 * if (!orders.isEmpty()) { billingTimestamp =
-
 
562
			 * orders.get(0).getBillingTimestamp(); }
-
 
563
			 */
-
 
564
			List<Scheme> schemes = schemeRepository.selectActiveAll(SchemeType.IN, purchase.getCompleteTimestamp());
558
			List<Scheme> schemes = schemeRepository.selectActiveAll(SchemeType.IN, purchase.getCompleteTimestamp());
565
			float totalCashback = 0;
559
			float totalCashback = 0;
566
			if (schemes.isEmpty()) {
560
			if (schemes.isEmpty()) {
567
				return;
561
				return;
568
			}
562
			}
Line 572... Line 566...
572
					inventoryItems);
566
					inventoryItems);
573
 
567
 
574
			if (inventoryItemSchemesMap.isEmpty()) {
568
			if (inventoryItemSchemesMap.isEmpty()) {
575
				return;
569
				return;
576
			}
570
			}
577
			Map<InventoryItem, Set<Scheme>> notAllInventoryItemSchemesMap = new HashMap<>();
-
 
578
			Map<InventoryItem, Set<Scheme>> allInventoryItemSchemesMap = new HashMap<>();
571
			Map<InventoryItem, Set<Scheme>> allInventoryItemSchemesMap = new HashMap<>();
579
			Set<Integer> itemIds = new HashSet<>();
572
			Set<Integer> itemIds = new HashSet<>();
580
 
573
 
581
			for (Map.Entry<InventoryItem, Set<Scheme>> inventoryItemSchemesEntry : inventoryItemSchemesMap.entrySet()) {
574
			for (Map.Entry<InventoryItem, Set<Scheme>> inventoryItemSchemesEntry : inventoryItemSchemesMap.entrySet()) {
582
				Set<Scheme> notAllSchemes = new HashSet<>();
575
				Set<Scheme> notAllSchemes = new HashSet<>();
Line 586... Line 579...
586
						notAllSchemes.add(scheme);
579
						notAllSchemes.add(scheme);
587
					} else {
580
					} else {
588
						allSchemes.add(scheme);
581
						allSchemes.add(scheme);
589
					}
582
					}
590
				}
583
				}
591
				notAllInventoryItemSchemesMap.put(inventoryItemSchemesEntry.getKey(), notAllSchemes);
-
 
592
				allInventoryItemSchemesMap.put(inventoryItemSchemesEntry.getKey(), allSchemes);
584
				allInventoryItemSchemesMap.put(inventoryItemSchemesEntry.getKey(), allSchemes);
593
				itemIds.add(inventoryItemSchemesEntry.getKey().getItemId());
585
				itemIds.add(inventoryItemSchemesEntry.getKey().getItemId());
594
			}
586
			}
595
 
587
 
596
			int itemsCount = 0;
588
			int itemsCount = 0;
Line 606... Line 598...
606
					totalCashback += inventoryItemCashback;
598
					totalCashback += inventoryItemCashback;
607
					itemsCount++;
599
					itemsCount++;
608
				}
600
				}
609
			}
601
			}
610
 
602
 
611
			Set<Integer> schemeIds = this.inventoryItemSchemesMapToSchemeIds(notAllInventoryItemSchemesMap);
-
 
612
			if (!schemeIds.isEmpty()) {
-
 
613
				List<Integer> foundSchemeIds = retailerSchemeRepository.selectSchemeIds(schemeIds, retailerId);
-
 
614
				for (Map.Entry<InventoryItem, Set<Scheme>> notAllInventoryItemSchemesEntry : notAllInventoryItemSchemesMap
-
 
615
						.entrySet()) {
-
 
616
					float inventoryItemCashback = 0;
-
 
617
					for (Scheme scheme : notAllInventoryItemSchemesEntry.getValue()) {
-
 
618
						if (foundSchemeIds.contains(scheme.getId())) {
-
 
619
							InventoryItem inventoryItem = notAllInventoryItemSchemesEntry.getKey();
-
 
620
							float cashback = this.createSchemeInOut(scheme, inventoryItem);
-
 
621
							inventoryItemCashback += cashback;
-
 
622
						}
-
 
623
					}
-
 
624
					if (inventoryItemCashback > 0) {
-
 
625
						totalCashback += inventoryItemCashback;
-
 
626
						itemsCount++;
-
 
627
					}
-
 
628
				}
-
 
629
			}
-
 
630
			LOGGER.info("Items count for purchase id {} is {}", purchaseId, itemsCount);
603
			LOGGER.info("Items count for purchase id {} is {}", purchaseId, itemsCount);
631
			if (itemsCount > 0) {
604
			if (itemsCount > 0) {
632
				walletService.addAmountToWallet(retailerId, purchaseId, WalletReferenceType.SCHEME_IN,
605
				walletService.addAmountToWallet(retailerId, purchaseId, WalletReferenceType.SCHEME_IN,
633
						"Added for SCHEME IN against invoice " + purchase.getPurchaseReference() + " (total "
606
						"Added for SCHEME IN against invoice " + purchase.getPurchaseReference() + " (total "
634
								+ itemsCount + " pcs)",
607
								+ itemsCount + " pcs)",