Subversion Repositories SmartDukaan

Rev

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

Rev 30858 Rev 30859
Line 40... Line 40...
40
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
40
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;
41
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
41
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
42
import com.spice.profitmandi.dao.enumuration.dtr.RefferalStatus;
42
import com.spice.profitmandi.dao.enumuration.dtr.RefferalStatus;
43
import com.spice.profitmandi.dao.enumuration.fofo.Gateway;
43
import com.spice.profitmandi.dao.enumuration.fofo.Gateway;
44
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
44
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
-
 
45
import com.spice.profitmandi.dao.enumuration.transaction.LoanReferenceType;
45
import com.spice.profitmandi.dao.enumuration.transaction.PriceDropImeiStatus;
46
import com.spice.profitmandi.dao.enumuration.transaction.PriceDropImeiStatus;
46
import com.spice.profitmandi.dao.model.PartnerDetailModel;
47
import com.spice.profitmandi.dao.model.PartnerDetailModel;
47
import com.spice.profitmandi.dao.model.SimpleCampaign;
48
import com.spice.profitmandi.dao.model.SimpleCampaign;
48
import com.spice.profitmandi.dao.model.SimpleCampaignParams;
49
import com.spice.profitmandi.dao.model.SimpleCampaignParams;
49
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
50
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
Line 70... Line 71...
70
import com.spice.profitmandi.service.pricing.PriceDropService;
71
import com.spice.profitmandi.service.pricing.PriceDropService;
71
import com.spice.profitmandi.service.recharge.provider.OxigenRechargeProviderService;
72
import com.spice.profitmandi.service.recharge.provider.OxigenRechargeProviderService;
72
import com.spice.profitmandi.service.recharge.provider.ThinkWalnutDigitalRechargeProviderService;
73
import com.spice.profitmandi.service.recharge.provider.ThinkWalnutDigitalRechargeProviderService;
73
import com.spice.profitmandi.service.scheme.SchemeService;
74
import com.spice.profitmandi.service.scheme.SchemeService;
74
import com.spice.profitmandi.service.serviceConfig.ServiceConfigService;
75
import com.spice.profitmandi.service.serviceConfig.ServiceConfigService;
-
 
76
import com.spice.profitmandi.service.transaction.SDCreditService;
75
import com.spice.profitmandi.service.transaction.TransactionService;
77
import com.spice.profitmandi.service.transaction.TransactionService;
76
import com.spice.profitmandi.service.user.RetailerService;
78
import com.spice.profitmandi.service.user.RetailerService;
77
import com.spice.profitmandi.service.wallet.WalletService;
79
import com.spice.profitmandi.service.wallet.WalletService;
78
import in.shop2020.model.v1.order.OrderStatus;
80
import in.shop2020.model.v1.order.OrderStatus;
79
import in.shop2020.model.v1.order.WalletReferenceType;
81
import in.shop2020.model.v1.order.WalletReferenceType;
Line 103... Line 105...
103
 
105
 
104
import javax.mail.MessagingException;
106
import javax.mail.MessagingException;
105
import javax.mail.internet.InternetAddress;
107
import javax.mail.internet.InternetAddress;
106
import javax.mail.internet.MimeMessage;
108
import javax.mail.internet.MimeMessage;
107
import java.io.*;
109
import java.io.*;
-
 
110
import java.math.BigDecimal;
-
 
111
import java.math.RoundingMode;
108
import java.nio.file.Files;
112
import java.nio.file.Files;
109
import java.nio.file.Paths;
113
import java.nio.file.Paths;
110
import java.sql.Timestamp;
114
import java.sql.Timestamp;
111
import java.time.*;
115
import java.time.*;
112
import java.time.format.DateTimeFormatter;
116
import java.time.format.DateTimeFormatter;
Line 3331... Line 3335...
3331
 
3335
 
3332
					LOGGER.info("filteredRows {}", filteredRows);
3336
					LOGGER.info("filteredRows {}", filteredRows);
3333
 
3337
 
3334
				}
3338
				}
3335
			}
3339
			}
-
 
3340
 
-
 
3341
		}
-
 
3342
	}
-
 
3343
 
-
 
3344
	@Autowired
-
 
3345
	private LoanRepository loanRepository;
-
 
3346
 
-
 
