Subversion Repositories SmartDukaan

Rev

Rev 24564 | Rev 24822 | 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
 
23968 amit.gupta 3
import java.text.MessageFormat;
24019 amit.gupta 4
import java.time.LocalDate;
23819 govind 5
import java.time.LocalDateTime;
24019 amit.gupta 6
import java.time.LocalTime;
23819 govind 7
import java.util.ArrayList;
23884 amit.gupta 8
import java.util.Arrays;
23819 govind 9
import java.util.HashMap;
10
import java.util.List;
11
import java.util.Map;
24063 amit.gupta 12
import java.util.Set;
23819 govind 13
import java.util.stream.Collectors;
14
 
15
import javax.servlet.http.HttpServletRequest;
16
import javax.transaction.Transactional;
17
 
23951 amit.gupta 18
import org.apache.commons.io.output.ByteArrayOutputStream;
19
import org.apache.commons.lang.StringUtils;
23945 amit.gupta 20
import org.apache.http.HttpResponse;
23819 govind 21
import org.apache.logging.log4j.LogManager;
22
import org.apache.logging.log4j.Logger;
23
import org.springframework.beans.factory.annotation.Autowired;
24
import org.springframework.beans.factory.annotation.Qualifier;
24191 amit.gupta 25
import org.springframework.cache.annotation.Cacheable;
23951 amit.gupta 26
import org.springframework.core.io.ByteArrayResource;
27
import org.springframework.http.HttpHeaders;
23819 govind 28
import org.springframework.http.HttpStatus;
29
import org.springframework.http.ResponseEntity;
30
import org.springframework.stereotype.Controller;
31
import org.springframework.ui.Model;
32
import org.springframework.web.bind.annotation.PathVariable;
23884 amit.gupta 33
import org.springframework.web.bind.annotation.RequestBody;
23819 govind 34
import org.springframework.web.bind.annotation.RequestMapping;
35
import org.springframework.web.bind.annotation.RequestMethod;
23951 amit.gupta 36
import org.springframework.web.bind.annotation.RequestParam;
23819 govind 37
 
23951 amit.gupta 38
import com.fasterxml.jackson.databind.JavaType;
23945 amit.gupta 39
import com.fasterxml.jackson.databind.ObjectMapper;
40
import com.fasterxml.jackson.databind.ObjectReader;
41
import com.spice.profitmandi.common.enumuration.ReporticoProject;
23819 govind 42
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23945 amit.gupta 43
import com.spice.profitmandi.common.model.ClosingStockOnDate;
44
import com.spice.profitmandi.common.model.GrnPendingDataModel;
23951 amit.gupta 45
import com.spice.profitmandi.common.model.ImeiDropSummaryModel;
23884 amit.gupta 46
import com.spice.profitmandi.common.model.ItemDescriptionModel;
23945 amit.gupta 47
import com.spice.profitmandi.common.model.PartnerImeiNotSold;
24083 amit.gupta 48
import com.spice.profitmandi.common.model.PriceDropImeisModel;
23884 amit.gupta 49
import com.spice.profitmandi.common.model.PriceDropModel;
23968 amit.gupta 50
import com.spice.profitmandi.common.model.PriceDropProcessModel;
23945 amit.gupta 51
import com.spice.profitmandi.common.model.ReporticoResponseModel;
52
import com.spice.profitmandi.common.services.ReporticoService;
23951 amit.gupta 53
import com.spice.profitmandi.common.util.FileUtil;
23945 amit.gupta 54
import com.spice.profitmandi.common.util.FormattingUtils;
23819 govind 55
import com.spice.profitmandi.dao.entity.catalog.Item;
23884 amit.gupta 56
import com.spice.profitmandi.dao.entity.catalog.TagListing;
23968 amit.gupta 57
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
23884 amit.gupta 58
import com.spice.profitmandi.dao.entity.inventory.VendorItemPricing;
23983 amit.gupta 59
import com.spice.profitmandi.dao.entity.transaction.LineItemImei;
23819 govind 60
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
24083 amit.gupta 61
import com.spice.profitmandi.dao.entity.transaction.PriceDropIMEI;
62
import com.spice.profitmandi.dao.enumuration.transaction.PriceDropImeiStatus;
23819 govind 63
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
23884 amit.gupta 64
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
23986 amit.gupta 65
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
66
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
23884 amit.gupta 67
import com.spice.profitmandi.dao.repository.inventory.VendorItemPricingRepository;
23983 amit.gupta 68
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
24083 amit.gupta 69
import com.spice.profitmandi.dao.repository.transaction.PriceDropIMEIRepository;
23819 govind 70
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
71
import com.spice.profitmandi.service.inventory.InventoryService;
23983 amit.gupta 72
import com.spice.profitmandi.service.pricing.PriceDropService;
23968 amit.gupta 73
import com.spice.profitmandi.service.scheme.SchemeService;
23884 amit.gupta 74
import com.spice.profitmandi.service.transaction.TransactionService;
23819 govind 75
import com.spice.profitmandi.service.wallet.WalletService;
76
import com.spice.profitmandi.web.util.MVCResponseSender;
77
 
