Subversion Repositories SmartDukaan

Rev

Rev 24970 | Rev 25023 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
23755 amit.gupta 1
package com.smartdukaan.cron.migrations;
2
 
24819 amit.gupta 3
import java.io.Serializable;
23824 amit.gupta 4
import java.time.LocalDate;
23755 amit.gupta 5
import java.time.LocalDateTime;
23824 amit.gupta 6
import java.time.LocalTime;
24819 amit.gupta 7
import java.util.ArrayList;
23755 amit.gupta 8
import java.util.Arrays;
23827 amit.gupta 9
import java.util.Collections;
24002 amit.gupta 10
import java.util.HashMap;
24005 amit.gupta 11
import java.util.HashSet;
23755 amit.gupta 12
import java.util.List;
24002 amit.gupta 13
import java.util.Map;
24641 amit.gupta 14
import java.util.stream.Collectors;
23755 amit.gupta 15
 
24819 amit.gupta 16
import org.apache.commons.io.output.ByteArrayOutputStream;
23755 amit.gupta 17
import org.apache.commons.lang.StringUtils;
18
import org.apache.logging.log4j.LogManager;
19
import org.apache.logging.log4j.Logger;
20
import org.springframework.beans.factory.annotation.Autowired;
24819 amit.gupta 21
import org.springframework.core.io.ByteArrayResource;
24767 amit.gupta 22
import org.springframework.mail.javamail.JavaMailSender;
23755 amit.gupta 23
import org.springframework.stereotype.Component;
24
import org.springframework.transaction.annotation.Transactional;
25021 amit.gupta 25
import org.w3c.dom.stylesheets.LinkStyle;
23755 amit.gupta 26
 
24962 amit.gupta 27
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24819 amit.gupta 28
import com.spice.profitmandi.common.util.FileUtil;
29
import com.spice.profitmandi.common.util.Utils;
25021 amit.gupta 30
import com.spice.profitmandi.dao.entity.catalog.Scheme;
24716 amit.gupta 31
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
24806 amit.gupta 32
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
33
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
24005 amit.gupta 34
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
24883 amit.gupta 35
import com.spice.profitmandi.dao.entity.fofo.PartnerTargetDetails;
23898 amit.gupta 36
import com.spice.profitmandi.dao.entity.fofo.Purchase;
24716 amit.gupta 37
import com.spice.profitmandi.dao.entity.fofo.ScanRecord;
25021 amit.gupta 38
import com.spice.profitmandi.dao.entity.fofo.SchemeItem;
24883 amit.gupta 39
import com.spice.profitmandi.dao.entity.fofo.TargetSlab;
24953 amit.gupta 40
import com.spice.profitmandi.dao.entity.inventory.SaholicInventorySnapshot;
24962 amit.gupta 41
import com.spice.profitmandi.dao.entity.inventory.SaholicReservationInventorySnapshot;
23824 amit.gupta 42
import com.spice.profitmandi.dao.entity.transaction.LineItem;
23755 amit.gupta 43
import com.spice.profitmandi.dao.entity.transaction.LineItemImei;
44
import com.spice.profitmandi.dao.entity.transaction.Order;
24802 amit.gupta 45
import com.spice.profitmandi.dao.entity.transaction.SellerWarehouse;
25021 amit.gupta 46
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
24716 amit.gupta 47
import com.spice.profitmandi.dao.repository.GenericRepository;
25021 amit.gupta 48
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
23899 amit.gupta 49
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
24716 amit.gupta 50
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
24002 amit.gupta 51
import com.spice.profitmandi.dao.repository.fofo.DebitNoteRepository;
24806 amit.gupta 52
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
53
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
24002 amit.gupta 54
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
24806 amit.gupta 55
import com.spice.profitmandi.dao.repository.fofo.InvoiceNumberGenerationSequenceRepository;
24883 amit.gupta 56
import com.spice.profitmandi.dao.repository.fofo.PartnerTargetRepository;
23898 amit.gupta 57
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
24716 amit.gupta 58
import com.spice.profitmandi.dao.repository.fofo.ScanRecordRepository;
24002 amit.gupta 59
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
25021 amit.gupta 60
import com.spice.profitmandi.dao.repository.fofo.SchemeItemRepository;
24883 amit.gupta 61
import com.spice.profitmandi.dao.repository.fofo.TargetSlabRepository;
62
import com.spice.profitmandi.dao.repository.fofo.TargetSlabRepositoryImpl;
24953 amit.gupta 63
import com.spice.profitmandi.dao.repository.inventory.SaholicInventorySnapshotRepository;
64
import com.spice.profitmandi.dao.repository.inventory.SaholicReservationInventorySnapshotRepository;
23755 amit.gupta 65
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
23824 amit.gupta 66
import com.spice.profitmandi.dao.repository.transaction.LineItemRepository;
23755 amit.gupta 67
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
24772 amit.gupta 68
import com.spice.profitmandi.dao.repository.transaction.ReturnOrderRepository;
24802 amit.gupta 69
import com.spice.profitmandi.dao.repository.transaction.SellerWarehouseRepository;
24767 amit.gupta 70
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
71
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
24002 amit.gupta 72
import com.spice.profitmandi.dao.repository.user.UserRepository;
23899 amit.gupta 73
import com.spice.profitmandi.service.inventory.InventoryService;
24266 amit.gupta 74
import com.spice.profitmandi.service.order.OrderService;
24005 amit.gupta 75
import com.spice.profitmandi.service.pricing.PriceDropService;
76
import com.spice.profitmandi.service.scheme.SchemeService;
23899 amit.gupta 77
import com.spice.profitmandi.service.transaction.TransactionService;
78
import com.spice.profitmandi.service.user.RetailerService;
79
import com.spice.profitmandi.service.wallet.WalletService;
23755 amit.gupta 80
 
24953 amit.gupta 81
import in.shop2020.model.v1.order.OrderStatus;
24002 amit.gupta 82
import in.shop2020.model.v1.order.WalletReferenceType;
83
 
23755 amit.gupta 84
@Component
85
@Transactional(rollbackFor = Throwable.class)
86
public class RunOnceTasks {
87
 
88
	private static final Logger LOGGER = LogManager.getLogger(RunOnceTasks.class);
89
 
90
	@Autowired
23824 amit.gupta 91
	private LineItemRepository lineItemRepository;
24711 amit.gupta 92
 
23905 amit.gupta 93
	@Autowired
24953 amit.gupta 94
	private SaholicInventorySnapshotRepository saholicInventorySnapshotRepository;
95
 
96
	@Autowired
97
	private SaholicReservationInventorySnapshotRepository saholicReservationInventorySnapshotRepository;
98
 
99
	@Autowired
24883 amit.gupta 100
	private TargetSlabRepository targetSlabRepository;
101
 
102
	@Autowired
103
	private PartnerTargetRepository partnerTargetRepository;
104
 
105
	@Autowired
24802 amit.gupta 106
	private SellerWarehouseRepository sellerWarehouseRepository;
24814 amit.gupta 107
 
24802 amit.gupta 108
	@Autowired
24806 amit.gupta 109
	private FofoOrderItemRepository fofoOrderItemRepository;
24814 amit.gupta 110
 
24806 amit.gupta 111
	@Autowired
112
	private FofoOrderRepository fofoOrderRepository;
24814 amit.gupta 113
 
24806 amit.gupta 114
	@Autowired
24767 amit.gupta 115
	private UserWalletRepository userWalletRepository;
116
 
117
	@Autowired
118
	private UserWalletHistoryRepository userWalletHistoryRepository;
119
 
120
	@Autowired
24002 amit.gupta 121
	private UserRepository userRepository;
24711 amit.gupta 122
 
24002 amit.gupta 123
	@Autowired
23899 amit.gupta 124
	private WalletService walletService;
125
 
126
	@Autowired
127
	private InventoryService inventoryService;
128
 
129
	@Autowired
130
	private TransactionService transactionService;
131
 
24711 amit.gupta 132
	// Service for Tertiary/Partner Orders
24266 amit.gupta 133
	@Autowired
134
	private OrderService orderService;
23767 amit.gupta 135
 
23755 amit.gupta 136
	@Autowired
25021 amit.gupta 137
	private SchemeRepository schemeRepository;
138
 
139
	@Autowired
140
	private SchemeItemRepository schemeItemRepository;
141
 
142
	@Autowired
24772 amit.gupta 143
	private ReturnOrderRepository returnOrderRepository;
144
 
145
	@Autowired
23899 amit.gupta 146
	private FofoStoreRepository fofoStoreRepository;
147
 
148
	@Autowired
23755 amit.gupta 149
	private LineItemImeisRepository lineItemImeisRepository;
24711 amit.gupta 150
 
24002 amit.gupta 151
	@Autowired
152
	private InventoryItemRepository inventoryItemRepository;
24814 amit.gupta 153
 
24806 amit.gupta 154
	@Autowired
155
	private InvoiceNumberGenerationSequenceRepository invoiceNumberGenerationSequenceRepository;
23901 amit.gupta 156
 
23898 amit.gupta 157
	@Autowired
23899 amit.gupta 158
	private RetailerService retailerService;
24711 amit.gupta 159
 
24002 amit.gupta 160
	@Autowired
161
	private SchemeInOutRepository schemeInOutRepository;
24711 amit.gupta 162
 
24002 amit.gupta 163
	@Autowired
164
	private DebitNoteRepository debitNoteRepository;
23899 amit.gupta 165
 
166
	@Autowired
24716 amit.gupta 167
	private GenericRepository genericRepository;
168
 
169
	@Autowired
23898 amit.gupta 170
	private PurchaseRepository purchaseRepository;
24711 amit.gupta 171
 
24005 amit.gupta 172
	@Autowired
173
	private PriceDropService priceDropService;
24883 amit.gupta 174
 
24819 amit.gupta 175
	@Autowired
176
	private JavaMailSender googleMailSender;
24711 amit.gupta 177
 
24005 amit.gupta 178
	@Autowired
179
	private SchemeService schemeService;
24711 amit.gupta 180
 
24716 amit.gupta 181
	@Autowired
182
	private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
183
 
184
	@Autowired
24767 amit.gupta 185
	private OrderRepository orderRepository;
186
 
187
	@Autowired
24716 amit.gupta 188
	private ScanRecordRepository scanRecordRepository;
189
 
24767 amit.gupta 190
	@Autowired
191
	private JavaMailSender mailSender;
192
 
24005 amit.gupta 193
	public void dropCorrection() throws Exception {
194
 
24711 amit.gupta 195
		walletService.rollbackAmountFromWallet(175128034, 274, 4, WalletReferenceType.PRICE_DROP,
196
				"Scheme  differential for Price Drop of Rs.712 on Samsung J6 J600GG, on 01-08-2018 for missing 1pc");
197
		walletService.rollbackAmountFromWallet(175128034, -259, 4, WalletReferenceType.PRICE_DROP,
198
				"Scheme  differential for Price Drop of Rs.712 on Samsung J6 J600GG, on 01-08-2018 for missing 1pc");
199
		List<InventoryItem> iis = inventoryItemRepository.selectByIds(new HashSet<>(Arrays.asList(3518, 3516)));
200
		schemeService.reverseSchemes(iis, 8,
201
				"Scheme  differential for Price Drop of Rs.712 on Samsung J6 J600GG, on 01-08-2018. Total 2 item(s)");
202
		List<InventoryItem> iis1 = inventoryItemRepository.selectByIds(new HashSet<>(Arrays.asList(3502, 3334, 3503)));
203
		schemeService.reverseSchemes(iis1, 13,
204
				"Scheme  differential for Price Drop of Rs.485 on Samsung Galaxy J4 J400FD, on 18-07-2018. Total 3 item(s)");
205
 
206
		List<InventoryItem> iis2 = inventoryItemRepository.selectByIds(new HashSet<>(Arrays.asList(3319)));
207
		schemeService.reverseSchemes(iis2, 13,
208
				"Scheme  differential for Price Drop of Rs.485 on Samsung Galaxy J4 J400FD, on 18-07-2018. Total 1 item(s)");
24005 amit.gupta 209
	}
24711 amit.gupta 210
 
24002 amit.gupta 211
	public void schemeRollback() {
212
		Map<Integer, Float> fofoIdAmount = new HashMap<>();
24003 amit.gupta 213
		fofoIdAmount.put(175135218, 1942f);
214
		String description = "Price drop/differential rolled out as, they were already returned, Total 2pcs.";
24711 amit.gupta 215
		for (Map.Entry<Integer, Float> fofoIdAmountEntry : fofoIdAmount.entrySet()) {
216
			Integer fofoId = fofoIdAmountEntry.getKey();
24002 amit.gupta 217
			Float amount = fofoIdAmountEntry.getValue();
24003 amit.gupta 218
			walletService.rollbackAmountFromWallet(fofoId, amount, 4, WalletReferenceType.PRICE_DROP, description);
24002 amit.gupta 219
		}
24004 amit.gupta 220
		fofoIdAmount.put(175135218, 438f);
24711 amit.gupta 221
		for (Map.Entry<Integer, Float> fofoIdAmountEntry : fofoIdAmount.entrySet()) {
222
			Integer fofoId = fofoIdAmountEntry.getKey();
24004 amit.gupta 223
			Float amount = fofoIdAmountEntry.getValue();
224
			walletService.rollbackAmountFromWallet(fofoId, amount, 4, WalletReferenceType.PRICE_DROP, description);
225
		}
24002 amit.gupta 226
	}
23755 amit.gupta 227
 
23898 amit.gupta 228
	public void populateGrnTimestamp() {
229
		List<Purchase> allPurchases = purchaseRepository.selectAll();
23899 amit.gupta 230
		for (Purchase p : allPurchases) {
23898 amit.gupta 231
			String invoiceNumber = p.getPurchaseReference();
23899 amit.gupta 232
			if (p.getCompleteTimestamp() == null) {
23898 amit.gupta 233
				LOGGER.info("GRN for invoice {} is delivered but partially Completed.", p.getPurchaseReference());
234
			} else {
235
				List<Order> orders = orderRepository.selectByAirwayBillOrInvoiceNumber(invoiceNumber, p.getFofoId());
23899 amit.gupta 236
				for (Order order : orders) {
23902 amit.gupta 237
					if (order.getPartnerGrnTimestamp() == null) {
238
						order.setPartnerGrnTimestamp(p.getCompleteTimestamp());
23898 amit.gupta 239
						orderRepository.persist(order);
240
					}
241
				}
242
			}
243
		}
23899 amit.gupta 244
 
23898 amit.gupta 245
	}
23899 amit.gupta 246
 
24716 amit.gupta 247
	public void migarateLineItemsToNewTable() throws Exception {
23755 amit.gupta 248
		LOGGER.info("Before Migrated LineItems Successfully");
23824 amit.gupta 249
		int lineItemImeiId = 0;
250
		LocalDateTime startDate = null;
251
		try {
252
			lineItemImeiId = lineItemImeisRepository.selectMaxId();
23899 amit.gupta 253
			LineItem lineItem = lineItemRepository
254
					.selectById(lineItemImeisRepository.selectById(lineItemImeiId).getLineItemId());
23824 amit.gupta 255
			Order order = orderRepository.selectById(lineItem.getOrderId());
256
			startDate = order.getBillingTimestamp();
257
		} catch (Exception e) {
258
			LOGGER.info("Running before first time");
23826 amit.gupta 259
			startDate = LocalDateTime.of(LocalDate.of(2017, 7, 1), LocalTime.MIDNIGHT);
23824 amit.gupta 260
		}
261
		List<Order> orders = orderRepository.selectAllByBillingDatesBetween(startDate, LocalDateTime.now());
23827 amit.gupta 262
		Collections.reverse(orders);
23899 amit.gupta 263
 
23755 amit.gupta 264
		for (Order order : orders) {
23824 amit.gupta 265
			try {
23767 amit.gupta 266
				String serialNumbers = order.getLineItem().getSerialNumber();
267
				if (!StringUtils.isEmpty(serialNumbers)) {
268
					List<String> serialNumberList = Arrays.asList(serialNumbers.split(","));
269
					for (String serialNumber : serialNumberList) {
270
						int lineItemId = order.getLineItem().getId();
271
						LineItemImei lineItemImei = new LineItemImei();
272
						lineItemImei.setSerialNumber(serialNumber);
273
						lineItemImei.setLineItemId(lineItemId);
274
						lineItemImeisRepository.persist(lineItemImei);
275
					}
276
				} else {
277
					LOGGER.info("Serial Numbers dont exist for Order {}", order.getId());
23755 amit.gupta 278
				}
23824 amit.gupta 279
			} catch (Exception e) {
23899 amit.gupta 280
				LOGGER.info("Error occurred while creating lineitem imei {}, because of {}", order.getId(),
281
						e.getMessage());
23755 amit.gupta 282
			}
283
		}
284
		LOGGER.info("Migrated LineItems Successfully");
285
	}
24266 amit.gupta 286
 
287
	public void cancelOrder(List<String> invoiceNumbers) throws Exception {
288
		orderService.cancelOrder(invoiceNumbers);
289
	}
24711 amit.gupta 290
 
24722 amit.gupta 291
	public void migratePurchase() throws Exception {
24641 amit.gupta 292
		List<Purchase> purchases = purchaseRepository.selectPurchaseAllPurchasesLessThanZero();
24706 amit.gupta 293
		System.out.printf("Total Purchases count is %s", purchases.size());
24711 amit.gupta 294
		for (Purchase purchase : purchases) {
295
			List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchase.getId());
296
			Map<Integer, List<InventoryItem>> itemIdInventoryMap = inventoryItems.stream()
297
					.collect(Collectors.groupingBy(InventoryItem::getItemId));
298
			List<Order> orders = orderRepository.selectByAirwayBillOrInvoiceNumber(purchase.getPurchaseReference(),
299
					purchase.getFofoId());
300
			Map<Integer, Integer> ourSaleItemQtyMap = orders.stream().collect(Collectors.groupingBy(
301
					x -> x.getLineItem().getItemId(), Collectors.summingInt(x -> x.getLineItem().getQuantity())));
302
			Map<Integer, Integer> theirPurchaseItemQtyMap = inventoryItems.stream().collect(Collectors
303
					.groupingBy(InventoryItem::getItemId, Collectors.summingInt(InventoryItem::getInitialQuantity)));
24709 amit.gupta 304
			for (Map.Entry<Integer, Integer> itemQtyEntry : theirPurchaseItemQtyMap.entrySet()) {
24711 amit.gupta 305
				if (!ourSaleItemQtyMap.containsKey(itemQtyEntry.getKey())) {
306
					LOGGER.info("Cannot find in Invoice {} item {}", purchase.getPurchaseReference(),
307
							itemQtyEntry.getKey());
24646 amit.gupta 308
					continue;
24645 amit.gupta 309
				}
24709 amit.gupta 310
				int ourSale = ourSaleItemQtyMap.get(itemQtyEntry.getKey());
24713 amit.gupta 311
				int quantityToReduce = itemQtyEntry.getValue() - ourSale;
24716 amit.gupta 312
				List<InventoryItem> itemIis = itemIdInventoryMap.get(itemQtyEntry.getKey());
313
				if (itemIdInventoryMap != null) {
314
					for (InventoryItem ii : itemIis) {
24915 amit.gupta 315
						if (ii.getSerialNumber() == null && ii.getGoodQuantity() == ii.getInitialQuantity()
316
								&& quantityToReduce >= ii.getInitialQuantity()) {
24734 amit.gupta 317
							LOGGER.info("Changed in inventoryItems {}, {}, {}, {}, {}, {}",
24716 amit.gupta 318
									purchase.getPurchaseReference(), ii.getId(), ii.getItemId(),
24734 amit.gupta 319
									ii.getInitialQuantity(), ii.getGoodQuantity(), quantityToReduce);
24912 amit.gupta 320
							List<ScanRecord> scanRecords = scanRecordRepository.selectByInventoryItemId(ii.getId());
321
							for (ScanRecord scanRecord : scanRecords) {
24915 amit.gupta 322
								CurrentInventorySnapshot cis = currentInventorySnapshotRepository
323
										.selectByItemIdAndFofoId(itemQtyEntry.getKey(), purchase.getFofoId());
324
								scanRecord.setQuantity(0);
325
								ii.setGoodQuantity(0);
326
								quantityToReduce = quantityToReduce - ii.getInitialQuantity();
327
								cis.setAvailability(cis.getAvailability() - ii.getInitialQuantity());
328
								purchase.setUnfullfilledNonSerializedQuantity(
329
										purchase.getUnfullfilledNonSerializedQuantity() + quantityToReduce);
330
								LOGGER.info("Rectified {}, {}, {}, {}, {}, {}", purchase.getPurchaseReference(),
331
										ii.getId(), ii.getItemId(), ii.getInitialQuantity(), ii.getGoodQuantity(),
332
										quantityToReduce);
24711 amit.gupta 333
							}
24641 amit.gupta 334
						}
335
					}
336
				}
337
			}
338
		}
24953 amit.gupta 339
		// throw new Exception();
24641 amit.gupta 340
	}
24767 amit.gupta 341
 
24802 amit.gupta 342
	public void migrateChallansToInvoices() throws Exception {
24814 amit.gupta 343
		Map<String, List<Order>> invoiceOrdersMap = orderRepository.selectAllChallans().stream()
344
				.filter(x -> !x.getLineItem().getHsnCode().equals("NOGST"))
24794 amit.gupta 345
				.collect(Collectors.groupingBy(Order::getInvoiceNumber, Collectors.toList()));
24814 amit.gupta 346
 
24819 amit.gupta 347
		List<List<? extends Serializable>> rows = new ArrayList<>();
24814 amit.gupta 348
		for (String invoice : invoiceOrdersMap.keySet()) {
24794 amit.gupta 349
			Order oneOrder = invoiceOrdersMap.get(invoice).get(0);
350
			int totalOrders = invoiceOrdersMap.get(invoice).size();
351
			LineItem lineItem = oneOrder.getLineItem();
24802 amit.gupta 352
			oneOrder.setBillingTimestamp(LocalDateTime.now());
353
			oneOrder.setInvoiceNumber(getInvoiceNumber(oneOrder));
24819 amit.gupta 354
			rows.add(Arrays.asList(oneOrder.getId(), invoice, oneOrder.getInvoiceNumber(), lineItem.getQuantity()));
24883 amit.gupta 355
			LOGGER.info(invoice + "\t" + oneOrder.getInvoiceNumber() + oneOrder.getId() + "\t",
356
					"\t" + totalOrders + "\t" + lineItem.getQuantity());
24814 amit.gupta 357
			Purchase p = null;
358
			try {
359
				p = purchaseRepository.selectByPurchaseReferenceAndFofoId(invoice, oneOrder.getRetailerId());
360
			} catch (Exception e) {
24815 amit.gupta 361
				LOGGER.info("Could not find purchase for invoice {}", invoice);
24812 amit.gupta 362
			}
24814 amit.gupta 363
			if (p != null) {
364
				List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(p.getId());
365
				for (InventoryItem inventoryItem : inventoryItems) {
24815 amit.gupta 366
					LOGGER.info(inventoryItem.getItemId() + " " + inventoryItem.getGoodQuantity() + " "
24883 amit.gupta 367
							+ inventoryItem.getHsnCode() + " " + inventoryItem.getSerialNumber() + " "
368
							+ p.getPurchaseReference());
24814 amit.gupta 369
				}
370
			}
371
 
24794 amit.gupta 372
		}
24818 amit.gupta 373
		changePartnerInvoices();
24883 amit.gupta 374
		ByteArrayOutputStream baos = FileUtil
375
				.getCSVByteStream(Arrays.asList("Order id", "Challan", "Invoice", "Quantity"), rows);
376
 
24819 amit.gupta 377
		Utils.sendMailWithAttachment(googleMailSender,
378
				new String[] { "amit.gupta@shop2020.in", "sunny.yadav@smartdukaan.com" }, null,
24883 amit.gupta 379
				"Challans Converted to Invoice", "PFA", "Challans-To-Invoice.csv",
380
				new ByteArrayResource(baos.toByteArray()));
24916 amit.gupta 381
		throw new Exception();
24794 amit.gupta 382
	}
