Subversion Repositories SmartDukaan

Rev

Rev 24019 | Rev 24050 | 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;
12
import java.util.stream.Collectors;
13
 
14
import javax.servlet.http.HttpServletRequest;
15
import javax.transaction.Transactional;
16
 
23951 amit.gupta 17
import org.apache.commons.io.output.ByteArrayOutputStream;
18
import org.apache.commons.lang.StringUtils;
23945 amit.gupta 19
import org.apache.http.HttpResponse;
23819 govind 20
import org.apache.logging.log4j.LogManager;
21
import org.apache.logging.log4j.Logger;
23884 amit.gupta 22
import org.json.JSONArray;
23819 govind 23
import org.springframework.beans.factory.annotation.Autowired;
24
import org.springframework.beans.factory.annotation.Qualifier;
23951 amit.gupta 25
import org.springframework.core.io.ByteArrayResource;
26
import org.springframework.http.HttpHeaders;
23819 govind 27
import org.springframework.http.HttpStatus;
28
import org.springframework.http.ResponseEntity;
29
import org.springframework.stereotype.Controller;
30
import org.springframework.ui.Model;
31
import org.springframework.web.bind.annotation.PathVariable;
23884 amit.gupta 32
import org.springframework.web.bind.annotation.RequestBody;
23819 govind 33
import org.springframework.web.bind.annotation.RequestMapping;
34
import org.springframework.web.bind.annotation.RequestMethod;
23951 amit.gupta 35
import org.springframework.web.bind.annotation.RequestParam;
23819 govind 36
 
23951 amit.gupta 37
import com.fasterxml.jackson.databind.JavaType;
23945 amit.gupta 38
import com.fasterxml.jackson.databind.ObjectMapper;
39
import com.fasterxml.jackson.databind.ObjectReader;
40
import com.spice.profitmandi.common.enumuration.ReporticoProject;
23819 govind 41
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23945 amit.gupta 42
import com.spice.profitmandi.common.model.ClosingStockOnDate;
43
import com.spice.profitmandi.common.model.GrnPendingDataModel;
23951 amit.gupta 44
import com.spice.profitmandi.common.model.ImeiDropSummaryModel;
23884 amit.gupta 45
import com.spice.profitmandi.common.model.ItemDescriptionModel;
23945 amit.gupta 46
import com.spice.profitmandi.common.model.PartnerImeiNotSold;
23884 amit.gupta 47
import com.spice.profitmandi.common.model.PriceDropModel;
23968 amit.gupta 48
import com.spice.profitmandi.common.model.PriceDropProcessModel;
23945 amit.gupta 49
import com.spice.profitmandi.common.model.ReporticoResponseModel;
50
import com.spice.profitmandi.common.services.ReporticoService;
23951 amit.gupta 51
import com.spice.profitmandi.common.util.FileUtil;
23945 amit.gupta 52
import com.spice.profitmandi.common.util.FormattingUtils;
23819 govind 53
import com.spice.profitmandi.dao.entity.catalog.Item;
23884 amit.gupta 54
import com.spice.profitmandi.dao.entity.catalog.TagListing;
23968 amit.gupta 55
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
23884 amit.gupta 56
import com.spice.profitmandi.dao.entity.inventory.VendorItemPricing;
23983 amit.gupta 57
import com.spice.profitmandi.dao.entity.transaction.LineItemImei;
23819 govind 58
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
59
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
23884 amit.gupta 60
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
23986 amit.gupta 61
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
62
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
23884 amit.gupta 63
import com.spice.profitmandi.dao.repository.inventory.VendorItemPricingRepository;
23983 amit.gupta 64
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
23819 govind 65
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
66
import com.spice.profitmandi.service.inventory.InventoryService;
23983 amit.gupta 67
import com.spice.profitmandi.service.pricing.PriceDropService;
23968 amit.gupta 68
import com.spice.profitmandi.service.scheme.SchemeService;
23884 amit.gupta 69
import com.spice.profitmandi.service.transaction.TransactionService;
23819 govind 70
import com.spice.profitmandi.service.wallet.WalletService;
71
import com.spice.profitmandi.web.util.MVCResponseSender;
72
 
