Subversion Repositories SmartDukaan

Rev

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