Subversion Repositories SmartDukaan

Rev

Rev 36777 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
22550 ashik.ali 1
package com.spice.profitmandi.service.wallet;
2
 
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
34261 tejus.loha 4
import com.spice.profitmandi.common.model.CustomRetailer;
30740 amit.gupta 5
import com.spice.profitmandi.common.model.ProfitMandiConstants;
34261 tejus.loha 6
import com.spice.profitmandi.common.util.FileUtil;
23269 ashik.ali 7
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
25542 amit.gupta 8
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
34261 tejus.loha 9
import com.spice.profitmandi.dao.entity.transaction.AddWalletRequest;
32494 amit.gupta 10
import com.spice.profitmandi.dao.entity.transaction.ManualPaymentType;
22550 ashik.ali 11
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
12
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
34674 aman.kumar 13
import com.spice.profitmandi.dao.entity.user.User;
22550 ashik.ali 14
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
34261 tejus.loha 15
import com.spice.profitmandi.dao.enumuration.transaction.AddWalletRequestStatus;
37588 amit 16
import com.spice.profitmandi.dao.enumuration.transaction.TransactionType;
34261 tejus.loha 17
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
32494 amit.gupta 18
import com.spice.profitmandi.dao.repository.catalog.ManualPaymentRequestRepository;
25542 amit.gupta 19
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
33845 tejus.loha 20
import com.spice.profitmandi.dao.repository.dtr.PartnerOnBoardingPanelRepository;
33172 tejus.loha 21
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
22550 ashik.ali 22
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
23
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
24
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
33845 tejus.loha 25
import com.spice.profitmandi.dao.repository.user.StoreTimelinetbRepository;
34674 aman.kumar 26
import com.spice.profitmandi.dao.repository.user.UserRepository;
30025 amit.gupta 27
import com.spice.profitmandi.service.NotificationService;
30740 amit.gupta 28
import com.spice.profitmandi.service.PartnerInvestmentService;
34261 tejus.loha 29
import com.spice.profitmandi.service.order.OrderService;
30
import com.spice.profitmandi.service.user.RetailerService;
33845 tejus.loha 31
import com.spice.profitmandi.service.user.StoreTimelineTatService;
22857 ashik.ali 32
import in.shop2020.model.v1.order.WalletReferenceType;
30025 amit.gupta 33
import org.apache.logging.log4j.LogManager;
34
import org.apache.logging.log4j.Logger;
35
import org.springframework.beans.factory.annotation.Autowired;
34261 tejus.loha 36
import org.springframework.http.ResponseEntity;
30025 amit.gupta 37
import org.springframework.stereotype.Component;
22857 ashik.ali 38
 
30025 amit.gupta 39
import java.time.LocalDateTime;
40
import java.util.*;
41
import java.util.stream.Collectors;
42
 
22550 ashik.ali 43
@Component
44
public class WalletServiceImpl implements WalletService {
45
 
32494 amit.gupta 46
    private static final Logger LOGGER = LogManager.getLogger(WalletServiceImpl.class);
25395 tejbeer 47
 
32494 amit.gupta 48
    @Autowired
49
    ManualPaymentRequestRepository manualPaymentRequestRepository;
34261 tejus.loha 50
    @Autowired
51
    AddWalletRequestRepository addWalletRequestRepository;
52
    @Autowired
53
    OrderService orderService;
54
    @Autowired
55
    RetailerService retailerService;
33194 amit.gupta 56
    ProfitMandiBusinessException pbse = new ProfitMandiBusinessException("Wallet", "Wallet", "Wallet is under maintainance, please try after some time");
57
    ProfitMandiBusinessException inactivepbse = new ProfitMandiBusinessException("Wallet", "Wallet", "Investment is incomplete, please add amount to wallet to complete the investment");
58
 
32494 amit.gupta 59
    @Autowired
60
    PartnerInvestmentService partnerInvestmentService;
61
    private boolean underMaintainance = false;
62
    @Autowired
63
    private UserAccountRepository userAccountRepository;
33172 tejus.loha 64
 
32494 amit.gupta 65
    @Autowired
66
    private UserWalletRepository userWalletRepository;
67
    @Autowired
68
    private FofoStoreRepository fofoStoreRepository;
69
    @Autowired
70
    private UserWalletHistoryRepository userWalletHistoryRepository;
71
    @Autowired
72
    private NotificationService notificationService;
24739 tejbeer 73
 
33172 tejus.loha 74
    @Autowired
75
    RetailerRepository retailerRepository;
33845 tejus.loha 76
    @Autowired
77
    PartnerOnBoardingPanelRepository partnerOnBoardingPanelRepository;
78
    @Autowired
79
    StoreTimelineTatService storeTimelineTatService;
80
    @Autowired
81
    StoreTimelinetbRepository storeTimelinetbRepository;
33172 tejus.loha 82
 
34674 aman.kumar 83
    @Autowired
84
    UserRepository userRepository;
85
 
32494 amit.gupta 86
    @Override
87
    public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
88
                                  String description, float amount, LocalDateTime businessTime) throws ProfitMandiBusinessException {
89
        if (Math.round(amount) == 0)
90
            return;
36372 amit 91
        UserWallet userWallet = userWalletRepository.selectByRetailerIdForUpdate(retailerId);
35828 amit 92
        userWallet.setAmount(userWallet.getAmount() + Math.round(amount));
36513 amit 93
        this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime, userWallet.getAmount());