23968 amit.gupta 73
import in.shop2020.model.v1.order.WalletReferenceType;
74
 
23819 govind 75
@Controller
76
@Transactional
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;
99
 
100
	@Autowired
23986 amit.gupta 101
	private InventoryItemRepository inventoryItemRepository;
102
 
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
113
	@Qualifier("catalogItemRepository")
114
	private ItemRepository itemRepository;
115
 
23945 amit.gupta 116
	@Autowired
23968 amit.gupta 117
	private SchemeService schemeService;
23986 amit.gupta 118
 
119
	@Autowired
120
	private SchemeInOutRepository schemeInOutRepository;
23968 amit.gupta 121
 
122
	@Autowired
23945 amit.gupta 123
	ReporticoService reporticoService;
23983 amit.gupta 124
 
125
	@Autowired
126
	LineItemImeisRepository lineItemImeisRepository;
23945 amit.gupta 127
 
23819 govind 128
	@RequestMapping(value = "/getItemDescription", method = RequestMethod.GET)
23945 amit.gupta 129
	public String getItemDescription(HttpServletRequest request, Model model) throws Throwable {
23819 govind 130
 
23884 amit.gupta 131
		List<TagListing> tagListings = tagListingRepository.selectAll(false);
132
		Map<Integer, TagListing> tagListingMap = new HashMap<>();
133
		List<ItemDescriptionModel> customItems = new ArrayList<>();
23968 amit.gupta 134
		Map<Integer, String> catalogDescription = new HashMap<>();
23945 amit.gupta 135
		for (TagListing tagListing : tagListings) {
23968 amit.gupta 136
			Item item = itemRepository.selectById(tagListing.getItemId());
137
			tagListing.setItemDescription(item.getItemDescription());
23884 amit.gupta 138
			tagListingMap.put(tagListing.getItemId(), tagListing);
139
			ItemDescriptionModel itemDescriptionModel = new ItemDescriptionModel();
140
			itemDescriptionModel.setItemId(tagListing.getItemId());
23945 amit.gupta 141
			itemDescriptionModel
142
					.setItemDescription(tagListing.getItemDescription() + "(" + tagListing.getItemId() + ")");
143
			customItems.add(itemDescriptionModel);
23968 amit.gupta 144
			catalogDescription.put(item.getCatalogItemId(), item.getItemDescriptionNoColor());
23884 amit.gupta 145
		}
23945 amit.gupta 146
 
23819 govind 147
		List<PriceDrop> priceDrops = priceDropRepository.selectAll();
23884 amit.gupta 148
		model.addAttribute("tagListingMap", tagListingMap);
149
		model.addAttribute("customItems", new JSONArray(customItems).toString());
23819 govind 150
		model.addAttribute("priceDrops", priceDrops);
23968 amit.gupta 151
		model.addAttribute("catalogDescription", catalogDescription);
23819 govind 152
		return "price-drop";
153
 
154
	}
23986 amit.gupta 155
 
24049 amit.gupta 156
	@RequestMapping(value = "/item-pricing/{itemId}", method = RequestMethod.GET)
