protected void SaveButton_Click(object sender, EventArgs e) { // UPDATE ADDRESS _OrderShipment.ShipToFirstName = ShipToFirstName.Text; _OrderShipment.ShipToLastName = ShipToLastName.Text; _OrderShipment.ShipToAddress1 = ShipToAddress1.Text; _OrderShipment.ShipToAddress2 = ShipToAddress2.Text; _OrderShipment.ShipToCity = ShipToCity.Text; _OrderShipment.ShipToProvince = ShipToProvince.Text; _OrderShipment.ShipToPostalCode = ShipToPostalCode.Text; _OrderShipment.ShipToCountryCode = ShipToCountryCode.SelectedValue; _OrderShipment.ShipToPhone = ShipToPhone.Text; _OrderShipment.ShipToCompany = ShipToCompany.Text; _OrderShipment.ShipToFax = StringHelper.StripHtml(ShipToFax.Text); _OrderShipment.ShipToResidence = (ShipToResidence.SelectedIndex == 0); int shipgwId = AlwaysConvert.ToInt(ShipGateway.SelectedValue); string trackingData = TrackingNumber.Text; if (!string.IsNullOrEmpty(trackingData)) { TrackingNumber tnum = new TrackingNumber(); tnum.TrackingNumberData = trackingData; tnum.ShipGatewayId = shipgwId; tnum.OrderShipmentId = _OrderShipment.Id; _OrderShipment.TrackingNumbers.Add(tnum); } // UPDATE OTHER DETAILS _OrderShipment.WarehouseId = AlwaysConvert.ToInt(ShipFrom.SelectedValue); _OrderShipment.ShipMessage = ShipMessage.Text; _OrderShipment.Save(); //DELETE ANY ITEMS WITH A ZERO QUANTITY for (int i = _OrderShipment.OrderItems.Count - 1; i >= 0; i--) { if (_OrderShipment.OrderItems[i].Quantity < 1) { _OrderShipment.OrderItems.DeleteAt(i); } } foreach (GridViewRow gvr in TrackingGrid.Rows) { TextBox trackingNumberData = (TextBox)gvr.Cells[0].FindControl("TrackingNumberData"); int trackingNumberId = AlwaysConvert.ToInt(TrackingGrid.DataKeys[gvr.RowIndex].Value); TrackingNumber trackingNumber = TrackingNumberDataSource.Load(trackingNumberId); if (trackingNumber != null && trackingNumberData != null) { trackingNumber.TrackingNumberData = trackingNumberData.Text; trackingNumber.Save(); Response.Write("."); } } //REDIRECT TO SHIPMENT PAGE Response.Redirect(CancelLink.NavigateUrl); }
protected void UpdateButton_Click(object sender, EventArgs e) { bool itemFound = false; OrderShipment newShipment = null; OrderShipment moveShipment = null; foreach (GridViewRow row in ShipmentItems.Rows) { HiddenField hf = (HiddenField)row.FindControl("Id"); int orderItemId = AlwaysConvert.ToInt(hf.Value); int index = _OrderShipment.OrderItems.IndexOf(orderItemId); if (index > -1) { TextBox tb = (TextBox)row.FindControl("MoveQty"); short qty = AlwaysConvert.ToInt16(tb.Text); DropDownList ddl = (DropDownList)row.FindControl("Shipment"); string selectedShipment = Request.Form[ddl.UniqueID]; if ((qty > 0) && (!string.IsNullOrEmpty(selectedShipment))) { OrderItem orderItem = _OrderShipment.OrderItems[index]; itemFound = true; int shipmentId = AlwaysConvert.ToInt(selectedShipment); moveShipment = OrderShipmentDataSource.Load(shipmentId); if (moveShipment == null) { if (newShipment == null) { newShipment = _OrderShipment.Copy(); newShipment.Save(); } moveShipment = newShipment; } if (qty < orderItem.Quantity) { //SPLIT PART OF THIS ITEM TO ANOTHER SHIPMENT OrderItem splitItem = OrderItem.Copy(orderItem.Id, false); splitItem.Quantity = qty; splitItem.OrderShipmentId = moveShipment.Id; splitItem.Save(); if (orderItem.ParentItemId == orderItem.Id) { splitItem.ParentItemId = splitItem.Id; splitItem.Save(); } moveShipment.OrderItems.Add(splitItem); orderItem.Quantity -= qty; orderItem.Save(); } else { //MOVE WHOLE ITEM TO ANOTHER SHIPMENT orderItem.OrderShipmentId = moveShipment.Id; orderItem.Save(); _OrderShipment.OrderItems.RemoveAt(index); } } } } if (itemFound) { Response.Redirect(CancelLink.NavigateUrl); } else { CustomValidator quantityError = new CustomValidator(); quantityError.ErrorMessage = "You must pick at least one item to move."; quantityError.Text = " "; quantityError.IsValid = false; phQuantityValidation.Controls.Add(quantityError); } }
protected void ChangeShipMethodOKButton_Click(object source, EventArgs e) { int shipmentId = AlwaysConvert.ToInt(Request.Form[ChangeShipMethodShipmentId.UniqueID]); int index = _Order.Shipments.IndexOf(shipmentId); if (index > -1) { // WE FOUND THE TARGET SHIPMENT. REMOVE OLD SHIPPING LINE ITEMS OrderShipment shipment = _Order.Shipments[index]; for (int i = shipment.OrderItems.Count - 1; i >= 0; i--) { OrderItemType itemType = shipment.OrderItems[i].OrderItemType; if (itemType == OrderItemType.Shipping || itemType == OrderItemType.Handling) { shipment.OrderItems.DeleteAt(i); } } // SEE IF WE HAVE A NEW SELECTED SHIPMETHOD int shipMethodId = AlwaysConvert.ToInt(Request.Form[NewShipMethod.UniqueID]); ShipMethod shipMethod = ShipMethodDataSource.Load(shipMethodId); if (shipMethod != null) { ShipRateQuote rate = shipMethod.GetShipRateQuote(shipment); if (rate != null) { // ADD NEW SHIPPING LINE ITEMS TO THE ORDER OrderItem shipRateLineItem = new OrderItem(); shipRateLineItem.OrderId = _Order.Id; shipRateLineItem.OrderItemType = OrderItemType.Shipping; shipRateLineItem.OrderShipmentId = shipmentId; shipRateLineItem.Name = shipMethod.Name; shipRateLineItem.Price = rate.Rate; shipRateLineItem.Quantity = 1; shipRateLineItem.TaxCodeId = shipMethod.TaxCodeId; shipRateLineItem.Save(); shipment.OrderItems.Add(shipRateLineItem); if (rate.Surcharge > 0) { shipRateLineItem = new OrderItem(); shipRateLineItem.OrderId = _Order.Id; shipRateLineItem.OrderItemType = OrderItemType.Handling; shipRateLineItem.OrderShipmentId = shipmentId; shipRateLineItem.Name = shipMethod.Name; shipRateLineItem.Price = rate.Surcharge; shipRateLineItem.Quantity = 1; shipRateLineItem.TaxCodeId = shipMethod.TaxCodeId; shipRateLineItem.Save(); shipment.OrderItems.Add(shipRateLineItem); } //Add the Tracking Number ShipGateway shipGateway = shipMethod.ShipGateway; foreach (TrackingNumber tn in shipment.TrackingNumbers) { tn.ShipGateway = shipGateway; } } } // UPDATE THE SHIPMENT WITH NEW METHOD ASSOCIATION shipment.ShipMethodId = shipMethodId; shipment.ShipMethodName = (shipMethod != null ? shipMethod.Name : string.Empty); shipment.Save(); // RELOAD ORDER AND REBIND THE PAGE FOR UPDATED INFO _Order = OrderDataSource.Load(_Order.Id); BindShipmentsGrid(); } }
private void Ship(bool requestTracking) { //WE HAVE TO LOOK FOR ANY ITEMS NOT BEING SHIPPED //BUILD A DICTIONARY OF QUANTITY TO SHIP bool itemFound = false; bool isPartial = false; bool quantityExceeded = false; Dictionary <int, short> quantities = new Dictionary <int, short>(); foreach (GridViewRow row in ShipmentItems.Rows) { HiddenField hf = (HiddenField)row.FindControl("Id"); int orderItemId = AlwaysConvert.ToInt(hf.Value); int index = _OrderShipment.OrderItems.IndexOf(orderItemId); if (index > -1) { TextBox tb = (TextBox)row.FindControl("Quantity"); short qty = AlwaysConvert.ToInt16(tb.Text); itemFound = itemFound || (qty > 0); isPartial = isPartial || (qty < _OrderShipment.OrderItems[index].Quantity); quantityExceeded = quantityExceeded || (qty > _OrderShipment.OrderItems[index].Quantity); quantities.Add(orderItemId, qty); } } if ((itemFound) && (!quantityExceeded)) { try { // start transation to do it in single step AbleContext.Current.Database.BeginTransaction(); //CHECK IF WE ARE NOT SHIPPING ALL OF THE ITEMS if (isPartial) { //AT LEAST ONE ITEM MUST BE MOVED TO A NEW SHIPMENT //CREATE A COPY OF THIS SHIPMENT OrderShipment newShipment = _OrderShipment.Copy(); newShipment.Save(); _Order.Shipments.Add(newShipment); //KEEP TRACK OF ITEMS TO REMOVE FROM THE CURRENT SHIPMENT List <int> removeItems = new List <int>(); //LOOP THE ITEMS AND DECIDE WHICH TO PUT IN THE NEW SHIPMENT foreach (OrderItem item in _OrderShipment.OrderItems) { int searchItemId = (AlwaysConvert.ToInt(item.ParentItemId) == 0) ? item.Id : AlwaysConvert.ToInt(item.ParentItemId); if (quantities.ContainsKey(searchItemId)) { short shipQty = quantities[searchItemId]; if (shipQty != item.Quantity) { if (shipQty > 0) { //WE HAVE TO SPLIT THIS ITEM OrderItem newItem = OrderItem.Copy(item.Id, true); newItem.Quantity = (short)(item.Quantity - shipQty); newItem.OrderShipmentId = newShipment.Id; newItem.Save(); newShipment.OrderItems.Add(newItem); //UPDATE THE CURRENT ITEM item.Quantity = shipQty; item.Save(); } else { //THIS ITEM JUST NEEDS TO BE MOVED item.OrderShipmentId = newShipment.Id; item.Save(); newShipment.OrderItems.Add(item); removeItems.Add(item.Id); } } } } //REMOVE ANY ITEMS THAT WERE MOVED TO ANOTHER SHIPMENT foreach (int id in removeItems) { int delIndex = _OrderShipment.OrderItems.IndexOf(id); if (delIndex > -1) { _OrderShipment.OrderItems.RemoveAt(delIndex); } } } //Add the Tracking Number int shipgwId = AlwaysConvert.ToInt(ShipGateway.SelectedValue); string trackingData = AddTrackingNumber.Text.Trim(); if (!string.IsNullOrEmpty(trackingData)) { TrackingNumber tnum = new TrackingNumber(); tnum.TrackingNumberData = trackingData; tnum.ShipGatewayId = shipgwId; tnum.OrderShipmentId = _OrderShipment.Id; _OrderShipment.TrackingNumbers.Add(tnum); } //SHIP THE CURRENT SHIPMENT _OrderShipment.Ship(requestTracking, LocaleHelper.LocalNow); // end transaction AbleContext.Current.Database.CommitTransaction(); //RETURN TO SHIPMENTS PAGE Response.Redirect(CancelButton.NavigateUrl, false); } catch (Exception ex) { AbleContext.Current.Database.RollbackTransaction(); Logger.Error(string.Format("An error occurred while trying to confirm shipment to provider: {0}", ex.Message), ex); CustomValidator shipError = new CustomValidator(); shipError.Text = "*"; shipError.ErrorMessage = ex.Message; shipError.IsValid = false; phValidation.Controls.Add(shipError); } } else { CustomValidator quantityError = new CustomValidator(); if (quantityExceeded) { quantityError.ErrorMessage = "You cannot move more than the existing quantity."; } else { quantityError.ErrorMessage = "You must pick at least one item to move."; } quantityError.Text = " "; quantityError.IsValid = false; phValidation.Controls.Add(quantityError); } }