23968 amit.gupta 78
import in.shop2020.model.v1.order.WalletReferenceType;
79
 
23819 govind 80
@Controller
81
@Transactional
82
public class PriceDropController {
83
 
84
	private static final Logger LOGGER = LogManager.getLogger(PriceDropController.class);
85
 
86
	@Autowired
87
	private PriceDropRepository priceDropRepository;
88
 
23951 amit.gupta 89
	@Autowired
90
	private ObjectMapper objectMapper;
23945 amit.gupta 91
 
23819 govind 92
	@Autowired
23884 amit.gupta 93
	private VendorItemPricingRepository vendorItemPricingRepository;
23819 govind 94
 
95
	@Autowired
23884 amit.gupta 96
	@Qualifier("fofoInventoryService")
97
	private InventoryService inventoryService;
23945 amit.gupta 98
 
23884 amit.gupta 99
	@Autowired
23819 govind 100
	private MVCResponseSender mvcResponseSender;
101
 
102
	@Autowired
23983 amit.gupta 103
	private PriceDropService priceDropService;
24083 amit.gupta 104
 
23983 amit.gupta 105
	@Autowired
23819 govind 106
	private WalletService walletService;
107
 
108
	@Autowired
23884 amit.gupta 109
	private TagListingRepository tagListingRepository;
23945 amit.gupta 110
 
23884 amit.gupta 111
	@Autowired
112
	private TransactionService transactionService;
23819 govind 113
 
114
	@Autowired
24083 amit.gupta 115
	private PriceDropIMEIRepository priceDropIMEIRepository;
116
 
117
	@Autowired
23819 govind 118
	@Qualifier("catalogItemRepository")
119
	private ItemRepository itemRepository;
120
 
23945 amit.gupta 121
	@Autowired
23968 amit.gupta 122
	private SchemeService schemeService;
24083 amit.gupta 123
 
23986 amit.gupta 124
	@Autowired
23945 amit.gupta 125
	ReporticoService reporticoService;
24083 amit.gupta 126
 
23983 amit.gupta 127
	@Autowired
128
	LineItemImeisRepository lineItemImeisRepository;
23945 amit.gupta 129
 
23819 govind 130
	@RequestMapping(value = "/getItemDescription", method = RequestMethod.GET)
23945 amit.gupta 131
	public String getItemDescription(HttpServletRequest request, Model model) throws Throwable {
23819 govind 132
		List<PriceDrop> priceDrops = priceDropRepository.selectAll();
24191 amit.gupta 133
		Set<Integer> catalogIds = priceDrops.stream().map(x -> x.getCatalogItemId()).collect(Collectors.toSet());
24180 amit.gupta 134
		List<Item> items = itemRepository.selectAllByCatalogIds(catalogIds);
24191 amit.gupta 135
		Map<Integer, String> catalogDescription = items.stream().collect(Collectors.toMap(x -> x.getCatalogItemId(),
136
				x -> x.getItemDescriptionNoColor(), (description1, description2) -> description1));
23819 govind 137
		model.addAttribute("priceDrops", priceDrops);
23968 amit.gupta 138
		model.addAttribute("catalogDescription", catalogDescription);
23819 govind 139
		return "price-drop";
140
	}
24083 amit.gupta 141
 
24049 amit.gupta 142
	@RequestMapping(value = "/item-pricing/{itemId}", method = RequestMethod.GET)
143
	public String getItemPricing(HttpServletRequest request, Model model, @PathVariable int itemId) throws Throwable {
24083 amit.gupta 144
 
24063 amit.gupta 145
		TagListing tagListing;
24049 amit.gupta 146
		PriceDropModel pm = new PriceDropModel();
24063 amit.gupta 147
		try {
148
			tagListing = tagListingRepository.selectByItemId(itemId);
24083 amit.gupta 149
			if (tagListing != null) {
24063 amit.gupta 150
				pm.setMop(tagListing.getMop());
151
				pm.setDp(tagListing.getSellingPrice());
152
				pm.setMrp(tagListing.getMrp());
153
				List<VendorItemPricing> vips = vendorItemPricingRepository.selectAll(itemId);
24083 amit.gupta 154
				if (vips.size() > 0) {
24063 amit.gupta 155
					VendorItemPricing vip = vips.get(0);
156
					pm.setNlc(vip.getNlc());
157
					pm.setTp(vip.getTp());
158
				} else {
159
					throw new ProfitMandiBusinessException("Item Id", itemId, "Vendor item pricing does not exist");
160
				}
161
			}
24083 amit.gupta 162
		} catch (Exception e) {
24063 amit.gupta 163
			LOGGER.info("Chose item that doesn't exist");
24049 amit.gupta 164
		}
165
		model.addAttribute("response", mvcResponseSender.createResponseString(pm));
166
		return "response";
167
	}
24083 amit.gupta 168
 
24168 amit.gupta 169
	@RequestMapping(value = "/item", method = RequestMethod.GET)
170
	public String getItemPricing(HttpServletRequest request, Model model, @RequestParam String query) throws Throwable {
24383 amit.gupta 171
		String query1 = query.toLowerCase();
24564 amit.gupta 172
		List<ItemDescriptionModel> partnersItemDescription = this.getAllPartnerItemStringDescription().parallelStream()
173
				.filter(x -> x.getItemDescription().toLowerCase().matches(".*?" + query1 + ".*?"))
174
				.collect(Collectors.toList());
24168 amit.gupta 175
 
24191 amit.gupta 176
		model.addAttribute("response", mvcResponseSender.createResponseString(partnersItemDescription));
24168 amit.gupta 177
		return "response";
178
	}
179
 
24191 amit.gupta 180
	@Cacheable(value = "fofoItems", cacheManager = "thirtyMinsTimeOutCacheManager")
181
	private List<ItemDescriptionModel> getAllPartnerItemStringDescription() {
182
		return itemRepository.selectPartnerItems(0).stream().map(x -> {
183
			String itemDescription = x.getItemDescription() + "(" + x.getId() + ")";
184
			ItemDescriptionModel itemDescriptionModel = new ItemDescriptionModel();
185
			itemDescriptionModel.setItemDescription(itemDescription);
186
			itemDescriptionModel.setItemId(x.getId());
24410 amit.gupta 187
			itemDescriptionModel.setCatalogId(x.getCatalogItemId());
24191 amit.gupta 188
			return itemDescriptionModel;
189
		}).collect(Collectors.toList());
190
	}
191
 
24083 amit.gupta 192
	/*
24191 amit.gupta 193
	 * IN-Request-20181101-0125
194
	 * 
24083 amit.gupta 195
	 * @RequestMapping(value = "/price-drop/imes1/download") public
196
	 * ResponseEntity<ByteArrayResource> downloadPriceDropImeis1(HttpServletRequest
197
	 * request,
198
	 * 
199
	 * @RequestParam LocalDateTime affectedDate, @RequestParam int itemId) throws
200
	 * Exception { Item item = itemRepository.selectById(itemId);
201
	 * List<ImeiDropSummaryModel> imeiDropSummaryModelList =
202
	 * this.getAllSerialNumbersByAffectedDate(affectedDate,
203
	 * item.getCatalogItemId()); List<String> imeis =
204
	 * imeiDropSummaryModelList.stream().map(x->x.getSerialNumber()).collect(
205
	 * Collectors.toList()); Map<String, ImeiDropSummaryModel> imeisDropMap =
206
	 * imeiDropSummaryModelList.stream().collect(Collectors.toMap(
207
	 * ImeiDropSummaryModel::getSerialNumber, x->x)); List<InventoryItem>
208
	 * inventoryItems = inventoryItemRepository.selectBySerialNumbers(new
209
	 * HashSet<>(imeis)); List<Integer> inventoryItemIds =
210
	 * inventoryItems.stream().map(InventoryItem::getId).collect(Collectors.toList()
211
	 * );
212
	 * 
213
	 * List<SchemeInOut> schemeInOuts =
214
	 * schemeInOutRepository.selectByInventoryItemIds(new
215
	 * HashSet<>(inventoryItemIds));
216
	 * 
217
	 * Set<ItemQuantity> itemSchemes = new HashSet<>(); Map<ItemQuantity,
218
	 * SchemeInOut> itemSchemeInOutMap = new HashMap<>(); for(SchemeInOut
219
	 * schemeInOut : schemeInOuts) { } List<List<Object>> rows = new ArrayList<>();
220
	 * for (ImeiDropSummaryModel imeiDropSummaryModel : imeiDropSummaryModelList) {
221
	 * List<Object> row = this.getRow(imeiDropSummaryModel);
222
	 * 
223
	 * rows.add(this.getRow(imeiDropSummaryModel)); }
224
	 * 
225
	 * FileUtil.getCSVByteStream(Arrays.asList("IMEI Number", "Store Name",
226
	 * "Store Code", "Item ID", "Brand", "Model Name", "Model Number", "Color",
227
	 * "Last Scanned"), rows); final HttpHeaders headers = new HttpHeaders();
228
	 * headers.set("Content-Type",
229
	 * "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
230
	 * headers.set("Content-disposition", "inline; filename=imei-" +
231
	 * item.getItemDescriptionNoColor() + ".csv"); return new
232
	 * ResponseEntity<ByteArrayResource>(null, headers, HttpStatus.OK);
233
	 * 
234
	 * }
235
	 */
23951 amit.gupta 236
	@RequestMapping(value = "/price-drop/imes/download")
237
	public ResponseEntity<ByteArrayResource> downloadPriceDropImeis(HttpServletRequest request,
238
			@RequestParam LocalDateTime affectedDate, @RequestParam int itemId) throws Exception {
239
		Item item = itemRepository.selectById(itemId);
23968 amit.gupta 240
		ByteArrayOutputStream baos = getByteArrayOutputStream(affectedDate, item.getCatalogItemId());
23951 amit.gupta 241
		final HttpHeaders headers = new HttpHeaders();
242
		headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
243
		headers.set("Content-disposition", "inline; filename=imei-" + item.getItemDescriptionNoColor() + ".csv");
244
		byte[] byteArray = baos.toByteArray();
245
		headers.setContentLength(byteArray.length);
246
		return new ResponseEntity<ByteArrayResource>(new ByteArrayResource(byteArray), headers, HttpStatus.OK);
247
	}
248
 
24083 amit.gupta 249
	@RequestMapping(value = "/price-drop/addPayout", method = RequestMethod.POST)
250
	public String updatePriceDrop(HttpServletRequest request, @RequestBody PriceDropProcessModel priceDropProcessModel,
251
			Model model) throws Exception {
252
		boolean response = false;
253
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropProcessModel.getPriceDropId());
254
		if (priceDrop.getProcessTimestamp() == null) {
255
			priceDrop.setPartnerPayout(priceDropProcessModel.getPartnerPayout());
256
			priceDrop.setPriceDropIn(priceDropProcessModel.getPriceDropIn());
257
			// priceDrop.setProcessTimestamp(LocalDateTime.now());
258
			priceDropRepository.persist(priceDrop);
259
			response = true;
260
		}
261
		model.addAttribute("response", mvcResponseSender.createResponseString(response));
262
		return "response";
263
	}