36305 amit 94
        if (WalletReferenceType.AUTOMATED_ADVANCE.equals(referenceType)
95
                || WalletReferenceType.ADVANCE_AMOUNT.equals(referenceType)
96
                || WalletReferenceType.PAYMENT_GATEWAY.equals(referenceType)
97
                || WalletReferenceType.ONLINE_ORDER_PAYMENT.equals(referenceType)
98
                || WalletReferenceType.PURCHASE.equals(referenceType)) {
99
            partnerInvestmentService.evictInvestmentCache(retailerId);
100
        }
32494 amit.gupta 101
    }
25609 amit.gupta 102
 
32494 amit.gupta 103
    @Override
104
    public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
105
                                        String description, float amount, LocalDateTime businessTime) throws ProfitMandiBusinessException {
106
        if (underMaintainance) {
107
            throw pbse;
108
        } else if (WalletReferenceType.RECHARGE.equals(referenceType) && !isActive(retailerId)) {
109
            throw inactivepbse;
110
        }
111
        if (amount == 0)
112
            return;
36372 amit 113
        UserWallet userWallet = userWalletRepository.selectByRetailerIdForUpdate(retailerId);
35828 amit 114
        int walletAmount = userWallet.getAmount();
33547 tejus.loha 115
        if (!WalletReferenceType.DAMAGE_PROTECTION.equals(referenceType) && amount > ProfitMandiConstants.MAX_NEGATIVE_WALLET_VALUE && Math.floor(amount) > walletAmount) {
34575 vikas.jang 116
            LOGGER.error("Wallet Balance is insufficient!, needed - {}, wallet has - {}, for retailer - {}", Math.floor(amount), walletAmount, retailerId);
34674 aman.kumar 117
            User user = userRepository.selectById(retailerId);
118
            throw new ProfitMandiBusinessException(user.getName(), walletAmount, "WLT_1000");
32494 amit.gupta 119
        }
120
        userWallet.setAmount(walletAmount - Math.round(amount));
36513 amit 121
        this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime, userWallet.getAmount());
32494 amit.gupta 122
    }
24739 tejbeer 123
 
32494 amit.gupta 124
    @Override
125
    public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
126
                                        String description, float amount, LocalDateTime businessTime, boolean forced) throws ProfitMandiBusinessException {
127
        if (underMaintainance) {
128
            throw pbse;
129
        } else if (WalletReferenceType.RECHARGE.equals(referenceType) && !isActive(retailerId)) {
130
            throw inactivepbse;
131
        }
132
        if (amount == 0)
133
            return;
36372 amit 134
        UserWallet userWallet = userWalletRepository.selectByRetailerIdForUpdate(retailerId);
35828 amit 135
        int walletAmount = userWallet.getAmount();
32494 amit.gupta 136
        if (!forced && amount > 2 && amount > walletAmount) {
137
            LOGGER.error("Wallet Balance is insufficient!");
138
            throw new ProfitMandiBusinessException("balance", walletAmount, "WLT_1000");
139
        }
140
        userWallet.setAmount(walletAmount - Math.round(amount));
36513 amit 141
        this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description, businessTime, userWallet.getAmount());
