private bool Populate_Inventory_Fields(ref PickingSlipLine qlQouteLine, bool bFocusDescription, string sDelNotes)
        {
            string sInventoryGroup = "";
            bool bExist = false;
            if (sDelNotes == "")
            {
                if (qlQouteLine.txtCode.Text == "'")
                {
                    //qlQouteLine.txtMultiplier.ReadOnly = true;
                    qlQouteLine.txtDescription.ReadOnly = false;
                    qlQouteLine.txtDiscount.ReadOnly = true;
                    qlQouteLine.txtExcPrice.ReadOnly = true;
                    qlQouteLine.txtLastInvoiceDate.Text = "";
                    qlQouteLine.txtNet.ReadOnly = true;
                    //qlQouteLine.txtQuantity.ReadOnly = true;
                    //qlQouteLine.txtQuantity.Text = "0.00";
                    qlQouteLine.txtUnit.ReadOnly = true;
                    qlQouteLine.txtDescription.Focus();
                    qlQouteLine.bDoCalculation = false;
                    bAllowDuplicateLines = false;
                    return true;
                }
                else
                {
                    //qlQouteLine.txtMultiplier.ReadOnly = false;
                    qlQouteLine.txtDiscount.ReadOnly = false;
                    qlQouteLine.txtExcPrice.ReadOnly = false;
                    //qlQouteLine.txtQuantity.ReadOnly = false;
                    qlQouteLine.txtDescription.ReadOnly = false;

                    qlQouteLine.bDoCalculation = true;
                    using (PsqlConnection oConn = new PsqlConnection(Solsage_Process_Management_System.Classes.Connect.sPastelConnStr))
                    {
                        oConn.Open();
                        string sSql = "SELECT distinct  Inventory.*, MultiStoreTrn.SellExcl01 from Inventory left join MultiStoreTrn on Inventory.ItemCode = MultiStoreTrn.ItemCode";
                        sSql += " where Inventory.ItemCode = '" + qlQouteLine.txtCode.Text.Trim() + "'";
                        sSql += " AND MultiStoreTrn.StoreCode <> ''";

                        PsqlDataReader rdReader = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteReader();
                        while (rdReader.Read())
                        {

                            if (!bNextLine)
                            {
                                bNextLine = true;
                                PickingSlipLine qlNewline = new PickingSlipLine();
                                ((Forms.Project.PickingSlip)(Parent.Parent.Parent.Parent)).AddSalesLine(qlNewline,false);

                            }
                            qlQouteLine.txtDescription.Text = rdReader["Description"].ToString().Trim();

                            try
                            {
                                qlQouteLine.txtExcPrice.Text = Convert.ToDecimal(rdReader["SellExcl01"].ToString()).ToString("N2");
                                dOriginalUnitPrice = Convert.ToDecimal(rdReader["SellExcl01"].ToString());
                            }
                            catch
                            {
                                MessageBox.Show("Error calculating Excluding Price of item.");
                                qlQouteLine.txtExcPrice.Text = "0.00";
                            }
                            qlQouteLine.txtTaxType.Text = rdReader["SalesTaxType"].ToString().Trim();
                            qlQouteLine.txtDiscountType.Text = rdReader["DiscountType"].ToString().Trim();

                            qlQouteLine.sLineType = Functions.getLineInventoryType(qlQouteLine.txtCode.Text.Trim());

                            if (qlQouteLine.txtTaxType.Text == "0" || qlQouteLine.txtTaxType.Text == "2")
                            {
                                qlQouteLine.txtNet.BackColor = Color.Yellow;
                            }
                            else
                            {
                                qlQouteLine.txtNet.BackColor = Color.White;
                            }

                            if (rdReader["DiscountType"].ToString() == "0" || rdReader["DiscountType"].ToString() == "1")//AJD 20-08-2009
                            {
                                qlQouteLine.txtDiscount.ReadOnly = true;
                            }
                            qlQouteLine.txtUnit.Text = rdReader["UnitSize"].ToString();
                            qlQouteLine.txtDescription.Focus();
                            bExist = true;

                        }//end while
                        rdReader.Close();
                        oConn.Dispose();

                        //get on hand qty of itemcode
                        using (PsqlConnection oConn2 = new PsqlConnection(Solsage_Process_Management_System.Classes.Connect.sConnStr))
                        {
                            //get del note  qty
                            decimal dDelNoteQty = 0;
                            oConn2.Open();

                            string sSql2 = "select Itemcode,SUM(Qty) as Qty from SOLPDL where DocNumber in " + sDelNotes;
                            sSql2 += " AND ItemCode = '" + qlQouteLine.txtCode.Text + "' group by ItemCode";
                            PsqlDataReader rdReader2 = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql2, oConn2).ExecuteReader();
                            while (rdReader2.Read())
                            {
                                dDelNoteQty = Convert.ToDecimal(rdReader2["Qty"].ToString());
                            }
                            rdReader2.Close();
                            //get ret note qty
                            decimal dRetNoteQty = 0;

                            string sSql3 = "select SOLPRL.ItemCode, SUM(SOLPRL.Qty) as Qty from SOLPRH ";
                            sSql3 += "inner join SOLPRL on SOLPRL.DocNumber = SOLPRH.DocNumber ";
                            sSql3 += "where LinkedDeliveryNote in " + sDelNotes + " And ItemCode = '" + qlQouteLine.txtCode.Text.Trim() + "' group by ItemCode";
                            PsqlDataReader rdReader3 = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql3, oConn2).ExecuteReader();
                            while (rdReader3.Read())
                            {
                                dRetNoteQty = Convert.ToDecimal(rdReader3["Qty"].ToString());
                            }
                            rdReader3.Close();
                            oConn2.Dispose();
                            decimal dOnHandQty = dDelNoteQty - dRetNoteQty;
                            if (dOnHandQty < 0)
                                qlQouteLine.txtQtyOnHand.Text = "0";
                            else
                                qlQouteLine.txtQtyOnHand.Text = dOnHandQty.ToString("N2");
                        }
                        if (!bExist)
                        {

                            MessageBox.Show("Code does not exist.", "Inventory Not Found", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            qlQouteLine.txtDescription.ReadOnly = true;
                            return false;
                        }
                        else
                        {

                        }
                        return true;
                    }
                }
            }
            else
            {
                if (qlQouteLine.txtCode.Text == "'")
                {
                    qlQouteLine.txtDescription.ReadOnly = false;
                    qlQouteLine.txtLastInvoiceDate.Text = "";
                    //qlQouteLine.txtQuantity.ReadOnly = true;
                    //qlQouteLine.txtQuantity.Text = "0.00";
                    qlQouteLine.txtUnit.ReadOnly = true;
                    qlQouteLine.txtDescription.Focus();
                    qlQouteLine.bDoCalculation = false;
                    bAllowDuplicateLines = false;
                    return true;
                }
                else if (!bNextLine)
                {
                    if (bFocusDescription)
                    {
                        bNextLine = true;
                        using (PsqlConnection oConn = new PsqlConnection(Solsage_Process_Management_System.Classes.Connect.sPastelConnStr))
                        {
                            oConn.Open();
                            string sSql = "SELECT MultiStoreTrn.SellExcl01 from Inventory left join MultiStoreTrn on Inventory.ItemCode = MultiStoreTrn.ItemCode";
                            sSql += " where Inventory.ItemCode = '" + qlQouteLine.txtCode.Text.Trim() + "'";
                            sSql += " AND MultiStoreTrn.StoreCode <> ''";

                            PsqlDataReader rdReader = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteReader();
                            while (rdReader.Read())
                            {
                                qlQouteLine.txtExcPrice.Text = Convert.ToDecimal(rdReader["SellExcl01"].ToString()).ToString("N2");
                            }
                            rdReader.Close();
                            oConn.Dispose();
                        }

                        PickingSlipLine qlNewline = new PickingSlipLine();
                        ((Forms.Project.PickingSlip)(Parent.Parent.Parent.Parent)).AddSalesLine(qlNewline,false);
                    }
                    else
                    {
                        using (PsqlConnection oConn = new PsqlConnection(Solsage_Process_Management_System.Classes.Connect.sPastelConnStr))
                        {
                            oConn.Open();
                            string sSql = "SELECT MultiStoreTrn.SellExcl01 from Inventory left join MultiStoreTrn on Inventory.ItemCode = MultiStoreTrn.ItemCode";
                            sSql += " where Inventory.ItemCode = '" + qlQouteLine.txtCode.Text.Trim() + "'";
                            sSql += " AND MultiStoreTrn.StoreCode <> ''";

                            PsqlDataReader rdReader = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteReader();
                            while (rdReader.Read())
                            {
                                qlQouteLine.txtExcPrice.Text = Convert.ToDecimal(rdReader["SellExcl01"].ToString()).ToString("N2");
                            }
                            rdReader.Close();
                            oConn.Dispose();
                        }
                    }
                }
                return true;
            }
        }
        public void AddQouteLine(PickingSlipLine qlNewLine)
        {
            Array.Resize<Control>(ref aSaleslines, aSaleslines.Length + 1);
            aSaleslines[aSaleslines.Length - 1] = qlNewLine;
            if (iLineRowIndex < 14)
                qlNewLine.Top = 17 + ((iLineRowIndex) * 20);
            else
                qlNewLine.Top = 268; //16 + ((iLineRowIndex) * 18);

            qlNewLine.Left = 4;

            qlNewLine.TabIndex = 50 + aSaleslines.Length;
            qlNewLine.TabStop = true;

            qlNewLine.iLineIndex = aSaleslines.Length - 1;
            qlNewLine.Name = "qlNewLine_" + (aSaleslines.Length - 1).ToString();

            this.pnlDetails.Controls.Add(qlNewLine);
            qlNewLine.BringToFront();
            iLineRowIndex++;
        }
        private void cmdCodeSearch_Click(object sender, EventArgs e)
        {
            bool bDelNotes = false;
            Cursor = System.Windows.Forms.Cursors.WaitCursor;
            string sDelNotes = "(";
            //create del note string list
                foreach (DataGridViewRow dgRow in ((Forms.Project.PickingSlip)(Parent.Parent.Parent.Parent)).dgDeliveryNotes.Rows)
                {
                   sDelNotes += ",'" + dgRow.Cells[0].Value.ToString() + "'";
                   bDelNotes = true;
                }
                try
                {
                    sDelNotes = sDelNotes.Remove(1, 1);
                    sDelNotes = sDelNotes + ")";
                }
                catch
                {

                }
                if (!bDelNotes)
                {
                    using (Finder.Inventory frmInventory = new Solsage_Process_Management_System.Finder.Inventory())
                    {
                        //Sending "None" to inventory ShowDialog to identify that no workshop action should be taken
                        if (frmInventory.ShowDialog("", "", sDelNotes) == DialogResult.OK)
                        {
                            if (frmInventory.sResult != "")
                            {
                                //check if this item has been selected...
                                if (((Forms.Project.PickingSlip)(Parent.Parent.Parent.Parent)).CheckDuplicateSalesLine(frmInventory.sResult))
                                {
                                    MessageBox.Show("No duplicate items are allowed in a sales order. Please select another inventory item.", "Duplicates", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                }
                                else
                                {
                                    txtCode.Text = frmInventory.sResult.Trim();
                                    PickingSlipLine pdlDeliveryNote = this;
                                    bool bValid = Populate_Inventory_Fields(ref pdlDeliveryNote, true, sDelNotes);
                                    txtCode.Focus();
                                    txtCode.SelectionStart = 0;
                                    txtCode.SelectionLength = txtCode.Text.Length;
                                }
                            }
                        }
                    }
                }
                else
                {
                    using (Finder.PickingSlipItemZoom frmPickSlipItemZoom = new Finder.PickingSlipItemZoom())
                    {
                        if (frmPickSlipItemZoom.ShowDialog(sDelNotes) == DialogResult.OK)
                        {
                            if (frmPickSlipItemZoom.ItemList.Count != 0)
                            {
                                ItemList = frmPickSlipItemZoom.ItemList;
                                if (ItemList.Count == 1)
                                {
                                    foreach (string sItem in ItemList)
                                    {

                                        string[] aItemDetails = sItem.Split("|".ToCharArray());
                                        txtCode.Text = aItemDetails[0].ToString();
                                        txtDescription.Text = aItemDetails[1].ToString();
                                        txtUnit.Text = aItemDetails[3].ToString();
                                        //get on hand qty of itemcode
                                        using (PsqlConnection oConn2 = new PsqlConnection(Solsage_Process_Management_System.Classes.Connect.sConnStr))
                                        {
                                            //get del note  qty
                                            decimal dDelNoteQty = 0;
                                            oConn2.Open();
                                            //sDelNotes = sDelNotes.Remove(0, 2);
                                            //sDelNotes = sDelNotes.Remove(1, 1) + ")";

                                            string sSql2 = "select Itemcode,SUM(Qty) as Qty from SOLPDL where DocNumber in " + sDelNotes;
                                            sSql2 += " AND ItemCode = '" + txtCode.Text + "' group by ItemCode";
                                            PsqlDataReader rdReader2 = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql2, oConn2).ExecuteReader();
                                            while (rdReader2.Read())
                                            {
                                                dDelNoteQty = Convert.ToDecimal(rdReader2["Qty"].ToString());
                                            }
                                            rdReader2.Close();

                                                //get ret note qty
                                                decimal dRetNoteQty = 0;
                                                string sSql3 = "select SOLPRL.ItemCode, SUM(SOLPRL.Qty) as Qty from SOLPRH ";
                                                sSql3 += "inner join SOLPRL on SOLPRL.DocNumber = SOLPRH.DocNumber ";
                                                sSql3 += "where LinkedDeliveryNote in " + sDelNotes + " And ItemCode = '" + txtCode.Text.Trim() + "' group by ItemCode";
                                                PsqlDataReader rdReader3 = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql3, oConn2).ExecuteReader();
                                                while (rdReader3.Read())
                                                {
                                                    dRetNoteQty = Convert.ToDecimal(rdReader3["Qty"].ToString());
                                                }
                                                rdReader3.Close();
                                                decimal dOnHandQty = dDelNoteQty - dRetNoteQty;
                                                if (dOnHandQty < 0)
                                                    txtQtyOnHand.Text = "0";
                                                else
                                                    txtQtyOnHand.Text = dOnHandQty.ToString("N2");
                                                oConn2.Dispose();
                                            }

                                        }
                                        PickingSlipLine pdlDeliveryNote = this;
                                        PickingSlipLine pdlLastControl = this;
                                        bool bValid = Populate_Inventory_Fields(ref pdlDeliveryNote, true,sDelNotes);
                                }
                                else
                                {

                                    foreach (string sItem in ItemList)
                                    {

                                        string[] aItemDetails = sItem.Split("|".ToCharArray());

                                        //txtCode.Text = aItemDetails[0].ToString();
                                        //txtDescription.Text = aItemDetails[1].ToString();
                                        //txtQuoteQty.Text = aItemDetails[2].ToString();
                                        //txtUnit.Text = aItemDetails[3].ToString();
                                        //ProjectDeliveryNote pdlDeliveryNote = this;
                                        PickingSlipLine pdlDelNote = new PickingSlipLine();
                                        pdlDelNote.txtCode.Text = aItemDetails[0].ToString();
                                        pdlDelNote.txtDescription.Text = aItemDetails[1].ToString();
                                        pdlDelNote.txtUnit.Text = aItemDetails[3].ToString();
                                        using (PsqlConnection oConn = new PsqlConnection(Solsage_Process_Management_System.Classes.Connect.sPastelConnStr))
                                        {
                                            oConn.Open();
                                            string sSql = "SELECT MultiStoreTrn.SellExcl01 from Inventory left join MultiStoreTrn on Inventory.ItemCode = MultiStoreTrn.ItemCode";
                                            sSql += " where Inventory.ItemCode = '" + pdlDelNote.txtCode.Text.Trim() + "'";
                                            sSql += " AND MultiStoreTrn.StoreCode <> ''";

                                            PsqlDataReader rdReader = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteReader();
                                            while (rdReader.Read())
                                            {
                                                pdlDelNote.txtExcPrice.Text = Convert.ToDecimal(rdReader["SellExcl01"].ToString()).ToString("N2");
                                            }
                                            rdReader.Close();
                                            oConn.Dispose();
                                        }
                                        //bool bValid = Populate_Inventory_Fields(ref pdlDelNote, false, sDelNotes);

                                        //get on hand qty of itemcode
                                        using (PsqlConnection oConn2 = new PsqlConnection(Solsage_Process_Management_System.Classes.Connect.sConnStr))
                                        {
                                            //get del note  qty
                                            decimal dDelNoteQty = 0;
                                            oConn2.Open();
                                            this.removeLine(null, null);
                                            string sSql2 = "select Itemcode,SUM(Qty) as Qty from SOLPDL where DocNumber in " + sDelNotes;
                                            sSql2 += " AND ItemCode = '" + pdlDelNote.txtCode.Text + "' group by ItemCode";
                                            PsqlDataReader rdReader2 = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql2, oConn2).ExecuteReader();
                                            while (rdReader2.Read())
                                            {
                                                dDelNoteQty = Convert.ToDecimal(rdReader2["Qty"].ToString());
                                            }
                                            rdReader2.Close();
                                            //get ret note qty
                                            decimal dRetNoteQty = 0;
                                            string sSql3 = "select SOLPRL.ItemCode, SUM(SOLPRL.Qty) as Qty from SOLPRH ";
                                            sSql3 += "inner join SOLPRL on SOLPRL.DocNumber = SOLPRH.DocNumber ";
                                            sSql3 += "where LinkedDeliveryNote in " + sDelNotes + " And ItemCode = '" + pdlDelNote.txtCode.Text.Trim() + "' group by ItemCode";
                                            PsqlDataReader rdReader3 = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql3, oConn2).ExecuteReader();
                                            while (rdReader3.Read())
                                            {
                                                dRetNoteQty = Convert.ToDecimal(rdReader3["Qty"].ToString());
                                            }
                                            rdReader3.Close();
                                            decimal dOnHandQty = dDelNoteQty - dRetNoteQty;
                                            if (dOnHandQty < 0)
                                                pdlDelNote.txtQtyOnHand.Text = "0";
                                            else
                                                pdlDelNote.txtQtyOnHand.Text = dOnHandQty.ToString("N2");
                                            oConn2.Dispose();
                                        }

                                        //ProjectDeliveryNote pdlLastControl = this;
                                        //bool bValid = Populate_Inventory_Fields(ref pdlDeliveryNote, false, iLineIndex);
                                        ((Forms.Project.PickingSlip)(Parent.Parent.Parent.Parent)).AddSalesLine(pdlDelNote,true);

                                    }
                                    PickingSlipLine pdlDelNoteNew = new PickingSlipLine();
                                    ((Forms.Project.PickingSlip)(Parent.Parent.Parent.Parent)).AddSalesLine(pdlDelNoteNew,true);
                                    ((Forms.Project.PickingSlip)(Parent.Parent.Parent.Parent)).deleteSalesLine(this, true);

                                }
                            }
                        }

                    }

                }

            //((Forms.Project.PickingSlip)(Parent.Parent.Parent.Parent)).addTotals();
            Cursor = System.Windows.Forms.Cursors.Default;
        }
        public void InsertSalesLine(int iLineIndex, PickingSlipLine slNewLine)
        {
            for (int iLines = 0; iLines < aSaleslines.Length; iLines++)
            {
                PickingSlipLine slThisline = (((PickingSlipLine)aSaleslines[iLines]));

                if (slThisline.iLineIndex == iLineIndex)//start line
                {
                    //Array.Resize<Control>(ref aSaleslines, aSaleslines.Length + 1);//Add new row
                    iLineRowIndex++;
                    for (int iShiftLines = aSaleslines.Length-1; iShiftLines > iLines+1; iShiftLines--)
                    {
                        aSaleslines[iShiftLines] = aSaleslines[iShiftLines - 1];
                        (((PickingSlipLine)aSaleslines[iShiftLines])).Location = new Point((((PickingSlipLine)aSaleslines[iShiftLines - 1])).Location.X, (((PickingSlipLine)aSaleslines[iShiftLines - 1])).Location.Y + 20); // move location of control to new position
                        (((PickingSlipLine)aSaleslines[iShiftLines])).iLineIndex++;//sync the lineindex of the control array
                    }
                    aSaleslines[iLines + 1] = slNewLine;
                    slNewLine.Top = 17 + ((iLineIndex+1) * 20);
                    slNewLine.Left = 4;
                    if (slNewLine.txtStore.Text == "")
                    {
                        slNewLine.txtStore.Text = Global.sDefaultStore;
                    }
                    slNewLine.TabIndex = 50 + aSaleslines.Length;
                    slNewLine.TabStop = true;
                    slNewLine.iLineIndex = iLines + 1;
                    slNewLine.Name = "slNewLine_" + (aSaleslines.Length - 1).ToString();
                    this.pnlDetails.Controls.Add(slNewLine);
                    slNewLine.BringToFront();
                    return;
                }
            }
        }
        public void loadLines(string sSalesCode, string sStore, string sCode, string sDescription, string sUnit, string sQty, string sExcPrice, string sDiscount, string sTaxType, string sLinkNum, string sDiscountType, string sLineType)
        {
            bool bLeaseItem = false;
            bool bInsert = false;
            int iInsertPos = 0;
            //LL Discount displyed incorrectly 18/09/2009
            double dDiscount = Convert.ToDouble(sDiscount.Trim()) / 100;
            PickingSlipLine slNewLine = new PickingSlipLine();
            slNewLine.bDoCalculation = false; //Do totals only after the control has been loaded.
            slNewLine.txtStore.Text = sStore.Trim();
            slNewLine.txtCode.Text = sCode.Trim();
            slNewLine.txtDescription.Text = sDescription.Trim();
            slNewLine.txtUnit.Text = sUnit.Trim();
            slNewLine.txtQuantity.Text = (Convert.ToDouble(sQty.Trim())).ToString("N2");
            slNewLine.txtExcPrice.Text = (Convert.ToDouble(sExcPrice.Trim())).ToString("N2");
            slNewLine.txtDiscount.Text =  dDiscount.ToString("N2");
            slNewLine.txtDiscountType.Text = sDiscountType;
            slNewLine.bNextLine = true; //Don't allow new rows if code field loose focus()
            double dTotalExDiscount = Convert.ToDouble(slNewLine.txtQuantity.Text.Replace(",", "")) * Convert.ToDouble(slNewLine.txtExcPrice.Text.Replace(",", ""));
            slNewLine.txtNet.Text = (dTotalExDiscount - (dTotalExDiscount * (Convert.ToDouble(slNewLine.txtDiscount.Text.Replace(",", "")) / 100))).ToString("N2");
            slNewLine.sPastelLineLink = sLinkNum;
            slNewLine.txtTaxType.Text = sTaxType;

            if (sTaxType == "0" || sTaxType == "2")
            {
                slNewLine.txtNet.BackColor = Color.Yellow;
            }

            if (slNewLine.txtCode.Text == "'")
            {
                slNewLine.txtQuantity.Visible = false;
                slNewLine.txtDiscount.Visible = false;
                slNewLine.txtExcPrice.Visible = false;
                slNewLine.txtNet.Visible = false;
            }
            //get on hand qty of itemcode
            string sDelNotes = "(";
            foreach (DataGridViewRow dgRow in dgDeliveryNotes.Rows)
            {
                sDelNotes += ",'" + dgRow.Cells[0].Value.ToString() + "'";
            }

            using (PsqlConnection oConn2 = new PsqlConnection(Solsage_Process_Management_System.Classes.Connect.sConnStr))
            {
                if (slNewLine.txtCode.Text != "'")
                {
                    //get del note  qty
                    decimal dDelNoteQty = 0;
                    oConn2.Open();
                    try
                    {
                        sDelNotes = sDelNotes.Remove(1, 1) + ")";
                    }
                    catch
                    {
                        sDelNotes = "('')";
                    }

                        string sSql2 = "select Itemcode,SUM(Qty) as Qty from SOLPDL where DocNumber in " + sDelNotes;
                        sSql2 += " AND ItemCode = '" + slNewLine.txtCode.Text + "' group by ItemCode";
                        PsqlDataReader rdReader2 = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql2, oConn2).ExecuteReader();
                        while (rdReader2.Read())
                        {
                            dDelNoteQty = Convert.ToDecimal(rdReader2["Qty"].ToString());
                        }
                        rdReader2.Close();
                        //get ret note qty
                        decimal dRetNoteQty = 0;
                        string sSql3 = "select SOLPRL.ItemCode, SUM(SOLPRL.Qty) as Qty from SOLPRH ";
                        sSql3 += "inner join SOLPRL on SOLPRL.DocNumber = SOLPRH.DocNumber ";
                        sSql3 += "where LinkedDeliveryNote in " + sDelNotes + " And ItemCode = '" + slNewLine.txtCode.Text.Trim() + "' group by ItemCode";
                        PsqlDataReader rdReader3 = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql3, oConn2).ExecuteReader();
                        while (rdReader3.Read())
                        {
                            dRetNoteQty = Convert.ToDecimal(rdReader3["Qty"].ToString());
                        }
                        rdReader3.Close();
                        oConn2.Dispose();
                        decimal dOnHandQty = dDelNoteQty - dRetNoteQty;
                        if (dOnHandQty < 0)
                            slNewLine.txtQtyOnHand.Text = "0";
                        else
                            slNewLine.txtQtyOnHand.Text = dOnHandQty.ToString("N2");
                }
            }

                AddSalesLine(slNewLine);
        }
        public void deleteSalesLine(PickingSlipLine slDeletedLine, bool bDeleteLastLine)
        {
            bool bDeleteControl = false;
            for (int iLines = 0; iLines < aSaleslines.Length; iLines++)
            {
                PickingSlipLine slThisline = (((PickingSlipLine)aSaleslines[iLines]));

                if (iLines != aSaleslines.Length - 1 || bDeleteLastLine) //Never delete the last row
                {
                    if (slDeletedLine.Name == slThisline.Name)
                    {
                        bDeleteControl = true;
                        this.pnlDetails.Controls.Remove(slDeletedLine);
                        if (iLines != aSaleslines.Length - 1)
                        {
                            (((PickingSlipLine)aSaleslines[iLines + 1])).txtCode.Focus(); // focus on the next line
                        }
                    }
                    if (bDeleteControl && iLines != aSaleslines.Length - 1) //resize the line array
                    {
                        aSaleslines[iLines] = aSaleslines[iLines + 1]; //Move all the controls one up in the list
                        (((PickingSlipLine)aSaleslines[iLines + 1])).Location = new Point((((PickingSlipLine)aSaleslines[iLines + 1])).Location.X, (((PickingSlipLine)aSaleslines[iLines + 1])).Location.Y - 20); // move location of control to new position
                        (((PickingSlipLine)aSaleslines[iLines + 1])).iLineIndex--;//sync the lineindex of the control array
                    }
                }
            }
            if (bDeleteControl)//update the line array
            {
                //Array.Resize<Control>(ref aSaleslines, aSaleslines.Length - 1);
                iLineRowIndex--;
            }
            addTotals();
            //Check if you want to close the order
            if (aSaleslines.Length == 0)
            {
                if (MessageBox.Show("There are no more lines in this order. Do you want to close this order?", "Close Order?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Functions.CloseOrder(txtNumber.Text.Trim());
                }
            }
        }
 public void focusNextLine(int iLineIndex)
 {
     //LL 17/09/2009 - start
     if ((iLineIndex >= aSaleslines.Length && txtNumber.Text == "*NEW*") || (iLineIndex >= aSaleslines.Length && txtNumber.Text == ""))
     {
         if (txtNumber.Text == "")
             txtNumber.Text = "*NEW*";
         //LL 17/09/2009 - end
         PickingSlipLine slNewline = new PickingSlipLine();
         AddSalesLine(slNewline);
     }
     if (iLineIndex < aSaleslines.Length)
     {
         PickingSlipLine slNewLine = (PickingSlipLine)aSaleslines[iLineIndex];
         slNewLine.txtCode.Focus();
     }
 }
        private void insertSolHistoryLine(PickingSlipLine slThisLine, Pervasive.Data.SqlClient.PsqlConnection oConn, string sHeaderDocumentNumber)
        {
            string sSql = "";
            string sStatus = slThisLine.txtStatus.Text;
            if (sStatus == "")
            {
                sStatus = "0";
            }
            if (slThisLine.txtCode.Text != "" && slThisLine.txtCode.Text != "'")
            {
                sSql = "INSERT into SOLHL";
                sSql += " (Header, ItemCode, Status,LinkNum, Qty, Description) ";
                sSql += " VALUES ";
                sSql += "(";
                sSql += "'" + sHeaderDocumentNumber.Trim() + "'";
                sSql += ",'" + slThisLine.txtCode.Text + "'";
                sSql += "," + sStatus;
                sSql += "," + (slThisLine.iLineIndex + 1).ToString();
                sSql += "," + slThisLine.txtQuantity.Text.Replace(",", "").Trim();
                sSql += ",'" + slThisLine.txtDescription.Text.Replace("'","''") + "'";
                sSql += ")";
                int Ret = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteNonQuery();
                slThisLine.sPastelLineLink = (slThisLine.iLineIndex + 1).ToString();
            }
            else if (slThisLine.txtCode.Text != "")
            {
                sSql = "INSERT into SOLHL";
                sSql += " (Header, ItemCode,Status,LinkNum,Qty, Description) ";
                sSql += " VALUES ";
                sSql += "(";
                sSql += "'" + sHeaderDocumentNumber.Trim() + "'";
                sSql += ",'C'";
                sSql += "," + sStatus;
                sSql += "," + (slThisLine.iLineIndex + 1).ToString();
                sSql += "," + slThisLine.txtQuantity.Text.Replace(",", "").Trim();
                sSql += ",'" + slThisLine.txtDescription.Text.Replace("'", "''") + "'";
                sSql += ")";
                int Ret = Solsage_Process_Management_System.Classes.Connect.getDataCommand(sSql, oConn).ExecuteNonQuery();
                slThisLine.sPastelLineLink = (slThisLine.iLineIndex + 1).ToString();

            }
        }
        private string getPastelLine(PickingSlipLine slThisLine)
        {
            string sLine = "";
            string sLineType = "4";
            if (slThisLine.txtCode.Text == "'")
            {
                sLine += "|"; //Cost Price
                sLine += "|"; //Line Quantity
                sLine += "|"; //Exclusive Price Per Unit
                sLine += "|"; //Inclusive Price Per Unit
                sLine +=  "|"; //Unit
                sLine += "|"; //Tax Type
                sLine += "|"; //Discount Type
                sLine +=  "|"; //Discount %
                sLine += "|"; //Code
                sLine += slThisLine.txtDescription.Text.Trim() + "|"; //Description
                sLine +=  "7|"; //Line Type
                sLine += "|"; //Multistore
                sLine += "|"; //CostCode
            }
            else if (slThisLine.txtCode.Text != "" )//line defined by Code
            {
                string sQuantity = slThisLine.txtQuantity.Text.Replace(",", "").Trim();
                sLine += "|"; //Cost Price
                sLine += sQuantity + "|"; //Line Quantity
                sLine += slThisLine.txtExcPrice.Text.Replace(",", "").Trim() + "|"; //Exclusive Price Per Unit
                //LL 16/09/2009 -- start
                if (slThisLine.txtTaxType.Text == "0")
                    sLine += (Convert.ToDouble(slThisLine.txtExcPrice.Text)).ToString().Replace(",", "") + "|"; //Inclusive Price Per Unit
                else if (slThisLine.txtTaxType.Text == "1")
                    sLine += (Convert.ToDouble(slThisLine.txtExcPrice.Text) * 1.14).ToString().Replace(",", "") + "|"; //Inclusive Price Per Unit
                else if (slThisLine.txtTaxType.Text == "2")
                    sLine += (Convert.ToDouble(slThisLine.txtExcPrice.Text)).ToString().Replace(",", "") + "|"; //Inclusive Price Per Unit

                //LL 16/09/2009 -- end
                sLine += slThisLine.txtUnit.Text.Trim() + "|"; //Unit
                sLine += slThisLine.txtTaxType.Text.PadLeft(2,"0".ToCharArray()[0]) + "|"; //Tax Type
                sLine += "0|"; //Discount Type
                sLine += slThisLine.txtDiscount.Text.Replace(".", "").Replace(",", "") + "|"; //Discount %
                sLine += slThisLine.txtCode.Text.Trim() + "|"; //Code
                sLine += slThisLine.txtDescription.Text.Trim() + "|"; //Description
                sLine +=  sLineType + "|"; //Line Type
                sLine += slThisLine.txtStore.Text.Trim() + "|"; //Multistore
                sLine += "|"; //CostCode
            }
            return (sLine);
        }
 private string CreateDocLineFromSalesline(PickingSlipLine slThisLine, int iLines)
 {
     string sLine = "";
     if (slThisLine.sLineType != "Comment" && slThisLine.txtCode.Text.Trim() != "'")
     {
         string sQuantity = Convert.ToDecimal(slThisLine.txtQuantity.Text.Replace(",", "").Trim()).ToString();
         sLine += "|"; //Cost Price
         sLine += sQuantity + "|"; //Line Quantity
         sLine += slThisLine.txtExcPrice.Text.Replace(",", "").Trim() + "|"; //Exclusive Price Per Unit
         if (slThisLine.txtTaxType.Text == "0")
             sLine += (Convert.ToDouble(slThisLine.txtExcPrice.Text)).ToString().Replace(",", "") + "|"; //Inclusive Price Per Unit
         else if (slThisLine.txtTaxType.Text == "1")
             sLine += (Convert.ToDouble(slThisLine.txtExcPrice.Text) * 1.14).ToString().Replace(",", "") + "|"; //Inclusive Price Per Unit
         else
             sLine += (Convert.ToDouble(slThisLine.txtExcPrice.Text)).ToString().Replace(",", "") + "|"; //All other tax type will send a 0 tax value through
         sLine += slThisLine.txtUnit.Text.Trim() + "|"; //Unit
         sLine += slThisLine.txtTaxType.Text.PadLeft(2, "0".ToCharArray()[0]) + "|"; //Tax Type
         sLine += "0|"; //Discount Type
         sLine += slThisLine.txtDiscount.Text.Replace(".", "").Replace(",", "") + "|"; //Discount %
         sLine += slThisLine.txtCode.Text.Trim() + "|"; //Code
         sLine += slThisLine.txtDescription.Text.Trim() + "~" + iLines + "|"; //Description
         sLine += "4|"; //Line Type
         //sLine += slThisLine.txtStore.Text.Trim() + "|"; //Line Type
         sLine += "|"; //Multistore
         sLine += "|"; // CostCode
         //sLine += "^" + slThisLine.sPastelLineLink;//Add linknum at end of string in order to delete line from SOLHL
         //string sReturned = "0";
         //sLine += "^" + sReturned;//Add linknum at end of string in order to delete line from SOLHL
     }
     else
     {
         string sDescription = slThisLine.txtDescription.Text;
     //                if (slThisLine.txtDescription.Text.StartsWith("*D"))
       //              {
     //
       //          }
         sLine = "|"; //Cost Price
         sLine += "|"; //Line Quantity
         sLine += "|"; //Exclusive Price Per Unit
         sLine += "|"; //Inclusive Price Per Unit
         sLine += "|"; //Unit
         sLine += "|"; //Tax Type
         sLine += "|"; //Discount Type
         sLine += "|"; //Discount %
         sLine += "'|"; //Code
         sLine += sDescription  + "|"; //Description
         sLine += "7|"; //Line Type
         sLine += "|"; //Line Type
         sLine += "|"; //CostCode
         //sLine += "^*C";
     }
     return sLine;
 }
 public void AddSalesLine(PickingSlipLine slNewLine)
 {
     Array.Resize<Control>(ref aSaleslines, aSaleslines.Length + 1);
     aSaleslines[aSaleslines.Length-1] = slNewLine;
     if (iLineRowIndex < 13)
         slNewLine.Top = 17 + ((iLineRowIndex) * 21);
     else
         slNewLine.Top = 270; //16 + ((iLineRowIndex) * 18);
     slNewLine.Left = 4;
     if (slNewLine.txtStore.Text == "")
     {
         slNewLine.txtStore.Text = Global.sDefaultStore;
     }
     slNewLine.TabIndex = 50 + aSaleslines.Length;
     slNewLine.TabStop = true;
     slNewLine.iLineIndex = aSaleslines.Length - 1;
     slNewLine.Name = "slNewLine_" + (aSaleslines.Length - 1).ToString();
     this.pnlDetails.Controls.Add(slNewLine);
     slNewLine.BringToFront();
     iLineRowIndex++;
 }
 private void cmdNewLine_Click(object sender, EventArgs e)
 {
     PickingSlipLine slNewline = new PickingSlipLine();
     AddSalesLine(slNewline);
     slNewline.bNextLine = true;
     slNewline.txtCode.Focus();
     slNewline.bFocusOnNextLine = false;
     addUpdateInstruction(slNewline.Name, 1,  slNewline,slNewline.txtQuantity.Text,"1.00");
 }