264
 
265
	@RequestMapping(value = "/priceDropImeis/{priceDropId}", method = RequestMethod.GET)
266
	public String priceDropStatus(HttpServletRequest request, @PathVariable int priceDropId, Model model)
267
			throws Exception {
268
 
269
		PriceDropImeisModel priceDropImeisModel = new PriceDropImeisModel();
24564 amit.gupta 270
 
271
		// This call is used to persit imeis to pricedrop imeis in case its not there.
24431 amit.gupta 272
		priceDropService.priceDropStatus(priceDropId);
24564 amit.gupta 273
 
24083 amit.gupta 274
		List<String> pendingImeis = new ArrayList<>();
275
		List<String> approvedImeis = new ArrayList<>();
276
		List<String> rejectedImeis = new ArrayList<>();
277
		List<PriceDropIMEI> priceDropImeis = priceDropIMEIRepository.selectByPriceDropId(priceDropId);
278
		for (PriceDropIMEI priceDropIMEI : priceDropImeis) {
279
			if (priceDropIMEI.getStatus().equals(PriceDropImeiStatus.PENDING)) {
280
				pendingImeis.add(priceDropIMEI.getImei());
281
			} else if (priceDropIMEI.getStatus().equals(PriceDropImeiStatus.APPROVED)) {
282
				approvedImeis.add(priceDropIMEI.getImei());
283
			}
284
			if (priceDropIMEI.getStatus().equals(PriceDropImeiStatus.REJECTED)) {
285
				rejectedImeis.add(priceDropIMEI.getImei());
286
			}
287
		}
288
		priceDropImeisModel.setPendingImeis(pendingImeis);
289
		priceDropImeisModel.setPriceDropId(priceDropId);
290
		priceDropImeisModel.setApprovedImeis(approvedImeis);
291
		priceDropImeisModel.setRejectedImeis(rejectedImeis);
292
		model.addAttribute("response", mvcResponseSender.createResponseString(priceDropImeisModel));
293
		return "response";
294
	}
