Subversion Repositories SmartDukaan

Rev

Rev 23884 | Rev 23951 | 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
 
3
import java.time.LocalDateTime;
4
import java.util.ArrayList;
23884 amit.gupta 5
import java.util.Arrays;
23819 govind 6
import java.util.HashMap;
7
import java.util.LinkedHashMap;
8
import java.util.List;
9
import java.util.Map;
10
import java.util.stream.Collectors;
11
 
12
import javax.servlet.http.HttpServletRequest;
13
import javax.transaction.Transactional;
14
 
23945 amit.gupta 15
import org.apache.http.HttpResponse;
23819 govind 16
import org.apache.logging.log4j.LogManager;
17
import org.apache.logging.log4j.Logger;
23884 amit.gupta 18
import org.json.JSONArray;
23819 govind 19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.beans.factory.annotation.Qualifier;
21
import org.springframework.http.HttpStatus;
22
import org.springframework.http.ResponseEntity;
23
import org.springframework.stereotype.Controller;
24
import org.springframework.ui.Model;
25
import org.springframework.web.bind.annotation.PathVariable;
23884 amit.gupta 26
import org.springframework.web.bind.annotation.RequestBody;
23819 govind 27
import org.springframework.web.bind.annotation.RequestMapping;
28
import org.springframework.web.bind.annotation.RequestMethod;
29
 
23945 amit.gupta 30
import com.fasterxml.jackson.core.type.TypeReference;
31
import com.fasterxml.jackson.databind.ObjectMapper;
32
import com.fasterxml.jackson.databind.ObjectReader;
33
import com.spice.profitmandi.common.enumuration.ReporticoProject;
23819 govind 34
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23945 amit.gupta 35
import com.spice.profitmandi.common.model.ClosingStockOnDate;
36
import com.spice.profitmandi.common.model.GrnPendingDataModel;
23884 amit.gupta 37
import com.spice.profitmandi.common.model.ItemDescriptionModel;
23945 amit.gupta 38
import com.spice.profitmandi.common.model.PartnerImeiNotSold;
23884 amit.gupta 39
import com.spice.profitmandi.common.model.PriceDropModel;
23945 amit.gupta 40
import com.spice.profitmandi.common.model.ReporticoResponseModel;
41
import com.spice.profitmandi.common.services.ReporticoService;
42
import com.spice.profitmandi.common.util.FormattingUtils;
23819 govind 43
import com.spice.profitmandi.dao.entity.catalog.Item;
23884 amit.gupta 44
import com.spice.profitmandi.dao.entity.catalog.TagListing;
45
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
46
import com.spice.profitmandi.dao.entity.inventory.VendorItemPricing;
23819 govind 47
import com.spice.profitmandi.dao.entity.transaction.LineItemImei;
48
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
49
import com.spice.profitmandi.dao.entity.transaction.PriceDropIMEI;
50
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
23884 amit.gupta 51
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
52
import com.spice.profitmandi.dao.repository.inventory.VendorItemPricingRepository;
23819 govind 53
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
54
import com.spice.profitmandi.dao.repository.transaction.PriceDropIMEIRepository;
55
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
56
import com.spice.profitmandi.service.inventory.InventoryService;
57
import com.spice.profitmandi.service.pricing.PriceDropService;
23884 amit.gupta 58
import com.spice.profitmandi.service.transaction.TransactionService;
23819 govind 59
import com.spice.profitmandi.service.wallet.WalletService;
60
import com.spice.profitmandi.web.util.MVCResponseSender;
61
 
62
import in.shop2020.model.v1.order.WalletReferenceType;
63
 
