Ejemplo n.º 1
0
        private static void ReportBuilder(List<SHPaySumListItem> stateList, int cropYear, string statementDate, string shid,
            string fromShid, string toShid, int paymentDescID, bool isCumulative, string rptFooter, string logoUrl, FileStream fs)
        {
            const string METHOD_NAME = "rptPaymentSummary.ReportBuilder: ";
            Document document = null;
            PdfWriter writer = null;
            PdfPTable table = null;
            PaymentSummaryEvent pgEvent = null;
            iTextSharp.text.Image imgLogo = null;

            decimal totalTons = 0;
            decimal totalEHPrem = 0;
            decimal totalGross = 0;
            decimal totalDeductions = 0;
            decimal totalNet = 0;
            decimal totalGrowerNet = 0;
            decimal totalLandownerNet = 0;
            decimal checkAmount = 0;
            int resetFlag = 0;
            int checkSequence = 0;
            int payeeNumber = 0;

            string rptTitle = "Western Sugar Cooperative Payment Summary";

            Font headerFont = FontFactory.GetFont("HELVETICA", 8F, Font.NORMAL);
            Font normalFont = FontFactory.GetFont("HELVETICA", 8F, Font.NORMAL);
            Font labelFont = FontFactory.GetFont("HELVETICA", 8F, Font.BOLD);

            try {

                int firstContractNumber = stateList.Min(c => c.i_ContractNumber);
                int lastContractNumber = stateList.Max(c => c.i_ContractNumber);

                List<TransDeductionListItem> deductionList = WSCPayment.GetTransmittalDeduction2(ConfigurationManager.ConnectionStrings["BeetConn"].ToString(),
                    cropYear, paymentDescID, 0, firstContractNumber, lastContractNumber, isCumulative);

                foreach (SHPaySumListItem item in stateList) {

                    if (document == null) {

                        // IF YOU CHANGE MARGINS, CHANGE YOUR TABLE LAYOUTS !!!
                        //  ***  US LETTER: 612 X 792  ***
                        //document = new Document(iTextSharp.text.PageSize.LETTER, 36, 36, 54, 72);
                        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);

                        imgLogo = PdfReports.GetImage(logoUrl, 127, 50, iTextSharp.text.Element.ALIGN_CENTER);

                        // Attach my override event handler(s)
                        checkSequence = item.i_CheckSequence;
                        pgEvent = new PaymentSummaryEvent();
                        pgEvent.FillEvent(item, cropYear, statementDate, resetFlag, rptTitle, imgLogo);

                        writer.PageEvent = pgEvent;
                        pgEvent.HeaderNameList = _detailSectionHdrNames;
                        pgEvent.HeaderTableLayout = _primaryTableLayout;

                        // Open the document
                        document.Open();
                        pgEvent.HeaderTableLayout = null;

                        table = PdfReports.CreateTable(_primaryTableLayout, 1);
                        checkAmount = item.d_checkAmount;
                        payeeNumber = item.i_Payee_Number;

                    } else {

                        if (checkSequence != item.i_CheckSequence) {

                            AddTotals(ref writer, ref document, ref table, labelFont, normalFont, totalTons,
                                totalEHPrem, totalGross, totalDeductions, totalNet, checkAmount,
                                payeeNumber, totalGrowerNet, totalLandownerNet, pgEvent);
                            AddFooter(ref writer, ref document, normalFont, rptFooter, pgEvent);

                            // Clear totals values
                            totalTons = 0;
                            totalEHPrem = 0;
                            totalGross = 0;
                            totalDeductions = 0;
                            totalNet = 0;
                            totalGrowerNet = 0;
                            totalLandownerNet = 0;

                            checkSequence = item.i_CheckSequence;

                            // NEW CHECK
                            pgEvent.FillEvent(item, cropYear, statementDate, resetFlag, rptTitle, imgLogo);
                            pgEvent.HeaderTableLayout = _primaryTableLayout;

                            document.NewPage();
                            pgEvent.HeaderTableLayout = null;

                            table = PdfReports.CreateTable(_primaryTableLayout, 1);
                            checkAmount = item.d_checkAmount;
                            payeeNumber = item.i_Payee_Number;

                            //AddDetailSectionHdr(ref table, labelFont, normalFont);
                        }
                    }

                    // =======================================================
                    // Contract Number Line
                    // =======================================================
                    PdfReports.AddText2Table(table, "Contract", labelFont, "left");
                    PdfReports.AddText2Table(table, item.i_ContractNumber.ToString(), labelFont, "center");
                    PdfReports.AddText2Table(table, " ", normalFont);
                    PdfReports.AddText2Table(table, item.d_Avg_SLM.ToString("N4"), normalFont, "right");

                    PdfReports.AddText2Table(table, " ", normalFont, 3);
                    PdfReports.AddText2Table(table, item.d_EH_Bonus.ToString("N2"), normalFont, "right", 2);
                    totalEHPrem += item.d_EH_Bonus;
                    PdfReports.AddText2Table(table, " ", normalFont, 4);

                    // =======================================================
                    // Station Name Line
                    // =======================================================
                    PdfReports.AddText2Table(table, "Station", normalFont);
                    PdfReports.AddText2Table(table, item.s_Station_Name, normalFont, "center");
                    PdfReports.AddText2Table(table, "EH", normalFont, "center");
                    PdfReports.AddText2Table(table, item.d_EH_SLM.ToString("N4"), normalFont, "right");
                    PdfReports.AddText2Table(table, item.d_EH_Sugar.ToString("N2"), normalFont, "right");
                    PdfReports.AddText2Table(table, item.d_EH_Paid.ToString("N3"), normalFont, "right");
                    PdfReports.AddText2Table(table, item.d_EH_Price.ToString("N3"), normalFont, "right");

                    PdfReports.AddText2Table(table, (item.d_EH_Tons - item.d_EH_tons_moved).ToString("N4"), normalFont, "right");
                    totalTons += item.d_EH_Tons;

                    PdfReports.AddText2Table(table, " ", normalFont);

                    PdfReports.AddText2Table(table, (item.d_EH_Gross_Pay - item.d_EH_amt_moved).ToString("N2"), normalFont, "right");
                    totalGross += item.d_EH_Gross_Pay;

                    PdfReports.AddText2Table(table, " ", normalFont, 3);

                    // =======================================================
                    // Landowner Name Line
                    // =======================================================
                    PdfReports.AddText2Table(table, "LO", normalFont);
                    PdfReports.AddText2Table(table, item.s_LOName, normalFont, "center");
                    PdfReports.AddText2Table(table, "RH", normalFont, "center");

                    PdfReports.AddText2Table(table, item.d_RH_SLM.ToString("N4"), normalFont, "right");
                    PdfReports.AddText2Table(table, item.d_RH_Sugar.ToString("N2"), normalFont, "right");
                    PdfReports.AddText2Table(table, item.d_RH_Paid.ToString("N3"), normalFont, "right");
                    PdfReports.AddText2Table(table, item.d_RH_Price.ToString("N3"), normalFont, "right");

                    PdfReports.AddText2Table(table, (item.d_RH_Tons - item.d_RH_tons_moved).ToString("N4"), normalFont, "right");
                    totalTons += item.d_RH_Tons;

                    PdfReports.AddText2Table(table, " ", normalFont);

                    PdfReports.AddText2Table(table, (item.d_RH_Gross_Pay - item.d_RH_amt_moved).ToString("N2"), normalFont, "right");
                    totalGross += item.d_RH_Gross_Pay;

                    PdfReports.AddText2Table(table, item.d_Deduct_Total.ToString("N2"), normalFont, "right");
                    totalDeductions += item.d_Deduct_Total;

                    PdfReports.AddText2Table(table, (item.d_Total_Net - item.d_EH_amt_moved - item.d_RH_amt_moved).ToString("N2"), normalFont, "right");
                    totalNet += item.d_Total_Net;

                    PdfReports.AddText2Table(table, " ", normalFont);

                    // =======================================================
                    // Reduced for Excess Beets
                    // =======================================================
                    // Reduced Early Harvest
                    PdfReports.AddText2Table(table, "Reduced Early Harvest Excess Beets", normalFont, 3);
                    PdfReports.AddText2Table(table, " ", normalFont, "right");
                    PdfReports.AddText2Table(table, " ", normalFont, "right");
                    PdfReports.AddText2Table(table, " ", normalFont, "right");
                    PdfReports.AddText2Table(table, " ", normalFont, "right");

                    PdfReports.AddText2Table(table, item.d_EH_tons_moved.ToString("#,#.0000;(#,#.0000)"), normalFont, "right");
                    //totalTons += item.d_RH_Tons;

                    PdfReports.AddText2Table(table, " ", normalFont);

                    PdfReports.AddText2Table(table, item.d_EH_amt_moved.ToString("#,#.00;(#,#.00)"), normalFont, "right");
                    //totalGross += item.d_RH_Gross_Pay;

                    PdfReports.AddText2Table(table, " ", normalFont, "right");
                    //totalDeductions += item.d_Deduct_Total;

                    PdfReports.AddText2Table(table, item.d_EH_amt_moved.ToString("#,#.00;(#,#.00)"), normalFont, "right");
                    //totalNet += item.d_Total_Net;

                    PdfReports.AddText2Table(table, " ", normalFont);

                    // Reduced Regular Harvest
                    PdfReports.AddText2Table(table, "Reduced Regular Harvest  Excess Beets", normalFont, 3);
                    PdfReports.AddText2Table(table, " ", normalFont, "right");
                    PdfReports.AddText2Table(table, " ", normalFont, "right");
                    PdfReports.AddText2Table(table, " ", normalFont, "right");
                    PdfReports.AddText2Table(table, " ", normalFont, "right");

                    PdfReports.AddText2Table(table, item.d_RH_tons_moved.ToString("#,#.0000;(#,#.0000)"), normalFont, "right");
                    //totalTons += item.d_RH_Tons;

                    PdfReports.AddText2Table(table, " ", normalFont);

                    PdfReports.AddText2Table(table, item.d_RH_amt_moved.ToString("#,#.00;(#,#.00)"), normalFont, "right");
                    //totalGross += item.d_RH_Gross_Pay;

                    PdfReports.AddText2Table(table, " ", normalFont, "right");
                    //totalDeductions += item.d_Deduct_Total;

                    PdfReports.AddText2Table(table, item.d_RH_amt_moved.ToString("#,#.00;(#,#.00)"), normalFont, "right");
                    //totalNet += item.d_Total_Net;

                    PdfReports.AddText2Table(table, " ", normalFont);

                    // =======================================================
                    // Grower / Landowner NET Split
                    // =======================================================
                    PdfReports.AddText2Table(table, " ", normalFont, 9);
                    PdfReports.AddText2Table(table, "Grower Net", labelFont, 2);
                    totalGrowerNet += item.d_groAmount;
                    PdfReports.AddText2Table(table, item.d_groAmount.ToString("N2"), normalFont, "right");
                    PdfReports.AddText2Table(table, " ", normalFont);

                    PdfReports.AddText2Table(table, " ", normalFont, 9);
                    PdfReports.AddText2Table(table, "Landowner Net", labelFont, 2);
                    totalLandownerNet += item.d_ldoAmount;
                    PdfReports.AddText2Table(table, item.d_ldoAmount.ToString("N2"), normalFont, "right");
                    PdfReports.AddText2Table(table, " ", normalFont);

                    // BLANK LINE
                    PdfReports.AddText2Table(table, " ", normalFont, 13);

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

                    //================================================================
                    // Add Deduction information
                    //================================================================

                    table = PdfReports.CreateTable(_primaryTableLayout, 1);

                    PdfReports.AddText2Table(table, " ", labelFont, 4);
                    PdfReports.AddText2Table(table, "Deduction", labelFont, "left", 4);
                    PdfReports.AddText2Table(table, "Payment", labelFont, "left", 2);
                    PdfReports.AddText2Table(table, "Amount", labelFont, "center");
                    PdfReports.AddText2Table(table, " ", labelFont, 2);

                    var contractDeductions = from deduction in deductionList
                                            where deduction.Contract_Number == item.i_ContractNumber
                                            && deduction.Payment_Number <= item.i_PaymentNumber
                                            orderby deduction.Payment_Number, deduction.Deduction_Number
                                            select deduction;

                    foreach(TransDeductionListItem dedItem in contractDeductions) {

                        if (dedItem.Amount != 0) {
                            PdfReports.AddText2Table(table, " ", labelFont, 4);
                            PdfReports.AddText2Table(table, dedItem.Deduction_Desc, normalFont, "left", 4);
                            PdfReports.AddText2Table(table, dedItem.Payment_Description, normalFont, "left", 2);
                            PdfReports.AddText2Table(table, dedItem.Amount.ToString("$#,##0.00"), normalFont, "right");
                            PdfReports.AddText2Table(table, " ", labelFont, 2);
                        }
                    }

                    PdfReports.AddText2Table(table, " ", normalFont, 13);
                }

                // BLANK LINE
                PdfReports.AddText2Table(table, " ", normalFont, 13);
                PdfReports.AddTableNoSplit(document, pgEvent, table);

                table = PdfReports.CreateTable(_primaryTableLayout, 1);

                // ======================================================
                // Close document
                // ======================================================
                if (document != null) {

                    table = PdfReports.CreateTable(_primaryTableLayout, 1);

                    AddTotals(ref writer, ref document, ref table, labelFont, normalFont, totalTons,
                        totalEHPrem, totalGross, totalDeductions, totalNet, checkAmount,
                        payeeNumber, totalGrowerNet, totalLandownerNet, pgEvent);

                    AddFooter(ref writer, ref document, normalFont, rptFooter, pgEvent);

                    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();
                }
            }
        }