public Delivery GetDelivery(int docNbr)
        {
            Delivery myOrder = new Delivery();
               string _cmd = "Select * from " + PODConstants.DELIVERY_TABLE_NAME + " WHERE DocNum = " + docNbr.ToString() + " and DocType = 13"; ;
               DataTable _table = Utilities.DataBaseUtility.GetTable(_cmd);

                int nrows = _table.Rows.Count;
                if (nrows != 1) return myOrder;

                myOrder.DocNumber = Convert.ToInt32(_table.Rows[0][0]);
                myOrder.SupplierID = Convert.ToInt32(_table.Rows[0]["ExtRef"]);
                myOrder.PO_Date = Convert.ToDateTime(_table.Rows[0]["TextValue2"]);
                myOrder.DeliveryDate = Convert.ToDateTime(_table.Rows[0]["TextValue1"]);
             //   myOrder.PurchaseQuotedPrice  = Convert.ToDouble(_table.Rows[0]["QuotedPrice"]);
                myOrder.Status = Convert.ToInt32(_table.Rows[0]["status"]);
                return myOrder;
        }
        private void button_SubmitOrder_Click(object sender, EventArgs e)
        {
            Delivery del = new Delivery();
            int order_no = 0;
            int.TryParse(textBox_DocNumber.Text, out order_no);
            if (order_no == 0) { MessageBox.Show("Invalid Order Number"); return; }
            del.DocNumber = order_no;
            del.SupplierID = comboBox_Supplier.SelectedIndex = 1;
            del.Status = comboBox_Status.SelectedIndex;
            del.PO_Date = dateTimePicker1.Value;
            del.DeliveryDate = dateTimePicker2.Value;

               // textBox_Employee.Text
            //          _action = new Utilities.Action();
            //_action.DocNumber = order_no;
            //_action.ActionType = 2;
            //_action.EmployeeID = Int32.Parse(textBox_Employee.Text);
            //_action.DepartmentID = 1;    //  not textBox_Role.Text;
            //_action.ActionDate = DateTime.Now;
            //_action.Status = 2;
            //_action.Command = "";
            //_action.Comment = textBox_Comment.Text;
            Purchases.PurchaseOrder po = new Purchases.PurchaseOrder();
            po.PO_ID = order_no;
            po.SupplierID = comboBox_Supplier.SelectedIndex + 0;
            po.PO_Status = comboBox_Status.SelectedIndex;
            po.EmployeeID = Int32.Parse(textBox_Employee.Text);
            po.PO_Date = dateTimePicker1.Value;
            po.DeliveryDate = dateTimePicker2.Value;
            po.Comment = textBox_Comment.Text;
            if (po.Comment.Length < 1)
                po.Comment = "create PO " + po.PO_ID.ToString();
            if (Convert.ToDateTime(dateTimePicker1.Text) > Convert.ToDateTime(dateTimePicker2.Text))
            {
                MessageBox.Show("Start Date should not be greater than End Date"); return;
            }
            string[] cmds = textBox_Details.Text.Split(';');
            int last = cmds.Length - 1;
            for (int i = 0; i < last; i++)
            {
                //action.Command = ;
                DataBaseUtility.Execute(cmds[i].Trim(), _ident);
            }
            ////     if (po.Comment.Length < 1)
               //         po.Comment = "create PO " + po.PO_ID.ToString();
               //     if (Convert.ToDateTime(dateTimePicker1.Text) > Convert.ToDateTime(dateTimePicker2.Text))
               //     {
               //         MessageBox.Show("Start Date should not be greater than End Date"); return;
               //     }
            cmds = textBox_Details.Text.Split(';');
            last = cmds.Length - 1;
            for (int i = 0; i < last; i++)
            {
              //  _action.Command = cmds[i];
              //  DataBaseUtility.Execute(_action, _ident);
            }
        }
        //private void FillProductDetails(int _id)
        //{
        //    listBox_Products.Items.Clear();
        //    //   listBox_Quant.Items.Clear();
        //    //   listBox_Cost.Items.Clear();
        //    foreach (DataRow drProdut in dt_PurchaseOrder.Rows)
        //    {
        //        listBox_Products.Items.Add(Convert.ToString(drProdut["Name"]));
        //        //     listBox_Quant.Items.Add(Convert.ToString(drProdut["Qty"]).PadLeft(5, ' '));
        //        //     listBox_Cost.Items.Add(Convert.ToString(drProdut["Cost"]).PadLeft(5, ' '));
        //    }
        //}
        private void button_Preview_Click(object sender, EventArgs e)
        {
            if (radioButton_SQL.Checked) radioButton_SQL_CheckedChanged(this, null);
            del = new Delivery(textBox_Details.Text );
            //int order_no = 0;
            //int.TryParse(textBox_DocNumber.Text, out order_no);
            //if (order_no == 0) { MessageBox.Show("Invalid Order Number"); return; }
            textBox_DocNumber.Text = del.DocNumber.ToString();  // = order_no;
            textBox_Supplier.Text = del.SupplierID.ToString();  // = Int32.Parse(textBox_Supplier.Text);
              //  comboBox_Supplier.SelectedIndex = del.SupplierID;
            comboBox_Status.SelectedIndex = del.Status;
            textBox_Comment.Text = del.Comment;
            textBox_Employee.Text = del.EmployeeID.ToString();   // _ident.UserID.ToString();
            textBox_AuthLevel.Text = _ident.ActionLevel.ToString();
            textBox_Role.Text = _ident.Role;
            dateTimePicker1.Value = DateTime.Now;  // Parse(_po.PO_Date).ToShortDateString();
              //  dateTimePicker1.Text = DateTime.Parse(_po.PO_Date).ToShortDateString();
            if (_del.SupplierID > 0)
            {
                _supplierID = _del.SupplierID;
                textBox_Supplier.Text = _supplierID.ToString();
                FillSupplier(_supplierID.ToString());
            }
            for (int i = 0; i < del.DeliveryDetails.Count; i++)
            {
                dataGridResults.Rows.Add();
                Del_DetailItem det = del.DeliveryDetails[i];
                dataGridResults.Rows[i].Cells[0].Value = det.Del_ID;
                dataGridResults.Rows[i].Cells[1].Value = det.ProductID ;
                dataGridResults.Rows[i].Cells[2].Value = det.Quantity ;
                dataGridResults.Rows[i].Cells[3].Value = det.Price ;

            }
            //dateTimePicker1.Value  = (DateTime) del.PO_Date.ToOADate();
            //del.DeliveryDate = dateTimePicker2.Value ;
            //del.PO_Date = dateTimePicker1.Value;
            //del.DeliveryDate = dateTimePicker2.Value ;
            //del.Comment = textBox_Comment.Text;
            //if (Convert.ToDateTime(dateTimePicker1.Text) > Convert.ToDateTime(dateTimePicker2.Text))
            //{
            //    MessageBox.Show("Start Date should not be greater than End Date"); return;
            //}
            //    string part1 = "INSERT into Deliveries values (";
        }
        private void button_FromPO_Click(object sender, EventArgs e)
        {
            //List<string> poStr = Utilities.DataBaseUtility.GetList("Select * from purchaseOrders where docnum = " + textBox_DocNumber.Text );
             //string[] fields = poStr[1].Split('\t');
             // _po = new PurchaseOrder(fields);
             // _del = new Delivery(fields);
             _ident = new IdentityObject();
             _poID_Str = textBox_DocNumber.Text.Trim();
             _poRecord = Utilities.DataBaseUtility.GetList("select * from PURCHASE_ORDERS where docnum = " + _poID_Str,',');
             if (_poRecord.Count != 2)
             {
                 MessageBox.Show("could not find purchase order :  " + _poID_Str);
                 return;
             }
             textBox_Details.Text = _poRecord[1] + "\r\n";
             //    _po = new PurchaseOrder(_poRecord[1]);
             _del = new Delivery(_poRecord[1]);
             textBox_Supplier.Text = _del.SupplierID.ToString();
             if (_ident == null) _ident = new IdentityObject("111", "Johnson", "clerk", 1, 2);
             textBox_Employee.Text = _ident.UserID.ToString();  // _del.EmployeeID.ToString();
             dateTimePicker1.Value  = DateTime.Now;  //.ToShortDateString();
             //  dateTimePicker1.Text = DateTime.Now.ToShortDateString();
             //   Parse(_po.PO_Date).ToShortDateString ();
             if (_del.SupplierID > 0)
             {
                 _supplierID = _del.SupplierID;
                 textBox_Supplier.Text = _supplierID.ToString();
                 FillSupplier(_supplierID.ToString());
                 FillProducts(_supplierID);
             }
             textBox_Comment.Text = "P. O. " + _poID_Str  + " delivery";
             // NewDelivery(_ident);
             // string _titles = String.Format("{0}\t{1}\t{2}\t{3}", "Del.ID", "prod.ID", "Quant.", "Cost");
             // textBox_Details.Text = _titles;
             List<string> detailRecords = Utilities.DataBaseUtility.GetList("select * from PurchaseOrderDetails where docnum = " + _poID_Str, ',');
             for (int i = 1; i < detailRecords.Count; i++)
             {
                 textBox_Details.Text += detailRecords[i] + "\r\n";

             }
        }
        private void button_Display_Click(object sender, EventArgs e)
        {
            try
            {

                Delivery _order = new Delivery();
                string _cmd = "Select * from " + PODConstants.DELIVERY_TABLE_NAME + " WHERE DocNum = " + textBox_DocNumber.Text + " and DocType = 13";
                DataTable _table = Utilities.DataBaseUtility.GetTable(_cmd);
                dataGridView1.DataSource = _table;
                string _supplierID = "3";
                if (_table != null && _table.Rows != null && _table.Rows.Count > 0)
                    _supplierID = _table.Rows[0][2].ToString();
                string supplierCmd = "Select * from suppliers WHERE supplierID = " + _supplierID;
                DataTable supplierTable = Utilities.DataBaseUtility.GetTable(supplierCmd);
                dataGridView2.DataSource = supplierTable;
                string detailsCmd = "Select * from " + PODConstants.DELIVERY_DETAILS_TABLE_NAME + " WHERE DocNum = " + Convert.ToInt32(textBox_DocNumber.Text) + " and DocType = 13";
                detailsTable = Utilities.DataBaseUtility.GetTable(detailsCmd);
                gboOrderStatus.Visible = true;

                _order = _order.GetDelivery(Convert.ToInt32(textBox_DocNumber.Text));
                string _supplierName = Convert.ToString(supplierTable.Rows[0]["Name"]);
                string _supplierAddress = Convert.ToString(supplierTable.Rows[0]["Address"]);
                string _supplierAddress2 = Convert.ToString(supplierTable.Rows[0]["City"]) + ", " + Convert.ToString(supplierTable.Rows[0]["State"]) + " " + Convert.ToString(supplierTable.Rows[0]["PostalCode"]);
                string _supplierPhone = Convert.ToString(supplierTable.Rows[0]["Phone"]);
                string _supplierEmail = Convert.ToString(supplierTable.Rows[0]["WebAddress"]);

                lblInvNumValue.Text = textBox_DocNumber.Text;    //_ Convert.ToString(_order.DeliveryID);
                //label_PO_Info.Text = _supplierName + "\r\n";
                label_SupplierName.Text = _supplierName;
                label_SupplierAddr.Text = _supplierAddress;
                label_SupplierAddress2.Text = _supplierAddress2;
                lblPhone.Text = Convert.ToString(_supplierPhone);
                label_ShipName.Text = "Local Bikes";
                label_ShippingAddr2.Text = "2700 Bay Area Blvd";
                label_ShipAddress.Text = "Houston, TX, 77058";
                lblDateValue.Text = _order.PO_Date.ToShortDateString();
                label_SupplierEmail.Text = _supplierEmail;
                comboBox2.SelectedIndex = _order.Status;
                label_Status.Text = "Status: " + comboBox2.SelectedItem.ToString();
                label_deliverydate.Text = _order.DeliveryDate.ToShortDateString();
                gboOrderStatus.Visible = true;
                int recnbr1 = 0;
                double partsTotal = 0.0;  //           double partsTax = 0.0;
                lvProducts.Items.Clear();
                while (recnbr1 < detailsTable.Rows.Count)
                {
                    string productCode = detailsTable.Rows[recnbr1][2].ToString();
                    _product_Names = Utilities.DataBaseUtility.GetList("Select productName from products where productID = " + productCode);
                    ListViewItem lvi = lvProducts.Items.Add(productCode);  //detailsTable.Rows[recnbr1][1].ToString());
                    if (_product_Names.Count > 1 && _product_Names[1] != null)
                    {
                        lvi.SubItems.Add(_product_Names[1]);
                    }
                    //else
                    //{
                    //    MessageBox.Show("Please check the products and inventory data, the inventory product " + productCode + " not available in products table");
                    //}
                    //lvi.SubItems.Add(detailsTable.Rows[recnbr1][2].ToString());
                    double _price = Double.Parse(detailsTable.Rows[recnbr1][6].ToString());
                    double _quant = Double.Parse(detailsTable.Rows[recnbr1][5].ToString());
                    lvi.SubItems.Add(_quant.ToString());
                    lvi.SubItems.Add(string.Format("{0:C}", _price));
                    lvi.SubItems.Add(string.Format("{0:C}", 0.00));
                    double _total = _quant * _price;
                    lvi.SubItems.Add(string.Format("{0:C}", _total));

                    partsTotal += _total;    // order.POProdDetails[recnbr1].DetailProductLineTotal;
                    recnbr1++;
                }

                lblPartsSubTotalValue.Text = string.Format("{0:C}", partsTotal);

                // int recnbr2 = 0;
                // double serviceTotal = 0.0;

                lblShippingCostValue.Text = string.Format("{0:C}", partsTotal * .04);
                double _totalCost = partsTotal + partsTotal * .04;
                lblGrandTotalValue.Text = string.Format("{0:C}", _totalCost);

                // pnlService.Visible = true;

            }
            catch (Exception ex) { MessageBox.Show("Due to invalid or old data in Database, Application could not load the Delivery doc."); }
        }
        private void LoadDelivery()
        {
            //   int poNum;
            Delivery _order = new Delivery();

            List<string> _ids = Utilities.DataBaseUtility.GetList("Select DocNum from " + PODConstants.DELIVERY_TABLE_NAME + " Where DocType = 13"); //order by PurchasOrderID");
            comboBox1.Items.Clear();
            for (int i =1; i< _ids.Count; i++)
                comboBox1.Items.Add(_ids[i]);
        }