64
@Controller
65
@Transactional
66
public class PriceDropController {
67
 
68
	private static final Logger LOGGER = LogManager.getLogger(PriceDropController.class);
69
 
70
	@Autowired
71
	private PriceDropRepository priceDropRepository;
72
 
23945 amit.gupta 73
	private ObjectMapper objectMapper = new ObjectMapper();
74
 
23819 govind 75
	@Autowired
23884 amit.gupta 76
	private PriceDropIMEIRepository priceDropIMEIRepository;
23945 amit.gupta 77
 
23819 govind 78
	@Autowired
23884 amit.gupta 79
	private VendorItemPricingRepository vendorItemPricingRepository;
23819 govind 80
 
81
	@Autowired
82
	private PriceDropService priceDropService;
83
 
84
	@Autowired
23884 amit.gupta 85
	@Qualifier("fofoInventoryService")
86
	private InventoryService inventoryService;
23945 amit.gupta 87
 
23884 amit.gupta 88
	@Autowired
23819 govind 89
	private MVCResponseSender mvcResponseSender;
90
 
91
	@Autowired
92
	private WalletService walletService;
93
 
94
	@Autowired
95
	private LineItemImeisRepository lineItemImeisRepository;
23945 amit.gupta 96
 
23884 amit.gupta 97
	@Autowired
98
	private TagListingRepository tagListingRepository;
23945 amit.gupta 99
 
23884 amit.gupta 100
	@Autowired
101
	private TransactionService transactionService;
23819 govind 102
 
103
	@Autowired
104
	@Qualifier("catalogItemRepository")
105
	private ItemRepository itemRepository;
106
 
23945 amit.gupta 107
	@Autowired
108
	ReporticoService reporticoService;
109
 
23819 govind 110
	@RequestMapping(value = "/getItemDescription", method = RequestMethod.GET)
23945 amit.gupta 111
	public String getItemDescription(HttpServletRequest request, Model model) throws Throwable {
23819 govind 112
 
23884 amit.gupta 113
		List<TagListing> tagListings = tagListingRepository.selectAll(false);
114
		Map<Integer, TagListing> tagListingMap = new HashMap<>();
115
		List<ItemDescriptionModel> customItems = new ArrayList<>();
23945 amit.gupta 116
		for (TagListing tagListing : tagListings) {
23884 amit.gupta 117
			tagListing.setItemDescription(itemRepository.selectById(tagListing.getItemId()).getItemDescription());
118
			tagListingMap.put(tagListing.getItemId(), tagListing);
119
			ItemDescriptionModel itemDescriptionModel = new ItemDescriptionModel();
120
			itemDescriptionModel.setItemId(tagListing.getItemId());
23945 amit.gupta 121
			itemDescriptionModel
122
					.setItemDescription(tagListing.getItemDescription() + "(" + tagListing.getItemId() + ")");
123
			customItems.add(itemDescriptionModel);
23884 amit.gupta 124
		}
23945 amit.gupta 125
 
23819 govind 126
		List<PriceDrop> priceDrops = priceDropRepository.selectAll();
23884 amit.gupta 127
		model.addAttribute("tagListingMap", tagListingMap);
128
		model.addAttribute("customItems", new JSONArray(customItems).toString());
23819 govind 129
		model.addAttribute("priceDrops", priceDrops);
130
		return "price-drop";
131
 
132
	}
133
 
23945 amit.gupta 134
	/*
135
	 * @RequestMapping(value = "/downloadtotalPriceDropIMEI/{priceDropId}", method =
136
	 * RequestMethod.GET) public ResponseEntity<?>
137
	 * downloadPriceDropIMEI(HttpServletRequest request, @PathVariable int
138
	 * priceDropId, Model model) throws ProfitMandiBusinessException {
139
	 * 
140
	 * PriceDrop priceDrop = priceDropRepository.selectById(priceDropId);
141
	 * List<PriceDropIMEI> priceDropIMEIs =
142
	 * priceDropIMEIRepository.selectByPriceDropId(priceDrop.getId()); List<String>
143
	 * imeis = priceDropIMEIs.stream().map(x ->
144
	 * x.getImei()).collect(Collectors.toList()); List<LineItemImei> lineItemImeis =
145
	 * lineItemImeisRepository.selectByIMEI(imeis); Map<String, String>
146
	 * priceDropIMEIfofoId = priceDropService.getIMEIandRetailerName(lineItemImeis,
147
	 * priceDrop); if (priceDropIMEIfofoId.size() > 0) { ByteArrayOutputStream
148
	 * byteArrayOutputStream = new ByteArrayOutputStream();
149
	 * ExcelUtils.writePriceDrop(priceDropIMEIfofoId, priceDrop.getItemId(),
150
	 * byteArrayOutputStream); final HttpHeaders headers = new HttpHeaders(); //
151
	 * private static final String CONTENT_TYPE_XLSX = //
152
	 * "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
153
	 * headers.set("Content-Type",
154
	 * "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //
155
	 * headers.set("Content-Type", "application/vnd.ms-excel");
156
	 * headers.set("Content-disposition", "inline; filename=priceDrop" +
157
	 * priceDrop.getId() + ".xlsx");
158
	 * headers.setContentLength(byteArrayOutputStream.toByteArray().length); final
159
	 * InputStream inputStream = new
160
	 * ByteArrayInputStream(byteArrayOutputStream.toByteArray()); final
161
	 * InputStreamResource inputStreamResource = new
162
	 * InputStreamResource(inputStream); return new
163
	 * ResponseEntity<InputStreamResource>(inputStreamResource, headers,
164
	 * HttpStatus.OK); } else { throw new
165
	 * ProfitMandiBusinessException("priceDropId", priceDropId,
166
	 * "No IMEI is Eligible For PriceDrop"); }
167
	 * 
168
	 * }
169
	 */
23819 govind 170
 
171
	@RequestMapping(value = "/paymentAgainstPriceDrop/{priceDropId}/{processedamount}", method = RequestMethod.POST)
172
	public String paymentAgainstPriceDropIMEI(HttpServletRequest request, @PathVariable int priceDropId,
173
			@PathVariable int processedamount, Model model) throws Exception {
174
 
175
		int noOfIMEIForPartner = 1;
176
		Map<Integer, Integer> noOfPriceDropToPartners = new HashMap<>();
177
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropId);
178
		Item item = itemRepository.selectById(priceDrop.getItemId());
179
		List<PriceDropIMEI> priceDropIMEIs = priceDropIMEIRepository.selectByPriceDropId(priceDrop.getId());
180
		List<String> imeis = priceDropIMEIs.stream().map(x -> x.getImei()).collect(Collectors.toList());
181
		List<LineItemImei> lineItemImeis = lineItemImeisRepository.selectByIMEI(imeis);
182
		Map<String, Integer> priceDropIMEIfofoIds = priceDropService.getIMEIAndfofoIdForPriceDrop(lineItemImeis,
183
				priceDrop);
184
		if (priceDropIMEIfofoIds.size() > 0) {
185
			for (Map.Entry<String, Integer> priceDropIMEIAndFofoId : priceDropIMEIfofoIds.entrySet()) {
186
				int fofoId = priceDropIMEIAndFofoId.getValue();
187
				if (noOfPriceDropToPartners.containsKey(fofoId)) {
188
					noOfPriceDropToPartners.put(fofoId, noOfPriceDropToPartners.get(fofoId) + 1);
189
				} else {
190
					noOfPriceDropToPartners.put(fofoId, noOfIMEIForPartner);
191
				}
192
 
193
			}
194
			for (Map.Entry<Integer, Integer> noOfPriceDropToPartner : noOfPriceDropToPartners.entrySet()) {
195
				String description = String.format("Price Drop amount added for %s, total %d pcs.",
196
						item.getItemDescription(), noOfPriceDropToPartner.getValue());
197
				walletService.addAmountToWallet(noOfPriceDropToPartner.getKey(), priceDrop.getId(),
198
						WalletReferenceType.PRICE_DROP, description,
199
						(noOfPriceDropToPartner.getValue() * processedamount));
200
				LOGGER.info(
201
						"Price Drop amount [{}] has been added with retailerId [{}]'s wallet, referenceId [{}], No Of serialNumber [{}]",
202
						(noOfPriceDropToPartner.getValue() * processedamount), noOfPriceDropToPartner.getKey(),
203
						priceDrop.getId(), noOfPriceDropToPartner.getValue());
204
			}
205
			priceDrop.setProcessTimestamp(LocalDateTime.now());
23823 amit.gupta 206
			priceDrop.setPartnerPayout(processedamount);
23819 govind 207
			priceDropRepository.persist(priceDrop);
208
			model.addAttribute("response", mvcResponseSender.createResponseString(true));
209
			return "response";
210
		} else {
211
			priceDrop.setProcessTimestamp(LocalDateTime.now());
212
			model.addAttribute("response", mvcResponseSender.createResponseString(false));
213
			return "false";
214
		}
215
	}
