Example #1
0
        public HttpResponseMessage Post()
        {
            try
            {
                //--
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                //--

                #region Authentication

                var re      = Request;
                var headers = re.Headers;

                string username = "", password = "";

                if (headers.Contains("username"))
                {
                    username = headers.GetValues("username").First();
                    DB.LogGenera(iam, "username", username);
                }
                else
                {
                    DB.LogGenera(iam, "username", "none found");
                }

                if (headers.Contains("password"))
                {
                    password = headers.GetValues("password").First();
                    DB.LogGenera(iam, "password", password);
                }
                else
                {
                    DB.LogGenera(iam, "password", "none found");
                }

                #endregion

                string data = this.Request.Content.ReadAsStringAsync().Result;

                DB.LogGenera("Get_LTL_ratesController " + AppCodeConstants.mode, "Public API data", data);

                string url_string = "", request_format = "", error_response = "", response_string = "",
                       content_type = "application/json";

                LTLQuoteRequest ltl_quote_request = new LTLQuoteRequest();
                QuoteData       quoteData         = new QuoteData();

                #region Parse/Deserialize request, make sure JSON is valid

                // Parse/Deserialize request, make sure JSON is valid
                Validator vld = new Validator();
                vld.Validate_request_json(ref data, ref url_string, ref error_response, ref request_format,
                                          ref ltl_quote_request);

                #endregion

                // Xml_helper xml_helper = new Xml_helper();
                Json_helper json_helper = new Json_helper();

                Models.Public.LTL.Helper public_helper = new Models.Public.LTL.Helper();

                //DB.LogGenera(iam, "Public API url_string", url_string);
                //DB.LogGenera(iam, "Public API error_response", error_response);

                //DB.LogGenera(iam, "Public API url_string", url_string);

                string sql = "";
                if (error_response == "")
                {
                    // Do nothing
                }
                else
                {
                    //return error_response;
                    response_string = error_response;
                    var response_message = public_helper.Get_response_message(ref response_string, ref content_type);
                    sql = string.Concat("INSERT INTO Genera_Rating(QuoteID,Request_Data,Response_Data,TotalCube) VALUES(",
                                        "0,'", data, "','", response_string, "',", ltl_quote_request.totalCube,
                                        ")");
                    HelperFuncs.ExecuteNonQuery(AppCodeConstants.connStringAesAPI, ref sql, "Get_LTL_ratesController");

                    return(response_message);
                }


                // If got to here, request is valid JSON or XML
                // Convert JSON or XML to URL Encoded string and get GCM rates

                url_string = string.Concat("username="******"&password="******"&mode=ws&subdomain=www&showDLSRates=True",
                                           "&isHHG=False&isUSED=False",
                                           url_string);

                //DB.LogGenera(iam, "Public API url_string", url_string);



                Models.LTL.Helper helper = new Models.LTL.Helper();

                helper.Set_parameters(ref ltl_quote_request, ref quoteData);
                quoteData.username     = username;
                quoteData.showDLSRates = true;
                quoteData.subdomain    = "www";

                #region Check for > 4 line items

                if (quoteData.m_lPiece.Length > 4)
                {
                    DB.LogGenera(iam, "m_lPiece.Length > 4", "true");
                    response_string = json_helper.Build_error_response("35",
                                                                       "Too many freight item nodes, maximum number of item nodes is 4");

                    return(public_helper.Get_response_message(ref response_string, ref content_type));
                }
                else
                {
                    DB.LogGenera(iam, "m_lPiece.Length > 4", "false");
                }

                #endregion

                Validator validator = new Validator(ref quoteData);
                RateServiceNotification validation_result = validator.Validate_rating_request();

                #region If validation was not successful return error result

                if (validation_result.Code == "0")
                {
                    // Do nothing
                    DB.LogGenera(iam, "validation_result.Code", validation_result.Code);
                }
                else
                {
                    DB.LogGenera(iam, "validation_result.Code", validation_result.Code);

                    response_string = json_helper.Build_error_response(validation_result.Code, validation_result.Message);

                    var response_message = public_helper.Get_response_message(ref response_string, ref content_type);

                    sql = string.Concat("INSERT INTO Genera_Rating(QuoteID,Request_Data,Response_Data,TotalCube) VALUES(",
                                        "0,'", data, "','", response_string, "',", ltl_quote_request.totalCube,
                                        ")");
                    HelperFuncs.ExecuteNonQuery(AppCodeConstants.connStringAesAPI, ref sql, "Get_LTL_ratesController");

                    return(response_message);
                }

                #endregion

                quoteData.destCity = quoteData.destCity.Replace(" Twp", " Township");
                quoteData.origCity = quoteData.origCity.Replace(" Twp", " Township");

                LTL_Carriers             carriers = new LTL_Carriers(quoteData);
                SharedLTL.CarriersResult result   = carriers.GetRates();

                #region If any rates were found return rates, if not give error result

                if (result.totalQuotes == null || result.totalQuotes.Length == 0)
                {
                    #region If did not find any rates return error result

                    // If did not find any rates return error result

                    response_string = json_helper.Build_error_response("2",
                                                                       string.Concat("Could not find any rates. Please try again."));

                    var response_message = public_helper.Get_response_message(ref response_string, ref content_type);
                    sql = string.Concat("INSERT INTO Genera_Rating(QuoteID,Request_Data,Response_Data,TotalCube) VALUES(",
                                        "0,'", data, "','", response_string, "',", ltl_quote_request.totalCube,
                                        ")");
                    HelperFuncs.ExecuteNonQuery(AppCodeConstants.connStringAesAPI, ref sql, "Get_LTL_ratesController");

                    return(response_message);

                    #endregion
                }
                else
                {
                    string[] additionalServices = new string[0];

                    string requestId;

                    public_helper.StoreLTLRequestsSql(
                        ref quoteData, ref result.totalQuotes, result.totalQuotes[0].NewLogId, data, response_string,
                        out requestId);

                    response_string = json_helper.Build_response(ref result.totalQuotes);

                    var response_message = public_helper.Get_response_message(ref response_string, ref content_type);

                    #region INSERT INTO Genera_Rating

                    sql = string.Concat("INSERT INTO Genera_Rating(QuoteID,Request_Data,Response_Data,TotalCube) VALUES(",
                                        result.totalQuotes[0].NewLogId, ",'", data, "','", response_string, "',", ltl_quote_request.totalCube,
                                        ")");
                    HelperFuncs.ExecuteNonQuery(AppCodeConstants.connStringAesAPI, ref sql, "Get_LTL_ratesController");

                    #endregion

                    #region INSERT INTO ResponseTimes
                    // INSERT INTO ResponseTimes

                    int UPS_freight_milliseconds = 0, SMTL_milliseconds = 0, BestOvernite_milliseconds = 0, NewPenn_milliseconds = 0,
                        RL_milliseconds = 0, RRD_Truckload_milliseconds = 0;

                    foreach (GCMRateQuote quote in result.totalQuotes)
                    {
                        //DB.LogGenera("quote.DisplayName", "quote.DisplayName", quote.DisplayName);
                        if (quote.DisplayName == "UPS - Genera")
                        {
                            UPS_freight_milliseconds = quote.Elapsed_milliseconds;
                        }
                        else if (quote.DisplayName == "SMTL - Genera")
                        {
                            SMTL_milliseconds = quote.Elapsed_milliseconds;
                        }
                        else if (quote.DisplayName == "Best Overnite - Genera")
                        {
                            BestOvernite_milliseconds = quote.Elapsed_milliseconds;
                        }
                        else if (quote.DisplayName == "New Penn - Genera")
                        {
                            NewPenn_milliseconds = quote.Elapsed_milliseconds;
                        }
                        else if (quote.DisplayName == "R&L Carrier - Genera")
                        {
                            RL_milliseconds = quote.Elapsed_milliseconds;
                        }
                        else if (quote.DisplayName == "RRD Truckload")
                        {
                            RRD_Truckload_milliseconds = quote.Elapsed_milliseconds;
                        }
                    }

                    stopwatch.Stop();
                    int elapsedMilliseconds = (int)stopwatch.ElapsedMilliseconds;

                    //int seconds = elapsedMilliseconds / 1000;

                    //int gcm_time = 0;
                    sql = string.Concat("INSERT INTO Rate_response_times(",
                                        "QuoteID,RequestID,GCM_API,DLS,UPS_freight,SMTL,BestOvernite,NewPenn,RL,P44,Truckload) VALUES(",
                                        result.totalQuotes[0].NewLogId, ",'", requestId, "',", elapsedMilliseconds, ",", result.elapsed_milliseconds_DLS_Genera,
                                        ",", UPS_freight_milliseconds, ",", SMTL_milliseconds, ",", BestOvernite_milliseconds, ",", NewPenn_milliseconds,
                                        ",", RL_milliseconds, ",", result.elapsed_milliseconds_P44, ",", RRD_Truckload_milliseconds,
                                        ")");

                    HelperFuncs.ExecuteNonQuery(AppCodeConstants.conn_string_Genera, ref sql, "Get_LTL_ratesController");

                    #endregion

                    return(response_message);
                }

                #endregion
            }
            catch (Exception e)
            {
                #region Unknown error

                DB.LogGenera(iam, "get_ltl_rates_test", e.ToString());

                Json_helper json_helper = new Json_helper();

                string response_string = "";

                response_string = json_helper.Build_error_response("2",
                                                                   string.Concat("Could not find any rates. Please try again."));

                Models.Public.LTL.Helper public_helper = new Models.Public.LTL.Helper();
                string content_type = "application/json";
                return(public_helper.Get_response_message(ref response_string, ref content_type));

                #endregion
            }
        }
