Subversion Repositories SmartDukaan

Rev

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

Rev 34576 Rev 34592
Line 115... Line 115...
115
            }
115
            }
116
        }
116
        }
117
        this.generatePurchaseOrder(bulkOrderModels, creatorId, ProfitMandiConstants.PO_TYPE.MANUAL, ProfitMandiConstants.BID_CRON_ENUM.TODAY);
117
        this.generatePurchaseOrder(bulkOrderModels, creatorId, ProfitMandiConstants.PO_TYPE.MANUAL, ProfitMandiConstants.BID_CRON_ENUM.TODAY);
118
    }
118
    }
119
 
119
 
120
    public void generatePurchaseOrder(List<BulkOrderModel> bulkOrderModels, int creatorId, ProfitMandiConstants.PO_TYPE type, ProfitMandiConstants.BID_CRON_ENUM scheduleType) throws Exception {
120
    public ProfitMandiConstants.BID_ENUM generatePurchaseOrder(List<BulkOrderModel> bulkOrderModels, int creatorId, ProfitMandiConstants.PO_TYPE type, ProfitMandiConstants.BID_CRON_ENUM scheduleType) throws Exception {
121
        Map<Integer, List<BulkOrderModel>> fofoBulkOrdersMap = bulkOrderModels.stream().collect(Collectors.groupingBy(x -> x.getFofoId()));
121
        Map<Integer, List<BulkOrderModel>> fofoBulkOrdersMap = bulkOrderModels.stream().collect(Collectors.groupingBy(x -> x.getFofoId()));
122
        boolean approvalRequired = false;
122
        boolean approvalRequired = false;
-
 
123
        ProfitMandiConstants.BID_ENUM finalBidStatus = ProfitMandiConstants.BID_ENUM.CLOSED;
123
        for (Map.Entry<Integer, List<BulkOrderModel>> fofoBulkOrderEntry : fofoBulkOrdersMap.entrySet()) {
124
        for (Map.Entry<Integer, List<BulkOrderModel>> fofoBulkOrderEntry : fofoBulkOrdersMap.entrySet()) {
124
            int fofoId = fofoBulkOrderEntry.getKey();
125
            int fofoId = fofoBulkOrderEntry.getKey();
125
            List<BulkOrderModel> fofoBulkOrderModels = fofoBulkOrderEntry.getValue();
126
            List<BulkOrderModel> fofoBulkOrderModels = fofoBulkOrderEntry.getValue();
126
            Map<Integer, Long> orderItemCountMap = fofoBulkOrderModels.stream().collect(Collectors.groupingBy(x -> x.getItemId(), Collectors.counting()));
127
            Map<Integer, Long> orderItemCountMap = fofoBulkOrderModels.stream().collect(Collectors.groupingBy(x -> x.getItemId(), Collectors.counting()));
127
 
128
 
Line 164... Line 165...
164
                }
165
                }
165
                totalPayableAmount += cartItem.getSellingPrice() * cartItem.getQuantity();
166
                totalPayableAmount += cartItem.getSellingPrice() * cartItem.getQuantity();
166
                cartItems.add(cartItem);
167
                cartItems.add(cartItem);
167
            }
168
            }
168
            Bid bid = null;
169
            Bid bid = null;
169
            ProfitMandiConstants.BID_ENUM finalBidStatus = ProfitMandiConstants.BID_ENUM.CLOSED;
-
 
170
            if (type.equals(ProfitMandiConstants.PO_TYPE.AUTO)) {
170
            if (type.equals(ProfitMandiConstants.PO_TYPE.AUTO)) {
171
                bid = bidRepository.selectById(fofoBulkOrderModels.get(0).getRowIndex());
171
                bid = bidRepository.selectById(fofoBulkOrderModels.get(0).getRowIndex());
-
 
172
                approvalRequired = false;
172
            }
173
            }
173
            LOGGER.info("totalAmount of item " + totalPayableAmount);
174
            LOGGER.info("totalAmount of item " + totalPayableAmount);
174
            double walletAmount = walletService.getWalletAmount(fofoId);
175
            double walletAmount = walletService.getWalletAmount(fofoId);
175
 
176
 
176
            BigDecimal creditAvailability = sdCreditService.getAvailableAmount(fofoId);
177
            BigDecimal creditAvailability = sdCreditService.getAvailableAmount(fofoId);
