Subversion Repositories SmartDukaan

Rev

Rev 24816 | Rev 24818 | 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
 
23824 amit.gupta 3
import java.time.LocalDate;
23755 amit.gupta 4
import java.time.LocalDateTime;
23824 amit.gupta 5
import java.time.LocalTime;
23755 amit.gupta 6
import java.util.Arrays;
23827 amit.gupta 7
import java.util.Collections;
24002 amit.gupta 8
import java.util.HashMap;
24005 amit.gupta 9
import java.util.HashSet;
23755 amit.gupta 10
import java.util.List;
24002 amit.gupta 11
import java.util.Map;
24641 amit.gupta 12
import java.util.stream.Collectors;
23755 amit.gupta 13
 
14
import org.apache.commons.lang.StringUtils;
15
import org.apache.logging.log4j.LogManager;
16
import org.apache.logging.log4j.Logger;
17
import org.springframework.beans.factory.annotation.Autowired;
24767 amit.gupta 18
import org.springframework.mail.javamail.JavaMailSender;
23755 amit.gupta 19
import org.springframework.stereotype.Component;
20
import org.springframework.transaction.annotation.Transactional;
21
 
24716 amit.gupta 22
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
24806 amit.gupta 23
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
24
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
24005 amit.gupta 25
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
23898 amit.gupta 26
import com.spice.profitmandi.dao.entity.fofo.Purchase;
24716 amit.gupta 27
import com.spice.profitmandi.dao.entity.fofo.ScanRecord;
23824 amit.gupta 28
import com.spice.profitmandi.dao.entity.transaction.LineItem;
23755 amit.gupta 29
import com.spice.profitmandi.dao.entity.transaction.LineItemImei;
30
import com.spice.profitmandi.dao.entity.transaction.Order;
24802 amit.gupta 31
import com.spice.profitmandi.dao.entity.transaction.SellerWarehouse;
24722 amit.gupta 32
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
24716 amit.gupta 33
import com.spice.profitmandi.dao.repository.GenericRepository;
23899 amit.gupta 34
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
24716 amit.gupta 35
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
24002 amit.gupta 36
import com.spice.profitmandi.dao.repository.fofo.DebitNoteRepository;
24806 amit.gupta 37
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
38
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
24002 amit.gupta 39
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
24806 amit.gupta 40
import com.spice.profitmandi.dao.repository.fofo.InvoiceNumberGenerationSequenceRepository;
23898 amit.gupta 41
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
24716 amit.gupta 42
import com.spice.profitmandi.dao.repository.fofo.ScanRecordRepository;
24002 amit.gupta 43
import com.spice.profitmandi.dao.repository.fofo.SchemeInOutRepository;
23755 amit.gupta 44
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
23824 amit.gupta 45
import com.spice.profitmandi.dao.repository.transaction.LineItemRepository;
23755 amit.gupta 46
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
24772 amit.gupta 47
import com.spice.profitmandi.dao.repository.transaction.ReturnOrderRepository;
24802 amit.gupta 48
import com.spice.profitmandi.dao.repository.transaction.SellerWarehouseRepository;
24767 amit.gupta 49
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
50
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
24002 amit.gupta 51
import com.spice.profitmandi.dao.repository.user.UserRepository;
23899 amit.gupta 52
import com.spice.profitmandi.service.inventory.InventoryService;
24266 amit.gupta 53
import com.spice.profitmandi.service.order.OrderService;
24005 amit.gupta 54
import com.spice.profitmandi.service.pricing.PriceDropService;
55
import com.spice.profitmandi.service.scheme.SchemeService;
23899 amit.gupta 56
import com.spice.profitmandi.service.transaction.TransactionService;
57
import com.spice.profitmandi.service.user.RetailerService;
58
import com.spice.profitmandi.service.wallet.WalletService;
23755 amit.gupta 59
 
24002 amit.gupta 60
import in.shop2020.model.v1.order.WalletReferenceType;
61
 