3347
	@Autowired
-
 
3348
	private LoanStatementRepository loanStatementRepository;
-
 
3349
 
-
 
3350
	@Autowired
-
 
3351
	private SDCreditService sdCreditService;
-
 
3352
 
-
 
3353
	@Autowired
-
 
3354
	private SDCreditRequirementRepository sdCreditRequirementRepository;
-
 
3355
 
-
 
3356
	public void calculateInterestAccured() throws ProfitMandiBusinessException {
-
 
3357
 
-
 
3358
		List<Loan> loans = loanRepository.selectAllActiveLoan();
-
 
3359
 
-
 
3360
		if (!loans.isEmpty()) {
-
 
3361
 
-
 
3362
			for (Loan loan : loans) {
-
 
3363
 
-
 
3364
				List<LoanStatement> loanStatements = loanStatementRepository.selectByLoanId(loan.getId());
-
 
3365
 
-
 
3366
				Map<LoanReferenceType, Double> loanStatusAmount = loanStatements.stream().collect(Collectors.groupingBy(
-
 
3367
						x -> x.getLoanReferenceType(), Collectors.summingDouble(x -> x.getAmount().doubleValue())));
-
 
3368
 
-
 
3369
				if (loan.getFreeDays() > 0) {
-
 
3370
					LocalDateTime freeDaysLimitDate = loan.getCreatedOn().plusDays(loan.getFreeDays() - 1);
-
 
3371
					if (LocalDateTime.now().isAfter(freeDaysLimitDate)) {
-
 
3372
						sdCreditService.calculateInterest(freeDaysLimitDate.toLocalDate(), LocalDate.now(), loan,
-
 
3373
								loanStatusAmount);
-
 
3374
 
-
 
3375
					}
-
 
3376
				} else {
-
 
3377
 
-
 
3378
					int loanStatementId = loanStatementRepository.selectLatestLoanSatement(loan.getFofoId(),
-
 
3379
							loan.getId());
-
 
3380
					LOGGER.info("loanStatementId {}", loanStatementId);
-
 
3381
					LoanStatement loanStatement = loanStatementRepository.selectById(loanStatementId);
-
 
3382
					if (loanStatement.getLoanId() == loan.getId()) {
-
 
3383
 
-
 
3384
						sdCreditService.calculateInterest(loanStatement.getCreatedAt().toLocalDate(), LocalDate.now(),
-
 
3385
								loan, loanStatusAmount);
-
 
3386
 
-
 
3387
					} else {
-
 
3388
						throw new ProfitMandiBusinessException("loanstament", loanStatement.getLoanId(),
-
 
3389
								"Invalid LoanId");
-
 
3390
					}
-
 
3391
 
-
 
3392
				}
-
 
3393
 
-
 
3394
			}
-
 
3395
		}
-
 
3396
 
-
 
3397
	}
-
 
3398
 
-
 