157
	public String getItemPricing(HttpServletRequest request, Model model, @PathVariable int itemId) throws Throwable {
158
 
159
		TagListing tagListing = tagListingRepository.selectByItemId(itemId);
160
		PriceDropModel pm = new PriceDropModel();
161
		pm.setMop(tagListing.getMop());
162
		pm.setDp(tagListing.getSellingPrice());
163
		List<VendorItemPricing> vips = vendorItemPricingRepository.selectAll(itemId);
164
		if(vips.size() > 0) {
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
		model.addAttribute("response", mvcResponseSender.createResponseString(pm));
172
		return "response";
173
	}
174
 
23986 amit.gupta 175
	/*@RequestMapping(value = "/price-drop/imes1/download")
176
	public ResponseEntity<ByteArrayResource> downloadPriceDropImeis1(HttpServletRequest request,
177
			@RequestParam LocalDateTime affectedDate, @RequestParam int itemId) throws Exception {
178
		Item item = itemRepository.selectById(itemId);
179
		List<ImeiDropSummaryModel> imeiDropSummaryModelList = this.getAllSerialNumbersByAffectedDate(affectedDate, item.getCatalogItemId());
180
		List<String> imeis = imeiDropSummaryModelList.stream().map(x->x.getSerialNumber()).collect(Collectors.toList());
181
		Map<String, ImeiDropSummaryModel> imeisDropMap = imeiDropSummaryModelList.stream().collect(Collectors.toMap(ImeiDropSummaryModel::getSerialNumber, x->x));
182
		List<InventoryItem> inventoryItems = inventoryItemRepository.selectBySerialNumbers(new HashSet<>(imeis));
183
		List<Integer> inventoryItemIds = inventoryItems.stream().map(InventoryItem::getId).collect(Collectors.toList());
184
 
185
		List<SchemeInOut> schemeInOuts = schemeInOutRepository.selectByInventoryItemIds(new HashSet<>(inventoryItemIds));
186
 
187
		Set<ItemQuantity> itemSchemes = new HashSet<>();
188
		Map<ItemQuantity, SchemeInOut> itemSchemeInOutMap = new HashMap<>(); 
189
		for(SchemeInOut schemeInOut : schemeInOuts) {
190
		}
191
		List<List<Object>> rows = new ArrayList<>();
192
		for (ImeiDropSummaryModel imeiDropSummaryModel : imeiDropSummaryModelList) {
193
			List<Object> row = this.getRow(imeiDropSummaryModel);
194
 
195
			rows.add(this.getRow(imeiDropSummaryModel));
196
		}
197
 
198
		FileUtil.getCSVByteStream(Arrays.asList("IMEI Number", "Store Name", "Store Code", "Item ID", "Brand",
199
				"Model Name", "Model Number", "Color", "Last Scanned"), 
200
				rows);
201
		final HttpHeaders headers = new HttpHeaders();
202
		headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
203
		headers.set("Content-disposition", "inline; filename=imei-" + item.getItemDescriptionNoColor() + ".csv");
204
		return new ResponseEntity<ByteArrayResource>(null, headers, HttpStatus.OK);
205
 
206
	}*/
23951 amit.gupta 207
	@RequestMapping(value = "/price-drop/imes/download")
208
	public ResponseEntity<ByteArrayResource> downloadPriceDropImeis(HttpServletRequest request,
209
			@RequestParam LocalDateTime affectedDate, @RequestParam int itemId) throws Exception {
210
		Item item = itemRepository.selectById(itemId);
23968 amit.gupta 211
		ByteArrayOutputStream baos = getByteArrayOutputStream(affectedDate, item.getCatalogItemId());
23951 amit.gupta 212
		final HttpHeaders headers = new HttpHeaders();
213
		headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
214
		headers.set("Content-disposition", "inline; filename=imei-" + item.getItemDescriptionNoColor() + ".csv");
215
		byte[] byteArray = baos.toByteArray();
216
		headers.setContentLength(byteArray.length);
217
		return new ResponseEntity<ByteArrayResource>(new ByteArrayResource(byteArray), headers, HttpStatus.OK);
218
	}
219
 
23968 amit.gupta 220
	@RequestMapping(value = "/processPriceDrop", method = RequestMethod.POST)
221
	public String processPriceDrop(HttpServletRequest request, @RequestBody PriceDropProcessModel priceDropProcess,
222
			Model model) throws Exception {
223
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropProcess.getPriceDropId());
224
		boolean response = false;
225
		if (priceDrop.getProcessTimestamp() == null) {
226
			priceDrop.setPartnerPayout(priceDropProcess.getPartnerPayout());
227
			priceDrop.setPriceDropIn(priceDropProcess.getPriceDropIn());
228
			priceDrop.setProcessTimestamp(LocalDateTime.now());
229
			priceDropRepository.persist(priceDrop);
23983 amit.gupta 230
			String description = itemRepository.selectAllByCatalogItemId(priceDrop.getCatalogItemId()).get(0)
231
					.getItemDescriptionNoColor();
232
			List<ImeiDropSummaryModel> imeiDropSummaryModels = this.getAllSerialNumbersByAffectedDate(priceDrop.getAffectedOn(),
23968 amit.gupta 233
					priceDrop.getCatalogItemId());
23983 amit.gupta 234
 
235
 
236
			if (imeiDropSummaryModels.size() > 0) {
237
				List<String> serialNumbers = imeiDropSummaryModels.stream().map(x->x.getSerialNumber()).collect(Collectors.toList());
238
				List<LineItemImei> lineItemImeis = lineItemImeisRepository.selectByIMEI(serialNumbers);
239
				Map<Integer, List<InventoryItem>> pendingPartnerInventoryMap = priceDropService.getInventoryForPriceDrop(lineItemImeis, priceDrop).
240
						stream().collect(Collectors.groupingBy(InventoryItem::getFofoId));
23819 govind 241
 
23983 amit.gupta 242
				for (Map.Entry<Integer, List<InventoryItem>> pendingPartnerInventory : pendingPartnerInventoryMap.entrySet()) {
243
					List<InventoryItem> fofoInventoryList = pendingPartnerInventory.getValue();
244
					int fofoId = pendingPartnerInventory.getKey();
245
 
23968 amit.gupta 246
					String reversalReason = MessageFormat.format(
23983 amit.gupta 247
							"Scheme  differential for Price Drop of Rs.{0} on {1}, on {3}. Total {2} item(s)",
248
							priceDrop.getAmount(), description, fofoInventoryList.size(),
23968 amit.gupta 249
							FormattingUtils.formatDate(priceDrop.getAffectedOn()));
250
					String aReason = MessageFormat.format(
23983 amit.gupta 251
							"Payout of Rs.{4} per unit for Price Drop of Rs.{0} on {1}, on {3}. Total {2} item(s)",
252
							priceDrop.getAmount(), description, fofoInventoryList.size(),
23968 amit.gupta 253
							FormattingUtils.formatDate(priceDrop.getAffectedOn()), priceDropProcess.getPartnerPayout());
23983 amit.gupta 254
					inventoryService.updatePriceDrop(fofoInventoryList, priceDrop.getAmount());
255
					schemeService.reverseSchemes(fofoInventoryList, priceDropProcess.getPriceDropId(), reversalReason);
256
					walletService.addAmountToWallet(fofoId, priceDrop.getId(), WalletReferenceType.PRICE_DROP, aReason,
23995 amit.gupta 257
							priceDrop.getPartnerPayout() * fofoInventoryList.size());
23819 govind 258
				}
259
 
260
			}
23968 amit.gupta 261
			response = true;
23819 govind 262
		}
23968 amit.gupta 263
		model.addAttribute("response", mvcResponseSender.createResponseString(response));
264
		return "response";
23819 govind 265
	}
