Subversion Repositories SmartDukaan

Rev

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