Beispiel #1
0
        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         = "&nbsp;";
                quantityError.IsValid      = false;
                phQuantityValidation.Controls.Add(quantityError);
            }
        }
        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    = "&nbsp;";
                quantityError.IsValid = false;
                phValidation.Controls.Add(quantityError);
            }
        }