24767 amit.gupta 383
 
24802 amit.gupta 384
	private String getInvoiceNumber(Order oneOrder) {
24805 amit.gupta 385
		String prefix = oneOrder.getInvoiceNumber().split("-")[1].replaceAll("\\d*", "");
24803 amit.gupta 386
		System.out.println("Prefix is " + prefix);
24802 amit.gupta 387
		SellerWarehouse sellerWarehouse = sellerWarehouseRepository.selectByPrefix(prefix);
388
		int newSequence = sellerWarehouse.getInvoiceSequence() + 1;
389
		sellerWarehouse.setInvoiceSequence(newSequence);
24814 amit.gupta 390
		return prefix + newSequence;
24802 amit.gupta 391
	}
24814 amit.gupta 392
 
24806 amit.gupta 393
	private void changePartnerInvoices() throws Exception {
24814 amit.gupta 394
		List<FofoOrder> fofoOrders = fofoOrderRepository.selectByInvoiceNumberLike("%SEC%");
395
		for (FofoOrder fofoOrder : fofoOrders) {
24806 amit.gupta 396
			FofoOrderItem fofoOrderItem = fofoOrderItemRepository.selectByOrderId(fofoOrder.getId()).get(0);
24814 amit.gupta 397
			if (fofoOrderItem.getBrand().equals("Vivo")) {
24806 amit.gupta 398
				String challanString = fofoOrder.getInvoiceNumber();
24814 amit.gupta 399
				String storeCode = invoiceNumberGenerationSequenceRepository.selectByFofoId(fofoOrder.getFofoId())
400
						.getPrefix();
24806 amit.gupta 401
				String invoiceNumber = orderService.getInvoiceNumber(fofoOrder.getFofoId(), storeCode);
402
				fofoOrder.setInvoiceNumber(invoiceNumber);
403
				fofoOrder.setCreateTimestamp(LocalDateTime.now());
24818 amit.gupta 404
				LOGGER.info(challanString + "\t" + invoiceNumber + "\t" + fofoOrderItem.getQuantity());
24806 amit.gupta 405
			}
24814 amit.gupta 406
 
24806 amit.gupta 407
		}
408
	}