295
 
23968 amit.gupta 296
	@RequestMapping(value = "/processPriceDrop", method = RequestMethod.POST)
297
	public String processPriceDrop(HttpServletRequest request, @RequestBody PriceDropProcessModel priceDropProcess,
298
			Model model) throws Exception {
299
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropProcess.getPriceDropId());
300
		boolean response = false;
301
		if (priceDrop.getProcessTimestamp() == null) {
302
			priceDrop.setProcessTimestamp(LocalDateTime.now());
303
			priceDropRepository.persist(priceDrop);
304
			response = true;
23819 govind 305
		}
23968 amit.gupta 306
		model.addAttribute("response", mvcResponseSender.createResponseString(response));
307
		return "response";
23819 govind 308
	}
24083 amit.gupta 309
 
23884 amit.gupta 310
	@RequestMapping(value = "/priceDrop", method = RequestMethod.POST)
23945 amit.gupta 311
	public String addPriceDrop(HttpServletRequest request, Model model, @RequestBody PriceDropModel priceDropModel)
312
			throws Exception {
23884 amit.gupta 313
		boolean response = false;
23968 amit.gupta 314
		priceDropModel.setAllColors(true);
23945 amit.gupta 315
		if (this.validatePriceDrop(priceDropModel)) {
23884 amit.gupta 316
			TagListing tagListing = tagListingRepository.selectByItemId(priceDropModel.getItemId());
24409 amit.gupta 317
			float oldDp = tagListing.getSellingPrice();
24410 amit.gupta 318
			float oldMop = tagListing.getMop();
319
			float oldTp = 0;
24409 amit.gupta 320
			float newDp = priceDropModel.getDp();
24564 amit.gupta 321
 
24410 amit.gupta 322
			if (newDp != oldDp) {
23884 amit.gupta 323
				List<Item> allItems = null;
324
				Item currentItem = itemRepository.selectById(priceDropModel.getItemId());
23945 amit.gupta 325
				if (priceDropModel.isAllColors()) {
23884 amit.gupta 326
					allItems = itemRepository.selectAllByCatalogItemId(currentItem.getCatalogItemId());
23945 amit.gupta 327
 
23884 amit.gupta 328
				} else {
23945 amit.gupta 329
					allItems = Arrays.asList(currentItem);
23884 amit.gupta 330
				}
23945 amit.gupta 331
				for (Item item : allItems) {
23884 amit.gupta 332
					TagListing itemTagListing = tagListingRepository.selectByItemId(item.getId());
23945 amit.gupta 333
					if (itemTagListing == null)
334
						continue;
23884 amit.gupta 335
					itemTagListing.setSellingPrice(newDp);
336
					itemTagListing.setMop(priceDropModel.getMop());
337
					tagListingRepository.persist(tagListing);
338
					List<VendorItemPricing> vipList = vendorItemPricingRepository.selectAll(item.getId());
23945 amit.gupta 339
					for (VendorItemPricing vip : vipList) {
24410 amit.gupta 340
						oldTp = vip.getNlc();
23884 amit.gupta 341
						vip.setDp(newDp);
342
						vip.setMop(priceDropModel.getMop());
24409 amit.gupta 343
						vip.setNlc(priceDropModel.getTp());
23884 amit.gupta 344
						vip.setTp(priceDropModel.getTp());
345
						vendorItemPricingRepository.persist(vip);
346
					}
347
					transactionService.updatePriceDrop(item.getId(), newDp);
348
				}
23951 amit.gupta 349
				PriceDrop priceDrop = new PriceDrop();
350
				priceDrop.setAffectedOn(priceDropModel.getAffectedDate());
24410 amit.gupta 351
				priceDrop.setTp(oldTp);
352
				priceDrop.setNlc(oldTp);
353
				priceDrop.setMop(oldMop);
24409 amit.gupta 354
				priceDrop.setOldDp(oldDp);
355
				priceDrop.setAmount(oldDp - newDp);
23968 amit.gupta 356
				priceDrop.setNewDp(newDp);
23951 amit.gupta 357
				priceDrop.setCreatedOn(LocalDateTime.now());
358
				priceDrop.setCatalogItemId(currentItem.getCatalogItemId());
359
				priceDropRepository.persist(priceDrop);
23884 amit.gupta 360
				response = true;
361
			} else {
23945 amit.gupta 362
				throw new ProfitMandiBusinessException("Price Drop", priceDropModel.getPd(),
24409 amit.gupta 363
						"Price Drop Should be greater than 0");
23884 amit.gupta 364
			}
365
		}
366
		model.addAttribute("response", mvcResponseSender.createResponseString(response));
367
		return "response";
368
	}
