Subversion Repositories SmartDukaan

Rev

Rev 23634 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.aramex.impl;

import in.shop2020.model.v1.order.Order;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.aramex.stub.Address;
import com.aramex.stub.ArrayOfShipment;
import com.aramex.stub.ClientInfo;
import com.aramex.stub.Contact;
import com.aramex.stub.LabelInfo;
import com.aramex.stub.Money;
import com.aramex.stub.Party;
import com.aramex.stub.ProcessedShipment;
import com.aramex.stub.Service_1_0_PortType;
import com.aramex.stub.Service_1_0_Service;
import com.aramex.stub.Service_1_0_ServiceLocator;
import com.aramex.stub.Shipment;
import com.aramex.stub.ShipmentCreationRequest;
import com.aramex.stub.ShipmentCreationResponse;
import com.aramex.stub.ShipmentDetails;
import com.aramex.stub.Weight;
import com.providers.logistics.Provider;
import com.providers.logistics.ShipmentInfo;

public class Aramex implements Provider {

        public String getAirwayBillNo(List<Order> orders) throws Exception{
                //return String.valueOf(new Date().getTime());
                try {
                        Order oneOrder = orders.get(0);
                        Party consignee = getConsigneeParty(oneOrder);
                        Party shipper = getTestShipper();
                        shipper.setReference1(oneOrder.getLogisticsTransactionId());
                        Shipment shp = getShipment(new ShipmentInfo(orders), consignee, shipper);
                        shp.setThirdParty(getThirdParty());
                        return getAwb(shp);
                } catch (Exception e) {
                        e.printStackTrace();
                        throw e;
                }
        }
        
        private Shipment getShipment(ShipmentInfo shipmentInfo, Party consignee, Party shipper) {
                Shipment shp = new Shipment();
                ShipmentDetails details = new ShipmentDetails();
                details.setProductGroup("DOM");
                details.setProductType("CDA");
                //For thirdParty
                details.setPaymentType("3");
                //details.setPaymentType("P");
                details.setChargeableWeight(new Weight("KG", shipmentInfo.getTotalWeightInKg()));
                details.setActualWeight(new Weight("KG", shipmentInfo.getTotalWeightInKg()));
                details.setDescriptionOfGoods("Electronics");
                details.setGoodsOriginCountry("IN");
                if(shipmentInfo.isCashOnDelivery()){
                        details.setServices("CODS");
                        details.setCashOnDeliveryAmount(new Money("INR", shipmentInfo.getNetPayable()));
                }
                details.setCustomsValueAmount(new Money("INR", shipmentInfo.getTotalAmount()));
                details.setNumberOfPieces(shipmentInfo.getTotalPcs());
                
                shp.setDetails(details);
                shp.setShipper(shipper);
                shp.setConsignee(consignee);
                shp.setPickupLocation("Delhi");
                shp.setShippingDateTime(Calendar.getInstance());
                Calendar dueDate = Calendar.getInstance();
                dueDate.add(Calendar.DATE, 4);
                shp.setDueDate(dueDate);
                
                return shp;
        }

        private Party getConsigneeParty(Order order) {
                Party consignee = new Party();
                
                Address consigneeAddress = new Address();
                consigneeAddress.setCountryCode("IN");
                consigneeAddress.setLine1(order.getCustomer_address1());
                consigneeAddress.setLine2(order.getCustomer_address2());
                consigneeAddress.setPostCode(order.getCustomer_pincode());
                consigneeAddress.setStateOrProvinceCode(order.getCustomer_state());
                consigneeAddress.setCity(order.getCustomer_city());

                Contact contact = new Contact();
                contact.setPhoneNumber1(order.getCustomer_mobilenumber());
                contact.setCellPhone(order.getCustomer_mobilenumber());
                contact.setPersonName(order.getCustomer_name());
                contact.setEmailAddress(order.getCustomer_email());
                contact.setCompanyName(order.getCustomer_name());
                
                consignee.setPartyAddress(consigneeAddress);
                consignee.setContact(contact);
                
                return consignee;
        }
        
