public List <AppCrmOrderHistoryResponse> AppOrderHistory(AppCrmOrderHistoryRequest orderHistoryRequest)
        {
            OrderOperation op = new OrderOperation();
            List <AppCrmOrderHistoryResponse> response = op.AppGetOrderHistory(orderHistoryRequest);

            return(response);
        }
        public List <AppCrmOrderHistoryResponse> AppGetOrderHistory(AppCrmOrderHistoryRequest orderHistoryRequest)
        {
            log.Info("AppGetOrderHistory:" + orderHistoryRequest.email);
            CrmUtil cu = new CrmUtil();
            OrganizationServiceProxy          organizationProxy = cu.getCrmService();
            List <AppCrmOrderHistoryResponse> response_s        = new List <AppCrmOrderHistoryResponse>();

            try
            {
                //Entity account = RetriveAccountByEmail(organizationProxy, orderHistoryRequest.email);

                QueryExpression qe = new QueryExpression();
                qe.EntityName = "salesorder";
                qe.ColumnSet  = new ColumnSet(true);


                ConditionExpression condition11 = new ConditionExpression();
                condition11.AttributeName = "emailaddress1";
                condition11.Operator      = ConditionOperator.Equal;
                condition11.Values.Add(orderHistoryRequest.email);

                FilterExpression filter11 = new FilterExpression();
                filter11.Conditions.Add(condition11);

                string beginDate = orderHistoryRequest.beginDate;
                string endDate   = orderHistoryRequest.endDate;
                if ((beginDate != null && !beginDate.Equals("")) && (endDate != null && !endDate.Equals("")))
                {
                    ConditionExpression condition12 = new ConditionExpression("createdon", ConditionOperator.Between, beginDate, endDate);

                    FilterExpression filter12 = new FilterExpression();
                    filter12.Conditions.Add(condition12);

                    qe.Criteria.AddFilter(filter12);
                }

                //ConditionExpression condition12 = new ConditionExpression();
                //condition12.AttributeName = "createdon";
                //condition12.Operator = ConditionOperator.Between;
                //condition12.Values.Add(new string[] { orderHistoryRequest.beginDate, orderHistoryRequest.endDate });



                qe.LinkEntities.Add(new LinkEntity("salesorder", "account", "customerid", "accountid", JoinOperator.Inner));
                qe.LinkEntities[0].Columns.AddColumns("accountid");
                qe.LinkEntities[0].EntityAlias = "customer";
                qe.LinkEntities[0].LinkCriteria.AddFilter(filter11);



                //  Query passed to service proxy.
                EntityCollection retrieved = organizationProxy.RetrieveMultiple(qe);
                foreach (Entity salseorder in retrieved.Entities)
                {
                    AppCrmOrderHistoryResponse res = new AppCrmOrderHistoryResponse();
                    res.orderName = salseorder.GetAttributeValue <string>("name");
                    if (salseorder.Contains("totalamount") && salseorder["totalamount"] != null)
                    {
                        decimal totalRRP = 0;
                        res.totalAmount = salseorder.GetAttributeValue <Money>("totalamount").Value.ToString("0.00");

                        res.createdOn = salseorder.GetAttributeValue <DateTime>("createdon").ToLocalTime().ToString("yyyyMMddHHmmss");

                        List <AppCrmOrderHistoryProduct> res_products = new List <AppCrmOrderHistoryProduct>();
                        Guid orderId = salseorder.GetAttributeValue <Guid>("salesorderid");

                        QueryExpression qe2 = new QueryExpression();
                        qe2.EntityName = "salesorderdetail";
                        qe2.ColumnSet  = new ColumnSet(true);
                        //qe2.ColumnSet.Columns.Add("quantity");
                        //qe2.ColumnSet.Columns.Add("priceperunit");
                        //qe2.ColumnSet.Columns.Add("baseamount");
                        //qe2.ColumnSet.Columns.Add("extendedamount");
                        //qe2.ColumnSet.Columns.Add("productid");

                        ConditionExpression condition2 = new ConditionExpression();
                        condition2.AttributeName = "salesorderid";
                        condition2.Operator      = ConditionOperator.Equal;
                        condition2.Values.Add(orderId);

                        FilterExpression filter2 = new FilterExpression();
                        filter2.Conditions.Add(condition2);

                        qe2.Criteria.AddFilter(filter2);


                        //  Query passed to service proxy.
                        EntityCollection retrieved2 = organizationProxy.RetrieveMultiple(qe2);
                        foreach (Entity entity in retrieved2.Entities)
                        {
                            bool isProduct = true;

                            //Product Status is Service Rate 10
                            //if (entity.GetAttributeValue<AliasedValue>("orderproduct.producttypecode") != null)
                            //{
                            //    OptionSetValue op = (OptionSetValue)(entity.GetAttributeValue<AliasedValue>("orderproduct.producttypecode").Value);

                            //    int ov = op.Value;
                            //    if (ov.Equals(10))
                            //    {
                            //        isProduct = false;
                            //    }
                            //}

                            if (isProduct)
                            {
                                AppCrmOrderHistoryProduct product = new AppCrmOrderHistoryProduct();

                                if (entity.Contains("productid") && entity["productid"] != null)
                                {
                                    EntityReference pro = (EntityReference)entity["productid"];
                                    product.ProductId  = pro.Id.ToString();
                                    product.ProductSku = pro.Name;

                                    Entity    product_entity = new Entity("product");
                                    ColumnSet attributes     = new ColumnSet(new string[] { "price", "new_rrp" });

                                    product_entity = organizationProxy.Retrieve(product_entity.LogicalName, pro.Id, attributes);
                                    if (product_entity.Contains("price") && product_entity["price"] != null)
                                    {
                                        product.PriceList = product_entity.GetAttributeValue <Money>("price").Value.ToString("#.##");
                                    }
                                    if (product_entity.Contains("new_rrp") && product_entity["new_rrp"] != null)
                                    {
                                        product.RRP = product_entity.GetAttributeValue <Money>("new_rrp").Value.ToString("#.##");
                                        totalRRP   += (product_entity.GetAttributeValue <Money>("new_rrp").Value) * (entity.GetAttributeValue <decimal>("quantity"));
                                    }
                                }
                                product.Quantity = entity.GetAttributeValue <decimal>("quantity").ToString("#");

                                res_products.Add(product);
                            }
                        }
                        res.totalRRP = totalRRP.ToString("0.00");
                        res.product  = res_products;
                    }
                    response_s.Add(res);
                }
                return(response_s);
            }
            catch (Exception ex)
            {
                log.Info(ex.Message + "--" + ex.ToString());
            }

            return(response_s);
        }