Subversion Repositories SmartDukaan

Rev

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