Subversion Repositories SmartDukaan

Rev

Rev 33173 | Rev 33771 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
32952 amit.gupta 1
package com.spice.profitmandi.service.catalog;
2
 
3
 
4
import com.spice.profitmandi.common.enumuration.ItemType;
5
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
33172 tejus.loha 6
import com.spice.profitmandi.common.util.ExcelUtils;
32952 amit.gupta 7
import com.spice.profitmandi.common.util.Utils;
8
import com.spice.profitmandi.dao.entity.catalog.Item;
33035 amit.gupta 9
import com.spice.profitmandi.dao.entity.inventory.VendorCatalogPricing;
32952 amit.gupta 10
import com.spice.profitmandi.dao.entity.inventory.VendorItemPricing;
11
import com.spice.profitmandi.dao.entity.warehouse.Supplier;
12
import com.spice.profitmandi.dao.model.StateGstRateModel;
13
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
14
import com.spice.profitmandi.dao.repository.catalog.StateGstRateRepository;
33035 amit.gupta 15
import com.spice.profitmandi.dao.repository.inventory.VendorCatalogPricingRepository;
32952 amit.gupta 16
import com.spice.profitmandi.dao.repository.inventory.VendorItemPricingRepository;
17
import com.spice.profitmandi.dao.repository.warehouse.SupplierRepository;
18
import in.shop2020.model.v1.catalog.status;
19
import org.apache.logging.log4j.LogManager;
20
import org.apache.logging.log4j.Logger;
21
import org.apache.poi.ss.usermodel.Cell;
22
import org.apache.poi.ss.usermodel.Row;
23
import org.apache.poi.xssf.usermodel.XSSFRow;
24
import org.apache.poi.xssf.usermodel.XSSFSheet;
25
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
26
import org.springframework.beans.factory.annotation.Autowired;
27
import org.springframework.stereotype.Component;
28
import org.springframework.web.multipart.MultipartFile;
29
 
30
import java.time.LocalDateTime;
31
import java.util.Arrays;
32
import java.util.List;
33
import java.util.Map;
34
import java.util.stream.Collectors;
35
 
36
 
