/// <summary>
        /// product Inquiry
        /// </summary>
        /// <param name="productCode"></param>
        /// <param name="companyName"></param>
        /// <param name="paging"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public List<Product> ProductInquiry(string productCode, string description, DataGridPagingInformation paging, out TransactionalInformation transaction)
        {
            transaction = new TransactionalInformation();

            List<Product> products = new List<Product>();

            try
            {             
                productsDataService.CreateSession();
                products = productsDataService.ProductInquiry(productCode, description, paging, out transaction);                                            
            }
            catch (Exception ex)
            {
                transaction.ReturnMessage = new List<string>();
                string errorMessage = ex.Message;
                transaction.ReturnStatus = false;
                transaction.ReturnMessage.Add(errorMessage);
            }
            finally
            {
                productsDataService.CloseSession();
            }

            return products;

        }
        /// <summary>
        /// customer Inquiry
        /// </summary>
        /// <param name="customerCode"></param>
        /// <param name="companyName"></param>
        /// <param name="paging"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public List<Customer> CustomerInquiry(string customerCode, string companyName, DataGridPagingInformation paging, out TransactionalInformation transaction)
        {
            transaction = new TransactionalInformation();

            List<Customer> customers = new List<Customer>();

            try
            {             
                customersDataService.CreateSession();
                customers = customersDataService.CustomerInquiry(customerCode, companyName, paging, out transaction);                                            
            }
            catch (Exception ex)
            {
                transaction.ReturnMessage = new List<string>();
                string errorMessage = ex.Message;
                transaction.ReturnStatus = false;
                transaction.ReturnMessage.Add(errorMessage);
            }
            finally
            {
                customersDataService.CloseSession();
            }

            return customers;

        }
        /// <summary>
        /// Product Inquiry
        /// </summary>
        /// <param name="productCode"></param>
        /// <param name="description"></param>
        /// <param name="paging"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public List<Product> ProductInquiry(string productCode, string description, DataGridPagingInformation paging, out TransactionalInformation transaction)
        {

            transaction = new TransactionalInformation();

            string sortExpression = paging.SortExpression;

            if (paging.SortDirection != string.Empty)
                sortExpression = sortExpression + " " + paging.SortDirection;

            List<Product> productInquiry = new List<Product>();

            int numberOfRows = 0;

            var productQuery = dbConnection.Products.AsQueryable();

            if (productCode != null && productCode.Trim().Length > 0)
            {
                productQuery = productQuery.Where(c => c.ProductCode.StartsWith(productCode));
            }

            if (description != null && description.Trim().Length > 0)
            {
                productQuery = productQuery.Where(c => c.Description.StartsWith(description));
            }

            var products = from p in productQuery
                            select new { p.ProductID, p.ProductCode, p.Description, p.UnitPrice, p.UnitOfMeasure };

            numberOfRows = products.Count();

            products = products.OrderBy(sortExpression);

            var productList = products.Skip((paging.CurrentPageNumber - 1) * paging.PageSize).Take(paging.PageSize);

            paging.TotalRows = numberOfRows;
            paging.TotalPages = AngularJSUtilities.Utilities.CalculateTotalPages(numberOfRows, paging.PageSize);

            foreach (var product in productList)
            {
                Product productData = new Product();
                productData.ProductID = product.ProductID;
                productData.ProductCode = product.ProductCode;
                productData.Description = product.Description;
                productData.UnitOfMeasure = product.UnitOfMeasure;
                productData.UnitPrice = product.UnitPrice;              

                productInquiry.Add(productData);
            }


            transaction.TotalPages = paging.TotalPages;
            transaction.TotalRows = paging.TotalRows;
            transaction.ReturnStatus = true;
            transaction.ReturnMessage.Add(numberOfRows.ToString() + " product records found.");

            return productInquiry;

        }
        public HttpResponseMessage CustomerInquiry([FromUri] CustomerInquiryDTO customerInquiryDTO)
        {

            if (customerInquiryDTO.CustomerCode == null) customerInquiryDTO.CustomerCode = string.Empty;
            if (customerInquiryDTO.CompanyName == null) customerInquiryDTO.CompanyName = string.Empty;
            if (customerInquiryDTO.SortDirection == null) customerInquiryDTO.SortDirection = string.Empty;
            if (customerInquiryDTO.SortExpression == null) customerInquiryDTO.SortExpression = string.Empty;

            CustomersApiModel customersWebApiModel = new CustomersApiModel();
            TransactionalInformation transaction = new TransactionalInformation();
            CustomersBusinessService customersBusinessService;

            customersWebApiModel = new CustomersApiModel();

            DataGridPagingInformation paging = new DataGridPagingInformation();
            paging.CurrentPageNumber = customerInquiryDTO.CurrentPageNumber;
            paging.PageSize = customerInquiryDTO.PageSize;
            paging.SortExpression = customerInquiryDTO.SortExpression;
            paging.SortDirection = customerInquiryDTO.SortDirection;

            if (paging.SortDirection == "") paging.SortDirection = "ASC";
            if (paging.SortExpression == "") paging.SortExpression = "CompanyName";

            customersBusinessService = new CustomersBusinessService(customersDataService);

            List<Customer> customers = customersBusinessService.CustomerInquiry(customerInquiryDTO.CustomerCode, customerInquiryDTO.CompanyName, paging, out transaction);

            customersWebApiModel.Customers = customers;
            customersWebApiModel.IsAuthenicated = true;
            customersWebApiModel.ReturnStatus = transaction.ReturnStatus;
            customersWebApiModel.ReturnMessage = transaction.ReturnMessage;
            customersWebApiModel.TotalPages = transaction.TotalPages;
            customersWebApiModel.TotalRows = transaction.TotalRows;
            customersWebApiModel.PageSize = paging.PageSize;
            customersWebApiModel.IsAuthenicated = true;

            if (transaction.ReturnStatus == true)
            {
                var response = Request.CreateResponse<CustomersApiModel>(HttpStatusCode.OK, customersWebApiModel);
                return response;
            }

            var badResponse = Request.CreateResponse<CustomersApiModel>(HttpStatusCode.BadRequest, customersWebApiModel);
            return badResponse;


        }
        /// <summary>
        /// Order Inquiry
        /// </summary>
        /// <param name="customerCode"></param>
        /// <param name="companyName"></param>
        /// <param name="paging"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public List<OrderInquiry> OrderInquiry(string customerCode, string companyName, DataGridPagingInformation paging, out TransactionalInformation transaction)
        {

            transaction = new TransactionalInformation();

            string sortExpression = paging.SortExpression;

            if (paging.SortDirection != string.Empty)
                sortExpression = sortExpression + " " + paging.SortDirection;

            List<OrderInquiry> orderInquiryList = new List<OrderInquiry>();

            int numberOfRows = 0;

            var customerQuery = dbConnection.Customers.AsQueryable();

            if (customerCode != null && customerCode.Trim().Length > 0)
            {
                customerQuery = customerQuery.Where(c => c.CustomerCode.StartsWith(customerCode));
            }

            if (companyName != null && companyName.Trim().Length > 0)
            {
                customerQuery = customerQuery.Where(c => c.CompanyName.StartsWith(companyName));
            }

            var orders = from customer in customerQuery
                            join order in dbConnection.Orders on customer.CustomerID equals order.CustomerID
                            select new { customer, order, order.OrderDate, order.OrderTotal, customer.CustomerCode, customer.CompanyName };                            

            numberOfRows = orders.Count();

            orders = orders.OrderBy(sortExpression);

            var listOfOrders = orders.Skip((paging.CurrentPageNumber - 1) * paging.PageSize).Take(paging.PageSize);

            paging.TotalRows = numberOfRows;
            paging.TotalPages = AngularJSUtilities.Utilities.CalculateTotalPages(numberOfRows, paging.PageSize);

            foreach (var order in listOfOrders)
            {
                OrderInquiry o = new OrderInquiry();
                o.Customer = order.customer;
                o.Order = order.order;
                orderInquiryList.Add(o);
            }


            transaction.TotalPages = paging.TotalPages;
            transaction.TotalRows = paging.TotalRows;
            transaction.ReturnStatus = true;
            transaction.ReturnMessage.Add(numberOfRows.ToString() + " orders found.");

            return orderInquiryList;

        }
        public HttpResponseMessage OrderInquiry([FromBody] OrderInquiryDTO orderInquiryDTO)
        {

            if (orderInquiryDTO.CustomerCode == null) orderInquiryDTO.CustomerCode = string.Empty;
            if (orderInquiryDTO.CompanyName == null) orderInquiryDTO.CompanyName = string.Empty;
            if (orderInquiryDTO.SortDirection == null) orderInquiryDTO.SortDirection = string.Empty;
            if (orderInquiryDTO.SortExpression == null) orderInquiryDTO.SortExpression = string.Empty;

            OrdersApiModel ordersWebApiModel = new OrdersApiModel();
            TransactionalInformation transaction = new TransactionalInformation();
            OrdersBusinessService ordersBusinessService;

            ordersWebApiModel.IsAuthenicated = true;

            DataGridPagingInformation paging = new DataGridPagingInformation();
            paging.CurrentPageNumber = orderInquiryDTO.CurrentPageNumber;
            paging.PageSize = orderInquiryDTO.PageSize;
            paging.SortExpression = orderInquiryDTO.SortExpression;
            paging.SortDirection = orderInquiryDTO.SortDirection;

            if (paging.SortDirection == "") paging.SortDirection = "DESC";
            if (paging.SortExpression == "") paging.SortExpression = "OrderDate";

            ordersBusinessService = new OrdersBusinessService(ordersDataService);

            List<OrderInquiry> orders = ordersBusinessService.OrderInquiry(orderInquiryDTO.CustomerCode, orderInquiryDTO.CompanyName, paging, out transaction);

            ordersWebApiModel.Orders = orders;           
            ordersWebApiModel.ReturnStatus = transaction.ReturnStatus;
            ordersWebApiModel.ReturnMessage = transaction.ReturnMessage;
            ordersWebApiModel.TotalPages = transaction.TotalPages;
            ordersWebApiModel.TotalRows = transaction.TotalRows;
            ordersWebApiModel.PageSize = paging.PageSize;

            if (transaction.ReturnStatus == true)
            {
                var response = Request.CreateResponse<OrdersApiModel>(HttpStatusCode.OK, ordersWebApiModel);
                return response;
            }

            var badResponse = Request.CreateResponse<OrdersApiModel>(HttpStatusCode.BadRequest, ordersWebApiModel);
            return badResponse;


        }
        /// <summary>
        /// Customer Inquiry
        /// </summary>
        /// <param name="customerCode"></param>
        /// <param name="companyName"></param>
        /// <param name="paging"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public List<Customer> CustomerInquiry(string customerCode, string companyName, DataGridPagingInformation paging, out TransactionalInformation transaction)
        {

            transaction = new TransactionalInformation();

            string sortExpression = paging.SortExpression;

            if (paging.SortDirection != string.Empty)
                sortExpression = sortExpression + " " + paging.SortDirection;

            List<Customer> customerInquiry = new List<Customer>();

            int numberOfRows = 0;

            var customerQuery = dbConnection.Customers.AsQueryable();

            if (customerCode != null && customerCode.Trim().Length > 0)
            {
                customerQuery = customerQuery.Where(c => c.CustomerCode.StartsWith(customerCode));
            }

            if (companyName != null && companyName.Trim().Length > 0)
            {
                customerQuery = customerQuery.Where(c => c.CompanyName.StartsWith(companyName));
            }

            var customers = from c in customerQuery
                            select new { c.CustomerID, c.CustomerCode, c.CompanyName, c.WebSiteUrl, c.City, c.Country, c.Address, c.Region, c.PostalCode };

            numberOfRows = customers.Count();

            customers = customers.OrderBy(sortExpression);

            var customerList = customers.Skip((paging.CurrentPageNumber - 1) * paging.PageSize).Take(paging.PageSize);

            paging.TotalRows = numberOfRows;
            paging.TotalPages = AngularJSUtilities.Utilities.CalculateTotalPages(numberOfRows, paging.PageSize);

            foreach (var customer in customerList)
            {
                Customer customerData = new Customer();
                customerData.CustomerID = customer.CustomerID;
                customerData.CustomerCode = customer.CustomerCode;
                customerData.CompanyName = customer.CompanyName;
                customerData.City = customer.City;
                customerData.Address = customer.Address;
                customerData.WebSiteUrl = customer.WebSiteUrl;

                if (customer.Region == "NULL")
                    customerData.Region = "NA";
                else 
                    customerData.Region = customer.Region;

                customerData.PostalCode = customer.PostalCode;

                customerInquiry.Add(customerData);
            }


            transaction.TotalPages = paging.TotalPages;
            transaction.TotalRows = paging.TotalRows;
            transaction.ReturnStatus = true;
            transaction.ReturnMessage.Add(numberOfRows.ToString() + " customer records found.");

            return customerInquiry;

        }
        public HttpResponseMessage ProductInquiry([FromUri] ProductInquiryDTO productInquiryDTO)
        {

            if (productInquiryDTO.ProductCode == null) productInquiryDTO.ProductCode = string.Empty;
            if (productInquiryDTO.Description == null) productInquiryDTO.Description = string.Empty;
            if (productInquiryDTO.SortDirection == null) productInquiryDTO.SortDirection = string.Empty;
            if (productInquiryDTO.SortExpression == null) productInquiryDTO.SortExpression = string.Empty;

            ProductsApiModel productsWebApiModel = new ProductsApiModel();
            TransactionalInformation transaction = new TransactionalInformation();
            ProductsBusinessService productsBusinessService;

            productsWebApiModel.IsAuthenicated = true;

            DataGridPagingInformation paging = new DataGridPagingInformation();
            paging.CurrentPageNumber = productInquiryDTO.CurrentPageNumber;
            paging.PageSize = productInquiryDTO.PageSize;
            paging.SortExpression = productInquiryDTO.SortExpression;
            paging.SortDirection = productInquiryDTO.SortDirection;

            if (paging.SortDirection == "") paging.SortDirection = "ASC";
            if (paging.SortExpression == "") paging.SortExpression = "Description";

            productsBusinessService = new ProductsBusinessService(productsDataService);

            List<Product> products = productsBusinessService.ProductInquiry(productInquiryDTO.ProductCode, productInquiryDTO.Description, paging, out transaction);

            productsWebApiModel.Products = products;       
            productsWebApiModel.ReturnStatus = transaction.ReturnStatus;
            productsWebApiModel.ReturnMessage = transaction.ReturnMessage;
            productsWebApiModel.TotalPages = transaction.TotalPages;
            productsWebApiModel.TotalRows = transaction.TotalRows;
            productsWebApiModel.PageSize = paging.PageSize;

            if (transaction.ReturnStatus == true)
            {
                var response = Request.CreateResponse<ProductsApiModel>(HttpStatusCode.OK, productsWebApiModel);
                return response;
            }

            var badResponse = Request.CreateResponse<ProductsApiModel>(HttpStatusCode.BadRequest, productsWebApiModel);
            return badResponse;


        }