3399
	public void loanSettlement() throws ProfitMandiBusinessException {
-
 
3400
		List<Loan> loans = loanRepository.selectAllActiveLoan();
-
 
3401
 
-
 
3402
		if (!loans.isEmpty()) {
-
 
3403
 
-
 
3404
			for (Loan loan : loans) {
-
 
3405
 
-
 
3406
				UserWallet userWallet = userWalletRepository.selectByRetailerId(loan.getFofoId());
-
 
3407
				SDCreditRequirement sdCreditRequirement = sdCreditRequirementRepository
-
 
3408
						.selectByFofoId(loan.getFofoId());
-
 
3409
 
-
 
3410
				if (userWallet.getAmount() >= 100) {
-
 
3411
					List<LoanStatement> loanStatements = loanStatementRepository.selectByLoanId(loan.getId());
-
 
3412
 
-
 
3413
					Map<LoanReferenceType, Double> loanStatusAmount = loanStatements.stream()
-
 
3414
							.collect(Collectors.groupingBy(x -> x.getLoanReferenceType(),
-
 
3415
									Collectors.summingDouble(x -> x.getAmount().doubleValue())));
-
 
3416
 
-
 
3417
					LOGGER.info("loanStatusAmount {}", loanStatusAmount);
-
 
3418
					double interestAmount = loanStatusAmount.get(LoanReferenceType.INTEREST);
-
 
3419
 
-
 
3420
					double principalAmount = loanStatusAmount.get(LoanReferenceType.PRINCIPAL);
-
 
3421
					double paidAmount = 0;
-
 
3422
					if (userWallet.getAmount() > Math.abs(interestAmount)) {
-
 
3423
 
-
 
3424
						if (interestAmount != 0) {
-
 
3425
 
-
 
3426
							settledLoanStatement(LoanReferenceType.INTEREST, BigDecimal.valueOf(interestAmount),
-
 
3427
									loan.getFofoId(), loan.getId(), "Amount adjusted against loan",
-
 
3428
									LocalDateTime.now());
-
 
3429
							loan.setInterestPaid(loan.getInterestPaid().add(BigDecimal.valueOf(interestAmount).abs()));
-
 
3430
 
-
 
3431
							paidAmount += Math.abs(interestAmount);
-
 
3432
						}
-
 
3433
 
-
 
3434
						double userWalletAmount = userWallet.getAmount() - Math.abs(interestAmount);
-
 
3435
 
-
 
3436
						if (userWalletAmount > Math.abs(principalAmount) && principalAmount != 0) {
-
 
3437
 
-
 
3438
							settledLoanStatement(LoanReferenceType.PRINCIPAL, BigDecimal.valueOf(principalAmount),
-
 
3439
									loan.getFofoId(), loan.getId(), "Amount adjusted against loan",
-
 
3440
									LocalDateTime.now());
-
 
3441
							double amount = userWalletAmount - Math.abs(principalAmount);
-
 
3442
 
-
 
3443
							paidAmount += Math.abs(principalAmount);
-
 
3444
 
-
 
3445
							userWallet.setAmount((int) amount);
-
 
3446
 
-
 
3447
							sdCreditRequirement.setAvailableLimit(sdCreditRequirement.getAvailableLimit()
-
 
3448
									.add(BigDecimal.valueOf(principalAmount).abs()));
-
 
3449
							sdCreditRequirement.setUtilizedAmount(sdCreditRequirement.getUtilizedAmount()
-
 
3450
									.subtract(BigDecimal.valueOf(principalAmount).abs()));
-
 
3451
 
-
 
3452
							loan.setPendingAmount(BigDecimal.ZERO);
-
 
3453
 
-
 
3454
						} else if (principalAmount != 0) {
-
 
3455
 
-
 
3456
							settledLoanStatement(LoanReferenceType.PRINCIPAL, BigDecimal.valueOf(userWalletAmount),
-
 
3457
									loan.getFofoId(), loan.getId(), "Amount adjusted against loan",
-
 
3458
									LocalDateTime.now());
-
 
3459
 
-
 
3460
							paidAmount += Math.abs(userWalletAmount);
-
 
3461
 
-
 
3462
							sdCreditRequirement.setAvailableLimit(sdCreditRequirement.getAvailableLimit()
-
 
3463
									.add(BigDecimal.valueOf(userWalletAmount)).abs());
-
 
3464
							sdCreditRequirement.setUtilizedAmount(sdCreditRequirement.getUtilizedAmount()
-
 
3465
									.subtract(BigDecimal.valueOf(userWalletAmount)).abs());
-
 
3466
							userWallet.setAmount(0);
-
 
3467
 
-
 
3468
							loan.setPendingAmount(
-
 
3469
									loan.getPendingAmount().subtract(BigDecimal.valueOf(userWalletAmount)));
-
 
3470
 
-
 
3471
						}
-
 
3472
 
-
 
3473
						createUserWalletHistory(userWallet.getId(), userWallet.getUserId(),
-
 
3474
								WalletReferenceType.LOAN_REPAYMENT, loan.getId(), Math.abs(paidAmount));
-
 
3475
 
-
 
3476
					} else {
-
 
3477
 
-
 
3478
						settledLoanStatement(LoanReferenceType.INTEREST, BigDecimal.valueOf(userWallet.getAmount()),
-
 
3479
								loan.getFofoId(), loan.getId(), "Amount adjusted against loan", LocalDateTime.now());
-
 
3480
 
-
 
3481
						paidAmount += Math.abs(userWallet.getAmount());
-
 
3482
 
-
 
3483
						createUserWalletHistory(userWallet.getId(), userWallet.getUserId(),
-
 
3484
								WalletReferenceType.LOAN_REPAYMENT, loan.getId(), Math.abs(paidAmount));
-
 
3485
 
-
 
3486
						loan.setInterestPaid(
-
 
3487
								loan.getInterestPaid().add(BigDecimal.valueOf(userWallet.getAmount()).abs()));
-
 
3488
 
-
 
3489
						userWallet.setAmount(0);
-
 
3490
					}
-
 
3491
 
-
 
3492
				}
-
 
3493
			}
3336
		}
3494
		}
