Subversion Repositories SmartDukaan

Rev

Rev 28117 | Rev 28136 | 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;
28102 tejbeer 4
import java.time.LocalDate;
23819 govind 5
import java.time.LocalDateTime;
6
import java.util.ArrayList;
23884 amit.gupta 7
import java.util.Arrays;
28102 tejbeer 8
import java.util.HashMap;
23819 govind 9
import java.util.List;
10
import java.util.Map;
28102 tejbeer 11
import java.util.Map.Entry;
24063 amit.gupta 12
import java.util.Set;
23819 govind 13
import java.util.stream.Collectors;
14
 
15
import javax.servlet.http.HttpServletRequest;
16
 
23951 amit.gupta 17
import org.apache.commons.io.output.ByteArrayOutputStream;
23819 govind 18
import org.apache.logging.log4j.LogManager;
19
import org.apache.logging.log4j.Logger;
20
import org.springframework.beans.factory.annotation.Autowired;
21
import org.springframework.beans.factory.annotation.Qualifier;
23951 amit.gupta 22
import org.springframework.core.io.ByteArrayResource;
23
import org.springframework.http.HttpHeaders;
23819 govind 24
import org.springframework.http.HttpStatus;
25
import org.springframework.http.ResponseEntity;
26
import org.springframework.stereotype.Controller;
25161 amit.gupta 27
import org.springframework.transaction.annotation.Transactional;
23819 govind 28
import org.springframework.ui.Model;
29
import org.springframework.web.bind.annotation.PathVariable;
23884 amit.gupta 30
import org.springframework.web.bind.annotation.RequestBody;
23819 govind 31
import org.springframework.web.bind.annotation.RequestMapping;
32
import org.springframework.web.bind.annotation.RequestMethod;
23951 amit.gupta 33
import org.springframework.web.bind.annotation.RequestParam;
23819 govind 34
 
23945 amit.gupta 35
import com.fasterxml.jackson.databind.ObjectMapper;
23819 govind 36
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
28102 tejbeer 37
import com.spice.profitmandi.common.model.CustomRetailer;
23951 amit.gupta 38
import com.spice.profitmandi.common.model.ImeiDropSummaryModel;
23884 amit.gupta 39
import com.spice.profitmandi.common.model.ItemDescriptionModel;
24083 amit.gupta 40
import com.spice.profitmandi.common.model.PriceDropImeisModel;
23884 amit.gupta 41
import com.spice.profitmandi.common.model.PriceDropModel;
23968 amit.gupta 42
import com.spice.profitmandi.common.model.PriceDropProcessModel;
23945 amit.gupta 43
import com.spice.profitmandi.common.services.ReporticoService;
23951 amit.gupta 44
import com.spice.profitmandi.common.util.FileUtil;
23945 amit.gupta 45
import com.spice.profitmandi.common.util.FormattingUtils;
28102 tejbeer 46
import com.spice.profitmandi.common.util.Utils;
23819 govind 47
import com.spice.profitmandi.dao.entity.catalog.Item;
23884 amit.gupta 48
import com.spice.profitmandi.dao.entity.catalog.TagListing;
28102 tejbeer 49
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
24822 amit.gupta 50
import com.spice.profitmandi.dao.entity.inventory.ItemPricingHistory;
23884 amit.gupta 51
import com.spice.profitmandi.dao.entity.inventory.VendorItemPricing;
23819 govind 52
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
24083 amit.gupta 53
import com.spice.profitmandi.dao.entity.transaction.PriceDropIMEI;
54
import com.spice.profitmandi.dao.enumuration.transaction.PriceDropImeiStatus;
28102 tejbeer 55
import com.spice.profitmandi.dao.model.PriceCircularModel;
23819 govind 56
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
23884 amit.gupta 57
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
28102 tejbeer 58
import com.spice.profitmandi.dao.repository.dtr.Mongo;
59
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
24822 amit.gupta 60
import com.spice.profitmandi.dao.repository.inventory.ItemPricingHistoryRepository;
23884 amit.gupta 61
import com.spice.profitmandi.dao.repository.inventory.VendorItemPricingRepository;
23983 amit.gupta 62
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
24083 amit.gupta 63
import com.spice.profitmandi.dao.repository.transaction.PriceDropIMEIRepository;
23819 govind 64
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
65
import com.spice.profitmandi.service.inventory.InventoryService;
23983 amit.gupta 66
import com.spice.profitmandi.service.pricing.PriceDropService;
23968 amit.gupta 67
import com.spice.profitmandi.service.scheme.SchemeService;
23884 amit.gupta 68
import com.spice.profitmandi.service.transaction.TransactionService;
28102 tejbeer 69
import com.spice.profitmandi.service.user.RetailerService;
23819 govind 70
import com.spice.profitmandi.service.wallet.WalletService;
28102 tejbeer 71
import com.spice.profitmandi.web.model.LoginDetails;
72
import com.spice.profitmandi.web.util.CookiesProcessor;
23819 govind 73
import com.spice.profitmandi.web.util.MVCResponseSender;
74
 