24019 amit.gupta 266
	@RequestMapping(value = "/updateMop", method = RequestMethod.POST)
267
	public String addMop(HttpServletRequest request, Model model, @RequestBody PriceDropModel priceDropModel) throws Exception {
268
		boolean response = false;
269
		priceDropModel.setAllColors(true);
270
		TagListing tagListing = tagListingRepository.selectByItemId(priceDropModel.getItemId());
271
		float newDp = tagListing.getSellingPrice() - priceDropModel.getPd();
272
		List<Item> allItems = null;
273
		Item currentItem = itemRepository.selectById(priceDropModel.getItemId());
274
		if (priceDropModel.isAllColors()) {
275
			allItems = itemRepository.selectAllByCatalogItemId(currentItem.getCatalogItemId());
23819 govind 276
 
24019 amit.gupta 277
		} else {
278
			allItems = Arrays.asList(currentItem);
279
		}
280
		for (Item item : allItems) {
281
			TagListing itemTagListing = tagListingRepository.selectByItemId(item.getId());
282
			if (itemTagListing == null)
283
				continue;
284
			itemTagListing.setMop(priceDropModel.getMop());
285
			tagListingRepository.persist(tagListing);
286
			List<VendorItemPricing> vipList = vendorItemPricingRepository.selectAll(item.getId());
287
			for (VendorItemPricing vip : vipList) {
288
				vip.setDp(newDp);
289
				vip.setMop(priceDropModel.getMop());
290
				vendorItemPricingRepository.persist(vip);
291
			}
292
			transactionService.updatePriceDrop(item.getId(), newDp);
293
		}
294
		PriceDrop priceDrop = new PriceDrop();
295
		priceDrop.setAffectedOn(LocalDateTime.of(LocalDate.now(),LocalTime.MIDNIGHT));
296
		priceDrop.setOldDp(tagListing.getSellingPrice());
297
		priceDrop.setAmount(priceDropModel.getPd());
298
		priceDrop.setNewDp(tagListing.getSellingPrice());
299
		priceDrop.setNlc(priceDropModel.getNlc());
300
		priceDrop.setTp(priceDropModel.getTp());
301
		priceDrop.setCreatedOn(LocalDateTime.now());
302
		priceDrop.setCatalogItemId(currentItem.getCatalogItemId());
303
		priceDrop.setProcessTimestamp(LocalDateTime.now());
304
		priceDrop.setPartnerPayout(0);
305
		priceDrop.setPriceDropIn(0);
306
		priceDropRepository.persist(priceDrop);
307
		response = true;
308
		model.addAttribute("response", mvcResponseSender.createResponseString(response));
309
		return "response";
310
	}