216
 
23884 amit.gupta 217
	@RequestMapping(value = "/priceDrop", method = RequestMethod.POST)
23945 amit.gupta 218
	public String addPriceDrop(HttpServletRequest request, Model model, @RequestBody PriceDropModel priceDropModel)
219
			throws Exception {
23884 amit.gupta 220
		boolean response = false;
23945 amit.gupta 221
		if (this.validatePriceDrop(priceDropModel)) {
23884 amit.gupta 222
			TagListing tagListing = tagListingRepository.selectByItemId(priceDropModel.getItemId());
223
			float newDp = tagListing.getSellingPrice() - priceDropModel.getPd();
23945 amit.gupta 224
			if (newDp > 0) {
23884 amit.gupta 225
				List<Item> allItems = null;
226
				Item currentItem = itemRepository.selectById(priceDropModel.getItemId());
23945 amit.gupta 227
				if (priceDropModel.isAllColors()) {
23884 amit.gupta 228
					allItems = itemRepository.selectAllByCatalogItemId(currentItem.getCatalogItemId());
23945 amit.gupta 229
 
23884 amit.gupta 230
				} else {
23945 amit.gupta 231
					allItems = Arrays.asList(currentItem);
23884 amit.gupta 232
				}
23945 amit.gupta 233
				for (Item item : allItems) {
23884 amit.gupta 234
					TagListing itemTagListing = tagListingRepository.selectByItemId(item.getId());
23945 amit.gupta 235
					if (itemTagListing == null)
236
						continue;
23884 amit.gupta 237
					itemTagListing.setSellingPrice(newDp);
238
					itemTagListing.setMop(priceDropModel.getMop());
239
					tagListingRepository.persist(tagListing);
240
					List<VendorItemPricing> vipList = vendorItemPricingRepository.selectAll(item.getId());
23945 amit.gupta 241
					for (VendorItemPricing vip : vipList) {
23884 amit.gupta 242
						vip.setDp(newDp);
243
						vip.setMop(priceDropModel.getMop());
244
						vip.setNlc(priceDropModel.getNlc());
245
						vip.setTp(priceDropModel.getTp());
246
						vendorItemPricingRepository.persist(vip);
247
					}
248
					PriceDrop priceDrop = new PriceDrop();
249
					priceDrop.setAffectedOn(priceDropModel.getAffectedDate());
250
					priceDrop.setAmount(priceDropModel.getPd());
251
					priceDrop.setCreatedOn(LocalDateTime.now());
252
					priceDrop.setItemId(item.getId());
253
					priceDropRepository.persist(priceDrop);
254
					transactionService.updatePriceDrop(item.getId(), newDp);
255
				}
256
				response = true;
257
			} else {
23945 amit.gupta 258
				throw new ProfitMandiBusinessException("Price Drop", priceDropModel.getPd(),
259
						"Price Drop Cannot be more than Current DP");
23884 amit.gupta 260
			}
261
		}
262
		model.addAttribute("response", mvcResponseSender.createResponseString(response));
263
		return "response";
264
	}