75
@Controller
27071 amit.gupta 76
@Transactional(rollbackFor = Throwable.class)
23819 govind 77
public class PriceDropController {
78
 
79
	private static final Logger LOGGER = LogManager.getLogger(PriceDropController.class);
80
 
81
	@Autowired
82
	private PriceDropRepository priceDropRepository;
83
 
23951 amit.gupta 84
	@Autowired
85
	private ObjectMapper objectMapper;
23945 amit.gupta 86
 
23819 govind 87
	@Autowired
23884 amit.gupta 88
	private VendorItemPricingRepository vendorItemPricingRepository;
23819 govind 89
 
90
	@Autowired
23884 amit.gupta 91
	@Qualifier("fofoInventoryService")
92
	private InventoryService inventoryService;
23945 amit.gupta 93
 
23884 amit.gupta 94
	@Autowired
23819 govind 95
	private MVCResponseSender mvcResponseSender;
96
 
97
	@Autowired
23983 amit.gupta 98
	private PriceDropService priceDropService;
27071 amit.gupta 99
 
24822 amit.gupta 100
	@Autowired
101
	private ItemPricingHistoryRepository itemPricingHistoryRepository;
24083 amit.gupta 102
 
23983 amit.gupta 103
	@Autowired
23819 govind 104
	private WalletService walletService;
105
 
106
	@Autowired
23884 amit.gupta 107
	private TagListingRepository tagListingRepository;
23945 amit.gupta 108
 
23884 amit.gupta 109
	@Autowired
110
	private TransactionService transactionService;
23819 govind 111
 
112
	@Autowired
24083 amit.gupta 113
	private PriceDropIMEIRepository priceDropIMEIRepository;
114
 
115
	@Autowired
23819 govind 116
	@Qualifier("catalogItemRepository")
117
	private ItemRepository itemRepository;
118
 
23945 amit.gupta 119
	@Autowired
23968 amit.gupta 120
	private SchemeService schemeService;
24083 amit.gupta 121
 
23986 amit.gupta 122
	@Autowired
23945 amit.gupta 123
	ReporticoService reporticoService;
24083 amit.gupta 124
 
23983 amit.gupta 125
	@Autowired
126
	LineItemImeisRepository lineItemImeisRepository;
23945 amit.gupta 127
 
28102 tejbeer 128
	@Autowired
129
	private Mongo mongoClient;
130
 
131
	@Autowired
132
	private CookiesProcessor cookiesProcessor;
133
 
134
	@Autowired
135
	private PartnerTypeChangeService partnerTypeChangeService;
136
 
137
	@Autowired
138
	private RetailerService retailerService;
139
 
23819 govind 140
	@RequestMapping(value = "/getItemDescription", method = RequestMethod.GET)
23945 amit.gupta 141
	public String getItemDescription(HttpServletRequest request, Model model) throws Throwable {
23819 govind 142
		List<PriceDrop> priceDrops = priceDropRepository.selectAll();
24191 amit.gupta 143
		Set<Integer> catalogIds = priceDrops.stream().map(x -> x.getCatalogItemId()).collect(Collectors.toSet());
24180 amit.gupta 144
		List<Item> items = itemRepository.selectAllByCatalogIds(catalogIds);
24191 amit.gupta 145
		Map<Integer, String> catalogDescription = items.stream().collect(Collectors.toMap(x -> x.getCatalogItemId(),
146
				x -> x.getItemDescriptionNoColor(), (description1, description2) -> description1));
23819 govind 147
		model.addAttribute("priceDrops", priceDrops);
23968 amit.gupta 148
		model.addAttribute("catalogDescription", catalogDescription);
23819 govind 149
		return "price-drop";
150
	}
24083 amit.gupta 151
 
24049 amit.gupta 152
	@RequestMapping(value = "/item-pricing/{itemId}", method = RequestMethod.GET)
153
	public String getItemPricing(HttpServletRequest request, Model model, @PathVariable int itemId) throws Throwable {
24083 amit.gupta 154
 
24063 amit.gupta 155
		TagListing tagListing;
24049 amit.gupta 156
		PriceDropModel pm = new PriceDropModel();
24063 amit.gupta 157
		try {
158
			tagListing = tagListingRepository.selectByItemId(itemId);
24083 amit.gupta 159
			if (tagListing != null) {
24063 amit.gupta 160
				pm.setMop(tagListing.getMop());
161
				pm.setDp(tagListing.getSellingPrice());
162
				pm.setMrp(tagListing.getMrp());
163
				List<VendorItemPricing> vips = vendorItemPricingRepository.selectAll(itemId);
24083 amit.gupta 164
				if (vips.size() > 0) {
24063 amit.gupta 165
					VendorItemPricing vip = vips.get(0);
166
					pm.setNlc(vip.getNlc());
167
					pm.setTp(vip.getTp());
168
				} else {
169
					throw new ProfitMandiBusinessException("Item Id", itemId, "Vendor item pricing does not exist");
170
				}
171
			}
24083 amit.gupta 172
		} catch (Exception e) {
24063 amit.gupta 173
			LOGGER.info("Chose item that doesn't exist");
24049 amit.gupta 174
		}
175
		model.addAttribute("response", mvcResponseSender.createResponseString(pm));
176
		return "response";
177
	}
24083 amit.gupta 178
 
24168 amit.gupta 179
	@RequestMapping(value = "/item", method = RequestMethod.GET)
28102 tejbeer 180
	public String getItemPricing(HttpServletRequest request, Model model, @RequestParam String query,
181
			@RequestParam boolean anyColor) throws Throwable {
24383 amit.gupta 182
		String query1 = query.toLowerCase();
28102 tejbeer 183
 
184
		List<ItemDescriptionModel> partnersItemDescription = inventoryService
185
				.getAllPartnerItemStringDescription(anyColor).parallelStream()
186
				.filter(x -> x.getItemDescription().toLowerCase().matches(".*?" + query1 + ".*?"))
24564 amit.gupta 187
				.collect(Collectors.toList());
28102 tejbeer 188
		LOGGER.info("partnersItemDescription" + partnersItemDescription);
24168 amit.gupta 189
 
24191 amit.gupta 190
		model.addAttribute("response", mvcResponseSender.createResponseString(partnersItemDescription));
24168 amit.gupta 191
		return "response";
192
	}
193
 
23951 amit.gupta 194
	@RequestMapping(value = "/price-drop/imes/download")
195
	public ResponseEntity<ByteArrayResource> downloadPriceDropImeis(HttpServletRequest request,
196
			@RequestParam LocalDateTime affectedDate, @RequestParam int itemId) throws Exception {
197
		Item item = itemRepository.selectById(itemId);
26368 amit.gupta 198
		ByteArrayOutputStream baos = getByteArrayOutputStream(affectedDate, item.getCatalogItemId(), null);
23951 amit.gupta 199
		final HttpHeaders headers = new HttpHeaders();
200
		headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
25408 amit.gupta 201
		headers.set("Content-disposition", "inline; filename=\"imei-" + item.getItemDescriptionNoColor() + ".csv\"");
23951 amit.gupta 202
		byte[] byteArray = baos.toByteArray();
203
		headers.setContentLength(byteArray.length);
204
		return new ResponseEntity<ByteArrayResource>(new ByteArrayResource(byteArray), headers, HttpStatus.OK);
205
	}
206
 
24083 amit.gupta 207
	@RequestMapping(value = "/price-drop/addPayout", method = RequestMethod.POST)
208
	public String updatePriceDrop(HttpServletRequest request, @RequestBody PriceDropProcessModel priceDropProcessModel,
209
			Model model) throws Exception {
210
		boolean response = false;
211
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropProcessModel.getPriceDropId());
212
		if (priceDrop.getProcessTimestamp() == null) {
213
			priceDrop.setPartnerPayout(priceDropProcessModel.getPartnerPayout());
214
			priceDrop.setPriceDropIn(priceDropProcessModel.getPriceDropIn());
215
			// priceDrop.setProcessTimestamp(LocalDateTime.now());
216
			priceDropRepository.persist(priceDrop);
217
			response = true;
218
		}
219
		model.addAttribute("response", mvcResponseSender.createResponseString(response));
220
		return "response";
221
	}
