protected void uiGridViewItems_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteDetail")
            {
                IStock.BLL.ClientReturnDetails objData = new IStock.BLL.ClientReturnDetails();
                objData.LoadByPrimaryKey(Convert.ToInt32(e.CommandArgument.ToString()));

                decimal price = 0;
                if (!objData.IsColumnNull("Discount") && objData.Discount != 0)
                {
                    price = objData.ItemPrice * objData.Quantity * (objData.Discount / 100);
                }
                else
                {
                    price = objData.ItemPrice * objData.Quantity;
                }

                IStock.BLL.Clients client = new IStock.BLL.Clients();
                IStock.BLL.ClientReturns returns = new IStock.BLL.ClientReturns ();
                returns.LoadByPrimaryKey(objData.ClientReturnID);
                client.LoadByPrimaryKey(returns.ClientID);
                client.StartCredit += price;
                client.Save();

                IStock.BLL.Items item = new IStock.BLL.Items();
                item.LoadByPrimaryKey(objData.ItemID);
                if(!objData.IsColumnNull("Valid"))
                    item.Quantity -= objData.Valid;
                item.Save();

                objData.MarkAsDeleted();
                objData.Save();
                BindItems();
            }
        }
        protected void uiGridViewItems_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteDetail")
            {
                IStock.BLL.DeliveryOrderDetails objData = new IStock.BLL.DeliveryOrderDetails();
                objData.LoadByPrimaryKey(Convert.ToInt32(e.CommandArgument.ToString()));
                decimal price = 0;
                if (!CurrentDeliveryOrder.IsColumnNull("Discount") && CurrentDeliveryOrder.Discount != 0)
                {
                    price = objData.ItemPrice * objData.Quantity * (CurrentDeliveryOrder.Discount / 100);
                }
                else
                {
                    price = objData.ItemPrice * objData.Quantity;
                }

                /* restore items to stock */
                IStock.BLL.Items item = new IStock.BLL.Items();
                item.LoadByPrimaryKey(objData.ItemID);
                item.Quantity += objData.Quantity;
                item.Save();
                /* restore items to stock */

                IStock.BLL.Clients client = new IStock.BLL.Clients();
                client.LoadByPrimaryKey(CurrentDeliveryOrder.ClientID);
                client.StartCredit -= price;
                client.Save();

                objData.MarkAsDeleted();
                objData.Save();
                BindItems();
            }
        }
        protected void uiLinkButtonAddItem_Click(object sender, EventArgs e)
        {
            IStock.BLL.ClientReturnDetails detail = new IStock.BLL.ClientReturnDetails();
            detail.AddNew();
            detail.ClientReturnID = CurrentReturn.ClientReturnID;
            //if (uiDropDownListItems.SelectedIndex != -1)
            if (!string.IsNullOrEmpty(uiHiddenFieldCurrentItem.Value))
            {
                //detail.ItemID = Convert.ToInt32(uiDropDownListItems.SelectedValue);
                detail.ItemID = Convert.ToInt32(uiHiddenFieldCurrentItem.Value);
            }
            else
            {
                uiPanelErrorItems.Visible = true;
                return;
            }
            try
            {
                detail.Quantity = Convert.ToInt32(uiTextBoxQty.Text);
            }
            catch (Exception ex)
            {
                return;
            }
            try
            {
                detail.Valid = Convert.ToInt32(uiTextBoxValid.Text);
            }
            catch (Exception ex)
            {
                return;
            }
            try
            {
                detail.ItemPrice = decimal.Parse(uiTextBoxPrice.Text);
            }
            catch (Exception ex)
            {
                return;
            }
            detail.Save();
            uiPanelErrorItems.Visible = false;

            decimal price = 0;
            if (!CurrentReturn.IsColumnNull("Discount") && CurrentReturn.Discount != 0)
            {
                price = detail.ItemPrice * detail.Quantity * (CurrentReturn.Discount / 100);
            }
            else
            {
                price = detail.ItemPrice * detail.Quantity;
            }

            IStock.BLL.Clients client = new IStock.BLL.Clients();
            client.LoadByPrimaryKey(CurrentReturn.ClientID);
            if (!client.IsColumnNull("StartCredit"))
                client.StartCredit -= price;
            else
                client.StartCredit = -price;
            client.Save();

            IStock.BLL.Items item = new IStock.BLL.Items();
            item.LoadByPrimaryKey(detail.ItemID);
            item.Quantity += detail.Valid;
            item.Save();

            BindItems();
        }
        protected void uiLinkButtonAddItem_Click(object sender, EventArgs e)
        {
            /* get items from stock */
            IStock.BLL.Items item = new IStock.BLL.Items();
            item.LoadByPrimaryKey(Convert.ToInt32(uiHiddenFieldCurrentItem.Value));
            if (!item.IsColumnNull("Quantity"))
            {
                if (item.Quantity == 0 || Convert.ToInt32(uiTextBoxQty.Text) > item.Quantity)
                {
                    ErrorDiv.Visible = true;
                    uiLabelError.Text = GetLocalResourceObject("ItemQtyError").ToString();
                    return;
                }
                item.Quantity -= Convert.ToInt32(uiTextBoxQty.Text);

            }
            else
            {
                ErrorDiv.Visible = true;
                uiLabelError.Text = GetLocalResourceObject("ItemQtyError").ToString();
                return;
            }
            item.Save();
            /* get items from stock */

            IStock.BLL.DeliveryOrderDetails detail = new IStock.BLL.DeliveryOrderDetails();
            detail.AddNew();
            detail.DeliveryOrderID = CurrentDeliveryOrder.DeliveryOrderID;
            detail.ItemID = Convert.ToInt32(uiHiddenFieldCurrentItem.Value);
            //detail.ItemID = Convert.ToInt32(uiDropDownListItems.SelectedValue);
            detail.Quantity = Convert.ToInt32(uiTextBoxQty.Text);
            detail.ItemPrice = decimal.Parse(uiTextBoxPrice.Text);
            detail.Save();

            decimal price = 0;
            if (!CurrentDeliveryOrder.IsColumnNull("Discount") && CurrentDeliveryOrder.Discount != 0)
            {
                price = detail.ItemPrice * detail.Quantity * (CurrentDeliveryOrder.Discount / 100);
            }
            else
            {
                price = detail.ItemPrice * detail.Quantity;
            }

            IStock.BLL.Clients client = new IStock.BLL.Clients();
            client.LoadByPrimaryKey(CurrentDeliveryOrder.ClientID);
            if (!client.IsColumnNull("StartCredit"))
                client.StartCredit += price;
            else
                client.StartCredit = price;
            client.Save();
            ErrorDiv.Visible = false;
            uiTextBoxItems.Text = "";
            uiTextBoxPrice.Text = "";
            uiTextBoxQty.Text = "";
            BindItems();
        }
        protected void uiGridViewOrders_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditOrder")
            {
                IStock.BLL.DeliveryOrder objData = new IStock.BLL.DeliveryOrder();
                objData.LoadByPrimaryKey(Convert.ToInt32(e.CommandArgument.ToString()));

                uiTextBoxCode.Text = objData.DeliveryOrderNo;
                uiTextBoxDate.Text = objData.DeliveryOrderDate.ToString("dd/MM/yyy");
                uiDropDownListClients.SelectedValue = objData.ClientID.ToString();
                uiDropDownListEmployee.SelectedValue = objData.EmployeeID.ToString();
                if(!objData.IsColumnNull("Discount"))
                    uiTextBoxDiscount.Text = objData.Discount.ToString();
                uiPanelAllOrders.Visible = false;
                uiPanelEditDeliveryOrder.Visible = true;
                uiPanelItems.Visible = true;
                CurrentDeliveryOrder = objData;
                //EnableDisableActions();
                BindItems();
            }
            else if (e.CommandName == "DeleteOrder")
            {
                try
                {
                    IStock.BLL.DeliveryOrder objData = new IStock.BLL.DeliveryOrder();
                    objData.LoadByPrimaryKey(Convert.ToInt32(e.CommandArgument.ToString()));

                    IStock.BLL.DeliveryOrderDetails details = new IStock.BLL.DeliveryOrderDetails();
                    details.GetDeliveryOrderDetails(objData.DeliveryOrderID);

                    decimal total = objData.GetDeliveryOrderTotals(objData.DeliveryOrderID);

                    /* restore items to stock */
                    for (int i = 0; i < details.RowCount; i++)
                    {
                        IStock.BLL.Items item = new IStock.BLL.Items();
                        item.LoadByPrimaryKey(details.ItemID);
                        item.Quantity += details.Quantity;
                        details.MoveNext();
                        item.Save();
                    }
                    /* restore items to stock */

                    details.MarkAsDeleted();
                    details.Save();

                    /* update client credit */
                    IStock.BLL.Clients client = new IStock.BLL.Clients();
                    client.LoadByPrimaryKey(objData.ClientID);
                    client.StartCredit -= total;
                    client.Save();
                    /* update client credit */

                    IStock.BLL.ClientReturns cr = new IStock.BLL.ClientReturns ();
                    cr.GetClientReturnByDeliveryOrderID(objData.DeliveryOrderID);
                    if (cr.RowCount > 0)
                    {
                        cr.SetColumnNull("DeliveryOrderID");
                        cr.Save();
                    }

                    objData.MarkAsDeleted();
                    objData.Save();

                    CurrentDeliveryOrder = null;
                    BindOrders();
                }
                catch (Exception ex)
                {
                    uipanelError.Visible = true;
                }
            }
        }
        protected void uiLinkButtonConfirm_Click(object sender, EventArgs e)
        {
            CurrentPurchaseOrder.Confirmed = true;
            CurrentPurchaseOrder.Save();
            uiLinkButtonConfirm.Enabled = false;

            IStock.BLL.PurchaseOrderDetails details = new IStock.BLL.PurchaseOrderDetails();
            details.GetPurchaseOrderDetails(CurrentPurchaseOrder.PurchaseOrderID);
            for (int i = 0; i < details.RowCount; i++)
            {
                IStock.BLL.Items item = new IStock.BLL.Items();
                item.LoadByPrimaryKey(details.ItemID);
                if (item.IsColumnNull("Quantity"))
                    item.SetColumn("Quantity", details.NoOfPackages * details.ItemOnPackages);
                else
                    item.Quantity += details.NoOfPackages * details.ItemOnPackages;
                item.Save();
                details.MoveNext();
            }
            EnableDisableControls();
            BindItems();
        }
        protected void uiLinkButtonItemTranHiostory_Click(object sender, EventArgs e)
        {
            uiReportViewerMain.Reset();
            uiReportViewerMain.LocalReport.ReportPath = BaseReportPath + "Report_GetItemTranHistory.rdlc";
            IStock.BLL.Items items = new IStock.BLL.Items();
            DateTime? From = null, To= null;
            if(!string.IsNullOrEmpty(uiTextBoxFrom.Text))
                From = DateTime.ParseExact(uiTextBoxFrom.Text, "dd/MM/yyyy", null);
            if(!string.IsNullOrEmpty(uiTextBoxTo.Text))
                To = DateTime.ParseExact(uiTextBoxTo.Text, "dd/MM/yyyy", null);

            if (!string.IsNullOrEmpty(uiHiddenFieldCurrentItem.Value))
                items.Report_GetItemTranHistory(Convert.ToInt32(uiHiddenFieldCurrentItem.Value), From, To);
            else
                items.Report_GetItemTranHistory(0, From, To);
            uiReportViewerMain.LocalReport.DataSources.Clear();
            uiReportViewerMain.LocalReport.DataSources.Add(new ReportDataSource("Report_GetItemTranHistoryDataSet", items.DefaultView));
            uiReportViewerMain.LocalReport.SetParameters(new ReportParameter("ItemID", uiHiddenFieldCurrentItem.Value));
            uiReportViewerMain.LocalReport.SetParameters(new ReportParameter("From", uiTextBoxFrom.Text));
            uiReportViewerMain.LocalReport.SetParameters(new ReportParameter("To", uiTextBoxTo.Text));
            uiReportViewerMain.LocalReport.Refresh();
        }
 protected void uiLinkButtonItemPrices_Click(object sender, EventArgs e)
 {
     if (uiDropDownListClientTypes.SelectedIndex == -1 || uiDropDownListClientTypes.SelectedValue == "0")
     {
         ClientScript.RegisterStartupScript( this.GetType(), "Error", "alert('من فضلك أختر نوع العميل');", true);
         return;
     }
     uiReportViewerMain.Reset();
     uiReportViewerMain.LocalReport.ReportPath = BaseReportPath + "Report_ItemPrices.rdlc";
     IStock.BLL.Items items = new IStock.BLL.Items();
     if(!string.IsNullOrEmpty(uiHiddenFieldCurrentItem.Value))
         items.Report_ItemPrices(Convert.ToInt32(uiHiddenFieldCurrentItem.Value), Convert.ToInt32(uiDropDownListClientTypes.SelectedValue));
     else
         items.Report_ItemPrices(0, Convert.ToInt32(uiDropDownListClientTypes.SelectedValue));
     uiReportViewerMain.LocalReport.DataSources.Clear();
     uiReportViewerMain.LocalReport.DataSources.Add(new ReportDataSource("Report_ItemPricesDataSet", items.DefaultView));
     uiReportViewerMain.LocalReport.SetParameters(new ReportParameter("ItemID", uiHiddenFieldCurrentItem.Value));
     uiReportViewerMain.LocalReport.SetParameters(new ReportParameter("ClientTypeID", uiDropDownListClientTypes.SelectedValue));
     uiReportViewerMain.LocalReport.Refresh();
 }