public void TruCongNoFromSub(Entity suborder) { if (suborder.HasValue("bsd_potentialcustomer") && suborder.HasValue("bsd_submittedcustomerdebt")) { SuborderService subService = new SuborderService(myService); Entity configdefault = myService.FetchXml(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> <entity name='bsd_configdefault'> <attribute name='bsd_configdefaultid' /> <attribute name='createdon' /> <attribute name='bsd_checkcustomerbalance' /> <order attribute='createdon' descending='true' /> </entity> </fetch>").Entities.FirstOrDefault(); bool check_customerbalance = configdefault.HasValue("bsd_checkcustomerbalance") ? (bool)configdefault["bsd_checkcustomerbalance"] : true; if (check_customerbalance) { EntityReference account_ref = (EntityReference)suborder["bsd_potentialcustomer"]; Entity submitted_customerdebt = myService.service.Retrieve("bsd_customerdebt", ((EntityReference)suborder["bsd_submittedcustomerdebt"]).Id, new ColumnSet(true)); Entity current_customerdebt = subService.GetCustomerDebtByTimeAndAccount(account_ref.Id, DateTime.Now); if (submitted_customerdebt.Id.Equals(current_customerdebt.Id)) { decimal current_debt = submitted_customerdebt.HasValue("bsd_newdebt") ? ((Money)submitted_customerdebt["bsd_newdebt"]).Value : 0; decimal submitted_grandtotal = ((Money)suborder["bsd_submittedgrandtotal"]).Value; Entity new_customerdebt = new Entity(current_customerdebt.LogicalName, current_customerdebt.Id); new_customerdebt["bsd_newdebt"] = new Money(current_debt - submitted_grandtotal); // Hiện tại trừ đi Grand Total đã submitted. myService.Update(new_customerdebt); } } } }
public void Execute(IServiceProvider serviceProvider) { myService = new MyService(serviceProvider); IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); if (myService.context.MessageName == "bsd_CreateDeliveryPlan") { string Issubmit = context.InputParameters["Issubmit"].ToString(); // throw new Exception("okie"); EntityReference target = myService.getTargetEntityReference(); myService.StartService(); Entity suborder = myService.service.Retrieve(target.LogicalName, target.Id, new ColumnSet(true)); if (!suborder.HasValue("bsd_requestedshipdate") || !suborder.HasValue("bsd_requestedreceiptdate")) { throw new Exception("You must provice a value for Ship Date and Receipt Date"); } if (myService.context.Depth > 1) { return; } #region check số lượng trên suborder EntityCollection list_noquantity = myService.FetchXml(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> <entity name='bsd_suborderproduct'> <attribute name='bsd_suborderproductid' /> <filter type='and'> <condition attribute='bsd_suborder' operator='eq' uitype='bsd_suborder' value='" + suborder.Id + @"' /> <condition attribute='bsd_shipquantity' operator='eq' value='0' /> </filter> </entity> </fetch>"); if (list_noquantity.Entities.Any()) { "No Quantity To Create Delivery Schedule".Throw(); } #endregion #region Check existing EntityCollection list_deliveryplan = myService.RetrieveOneCondition("bsd_deliveryplan", "bsd_suborder", target.Id); if (list_deliveryplan.Entities.Any()) { throw new Exception("Đã tạo rồi"); } #endregion Entity deliveryplan = new Entity("bsd_deliveryplan"); deliveryplan["bsd_suborder"] = target; try { deliveryplan["bsd_type"] = suborder["bsd_type"]; // vinhlh 21-12-2017 suborder Site Kí gửi if (((OptionSetValue)suborder["bsd_typeorder"]).Value == 861450002) { EntityReference site_Consignment = getDefaultConfig(); deliveryplan["bsd_site"] = new EntityReference(site_Consignment.LogicalName, site_Consignment.Id); //Entity Site_Consignment = myService.service.Retrieve(site_Consignment.LogicalName, site_Consignment.Id, new ColumnSet("bsd_address")); //if (Site_Consignment.HasValue("bsd_address")) // deliveryplan["bsd_siteaddress"] = (EntityReference)Site_Consignment["bsd_address"]; } //end vinhlh } catch { } NewEntity ne = new NewEntity(suborder, deliveryplan); ne.Set("bsd_order", true); ne.Set("bsd_quote", true); ne.Set("bsd_potentialcustomer", true); ne.Set("bsd_addresscustomeraccount", true); ne.Set("bsd_telephone", true); ne.Set("bsd_contact", true); ne.Set("bsd_requestedshipdate", true); ne.Set("bsd_requestedreceiptdate", true); ne.Set("bsd_confirmedreceiptdate", true); ne.Set("bsd_historyreceiptcustomer", true); ne.Set("bsd_shiptoaccount", "bsd_potentialcustomer", true); ne.Set("bsd_shiptoaddress", "bsd_shiptoaddress", true); // vinhlh 21-12-2017 suborder Site Kí gửi if (suborder.HasValue("bsd_typeorder")) { if (((OptionSetValue)suborder["bsd_typeorder"]).Value != 861450002) { ne.Set("bsd_site", true); //ne.Set("bsd_siteaddress", true); } } else { ne.Set("bsd_site", true); //ne.Set("bsd_siteaddress", true); } //end vinhlh ne.Set("bsd_siteaddress", true); ne.Set("bsd_port", true); ne.Set("bsd_addressport", true); ne.Set("bsd_shippingaddress", true); ne.Set("bsd_shippingfromaddress", true); ne.Set("bsd_deliveryfrom", true); ne.Set("bsd_carrier", true); ne.Set("bsd_transportation", true); ne.Set("bsd_requestporter", true); Guid deliveryplan_id = myService.service.Create(deliveryplan); EntityCollection list_sub_product = myService.RetrieveOneCondition("bsd_suborderproduct", "bsd_suborder", suborder.Id); if (list_sub_product.Entities.Any()) { EntityReference deliveryplan_ref = new EntityReference("bsd_deliveryplan", deliveryplan_id); foreach (var item in list_sub_product.Entities) { Entity deliveryplan_product = new Entity("bsd_deliveryplanproduct"); deliveryplan_product["bsd_deliveryplan"] = deliveryplan_ref; deliveryplan_product["bsd_unit"] = item["bsd_unit"]; deliveryplan_product["bsd_product"] = item["bsd_product"]; if (item.HasValue("bsd_productid")) { deliveryplan_product["bsd_productid"] = item["bsd_productid"]; } if (item.HasValue("bsd_descriptionproduct")) { deliveryplan_product["bsd_descriptionproduct"] = item["bsd_descriptionproduct"]; } if (deliveryplan_product.HasValue("bsd_orderquantity")) { deliveryplan_product["bsd_orderquantity"] = item["bsd_orderquantity"]; } //huy if (!item.HasValue("bsd_appendixcontract"))//hợp đồng không có phục lục thì tính bình thường { deliveryplan_product["bsd_shipquantity"] = item["bsd_shipquantity"]; deliveryplan_product["bsd_remainingquantity"] = item["bsd_shipquantity"]; deliveryplan_product["bsd_remainaddtruck"] = item["bsd_shipquantity"]; } else//có phụ lục thì tính theo số lượng phụ lục + số lượng trong order { deliveryplan_product["bsd_shipquantity"] = item["bsd_newquantity"]; deliveryplan_product["bsd_remainingquantity"] = item["bsd_newquantity"]; deliveryplan_product["bsd_remainaddtruck"] = item["bsd_newquantity"]; } //end huy if (item.HasValue("bsd_standardquantity")) { deliveryplan_product["bsd_standardquantity"] = item["bsd_standardquantity"]; } // setname. Entity product = myService.service.Retrieve("product", ((EntityReference)item["bsd_product"]).Id, new ColumnSet("name")); deliveryplan_product["bsd_name"] = product["name"]; // setname myService.service.Create(deliveryplan_product); } } else { throw new Exception("No Product to create Delivery Plan !"); } #region cập nhật lai created delivery plan của suborder Entity new_suborder = new Entity(suborder.LogicalName, suborder.Id); new_suborder["bsd_createddeliveryplan"] = true; myService.Update(new_suborder); #endregion #region Phong 2-2-2018 Approve if (Issubmit.ToLower().Trim().Equals("true")) { string entityName = context.InputParameters["Entity"].ToString(); Guid RecordID = Guid.Parse(context.InputParameters["ObjectId"].ToString()); Guid StageId = Guid.Parse(context.InputParameters["StageId"].ToString()); Guid User = Guid.Parse(context.InputParameters["Username"].ToString()); string attributeName = context.InputParameters["Attribute"].ToString(); string approveperson = context.InputParameters["Approveperson"].ToString(); string Approvedate = context.InputParameters["Approvedate"].ToString(); string traversedpath = context.InputParameters["StageId"].ToString(); int attributeValue = int.Parse(context.InputParameters["Value"].ToString()); //Guid gUserId = ((WhoAmIResponse)myService.service.Execute(new WhoAmIRequest())).UserId; Entity updatedStage = new Entity(entityName); SuborderService.Stage_Aprrove(myService.service, entityName, RecordID, User, StageId, traversedpath, attributeName, attributeValue, approveperson, Approvedate, updatedStage); } #endregion myService.context.OutputParameters["ReturnId"] = deliveryplan_id.ToString(); } #region delete else if (myService.context.MessageName == "Delete") { // Cập nhật suborder myService.StartService(); EntityReference target = myService.getTargetEntityReference(); Entity deliveryplan = myService.service.Retrieve(target.LogicalName, target.Id, new ColumnSet("bsd_suborder")); Entity suborder = new Entity("bsd_suborder", ((EntityReference)deliveryplan["bsd_suborder"]).Id); suborder["bsd_createddeliveryplan"] = false; myService.service.Update(suborder); } #endregion }
public void Execute(IServiceProvider serviceProvider) { myService = new MyService(serviceProvider); if (myService.context.Depth > 1) { return; } #region Create if (myService.context.MessageName == "Create") { myService.StartService(); Entity target = myService.getTarget(); if (target.HasValue("bsd_skipplugin") && (bool)target["bsd_skipplugin"]) { return; } Entity suborder_product = myService.service.Retrieve(target.LogicalName, target.Id, new ColumnSet(true)); Guid suborderid = ((EntityReference)suborder_product["bsd_suborder"]).Id; if (checkSubmit_SubOder(suborderid)) { throw new Exception("Can't add suborder product!"); } #region Kiểm tra tạo từ b2c Entity suborder = myService.service.Retrieve("bsd_suborder", ((EntityReference)suborder_product["bsd_suborder"]).Id, new ColumnSet("bsd_fromb2c")); if (suborder.HasValue("bsd_fromb2c") && (bool)suborder["bsd_fromb2c"]) { return; } #endregion SuborderService suborderService = new SuborderService(myService); suborderService.Create_Update_Suborder_Product(suborder_product, 1, true); // lấy suborderproduct của suborder này, và khác thằng hiện tại ! QueryExpression q = new QueryExpression("bsd_suborderproduct"); q.ColumnSet = new ColumnSet(true); FilterExpression f = new FilterExpression(); f.AddCondition(new ConditionExpression("bsd_suborder", ConditionOperator.Equal, ((EntityReference)suborder_product["bsd_suborder"]).Id)); f.AddCondition(new ConditionExpression("bsd_suborderproductid", ConditionOperator.NotEqual, suborder_product.Id)); q.Criteria = f; EntityCollection list_suborderproduct = myService.service.RetrieveMultiple(q); // cập nhật ở trên kia rồi mới xuống dưới tính if (list_suborderproduct.Entities.Any()) { // Cập nhật lại suborder_product = myService.service.Retrieve(target.LogicalName, target.Id, new ColumnSet(true)); Entity old_suborder = list_suborderproduct.Entities.First(); decimal tax_old = old_suborder.HasValue("bsd_itemsalestax") ? (decimal)old_suborder["bsd_itemsalestax"] : 0; decimal tax_new = suborder_product.HasValue("bsd_itemsalestax") ? (decimal)suborder_product["bsd_itemsalestax"] : 0; if (tax_new != tax_old) { throw new Exception("Tax dont match"); } } } #endregion #region update if (myService.context.MessageName == "Update") { myService.StartService(); Entity target = myService.getTarget(); if (target.HasValue("bsd_skipplugin") && (bool)target["bsd_skipplugin"]) { return; } Entity suborderproduct = myService.service.Retrieve(target.LogicalName, target.Id, new ColumnSet(true)); Guid suborderid = ((EntityReference)suborderproduct["bsd_suborder"]).Id; if (checkSubmit_SubOder(suborderid)) { //throw new Exception("Can't edit suborder product!"); } if (!suborderproduct.HasValue("bsd_appendixcontract")) { Entity suborder = myService.service.Retrieve("bsd_suborder", ((EntityReference)suborderproduct["bsd_suborder"]).Id, new ColumnSet("bsd_fromb2c", "bsd_duyet", "statuscode")); int statuscode = ((OptionSetValue)suborder["statuscode"]).Value; bool duyet = suborder.HasValue("bsd_duyet") ? (bool)suborder["bsd_duyet"] : false; if (duyet == true || statuscode == 861450002) { return; } #region Kiểm tra tạo từ b2c if (suborder.HasValue("bsd_fromb2c") && (bool)suborder["bsd_fromb2c"]) { return; } #endregion var type = ((OptionSetValue)suborderproduct["bsd_type"]).Value; if (type == 861450000 || type == 861450001 || type == 861450002) { SuborderService suborderService = new SuborderService(myService); Entity pre_image = myService.context.PreEntityImages["PreImage"]; decimal pre_quantity = (decimal)pre_image["bsd_shipquantity"]; suborderService.Create_Update_Suborder_Product(suborderproduct, 2, true, pre_quantity); } } } #endregion #region Delete else if (myService.context.MessageName == "Delete") { myService.StartService(); Entity suborderproduct = myService.context.PreEntityImages["PreImage"]; Entity suborder = myService.service.Retrieve("bsd_suborder", ((EntityReference)suborderproduct["bsd_suborder"]).Id, new ColumnSet("statecode", "bsd_fromb2c")); int status = ((OptionSetValue)suborder["statecode"]).Value; if (status == 1) { return; } if (!suborderproduct.HasValue("bsd_appendixcontract")) // bawngf null thi chay { #region Kiểm tra tạo từ b2c if (suborder.HasValue("bsd_fromb2c") && (bool)suborder["bsd_fromb2c"]) { return; } #endregion int type = ((OptionSetValue)suborderproduct["bsd_type"]).Value; if (type == 861450000 || type == 861450001 || type == 861450002) { SuborderService suborderService = new SuborderService(myService); suborderService.DeleteSuborderProduct(suborderproduct); } } } #endregion }
public void Update_StatusOrderAndQuote(Entity suborder_product, decimal net_quantity, decimal preimage_net_quantity, decimal net_quantityorder, decimal preimage_quantityorder, decimal net_quantityappendix, decimal preimage_quantityappendix) { Entity suborder = myService.service.Retrieve("bsd_suborder", ((EntityReference)suborder_product["bsd_suborder"]).Id, new ColumnSet(true)); int type = ((OptionSetValue)suborder["bsd_type"]).Value; bool multipleaddress = (bool)suborder["bsd_multipleaddress"]; #region quote if (type == 861450001) // quote { SuborderService suborderService = new SuborderService(myService); Entity quotedetail = suborderService.getQuoteDetailFromSuborderProduct(suborder_product, 2); Entity quote = myService.service.Retrieve("quote", ((EntityReference)quotedetail["quoteid"]).Id, new ColumnSet("bsd_havequantity")); bool have_quantity = (bool)quote["bsd_havequantity"]; EntityReference quote_ref = (EntityReference)quotedetail["quoteid"]; myService.SetState(quote_ref.Id, quote_ref.LogicalName, 0, 1); // mở ra để udpate decimal order_quantity = (multipleaddress && have_quantity == false) ? (decimal)quotedetail["bsd_suborderquantity"] : (decimal)quotedetail["quantity"]; decimal shipped_quantity = (decimal)quotedetail["bsd_shippedquantity"]; decimal new_shipped_quantity = shipped_quantity - preimage_net_quantity + net_quantity; Entity new_quote_product = new Entity(quotedetail.LogicalName, quotedetail.Id); new_quote_product["bsd_shippedquantity"] = new_shipped_quantity; decimal remaining_quantity = order_quantity - new_shipped_quantity; new_quote_product["bsd_residualquantity"] = remaining_quantity; int status = 0; if (remaining_quantity == 0) { status = 861450003; // giao hết } else if (remaining_quantity == order_quantity) { status = 861450004; // không nhận } else { status = 861450002; // đã giao 1 phần } new_quote_product["bsd_deliverystatus"] = new OptionSetValue(status); myService.Update(new_quote_product); #region won quote myService.SetState(quote_ref.Id, quote_ref.LogicalName, 1, 2); WinQuoteRequest winQuoteRequest = new WinQuoteRequest(); Entity quoteClose = new Entity("quoteclose"); quoteClose.Attributes["quoteid"] = new EntityReference("quote", new Guid(quote_ref.Id.ToString())); quoteClose.Attributes["subject"] = "Quote Close" + DateTime.Now.ToString(); winQuoteRequest.QuoteClose = quoteClose; winQuoteRequest.Status = new OptionSetValue(-1); myService.service.Execute(winQuoteRequest); #endregion if (have_quantity == false) { SuborderService subService = new SuborderService(myService); EntityReference product_ref = (EntityReference)quotedetail["productid"]; Entity totalline = subService.Get_LineTotal_QuoteProduct_Quantity(product_ref.Id, quote_ref.Id); Update_DeliveryStatusTotalLine(totalline, preimage_net_quantity, net_quantity); } } #endregion #region appendix contract else if (type == 861450002 && suborder_product.HasValue("bsd_appendixcontract"))//huy: trường hợp có phụ lục { Entity appendixcontract = myService.service.Retrieve("bsd_appendixcontract", ((EntityReference)suborder_product["bsd_appendixcontract"]).Id, new ColumnSet(true)); SuborderService suborderService = new SuborderService(myService); Entity salesorderdetail = suborderService.getSalesorderDetailFromSuborderProduct(suborder_product, 2); Entity salesorder = myService.service.Retrieve("salesorder", ((EntityReference)salesorderdetail["salesorderid"]).Id, new ColumnSet("bsd_havequantity")); Entity appendixdetail = suborderService.getAppendixContractdetailFromSuborderProduct(suborder_product, 2); bool have_quantity = (bool)salesorder["bsd_havequantity"]; decimal order_quantity = (multipleaddress && have_quantity == false) ? (decimal)salesorderdetail["bsd_suborderquantity"] : (decimal)salesorderdetail["quantity"]; decimal appendix_quantity = (multipleaddress && have_quantity == false) ? (decimal)appendixdetail["bsd_suborderquantityappendix"] : (decimal)appendixdetail["bsd_newquantity"]; decimal shipped_quantity = (decimal)salesorderdetail["bsd_shippedquantity"]; decimal shipped_quantity_appendix = appendixdetail.HasValue("bsd_shippedquantityappendix") ? (decimal)appendixdetail["bsd_shippedquantityappendix"] : 0; decimal new_shipped_quantity = shipped_quantity - preimage_quantityorder + net_quantityorder; decimal new_shipped_quantity_appendix = shipped_quantity_appendix - preimage_quantityappendix + net_quantityappendix; #region cập nhật ở hdkt Entity new_order_product = new Entity(salesorderdetail.LogicalName, salesorderdetail.Id); new_order_product["bsd_shippedquantity"] = new_shipped_quantity; decimal remaining_quantity = order_quantity - new_shipped_quantity; new_order_product["bsd_residualquantity"] = remaining_quantity; int status = 0; if (remaining_quantity == 0) { status = 861450003; // giao hết } else if (remaining_quantity == order_quantity) { status = 861450004; // không nhận } else { status = 861450002; // đã giao 1 phần } new_order_product["bsd_deliverystatus"] = new OptionSetValue(status); myService.Update(new_order_product); #endregion #region cập nhật ở phụ lục Entity new_appendix_product = new Entity(appendixdetail.LogicalName, appendixdetail.Id); new_appendix_product["bsd_shippedquantityappendix"] = new_shipped_quantity_appendix; decimal remaining_quantity_appendix = appendix_quantity - new_shipped_quantity_appendix; new_appendix_product["bsd_residualquantity"] = remaining_quantity_appendix; int status_appendix = 0; if (remaining_quantity_appendix == 0) { status_appendix = 861450003; // giao hết } else if (remaining_quantity_appendix == appendix_quantity) { status_appendix = 861450004; // không nhận } else { status_appendix = 861450002; // đã giao 1 phần } new_appendix_product["bsd_deliverystatus"] = new OptionSetValue(status_appendix); myService.Update(new_appendix_product); #endregion if (have_quantity == false) { SuborderService subService = new SuborderService(myService); EntityReference order_product_ref = (EntityReference)salesorderdetail["productid"]; EntityReference order_ref = (EntityReference)salesorderdetail["salesorderid"]; Entity totalline = subService.Get_LineTotal_OrderProduct_Quantity(order_product_ref.Id, order_ref.Id); Update_DeliveryStatusTotalLine(totalline, preimage_quantityorder, net_quantityorder); } } #endregion #region order else if (type == 861450002)// order { SuborderService suborderService = new SuborderService(myService); Entity salesorderdetail = suborderService.getSalesorderDetailFromSuborderProduct(suborder_product, 2); Entity salesorder = myService.service.Retrieve("salesorder", ((EntityReference)salesorderdetail["salesorderid"]).Id, new ColumnSet("bsd_havequantity")); bool have_quantity = (bool)salesorder["bsd_havequantity"]; decimal order_quantity = (multipleaddress && have_quantity == false) ? (decimal)salesorderdetail["bsd_suborderquantity"] : (decimal)salesorderdetail["quantity"]; decimal shipped_quantity = (decimal)salesorderdetail["bsd_shippedquantity"]; decimal new_shipped_quantity = shipped_quantity - preimage_net_quantity + net_quantity; Entity new_order_product = new Entity(salesorderdetail.LogicalName, salesorderdetail.Id); new_order_product["bsd_shippedquantity"] = new_shipped_quantity; decimal remaining_quantity = order_quantity - new_shipped_quantity; new_order_product["bsd_residualquantity"] = remaining_quantity; int status = 0; if (remaining_quantity == 0) { status = 861450003; // giao hết } else if (remaining_quantity == order_quantity) { status = 861450004; // không nhận } else { status = 861450002; // đã giao 1 phần } new_order_product["bsd_deliverystatus"] = new OptionSetValue(status); myService.Update(new_order_product); if (have_quantity == false) { SuborderService subService = new SuborderService(myService); EntityReference product_ref = (EntityReference)salesorderdetail["productid"]; EntityReference order_ref = (EntityReference)salesorderdetail["salesorderid"]; Entity totalline = subService.Get_LineTotal_OrderProduct_Quantity(product_ref.Id, order_ref.Id); Update_DeliveryStatusTotalLine(totalline, preimage_net_quantity, net_quantity); } } #endregion }