32494 amit.gupta 142
    }
24739 tejbeer 143
 
32494 amit.gupta 144
    @Override
145
    public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
146
                                         WalletReferenceType walletReferenceType, String rollbackReason, LocalDateTime businessTime) throws ProfitMandiBusinessException {
25542 amit.gupta 147
 
32494 amit.gupta 148
        if (amountToRollback == 0)
149
            return;
36372 amit 150
        UserWallet userWallet = userWalletRepository.selectByRetailerIdForUpdate(retailerId);
35828 amit 151
        int walletAmount = userWallet.getAmount();
32494 amit.gupta 152
        List<UserWalletHistory> uwh = userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(rollbackReference, walletReferenceType)
153
                .stream().filter(x -> x.getWalletId() == userWallet.getId()).collect(Collectors.toList());
154
        if (uwh.size() == 0) {
155
            LOGGER.info("Retailer with id {} dont have valid reference {} and reference type {}",
156
                    retailerId, rollbackReference, walletReferenceType);
157
            throw new ProfitMandiBusinessException("Retailer specific wallet entries doesn't exist", retailerId, "Nothing to rollback");
158
        }
159
        userWallet.setAmount(walletAmount - Math.round(amountToRollback));
160
        this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
36513 amit 161
                walletReferenceType, rollbackReason, businessTime, userWallet.getAmount());
32494 amit.gupta 162
        userWalletRepository.persist(userWallet);
24739 tejbeer 163
 
32494 amit.gupta 164
    }
30025 amit.gupta 165
 
32494 amit.gupta 166
    private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType,
36513 amit 167
                                         String description, LocalDateTime businessTimestamp, long walletBalance) {
32494 amit.gupta 168
        if (amount == 0)
169
            return;
170
        UserWalletHistory userWalletHistory = new UserWalletHistory();
171
        userWalletHistory.setWalletId(walletId);
172
        userWalletHistory.setAmount(Math.round(amount));
173
        userWalletHistory.setReference(referenceId);
174
        userWalletHistory.setReferenceType(referenceType);
175
        userWalletHistory.setTimestamp(LocalDateTime.now());
176
        userWalletHistory.setDescription(description);
177
        userWalletHistory.setBusinessTimestamp(businessTimestamp);
36513 amit 178
        userWalletHistory.setRunningBalance(walletBalance);
32494 amit.gupta 179
        userWalletHistoryRepository.persist(userWalletHistory);
180
    }
24739 tejbeer 181
 
32494 amit.gupta 182
    @Override
183
    public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
184
        UserAccount userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
23509 amit.gupta 185
 
32494 amit.gupta 186
        if (underMaintainance) {
187
            throw pbse;
188
        }
189
        return userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccountKey()));
190
    }
30889 amit.gupta 191
 
32494 amit.gupta 192
    @Override
193
    public List<UserWalletHistory> getUserWalletHistoryByUserId(int userId) throws ProfitMandiBusinessException {
194
        if (underMaintainance || !isActive(userId)) {
195
            throw pbse;
196
        }
197
        UserWallet userWallet = this.getUserWalletByUserId(userId);
198
        List<UserWalletHistory> userWalletHistories = userWalletHistoryRepository.selectByWalletId(userWallet.getId());
199
        return userWalletHistories;
200
    }
25395 tejbeer 201
 
32494 amit.gupta 202
    //Definition is now changed, active also means valid investment should be ok
203
    private boolean isActive(int userId) {
204
        boolean active = true;
205
        try {
206
            FofoStore fs = fofoStoreRepository.selectByRetailerId(userId);
207
            active = fs.isActive() && partnerInvestmentService.isInvestmentOk(userId, ProfitMandiConstants.MIN_INVESTMENT_PERCENTAGE, ProfitMandiConstants.CUTOFF_INVESTMENT);
208
        } catch (Exception e) {
24739 tejbeer 209
 
32494 amit.gupta 210
        }
211
        return active;
212
    }
24739 tejbeer 213
 
32494 amit.gupta 214
    @Override
215
    public List<UserWalletHistory> getUserWalletHistoryByRetailerId(int retailerId)
216
            throws ProfitMandiBusinessException {
217
        if (underMaintainance) {
218
            throw pbse;
219
        }
220
        UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
221
        return userWalletHistoryRepository.selectByWalletId(userWallet.getId());
222
    }
