protected void btnPO_Click(object sender, EventArgs e)
 {
     ApplicationName = "JKSPOINVExportWinService";
     EventLogNew.Write(ApplicationName, "Operation has started.", EventLogEntryType.Information, 4);
     Start();
     EventLogNew.Write(ApplicationName, "Operation has ended.", EventLogEntryType.Information, 4);
 }
    protected void btnNONPO_Click(object sender, EventArgs e)
    {
        try
        {
            ApplicationName = "JKSNONPOINVExportWinService";
            EventLogNew.Write(ApplicationName, "Operation has started.", EventLogEntryType.Information, 4);
            DataTable DT1 = DA.ReturnChildCompanies(ParentCompanyID);
            //DataTable DT2 = DA.ReturnUserIDTable(ParentCompanyID);
            foreach (DataRow DR1 in DT1.Rows)
            {
                string BuyerCompanyName = DR1["CompanyName"].ToString();
                int    BuyerCompanyID   = Convert.ToInt32(DR1["CompanyID"].ToString());

                EventLogNew.Write(ApplicationName, "BuyerCompanyName: " + BuyerCompanyName, EventLogEntryType.Information, 9);
                EventLogNew.Write(ApplicationName, "BuyerCompanyID: " + BuyerCompanyID, EventLogEntryType.Information, 9);

                //foreach (DataRow DR2 in DT2.Rows)
                //{
                string DateTimeString = Convert.ToDateTime(DateTime.Now.ToLongDateString()).ToString("ddMMyyyy") + Convert.ToDateTime(DateTime.Now.ToLongTimeString()).ToString("HHmmss");

                string UserID = "";//DR2[0].ToString();

                EventLogNew.Write(ApplicationName, "UserID: " + UserID, EventLogEntryType.Information, 9);

                DataTable DT = DA.ReturnSageDataTableNONPO(BuyerCompanyID);

                if (DT.Rows.Count > 0)
                {
                    EventLogNew.Write(ApplicationName, "DT.Rows.Count: " + DT.Rows.Count, EventLogEntryType.Information, 9);

                    #region Export Path
                    string fpath1 = ConfigurationManager.AppSettings["InvoiceExportPath_JKS"].Trim() + @"\" + "NONPOINV_" + DateTimeString + "_0" + ".csv";

                    Stream fsA = File.Create(fpath1);
                    fsA.Close();

                    Stream fs1 = File.Open(fpath1, FileMode.Open, FileAccess.ReadWrite);

                    EventLogNew.Write(ApplicationName, "Export Path: " + fpath1, EventLogEntryType.Information, 9);
                    #endregion

                    #region Duplicate Path
                    string fpath2 = ConfigurationManager.AppSettings["InvoiceExportPathDuplicate_JKS"].Trim() + @"\" + "NONPOINV_" + DateTimeString + "_0" + ".csv";

                    Stream fsB = File.Create(fpath2);
                    fsB.Close();

                    Stream fs2 = File.Open(fpath2, FileMode.Open, FileAccess.ReadWrite);

                    EventLogNew.Write(ApplicationName, "Duplicate Path: " + fpath2, EventLogEntryType.Information, 9);
                    #endregion

                    //string csvTxt = "H:DocumentID,H:Document Type,H:Supplier Code,H:Invoice No.,H:Invoice Date,H:Net Value,H:VAT,H:Total Value,H:URL,L:Purchase Order No.,L:Product Code,L:Quantity,L:Price,L:Amount,L:VAT,H:Currency";
                    string csvTxt = "H:DocumentID,H:Type,H:CompanyCode,H:SupplierCode,H:DocType,H:InvoiceNumber,H:InvoiceDate,H:Currency,H:NetValue,H:VAT,H:Total,H:URL,L:PONumber,L:POLineNo,L:GRN,L:Quantity,L:UOM,L:Price,L:NetValue,L:VAT,L:TaxCode,L:Total,L:Description,L:Nominal,L:CostCentre,L:InternalOrderNo,L:Profit Centre";

                    StreamWriter SW;
                    StreamWriter SW1;
                    StreamWriter SW2;

                    SW = new StreamWriter(fs1);
                    SW.WriteLine(csvTxt);
                    SW.Flush();

                    SW = new StreamWriter(fs2);
                    SW.WriteLine(csvTxt);
                    SW.Flush();
                    string value = "";
                    foreach (DataRow DR in DT.Rows)
                    {
                        csvTxt = DR["DocumentID"].ToString() + ",";   //H:DocumentID
                        //added BY kd on 27-08-2018
                        csvTxt += "NONPO" + ",";                      //H:Type
                        csvTxt += DR["CompanyCode"].ToString() + ","; //H:CompanyCode
                        if (DR["Supplier a/c ref"].ToString().Contains(","))
                        {
                            value = '"' + DR["Supplier a/c ref"].ToString() + '"';
                        }
                        else
                        {
                            value = DR["Supplier a/c ref"].ToString();
                        }
                        csvTxt += value + ",";                 //H:SupplierCode
                        csvTxt += DR["Type"].ToString() + ","; //H:DocType

                        value = DR["Reference"].ToString();    //H:Invoice No.
                        value = value.Replace(",", "");
                        if (value.Length >= 16)
                        {
                            csvTxt += value.Substring(0, 16) + ",";
                        }
                        else
                        {
                            csvTxt += value + ",";
                        }
                        //csvTxt += Convert.ToDateTime(DR["Date"].ToString()).ToString("ddMMyyyy") + ",";//H:InvoiceDate
                        //csvTxt += DR["Date"].ToString() + ",";
                        value   = DR["Date"].ToString();
                        value   = value.Replace("/", "");
                        csvTxt += value + ",";
                        csvTxt += DR["Currency"].ToString() + ",";    //H:Currency
                        csvTxt += DR["Net Value"].ToString() + ",";   //H:NetValue
                        csvTxt += DR["VAT"].ToString() + ",";         //H:VAT
                        csvTxt += DR["Total Value"].ToString() + ","; //H:Total
                        csvTxt += DR["URL"].ToString() + ",";         //H:URL
                        //--------------------------------------------------------------------- End of Header values
                        csvTxt += "" + ",";                           //L:PONumber

                        //added BY kd on 27-08-2018
                        csvTxt += "" + ",";                        //L:POLineNo
                        csvTxt += "" + ",";                        //L:GRN
                        csvTxt += "1" + ",";                       //L:Quantity
                        csvTxt += "EA" + ",";                      //L:UOM
                        csvTxt += DR["Price"].ToString() + ",";    //L:Price
                        csvTxt += DR["NetValue"].ToString() + ","; //L:NetValue
                        csvTxt += DR["GVAT"].ToString() + ",";     //L:VAT
                        csvTxt += DR["vatcode"].ToString() + ",";  //L:TaxCode
                        decimal value3 = Convert.ToDecimal(DR["NetValue"]);
                        decimal value4 = Convert.ToDecimal(DR["GVAT"]);
                        csvTxt += Convert.ToString(value3 + value4) + ","; //L:Total
                        //added by kd on 14-09-2018
                        value = DR["Description"].ToString();              //L:Description
                        value = value.Replace(",", "");

                        if (value.Length >= 50)
                        {
                            value = value.Substring(0, 50);
                        }

                        if (DR["Description"].ToString() != "")
                        {
                            csvTxt += value + ",";//L:Description
                        }
                        else
                        {
                            csvTxt += DR["NominalName"].ToString() + ","; //L:Description
                        }
                        csvTxt += DR["NominalCode"].ToString() + ",";     //L:NominalCode
                        if (DR["BalanceSheet"].ToString() == "1")
                        {
                            csvTxt += " " + ",";//L:CostCentre CostCentre
                        }
                        else
                        {
                            csvTxt += DR["CostCentre"].ToString() + ",";//L:CostCentre
                        }

                        csvTxt += DR["BusinessUnitCode"].ToString() + ",";//L:InternalOrderNo
                        //Modified by kd on 14-09-2018
                        if (DR["BalanceSheet"].ToString() == "0" || DR["BalanceSheet"].ToString() == "")
                        {
                            csvTxt += " " + ",";//L:ProfitCentre
                        }
                        else
                        {
                            csvTxt += DR["CostCentre"].ToString() + ",";//L:ProfitCentre
                        }
                        SW1 = new StreamWriter(fs1);
                        SW1.WriteLine(csvTxt);
                        SW1.Flush();

                        SW2 = new StreamWriter(fs2);
                        SW2.WriteLine(csvTxt);
                        SW2.Flush();

                        csvTxt = "";
                    }

                    fs1.Close();
                    fs2.Close();
                }
                //}
            }
            Page.RegisterStartupScript("reg", "<script> alert('Operation performed successfully.');</script>");
        }
        catch (Exception ex)
        {
            string ss = ex.Message + "\r\n" + ex.Source + "\r\n" + ex.StackTrace + "\r\n" + ex.TargetSite + "\r\n" + ex.InnerException + "\r\n" + ex.Source;

            EventLogNew.Write(ApplicationName, "Error: " + ss, EventLogEntryType.Error, 5);
        }
        finally {
            EventLogNew.Write(ApplicationName, "Operation has ended.", EventLogEntryType.Information, 4);
        }
    }
    public void Start()
    {
        try
        {
            int ParentCompanyID = 180918;
            ApplicationName = "JKSPOINVExportWinService";
            DataTable DT1 = DA.ReturnChildCompanies(ParentCompanyID);
            //DataTable DT2 = DA.ReturnUserIDTable(ParentCompanyID);
            // added by kd on 01/08/2018
            foreach (DataRow DR1 in DT1.Rows)
            {
                int       BuyerCompanyID = Convert.ToInt32(DR1["CompanyID"].ToString());
                DataTable DT             = DA.ReturnInvoiceNo(BuyerCompanyID);
                if (DT.Rows.Count > 0)
                {
                    foreach (DataRow DR2 in DT.Rows)
                    {
                        DA.INVVatUpdateBeforeExport(Convert.ToInt32(DR2["InvoiceID"].ToString()));
                    }
                }
            }
            foreach (DataRow DR1 in DT1.Rows)
            {
                int       BuyerCompanyID = Convert.ToInt32(DR1["CompanyID"].ToString());
                DataTable DT             = DA.ReturnCRNNo(BuyerCompanyID);
                if (DT.Rows.Count > 0)
                {
                    foreach (DataRow DR2 in DT.Rows)
                    {
                        DA.CRNVatUpdateBeforeExport(Convert.ToInt32(DR2["CreditNoteID"].ToString()));
                    }
                }
            }
            foreach (DataRow DR1 in DT1.Rows)
            {
                string BuyerCompanyName = DR1["CompanyName"].ToString();
                int    BuyerCompanyID   = Convert.ToInt32(DR1["CompanyID"].ToString());

                EventLogNew.Write(ApplicationName, "BuyerCompanyName: " + BuyerCompanyName, EventLogEntryType.Information, 9);
                EventLogNew.Write(ApplicationName, "BuyerCompanyID: " + BuyerCompanyID, EventLogEntryType.Information, 9);

                //foreach (DataRow DR2 in DT2.Rows)
                //{
                string DateTimeString = Convert.ToDateTime(DateTime.Now.ToLongDateString()).ToString("ddMMyyyy") + Convert.ToDateTime(DateTime.Now.ToLongTimeString()).ToString("HHmmss");

                string UserID = "";//DR2[0].ToString();

                EventLogNew.Write(ApplicationName, "UserID: " + UserID, EventLogEntryType.Information, 9);

                DataTable DT = DA.ReturnSageDataTable(BuyerCompanyID);

                if (DT.Rows.Count > 0)
                {
                    EventLogNew.Write(ApplicationName, "DT.Rows.Count: " + DT.Rows.Count, EventLogEntryType.Information, 9);

                    #region Export Path
                    string fpath1 = ConfigurationManager.AppSettings["InvoiceExportPath_JKS"].Trim() + @"\" + "POINV_" + DateTimeString + "_0" + ".csv";

                    Stream fsA = File.Create(fpath1);
                    fsA.Close();

                    Stream fs1 = File.Open(fpath1, FileMode.Open, FileAccess.ReadWrite);

                    EventLogNew.Write(ApplicationName, "Export Path: " + fpath1, EventLogEntryType.Information, 9);
                    #endregion

                    #region Duplicate Path
                    string fpath2 = ConfigurationManager.AppSettings["InvoiceExportPathDuplicate_JKS"].Trim() + @"\" + "POINV_" + DateTimeString + "_0" + ".csv";

                    Stream fsB = File.Create(fpath2);
                    fsB.Close();

                    Stream fs2 = File.Open(fpath2, FileMode.Open, FileAccess.ReadWrite);

                    EventLogNew.Write(ApplicationName, "Duplicate Path: " + fpath2, EventLogEntryType.Information, 9);
                    #endregion

                    //string csvTxt = "H:DocumentID,H:Document Type,H:Supplier Code,H:Invoice No.,H:Invoice Date,H:Net Value,H:VAT,H:Total Value,H:URL,L:Purchase Order No.,L:Product Code,L:Quantity,L:Price,L:Amount,L:VAT,H:Currency";
                    string csvTxt = "H:DocumentID,H:Type,H:CompanyCode,H:SupplierCode,H:DocType,H:InvoiceNumber,H:InvoiceDate,H:Currency,H:NetValue,H:VAT,H:Total,H:URL,L:PONumber,L:POLineNo,L:GRN,L:Quantity,L:UOM,L:Price,L:NetValue,L:VAT,L:TaxCode,L:Total,L:Description,L:Nominal,L:CostCentre,L:InternalOrderNo,L:Profit Centre";

                    StreamWriter SW;
                    StreamWriter SW1;
                    StreamWriter SW2;

                    SW = new StreamWriter(fs1);
                    SW.WriteLine(csvTxt);
                    SW.Flush();

                    SW = new StreamWriter(fs2);
                    SW.WriteLine(csvTxt);
                    SW.Flush();
                    string value = "";
                    foreach (DataRow DR in DT.Rows)
                    {
                        csvTxt = DR["DocumentID"].ToString() + ",";   //H:DocumentID
                        //added BY kd on 06-08-2018
                        csvTxt += "PO" + ",";                         //H:Type
                        csvTxt += DR["CompanyCode"].ToString() + ","; //H:CompanyCode
                        //Blocked By sonali 19.6.2018
                        // csvTxt += DR["Supplier a/c ref"].ToString().Replace(",", ";") + ",";//H:Supplier Code
                        //added BY soanli 19.6.2018
                        if (DR["Supplier a/c ref"].ToString().Contains(","))
                        {
                            value = '"' + DR["Supplier a/c ref"].ToString() + '"';
                        }
                        else
                        {
                            value = DR["Supplier a/c ref"].ToString();
                        }
                        csvTxt += value + ",";                 //H:SupplierCode
                        csvTxt += DR["Type"].ToString() + ","; //H:DocType
                        //Blocked By sonali 19.6.2018
                        // csvTxt += DR["Supplier a/c ref"].ToString().Replace(",", ";") + ",";//H:Supplier Code

                        //Blocked By sonali 19.6.2018
                        //   csvTxt += DR["Reference"].ToString().Replace(",", ";") + ",";//H:Invoice No.
                        //added BY sonali 19.6.2018

                        value = DR["Reference"].ToString();

                        value = value.Replace(",", "");

                        //string txt=value.Replace(",","");
                        // csvTxt += value.Replace(",", "") + ",";//H:Invoice No.Replace("-","/");
                        if (value.Length >= 16)
                        {
                            csvTxt += value.Substring(0, 16) + ",";
                        }
                        else
                        {
                            csvTxt += value + ",";
                        }
                        // csvTxt += Convert.ToDateTime(DR["Date"].ToString()).ToString("ddMMyyyy") + ",";//H:InvoiceDate  Convert.ToDateTime(DR["Date"].ToString()).ToString("ddMMyyyy");
                        value   = DR["Date"].ToString();
                        value   = value.Replace("/", "");
                        csvTxt += value + ",";
                        csvTxt += DR["Currency"].ToString() + ",";    //H:Currency
                        decimal NetValue = Convert.ToDecimal(DR["Net Value"]);
                        csvTxt += NetValue.ToString("0.00") + ",";    //H:NetValue
                        csvTxt += DR["VAT"].ToString() + ",";         //H:VAT
                        csvTxt += DR["Total Value"].ToString() + ","; //H:Total
                        csvTxt += DR["URL"].ToString() + ",";         //H:URL
                        //Blocked By sonali 19.6.2018
                        // csvTxt += DR["Purchase Order No."].ToString().Replace(",", ";") + ",";//L:Purchase Order No.
                        //added BY sonali 19.6.2018
                        if (DR["Purchase Order No."].ToString().Contains(","))
                        {
                            value = '"' + DR["Purchase Order No."].ToString() + '"';
                        }
                        else
                        {
                            value = DR["Purchase Order No."].ToString();
                        }
                        csvTxt += value.Replace(",", "") + ",";//L:PONumber
                        //Blocked By sonali 19.6.2018
                        // csvTxt += DR["Product Code"].ToString().Replace(",", ";") + ",";//L:Product Code
                        //added BY sonali 19.6.2018//Blocked by kd on 07-08-2018
                        //if (DR["Product Code"].ToString().Contains(","))
                        //    value = '"' + DR["Product Code"].ToString() + '"';
                        //else
                        //    value = DR["Product Code"].ToString();
                        //csvTxt += value + ",";//L:Product Code
                        //L:POLineNo,L:GRN,L:Quantity,L:Price,L:NetValue,L:VAT,L:TaxCode,L:Total,L:Description,L:Nominal,L:CostCentre,L:InternalOrderNo,L:Profit Centre";
                        //added BY kd on 06-08-2018
                        csvTxt += DR["PurOrderLineNo"].ToString() + ",";          //L:POLineNo
                        csvTxt += DR["GRN_No"].ToString().Replace(",", "") + ","; //L:GRN
                        csvTxt += DR["Quantity"].ToString() + ",";                //L:Quantity
                        csvTxt += DR["UOM"].ToString() + ",";                     //L:UOM
                        csvTxt += DR["Price"].ToString() + ",";                   //L:Price
                        csvTxt += DR["Amount"].ToString() + ",";                  //L:NetValue
                        decimal value1;
                        decimal value2;
                        decimal VATvalue;
                        if (DR["TaxRate"].ToString() != "")
                        {
                            value1   = Convert.ToDecimal(DR["TaxRate"]);
                            value2   = Convert.ToDecimal(DR["Amount"]);
                            VATvalue = value1 * value2;                 //L:VAT
                            csvTxt  += VATvalue.ToString("0.00") + ","; //L:VAT
                        }
                        else
                        {
                            value   = DR["VAT2"].ToString();
                            csvTxt += value + ",";//L:VAT
                        }

                        //  modified by kd on 26-10-2018
                        if (DR["vatcodeFromGRD"].ToString() != "")
                        {
                            csvTxt += DR["vatcodeFromGRD"].ToString() + ",";//L:TaxCode
                        }
                        else
                        {
                            csvTxt += DR["vatcode"].ToString() + ",";//L:TaxCode
                        }
                        // modified by kd on 22-09-2018
                        if (DR["TaxRate"].ToString() != "")
                        {
                            value1   = Convert.ToDecimal(DR["TaxRate"]);
                            value2   = Convert.ToDecimal(DR["Amount"]);
                            VATvalue = value1 * value2;//L:VAT
                            decimal value3 = Convert.ToDecimal(DR["Amount"]);
                            decimal value4 = VATvalue;
                            decimal VATvalue1;
                            VATvalue1 = value3 + value4;
                            csvTxt   += VATvalue1.ToString("0.00") + ",";//L:Total =L:VAT + L:NetValue
                        }
                        else
                        {
                            decimal value3 = Convert.ToDecimal(DR["Amount"]);
                            decimal value4 = Convert.ToDecimal(DR["VAT2"]);
                            decimal VATvalue2;
                            VATvalue2 = value3 + value4;
                            csvTxt   += VATvalue2.ToString("0.00") + ","; //L:Total =L:VAT + L:NetValue
                        }
                        csvTxt += " " + ",";                              //L:Description
                        csvTxt += " " + ",";                              //L:Nominal
                        csvTxt += " " + ",";                              //L:CostCentre
                        csvTxt += " " + ",";                              //L:InternalOrderNo
                        csvTxt += " " + ",";                              //L:ProfitCentre
                        SW1     = new StreamWriter(fs1);
                        SW1.WriteLine(csvTxt);
                        SW1.Flush();

                        SW2 = new StreamWriter(fs2);
                        SW2.WriteLine(csvTxt);
                        SW2.Flush();

                        csvTxt = "";
                    }

                    fs1.Close();
                    fs2.Close();
                }
                //}
            }
            Page.RegisterStartupScript("reg", "<script> alert('Operation performed successfully.');</script>");
        }
        catch (Exception ex)
        {
            string ss = ex.Message + "\r\n" + ex.Source + "\r\n" + ex.StackTrace + "\r\n" + ex.TargetSite + "\r\n" + ex.InnerException + "\r\n" + ex.Source;

            EventLogNew.Write(ApplicationName, "Error: " + ss, EventLogEntryType.Error, 5);
        }

        finally { }
    }