Line 180... Line 181...
180
            if (totalPayableAmount > ProfitMandiConstants.MAX_NEGATIVE_WALLET_VALUE && netAmountInHand < totalPayableAmount) {
181
            if (totalPayableAmount > ProfitMandiConstants.MAX_NEGATIVE_WALLET_VALUE && netAmountInHand < totalPayableAmount) {
181
                if (type.equals(ProfitMandiConstants.PO_TYPE.MANUAL)) {
182
                if (type.equals(ProfitMandiConstants.PO_TYPE.MANUAL)) {
182
                    throw new ProfitMandiBusinessException("Skipping order due to insufficient balance for id - ", fofoId, " ,Check wallet Balance once");
183
                    throw new ProfitMandiBusinessException("Skipping order due to insufficient balance for id - ", fofoId, " ,Check wallet Balance once");
183
                } else {
184
                } else {
184
                    if (scheduleType.equals(ProfitMandiConstants.BID_CRON_ENUM.TODAY)) {
185
                    if (scheduleType.equals(ProfitMandiConstants.BID_CRON_ENUM.TODAY)) {
185
                        bidService.sendMailToRBM(netAmountInHand, totalPayableAmount, fofoId);
186
                        finalBidStatus = bidService.sendMailToRBM(netAmountInHand, totalPayableAmount, fofoId);
186
                        finalBidStatus = ProfitMandiConstants.BID_ENUM.PROCESSING;
-
 
187
                        LOGGER.info("Skipping order due to insufficient balance for id - "+ fofoId+ " Sending mail to RBM");
187
                        LOGGER.info("Skipping order due to insufficient balance for id - "+ fofoId+ " Sending mail to RBM");
188
                        //throw new ProfitMandiBusinessException("Skipping order due to insufficient balance for id - ", fofoId, " ,Sending mail to RBM");
188
                        //throw new ProfitMandiBusinessException("Skipping order due to insufficient balance for id - ", fofoId, " ,Sending mail to RBM");
189
                    } else {
189
                    } else {
190
                        bidService.cancelYesterdayProcessBid(bid);
190
                        finalBidStatus = bidService.cancelYesterdayProcessBid(bid);
191
                        LOGGER.info("Skipping order due to insufficient balance for id - "+ fofoId+ " Cancelling the BID");
191
                        LOGGER.info("Skipping order due to insufficient balance for id - "+ fofoId+ " Cancelling the BID");
192
                        finalBidStatus = ProfitMandiConstants.BID_ENUM.CANCELLED;
-
 
193
                        //throw new ProfitMandiBusinessException("Skipping order due to insufficient balance for id - ", fofoId, " ,Cancelling the BID");
192
                        //throw new ProfitMandiBusinessException("Skipping order due to insufficient balance for id - ", fofoId, " ,Cancelling the BID");
194
                    }
193
                    }
195
                }
194
                }
196
            }
195
            }
197
            UserCart userCart = cartService.setCartItems(fofoId, cartItems);
196
            UserCart userCart = cartService.setCartItems(fofoId, cartItems);
Line 205... Line 204...
205
                    Loan loan = sdCreditService.createLoan(userCart.getUserId(), creditAmountRequired, 0, "Credit limit assigned via SD Credit");
204
                    Loan loan = sdCreditService.createLoan(userCart.getUserId(), creditAmountRequired, 0, "Credit limit assigned via SD Credit");
206
                    loanId = loan.getId();
205
                    loanId = loan.getId();
207
                }
206
                }
208
            } catch (Exception exception){
207
            } catch (Exception exception){
209
                if (type.equals(ProfitMandiConstants.PO_TYPE.AUTO)) {
208
                if (type.equals(ProfitMandiConstants.PO_TYPE.AUTO)) {
210
                    bidService.sendMailToRBM(netAmountInHand, totalPayableAmount, fofoId);
209
                    finalBidStatus = bidService.sendMailToRBM(netAmountInHand, totalPayableAmount, fofoId);
211
                    finalBidStatus = ProfitMandiConstants.BID_ENUM.PROCESSING;
-
 
212
                    LOGGER.info("Skipping order due to insufficient balance for id - "+ fofoId+ " Cancelling the BID");
210
                    LOGGER.info("Skipping order due to insufficient balance for id - "+ fofoId+ " Cancelling the BID");
213
                    //throw new ProfitMandiBusinessException("Skipping order unable to create load for id - ", fofoId, " ,Sending mail to RBM");
211
                    //throw new ProfitMandiBusinessException("Skipping order unable to create load for id - ", fofoId, " ,Sending mail to RBM");
214
                }
212
                }
215
            }
213
            }
216
 
214
 
Line 222... Line 220...
222
                //Set here created by
220
                //Set here created by
223
                Transaction transaction = transactionRepository.selectById(transactionId);
221
                Transaction transaction = transactionRepository.selectById(transactionId);
224
                transaction.setCreatedBy(creatorId);
222
                transaction.setCreatedBy(creatorId);
225
                LOGGER.info("transaction  created by {}", transaction.getCreatedBy());
223
                LOGGER.info("transaction  created by {}", transaction.getCreatedBy());
226
                commonPaymentService.payThroughWallet(transactionId);
224
                commonPaymentService.payThroughWallet(transactionId);
227
                if (type.equals(ProfitMandiConstants.PO_TYPE.AUTO)) {
-
 
228
                    bid.setStatus(finalBidStatus);
-
 
229
                    approvalRequired = false;
-
 
230
                }
-
 
231
                if (approvalRequired) {
225
                if (approvalRequired) {
232
                    this.createApproval(transactionId);
226
                    this.createApproval(transactionId);
233
                    if (loanId > 0) {
227
                    if (loanId > 0) {
234
                        LoanTransaction loanTransaction = new LoanTransaction();
228
                        LoanTransaction loanTransaction = new LoanTransaction();
235
                        loanTransaction.setLoanId(loanId);
229
                        loanTransaction.setLoanId(loanId);
Line 240... Line 234...
240
                    transactionService.processTransaction(transactionId, loanId);
234
                    transactionService.processTransaction(transactionId, loanId);
241
                }
235
                }
242
            }
236
            }
243
 
237
 
244
        }
238
        }
-
 
239
        return finalBidStatus;
245
    }
240
    }
246
 
241
 
247
    public void createApproval(int transactionId) {
242
    public void createApproval(int transactionId) {
248
        TransactionApproval transactionApproval = new TransactionApproval();
243
        TransactionApproval transactionApproval = new TransactionApproval();
249
        transactionApproval.setId(transactionId);
244
        transactionApproval.setId(transactionId);