311
 
23884 amit.gupta 312
	@RequestMapping(value = "/priceDrop", method = RequestMethod.POST)
23945 amit.gupta 313
	public String addPriceDrop(HttpServletRequest request, Model model, @RequestBody PriceDropModel priceDropModel)
314
			throws Exception {
23884 amit.gupta 315
		boolean response = false;
23968 amit.gupta 316
		priceDropModel.setAllColors(true);
23945 amit.gupta 317
		if (this.validatePriceDrop(priceDropModel)) {
23884 amit.gupta 318
			TagListing tagListing = tagListingRepository.selectByItemId(priceDropModel.getItemId());
319
			float newDp = tagListing.getSellingPrice() - priceDropModel.getPd();
23945 amit.gupta 320
			if (newDp > 0) {
23884 amit.gupta 321
				List<Item> allItems = null;
322
				Item currentItem = itemRepository.selectById(priceDropModel.getItemId());
23945 amit.gupta 323
				if (priceDropModel.isAllColors()) {
23884 amit.gupta 324
					allItems = itemRepository.selectAllByCatalogItemId(currentItem.getCatalogItemId());
23945 amit.gupta 325
 
23884 amit.gupta 326
				} else {
23945 amit.gupta 327
					allItems = Arrays.asList(currentItem);
23884 amit.gupta 328
				}
23945 amit.gupta 329
				for (Item item : allItems) {
23884 amit.gupta 330
					TagListing itemTagListing = tagListingRepository.selectByItemId(item.getId());
23945 amit.gupta 331
					if (itemTagListing == null)
332
						continue;
23884 amit.gupta 333
					itemTagListing.setSellingPrice(newDp);
334
					itemTagListing.setMop(priceDropModel.getMop());
335
					tagListingRepository.persist(tagListing);
336
					List<VendorItemPricing> vipList = vendorItemPricingRepository.selectAll(item.getId());
23945 amit.gupta 337
					for (VendorItemPricing vip : vipList) {
23884 amit.gupta 338
						vip.setDp(newDp);
339
						vip.setMop(priceDropModel.getMop());
340
						vip.setNlc(priceDropModel.getNlc());
341
						vip.setTp(priceDropModel.getTp());
342
						vendorItemPricingRepository.persist(vip);
343
					}
344
					transactionService.updatePriceDrop(item.getId(), newDp);
345
				}
23951 amit.gupta 346
				PriceDrop priceDrop = new PriceDrop();
347
				priceDrop.setAffectedOn(priceDropModel.getAffectedDate());
23968 amit.gupta 348
				priceDrop.setOldDp(newDp + priceDropModel.getPd());
23951 amit.gupta 349
				priceDrop.setAmount(priceDropModel.getPd());
23968 amit.gupta 350
				priceDrop.setNewDp(newDp);
351
				priceDrop.setNlc(priceDropModel.getNlc());
24049 amit.gupta 352
				priceDrop.setMop(priceDropModel.getMop());
23968 amit.gupta 353
				priceDrop.setTp(priceDropModel.getTp());
23951 amit.gupta 354
				priceDrop.setCreatedOn(LocalDateTime.now());
355
				priceDrop.setCatalogItemId(currentItem.getCatalogItemId());
356
				priceDropRepository.persist(priceDrop);
23884 amit.gupta 357
				response = true;
358
			} else {
23945 amit.gupta 359
				throw new ProfitMandiBusinessException("Price Drop", priceDropModel.getPd(),
360
						"Price Drop Cannot be more than Current DP");
23884 amit.gupta 361
			}
362
		}
363
		model.addAttribute("response", mvcResponseSender.createResponseString(response));
364
		return "response";
365
	}
