Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void MemberRetrieve(IOrganizationService service, EntityCollection results)
- {
- canPlaceBBWInternetOrder = true;
- canPlaceComputerOrder = true;
- errorMessage = "";
- try
- {
- /*OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result);
- EntityCollection results = (EntityCollection)response["EntityCollection"];*/
- StringBuilder sb = new StringBuilder();
- if (results.Entities.Count != 0)
- {
- address1 = results.Entities[0].GetAttributeValue<string>("address1_line1");
- address2 = results.Entities[0].GetAttributeValue<string>("address1_line2");
- address3 = results.Entities[0].GetAttributeValue<string>("address1_line3");
- telephone1 = results.Entities[0].GetAttributeValue<string>("telephone1");
- this.Dispatcher.BeginInvoke(delegate()
- {
- //do something here.
- MemberID = TextBoxMembershipNumber.Text = results.Entities[0].GetAttributeValue<string>("new_memberid");
- //textBlockStudentName.Text = LookupContact.Items.First().Name;*/
- if (
- (results.Entities[0].GetAttributeValue<bool>("new_istb") != null && results.Entities[0].GetAttributeValue<bool>("new_istb") == true)
- ||
- (results.Entities[0].GetAttributeValue<object>("account.new_istb") != null && (bool)((AliasedValue)results.Entities[0].GetAttributeValue<object>("account.new_istb")).Value == true))
- {
- IsTB = true;
- }
- else
- {
- IsTB = false;
- }
- // Check Role Type (Computer)
- if (results.Entities[0].GetAttributeValue<OptionSetValue>("new_roletype") != null)
- {
- OptionSetValue roleType = results.Entities[0].GetAttributeValue<OptionSetValue>("new_roletype");
- if (roleType.Value == 100000000 || roleType.Value == 100000001 || roleType.Value == 100000002)
- {
- if (!errorMessage.Contains("- Father / Mother / Guardian cannot order computer items."))
- errorMessage += "- Father / Mother / Guardian cannot order computer items.\n";
- canPlaceComputerOrder = false;
- }
- }
- // Check Role Type (Computer) Finish
- // Check Order within 3 years (Computer) & 1 year (Internet)
- foreach (Entity tmpEntity in results.Entities)
- {
- // Computer
- DateTime orderDate = new DateTime();
- if (tmpEntity.GetAttributeValue<AliasedValue>("orderitemcomputer.new_purchasedate") != null &&
- tmpEntity.GetAttributeValue<AliasedValue>("product.productid") != null)
- {
- orderDate = ((DateTime)(tmpEntity.GetAttributeValue<AliasedValue>("orderitemcomputer.new_purchasedate").Value)).ToLocalTime();
- /* modified by Justina
- DateTime threeYearsAgo = DateTime.Today.AddYears(-3); */
- DateTime threeYearsAgo = DateTime.Now.ToUniversalTime().AddYears(-3);
- if (orderDate > threeYearsAgo)
- {
- if (!errorMessage.Contains("- Cannot order computer items within 3 years."))
- errorMessage += "- Cannot order computer items within 3 years.\n";
- canPlaceComputerOrder = false;
- //break;
- }
- }
- // Internet Service
- orderDate = new DateTime();
- if (tmpEntity.GetAttributeValue<AliasedValue>("allinternetorders.new_orderdate") != null
- && tmpEntity.GetAttributeValue<AliasedValue>("allinternetorders.new_servicetype") != null
- && tmpEntity.GetAttributeValue<AliasedValue>("allinternetorders_product.productid") != null)
- {
- OptionSetValue serviceTypeOption = (OptionSetValue)(tmpEntity.GetAttributeValue<AliasedValue>("allinternetorders.new_servicetype").Value);
- orderDate = ((DateTime)(tmpEntity.GetAttributeValue<AliasedValue>("allinternetorders.new_orderdate").Value)).ToLocalTime();
- /* modified by justina
- DateTime oneYearAgo = DateTime.Today.AddYears(-1);
- DateTime threeYearsAgo = DateTime.Today.AddYears(-3); */
- DateTime oneYearAgo = DateTime.Now.ToUniversalTime().AddYears(-1);
- DateTime threeYearsAgo = DateTime.Now.ToUniversalTime().AddYears(-3);
- if (orderDate > threeYearsAgo)
- {
- if (!errorMessage.Contains("- Cannot order BBW/BBM items or other family members placed BBW/BBM items within 3 years."))
- errorMessage += "- Cannot order BBW/BBM items or other family members placed BBW/BBM items within 3 years.\n";
- canPlaceBBWInternetOrder = false;
- canPlaceBBMInternetOrder = false;
- }
- /*if (orderDate > oneYearAgo && serviceTypeOption.Value == 6)
- {
- if (!errorMessage.Contains("- Cannot order BBW internet items or other family members placed BBW internet items within 1 year"))
- errorMessage += "- Cannot order BBW internet items or other family members placed BBW internet items within 1 year\n";
- canPlaceBBWInternetOrder = false;
- //break;
- }
- else if (serviceTypeOption.Value == 100000000)
- {
- PlacedBBMInternetOrder = true;
- }*/
- }
- }
- // Check Order within 3 years (Computer) & 1 year (Internet) Finish
- // Check isTB & Age<18 & Has Main Contact (Computer)
- if (IsTB && results.Entities[0].GetAttributeValue<Object>("new_age") != null && results.Entities[0].GetAttributeValue<AliasedValue>("account.primarycontactid") == null)
- {
- Decimal age = results.Entities[0].GetAttributeValue<Decimal>("new_age");
- if (age < 18)
- {
- if (!errorMessage.Contains("- TB member with Age < 18 without Main Contact cannot order computer items."))
- errorMessage += "- TB member with Age < 18 without Main Contact cannot order computer items.\n";
- canPlaceComputerOrder = false;
- }
- }
- // Check isTB & Age<18 & Has Main Contact (Computer) Finish
- // Check Age<18 & Has Main Contact (Internet)
- if (results.Entities[0].GetAttributeValue<Object>("new_age") != null && results.Entities[0].GetAttributeValue<AliasedValue>("account.primarycontactid") == null)
- {
- Decimal age = results.Entities[0].GetAttributeValue<Decimal>("new_age");
- if (age < 18)
- {
- if (!errorMessage.Contains("- TB member with Age < 18 without Main Contact cannot order internet services."))
- errorMessage += "- TB member with Age < 18 without Main Contact cannot order internet services.\n";
- canPlaceBBWInternetOrder = false;
- canPlaceBBMInternetOrder = false;
- }
- }
- // Check Age<18 & Has Main Contact (Internet) Finish
- if (errorMessage != null && errorMessage != "")
- {
- //this.Dispatcher.BeginInvoke(() =>
- //{
- MessageBox.Show(errorMessage);
- return;
- //});
- }
- ProductList();
- });
- }
- }
- catch (Exception ex)
- {
- this.ReportError(ex);
- }
- }
- private void LookupContact_LookupControlOnUserChangeValue(object sender, RoutedEventArgs e)
- {
- try
- {
- QueryExpression query = new QueryExpression()
- {
- EntityName = "contact",
- ColumnSet = new ColumnSet()
- {
- Columns = new System.Collections.ObjectModel.ObservableCollection<string>(
- new string[] {
- "new_memberid", "new_istb", "address1_line1", "address1_line2", "address1_line3",
- "new_roletype", "new_age", "telephone1", "telephone2"
- }
- )
- },
- Criteria = new FilterExpression()
- {
- Conditions = new System.Collections.ObjectModel.ObservableCollection<ConditionExpression>
- (new ConditionExpression[] {
- new ConditionExpression(){
- AttributeName = "contactid",
- Operator = ConditionOperator.Equal,
- Values = new System.Collections.ObjectModel.ObservableCollection<object>(
- new object[] {
- LookupContact.Items.First().Id
- }
- )
- }
- })
- },
- Orders = new System.Collections.ObjectModel.ObservableCollection<OrderExpression>(new OrderExpression[]
- {
- new OrderExpression() { AttributeName = "new_memberid", OrderType = OrderType.Ascending }
- })
- };
- //query.PageInfo = new PagingInfo { Count = MaxRecordsToReturn, PageNumber = 1, PagingCookie = null };
- LinkEntity le = new LinkEntity();
- le.LinkFromEntityName = "contact";
- le.LinkFromAttributeName = "parentcustomerid";
- le.LinkToEntityName = "account";
- le.LinkToAttributeName = "accountid";
- le.JoinOperator = JoinOperator.LeftOuter;
- le.EntityAlias = "account";
- le.Columns = new ColumnSet()
- {
- Columns = new System.Collections.ObjectModel.ObservableCollection<string>(new string[] {
- "accountid", "new_istb", "primarycontactid"
- })
- };
- // Get all contacts from same account and the corresponding orderiteminternetservice records
- LinkEntity le1a = new LinkEntity();
- le1a.LinkFromEntityName = "account";
- le1a.LinkFromAttributeName = "accountid";
- le1a.LinkToEntityName = "contact";
- le1a.LinkToAttributeName = "parentcustomerid";
- le1a.JoinOperator = JoinOperator.LeftOuter;
- le1a.EntityAlias = "allcontacts";
- le1a.Columns = new ColumnSet()
- {
- Columns = new System.Collections.ObjectModel.ObservableCollection<string>(new string[] {
- "parentcustomerid"
- })
- };
- LinkEntity le1b = new LinkEntity();
- le1b.LinkFromEntityName = "contact";
- le1b.LinkFromAttributeName = "contactid";
- le1b.LinkToEntityName = "salesorder";
- le1b.LinkToAttributeName = "customerid";
- le1b.JoinOperator = JoinOperator.LeftOuter;
- le1b.EntityAlias = "allorders";
- le1b.Columns = new ColumnSet()
- {
- Columns = new System.Collections.ObjectModel.ObservableCollection<string>(new string[] {
- "customerid", "createdon"
- })
- };
- le1b.LinkCriteria = new FilterExpression()
- {
- Conditions = new System.Collections.ObjectModel.ObservableCollection<ConditionExpression>(
- new ConditionExpression[] {
- new ConditionExpression(){
- AttributeName = "statecode",
- Operator = ConditionOperator.Equal,
- Values = new System.Collections.ObjectModel.ObservableCollection<object>(
- new object[] { 0 }
- )
- }
- }),
- };
- /*le1b.LinkCriteria.Filters.Add(
- new FilterExpression()
- {
- Conditions = new System.Collections.ObjectModel.ObservableCollection<ConditionExpression>(
- new ConditionExpression[] {
- new ConditionExpression(){
- AttributeName = "new_advanceterminationdate",
- Operator = ConditionOperator.Null
- }
- })
- });*/
- le1a.LinkEntities.Add(le1b);
- LinkEntity le1c = new LinkEntity();
- le1c.LinkFromEntityName = "salesorder";
- le1c.LinkFromAttributeName = "salesorderid";
- le1c.LinkToEntityName = "new_orderiteminternetservice";
- le1c.LinkToAttributeName = "new_orderidid";
- le1c.JoinOperator = JoinOperator.LeftOuter;
- le1c.EntityAlias = "allinternetorders";
- le1c.Columns = new ColumnSet()
- {
- Columns = new System.Collections.ObjectModel.ObservableCollection<string>(new string[] {
- "createdon", "new_servicetype", "new_orderdate"
- })
- };
- le1c.LinkCriteria = new FilterExpression()
- {
- Conditions = new System.Collections.ObjectModel.ObservableCollection<ConditionExpression>(
- new ConditionExpression[] {
- new ConditionExpression(){
- AttributeName = "statecode",
- Operator = ConditionOperator.Equal,
- Values = new System.Collections.ObjectModel.ObservableCollection<object>(
- new object[] { 0 }
- )
- },
- new ConditionExpression(){
- AttributeName = "new_advanceterminationdate",
- Operator = ConditionOperator.Null
- }
- }),
- FilterOperator = LogicalOperator.And
- };
- LinkEntity le_AllInternetOrders_Product = new LinkEntity();
- le_AllInternetOrders_Product.LinkFromEntityName = "new_orderiteminternetservice";
- le_AllInternetOrders_Product.LinkFromAttributeName = "new_productidid";
- le_AllInternetOrders_Product.LinkToEntityName = "product";
- le_AllInternetOrders_Product.LinkToAttributeName = "productid";
- le_AllInternetOrders_Product.JoinOperator = JoinOperator.LeftOuter;
- le_AllInternetOrders_Product.EntityAlias = "allinternetorders_product";
- le_AllInternetOrders_Product.Columns = new ColumnSet()
- {
- Columns = new System.Collections.ObjectModel.ObservableCollection<string>(new string[] {
- "productid", "new_type"
- })
- };
- le_AllInternetOrders_Product.LinkCriteria = new FilterExpression()
- {
- Conditions = new System.Collections.ObjectModel.ObservableCollection<ConditionExpression>(
- new ConditionExpression[] {
- new ConditionExpression(){
- AttributeName = "new_wifiservice",
- Operator = ConditionOperator.Equal,
- Values = new System.Collections.ObjectModel.ObservableCollection<object>(
- new object[] {false}
- )
- },
- new ConditionExpression(){
- AttributeName = "new_wifiservice",
- Operator = ConditionOperator.Null
- }
- })
- , FilterOperator = LogicalOperator.Or
- };
- le1c.LinkEntities.Add(le_AllInternetOrders_Product);
- le1b.LinkEntities.Add(le1c);
- // Get all contacts from same account and the corresponding orderiteminternetservice records Finish
- le.LinkEntities.Add(le1a);
- LinkEntity le2 = new LinkEntity();
- le2.LinkFromEntityName = "contact";
- le2.LinkFromAttributeName = "contactid";
- le2.LinkToEntityName = "salesorder";
- le2.LinkToAttributeName = "customerid";
- le2.JoinOperator = JoinOperator.LeftOuter;
- le2.EntityAlias = "order";
- le2.Columns = new ColumnSet()
- {
- Columns = new System.Collections.ObjectModel.ObservableCollection<string>(new string[] {
- "customerid", "createdon"
- })
- };
- le2.LinkCriteria = new FilterExpression()
- {
- Conditions = new System.Collections.ObjectModel.ObservableCollection<ConditionExpression>(
- new ConditionExpression[] {
- new ConditionExpression(){
- AttributeName = "statecode",
- Operator = ConditionOperator.Equal,
- Values = new System.Collections.ObjectModel.ObservableCollection<object>(
- new object[] { 0 }
- )
- }
- })
- };
- LinkEntity le3 = new LinkEntity();
- le3.LinkFromEntityName = "salesorder";
- le3.LinkFromAttributeName = "salesorderid";
- le3.LinkToEntityName = "new_orderitemcomputer";
- le3.LinkToAttributeName = "new_orderidid";
- le3.JoinOperator = JoinOperator.LeftOuter;
- le3.EntityAlias = "orderitemcomputer";
- le3.Columns = new ColumnSet()
- {
- Columns = new System.Collections.ObjectModel.ObservableCollection<string>(new string[] {
- "new_orderidid", "createdon", "new_purchasedate"
- })
- };
- LinkEntity le_OrderItemComputer_Product = new LinkEntity();
- le_OrderItemComputer_Product.LinkFromEntityName = "new_orderitemcomputer";
- le_OrderItemComputer_Product.LinkFromAttributeName = "new_productidid";
- le_OrderItemComputer_Product.LinkToEntityName = "product";
- le_OrderItemComputer_Product.LinkToAttributeName = "productid";
- le_OrderItemComputer_Product.JoinOperator = JoinOperator.LeftOuter;
- le_OrderItemComputer_Product.EntityAlias = "product";
- le_OrderItemComputer_Product.Columns = new ColumnSet()
- {
- Columns = new System.Collections.ObjectModel.ObservableCollection<string>(new string[] {
- "productid", "new_type"
- })
- };
- le_OrderItemComputer_Product.LinkCriteria = new FilterExpression()
- {
- Conditions = new System.Collections.ObjectModel.ObservableCollection<ConditionExpression>(
- new ConditionExpression[] {
- new ConditionExpression(){
- AttributeName = "new_type",
- Operator = ConditionOperator.In,
- Values = new System.Collections.ObjectModel.ObservableCollection<object>(
- new object[] { 1, 2, 3, 5 }
- )
- }
- })
- };
- le3.LinkEntities.Add(le_OrderItemComputer_Product);
- le2.LinkEntities.Add(le3);
- LinkEntity le4 = new LinkEntity();
- le4.LinkFromEntityName = "salesorder";
- le4.LinkFromAttributeName = "salesorderid";
- le4.LinkToEntityName = "new_orderiteminternetservice";
- le4.LinkToAttributeName = "new_orderidid";
- le4.JoinOperator = JoinOperator.LeftOuter;
- le4.EntityAlias = "orderiteminternetservice";
- le4.Columns = new ColumnSet()
- {
- Columns = new System.Collections.ObjectModel.ObservableCollection<string>(new string[] {
- "new_orderidid", "createdon", "new_servicetype"
- })
- };
- le2.LinkEntities.Add(le4);
- query.LinkEntities = new System.Collections.ObjectModel.ObservableCollection<LinkEntity>(
- new LinkEntity[] { le, le2 }
- );
- OrganizationRequest request = new OrganizationRequest() { RequestName = "RetrieveMultiple" };
- request["Query"] = query;
- IOrganizationService service = SilverlightUtility.GetSoapService();
- Guid sysAdmin = SilverlightUtility.GetAdminUserGuid();
- OrganizationServiceClient serviceWithCRMAdminRight = (OrganizationServiceClient)SilverlightUtility.GetSoapService();
- using (var scope = new OperationContextScope(serviceWithCRMAdminRight.InnerChannel))
- {
- var ns = "http://schemas.microsoft.com/xrm/2011/Contracts";
- var userHeader = MessageHeader.CreateHeader("CallerId", ns, sysAdmin);
- OperationContext.Current.OutgoingMessageHeaders.Add(userHeader);
- //service.BeginExecute(request, new AsyncCallback(MembershipNo_ClickCallback), service);
- var memberRetrieveTask = SilverlightUtility.CreateRetrieveMultipleTask(serviceWithCRMAdminRight, query);
- Task.Factory.ContinueWhenAll(new Task[] { memberRetrieveTask },
- completedTasks =>
- {
- MemberRetrieve(serviceWithCRMAdminRight, memberRetrieveTask.Result);
- }
- );
- }
- }
- catch (Exception ex)
- {
- this.ReportError(ex);
- }
- }
- private void ButtonSave_Click(object sender, RoutedEventArgs e)
- {
- if (LookupContact.Items.Count == 0 || TextBoxMembershipNumber.Text == "")
- {
- //MessageBox.Show("You must provide a value for Student Name");
- SilverlightMessageBoxLibrary.Message.ErrorMessage("You must provide a value for Student Name");
- return;
- }
- SelectedMembershipNumber = TextBoxMembershipNumber.Text;
- bool isInternetService = false;
- bool isComputer = false;
- int selectedCount = 0;
- foreach (Product product in OCProducts)
- {
- if (product.IsSelect)
- {
- if (product.KitProducts.Count == 0) // Not Kit product
- {
- if (product.ProductType.Value == 1) isComputer = true;
- else if (product.ProductType.Value == 2) isInternetService = true;
- }
- else
- {
- foreach (Product kitProduct in product.KitProducts)
- {
- if (kitProduct.ProductType.Value == 1) isComputer = true;
- else if (kitProduct.ProductType.Value == 2) isInternetService = true;
- }
- }
- selectedCount++;
- }
- }
- if (selectedCount == 0)
- {
- SilverlightMessageBoxLibrary.Message.ErrorMessage("You must at least select one Product/Kit Product");
- return;
- }
- int deliveryMethodCount = 0;
- if (CheckBoxDeliverToHome.IsChecked == true) deliveryMethodCount++;
- if (CheckBoxCollectatSchool.IsChecked == true) deliveryMethodCount++;
- if (CheckBoxCollectatCentres.IsChecked == true) deliveryMethodCount++;
- if (isComputer == true && deliveryMethodCount == 0)
- {
- SilverlightMessageBoxLibrary.Message.ErrorMessage("You must select the Delivery Method for Computer Product");
- return;
- }
- else if (isComputer == true && deliveryMethodCount > 1)
- {
- SilverlightMessageBoxLibrary.Message.ErrorMessage("You cannot select more than one Delivery Method for Computer Product");
- return;
- }
- else if (isInternetService == true && isComputer == false && deliveryMethodCount >= 1)
- {
- SilverlightMessageBoxLibrary.Message.ErrorMessage("Please unselect all Delivery Method for Internet Service Order");
- return;
- }
- //installmentYear = 1;
- Entity newOrderRecord = new Entity();
- newOrderRecord.LogicalName = "salesorder";
- //newOrderRecord["name"] = "Order";
- ContactGUID = LookupContact.Items.First().Id;
- string contactName = LookupContact.Items.First().Name;
- newOrderRecord["customerid"] = new EntityReference() { Id = ContactGUID, LogicalName = "contact" };
- newOrderRecord["pricelevelid"] = new EntityReference() { Id = PriceListGuid, LogicalName = "pricelevel" };
- IsOneOffPayment = CheckBoxOneoffPayment.IsChecked.Value;
- newOrderRecord["new_oneoffpayment"] = IsOneOffPayment;
- newOrderRecord["new_delivertohome"] = CheckBoxDeliverToHome.IsChecked;
- newOrderRecord["new_collectatschool"] = CheckBoxCollectatSchool.IsChecked;
- newOrderRecord["new_collectatcentres"] = CheckBoxCollectatCentres.IsChecked;
- if (CheckBoxDeliverToHome.IsChecked == true || isInternetService == true)
- {
- newOrderRecord["billto_name"] = newOrderRecord["shipto_name"] = contactName;
- newOrderRecord["billto_line1"] = newOrderRecord["shipto_line1"] = address1;
- newOrderRecord["billto_line2"] = newOrderRecord["shipto_line2"] = address2;
- newOrderRecord["billto_line3"] = newOrderRecord["shipto_line3"] = address3;
- newOrderRecord["billto_telephone"] = newOrderRecord["shipto_telephone"] = telephone1;
- }
- if (isInternetService == true)
- {
- OptionSetValue timeSlot = new OptionSetValue();
- if (TimeSlot1.IsChecked == true) timeSlot.Value = 100000000;
- if (TimeSlot2.IsChecked == true) timeSlot.Value = 100000001;
- if (TimeSlot3.IsChecked == true) timeSlot.Value = 100000002;
- if (TimeSlot4.IsChecked == true) timeSlot.Value = 100000003;
- newOrderRecord["new_preferredtimeslotforhomebroadbandinstalla"] = timeSlot;
- }
- Total = System.Convert.ToDecimal(TextBoxTotal.Text);
- newOrderRecord["new_totalamount"] = new Money() { Value = Total };
- ButtonSave.IsEnabled = false;
- LookupContact.IsEnabled = false;
- IOrganizationService service = SilverlightUtility.GetSoapService();
- //service.BeginCreate(newOrderRecord, new AsyncCallback(OrderAdd_Callback), service);
- Task<Guid> addOrderTask = SilverlightUtility.CreateEntityRecordTask(service, newOrderRecord);
- Task.Factory.ContinueWhenAll(new Task[] { addOrderTask },
- completedTasks =>
- {
- OrderAdd(service, addOrderTask.Result);
- }
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement