| Line 101... |
Line 101... |
| 101 |
@Autowired
|
101 |
@Autowired
|
| 102 |
private OrderRepository orderRepository;
|
102 |
private OrderRepository orderRepository;
|
| 103 |
|
103 |
|
| 104 |
@Autowired
|
104 |
@Autowired
|
| 105 |
private FofoOrderRepository fofoOrderRepository;
|
105 |
private FofoOrderRepository fofoOrderRepository;
|
| 106 |
|
106 |
|
| 107 |
@Autowired
|
107 |
@Autowired
|
| 108 |
private RoleRepository roleRepository;
|
108 |
private RoleRepository roleRepository;
|
| 109 |
|
109 |
|
| 110 |
@Override
|
110 |
@Override
|
| 111 |
public void saveScheme(int creatorId, CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
111 |
public void saveScheme(int creatorId, CreateSchemeRequest createSchemeRequest) throws ProfitMandiBusinessException {
|
| Line 357... |
Line 357... |
| 357 |
if (scheme.isRetailerAll()) {
|
357 |
if (scheme.isRetailerAll()) {
|
| 358 |
for (InventoryItem inventoryItem : completedInventoryItems) {
|
358 |
for (InventoryItem inventoryItem : completedInventoryItems) {
|
| 359 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
359 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
| 360 |
walletService.addAmountToWallet(inventoryItem.getFofoId(), inventoryItem.getPurchaseId(),
|
360 |
walletService.addAmountToWallet(inventoryItem.getFofoId(), inventoryItem.getPurchaseId(),
|
| 361 |
WalletReferenceType.SCHEME_IN,
|
361 |
WalletReferenceType.SCHEME_IN,
|
| - |
|
362 |
"Added SCHEME_IN for item " + itemStringMap.get(inventoryItem.getItemId())
|
| 362 |
"Added SCHEME_IN for item " + itemStringMap.get(inventoryItem.getItemId()) + " against invoiceNumber " + inventoryItem.getPurchase().getPurchaseReference(),
|
363 |
+ " against invoiceNumber " + inventoryItem.getPurchase().getPurchaseReference(),
|
| 363 |
cashback);
|
364 |
cashback);
|
| 364 |
}
|
365 |
}
|
| 365 |
} else {
|
366 |
} else {
|
| 366 |
List<Integer> retailerIds = retailerSchemeRepository.selectRetailerIdsBySchemeId(scheme.getId());
|
367 |
List<Integer> retailerIds = retailerSchemeRepository.selectRetailerIdsBySchemeId(scheme.getId());
|
| 367 |
for (InventoryItem inventoryItem : completedInventoryItems) {
|
368 |
for (InventoryItem inventoryItem : completedInventoryItems) {
|
| 368 |
if (retailerIds.contains(inventoryItem.getFofoId())) {
|
369 |
if (retailerIds.contains(inventoryItem.getFofoId())) {
|
| 369 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
370 |
float cashback = this.createSchemeInOut(scheme, inventoryItem);
|
| 370 |
walletService.addAmountToWallet(inventoryItem.getFofoId(), inventoryItem.getPurchaseId(),
|
371 |
walletService.addAmountToWallet(inventoryItem.getFofoId(), inventoryItem.getPurchaseId(),
|
| 371 |
WalletReferenceType.SCHEME_IN,
|
372 |
WalletReferenceType.SCHEME_IN,
|
| - |
|
373 |
"Added SCHEME_IN for item " + itemStringMap.get(inventoryItem.getItemId())
|
| 372 |
"Added SCHEME_IN for item " + itemStringMap.get(inventoryItem.getItemId()) + " against invoiceNumber " + inventoryItem.getPurchase().getPurchaseReference(), cashback);
|
374 |
+ " against invoiceNumber " + inventoryItem.getPurchase().getPurchaseReference(),
|
| - |
|
375 |
cashback);
|
| 373 |
}
|
376 |
}
|
| 374 |
}
|
377 |
}
|
| 375 |
}
|
378 |
}
|
| 376 |
|
379 |
|
| 377 |
}
|
380 |
}
|
| Line 657... |
Line 660... |
| 657 |
itemsCount++;
|
660 |
itemsCount++;
|
| 658 |
}
|
661 |
}
|
| 659 |
}
|
662 |
}
|
| 660 |
}
|
663 |
}
|
| 661 |
LOGGER.info("Items count for purchase id {} is {}", purchaseId, itemsCount);
|
664 |
LOGGER.info("Items count for purchase id {} is {}", purchaseId, itemsCount);
|
| 662 |
if(itemsCount > 0) {
|
665 |
if (itemsCount > 0) {
|
| 663 |
walletService.addAmountToWallet(retailerId, purchaseId, WalletReferenceType.SCHEME_IN,
|
666 |
walletService.addAmountToWallet(retailerId, purchaseId, WalletReferenceType.SCHEME_IN,
|
| 664 |
"Added for SCHEME IN against invoice " + purchase.getPurchaseReference() + " (total " + itemsCount + " pcs)", totalCashback);
|
667 |
"Added for SCHEME IN against invoice " + purchase.getPurchaseReference() + " (total "
|
| - |
|
668 |
+ itemsCount + " pcs)",
|
| - |
|
669 |
totalCashback);
|
| 665 |
LOGGER.info("Added Rs.{} for SCHEME IN against invoice {} total pcs({}) {}", totalCashback, purchase.getPurchaseReference(), itemsCount);
|
670 |
LOGGER.info("Added Rs.{} for SCHEME IN against invoice {} total pcs({}) {}", totalCashback,
|
| - |
|
671 |
purchase.getPurchaseReference(), itemsCount);
|
| 666 |
purchase.setCashback(purchase.getCashback() + totalCashback);
|
672 |
purchase.setCashback(purchase.getCashback() + totalCashback);
|
| 667 |
purchaseRepository.persist(purchase);
|
673 |
purchaseRepository.persist(purchase);
|
| 668 |
}
|
674 |
}
|
| 669 |
}
|
675 |
}
|
| 670 |
}
|
676 |
}
|
| Line 764... |
Line 770... |
| 764 |
}
|
770 |
}
|
| 765 |
|
771 |
|
| 766 |
for (Map.Entry<FofoOrderItem, Set<Scheme>> allFofoOrderItemSchemesEntry : allFofoOrderItemSchemesMap
|
772 |
for (Map.Entry<FofoOrderItem, Set<Scheme>> allFofoOrderItemSchemesEntry : allFofoOrderItemSchemesMap
|
| 767 |
.entrySet()) {
|
773 |
.entrySet()) {
|
| 768 |
for (FofoLineItem fofoLineItem : allFofoOrderItemSchemesEntry.getKey().getFofoLineItems()) {
|
774 |
for (FofoLineItem fofoLineItem : allFofoOrderItemSchemesEntry.getKey().getFofoLineItems()) {
|
| - |
|
775 |
float itemCashback = 0;
|
| 769 |
for (Scheme scheme : allFofoOrderItemSchemesEntry.getValue()) {
|
776 |
for (Scheme scheme : allFofoOrderItemSchemesEntry.getValue()) {
|
| 770 |
float cashback = this.createSchemeInOut(scheme,
|
777 |
itemCashback += this.createSchemeInOut(scheme,
|
| 771 |
inventoryItemIdInventoryItemMap.get(fofoLineItem.getInventoryItemId()));
|
778 |
inventoryItemIdInventoryItemMap.get(fofoLineItem.getInventoryItemId()));
|
| - |
|
779 |
}
|
| 772 |
if(cashback > 0) {
|
780 |
if (itemCashback > 0) {
|
| 773 |
totalCashback = totalCashback + cashback;
|
781 |
totalCashback += itemCashback;
|
| 774 |
count += 1;
|
782 |
count += 1;
|
| 775 |
}
|
- |
|
| 776 |
}
|
783 |
}
|
| 777 |
}
|
784 |
}
|
| 778 |
}
|
785 |
}
|
| 779 |
|
786 |
|
| 780 |
Set<Integer> schemeIds = this.fofoLineItemSchemesMapToSchemeIds(notAllFofoOrderItemSchemesMap);
|
787 |
Set<Integer> schemeIds = this.fofoLineItemSchemesMapToSchemeIds(notAllFofoOrderItemSchemesMap);
|
| Line 783... |
Line 790... |
| 783 |
List<Integer> foundSchemeIds = retailerSchemeRepository.selectSchemeIds(schemeIds, retailerId);
|
790 |
List<Integer> foundSchemeIds = retailerSchemeRepository.selectSchemeIds(schemeIds, retailerId);
|
| 784 |
|
791 |
|
| 785 |
for (Map.Entry<FofoOrderItem, Set<Scheme>> notAllFofoOrderItemSchemesEntry : notAllFofoOrderItemSchemesMap
|
792 |
for (Map.Entry<FofoOrderItem, Set<Scheme>> notAllFofoOrderItemSchemesEntry : notAllFofoOrderItemSchemesMap
|
| 786 |
.entrySet()) {
|
793 |
.entrySet()) {
|
| 787 |
for (FofoLineItem fofoLineItem : notAllFofoOrderItemSchemesEntry.getKey().getFofoLineItems()) {
|
794 |
for (FofoLineItem fofoLineItem : notAllFofoOrderItemSchemesEntry.getKey().getFofoLineItems()) {
|
| - |
|
795 |
float itemCashback = 0;
|
| 788 |
for (Scheme scheme : notAllFofoOrderItemSchemesEntry.getValue()) {
|
796 |
for (Scheme scheme : notAllFofoOrderItemSchemesEntry.getValue()) {
|
| 789 |
if (foundSchemeIds.contains(scheme.getId())) {
|
797 |
if (foundSchemeIds.contains(scheme.getId())) {
|
| 790 |
float cashback = this.createSchemeInOut(scheme,
|
798 |
itemCashback += this.createSchemeInOut(scheme,
|
| 791 |
inventoryItemIdInventoryItemMap.get(fofoLineItem.getInventoryItemId()));
|
799 |
inventoryItemIdInventoryItemMap.get(fofoLineItem.getInventoryItemId()));
|
| 792 |
totalCashback = totalCashback + cashback;
|
- |
|
| 793 |
if(cashback > 0) {
|
- |
|
| 794 |
totalCashback = totalCashback + cashback;
|
- |
|
| 795 |
count += 1;
|
- |
|
| 796 |
}
|
- |
|
| 797 |
}
|
800 |
}
|
| 798 |
}
|
801 |
}
|
| - |
|
802 |
if (itemCashback > 0) {
|
| - |
|
803 |
totalCashback += itemCashback;
|
| - |
|
804 |
count += 1;
|
| - |
|
805 |
}
|
| 799 |
}
|
806 |
}
|
| 800 |
}
|
807 |
}
|
| 801 |
}
|
808 |
}
|
| 802 |
if (totalCashback > 0) {
|
809 |
if (totalCashback > 0) {
|
| 803 |
walletService.addAmountToWallet(retailerId, fofoOrderId, WalletReferenceType.SCHEME_OUT,
|
810 |
walletService.addAmountToWallet(retailerId, fofoOrderId, WalletReferenceType.SCHEME_OUT,
|
| 804 |
"Sales margin for invoice number " + fofoOrder.getInvoiceNumber() + ". Total " + count + " pc(s)", totalCashback);
|
811 |
"Sales margin for invoice number " + fofoOrder.getInvoiceNumber() + ". Total " + count + " pc(s)",
|
| - |
|
812 |
totalCashback);
|
| 805 |
fofoOrder.setCashback(totalCashback);
|
813 |
fofoOrder.setCashback(totalCashback);
|
| 806 |
fofoOrderRepository.persist(fofoOrder);
|
814 |
fofoOrderRepository.persist(fofoOrder);
|
| 807 |
}
|
815 |
}
|
| 808 |
}
|
816 |
}
|
| 809 |
|
817 |
|
| 810 |
@Override
|
818 |
@Override
|
| 811 |
public void rollbackSchemes(List<Integer> inventoryItemIds, int rollbackReference, String rollbackReason) throws Exception{
|
819 |
public void rollbackSchemes(List<Integer> inventoryItemIds, int rollbackReference, String rollbackReason)
|
| - |
|
820 |
throws Exception {
|
| 812 |
Set<Integer> inventoryItemIdSet = new HashSet<>(inventoryItemIds);
|
821 |
Set<Integer> inventoryItemIdSet = new HashSet<>(inventoryItemIds);
|
| 813 |
float amountToRollback = 0;
|
822 |
float amountToRollback = 0;
|
| 814 |
List<SchemeInOut> schemes = schemeInOutRepository.selectByInventoryItemIds(inventoryItemIdSet);
|
823 |
List<SchemeInOut> schemes = schemeInOutRepository.selectByInventoryItemIds(inventoryItemIdSet);
|
| 815 |
for(SchemeInOut schemeInOut: schemes) {
|
824 |
for (SchemeInOut schemeInOut : schemes) {
|
| 816 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
825 |
schemeInOut.setRolledBackTimestamp(LocalDateTime.now());
|
| 817 |
schemeInOutRepository.persist(schemeInOut);
|
826 |
schemeInOutRepository.persist(schemeInOut);
|
| 818 |
amountToRollback += schemeInOut.getAmount();
|
827 |
amountToRollback += schemeInOut.getAmount();
|
| 819 |
}
|
828 |
}
|
| 820 |
if(schemes.size() > 0) {
|
829 |
if (schemes.size() > 0) {
|
| 821 |
int inventoryItemId = inventoryItemIds.get(0);
|
830 |
int inventoryItemId = inventoryItemIds.get(0);
|
| 822 |
Integer fofoId = inventoryItemRepository.selectById(inventoryItemId).getFofoId();
|
831 |
Integer fofoId = inventoryItemRepository.selectById(inventoryItemId).getFofoId();
|
| 823 |
walletService.rollbackAmountFromWallet(fofoId, amountToRollback, rollbackReference, WalletReferenceType.SCHEME_IN, rollbackReason);
|
832 |
walletService.rollbackAmountFromWallet(fofoId, amountToRollback, rollbackReference,
|
| - |
|
833 |
WalletReferenceType.SCHEME_IN, rollbackReason);
|
| 824 |
}
|
834 |
}
|
| 825 |
}
|
835 |
}
|
| 826 |
|
836 |
|
| 827 |
@Override
|
837 |
@Override
|
| 828 |
public Map<String, Object> getSchemes(Set<Integer> roleIds, int offset, int limit) throws ProfitMandiBusinessException{
|
838 |
public Map<String, Object> getSchemes(Set<Integer> roleIds, int offset, int limit)
|
| - |
|
839 |
throws ProfitMandiBusinessException {
|
| 829 |
Map<String, Object> map = new HashMap<>();
|
840 |
Map<String, Object> map = new HashMap<>();
|
| 830 |
List<Scheme> schemes = null;
|
841 |
List<Scheme> schemes = null;
|
| 831 |
long size = 0;
|
842 |
long size = 0;
|
| 832 |
Role role = roleRepository.selectByName(RoleType.FOFO_ADMIN.toString());
|
843 |
Role role = roleRepository.selectByName(RoleType.FOFO_ADMIN.toString());
|
| 833 |
if(roleIds.contains(role.getId())){
|
844 |
if (roleIds.contains(role.getId())) {
|
| 834 |
schemes = schemeRepository.selectAll(offset, limit);
|
845 |
schemes = schemeRepository.selectAll(offset, limit);
|
| 835 |
size = schemeRepository.selectAllCount();
|
846 |
size = schemeRepository.selectAllCount();
|
| 836 |
}else{
|
847 |
} else {
|
| 837 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
848 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
| 838 |
size = schemeRepository.selectAllActiveCount();
|
849 |
size = schemeRepository.selectAllActiveCount();
|
| 839 |
}
|
850 |
}
|
| 840 |
map.put("schemes", schemes);
|
851 |
map.put("schemes", schemes);
|
| 841 |
map.put("start", offset + 1);
|
852 |
map.put("start", offset + 1);
|
| 842 |
map.put("size", size);
|
853 |
map.put("size", size);
|
| 843 |
if (schemes.size() < limit){
|
854 |
if (schemes.size() < limit) {
|
| 844 |
map.put("end", offset + schemes.size());
|
855 |
map.put("end", offset + schemes.size());
|
| 845 |
}else{
|
856 |
} else {
|
| 846 |
map.put("end", offset + limit);
|
857 |
map.put("end", offset + limit);
|
| 847 |
}
|
858 |
}
|
| 848 |
return map;
|
859 |
return map;
|
| 849 |
}
|
860 |
}
|
| 850 |
|
861 |
|
| 851 |
@Override
|
862 |
@Override
|
| 852 |
public List<Scheme> getPaginatedSchemes(Set<Integer> roleIds, int offset, int limit) throws ProfitMandiBusinessException{
|
863 |
public List<Scheme> getPaginatedSchemes(Set<Integer> roleIds, int offset, int limit)
|
| - |
|
864 |
throws ProfitMandiBusinessException {
|
| 853 |
LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
|
865 |
LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
|
| 854 |
List<Scheme> schemes = null;
|
866 |
List<Scheme> schemes = null;
|
| 855 |
Role role = roleRepository.selectByName(RoleType.FOFO_ADMIN.toString());
|
867 |
Role role = roleRepository.selectByName(RoleType.FOFO_ADMIN.toString());
|
| 856 |
if(roleIds.contains(role.getId())){
|
868 |
if (roleIds.contains(role.getId())) {
|
| 857 |
schemes = schemeRepository.selectAll(offset, limit);
|
869 |
schemes = schemeRepository.selectAll(offset, limit);
|
| 858 |
}else{
|
870 |
} else {
|
| 859 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
871 |
schemes = schemeRepository.selectActiveAll(offset, limit);
|
| 860 |
}
|
872 |
}
|
| 861 |
return schemes;
|
873 |
return schemes;
|
| 862 |
}
|
874 |
}
|
| 863 |
|
875 |
|