Subversion Repositories SmartDukaan

Rev

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

Rev 36372 Rev 36513
Line 87... Line 87...
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.selectByRetailerIdForUpdate(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, userWallet.getAmount());
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)
96
                || WalletReferenceType.ONLINE_ORDER_PAYMENT.equals(referenceType)
96
                || WalletReferenceType.ONLINE_ORDER_PAYMENT.equals(referenceType)
97
                || WalletReferenceType.PURCHASE.equals(referenceType)) {
97
                || WalletReferenceType.PURCHASE.equals(referenceType)) {
Line 115... Line 115...
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");
118
        }
118
        }
119
        userWallet.setAmount(walletAmount - Math.round(amount));
119
        userWallet.setAmount(walletAmount - Math.round(amount));
120
        this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
120
        this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime, userWallet.getAmount());
121
    }
121
    }
122
 
122
 
123
    @Override
123
    @Override
124
    public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
124
    public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
125
                                        String description, float amount, LocalDateTime businessTime, boolean forced) throws ProfitMandiBusinessException {
125
                                        String description, float amount, LocalDateTime businessTime, boolean forced) throws ProfitMandiBusinessException {
Line 135... Line 135...
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
        }
139
        userWallet.setAmount(walletAmount - Math.round(amount));
139
        userWallet.setAmount(walletAmount - Math.round(amount));
140
        this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime);
140
        this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime, userWallet.getAmount());
141
    }
141
    }
142
 
142
 
143
    @Override
143
    @Override
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 {
Line 155... Line 155...
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.setAmount(walletAmount - Math.round(amountToRollback));
158
        userWallet.setAmount(walletAmount - Math.round(amountToRollback));
159
        this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
159
        this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
160
                walletReferenceType, rollbackReason, businessTime);
160
                walletReferenceType, rollbackReason, businessTime, userWallet.getAmount());
161
        userWalletRepository.persist(userWallet);
161
        userWalletRepository.persist(userWallet);
162
 
162
 
163
    }
163
    }
164
 
164
 
165
    private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType,
165
    private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType,
166
                                         String description, LocalDateTime businessTimestamp) {
166
                                         String description, LocalDateTime businessTimestamp, long walletBalance) {
167
        if (amount == 0)
167
        if (amount == 0)
168
            return;
168
            return;
169
        UserWalletHistory userWalletHistory = new UserWalletHistory();
169
        UserWalletHistory userWalletHistory = new UserWalletHistory();
170
        userWalletHistory.setWalletId(walletId);
170
        userWalletHistory.setWalletId(walletId);
171
        userWalletHistory.setAmount(Math.round(amount));
171
        userWalletHistory.setAmount(Math.round(amount));
172
        userWalletHistory.setReference(referenceId);
172
        userWalletHistory.setReference(referenceId);
173
        userWalletHistory.setReferenceType(referenceType);
173
        userWalletHistory.setReferenceType(referenceType);
174
        userWalletHistory.setTimestamp(LocalDateTime.now());
174
        userWalletHistory.setTimestamp(LocalDateTime.now());
175
        userWalletHistory.setDescription(description);
175
        userWalletHistory.setDescription(description);
176
        userWalletHistory.setBusinessTimestamp(businessTimestamp);
176
        userWalletHistory.setBusinessTimestamp(businessTimestamp);
-
 
177
        userWalletHistory.setRunningBalance(walletBalance);
177
        userWalletHistoryRepository.persist(userWalletHistory);
178
        userWalletHistoryRepository.persist(userWalletHistory);
178
    }
179
    }
179
 
180
 
180
    @Override
181
    @Override
181
    public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
182
    public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
Line 331... Line 332...
331
        userWalletHistory.setReferenceType(walletReferenceType);
332
        userWalletHistory.setReferenceType(walletReferenceType);
332
        userWalletHistory.setWalletId(userWallet.getId());
333
        userWalletHistory.setWalletId(userWallet.getId());
333
        userWalletHistory.setTimestamp(LocalDateTime.now());
334
        userWalletHistory.setTimestamp(LocalDateTime.now());
334
        userWalletHistory.setDescription(description);
335
        userWalletHistory.setDescription(description);
335
        userWalletHistory.setBusinessTimestamp(all_entries.get(0).getBusinessTimestamp());
336
        userWalletHistory.setBusinessTimestamp(all_entries.get(0).getBusinessTimestamp());
-
 
337
        userWalletHistory.setRunningBalance(userWallet.getAmount());
336
 
338
 
337
        userWalletHistoryRepository.persist(userWalletHistory);
339
        userWalletHistoryRepository.persist(userWalletHistory);
338
        return true;
340
        return true;
339
 
341
 
340
    }
342
    }