-
 
3495
 
-
 
3496
	}
-
 
3497
 
-
 
3498
	private void settledLoanStatement(LoanReferenceType loanReferneceType, BigDecimal amount, int fofoId, int loanId,
-
 
3499
			String description, LocalDateTime now) {
-
 
3500
 
-
 
3501
		sdCreditService.createLoanStatement(loanReferneceType, amount.abs(), fofoId, loanId, description,
-
 
3502
				LocalDateTime.now());
-
 
3503
 
-
 
3504
	}
-
 
3505
 
-
 
3506
	private void createUserWalletHistory(int walletId, int userId, WalletReferenceType referenceType, int loanId,
-
 
3507
			double interestAmount) {
-
 
3508
		UserWalletHistory uwh = new UserWalletHistory();
-
 
3509
		uwh.setWalletId(walletId);
-
 
3510
		uwh.setFofoId(userId);
-
 
3511
		uwh.setBusinessTimestamp(LocalDateTime.now());
-
 
3512
		uwh.setReferenceType(referenceType);
-
 
3513
		uwh.setReference(loanId);
-
 
3514
		uwh.setTimestamp(LocalDateTime.now());
-
 
3515
		uwh.setAmount((int) -(interestAmount));
-
 
3516
		userWalletHistoryRepository.persist(uwh);
-
 
3517
 
-
 
3518
	}
-
 
3519
 
-
 
3520
	public void dailyLoanAlert() throws ProfitMandiBusinessException {
-
 
3521
		List<Loan> loans = loanRepository.selectAllActiveLoan();
-
 
3522
 
-
 
3523
		Map<Integer, Double> partnerLoanAmount = new HashMap<>();
-
 
3524
 
-
 
3525
		if (!loans.isEmpty()) {
-
 
3526
 
-
 
3527
			for (Loan loan : loans) {
-
 
3528
 
-
 
3529
				List<LoanStatement> loanStatements = loanStatementRepository.selectByLoanId(loan.getId());
-
 
3530
 
-
 
3531
				double amount = loanStatements.stream().map(x -> x.getAmount())
-
 
3532
						.collect(Collectors.summingDouble(x -> x.doubleValue()));
-
 
3533
				if (partnerLoanAmount.get(loan.getFofoId()) != null) {
-
 
3534
					amount += partnerLoanAmount.get(loan.getFofoId());
-
 
3535
					partnerLoanAmount.put(loan.getFofoId(), amount);
-
 
3536
				} else {
-
 
3537
					partnerLoanAmount.put(loan.getFofoId(), amount);
-
 
3538
				}
-
 
3539
 
-
 
3540
			}
-
 
3541
 
-
 
3542
		}
-
 
3543
 
-
 
3544
		if (!partnerLoanAmount.isEmpty()) {
-
 
3545
 
-
 
3546
			for (Entry<Integer, Double> partnerLoanAmountEnrty : partnerLoanAmount.entrySet()) {
-
 
3547
 
-
 
3548
				int fofoId = partnerLoanAmountEnrty.getKey();
-
 
3549
 
-
 
3550
				String title = "Alert! Credit o/s";
-
 
3551
				String url = "http://app.smartdukaan.com/pages/home/credit";
-
 
3552
				String message = "Your total pending Loan amount is Rs."
-
 
3553
						+ FormattingUtils.formatDecimal(Math.abs(partnerLoanAmountEnrty.getValue()));
-
 
3554
				notificationService.sendNotification(fofoId, title, MessageType.notification, title, message, url);
-
 
3555
 
-
 
3556
			}
-
 
3557
 
-
 
3558
		}
-
 
3559
 
3337
	}
3560
	}
3338
 
3561
 
3339
}
3562
}
3340
 
3563
 
3341
//2284
3564
//2284
3342
3565