222
 
223
	@RequestMapping(value = "/priceDropImeis/{priceDropId}", method = RequestMethod.GET)
224
	public String priceDropStatus(HttpServletRequest request, @PathVariable int priceDropId, Model model)
225
			throws Exception {
226
 
227
		PriceDropImeisModel priceDropImeisModel = new PriceDropImeisModel();
24564 amit.gupta 228
 
229
		// This call is used to persit imeis to pricedrop imeis in case its not there.
27071 amit.gupta 230
		// This should be called while creating price drop.
231
		// priceDropService.priceDropStatus(priceDropId);
24564 amit.gupta 232
 
24083 amit.gupta 233
		List<String> pendingImeis = new ArrayList<>();
234
		List<String> approvedImeis = new ArrayList<>();
235
		List<String> rejectedImeis = new ArrayList<>();
236
		List<PriceDropIMEI> priceDropImeis = priceDropIMEIRepository.selectByPriceDropId(priceDropId);
237
		for (PriceDropIMEI priceDropIMEI : priceDropImeis) {
238
			if (priceDropIMEI.getStatus().equals(PriceDropImeiStatus.PENDING)) {
239
				pendingImeis.add(priceDropIMEI.getImei());
240
			} else if (priceDropIMEI.getStatus().equals(PriceDropImeiStatus.APPROVED)) {
241
				approvedImeis.add(priceDropIMEI.getImei());
242
			}
243
			if (priceDropIMEI.getStatus().equals(PriceDropImeiStatus.REJECTED)) {
244
				rejectedImeis.add(priceDropIMEI.getImei());
245
			}
246
		}
247
		priceDropImeisModel.setPendingImeis(pendingImeis);
248
		priceDropImeisModel.setPriceDropId(priceDropId);
249
		priceDropImeisModel.setApprovedImeis(approvedImeis);
250
		priceDropImeisModel.setRejectedImeis(rejectedImeis);
251
		model.addAttribute("response", mvcResponseSender.createResponseString(priceDropImeisModel));
252
		return "response";
253
	}
