protected void uiLinkButtonCreateInvoice_Click(object sender, EventArgs e)
        {
            IStock.BLL.Invoices invoice = new IStock.BLL.Invoices();
            string code = invoice.getNewSerial();
            invoice.AddNew();
            invoice.InvoiceNo = code;
            invoice.ClientID = CurrentDeliveryOrder.ClientID;
            invoice.InvoiceDate = CurrentDeliveryOrder.DeliveryOrderDate;
            invoice.EmployeeID = CurrentDeliveryOrder.EmployeeID;
            invoice.DeliveryOrderID = CurrentDeliveryOrder.DeliveryOrderID;
            if (!CurrentDeliveryOrder.IsColumnNull("Discount"))
                invoice.Discount = CurrentDeliveryOrder.Discount;
            else
                invoice.Discount = 0;
            invoice.Save();

            IStock.BLL.DeliveryOrderDetails details = new IStock.BLL.DeliveryOrderDetails();
            IStock.BLL.InvoiceDetails invoicedetails = new IStock.BLL.InvoiceDetails();

            details.GetDeliveryOrderDetails(CurrentDeliveryOrder.DeliveryOrderID);

            for (int i = 0; i < details.RowCount; i++)
            {
                invoicedetails.AddNew();
                invoicedetails.InvoiceID = invoice.InvoiceID;
                invoicedetails.ItemID = details.ItemID;
                invoicedetails.ItemPrice = details.ItemPrice;
                invoicedetails.Quantity = details.Quantity;
                details.MoveNext();
            }
            invoicedetails.Save();
            //EnableDisableActions();
        }
        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;
                }
            }
        }