| 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;
|
| 37071 |
amit |
7 |
import com.spice.profitmandi.common.util.StringUtils;
|
| 32952 |
amit.gupta |
8 |
import com.spice.profitmandi.common.util.Utils;
|
| 33771 |
amit.gupta |
9 |
import com.spice.profitmandi.dao.entity.catalog.BrandCatalog;
|
|
|
10 |
import com.spice.profitmandi.dao.entity.catalog.Catalog;
|
| 35343 |
ranu |
11 |
import com.spice.profitmandi.dao.entity.catalog.CategorisedCatalog;
|
| 32952 |
amit.gupta |
12 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 33035 |
amit.gupta |
13 |
import com.spice.profitmandi.dao.entity.inventory.VendorCatalogPricing;
|
| 32952 |
amit.gupta |
14 |
import com.spice.profitmandi.dao.entity.inventory.VendorItemPricing;
|
|
|
15 |
import com.spice.profitmandi.dao.entity.warehouse.Supplier;
|
| 35343 |
ranu |
16 |
import com.spice.profitmandi.dao.enumuration.inventory.CatalogMovingEnum;
|
| 32952 |
amit.gupta |
17 |
import com.spice.profitmandi.dao.model.StateGstRateModel;
|
| 35343 |
ranu |
18 |
import com.spice.profitmandi.dao.repository.catalog.*;
|
| 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 |
|
| 35343 |
ranu |
34 |
import java.time.LocalDate;
|
| 32952 |
amit.gupta |
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()));
|
| 37071 |
amit |
94 |
itemLoaderModel.setBrand(StringUtils.normalizeWhitespace(row.getCell(i++).getStringCellValue()));
|
| 33771 |
amit.gupta |
95 |
Cell modelNumberCell = row.getCell(i++);
|
| 37071 |
amit |
96 |
if(modelNumberCell== null || StringUtils.normalizeWhitespace(modelNumberCell.getStringCellValue()).isEmpty()) {
|
| 33771 |
amit.gupta |
97 |
throw new Exception("Model Number cant be null or empty");
|
|
|
98 |
} else {
|
| 37071 |
amit |
99 |
itemLoaderModel.setModelNumber(StringUtils.normalizeWhitespace(modelNumberCell.getStringCellValue()));
|
| 33771 |
amit.gupta |
100 |
}
|
|
|
101 |
Cell modelNameCell = row.getCell(i++);
|
|
|
102 |
if (modelNameCell == null) {
|
| 32959 |
amit.gupta |
103 |
itemLoaderModel.setModelName("");
|
|
|
104 |
} else {
|
| 37071 |
amit |
105 |
itemLoaderModel.setModelName(StringUtils.normalizeWhitespace(modelNameCell.getStringCellValue()));
|
| 32959 |
amit.gupta |
106 |
}
|
| 37071 |
amit |
107 |
itemLoaderModel.setColor(StringUtils.normalizeWhitespace(row.getCell(i++).getStringCellValue()));
|
| 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 {
|
| 34663 |
aman.kumar |
134 |
if (Utils.compareDouble(itemLoaderModel.getTaxRate(), 28d) > 0)
|
| 32952 |
amit.gupta |
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 |
|
| 35343 |
ranu |
148 |
@Autowired
|
|
|
149 |
CategorisedCatalogRepository categorisedCatalogRepository;
|
|
|
150 |
|
| 32952 |
amit.gupta |
151 |
private void addItem(ItemLoaderModel itemLoaderModel) throws ProfitMandiBusinessException {
|
|
|
152 |
this.validateItemLoaderModel(itemLoaderModel);
|
| 33771 |
amit.gupta |
153 |
Catalog catalog = catalogRepository.selectCatalog(itemLoaderModel.getBrand(), itemLoaderModel.getModelNumber(), itemLoaderModel.getModelName());
|
|
|
154 |
if (catalog != null) {
|
|
|
155 |
List<Item> items = itemRepository.selectAllByCatalogItemId(catalog.getId());
|
| 33172 |
tejus.loha |
156 |
long similarColorCount = items.stream().filter(x -> itemLoaderModel.getColor().equals(x.getColorNatural())).count();
|
|
|
157 |
if (similarColorCount > 0) {
|
| 34709 |
amit.gupta |
158 |
throw new ProfitMandiBusinessException("Color already exist", "Pls check", itemLoaderModel.getColor());
|
| 32962 |
amit.gupta |
159 |
}
|
|
|
160 |
|
| 32952 |
amit.gupta |
161 |
List<Integer> categoryIds = items.stream().map(x -> x.getCategoryId()).distinct().collect(Collectors.toList());
|
|
|
162 |
if (categoryIds.size() > 1) {
|
|
|
163 |
throw new ProfitMandiBusinessException("Model Wrongly Mapped to different categories", "Pls check", categoryIds.toString());
|
|
|
164 |
} else if (categoryIds.get(0) != itemLoaderModel.getCategoryId()) {
|
|
|
165 |
throw new ProfitMandiBusinessException("Model already Mapped to different category", "Pls check", categoryIds.toString());
|
|
|
166 |
}
|
|
|
167 |
List<Integer> catalogIds = items.stream().map(x -> x.getCategoryId()).distinct().collect(Collectors.toList());
|
|
|
168 |
if (catalogIds.size() > 1) {
|
|
|
169 |
throw new ProfitMandiBusinessException("Model Wrongly Mapped to different CatalogIds", "Pls check", catalogIds.toString());
|
|
|
170 |
}
|
| 32962 |
amit.gupta |
171 |
Map<String, Long> colorsCount = items.stream().collect(Collectors.groupingBy(x -> x.getColorNatural(), Collectors.counting()));
|
| 32952 |
amit.gupta |
172 |
for (Map.Entry<String, Long> colorCountEntrySet : colorsCount.entrySet()) {
|
|
|
173 |
if (colorCountEntrySet.getValue() > 1) {
|
|
|
174 |
throw new ProfitMandiBusinessException("Model repeats the same color", "Pls check", colorCountEntrySet.getKey());
|
|
|
175 |
}
|
|
|
176 |
}
|
| 35343 |
ranu |
177 |
|
|
|
178 |
CategorisedCatalog categorisedCatalog = categorisedCatalogRepository.getCurrentCatalogMovement(catalog.getId());
|
|
|
179 |
if (categorisedCatalog == null) {
|
|
|
180 |
CategorisedCatalog categorisedCatalogNew = new CategorisedCatalog();
|
|
|
181 |
categorisedCatalogNew.setCatalogId(catalog.getId());
|
|
|
182 |
categorisedCatalogNew.setStatus(CatalogMovingEnum.RUNNING);
|
|
|
183 |
categorisedCatalogNew.setStartDate(LocalDate.now());
|
|
|
184 |
categorisedCatalogRepository.persist(categorisedCatalogNew);
|
|
|
185 |
} else if (categorisedCatalog.getStatus().equals(CatalogMovingEnum.OTHER)) {
|
|
|
186 |
categorisedCatalog.setStatus(CatalogMovingEnum.SLOWMOVING);
|
|
|
187 |
}
|
|
|
188 |
|
| 32952 |
amit.gupta |
189 |
} else {
|
| 33771 |
amit.gupta |
190 |
catalog = new Catalog();
|
|
|
191 |
BrandCatalog brand = brandsRepository.selectByBrand(itemLoaderModel.getBrand());
|
|
|
192 |
if(brand == null) {
|
|
|
193 |
throw new ProfitMandiBusinessException("Brand Does not exist", "Pls check", itemLoaderModel.getBrand());
|
|
|
194 |
}
|
|
|
195 |
catalog.setBrandId(brand.getId());
|
|
|
196 |
catalog.setBrand(itemLoaderModel.getBrand());
|
|
|
197 |
catalog.setModelName(itemLoaderModel.getModelName());
|
|
|
198 |
catalog.setModelNumber(itemLoaderModel.getModelNumber());
|
| 33888 |
ranu |
199 |
catalog.setCategoryId(itemLoaderModel.getCategoryId());
|
| 33771 |
amit.gupta |
200 |
catalog.setCreated(LocalDateTime.now());
|
|
|
201 |
catalog.setCreatedBy("System");
|
|
|
202 |
catalogRepository.persist(catalog);
|
| 35343 |
ranu |
203 |
|
|
|
204 |
|
|
|
205 |
CategorisedCatalog categorisedCatalog = new CategorisedCatalog();
|
|
|
206 |
categorisedCatalog.setCatalogId(catalog.getId());
|
|
|
207 |
categorisedCatalog.setStatus(CatalogMovingEnum.RUNNING);
|
|
|
208 |
categorisedCatalog.setStartDate(LocalDate.now());
|
|
|
209 |
categorisedCatalogRepository.persist(categorisedCatalog);
|
| 32952 |
amit.gupta |
210 |
}
|
|
|
211 |
//Finally either got valid existing model or new model altogether
|
|
|
212 |
Item item = new Item();
|
|
|
213 |
item.setBrand(itemLoaderModel.getBrand());
|
|
|
214 |
item.setModelName(itemLoaderModel.getModelName());
|
|
|
215 |
item.setModelNumber(itemLoaderModel.getModelNumber());
|
|
|
216 |
item.setColor(itemLoaderModel.getColor());
|
|
|
217 |
item.setSellingPrice(new Float(Math.round(itemLoaderModel.getSp())));
|
|
|
218 |
item.setMrp(new Float(Math.round(itemLoaderModel.getMrp())));
|
|
|
219 |
item.setStartDate(itemLoaderModel.getStartDate());
|
|
|
220 |
item.setCategoryId(itemLoaderModel.getCategoryId());
|
|
|
221 |
item.setPreferredVendor(itemLoaderModel.getPreferredVendor());
|
|
|
222 |
item.setRisky(itemLoaderModel.isRisky());
|
| 33771 |
amit.gupta |
223 |
item.setCatalogItemId(catalog.getId());
|
| 32952 |
amit.gupta |
224 |
if (itemLoaderModel.getWeight() <= 0) {
|
|
|
225 |
throw new ProfitMandiBusinessException("Weight", "0", "Weight should be greater than 0");
|
|
|
226 |
}
|
|
|
227 |
item.setWeight(itemLoaderModel.getWeight());
|
|
|
228 |
item.setStatus(status.PARTIALLY_ACTIVE);
|
|
|
229 |
item.setUpdatedOn(LocalDateTime.now());
|
|
|
230 |
item.setAddedOn(LocalDateTime.now());
|
|
|
231 |
|
|
|
232 |
//if(item.getHsnCode())
|
|
|
233 |
item.setHsnCode(itemLoaderModel.getHsnCode());
|
|
|
234 |
ItemType itemType = ItemType.findByValue(itemLoaderModel.getItemType());
|
|
|
235 |
if (itemType == null) {
|
|
|
236 |
throw new ProfitMandiBusinessException("Item Type", "Invalid", "Should be 1 for SERIALIZED or 2 for Non Serialized");
|
|
|
237 |
}
|
|
|
238 |
item.setType(itemType);
|
|
|
239 |
itemRepository.persist(item);
|
|
|
240 |
//Logic for model here
|
|
|
241 |
|
|
|
242 |
//Set Tax Rate
|
|
|
243 |
StateGstRateModel stateGstRateModel = new StateGstRateModel();
|
|
|
244 |
stateGstRateModel.setItemId(item.getId());
|
|
|
245 |
stateGstRateModel.setIgstRate(itemLoaderModel.getTaxRate());
|
|
|
246 |
stateGstRateRepository.addStateGstRates(Arrays.asList(stateGstRateModel));
|
|
|
247 |
|
|
|
248 |
//Add Vendor
|
| 33036 |
amit.gupta |
249 |
this.addVendorItemPricing(item);
|
| 32952 |
amit.gupta |
250 |
}
|
| 33035 |
amit.gupta |
251 |
|
| 33036 |
amit.gupta |
252 |
private void addVendorItemPricing(Item item) {
|
| 33035 |
amit.gupta |
253 |
List<VendorCatalogPricing> vendorCatalogPricingList = vendorCatalogPricingRepository.selectByCatalogId(item.getCatalogItemId());
|
|
|
254 |
for (VendorCatalogPricing vendorCatalogPricing : vendorCatalogPricingList) {
|
|
|
255 |
VendorItemPricing vendorItemPricing = new VendorItemPricing();
|
|
|
256 |
vendorItemPricing.setItemId(item.getId());
|
|
|
257 |
vendorItemPricing.setVendorId(vendorCatalogPricing.getVendorId());
|
|
|
258 |
vendorItemPricing.setDp(vendorCatalogPricing.getDealerPrice());
|
|
|
259 |
vendorItemPricing.setMop(vendorCatalogPricing.getMop());
|
|
|
260 |
vendorItemPricing.setNlc(vendorCatalogPricing.getTransferPrice());
|
|
|
261 |
vendorItemPricing.setTp(vendorCatalogPricing.getTransferPrice());
|
|
|
262 |
vendorItemPricingRepository.persist(vendorItemPricing);
|
|
|
263 |
}
|
|
|
264 |
}
|
| 32952 |
amit.gupta |
265 |
|
|
|
266 |
}
|