24802 amit.gupta 409
 
24883 amit.gupta 410
	public void createMonthlyTargets() throws Exception {
411
		Map<String, List<TargetSlab>> defaultSlabs = targetSlabRepository.getAllDefaultSlabs();
412
		for (String memberType : defaultSlabs.keySet()) {
413
			PartnerTargetDetails ptd = new PartnerTargetDetails();
414
			ptd.setBrandName(null);
415
 
416
			LocalDate startDate = LocalDate.now().withDayOfMonth(1);
417
			int totalDays = startDate.lengthOfMonth();
418
			String monthName = startDate.getMonth().toString();
419
			ptd.setStartDate(startDate.atStartOfDay());
420
			ptd.setEndDate(startDate.plusDays(totalDays - 1).atTime(LocalTime.MAX));
421
			ptd.setTargetName(String.format("%s Sales Target for %s Partners", monthName, memberType));
422
			partnerTargetRepository.persist(ptd);
423
			TargetSlabRepositoryImpl.TYPE_PARTNER_MAPPING.get(memberType).forEach(x -> {
424
				x.setTargetId(ptd.getId());
425
				partnerTargetRepository.persist(x);
426
			});
427
 
428
			List<TargetSlab> slabs = defaultSlabs.get(memberType);
429
			slabs.stream().forEach(x -> {
430
				x.setTargetId(ptd.getId());
431
				targetSlabRepository.persist(x);
432
			});
433
		}
434
	}