23945 amit.gupta 369
 
23819 govind 370
	@RequestMapping(value = "/downloadtotalPriceDropIMEI/{priceDropId}", method = RequestMethod.GET)
371
	public ResponseEntity<?> downloadTotalPriceDropIMEI(HttpServletRequest request, @PathVariable int priceDropId,
23945 amit.gupta 372
			Model model) throws ProfitMandiBusinessException, Exception {
23819 govind 373
 
374
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropId);
23968 amit.gupta 375
		Item item = itemRepository.selectAllByCatalogItemId(priceDrop.getCatalogItemId()).get(0);
376
		ByteArrayOutputStream baos = getByteArrayOutputStream(priceDrop.getAffectedOn(), priceDrop.getCatalogItemId());
377
		final HttpHeaders headers = new HttpHeaders();
378
		headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
23983 amit.gupta 379
		headers.set("Content-disposition",
380
				"inline; filename=pricedrop-" + item.getItemDescriptionNoColor().replaceAll("\\s?,\\s?", " ") + "-"
381
						+ FormattingUtils.formatDate(priceDrop.getAffectedOn()) + ".csv");
23968 amit.gupta 382
		byte[] byteArray = baos.toByteArray();
383
		headers.setContentLength(byteArray.length);
384
		return new ResponseEntity<ByteArrayResource>(new ByteArrayResource(byteArray), headers, HttpStatus.OK);
