Subversion Repositories SmartDukaan

Rev

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