Subversion Repositories SmartDukaan

Rev

Rev 33346 | Rev 33888 | 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;
33771 amit.gupta 11
import com.spice.profitmandi.dao.entity.dtr.Brand;
33035 amit.gupta 12
import com.spice.profitmandi.dao.entity.inventory.VendorCatalogPricing;
32952 amit.gupta 13
import com.spice.profitmandi.dao.entity.inventory.VendorItemPricing;
14
import com.spice.profitmandi.dao.entity.warehouse.Supplier;
15
import com.spice.profitmandi.dao.model.StateGstRateModel;
33771 amit.gupta 16
import com.spice.profitmandi.dao.repository.catalog.BrandsRepository;
17
import com.spice.profitmandi.dao.repository.catalog.CatalogRepository;
32952 amit.gupta 18
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
19
import com.spice.profitmandi.dao.repository.catalog.StateGstRateRepository;
33035 amit.gupta 20
import com.spice.profitmandi.dao.repository.inventory.VendorCatalogPricingRepository;
32952 amit.gupta 21
import com.spice.profitmandi.dao.repository.inventory.VendorItemPricingRepository;
22
import com.spice.profitmandi.dao.repository.warehouse.SupplierRepository;
23
import in.shop2020.model.v1.catalog.status;
24
import org.apache.logging.log4j.LogManager;
25
import org.apache.logging.log4j.Logger;
26
import org.apache.poi.ss.usermodel.Cell;
27
import org.apache.poi.ss.usermodel.Row;
28
import org.apache.poi.xssf.usermodel.XSSFRow;
29
import org.apache.poi.xssf.usermodel.XSSFSheet;
30
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
31
import org.springframework.beans.factory.annotation.Autowired;
32
import org.springframework.stereotype.Component;
33
import org.springframework.web.multipart.MultipartFile;
34
 
35
import java.time.LocalDateTime;
36
import java.util.Arrays;
37
import java.util.List;
38
import java.util.Map;
39
import java.util.stream.Collectors;
40
 
41
 
