public static void ReportBuilder(SqlDataReader drHdr, int cropYear, int contractNumber, string deliveryDates, FileStream fs)
        {
            const string METHOD_NAME = "ReportBuilder";
            Document     document    = null;
            PdfWriter    writer      = null;

            DailyGrowerTareDetailEvent pgEvent = null;

            int    resetFlag         = 0;
            string dates             = null;
            int    contractID        = 0;
            string factoryNo         = "";
            string firstDeliveryDate = null;
            string nextDeliveryDate  = null;
            string lastDeliveryDate  = null;
            string busName           = "";
            string address1          = "";
            string address2          = "";
            string CSZ = "";

            string rptTitle = "Western Sugar Cooperative\nDaily Grower Tare Detail Report";

            try {
                if (drHdr.Read())
                {
                    contractID        = drHdr.GetInt32(drHdr.GetOrdinal("ContractID"));
                    factoryNo         = drHdr.GetString(drHdr.GetOrdinal("Delivery_Factory_No"));
                    firstDeliveryDate = drHdr.GetString(drHdr.GetOrdinal("Delivery_Date"));
                    nextDeliveryDate  = firstDeliveryDate;
                }
                else
                {
                    // Warn that we have no data.
                    WSCIEMP.Common.CWarning warn = new WSCIEMP.Common.CWarning("No records matched your report criteria.");
                    throw (warn);
                }

                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BeetConn"].ToString())) {
                    // Header Address Info
                    if (contractID > 0)
                    {
                        using (SqlDataReader drAddr = WSCReportsExec.GrowerDetailReportAddr(conn, contractID)) {
                            if (drAddr.Read())
                            {
                                busName  = drAddr.GetString(drAddr.GetOrdinal("Business_Name"));
                                address1 = drAddr.GetString(drAddr.GetOrdinal("Address_1"));
                                address2 = drAddr.GetString(drAddr.GetOrdinal("Address_2"));
                                CSZ      = drAddr.GetString(drAddr.GetOrdinal("City")) + ", " +
                                           drAddr.GetString(drAddr.GetOrdinal("State")) + " " +
                                           drAddr.GetString(drAddr.GetOrdinal("Zip"));
                            }
                        }
                    }

                    // Sample / Tare information
                    // I use nextDeliveryDate as a trick to allow reading drHdr.  Initially,
                    // nextDeliveryDate is loaded and we skip reading drHdr.  Subsequently,
                    // the bottom of each iteration will null nextDeliveryDate and force reading drHdr.
                    int loadCount = 0;

                    while (nextDeliveryDate != null || drHdr.Read())
                    {
                        if (document == null)
                        {
                            // IF YOU CHANGE MARGINS, CHANGE YOUR TABLE LAYOUTS !!!
                            //  ***  US LETTER: 612 X 792  ***
                            document = new Document(PortraitPageSize.PgPageSize, PortraitPageSize.PgLeftMargin,
                                                    PortraitPageSize.PgRightMargin, PortraitPageSize.PgTopMargin, PortraitPageSize.PgBottomMargin);

                            // we create a writer that listens to the document
                            // and directs a PDF-stream to a file
                            writer = PdfWriter.GetInstance(document, fs);

                            // Attach my override event handler(s)
                            pgEvent = new DailyGrowerTareDetailEvent();
                            pgEvent.FillEvent(contractNumber, busName, factoryNo, address1, address2, CSZ, resetFlag, rptTitle);
                            writer.PageEvent = pgEvent;

                            // Open the document
                            document.Open();
                        }

                        // load the delivery date.
                        nextDeliveryDate = drHdr.GetString(drHdr.GetOrdinal("Delivery_Date"));

                        // Acquire the sample details for the first station on a given day
                        if (lastDeliveryDate != nextDeliveryDate)
                        {
                            AddSampleHdr(ref document, drHdr, cropYear, pgEvent);
                            lastDeliveryDate = nextDeliveryDate;
                            using (SqlDataReader drSamples = WSCReportsExec.GrowerDetailReportTares(conn, contractID, nextDeliveryDate)) {
                                AddSampleDetail(ref document, drSamples, pgEvent);
                            }
                        }

                        loadCount = 0;

                        // Display Truck information for each station
                        // on the first delivery day.
                        if (nextDeliveryDate == firstDeliveryDate)
                        {
                            // Get the truck data.
                            SqlParameter[] spParams = null;
                            using (SqlDataReader drTrucks = WSCReportsExec.GrowerDetailReportASH(conn, contractID,
                                                                                                 drHdr.GetInt32(drHdr.GetOrdinal("Delivery_Station_ID")),
                                                                                                 firstDeliveryDate, ref spParams)) {
                                AddTruckDetail(ref document, drTrucks, pgEvent);

                                drTrucks.Close();
                                loadCount = Convert.ToInt32(spParams[3].Value);
                                if (loadCount > 0)
                                {
                                    AddTruckTotals(ref document, loadCount.ToString(),
                                                   Convert.ToInt32(spParams[4].Value).ToString("#,##0"),
                                                   Convert.ToInt32(spParams[5].Value).ToString("#,##0"), pgEvent);
                                }
                            }
                        }

                        // clear date to force read in top of loop
                        nextDeliveryDate = null;
                    }
                }

                // ======================================================
                // Close document
                // ======================================================
                if (document != null)
                {
                    pgEvent.IsDocumentClosing = true;
                    document.Close();
                    document = null;
                }
                if (writer == null)
                {
                    // Warn that we have no data.
                    WSCIEMP.Common.CWarning warn = new WSCIEMP.Common.CWarning("No records matched your report criteria.");
                    throw (warn);
                }
            }
            catch (Exception ex) {
                string errMsg = "document is null: " + (document == null).ToString() + "; " +
                                "writer is null: " + (writer == null).ToString();
                WSCIEMP.Common.CException wscex = new WSCIEMP.Common.CException(METHOD_NAME + errMsg, ex);
                throw (wscex);
            }
            finally {
                if (document != null)
                {
                    pgEvent.IsDocumentClosing = true;
                    document.Close();
                }
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }
        public static void AddTruckTotals(ref Document document, string loadCount, string dirtWeight, string netWeight, DailyGrowerTareDetailEvent pgEvent)
        {
            const string METHOD_NAME = "AddTruckTotals";

            try {
                PdfPTable table = PdfReports.CreateTable(_primaryTableLayout, 1);

                // Let the PDF Page event handler take care of adding column labels on page breaks.
                pgEvent.HeaderNameList    = _truckDetailHdrNames;
                pgEvent.HeaderTableLayout = _primaryTableLayout;

                PdfReports.AddText2Table(table, " ", _normalFont);
                PdfReports.AddText2Table(table, "Load Count:", _labelFont);
                PdfReports.AddText2Table(table, loadCount, _normalFont);
                PdfReports.AddText2Table(table, " ", _normalFont);
                PdfReports.AddText2Table(table, " ", _normalFont);
                PdfReports.AddText2Table(table, dirtWeight, _normalFont, "right");
                PdfReports.AddText2Table(table, " ", _normalFont);
                PdfReports.AddText2Table(table, netWeight, _normalFont, "right");
                PdfReports.AddText2Table(table, " ", _normalFont, _primaryTableLayout.Length);

                PdfReports.AddTableNoSplit(document, pgEvent, table);
                pgEvent.HeaderNameList    = null;
                pgEvent.HeaderTableLayout = null;
            }
            catch (Exception ex) {
                WSCIEMP.Common.CException wscEx = new WSCIEMP.Common.CException(MOD_NAME + "." + METHOD_NAME, ex);
                throw (wscEx);
            }
        }
        public static void AddTruckDetail(ref Document document, SqlDataReader drTrucks, DailyGrowerTareDetailEvent pgEvent)
        {
            const string METHOD_NAME = "AddTruckDetail";
            string       yardCard    = null;

            try {
                pgEvent.HeaderNameList = _truckDetailHdrNames;
                //PdfPTable table = PdfReports.CreateTable(_primaryTableLayout, 1);

                int iYardCard     = drTrucks.GetOrdinal("Yard_Card_Sequence_Number");
                int iDeliveryDate = drTrucks.GetOrdinal("Delivery_Date");
                int iTruckNumber  = drTrucks.GetOrdinal("Truck_Number");
                int iWeightIn     = drTrucks.GetOrdinal("Weight_In");
                int iWeightOut    = drTrucks.GetOrdinal("Weight_Out");
                int iDirtWt       = drTrucks.GetOrdinal("Dirt_Weight");
                int iDirtTaken    = drTrucks.GetOrdinal("Dirt_Taken");
                int iNetWt        = drTrucks.GetOrdinal("Net_Weight");

                while (drTrucks.Read())
                {
                    PdfPTable table = PdfReports.CreateTable(_primaryTableLayout, 1);

                    // On first pass, add the header labels here.
                    if (yardCard == null)
                    {
                        PdfReports.FillHeaderLabels(ref table, _truckDetailHdrNames, _labelFont);
                    }
                    else
                    {
                        // Now let the PDF Page event handler take care of adding column labels on page breaks.
                        pgEvent.HeaderTableLayout = _primaryTableLayout;
                    }

                    yardCard = drTrucks.GetString(iYardCard);
                    PdfReports.AddText2Table(table, yardCard, _normalFont, "center");
                    PdfReports.AddText2Table(table, drTrucks.GetString(iDeliveryDate), _normalFont, "center");
                    PdfReports.AddText2Table(table, drTrucks.GetString(iTruckNumber), _normalFont, "center");
                    PdfReports.AddText2Table(table, drTrucks.GetInt32(iWeightIn).ToString("#,##0"), _normalFont, "right");
                    PdfReports.AddText2Table(table, drTrucks.GetInt32(iWeightOut).ToString("#,##0"), _normalFont, "right");
                    PdfReports.AddText2Table(table, drTrucks.GetInt32(iDirtWt).ToString("#,##0"), _normalFont, "right");
                    PdfReports.AddText2Table(table, drTrucks.GetString(iDirtTaken), _normalFont, "center");
                    PdfReports.AddText2Table(table, drTrucks.GetInt32(iNetWt).ToString("#,##0"), _normalFont, "right");

                    PdfReports.AddTableNoSplit(document, pgEvent, table);
                }

                //PdfReports.AddTableNoSplit(document, pgEvent, table);

                pgEvent.HeaderNameList    = null;
                pgEvent.HeaderTableLayout = null;
            }
            catch (Exception ex) {
                WSCIEMP.Common.CException wscEx = new WSCIEMP.Common.CException(MOD_NAME + "." + METHOD_NAME, ex);
                throw (wscEx);
            }
        }
        private static void AddSampleHdr(ref Document document, SqlDataReader drHdr, int cropYear, DailyGrowerTareDetailEvent pgEvent)
        {
            PdfPTable table = PdfReports.CreateTable(_primaryTableLayout, 1);

            PdfReports.AddText2Table(table, "Delivery Date", _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetString(drHdr.GetOrdinal("Delivery_Date")), _normalFont);
            PdfReports.AddText2Table(table, "1st Net Lbs", _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetInt32(drHdr.GetOrdinal("First_Net_Pounds")).ToString(), _normalFont);
            PdfReports.AddText2Table(table, " ", _normalFont);
            PdfReports.AddText2Table(table, "% Sugar", _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetDecimal(drHdr.GetOrdinal("Sugar_Content")).ToString("0.00"), _normalFont);
            PdfReports.AddText2Table(table, " ", _normalFont);

            PdfReports.AddText2Table(table, "Station No", _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetInt32(drHdr.GetOrdinal("Delivery_Station_No")).ToString(), _normalFont);
            PdfReports.AddText2Table(table, "Tare Lbs", _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetInt32(drHdr.GetOrdinal("Tare_Pounds")).ToString(), _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetDecimal(drHdr.GetOrdinal("Tare")).ToString("0.00"), _normalFont);
            PdfReports.AddText2Table(table, "SLM", _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetDecimal(drHdr.GetOrdinal("SLM_Pct")).ToString("0.0000"), _normalFont);
            PdfReports.AddText2Table(table, " ", _normalFont);

            PdfReports.AddText2Table(table, "Station Name", _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetString(drHdr.GetOrdinal("Delivery_Station_Name")), _normalFont);
            PdfReports.AddText2Table(table, "Final Net Lbs", _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetInt32(drHdr.GetOrdinal("Final_Net_Pounds")).ToString(), _normalFont);
            PdfReports.AddText2Table(table, " ", _normalFont);
            PdfReports.AddText2Table(table, "Lbs Extractable Sugar/Ton", _normalFont, 2);
            PdfReports.AddText2Table(table, drHdr.GetInt32(drHdr.GetOrdinal("ExSugarPerTon")).ToString(), _normalFont);
            PdfReports.AddText2Table(table, " ", _labelFont, _primaryTableLayout.Length);
            PdfReports.AddTableNoSplit(document, pgEvent, table);
        }
        public static void AddSampleDetail(ref Document document, SqlDataReader drSamples, DailyGrowerTareDetailEvent pgEvent)
        {
            const string METHOD_NAME = "AddSampleDetail";
            string       yardCard    = null;
            PdfPTable    table       = null;

            try {
                pgEvent.HeaderNameList = _sampleDetailHdrNames;

                while (drSamples.Read())
                {
                    table = PdfReports.CreateTable(_tareTableLayout, 1);

                    // On first pass, add the header labels here.
                    if (yardCard == null)
                    {
                        PdfReports.FillHeaderLabels(ref table, _sampleDetailHdrNames, _labelFont);
                    }
                    else
                    {
                        // Now let the PDF Page event handler take care of adding column labels on page breaks.
                        pgEvent.HeaderTableLayout = _tareTableLayout;
                    }

                    yardCard = drSamples.GetString(drSamples.GetOrdinal("Yard_Card_No"));
                    PdfReports.AddText2Table(table, yardCard, _normalFont, "center");
                    PdfReports.AddText2Table(table, drSamples.GetInt32(drSamples.GetOrdinal("SampleNo")).ToString("#"), _normalFont, "center");
                    PdfReports.AddText2Table(table, drSamples.GetDecimal(drSamples.GetOrdinal("Sugar_Content")).ToString("0.00"), _normalFont, "center");
                    PdfReports.AddText2Table(table, drSamples.GetDecimal(drSamples.GetOrdinal("Gross_Weight")).ToString("#,##0.0"), _normalFont, "right");
                    PdfReports.AddText2Table(table, drSamples.GetDecimal(drSamples.GetOrdinal("Clean_Weight")).ToString("#,##0.0"), _normalFont, "right");
                    PdfReports.AddText2Table(table, drSamples.GetDecimal(drSamples.GetOrdinal("Tare")).ToString("0.00"), _normalFont, "center");
                    PdfReports.AddText2Table(table, drSamples.GetString(drSamples.GetOrdinal("High_Tare")), _normalFont, "center");
                    PdfReports.AddText2Table(table, drSamples.GetInt32(drSamples.GetOrdinal("Sample_Quality")).ToString(), _normalFont, "center");
                    PdfReports.AddText2Table(table, drSamples.GetDecimal(drSamples.GetOrdinal("SLM_Pct")).ToString("0.0000"), _normalFont, "center");

                    PdfReports.AddTableNoSplit(document, pgEvent, table);
                }

                pgEvent.HeaderNameList    = null;
                pgEvent.HeaderTableLayout = null;

                // Follow the table with a blank line
                table = PdfReports.CreateTable(_tareTableLayout, 1);
                PdfReports.AddText2Table(table, " ", _normalFont, _tareTableLayout.Length);

                PdfReports.AddTableNoSplit(document, pgEvent, table);
            }
            catch (Exception ex) {
                WSCIEMP.Common.CException wscEx = new WSCIEMP.Common.CException(MOD_NAME + "." + METHOD_NAME, ex);
                throw (wscEx);
            }
        }
        public static void ReportBuilder(SqlDataReader drHdr, int cropYear, int contractNumber, string deliveryDates, FileStream fs)
        {
            const string METHOD_NAME = "ReportBuilder";
            Document document = null;
            PdfWriter writer = null;

            DailyGrowerTareDetailEvent pgEvent = null;

            int resetFlag = 0;
            string dates = null;
            int contractID = 0;
            string factoryNo = "";
            string firstDeliveryDate = null;
            string nextDeliveryDate = null;
            string lastDeliveryDate = null;
            string busName = "";
            string address1 = "";
            string address2 = "";
            string CSZ = "";

            string rptTitle = "Western Sugar Cooperative\nDaily Grower Tare Detail Report";

            try {

                if (drHdr.Read()) {

                    contractID = drHdr.GetInt32(drHdr.GetOrdinal("ContractID"));
                    factoryNo = drHdr.GetString(drHdr.GetOrdinal("Delivery_Factory_No"));
                    firstDeliveryDate = drHdr.GetString(drHdr.GetOrdinal("Delivery_Date"));
                    nextDeliveryDate = firstDeliveryDate;

                } else {
                    // Warn that we have no data.
                    WSCIEMP.Common.CWarning warn = new WSCIEMP.Common.CWarning("No records matched your report criteria.");
                    throw (warn);
                }

                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BeetConn"].ToString())) {

                    // Header Address Info
                    if (contractID > 0) {

                        using (SqlDataReader drAddr = WSCReportsExec.GrowerDetailReportAddr(conn, contractID)) {

                            if (drAddr.Read()) {

                                busName = drAddr.GetString(drAddr.GetOrdinal("Business_Name"));
                                address1 = drAddr.GetString(drAddr.GetOrdinal("Address_1"));
                                address2 = drAddr.GetString(drAddr.GetOrdinal("Address_2"));
                                CSZ = drAddr.GetString(drAddr.GetOrdinal("City")) + ", " +
                                    drAddr.GetString(drAddr.GetOrdinal("State")) + " " +
                                    drAddr.GetString(drAddr.GetOrdinal("Zip"));
                            }
                        }
                    }

                    // Sample / Tare information
                    // I use nextDeliveryDate as a trick to allow reading drHdr.  Initially,
                    // nextDeliveryDate is loaded and we skip reading drHdr.  Subsequently,
                    // the bottom of each iteration will null nextDeliveryDate and force reading drHdr.
                    int loadCount = 0;

                    while (nextDeliveryDate != null || drHdr.Read()) {

                        if (document == null) {

                            // IF YOU CHANGE MARGINS, CHANGE YOUR TABLE LAYOUTS !!!
                            //  ***  US LETTER: 612 X 792  ***
                            document = new Document(PortraitPageSize.PgPageSize, PortraitPageSize.PgLeftMargin,
                                PortraitPageSize.PgRightMargin, PortraitPageSize.PgTopMargin, PortraitPageSize.PgBottomMargin);

                            // we create a writer that listens to the document
                            // and directs a PDF-stream to a file
                            writer = PdfWriter.GetInstance(document, fs);

                            // Attach my override event handler(s)
                            pgEvent = new DailyGrowerTareDetailEvent();
                            pgEvent.FillEvent(contractNumber, busName, factoryNo, address1, address2, CSZ, resetFlag, rptTitle);
                            writer.PageEvent = pgEvent;

                            // Open the document
                            document.Open();
                        }

                        // load the delivery date.
                        nextDeliveryDate = drHdr.GetString(drHdr.GetOrdinal("Delivery_Date"));

                        // Acquire the sample details for the first station on a given day
                        if (lastDeliveryDate != nextDeliveryDate) {

                            AddSampleHdr(ref document, drHdr, cropYear, pgEvent);
                            lastDeliveryDate = nextDeliveryDate;
                            using (SqlDataReader drSamples = WSCReportsExec.GrowerDetailReportTares(conn, contractID, nextDeliveryDate)) {
                                AddSampleDetail(ref document, drSamples, pgEvent);
                            }
                        }

                        loadCount = 0;

                        // Display Truck information for each station
                        // on the first delivery day.
                        if (nextDeliveryDate == firstDeliveryDate) {

                            // Get the truck data.
                            SqlParameter[] spParams = null;
                            using (SqlDataReader drTrucks = WSCReportsExec.GrowerDetailReportASH(conn, contractID,
                                      drHdr.GetInt32(drHdr.GetOrdinal("Delivery_Station_ID")),
                                      firstDeliveryDate, ref spParams)) {

                                AddTruckDetail(ref document, drTrucks, pgEvent);

                                drTrucks.Close();
                                loadCount = Convert.ToInt32(spParams[3].Value);
                                if (loadCount > 0) {

                                    AddTruckTotals(ref document, loadCount.ToString(),
                                        Convert.ToInt32(spParams[4].Value).ToString("#,##0"),
                                        Convert.ToInt32(spParams[5].Value).ToString("#,##0"), pgEvent);
                                }
                            }
                        }

                        // clear date to force read in top of loop
                        nextDeliveryDate = null;

                    }
                }

                // ======================================================
                // Close document
                // ======================================================
                if (document != null) {
                    pgEvent.IsDocumentClosing = true;
                    document.Close();
                    document = null;
                }
                if (writer == null) {
                    // Warn that we have no data.
                    WSCIEMP.Common.CWarning warn = new WSCIEMP.Common.CWarning("No records matched your report criteria.");
                    throw (warn);
                }
            }
            catch (Exception ex) {
                string errMsg = "document is null: " + (document == null).ToString() + "; " +
                    "writer is null: " + (writer == null).ToString();
                WSCIEMP.Common.CException wscex = new WSCIEMP.Common.CException(METHOD_NAME + errMsg, ex);
                throw (wscex);
            }
            finally {

                if (document != null) {
                    pgEvent.IsDocumentClosing = true;
                    document.Close();
                }
                if (writer != null) {
                    writer.Close();
                }
            }
        }
        private static void AddSampleHdr(ref Document document, SqlDataReader drHdr, int cropYear, DailyGrowerTareDetailEvent pgEvent)
        {
            PdfPTable table = PdfReports.CreateTable(_primaryTableLayout, 1);

            PdfReports.AddText2Table(table, "Delivery Date", _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetString(drHdr.GetOrdinal("Delivery_Date")), _normalFont);
            PdfReports.AddText2Table(table, "1st Net Lbs", _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetInt32(drHdr.GetOrdinal("First_Net_Pounds")).ToString(), _normalFont);
            PdfReports.AddText2Table(table, " ", _normalFont);
            PdfReports.AddText2Table(table, "% Sugar", _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetDecimal(drHdr.GetOrdinal("Sugar_Content")).ToString("0.00"), _normalFont);
            PdfReports.AddText2Table(table, " ", _normalFont);

            PdfReports.AddText2Table(table, "Station No", _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetInt32(drHdr.GetOrdinal("Delivery_Station_No")).ToString(), _normalFont);
            PdfReports.AddText2Table(table, "Tare Lbs", _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetInt32(drHdr.GetOrdinal("Tare_Pounds")).ToString(), _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetDecimal(drHdr.GetOrdinal("Tare")).ToString("0.00"), _normalFont);
            PdfReports.AddText2Table(table, "SLM", _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetDecimal(drHdr.GetOrdinal("SLM_Pct")).ToString("0.0000"), _normalFont);
            PdfReports.AddText2Table(table, " ", _normalFont);

            PdfReports.AddText2Table(table, "Station Name", _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetString(drHdr.GetOrdinal("Delivery_Station_Name")), _normalFont);
            PdfReports.AddText2Table(table, "Final Net Lbs", _normalFont);
            PdfReports.AddText2Table(table, drHdr.GetInt32(drHdr.GetOrdinal("Final_Net_Pounds")).ToString(), _normalFont);
            PdfReports.AddText2Table(table, " ", _normalFont);
            PdfReports.AddText2Table(table, "Lbs Extractable Sugar/Ton", _normalFont, 2);
            PdfReports.AddText2Table(table, drHdr.GetInt32(drHdr.GetOrdinal("ExSugarPerTon")).ToString(), _normalFont);
            PdfReports.AddText2Table(table, " ", _labelFont, _primaryTableLayout.Length);
            PdfReports.AddTableNoSplit(document, pgEvent, table);
        }
        public static void AddTruckDetail(ref Document document, SqlDataReader drTrucks, DailyGrowerTareDetailEvent pgEvent)
        {
            const string METHOD_NAME = "AddTruckDetail";
            string yardCard = null;

            try {

                pgEvent.HeaderNameList = _truckDetailHdrNames;
                //PdfPTable table = PdfReports.CreateTable(_primaryTableLayout, 1);

                int iYardCard = drTrucks.GetOrdinal("Yard_Card_Sequence_Number");
                int iDeliveryDate = drTrucks.GetOrdinal("Delivery_Date");
                int iTruckNumber = drTrucks.GetOrdinal("Truck_Number");
                int iWeightIn = drTrucks.GetOrdinal("Weight_In");
                int iWeightOut = drTrucks.GetOrdinal("Weight_Out");
                int iDirtWt = drTrucks.GetOrdinal("Dirt_Weight");
                int iDirtTaken = drTrucks.GetOrdinal("Dirt_Taken");
                int iNetWt = drTrucks.GetOrdinal("Net_Weight");

                while (drTrucks.Read()) {

                    PdfPTable table = PdfReports.CreateTable(_primaryTableLayout, 1);

                    // On first pass, add the header labels here.
                    if (yardCard == null) {
                        PdfReports.FillHeaderLabels(ref table, _truckDetailHdrNames, _labelFont);
                    } else {
                        // Now let the PDF Page event handler take care of adding column labels on page breaks.
                        pgEvent.HeaderTableLayout = _primaryTableLayout;
                    }

                    yardCard = drTrucks.GetString(iYardCard);
                    PdfReports.AddText2Table(table, yardCard, _normalFont, "center");
                    PdfReports.AddText2Table(table, drTrucks.GetString(iDeliveryDate), _normalFont, "center");
                    PdfReports.AddText2Table(table, drTrucks.GetString(iTruckNumber), _normalFont, "center");
                    PdfReports.AddText2Table(table, drTrucks.GetInt32(iWeightIn).ToString("#,##0"), _normalFont, "right");
                    PdfReports.AddText2Table(table, drTrucks.GetInt32(iWeightOut).ToString("#,##0"), _normalFont, "right");
                    PdfReports.AddText2Table(table, drTrucks.GetInt32(iDirtWt).ToString("#,##0"), _normalFont, "right");
                    PdfReports.AddText2Table(table, drTrucks.GetString(iDirtTaken), _normalFont, "center");
                    PdfReports.AddText2Table(table, drTrucks.GetInt32(iNetWt).ToString("#,##0"), _normalFont, "right");

                    PdfReports.AddTableNoSplit(document, pgEvent, table);
                }

                //PdfReports.AddTableNoSplit(document, pgEvent, table);

                pgEvent.HeaderNameList = null;
                pgEvent.HeaderTableLayout = null;
            }
            catch (Exception ex) {
                WSCIEMP.Common.CException wscEx = new WSCIEMP.Common.CException(MOD_NAME + "." + METHOD_NAME, ex);
                throw (wscEx);
            }
        }
        public static void AddTruckTotals(ref Document document, string loadCount, string dirtWeight, string netWeight, DailyGrowerTareDetailEvent pgEvent)
        {
            const string METHOD_NAME = "AddTruckTotals";

            try {

                PdfPTable table = PdfReports.CreateTable(_primaryTableLayout, 1);

                // Let the PDF Page event handler take care of adding column labels on page breaks.
                pgEvent.HeaderNameList = _truckDetailHdrNames;
                pgEvent.HeaderTableLayout = _primaryTableLayout;

                PdfReports.AddText2Table(table, " ", _normalFont);
                PdfReports.AddText2Table(table, "Load Count:", _labelFont);
                PdfReports.AddText2Table(table, loadCount, _normalFont);
                PdfReports.AddText2Table(table, " ", _normalFont);
                PdfReports.AddText2Table(table, " ", _normalFont);
                PdfReports.AddText2Table(table, dirtWeight, _normalFont, "right");
                PdfReports.AddText2Table(table, " ", _normalFont);
                PdfReports.AddText2Table(table, netWeight, _normalFont, "right");
                PdfReports.AddText2Table(table, " ", _normalFont, _primaryTableLayout.Length);

                PdfReports.AddTableNoSplit(document, pgEvent, table);
                pgEvent.HeaderNameList = null;
                pgEvent.HeaderTableLayout = null;

            }
            catch (Exception ex) {
                WSCIEMP.Common.CException wscEx = new WSCIEMP.Common.CException(MOD_NAME + "." + METHOD_NAME, ex);
                throw (wscEx);
            }
        }
        public static void ReportBuilder(List<ListGrowerTareItem> hdrList, int cropYear, bool isPosted, bool isPreview, bool isHardCopy, bool isEmail,
			bool isFax, FileStream fs, string filePath)
        {
            const string METHOD_NAME = "ReportBuilder";
            const int RESET_FLAG = 0;

            Document document = null;
            PdfWriter writer = null;
            DailyGrowerTareDetailEvent pgEvent = null;
            List<ListGrowerTareItem> emailFaxList = null;
            List<ListGrowerTareItem> hardCopyList = null;

            int lastContractID = 0, lastStationID = 0;
            int index = 0;
            string firstDeliveryDate = "", busName = "", address1 = "", address2 = "", CSZ = "", emailAddress = "", faxNumber = "";

            string rptTitle = "Western Sugar Cooperative\nDaily Grower Tare Detail Report";

            try {

                if (!isPreview) {
                    emailFaxList = hdrList.FindAll(item => item.RptType == "E" || item.RptType == "F");
                    hardCopyList = hdrList.FindAll(item => item.RptType == "M");
                }
                else {
                    // In preview mode we're going to lump these all together and print them.
                    hardCopyList = hdrList;
                }

                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BeetConn"].ToString())) {

                    if (emailFaxList != null && emailFaxList.Count > 0) {
                        GrowerTareReportEmailFax(conn, filePath, ref emailFaxList, rptTitle, cropYear);
                    }

                    for (index = 0; index < hardCopyList.Count; index++) {

                        ListGrowerTareItem hdrItem = hardCopyList[index];

                        //----------------------------------------------------------------
                        // Changed contract or station, start a new print out.
                        //----------------------------------------------------------------
                        if (hdrItem.ContractID != lastContractID || hdrItem.Delivery_Station_ID != lastStationID) {

                            if (document == null) {

                                GetAddressInfo(conn, hdrItem.ContractID, out busName, out address1, out address2, out CSZ,
                                               out emailAddress, out faxNumber);

                                // IF YOU CHANGE MARGINS, CHANGE YOUR TABLE LAYOUTS !!!
                                //  ***  US LETTER: 612 X 792  ***
                                document = new Document(PortraitPageSize.PgPageSize, PortraitPageSize.PgLeftMargin,
                                                        PortraitPageSize.PgRightMargin, PortraitPageSize.PgTopMargin,
                                                        PortraitPageSize.PgBottomMargin);

                                // we create a writer that listens to the document
                                // and directs a PDF-stream to a file
                                writer = PdfWriter.GetInstance(document, fs);

                                // Attach my override event handler(s)
                                pgEvent = new DailyGrowerTareDetailEvent();
                                pgEvent.FillEvent(hdrItem.Contract_No, busName, hdrItem.Delivery_Factory_No, address1, address2, CSZ,
                                                  RESET_FLAG, rptTitle + (!isPreview ? "" : " - " + hdrItem.RptType));
                                writer.PageEvent = pgEvent;

                                // Open the document
                                document.Open();
                            }

                            if (lastContractID != 0) {

                                //--------------------------------------------------------------------------
                                // Display Truck information for the first delivery day.
                                //--------------------------------------------------------------------------
                                int loadCount = 0;
                                //if (nextDeliveryDate == firstDeliveryDate) {

                                try {
                                    // Get the truck data.
                                    SqlParameter[] spParams = null;
                                    using (
                                        SqlDataReader drTrucks = WSCReportsExec.GrowerDetailReportASH(conn,
                                        lastContractID, lastStationID, firstDeliveryDate, ref spParams)) {

                                        rptDailyGrowerTareDetail.AddTruckDetail(ref document, drTrucks, pgEvent);
                                        drTrucks.Close();

                                        loadCount = Convert.ToInt32(spParams[3].Value);

                                        if (loadCount > 0) {

                                            rptDailyGrowerTareDetail.AddTruckTotals(ref document, loadCount.ToString(),
                                                Convert.ToInt32(spParams[4].Value).ToString("#,##0"),
                                                Convert.ToInt32(spParams[5].Value).ToString("#,##0"), pgEvent);
                                        }
                                    }
                                }
                                catch {
                                    hardCopyList[index-1].Success += "Fail: Truck Detail ";
                                }
                                GetAddressInfo(conn, hdrItem.ContractID, out busName, out address1, out address2, out CSZ,
                                               out emailAddress, out faxNumber);

                                // New Page !: in Preview mode, append the report type to the title of the report.
                                pgEvent.FillEvent(hdrItem.Contract_No, busName, hdrItem.Delivery_Factory_No, address1, address2, CSZ,
                                                  RESET_FLAG, rptTitle + (!isPreview ? "" : " - " + hdrItem.RptType));
                                document.NewPage();

                            }

                            firstDeliveryDate = hdrItem.Delivery_Date;
                            lastStationID = hdrItem.Delivery_Station_ID;
                            lastContractID = hdrItem.ContractID;
                        }

                        try {
                            AddSampleHdr(ref document, hdrItem, cropYear, pgEvent);

                            using (
                                SqlDataReader drSamples = WSCReportsExec.GrowerDetailReportTares(conn, hdrItem.ContractID, hdrItem.Delivery_Date)) {
                                rptDailyGrowerTareDetail.AddSampleDetail(ref document, drSamples, pgEvent);
                            }
                        }
                        catch {
                            hardCopyList[index].Success += "Fail: Sample Detail ";
                        }
                    }

                    if (lastContractID != 0) {

                        //--------------------------------------------------------------------------
                        // Display Truck information for the first delivery day.
                        //--------------------------------------------------------------------------
                        int loadCount = 0;

                        try {
                            // Get the truck data.
                            SqlParameter[] spParams = null;
                            using (SqlDataReader drTrucks = WSCReportsExec.GrowerDetailReportASH(conn,
                                lastContractID, lastStationID, firstDeliveryDate, ref spParams)) {

                                rptDailyGrowerTareDetail.AddTruckDetail(ref document, drTrucks, pgEvent);
                                drTrucks.Close();

                                loadCount = Convert.ToInt32(spParams[3].Value);

                                if (loadCount > 0) {

                                    rptDailyGrowerTareDetail.AddTruckTotals(ref document, loadCount.ToString(),
                                        Convert.ToInt32(spParams[4].Value).ToString("#,##0"), Convert.ToInt32(spParams[5].Value).ToString("#,##0"),
                                        pgEvent);
                                }
                            }
                        }
                        catch {
                            hardCopyList[index-1].Success += "Fail: Truck Detail ";
                        }
                    }
                }

                //------------------------------------
                // Print a Process Summary
                //------------------------------------
                if (document != null && writer != null) {

                    pgEvent.IsSummary = true;
                    document.NewPage();

                    PdfPTable procSumTab = null;
                    PrintSummary(document, ref procSumTab, hdrList, isPreview);
                    PdfReports.AddTableNoSplit(document, pgEvent, procSumTab);

                } else {

                    document = new Document(PortraitPageSize.PgPageSize, PortraitPageSize.PgLeftMargin,
                                            PortraitPageSize.PgRightMargin, PortraitPageSize.PgTopMargin,
                                            PortraitPageSize.PgBottomMargin);

                    // we create a writer that listens to the document
                    // and directs a PDF-stream to a file
                    writer = PdfWriter.GetInstance(document, fs);

                    // Attach my override event handler(s)
                    pgEvent = new DailyGrowerTareDetailEvent();
                    pgEvent.IsSummary = true;
                    writer.PageEvent = pgEvent;

                    // Open the document
                    document.Open();

                    PdfPTable procSumTab = null;
                    PrintSummary(document, ref procSumTab, hdrList, isPreview);
                    PdfReports.AddTableNoSplit(document, pgEvent, procSumTab);
                }

            // ======================================================
                // Close document
                // ======================================================
                if (document != null) {
                    pgEvent.IsDocumentClosing = true;
                    document.Close();
                    document = null;
                }
            }
            catch (Exception ex) {
                string errMsg = "document is null: " + (document == null).ToString() + "; " +
                    "writer is null: " + (writer == null).ToString();
                CException wscex = new CException(METHOD_NAME + errMsg, ex);
                throw (wscex);
            }
            finally {

                if (document != null) {
                    pgEvent.IsDocumentClosing = true;
                    document.Close();
                }
                if (writer != null) {
                    writer.Close();
                }
            }
        }
        public static void AddSampleDetail(ref Document document, SqlDataReader drSamples, DailyGrowerTareDetailEvent pgEvent)
        {
            const string METHOD_NAME = "AddSampleDetail";
            string yardCard = null;
            PdfPTable table = null;

            try {

                pgEvent.HeaderNameList = _sampleDetailHdrNames;

                while (drSamples.Read()) {

                    table = PdfReports.CreateTable(_tareTableLayout, 1);

                    // On first pass, add the header labels here.
                    if (yardCard == null) {
                        PdfReports.FillHeaderLabels(ref table, _sampleDetailHdrNames, _labelFont);
                    } else {
                        // Now let the PDF Page event handler take care of adding column labels on page breaks.
                        pgEvent.HeaderTableLayout = _tareTableLayout;
                    }

                    yardCard = drSamples.GetString(drSamples.GetOrdinal("Yard_Card_No"));
                    PdfReports.AddText2Table(table, yardCard, _normalFont, "center");
                    PdfReports.AddText2Table(table, drSamples.GetInt32(drSamples.GetOrdinal("SampleNo")).ToString("#"), _normalFont, "center");
                    PdfReports.AddText2Table(table, drSamples.GetDecimal(drSamples.GetOrdinal("Sugar_Content")).ToString("0.00"), _normalFont, "center");
                    PdfReports.AddText2Table(table, drSamples.GetDecimal(drSamples.GetOrdinal("Gross_Weight")).ToString("#,##0.0"), _normalFont, "right");
                    PdfReports.AddText2Table(table, drSamples.GetDecimal(drSamples.GetOrdinal("Clean_Weight")).ToString("#,##0.0"), _normalFont, "right");
                    PdfReports.AddText2Table(table, drSamples.GetDecimal(drSamples.GetOrdinal("Tare")).ToString("0.00"), _normalFont, "center");
                    PdfReports.AddText2Table(table, drSamples.GetString(drSamples.GetOrdinal("High_Tare")), _normalFont, "center");
                    PdfReports.AddText2Table(table, drSamples.GetInt32(drSamples.GetOrdinal("Sample_Quality")).ToString(), _normalFont, "center");
                    PdfReports.AddText2Table(table, drSamples.GetDecimal(drSamples.GetOrdinal("SLM_Pct")).ToString("0.0000"), _normalFont, "center");

                    PdfReports.AddTableNoSplit(document, pgEvent, table);
                }

                pgEvent.HeaderNameList = null;
                pgEvent.HeaderTableLayout = null;

                // Follow the table with a blank line
                table = PdfReports.CreateTable(_tareTableLayout, 1);
                PdfReports.AddText2Table(table, " ", _normalFont, _tareTableLayout.Length);

                PdfReports.AddTableNoSplit(document, pgEvent, table);
            }
            catch (Exception ex) {

                WSCIEMP.Common.CException wscEx = new WSCIEMP.Common.CException(MOD_NAME + "." + METHOD_NAME, ex);
                throw (wscEx);
            }
        }
        private static void GrowerTareReportEmailFax(SqlConnection conn, string filePath, ref List<ListGrowerTareItem> emailList, string rptTitle, int cropYear )
        {
            const string METHOD_NAME = "GrowerTareReportEmailFax";
            const string SEND_RPT_SUBJECT = "WESTERN SUGAR COOPERATIVE - Daily Grower Tare Detail Report";
            const int RESET_FLAG = 0;

            FileStream fs = null;
            Document document = null;
            PdfWriter writer = null;
            DailyGrowerTareDetailEvent pgEvent = null;

            int lastContractID = 0, lastStationID = 0;
            int index = 0;
            string destinationFile = "", lastRptType = "";
            string firstDeliveryDate = "", busName = "", address1 = "", address2 = "", CSZ = "", emailAddress = "", faxNumber = "";

            try {

                for (index = 0; index < emailList.Count; index++) {

                    ListGrowerTareItem hdrItem = emailList[index];

                    //----------------------------------------------------------------
                    // Changed contract or station, start a new print out.
                    //----------------------------------------------------------------
                    if (hdrItem.ContractID != lastContractID || hdrItem.Delivery_Station_ID != lastStationID) {

                        if (document == null) {

                            GetAddressInfo(conn, hdrItem.ContractID, out busName, out address1, out address2, out CSZ,
                                            out emailAddress, out faxNumber);

                            // IF YOU CHANGE MARGINS, CHANGE YOUR TABLE LAYOUTS !!!
                            //  ***  US LETTER: 612 X 792  ***
                            document = new Document(PortraitPageSize.PgPageSize, PortraitPageSize.PgLeftMargin,
                                                    PortraitPageSize.PgRightMargin, PortraitPageSize.PgTopMargin,
                                                    PortraitPageSize.PgBottomMargin);

                            destinationFile = filePath.Substring(0, filePath.Length - 4) + "_" + hdrItem.Delivery_Station_No + "_" + hdrItem.Contract_No.ToString() + ".pdf";

                            // we create a writer that listens to the document
                            // and directs a PDF-stream to a file
                            if (File.Exists(destinationFile)) {
                                File.Delete(destinationFile);
                            }
                            fs = new FileStream(destinationFile, FileMode.Create, FileAccess.Write, FileShare.Read);
                            writer = PdfWriter.GetInstance(document, fs);

                            // Attach my override event handler(s)
                            pgEvent = new DailyGrowerTareDetailEvent();
                            pgEvent.FillEvent(hdrItem.Contract_No, busName, hdrItem.Delivery_Factory_No, address1, address2, CSZ,
                                                RESET_FLAG, rptTitle);
                            writer.PageEvent = pgEvent;

                            // Open the document
                            document.Open();
                        }

                        if (lastContractID != 0) {

                            //--------------------------------------------------------------------------
                            // Display Truck information for the first delivery day.
                            //--------------------------------------------------------------------------
                            int loadCount = 0;

                            try {
                                // Get the truck data.
                                SqlParameter[] spParams = null;
                                using (
                                    SqlDataReader drTrucks = WSCReportsExec.GrowerDetailReportASH(conn,
                                        lastContractID, lastStationID, firstDeliveryDate, ref spParams)) {

                                    rptDailyGrowerTareDetail.AddTruckDetail(ref document, drTrucks, pgEvent);
                                    drTrucks.Close();

                                    loadCount = Convert.ToInt32(spParams[3].Value);

                                    if (loadCount > 0) {

                                        rptDailyGrowerTareDetail.AddTruckTotals(ref document, loadCount.ToString(),
                                            Convert.ToInt32(spParams[4].Value).ToString("#,##0"),
                                            Convert.ToInt32(spParams[5].Value).ToString("#,##0"), pgEvent);
                                    }
                                }
                            }
                            catch {
                                emailList[index - 1].Success += "Fail: Truck Detail ";
                            }

                            // Save File & Send File
                            // ======================================================
                            // Close document and write effectively saves the file
                            // ======================================================
                            if (document != null) {
                                if (pgEvent != null) {
                                    pgEvent.IsDocumentClosing = true;
                                }
                                document.Close();
                                document = null;
                            }
                            if (writer != null) {
                                writer.Close();
                                writer = null;
                            }
                            fs.Close();
                            fs = null;

                            // Send Report File
                            if (lastRptType == "E") {

                                if (!SendEmailReport(SEND_RPT_SUBJECT,
                                    ConfigurationManager.AppSettings["email.target.employeeServiceFrom"].ToString(), emailAddress, destinationFile)) {

                                    emailList[index - 1].Success += "Fail: Email ";
                                }
                            } else {

                                // Send FAX
                                if (!SendFaxReport(SEND_RPT_SUBJECT,
                                    ConfigurationManager.AppSettings["email.target.employeeServiceFrom"].ToString(),
                                    "", faxNumber, busName, "", busName, destinationFile)) {

                                    emailList[index - 1].Success += "Fail: Fax ";
                                }

                            }

                            GetAddressInfo(conn, hdrItem.ContractID, out busName, out address1, out address2, out CSZ,
                                            out emailAddress, out faxNumber);

                            // New document needed.
                            document = new Document(PortraitPageSize.PgPageSize, PortraitPageSize.PgLeftMargin,
                                                    PortraitPageSize.PgRightMargin, PortraitPageSize.PgTopMargin,
                                                    PortraitPageSize.PgBottomMargin);

                            destinationFile = filePath.Substring(0, filePath.Length - 4) + "_" + hdrItem.Delivery_Station_No + "_" + hdrItem.Contract_No.ToString() + ".pdf";

                            // we create a writer that listens to the document
                            // and directs a PDF-stream to a file
                            if (File.Exists(destinationFile)) {
                                File.Delete(destinationFile);
                            }
                            fs = new FileStream(destinationFile, FileMode.Create, FileAccess.Write, FileShare.Read);
                            writer = PdfWriter.GetInstance(document, fs);

                            // Attach my override event handler(s)
                            pgEvent = new DailyGrowerTareDetailEvent();
                            pgEvent.FillEvent(hdrItem.Contract_No, busName, hdrItem.Delivery_Factory_No, address1, address2, CSZ,
                                                RESET_FLAG, rptTitle);
                            writer.PageEvent = pgEvent;

                            // Open the document
                            document.Open();

                        }

                        firstDeliveryDate = hdrItem.Delivery_Date;
                        lastStationID = hdrItem.Delivery_Station_ID;
                        lastContractID = hdrItem.ContractID;
                        lastRptType = hdrItem.RptType;
                    }

                    try {
                        AddSampleHdr(ref document, hdrItem, cropYear, pgEvent);

                        using (
                            SqlDataReader drSamples = WSCReportsExec.GrowerDetailReportTares(conn, hdrItem.ContractID, hdrItem.Delivery_Date)) {
                            rptDailyGrowerTareDetail.AddSampleDetail(ref document, drSamples, pgEvent);
                        }
                    }
                    catch {
                        emailList[index].Success += "Fail: Sample Detail ";
                    }
                }

                if (lastContractID != 0) {

                    //--------------------------------------------------------------------------
                    // Display Truck information for the first delivery day.
                    //--------------------------------------------------------------------------
                    int loadCount = 0;

                    try {
                        // Get the truck data.
                        SqlParameter[] spParams = null;
                        using (SqlDataReader drTrucks = WSCReportsExec.GrowerDetailReportASH(conn,
                            lastContractID, lastStationID, firstDeliveryDate, ref spParams)) {

                            rptDailyGrowerTareDetail.AddTruckDetail(ref document, drTrucks, pgEvent);
                            drTrucks.Close();

                            loadCount = Convert.ToInt32(spParams[3].Value);

                            if (loadCount > 0) {

                                rptDailyGrowerTareDetail.AddTruckTotals(ref document, loadCount.ToString(),
                                    Convert.ToInt32(spParams[4].Value).ToString("#,##0"), Convert.ToInt32(spParams[5].Value).ToString("#,##0"),
                                    pgEvent);
                            }
                        }
                    }
                    catch {
                        emailList[index - 1].Success += "Fail: Truck Detail ";
                    }
                }

                // Save File & Send File
                // ======================================================
                // Close document and write effectively saves the file
                // ======================================================
                if (document != null) {
                    if (pgEvent != null) {
                        pgEvent.IsDocumentClosing = true;
                    }
                    document.Close();
                    document = null;
                }
                if (writer != null) {
                    writer.Close();
                    writer = null;
                }
                fs.Close();
                fs = null;

                // Send Report File
                if (lastRptType == "E") {

                    if (!SendEmailReport(SEND_RPT_SUBJECT,
                        ConfigurationManager.AppSettings["email.target.employeeServiceFrom"].ToString(), emailAddress, destinationFile)) {

                        emailList[index - 1].Success += "Fail: Email ";
                    }
                } else {

                    // Send FAX
                    if (!SendFaxReport(SEND_RPT_SUBJECT,
                        ConfigurationManager.AppSettings["email.target.employeeServiceFrom"].ToString(),
                        "", faxNumber, busName, "", busName, destinationFile)) {

                        emailList[index - 1].Success += "Fail: FAx ";
                    }

                }
            }
            catch (Exception ex) {
                string errMsg = "document is null: " + (document == null).ToString() + "; " +
                    "writer is null: " + (writer == null).ToString();
                CException wscex = new CException(METHOD_NAME + errMsg, ex);
                throw (wscex);
            }
            finally {

                if (document != null) {
                    pgEvent.IsDocumentClosing = true;
                    document.Close();
                }
                if (writer != null) {
                    writer.Close();
                }
            }
        }
        private static void AddSampleHdr(ref Document document, ListGrowerTareItem hdrItem, int cropYear, DailyGrowerTareDetailEvent pgEvent)
        {
            PdfPTable table = PdfReports.CreateTable(_primaryTableLayout, 1);

            PdfReports.AddText2Table(table, "Delivery Date", _normalFont);
            PdfReports.AddText2Table(table, hdrItem.Delivery_Date, _normalFont);
            PdfReports.AddText2Table(table, "1st Net Lbs", _normalFont);
            PdfReports.AddText2Table(table, hdrItem.First_Net_Pounds.ToString(), _normalFont);
            PdfReports.AddText2Table(table, " ", _normalFont);
            PdfReports.AddText2Table(table, "% Sugar", _normalFont);
            PdfReports.AddText2Table(table, hdrItem.Sugar_Content.ToString("0.00"), _normalFont);
            PdfReports.AddText2Table(table, " ", _normalFont);

            PdfReports.AddText2Table(table, "Station No", _normalFont);
            PdfReports.AddText2Table(table, hdrItem.Delivery_Station_No, _normalFont);
            PdfReports.AddText2Table(table, "Tare Lbs", _normalFont);
            PdfReports.AddText2Table(table, hdrItem.Tare_Pounds.ToString(), _normalFont);
            PdfReports.AddText2Table(table, hdrItem.Tare.ToString("0.00"), _normalFont);
            PdfReports.AddText2Table(table, "SLM", _normalFont);
            PdfReports.AddText2Table(table, hdrItem.SLM_Pct.ToString("0.0000"), _normalFont);
            PdfReports.AddText2Table(table, " ", _normalFont);

            PdfReports.AddText2Table(table, "Station Name", _normalFont);
            PdfReports.AddText2Table(table, hdrItem.Delivery_Station_Name, _normalFont);
            PdfReports.AddText2Table(table, "Final Net Lbs", _normalFont);
            PdfReports.AddText2Table(table, hdrItem.Final_Net_Pounds.ToString(), _normalFont);
            PdfReports.AddText2Table(table, " ", _normalFont);
            PdfReports.AddText2Table(table, "Lbs Extractable Sugar/Ton", _normalFont, 2);
            PdfReports.AddText2Table(table, hdrItem.ExSugarPerTon.ToString(), _normalFont);
            PdfReports.AddText2Table(table, " ", _labelFont, _primaryTableLayout.Length);
            PdfReports.AddTableNoSplit(document, pgEvent, table);
        }