Example #2
0
        public RateServiceNotification Validate_rating_request()
        {
            RateServiceNotification res = new RateServiceNotification();

            DB.LogGenera(iam, "m_lPiece.Length", m_lPiece.Length.ToString());

            if (m_lPiece.Length == 0)
            {
                res.Code    = "307";
                res.Message = "Please give at least one line item";
                return(res);
            }

            bool first_line_has_class = false;

            for (byte i = 0; i < m_lPiece.Length; i++)
            {
                #region Check Class and dimensions

                if (m_lPiece[i].Weight > 0 && m_lPiece[i].FreightClass == "")
                {
                    //if (m_lPiece[i].Length < 1 || m_lPiece[i].Width < 1 || m_lPiece[i].Height < 1)
                    //{
                    //    //throw new RateServiceException("338", "Please enter dimensions for each item to get No NMFC No Freight Class Shipping LTL rates");
                    //    res.Code = "338";
                    //    res.Message = "Please enter dimensions for each item to get No NMFC No Freight Class Shipping LTL rates";
                    //    return res;
                    //}
                    //if (count == 0)
                    //{
                    //    hasFreightClass = false;
                    //}
                    //else if (m_lPiece[i].FreightClass == "" && hasFreightClass == true)
                    //{
                    //    // Not the first lineitem is "no class", but first was with a class
                    //    throw new RateServiceException("337", "Freight Class fields must be either filled in for each line item or all left blank");
                    //}
                    m_lPiece[i].FreightClass = "-1";
                }

                #endregion

                if (Is_valid_freight_class(m_lPiece[i].FreightClass))
                {
                    quoteData.hasFreightClass = true;
                    if (i == 0)
                    {
                        first_line_has_class = true;
                    }
                    else
                    {
                        if (first_line_has_class == false)
                        {
                            res.Code    = "337";
                            res.Message = "Freight Class fields must be either filled in for each line item or all left blank";
                            return(res);
                        }
                    }
                }
                else if (string.IsNullOrEmpty(m_lPiece[i].FreightClass)) // Empty Freight class
                {
                    if (i == 0)
                    {
                        first_line_has_class = false;
                    }
                    else
                    {
                        if (first_line_has_class == true)
                        {
                            res.Code    = "337";
                            res.Message = "Freight Class fields must be either filled in for each line item or all left blank";
                            return(res);
                        }
                    }
                }
                else // Freight class is not valid
                {
                    //res.Code = "337";
                    //res.Message = string.Concat("Freight Class ", m_lPiece[i].FreightClass, " is not valid");
                    //return res;
                }


                if (m_lPiece[i].Commodity.Equals("HHG"))
                {
                    m_lPiece[i].FreightClass = "150";
                }

                if (m_lPiece[i].Length > 0 && m_lPiece[i].Width > 0 || m_lPiece[i].Height > 0)
                {
                    quoteData.hasDimensions = true;
                }
                else
                {
                    // Do nothing
                }

                if (m_lPiece[i].Length > 94 || m_lPiece[i].Width > 94 || m_lPiece[i].Height > 94)
                {
                    //throw new RateServiceException("308", "Your item\'s dimension is 95\" or more. Please contact us at [email protected] or 877-890-2295 x 2 to get a quote.");
                    res.Code    = "308";
                    res.Message = "Your item\'s dimension is 95 inches or more. Please contact us at [email protected] or 877-890-2295 x 2 to get a quote.";
                    return(res);
                }
                else if (m_lPiece[i].Length > 50 || m_lPiece[i].Width > 48)
                {
                    if (quoteData.AccessorialsObj.LGPU == true || quoteData.AccessorialsObj.LGDEL == true)
                    {
                        //throw new RateServiceException("310", "Items over 50” in Length or 48” Width will not fit on a Liftgate Trailer, Email [email protected] for a rate quote or call 206-214-0341 ext 3");
                        res.Code    = "310";
                        res.Message = "Items over 50” in Length or 48” Width will not fit on a Liftgate Trailer, Email [email protected] for a rate quote or call 206-214-0341 ext 3";
                        return(res);
                    }
                }
                //else if (m_lPiece[i].Length > 0 || m_lPiece[i].Width > 0 || m_lPiece[i].Height > 0)
                //{
                //    hasDimensions = true;
                //}
                else
                {
                    // Do nothing
                }

                //
            }

            #region Ensure total weight as 500lb for HHG

            // Ensure total weight as 500lb for HHG
            int    firstIndexHHG = -1;
            double totalWeight   = 0;
            for (int i = 0; i < m_lPiece.Length; i++)
            {
                if (m_lPiece[i].Commodity.Equals("HHG") && firstIndexHHG == -1)
                {
                    firstIndexHHG = i;
                }
                totalWeight += m_lPiece[i].Weight;
            }
            if (firstIndexHHG > -1 && totalWeight < 500)
            {
                m_lPiece[firstIndexHHG].Weight += 500 - totalWeight;
            }
            // End

            #endregion

            #region Alaska

            if (quoteData.destState.Equals("AK") && AccessorialsObj.RESDEL.Equals("RSD"))
            {
                res.Code    = "391";
                res.Message =
                    string.Concat("Please contact AES customer service 877-890-2295 for this quote");
                return(res);
                // Alaska Residential Delivery
                //throw new RateServiceException("391", "Please contact AES customer service 877-890-2295 for this quote");
            }

            #endregion

            #region Hawaii

            bool isHawaiiRate = false;
            if (quoteData.origState == "HI" || quoteData.destState == "HI")
            {
                isHawaiiRate = true;
                foreach (LTLPiece lpiece in m_lPiece)
                {
                    if (lpiece.Length == 0 || lpiece.Length == 0 || lpiece.Width == 0 || lpiece.Width == 0 ||
                        lpiece.Height == 0 || lpiece.Height == 0)
                    {
                        res.Code    = "340";
                        res.Message =
                            string.Concat("Please enter dimensions for each item to get Hawaii rates");
                        return(res);
                        //throw new RateServiceException("340", "Please enter dimensions for each item to get Hawaii rates");
                    }
                }
            }

            #endregion

            #region Origin and Destination

            quoteData.origCity = quoteData.origCity.Replace("'", "").Replace("`", "");
            quoteData.destCity = quoteData.destCity.Replace("'", "").Replace("`", "");

            if (String.IsNullOrEmpty(quoteData.origZip))
            {
                //throw new RateServiceException("301", "Please give origin zip code");
                res.Code    = "301";
                res.Message =
                    string.Concat("Please give origin zip code");
                return(res);
            }
            if (String.IsNullOrEmpty(quoteData.origCity))
            {
                //throw new RateServiceException("302", "Please give origin city");
                res.Code    = "302";
                res.Message =
                    string.Concat("Please give origin city");
                return(res);
            }
            if (String.IsNullOrEmpty(quoteData.origState))
            {
                //throw new RateServiceException("303", "Please give origin state");
                res.Code    = "303";
                res.Message =
                    string.Concat("Please give origin state");
                return(res);
            }
            if (String.IsNullOrEmpty(quoteData.destZip))
            {
                //throw new RateServiceException("304", "Please give destination zip code");
                res.Code    = "304";
                res.Message =
                    string.Concat("Please give destination zip code");
                return(res);
            }
            if (String.IsNullOrEmpty(quoteData.destCity))
            {
                //throw new RateServiceException("305", "Please give destination city");
                res.Code    = "305";
                res.Message =
                    string.Concat("Please give destination city");
                return(res);
            }
            if (String.IsNullOrEmpty(quoteData.destState))
            {
                //throw new RateServiceException("306", "Please give destination state");
                res.Code    = "306";
                res.Message =
                    string.Concat("Please give destination state");
                return(res);
            }

            #endregion



            res.Code    = "0";
            res.Message = "Success";

            return(res);
        }