Subversion Repositories SmartDukaan

Rev

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