37
@Component
38
public class ItemLoaderService {
39
 
40
    @Autowired
41
    ItemRepository itemRepository;
42
 
43
    @Autowired
33035 amit.gupta 44
    VendorCatalogPricingRepository vendorCatalogPricingRepository;
45
 
46
    @Autowired
32952 amit.gupta 47
    StateGstRateRepository stateGstRateRepository;
48
 
49
    @Autowired
50
    VendorItemPricingRepository vendorItemPricingRepository;
51
 
33172 tejus.loha 52
 
32952 amit.gupta 53
    private static final Logger LOGGER = LogManager.getLogger(ItemLoaderService.class);
54
 
55
    public void parse(MultipartFile file) throws Exception {
56
        XSSFWorkbook myWorkBook = new XSSFWorkbook(file.getInputStream());
57
 
58
        myWorkBook.setMissingCellPolicy(Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
59
        // Return first sheet from the XLSX workbook
60
        XSSFSheet mySheet = myWorkBook.getSheetAt(0);
61
        LOGGER.info("rowCellNum {}", mySheet.getLastRowNum());
62
        for (int rowNumber = 1; rowNumber <= mySheet.getLastRowNum(); rowNumber++) {
63
            XSSFRow row = mySheet.getRow(rowNumber);
64
            ItemLoaderModel itemLoaderModel = this.createLoaderModel(row);
65
            try {
33346 amit.gupta 66
                LOGGER.info("rownum - {}", rowNumber);
32952 amit.gupta 67
                this.addItem(itemLoaderModel);
68
            } catch (ProfitMandiBusinessException e) {
69
                throw new ProfitMandiBusinessException(e.getRejectedType(), e.getRejectedValue(), e.getCode() + "at " + itemLoaderModel.getRowIndex());
70
            }
71
        }
72
    }
73
 
74
 
75
 
76
 
77
    private ItemLoaderModel createLoaderModel(XSSFRow row) throws ProfitMandiBusinessException {
78
        ItemLoaderModel itemLoaderModel = new ItemLoaderModel();
79
        int i = 0;
80
        itemLoaderModel.setRowIndex(row.getRowNum());
81
        try {
33037 amit.gupta 82
            itemLoaderModel.setProductGroup(row.getCell(i++).getStringCellValue().trim());
32952 amit.gupta 83
            itemLoaderModel.setCategoryId((int) row.getCell(i++).getNumericCellValue());
33037 amit.gupta 84
            itemLoaderModel.setCategory(row.getCell(i++).getStringCellValue().trim());
32952 amit.gupta 85
            itemLoaderModel.setHsnCode(String.valueOf((int) row.getCell(i++).getNumericCellValue()));
33037 amit.gupta 86
            itemLoaderModel.setBrand(row.getCell(i++).getStringCellValue().trim());
87
            itemLoaderModel.setModelNumber(row.getCell(i++).getStringCellValue().trim());
32959 amit.gupta 88
            Cell modelCell = row.getCell(i++);
89
            if (modelCell == null) {
90
                itemLoaderModel.setModelName("");
91
            } else {
33037 amit.gupta 92
                itemLoaderModel.setModelName(modelCell.getStringCellValue().trim());
32959 amit.gupta 93
            }
33037 amit.gupta 94
            itemLoaderModel.setColor(row.getCell(i++).getStringCellValue().trim());
32952 amit.gupta 95
            //MRP	SP	MOP	DP	TP	NLC	Startdate	Preferred Vendor	Risky
96
            // Weight	Item type(1 for Serialized and 2 for non-serialized)	TaxRate
97
            itemLoaderModel.setMrp(row.getCell(i++).getNumericCellValue());
98
            itemLoaderModel.setSp(row.getCell(i++).getNumericCellValue());
99
            itemLoaderModel.setMop(row.getCell(i++).getNumericCellValue());
100
            itemLoaderModel.setDp(row.getCell(i++).getNumericCellValue());
101
            itemLoaderModel.setTp(row.getCell(i++).getNumericCellValue());
102
            itemLoaderModel.setNlc(row.getCell(i++).getNumericCellValue());
103
            itemLoaderModel.setStartDate(Utils.convertToLocalDateTime(row.getCell(i++).getDateCellValue()));
104
            itemLoaderModel.setPreferredVendor((int) (row.getCell(i++).getNumericCellValue()));
105
            itemLoaderModel.setRisky(row.getCell(i++).getNumericCellValue() == 0 ? false : true);
106
            itemLoaderModel.setWeight(row.getCell(i++).getNumericCellValue());
107
            itemLoaderModel.setItemType((int) row.getCell(i++).getNumericCellValue());
108
            itemLoaderModel.setTaxRate(row.getCell(i++).getNumericCellValue() * 100);
109
        } catch (Throwable e) {
32963 amit.gupta 110
            LOGGER.info(e.getCause());
32952 amit.gupta 111
            throw new ProfitMandiBusinessException("Field", "Field at row - " + row.getRowNum() + ", column - " + (i - 1),
33173 amit.gupta 112
                    "Invalid field value at - " + ExcelUtils.toAlphabet(i - 1) + (row.getRowNum() + 1) + ", " + ExcelUtils.getCellValue(row.getCell(i - 1)));
32952 amit.gupta 113
        }
114
        return itemLoaderModel;
115
    }
116
 
117
    @Autowired
118
    SupplierRepository supplierRepository;
119
 
120
    private void validateItemLoaderModel(ItemLoaderModel itemLoaderModel) throws ProfitMandiBusinessException {
121
        if (itemLoaderModel.getTaxRate() > 28)
122
            throw new ProfitMandiBusinessException("Tax Rate should not be above 28%", "", "");
123
        if (itemLoaderModel.getMrp() < itemLoaderModel.getSp())
124
            throw new ProfitMandiBusinessException("Selling Price should not be greater than MRP", "", "");
125
        Supplier supplier = supplierRepository.selectById(itemLoaderModel.getPreferredVendor());
126
        if (supplier == null) throw new ProfitMandiBusinessException("Invalid Supplier", "", "");
127
 
128
    }
129
 
130
 
33172 tejus.loha 131
 
32952 amit.gupta 132
    //@Autowired
133
 
134
    private void addItem(ItemLoaderModel itemLoaderModel) throws ProfitMandiBusinessException {
135
        this.validateItemLoaderModel(itemLoaderModel);
136
        List<Item> items = itemRepository.selectAll(itemLoaderModel.getBrand(), itemLoaderModel.getModelName(), itemLoaderModel.getModelNumber());
137
        int catalogId = 0;
138
        if (items.size() > 0) {
32962 amit.gupta 139
            //Check if color already exist
33172 tejus.loha 140
            long similarColorCount = items.stream().filter(x -> itemLoaderModel.getColor().equals(x.getColorNatural())).count();
141
            if (similarColorCount > 0) {
32962 amit.gupta 142
                throw new ProfitMandiBusinessException("Color alerady exist", "Pls check", itemLoaderModel.getColor());
143
            }
144
 
32952 amit.gupta 145
            List<Integer> categoryIds = items.stream().map(x -> x.getCategoryId()).distinct().collect(Collectors.toList());
146
            if (categoryIds.size() > 1) {
147
                throw new ProfitMandiBusinessException("Model Wrongly Mapped to different categories", "Pls check", categoryIds.toString());
148
            } else if (categoryIds.get(0) != itemLoaderModel.getCategoryId()) {
149
                throw new ProfitMandiBusinessException("Model already Mapped to different category", "Pls check", categoryIds.toString());
150
            }
151
            List<Integer> catalogIds = items.stream().map(x -> x.getCategoryId()).distinct().collect(Collectors.toList());
152
            if (catalogIds.size() > 1) {
153
                throw new ProfitMandiBusinessException("Model Wrongly Mapped to different CatalogIds", "Pls check", catalogIds.toString());
154
            }
32962 amit.gupta 155
            Map<String, Long> colorsCount = items.stream().collect(Collectors.groupingBy(x -> x.getColorNatural(), Collectors.counting()));
32952 amit.gupta 156
            for (Map.Entry<String, Long> colorCountEntrySet : colorsCount.entrySet()) {
157
                if (colorCountEntrySet.getValue() > 1) {
158
                    throw new ProfitMandiBusinessException("Model repeats the same color", "Pls check", colorCountEntrySet.getKey());
159
                }
160
            }
161
            catalogId = items.get(0).getCatalogItemId();
162
        } else {
163
            catalogId = itemRepository.getNextEntity();
164
        }
165
        //Finally either got valid existing model or new model altogether
166
        Item item = new Item();
167
        item.setBrand(itemLoaderModel.getBrand());
168
        item.setModelName(itemLoaderModel.getModelName());
169
        item.setModelNumber(itemLoaderModel.getModelNumber());
170
        item.setColor(itemLoaderModel.getColor());
171
        item.setSellingPrice(new Float(Math.round(itemLoaderModel.getSp())));
172
        item.setMrp(new Float(Math.round(itemLoaderModel.getMrp())));
173
        item.setStartDate(itemLoaderModel.getStartDate());
174
        item.setCategoryId(itemLoaderModel.getCategoryId());
175
        item.setPreferredVendor(itemLoaderModel.getPreferredVendor());
176
        item.setRisky(itemLoaderModel.isRisky());
177
        item.setCatalogItemId(catalogId);
178
        if (itemLoaderModel.getWeight() <= 0) {
179
            throw new ProfitMandiBusinessException("Weight", "0", "Weight should be greater than 0");
180
        }
181
        item.setWeight(itemLoaderModel.getWeight());
182
        item.setStatus(status.PARTIALLY_ACTIVE);
183
        item.setUpdatedOn(LocalDateTime.now());
184
        item.setAddedOn(LocalDateTime.now());
185
 
186
        //if(item.getHsnCode())
187
        item.setHsnCode(itemLoaderModel.getHsnCode());
188
        ItemType itemType = ItemType.findByValue(itemLoaderModel.getItemType());
189
        if (itemType == null) {
190
            throw new ProfitMandiBusinessException("Item Type", "Invalid", "Should be 1 for SERIALIZED or 2 for Non Serialized");
191
        }
192
        item.setType(itemType);
193
        itemRepository.persist(item);
194
        //Logic for model here
195
 
196
        //Set Tax Rate
197
        StateGstRateModel stateGstRateModel = new StateGstRateModel();
198
        stateGstRateModel.setItemId(item.getId());
199
        stateGstRateModel.setIgstRate(itemLoaderModel.getTaxRate());
200
        stateGstRateRepository.addStateGstRates(Arrays.asList(stateGstRateModel));
201
 
202
        //Add Vendor
33036 amit.gupta 203
        this.addVendorItemPricing(item);
32952 amit.gupta 204
    }
33035 amit.gupta 205
 
33036 amit.gupta 206
    private void addVendorItemPricing(Item item) {
33035 amit.gupta 207
        List<VendorCatalogPricing> vendorCatalogPricingList = vendorCatalogPricingRepository.selectByCatalogId(item.getCatalogItemId());
208
        for (VendorCatalogPricing vendorCatalogPricing : vendorCatalogPricingList) {
209
            VendorItemPricing vendorItemPricing = new VendorItemPricing();
210
            vendorItemPricing.setItemId(item.getId());
211
            vendorItemPricing.setVendorId(vendorCatalogPricing.getVendorId());
212
            vendorItemPricing.setDp(vendorCatalogPricing.getDealerPrice());
213
            vendorItemPricing.setMop(vendorCatalogPricing.getMop());
214
            vendorItemPricing.setNlc(vendorCatalogPricing.getTransferPrice());
215
            vendorItemPricing.setTp(vendorCatalogPricing.getTransferPrice());
216
            vendorItemPricingRepository.persist(vendorItemPricing);
217
        }
218
    }
32952 amit.gupta 219
 
220
}