| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.service.catalog;
|
1 |
package com.spice.profitmandi.service.catalog;
|
| 2 |
|
2 |
|
| 3 |
|
3 |
|
| 4 |
import com.spice.profitmandi.common.enumuration.ItemType;
|
4 |
import com.spice.profitmandi.common.enumuration.ItemType;
|
| 5 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
5 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| - |
|
6 |
import com.spice.profitmandi.common.util.ExcelUtils;
|
| 6 |
import com.spice.profitmandi.common.util.Utils;
|
7 |
import com.spice.profitmandi.common.util.Utils;
|
| 7 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
8 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 8 |
import com.spice.profitmandi.dao.entity.inventory.VendorCatalogPricing;
|
9 |
import com.spice.profitmandi.dao.entity.inventory.VendorCatalogPricing;
|
| 9 |
import com.spice.profitmandi.dao.entity.inventory.VendorItemPricing;
|
10 |
import com.spice.profitmandi.dao.entity.inventory.VendorItemPricing;
|
| 10 |
import com.spice.profitmandi.dao.entity.warehouse.Supplier;
|
11 |
import com.spice.profitmandi.dao.entity.warehouse.Supplier;
|
| 11 |
import com.spice.profitmandi.dao.model.StateGstRateModel;
|
12 |
import com.spice.profitmandi.dao.model.StateGstRateModel;
|
| 12 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
13 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
| 13 |
import com.spice.profitmandi.dao.repository.catalog.StateGstRateRepository;
|
14 |
import com.spice.profitmandi.dao.repository.catalog.StateGstRateRepository;
|
| 14 |
import com.spice.profitmandi.dao.repository.inventory.VendorCatalogPricingLogRepository;
|
- |
|
| 15 |
import com.spice.profitmandi.dao.repository.inventory.VendorCatalogPricingRepository;
|
15 |
import com.spice.profitmandi.dao.repository.inventory.VendorCatalogPricingRepository;
|
| 16 |
import com.spice.profitmandi.dao.repository.inventory.VendorItemPricingRepository;
|
16 |
import com.spice.profitmandi.dao.repository.inventory.VendorItemPricingRepository;
|
| 17 |
import com.spice.profitmandi.dao.repository.warehouse.SupplierRepository;
|
17 |
import com.spice.profitmandi.dao.repository.warehouse.SupplierRepository;
|
| 18 |
import com.spice.profitmandi.service.inventory.VendorCatalogPricingService;
|
- |
|
| 19 |
import in.shop2020.model.v1.catalog.status;
|
18 |
import in.shop2020.model.v1.catalog.status;
|
| 20 |
import org.apache.logging.log4j.LogManager;
|
19 |
import org.apache.logging.log4j.LogManager;
|
| 21 |
import org.apache.logging.log4j.Logger;
|
20 |
import org.apache.logging.log4j.Logger;
|
| 22 |
import org.apache.poi.ss.usermodel.Cell;
|
21 |
import org.apache.poi.ss.usermodel.Cell;
|
| 23 |
import org.apache.poi.ss.usermodel.DateUtil;
|
- |
|
| 24 |
import org.apache.poi.ss.usermodel.Row;
|
22 |
import org.apache.poi.ss.usermodel.Row;
|
| 25 |
import org.apache.poi.xssf.usermodel.XSSFRow;
|
23 |
import org.apache.poi.xssf.usermodel.XSSFRow;
|
| 26 |
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
24 |
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
| 27 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
25 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
| 28 |
import org.springframework.beans.factory.annotation.Autowired;
|
26 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 29 |
import org.springframework.stereotype.Component;
|
27 |
import org.springframework.stereotype.Component;
|
| 30 |
import org.springframework.web.multipart.MultipartFile;
|
28 |
import org.springframework.web.multipart.MultipartFile;
|
| 31 |
|
29 |
|
| 32 |
import java.io.FileInputStream;
|
- |
|
| 33 |
import java.time.LocalDateTime;
|
30 |
import java.time.LocalDateTime;
|
| 34 |
import java.util.Arrays;
|
31 |
import java.util.Arrays;
|
| 35 |
import java.util.List;
|
32 |
import java.util.List;
|
| 36 |
import java.util.Map;
|
33 |
import java.util.Map;
|
| 37 |
import java.util.stream.Collectors;
|
34 |
import java.util.stream.Collectors;
|
| 38 |
|
35 |
|
| 39 |
|
36 |
|
| 40 |
@Component
|
37 |
@Component
|
| 41 |
public class ItemLoaderService {
|
38 |
public class ItemLoaderService {
|
| 42 |
|
39 |
|
| 43 |
private static List<Character> ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".chars().mapToObj(c -> (char) c).collect(Collectors.toList());
|
- |
|
| 44 |
@Autowired
|
40 |
@Autowired
|
| 45 |
ItemRepository itemRepository;
|
41 |
ItemRepository itemRepository;
|
| 46 |
|
42 |
|
| 47 |
@Autowired
|
43 |
@Autowired
|
| 48 |
VendorCatalogPricingRepository vendorCatalogPricingRepository;
|
44 |
VendorCatalogPricingRepository vendorCatalogPricingRepository;
|
| Line 51... |
Line 47... |
| 51 |
StateGstRateRepository stateGstRateRepository;
|
47 |
StateGstRateRepository stateGstRateRepository;
|
| 52 |
|
48 |
|
| 53 |
@Autowired
|
49 |
@Autowired
|
| 54 |
VendorItemPricingRepository vendorItemPricingRepository;
|
50 |
VendorItemPricingRepository vendorItemPricingRepository;
|
| 55 |
|
51 |
|
| - |
|
52 |
@Autowired
|
| - |
|
53 |
ExcelUtils excelUtils;
|
| - |
|
54 |
|
| 56 |
private static final Logger LOGGER = LogManager.getLogger(ItemLoaderService.class);
|
55 |
private static final Logger LOGGER = LogManager.getLogger(ItemLoaderService.class);
|
| 57 |
|
56 |
|
| 58 |
public void parse(MultipartFile file) throws Exception {
|
57 |
public void parse(MultipartFile file) throws Exception {
|
| 59 |
XSSFWorkbook myWorkBook = new XSSFWorkbook(file.getInputStream());
|
58 |
XSSFWorkbook myWorkBook = new XSSFWorkbook(file.getInputStream());
|
| 60 |
|
59 |
|
| Line 72... |
Line 71... |
| 72 |
}
|
71 |
}
|
| 73 |
}
|
72 |
}
|
| 74 |
}
|
73 |
}
|
| 75 |
|
74 |
|
| 76 |
|
75 |
|
| 77 |
private String toAlphabet(int number) {
|
- |
|
| 78 |
StringBuffer sb = new StringBuffer();
|
- |
|
| 79 |
boolean loop = true;
|
- |
|
| 80 |
while (loop) {
|
- |
|
| 81 |
sb.append(ALPHABETS.get(number % 26));
|
- |
|
| 82 |
number = number / 26;
|
- |
|
| 83 |
loop = number > 0;
|
- |
|
| 84 |
|
76 |
|
| 85 |
}
|
- |
|
| 86 |
return sb.reverse().toString();
|
- |
|
| 87 |
}
|
- |
|
| 88 |
|
77 |
|
| 89 |
private ItemLoaderModel createLoaderModel(XSSFRow row) throws ProfitMandiBusinessException {
|
78 |
private ItemLoaderModel createLoaderModel(XSSFRow row) throws ProfitMandiBusinessException {
|
| 90 |
ItemLoaderModel itemLoaderModel = new ItemLoaderModel();
|
79 |
ItemLoaderModel itemLoaderModel = new ItemLoaderModel();
|
| 91 |
int i = 0;
|
80 |
int i = 0;
|
| 92 |
itemLoaderModel.setRowIndex(row.getRowNum());
|
81 |
itemLoaderModel.setRowIndex(row.getRowNum());
|
| Line 119... |
Line 108... |
| 119 |
itemLoaderModel.setItemType((int) row.getCell(i++).getNumericCellValue());
|
108 |
itemLoaderModel.setItemType((int) row.getCell(i++).getNumericCellValue());
|
| 120 |
itemLoaderModel.setTaxRate(row.getCell(i++).getNumericCellValue() * 100);
|
109 |
itemLoaderModel.setTaxRate(row.getCell(i++).getNumericCellValue() * 100);
|
| 121 |
} catch (Throwable e) {
|
110 |
} catch (Throwable e) {
|
| 122 |
LOGGER.info(e.getCause());
|
111 |
LOGGER.info(e.getCause());
|
| 123 |
throw new ProfitMandiBusinessException("Field", "Field at row - " + row.getRowNum() + ", column - " + (i - 1),
|
112 |
throw new ProfitMandiBusinessException("Field", "Field at row - " + row.getRowNum() + ", column - " + (i - 1),
|
| 124 |
"Invalid field value at - " + this.toAlphabet(i - 1) + (row.getRowNum() + 1) + ", " + this.getCellValue(row.getCell(i - 1)));
|
113 |
"Invalid field value at - " + excelUtils.toAlphabet(i - 1) + (row.getRowNum() + 1) + ", " + excelUtils.getCellValue(row.getCell(i - 1)));
|
| 125 |
}
|
114 |
}
|
| 126 |
return itemLoaderModel;
|
115 |
return itemLoaderModel;
|
| 127 |
}
|
116 |
}
|
| 128 |
|
117 |
|
| 129 |
@Autowired
|
118 |
@Autowired
|
| Line 137... |
Line 126... |
| 137 |
Supplier supplier = supplierRepository.selectById(itemLoaderModel.getPreferredVendor());
|
126 |
Supplier supplier = supplierRepository.selectById(itemLoaderModel.getPreferredVendor());
|
| 138 |
if (supplier == null) throw new ProfitMandiBusinessException("Invalid Supplier", "", "");
|
127 |
if (supplier == null) throw new ProfitMandiBusinessException("Invalid Supplier", "", "");
|
| 139 |
|
128 |
|
| 140 |
}
|
129 |
}
|
| 141 |
|
130 |
|
| 142 |
private String getCellValue(Cell cell) {
|
- |
|
| 143 |
if (cell == null) {
|
- |
|
| 144 |
return "Null cell";
|
- |
|
| 145 |
}
|
- |
|
| 146 |
switch (cell.getCellTypeEnum()) {
|
- |
|
| 147 |
case STRING:
|
- |
|
| 148 |
return cell.getStringCellValue().trim();
|
- |
|
| 149 |
case NUMERIC:
|
- |
|
| 150 |
if (DateUtil.isCellDateFormatted(cell)) {
|
- |
|
| 151 |
return cell.getDateCellValue().toString();
|
- |
|
| 152 |
} else {
|
- |
|
| 153 |
return Double.toString(cell.getNumericCellValue());
|
- |
|
| 154 |
}
|
- |
|
| 155 |
case BOOLEAN:
|
- |
|
| 156 |
return Boolean.toString(cell.getBooleanCellValue());
|
- |
|
| 157 |
case FORMULA:
|
- |
|
| 158 |
return cell.getCellFormula();
|
- |
|
| 159 |
default:
|
- |
|
| 160 |
return "";
|
- |
|
| 161 |
}
|
- |
|
| 162 |
}
|
131 |
|
| 163 |
|
132 |
|
| 164 |
//@Autowired
|
133 |
//@Autowired
|
| 165 |
|
134 |
|
| 166 |
private void addItem(ItemLoaderModel itemLoaderModel) throws ProfitMandiBusinessException {
|
135 |
private void addItem(ItemLoaderModel itemLoaderModel) throws ProfitMandiBusinessException {
|
| 167 |
this.validateItemLoaderModel(itemLoaderModel);
|
136 |
this.validateItemLoaderModel(itemLoaderModel);
|
| 168 |
List<Item> items = itemRepository.selectAll(itemLoaderModel.getBrand(), itemLoaderModel.getModelName(), itemLoaderModel.getModelNumber());
|
137 |
List<Item> items = itemRepository.selectAll(itemLoaderModel.getBrand(), itemLoaderModel.getModelName(), itemLoaderModel.getModelNumber());
|
| 169 |
int catalogId = 0;
|
138 |
int catalogId = 0;
|
| 170 |
if (items.size() > 0) {
|
139 |
if (items.size() > 0) {
|
| 171 |
//Check if color already exist
|
140 |
//Check if color already exist
|
| 172 |
long similarColorCount = items.stream().filter(x-> itemLoaderModel.getColor().equals(x.getColorNatural())).count();
|
141 |
long similarColorCount = items.stream().filter(x -> itemLoaderModel.getColor().equals(x.getColorNatural())).count();
|
| 173 |
if(similarColorCount > 0) {
|
142 |
if (similarColorCount > 0) {
|
| 174 |
throw new ProfitMandiBusinessException("Color alerady exist", "Pls check", itemLoaderModel.getColor());
|
143 |
throw new ProfitMandiBusinessException("Color alerady exist", "Pls check", itemLoaderModel.getColor());
|
| 175 |
}
|
144 |
}
|
| 176 |
|
145 |
|
| 177 |
List<Integer> categoryIds = items.stream().map(x -> x.getCategoryId()).distinct().collect(Collectors.toList());
|
146 |
List<Integer> categoryIds = items.stream().map(x -> x.getCategoryId()).distinct().collect(Collectors.toList());
|
| 178 |
if (categoryIds.size() > 1) {
|
147 |
if (categoryIds.size() > 1) {
|