Subversion Repositories SmartDukaan

Rev

Rev 24822 | Rev 25408 | 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");
189
		headers.set("Content-disposition", "inline; filename=imei-" + item.getItemDescriptionNoColor() + ".csv");
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.
24431 amit.gupta 218
		priceDropService.priceDropStatus(priceDropId);
24564 amit.gupta 219
 
24083 amit.gupta 220
		List<String> pendingImeis = new ArrayList<>();
221
		List<String> approvedImeis = new ArrayList<>();
222
		List<String> rejectedImeis = new ArrayList<>();
223
		List<PriceDropIMEI> priceDropImeis = priceDropIMEIRepository.selectByPriceDropId(priceDropId);
224
		for (PriceDropIMEI priceDropIMEI : priceDropImeis) {
225
			if (priceDropIMEI.getStatus().equals(PriceDropImeiStatus.PENDING)) {
226
				pendingImeis.add(priceDropIMEI.getImei());
227
			} else if (priceDropIMEI.getStatus().equals(PriceDropImeiStatus.APPROVED)) {
228
				approvedImeis.add(priceDropIMEI.getImei());
229
			}
230
			if (priceDropIMEI.getStatus().equals(PriceDropImeiStatus.REJECTED)) {
231
				rejectedImeis.add(priceDropIMEI.getImei());
232
			}
233
		}
234
		priceDropImeisModel.setPendingImeis(pendingImeis);
235
		priceDropImeisModel.setPriceDropId(priceDropId);
236
		priceDropImeisModel.setApprovedImeis(approvedImeis);
237
		priceDropImeisModel.setRejectedImeis(rejectedImeis);
238
		model.addAttribute("response", mvcResponseSender.createResponseString(priceDropImeisModel));
239
		return "response";
240
	}
241
 
23968 amit.gupta 242
	@RequestMapping(value = "/processPriceDrop", method = RequestMethod.POST)
243
	public String processPriceDrop(HttpServletRequest request, @RequestBody PriceDropProcessModel priceDropProcess,
244
			Model model) throws Exception {
245
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropProcess.getPriceDropId());
246
		boolean response = false;
247
		if (priceDrop.getProcessTimestamp() == null) {
248
			priceDrop.setProcessTimestamp(LocalDateTime.now());
249
			priceDropRepository.persist(priceDrop);
250
			response = true;
23819 govind 251
		}
23968 amit.gupta 252
		model.addAttribute("response", mvcResponseSender.createResponseString(response));
253
		return "response";
23819 govind 254
	}
24083 amit.gupta 255
 
23884 amit.gupta 256
	@RequestMapping(value = "/priceDrop", method = RequestMethod.POST)
23945 amit.gupta 257
	public String addPriceDrop(HttpServletRequest request, Model model, @RequestBody PriceDropModel priceDropModel)
258
			throws Exception {
23884 amit.gupta 259
		boolean response = false;
23968 amit.gupta 260
		priceDropModel.setAllColors(true);
23945 amit.gupta 261
		if (this.validatePriceDrop(priceDropModel)) {
23884 amit.gupta 262
			TagListing tagListing = tagListingRepository.selectByItemId(priceDropModel.getItemId());
24409 amit.gupta 263
			float oldDp = tagListing.getSellingPrice();
24410 amit.gupta 264
			float oldMop = tagListing.getMop();
265
			float oldTp = 0;
24409 amit.gupta 266
			float newDp = priceDropModel.getDp();
24564 amit.gupta 267
 
24410 amit.gupta 268
			if (newDp != oldDp) {
23884 amit.gupta 269
				List<Item> allItems = null;
270
				Item currentItem = itemRepository.selectById(priceDropModel.getItemId());
23945 amit.gupta 271
				if (priceDropModel.isAllColors()) {
23884 amit.gupta 272
					allItems = itemRepository.selectAllByCatalogItemId(currentItem.getCatalogItemId());
23945 amit.gupta 273
 
23884 amit.gupta 274
				} else {
23945 amit.gupta 275
					allItems = Arrays.asList(currentItem);
23884 amit.gupta 276
				}
23945 amit.gupta 277
				for (Item item : allItems) {
23884 amit.gupta 278
					TagListing itemTagListing = tagListingRepository.selectByItemId(item.getId());
23945 amit.gupta 279
					if (itemTagListing == null)
280
						continue;
23884 amit.gupta 281
					itemTagListing.setSellingPrice(newDp);
282
					itemTagListing.setMop(priceDropModel.getMop());
283
					tagListingRepository.persist(tagListing);
284
					List<VendorItemPricing> vipList = vendorItemPricingRepository.selectAll(item.getId());
23945 amit.gupta 285
					for (VendorItemPricing vip : vipList) {
24410 amit.gupta 286
						oldTp = vip.getNlc();
23884 amit.gupta 287
						vip.setDp(newDp);
288
						vip.setMop(priceDropModel.getMop());
24409 amit.gupta 289
						vip.setNlc(priceDropModel.getTp());
23884 amit.gupta 290
						vip.setTp(priceDropModel.getTp());
291
						vendorItemPricingRepository.persist(vip);
292
					}
293
					transactionService.updatePriceDrop(item.getId(), newDp);
294
				}
24822 amit.gupta 295
 
296
				//Add to itemPricing history
297
				ItemPricingHistory iph = new ItemPricingHistory();
298
				iph.setCatalogId(currentItem.getCatalogItemId());
299
				iph.setTp(priceDropModel.getTp());
300
				iph.setDp(priceDropModel.getDp());
301
				iph.setMop(priceDropModel.getMop());
302
				//TODO: changedBy
303
				iph.setChangedBy("me");
304
				iph.setCreateTimestamp(LocalDateTime.now());
305
				itemPricingHistoryRepository.persist(iph);
306
 
307
 
23951 amit.gupta 308
				PriceDrop priceDrop = new PriceDrop();
309
				priceDrop.setAffectedOn(priceDropModel.getAffectedDate());
24410 amit.gupta 310
				priceDrop.setTp(oldTp);
311
				priceDrop.setNlc(oldTp);
312
				priceDrop.setMop(oldMop);
24409 amit.gupta 313
				priceDrop.setOldDp(oldDp);
314
				priceDrop.setAmount(oldDp - newDp);
23968 amit.gupta 315
				priceDrop.setNewDp(newDp);
23951 amit.gupta 316
				priceDrop.setCreatedOn(LocalDateTime.now());
317
				priceDrop.setCatalogItemId(currentItem.getCatalogItemId());
318
				priceDropRepository.persist(priceDrop);
23884 amit.gupta 319
				response = true;
320
			} else {
23945 amit.gupta 321
				throw new ProfitMandiBusinessException("Price Drop", priceDropModel.getPd(),
24409 amit.gupta 322
						"Price Drop Should be greater than 0");
23884 amit.gupta 323
			}
324
		}
325
		model.addAttribute("response", mvcResponseSender.createResponseString(response));
326
		return "response";
327
	}