        private Party getThirdParty() {
                Party thirdParty = new Party();
                
                thirdParty.setAccountNumber("IDA10002");
                
                Address thirdPartyAddress = new Address();
                thirdPartyAddress.setCountryCode("IN");
                thirdPartyAddress.setLine1("S Global Knowledge Park");
                thirdPartyAddress.setLine2("19A & 19B, Sector-125");
                thirdPartyAddress.setPostCode("201301");
                thirdPartyAddress.setStateOrProvinceCode("Uttar Pradesh");
                thirdPartyAddress.setCity("Noida");
                
                Contact sc = new Contact();
                sc.setEmailAddress("deena.nath@smartdukaan.com");
                sc.setCellPhone("9311608716");
                sc.setCompanyName("New Spice Solutions Private Limited");
                sc.setPersonName("Deenanath Gupta");
                sc.setPhoneNumber1("9311608716");
                
                thirdParty.setPartyAddress(thirdPartyAddress);
                thirdParty.setContact(sc);
                
                
                return thirdParty;
        }
        
        private String getAwb(Shipment shipment) throws Exception{
                Service_1_0_Service aramexService = new Service_1_0_ServiceLocator();
                Service_1_0_PortType service = aramexService.getBasicHttpBinding_Service_1_0();
                ShipmentCreationRequest parameters = new ShipmentCreationRequest();
                ArrayOfShipment shipments = new ArrayOfShipment();
                Shipment [] shipArray = new Shipment[1];
                
                
                shipArray[0]=shipment;
                shipments.setShipment(shipArray);
                
                LabelInfo labelInfo = new LabelInfo();
                labelInfo.setReportID(9729);
                labelInfo.setReportType("URL");
                parameters.setLabelInfo(labelInfo);
                parameters.setClientInfo(getLiveClientInfo());
                parameters.setShipments(shipArray);
                
                System.out.println(parameters);
                ShipmentCreationResponse scr = service.createShipments(parameters);
                System.out.println("Shipment response of is \n" + scr);
                if(scr.getHasErrors()){
                        if(scr.getNotifications() != null && scr.getNotifications()[0]!= null){
                                System.out.println("Some error " +  scr.getNotifications()[0].getMessage());
                        }
                        ProcessedShipment s = scr.getShipments()[0]; 
                        if (s!=null && s.getNotifications() != null && s.getNotifications()[0] != null) {
                                System.out.println("Some error in shipment" +  s.getNotifications()[0].getMessage());
                        }
                        throw new Exception();
                }
                ProcessedShipment ps = scr.getShipments()[0];
                return ps.getID();
        }
        
        private static ClientInfo getLiveClientInfo() {
                ClientInfo clientInfo = new ClientInfo();
                clientInfo.setAccountCountryCode("IN");
                clientInfo.setAccountNumber("IDA10002");
                clientInfo.setVersion("v1.0");  
                clientInfo.setAccountEntity("IDA");
                clientInfo.setUserName("deena.nath@smartdukaan.com");
                clientInfo.setPassword("Profitmandi@123");
                clientInfo.setAccountPin("554654");
                return clientInfo;
        }
        
        /*private static ClientInfo getTestClientInfo() {
                ClientInfo clientInfo = new ClientInfo();
                clientInfo.setAccountCountryCode("IN");
                clientInfo.setAccountNumber("50615758");
                clientInfo.setVersion("v1.0");  
                clientInfo.setAccountEntity("DEL");
                clientInfo.setUserName("adonismobitrade@yahoo.com");
                clientInfo.setPassword("Adonis@12345");
                clientInfo.setAccountPin("231164");
                return clientInfo;
        }*/
        
        private Party getTestShipper() {
                Party shipper = new Party();
                Contact sc = new Contact();
                sc.setEmailAddress("deena.nath@smartdukaan.com");
                sc.setCellPhone("9311608716");
                sc.setCompanyName("New Spice Solutions Private Limited");
                sc.setPersonName("Deenanath Gupta");
                sc.setPhoneNumber1("9311608716");
                shipper.setContact(sc);
                Address shipperAddress = new Address();
                shipperAddress.setLine1("Khasra No-322, Second Floor");
                shipperAddress.setLine2("Opp. Purani Tel Mil, Neb Sarai");
                shipperAddress.setCountryCode("IN");
                shipperAddress.setPostCode("110068");
                shipper.setPartyAddress(shipperAddress);
                //ccountNumber("IDA10002");
                return shipper;
        }
        
        public static void main(String[] args) {
                List<Order> orders = new ArrayList<Order>();
                Order o = new Order();
                o.setTotal_amount(1000.00);
                o.setTotal_weight(.900);
                o.setNet_payable_amount(0.01);
                o.setShippingCost(20.0);
                o.setCod(true);
                orders.add(o);
                ShipmentInfo s = new ShipmentInfo(orders);
                System.out.println(s.isCashOnDelivery());
                
                
        }
        
}