435
 
25021 amit.gupta 436
	public void populateScheme() {
437
		List<Integer> list05 = Arrays.asList(29093, 29094, 29095, 29096, 29097, 29098, 29099, 29101, 29102);
438
		List<Integer> list10 = Arrays.asList(29187, 29188);
439
		List<Integer> list13 = Arrays.asList(28095, 28097, 28098, 28101, 28102, 28104, 28106, 28107, 28108, 28227,
440
				28228, 28229, 28231, 29055, 29056, 29066, 29079);
441
		List<Integer> list15 = Arrays.asList(20891, 27938, 27939, 27940, 27941, 27943, 27947, 27948, 27953, 27954,
442
				27955, 28220, 28467, 29020, 29033, 29034, 29035, 29592, 29886, 29890, 29891, 29941, 29942, 27445, 27958,
443
				27960, 27961);
444
		List<Integer> list18 = Arrays.asList(28137, 28138, 28140, 28157, 28158, 28161, 28162, 28163, 28164, 28176,
445
				28177, 28178, 28199, 28200, 28208, 28209, 28210, 28211, 28215, 28216, 28217, 28230, 28380, 28381, 28382,
446
				28383, 28384, 28385, 28386, 28387, 28388, 28389, 28390, 28391, 28392, 28395, 28461, 28462, 28463, 28464,
447
				28465, 28466, 28488, 28489, 28490, 28491, 28564, 28565, 28762, 28773, 28871, 28872, 28873, 28874, 28880,
448
				28881, 28914, 28915, 28916, 28917, 28918, 28919, 28920, 29046, 29047, 29048, 29068, 29069, 29110, 29111,
449
				29112, 29113, 29114, 29115, 29116, 29117, 29207, 29208, 29217, 29218, 29225, 29474, 29475, 29476, 29477,
450
				29478, 29479, 29483, 29485, 29486, 29487, 29488, 29739, 29740, 29741, 29742, 29743, 29744, 29745, 29746,
451
				29747, 29748, 29749, 29753, 29756, 29757, 29758, 29760, 29761, 29762, 29763, 29764, 29765, 29766, 29767,
452
				29779, 29780, 29781, 29792, 29793, 29828, 29829, 29830, 29879, 29880, 29881, 29882, 29951);
453
		List<Integer> list20 = Arrays.asList(27975, 27976, 28091, 28363, 29023, 29024, 29025);
454
		List<Integer> list25 = Arrays.asList(28763, 28764, 28765, 29152, 29154, 29157, 29209, 29210, 29601, 29602,
455
				29720, 29721, 29722, 29723, 29725, 29726, 29727);
456
		List<Integer> list30 = Arrays.asList(27962, 27963, 27964, 27965, 27966, 27968, 27969, 28328, 28362, 28566,
457
				28567, 28568, 28571, 28572, 29073, 29074, 29075, 29080, 29081, 29120, 29121, 29122, 29124, 29189, 29190,
458
				29191, 29192, 29193, 29194, 29196, 29197, 29198, 29199, 29515, 29516, 29517, 29675, 29676, 29677, 29678,
459
				29679, 29707, 29708, 29794, 29795, 29883, 29884, 29885, 29887, 29888, 29889, 29931, 29932, 29935, 29936,
460
				29937, 29938, 29939, 29940);
461
		List<Integer> list35 = Arrays.asList(27996, 27998, 29067, 29071, 29643, 29644, 29945, 29946, 29987, 29052,
462
				29053, 29078, 29085, 29086, 29567, 29568, 29569, 29570, 29797, 29914, 29915, 29930, 28877, 28878, 29604,
463
				29605, 29606, 29775, 29776, 29777, 29778, 28862, 28863, 28864, 29026, 29103, 29104, 29105, 28979, 28980,
464
				28981, 28982, 28983, 29002, 29003, 29004, 29834, 29835, 29836, 29837, 29943);
465
		List<Integer> list40 = Arrays.asList(29082, 29083, 28766, 28767, 28768, 28769, 28771);
466
		List<Integer> list45 = Arrays.asList(28026, 28027, 28028, 29528, 29529, 29530, 29534, 29535, 29536, 29538,
467
				29755);
468
		List<Integer> list55 = Arrays.asList(28010, 28011, 28015, 28016, 28019, 28240, 28469, 28470, 28471, 28569,
469
				28570, 28674, 28759, 28761, 28909, 28910, 28911, 28912, 28913, 29108, 29109, 29143, 29144, 29145, 29146,
470
				29211, 29212, 29213, 29214, 29482, 29511, 29512, 29521, 29525, 29526, 29527, 29574, 29575, 29576, 29611,
471
				29612, 29616, 29641, 29642, 29728, 29729, 29750, 29771, 29782, 29783, 29784, 29923, 29924, 29944, 29953,
472
				29992, 29994, 27977, 27978, 28092, 28325, 28326, 28327, 28486, 28487, 29148, 29149, 29150, 29151, 29384,
473
				29385, 29386, 29427, 29489, 29518, 29519, 29520, 29577, 29578, 29579, 29580, 29586, 29660, 29661, 29662,
474
				29663, 29769, 29770, 29790, 29791, 29796, 29928, 29929, 29947, 29948, 28093, 28094, 28556, 28557, 28673,
475
				28867, 28868, 28870, 29089, 29090, 29091, 29106, 29107);
476
		List<Integer> list65 = Arrays.asList(28058, 28223, 28224, 28225, 28226, 28329, 29201, 29202, 29203, 29204,
477
				29205, 29206, 29466, 29467, 29468, 29469, 29470, 29471, 29472, 29573);
478
		Map<Float, List<Integer>> m = new HashMap<>();
479
		m.put(0.5f, list05);
480
		m.put(1.0f, list10);
481
		m.put(1.3f, list13);
482
		m.put(1.5f, list15);
483
		m.put(1.8f, list18);
484
		m.put(2.0f, list20);
485
		m.put(2.5f, list25);
486
		m.put(3.0f, list30);
487
		m.put(3.5f, list35);
488
		m.put(4.0f, list40);
489
		m.put(4.5f, list45);
490
		m.put(5.5f, list55);
491
		m.put(6.5f, list65);
492
		for (Map.Entry<Float, List<Integer>> itemsListEntry : m.entrySet()) {
493
			Scheme s1 = getScheme();
494
			s1.setAmount(itemsListEntry.getKey());
495
			schemeRepository.persist(s1);
496
			for(Integer itemId : itemsListEntry.getValue()) {
497
				SchemeItem schemeItem = new SchemeItem();
498
				schemeItem.setItemId(itemId);
499
				schemeItem.setSchemeId(s1.getId());
500
				schemeItemRepository.persist(schemeItem);
501
			}
502
		}
503
 
504
	}