23945 amit.gupta 328
 
23819 govind 329
	@RequestMapping(value = "/downloadtotalPriceDropIMEI/{priceDropId}", method = RequestMethod.GET)
330
	public ResponseEntity<?> downloadTotalPriceDropIMEI(HttpServletRequest request, @PathVariable int priceDropId,
23945 amit.gupta 331
			Model model) throws ProfitMandiBusinessException, Exception {
23819 govind 332
 
333
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropId);
23968 amit.gupta 334
		Item item = itemRepository.selectAllByCatalogItemId(priceDrop.getCatalogItemId()).get(0);
335
		ByteArrayOutputStream baos = getByteArrayOutputStream(priceDrop.getAffectedOn(), priceDrop.getCatalogItemId());
336
		final HttpHeaders headers = new HttpHeaders();
337
		headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
23983 amit.gupta 338
		headers.set("Content-disposition",
339
				"inline; filename=pricedrop-" + item.getItemDescriptionNoColor().replaceAll("\\s?,\\s?", " ") + "-"
340
						+ FormattingUtils.formatDate(priceDrop.getAffectedOn()) + ".csv");
23968 amit.gupta 341
		byte[] byteArray = baos.toByteArray();
342
		headers.setContentLength(byteArray.length);
343
		return new ResponseEntity<ByteArrayResource>(new ByteArrayResource(byteArray), headers, HttpStatus.OK);
23951 amit.gupta 344
 
345
	}
346
 
24083 amit.gupta 347
	@RequestMapping(value = "/updatePriceDropImeis", method = RequestMethod.POST)
24822 amit.gupta 348
	public String updatePriceDropImeis(HttpServletRequest request,
24083 amit.gupta 349
			@RequestBody PriceDropImeisModel priceDropImeisModel, Model model)
350
			throws ProfitMandiBusinessException, Exception {
24168 amit.gupta 351
		PriceDropImeiStatus status = PriceDropImeiStatus.PENDING;
352
		;
24083 amit.gupta 353
 
354
		switch (priceDropImeisModel.getUpdatedStatus()) {
355
		case "pending": {
356
			status = PriceDropImeiStatus.PENDING;
357
			break;
358
		}
359
		case "approved": {
360
			status = PriceDropImeiStatus.APPROVED;
361
			break;
362
		}
363
		case "rejected": {
364
			status = PriceDropImeiStatus.REJECTED;
365
			break;
366
		}
367
 
368
		}
369
		List<PriceDropIMEI> priceDropIMEIs = priceDropIMEIRepository
370
				.selectByPriceDropId(priceDropImeisModel.getPriceDropId());
371
 
372
		List<PriceDropIMEI> priceDropIMEIsToProcess = new ArrayList<>();
373
		for (PriceDropIMEI priceDropIMEI : priceDropIMEIs) {
374
			if (!priceDropImeisModel.getUpdatedImeis().contains(priceDropIMEI.getImei())
375
					|| priceDropIMEI.getStatus().equals(PriceDropImeiStatus.APPROVED)
376
					|| priceDropIMEI.getStatus().equals(status)) {
377
				continue;
378
			} else {
24168 amit.gupta 379
				if (status.equals(PriceDropImeiStatus.APPROVED)) {
24083 amit.gupta 380
					priceDropIMEIsToProcess.add(priceDropIMEI);
381
				}
382
				priceDropIMEI.setStatus(status);
383
				priceDropIMEI.setUpdateTimestamp(LocalDateTime.now());
384
				priceDropIMEIRepository.persist(priceDropIMEI);
385
			}
386
		}
387
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropImeisModel.getPriceDropId());
388
		Item item = itemRepository.selectAllByCatalogItemId(priceDrop.getCatalogItemId()).get(0);