254
 
23968 amit.gupta 255
	@RequestMapping(value = "/processPriceDrop", method = RequestMethod.POST)
27071 amit.gupta 256
	public String processPriceDrop(HttpServletRequest request, @RequestBody PriceDropProcessModel priceDropProcessModel,
23968 amit.gupta 257
			Model model) throws Exception {
27071 amit.gupta 258
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropProcessModel.getPriceDropId());
23968 amit.gupta 259
		boolean response = false;
28102 tejbeer 260
		if (priceDrop.getPartnerPayout() == 0) {
27071 amit.gupta 261
			priceDrop.setPartnerPayout(priceDropProcessModel.getPartnerPayout());
262
		}
263
		priceDrop.setProcessTimestamp(LocalDateTime.now());
264
		priceDropRepository.persist(priceDrop);
265
		priceDropService.processPriceDrop(priceDrop.getId(), priceDropProcessModel.isActivatedOnly());
28102 tejbeer 266
		response = true;
23968 amit.gupta 267
		model.addAttribute("response", mvcResponseSender.createResponseString(response));
268
		return "response";
23819 govind 269
	}
24083 amit.gupta 270
 
23884 amit.gupta 271
	@RequestMapping(value = "/priceDrop", method = RequestMethod.POST)
23945 amit.gupta 272
	public String addPriceDrop(HttpServletRequest request, Model model, @RequestBody PriceDropModel priceDropModel)