24739 tejbeer 223
 
32494 amit.gupta 224
    @Override
225
    public long getSizeByRetailerId(int retailerId, LocalDateTime startDateTime, LocalDateTime endDateTime)
226
            throws ProfitMandiBusinessException {
227
        if (underMaintainance) {
228
            throw pbse;
229
        }
230
        UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
231
        return userWalletHistoryRepository.selectCountByWalletId(userWallet.getId(), startDateTime, endDateTime);
232
    }
25542 amit.gupta 233
 
32494 amit.gupta 234
    @Override
235
    public List<UserWalletHistory> getPaginatedUserWalletHistoryByRetailerId(int retailerId,
236
                                                                             LocalDateTime startDateTime, LocalDateTime endDateTime, int offset, int limit)
237
            throws ProfitMandiBusinessException {
238
        if (underMaintainance) {
239
            throw pbse;
240
        }
241
        UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
242
        return userWalletHistoryRepository.selectPaginatedByWalletId(userWallet.getId(), startDateTime, endDateTime,
243
                offset, limit);
244
    }
22550 ashik.ali 245
 
32494 amit.gupta 246
    @Override
33247 ranu 247
    public Map<Integer, UserWallet> getRetailerIdUserWalletMap(Set<Integer> retailerIds) throws ProfitMandiBusinessException {
32494 amit.gupta 248
        List<UserWallet> userWallets = userWalletRepository.selectByRetailerIds(retailerIds);
249
        Map<Integer, UserWallet> retailerIdUserWalletMap = new HashMap<>();
250
        for (UserWallet userWallet : userWallets) {
251
            retailerIdUserWalletMap.put(userWallet.getUserId(), userWallet);
252
        }
253
        return retailerIdUserWalletMap;
254
    }
24739 tejbeer 255
 
32494 amit.gupta 256
    @Override
33247 ranu 257
    public Map<Integer, Integer> getWaleltRetailerMap(Set<Integer> walletIds) throws ProfitMandiBusinessException {
32494 amit.gupta 258
        List<UserWallet> userWallets = userWalletRepository.selectAllById(walletIds);
259
        Map<Integer, Integer> walletRetailerMap = new HashMap<>();
260
        for (UserWallet userWallet : userWallets) {
261
            walletRetailerMap.put(userWallet.getId(), userWallet.getUserId());
262
        }
263
        return walletRetailerMap;
264
    }
30740 amit.gupta 265
 
32494 amit.gupta 266
    @Override
267
    public boolean isExistWalletHistory(int retailerId, int referenceId, WalletReferenceType referenceType)
268
            throws ProfitMandiBusinessException {
269
        if (underMaintainance) {
270
            throw pbse;
271
        }
272
        UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
273
        return userWalletHistoryRepository.isExist(userWallet.getId(), referenceType, referenceId);
274
    }
25542 amit.gupta 275
 
32494 amit.gupta 276
    @Override
277
    public UserWallet getUserWallet(int retailerId) throws ProfitMandiBusinessException {
278
        if (underMaintainance) {
279
            throw pbse;
280
        }
281
        try {
282
            return userWalletRepository.selectByRetailerId(retailerId);
283
        } catch (Exception e) {
284
            UserWallet uw = new UserWallet();
285
            uw.setAmount(0);
286
            uw.setRefundableAmount(0);
287
            uw.setUserId(retailerId);
288
            userWalletRepository.persist(uw);
289
            return uw;
290
        }
291
    }
25542 amit.gupta 292
 
32494 amit.gupta 293
    @Override
294
    public float getOpeningTill(int fofoId, LocalDateTime date) throws ProfitMandiBusinessException {
295
        UserWallet wallet = userWalletRepository.selectByRetailerId(fofoId);
296
        return userWalletHistoryRepository.getSumTillDate(wallet.getId(), date);
297
    }
24739 tejbeer 298
 
32494 amit.gupta 299
    @Override
