Subversion Repositories SmartDukaan

Rev

Rev 36305 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 36305 Rev 36372
Line 85... Line 85...
85
    @Override
85
    @Override
86
    public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
86
    public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
87
                                  String description, float amount, LocalDateTime businessTime) throws ProfitMandiBusinessException {
87
                                  String description, float amount, LocalDateTime businessTime) throws ProfitMandiBusinessException {
88
        if (Math.round(amount) == 0)
88
        if (Math.round(amount) == 0)
89
            return;
89
            return;
90
        UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
90
        UserWallet userWallet = userWalletRepository.selectByRetailerIdForUpdate(retailerId);
91
        userWallet.setAmount(userWallet.getAmount() + Math.round(amount));
91
        userWallet.setAmount(userWallet.getAmount() + Math.round(amount));
92
        this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
92
        this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
93
        if (WalletReferenceType.AUTOMATED_ADVANCE.equals(referenceType)
93
        if (WalletReferenceType.AUTOMATED_ADVANCE.equals(referenceType)
94
                || WalletReferenceType.ADVANCE_AMOUNT.equals(referenceType)
94
                || WalletReferenceType.ADVANCE_AMOUNT.equals(referenceType)
95
                || WalletReferenceType.PAYMENT_GATEWAY.equals(referenceType)
95
                || WalletReferenceType.PAYMENT_GATEWAY.equals(referenceType)
Line 107... Line 107...
107
        } else if (WalletReferenceType.RECHARGE.equals(referenceType) && !isActive(retailerId)) {
107
        } else if (WalletReferenceType.RECHARGE.equals(referenceType) && !isActive(retailerId)) {
108
            throw inactivepbse;
108
            throw inactivepbse;
109
        }
109
        }
110
        if (amount == 0)
110
        if (amount == 0)
111
            return;
111
            return;
112
        UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
112
        UserWallet userWallet = userWalletRepository.selectByRetailerIdForUpdate(retailerId);
113
        int walletAmount = userWallet.getAmount();
113
        int walletAmount = userWallet.getAmount();
114
        if (!WalletReferenceType.DAMAGE_PROTECTION.equals(referenceType) && amount > ProfitMandiConstants.MAX_NEGATIVE_WALLET_VALUE && Math.floor(amount) > walletAmount) {
114
        if (!WalletReferenceType.DAMAGE_PROTECTION.equals(referenceType) && amount > ProfitMandiConstants.MAX_NEGATIVE_WALLET_VALUE && Math.floor(amount) > walletAmount) {
115
            LOGGER.error("Wallet Balance is insufficient!, needed - {}, wallet has - {}, for retailer - {}", Math.floor(amount), walletAmount, retailerId);
115
            LOGGER.error("Wallet Balance is insufficient!, needed - {}, wallet has - {}, for retailer - {}", Math.floor(amount), walletAmount, retailerId);
116
            User user = userRepository.selectById(retailerId);
116
            User user = userRepository.selectById(retailerId);
117
            throw new ProfitMandiBusinessException(user.getName(), walletAmount, "WLT_1000");
117
            throw new ProfitMandiBusinessException(user.getName(), walletAmount, "WLT_1000");
Line 128... Line 128...
128
        } else if (WalletReferenceType.RECHARGE.equals(referenceType) && !isActive(retailerId)) {
128
        } else if (WalletReferenceType.RECHARGE.equals(referenceType) && !isActive(retailerId)) {
129
            throw inactivepbse;
129
            throw inactivepbse;
130
        }
130
        }
131
        if (amount == 0)
131
        if (amount == 0)
132
            return;
132
            return;
133
        UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
133
        UserWallet userWallet = userWalletRepository.selectByRetailerIdForUpdate(retailerId);
134
        int walletAmount = userWallet.getAmount();
134
        int walletAmount = userWallet.getAmount();
135
        if (!forced && amount > 2 && amount > walletAmount) {
135
        if (!forced && amount > 2 && amount > walletAmount) {
136
            LOGGER.error("Wallet Balance is insufficient!");
136
            LOGGER.error("Wallet Balance is insufficient!");
137
            throw new ProfitMandiBusinessException("balance", walletAmount, "WLT_1000");
137
            throw new ProfitMandiBusinessException("balance", walletAmount, "WLT_1000");
138
        }
138
        }
Line 144... Line 144...
144
    public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
144
    public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
145
                                         WalletReferenceType walletReferenceType, String rollbackReason, LocalDateTime businessTime) throws ProfitMandiBusinessException {
145
                                         WalletReferenceType walletReferenceType, String rollbackReason, LocalDateTime businessTime) throws ProfitMandiBusinessException {
146
 
146
 
147
        if (amountToRollback == 0)
147
        if (amountToRollback == 0)
148
            return;
148
            return;
149
        UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
149
        UserWallet userWallet = userWalletRepository.selectByRetailerIdForUpdate(retailerId);
150
        int walletAmount = userWallet.getAmount();
150
        int walletAmount = userWallet.getAmount();
151
        List<UserWalletHistory> uwh = userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(rollbackReference, walletReferenceType)
151
        List<UserWalletHistory> uwh = userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(rollbackReference, walletReferenceType)
152
                .stream().filter(x -> x.getWalletId() == userWallet.getId()).collect(Collectors.toList());
152
                .stream().filter(x -> x.getWalletId() == userWallet.getId()).collect(Collectors.toList());
153
        if (uwh.size() == 0) {
153
        if (uwh.size() == 0) {
154
            LOGGER.info("Retailer with id {} dont have valid reference {} and reference type {}",
154
            LOGGER.info("Retailer with id {} dont have valid reference {} and reference type {}",
155
                    retailerId, rollbackReference, walletReferenceType);
155
                    retailerId, rollbackReference, walletReferenceType);
156
            throw new ProfitMandiBusinessException("Retailer specific wallet entries doesn't exist", retailerId, "Nothing to rollback");
156
            throw new ProfitMandiBusinessException("Retailer specific wallet entries doesn't exist", retailerId, "Nothing to rollback");
157
        }
157
        }
158
        // userWallet = userWalletRepository.selectByIdForUpdate(userWallet.getId());
-
 
159
        userWallet.setAmount(walletAmount - Math.round(amountToRollback));
158
        userWallet.setAmount(walletAmount - Math.round(amountToRollback));
160
        this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
159
        this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
161
                walletReferenceType, rollbackReason, businessTime);
160
                walletReferenceType, rollbackReason, businessTime);
162
        userWalletRepository.persist(userWallet);
161
        userWalletRepository.persist(userWallet);
163
 
162