42
@Component
43
public class ItemLoaderService {
44
 
45
    @Autowired
33771 amit.gupta 46
    BrandsRepository brandsRepository;
47
 
48
    @Autowired
32952 amit.gupta 49
    ItemRepository itemRepository;
50
 
51
    @Autowired
33035 amit.gupta 52
    VendorCatalogPricingRepository vendorCatalogPricingRepository;
53
 
54
    @Autowired
32952 amit.gupta 55
    StateGstRateRepository stateGstRateRepository;
56
 
57
    @Autowired
58
    VendorItemPricingRepository vendorItemPricingRepository;
59
 
33172 tejus.loha 60
 
32952 amit.gupta 61
    private static final Logger LOGGER = LogManager.getLogger(ItemLoaderService.class);
62
 
63
    public void parse(MultipartFile file) throws Exception {
64
        XSSFWorkbook myWorkBook = new XSSFWorkbook(file.getInputStream());
65
 
66
        myWorkBook.setMissingCellPolicy(Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
67
        // Return first sheet from the XLSX workbook
68
        XSSFSheet mySheet = myWorkBook.getSheetAt(0);
69
        LOGGER.info("rowCellNum {}", mySheet.getLastRowNum());
70
        for (int rowNumber = 1; rowNumber <= mySheet.getLastRowNum(); rowNumber++) {
71
            XSSFRow row = mySheet.getRow(rowNumber);
72
            ItemLoaderModel itemLoaderModel = this.createLoaderModel(row);
73
            try {
33346 amit.gupta 74
                LOGGER.info("rownum - {}", rowNumber);
32952 amit.gupta 75
                this.addItem(itemLoaderModel);
76
            } catch (ProfitMandiBusinessException e) {
77
                throw new ProfitMandiBusinessException(e.getRejectedType(), e.getRejectedValue(), e.getCode() + "at " + itemLoaderModel.getRowIndex());
78
            }
79
        }
80
    }
81
 
82
 
83
 
84
 
85
    private ItemLoaderModel createLoaderModel(XSSFRow row) throws ProfitMandiBusinessException {
86
        ItemLoaderModel itemLoaderModel = new ItemLoaderModel();
87
        int i = 0;
88
        itemLoaderModel.setRowIndex(row.getRowNum());
89
        try {
33037 amit.gupta 90
            itemLoaderModel.setProductGroup(row.getCell(i++).getStringCellValue().trim());
32952 amit.gupta 91
            itemLoaderModel.setCategoryId((int) row.getCell(i++).getNumericCellValue());
33037 amit.gupta 92
            itemLoaderModel.setCategory(row.getCell(i++).getStringCellValue().trim());
32952 amit.gupta 93
            itemLoaderModel.setHsnCode(String.valueOf((int) row.getCell(i++).getNumericCellValue()));
33037 amit.gupta 94
            itemLoaderModel.setBrand(row.getCell(i++).getStringCellValue().trim());
33771 amit.gupta 95
            Cell modelNumberCell = row.getCell(i++);
96
            if(modelNumberCell== null || modelNumberCell.getStringCellValue().trim().equals("")) {
97
                throw new Exception("Model Number cant be null or empty");
98
            } else {
99
                itemLoaderModel.setModelNumber(modelNumberCell.getStringCellValue().trim());
100
            }
101
            Cell modelNameCell = row.getCell(i++);
102
            if (modelNameCell == null) {
32959 amit.gupta 103
                itemLoaderModel.setModelName("");
104
            } else {
33771 amit.gupta 105
                itemLoaderModel.setModelName(modelNameCell.getStringCellValue().trim());
32959 amit.gupta 106
            }
33037 amit.gupta 107
            itemLoaderModel.setColor(row.getCell(i++).getStringCellValue().trim());
32952 amit.gupta 108
            //MRP	SP	MOP	DP	TP	NLC	Startdate	Preferred Vendor	Risky
109
            // Weight	Item type(1 for Serialized and 2 for non-serialized)	TaxRate
110
            itemLoaderModel.setMrp(row.getCell(i++).getNumericCellValue());
111
            itemLoaderModel.setSp(row.getCell(i++).getNumericCellValue());
112
            itemLoaderModel.setMop(row.getCell(i++).getNumericCellValue());
113
            itemLoaderModel.setDp(row.getCell(i++).getNumericCellValue());
114
            itemLoaderModel.setTp(row.getCell(i++).getNumericCellValue());
115
            itemLoaderModel.setNlc(row.getCell(i++).getNumericCellValue());
116
            itemLoaderModel.setStartDate(Utils.convertToLocalDateTime(row.getCell(i++).getDateCellValue()));
117
            itemLoaderModel.setPreferredVendor((int) (row.getCell(i++).getNumericCellValue()));
118
            itemLoaderModel.setRisky(row.getCell(i++).getNumericCellValue() == 0 ? false : true);
119
            itemLoaderModel.setWeight(row.getCell(i++).getNumericCellValue());
120
            itemLoaderModel.setItemType((int) row.getCell(i++).getNumericCellValue());
121
            itemLoaderModel.setTaxRate(row.getCell(i++).getNumericCellValue() * 100);
122
        } catch (Throwable e) {
32963 amit.gupta 123
            LOGGER.info(e.getCause());
32952 amit.gupta 124
            throw new ProfitMandiBusinessException("Field", "Field at row - " + row.getRowNum() + ", column - " + (i - 1),
33173 amit.gupta 125
                    "Invalid field value at - " + ExcelUtils.toAlphabet(i - 1) + (row.getRowNum() + 1) + ", " + ExcelUtils.getCellValue(row.getCell(i - 1)));
32952 amit.gupta 126
        }
127
        return itemLoaderModel;
128
    }
129
 
130
    @Autowired
131
    SupplierRepository supplierRepository;
132
 
133
    private void validateItemLoaderModel(ItemLoaderModel itemLoaderModel) throws ProfitMandiBusinessException {
134
        if (itemLoaderModel.getTaxRate() > 28)
135
            throw new ProfitMandiBusinessException("Tax Rate should not be above 28%", "", "");
136
        if (itemLoaderModel.getMrp() < itemLoaderModel.getSp())
137
            throw new ProfitMandiBusinessException("Selling Price should not be greater than MRP", "", "");
138
        Supplier supplier = supplierRepository.selectById(itemLoaderModel.getPreferredVendor());
139
        if (supplier == null) throw new ProfitMandiBusinessException("Invalid Supplier", "", "");
140
 
141
    }
142
 
143
 
33172 tejus.loha 144
 
33771 amit.gupta 145
    @Autowired
146
    CatalogRepository catalogRepository;
32952 amit.gupta 147
 
148
    private void addItem(ItemLoaderModel itemLoaderModel) throws ProfitMandiBusinessException {
149
        this.validateItemLoaderModel(itemLoaderModel);
33771 amit.gupta 150
        Catalog catalog = catalogRepository.selectCatalog(itemLoaderModel.getBrand(), itemLoaderModel.getModelNumber(), itemLoaderModel.getModelName());
151
        if (catalog != null) {
152
            List<Item> items = itemRepository.selectAllByCatalogItemId(catalog.getId());
33172 tejus.loha 153
            long similarColorCount = items.stream().filter(x -> itemLoaderModel.getColor().equals(x.getColorNatural())).count();
154
            if (similarColorCount > 0) {
32962 amit.gupta 155
                throw new ProfitMandiBusinessException("Color alerady exist", "Pls check", itemLoaderModel.getColor());
156
            }
157
 
32952 amit.gupta 158
            List<Integer> categoryIds = items.stream().map(x -> x.getCategoryId()).distinct().collect(Collectors.toList());
159
            if (categoryIds.size() > 1) {
160
                throw new ProfitMandiBusinessException("Model Wrongly Mapped to different categories", "Pls check", categoryIds.toString());
161
            } else if (categoryIds.get(0) != itemLoaderModel.getCategoryId()) {
162
                throw new ProfitMandiBusinessException("Model already Mapped to different category", "Pls check", categoryIds.toString());
163
            }
164
            List<Integer> catalogIds = items.stream().map(x -> x.getCategoryId()).distinct().collect(Collectors.toList());
165
            if (catalogIds.size() > 1) {
166
                throw new ProfitMandiBusinessException("Model Wrongly Mapped to different CatalogIds", "Pls check", catalogIds.toString());
167
            }
32962 amit.gupta 168
            Map<String, Long> colorsCount = items.stream().collect(Collectors.groupingBy(x -> x.getColorNatural(), Collectors.counting()));
32952 amit.gupta 169
            for (Map.Entry<String, Long> colorCountEntrySet : colorsCount.entrySet()) {
170
                if (colorCountEntrySet.getValue() > 1) {
171
                    throw new ProfitMandiBusinessException("Model repeats the same color", "Pls check", colorCountEntrySet.getKey());
172
                }
173
            }
174
        } else {
33771 amit.gupta 175
            catalog = new Catalog();
176
            BrandCatalog brand = brandsRepository.selectByBrand(itemLoaderModel.getBrand());
177
            if(brand == null) {
178
                    throw new ProfitMandiBusinessException("Brand Does not exist", "Pls check", itemLoaderModel.getBrand());
179
            }
180
            catalog.setBrandId(brand.getId());
181
            catalog.setBrand(itemLoaderModel.getBrand());
182
            catalog.setModelName(itemLoaderModel.getModelName());
183
            catalog.setModelNumber(itemLoaderModel.getModelNumber());
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
}