public ShipmentItem(model.ShipmentItem item = null) { if (item != null) { this.InjectFrom(item); var propInfo = item.GetType().FindPropertiesWithAttribute(typeof(KeyAttribute)).First(); ShipmentItemId = propInfo.GetValue(item) as string ?? Guid.NewGuid().ToString(); ItemCode = item.LineItem != null ? item.LineItem.CatalogItemCode : null; ItemName = item.LineItem != null ? item.LineItem.DisplayName : null; UnitPrice = item.LineItem != null ? item.LineItem.PlacedPrice : 0; ShippingAddress = item.Shipment != null && item.Shipment.OrderForm != null?item.Shipment.OrderForm.OrderGroup.OrderAddresses.Where(address => address.Name == "Shipping").FirstOrDefault().ToString() : string.Empty; BillingAddress = item.Shipment != null && item.Shipment.OrderForm != null?item.Shipment.OrderForm.OrderGroup.OrderAddresses.Where(address => address.Name == "Billing").FirstOrDefault().ToString() : string.Empty; Customer = item.Shipment != null && item.Shipment.OrderForm != null ? item.Shipment.OrderForm.OrderGroup.CustomerName : string.Empty; Order = item.Shipment != null && item.Shipment.OrderForm != null ? ((Order)item.Shipment.OrderForm.OrderGroup).TrackingNumber : string.Empty; OrderDate = item.Shipment != null && item.Shipment.OrderForm != null ? item.Shipment.OrderForm.OrderGroup.Created : null; ShippingTaxTotal = item.Shipment != null ? item.Shipment.ShippingTaxTotal : 0; ItemTaxTotal = item.Shipment != null ? item.Shipment.ItemTaxTotal : 0; ItemSubtotal = item.Shipment != null ? item.Shipment.ItemSubtotal : 0; Subtotal = item.Shipment != null ? item.Shipment.Subtotal : 0; TotalBeforeTax = item.Shipment != null ? item.Shipment.TotalBeforeTax : 0; BillingCurrency = item.Shipment != null && item.Shipment.OrderForm != null ? item.Shipment.OrderForm.OrderGroup.BillingCurrency : string.Empty; ShippingTotal = item.Shipment != null ? item.Shipment.ShipmentTotal : 0; DiscountAmount = item.Shipment != null ? item.Shipment.ShippingDiscountAmount : 0; } }
private void SplitForm(OrderForm form) { foreach (var item in form.LineItems) { Shipment itemShipment = null; // Find appropriate shipment for item foreach (var shipment in form.Shipments) { if (shipment.ShippingMethodId == item.ShippingMethodId && string.CompareOrdinal(shipment.ShippingAddressId, item.ShippingAddressId) == 0 && string.Compare(shipment.FulfillmentCenterId, item.FulfillmentCenterId, StringComparison.OrdinalIgnoreCase) == 0) { // we found out match, exit itemShipment = shipment; //break; } else { // if shipment contains current LineItem, remove it from the shipment RemoveLineItemFromShipment(shipment, item.LineItemId); } } // did we find any shipment? if (itemShipment == null) { itemShipment = new Shipment { ShippingAddressId = item.ShippingAddressId, ShippingMethodId = item.ShippingMethodId, ShippingMethodName = item.ShippingMethodName, FulfillmentCenterId = item.FulfillmentCenterId, OrderForm = form }; form.Shipments.Add(itemShipment); } // Add item to the shipment //if (item.LineItemId == 0) // throw new ArgumentNullException("LineItemId = 0"); RemoveLineItemFromShipment(itemShipment, item.LineItemId); var link = new ShipmentItem { LineItemId = item.LineItemId, LineItem = item, Quantity = item.Quantity, ShipmentId = itemShipment.ShipmentId }; itemShipment.ShipmentItems.Add(link); } }
[RepositoryTheory] // fixed be setting LineItemId explicitly public void Can_add_shipmentitem() { var order = TestOrderBuilder.BuildOrder() .WithAddresess() .WithShipment() .GetOrder(); var repository = GetRepository(); repository.Add(order); repository.UnitOfWork.Commit(); RefreshRepository(ref repository); order = repository.Orders .ExpandAll() .Expand("RmaRequests/RmaReturnItems/RmaLineItems/LineItem") .Expand("RmaRequests/Order") .Expand("OrderForms/Shipments/OrderForm/OrderGroup") .Expand("OrderForms/Shipments/ShipmentItems/LineItem") .Expand("OrderForms/Shipments/ShipmentItems/Shipment/OrderForm/OrderGroup") .FirstOrDefault(); // adding new LineItem first var newLineItem = new LineItem { CatalogItemId = "ItemId", CatalogItemCode = "ItemCode", Quantity = 1, PlacedPrice = 10, TaxTotal = 1 }; order.OrderForms[0].LineItems.Add(newLineItem); // adding new ShipmentItem var item = new ShipmentItem(); item.LineItem = newLineItem; item.LineItemId = item.LineItem.LineItemId; // this line shouldn't be necessary. But it is. //item.Shipment = order.OrderForms[0].Shipments[0]; // this line of code was causing exception in original code order.OrderForms[0].Shipments[0].ShipmentItems.Add(item); repository.UnitOfWork.Commit(); RefreshRepository(ref repository); order = repository.Orders .ExpandAll() .Expand("OrderForms/Shipments/ShipmentItems") .Expand("OrderForms/LineItems") .FirstOrDefault(); Assert.True(order.OrderForms[0].LineItems.Any(x => x.LineItemId == newLineItem.LineItemId)); Assert.True(order.OrderForms[0].Shipments[0].ShipmentItems.Any(x => x.LineItemId == newLineItem.LineItemId)); }
public void Initialize(ShipmentItem originalItem) { ItemDisplayName = originalItem.LineItem.DisplayName; MaxQuantity = originalItem.Quantity; }
private void RaiseRemoveLineItemInteractionRequest(ShipmentItem removingItem) { var itemVM = _lineItemVmFactory.GetViewModelInstance(); itemVM.Initialize(removingItem); CommonShipmentConfirmRequest.Raise( new ConditionalConfirmation { Title = "Item removal details", Content = itemVM }, (x) => { if (x.Confirmed) { // removing is moving item to fake shipment var fakeShipment = new Shipment { OrderForm = CurrentShipment.OrderForm }; _currentOrder.MoveShippingItem(removingItem, itemVM.Quantity, fakeShipment, CurrentShipment); // update LineItem Quantity or remove it completely var lineItem = removingItem.LineItem; if (lineItem.Quantity > itemVM.Quantity) { lineItem.Quantity -= itemVM.Quantity; } else { _currentOrder.OrderForms[0].LineItems.Remove(lineItem); } ParentViewModel.Recalculate(); } }); }
private void RaiseMoveLineItemInteractionRequest(ShipmentItem movingItem) { if (movingItem != null) { var itemVM = _splitVmFactory.GetViewModelInstance( new KeyValuePair<string, object>("currentOrder", _currentOrder.InnerItem) , new KeyValuePair<string, object>("sourceShipment", CurrentShipment) , new KeyValuePair<string, object>("movingItem", movingItem)); var confirmation = new ConditionalConfirmation { Title = "Split shipment", Content = itemVM }; CommonShipmentConfirmRequest.Raise(confirmation, (x) => { if (x.Confirmed) { var quantity = itemVM.MoveQuantity; var targetShipment = itemVM.TargetShipment; var anyOperationSucceded = false; try { ParentViewModel.CloseAllSubscription1(); _currentOrder.MoveShippingItem(movingItem, quantity, targetShipment, CurrentShipment); if (itemVM.IsNewShipmentSelected) { targetShipment.ShipmentId = targetShipment.GenerateNewKey(); targetShipment.OrderFormId = _currentOrder.OrderForms[0].OrderFormId; _currentOrder.OrderForms[0].Shipments.Add(targetShipment); } ParentViewModel.OrderShipmentViewModels.Clear(); ParentViewModel.InitializeOrderShipmentViewModels(); ParentViewModel.Recalculate(); anyOperationSucceded = true; } finally { ParentViewModel.SetAllSubscription1(); // fake assignment for triggers to fire if (anyOperationSucceded) ParentViewModel.InnerItem.OrderGroupId = ParentViewModel.InnerItem.OrderGroupId; } RaiseCanExecuteChanged(); } }); } }
public void MoveShippingItem(ShipmentItem item, decimal quantity, Shipment targetShipment, Shipment sourceShipment) { if (item == null) { throw new ArgumentNullException("item"); } if (targetShipment == null) { throw new ArgumentNullException("targetShipment"); } // prevent removing last Shipment if (item.Quantity == quantity && sourceShipment.ShipmentItems.Count == 1 && _innerItem.OrderForms[0].Shipments.Count == 1) { throw new OperationCanceledException("Can't remove last item. Update Shipment instead"); } item.Quantity -= quantity; if (item.Quantity <= 0) { // remove empty ShipmentItem, Shipment sourceShipment.ShipmentItems.Remove(item); if (sourceShipment.ShipmentItems.Count == 0) { _innerItem.OrderForms[0].Shipments.Remove(sourceShipment); } } var targetShipmentItem = targetShipment.ShipmentItems.FirstOrDefault(x => x.LineItemId == item.LineItemId); if (targetShipmentItem == null) { targetShipmentItem = _entityFactory.CreateEntityForType(typeof(ShipmentItem)) as ShipmentItem; targetShipmentItem.LineItemId = item.LineItemId; targetShipmentItem.LineItem = item.LineItem; targetShipmentItem.ShipmentId = targetShipment.ShipmentId; //targetShipmentItem.Shipment = targetShipment; targetShipment.ShipmentItems.Add(targetShipmentItem); } targetShipmentItem.Quantity += quantity; }
//[RepositoryTheory(Skip = "One or both of the ends of the relationship is in the added state.")] /// <summary> /// problem while creating the second ShipmentItem in a new Shipment. /// </summary> public void Can_create_shipment_with_2shipmentitems() { var order = TestOrderBuilder.BuildOrder() .WithAddresess() .WithShipment() .WithLineItemsCount(2).GetOrder(); var repository = GetRepository(); repository.Add(order); repository.UnitOfWork.Commit(); RefreshRepository(ref repository); order = repository.Orders.ExpandAll() .Where(x => x.OrderGroupId == order.OrderGroupId) .ExpandAll() .Expand("RmaRequests/RmaReturnItems/RmaLineItems/LineItem") .Expand("RmaRequests/Order") .Expand("OrderForms/OrderGroup") .Expand("OrderForms/Shipments/ShipmentItems/LineItem") .SingleOrDefault(); //adding new Shipment var targetShipment = new Shipment(); //adding new ShipmentItem 1 var movingShipmentItem = order.OrderForms[0].Shipments[0].ShipmentItems[0]; movingShipmentItem.Quantity--; var targetShipmentItem = new ShipmentItem(); targetShipmentItem.Quantity = 1; targetShipmentItem.LineItemId = movingShipmentItem.LineItemId; targetShipmentItem.LineItem = movingShipmentItem.LineItem; targetShipmentItem.ShipmentId = targetShipment.ShipmentId; targetShipment.ShipmentItems.Add(targetShipmentItem); targetShipment.ShipmentId = targetShipment.GenerateNewKey(); targetShipment.OrderFormId = order.OrderForms[0].OrderFormId; order.OrderForms[0].Shipments.Add(targetShipment); //adding new ShipmentItem 2 movingShipmentItem = order.OrderForms[0].Shipments[0].ShipmentItems[1]; movingShipmentItem.Quantity--; targetShipmentItem = new ShipmentItem(); targetShipmentItem.Quantity = 1; targetShipmentItem.LineItemId = movingShipmentItem.LineItemId; targetShipmentItem.LineItem = movingShipmentItem.LineItem; // commenting out this single line makes the test to pass targetShipmentItem.ShipmentId = targetShipment.ShipmentId; targetShipment.ShipmentItems.Add(targetShipmentItem); repository.UnitOfWork.Commit(); RefreshRepository(ref repository); order = repository.Orders.ExpandAll() .Where(x => x.OrderGroupId == order.OrderGroupId) .ExpandAll() .Expand("RmaRequests/RmaReturnItems/RmaLineItems/LineItem") .Expand("RmaRequests/Order") .Expand("OrderForms/OrderGroup") .Expand("OrderForms/Shipments/ShipmentItems/LineItem") .SingleOrDefault(); Assert.NotNull(order); Assert.Equal(2, order.OrderForms[0].Shipments.Count); Assert.Equal(2, order.OrderForms[0].Shipments[0].ShipmentItems.Count); Assert.Equal(2, order.OrderForms[0].Shipments[1].ShipmentItems.Count); }
public void Can_add_collection_item_items() { var order = TestOrderBuilder.BuildOrder().WithLineItemsCount(1).GetOrder(); var repository = GetRepository(); repository.Add(order); repository.UnitOfWork.Commit(); order = repository.Orders.ExpandAll().FirstOrDefault(); // adding new ShipmentItem var item = new ShipmentItem(); item.Quantity = 1; item.LineItem = order.OrderForms[0].LineItems[0]; // adding new Shipment var newShipment = new Shipment(); newShipment.ShipmentItems.Add(item); order.OrderForms[0].Shipments.Add(newShipment); repository.UnitOfWork.Commit(); }