23945 amit.gupta 265
 
23819 govind 266
	@RequestMapping(value = "/downloadtotalPriceDropIMEI/{priceDropId}", method = RequestMethod.GET)
267
	public ResponseEntity<?> downloadTotalPriceDropIMEI(HttpServletRequest request, @PathVariable int priceDropId,
23945 amit.gupta 268
			Model model) throws ProfitMandiBusinessException, Exception {
23819 govind 269
 
270
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropId);
23945 amit.gupta 271
		Map<String, String> params = new HashMap<>();
272
		params.put("MANUAL_criteria1_FROMDATE", FormattingUtils.formatReporitcoDate(priceDrop.getAffectedOn()));
273
		params.put("MANUAL_itemId", String.valueOf(priceDrop.getItemId()));
274
		Map<String, String> params1 = new HashMap<>();
275
		params1.put("MANUAL_criteriaDate_FROMDATE", FormattingUtils.formatReporitcoDate(priceDrop.getAffectedOn()));
276
		params1.put("MANUAL_criteriaItemId", String.valueOf(priceDrop.getItemId()));
277
 
278
		Map<String, String> params2 = new HashMap<>();
279
		params2.put("MANUAL_Criteria2_FROMDATE", FormattingUtils.formatReporitcoDate(priceDrop.getAffectedOn()));
