Subversion Repositories SmartDukaan

Rev

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