protected void gvCreateTOD_DataBound(object sender, EventArgs e)
    {
        if (gvCreateTOD.Rows.Count > 0)
        {
            TransferOrderHeader toh = null;
            string orderNumber      = Request.QueryString["OrderNum"];
            if (!string.IsNullOrEmpty(orderNumber))
            {
                toh = TransferOrderHeader.GetTransferOrderHeaderByOrderNum(int.Parse(orderNumber));
            }

            foreach (GridViewRow gvr in gvCreateTOD.Rows)
            {
                DropDownList     ddlUom  = (DropDownList)gvr.Cells[3].FindControl("ddlUom2");
                ObjectDataSource odsUom  = (ObjectDataSource)gvr.Cells[3].FindControl("odsUom");
                Label            lblGtin = (Label)gvr.Cells[1].FindControl("lblGtin");

                // District stock
                // HACK: If you see it, you know why it needs to be changed, if not...
                Label lblFromStock = (Label)gvr.FindControl("lblDistrictStock");
                var   bal          = GIIS.DataLayer.HealthFacilityBalance.GetHealthFacilityBalanceByHealthFacilityCode(toh.OrderFacilityFrom).Find(o => o.Gtin == lblGtin.Text);
                if (bal != null)
                {
                    lblFromStock.Text = String.Format("{0} {1}", bal.Balance, bal.GtinObject.BaseUom);
                }

                odsUom.SelectParameters.Clear();
                odsUom.SelectParameters.Add("gtin", lblGtin.Text);
                odsUom.DataBind();
                ddlUom.DataSourceID = "odsUom";
                ddlUom.DataBind();
            }
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string orderNumber = Request.QueryString["OrderNum"];

        if (!string.IsNullOrEmpty(orderNumber))
        {
            TransferOrderHeader toh = TransferOrderHeader.GetTransferOrderHeaderByOrderNum(int.Parse(orderNumber));

            OrderManagementLogic oml       = new OrderManagementLogic();
            TransferOrderHeader  tohCancel = oml.CancelOrder(toh, CurrentEnvironment.LoggedUser.Id);

            string url = string.Format("TransferOrder.aspx?OrderNum={0}", orderNumber);
            Response.Redirect(url, false);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.Page.IsPostBack)
        {
            List <string> actionList        = null;
            string        sessionNameAction = "";
            if (CurrentEnvironment.LoggedUser != null)
            {
                sessionNameAction = "__GIS_actionList_" + CurrentEnvironment.LoggedUser.Id;
                actionList        = (List <string>)Session[sessionNameAction];
            }

            if ((actionList != null) && actionList.Contains("ViewTransferOrderDetails") && (CurrentEnvironment.LoggedUser != null))
            {
                int    userId     = CurrentEnvironment.LoggedUser.Id;
                string language   = CurrentEnvironment.Language;
                int    languageId = int.Parse(language);
                Dictionary <string, string> wtList = (Dictionary <string, string>)HttpContext.Current.Cache["TransferOrderHeader-dictionary" + language];
                if (wtList == null)
                {
                    List <WordTranslate> wordTranslateList = WordTranslate.GetWordByLanguage(languageId, "TransferOrderHeader");
                    wtList = new Dictionary <string, string>();
                    foreach (WordTranslate vwt in wordTranslateList)
                    {
                        wtList.Add(vwt.Code, vwt.Name);
                    }
                    HttpContext.Current.Cache.Insert("TransferOrderHeader-dictionary" + language, wtList);
                }

                //controls

                //actions
                //btnRelease.Visible = actionList.Contains("ReleaseTransferOrderDetail");
                //btnPack.Visible = actionList.Contains("PackTransferOrderDetail");
                //btnShip.Visible = actionList.Contains("ShipTransferOrderDetail");
                //btnCancel.Visible = actionList.Contains("CancelTransferOrderDetail");

                //buttons
                //btnRelease.Text = wtList["TransferOrderDetailReleaseButton"];
                //btnPack.Text = wtList["TransferOrderDetailPackButton"];
                //btnShip.Text = wtList["TransferOrderDetailShipButton"];
                //btnCancel.Text = wtList["TransferOrderDetailCancelButton"];

                //messages

                //Page Title
                lblTitle.Text = wtList["TransferOrderHeaderListPageTitle"];

                btnRelease.Visible = false;
                btnPack.Visible    = false;
                btnShip.Visible    = false;
                btnCancel.Visible  = false;

                string orderNumber = Request.QueryString["OrderNum"];
                if (!string.IsNullOrEmpty(orderNumber))
                {
                    //Requested = 0,
                    //Released = 1,
                    //Packed = 2,
                    //Shipped = 3,
                    //Cancelled = -1

                    TransferOrderHeader toh = TransferOrderHeader.GetTransferOrderHeaderByOrderNum(int.Parse(orderNumber));
                    if (toh.OrderStatus == 0)
                    {
                        btnRelease.Visible = true;
                        btnCancel.Visible  = true;
                    }
                    else if (toh.OrderStatus == 1)
                    {
                        btnPack.Visible   = true;
                        btnCancel.Visible = true;
                    }
                    else if (toh.OrderStatus == 2)
                    {
                        btnShip.Visible   = true;
                        btnCancel.Visible = true;
                    }
                }
                string orderStatus = Request.QueryString["Status"];

                odsTransferOrderHeader.SelectParameters.Clear();
                odsTransferOrderHeader.SelectParameters.Add("OrderFacilityFrom", CurrentEnvironment.LoggedUser.HealthFacility.Code);
                odsTransferOrderHeader.SelectParameters.Add("orderStatus", orderStatus);
                gvTransferOrderHeader.DataSourceID = "odsTransferOrderHeader";
                gvTransferOrderHeader.DataBind();

                //odsTransferOrderDetails.SelectParameters.Clear();
                //odsTransferOrderDetails.SelectParameters.Add("OrderFacilityFrom", CurrentEnvironment.LoggedUser.HealthFacility.Code);
                //gvTransferOrderDetails.DataSourceID = "odsTransferOrderDetails";
                //gvTransferOrderDetails.DataBind();

                odsTransferOrderStatus.SelectParameters.Clear();
                odsTransferOrderStatus.SelectParameters.Add("OrderFacilityFrom", CurrentEnvironment.LoggedUser.HealthFacility.Code);
                gvTransferOrderStatus.DataSourceID = "odsTransferOrderStatus";
                gvTransferOrderStatus.DataBind();

                //grid header text
                //gvTransferOrderStatus.Columns[0].HeaderText = wtList["TransferOrderDetailOrderStatus"];
                //gvTransferOrderStatus.Columns[1].HeaderText = wtList["TransferOrderDetailTotal"];

                gvTransferOrderHeader.Columns[0].HeaderText = wtList["TransferOrderHeaderOrderNum"];
                gvTransferOrderHeader.Columns[1].HeaderText = wtList["TransferOrderHeaderOrderSchedReplenishDate"];
                gvTransferOrderHeader.Columns[2].HeaderText = wtList["TransferOrderHeaderOrderFacilityFrom"];
                gvTransferOrderHeader.Columns[3].HeaderText = wtList["TransferOrderHeaderOrderFacilityTo"];
                //gvTransferOrderHeader.Columns[4].HeaderText = wtList["TransferOrderDetailCarrier"];
                gvTransferOrderHeader.Columns[5].HeaderText = wtList["TransferOrderHeaderOrderIsActive"];
            }
        }
    }
        /// <summary>
        /// Updates the specified <paramref name="orderLine"/> based on business rules
        /// </summary>
        /// <param name="orderLine">The <see cref="T:GIIS.DataLayer.TransferOrderDetail"/> line to be updated</param>
        /// <returns>The <see cref="T:GIIS.DataLayer.TransferOrderDetail"/> which was updated</returns>
        /// <remarks>
        /// <list type="ordered">
        /// <item><description>	Load the current order line for the database using the order line #</description></item>
        /// <item><description>	Instantiate an instance of StockManagementLogic BLL class.</description></item>
        /// <item><description>	If the status of the current order line is:
        ///     <list type="table">
        ///         <listHeader>
        ///             <term>State</term>
        ///             <description>Actions</description>
        ///         </listHeader>
        ///         <item>
        ///             <term>Requested</term>
        ///             <description>
        ///                 <list type="ordered">
        ///                     <item><description>[Guard Condition] Ensure the new state is either “Cancelled”, “Requested” or “Released” otherwise throw an invalid state transition exception</description></item>
        ///                     <item><description>Update the quantity and status of the  order detail item</description></item>
        ///                     <item><description>If the new state is “Released” then call the Allocate function of the StockManagementLogic instance to allocate the specified order detail.</description></item>
        ///                     <item><description>Save the order detail</description></item>
        ///                 </list>
        ///             </description>
        ///         </item>
        ///         <item>
        ///             <term>Released</term>
        ///             <description>
        ///                 <list type="ordered">
        ///                     <item><description>[Guard Condition] Ensure the new state is either “Cancelled”, “Released” or “Packed” otherwise thrown an invalid state transition exception</description></item>
        ///                     <item><description>If the current state is “Released” then
        ///                         <list type="ordered">
        ///                             <item><description>Calculate the difference in quantity from the “old” record and “new” record</description></item>
        ///                             <item><description>Call the Allocate method of the StockManagementLogic instance to perform the additional allocation/de-allocation.</description></item>
        ///                         </list>
        ///                     </description></item>
        ///                     <item><description>	Update the quantity and status of the order detail item.</description></item>
        ///                     <item><description>If the new state is “Cancelled” then call the Allocate method of the StockManagementLogic instance to perform the de-allocation of the item.</description></item>
        ///                     <item><description>Save the order detail</description></item>
        ///                 </list>
        ///             </description>
        ///         </item>
        ///         <item>
        ///             <term>Packed</term>
        ///             <description>
        ///                 <list type="ordered">
        ///                     <item><description>[Guard Condition] Ensure the new state is either “Cancelled”, “Packed” or “Shipped”</description></item>
        ///                     <item><description>Update the quantity and status of the order detail item.</description></item>
        ///                     <item><description>If the new state is “cancelled” then call the Allocate method of the StockManagementLogic instance to perform the de-allocation of the item.</description></item>
        ///                     <item><description>If the new state is “Shipped” then
        ///                         <list type="ordered">
        ///                             <item><description>Call the allocate method of the StockManagementLogic instance to perform the de-allocation of the line item.</description></item>
        ///                             <item><description>Call the Transfer method of the StockManagementLogic instance to perform the transfer transactions between the source and target facilities.</description></item>
        ///                         </list>
        ///                     </description></item>
        ///                     <item><description>Save the order detail</description></item>
        ///                 </list>
        ///             </description>
        ///         </item>
        ///         <item>
        ///             <term>Shipped</term>
        ///             <description>Throw an invalid operation exception as shipped orders (and their lines) cannot be edited</description>
        ///         </item>
        ///         <item>
        ///             <term>Cancelled</term>
        ///             <description>Throw an invalid operation exception as cancelled orders (and their lines) cannot be edited</description>
        ///         </item>
        ///     </list>
        /// </description></item>
        /// </list>
        /// </remarks>
        public TransferOrderDetail UpdateOrderLine(TransferOrderDetail orderLine, Int32 modifiedBy)
        {
            if (orderLine == null)
            {
                throw new ArgumentNullException("orderLine");
            }
            else if (orderLine.OrderDetailNum == default(Int32))
            {
                throw new ArgumentException("Order line is not saved", "orderLine");
            }

            // Load the current order line from the database
            TransferOrderDetail currentOrderLine   = TransferOrderDetail.GetTransferOrderDetailByOrderDetailNum(orderLine.OrderDetailNum);
            TransferOrderHeader currentOrderHeader = TransferOrderHeader.GetTransferOrderHeaderByOrderNum(orderLine.OrderNum);

            // Can't change the GTIN with this function
            if (orderLine.OrderGtin != currentOrderLine.OrderGtin)
            {
                throw new InvalidOperationException("Cannot change the GTIN with this function. Remove the order line first and add another order-line with the new GTIN");
            }

            // New order lot number is null?  We need to get oldest lot
            if (String.IsNullOrEmpty(orderLine.OrderGtinLotnum) || orderLine.OrderGtinLotnum == "*")
            {
                ItemLot itemLot = GetOldestLot(currentOrderHeader.OrderFacilityFromObject, orderLine.OrderGtin); //currentOrderLine.OrderGtinLotnum;
                if (itemLot != null)
                {
                    orderLine.OrderGtinLotnum = itemLot.LotNumber;
                }
            }

            StockManagementLogic stockLogic = new StockManagementLogic();

            // Apply rules
            switch ((OrderStatusType)currentOrderLine.OrderDetailStatus)
            {
            case OrderStatusType.Requested:
                // State transitions
                if (orderLine.OrderDetailStatus != (int)OrderStatusType.Cancelled &&
                    orderLine.OrderDetailStatus != (int)OrderStatusType.Released &&
                    orderLine.OrderDetailStatus != (int)OrderStatusType.Requested)
                {
                    throw new IllegalStateException((OrderStatusType)orderLine.OrderDetailStatus, "TransferOrderDetail", "UpdateOrderLine");
                }

                // Allocate the data if this is a transition
                if (orderLine.OrderDetailStatus == (int)OrderStatusType.Released)
                {
                    stockLogic.Allocate(currentOrderHeader.OrderFacilityFromObject, orderLine.OrderGtin, orderLine.OrderGtinLotnum, (int)orderLine.OrderQty, orderLine, modifiedBy);
                }

                break;

            case OrderStatusType.Released:

                // Guard conditions
                if (orderLine.OrderDetailStatus != (int)OrderStatusType.Cancelled &&
                    orderLine.OrderDetailStatus != (int)OrderStatusType.Released &&
                    orderLine.OrderDetailStatus != (int)OrderStatusType.Packed)
                {
                    throw new IllegalStateException((OrderStatusType)orderLine.OrderDetailStatus, "TransferOrderDetail", "UpdateOrderLine");
                }

                // We need to adjust the allocations?
                if (currentOrderLine.OrderQty != orderLine.OrderQty)
                {
                    stockLogic.Allocate(currentOrderHeader.OrderFacilityFromObject, orderLine.OrderGtin, orderLine.OrderGtinLotnum, (int)(orderLine.OrderQty - currentOrderLine.OrderQty), orderLine, modifiedBy);
                }

                // Released -> Cancelled = Deallocate
                if (orderLine.OrderDetailStatus == (int)OrderStatusType.Cancelled)
                {
                    stockLogic.Allocate(currentOrderHeader.OrderFacilityFromObject, orderLine.OrderGtin, orderLine.OrderGtinLotnum, -(int)orderLine.OrderQty, orderLine, modifiedBy);
                }

                break;

            case OrderStatusType.Packed:
                // Guard conditions
                if (orderLine.OrderDetailStatus != (int)OrderStatusType.Cancelled &&
                    orderLine.OrderDetailStatus != (int)OrderStatusType.Packed &&
                    orderLine.OrderDetailStatus != (int)OrderStatusType.Shipped)
                {
                    throw new IllegalStateException((OrderStatusType)orderLine.OrderDetailStatus, "TransferOrderDetail", "UpdateOrderLine");
                }

                // We need to adjust the allocations?
                if (currentOrderLine.OrderQty != orderLine.OrderQty)
                {
                    stockLogic.Allocate(currentOrderHeader.OrderFacilityFromObject, orderLine.OrderGtin, orderLine.OrderGtinLotnum, (int)(orderLine.OrderQty - currentOrderLine.OrderQty), orderLine, modifiedBy);
                }

                // Packed -> Cancelled = Deallocate
                if (orderLine.OrderDetailStatus == (int)OrderStatusType.Cancelled)
                {
                    stockLogic.Allocate(currentOrderHeader.OrderFacilityFromObject, orderLine.OrderGtin, orderLine.OrderGtinLotnum, -(int)orderLine.OrderQty, orderLine, modifiedBy);
                }
                // Packed -> Shipped = Deallocate then Transfer
                else if (orderLine.OrderDetailStatus == (int)OrderStatusType.Shipped)
                {
                    stockLogic.Allocate(currentOrderHeader.OrderFacilityFromObject, orderLine.OrderGtin, orderLine.OrderGtinLotnum, -(int)orderLine.OrderQty, orderLine, modifiedBy);
                    stockLogic.Transfer(currentOrderHeader.OrderFacilityFromObject, currentOrderHeader.OrderFacilityToObject, orderLine.OrderGtin, orderLine.OrderGtinLotnum, orderLine, (int)orderLine.OrderQty, modifiedBy);
                }

                break;

            case OrderStatusType.Shipped:
                throw new InvalidOperationException("Shipped orders cannot be modified " + orderLine.OrderDetailNum.ToString());

            case OrderStatusType.Cancelled:
                throw new InvalidOperationException("Cancelled orders cannot be modified");
            }

            // Update
            orderLine.ModifiedBy = modifiedBy;
            orderLine.ModifiedOn = DateTime.Now;
            TransferOrderDetail.Update(orderLine);

            // Return the order line
            return(orderLine);
        }
    protected void gvGtinValue_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[0].Visible = false;

        TransferOrderHeader toh = null;
        string orderNumber      = Request.QueryString["OrderNum"];

        if (!string.IsNullOrEmpty(orderNumber))
        {
            toh = TransferOrderHeader.GetTransferOrderHeaderByOrderNum(int.Parse(orderNumber));
            if (toh.OrderStatus == 0)
            {
                e.Row.Cells[4].Visible = false;
            }
        }


        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label   lblOrderGtin = (Label)e.Row.FindControl("lblOrderGtin");
            TextBox txtValue     = (TextBox)e.Row.FindControl("txtValue");
            Label   lblId        = (Label)e.Row.FindControl("lblId");
            Label   lblId2       = (Label)e.Row.FindControl("lblId2");
            Label   lblItem      = (Label)e.Row.FindControl("lblItem");
            Label   lblFromStock = (Label)e.Row.FindControl("lblDistrictStock");

            DropDownList ddlUom = (DropDownList)e.Row.FindControl("ddlUom");
            DataTable    dt     = ItemManufacturer.GetUomFromGtin(lblOrderGtin.Text);
            // List<Uom> uomList = Uom.GetUomList();
            ddlUom.DataSource = dt;
            ddlUom.DataBind();


            //if (toh != null) // && toh.OrderStatus == 0)
            //    ddlUom.Enabled = true;
            //else ddlUom.Enabled = false;

            string id  = lblId.Text;
            int    id2 = int.Parse(lblId2.Text);

            TransferOrderDetail tod = TransferOrderDetail.GetTransferOrderDetailByOrderNumAndOrderDetail(int.Parse(id), id2);

            // lblFromStock.Text = GIIS.DataLayer.HealthFacilityBalance.GetHealthFacilityBalanceByHealthFacilityCode(toh.OrderFacilityFrom).Find(o=>o.Gtin == tod.OrderGtin).Balance.ToString();
            lblFromStock.Text += " " + tod.OrderUom;

            if (tod != null)
            {
                txtValue.Text        = tod.OrderQty.ToString();
                ddlUom.SelectedValue = tod.OrderUom;
                ddlUom.Enabled       = false;
            }


            DropDownList     ddlItemLot = (DropDownList)e.Row.FindControl("ddlItemLot");
            ObjectDataSource odsItemLot = (ObjectDataSource)e.Row.FindControl("odsItemLot");

            odsItemLot.SelectParameters.Clear();
            odsItemLot.SelectParameters.Add("gtin", lblOrderGtin.Text);
            odsItemLot.DataBind();
            ddlItemLot.DataSource = odsItemLot;
            ddlItemLot.DataBind();
            if (tod.OrderDetailStatus > 0)
            {
                ddlItemLot.SelectedValue = tod.OrderGtinLotnum;
            }
            if (tod.OrderDetailStatus == 3)
            {
                txtValue.Enabled   = false;
                ddlItemLot.Enabled = false;
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.Page.IsPostBack)
        {
            List <string> actionList        = null;
            string        sessionNameAction = "";
            if (CurrentEnvironment.LoggedUser != null)
            {
                sessionNameAction = "__GIS_actionList_" + CurrentEnvironment.LoggedUser.Id;
                actionList        = (List <string>)Session[sessionNameAction];
            }

            if ((actionList != null) && actionList.Contains("ViewTransferOrderDetails") && (CurrentEnvironment.LoggedUser != null))
            {
                int    userId     = CurrentEnvironment.LoggedUser.Id;
                string language   = CurrentEnvironment.Language;
                int    languageId = int.Parse(language);
                Dictionary <string, string> wtList = (Dictionary <string, string>)HttpContext.Current.Cache["TransferOrderDetail-dictionary" + language];
                if (wtList == null)
                {
                    List <WordTranslate> wordTranslateList = WordTranslate.GetWordByLanguage(languageId, "TransferOrderDetail");
                    wtList = new Dictionary <string, string>();
                    foreach (WordTranslate vwt in wordTranslateList)
                    {
                        wtList.Add(vwt.Code, vwt.Name);
                    }
                    HttpContext.Current.Cache.Insert("TransferOrderDetail-dictionary" + language, wtList);
                }

                this.lblOrderNum.Text = wtList["TransferOrderDetailOrderNum"];
                //this.lblOrderDetailNum.Text = wtList["TransferOrderDetailOrderDetailNum"];
                //this.lblOrderGtin.Text = wtList["TransferOrderDetailOrderGtin"];
                //this.lblOrderGtinLotnum.Text = wtList["TransferOrderDetailOrderGtinLotnum"];
                //this.lblOrderCustomItem.Text = wtList["TransferOrderDetailOrderCustomItem"];
                //this.lblOrderDetailDescription.Text = wtList["TransferOrderDetailOrderDetailDescription"];
                //this.lblOrderUom.Text = wtList["TransferOrderDetailOrderUom"];
                //this.lblOrderQtyInBaseUom.Text = wtList["TransferOrderDetailOrderQtyInBaseUom"];
                //this.lblOrderDetailStatus.Text = wtList["TransferOrderDetailOrderDetailStatus"];
                //this.lblRevNum.Text = wtList["TransferOrderDetailRevNum"];


                //gvGtinValue.Columns[2].HeaderText = wtList["TransferOrderDetailOrderItem"];
                gvGtinValue.Columns[4].HeaderText = wtList["TransferOrderDetailOrderGtin"];
                gvGtinValue.Columns[5].HeaderText = wtList["TransferOrderDetailOrderGtinLotnum"];
                gvGtinValue.Columns[6].HeaderText = wtList["TransferOrderDetailOrderQtyInBaseUom"];
                gvGtinValue.Columns[7].HeaderText = wtList["TransferOrderDetailOrderUom"];


                //    this.btnAdd.Visible = actionList.Contains("AddTransferOrderDetail");
                //    this.btnAdd.Text = wtList["TransferOrderDetailAddButton"];

                btnRelease.Visible = false;
                btnPack.Visible    = false;
                btnShip.Visible    = false;
                btnCancel.Visible  = false;

                if (Request.QueryString["orderNum"] != null)
                {
                    string orderNumber = Request.QueryString["orderNum"].ToString();

                    //Requested = 0,
                    //Released = 1,
                    //Packed = 2,
                    //Shipped = 3,
                    //Cancelled = -1

                    TransferOrderHeader orderheader = TransferOrderHeader.GetTransferOrderHeaderByOrderNum(int.Parse(orderNumber));

                    txtOrderNum.Text = orderheader.OrderNum.ToString();
                    txtOrderSchedReplenishDate.Text = orderheader.OrderSchedReplenishDate.ToString("dd-MMM-yyyy");
                    txtOrderFacilityFrom.Text       = orderheader.OrderFacilityFromObject.Name;
                    txtOrderFacilityTo.Text         = orderheader.OrderFacilityToObject.Name;
                    int ostatus = orderheader.OrderStatus;
                    switch (ostatus)
                    {
                    case 0:
                        txtOrderStatus.Text = "Requested";
                        break;

                    case 1:
                        txtOrderStatus.Text = "Released";
                        break;

                    case 2:
                        txtOrderStatus.Text = "Packed";
                        break;

                    case 3:
                        txtOrderStatus.Text = "Shipped";
                        break;

                    case -1:
                        txtOrderStatus.Text = "Canceled";
                        break;
                    }
                    txtOrderCarrier.Text = orderheader.OrderCarrier;

                    List <TransferOrderDetail> orderDetailList = TransferOrderDetail.GetTransferOrderDetailByOrderNumAsList(int.Parse(orderNumber), CurrentEnvironment.LoggedUser.HealthFacility.Code);

                    if (orderDetailList.Count >= 1)
                    {
                        if (orderheader.OrderStatus == 0)
                        {
                            btnRelease.Visible  = true;
                            btnCancel.Visible   = true;
                            gvCreateTOD.Visible = false;
                            gvGtinValue.Visible = true;
                            ddlItemLot.Visible  = false;
                        }
                        else if (orderheader.OrderStatus == 1)
                        {
                            btnPack.Visible     = true;
                            btnCancel.Visible   = true;
                            gvCreateTOD.Visible = false;
                            gvGtinValue.Visible = true;
                        }
                        else if (orderheader.OrderStatus == 2)
                        {
                            btnShip.Visible     = true;
                            btnCancel.Visible   = true;
                            gvCreateTOD.Visible = false;
                            gvGtinValue.Visible = true;
                        }
                        else if (orderheader.OrderStatus == 3)
                        {
                            btnAdd.Visible       = false;
                            ddlGtin.Visible      = false;
                            ddlItemLot.Visible   = false;
                            ddlUom.Visible       = false;
                            txtQuantity.Visible  = false;
                            btnEdit.Visible      = false;
                            btnAddDetail.Visible = false;
                            tbl.Visible          = false;
                        }
                        else if (orderheader.OrderStatus == -1)
                        {
                            btnAdd.Visible       = false;
                            ddlGtin.Visible      = false;
                            ddlItemLot.Visible   = false;
                            ddlUom.Visible       = false;
                            txtQuantity.Visible  = false;
                            btnEdit.Visible      = false;
                            btnAddDetail.Visible = false;
                            tbl.Visible          = false;
                        }
                        hlPrintPackingSlip.Visible = orderheader.OrderStatus > 0;

                        hlPrintPackingSlip.NavigateUrl = String.Format("{0}/Pack_Order_Report.pdf?j_username={1}&j_password={2}&OrderNum={3}",
                                                                       ConfigurationManager.AppSettings["JasperServer"],
                                                                       ConfigurationManager.AppSettings["JasperUser"],
                                                                       ConfigurationManager.AppSettings["JasperPassword"],
                                                                       orderheader.OrderNum);
                        odsGtinValue.SelectParameters.Clear();
                        odsGtinValue.SelectParameters.Add("orderNumber", orderNumber);
                        odsGtinValue.SelectParameters.Add("healthFacilityCode", CurrentEnvironment.LoggedUser.HealthFacility.Code);
                        gvGtinValue.DataSourceID = "odsGtinValue";
                        gvGtinValue.DataBind();

                        //List<Uom> uomList = Uom.GetUomList();
                        //ddlUom.DataSource = uomList;
                        //ddlUom.DataBind();

                        DataTable dt = HealthFacilityBalance.GetItemManufacturerBalanceForDropDown(CurrentEnvironment.LoggedUser.HealthFacilityId);
                        ddlGtin.DataSource = dt;
                        ddlGtin.DataBind();
                    }
                    else
                    {
                        gvCreateTOD.Visible = true;
                        gvGtinValue.Visible = false;
                        odsCreateTOD.SelectParameters.Clear();
                        odsCreateTOD.SelectParameters.Add("healthFacilityCode", CurrentEnvironment.LoggedUser.HealthFacility.Code);
                        gvCreateTOD.DataSourceID = "odsCreateTOD";
                        gvCreateTOD.DataBind();
                        btnAdd.Visible = true;

                        //hide cotrols
                        tbl.Visible          = false;
                        ddlGtin.Visible      = false;
                        ddlItemLot.Visible   = false;
                        ddlUom.Visible       = false;
                        txtQuantity.Visible  = false;
                        btnEdit.Visible      = false;
                        btnAddDetail.Visible = false;
                    }
                }
            }
        }
    }
    protected void btnAddDetail_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {
                string orderNumber = "";
                if (Request.QueryString["orderNum"] != null)
                {
                    orderNumber = Request.QueryString["orderNum"].ToString();
                }
                TransferOrderHeader toh = TransferOrderHeader.GetTransferOrderHeaderByOrderNum(int.Parse(orderNumber));

                OrderManagementLogic oml = new OrderManagementLogic();
                string gtin = ddlGtin.SelectedValue;
                string lot  = "*";
                if (ddlItemLot.SelectedIndex > 0)
                {
                    lot = ddlItemLot.SelectedValue;
                }

                int qty      = 0;
                int quantity = 0;
                if (!String.IsNullOrEmpty(txtQuantity.Text))
                {
                    qty = int.Parse(txtQuantity.Text);
                    string uom = ddlUom.SelectedValue;

                    ItemManufacturer im = ItemManufacturer.GetItemManufacturerByGtin(gtin);
                    if (uom.Equals(im.BaseUom))
                    {
                        quantity = qty;
                    }
                    else if (uom.Equals(im.Alt1Uom))
                    {
                        quantity = qty * im.Alt1QtyPer;
                    }
                    else if (uom.Equals(im.Alt2Uom))
                    {
                        quantity = qty * im.Alt2QtyPer;
                    }
                }
                Uom u = Uom.GetUomByName(ddlUom.SelectedValue); //GetuomByName

                TransferOrderDetail tod = TransferOrderDetail.GetTransferOrderDetailByOrderNumGtinLotNumber(int.Parse(orderNumber), gtin, lot);
                if (tod == null)
                {
                    TransferOrderDetail td = oml.AddOrderLine(toh, gtin, lot, quantity, u, CurrentEnvironment.LoggedUser.Id);

                    if (td != null)
                    {
                        //ClearControls(this);
                        gvGtinValue.DataBind();
                        ddlGtin.SelectedIndex = 0;
                        //ddlItemLot.SelectedIndex = 0;
                        txtQuantity.Text   = string.Empty;
                        lblSuccess.Visible = true;
                        lblWarning.Visible = false;
                        lblError.Visible   = false;
                    }
                    else
                    {
                        lblSuccess.Visible = false;
                        lblWarning.Visible = false;
                        lblError.Visible   = true;
                    }
                }
                else
                {
                    lblSuccess.Visible = false;
                    lblWarning.Visible = true;
                    lblError.Visible   = false;
                }
            }
        }
        catch (Exception ex)
        {
            lblSuccess.Visible = false;
            lblWarning.Visible = false;
            lblError.Visible   = true;
        }
    }