Subversion Repositories SmartDukaan

Rev

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