273
			throws Exception {
23884 amit.gupta 274
		boolean response = false;
23968 amit.gupta 275
		priceDropModel.setAllColors(true);
23945 amit.gupta 276
		if (this.validatePriceDrop(priceDropModel)) {
23884 amit.gupta 277
			TagListing tagListing = tagListingRepository.selectByItemId(priceDropModel.getItemId());
24409 amit.gupta 278
			float oldDp = tagListing.getSellingPrice();
24410 amit.gupta 279
			float oldMop = tagListing.getMop();
280
			float oldTp = 0;
24409 amit.gupta 281
			float newDp = priceDropModel.getDp();
24564 amit.gupta 282
 
24410 amit.gupta 283
			if (newDp != oldDp) {
23884 amit.gupta 284
				List<Item> allItems = null;
285
				Item currentItem = itemRepository.selectById(priceDropModel.getItemId());
23945 amit.gupta 286
				if (priceDropModel.isAllColors()) {
23884 amit.gupta 287
					allItems = itemRepository.selectAllByCatalogItemId(currentItem.getCatalogItemId());
23945 amit.gupta 288
 
23884 amit.gupta 289
				} else {
23945 amit.gupta 290
					allItems = Arrays.asList(currentItem);
23884 amit.gupta 291
				}
23945 amit.gupta 292
				for (Item item : allItems) {
23884 amit.gupta 293
					TagListing itemTagListing = tagListingRepository.selectByItemId(item.getId());
23945 amit.gupta 294
					if (itemTagListing == null)
295
						continue;
23884 amit.gupta 296
					itemTagListing.setSellingPrice(newDp);
297
					itemTagListing.setMop(priceDropModel.getMop());
298
					tagListingRepository.persist(tagListing);
299
					List<VendorItemPricing> vipList = vendorItemPricingRepository.selectAll(item.getId());
23945 amit.gupta 300
					for (VendorItemPricing vip : vipList) {
24410 amit.gupta 301
						oldTp = vip.getNlc();
23884 amit.gupta 302
						vip.setDp(newDp);
303
						vip.setMop(priceDropModel.getMop());
24409 amit.gupta 304
						vip.setNlc(priceDropModel.getTp());
23884 amit.gupta 305
						vip.setTp(priceDropModel.getTp());
306
						vendorItemPricingRepository.persist(vip);
307
					}
308
					transactionService.updatePriceDrop(item.getId(), newDp);
309
				}
27071 amit.gupta 310
 
311
				// Add to itemPricing history
24822 amit.gupta 312
				ItemPricingHistory iph = new ItemPricingHistory();
313
				iph.setCatalogId(currentItem.getCatalogItemId());
314
				iph.setTp(priceDropModel.getTp());
315
				iph.setDp(priceDropModel.getDp());
316
				iph.setMop(priceDropModel.getMop());
27071 amit.gupta 317
				// TODO: changedBy
24822 amit.gupta 318
				iph.setChangedBy("me");
319
				iph.setCreateTimestamp(LocalDateTime.now());
320
				itemPricingHistoryRepository.persist(iph);
27071 amit.gupta 321
 
23951 amit.gupta 322
				PriceDrop priceDrop = new PriceDrop();
323
				priceDrop.setAffectedOn(priceDropModel.getAffectedDate());
24410 amit.gupta 324
				priceDrop.setTp(oldTp);
325
				priceDrop.setNlc(oldTp);
326
				priceDrop.setMop(oldMop);
24409 amit.gupta 327
				priceDrop.setOldDp(oldDp);
328
				priceDrop.setAmount(oldDp - newDp);
23968 amit.gupta 329
				priceDrop.setNewDp(newDp);
23951 amit.gupta 330
				priceDrop.setCreatedOn(LocalDateTime.now());
331
				priceDrop.setCatalogItemId(currentItem.getCatalogItemId());
332
				priceDropRepository.persist(priceDrop);
26116 amit.gupta 333
				priceDropService.priceDropStatus(priceDrop.getId());
23884 amit.gupta 334
				response = true;
335
			} else {
23945 amit.gupta 336
				throw new ProfitMandiBusinessException("Price Drop", priceDropModel.getPd(),
24409 amit.gupta 337
						"Price Drop Should be greater than 0");
23884 amit.gupta 338
			}
339
		}
340
		model.addAttribute("response", mvcResponseSender.createResponseString(response));
341
		return "response";
342
	}
