Subversion Repositories SmartDukaan

Rev

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

Rev 33173 Rev 33213
Line 3... Line 3...
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
4
import com.spice.profitmandi.common.model.BulkOrderModel;
4
import com.spice.profitmandi.common.model.BulkOrderModel;
5
import com.spice.profitmandi.common.util.ExcelUtils;
5
import com.spice.profitmandi.common.util.ExcelUtils;
6
import com.spice.profitmandi.dao.cart.CartService;
6
import com.spice.profitmandi.dao.cart.CartService;
7
import com.spice.profitmandi.dao.entity.catalog.TagListing;
7
import com.spice.profitmandi.dao.entity.catalog.TagListing;
-
 
8
import com.spice.profitmandi.dao.entity.transaction.Transaction;
-
 
9
import com.spice.profitmandi.dao.entity.transaction.TransactionApproval;
-
 
10
import com.spice.profitmandi.dao.enumuration.transaction.TransactionApprovalStatus;
8
import com.spice.profitmandi.dao.model.CartItem;
11
import com.spice.profitmandi.dao.model.CartItem;
9
import com.spice.profitmandi.dao.model.UserCart;
12
import com.spice.profitmandi.dao.model.UserCart;
10
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
13
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
11
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
14
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
-
 
15
import com.spice.profitmandi.dao.repository.transaction.TransactionApprovalRepository;
-
 
16
import com.spice.profitmandi.dao.repository.transaction.TransactionRepository;
-
 
17
import com.spice.profitmandi.dao.repository.user.AddressRepository;
12
import com.spice.profitmandi.service.transaction.TransactionService;
18
import com.spice.profitmandi.service.transaction.TransactionService;
13
import com.spice.profitmandi.service.wallet.CommonPaymentService;
19
import com.spice.profitmandi.service.wallet.CommonPaymentService;
14
import com.spice.profitmandi.service.wallet.WalletService;
20
import com.spice.profitmandi.service.wallet.WalletService;
15
import org.apache.logging.log4j.LogManager;
21
import org.apache.logging.log4j.LogManager;
16
import org.apache.logging.log4j.Logger;
22
import org.apache.logging.log4j.Logger;
-
 
23
import org.apache.poi.ss.usermodel.Cell;
17
import org.apache.poi.ss.usermodel.Row;
24
import org.apache.poi.ss.usermodel.Row;
18
import org.apache.poi.xssf.usermodel.XSSFRow;
25
import org.apache.poi.xssf.usermodel.XSSFRow;
19
import org.apache.poi.xssf.usermodel.XSSFSheet;
26
import org.apache.poi.xssf.usermodel.XSSFSheet;
20
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
27
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
21
import org.springframework.beans.factory.annotation.Autowired;
28
import org.springframework.beans.factory.annotation.Autowired;
22
import org.springframework.stereotype.Controller;
29
import org.springframework.stereotype.Service;
-
 
30
import org.springframework.transaction.annotation.Transactional;
23
import org.springframework.web.multipart.MultipartFile;
31
import org.springframework.web.multipart.MultipartFile;
24
 
32
 
25
import java.util.ArrayList;
33
import java.util.ArrayList;
26
import java.util.List;
34
import java.util.List;
27
import java.util.Map;
35
import java.util.Map;
28
import java.util.stream.Collectors;
36
import java.util.stream.Collectors;
29
 
37
 
30
@Controller
38
@Service
-
 
