Subversion Repositories SmartDukaan

Rev

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