Subversion Repositories SmartDukaan

Rev

Rev 23945 | Rev 23968 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
23819 govind 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.time.LocalDateTime;
4
import java.util.ArrayList;
23884 amit.gupta 5
import java.util.Arrays;
23819 govind 6
import java.util.HashMap;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.stream.Collectors;
10
 
11
import javax.servlet.http.HttpServletRequest;
12
import javax.transaction.Transactional;
13
 
23951 amit.gupta 14
import org.apache.commons.io.output.ByteArrayOutputStream;
15
import org.apache.commons.lang.StringUtils;
23945 amit.gupta 16
import org.apache.http.HttpResponse;
23819 govind 17
import org.apache.logging.log4j.LogManager;
18
import org.apache.logging.log4j.Logger;
23884 amit.gupta 19
import org.json.JSONArray;
23819 govind 20
import org.springframework.beans.factory.annotation.Autowired;
21
import org.springframework.beans.factory.annotation.Qualifier;
23951 amit.gupta 22
import org.springframework.core.io.ByteArrayResource;
23
import org.springframework.http.HttpHeaders;
23819 govind 24
import org.springframework.http.HttpStatus;
25
import org.springframework.http.ResponseEntity;
26
import org.springframework.stereotype.Controller;
27
import org.springframework.ui.Model;
28
import org.springframework.web.bind.annotation.PathVariable;
23884 amit.gupta 29
import org.springframework.web.bind.annotation.RequestBody;
23819 govind 30
import org.springframework.web.bind.annotation.RequestMapping;
31
import org.springframework.web.bind.annotation.RequestMethod;
23951 amit.gupta 32
import org.springframework.web.bind.annotation.RequestParam;
23819 govind 33
 
23951 amit.gupta 34
import com.fasterxml.jackson.databind.JavaType;
23945 amit.gupta 35
import com.fasterxml.jackson.databind.ObjectMapper;
36
import com.fasterxml.jackson.databind.ObjectReader;
37
import com.spice.profitmandi.common.enumuration.ReporticoProject;
23819 govind 38
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23945 amit.gupta 39
import com.spice.profitmandi.common.model.ClosingStockOnDate;
40
import com.spice.profitmandi.common.model.GrnPendingDataModel;
23951 amit.gupta 41
import com.spice.profitmandi.common.model.ImeiDropSummaryModel;
23884 amit.gupta 42
import com.spice.profitmandi.common.model.ItemDescriptionModel;
23945 amit.gupta 43
import com.spice.profitmandi.common.model.PartnerImeiNotSold;
23884 amit.gupta 44
import com.spice.profitmandi.common.model.PriceDropModel;
23945 amit.gupta 45
import com.spice.profitmandi.common.model.ReporticoResponseModel;
46
import com.spice.profitmandi.common.services.ReporticoService;
23951 amit.gupta 47
import com.spice.profitmandi.common.util.FileUtil;
23945 amit.gupta 48
import com.spice.profitmandi.common.util.FormattingUtils;
23819 govind 49
import com.spice.profitmandi.dao.entity.catalog.Item;
23884 amit.gupta 50
import com.spice.profitmandi.dao.entity.catalog.TagListing;
51
import com.spice.profitmandi.dao.entity.inventory.VendorItemPricing;
23819 govind 52
import com.spice.profitmandi.dao.entity.transaction.LineItemImei;
53
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
54
import com.spice.profitmandi.dao.entity.transaction.PriceDropIMEI;
55
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
23884 amit.gupta 56
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
57
import com.spice.profitmandi.dao.repository.inventory.VendorItemPricingRepository;
23819 govind 58
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
59
import com.spice.profitmandi.dao.repository.transaction.PriceDropIMEIRepository;
60
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
61
import com.spice.profitmandi.service.inventory.InventoryService;
62
import com.spice.profitmandi.service.pricing.PriceDropService;
23884 amit.gupta 63
import com.spice.profitmandi.service.transaction.TransactionService;
23819 govind 64
import com.spice.profitmandi.service.wallet.WalletService;
65
import com.spice.profitmandi.web.util.MVCResponseSender;
66
 