23945 amit.gupta 343
 
23819 govind 344
	@RequestMapping(value = "/downloadtotalPriceDropIMEI/{priceDropId}", method = RequestMethod.GET)
345
	public ResponseEntity<?> downloadTotalPriceDropIMEI(HttpServletRequest request, @PathVariable int priceDropId,
23945 amit.gupta 346
			Model model) throws ProfitMandiBusinessException, Exception {
23819 govind 347
 
348
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropId);
27071 amit.gupta 349
		Map<String, PriceDropIMEI> priceDropIMEIsMap = priceDropIMEIRepository.selectByPriceDropId(priceDropId).stream()
350
				.collect(Collectors.toMap(x -> x.getImei(), x -> x));
351
 
352
		if (priceDropIMEIsMap.size() == 0 && priceDrop.getProcessTimestamp() == null) {
26372 amit.gupta 353
			priceDrop.setProcessTimestamp(LocalDateTime.now());
354
		} else {
355
			priceDropService.priceDropStatus(priceDrop.getId());
356
		}
27071 amit.gupta 357
 
23968 amit.gupta 358
		Item item = itemRepository.selectAllByCatalogItemId(priceDrop.getCatalogItemId()).get(0);
27071 amit.gupta 359
		ByteArrayOutputStream baos = getByteArrayOutputStream(priceDrop.getAffectedOn(), priceDrop.getCatalogItemId(),
360
				priceDropIMEIsMap);
23968 amit.gupta 361
		final HttpHeaders headers = new HttpHeaders();
362
		headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
23983 amit.gupta 363
		headers.set("Content-disposition",
364
				"inline; filename=pricedrop-" + item.getItemDescriptionNoColor().replaceAll("\\s?,\\s?", " ") + "-"
365
						+ FormattingUtils.formatDate(priceDrop.getAffectedOn()) + ".csv");
23968 amit.gupta 366
		byte[] byteArray = baos.toByteArray();
367
		headers.setContentLength(byteArray.length);
368
		return new ResponseEntity<ByteArrayResource>(new ByteArrayResource(byteArray), headers, HttpStatus.OK);
23951 amit.gupta 369
 
370
	}
371
 
26400 amit.gupta 372
	@RequestMapping(value = "/updatePriceDropImeis-DNC", method = RequestMethod.POST)
27071 amit.gupta 373
	public String updatePriceDropImeis(HttpServletRequest request, @RequestBody PriceDropImeisModel priceDropImeisModel,
374
			Model model) throws ProfitMandiBusinessException, Exception {
24168 amit.gupta 375
		PriceDropImeiStatus status = PriceDropImeiStatus.PENDING;
24083 amit.gupta 376
 
377
		switch (priceDropImeisModel.getUpdatedStatus()) {
27071 amit.gupta 378
		case "approved": {
379
			status = PriceDropImeiStatus.APPROVED_OURS;
380
			break;
24083 amit.gupta 381
		}
27071 amit.gupta 382
		case "rejected": {
383
			status = PriceDropImeiStatus.REJECTED;
384
			break;
24083 amit.gupta 385
		}
27071 amit.gupta 386
		}
387
 
388
		if (PriceDropImeiStatus.PENDING.equals(status)) {
389
			throw new ProfitMandiBusinessException("INVALID STATUS", "PENDING",
390
					"only approved and rejected is allowed");
391
		}
392
 
393
		// TODO:PD
24083 amit.gupta 394
		List<PriceDropIMEI> priceDropIMEIs = priceDropIMEIRepository
395
				.selectByPriceDropId(priceDropImeisModel.getPriceDropId());
396
 
397
		List<PriceDropIMEI> priceDropIMEIsToProcess = new ArrayList<>();
398
		for (PriceDropIMEI priceDropIMEI : priceDropIMEIs) {
399
			if (!priceDropImeisModel.getUpdatedImeis().contains(priceDropIMEI.getImei())
400
					|| priceDropIMEI.getStatus().equals(status)) {
401
				continue;
402
			}
26368 amit.gupta 403
			priceDropIMEIsToProcess.add(priceDropIMEI);
27071 amit.gupta 404
			if (status.equals(PriceDropImeiStatus.REJECTED)) {
26368 amit.gupta 405
				priceDropIMEI.setRejectionReason(priceDropImeisModel.getRejectionReason());
406
			}
407
			priceDropIMEI.setUpdateTimestamp(LocalDateTime.now());
24083 amit.gupta 408
		}
27071 amit.gupta 409
		/*
410
		 * if (priceDropIMEIsToProcess.size() > 0) {
411
		 * priceDropService.processManualPriceDrop(priceDropImeisModel.getPriceDropId(),
412
		 * priceDropIMEIsToProcess, status); }
413
		 */
24168 amit.gupta 414
 
24083 amit.gupta 415
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
416
		return "response";
417
 
418
	}
