Wednesday 26 December 2012

order details

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.ServiceModel.Description;
using System.Net;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Xrm.Sdk.Query;
using System.Configuration;
//using Sulekha.Order.CrmSdk;
using System.Linq.Expressions;
using Xrm;
using System.Collections.ObjectModel;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI.WebControls;
//using System.Windows.Forms;



namespace Sulekha.Order
{
    public partial class Order_Details : System.Web.UI.Page
    {
        // Login logs = new Login();

        ClientCredentials credentials = new ClientCredentials();
       
           // credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            Uri OrganizationUri = new Uri("http://smdev-crm/SulekhaCRM/XRMServices/2011/Organization.svc");
            Uri HomeRealmUri = null;

        //Microsoft.Xrm.Sdk.IOrganizationService service = (Microsoft.Xrm.Sdk.IOrganizationService)serviceProxy;
        IOrganizationService service;
      
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Order_Approved();
                Order_Accepted();
                Order_Shipped();
            }

           
          
        }

        [WebMethod]
        protected void Order_Approved()
        {

            ClientCredentials credentials = new ClientCredentials();
            credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            Uri OrganizationUri = new Uri("http://smdev-crm/SulekhaCRM/XRMServices/2011/Organization.svc");
            Uri HomeRealmUri = null;
            using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, credentials, null))
            {
                Microsoft.Xrm.Sdk.IOrganizationService service = (Microsoft.Xrm.Sdk.IOrganizationService)serviceProxy;


                string vendor = Session["accountname"].ToString();

                ConditionExpression condition = new ConditionExpression();
                condition.AttributeName = "new_vendername";
                condition.Operator = ConditionOperator.Equal;
                condition.Values.Add(vendor);

                ConditionExpression condition1 = new ConditionExpression();
                condition1.AttributeName = "new_venderorderstatus";
                condition1.Operator = ConditionOperator.Equal;
                condition1.Values.Add("Order Approved");
              
                FilterExpression filter1 = new FilterExpression();
                filter1.FilterOperator = LogicalOperator.And;
                filter1.Conditions.Add(condition);
                filter1.Conditions.Add(condition1);
               // filter1.Conditions = new ConditionExpression[] { condition, condition1 };
              

                QueryExpression query = new QueryExpression("salesorder");

                query.ColumnSet.AddColumns("new_couponcode", "customerid", "name", "new_category", "new_actualpurchaseprice", "shipto_city","statuscode", "new_venderorderstatus","shipto_postalcode","salesorderid");

                query.Criteria.AddFilter(filter1);               

                EntityCollection result1 = serviceProxy.RetrieveMultiple(query);               

                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                dt.Columns.Add("Order No");
                dt.Columns.Add("Customer Name");
                dt.Columns.Add("Product Name");
                //dt.Columns.Add("Product Category");
                dt.Columns.Add("Product Price");
                dt.Columns.Add("Order Status");
                dt.Columns.Add("Shipped to City");
                dt.Columns.Add("SKU");
                dt.Columns.Add("Offer Price");
                dt.Columns.Add("Payable Price");
                dt.Columns.Add("Order Id");
                foreach (Entity entities in result1.Entities)
                {                                

                       
                        DataRow dr = dt.NewRow();
                        dr["Order No"] = entities.Attributes["new_couponcode"].ToString(); //so.OrderNumber;
                        EntityReference customer = (EntityReference)entities.Attributes["customerid"];
                        dr["Customer Name"] = customer.Name;
                        string productname = entities.Attributes["name"].ToString();
                        string alteredproductname = productname.Substring(12);
                        dr["Product Name"] = alteredproductname;                       
                        //dr["Product Category"] = entities.Attributes["new_category"].ToString();
                        dr["Product Price"] = entities.Attributes["new_actualpurchaseprice"].ToString();                 


                        dr["Order Status"] = entities.Attributes["new_venderorderstatus"].ToString();
                        string scity = entities.Attributes["shipto_city"].ToString();
                        string spin = entities.Attributes["shipto_postalcode"].ToString();
                        dr["Shipped to City"] = scity + "-" + spin;

                        //EntityReference orderguid = (EntityReference)entities.Attributes["salesorderid"];
                        Guid orderguid = (Guid)entities.Attributes["salesorderid"];
                        dr["Order Id"] = orderguid;

                        string fetchquery = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
 "<entity name='salesorderdetail'>" +
   "<attribute name='new_skuid' />" +
   "<attribute name='new_ip_offerprice' />" +
   "<attribute name='new_ip_payable' />" +
  
   "<attribute name='createdon' />" +  
   "<filter type='and'>" +
     "<condition attribute='salesorderid' operator='eq' uitype='salesorder' value='" + orderguid + "' />" +
   "</filter>" +
 "</entity>" +
"</fetch>";

                        FetchExpression fetch = new FetchExpression(fetchquery);
                        EntityCollection salesorderinfo = serviceProxy.RetrieveMultiple(fetch);

                        foreach (Entity sorder in salesorderinfo.Entities)
                        {

                            dr["SKU"] = sorder.Attributes["new_skuid"].ToString();
                            if (sorder.Attributes.Keys.Contains("new_ip_offerprice") == true)
                            {
                                dr["Offer Price"] = (Money)sorder.Attributes["new_ip_offerprice"];
                            }
                            else
                            {
                                dr["Offer Price"] = "0"; //new Money { Value = 0 };
                            }

                            if (sorder.Attributes.Keys.Contains("new_ip_payable") == true)
                            {
                                dr["Payable Price"] = (Money)sorder.Attributes["new_ip_payable"];
                            }
                            else
                            {
                                dr["Payable Price"] = "0"; //new Money { Value = 0 };
                            }

                           
                        }
                        

                        //ColumnSet cols = new ColumnSet(true);
                        //var orderdetailinfo = service.Retrieve("salesorderdetail", orderguid, cols);

                        //dr["SKU"] = orderdetailinfo.Attributes["new_skuid"];
                        //dr["Offer Price"] = orderdetailinfo.Attributes["new_ip_offerprice"];
                        //dr["Payable Price"] = orderdetailinfo.Attributes["new_ip_payable"];


                        dt.Rows.Add(dr);
                 

                }

                ds.Tables.Add(dt);

               
                orderapproved.DataSource = ds;
                orderapproved.DataBind();

                //orderapproved.HeaderRow.Cells[10].Visible = true;
                if (result1.Entities.Count > 0)
                {
                    orderapproved.HeaderRow.Cells[10].Visible = false;
                    foreach (GridViewRow gvr in orderapproved.Rows)
                    {
                        gvr.Cells[10].Visible = false;
                    }
                }
               

            }

        }

        [WebMethod]
        protected void Order_Accepted()
        {

            ClientCredentials credentials = new ClientCredentials();
            credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            Uri OrganizationUri = new Uri("http://smdev-crm/SulekhaCRM/XRMServices/2011/Organization.svc");
            Uri HomeRealmUri = null;
            using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, credentials, null))
            {
                Microsoft.Xrm.Sdk.IOrganizationService service = (Microsoft.Xrm.Sdk.IOrganizationService)serviceProxy;


                string vendor = Session["accountname"].ToString();

                ConditionExpression condition = new ConditionExpression();
                condition.AttributeName = "new_vendername";
                condition.Operator = ConditionOperator.Equal;
                condition.Values.Add(vendor);

                ConditionExpression condition1 = new ConditionExpression();
                condition1.AttributeName = "new_venderorderstatus";
                condition1.Operator = ConditionOperator.Equal;
                condition1.Values.Add("Accepted");

                FilterExpression filter1 = new FilterExpression();
                filter1.FilterOperator = LogicalOperator.And;
                filter1.Conditions.Add(condition);
                filter1.Conditions.Add(condition1);
                // filter1.Conditions = new ConditionExpression[] { condition, condition1 };


                QueryExpression query = new QueryExpression("salesorder");

                query.ColumnSet.AddColumns("new_couponcode", "customerid", "name", "new_category", "new_actualpurchaseprice", "shipto_city", "statuscode", "new_venderorderstatus", "shipto_postalcode","salesorderid");

                query.Criteria.AddFilter(filter1);

                EntityCollection result1 = serviceProxy.RetrieveMultiple(query);

                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                dt.Columns.Add("Order No");
                dt.Columns.Add("Customer Name");
                dt.Columns.Add("Product Name");
                //dt.Columns.Add("Product Category");
                dt.Columns.Add("Product Price");
                dt.Columns.Add("Order Status");
                dt.Columns.Add("Shipped to City");
                dt.Columns.Add("SKU");
                dt.Columns.Add("Offer Price");
                dt.Columns.Add("Payable Price");
                dt.Columns.Add("Order Id");

                foreach (Entity entities in result1.Entities)
                {


                    DataRow dr = dt.NewRow();
                    dr["Order No"] = entities.Attributes["new_couponcode"].ToString();

                   
                    EntityReference var1 = (EntityReference)entities.Attributes["customerid"];
                    dr["Customer Name"] = var1.Name;

                   
                    string productname = entities.Attributes["name"].ToString();
                    string alteredproductname = productname.Substring(12);
                    dr["Product Name"] = alteredproductname;
                   // dr["Product Category"] = entities.Attributes["new_category"].ToString();
                    dr["Product Price"] = entities.Attributes["new_actualpurchaseprice"].ToString();                 


                    dr["Order Status"] = entities.Attributes["new_venderorderstatus"].ToString();

                    string scity = entities.Attributes["shipto_city"].ToString();
                    string spin = entities.Attributes["shipto_postalcode"].ToString();


                    dr["Shipped to City"] = scity + "-" + spin;

                    Guid orderguid = (Guid)entities.Attributes["salesorderid"];

                    dr["Order Id"] = orderguid;

                    string fetchquery = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='salesorderdetail'>" +
"<attribute name='new_skuid' />" +
"<attribute name='new_ip_offerprice' />" +
"<attribute name='new_ip_payable' />" +

"<attribute name='createdon' />" +
"<filter type='and'>" +
 "<condition attribute='salesorderid' operator='eq' uitype='salesorder' value='" + orderguid + "' />" +
"</filter>" +
"</entity>" +
"</fetch>";

                    FetchExpression fetch = new FetchExpression(fetchquery);
                    EntityCollection salesorderinfo = serviceProxy.RetrieveMultiple(fetch);

                    foreach (Entity sorder in salesorderinfo.Entities)
                    {

                        dr["SKU"] = sorder.Attributes["new_skuid"].ToString();
                        if (sorder.Attributes.Keys.Contains("new_ip_offerprice") == true)
                        {
                            dr["Offer Price"] = (Money)sorder.Attributes["new_ip_offerprice"];
                        }
                        else
                        {
                            dr["Offer Price"] = "0"; //new Money { Value = 0 };
                        }

                        if (sorder.Attributes.Keys.Contains("new_ip_payable") == true)
                        {
                            dr["Payable Price"] = (Money)sorder.Attributes["new_ip_payable"];
                        }
                        else
                        {
                            dr["Payable Price"] = "0"; //new Money { Value = 0 };
                        }


                    }

                    dt.Rows.Add(dr);


                }

                ds.Tables.Add(dt);

                orderaccepted.DataSource = ds;
                orderaccepted.DataBind();

                if (result1.Entities.Count>0)
                {

                    orderaccepted.HeaderRow.Cells[10].Visible = false;
                    foreach (GridViewRow gvr in orderaccepted.Rows)
                    {
                        gvr.Cells[10].Visible = false;

                    }              


                }

            }

        }


        [WebMethod]
        protected void Order_Shipped()
        {

            ClientCredentials credentials = new ClientCredentials();
            credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            Uri OrganizationUri = new Uri("http://smdev-crm/SulekhaCRM/XRMServices/2011/Organization.svc");
            Uri HomeRealmUri = null;
            using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, credentials, null))
            {
                Microsoft.Xrm.Sdk.IOrganizationService service = (Microsoft.Xrm.Sdk.IOrganizationService)serviceProxy;


                string vendor = Session["accountname"].ToString();

                ConditionExpression condition = new ConditionExpression();
                condition.AttributeName = "new_vendername";
                condition.Operator = ConditionOperator.Equal;
                condition.Values.Add(vendor);

                ConditionExpression condition1 = new ConditionExpression();
                condition1.AttributeName = "new_venderorderstatus";
                condition1.Operator = ConditionOperator.Equal;
                condition1.Values.Add("Shipped");

                FilterExpression filter1 = new FilterExpression();
                filter1.FilterOperator = LogicalOperator.And;
                filter1.Conditions.Add(condition);
                filter1.Conditions.Add(condition1);
                // filter1.Conditions = new ConditionExpression[] { condition, condition1 };


                QueryExpression query = new QueryExpression("salesorder");

                query.ColumnSet.AddColumns("new_couponcode", "customerid", "name", "new_category", "new_actualpurchaseprice", "shipto_city", "statuscode", "new_venderorderstatus", "shipto_postalcode","salesorderid");

                query.Criteria.AddFilter(filter1);

                EntityCollection result1 = serviceProxy.RetrieveMultiple(query);

                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                dt.Columns.Add("Order No");
                dt.Columns.Add("Customer Name");
                dt.Columns.Add("Product Name");
                //dt.Columns.Add("Product Category");
                dt.Columns.Add("Product Price");
                dt.Columns.Add("Order Status");
                dt.Columns.Add("Shipped to City");
                dt.Columns.Add("SKU");
                dt.Columns.Add("Offer Price");
                dt.Columns.Add("Payable Price");
                dt.Columns.Add("Order Id");
                foreach (Entity entities in result1.Entities)
                {


                    DataRow dr = dt.NewRow();
                    dr["Order No"] = entities.Attributes["new_couponcode"].ToString(); //so.OrderNumber;
                    EntityReference var1 = (EntityReference)entities.Attributes["customerid"];
                    dr["Customer Name"] = var1.Name;

                    string productname = entities.Attributes["name"].ToString();
                    string alteredproductname = productname.Substring(12);
                    dr["Product Name"] = alteredproductname;                 
                   // dr["Product Category"] = entities.Attributes["new_category"].ToString();
                    dr["Product Price"] = entities.Attributes["new_actualpurchaseprice"].ToString();            


                    dr["Order Status"] = entities.Attributes["new_venderorderstatus"].ToString();
                    string scity = entities.Attributes["shipto_city"].ToString();
                    string spin = entities.Attributes["shipto_postalcode"].ToString();
                    dr["Shipped to City"] = scity + "-" + spin;

                    Guid orderguid = (Guid)entities.Attributes["salesorderid"];

                    string fetchquery = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='salesorderdetail'>" +
"<attribute name='new_skuid' />" +
"<attribute name='new_ip_offerprice' />" +
"<attribute name='new_ip_payable' />" +

"<attribute name='createdon' />" +
"<filter type='and'>" +
 "<condition attribute='salesorderid' operator='eq' uitype='salesorder' value='" + orderguid + "' />" +
"</filter>" +
"</entity>" +
"</fetch>";

                    FetchExpression fetch = new FetchExpression(fetchquery);
                    EntityCollection salesorderinfo = serviceProxy.RetrieveMultiple(fetch);

                    foreach (Entity sorder in salesorderinfo.Entities)
                    {

                        dr["SKU"] = sorder.Attributes["new_skuid"].ToString();
                        if (sorder.Attributes.Keys.Contains("new_ip_offerprice") == true)
                        {
                            dr["Offer Price"] = (Money)sorder.Attributes["new_ip_offerprice"];
                        }
                        else
                        {
                            dr["Offer Price"] = "0"; //new Money { Value = 0 };
                        }

                        if (sorder.Attributes.Keys.Contains("new_ip_payable") == true)
                        {
                            dr["Payable Price"] = (Money)sorder.Attributes["new_ip_payable"];
                        }
                        else
                        {
                            dr["Payable Price"] = "0"; //new Money { Value = 0 };
                        }


                    }

                    dt.Rows.Add(dr);


                }

                ds.Tables.Add(dt);

                ordershipped.DataSource = ds;
                ordershipped.DataBind();

                if (result1.Entities.Count > 0)
                {
                    ordershipped.HeaderRow.Cells[10].Visible = false;

                    foreach (GridViewRow gvr in ordershipped.Rows)
                    {
                        gvr.Cells[10].Visible = false;

                    }
                }
               
            }

        }

        protected void ordershipped_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            ordershipped.PageIndex = e.NewPageIndex;
            Order_Shipped();
            ordershipped.DataBind();
            ordershipped.HeaderRow.Cells[10].Visible = false;

            foreach (GridViewRow gvr in ordershipped.Rows)
            {
                gvr.Cells[10].Visible = false;

            }
           
         
        }
        protected void orderapproved_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            orderapproved.PageIndex = e.NewPageIndex;
            Order_Approved();           
            orderapproved.DataBind();
            orderapproved.HeaderRow.Cells[10].Visible = false;
            foreach (GridViewRow gvr in orderapproved.Rows)
            {
                gvr.Cells[10].Visible = false;
            }


        }
        protected void orderaccepted_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            orderaccepted.PageIndex = e.NewPageIndex;
            Order_Accepted();
            orderaccepted.DataBind();
            orderaccepted.HeaderRow.Cells[10].Visible = false;
            foreach (GridViewRow gvr in orderaccepted.Rows)
            {
                gvr.Cells[10].Visible = false;

            }    

        }

        protected void approvalsubmit_Click(object sender, EventArgs e)
        {      

           
            foreach (GridViewRow gvrow in orderapproved.Rows)
            {
                //CheckBox chkChild = (CheckBox)gvrow.FindControl("chkChild");
                CheckBox chkChild = (CheckBox)gvrow.FindControl("chkChild");


                if (chkChild.Checked == true)
                {
                    string skucode = gvrow.Cells[10].Text;
                    Guid chid = new Guid();//Guid.Empty();
                    chid = new Guid(skucode);

                    using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, credentials, null))
                    {
                        Microsoft.Xrm.Sdk.IOrganizationService service = (Microsoft.Xrm.Sdk.IOrganizationService)serviceProxy;
                        ColumnSet colu = new ColumnSet("new_venderorderstatus");
                        var orderdetails = service.Retrieve("salesorder", chid, colu);
                        orderdetails["new_venderorderstatus"] = "Accepted";
                        service.Update(orderdetails);

                    }
                }

            }

           
            }

        protected void approvalnostock_Click(object sender, EventArgs e)
        {

            foreach (GridViewRow gvrow in orderapproved.Rows)
            {
                //CheckBox chkChild = (CheckBox)gvrow.FindControl("chkChild");
                CheckBox chkChild = (CheckBox)gvrow.FindControl("chkChild");


                if (chkChild.Checked == true)
                {
                    string ordercode = gvrow.Cells[10].Text;
                    Guid chid = new Guid();//Guid.Empty();
                    chid = new Guid(ordercode);

                    string skucode = gvrow.Cells[7].Text;

                    using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, credentials, null))
                    {
                        Microsoft.Xrm.Sdk.IOrganizationService service = (Microsoft.Xrm.Sdk.IOrganizationService)serviceProxy;
                        ColumnSet colu = new ColumnSet("new_venderorderstatus");
                        var orderdetails = service.Retrieve("salesorder", chid, colu);
                        orderdetails["new_venderorderstatus"] = "No Stock";
                        service.Update(orderdetails);

                        ConditionExpression condition = new ConditionExpression();
                        condition.AttributeName = "new_vendorsku";
                        condition.Operator = ConditionOperator.Equal;
                        condition.Values.Add(skucode);                       

                        FilterExpression filter1 = new FilterExpression();
                        //filter1.FilterOperator = LogicalOperator.And;
                        filter1.Conditions.Add(condition);
                       // filter1.Conditions.Add(condition1);

                        QueryExpression query = new QueryExpression("new_deal");
                        query.ColumnSet.AddColumns("new_dealstatus", "new_dealid", "new_vendorsku");
                        query.Criteria.AddFilter(filter1);


                        EntityCollection queryresult = serviceProxy.RetrieveMultiple(query);

                        Entity deal = new Entity();
                        deal.LogicalName = "new_deal";

                        foreach (var c in queryresult.Entities)
                        {

                            deal["new_dealstatus"] = "100000012";
                            deal.Id = c.Id;
                            deal.LogicalName = c.LogicalName;
                            service.Update(deal);

                        }


                    }
                }

            }

        }

        protected void shippingdeliverd_Click(object sender, EventArgs e)
        {


        }

        protected void noshipping_Click(object sender, EventArgs e)
        {

        }

        protected void acceptshipping_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach (GridViewRow gvrow in orderaccepted.Rows)
            {
                CheckBox chkChild = (CheckBox)gvrow.FindControl("chkChild1");

                if (chkChild.Checked)
                {
                    string skucode = gvrow.Cells[1].Text;
                    string ordId = gvrow.Cells[10].Text;
                    sb.Append(skucode + "," + ordId + "#");
                }
            }

            string sbstring = sb.ToString().Substring(0, sb.ToString().Length - 1);
            Session["stringbuild"] = sbstring;
            Response.Redirect("acceptpopup.aspx");
        }

        protected void acceptnostock_Click(object sender, EventArgs e)
        {

        }

     
       
    }
}

No comments:

Post a Comment