protected void ShipmentCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Recalc")
            {
                int itemId = AlwaysConvert.ToInt(e.CommandArgument);
                int index  = _Order.Shipments.IndexOf(itemId);
                if (index > -1)
                {
                    OrderShipment shipment   = _Order.Shipments[index];
                    ShipMethod    shipMethod = shipment.ShipMethod;
                    if (shipMethod != null)
                    {
                        ShipRateQuote rate = shipMethod.GetShipRateQuote(shipment);
                        if (rate != null)
                        {
                            // REMOVE OLD SHIPPING CHARGES FOR THIS SHIPMENT
                            for (int i = _Order.Items.Count - 1; i >= 0; i--)
                            {
                                OrderItem item = _Order.Items[i];
                                if (item.OrderShipmentId == shipment.Id)
                                {
                                    if (item.OrderItemType == OrderItemType.Shipping || item.OrderItemType == OrderItemType.Handling)
                                    {
                                        _Order.Items.DeleteAt(i);
                                    }
                                }
                            }

                            // ADD NEW SHIPPING LINE ITEMS TO THE ORDER
                            OrderItem shipRateLineItem = new OrderItem();
                            shipRateLineItem.OrderId         = _Order.Id;
                            shipRateLineItem.OrderItemType   = OrderItemType.Shipping;
                            shipRateLineItem.OrderShipmentId = shipment.Id;
                            shipRateLineItem.Name            = shipMethod.Name;
                            shipRateLineItem.Price           = rate.Rate;
                            shipRateLineItem.Quantity        = 1;
                            shipRateLineItem.TaxCodeId       = shipMethod.TaxCodeId;
                            _Order.Items.Add(shipRateLineItem);
                            if (rate.Surcharge > 0)
                            {
                                shipRateLineItem                 = new OrderItem();
                                shipRateLineItem.OrderId         = _Order.Id;
                                shipRateLineItem.OrderItemType   = OrderItemType.Handling;
                                shipRateLineItem.OrderShipmentId = shipment.Id;
                                shipRateLineItem.Name            = shipMethod.Name;
                                shipRateLineItem.Price           = rate.Surcharge;
                                shipRateLineItem.Quantity        = 1;
                                shipRateLineItem.TaxCodeId       = shipMethod.TaxCodeId;
                                _Order.Items.Add(shipRateLineItem);
                            }
                            _Order.Save(true, false);
                            BindGrids();
                            ShippingRecalculatedMessage.Visible = true;
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public override ShipRateQuote GetShipRateQuote(Warehouse origin, CommerceBuilder.Users.Address destination, BasketItemCollection contents, string serviceCode)
        {
            //GET THE SHIP QUOTE FOR THE GIVEN SERVICE
            //GET THE RATE FOR ALL SERVICES
            Dictionary <String, ProviderShipRateQuote> allProviderQuotes = GetAllProviderShipRateQuotes(origin, destination, contents);

            if (allProviderQuotes != null && allProviderQuotes.ContainsKey(serviceCode))
            {
                ProviderShipRateQuote providerQuote = allProviderQuotes[serviceCode];
                ShipRateQuote         quote         = new ShipRateQuote();
                quote.Rate = providerQuote.Rate;
                return(quote);
            }
            return(null);
        }
 /// <summary>
 /// Apply a ship method to this basket shipment
 /// </summary>
 /// <param name="shipMethod">The ship method to apply</param>
 public void ApplyShipMethod(ShipMethod shipMethod)
 {
     if (shipMethod == null) throw new ArgumentNullException("shipMethod");
     //WIPE OUT ANY SHIPPING CHARGES CURRENTLY IN THIS SHIPMENT
     this.ShipMethodId = shipMethod.ShipMethodId;
     this._ShipMethod = shipMethod;
     BasketItem item;
     BasketItemCollection basketItems = this.Basket.Items;
     for (int i = basketItems.Count - 1; i >= 0; i--)
     {
         item = basketItems[i];
         if (item.BasketShipmentId.Equals(this.BasketShipmentId) && ((item.OrderItemType == OrderItemType.Shipping || item.OrderItemType == OrderItemType.Handling)))
         {
             basketItems.DeleteAt(i);
         }
     }
     ShipRateQuote quote = shipMethod.GetShipRateQuote(this);
     if (quote == null) throw new ArgumentException("The specified shipping method is not valid for these items.", "shipMethod");
     item = new BasketItem();
     item.BasketId = this.BasketId;
     item.BasketShipmentId = this.BasketShipmentId;
     item.Name = quote.ShipMethod.Name;
     item.OrderItemType = OrderItemType.Shipping;
     item.Price = quote.Rate;
     item.Weight = 0;
     item.Quantity = 1;
     basketItems.Add(item);
     if (quote.Surcharge > 0)
     {
         item = new BasketItem();
         item.BasketId = this.BasketId;
         item.BasketShipmentId = this.BasketShipmentId;
         item.Name = quote.ShipMethod.Name;
         item.OrderItemType = OrderItemType.Handling;
         item.Price = quote.Surcharge;
         item.Weight = 0;
         item.Quantity = 1;
         basketItems.Add(item);
     }
     basketItems.Save();
     this.Save();
 }
        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();
            }
        }
        protected void EditShipmentsGrid_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DelShp")
            {
                int shipmentId = AlwaysConvert.ToInt(e.CommandArgument);
                int index      = _Order.Shipments.IndexOf(shipmentId);
                if (index > -1)
                {
                    OrderShipment shipment = _Order.Shipments[index];

                    // DELETE FROM ORDER
                    _Order.Shipments.Remove(shipment);
                    foreach (OrderItem item in shipment.OrderItems)
                    {
                        _Order.Items.Remove(item);
                    }

                    _Order.Save(true, false);
                    BindShipmentsGrid();
                }
            }
            else if (e.CommandName == "ChangeShipMethod")
            {
                int shipmentId = AlwaysConvert.ToInt(e.CommandArgument);
                int index      = _Order.Shipments.IndexOf(shipmentId);
                if (index > -1)
                {
                    // SHOW THE CHANGE SHIPMENT POPUP
                    ChangeShipMethodShipmentId.Value   = shipmentId.ToString();
                    ChangeShipMethodDialogCaption.Text = string.Format(ChangeShipMethodDialogCaption.Text, index + 1);
                    OrderShipment shipment = _Order.Shipments[index];
                    ExistingShipMethod.Text = shipment.ShipMethodName;

                    // GENERATE RATE QUOTES FOR ALL SHIPPING METHODS
                    List <ShipRateQuote> rateQuotes  = new List <ShipRateQuote>();
                    IList <ShipMethod>   shipMethods = ShipMethodDataSource.LoadAll();
                    foreach (ShipMethod method in shipMethods)
                    {
                        ShipRateQuote quote = method.GetShipRateQuote(shipment);
                        if (quote != null)
                        {
                            rateQuotes.Add(quote);
                        }
                    }

                    // GET LIST OF SHIPPING METHODS THAT WOULD BE AVAILABLE TO THE CUSTOMER
                    IList <ShipMethod> customerShipMethods = ShipMethodDataSource.LoadForShipment(shipment);

                    // ADD RATE QUOTES TO THE DROPDOWN
                    foreach (ShipRateQuote quote in rateQuotes)
                    {
                        string name = string.Format("{0} : {1}", quote.Name, quote.Rate.LSCurrencyFormat("lc"));
                        if (customerShipMethods.IndexOf(quote.ShipMethodId) < 0)
                        {
                            // SHOW NOTE IF HIDDEN SHIPPING METHODS ARE AVAIALBLE
                            name = "** " + name;
                            HiddenShipMethodWarning.Visible = true;
                        }
                        NewShipMethod.Items.Add(new ListItem(name, quote.ShipMethodId.ToString()));
                    }
                    ChangeShipMethodPopup.Show();
                }
            }
            else if (e.CommandName == "VoidShp")
            {
                int shipmentId = AlwaysConvert.ToInt(e.CommandArgument);
                int index      = _Order.Shipments.IndexOf(shipmentId);
                if (index > -1)
                {
                    OrderShipment shipment = _Order.Shipments[index];
                    shipment.Void();

                    _Order.Save(true, false);
                    BindShipmentsGrid();
                }
            }
        }