67
@Controller
68
@Transactional
69
public class PriceDropController {
70
 
71
	private static final Logger LOGGER = LogManager.getLogger(PriceDropController.class);
72
 
73
	@Autowired
74
	private PriceDropRepository priceDropRepository;
75
 
23951 amit.gupta 76
	@Autowired
77
	private ObjectMapper objectMapper;
23945 amit.gupta 78
 
23819 govind 79
	@Autowired
23884 amit.gupta 80
	private PriceDropIMEIRepository priceDropIMEIRepository;
23945 amit.gupta 81
 
23819 govind 82
	@Autowired
23884 amit.gupta 83
	private VendorItemPricingRepository vendorItemPricingRepository;
23819 govind 84
 
85
	@Autowired
86
	private PriceDropService priceDropService;
87
 
88
	@Autowired
23884 amit.gupta 89
	@Qualifier("fofoInventoryService")
90
	private InventoryService inventoryService;
23945 amit.gupta 91
 
23884 amit.gupta 92
	@Autowired
23819 govind 93
	private MVCResponseSender mvcResponseSender;
94
 
95
	@Autowired
96
	private WalletService walletService;
97
 
98
	@Autowired
99
	private LineItemImeisRepository lineItemImeisRepository;
23945 amit.gupta 100
 
23884 amit.gupta 101
	@Autowired
102
	private TagListingRepository tagListingRepository;
23945 amit.gupta 103
 
23884 amit.gupta 104
	@Autowired
105
	private TransactionService transactionService;
23819 govind 106
 
107
	@Autowired
108
	@Qualifier("catalogItemRepository")
109
	private ItemRepository itemRepository;
110
 
23945 amit.gupta 111
	@Autowired
112
	ReporticoService reporticoService;
113
 
23819 govind 114
	@RequestMapping(value = "/getItemDescription", method = RequestMethod.GET)
23945 amit.gupta 115
	public String getItemDescription(HttpServletRequest request, Model model) throws Throwable {
23819 govind 116
 
23884 amit.gupta 117
		List<TagListing> tagListings = tagListingRepository.selectAll(false);
118
		Map<Integer, TagListing> tagListingMap = new HashMap<>();
119
		List<ItemDescriptionModel> customItems = new ArrayList<>();
23945 amit.gupta 120
		for (TagListing tagListing : tagListings) {
23884 amit.gupta 121
			tagListing.setItemDescription(itemRepository.selectById(tagListing.getItemId()).getItemDescription());
122
			tagListingMap.put(tagListing.getItemId(), tagListing);
123
			ItemDescriptionModel itemDescriptionModel = new ItemDescriptionModel();
124
			itemDescriptionModel.setItemId(tagListing.getItemId());
23945 amit.gupta 125
			itemDescriptionModel
126
					.setItemDescription(tagListing.getItemDescription() + "(" + tagListing.getItemId() + ")");
127
			customItems.add(itemDescriptionModel);
23884 amit.gupta 128
		}
23945 amit.gupta 129
 
23819 govind 130
		List<PriceDrop> priceDrops = priceDropRepository.selectAll();
23884 amit.gupta 131
		model.addAttribute("tagListingMap", tagListingMap);
132
		model.addAttribute("customItems", new JSONArray(customItems).toString());
23819 govind 133
		model.addAttribute("priceDrops", priceDrops);
134
		return "price-drop";
135
 
136
	}
137
 
23945 amit.gupta 138
	/*
139
	 * @RequestMapping(value = "/downloadtotalPriceDropIMEI/{priceDropId}", method =
140
	 * RequestMethod.GET) public ResponseEntity<?>
141
	 * downloadPriceDropIMEI(HttpServletRequest request, @PathVariable int
142
	 * priceDropId, Model model) throws ProfitMandiBusinessException {
143
	 * 
144
	 * PriceDrop priceDrop = priceDropRepository.selectById(priceDropId);
145
	 * List<PriceDropIMEI> priceDropIMEIs =
146
	 * priceDropIMEIRepository.selectByPriceDropId(priceDrop.getId()); List<String>
147
	 * imeis = priceDropIMEIs.stream().map(x ->
148
	 * x.getImei()).collect(Collectors.toList()); List<LineItemImei> lineItemImeis =
149
	 * lineItemImeisRepository.selectByIMEI(imeis); Map<String, String>
150
	 * priceDropIMEIfofoId = priceDropService.getIMEIandRetailerName(lineItemImeis,
151
	 * priceDrop); if (priceDropIMEIfofoId.size() > 0) { ByteArrayOutputStream
152
	 * byteArrayOutputStream = new ByteArrayOutputStream();
153
	 * ExcelUtils.writePriceDrop(priceDropIMEIfofoId, priceDrop.getItemId(),
154
	 * byteArrayOutputStream); final HttpHeaders headers = new HttpHeaders(); //
155
	 * private static final String CONTENT_TYPE_XLSX = //
156
	 * "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
157
	 * headers.set("Content-Type",
158
	 * "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //
159
	 * headers.set("Content-Type", "application/vnd.ms-excel");
160
	 * headers.set("Content-disposition", "inline; filename=priceDrop" +
161
	 * priceDrop.getId() + ".xlsx");
162
	 * headers.setContentLength(byteArrayOutputStream.toByteArray().length); final
163
	 * InputStream inputStream = new
164
	 * ByteArrayInputStream(byteArrayOutputStream.toByteArray()); final
165
	 * InputStreamResource inputStreamResource = new
166
	 * InputStreamResource(inputStream); return new
167
	 * ResponseEntity<InputStreamResource>(inputStreamResource, headers,
168
	 * HttpStatus.OK); } else { throw new
169
	 * ProfitMandiBusinessException("priceDropId", priceDropId,
170
	 * "No IMEI is Eligible For PriceDrop"); }
171
	 * 
172
	 * }
173
	 */
23819 govind 174
 
23951 amit.gupta 175
	@RequestMapping(value = "/price-drop/imes/download")
176
	public ResponseEntity<ByteArrayResource> downloadPriceDropImeis(HttpServletRequest request,
177
			@RequestParam LocalDateTime affectedDate, @RequestParam int itemId) throws Exception {
178
		Item item = itemRepository.selectById(itemId);
179
		ByteArrayOutputStream baos = getByteArrayOutputStream(affectedDate,
180
				item.getCatalogItemId());
181
		final HttpHeaders headers = new HttpHeaders();
182
		headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
183
		headers.set("Content-disposition", "inline; filename=imei-" + item.getItemDescriptionNoColor() + ".csv");
184
		byte[] byteArray = baos.toByteArray();
185
		headers.setContentLength(byteArray.length);
186
		return new ResponseEntity<ByteArrayResource>(new ByteArrayResource(byteArray), headers, HttpStatus.OK);
187
	}
188
 
23819 govind 189
	@RequestMapping(value = "/paymentAgainstPriceDrop/{priceDropId}/{processedamount}", method = RequestMethod.POST)
190
	public String paymentAgainstPriceDropIMEI(HttpServletRequest request, @PathVariable int priceDropId,
191
			@PathVariable int processedamount, Model model) throws Exception {
192
 
193
		int noOfIMEIForPartner = 1;
194
		Map<Integer, Integer> noOfPriceDropToPartners = new HashMap<>();
195
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropId);
23951 amit.gupta 196
		List<Item> items = itemRepository.selectAllByCatalogItemId(priceDrop.getCatalogItemId());
23819 govind 197
		List<PriceDropIMEI> priceDropIMEIs = priceDropIMEIRepository.selectByPriceDropId(priceDrop.getId());
198
		List<String> imeis = priceDropIMEIs.stream().map(x -> x.getImei()).collect(Collectors.toList());
199
		List<LineItemImei> lineItemImeis = lineItemImeisRepository.selectByIMEI(imeis);
200
		Map<String, Integer> priceDropIMEIfofoIds = priceDropService.getIMEIAndfofoIdForPriceDrop(lineItemImeis,
201
				priceDrop);
202
		if (priceDropIMEIfofoIds.size() > 0) {
203
			for (Map.Entry<String, Integer> priceDropIMEIAndFofoId : priceDropIMEIfofoIds.entrySet()) {
204
				int fofoId = priceDropIMEIAndFofoId.getValue();
205
				if (noOfPriceDropToPartners.containsKey(fofoId)) {
206
					noOfPriceDropToPartners.put(fofoId, noOfPriceDropToPartners.get(fofoId) + 1);
207
				} else {
208
					noOfPriceDropToPartners.put(fofoId, noOfIMEIForPartner);
209
				}
210
 
211
			}
212
			priceDrop.setProcessTimestamp(LocalDateTime.now());
23823 amit.gupta 213
			priceDrop.setPartnerPayout(processedamount);
23819 govind 214
			priceDropRepository.persist(priceDrop);
215
			model.addAttribute("response", mvcResponseSender.createResponseString(true));
216
			return "response";
217
		} else {
218
			priceDrop.setProcessTimestamp(LocalDateTime.now());
219
			model.addAttribute("response", mvcResponseSender.createResponseString(false));
220
			return "false";
221
		}
222
	}