23951 amit.gupta 385
 
386
	}
387
 
24083 amit.gupta 388
	@RequestMapping(value = "/updatePriceDropImeis", method = RequestMethod.POST)
389
	public String downloadTotalPriceDropIMEI(HttpServletRequest request,
390
			@RequestBody PriceDropImeisModel priceDropImeisModel, Model model)
391
			throws ProfitMandiBusinessException, Exception {
24168 amit.gupta 392
		PriceDropImeiStatus status = PriceDropImeiStatus.PENDING;
393
		;
24083 amit.gupta 394
 
395
		switch (priceDropImeisModel.getUpdatedStatus()) {
396
		case "pending": {
397
			status = PriceDropImeiStatus.PENDING;
398
			break;
399
		}
400
		case "approved": {
401
			status = PriceDropImeiStatus.APPROVED;
402
			break;
403
		}
404
		case "rejected": {
405
			status = PriceDropImeiStatus.REJECTED;
406
			break;
407
		}
408
 
409
		}
410
		List<PriceDropIMEI> priceDropIMEIs = priceDropIMEIRepository
411
				.selectByPriceDropId(priceDropImeisModel.getPriceDropId());
412
 
413
		List<PriceDropIMEI> priceDropIMEIsToProcess = new ArrayList<>();
414
		for (PriceDropIMEI priceDropIMEI : priceDropIMEIs) {
415
			if (!priceDropImeisModel.getUpdatedImeis().contains(priceDropIMEI.getImei())
416
					|| priceDropIMEI.getStatus().equals(PriceDropImeiStatus.APPROVED)
417
					|| priceDropIMEI.getStatus().equals(status)) {
418
				continue;
419
			} else {
24168 amit.gupta 420
				if (status.equals(PriceDropImeiStatus.APPROVED)) {
24083 amit.gupta 421
					priceDropIMEIsToProcess.add(priceDropIMEI);
422
				}
423
				priceDropIMEI.setStatus(status);
424
				priceDropIMEI.setUpdateTimestamp(LocalDateTime.now());
425
				priceDropIMEIRepository.persist(priceDropIMEI);
426
			}
427
		}
428
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropImeisModel.getPriceDropId());
429
		Item item = itemRepository.selectAllByCatalogItemId(priceDrop.getCatalogItemId()).get(0);