23755 amit.gupta 62
@Component
63
@Transactional(rollbackFor = Throwable.class)
64
public class RunOnceTasks {
65
 
66
	private static final Logger LOGGER = LogManager.getLogger(RunOnceTasks.class);
67
 
68
	@Autowired
23824 amit.gupta 69
	private LineItemRepository lineItemRepository;
24711 amit.gupta 70
 
23905 amit.gupta 71
	@Autowired
24802 amit.gupta 72
	private SellerWarehouseRepository sellerWarehouseRepository;
24814 amit.gupta 73
 
24802 amit.gupta 74
	@Autowired
24806 amit.gupta 75
	private FofoOrderItemRepository fofoOrderItemRepository;
24814 amit.gupta 76
 
24806 amit.gupta 77
	@Autowired
78
	private FofoOrderRepository fofoOrderRepository;
24814 amit.gupta 79
 
24806 amit.gupta 80
	@Autowired
24767 amit.gupta 81
	private UserWalletRepository userWalletRepository;
82
 
83
	@Autowired
84
	private UserWalletHistoryRepository userWalletHistoryRepository;
85
 
86
	@Autowired
24002 amit.gupta 87
	private UserRepository userRepository;
24711 amit.gupta 88
 
24002 amit.gupta 89
	@Autowired
23899 amit.gupta 90
	private WalletService walletService;
91
 
92
	@Autowired
93
	private InventoryService inventoryService;
94
 
95
	@Autowired
96
	private TransactionService transactionService;
97
 
24711 amit.gupta 98
	// Service for Tertiary/Partner Orders
24266 amit.gupta 99
	@Autowired
100
	private OrderService orderService;
23767 amit.gupta 101
 
23755 amit.gupta 102
	@Autowired
24772 amit.gupta 103
	private ReturnOrderRepository returnOrderRepository;
104
 
105
	@Autowired
23899 amit.gupta 106
	private FofoStoreRepository fofoStoreRepository;
107
 
108
	@Autowired
23755 amit.gupta 109
	private LineItemImeisRepository lineItemImeisRepository;
24711 amit.gupta 110
 
24002 amit.gupta 111
	@Autowired
112
	private InventoryItemRepository inventoryItemRepository;
24814 amit.gupta 113
 
24806 amit.gupta 114
	@Autowired
115
	private InvoiceNumberGenerationSequenceRepository invoiceNumberGenerationSequenceRepository;
23901 amit.gupta 116
 
23898 amit.gupta 117
	@Autowired
23899 amit.gupta 118
	private RetailerService retailerService;
24711 amit.gupta 119
 
24002 amit.gupta 120
	@Autowired
121
	private SchemeInOutRepository schemeInOutRepository;
24711 amit.gupta 122
 
24002 amit.gupta 123
	@Autowired
124
	private DebitNoteRepository debitNoteRepository;
23899 amit.gupta 125
 
126
	@Autowired
24716 amit.gupta 127
	private GenericRepository genericRepository;
128
 
129
	@Autowired
23898 amit.gupta 130
	private PurchaseRepository purchaseRepository;
24711 amit.gupta 131
 
24005 amit.gupta 132
	@Autowired
133
	private PriceDropService priceDropService;
24711 amit.gupta 134
 
24005 amit.gupta 135
	@Autowired
136
	private SchemeService schemeService;
24711 amit.gupta 137
 
24716 amit.gupta 138
	@Autowired
139
	private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
140
 
141
	@Autowired
24767 amit.gupta 142
	private OrderRepository orderRepository;
143
 
144
	@Autowired
24716 amit.gupta 145
	private ScanRecordRepository scanRecordRepository;
146
 
24767 amit.gupta 147
	@Autowired
148
	private JavaMailSender mailSender;
149
 
24005 amit.gupta 150
	public void dropCorrection() throws Exception {
151
 
24711 amit.gupta 152
		walletService.rollbackAmountFromWallet(175128034, 274, 4, WalletReferenceType.PRICE_DROP,
153
				"Scheme  differential for Price Drop of Rs.712 on Samsung J6 J600GG, on 01-08-2018 for missing 1pc");
154
		walletService.rollbackAmountFromWallet(175128034, -259, 4, WalletReferenceType.PRICE_DROP,
155
				"Scheme  differential for Price Drop of Rs.712 on Samsung J6 J600GG, on 01-08-2018 for missing 1pc");
156
		List<InventoryItem> iis = inventoryItemRepository.selectByIds(new HashSet<>(Arrays.asList(3518, 3516)));
157
		schemeService.reverseSchemes(iis, 8,
158
				"Scheme  differential for Price Drop of Rs.712 on Samsung J6 J600GG, on 01-08-2018. Total 2 item(s)");
159
		List<InventoryItem> iis1 = inventoryItemRepository.selectByIds(new HashSet<>(Arrays.asList(3502, 3334, 3503)));
160
		schemeService.reverseSchemes(iis1, 13,
161
				"Scheme  differential for Price Drop of Rs.485 on Samsung Galaxy J4 J400FD, on 18-07-2018. Total 3 item(s)");
162
 
163
		List<InventoryItem> iis2 = inventoryItemRepository.selectByIds(new HashSet<>(Arrays.asList(3319)));
164
		schemeService.reverseSchemes(iis2, 13,
165
				"Scheme  differential for Price Drop of Rs.485 on Samsung Galaxy J4 J400FD, on 18-07-2018. Total 1 item(s)");
24005 amit.gupta 166
	}
24711 amit.gupta 167
 
24002 amit.gupta 168
	public void schemeRollback() {
169
		Map<Integer, Float> fofoIdAmount = new HashMap<>();
24003 amit.gupta 170
		fofoIdAmount.put(175135218, 1942f);
171
		String description = "Price drop/differential rolled out as, they were already returned, Total 2pcs.";
24711 amit.gupta 172
		for (Map.Entry<Integer, Float> fofoIdAmountEntry : fofoIdAmount.entrySet()) {
173
			Integer fofoId = fofoIdAmountEntry.getKey();
24002 amit.gupta 174
			Float amount = fofoIdAmountEntry.getValue();
24003 amit.gupta 175
			walletService.rollbackAmountFromWallet(fofoId, amount, 4, WalletReferenceType.PRICE_DROP, description);
24002 amit.gupta 176
		}
24004 amit.gupta 177
		fofoIdAmount.put(175135218, 438f);
24711 amit.gupta 178
		for (Map.Entry<Integer, Float> fofoIdAmountEntry : fofoIdAmount.entrySet()) {
179
			Integer fofoId = fofoIdAmountEntry.getKey();
24004 amit.gupta 180
			Float amount = fofoIdAmountEntry.getValue();
181
			walletService.rollbackAmountFromWallet(fofoId, amount, 4, WalletReferenceType.PRICE_DROP, description);
182
		}
24002 amit.gupta 183
	}
23755 amit.gupta 184
 
23898 amit.gupta 185
	public void populateGrnTimestamp() {
186
		List<Purchase> allPurchases = purchaseRepository.selectAll();
23899 amit.gupta 187
		for (Purchase p : allPurchases) {
23898 amit.gupta 188
			String invoiceNumber = p.getPurchaseReference();
23899 amit.gupta 189
			if (p.getCompleteTimestamp() == null) {
23898 amit.gupta 190
				LOGGER.info("GRN for invoice {} is delivered but partially Completed.", p.getPurchaseReference());
191
			} else {
192
				List<Order> orders = orderRepository.selectByAirwayBillOrInvoiceNumber(invoiceNumber, p.getFofoId());
23899 amit.gupta 193
				for (Order order : orders) {
23902 amit.gupta 194
					if (order.getPartnerGrnTimestamp() == null) {
195
						order.setPartnerGrnTimestamp(p.getCompleteTimestamp());
23898 amit.gupta 196
						orderRepository.persist(order);
197
					}
198
				}
199
			}
200
		}
23899 amit.gupta 201
 
23898 amit.gupta 202
	}
23899 amit.gupta 203
 
24716 amit.gupta 204
	public void migarateLineItemsToNewTable() throws Exception {
23755 amit.gupta 205
		LOGGER.info("Before Migrated LineItems Successfully");
23824 amit.gupta 206
		int lineItemImeiId = 0;
207
		LocalDateTime startDate = null;
208
		try {
209
			lineItemImeiId = lineItemImeisRepository.selectMaxId();
23899 amit.gupta 210
			LineItem lineItem = lineItemRepository
211
					.selectById(lineItemImeisRepository.selectById(lineItemImeiId).getLineItemId());
23824 amit.gupta 212
			Order order = orderRepository.selectById(lineItem.getOrderId());
213
			startDate = order.getBillingTimestamp();
214
		} catch (Exception e) {
215
			LOGGER.info("Running before first time");
23826 amit.gupta 216
			startDate = LocalDateTime.of(LocalDate.of(2017, 7, 1), LocalTime.MIDNIGHT);
23824 amit.gupta 217
		}
218
		List<Order> orders = orderRepository.selectAllByBillingDatesBetween(startDate, LocalDateTime.now());
23827 amit.gupta 219
		Collections.reverse(orders);
23899 amit.gupta 220
 
23755 amit.gupta 221
		for (Order order : orders) {
23824 amit.gupta 222
			try {
23767 amit.gupta 223
				String serialNumbers = order.getLineItem().getSerialNumber();
224
				if (!StringUtils.isEmpty(serialNumbers)) {
225
					List<String> serialNumberList = Arrays.asList(serialNumbers.split(","));
226
					for (String serialNumber : serialNumberList) {
227
						int lineItemId = order.getLineItem().getId();
228
						LineItemImei lineItemImei = new LineItemImei();
229
						lineItemImei.setSerialNumber(serialNumber);
230
						lineItemImei.setLineItemId(lineItemId);
231
						lineItemImeisRepository.persist(lineItemImei);
232
					}
233
				} else {
234
					LOGGER.info("Serial Numbers dont exist for Order {}", order.getId());
23755 amit.gupta 235
				}
23824 amit.gupta 236
			} catch (Exception e) {
23899 amit.gupta 237
				LOGGER.info("Error occurred while creating lineitem imei {}, because of {}", order.getId(),
238
						e.getMessage());
23755 amit.gupta 239
			}
240
		}
241
		LOGGER.info("Migrated LineItems Successfully");
242
	}
24266 amit.gupta 243
 
244
	public void cancelOrder(List<String> invoiceNumbers) throws Exception {
245
		orderService.cancelOrder(invoiceNumbers);
246
	}
24711 amit.gupta 247
 
24722 amit.gupta 248
	public void migratePurchase() throws Exception {
24641 amit.gupta 249
		List<Purchase> purchases = purchaseRepository.selectPurchaseAllPurchasesLessThanZero();
24706 amit.gupta 250
		System.out.printf("Total Purchases count is %s", purchases.size());
24711 amit.gupta 251
		for (Purchase purchase : purchases) {
252
			List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(purchase.getId());
253
			Map<Integer, List<InventoryItem>> itemIdInventoryMap = inventoryItems.stream()
254
					.collect(Collectors.groupingBy(InventoryItem::getItemId));
255
			List<Order> orders = orderRepository.selectByAirwayBillOrInvoiceNumber(purchase.getPurchaseReference(),
256
					purchase.getFofoId());
257
			Map<Integer, Integer> ourSaleItemQtyMap = orders.stream().collect(Collectors.groupingBy(
258
					x -> x.getLineItem().getItemId(), Collectors.summingInt(x -> x.getLineItem().getQuantity())));
259
			Map<Integer, Integer> theirPurchaseItemQtyMap = inventoryItems.stream().collect(Collectors
260
					.groupingBy(InventoryItem::getItemId, Collectors.summingInt(InventoryItem::getInitialQuantity)));
24709 amit.gupta 261
			for (Map.Entry<Integer, Integer> itemQtyEntry : theirPurchaseItemQtyMap.entrySet()) {
24711 amit.gupta 262
				if (!ourSaleItemQtyMap.containsKey(itemQtyEntry.getKey())) {
263
					LOGGER.info("Cannot find in Invoice {} item {}", purchase.getPurchaseReference(),
264
							itemQtyEntry.getKey());
24646 amit.gupta 265
					continue;
24645 amit.gupta 266
				}
24709 amit.gupta 267
				int ourSale = ourSaleItemQtyMap.get(itemQtyEntry.getKey());
24713 amit.gupta 268
				int quantityToReduce = itemQtyEntry.getValue() - ourSale;
24716 amit.gupta 269
				List<InventoryItem> itemIis = itemIdInventoryMap.get(itemQtyEntry.getKey());
270
				if (itemIdInventoryMap != null) {
271
					for (InventoryItem ii : itemIis) {
24732 amit.gupta 272
						if (quantityToReduce > 0 && ii.getGoodQuantity() > 0) {
24734 amit.gupta 273
							LOGGER.info("Changed in inventoryItems {}, {}, {}, {}, {}, {}",
24716 amit.gupta 274
									purchase.getPurchaseReference(), ii.getId(), ii.getItemId(),
24734 amit.gupta 275
									ii.getInitialQuantity(), ii.getGoodQuantity(), quantityToReduce);
24730 amit.gupta 276
							if (ii.getGoodQuantity() > 0) {
24716 amit.gupta 277
								List<ScanRecord> scanRecords = scanRecordRepository.selectByInventoryItemId(ii.getId());
24722 amit.gupta 278
								for (ScanRecord scanRecord : scanRecords) {
279
									if (scanRecord.getType().equals(ScanType.PURCHASE)) {
280
										CurrentInventorySnapshot cis = currentInventorySnapshotRepository
281
												.selectByItemIdAndFofoId(itemQtyEntry.getKey(), purchase.getFofoId());
24733 amit.gupta 282
										scanRecord.setQuantity(scanRecord.getQuantity() - ii.getGoodQuantity());
283
										ii.setInitialQuantity(ii.getInitialQuantity() - ii.getBadQuantity());
24735 amit.gupta 284
										quantityToReduce = quantityToReduce - ii.getGoodQuantity();
24733 amit.gupta 285
										ii.setGoodQuantity(0);
286
										cis.setAvailability(cis.getAvailability() - ii.getGoodQuantity());
287
										purchase.setUnfullfilledNonSerializedQuantity(
288
												purchase.getUnfullfilledNonSerializedQuantity() + quantityToReduce);
24767 amit.gupta 289
										LOGGER.info("Rectified {}, {}, {}, {}, {}, {}", purchase.getPurchaseReference(),
290
												ii.getId(), ii.getItemId(), ii.getInitialQuantity(),
291
												ii.getGoodQuantity(), quantityToReduce);
24733 amit.gupta 292
										break;
24716 amit.gupta 293
									}
294
								}
24711 amit.gupta 295
							}
24641 amit.gupta 296
						}
297
					}
298
				}
299
			}
300
		}
24767 amit.gupta 301
		// throw new Exception();
24641 amit.gupta 302
	}
24767 amit.gupta 303
 
24802 amit.gupta 304
	public void migrateChallansToInvoices() throws Exception {
24814 amit.gupta 305
		Map<String, List<Order>> invoiceOrdersMap = orderRepository.selectAllChallans().stream()
306
				.filter(x -> !x.getLineItem().getHsnCode().equals("NOGST"))
24794 amit.gupta 307
				.collect(Collectors.groupingBy(Order::getInvoiceNumber, Collectors.toList()));
24814 amit.gupta 308
 
309
		for (String invoice : invoiceOrdersMap.keySet()) {
24794 amit.gupta 310
			Order oneOrder = invoiceOrdersMap.get(invoice).get(0);
311
			int totalOrders = invoiceOrdersMap.get(invoice).size();
312
			LineItem lineItem = oneOrder.getLineItem();
24802 amit.gupta 313
			oneOrder.setBillingTimestamp(LocalDateTime.now());
314
			oneOrder.setInvoiceNumber(getInvoiceNumber(oneOrder));
24817 amit.gupta 315
			LOGGER.info(invoice + "\t" + oneOrder.getInvoiceNumber() + "\t" + totalOrders + "\t" + lineItem.getQuantity());
24814 amit.gupta 316
			Purchase p = null;
317
			try {
318
				p = purchaseRepository.selectByPurchaseReferenceAndFofoId(invoice, oneOrder.getRetailerId());
319
			} catch (Exception e) {
24815 amit.gupta 320
				LOGGER.info("Could not find purchase for invoice {}", invoice);
24812 amit.gupta 321
			}
24814 amit.gupta 322
			if (p != null) {
323
				List<InventoryItem> inventoryItems = inventoryItemRepository.selectByPurchaseId(p.getId());
324
				for (InventoryItem inventoryItem : inventoryItems) {
24815 amit.gupta 325
					LOGGER.info(inventoryItem.getItemId() + " " + inventoryItem.getGoodQuantity() + " "
24816 amit.gupta 326
							+ inventoryItem.getHsnCode() + " " + inventoryItem.getSerialNumber() + " " + p.getPurchaseReference());
24814 amit.gupta 327
				}
328
			}
329
 
24794 amit.gupta 330
		}
24814 amit.gupta 331
		//changePartnerInvoices();
24802 amit.gupta 332
		throw new Exception();
24794 amit.gupta 333
	}
24767 amit.gupta 334
 
24802 amit.gupta 335
	private String getInvoiceNumber(Order oneOrder) {
24805 amit.gupta 336
		String prefix = oneOrder.getInvoiceNumber().split("-")[1].replaceAll("\\d*", "");
24803 amit.gupta 337
		System.out.println("Prefix is " + prefix);
24802 amit.gupta 338
		SellerWarehouse sellerWarehouse = sellerWarehouseRepository.selectByPrefix(prefix);
339
		int newSequence = sellerWarehouse.getInvoiceSequence() + 1;
340
		sellerWarehouse.setInvoiceSequence(newSequence);
24814 amit.gupta 341
		return prefix + newSequence;
24802 amit.gupta 342
	}
24814 amit.gupta 343
 
24806 amit.gupta 344
	private void changePartnerInvoices() throws Exception {
24814 amit.gupta 345
		List<FofoOrder> fofoOrders = fofoOrderRepository.selectByInvoiceNumberLike("%SEC%");
346
		for (FofoOrder fofoOrder : fofoOrders) {
24806 amit.gupta 347
			FofoOrderItem fofoOrderItem = fofoOrderItemRepository.selectByOrderId(fofoOrder.getId()).get(0);
24814 amit.gupta 348
			if (fofoOrderItem.getBrand().equals("Vivo")) {
24806 amit.gupta 349
				String challanString = fofoOrder.getInvoiceNumber();
24814 amit.gupta 350
				String storeCode = invoiceNumberGenerationSequenceRepository.selectByFofoId(fofoOrder.getFofoId())
351
						.getPrefix();
24806 amit.gupta 352
				String invoiceNumber = orderService.getInvoiceNumber(fofoOrder.getFofoId(), storeCode);
353
				fofoOrder.setInvoiceNumber(invoiceNumber);
354
				fofoOrder.setCreateTimestamp(LocalDateTime.now());
355
				LOGGER.info(challanString + "\t" + invoiceNumber);
356
			}
24814 amit.gupta 357
 
24806 amit.gupta 358
		}
359
	}
24802 amit.gupta 360
 
23755 amit.gupta 361
}