419
 
27071 amit.gupta 420
	private ByteArrayOutputStream getByteArrayOutputStream(LocalDateTime affectedOn, Integer catalogItemId,
421
			Map<String, PriceDropIMEI> priceDropImeis) throws Exception {
24564 amit.gupta 422
		List<ImeiDropSummaryModel> imeiDropSummaryModelList = priceDropService
423
				.getAllSerialNumbersByAffectedDate(affectedOn, catalogItemId);
25852 amit.gupta 424
		List<List<?>> rows = new ArrayList<>();
23983 amit.gupta 425
		for (ImeiDropSummaryModel imeiDropSummaryModel : imeiDropSummaryModelList) {
27071 amit.gupta 426
			if (priceDropImeis == null) {
26368 amit.gupta 427
				rows.add(this.getRow(imeiDropSummaryModel, null));
27071 amit.gupta 428
			} else if (priceDropImeis.get(imeiDropSummaryModel.getSerialNumber()) != null) {
26368 amit.gupta 429
				rows.add(this.getRow(imeiDropSummaryModel, priceDropImeis.get(imeiDropSummaryModel.getSerialNumber())));
430
			}
23951 amit.gupta 431
		}
27071 amit.gupta 432
		return FileUtil.getCSVByteStream(Arrays.asList("IMEI Number", "Store Name", "Store Code", "Item ID", "Brand",
433
				"Model Name", "Model Number", "Color", "Status", "Rejection Reason", "Last Scanned", "Vendor Name",
434
				"Grn On", "Activation Timestamp", "Activation Added On"), rows);
23819 govind 435
	}
24083 amit.gupta 436
 
27071 amit.gupta 437
	private List<? extends Serializable> getRow(ImeiDropSummaryModel imeiDropSummaryModel,
438
			PriceDropIMEI priceDropIMEI) {
24822 amit.gupta 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());
27071 amit.gupta 448
		if (priceDropIMEI != null) {
26368 amit.gupta 449
			row.add(priceDropIMEI.getStatus());
450
			row.add(priceDropIMEI.getRejectionReason());
451
		} else {
452
			row.add(PriceDropImeiStatus.PENDING);
453
			row.add("");
454
		}
26373 amit.gupta 455
		row.add(FormattingUtils.formatReporitcoDate(imeiDropSummaryModel.getLastScanned()));
456
		row.add(imeiDropSummaryModel.getVendorName());
26420 amit.gupta 457
		row.add(FormattingUtils.formatReporitcoDate(imeiDropSummaryModel.getGrnOn()));
26368 amit.gupta 458
		row.add(imeiDropSummaryModel.getActivationTimestamp());
459
		row.add(imeiDropSummaryModel.getActivationAddedOn());
23986 amit.gupta 460
		return row;
461
	}
23819 govind 462
 
23884 amit.gupta 463
	private boolean validatePriceDrop(PriceDropModel priceDropModel) throws ProfitMandiBusinessException {
24564 amit.gupta 464
		if (priceDropModel.getMop() > 0 && priceDropModel.getDp() > 0 && priceDropModel.getTp() > 0) {
23884 amit.gupta 465
			return true;
466
		}
467
		return false;
468
	}
23945 amit.gupta 469
 
