Rev 26003 | Blame | Compare with Previous | Last modification | View Log | RSS feed
/****/package in.shop2020.support.controllers;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Collections;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.thrift.TException;import com.opensymphony.xwork2.ValidationAwareSupport;import in.shop2020.logistics.PickUpType;import in.shop2020.model.v1.catalog.CatalogServiceException;import in.shop2020.model.v1.catalog.HsItem;import in.shop2020.model.v1.catalog.Item;import in.shop2020.model.v1.order.LineItem;import in.shop2020.model.v1.order.Order;import in.shop2020.model.v1.order.OrderSource;import in.shop2020.model.v1.order.OrderType;import in.shop2020.model.v1.order.ProductCondition;import in.shop2020.model.v1.order.Transaction;import in.shop2020.model.v1.order.TransactionService.Client;import in.shop2020.model.v1.order.TransactionServiceException;import in.shop2020.model.v1.order.TransactionStatus;import in.shop2020.model.v1.user.Address;import in.shop2020.model.v1.user.ItemPriceQuantity;import in.shop2020.model.v1.user.ShoppingCartException;import in.shop2020.model.v1.user.User;import in.shop2020.payments.Attribute;import in.shop2020.payments.Payment;import in.shop2020.payments.PaymentException;import in.shop2020.payments.PaymentStatus;import in.shop2020.thrift.clients.CatalogClient;import in.shop2020.thrift.clients.PaymentClient;import in.shop2020.thrift.clients.TransactionClient;import in.shop2020.thrift.clients.UserClient;import in.shop2020.utils.ModelUtils;/*** @author mandeep**/@SuppressWarnings("serial")public class BulkOrderController extends ValidationAwareSupport {private static final int WALLET_GATEWAY_ID = 8;private static Log log = LogFactory.getLog(BulkOrderController.class);private SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");private static final String PAY_METHOD = "payMethod";private String id;private String transactionId;private String customerEmailId;private String rtgsId;private String rtgsBank;private String rtgsBranch;private String ifscCode;private String amount;private String itemId;private String output;private String pickUp;private String orderType;private String tinNumber;private String poRefNumber;private int productCondition;private Transaction transaction;private Payment payment;private long userWalletAmount;private int userId;private List<Address> userAddresses;private int addressId;private Map<Transaction, Double> transactions;private List<LineItem> lineItems;private String secret;public String getSecret() {return secret;}public void setSecret(String secret) {this.secret = secret;}private Client transactionClient;private in.shop2020.model.v1.user.UserContextService.Client userClient;private in.shop2020.payments.PaymentService.Client paymentClient;private in.shop2020.model.v1.catalog.CatalogService.Client catalogClient;public String index() {return "index";}public String editNew() throws Throwable {if(this.secret != null && this.secret.equals("shazam!")) {this.userClient = new UserClient().getClient();this.transactionClient = new TransactionClient().getClient();User user = userClient.getUserByEmail(customerEmailId);List<Address> addresses = this.userClient.getAllAddressesForUser(user.getUserId());this.setUserAddresses(addresses);this.userWalletAmount = this.transactionClient.getUserWallet(user.getUserId()).getAmount();return "editNew";}return "index1";}public String show() throws Throwable {this.paymentClient = new PaymentClient().getClient();this.transactionClient = new TransactionClient().getClient();try {transactionId = id;transaction = this.transactionClient.getTransaction(Long.valueOf(transactionId));payment = this.paymentClient.getPaymentForTxnId(Long.valueOf(transactionId)).get(0);} catch (NumberFormatException e) {log.error("Could not lookup transaction Id: " + transactionId, e);} catch (TransactionServiceException e) {log.error("Could not lookup transaction Id: " + transactionId, e);} catch (TException e) {log.error("Could not lookup transaction Id: " + transactionId, e);} catch (PaymentException e) {log.error("Could not lookup payment for transaction Id: " + transactionId, e);}return "show";}public String create() throws Throwable {this.userClient = new UserClient().getClient();this.paymentClient = new PaymentClient().getClient();this.transactionClient = new TransactionClient().getClient();this.catalogClient = new CatalogClient().getClient();log.info("Creating orders for " + lineItems);boolean hasError = true;try {User user = this.userClient.getUserByEmail(customerEmailId);List<ItemPriceQuantity> ipqList = new ArrayList<ItemPriceQuantity>();double totalAmount = 0;if (user == null) {addActionError("Could not find user by email: " + customerEmailId);} else {double walletAmount = this.transactionClient.getUserWallet(user.getUserId()).getAmount();for (LineItem li : lineItems) {ItemPriceQuantity ipq = new ItemPriceQuantity();ipq.setQty((long) li.getQuantity());ipq.setItemId(li.getItem_id());log.info("Item ID ----------- " + li.getItem_id());if(li.getUnit_price() == 0) {addActionError("Unit price should not be zero for any items");}ipq.setPrice(li.getUnit_price());ipqList.add(ipq);totalAmount += li.getUnit_price() * li.getQuantity();}if (totalAmount > walletAmount) {addActionError("Total Amount not be greater than wallet amount");} else if (!this.userClient.addItemPricingToCart(user.getActiveCartId(), ipqList)) {addActionError("Failed to add pricing to cart, please try again");} else if (this.addressId == 0 && user.getDefaultAddressId() == 0) {addActionError("Please make sure default address is set");} else {log.info("Setting wallet amount in cart");this.userClient.addAddressToCart(user.getActiveCartId(), user.getDefaultAddressId());this.userClient.setWalletAmountInCart(user.getActiveCartId(), totalAmount);transactionId = String.valueOf(this.userClient.createOrders(user.getActiveCartId(), "BACKEND", 0, "", 0,user.getUserId(), 7890, OrderSource.WEBSITE.getValue(), true));log.info("Creating wallet payment for transactionId - " + transactionId);createPayment(user);in.shop2020.model.v1.order.Attribute orderAttribute1 = new in.shop2020.model.v1.order.Attribute();if (poRefNumber != null && !poRefNumber.equals("")) {orderAttribute1.setName("poRefNumber");orderAttribute1.setValue(poRefNumber);this.transactionClient.setOrderAttributeForTransaction(Long.parseLong(transactionId),orderAttribute1);}this.transactionClient.changeTransactionStatus(Long.valueOf(transactionId),TransactionStatus.AUTHORIZED, "Payment received for the order",PickUpType.valueOf(pickUp).getValue(), OrderType.valueOf(orderType), OrderSource.WEBSITE);this.transactionClient.changeTransactionStatus(Long.valueOf(transactionId),TransactionStatus.IN_PROCESS, "Paid fully through wallet",PickUpType.valueOf(pickUp).getValue(), OrderType.valueOf(orderType), OrderSource.WEBSITE);log.info("Successfully created transaction: " + transactionId + " for amount: " + amount);this.transactionClient = new TransactionClient().getClient();this.transactionClient.markOrderForRegisteredGstInvoice(Collections.singletonList(Long.valueOf(transactionId)));try {this.transactionClient.enqueueTransactionInfoEmail(Long.parseLong(transactionId));} catch (Exception e1) {log.error("Unable to update status of transaction. Thrift Exception:", e1);}this.userClient = new UserClient().getClient();transaction = this.transactionClient.getTransaction(Long.valueOf(transactionId));this.resetCart(transaction);hasError = false;}}} catch (Exception e) {addActionError("Error occerred - " + e.getMessage());e.printStackTrace();}if (hasError) {return "error";}id = transactionId;return show();}private long resetCart(Transaction transaction) {long sum = 0;Map<Long, Double> items = new HashMap<Long, Double>();for (Order order : transaction.getOrders()) {sum += order.getGvAmount();for (LineItem lineitem : order.getLineitems()) {Long itemId = lineitem.getItem_id();Double quantity = items.get(itemId);if (quantity == null) {quantity = lineitem.getQuantity();} else {quantity = quantity + lineitem.getQuantity();}items.put(itemId, quantity);}}log.debug("Items to reset in cart are: " + items);try {// TODO Optimize the function to send less data over the wirethis.userClient.resetCart(transaction.getShoppingCartid(), items);} catch (TException e) {log.error("Error while updating information in payment database.", e);} catch (ShoppingCartException e) {log.error("Error while reseting the cart in cart database.", e);} catch (Exception e) {log.error("Unexpected exception", e);}return sum;}public String getModelName() throws Throwable {this.catalogClient = new CatalogClient().getClient();output = "Invalid Item Id: " + itemId;try {Item item = this.catalogClient.getItem(Long.parseLong(itemId));if (item != null && item.getId() != 0) {output = ModelUtils.extractProductNameFromItem(item);}} catch (NumberFormatException e) {log.error("Could not parse itemId: " + itemId, e);} catch (CatalogServiceException e) {log.error("Could not lookup itemId: " + itemId, e);} catch (TException e) {log.error("Could not find itemId: " + itemId, e);}return "output";}public String getPartnerPrice() throws Throwable {this.catalogClient = new CatalogClient().getClient();output = "Invalid Item Id: " + itemId;HsItem item = this.catalogClient.getHsItem(itemId);if (item != null && item.getItemId() != 0) {output = String.valueOf(item.getListingPrice());}return "output";}/*** As it is always going to be wallet**/private void createPayment(User user) throws NumberFormatException, PaymentException, TException {List<Attribute> paymentAttributes = new ArrayList<Attribute>();paymentAttributes.add(new Attribute(PAY_METHOD, "7890"));long paymentId = this.paymentClient.createPayment(user.getUserId(), Double.valueOf(amount), WALLET_GATEWAY_ID,Long.valueOf(transactionId), false);this.paymentClient.updatePaymentDetails(paymentId, null, null, null, null, null, null, null, null,PaymentStatus.SUCCESS, null, paymentAttributes);}public String convertDate(long date) {return SDF.format(new Date(date));}public String extractProductName(LineItem lineItem) {if (lineItem == null) {return "N/A";}return ModelUtils.extractProductNameFromLineItem(lineItem);}// Getters and Setterspublic String getCustomerEmailId() {return customerEmailId;}public void setCustomerEmailId(String customerEmailId) {this.customerEmailId = customerEmailId;}public String getRtgsId() {return rtgsId;}public void setRtgsId(String rtgsId) {this.rtgsId = rtgsId;}public String getRtgsBank() {return rtgsBank;}public void setRtgsBank(String rtgsBank) {this.rtgsBank = rtgsBank;}public String getRtgsBranch() {return rtgsBranch;}public void setRtgsBranch(String rtgsBranch) {this.rtgsBranch = rtgsBranch;}public String getIfscCode() {return ifscCode;}public void setIfscCode(String ifscCode) {this.ifscCode = ifscCode;}public String getAmount() {return amount;}public void setAmount(String amount) {this.amount = amount;}public List<LineItem> getLineItems() {return lineItems;}public void setLineItems(List<LineItem> lineItems) {this.lineItems = lineItems;}public Map<Transaction, Double> getTransactions() {return transactions;}public void setTransactions(Map<Transaction, Double> transactions) {this.transactions = transactions;}public String getTransactionId() {return transactionId;}public void setTransactionId(String transactionId) {this.transactionId = transactionId;}public Transaction getTransaction() {return transaction;}public void setTransaction(Transaction transaction) {this.transaction = transaction;}public Payment getPayment() {return payment;}public void setPayment(Payment payment) {this.payment = payment;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getItemId() {return itemId;}public void setItemId(String itemId) {this.itemId = itemId;}public String getOutput() {return output;}public void setOutput(String output) {this.output = output;}public String getPickUp() {return pickUp;}public void setPickUp(String pickUp) {this.pickUp = pickUp;}public String getOrderType() {return orderType;}public void setOrderType(String orderType) {this.orderType = orderType;}public String getTinNumber() {return tinNumber;}public void setTinNumber(String tinNumber) {this.tinNumber = tinNumber;}public int getProductCondition() {return productCondition;}public void setProductCondition(int productCondition) {this.productCondition = productCondition;}public String getPoRefNumber() {return poRefNumber;}public void setPoRefNumber(String poRefNumber) {this.poRefNumber = poRefNumber;}public static void main(String[] args) {BulkOrderController blc = new BulkOrderController();blc.setCustomerEmailId("neavins@amazon.com");blc.setRtgsId("rtgsId");blc.setRtgsBank("rtgsBank");blc.setRtgsBranch("rtgsBranch");blc.setIfscCode("ifscCode");blc.setAmount("3400");blc.setItemId("10661");// blc.setOutput();blc.setPickUp("COURIER");blc.setOrderType("B2Cbulk");// blc.setTinNumber();blc.setProductCondition(ProductCondition.GOOD.getValue());blc.index();}public double getWalletAmount() {return this.userWalletAmount;}public List<Address> getUserAddresses() {return userAddresses;}public void setUserAddresses(List<Address> userAddresses) {this.userAddresses = userAddresses;}}