23945 amit.gupta 366
 
23819 govind 367
	@RequestMapping(value = "/downloadtotalPriceDropIMEI/{priceDropId}", method = RequestMethod.GET)
368
	public ResponseEntity<?> downloadTotalPriceDropIMEI(HttpServletRequest request, @PathVariable int priceDropId,
23945 amit.gupta 369
			Model model) throws ProfitMandiBusinessException, Exception {
23819 govind 370
 
371
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropId);
23968 amit.gupta 372
		Item item = itemRepository.selectAllByCatalogItemId(priceDrop.getCatalogItemId()).get(0);
373
		ByteArrayOutputStream baos = getByteArrayOutputStream(priceDrop.getAffectedOn(), priceDrop.getCatalogItemId());
374
		final HttpHeaders headers = new HttpHeaders();
375
		headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
23983 amit.gupta 376
		headers.set("Content-disposition",
377
				"inline; filename=pricedrop-" + item.getItemDescriptionNoColor().replaceAll("\\s?,\\s?", " ") + "-"
378
						+ FormattingUtils.formatDate(priceDrop.getAffectedOn()) + ".csv");
23968 amit.gupta 379
		byte[] byteArray = baos.toByteArray();
380
		headers.setContentLength(byteArray.length);
381
		return new ResponseEntity<ByteArrayResource>(new ByteArrayResource(byteArray), headers, HttpStatus.OK);
23951 amit.gupta 382
 
383
	}
384
 