28102 tejbeer 470
	@RequestMapping(value = "/priceCircular")
471
	public String priceCircular(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
472
		int fofoId = Utils.SYSTEM_PARTNER_ID;
473
		Set<String> brands = mongoClient.getMongoBrands(fofoId, null, 3).stream().map(x -> (String) x.get("name"))
474
				.collect(Collectors.toSet());
475
		Map<Integer, CustomRetailer> customRetailers = retailerService.getFofoRetailers(true);
476
		Map<Integer, PartnerType> fofoIdPartnerTypeMap = new HashMap<>();
477
 
478
		for (Entry<Integer, CustomRetailer> customRetailer : customRetailers.entrySet()) {
479
			PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(customRetailer.getKey(), LocalDate.now());
480
			fofoIdPartnerTypeMap.put(customRetailer.getKey(), partnerType);
481
		}
482
 
483
		Map<PartnerType, List<Integer>> partnerTypePartnersMap = fofoIdPartnerTypeMap.entrySet().stream().collect(
484
				Collectors.groupingBy(x -> x.getValue(), Collectors.mapping(x -> x.getKey(), Collectors.toList())));
485
		model.addAttribute("brands", brands);
486
 
487
		return "price-circular";
488
	}
489
 
490
	@RequestMapping(value = "/partnerPriceCircular")
491
	public String partnerPriceCircular(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
492
		int fofoId = Utils.SYSTEM_PARTNER_ID;
493
		Set<String> brands = mongoClient.getMongoBrands(fofoId, null, 3).stream().map(x -> (String) x.get("name"))
494
				.collect(Collectors.toSet());
495
		brands.addAll(mongoClient.getMongoBrands(fofoId, null, 6).stream().map(x -> (String) x.get("name"))
496
				.collect(Collectors.toSet()));
497
		model.addAttribute("brands", brands);
498
		model.addAttribute("date", LocalDate.now());
499
 
500
		return "partner-price-circular";
501
	}
502
 
503
	@RequestMapping(value = "/priceCircularByBrand")
504
	public String priceCircularByBrand(HttpServletRequest request, @RequestParam String brand, Model model)
505
			throws ProfitMandiBusinessException {
506
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
507
 
508
		PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(loginDetails.getFofoId(), LocalDate.now());
509
		List<PartnerType> partnerTypes = new ArrayList<>();
510
		partnerTypes.add(partnerType);
511
		partnerTypes.add(PartnerType.ALL);
512
		List<PriceCircularModel> priceCircular = tagListingRepository.getPriceCircularByBrandAndType(partnerTypes,
513
				brand, LocalDateTime.now(), LocalDateTime.now().plusDays(1).toLocalDate().atStartOfDay());
514
 
515
		for (PriceCircularModel pc : priceCircular) {
516
 
517
			Long totalScheme = pc.getBasePayout() + pc.getCashDiscount() + pc.getTertiaryPayout()
518
					+ pc.getHygienePayout() + pc.getCategoryPayout() + pc.getInvestmentPayout() + pc.getModelSpecfic();
519
			pc.setTotalScheme(totalScheme);
520
			long netprice = pc.getSellingPrice() - pc.getTotalScheme();
521
			pc.setNetPrice(netprice);
522
			int mopdpdiff = pc.getMop() - pc.getSellingPrice();
523
			pc.setMopdp(mopdpdiff);
524
			pc.setNetPrice2((int) pc.getNetPrice() - pc.getMopdp());
525
			long totalProfit = totalScheme + mopdpdiff;
28124 tejbeer 526
			pc.setTotalProfit((int) totalProfit);
28102 tejbeer 527
			double rouoff = totalProfit / (pc.getSellingPrice() / 1.18) * 100;
528
			double roundOff = Math.round(rouoff * 100.0) / 100.0;
529
			pc.setNetMargin(roundOff);
530
 
531
		}
532
		LOGGER.info("priceCircular" + priceCircular);
533
		model.addAttribute("priceCircular", priceCircular);
28117 tejbeer 534
		model.addAttribute("partnerId", loginDetails.getFofoId());
28102 tejbeer 535
		return "price-circular-detail";
536
	}
537
 
23819 govind 538
}