Subversion Repositories SmartDukaan

Rev

Rev 13742 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020;

import in.shop2020.model.v1.catalog.CatalogService;
import in.shop2020.model.v1.catalog.SnapdealItem;
import in.shop2020.model.v1.catalog.CatalogServiceException;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.inventory.InventoryService;
import in.shop2020.model.v1.inventory.InventoryServiceException;
import in.shop2020.model.v1.inventory.InventoryType;
import in.shop2020.model.v1.inventory.WarehouseType;

import in.shop2020.model.v1.inventory.VendorItemPricing;
import in.shop2020.model.v1.inventory.Warehouse;
import in.shop2020.model.v1.order.LineItem;
import in.shop2020.model.v1.order.OrderStatus;
import in.shop2020.model.v1.order.OrderType;
import in.shop2020.model.v1.order.SnapdealOrder;
import in.shop2020.model.v1.order.SourceDetail;
import in.shop2020.model.v1.order.Transaction;
import in.shop2020.model.v1.order.TransactionServiceException;
import in.shop2020.model.v1.order.TransactionStatus;
import in.shop2020.model.v1.order.TransactionService.Client;
import in.shop2020.model.v1.user.User;
import in.shop2020.payments.PaymentException;
import in.shop2020.payments.PaymentStatus;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.InventoryClient;
import in.shop2020.thrift.clients.PaymentClient;
import in.shop2020.thrift.clients.TransactionClient;
import in.shop2020.thrift.clients.UserClient;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import in.shop2020.utils.GmailUtils;

import order.SaholicAPI;
import order.SaholicAPI.Order;

public class ProcessSnapdealOrder {
        private static final int SNAPDEAL_SOURCE_ID = 7;
        /*private static final String FIRSTFLIGHT = "First Flight";
        private static final String DELHIVERY = "DELHIVERY";
        private static final String BLUEDART = "Bluedart";
        private static final String CHHOTU = "CHHOTU";  
        private static final String LAST_MILE = "LAST_MILE";
        private static final String  ECOMM_EXP = "Ecom Express";
        private static final String  EWINGS = "EWINGS";
        private static final String DTDC = "DTDC";
        private static final String WAREOLOG = "Wareolog";
        private static final String  MIRAKLE = "MIRAKLE";
        private static final String RAJIV_TRANSPORT = "RAJIV_TRANSPORT";
        private static final String BLUEDART_HIGH_VALUE = "BLUEDART_HIGH_VALUE";
        private static final String BLUEDART_SURFACE = "BLUEDART_SURFACE";
        private static final String BLUEDART_APEX = "BLUEDART APEX";
        private static final String ARAMEX = "ARAMEX";
        private static final String GATI = "GATI";
        private static final String INDIA_POST = "INDIA POST";
        private static final String FEDEX = "FEDEX";
        private static final String IOT = "IOT";
        private static final String RED_EXP = "Red Express";
        private static final String GHARPAY = "GHARPAY";
        private static final String QUANTIUM = "Quantium Solutions";
        private static final String GATI_AIR = "GATI AIR";
        private static final String VDELIVER = "VDELIVER";
        private static final String OVERNITE_EXP = "Overnite Express";
        private static final String BIBHA_CARGO =  "BIBHA CARGO AND COURIER PVT LTD";
        private static final String FF_DELHI = "FF-Delhi";
        private static final String FF_GURGAON = "FF-GURGAON";
        private static final String TANVI_EXPRESS = "Tanvi Express";
        private static final String VELIX = "Velex Logistics Pvt. Ltd";
        private static final String XPRESSBEES = "XPRESSBEES";*/
        private static String transactionId;
        private static long paymentId;
        private static final int SNAPDEAL_GATEWAY_ID = 18;
        private static Logger logger;