300
    public boolean refundToWallet(int retailerId, float amountToRefund, int transactionId,
301
                                  WalletReferenceType walletReferenceType, String description) throws ProfitMandiBusinessException {
24739 tejbeer 302
 
32494 amit.gupta 303
        List<UserWalletHistory> all_entries = userWalletHistoryRepository
304
                .selectAllByreferenceIdandreferenceType(transactionId, walletReferenceType);
30449 amit.gupta 305
 
32494 amit.gupta 306
        UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
35828 amit 307
        int walletAmount = userWallet.getAmount();
32494 amit.gupta 308
        LOGGER.info("userWallet" + userWallet);
309
        int max_eligible_credit_amount = 0;
24739 tejbeer 310
 
32494 amit.gupta 311
        LOGGER.info("all_entries {}", all_entries);
312
        for (UserWalletHistory history : all_entries) {
313
            max_eligible_credit_amount -= history.getAmount();
314
        }
315
        if (max_eligible_credit_amount < amountToRefund) {
316
            LOGGER.info("Cant be credited back to wallet as most of it has been already credited");
317
            return false;
318
        }
22550 ashik.ali 319
 
32494 amit.gupta 320
        userWallet.setAmount(walletAmount + Math.round(amountToRefund));
23509 amit.gupta 321
 
32494 amit.gupta 322
        LOGGER.info("userWallet" + userWallet);
30449 amit.gupta 323
 
32494 amit.gupta 324
        UserWalletHistory userWalletHistory = new UserWalletHistory();
325
        userWalletHistory.setAmount(Math.round(amountToRefund));
326
        userWalletHistory.setReference(transactionId);
327
        userWalletHistory.setReferenceType(walletReferenceType);
328
        userWalletHistory.setWalletId(userWallet.getId());
329
        userWalletHistory.setTimestamp(LocalDateTime.now());
330
        userWalletHistory.setDescription(description);
331
        userWalletHistory.setBusinessTimestamp(all_entries.get(0).getBusinessTimestamp());
36513 amit 332
        userWalletHistory.setRunningBalance(userWallet.getAmount());
30449 amit.gupta 333
 
32494 amit.gupta 334
        userWalletHistoryRepository.persist(userWalletHistory);
335
        return true;
24739 tejbeer 336
 
32494 amit.gupta 337
    }
26254 amit.gupta 338
 
32494 amit.gupta 339
    @Override
340
    public int getWalletAmount(int retailerId) throws ProfitMandiBusinessException {
341
        UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
342
        if (userWallet == null) {
33421 amit.gupta 343
            userWallet = new UserWallet();
344
            userWallet.setUserId(retailerId);
345
            userWalletRepository.persist(userWallet);
32494 amit.gupta 346
        }
35828 amit 347
        int sum = (int) userWalletHistoryRepository.selectSumByWallet(userWallet.getId());
348
        userWallet.setAmount(sum);
349
        return sum;
32494 amit.gupta 350
    }
24739 tejbeer 351
 
32494 amit.gupta 352
    @Override
353
    public List<UserWalletHistory> getAllByReference(int fofoId, int reference, WalletReferenceType walletReferenceType)
354
            throws ProfitMandiBusinessException {
355
        UserWallet userWallet = userWalletRepository.selectByRetailerId(fofoId);
356
        if (userWallet == null) {
357
            return new ArrayList<UserWalletHistory>();
358
        }
359
        return userWalletHistoryRepository.selectAllByreferenceIdandreferenceType(userWallet.getId(), reference,
360
                walletReferenceType);
361
    }
30449 amit.gupta 362
 
32494 amit.gupta 363
    @Override
364
    public void resetWallet() throws ProfitMandiBusinessException {
365
        List<UserWallet> userWallets = userWalletRepository.selectAll();
366
        for (UserWallet userWallet : userWallets) {
367
            userWallet.setAmount(this.getWalletAmount(userWallet.getUserId()));
24739 tejbeer 368
 
32494 amit.gupta 369
        }
370
    }
25395 tejbeer 371
 
32494 amit.gupta 372
    @Override
373
    public int getManualReference(WalletReferenceType referenceType) {
374
        ManualPaymentType paymentType = manualPaymentRequestRepository.selectByReferenceType(referenceType);
24739 tejbeer 375
 
32494 amit.gupta 376
        if (paymentType == null) {
377
            paymentType = new ManualPaymentType();
378
            paymentType.setReferenceType(referenceType);
379
            manualPaymentRequestRepository.persist(paymentType);
380
        }
381
        paymentType.setCounter(paymentType.getCounter() + 1);
382
        return paymentType.getCounter();
24739 tejbeer 383
 
32494 amit.gupta 384
    }
