Subversion Repositories SmartDukaan

Rev

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