Subversion Repositories SmartDukaan

Rev

Rev 33172 | Rev 33346 | 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 {
66
                this.addItem(itemLoaderModel);
67
            } catch (ProfitMandiBusinessException e) {
68
                throw new ProfitMandiBusinessException(e.getRejectedType(), e.getRejectedValue(), e.getCode() + "at " + itemLoaderModel.getRowIndex());
69
            }
70
        }
71
    }
72
 
73
 
74
 
75
 
76
    private ItemLoaderModel createLoaderModel(XSSFRow row) throws ProfitMandiBusinessException {
77
        ItemLoaderModel itemLoaderModel = new ItemLoaderModel();
78
        int i = 0;
79
        itemLoaderModel.setRowIndex(row.getRowNum());
80
        try {
33037 amit.gupta 81
            itemLoaderModel.setProductGroup(row.getCell(i++).getStringCellValue().trim());
32952 amit.gupta 82
            itemLoaderModel.setCategoryId((int) row.getCell(i++).getNumericCellValue());
33037 amit.gupta 83
            itemLoaderModel.setCategory(row.getCell(i++).getStringCellValue().trim());
32952 amit.gupta 84
            itemLoaderModel.setHsnCode(String.valueOf((int) row.getCell(i++).getNumericCellValue()));
33037 amit.gupta 85
            itemLoaderModel.setBrand(row.getCell(i++).getStringCellValue().trim());
86
            itemLoaderModel.setModelNumber(row.getCell(i++).getStringCellValue().trim());
32959 amit.gupta 87
            Cell modelCell = row.getCell(i++);
88
            if (modelCell == null) {
89
                itemLoaderModel.setModelName("");
90
            } else {
33037 amit.gupta 91
                itemLoaderModel.setModelName(modelCell.getStringCellValue().trim());
32959 amit.gupta 92
            }
33037 amit.gupta 93
            itemLoaderModel.setColor(row.getCell(i++).getStringCellValue().trim());
32952 amit.gupta 94
            //MRP	SP	MOP	DP	TP	NLC	Startdate	Preferred Vendor	Risky
95
            // Weight	Item type(1 for Serialized and 2 for non-serialized)	TaxRate
96
            itemLoaderModel.setMrp(row.getCell(i++).getNumericCellValue());
97
            itemLoaderModel.setSp(row.getCell(i++).getNumericCellValue());
98
            itemLoaderModel.setMop(row.getCell(i++).getNumericCellValue());
99
            itemLoaderModel.setDp(row.getCell(i++).getNumericCellValue());
100
            itemLoaderModel.setTp(row.getCell(i++).getNumericCellValue());
101
            itemLoaderModel.setNlc(row.getCell(i++).getNumericCellValue());
102
            itemLoaderModel.setStartDate(Utils.convertToLocalDateTime(row.getCell(i++).getDateCellValue()));
103
            itemLoaderModel.setPreferredVendor((int) (row.getCell(i++).getNumericCellValue()));
104
            itemLoaderModel.setRisky(row.getCell(i++).getNumericCellValue() == 0 ? false : true);
105
            itemLoaderModel.setWeight(row.getCell(i++).getNumericCellValue());
106
            itemLoaderModel.setItemType((int) row.getCell(i++).getNumericCellValue());
107
            itemLoaderModel.setTaxRate(row.getCell(i++).getNumericCellValue() * 100);
108
        } catch (Throwable e) {
32963 amit.gupta 109
            LOGGER.info(e.getCause());
32952 amit.gupta 110
            throw new ProfitMandiBusinessException("Field", "Field at row - " + row.getRowNum() + ", column - " + (i - 1),
33173 amit.gupta 111
                    "Invalid field value at - " + ExcelUtils.toAlphabet(i - 1) + (row.getRowNum() + 1) + ", " + ExcelUtils.getCellValue(row.getCell(i - 1)));
32952 amit.gupta 112
        }
113
        return itemLoaderModel;
114
    }
115
 
116
    @Autowired
117
    SupplierRepository supplierRepository;
118
 
119
    private void validateItemLoaderModel(ItemLoaderModel itemLoaderModel) throws ProfitMandiBusinessException {
120
        if (itemLoaderModel.getTaxRate() > 28)
121
            throw new ProfitMandiBusinessException("Tax Rate should not be above 28%", "", "");
122
        if (itemLoaderModel.getMrp() < itemLoaderModel.getSp())
123
            throw new ProfitMandiBusinessException("Selling Price should not be greater than MRP", "", "");
124
        Supplier supplier = supplierRepository.selectById(itemLoaderModel.getPreferredVendor());
125
        if (supplier == null) throw new ProfitMandiBusinessException("Invalid Supplier", "", "");
126
 
127
    }
128
 
129
 
33172 tejus.loha 130
 
32952 amit.gupta 131
    //@Autowired
132
 
