Subversion Repositories SmartDukaan

Rev

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