        public static void processSnapdealOrders(File file) throws JAXBException{
                logger = LoggerFactory.getLogger(ProcessSnapdealOrder.class);
                JAXBContext jc = JAXBContext.newInstance(SaholicAPI.class);
                Unmarshaller unmarshaller = jc.createUnmarshaller();
                SaholicAPI orders = (SaholicAPI) unmarshaller.unmarshal(file);
                Marshaller marshaller = jc.createMarshaller();
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                marshaller.marshal(orders, System.out);
                SourceDetail sourceDetail = null;
                User user = null;
                TransactionClient tsc = null;
                try {
                        tsc = new TransactionClient();
                        sourceDetail = tsc.getClient().getSourceDetail(SNAPDEAL_SOURCE_ID);
                } catch (Exception e) {
                        logger.error("Unable to establish connection to the transaction service while getting Snapdeal Source Detail ", e);
                }
                try {   
                        in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient().getClient();
                        user = userClient.getUserByEmail(sourceDetail.getEmail());
                } catch (Exception e) {
                        logger.error("Unable to establish connection to the User service ", e);
                }
                int duplicate_orders = 0;
                int new_orders = 0;
                int processed_orders = 0;
                StringBuffer sb = new StringBuffer();
                LineItem lineItem = null;
                CatalogService.Client catalogClient = null;
                SnapdealItem snapdealItem = null;
                double SellingPrice = 0;
                long sku;
                String subOrderId;
                for(Order order:orders.getOrder()){
                        subOrderId = order.getSuborderId();
                        Client transaction_client = null;
                        boolean flag = false;
                        try {
                                transaction_client = new TransactionClient().getClient();
                        } catch (TTransportException e2) {
                                try {
                                        transaction_client = new TransactionClient().getClient();
                                } catch (TTransportException e) {
                                        logger.error("Problem with Transaction service " + subOrderId);
                                        e.printStackTrace();
                                        continue;
                                }
                                e2.printStackTrace();
                        }
                        try {
                                flag = transaction_client.snapdealOrderExists(subOrderId, order.getReferenceCode());
                        } catch (TException e2) {
                                try {
                                        flag = new TransactionClient().getClient().snapdealOrderExists(subOrderId, order.getReferenceCode());
                                } catch (TTransportException e) {
                                        logger.error("Problem with Transaction service " + subOrderId);
                                        e.printStackTrace();
                                        continue;
                                } catch (TException e) {
                                        logger.error("Problem with Transaction service " + subOrderId);
                                        e.printStackTrace();
                                        continue;

                                }
                        }
                        if(flag) {
                                logger.error("Snapdeal suborder id exists " + subOrderId);
                                duplicate_orders++;
                                continue;
                        }
                        new_orders++;
                        Transaction txn = new Transaction();
                        txn.setShoppingCartid(user.getActiveCartId());
                        txn.setCustomer_id(user.getUserId());
                        System.out.println("User Id is " + user.getUserId());
                        txn.setCreatedOn(new Date().getTime());
                        txn.setTransactionStatus(TransactionStatus.INIT);
                        txn.setStatusDescription("Order for Snapdeal ");
                        List<in.shop2020.model.v1.order.Order> orderlist = new ArrayList<in.shop2020.model.v1.order.Order>();
                        try {
                                flag = true;
                                while(flag){
                                        try{
                                                catalogClient =  new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
                                                snapdealItem = catalogClient.getSnapdealItembySkuAtSnapdeal(order.getSKUCode());
                                                flag=false;
                                        }
                                        catch (TException e) {
                                        }
                                }
                                if(snapdealItem.getItem_id()!=0){
                                        sku = snapdealItem.getItem_id();
                                }
                                else{
                                        sb.append(order.getSuborderId() +" "+"SKU not found "+"\n");
                                        continue;
                                }
                                if(snapdealItem.getExceptionPrice()!=0 && snapdealItem.getItem_id()!=0 && (order.getSellingPricePerItem() <  snapdealItem.getExceptionPrice())){
                                        SellingPrice = snapdealItem.getExceptionPrice();
                                }
                                else{
                                        SellingPrice = order.getSellingPricePerItem();
                                }
                                try{
                                        lineItem = createLineItem(sku,SellingPrice);
                                }
                                catch (TException e) {
                                        logger.error("Unable to create order for suborderid  " + order.getSuborderId() , e);
                                        e.printStackTrace();
                                        sb.append(order.getSuborderId() + " " + "Exception while creating lineitem" + "\n");
                                        continue;
                                }
                                if(lineItem == null){
                                        sb.append(order.getSuborderId() +" "+"SKU not found "+"\n");
                                        continue;
                                }
                                lineItem.setExtra_info("SubOrderId = " + order.getSuborderId() + " ReferenceCode = " + order.getReferenceCode());
                        } catch (CatalogServiceException e) {
                                logger.error("Unable to create order for suborderid  " + order.getSuborderId() , e);
                                e.printStackTrace();
                                sb.append(order.getSuborderId() + " " + "Problem with catalog service" + "\n");
                                continue;
                        } 
                        in.shop2020.model.v1.order.Order t_order = new in.shop2020.model.v1.order.Order();
                        t_order.setCustomer_id(user.getUserId());
                        t_order.setCustomer_email(sourceDetail.getEmail());
                        t_order.setCustomer_name(order.getCustomerName());
                        t_order.setCustomer_address1("");
                        t_order.setCustomer_address2("");
                        t_order.setCustomer_city(order.getCity());
                        t_order.setCustomer_mobilenumber("+91-9999999999");
                        t_order.setCustomer_state(order.getState());
                        t_order.setCustomer_pincode(String.valueOf(order.getPINCode()));
                        t_order.setTotal_amount(SellingPrice);            
                        t_order.setTotal_weight(lineItem.getTotal_weight());
                        t_order.setLineitems(Collections.singletonList(lineItem));            
                        t_order.setStatus(OrderStatus.PAYMENT_PENDING);
                        t_order.setStatusDescription("Payment Pending");
                        t_order.setCreated_timestamp(new Date().getTime());
                        t_order.setOrderType(OrderType.B2C);
                        t_order.setCod(false);
                        System.out.println("t order created");
                        SimpleDateFormat istFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date snapdealTxnDate = null;
                        try {
                                snapdealTxnDate = istFormatter.parse(order.getOrderPlacedDate());
                        } catch (ParseException e) {
                                logger.error(order.getSuborderId() + " Could not parse snapdeal order date from file " , e);
                                sb.append(order.getSuborderId() + " Could not parse order date" +"\n");
                                continue;
                        }
                        try {
                                Date shipDate = new Date(snapdealTxnDate.getTime()+172800000l);
                                Calendar time = Calendar.getInstance();
                                t_order.setPromised_shipping_time(shipDate.getTime());
                                t_order.setExpected_shipping_time(shipDate.getTime());
                                time.add(Calendar.DAY_OF_MONTH, 4);
                                t_order.setPromised_delivery_time(time.getTimeInMillis());
                                t_order.setExpected_delivery_time(time.getTimeInMillis());
                                System.out.println("Dates set in transaction");
                        } catch(Exception e) {  
                                logger.error("Error in updating Shipping or Delivery Time for suborderid  " + order.getSuborderId());
                                sb.append(order.getSuborderId() + " Could not update delivery time" + " " + "\n");
                                continue;
                        }
                        InventoryService.Client inventoryClient = null;
                        Warehouse fulfillmentWarehouse= null;
                        try {
                                inventoryClient = new InventoryClient().getClient();
                                if(snapdealItem.getItem_id()!=0 && snapdealItem.getWarehouseId()!=0) {
                                        fulfillmentWarehouse = inventoryClient.getWarehouse(snapdealItem.getWarehouseId());
                                } else {
                                        List<Long> itemAvailability = inventoryClient.getItemAvailabilityAtLocation(sku, 1);
                                        fulfillmentWarehouse = inventoryClient.getWarehouse(itemAvailability.get(0));
                                        if(fulfillmentWarehouse.getStateId()!=0){
                                                fulfillmentWarehouse = inventoryClient.getWarehouse(7);
                                        }
                                }
                                t_order.setFulfilmentWarehouseId(fulfillmentWarehouse.getId());
                                long billingWarehouseId = 0;
                                if(fulfillmentWarehouse.getBillingWarehouseId()== 0) {
                                        inventoryClient = new InventoryClient().getClient();
                                        List<Warehouse> warehouses = inventoryClient.getWarehouses(WarehouseType.OURS, InventoryType.GOOD, fulfillmentWarehouse.getVendor().getId(), 0, 0);
                                        for(Warehouse warehouse : warehouses) {
                                                if(warehouse.getBillingWarehouseId()!=0) {
                                                        billingWarehouseId = warehouse.getBillingWarehouseId();
                                                        break;
                                                }
                                        }
                                }else {
                                        billingWarehouseId = fulfillmentWarehouse.getBillingWarehouseId();
                                }

                                //logger.info("Billing warehouse id for suborderid  " + order.getSuborderId() + " is " + fulfillmentWarehouse.getBillingWarehouseId());
                                t_order.setWarehouse_id(billingWarehouseId);
                                VendorItemPricing vendorItemPricing = new VendorItemPricing();
                                if(fulfillmentWarehouse.getId()==7) {
                                        Item item = new CatalogClient().getClient().getItem(lineItem.getItem_id());
                                        vendorItemPricing = inventoryClient.getItemPricing(lineItem.getItem_id(), item.getPreferredVendor());
                                } else {
                                        vendorItemPricing = inventoryClient.getItemPricing(lineItem.getItem_id(), fulfillmentWarehouse.getVendor().getId());
                                }

                                t_order.getLineitems().get(0).setTransfer_price(vendorItemPricing.getTransferPrice());
                                t_order.getLineitems().get(0).setNlc(vendorItemPricing.getNlc());
                        } catch (InventoryServiceException e) {
                                logger.error("Error connecting inventory service for suborderid  " + order.getSuborderId() , e);
                                sb.append(order.getSuborderId() + " Inventory Service Exception" + " " + "\n");
                                continue;
                        } catch (TTransportException e) {
                                logger.error("Transport Exception with Inventory Service for suborderid  " + order.getSuborderId() , e);
                                sb.append(order.getSuborderId() + " Transport Exception with Inventory Service" + " " + "\n");
                                continue;
                        } catch (TException e) {
                                logger.error("Exception with Inventory Service for suborderid  " + order.getSuborderId() , e);
                                sb.append(order.getSuborderId() + " Exception in Inventory Service" + " " + "\n");
                                continue;
                        } catch (CatalogServiceException e) {
                                logger.error("Exception with Catalog Service for suborderid  " + order.getSuborderId() + " while getting item " + lineItem.getItem_id(), e);
                                sb.append(order.getSuborderId() + " Exception in Catalog Service" + " " + "\n");
                                continue;
                        }
                        /*if(order.getCourier().equalsIgnoreCase(FIRSTFLIGHT)) {
                                t_order.setLogistics_provider_id(12);
                        } else if(order.getCourier().equalsIgnoreCase(DELHIVERY)) {
                                t_order.setLogistics_provider_id(13);
                        } else if(order.getCourier().equalsIgnoreCase(BLUEDART)) {
                                t_order.setLogistics_provider_id(14);
                        }else if(order.getCourier().equalsIgnoreCase(CHHOTU)) {
                                t_order.setLogistics_provider_id(16);
                        }
                        else if(order.getCourier().equalsIgnoreCase(LAST_MILE)){
                                t_order.setLogistics_provider_id(17);
                        }
                        else if(order.getCourier().equalsIgnoreCase(ECOMM_EXP)){
                                t_order.setLogistics_provider_id(18);
                        }
                        else if(order.getCourier().equalsIgnoreCase(EWINGS)){
                                t_order.setLogistics_provider_id(19);
                        }
                        else if(order.getCourier().equalsIgnoreCase(DTDC)){
                                t_order.setLogistics_provider_id(20);
                        }
                        else if(order.getCourier().equalsIgnoreCase(WAREOLOG)){
                                t_order.setLogistics_provider_id(21);
                        }
                        else if(order.getCourier().equalsIgnoreCase(MIRAKLE)){
                                t_order.setLogistics_provider_id(22);
                        }
                        else if(order.getCourier().equalsIgnoreCase(RAJIV_TRANSPORT)){
                                t_order.setLogistics_provider_id(23);
                        }
                        else if(order.getCourier().equalsIgnoreCase(BLUEDART_HIGH_VALUE)){
                                t_order.setLogistics_provider_id(24);
                        }
                        else if(order.getCourier().equalsIgnoreCase(BLUEDART_SURFACE)){
                                t_order.setLogistics_provider_id(25);
                        }
                        else if(order.getCourier().equalsIgnoreCase(BLUEDART_APEX)){
                                t_order.setLogistics_provider_id(26);
                        }
                        else if(order.getCourier().equalsIgnoreCase(ARAMEX)){
                                t_order.setLogistics_provider_id(27);
                        }
                        else if(order.getCourier().equalsIgnoreCase(GATI)){
                                t_order.setLogistics_provider_id(28);
                        }
                        else if(order.getCourier().equalsIgnoreCase(INDIA_POST)){
                                t_order.setLogistics_provider_id(29);
                        }
                        else if(order.getCourier().equalsIgnoreCase(FEDEX)){
                                t_order.setLogistics_provider_id(30);
                        }
                        else if(order.getCourier().equalsIgnoreCase(IOT)){
                                t_order.setLogistics_provider_id(31);
                        }
                        else if(order.getCourier().equalsIgnoreCase(RED_EXP)){
                                t_order.setLogistics_provider_id(32);
                        }
                        else if(order.getCourier().equalsIgnoreCase(VDELIVER)){
                                t_order.setLogistics_provider_id(33);
                        }
                        else if(order.getCourier().equalsIgnoreCase(GHARPAY)){
                                t_order.setLogistics_provider_id(34);
                        }
                        else if(order.getCourier().equalsIgnoreCase(QUANTIUM)){
                                t_order.setLogistics_provider_id(35);
                        }
                        else if(order.getCourier().equalsIgnoreCase(GATI_AIR)){
                                t_order.setLogistics_provider_id(36);
                        }
                        else if(order.getCourier().equalsIgnoreCase(OVERNITE_EXP)){
                                t_order.setLogistics_provider_id(37);
                        }
                        else if(order.getCourier().equalsIgnoreCase(BIBHA_CARGO)){
                                t_order.setLogistics_provider_id(38);
                        }
                        else if(order.getCourier().equalsIgnoreCase(FF_DELHI)){
                                t_order.setLogistics_provider_id(39);
                        }
                        else if(order.getCourier().equalsIgnoreCase(FF_GURGAON)){
                                t_order.setLogistics_provider_id(40);
                        }
                        else if(order.getCourier().trim().equalsIgnoreCase(TANVI_EXPRESS)){
                                t_order.setLogistics_provider_id(41);
                        }
                        else if(order.getCourier().trim().equalsIgnoreCase(VELIX)){
                                t_order.setLogistics_provider_id(42);
                        }
                        else if(order.getCourier().trim().equalsIgnoreCase(XPRESSBEES)){
                                t_order.setLogistics_provider_id(43);
                        }

                        else {*/
                        t_order.setLogistics_provider_id(44);
                                /*logger.error("Error No Courier Match please add courier to our system for suborderid  " + order.getSuborderId() + "Courier Name " + order.getCourier());
                                sb.append(order.getSuborderId() + " Error No Courier Match " + order.getCourier() +"\n");
                                continue;
                                 */     
                        //}

                        t_order.setAirwaybill_no(String.valueOf(order.getAWBNumber()));
                        t_order.setTracking_id(String.valueOf(order.getAWBNumber()));
                        t_order.setTotal_amount(SellingPrice);
                        t_order.setOrderType(OrderType.B2C);
                        t_order.setSource(SNAPDEAL_SOURCE_ID);
                        t_order.setOrderType(OrderType.B2C);
                        orderlist.add(t_order);
                        txn.setOrders(orderlist);
                        String referenceCode = order.getReferenceCode();
                        String productName = order.getProduct();
                        
                        
                        try {
                                transactionId =  String.valueOf(transaction_client.createTransaction(txn));
                        } catch (TransactionServiceException e) {
                                logger.error(order.getSuborderId() + " Could not create transaction " , e);
                                sb.append(order.getSuborderId() + " Could not create transaction" +"\n");
                                continue;
                        } catch (TException e) {
                                logger.error("Problem with transaction service while creating transaction", e);
                                sb.append(order.getSuborderId() + " Transaction Service Exception could not create transaction" +"\n");
                                continue;
                        }
                        try{
                                paymentId = createPayment(user, (new Long(subOrderId)).toString(),lineItem.getTotal_price());
                        }
                        catch (NumberFormatException e) {
                                logger.error("Could not create payment",e);
                                sb.append(order.getSuborderId() + " Could not create payment");
                                e.printStackTrace();
                                continue;
                        } catch (PaymentException e) {
                                logger.error("Could not create payment payment exception",e);
                                sb.append(order.getSuborderId() + " Could not create payment Payment exception");
                                e.printStackTrace();
                                continue;
                        } catch (TException e) {
                                logger.error("Could not create payment thrift exception",e);
                                sb.append(order.getSuborderId() + " Could not create payment Thrift exception");
                                e.printStackTrace();
                                continue;
                        }
                        Transaction transaction = null;
                        try {
                                transaction = transaction_client.getTransaction(Long.parseLong(transactionId));
                        } catch (NumberFormatException e) {
                                logger.error("Problem parsing transaction id " + transactionId);
                                sb.append(order.getSuborderId() + " Problem parsing transaction id "+ transactionId +"\n");
                                e.printStackTrace();
                                continue;
                        } catch (TransactionServiceException e) {
                                logger.error("Problem getting transaction from service transaction id " + transactionId);
                                sb.append(order.getSuborderId() + " Problem getting transaction id "+ transactionId +"\n");
                                e.printStackTrace();
                                continue;
                        } catch (TException e) {
                                logger.error("Problem with transaction service while getting transaction id " + transactionId);
                                sb.append(order.getSuborderId() + " Problem with transaction service while getting transaction id "+ transactionId +"\n");
                                e.printStackTrace();
                                continue;
                        }
                        in.shop2020.model.v1.order.Order snapdealorder = transaction.getOrders().get(0);
                        try {
                                inventoryClient.reserveItemInWarehouse(lineItem.getItem_id(), fulfillmentWarehouse.getId(), 1, 
                                                snapdealorder.getId(), snapdealorder.getCreated_timestamp(), snapdealorder.getPromised_shipping_time(), snapdealorder.getLineitems().get(0).getQuantity());
                        } catch (InventoryServiceException e1) {
                                logger.error("Problem while reserving item in inventory service" + snapdealorder.getId());
                                sb.append(order.getSuborderId() + " Could not reserve inventory for sku "+ sku +"\n");
                                continue;
                        } catch (TException e1) {
                                logger.error("Problem with inventory service" + snapdealorder.getId());
                                sb.append(order.getSuborderId() + " Problem with inventory service while reserving inventory for sku "+ sku +"\n");
                                continue;
                        }
                        SnapdealOrder snapdealOrder = new SnapdealOrder();
                        snapdealOrder.setOrderId(snapdealorder.getId());
                        snapdealOrder.setSubOrderId(subOrderId);
                        snapdealOrder.setReferenceCode(referenceCode);
                        snapdealOrder.setProductName(productName);
                        snapdealOrder.setSnapdealTxnDate(snapdealTxnDate.getTime());
                        snapdealOrder.setListingPrice(order.getSellingPricePerItem());
                        snapdealOrder.setMaxNlc(snapdealItem.getMaxNlc());
                        try {
                                transaction_client.createSnapdealOrder(snapdealOrder);
                                new PaymentClient().getClient().updatePaymentDetails(paymentId, null, null, null, null, null, null, subOrderId, null, PaymentStatus.SUCCESS, null, null);

                        } catch (TException e) {
                                logger.error("Could not create Snapdeal Order");
                                sb.append(order.getSuborderId() + " Could not create snapdeal order"+"\n");
                        } catch (PaymentException e) {
                                logger.error("Could not update payment for Snapdeal Order");
                                sb.append(order.getSuborderId() + " Could not update payment for snapdeal order"+"\n");
                        }
                        processed_orders++;

                }
                java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
                Calendar cal=GregorianCalendar.getInstance();
                String emailFromAddress = "build@shop2020.in";
                String password = "cafe@nes";
                GmailUtils mailer = new GmailUtils();
                //String sendTo[] = new String[]{"vikram.raghav@shop2020.in"};
                String sendTo[] = new String[]{ "sandeep.sachdeva@shop2020.in", "vikram.raghav@shop2020.in", "rajneesh.arora@shop2020.in",
                                "khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
                                "yukti.jain@shop2020.in","manisha.sharma@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","anikendra.das@shop2020.in"};
                try {
                        if(new_orders==processed_orders && new_orders > 0){
                                String emailSubjectTxt = "Snapdeal Orders Created Successfully "+sdf.format(cal.getTime());
                                String text = "Total Orders Received : " + ( new_orders + duplicate_orders ) + "\n" + 
                                "Orders Already Existing : " + duplicate_orders + "\n" +
                                "Processed Orders : " + processed_orders + "\n" +
                                "Failed Orders : " + (new_orders - processed_orders);
                                mailer.sendSSLMessage(sendTo, emailSubjectTxt, text, emailFromAddress, password, new ArrayList<File>());
                        }
                        else if(new_orders > 0){
                                String emailSubjectTxt = "Snapdeal Orders Failed while creation "+sdf.format(cal.getTime());
                                String text = "Total Orders Received : " + ( new_orders + duplicate_orders ) + "\n" + 
                                "Orders Already Existing : " + duplicate_orders + "\n" +
                                "Processed Orders : " + processed_orders + "\n" +
                                "Failed Orders : " + (new_orders - processed_orders) + "\n"+ "\n" +
                                sb.toString();
                                mailer.sendSSLMessage(sendTo, emailSubjectTxt, text, emailFromAddress, password, new ArrayList<File>());
                        }
                        else{
                                String emailSubjectTxt = "No new Snapdeal Orders " + sdf.format(cal.getTime());
                                String text = "Total Orders Received : " + ( new_orders + duplicate_orders ) + "\n" + 
                                "Orders Already Existing : " + duplicate_orders + "\n" +
                                "Processed Orders : " + processed_orders + "\n" +
                                "Failed Orders : " + (new_orders - processed_orders) + "\n"+ "\n" +
                                sb.toString();
                                mailer.sendSSLMessage(sendTo, emailSubjectTxt, text, emailFromAddress, password, new ArrayList<File>());
                        }
                }
                catch (Exception e) {
                        e.printStackTrace();
                }



        }