示例#14
0
        public static void ReportBuilder(List <ListGrowerTareItem> hdrList, int cropYear, bool isPosted, bool isPreview, bool isHardCopy, bool isEmail,
                                         bool isFax, FileStream fs, string filePath)
        {
            const string METHOD_NAME = "ReportBuilder";
            const int    RESET_FLAG  = 0;

            Document  document = null;
            PdfWriter writer   = null;
            DailyGrowerTareDetailEvent pgEvent      = null;
            List <ListGrowerTareItem>  emailFaxList = null;
            List <ListGrowerTareItem>  hardCopyList = null;

            int    lastContractID = 0, lastStationID = 0;
            int    index = 0;
            string firstDeliveryDate = "", busName = "", address1 = "", address2 = "", CSZ = "", emailAddress = "", faxNumber = "";

            string rptTitle = "Western Sugar Cooperative\nDaily Grower Tare Detail Report";

            try {
                if (!isPreview)
                {
                    emailFaxList = hdrList.FindAll(item => item.RptType == "E" || item.RptType == "F");
                    hardCopyList = hdrList.FindAll(item => item.RptType == "M");
                }
                else
                {
                    // In preview mode we're going to lump these all together and print them.
                    hardCopyList = hdrList;
                }

                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BeetConn"].ToString())) {
                    if (emailFaxList != null && emailFaxList.Count > 0)
                    {
                        GrowerTareReportEmailFax(conn, filePath, ref emailFaxList, rptTitle, cropYear);
                    }


                    for (index = 0; index < hardCopyList.Count; index++)
                    {
                        ListGrowerTareItem hdrItem = hardCopyList[index];

                        //----------------------------------------------------------------
                        // Changed contract or station, start a new print out.
                        //----------------------------------------------------------------
                        if (hdrItem.ContractID != lastContractID || hdrItem.Delivery_Station_ID != lastStationID)
                        {
                            if (document == null)
                            {
                                GetAddressInfo(conn, hdrItem.ContractID, out busName, out address1, out address2, out CSZ,
                                               out emailAddress, out faxNumber);

                                // IF YOU CHANGE MARGINS, CHANGE YOUR TABLE LAYOUTS !!!
                                //  ***  US LETTER: 612 X 792  ***
                                document = new Document(PortraitPageSize.PgPageSize, PortraitPageSize.PgLeftMargin,
                                                        PortraitPageSize.PgRightMargin, PortraitPageSize.PgTopMargin,
                                                        PortraitPageSize.PgBottomMargin);

                                // we create a writer that listens to the document
                                // and directs a PDF-stream to a file
                                writer = PdfWriter.GetInstance(document, fs);

                                // Attach my override event handler(s)
                                pgEvent = new DailyGrowerTareDetailEvent();
                                pgEvent.FillEvent(hdrItem.Contract_No, busName, hdrItem.Delivery_Factory_No, address1, address2, CSZ,
                                                  RESET_FLAG, rptTitle + (!isPreview ? "" : " - " + hdrItem.RptType));
                                writer.PageEvent = pgEvent;

                                // Open the document
                                document.Open();
                            }

                            if (lastContractID != 0)
                            {
                                //--------------------------------------------------------------------------
                                // Display Truck information for the first delivery day.
                                //--------------------------------------------------------------------------
                                int loadCount = 0;
                                //if (nextDeliveryDate == firstDeliveryDate) {

                                try {
                                    // Get the truck data.
                                    SqlParameter[] spParams = null;
                                    using (
                                        SqlDataReader drTrucks = WSCReportsExec.GrowerDetailReportASH(conn,
                                                                                                      lastContractID, lastStationID, firstDeliveryDate, ref spParams)) {
                                        rptDailyGrowerTareDetail.AddTruckDetail(ref document, drTrucks, pgEvent);
                                        drTrucks.Close();

                                        loadCount = Convert.ToInt32(spParams[3].Value);

                                        if (loadCount > 0)
                                        {
                                            rptDailyGrowerTareDetail.AddTruckTotals(ref document, loadCount.ToString(),
                                                                                    Convert.ToInt32(spParams[4].Value).ToString("#,##0"),
                                                                                    Convert.ToInt32(spParams[5].Value).ToString("#,##0"), pgEvent);
                                        }
                                    }
                                }
                                catch {
                                    hardCopyList[index - 1].Success += "Fail: Truck Detail ";
                                }
                                GetAddressInfo(conn, hdrItem.ContractID, out busName, out address1, out address2, out CSZ,
                                               out emailAddress, out faxNumber);

                                // New Page !: in Preview mode, append the report type to the title of the report.
                                pgEvent.FillEvent(hdrItem.Contract_No, busName, hdrItem.Delivery_Factory_No, address1, address2, CSZ,
                                                  RESET_FLAG, rptTitle + (!isPreview ? "" : " - " + hdrItem.RptType));
                                document.NewPage();
                            }

                            firstDeliveryDate = hdrItem.Delivery_Date;
                            lastStationID     = hdrItem.Delivery_Station_ID;
                            lastContractID    = hdrItem.ContractID;
                        }

                        try {
                            AddSampleHdr(ref document, hdrItem, cropYear, pgEvent);

                            using (
                                SqlDataReader drSamples = WSCReportsExec.GrowerDetailReportTares(conn, hdrItem.ContractID, hdrItem.Delivery_Date)) {
                                rptDailyGrowerTareDetail.AddSampleDetail(ref document, drSamples, pgEvent);
                            }
                        }
                        catch {
                            hardCopyList[index].Success += "Fail: Sample Detail ";
                        }
                    }

                    if (lastContractID != 0)
                    {
                        //--------------------------------------------------------------------------
                        // Display Truck information for the first delivery day.
                        //--------------------------------------------------------------------------
                        int loadCount = 0;

                        try {
                            // Get the truck data.
                            SqlParameter[] spParams = null;
                            using (SqlDataReader drTrucks = WSCReportsExec.GrowerDetailReportASH(conn,
                                                                                                 lastContractID, lastStationID, firstDeliveryDate, ref spParams)) {
                                rptDailyGrowerTareDetail.AddTruckDetail(ref document, drTrucks, pgEvent);
                                drTrucks.Close();

                                loadCount = Convert.ToInt32(spParams[3].Value);

                                if (loadCount > 0)
                                {
                                    rptDailyGrowerTareDetail.AddTruckTotals(ref document, loadCount.ToString(),
                                                                            Convert.ToInt32(spParams[4].Value).ToString("#,##0"), Convert.ToInt32(spParams[5].Value).ToString("#,##0"),
                                                                            pgEvent);
                                }
                            }
                        }
                        catch {
                            hardCopyList[index - 1].Success += "Fail: Truck Detail ";
                        }
                    }
                }

                //------------------------------------
                // Print a Process Summary
                //------------------------------------
                if (document != null && writer != null)
                {
                    pgEvent.IsSummary = true;
                    document.NewPage();

                    PdfPTable procSumTab = null;
                    PrintSummary(document, ref procSumTab, hdrList, isPreview);
                    PdfReports.AddTableNoSplit(document, pgEvent, procSumTab);
                }
                else
                {
                    document = new Document(PortraitPageSize.PgPageSize, PortraitPageSize.PgLeftMargin,
                                            PortraitPageSize.PgRightMargin, PortraitPageSize.PgTopMargin,
                                            PortraitPageSize.PgBottomMargin);

                    // we create a writer that listens to the document
                    // and directs a PDF-stream to a file
                    writer = PdfWriter.GetInstance(document, fs);

                    // Attach my override event handler(s)
                    pgEvent           = new DailyGrowerTareDetailEvent();
                    pgEvent.IsSummary = true;
                    writer.PageEvent  = pgEvent;

                    // Open the document
                    document.Open();

                    PdfPTable procSumTab = null;
                    PrintSummary(document, ref procSumTab, hdrList, isPreview);
                    PdfReports.AddTableNoSplit(document, pgEvent, procSumTab);
                }


                // ======================================================
                // Close document
                // ======================================================
                if (document != null)
                {
                    pgEvent.IsDocumentClosing = true;
                    document.Close();
                    document = null;
                }
            }
            catch (Exception ex) {
                string errMsg = "document is null: " + (document == null).ToString() + "; " +
                                "writer is null: " + (writer == null).ToString();
                CException wscex = new CException(METHOD_NAME + errMsg, ex);
                throw (wscex);
            }
            finally {
                if (document != null)
                {
                    pgEvent.IsDocumentClosing = true;
                    document.Close();
                }
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }
示例#15
0
        private static void AddSampleHdr(ref Document document, ListGrowerTareItem hdrItem, int cropYear, DailyGrowerTareDetailEvent pgEvent)
        {
            PdfPTable table = PdfReports.CreateTable(_primaryTableLayout, 1);

            PdfReports.AddText2Table(table, "Delivery Date", _normalFont);
            PdfReports.AddText2Table(table, hdrItem.Delivery_Date, _normalFont);
            PdfReports.AddText2Table(table, "1st Net Lbs", _normalFont);
            PdfReports.AddText2Table(table, hdrItem.First_Net_Pounds.ToString(), _normalFont);
            PdfReports.AddText2Table(table, " ", _normalFont);
            PdfReports.AddText2Table(table, "% Sugar", _normalFont);
            PdfReports.AddText2Table(table, hdrItem.Sugar_Content.ToString("0.00"), _normalFont);
            PdfReports.AddText2Table(table, " ", _normalFont);

            PdfReports.AddText2Table(table, "Station No", _normalFont);
            PdfReports.AddText2Table(table, hdrItem.Delivery_Station_No, _normalFont);
            PdfReports.AddText2Table(table, "Tare Lbs", _normalFont);
            PdfReports.AddText2Table(table, hdrItem.Tare_Pounds.ToString(), _normalFont);
            PdfReports.AddText2Table(table, hdrItem.Tare.ToString("0.00"), _normalFont);
            PdfReports.AddText2Table(table, "SLM", _normalFont);
            PdfReports.AddText2Table(table, hdrItem.SLM_Pct.ToString("0.0000"), _normalFont);
            PdfReports.AddText2Table(table, " ", _normalFont);

            PdfReports.AddText2Table(table, "Station Name", _normalFont);
            PdfReports.AddText2Table(table, hdrItem.Delivery_Station_Name, _normalFont);
            PdfReports.AddText2Table(table, "Final Net Lbs", _normalFont);
            PdfReports.AddText2Table(table, hdrItem.Final_Net_Pounds.ToString(), _normalFont);
            PdfReports.AddText2Table(table, " ", _normalFont);
            PdfReports.AddText2Table(table, "Lbs Extractable Sugar/Ton", _normalFont, 2);
            PdfReports.AddText2Table(table, hdrItem.ExSugarPerTon.ToString(), _normalFont);
            PdfReports.AddText2Table(table, " ", _labelFont, _primaryTableLayout.Length);
            PdfReports.AddTableNoSplit(document, pgEvent, table);
        }