23983 amit.gupta 385
	private List<ImeiDropSummaryModel> getAllSerialNumbersByAffectedDate(LocalDateTime affectedOn,
386
			Integer catalogItemId) throws Exception {
23951 amit.gupta 387
		List<Item> items = itemRepository.selectAllByCatalogItemId(catalogItemId);
388
		List<String> itemIds = items.stream().map(x -> String.valueOf(x.getId())).collect(Collectors.toList());
23945 amit.gupta 389
 
23968 amit.gupta 390
		Map<String, String> params = new HashMap<>();
391
		params.put("MANUAL_criteria1_FROMDATE", FormattingUtils.formatReporitcoDate(affectedOn));
392
		params.put("MANUAL_itemId", StringUtils.join(itemIds, ","));
393
 
394
		Map<String, String> params1 = new HashMap<>();
23979 amit.gupta 395
		params1.put("MANUAL_criteriaDate_FROMDATE", FormattingUtils.formatReporitcoDate(affectedOn.minusDays(1)));
23968 amit.gupta 396
		params1.put("MANUAL_criteriaItemId", StringUtils.join(itemIds, ","));
397
 
23970 amit.gupta 398
		Map<String, String> params2 = new HashMap<>();
23981 amit.gupta 399
		params2.put("MANUAL_Criteria2_FROMDATE", FormattingUtils.formatReporitcoDate(affectedOn.minusDays(1)));
23970 amit.gupta 400
		params2.put("MANUAL_Criteria1", StringUtils.join(itemIds, ","));
401
 
23968 amit.gupta 402
		List<? extends ImeiDropSummaryModel> grnPendingList = getReports(GrnPendingDataModel.class,
403
				ReporticoProject.FOCO, "imeispendinggrn.xml", params);
404
		List<? extends ImeiDropSummaryModel> partnerImeiNotSoldList = getReports(PartnerImeiNotSold.class,
405
				ReporticoProject.FOCO, "PartnerIMEINotSold.xml", params1);
23970 amit.gupta 406
		List<? extends ImeiDropSummaryModel> itemStockOnDateList = getReports(ClosingStockOnDate.class,
407
				ReporticoProject.WAREHOUSENEW, "itemstockondate.xml", params2);
23983 amit.gupta 408
 
23968 amit.gupta 409
		List<ImeiDropSummaryModel> mergedImeis = new ArrayList<>();
410
		mergedImeis.addAll(grnPendingList);
411
		mergedImeis.addAll(partnerImeiNotSoldList);
23970 amit.gupta 412
		mergedImeis.addAll(itemStockOnDateList);
23983 amit.gupta 413
		return mergedImeis;
414
	}
23968 amit.gupta 415
 
23983 amit.gupta 416
	private ByteArrayOutputStream getByteArrayOutputStream(LocalDateTime affectedOn, Integer catalogItemId)
417
			throws Exception {
418
		List<ImeiDropSummaryModel> imeiDropSummaryModelList = this.getAllSerialNumbersByAffectedDate(affectedOn, catalogItemId);
23951 amit.gupta 419
		List<List<Object>> 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",
424
				"Model Name", "Model Number", "Color", "Last Scanned"), rows);
23819 govind 425
	}
23986 amit.gupta 426
 
427
	private List<Object> getRow(ImeiDropSummaryModel imeiDropSummaryModel) {
428
		List<Object> row = new ArrayList<>();
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()));
438
		return row;
439
	}
23819 govind 440
 
23945 amit.gupta 441
	private <T> List<T> getReports(Class<T> className, ReporticoProject project, String reportName,
442
			Map<String, String> params) throws Exception {
443
		HttpResponse reportResponse = reporticoService.getJsonFile(project, reportName, params);
23951 amit.gupta 444
		JavaType type = objectMapper.getTypeFactory().constructParametricType(ReporticoResponseModel.class, className);
445
		ObjectReader or = objectMapper.readerFor(type);
23945 amit.gupta 446
		ReporticoResponseModel<T> responseObj = or.readValue(reportResponse.getEntity().getContent());
447
		return responseObj.getData();
448
	}
449
 
23884 amit.gupta 450
	private boolean validatePriceDrop(PriceDropModel priceDropModel) throws ProfitMandiBusinessException {
23945 amit.gupta 451
		if (priceDropModel.getMop() > 0 && priceDropModel.getMop() > 0 && priceDropModel.getNlc() > 0
452
				&& priceDropModel.getTp() > 0 && priceDropModel.getNlc() > 0) {
23884 amit.gupta 453
			return true;
454
		}
455
		return false;
456
	}
23945 amit.gupta 457
 
23819 govind 458
}