505
 
506
	private Scheme getScheme() {
507
		Scheme s = new Scheme();
508
		s.setName("List Price Margin");
509
		s.setDescription("List Price Margin");
510
		s.setRetailerAll(true);
511
		s.setActiveTimestamp(null);
512
		s.setStartDateTime(LocalDate.of(2019, 6, 1).atStartOfDay());
513
		s.setEndDateTime(LocalDate.of(2019, 7, 1).atStartOfDay());
514
		s.setAmountType(AmountType.PERCENTAGE);
515
		s.setCreateTimestamp(LocalDateTime.now());
516
		s.setExpireTimestamp(null);
517
		s.setCreatedBy(175120474);
518
		return s;
519
	}
520
 
24957 amit.gupta 521
	public void findMismatchesInIndent() throws Exception {
25021 amit.gupta 522
		/*
523
		 * List<SaholicInventorySnapshot> saholicInventorySnapshots =
524
		 * saholicInventorySnapshotRepository.selectAll(); Map<Integer, Integer>
525
		 * itemReservedMap = saholicInventorySnapshots.stream().filter(x ->
526
		 * x.getReserved() > 0)
527
		 * .collect(Collectors.groupingBy(SaholicInventorySnapshot::getItemId,
528
		 * Collectors.summingInt(SaholicInventorySnapshot::getReserved)));
529
		 */
24957 amit.gupta 530
		List<Order> allInProcessOrders = orderRepository.selectAllOrder(OrderStatus.SUBMITTED_FOR_PROCESSING);
24967 amit.gupta 531
 
25021 amit.gupta 532
		/*
533
		 * Map<Integer, Integer> lineItemQtyMap = allInProcessOrders
534
		 * .stream().collect(Collectors.groupingBy(y -> y.getLineItem().getItemId(),
535
		 * Collectors.summingInt(y -> y.getLineItem().getQuantity())));
536
		 */
537
 
538
		/*
539
		 * Map<Integer, Order> orderMap =
540
		 * allInProcessOrders.stream().collect(Collectors.toMap(Order::getId, x->x));
541
		 * Map<Integer, List<Order>> itemOrdersMap = allInProcessOrders.stream()
542
		 * .collect(Collectors.groupingBy(o->o.getLineItem().getItemId(),
543
		 * Collectors.toList()));
544
		 */
545
 
546
		Map<String, Integer> lineItemWhQtyMap = allInProcessOrders.stream()
547
				.collect(Collectors.groupingBy(y -> y.getLineItem().getItemId() + "-" + y.getWarehouseId(),
24967 amit.gupta 548
						Collectors.summingInt(y -> y.getLineItem().getQuantity())));
25021 amit.gupta 549
 
550
		/*
551
		 * itemReservedMap.forEach((itemId, reserved)->{
552
		 * if(lineItemQtyMap.containsKey(itemId)) { int orderedQty =
553
		 * lineItemQtyMap.get(itemId); System.out.printf("%d\t%d\t%d\n",itemId,
554
		 * orderedQty, reserved); } else {
555
		 * saholicInventorySnapshotRepository.removeAllReservations(itemId);
556
		 * System.out.printf("%d\t%d\t%d\n",itemId, 0, reserved); } });
557
		 */
558
 
559
		lineItemWhQtyMap.forEach((key, value) -> {
560
 
24967 amit.gupta 561
			int itemId = Integer.parseInt(key.split("-")[0]);
562
			int warehouseId = Integer.parseInt(key.split("-")[1]);
25021 amit.gupta 563
			System.out.printf("%d\t%d\t%d\n", itemId, warehouseId, value);
564
			SaholicInventorySnapshot cis = saholicInventorySnapshotRepository.selectByWarehouseIdandItemId(warehouseId,
565
					itemId);
566
			if (cis == null) {
24970 amit.gupta 567
				cis = new SaholicInventorySnapshot();
568
				cis.setItemId(itemId);
569
				cis.setWarehouseId(warehouseId);
570
				cis.setAvailability(0);
571
				saholicInventorySnapshotRepository.persist(cis);
572
			}
24967 amit.gupta 573
			cis.setReserved(value);
574
		});
24964 amit.gupta 575
		/*
25021 amit.gupta 576
		 * List<SaholicReservationInventorySnapshot> reservationInventorySnapshots =
577
		 * saholicReservationInventorySnapshotRepository.selectAll(); Map<Integer,
578
		 * SaholicReservationInventorySnapshot> reservationInventorySnapshotsMap
579
		 * =reservationInventorySnapshots.stream()
580
		 * .collect(Collectors.toMap(x->x.getOrderId(), x->x));
581
		 * 
582
		 * List<Integer> orderIds =
583
		 * reservationInventorySnapshots.stream().map(x->x.getOrderId()).collect(
584
		 * Collectors.toList()); List<Order> orders =
585
		 * orderRepository.selectByOrderIds(orderIds);
586
		 * orders.stream().filter(x->Arrays.asList(OrderStatus.PAYMENT_PENDING,
587
		 * OrderStatus.PAYMENT_FAILED). contains(x.getStatus())).forEach(x->{
588
		 * SaholicReservationInventorySnapshot snapshot =
589
		 * reservationInventorySnapshotsMap.get(x.getId()); try {
590
		 * saholicInventorySnapshotRepository.reduceReservationCount(snapshot.getItemId(
591
		 * ), snapshot.getWarehouseId(), snapshot.getReserved(), snapshot.getOrderId());
592
		 * System.out.printf("%d Reduced by %d for Order Id %d\n",snapshot.getItemId(),
593
		 * snapshot.getReserved(), snapshot.getOrderId()); }
594
		 * catch(ProfitMandiBusinessException e) {
595
		 * LOGGER.info("Problem whiile reducing count for Order Id - {}",
596
		 * snapshot.getOrderId()); } });
597
		 */
598
		// throw new Exception();
24953 amit.gupta 599
	}
600
 
23755 amit.gupta 601
}