Exemple #1
0
        public void Execute(IServiceProvider serviceProvider)
        {
            context = serviceProvider.GetService(typeof(IPluginExecutionContext)) as IPluginExecutionContext;
            factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            service = factory.CreateOrganizationService(context.UserId);
            try
            {
                //vinhlh 26-12-2017
                bool connect_ax = getConfigdefaultconnectax();
                if (connect_ax == false)
                {
                    throw new Exception("Thiếu khai báo connect ax");
                }
                #region KHai báo service AX
                NetTcpBinding binding = new NetTcpBinding();
                //net.tcp://AOS_SERVICE_HOST/DynamicsAx/Services/AXConectorAIF
                binding.Name = "NetTcpBinding_BHS_BSD_CRMSERVICEAXService";
                EndpointAddress endpoint = new EndpointAddress(new Uri("net.tcp://" + Utilites._port + "/DynamicsAx/Services/BHS_BSD_CRMSERVICEAXServiceGroup"));
                ServiceReferenceAIF.BHS_BSD_CRMSERVICEAXServiceClient client = new ServiceReferenceAIF.BHS_BSD_CRMSERVICEAXServiceClient(binding, endpoint);
                client.ClientCredentials.Windows.ClientCredential.Domain   = Utilites._domain;
                client.ClientCredentials.Windows.ClientCredential.UserName = Utilites._userName;
                client.ClientCredentials.Windows.ClientCredential.Password = Utilites._passWord;
                ServiceReferenceAIF.CallContext contextService = new ServiceReferenceAIF.CallContext()
                {
                    Company = Utilites._company
                };
                #endregion
                #region vinhlh 21-11-2017
                if (context.MessageName == "bsd_Action_CloseSubOrder")
                {
                    #region bsd_Action_CloseSubOrder
                    EntityReference rf;
                    EntityReference target   = (EntityReference)context.InputParameters["Target"];
                    Entity          suborder = service.Retrieve(target.LogicalName, target.Id, new ColumnSet(true));
                    if (suborder.HasValue("bsd_type"))
                    {
                        if (((OptionSetValue)suborder["bsd_type"]).Value == 861450001) //861450001 Quote
                        {
                            #region Cập nhật lại số lượng còn lại Quote bsd_quote
                            if (suborder.HasValue("bsd_quote"))
                            {
                                rf = (EntityReference)suborder["bsd_quote"];
                                EntityCollection lst_suborderProduct = getsubOrderProduct(suborder.Id);

                                if (lst_suborderProduct.Entities.Any())
                                {
                                    this.SetState(rf.Id, rf.LogicalName, 0, 1);
                                    foreach (var suborderProduct in lst_suborderProduct.Entities)
                                    {
                                        EntityReference rf_product = (EntityReference)suborderProduct["bsd_product"];
                                        Entity          qouteProduct = getQuoteProduct(rf.Id, rf_product.Id);
                                        Entity          qouteProduct_Update = new Entity(qouteProduct.LogicalName, qouteProduct.Id);
                                        decimal         bsd_remainingquantity = 0m; decimal bsd_shipquantity = 0m; decimal bsd_shippedquantity = 0m; decimal bsd_suborderquantity = 0m;
                                        if (qouteProduct.HasValue("bsd_remainingquantity"))
                                        {
                                            bsd_remainingquantity = (decimal)qouteProduct["bsd_remainingquantity"];
                                        }
                                        if (qouteProduct.HasValue("bsd_suborderquantity"))
                                        {
                                            bsd_suborderquantity = (decimal)qouteProduct["bsd_suborderquantity"];
                                        }
                                        if (suborderProduct.HasValue("bsd_shipquantity"))
                                        {
                                            bsd_shipquantity = (decimal)suborderProduct["bsd_shipquantity"];
                                        }
                                        if (suborderProduct.HasValue("bsd_shippedquantity"))
                                        {
                                            bsd_shippedquantity = (decimal)suborderProduct["bsd_shippedquantity"];
                                        }
                                        // throw new Exception((bsd_remainingquantity + (bsd_shipquantity - bsd_shippedquantity)).ToString());
                                        qouteProduct_Update["bsd_remainingquantity"] = bsd_remainingquantity + (bsd_shipquantity - bsd_shippedquantity);
                                        qouteProduct_Update["bsd_suborderquantity"]  = bsd_suborderquantity - (bsd_shipquantity - bsd_shippedquantity);
                                        // qouteProduct_Update["bsd_remainingquantity"] = 0m;
                                        service.Update(qouteProduct_Update);
                                    }
                                    #region Won Quote
                                    this.SetState(rf.Id, rf.LogicalName, 1, 2);
                                    WinQuoteRequest winQuoteRequest = new WinQuoteRequest();
                                    Entity          quoteClose      = new Entity("quoteclose");
                                    quoteClose.Attributes["quoteid"] = new EntityReference("quote", rf.Id);
                                    quoteClose.Attributes["subject"] = "Quote Close" + DateTime.Now.ToString();
                                    winQuoteRequest.QuoteClose       = quoteClose;
                                    winQuoteRequest.Status           = new OptionSetValue(-1);
                                    service.Execute(winQuoteRequest);
                                    #endregion
                                }
                            }
                            #endregion
                        }
                        else if (((OptionSetValue)suborder["bsd_type"]).Value == 861450002)//861450002 Order
                        {
                            #region Cập nhật lại số lượng còn lại Sale Contract
                            if (suborder.HasValue("bsd_order"))
                            {
                                rf = (EntityReference)suborder["bsd_order"];
                                EntityCollection lst_suborderProduct = getsubOrderProduct(suborder.Id);
                                if (lst_suborderProduct.Entities.Any())
                                {
                                    foreach (var suborderProduct in lst_suborderProduct.Entities)
                                    {
                                        EntityReference rf_product = (EntityReference)suborderProduct["bsd_product"];

                                        Entity  orderProduct = getOrderProduct(rf.Id, rf_product.Id);
                                        Entity  orderProduct_Update = new Entity(orderProduct.LogicalName, orderProduct.Id);
                                        decimal bsd_remainingquantity = 0m; decimal bsd_shipquantity = 0m; decimal bsd_shippedquantity = 0m; decimal bsd_suborderquantity = 0m;
                                        if (orderProduct.HasValue("bsd_remainingquantity"))
                                        {
                                            bsd_remainingquantity = (decimal)orderProduct["bsd_remainingquantity"];
                                        }
                                        if (orderProduct.HasValue("bsd_suborderquantity"))
                                        {
                                            bsd_suborderquantity = (decimal)orderProduct["bsd_suborderquantity"];
                                        }
                                        if (suborderProduct.HasValue("bsd_shipquantity"))
                                        {
                                            bsd_shipquantity = (decimal)suborderProduct["bsd_shipquantity"];
                                        }
                                        if (suborderProduct.HasValue("bsd_shippedquantity"))
                                        {
                                            bsd_shippedquantity = (decimal)suborderProduct["bsd_shippedquantity"];
                                        }
                                        orderProduct_Update["bsd_remainingquantity"] = bsd_remainingquantity + (bsd_shipquantity - bsd_shippedquantity);
                                        orderProduct_Update["bsd_suborderquantity"]  = bsd_suborderquantity - (bsd_shipquantity - bsd_shippedquantity);
                                        // orderProduct_Update["bsd_remainingquantity"] = 0m;
                                        service.Update(orderProduct_Update);
                                    }
                                }
                            }
                            #endregion
                        }
                    }
                    #region Cập nhật trạng thái suborder thành  Closed
                    Entity suborder_Update = new Entity(suborder.LogicalName, suborder.Id);
                    suborder_Update["statuscode"] = new OptionSetValue(861450008);
                    service.Update(suborder_Update);
                    //throw new Exception("okie");
                    #endregion
                    #region 27-12-2017 call service AX
                    if (suborder.Contains("bsd_suborderax") && suborder["bsd_suborderax"] != null)
                    {
                        string s_Result = client.BHS_CancelSalesOrder(contextService, suborder["bsd_suborderax"].ToString().Trim());
                        if (s_Result != "Success")
                        {
                            throw new Exception("AX " + s_Result);
                        }
                    }
                    #endregion
                    #endregion
                }
                else if (context.MessageName == "bsd_Action_CloseDeliverySchedule")
                {
                    #region bsd_Action_CloseDeliverySchedule
                    EntityReference  target              = (EntityReference)context.InputParameters["Target"];
                    Entity           deliverySchedule    = service.Retrieve(target.LogicalName, target.Id, new ColumnSet(true));
                    EntityCollection lst_requestDelivery = getRequestDelivery(deliverySchedule.Id);
                    if (lst_requestDelivery.Entities.Any())
                    {
                        foreach (var requestDelivery in lst_requestDelivery.Entities)
                        {
                            EntityCollection lst_deliveryNote = getDeliveryNote(requestDelivery.Id);
                            if (lst_deliveryNote.Entities.Any())
                            {
                                foreach (var deliveryNote in lst_deliveryNote.Entities)
                                {
                                    #region Cập nhật lại Delivery Note trạng thái thành fullfil
                                    Entity deliveryNote_Update = new Entity(deliveryNote.LogicalName, deliveryNote.Id);
                                    deliveryNote_Update["bsd_status"] = new OptionSetValue(861450002);
                                    service.Update(deliveryNote_Update);
                                    #endregion
                                }
                            }
                        }
                    }
                    #region Cập nhật lại delivery Schedule trạng thái thành fullfil
                    Entity deliverySchedule_Update = new Entity(deliverySchedule.LogicalName, deliverySchedule.Id);
                    deliverySchedule_Update["bsd_status"] = new OptionSetValue(861450003);
                    service.Update(deliverySchedule_Update);
                    #endregion
                    #endregion
                }
                #endregion
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Exemple #2
0
        public void Execute(IServiceProvider serviceProvider)
        {
            context = serviceProvider.GetService(typeof(IPluginExecutionContext)) as IPluginExecutionContext;
            factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            service = factory.CreateOrganizationService(context.UserId);
            try
            {
                bool connect_ax = getConfigdefaultconnectax();
                if (connect_ax == false)
                {
                    throw new Exception("Thiếu khai báo connect ax");
                }

                //vinhlh 26-12-2017
                #region KHai báo service AX
                NetTcpBinding binding = new NetTcpBinding();
                //net.tcp://AOS_SERVICE_HOST/DynamicsAx/Services/AXConectorAIF
                binding.Name = "NetTcpBinding_BHS_BSD_CRMSERVICEAXService";
                EndpointAddress endpoint = new EndpointAddress(new Uri("net.tcp://" + Utilites._port + "/DynamicsAx/Services/BHS_BSD_CRMSERVICEAXServiceGroup"));
                ServiceReferenceAIF.BHS_BSD_CRMSERVICEAXServiceClient client = new ServiceReferenceAIF.BHS_BSD_CRMSERVICEAXServiceClient(binding, endpoint);
                client.ClientCredentials.Windows.ClientCredential.Domain   = Utilites._domain;
                client.ClientCredentials.Windows.ClientCredential.UserName = Utilites._userName;
                client.ClientCredentials.Windows.ClientCredential.Password = Utilites._passWord;
                ServiceReferenceAIF.CallContext contextService = new ServiceReferenceAIF.CallContext()
                {
                    Company = Utilites._company
                };
                #endregion

                #region bsd_Action_CloseSubOrder
                EntityReference rf;
                EntityReference target   = (EntityReference)context.InputParameters["Target"];
                Entity          suborder = service.Retrieve(target.LogicalName, target.Id, new ColumnSet(true));



                SetStateRequest setStateRequest = new SetStateRequest()
                {
                    EntityMoniker = new EntityReference
                    {
                        Id          = suborder.Id,
                        LogicalName = suborder.LogicalName
                    },
                    State = new OptionSetValue(1),
                    //B2C
                    //Status = new OptionSetValue(861450009)
                    //B2B
                    Status = new OptionSetValue(861450007)
                };
                service.Execute(setStateRequest);
                //Đơn hàng là đơn hàng trả
                if (((OptionSetValue)suborder["bsd_type"]).Value == 861450004 && suborder.HasValue("bsd_returnorder"))
                {
                    Entity returnorder = service.Retrieve("bsd_returnorder", ((EntityReference)suborder["bsd_returnorder"]).Id, new ColumnSet(true));
                    returnorder["bsd_description"] = "Return Order had been canceled";
                    service.Update(returnorder);
                    SetStateRequest setStateRequestreturn = new SetStateRequest()
                    {
                        EntityMoniker = new EntityReference
                        {
                            Id          = returnorder.Id,
                            LogicalName = returnorder.LogicalName
                        },
                        State  = new OptionSetValue(1),
                        Status = new OptionSetValue(2)
                    };
                    service.Execute(setStateRequestreturn);
                }
                //
                #region 27-12-2017 call service AX
                if (suborder.Contains("bsd_suborderax") && suborder["bsd_suborderax"] != null)
                {
                    string s_Result = client.BHS_CancelSalesOrder(contextService, suborder["bsd_suborderax"].ToString().Trim());
                    if (s_Result != "Success")
                    {
                        throw new Exception("AX " + s_Result);
                    }
                }
                #endregion
                context.OutputParameters["Return"] = "success";

                #endregion
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Exemple #3
0
        public void Execute(IServiceProvider serviceProvider)
        {
            context = serviceProvider.GetService(typeof(IPluginExecutionContext)) as IPluginExecutionContext;
            factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            service = factory.CreateOrganizationService(context.UserId);
            if (context.MessageName == "bsd_Action_CheckUpdateWareHouseProduct")
            {
                EntityReference target = (EntityReference)context.InputParameters["Target"];
                try
                {
                    bool connect_ax = getConfigdefaultconnectax();
                    if (connect_ax == false)
                    {
                        throw new Exception("Thiếu khai báo connect ax");
                    }
                    #region KHai báo service AX
                    NetTcpBinding binding = new NetTcpBinding();
                    //net.tcp://AOS_SERVICE_HOST/DynamicsAx/Services/AXConectorAIF
                    binding.Name = "NetTcpBinding_BHS_BSD_CRMSERVICEAXService";
                    EndpointAddress endpoint = new EndpointAddress(new Uri("net.tcp://" + _port + "/DynamicsAx/Services/BHS_BSD_CRMSERVICEAXServiceGroup"));
                    ServiceReferenceAIF.BHS_BSD_CRMSERVICEAXServiceClient client = new ServiceReferenceAIF.BHS_BSD_CRMSERVICEAXServiceClient(binding, endpoint);
                    client.ClientCredentials.Windows.ClientCredential.Domain   = _domain;
                    client.ClientCredentials.Windows.ClientCredential.UserName = _userName;
                    client.ClientCredentials.Windows.ClientCredential.Password = _passWord;
                    ServiceReferenceAIF.CallContext contextService = new ServiceReferenceAIF.CallContext()
                    {
                        Company = _company
                    };
                    #endregion
                    string bsd_site           = "";
                    string productnumber      = "";
                    string lst_CheckProductAX = "";
                    string lst_Product        = "";
                    Entity requestDelivery    = service.Retrieve(target.LogicalName, target.Id, new ColumnSet(true));
                    // throw new Exception("okie");
                    if (requestDelivery.HasValue("bsd_site"))
                    {
                        #region
                        EntityReference rf_Entity = (EntityReference)requestDelivery["bsd_site"];

                        #endregion
                        EntityCollection lstWarehouse;
                        if (getConsignmentSitesConfigDefault(rf_Entity.Id))
                        {
                            lstWarehouse = getWareHouse();
                        }
                        else
                        {
                            lstWarehouse = getWareHouse(rf_Entity.Id);
                        }
                        #region
                        EntityCollection lstRequestDeliveryProduct = getRequestDeliveryProduct(requestDelivery.Id);
                        //string s_Result1 = client.BHS_ValidateOnHand(contextService, "01D.01.013:102:KD01-102:0");
                        //throw new Exception(s_Result1);
                        if (lstWarehouse.Entities.Any() && lstRequestDeliveryProduct.Entities.Any())
                        {
                            //throw new Exception(lstWarehouse.Entities.Count.ToString());
                            foreach (Entity warehouse in lstWarehouse.Entities)
                            {
                                int i = 0;
                                rf_Entity = (EntityReference)warehouse["bsd_site"];
                                Entity en_Site = service.Retrieve(rf_Entity.LogicalName, rf_Entity.Id, new ColumnSet("bsd_code"));
                                if (en_Site.HasValue("bsd_code"))
                                {
                                    bsd_site = en_Site["bsd_code"].ToString().Trim();
                                }
                                foreach (Entity RequestDeliveryProduct in lstRequestDeliveryProduct.Entities)
                                {
                                    rf_Entity = (EntityReference)RequestDeliveryProduct["bsd_product"];
                                    Entity en = service.Retrieve(rf_Entity.LogicalName, rf_Entity.Id, new ColumnSet("productnumber"));
                                    if (en.HasValue("productnumber"))
                                    {
                                        productnumber = en["productnumber"].ToString().Trim();
                                    }
                                    if (i == 0)
                                    {
                                        lst_Product = productnumber + ":" + bsd_site + ":" + warehouse["bsd_warehouseid"].ToString().Trim() + ":0";
                                    }
                                    else
                                    {
                                        lst_Product += ";" + productnumber + ":" + bsd_site + ":" + warehouse["bsd_warehouseid"].ToString().Trim() + ":0";
                                    }
                                    i++;
                                }

                                #region call Service Update Avalible WareHouse
                                string s_Result = "";
                                // throw new Exception("okie1" + lst_Product);
                                try
                                {
                                    s_Result = client.BHS_ValidateOnHand_RequestDelivery(contextService, lst_Product);
                                }
                                catch (Exception ex) { throw new Exception("Service:" + ex.Message + " lst_Product:" + lst_Product); }
                                // if (warehouse["bsd_warehouseid"].ToString().Trim() == "KD01-102") throw new Exception("okie:"+ s_Result);
                                //throw new Exception(s_Result + "---" + lst_Product);
                                string[] lstProduct_Result = new string[] { };
                                string[] lstitem           = new string[] { };
                                lstProduct_Result = s_Result.Split(';');
                                // throw new Exception(s_Result);
                                foreach (string item in lstProduct_Result)
                                {
                                    lstitem = item.Split(':');

                                    decimal Quantity = Convert.ToDecimal(lstitem[4]);

                                    decimal QuantityRequestDeliveryProduct = 0m;
                                    //throw new Exception(QuantityRequestDeliveryProduct.ToString());
                                    //throw new Exception(QuantityRequestDeliveryProduct.ToString());
                                    EntityCollection wareHouse_Product = getWareHouseProduct(en_Site.Id, warehouse.Id, lstitem[0]);
                                    if (wareHouse_Product.Entities.Any())
                                    {
                                        //foreach (var en in wareHouse_Product.Entities)
                                        //{
                                        //    Entity wareHouse_Product_Update = new Entity(en.LogicalName, en.Id);
                                        //    wareHouse_Product_Update["bsd_date"] = DateTime.Now;
                                        //    wareHouse_Product_Update["bsd_quantity"] = Quantity;
                                        //    service.Update(wareHouse_Product_Update);
                                        //}
                                        foreach (var en in wareHouse_Product.Entities)
                                        {
                                            QuantityRequestDeliveryProduct = getQuantityRequestDeliveryProduct(lstitem[0], ((EntityReference)en["bsd_warehouses"]).Name);//productnumber
                                            Entity wareHouse_Product_Update = new Entity(en.LogicalName, en.Id);
                                            wareHouse_Product_Update["bsd_date"] = DateTime.Now;
                                            if (Quantity < 0 && Quantity != 0)
                                            {
                                                wareHouse_Product_Update["bsd_quantity"] = (Quantity - QuantityRequestDeliveryProduct) * -1;
                                            }
                                            //else
                                            //{
                                            //    wareHouse_Product_Update["bsd_quantity"] = Quantity;
                                            //}
                                            //wareHouse_Product_Update["bsd_quantity"] = Quantity;
                                            if (Quantity == 0)
                                            {
                                                wareHouse_Product_Update["bsd_quantity"] = Quantity;
                                            }
                                            if (Quantity > 0)
                                            {
                                                wareHouse_Product_Update["bsd_quantity"]    = Quantity - QuantityRequestDeliveryProduct;
                                                wareHouse_Product_Update["bsd_description"] = "AX Check WareHouse3";
                                            }
                                            service.Update(wareHouse_Product_Update);
                                        }
                                    }
                                    else
                                    {
                                        Entity wareHouse_Product_Create = new Entity("bsd_warehourseproduct", Guid.NewGuid());
                                        if (Quantity < 0 && Quantity != 0)
                                        {
                                            wareHouse_Product_Create["bsd_quantity"] = (Quantity - QuantityRequestDeliveryProduct) * -1;
                                        }
                                        if (Quantity == 0)
                                        {
                                            wareHouse_Product_Create["bsd_quantity"] = 0m;
                                        }
                                        if (Quantity > 0)
                                        {
                                            wareHouse_Product_Create["bsd_quantity"] = Quantity - QuantityRequestDeliveryProduct;
                                        }
                                        //wareHouse_Product_Create["bsd_quantity"] = Quantity;
                                        wareHouse_Product_Create["bsd_name"]       = lstitem[1].ToString();
                                        wareHouse_Product_Create["bsd_date"]       = DateTime.Now;
                                        wareHouse_Product_Create["bsd_site"]       = new EntityReference(en_Site.LogicalName, en_Site.Id);
                                        wareHouse_Product_Create["bsd_warehouses"] = new EntityReference(warehouse.LogicalName, warehouse.Id);
                                        wareHouse_Product_Create["bsd_productid"]  = lstitem[0].ToString().Trim();
                                        Entity en_product = getProduct(lstitem[0].ToString().Trim());
                                        wareHouse_Product_Create["bsd_product"]     = new EntityReference(en_product.LogicalName, en_product.Id);
                                        wareHouse_Product_Create["bsd_unit"]        = (EntityReference)en_product["defaultuomid"];
                                        wareHouse_Product_Create["bsd_description"] = "AX Check WareHouse";
                                        if (Quantity > 0)
                                        {
                                            wareHouse_Product_Create["bsd_description"] = "AX Check WareHouse1";
                                        }
                                        service.Create(wareHouse_Product_Create);
                                    }
                                }
                                #endregion
                            }
                        }
                        #endregion
                    }
                    //string bsd_site = "";
                    //string productnumber = "";
                    //string lst_CheckProductAX = "";
                    //string lst_Product = "";
                    //Entity requestDelivery = service.Retrieve(target.LogicalName, target.Id, new ColumnSet(true));
                    //// throw new Exception("okie");
                    //if (requestDelivery.HasValue("bsd_site"))
                    //{
                    //    #region
                    //    EntityReference rf_Entity = (EntityReference)requestDelivery["bsd_site"];

                    //    #endregion
                    //    EntityCollection lstWarehouse;
                    //    if (getConsignmentSitesConfigDefault(rf_Entity.Id))
                    //    {

                    //        lstWarehouse = getWareHouse();
                    //    }
                    //    else
                    //        lstWarehouse = getWareHouse(rf_Entity.Id);
                    //    #region
                    //    EntityCollection lstRequestDeliveryProduct = getRequestDeliveryProduct(requestDelivery.Id);
                    //    //string s_Result1 = client.BHS_ValidateOnHand(contextService, "01D.01.013:102:KD01-102:0");
                    //    //throw new Exception(s_Result1);
                    //    if (lstWarehouse.Entities.Any() && lstRequestDeliveryProduct.Entities.Any())
                    //    {

                    //        foreach (Entity warehouse in lstWarehouse.Entities)
                    //        {
                    //            int i = 0;
                    //            rf_Entity = (EntityReference)warehouse["bsd_site"];
                    //            Entity en_Site = service.Retrieve(rf_Entity.LogicalName, rf_Entity.Id, new ColumnSet("bsd_code"));
                    //            if (en_Site.HasValue("bsd_code")) bsd_site = en_Site["bsd_code"].ToString().Trim();
                    //            foreach (Entity RequestDeliveryProduct in lstRequestDeliveryProduct.Entities)
                    //            {
                    //                rf_Entity = (EntityReference)RequestDeliveryProduct["bsd_product"];
                    //                Entity en = service.Retrieve(rf_Entity.LogicalName, rf_Entity.Id, new ColumnSet("productnumber"));
                    //                if (en.HasValue("productnumber")) productnumber = en["productnumber"].ToString().Trim();
                    //                if (i == 0) lst_Product = productnumber + ":" + bsd_site + ":" + warehouse["bsd_warehouseid"].ToString().Trim() + ":0";
                    //                else lst_Product += ";" + productnumber + ":" + bsd_site + ":" + warehouse["bsd_warehouseid"].ToString().Trim() + ":0";
                    //                i++;
                    //            }

                    //            #region call Service Update Avalible WareHouse
                    //            string s_Result = "";
                    //            // throw new Exception("okie1" + lst_Product);
                    //            try
                    //            {
                    //                s_Result = client.BHS_ValidateOnHand_RequestDelivery(contextService, lst_Product);
                    //            }
                    //            catch (Exception ex) { throw new Exception("Service:" + ex.Message + " lst_Product:" + lst_Product); }
                    //            // if (warehouse["bsd_warehouseid"].ToString().Trim() == "KD01-102") throw new Exception("okie:"+ s_Result);
                    //            //throw new Exception(s_Result + "---" + lst_Product);
                    //            string[] lstProduct_Result = new string[] { };
                    //            string[] lstitem = new string[] { };
                    //            lstProduct_Result = s_Result.Split(';');
                    //            // throw new Exception(s_Result);
                    //            //throw new Exception(lstProduct_Result.Count().ToString() + "ta");
                    //            foreach (string item in lstProduct_Result)
                    //            {
                    //                lstitem = item.Split(':');

                    //                decimal Quantity = Convert.ToDecimal(lstitem[4]);

                    //                // decimal Quantity = 11000000;

                    //                EntityCollection wareHouse_Product = getWareHouseProduct(en_Site.Id, warehouse.Id, lstitem[0]);
                    //                throw new Exception(lstWarehouse.Entities.Count.ToString());
                    //                decimal QuantityRequestDeliveryProduct = getQuantityRequestDeliveryProduct(lstitem[0]);//productnumber
                    //                if (wareHouse_Product.Entities.Any())
                    //                {

                    //                    foreach (var en in wareHouse_Product.Entities)
                    //                    {
                    //                        Entity wareHouse_Product_Update = new Entity(en.LogicalName, en.Id);
                    //                        wareHouse_Product_Update["bsd_date"] = DateTime.Now;
                    //                        if (Quantity ==0)
                    //                        {
                    //                            wareHouse_Product_Update["bsd_quantity"] = QuantityRequestDeliveryProduct;
                    //                        }
                    //                        else
                    //                        {
                    //                            if (Quantity - QuantityRequestDeliveryProduct < 0)
                    //                            {
                    //                                wareHouse_Product_Update["bsd_quantity"] = (Quantity - QuantityRequestDeliveryProduct) * -1;
                    //                            }
                    //                            else
                    //                            {
                    //                                wareHouse_Product_Update["bsd_quantity"] = Quantity - QuantityRequestDeliveryProduct;
                    //                            }

                    //                        }


                    //                        service.Update(wareHouse_Product_Update);
                    //                    }

                    //                }
                    //                else
                    //                {

                    //                    Entity wareHouse_Product_Create = new Entity("bsd_warehourseproduct", Guid.NewGuid());
                    //                    if (Quantity == 0)
                    //                    {

                    //                        wareHouse_Product_Create["bsd_quantity"] = QuantityRequestDeliveryProduct;
                    //                    }
                    //                    else
                    //                    {
                    //                        if (Quantity - QuantityRequestDeliveryProduct < 0)
                    //                        {
                    //                            wareHouse_Product_Create["bsd_quantity"] = (Quantity - QuantityRequestDeliveryProduct)*-1;
                    //                        }
                    //                        else
                    //                        {
                    //                            wareHouse_Product_Create["bsd_quantity"] = Quantity - QuantityRequestDeliveryProduct;
                    //                        }

                    //                    }

                    //                    wareHouse_Product_Create["bsd_name"] = lstitem[1].ToString();
                    //                    wareHouse_Product_Create["bsd_date"] = DateTime.Now;
                    //                    wareHouse_Product_Create["bsd_site"] = new EntityReference(en_Site.LogicalName, en_Site.Id);
                    //                    wareHouse_Product_Create["bsd_warehouses"] = new EntityReference(warehouse.LogicalName, warehouse.Id);
                    //                    wareHouse_Product_Create["bsd_productid"] = lstitem[0].ToString().Trim();
                    //                    Entity en_product = getProduct(lstitem[0].ToString().Trim());
                    //                    wareHouse_Product_Create["bsd_product"] = new EntityReference(en_product.LogicalName, en_product.Id);
                    //                    wareHouse_Product_Create["bsd_unit"] = (EntityReference)en_product["defaultuomid"];
                    //                    wareHouse_Product_Create["bsd_description"] = "AX Check WareHouse";
                    //                    service.Create(wareHouse_Product_Create);
                    //                }
                    //            }
                    //            #endregion
                    //        }

                    //    }
                    //    #endregion
                    //}
                    context.OutputParameters["ReturnId"] = "true";
                }
                catch (Exception ex)
                {
                    throw new Exception("Can not check available Warehouse because: " + ex.Message);
                }
            }
        }
Exemple #4
0
        public void Execute(IServiceProvider serviceProvider)
        {
            context = serviceProvider.GetService(typeof(IPluginExecutionContext)) as IPluginExecutionContext;
            factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            service = factory.CreateOrganizationService(context.UserId);
            if (context.MessageName == "bsd_Check_creditlimit")
            {
                int i = 0;
                try
                {
                    bool connect_ax = getConfigdefaultconnectax();
                    if (connect_ax == false)
                    {
                        throw new Exception("Thiếu khai báo connect ax");
                    }
                    EntityReference target = (EntityReference)context.InputParameters["Target"];
                    #region KHai báo service AX
                    NetTcpBinding binding = new NetTcpBinding();
                    //net.tcp://AOS_SERVICE_HOST/DynamicsAx/Services/AXConectorAIF
                    binding.Name = "NetTcpBinding_BHS_BSD_CRMSERVICEAXService";
                    EndpointAddress endpoint = new EndpointAddress(new Uri("net.tcp://" + _port + "/DynamicsAx/Services/BHS_BSD_CRMSERVICEAXServiceGroup"));
                    ServiceReferenceAIF.BHS_BSD_CRMSERVICEAXServiceClient client = new ServiceReferenceAIF.BHS_BSD_CRMSERVICEAXServiceClient(binding, endpoint);
                    client.ClientCredentials.Windows.ClientCredential.Domain   = _domain;
                    client.ClientCredentials.Windows.ClientCredential.UserName = _userName;
                    client.ClientCredentials.Windows.ClientCredential.Password = _passWord;
                    ServiceReferenceAIF.CallContext contextService = new ServiceReferenceAIF.CallContext()
                    {
                        Company = _company
                    };
                    #endregion

                    Entity suborder = service.Retrieve(target.LogicalName, target.Id, new ColumnSet(true));
                    if (suborder.HasValue("bsd_invoiceaccount"))
                    {
                        bsd_customercode = suborder["bsd_invoiceaccount"].ToString().Trim();
                        decimal s_Result = client.BHS_ValidateCustOpenBalanceOne(contextService, bsd_customercode);
                        // s_Result = 100000000;
                        Entity suborder_Update = new Entity(suborder.LogicalName, suborder.Id);

                        i = 5;
                        suborder_Update["bsd_olddebt"]     = new Money(s_Result);
                        suborder_Update["bsd_olddebttext"] = String.Format("{0:#,###.####}", (new Money(s_Result)).Value);
                        i = 6;
                        decimal bsd_totalcurrencyexchange = ((Money)suborder["bsd_totalcurrencyexchange"]).Value;
                        suborder_Update["bsd_newdebt"]     = new Money(s_Result + bsd_totalcurrencyexchange);
                        suborder_Update["bsd_newdebttext"] = String.Format("{0:#,###.####}", new Money(s_Result + bsd_totalcurrencyexchange).Value);
                        i = 7;
                        suborder_Update["bsd_submittedgrandtotal"]     = new Money(s_Result + bsd_totalcurrencyexchange);
                        suborder_Update["bsd_submittedgrandtotaltext"] = String.Format("{0:#,###.####}", (new Money(s_Result + bsd_totalcurrencyexchange)).Value);
                        i = 8;
                        decimal bsd_creditlimit = 0m;
                        if (suborder.HasValue("bsd_creditlimit"))
                        {
                            bsd_creditlimit = ((Money)suborder["bsd_creditlimit"]).Value;
                        }
                        //throw new Exception(s_Result.ToString() + "----" + suborder["bsd_totalcurrencyexchange"].ToString() + "-----" + ((decimal)suborder["bsd_totalcurrencyexchange"]).ToString());
                        if ((s_Result + bsd_totalcurrencyexchange) > bsd_creditlimit)
                        {
                            Return = "Customer exceeds credit limit";
                        }
                        else if ((s_Result + bsd_totalcurrencyexchange) <= bsd_creditlimit)
                        {
                            Return = "success";
                        }
                        i = 9;
                        service.Update(suborder_Update);
                        i = 16;
                    }
                    else
                    {
                        Return = "success";
                    }
                    // throw new Exception(Return);
                    context.OutputParameters["Return"] = Return;
                }
                catch (Exception ex)
                {
                    // context.OutputParameters["Return"] = "Customer exceeds credit limit";
                    throw new Exception("Error service AX check credit limit: " + ex.Message + i.ToString());
                }
            }
        }