// công nợ và tồn kho . khi submit lần đầu và khi bị reject lại xong submite public void Execute(IServiceProvider serviceProvider) { myService = new MyService(serviceProvider); if (myService.context.MessageName == "Update") { Entity target = myService.getTarget(); if (target.HasValue("bsd_skipplugin") && (bool)target["bsd_skipplugin"]) { return; } myService.StartService(); if (!target.HasValue("statuscode")) { return; } Service.SuborderService subService = new Service.SuborderService(myService); Entity suborder = myService.service.Retrieve(target.LogicalName, target.Id, new ColumnSet(true)); Entity pre_suborder = myService.context.PreEntityImages["PreImage"]; int pre_statuscode = ((OptionSetValue)pre_suborder["statuscode"]).Value; int new_statuscode = ((OptionSetValue)target["statuscode"]).Value; if (pre_statuscode == new_statuscode) { return; } 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) { return; } EntityReference owner = (EntityReference)suborder["ownerid"]; string role = subService.GetRoleName(owner.Id, myService.service); EntityReference account_ref = (EntityReference)suborder["bsd_potentialcustomer"];; Entity customerdebt = subService.GetCustomerDebtByTimeAndAccount(account_ref.Id, DateTime.Now); if (customerdebt == null) { throw new Exception("Account has no Customer Debt"); } decimal current_debt = customerdebt.HasValue("bsd_newdebt") ? ((Money)customerdebt["bsd_newdebt"]).Value : 0m; decimal grand_total = ((Money)suborder["bsd_totalcurrencyexchange"]).Value; #region Kiểm tra submit lần đầu. bool staff_submit = pre_statuscode == 861450000 && new_statuscode == 861450003 && role == "Nhân Viên"; // Staff Create => Manager Pendding Approval bool manager_submit = pre_statuscode == 861450001 && new_statuscode == 861450004 && role == "Trưởng Phòng"; // Manager Create => Chief Offcie Pendding Approval if (staff_submit || manager_submit) { #region Cập nhật công nợ Entity new_customerdebt = new Entity(customerdebt.LogicalName, customerdebt.Id); decimal new_debt = current_debt + grand_total; new_customerdebt["bsd_newdebt"] = new Money(new_debt); myService.service.Update(new_customerdebt); #endregion #region cập nhật submitted quantity và submitted customerdebt Entity update_suborder = new Entity(suborder.LogicalName, suborder.Id); update_suborder["bsd_submittedgrandtotal"] = new Money(grand_total); update_suborder["bsd_submittedgrandtotaltext"] = grand_total.DecimalToStringHideSymbol(); update_suborder["bsd_customerdebt"] = new EntityReference(customerdebt.LogicalName, customerdebt.Id); update_suborder["bsd_submittedcustomerdebt"] = new EntityReference(customerdebt.LogicalName, customerdebt.Id); myService.Update(update_suborder); #endregion Update_SuborderProduct_SubmitedQuantity(suborder.Id); } else { // lần 2 bool staff_submit_2 = pre_statuscode == 861450005 && new_statuscode == 861450003 && role == "Nhân Viên"; // Manager Unapprove => Manager Pendding Approval bool manager_submit_2 = pre_statuscode == 861450006 && new_statuscode == 861450004 && role == "Trưởng Phòng"; // Chief office unapprove => Chief Offcie Pendding Approval if (staff_submit_2 || manager_submit_2) { EntityReference submitted_customerdebt_ref = (EntityReference)suborder["bsd_submittedcustomerdebt"]; decimal submitted_grandtotal = ((Money)suborder["bsd_submittedgrandtotal"]).Value; #region Cập nhật công nợ Entity new_customerdebt = new Entity(customerdebt.LogicalName, customerdebt.Id); decimal new_debt = 0m; // Nếu cái đã submit lên trước khi reject bằng với bảng công nợ đang submit. thì trừ submited_grandtotal if (submitted_customerdebt_ref.Id.Equals(customerdebt.Id)) { new_debt = (current_debt - submitted_grandtotal) + grand_total; } else // ngược lại nó khác nhau thì không trừ cái đã submit, mà chỉ cộng cái grand total mới. { new_debt = current_debt + grand_total; } new_customerdebt["bsd_newdebt"] = new Money(new_debt); myService.service.Update(new_customerdebt); #endregion #region cập nhật submitted quantity vaf submitted customerdebt Entity update_suborder = new Entity(suborder.LogicalName, suborder.Id); update_suborder["bsd_submittedgrandtotal"] = new Money(grand_total); update_suborder["bsd_submittedgrandtotaltext"] = grand_total.DecimalToStringHideSymbol(); update_suborder["bsd_customerdebt"] = new EntityReference(customerdebt.LogicalName, customerdebt.Id); update_suborder["bsd_submittedcustomerdebt"] = new EntityReference(customerdebt.LogicalName, customerdebt.Id); myService.Update(update_suborder); #endregion Update_SuborderProduct_SubmitedQuantity(suborder.Id); } } #endregion } }
public void Execute(IServiceProvider serviceProvider) { myService = new MyService(serviceProvider); #region Update if (myService.context.MessageName == "Update") { myService.StartService(); Entity target = myService.getTarget(); if (target.HasValue("bsd_skipplugin") && (bool)target["bsd_skipplugin"]) { return; } Entity suborder = myService.service.Retrieve(target.LogicalName, target.Id, new ColumnSet(true)); if (target.HasValue("statecode")) { int statecode = ((OptionSetValue)target["statecode"]).Value; if (statecode == 1) { #region Nếu có Status và nó là từ quote hoặc order if (suborder.HasValue("bsd_quote") || suborder.HasValue("bsd_order")) { Service.SuborderService subService = new Service.SuborderService(myService); EntityCollection list_suborderproduct = myService.RetrieveOneCondition("bsd_suborderproduct", "bsd_suborder", target.Id); foreach (var suborder_product in list_suborderproduct.Entities) { decimal ship_quantity = (decimal)suborder_product["bsd_shipquantity"]; if (suborder.HasValue("bsd_quote")) { Entity quotedetail = subService.getQuoteDetailFromSuborderProduct(suborder_product, 2); Entity quote = myService.service.Retrieve("quote", ((EntityReference)quotedetail["quoteid"]).Id, new ColumnSet(true)); bool multiple_address = (bool)quote["bsd_multipleaddress"]; bool have_quantity = (bool)quote["bsd_havequantity"]; if (multiple_address == false || (multiple_address == true && have_quantity == true)) { decimal quantity = (decimal)quotedetail["quantity"]; decimal old_suborder_quantity = (decimal)quotedetail["bsd_suborderquantity"]; decimal remaining_quantity = (decimal)quotedetail["bsd_remainingquantity"]; decimal new_suborder_quantity = old_suborder_quantity - ship_quantity; decimal new_remaining_quantity = quantity - new_suborder_quantity; #region Cập nhật lại quantity EntityReference quote_ref = (EntityReference)suborder["bsd_quote"]; myService.SetState(quote_ref.Id, quote_ref.LogicalName, 0, 1); Entity new_quotedetail = new Entity(quotedetail.LogicalName, quotedetail.Id); new_quotedetail["bsd_suborderquantity"] = new_suborder_quantity; new_quotedetail["bsd_remainingquantity"] = new_remaining_quantity; myService.Update(new_quotedetail); #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 #endregion } else { // cancel thì trừ đi cũng giống như xóa subService.DeleteSuborderProduct(suborder_product); } } else if (suborder.HasValue("bsd_order") && !suborder.HasValue("bsd_appendixcontract"))//không có phụ lục, nếu có thì chạy qua code Huy { Entity salesorderdetail = subService.getSalesorderDetailFromSuborderProduct(suborder_product, 2); Entity order = myService.service.Retrieve("salesorder", ((EntityReference)salesorderdetail["salesorderid"]).Id, new ColumnSet(true)); bool multiple_address = (bool)order["bsd_multipleaddress"]; bool have_quantity = (bool)order["bsd_havequantity"]; if (multiple_address == false || (multiple_address == true && have_quantity == true)) { decimal quantity = (decimal)salesorderdetail["quantity"]; decimal old_suborder_quantity = (decimal)salesorderdetail["bsd_suborderquantity"]; decimal remaining_quantity = (decimal)salesorderdetail["bsd_remainingquantity"]; decimal new_suborder_quantity = old_suborder_quantity - ship_quantity; decimal new_remaining_quantity = quantity - new_suborder_quantity; #region Cập nhật lại quantity Entity new_salesorderdetail = new Entity(salesorderdetail.LogicalName, salesorderdetail.Id); new_salesorderdetail["bsd_suborderquantity"] = new_suborder_quantity; new_salesorderdetail["bsd_remainingquantity"] = new_remaining_quantity; myService.Update(new_salesorderdetail); #endregion } else { subService.DeleteSuborderProduct(suborder_product); } } } } #endregion TruCongNoFromSub(suborder); } } } #endregion }