133
    private void addItem(ItemLoaderModel itemLoaderModel) throws ProfitMandiBusinessException {
134
        this.validateItemLoaderModel(itemLoaderModel);
135
        List<Item> items = itemRepository.selectAll(itemLoaderModel.getBrand(), itemLoaderModel.getModelName(), itemLoaderModel.getModelNumber());
136
        int catalogId = 0;
137
        if (items.size() > 0) {
32962 amit.gupta 138
            //Check if color already exist
33172 tejus.loha 139
            long similarColorCount = items.stream().filter(x -> itemLoaderModel.getColor().equals(x.getColorNatural())).count();
140
            if (similarColorCount > 0) {
32962 amit.gupta 141
                throw new ProfitMandiBusinessException("Color alerady exist", "Pls check", itemLoaderModel.getColor());
142
            }
143
 
32952 amit.gupta 144
            List<Integer> categoryIds = items.stream().map(x -> x.getCategoryId()).distinct().collect(Collectors.toList());
145
            if (categoryIds.size() > 1) {
146
                throw new ProfitMandiBusinessException("Model Wrongly Mapped to different categories", "Pls check", categoryIds.toString());
147
            } else if (categoryIds.get(0) != itemLoaderModel.getCategoryId()) {
148
                throw new ProfitMandiBusinessException("Model already Mapped to different category", "Pls check", categoryIds.toString());
149
            }
150
            List<Integer> catalogIds = items.stream().map(x -> x.getCategoryId()).distinct().collect(Collectors.toList());
151
            if (catalogIds.size() > 1) {
152
                throw new ProfitMandiBusinessException("Model Wrongly Mapped to different CatalogIds", "Pls check", catalogIds.toString());
153
            }
32962 amit.gupta 154
            Map<String, Long> colorsCount = items.stream().collect(Collectors.groupingBy(x -> x.getColorNatural(), Collectors.counting()));
32952 amit.gupta 155
            for (Map.Entry<String, Long> colorCountEntrySet : colorsCount.entrySet()) {
156
                if (colorCountEntrySet.getValue() > 1) {
157
                    throw new ProfitMandiBusinessException("Model repeats the same color", "Pls check", colorCountEntrySet.getKey());
158
                }
159
            }
160
            catalogId = items.get(0).getCatalogItemId();
161
        } else {
162
            catalogId = itemRepository.getNextEntity();
163
        }
164
        //Finally either got valid existing model or new model altogether
165
        Item item = new Item();
166
        item.setBrand(itemLoaderModel.getBrand());
167
        item.setModelName(itemLoaderModel.getModelName());
168
        item.setModelNumber(itemLoaderModel.getModelNumber());
169
        item.setColor(itemLoaderModel.getColor());
170
        item.setSellingPrice(new Float(Math.round(itemLoaderModel.getSp())));
171
        item.setMrp(new Float(Math.round(itemLoaderModel.getMrp())));
172
        item.setStartDate(itemLoaderModel.getStartDate());
173
        item.setCategoryId(itemLoaderModel.getCategoryId());
174
        item.setPreferredVendor(itemLoaderModel.getPreferredVendor());
175
        item.setRisky(itemLoaderModel.isRisky());
176
        item.setCatalogItemId(catalogId);
177
        if (itemLoaderModel.getWeight() <= 0) {
178
            throw new ProfitMandiBusinessException("Weight", "0", "Weight should be greater than 0");
179
        }
180
        item.setWeight(itemLoaderModel.getWeight());
181
        item.setStatus(status.PARTIALLY_ACTIVE);
182
        item.setUpdatedOn(LocalDateTime.now());
183
        item.setAddedOn(LocalDateTime.now());
184
 
185
        //if(item.getHsnCode())
186
        item.setHsnCode(itemLoaderModel.getHsnCode());
187
        ItemType itemType = ItemType.findByValue(itemLoaderModel.getItemType());
188
        if (itemType == null) {
189
            throw new ProfitMandiBusinessException("Item Type", "Invalid", "Should be 1 for SERIALIZED or 2 for Non Serialized");
190
        }
191
        item.setType(itemType);
192
        itemRepository.persist(item);
193
        //Logic for model here
194
 
195
        //Set Tax Rate
196
        StateGstRateModel stateGstRateModel = new StateGstRateModel();
197
        stateGstRateModel.setItemId(item.getId());
198
        stateGstRateModel.setIgstRate(itemLoaderModel.getTaxRate());
199
        stateGstRateRepository.addStateGstRates(Arrays.asList(stateGstRateModel));
200
 
201
        //Add Vendor
33036 amit.gupta 202
        this.addVendorItemPricing(item);
32952 amit.gupta 203
    }
33035 amit.gupta 204
 
33036 amit.gupta 205
    private void addVendorItemPricing(Item item) {
33035 amit.gupta 206
        List<VendorCatalogPricing> vendorCatalogPricingList = vendorCatalogPricingRepository.selectByCatalogId(item.getCatalogItemId());
207
        for (VendorCatalogPricing vendorCatalogPricing : vendorCatalogPricingList) {
208
            VendorItemPricing vendorItemPricing = new VendorItemPricing();
209
            vendorItemPricing.setItemId(item.getId());
210
            vendorItemPricing.setVendorId(vendorCatalogPricing.getVendorId());
211
            vendorItemPricing.setDp(vendorCatalogPricing.getDealerPrice());
212
            vendorItemPricing.setMop(vendorCatalogPricing.getMop());
213
            vendorItemPricing.setNlc(vendorCatalogPricing.getTransferPrice());
214
            vendorItemPricing.setTp(vendorCatalogPricing.getTransferPrice());
215
            vendorItemPricingRepository.persist(vendorItemPricing);
216
        }
217
    }
32952 amit.gupta 218
 
219
}