430
		String description = item.getItemDescriptionNoColor();
431
		if (priceDropIMEIsToProcess.size() > 0) {
24564 amit.gupta 432
 
24083 amit.gupta 433
			Map<Integer, List<InventoryItem>> pendingPartnerInventoryMap = priceDropService
24564 amit.gupta 434
					.getInventoryForPriceDrop(priceDropIMEIsToProcess, priceDrop).stream()
24083 amit.gupta 435
					.collect(Collectors.groupingBy(InventoryItem::getFofoId));
436
 
437
			for (Map.Entry<Integer, List<InventoryItem>> pendingPartnerInventory : pendingPartnerInventoryMap
438
					.entrySet()) {
439
				List<InventoryItem> fofoInventoryList = pendingPartnerInventory.getValue();
440
				int fofoId = pendingPartnerInventory.getKey();
441
 
442
				String reversalReason = MessageFormat.format(
443
						"Scheme  differential for Price Drop of Rs.{0} on {1}, on {3}. Total {2} item(s)",
444
						priceDrop.getAmount(), description, fofoInventoryList.size(),
445
						FormattingUtils.formatDate(priceDrop.getAffectedOn()));
446
				String aReason = MessageFormat.format(
447
						"Payout of Rs.{4} per unit for Price Drop of Rs.{0} on {1}, on {3}. Total {2} item(s)",
448
						priceDrop.getAmount(), description, fofoInventoryList.size(),
449
						FormattingUtils.formatDate(priceDrop.getAffectedOn()), priceDrop.getPartnerPayout());
450
				inventoryService.updatePriceDrop(fofoInventoryList, priceDrop.getAmount());
451
				if (item.getBrand().equals("Samsung")) {
24168 amit.gupta 452
					schemeService.reverseSchemes(fofoInventoryList, priceDrop.getId(), reversalReason);
24083 amit.gupta 453
				}
454
				walletService.addAmountToWallet(fofoId, priceDrop.getId(), WalletReferenceType.PRICE_DROP, aReason,
455
						priceDrop.getPartnerPayout() * fofoInventoryList.size());
456
			}
457
 
458
		}