223
 
23884 amit.gupta 224
	@RequestMapping(value = "/priceDrop", method = RequestMethod.POST)
23945 amit.gupta 225
	public String addPriceDrop(HttpServletRequest request, Model model, @RequestBody PriceDropModel priceDropModel)
226
			throws Exception {
23884 amit.gupta 227
		boolean response = false;
23945 amit.gupta 228
		if (this.validatePriceDrop(priceDropModel)) {
23884 amit.gupta 229
			TagListing tagListing = tagListingRepository.selectByItemId(priceDropModel.getItemId());
230
			float newDp = tagListing.getSellingPrice() - priceDropModel.getPd();
23945 amit.gupta 231
			if (newDp > 0) {
23884 amit.gupta 232
				List<Item> allItems = null;
233
				Item currentItem = itemRepository.selectById(priceDropModel.getItemId());
23945 amit.gupta 234
				if (priceDropModel.isAllColors()) {
23884 amit.gupta 235
					allItems = itemRepository.selectAllByCatalogItemId(currentItem.getCatalogItemId());
23945 amit.gupta 236
 
23884 amit.gupta 237
				} else {
23945 amit.gupta 238
					allItems = Arrays.asList(currentItem);
23884 amit.gupta 239
				}
23945 amit.gupta 240
				for (Item item : allItems) {
23884 amit.gupta 241
					TagListing itemTagListing = tagListingRepository.selectByItemId(item.getId());
23945 amit.gupta 242
					if (itemTagListing == null)
243
						continue;
23884 amit.gupta 244
					itemTagListing.setSellingPrice(newDp);
245
					itemTagListing.setMop(priceDropModel.getMop());
246
					tagListingRepository.persist(tagListing);
247
					List<VendorItemPricing> vipList = vendorItemPricingRepository.selectAll(item.getId());
23945 amit.gupta 248
					for (VendorItemPricing vip : vipList) {
23884 amit.gupta 249
						vip.setDp(newDp);
250
						vip.setMop(priceDropModel.getMop());
251
						vip.setNlc(priceDropModel.getNlc());
252
						vip.setTp(priceDropModel.getTp());
253
						vendorItemPricingRepository.persist(vip);
254
					}
255
					transactionService.updatePriceDrop(item.getId(), newDp);
256
				}
23951 amit.gupta 257
				PriceDrop priceDrop = new PriceDrop();
258
				priceDrop.setAffectedOn(priceDropModel.getAffectedDate());
259
				priceDrop.setAmount(priceDropModel.getPd());
260
				priceDrop.setCreatedOn(LocalDateTime.now());
261
				priceDrop.setCatalogItemId(currentItem.getCatalogItemId());
262
				priceDropRepository.persist(priceDrop);
23884 amit.gupta 263
				response = true;
264
			} else {
23945 amit.gupta 265
				throw new ProfitMandiBusinessException("Price Drop", priceDropModel.getPd(),
266
						"Price Drop Cannot be more than Current DP");
23884 amit.gupta 267
			}
268
		}
269
		model.addAttribute("response", mvcResponseSender.createResponseString(response));
270
		return "response";
271
	}
23945 amit.gupta 272
 
23819 govind 273
	@RequestMapping(value = "/downloadtotalPriceDropIMEI/{priceDropId}", method = RequestMethod.GET)
274
	public ResponseEntity<?> downloadTotalPriceDropIMEI(HttpServletRequest request, @PathVariable int priceDropId,
23945 amit.gupta 275
			Model model) throws ProfitMandiBusinessException, Exception {
23819 govind 276
 
277
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropId);
23951 amit.gupta 278
 
279
		return new ResponseEntity<Boolean>(true, null, HttpStatus.OK);
280
	}
281
 
282
	private ByteArrayOutputStream getByteArrayOutputStream(LocalDateTime affectedOn, Integer catalogItemId)
283
			throws Exception {
23945 amit.gupta 284
		Map<String, String> params = new HashMap<>();
23951 amit.gupta 285
		params.put("MANUAL_criteria1_FROMDATE", FormattingUtils.formatReporitcoDate(affectedOn));
286
		List<Item> items = itemRepository.selectAllByCatalogItemId(catalogItemId);
287
		List<String> itemIds = items.stream().map(x -> String.valueOf(x.getId())).collect(Collectors.toList());
288
		params.put("MANUAL_itemId", StringUtils.join(itemIds, ","));
23945 amit.gupta 289
		Map<String, String> params1 = new HashMap<>();
23951 amit.gupta 290
		params1.put("MANUAL_criteriaDate_FROMDATE", FormattingUtils.formatReporitcoDate(affectedOn));
291
		params1.put("MANUAL_criteriaItemId", StringUtils.join(itemIds, ","));
23945 amit.gupta 292
 
293
		Map<String, String> params2 = new HashMap<>();
23951 amit.gupta 294
		params2.put("MANUAL_Criteria2_FROMDATE", FormattingUtils.formatReporitcoDate(affectedOn));
295
		params2.put("MANUAL_Criteria1", StringUtils.join(itemIds, ","));
296
		List<? extends ImeiDropSummaryModel> grnPendingList = getReports(GrnPendingDataModel.class,
297
				ReporticoProject.FOCO, "imeispendinggrn.xml", params);
298
		List<? extends ImeiDropSummaryModel> partnerImeiNotSoldList = getReports(PartnerImeiNotSold.class,
299
				ReporticoProject.FOCO, "PartnerIMEINotSold.xml", params1);
300
		List<? extends ImeiDropSummaryModel> itemStockOnDateList = getReports(ClosingStockOnDate.class,
301
				ReporticoProject.WAREHOUSENEW, "itemstockondate.xml", params2);
302
		List<ImeiDropSummaryModel> mergedImeis = new ArrayList<>();
303
		mergedImeis.addAll(grnPendingList);
304
		mergedImeis.addAll(partnerImeiNotSoldList);
305
		mergedImeis.addAll(itemStockOnDateList);
306
		List<List<Object>> rows = new ArrayList<>();
307
		for (ImeiDropSummaryModel imeiDropSummaryModel : mergedImeis) {
308
			List<Object> row = new ArrayList<>();
309
			row.add(imeiDropSummaryModel.getSerialNumber());
310
			row.add(imeiDropSummaryModel.getStoreName());
311
			row.add(imeiDropSummaryModel.getPartnerCode());
312
			row.add(imeiDropSummaryModel.getItemId());
313
			row.add(imeiDropSummaryModel.getBrand());
314
			row.add(imeiDropSummaryModel.getModelName());
315
			row.add(imeiDropSummaryModel.getModelNumber());
316
			row.add(imeiDropSummaryModel.getColor());
317
			row.add(FormattingUtils.formatReporitcoDate(imeiDropSummaryModel.getLastScanned()));
318
			rows.add(row);
319
		}
320
 
321
		return FileUtil.getCSVByteStream(Arrays.asList("IMEI Number", "Store Name", "Store Code", "Item ID", "Brand",
322
				"Model Name", "Model Number", "Color", "Last Scanned"), rows);
23819 govind 323
	}
324
 
23945 amit.gupta 325
	private <T> List<T> getReports(Class<T> className, ReporticoProject project, String reportName,
326
			Map<String, String> params) throws Exception {
327
		HttpResponse reportResponse = reporticoService.getJsonFile(project, reportName, params);
23951 amit.gupta 328
		JavaType type = objectMapper.getTypeFactory().constructParametricType(ReporticoResponseModel.class, className);
329
		ObjectReader or = objectMapper.readerFor(type);
23945 amit.gupta 330
		ReporticoResponseModel<T> responseObj = or.readValue(reportResponse.getEntity().getContent());
331
		return responseObj.getData();
332
	}
333
 
23884 amit.gupta 334
	private boolean validatePriceDrop(PriceDropModel priceDropModel) throws ProfitMandiBusinessException {
23945 amit.gupta 335
		if (priceDropModel.getMop() > 0 && priceDropModel.getMop() > 0 && priceDropModel.getNlc() > 0
336
				&& priceDropModel.getTp() > 0 && priceDropModel.getNlc() > 0) {
23884 amit.gupta 337
			return true;
338
		}
339
		return false;
340
	}
23945 amit.gupta 341
 
23819 govind 342
}