389
		String description = item.getItemDescriptionNoColor();
390
		if (priceDropIMEIsToProcess.size() > 0) {
24564 amit.gupta 391
 
24083 amit.gupta 392
			Map<Integer, List<InventoryItem>> pendingPartnerInventoryMap = priceDropService
24564 amit.gupta 393
					.getInventoryForPriceDrop(priceDropIMEIsToProcess, priceDrop).stream()
24083 amit.gupta 394
					.collect(Collectors.groupingBy(InventoryItem::getFofoId));
395
 
396
			for (Map.Entry<Integer, List<InventoryItem>> pendingPartnerInventory : pendingPartnerInventoryMap
397
					.entrySet()) {
398
				List<InventoryItem> fofoInventoryList = pendingPartnerInventory.getValue();
399
				int fofoId = pendingPartnerInventory.getKey();
400
 
401
				String reversalReason = MessageFormat.format(
402
						"Scheme  differential for Price Drop of Rs.{0} on {1}, on {3}. Total {2} item(s)",
403
						priceDrop.getAmount(), description, fofoInventoryList.size(),
404
						FormattingUtils.formatDate(priceDrop.getAffectedOn()));
405
				String aReason = MessageFormat.format(
406
						"Payout of Rs.{4} per unit for Price Drop of Rs.{0} on {1}, on {3}. Total {2} item(s)",
407
						priceDrop.getAmount(), description, fofoInventoryList.size(),
408
						FormattingUtils.formatDate(priceDrop.getAffectedOn()), priceDrop.getPartnerPayout());
409
				inventoryService.updatePriceDrop(fofoInventoryList, priceDrop.getAmount());
410
				if (item.getBrand().equals("Samsung")) {
24168 amit.gupta 411
					schemeService.reverseSchemes(fofoInventoryList, priceDrop.getId(), reversalReason);
24083 amit.gupta 412
				}
413
				walletService.addAmountToWallet(fofoId, priceDrop.getId(), WalletReferenceType.PRICE_DROP, aReason,
414
						priceDrop.getPartnerPayout() * fofoInventoryList.size());
415
			}
416
 
417
		}
24168 amit.gupta 418
 
24083 amit.gupta 419
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
420
		return "response";
421
 
422
	}
423
 
23983 amit.gupta 424
	private ByteArrayOutputStream getByteArrayOutputStream(LocalDateTime affectedOn, Integer catalogItemId)
425
			throws Exception {
24564 amit.gupta 426
		List<ImeiDropSummaryModel> imeiDropSummaryModelList = priceDropService
427
				.getAllSerialNumbersByAffectedDate(affectedOn, catalogItemId);
24822 amit.gupta 428
		List<List<? extends Serializable>> rows = new ArrayList<>();
23983 amit.gupta 429
		for (ImeiDropSummaryModel imeiDropSummaryModel : imeiDropSummaryModelList) {
23986 amit.gupta 430
			rows.add(this.getRow(imeiDropSummaryModel));
23951 amit.gupta 431
		}
432
		return FileUtil.getCSVByteStream(Arrays.asList("IMEI Number", "Store Name", "Store Code", "Item ID", "Brand",
433
				"Model Name", "Model Number", "Color", "Last Scanned"), rows);
23819 govind 434
	}
24083 amit.gupta 435
 
24822 amit.gupta 436
	private List<? extends Serializable> getRow(ImeiDropSummaryModel imeiDropSummaryModel) {
437
		List<Serializable> row = new ArrayList<>();
23986 amit.gupta 438
		row.add(imeiDropSummaryModel.getSerialNumber());
439
		row.add(imeiDropSummaryModel.getStoreName());
440
		row.add(imeiDropSummaryModel.getPartnerCode());
441
		row.add(imeiDropSummaryModel.getItemId());
442
		row.add(imeiDropSummaryModel.getBrand());
443
		row.add(imeiDropSummaryModel.getModelName());
444
		row.add(imeiDropSummaryModel.getModelNumber());
445
		row.add(imeiDropSummaryModel.getColor());
446
		row.add(FormattingUtils.formatReporitcoDate(imeiDropSummaryModel.getLastScanned()));
447
		return row;
448
	}
23819 govind 449
 
23884 amit.gupta 450
	private boolean validatePriceDrop(PriceDropModel priceDropModel) throws ProfitMandiBusinessException {
24564 amit.gupta 451
		if (priceDropModel.getMop() > 0 && priceDropModel.getDp() > 0 && priceDropModel.getTp() > 0) {
23884 amit.gupta 452
			return true;
453
		}
454
		return false;
455
	}
23945 amit.gupta 456
 
23819 govind 457
}