示例#16
0
        private static void GrowerTareReportEmailFax(SqlConnection conn, string filePath, ref List <ListGrowerTareItem> emailList, string rptTitle, int cropYear)
        {
            const string METHOD_NAME      = "GrowerTareReportEmailFax";
            const string SEND_RPT_SUBJECT = "WESTERN SUGAR COOPERATIVE - Daily Grower Tare Detail Report";
            const int    RESET_FLAG       = 0;

            FileStream fs       = null;
            Document   document = null;
            PdfWriter  writer   = null;
            DailyGrowerTareDetailEvent pgEvent = null;

            int    lastContractID = 0, lastStationID = 0;
            int    index = 0;
            string destinationFile = "", lastRptType = "";
            string firstDeliveryDate = "", busName = "", address1 = "", address2 = "", CSZ = "", emailAddress = "", faxNumber = "";

            try {
                for (index = 0; index < emailList.Count; index++)
                {
                    ListGrowerTareItem hdrItem = emailList[index];

                    //----------------------------------------------------------------
                    // Changed contract or station, start a new print out.
                    //----------------------------------------------------------------
                    if (hdrItem.ContractID != lastContractID || hdrItem.Delivery_Station_ID != lastStationID)
                    {
                        if (document == null)
                        {
                            GetAddressInfo(conn, hdrItem.ContractID, out busName, out address1, out address2, out CSZ,
                                           out emailAddress, out faxNumber);

                            // IF YOU CHANGE MARGINS, CHANGE YOUR TABLE LAYOUTS !!!
                            //  ***  US LETTER: 612 X 792  ***
                            document = new Document(PortraitPageSize.PgPageSize, PortraitPageSize.PgLeftMargin,
                                                    PortraitPageSize.PgRightMargin, PortraitPageSize.PgTopMargin,
                                                    PortraitPageSize.PgBottomMargin);

                            destinationFile = filePath.Substring(0, filePath.Length - 4) + "_" + hdrItem.Delivery_Station_No + "_" + hdrItem.Contract_No.ToString() + ".pdf";

                            // we create a writer that listens to the document
                            // and directs a PDF-stream to a file
                            if (File.Exists(destinationFile))
                            {
                                File.Delete(destinationFile);
                            }
                            fs     = new FileStream(destinationFile, FileMode.Create, FileAccess.Write, FileShare.Read);
                            writer = PdfWriter.GetInstance(document, fs);

                            // Attach my override event handler(s)
                            pgEvent = new DailyGrowerTareDetailEvent();
                            pgEvent.FillEvent(hdrItem.Contract_No, busName, hdrItem.Delivery_Factory_No, address1, address2, CSZ,
                                              RESET_FLAG, rptTitle);
                            writer.PageEvent = pgEvent;

                            // Open the document
                            document.Open();
                        }

                        if (lastContractID != 0)
                        {
                            //--------------------------------------------------------------------------
                            // Display Truck information for the first delivery day.
                            //--------------------------------------------------------------------------
                            int loadCount = 0;

                            try {
                                // Get the truck data.
                                SqlParameter[] spParams = null;
                                using (
                                    SqlDataReader drTrucks = WSCReportsExec.GrowerDetailReportASH(conn,
                                                                                                  lastContractID, lastStationID, firstDeliveryDate, ref spParams)) {
                                    rptDailyGrowerTareDetail.AddTruckDetail(ref document, drTrucks, pgEvent);
                                    drTrucks.Close();

                                    loadCount = Convert.ToInt32(spParams[3].Value);

                                    if (loadCount > 0)
                                    {
                                        rptDailyGrowerTareDetail.AddTruckTotals(ref document, loadCount.ToString(),
                                                                                Convert.ToInt32(spParams[4].Value).ToString("#,##0"),
                                                                                Convert.ToInt32(spParams[5].Value).ToString("#,##0"), pgEvent);
                                    }
                                }
                            }
                            catch {
                                emailList[index - 1].Success += "Fail: Truck Detail ";
                            }

                            // Save File & Send File
                            // ======================================================
                            // Close document and write effectively saves the file
                            // ======================================================
                            if (document != null)
                            {
                                if (pgEvent != null)
                                {
                                    pgEvent.IsDocumentClosing = true;
                                }
                                document.Close();
                                document = null;
                            }
                            if (writer != null)
                            {
                                writer.Close();
                                writer = null;
                            }
                            fs.Close();
                            fs = null;

                            // Send Report File
                            if (lastRptType == "E")
                            {
                                if (!SendEmailReport(SEND_RPT_SUBJECT,
                                                     ConfigurationManager.AppSettings["email.target.employeeServiceFrom"].ToString(), emailAddress, destinationFile))
                                {
                                    emailList[index - 1].Success += "Fail: Email ";
                                }
                            }
                            else
                            {
                                // Send FAX
                                if (!SendFaxReport(SEND_RPT_SUBJECT,
                                                   ConfigurationManager.AppSettings["email.target.employeeServiceFrom"].ToString(),
                                                   "", faxNumber, busName, "", busName, destinationFile))
                                {
                                    emailList[index - 1].Success += "Fail: Fax ";
                                }
                            }

                            GetAddressInfo(conn, hdrItem.ContractID, out busName, out address1, out address2, out CSZ,
                                           out emailAddress, out faxNumber);

                            // New document needed.
                            document = new Document(PortraitPageSize.PgPageSize, PortraitPageSize.PgLeftMargin,
                                                    PortraitPageSize.PgRightMargin, PortraitPageSize.PgTopMargin,
                                                    PortraitPageSize.PgBottomMargin);

                            destinationFile = filePath.Substring(0, filePath.Length - 4) + "_" + hdrItem.Delivery_Station_No + "_" + hdrItem.Contract_No.ToString() + ".pdf";

                            // we create a writer that listens to the document
                            // and directs a PDF-stream to a file
                            if (File.Exists(destinationFile))
                            {
                                File.Delete(destinationFile);
                            }
                            fs     = new FileStream(destinationFile, FileMode.Create, FileAccess.Write, FileShare.Read);
                            writer = PdfWriter.GetInstance(document, fs);

                            // Attach my override event handler(s)
                            pgEvent = new DailyGrowerTareDetailEvent();
                            pgEvent.FillEvent(hdrItem.Contract_No, busName, hdrItem.Delivery_Factory_No, address1, address2, CSZ,
                                              RESET_FLAG, rptTitle);
                            writer.PageEvent = pgEvent;

                            // Open the document
                            document.Open();
                        }

                        firstDeliveryDate = hdrItem.Delivery_Date;
                        lastStationID     = hdrItem.Delivery_Station_ID;
                        lastContractID    = hdrItem.ContractID;
                        lastRptType       = hdrItem.RptType;
                    }

                    try {
                        AddSampleHdr(ref document, hdrItem, cropYear, pgEvent);

                        using (
                            SqlDataReader drSamples = WSCReportsExec.GrowerDetailReportTares(conn, hdrItem.ContractID, hdrItem.Delivery_Date)) {
                            rptDailyGrowerTareDetail.AddSampleDetail(ref document, drSamples, pgEvent);
                        }
                    }
                    catch {
                        emailList[index].Success += "Fail: Sample Detail ";
                    }
                }

                if (lastContractID != 0)
                {
                    //--------------------------------------------------------------------------
                    // Display Truck information for the first delivery day.
                    //--------------------------------------------------------------------------
                    int loadCount = 0;

                    try {
                        // Get the truck data.
                        SqlParameter[] spParams = null;
                        using (SqlDataReader drTrucks = WSCReportsExec.GrowerDetailReportASH(conn,
                                                                                             lastContractID, lastStationID, firstDeliveryDate, ref spParams)) {
                            rptDailyGrowerTareDetail.AddTruckDetail(ref document, drTrucks, pgEvent);
                            drTrucks.Close();

                            loadCount = Convert.ToInt32(spParams[3].Value);

                            if (loadCount > 0)
                            {
                                rptDailyGrowerTareDetail.AddTruckTotals(ref document, loadCount.ToString(),
                                                                        Convert.ToInt32(spParams[4].Value).ToString("#,##0"), Convert.ToInt32(spParams[5].Value).ToString("#,##0"),
                                                                        pgEvent);
                            }
                        }
                    }
                    catch {
                        emailList[index - 1].Success += "Fail: Truck Detail ";
                    }
                }

                // Save File & Send File
                // ======================================================
                // Close document and write effectively saves the file
                // ======================================================
                if (document != null)
                {
                    if (pgEvent != null)
                    {
                        pgEvent.IsDocumentClosing = true;
                    }
                    document.Close();
                    document = null;
                }
                if (writer != null)
                {
                    writer.Close();
                    writer = null;
                }
                fs.Close();
                fs = null;

                // Send Report File
                if (lastRptType == "E")
                {
                    if (!SendEmailReport(SEND_RPT_SUBJECT,
                                         ConfigurationManager.AppSettings["email.target.employeeServiceFrom"].ToString(), emailAddress, destinationFile))
                    {
                        emailList[index - 1].Success += "Fail: Email ";
                    }
                }
                else
                {
                    // Send FAX
                    if (!SendFaxReport(SEND_RPT_SUBJECT,
                                       ConfigurationManager.AppSettings["email.target.employeeServiceFrom"].ToString(),
                                       "", faxNumber, busName, "", busName, destinationFile))
                    {
                        emailList[index - 1].Success += "Fail: FAx ";
                    }
                }
            }
            catch (Exception ex) {
                string errMsg = "document is null: " + (document == null).ToString() + "; " +
                                "writer is null: " + (writer == null).ToString();
                CException wscex = new CException(METHOD_NAME + errMsg, ex);
                throw (wscex);
            }
            finally {
                if (document != null)
                {
                    pgEvent.IsDocumentClosing = true;
                    document.Close();
                }
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }