Subversion Repositories SmartDukaan

Rev

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