protected void ShipButton_Click(object sender, EventArgs e) { foreach (GridViewRow row in ShipmentGrid.Rows) { DateTime shipDate = LocaleHelper.LocalNow; int shipmentId = (int)ShipmentGrid.DataKeys[row.DataItemIndex].Value; int index = _Shipments.IndexOf(shipmentId); if (index > -1) { OrderShipment shipment = _Shipments[index]; TextBox tb = row.FindControl("ShipDate") as TextBox; if (tb != null) { shipDate = AlwaysConvert.ToDateTime(tb.Text, LocaleHelper.LocalNow); } DropDownList ddl = row.FindControl("ShipGateway") as DropDownList; tb = row.FindControl("TrackingNumber") as TextBox; if ((ddl != null) && (tb != null)) { TrackingNumber tn = new TrackingNumber(); tn.OrderShipmentId = shipmentId; tn.ShipGatewayId = AlwaysConvert.ToInt(ddl.SelectedValue); tn.TrackingNumberData = tb.Text; shipment.TrackingNumbers.Add(tn); shipment.TrackingNumbers.Save(); } shipment.Ship(shipDate); } } UpdatePanel.Visible = false; ConfirmPanel.Visible = true; }
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); } }
protected void BatchButton_Click(object sender, EventArgs e) { List <string> messages = new List <string>(); List <int> orderIds = GetSelectedOrderIds(); if (orderIds.Count > 0) { if (BatchAction.SelectedValue.StartsWith("OS_")) { //UPDATE ORDER STATUS REQUESTED int orderStatusId = AlwaysConvert.ToInt(BatchAction.SelectedValue.Substring(3)); //VALIDATE STATUS OrderStatus status = OrderStatusDataSource.Load(orderStatusId); if (status != null) { IDatabaseSessionManager database = AbleContext.Current.Database; database.BeginTransaction(); foreach (int orderId in orderIds) { Order order = OrderDataSource.Load(orderId); if (order != null) { order.UpdateOrderStatus(status); } } database.CommitTransaction(); } } else { switch (BatchAction.SelectedValue) { case "INVOICE": AbleContext.Current.Session.SelectedOrderIds = orderIds; Response.Redirect("Print/Invoices.aspx"); break; case "PACKSLIP": AbleContext.Current.Session.SelectedOrderIds = orderIds; Response.Redirect("Print/PackSlips.aspx"); break; case "PULLSHEET": AbleContext.Current.Session.SelectedOrderIds = orderIds; Response.Redirect("Print/PullSheet.aspx"); break; case "CANCEL": AbleContext.Current.Session.SelectedOrderIds = orderIds; Response.Redirect("Batch/Cancel.aspx"); break; case "SHIPOPT": AbleContext.Current.Session.SelectedOrderIds = orderIds; Response.Redirect("Batch/Ship.aspx"); break; case "SHIP": AbleContext.Current.Database.BeginTransaction(); int shipCount = 0; foreach (int orderId in orderIds) { Order order = OrderDataSource.Load(orderId); if (order != null && order.Shipments != null) { bool shipped = false; int shipmentCount = order.Shipments.Count; for (int i = 0; i < shipmentCount; i++) { OrderShipment shipment = order.Shipments[i]; if (shipment != null && !shipment.IsShipped) { shipment.Ship(); shipped = true; } } if (shipped) { messages.Add("Order #" + order.OrderNumber + " shipped."); shipCount++; } else { messages.Add("Order #" + order.OrderNumber + " did not have any unshipped items."); } } } AbleContext.Current.Database.CommitTransaction(); messages.Add(shipCount + " orders shipped."); break; case "PAY": AbleContext.Current.Database.BeginTransaction(); int payCount = 0; foreach (int orderId in orderIds) { Order order = OrderDataSource.Load(orderId); if (order != null) { bool paid = false; int paymentCount = order.Payments.Count; for (int i = 0; i < paymentCount; i++) { Payment payment = order.Payments[i]; if (payment.PaymentStatus == PaymentStatus.Authorized) { payment.Capture(payment.Amount, true); paid = true; } else if (payment.PaymentStatus == PaymentStatus.Unprocessed) { payment.Authorize(); paid = true; } } if (paid) { payCount++; messages.Add("Order " + order.OrderNumber.ToString() + " processed."); } else { messages.Add("Order " + order.OrderNumber.ToString() + " does not have any payments to be processed."); } } } AbleContext.Current.Database.CommitTransaction(); messages.Add(payCount + " orders processed."); break; case "DELETE": if (AbleContext.Current.User.IsSystemAdmin) { IDatabaseSessionManager database = AbleContext.Current.Database; database.BeginTransaction(); foreach (int orderId in orderIds) { OrderDataSource.Delete(orderId); } database.CommitTransaction(); OrderGrid.DataBind(); } break; case "EXPORT": AbleContext.Current.Session.SelectedOrderIds = orderIds; Response.Redirect("../DataExchange/OrdersExport.aspx?type=selected"); break; } } } if (messages.Count > 0) { BatchMessage.Visible = true; BatchMessage.Text = string.Join("<br />", messages.ToArray()); } BatchAction.SelectedIndex = -1; OrderGrid.DataBind(); }