24168 amit.gupta 459
 
24083 amit.gupta 460
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
461
		return "response";
462
 
463
	}
464
 
23983 amit.gupta 465
	private ByteArrayOutputStream getByteArrayOutputStream(LocalDateTime affectedOn, Integer catalogItemId)
466
			throws Exception {
24564 amit.gupta 467
		List<ImeiDropSummaryModel> imeiDropSummaryModelList = priceDropService
468
				.getAllSerialNumbersByAffectedDate(affectedOn, catalogItemId);
24596 amit.gupta 469
		List<List<?>> rows = new ArrayList<>();
23983 amit.gupta 470
		for (ImeiDropSummaryModel imeiDropSummaryModel : imeiDropSummaryModelList) {
23986 amit.gupta 471
			rows.add(this.getRow(imeiDropSummaryModel));
23951 amit.gupta 472
		}
473
		return FileUtil.getCSVByteStream(Arrays.asList("IMEI Number", "Store Name", "Store Code", "Item ID", "Brand",
474
				"Model Name", "Model Number", "Color", "Last Scanned"), rows);
23819 govind 475
	}
24083 amit.gupta 476
 
23986 amit.gupta 477
	private List<Object> getRow(ImeiDropSummaryModel imeiDropSummaryModel) {
478
		List<Object> row = new ArrayList<>();
479
		row.add(imeiDropSummaryModel.getSerialNumber());
480
		row.add(imeiDropSummaryModel.getStoreName());
481
		row.add(imeiDropSummaryModel.getPartnerCode());
482
		row.add(imeiDropSummaryModel.getItemId());
483
		row.add(imeiDropSummaryModel.getBrand());
484
		row.add(imeiDropSummaryModel.getModelName());
485
		row.add(imeiDropSummaryModel.getModelNumber());
486
		row.add(imeiDropSummaryModel.getColor());
487
		row.add(FormattingUtils.formatReporitcoDate(imeiDropSummaryModel.getLastScanned()));
488
		return row;
489
	}
23819 govind 490
 
23884 amit.gupta 491
	private boolean validatePriceDrop(PriceDropModel priceDropModel) throws ProfitMandiBusinessException {
24564 amit.gupta 492
		if (priceDropModel.getMop() > 0 && priceDropModel.getDp() > 0 && priceDropModel.getTp() > 0) {
23884 amit.gupta 493
			return true;
494
		}
495
		return false;
496
	}
23945 amit.gupta 497
 
23819 govind 498
}