        public static long createPayment(User user, String subOrderId, double amount) throws NumberFormatException, PaymentException, TException {
                in.shop2020.payments.PaymentService.Client client = new PaymentClient().getClient();
                return client.createPayment(user.getUserId(), amount, SNAPDEAL_GATEWAY_ID, Long.valueOf(transactionId), false);

        }   

        public static LineItem createLineItem(long itemId, double amount) throws CatalogServiceException, TException {
                LineItem lineItem = new LineItem();
                CatalogService.Client catalogClient = new CatalogClient().getClient();
                Item item = catalogClient.getItem(itemId);
                if(item.getId()==0){
                        //in case item id is incorrect..
                        return null;
                }

                lineItem.setProductGroup(item.getProductGroup());
                lineItem.setBrand(item.getBrand());
                lineItem.setModel_number(item.getModelNumber());
                lineItem.setModel_name(item.getModelName());
                lineItem.setExtra_info(item.getFeatureDescription());
                lineItem.setQuantity(1);
                lineItem.setItem_id(item.getId());
                lineItem.setUnit_weight(item.getWeight());
                lineItem.setTotal_weight(item.getWeight());
                lineItem.setUnit_price(amount);
                lineItem.setTotal_price(amount);

                if (item.getColor() == null || "NA".equals(item.getColor())) {
                        lineItem.setColor("");
                } else {
                        lineItem.setColor(item.getColor());
                }
                return lineItem;
        }

}