280
		params2.put("MANUAL_Criteria1", String.valueOf(priceDrop.getItemId()));
281
		List<GrnPendingDataModel> grnPendingList = getReports(GrnPendingDataModel.class, ReporticoProject.FOCO, "imeispendinggrn.xml", params);
282
		List<PartnerImeiNotSold> partnerImeiNotSoldList = getReports(PartnerImeiNotSold.class, ReporticoProject.FOCO, "PartnerIMEINotSold.xml", params1);
283
		List<ClosingStockOnDate> itemStockOnDateList = getReports(ClosingStockOnDate.class, ReporticoProject.WAREHOUSENEW, "itemstockondate.xml", params2);
284
		return new ResponseEntity<Boolean>(true, null, HttpStatus.OK);
285
		//List<Strng> 
286
		/*
287
		 * ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
288
		 * ExcelUtils.writePriceDropForAllIMEI(priceDropIMEIAndItemId,
289
		 * byteArrayOutputStream); final HttpHeaders headers = new HttpHeaders();
290
		 * headers.set("Content-Type",
291
		 * "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
292
		 * headers.set("Content-disposition", "inline; filename=totalPriceDropIMEI" +
293
		 * priceDrop.getItemId() + ".xlsx");
294
		 * headers.setContentLength(byteArrayOutputStream.toByteArray().length); final
295
		 * InputStream inputStream = new
296
		 * ByteArrayInputStream(byteArrayOutputStream.toByteArray()); final
297
		 * InputStreamResource inputStreamResource = new
298
		 * InputStreamResource(inputStream); return new
299
		 * ResponseEntity<InputStreamResource>(inputStreamResource, headers,
300
		 * HttpStatus.OK); } else { priceDrop.setProcessTimestamp(LocalDateTime.now());
301
		 * throw new ProfitMandiBusinessException("IMEI", 1,
302
		 * "IMEI not Found For PriceDrop"); }
303
		 */
23819 govind 304
	}
305
 
23945 amit.gupta 306
	private <T> List<T> getReports(Class<T> className, ReporticoProject project, String reportName,
307
			Map<String, String> params) throws Exception {
308
		HttpResponse reportResponse = reporticoService.getJsonFile(project, reportName, params);
309
		TypeReference<ReporticoResponseModel<T>> typeReference = new TypeReference<ReporticoResponseModel<T>>() {
310
		};
311
		ObjectReader or = objectMapper.readerFor(typeReference);
312
		ReporticoResponseModel<T> responseObj = or.readValue(reportResponse.getEntity().getContent());
313
		return responseObj.getData();
314
	}
315
 
23884 amit.gupta 316
	private boolean validatePriceDrop(PriceDropModel priceDropModel) throws ProfitMandiBusinessException {
23945 amit.gupta 317
		if (priceDropModel.getMop() > 0 && priceDropModel.getMop() > 0 && priceDropModel.getNlc() > 0
318
				&& priceDropModel.getTp() > 0 && priceDropModel.getNlc() > 0) {
23884 amit.gupta 319
			return true;
320
		}
321
		return false;
322
	}
23945 amit.gupta 323
 
23819 govind 324
	private Map<String, String> getpriceDropIMEIAndItemDescription(PriceDrop priceDrop)
325
			throws ProfitMandiBusinessException {
326
		Map<String, String> priceDropIMEIAndItemId = new LinkedHashMap<>();
23945 amit.gupta 327
		List<InventoryItem> inventoryInStock = inventoryService.getInventoryInStock(priceDrop.getItemId(),
328
				priceDrop.getAffectedOn());
23819 govind 329
		return priceDropIMEIAndItemId;
330
	}
331
 
332
}