Subversion Repositories SmartDukaan

Rev

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