39
@Transactional(rollbackFor = Throwable.class)
31
public class BulkOrderService {
40
public class BulkOrderService {
32
    private static final Logger LOGGER = LogManager.getLogger(BulkOrderService.class);
41
    private static final Logger LOGGER = LogManager.getLogger(BulkOrderService.class);
33
    @Autowired
42
    @Autowired
34
    CartService cartService;
43
    CartService cartService;
35
    @Autowired
44
    @Autowired
Line 40... Line 49...
40
    TagListingRepository tagListingRepository;
49
    TagListingRepository tagListingRepository;
41
    @Autowired
50
    @Autowired
42
    WalletService walletService;
51
    WalletService walletService;
43
    @Autowired
52
    @Autowired
44
    UserRepository userRepository;
53
    UserRepository userRepository;
-
 
54
    @Autowired
-
 
55
    TransactionRepository transactionRepository;
-
 
56
    @Autowired
-
 
57
    TransactionApprovalRepository transactionApprovalRepository;
-
 
58
    @Autowired
-
 
59
    AddressRepository addressRepository;
-
 
60
    //TODO:Tejus need to check
45
 
61
 
46
    public void parseBulkOrders(MultipartFile file) throws Exception {
62
    public void parseBulkOrders(MultipartFile file, int creatorId) throws Exception {
47
        XSSFWorkbook myWorkBook = new XSSFWorkbook(file.getInputStream());
63
        XSSFWorkbook myWorkBook = new XSSFWorkbook(file.getInputStream());
48
 
64
 
49
        myWorkBook.setMissingCellPolicy(Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
65
        myWorkBook.setMissingCellPolicy(Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
50
        // Return first sheet from the XLSX workbook
66
        // Return first sheet from the XLSX workbook
51
        XSSFSheet mySheet = myWorkBook.getSheetAt(0);
67
        XSSFSheet mySheet = myWorkBook.getSheetAt(0);
Line 55... Line 71...
55
            XSSFRow row = mySheet.getRow(rowNumber);
71
            XSSFRow row = mySheet.getRow(rowNumber);
56
            BulkOrderModel bulkOrderModel = this.createBulkModel(row);
72
            BulkOrderModel bulkOrderModel = this.createBulkModel(row);
57
            bulkOrderModels.add(bulkOrderModel);
73
            bulkOrderModels.add(bulkOrderModel);
58
        }
74
        }
59
        Map<Integer, List<BulkOrderModel>> fofoBulkOrdersMap = bulkOrderModels.stream().collect(Collectors.groupingBy(x -> x.getFofoId()));
75
        Map<Integer, List<BulkOrderModel>> fofoBulkOrdersMap = bulkOrderModels.stream().collect(Collectors.groupingBy(x -> x.getFofoId()));
-
 
76
        boolean approvalNotRequired = true;
60
        for (Map.Entry<Integer, List<BulkOrderModel>> fofoBulkOrderEntry : fofoBulkOrdersMap.entrySet()) {
77
        for (Map.Entry<Integer, List<BulkOrderModel>> fofoBulkOrderEntry : fofoBulkOrdersMap.entrySet()) {
61
            int fofoId = fofoBulkOrderEntry.getKey();
78
            int fofoId = fofoBulkOrderEntry.getKey();
62
            List<BulkOrderModel> fofoBulkOrderModels = fofoBulkOrderEntry.getValue();
79
            List<BulkOrderModel> fofoBulkOrderModels = fofoBulkOrderEntry.getValue();
63
            Map<Integer, Long> orderItemCountMap = fofoBulkOrderModels.stream().collect(Collectors.groupingBy(x -> x.getItemId(), Collectors.counting()));
80
            Map<Integer, Long> orderItemCountMap = fofoBulkOrderModels.stream().collect(Collectors.groupingBy(x -> x.getItemId(), Collectors.counting()));
64
 
81
 
Line 75... Line 92...
75
            double totalAmount = 0;
92
            double totalAmount = 0;
76
            for (BulkOrderModel fofoBulkOrderModel : fofoBulkOrderModels) {
93
            for (BulkOrderModel fofoBulkOrderModel : fofoBulkOrderModels) {
77
                CartItem cartItem = new CartItem();
94
                CartItem cartItem = new CartItem();
78
                cartItem.setQuantity(fofoBulkOrderModel.getQuantity());
95
                cartItem.setQuantity(fofoBulkOrderModel.getQuantity());
79
                cartItem.setItemId(fofoBulkOrderModel.getItemId());
96
                cartItem.setItemId(fofoBulkOrderModel.getItemId());
-
 
97
                boolean isPriceZero = fofoBulkOrderModel.getItemPrice() == 0.0;
80
                TagListing tagListing = tagListingRepository.selectByItemId(fofoBulkOrderModel.getItemId());
98
                TagListing tagListing = tagListingRepository.selectByItemId(fofoBulkOrderModel.getItemId());
-
 
99
                double itemSellingPrice = tagListing.getSellingPrice();
-
 
100
                double itemMrp = tagListing.getMrp();
-
 
101
                double customSellingPrice = fofoBulkOrderModel.getItemPrice();
-
 
102
                int itemId = cartItem.getItemId();
81
                //TODO:Tejus need to check
103
                if (isPriceZero) {
82
                cartItem.setSellingPrice(tagListing.getSellingPrice());
104
                    cartItem.setSellingPrice(itemSellingPrice);
-
 
105
                } else {
-
 
106
                    if (customSellingPrice <= itemMrp) {
-
 
107
                        cartItem.setSellingPrice(customSellingPrice);
-
 
108
                        approvalNotRequired = false;
-
 
109
 
-
 
110
                    } else {
-
 
111
                        throw new ProfitMandiBusinessException("Given price is greater than selling price for item Id - ", itemId, " it should be less or equal of DP");
-
 
112
                    }
-
 
113
 
-
 
114
                }
83
                totalAmount += cartItem.getSellingPrice() * cartItem.getQuantity();
115
                totalAmount += cartItem.getSellingPrice() * cartItem.getQuantity();
84
                cartItems.add(cartItem);
116
                cartItems.add(cartItem);
85
 
-
 
86
 
-
 
87
            }
117
            }
88
            LOGGER.info("totalAmount of item " + totalAmount);
118
            LOGGER.info("totalAmount of item " + totalAmount);
89
            double walletAmount = walletService.getWalletAmount(fofoId);
119
            double walletAmount = walletService.getWalletAmount(fofoId);
90
 
-
 
91
            if (walletAmount < totalAmount) {
120
            if (walletAmount < totalAmount) {
92
                LOGGER.info("Skippin order due to insufficent balance for id - {}", fofoId);
121
                throw new ProfitMandiBusinessException("Skippin order due to insufficent balance for id - ", fofoId, " ,Check wallet Balance ones");
93
                continue;
-
 
94
            }
122
            }
95
            UserCart userCart = cartService.setCartItems(fofoId, cartItems);
123
            UserCart userCart = cartService.setCartItems(fofoId, cartItems);
-
 
124
            // createtransactionInternally set the value in transaction table
96
            int transactionId = transactionService.createTransactionInternally(userCart, totalAmount, 0);
125
            int transactionId = transactionService.createTransactionInternally(userCart, totalAmount, 0);
-
 
126
            //Set here created by
-
 
127
            Transaction transaction = transactionRepository.selectById(transactionId);
-
 
128
            transaction.setCreatedBy(creatorId);
-
 
129
            LOGGER.info("transaction  created by {}", transaction.getCreatedBy());
97
            commonPaymentService.payThroughWallet(transactionId);
130
            commonPaymentService.payThroughWallet(transactionId);
98
            // Once paid let proceed to process the order further.
131
            if (approvalNotRequired) {
99
            transactionService.processTransaction(transactionId);
132
                transactionService.processTransaction(transactionId);
-
 
133
            } else {
-
 
134
                this.createApproval(transactionId);
-
 
135
            }
-
 
136
 
100
        }
137
        }
-
 
138
 
-
 
139
    }
-
 
140
 
-
 
141
    public void createApproval(int transactionId) {
-
 
142
        TransactionApproval transactionApproval = new TransactionApproval();
-
 
143
        transactionApproval.setId(transactionId);
-
 
144
        transactionApproval.setStatus(TransactionApprovalStatus.PENDING);
-
 
145
        transactionApprovalRepository.persist(transactionApproval);
101
    }
146
    }
102
 
147
 
103
    private BulkOrderModel createBulkModel(XSSFRow row) throws ProfitMandiBusinessException {
148
    private BulkOrderModel createBulkModel(XSSFRow row) throws ProfitMandiBusinessException {
104
        BulkOrderModel bulkOrderModel = new BulkOrderModel();
149
        BulkOrderModel bulkOrderModel = new BulkOrderModel();
105
        int i = 0;
150
        int i = 0;
106
        bulkOrderModel.setRowIndex(row.getRowNum());
151
        bulkOrderModel.setRowIndex(row.getRowNum());
107
        try {
152
        try {
-
 
153
            Cell partneranme = row.getCell(i++);
-
 
154
            if (partneranme == null)
-
 
155
                bulkOrderModel.setPartnerName("");
-
 
156
            else
108
            bulkOrderModel.setName(row.getCell(i++).getStringCellValue());
157
                bulkOrderModel.setPartnerName(partneranme.getStringCellValue().trim());
-
 
158
            Cell description = row.getCell(i++);
-
 
159
            if (description == null)
-
 
160
                bulkOrderModel.setDescription("");
-
 
161
            else
109
            bulkOrderModel.setDescription(row.getCell(i++).getStringCellValue());
162
                bulkOrderModel.setDescription(description.getStringCellValue().trim());
-
 
163
 
110
            bulkOrderModel.setFofoId((int) row.getCell(i++).getNumericCellValue());
164
            bulkOrderModel.setFofoId((int) row.getCell(i++).getNumericCellValue());
111
            bulkOrderModel.setItemId((int) row.getCell(i++).getNumericCellValue());
165
            bulkOrderModel.setItemId((int) row.getCell(i++).getNumericCellValue());
-
 
166
            bulkOrderModel.setItemPrice(row.getCell(i++).getNumericCellValue());
112
            bulkOrderModel.setQuantity((int) row.getCell(i++).getNumericCellValue());
167
            bulkOrderModel.setQuantity((int) row.getCell(i++).getNumericCellValue());
113
        } catch (Throwable e) {
168
        } catch (Throwable e) {
114
            LOGGER.info(e.getCause());
169
            LOGGER.info(e.getCause());
115
            throw new ProfitMandiBusinessException("Field", "Field at row - " + row.getRowNum() + ", column - " + (i - 1),
-
 
116
                    "Invalid field value at - " + ExcelUtils.toAlphabet(i - 1) + (row.getRowNum() + 1) + ", " + ExcelUtils.getCellValue(row.getCell(i - 1)));
170
            throw new ProfitMandiBusinessException("Field", "Field at row - " + row.getRowNum() + ", column - " + (i - 1), "Invalid field value at - " + ExcelUtils.toAlphabet(i - 1) + (row.getRowNum() + 1) + ", " + ExcelUtils.getCellValue(row.getCell(i - 1)));
117
        }
171
        }
118
        LOGGER.info(bulkOrderModel);
172
        LOGGER.info(bulkOrderModel);
119
        return bulkOrderModel;
173
        return bulkOrderModel;
120
    }
174
    }
121
 
175