34261 tejus.loha 385
 
37588 amit 386
    /**
387
     * Manual (admin) wallet adjustment. Allocates a manual reference when one is not supplied,
388
     * checks the reference is not already assigned to a different partner, and then moves the
389
     * money through {@link #addAmountToWallet}/{@link #consumeAmountFromWallet} so the balance is
390
     * only ever changed under the wallet row lock those methods take.
391
     *
392
     * @return the reference actually used (allocated here when the caller passed 0)
393
     */
34261 tejus.loha 394
    @Override
37588 amit 395
    public int applyManualAdjustment(int retailerId, int reference, WalletReferenceType referenceType,
396
                                     TransactionType transactionType, int amount, String description,
397
                                     LocalDateTime businessTimestamp) throws ProfitMandiBusinessException {
398
        if (reference == 0 && referenceType.getValue() >= WalletReferenceType.INCENTIVES.getValue()) {
399
            reference = this.getManualReference(referenceType);
400
        } else if (reference == 0) {
401
            throw new ProfitMandiBusinessException("RefrenceId", reference,
402
                    "System specific reference type cant be used manually");
403
        }
404
 
405
        UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
406
        final int walletId = userWallet != null ? userWallet.getId() : 0;
407
        List<UserWalletHistory> existingEntries = userWalletHistoryRepository
408
                .selectAllByreferenceIdandreferenceType(reference, referenceType);
409
        if (!existingEntries.isEmpty()
410
                && existingEntries.stream().noneMatch(x -> x.getWalletId() == walletId)) {
411
            throw new ProfitMandiBusinessException("RefrenceId", reference, "Reference Id assign to Other  partner");
412
        }
413
 
414
        if (TransactionType.DEBIT.equals(transactionType)) {
415
            this.consumeAmountFromWallet(retailerId, reference, referenceType, description, amount,
416
                    businessTimestamp, true);
417
        } else {
418
            this.addAmountToWallet(retailerId, reference, referenceType, description, amount, businessTimestamp);
419
        }
420
        return reference;
421
    }
422
 
423
    @Override
34261 tejus.loha 424
    public ResponseEntity<?> createAddWalletRequestReport(LocalDateTime startDate, LocalDateTime endDate, AddWalletRequestStatus status) throws Exception {
425
        List<List<?>> rows = new ArrayList<>();
426
        List<String> header = Arrays.asList(
427
                "Retailer Id",
428
                "Retailer Name",
429
                "Email",
430
                "City",
431
                "State",
432
                "Transaction Reference",
433
                "Bank Name",
434
                "Reference Date",
435
                "Amount",
436
                "status",
437
                "Created On",
438
                "Updated On"
439
        );
440
 
441
        List<AddWalletRequest> addWalletRequests = addWalletRequestRepository.selectAllRetailerIdByDateAndStatus(startDate, endDate, status);
442
        Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
443
        for (AddWalletRequest addWalletRequest : addWalletRequests) {
444
            CustomRetailer customRetailer = customRetailerMap.get(addWalletRequest.getRetailerId());
445
            rows.add(Arrays
446
                    .asList(
447
                            addWalletRequest.getRetailerId(),
448
                            customRetailer.getBusinessName(),
449
                            customRetailer.getEmail(),
450
                            customRetailer.getAddress().getCity(),
451
                            customRetailer.getAddress().getState(),
452
                            addWalletRequest.getTransaction_reference(),
453
                            addWalletRequest.getBank_name(),
454
                            addWalletRequest.getReference_date(),
455
                            addWalletRequest.getAmount(),
456
                            addWalletRequest.getStatus(),
457
                            addWalletRequest.getCreateTimestamp(),
458
                            addWalletRequest.getUpdateTimestamp()
459
                    )
460
            );
461
        }
462
        org.apache.commons.io.output.ByteArrayOutputStream baos = FileUtil.getCSVByteStream(header, rows);
463
        ResponseEntity<?> responseEntity = orderService.downloadReportInCsv(baos, rows, "Add-wallet-request-"+status+"-Reort-" + startDate.toLocalDate() + "-To-" + endDate.toLocalDate());
464
        return responseEntity;
465
    }
32494 amit.gupta 466
}
24739 tejbeer 467