private static void SavePdfReport(PrintLogService pLogService, XtraReport stvReport)
 {
     // This STV Log is Highly experimental,
     // TODO: remove this try catch when the method is stable.
     try
     {
         DataTable dtbl      = (DataTable)stvReport.DataSource;
         var       refNumber = (from v in dtbl.AsEnumerable()
                                select Convert.ToInt32(v["STVNumber"])).Distinct().ToList();
         int i = 0;
         foreach (DevExpress.XtraPrinting.Page page in stvReport.Pages)
         {
             HCMIS.Desktop.Reports.STVonHeadedPaper xReport = new HCMIS.Desktop.Reports.STVonHeadedPaper();
             xReport.Pages.Add(page);
             MemoryStream stream = new MemoryStream();
             xReport.ExportToPdf(stream);
             pLogService.SaveLog(stream, "STV", true, refNumber[i], CurrentContext.UserId,
                                 BLL.DateTimeHelper.ServerDateTime);
             i++;
         }
     }
     catch
     {
         //
     }
 }
        public static STVonHeadedPaper CreateSTVonHeadedPaper(Order ord, DataTable dvPriced, string stvSentTo, int pickListID, int?stvLogID, bool convertDN, bool generateNewID,
                                                              bool hasInsurance, string transferType)
        {
            var iss       = new Issue();
            var stvReport = new STVonHeadedPaper(ord.ID, hasInsurance, CurrentContext.LoggedInUserName);

            PrepareTheReport(stvReport);
            stvReport.DataSource = Order.ReorganizeDataViewForSTVPrint_Program(dvPriced.AsDataView(), ord.ID, pickListID,
                                                                               CurrentContext.UserId, stvLogID, convertDN, generateNewID,
                                                                               hasInsurance);

            stvReport.From.Text = GeneralInfo.Current.HospitalName;
            //if (rus.RowCount != 0)
            //    stvReport.To.Text = rus.Name;
            stvReport.To.Text = stvSentTo;

            if (!string.IsNullOrEmpty(transferType)) //If this is store to store transfer
            {
                stvReport.xrTransferType.Visible = true;
                stvReport.xrTransferType.Text    = transferType;
            }


            DateTimePickerEx dtDate = new DateTimePickerEx();

            dtDate.Value = DateTimeHelper.ServerDateTime;

            if (stvLogID == null)
            {
                iss.LoadByPicklist(pickListID);
            }

            iss.LoadByPicklist(pickListID);
            stvReport.lblORiginalPrintedDate.Text = iss.PrintedDate.ToString();
            stvReport.Date.Text = dtDate.Text;
            return(stvReport);
        }
 private static void SavePdfReport(PrintLogService pLogService, XtraReport stvReport)
 {
     // This STV Log is Highly experimental,
     // TODO: remove this try catch when the method is stable.
     try
     {
         DataTable dtbl = (DataTable)stvReport.DataSource;
         var refNumber = (from v in dtbl.AsEnumerable()
                          select Convert.ToInt32(v["STVNumber"])).Distinct().ToList();
         int i = 0;
         foreach (DevExpress.XtraPrinting.Page page in stvReport.Pages)
         {
             HCMIS.Desktop.Reports.STVonHeadedPaper xReport = new HCMIS.Desktop.Reports.STVonHeadedPaper();
             xReport.Pages.Add(page);
             MemoryStream stream = new MemoryStream();
             xReport.ExportToPdf(stream);
             pLogService.SaveLog(stream, "STV", true, refNumber[i], CurrentContext.UserId,
                                 BLL.DateTimeHelper.ServerDateTime);
             i++;
         }
     }
     catch
     {
         //
     }
 }
        /// <summary>
        /// Re print STV.
        /// </summary>
        /// <param name="dv">The dv.</param>
        /// <param name="orderId">The order id.</param>
        /// <param name="pl">The pl.</param>
        /// <param name="stvLogID">The STV log ID.</param>
        /// <param name="isConversion">if set to <c>true</c> [is conversion].</param>
        /// <param name="generateNewPrintID">if set to <c>true</c> [generate new print ID].</param>
        //public XtraReport RePrintSTV(DataView dv,int orderId, PickList pl,int? stvLogID, bool isConversion, bool generateNewPrintID = true)
        //{
        //     var includeInsurance =XtraMessageBox.Show("Include insurance?", "Insurance",MessageBoxButtons.YesNo,MessageBoxIcon.Question)== DialogResult.Yes;
        //     var ord = new Order();
        //    ord.LoadByPrimaryKey(orderId);
        //    var order = new Order();
        //    order.LoadByPrimaryKey(orderId);
        //    if (!order.IsPaymentTypeValid())
        //    {
        //        //TODO: This needs to be handled well.
        //        throw new Exception(
        //            string.Format("PaymentTypeID has a problem.  The order has a payment type id of {0}",
        //                          ord.PaymentTypeID));
        //    }
        //    var stores = new Activity();
        //    stores.LoadByPrimaryKey(order.FromStore);
        //    var textDeliveryNote = "";
        //    var sendToString = GetReceivingUnitName(order);
        //    if (stvLogID != null)
        //    {
        //        var stvLog = new Issue();
        //        stvLog.LoadByPrimaryKey(stvLogID.Value);
        //        if (!stvLog.IsColumnNull("IsDeliveryNote") && stvLog.IsDeliveryNote && isConversion)
        //        {
        //            textDeliveryNote = stvLog.IDPrinted.ToString("00000");
        //        }
        //    }
        //    if (PaymentType.Constants.CASH == ord.PaymentTypeID)
        //    {
        //        var rus = new Institution();
        //        rus.LoadByPrimaryKey(order.RequestedBy);
        //        var stvReport = new HCMIS.Desktop.Reports.CashInvoice(CurrentContext.LoggedInUserName);
        //        pl.LoadByOrderID(orderId);
        //        PrepareTheReport(stvReport);
        //        stvReport.DeliveryNoteNo.Text = textDeliveryNote;
        //        stvReport.DataSource = Order.ReorganizeDataViewForSTVPrint_Program(dv, ord.ID, pl.ID, CurrentContext.UserId, stvLogID, isConversion, generateNewPrintID, includeInsurance);
        //        stvReport.From.Text = GeneralInfo.Current.HospitalName;
        //        stvReport.To.Text = sendToString;
        //        stvReport.TIN.Text = rus.TinNo;
        //        stvReport.VAT.Text = rus.VATNo;
        //        stvReport.Town.Text = rus.Town;
        //        stvReport.Woreda.Text = rus.WoredaText;
        //        stvReport.Zone.Text = rus.ZoneText;
        //        stvReport.Region.Text = rus.Region;
        //        stvReport.License.Text = (!rus.IsColumnNull("Ownership") && rus.IsPrivatelyOwned) ? rus.LicenseNo : (!ord.IsColumnNull("LetterNo")) ? ord.LetterNo : "";
        //        //stvReport.Program.Text = stores.StoreName;
        //        DateTimePickerEx dtDate = new DateTimePickerEx();
        //        dtDate.Value = DateTimeHelper.ServerDateTime;
        //        stvReport.Date.Text = dtDate.Text;
        //        return stvReport;
        //    }
        //    else if (PaymentType.Constants.CREDIT == ord.PaymentTypeID)
        //    {
        //        var rus = new Institution();
        //        rus.LoadByPrimaryKey(order.RequestedBy);
        //        HCMIS.Desktop.Reports.CreditInvoice stvReport =
        //            new HCMIS.Desktop.Reports.CreditInvoice(CurrentContext.LoggedInUserName);
        //        pl.LoadByOrderID(orderId);
        //        PrepareTheReport(stvReport);
        //        stvReport.DeliveryNoteNo.Text = textDeliveryNote;
        //        stvReport.DataSource = Order.ReorganizeDataViewForSTVPrint_Program(dv, ord.ID, pl.ID,
        //            CurrentContext.UserId, stvLogID, isConversion, generateNewPrintID, includeInsurance);
        //        stvReport.From.Text = GeneralInfo.Current.HospitalName;
        //        stvReport.To.Text = sendToString;
        //        //stvReport.Region.Text = rus.Woreda;
        //        stvReport.Zone.Text = rus.Town;
        //        stvReport.Letter.Text = ord.LetterNo;
        //        //stvReport.Program.Text = stores.StoreName;
        //        DateTimePickerEx dtDate = new DateTimePickerEx();
        //        dtDate.Value = DateTimeHelper.ServerDateTime;
        //        stvReport.Date.Text = dtDate.Text;
        //    }
        //}
        /// <summary>
        /// Re print STV.
        /// </summary>
        /// <param name="dv">The dv.</param>
        /// <param name="orderId">The order id.</param>
        /// <param name="pl">The pl.</param>
        /// <param name="stvLogID">The STV log ID.</param>
        /// <param name="isConversion">if set to <c>true</c> [is conversion].</param>
        /// <param name="generateNewPrintID">if set to <c>true</c> [generate new print ID].</param>
        public XtraReport RePrintSTV(DataView dv, int orderId, PickList pl, int? stvLogID, bool isConversion, bool generateNewPrintID = true)
        {
            HCMIS.Core.Distribution.Services.PrintLogService pLogService = new HCMIS.Core.Distribution.Services.PrintLogService();

            pLogService.StartPrintingSession();

            includeInsurance = XtraMessageBox.Show("Include insurance?", "Insurance", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
            var ord = new Order();
            ord.LoadByPrimaryKey(orderId);
            //loading two times
            var order = new Order();
            order.LoadByPrimaryKey(orderId);

            if (!order.IsPaymentTypeValid())
            {

                //TODO: This needs to be handled well.
                throw new Exception(
                    string.Format("PaymentTypeID has a problem.  The order has a payment type id of {0}", ord.PaymentTypeID));
            }

            var rus = new Institution();
            if (ord.PaymentTypeID == PaymentType.Constants.CASH || ord.PaymentTypeID == PaymentType.Constants.CREDIT)
            {
                rus.LoadByPrimaryKey(order.RequestedBy);
            }

            var stores = new Activity();

            stores.LoadByPrimaryKey(order.FromStore);
            var textDeliveryNote = "";
            var sendToString = GetReceivingUnitName(order);
            if (stvLogID != null)
            {
                var stvLog = new Issue();
                stvLog.LoadByPrimaryKey(stvLogID.Value);
                if (!stvLog.IsColumnNull("IsDeliveryNote") && stvLog.IsDeliveryNote && isConversion)
                {
                    textDeliveryNote = stvLog.IDPrinted.ToString("00000");
                }
            }
            var xtraReport = new XtraReport();
            string stvPrinterName = "";
            if (PaymentType.Constants.CASH == ord.PaymentTypeID && !chkDeliveryNotes.Checked)
            {
                xtraReport = FormatCashRePrintInvoice(ord, dv.Table, rus, pl, false, stvPrinterName, pLogService, _stvLogIdChosen);
                pLogService.CommitPrintLog();
                return xtraReport;
            }

            else if (PaymentType.Constants.CASH == ord.PaymentTypeID && chkDeliveryNotes.Checked)
            {
                xtraReport = FormatCashRePrintInvoice(ord, pl.DefaultView.ToTable(), rus, pl, true, stvPrinterName, pLogService, _stvLogIdChosen);
                pLogService.CommitPrintLog();
                return xtraReport;
            }

            else if (PaymentType.Constants.CREDIT == ord.PaymentTypeID && !chkDeliveryNotes.Checked)
            {
                xtraReport = FormatCreditRePrintInvoice(ord, pl.DefaultView.ToTable(), rus, pl, false, stvPrinterName, pLogService, _stvLogIdChosen);
                pLogService.CommitPrintLog();
                return xtraReport;
            }

            else if (PaymentType.Constants.CREDIT == ord.PaymentTypeID && chkDeliveryNotes.Checked)
            {
                xtraReport = FormatCreditRePrintInvoice(ord, pl.DefaultView.ToTable(), rus, pl, true, stvPrinterName, pLogService, _stvLogIdChosen);
                pLogService.CommitPrintLog();
                return xtraReport;
            }
            //if (PaymentType.Constants.CASH == ord.PaymentTypeID)
            //{
            //    var stvReport = new HCMIS.Desktop.Reports.CashInvoice(CurrentContext.LoggedInUserName);
            //    pl.LoadByOrderID(orderId);
            //    PrepareTheReport(stvReport);
            //    stvReport.DeliveryNoteNo.Text = textDeliveryNote;
            //    stvReport.DataSource = Order.ReorganizeDataViewForSTVPrint_Program(dv, ord.ID, pl.ID, CurrentContext.UserId, stvLogID, isConversion, generateNewPrintID, includeInsurance);
            //    stvReport.From.Text = GeneralInfo.Current.HospitalName;
            //    stvReport.To.Text = sendToString;
            //    stvReport.TIN.Text = rus.TinNo;
            //    stvReport.VAT.Text = rus.VATNo;
            //    stvReport.Town.Text = rus.Town;
            //    stvReport.Woreda.Text = rus.WoredaText;
            //    stvReport.Zone.Text = rus.ZoneText;
            //    stvReport.Region.Text = rus.Region;

            //    stvReport.License.Text = (!rus.IsColumnNull("Ownership") && rus.IsPrivatelyOwned) ? rus.LicenseNo : (!ord.IsColumnNull("LetterNo")) ? ord.LetterNo : "";
            //    //stvReport.Program.Text = stores.StoreName;
            //    DateTimePickerEx dtDate = new DateTimePickerEx();
            //    dtDate.Value = DateTimeHelper.ServerDateTime;
            //    stvReport.Date.Text = dtDate.Text;

            //    return stvReport;
            //}
            //else if (PaymentType.Constants.CREDIT == ord.PaymentTypeID)
            //{
            //    HCMIS.Desktop.Reports.CreditInvoice stvReport = new HCMIS.Desktop.Reports.CreditInvoice(CurrentContext.LoggedInUserName);
            //    pl.LoadByOrderID(orderId);
            //    PrepareTheReport(stvReport);
            //    stvReport.DeliveryNoteNo.Text = textDeliveryNote;
            //    stvReport.DataSource = Order.ReorganizeDataViewForSTVPrint_Program(dv, ord.ID, pl.ID, CurrentContext.UserId, stvLogID, isConversion, generateNewPrintID, includeInsurance);
            //    stvReport.From.Text = GeneralInfo.Current.HospitalName;
            //    stvReport.To.Text = sendToString;

            //    //stvReport.Region.Text = rus.Woreda;
            //    stvReport.Zone.Text = rus.Town;
            //    stvReport.Letter.Text = ord.LetterNo;
            //    //stvReport.Program.Text = stores.StoreName;
            //    DateTimePickerEx dtDate = new DateTimePickerEx();
            //    dtDate.Value = DateTimeHelper.ServerDateTime;
            //    stvReport.Date.Text = dtDate.Text;

            //    return stvReport;
            //}
            else if (PaymentType.Constants.STV == ord.PaymentTypeID && generateNewPrintID)
            {
                pl.LoadByOrderID(orderId);

                // check if this is a delivery note
                Issue sl = new Issue();
                sl.LoadByPrimaryKey(stvLogID.Value);
                if (!sl.IsColumnNull("IsDeliveryNote") && sl.IsDeliveryNote && !isConversion)
                {
                    var stvReport = new HCMIS.Desktop.Reports.DeliveryNoteForIssue(ord.ID, includeInsurance, CurrentContext.LoggedInUserName);

                    PrepareTheReport(stvReport);
                    stvReport.DataSource = Order.ReorganizeDataViewForSTVPrint_Program(dv, ord.ID, pl.ID, CurrentContext.UserId, stvLogID, isConversion, generateNewPrintID, includeInsurance);
                    stvReport.From.Text = GeneralInfo.Current.HospitalName;
                    stvReport.To.Text = sendToString;
                    stvReport.STVNO.Text = Issue.GetLogFor(ord.RefNo);
                    //stvReport.Date.Text = @"(Reprint) " + Convert.ToDateTime(dv[0]["IssueDate"]).ToString("MM/dd/yyyy");
                    DateTimePickerEx dtDate = new DateTimePickerEx();
                    dtDate.Value = DateTimeHelper.ServerDateTime;
                    stvReport.Date.Text = dtDate.Text;
                    //stvReport.ShowPreview();

                    return stvReport;
                }
                else
                {
                    var stvReport = new HCMIS.Desktop.Reports.STVonHeadedPaper(ord.ID, includeInsurance, CurrentContext.LoggedInUserName);
                    stvReport.DeliveryNoteNo.Text = textDeliveryNote;

                    PrepareTheReport(stvReport);
                    stvReport.DataSource = Order.ReorganizeDataViewForSTVPrint_Program(dv, ord.ID, pl.ID, CurrentContext.UserId, stvLogID, isConversion, generateNewPrintID, includeInsurance);
                    stvReport.From.Text = GeneralInfo.Current.HospitalName;
                    stvReport.To.Text = sendToString;
                    stvReport.STVNO.Text = Issue.GetLogFor(ord.RefNo);
                    //stvReport.Date.Text = @"(Reprint) " + Convert.ToDateTime(dv[0]["IssueDate"]).ToString("MM/dd/yyyy");
                    DateTimePickerEx dtDate = new DateTimePickerEx();
                    dtDate.Value = DateTimeHelper.ServerDateTime;
                    stvReport.Date.Text = dtDate.Text;
                    //stvReport.ShowPreview();

                    return stvReport;
                }
            }
            else if (PaymentType.Constants.STV == ord.PaymentTypeID && !generateNewPrintID)
            {
                pl.LoadByOrderID(orderId);
                //System.Windows.Forms.DialogResult result = XtraMessageBox.Show("Include insurance?", "Insurance",
                //MessageBoxButtons.YesNo,
                //MessageBoxIcon.Question);
                Issue log = new Issue();
                log.LoadByPrimaryKey((_reprintId != null) ? _reprintId.Value : stvLogID.Value);

                if (log.IsColumnNull("HasInsurance") || BLL.Settings.IsCenter) //If it is null, we have no idea about what the insurance status was.  So let's ask the user.
                {
                    System.Windows.Forms.DialogResult result = XtraMessageBox.Show("Include insurance?", "Insurance",
                                                                                   MessageBoxButtons.YesNo,
                                                                                   MessageBoxIcon.Question);
                    includeInsurance = result == DialogResult.Yes ? true : false;
                    log.HasInsurance = includeInsurance;
                    log.Save();
                }
                else
                {
                    includeInsurance = log.HasInsurance;
                }

                //bool includeInsurance = (!log.IsColumnNull("HasInsurance"))? log.HasInsurance : false;
                var stvReport = new HCMIS.Desktop.Reports.STVonA4(ord.ID, includeInsurance,
                                                                  CurrentContext.LoggedInUserName);
                stvReport.DeliveryNoteNo.Text = textDeliveryNote;

                PrepareTheReport(stvReport);
                stvReport.DataSource = Order.ReorganizeDataViewForSTVPrint_Program(dv, ord.ID, pl.ID, CurrentContext.UserId, (_reprintId != null) ? _reprintId : stvLogID, isConversion, generateNewPrintID, includeInsurance);
                stvReport.From.Text = GeneralInfo.Current.HospitalName;
                stvReport.To.Text = sendToString;
                //stvReport.STVNO.Text =  //STVLog.GetLogFor(ord.RefNo);
                //stvReport.Date.Text = @"(Reprint) " + Convert.ToDateTime(dv[0]["IssueDate"]).ToString("MM/dd/yyyy");
                DateTimePickerEx dtDate = new DateTimePickerEx();
                dtDate.Value = DateTimeHelper.ServerDateTime;
                stvReport.Date.Text = dtDate.Text;
                return stvReport;
            }
            return null;
        }
        /// <summary>
        /// Re print STV.
        /// </summary>
        /// <param name="dv">The dv.</param>
        /// <param name="orderId">The order id.</param>
        /// <param name="pl">The pl.</param>
        /// <param name="stvLogID">The STV log ID.</param>
        /// <param name="isConversion">if set to <c>true</c> [is conversion].</param>
        /// <param name="generateNewPrintID">if set to <c>true</c> [generate new print ID].</param>
        //public XtraReport RePrintSTV(DataView dv,int orderId, PickList pl,int? stvLogID, bool isConversion, bool generateNewPrintID = true)
        //{
        //     var includeInsurance =XtraMessageBox.Show("Include insurance?", "Insurance",MessageBoxButtons.YesNo,MessageBoxIcon.Question)== DialogResult.Yes;
        //     var ord = new Order();
        //    ord.LoadByPrimaryKey(orderId);
        //    var order = new Order();
        //    order.LoadByPrimaryKey(orderId);

        //    if (!order.IsPaymentTypeValid())
        //    {

        //        //TODO: This needs to be handled well.
        //        throw new Exception(
        //            string.Format("PaymentTypeID has a problem.  The order has a payment type id of {0}",
        //                          ord.PaymentTypeID));

        //    }

        //    var stores = new Activity();

        //    stores.LoadByPrimaryKey(order.FromStore);
        //    var textDeliveryNote = "";
        //    var sendToString = GetReceivingUnitName(order);
        //    if (stvLogID != null)
        //    {
        //        var stvLog = new Issue();
        //        stvLog.LoadByPrimaryKey(stvLogID.Value);
        //        if (!stvLog.IsColumnNull("IsDeliveryNote") && stvLog.IsDeliveryNote && isConversion)
        //        {
        //            textDeliveryNote = stvLog.IDPrinted.ToString("00000");
        //        }
        //    }

        //    if (PaymentType.Constants.CASH == ord.PaymentTypeID)
        //    {
        //        var rus = new Institution();
        //        rus.LoadByPrimaryKey(order.RequestedBy);
        //        var stvReport = new HCMIS.Desktop.Reports.CashInvoice(CurrentContext.LoggedInUserName);
        //        pl.LoadByOrderID(orderId);
        //        PrepareTheReport(stvReport);
        //        stvReport.DeliveryNoteNo.Text = textDeliveryNote;
        //        stvReport.DataSource = Order.ReorganizeDataViewForSTVPrint_Program(dv, ord.ID, pl.ID, CurrentContext.UserId, stvLogID, isConversion, generateNewPrintID, includeInsurance);
        //        stvReport.From.Text = GeneralInfo.Current.HospitalName;
        //        stvReport.To.Text = sendToString;
        //        stvReport.TIN.Text = rus.TinNo;
        //        stvReport.VAT.Text = rus.VATNo;
        //        stvReport.Town.Text = rus.Town;
        //        stvReport.Woreda.Text = rus.WoredaText;
        //        stvReport.Zone.Text = rus.ZoneText;
        //        stvReport.Region.Text = rus.Region;
        //        stvReport.License.Text = (!rus.IsColumnNull("Ownership") && rus.IsPrivatelyOwned) ? rus.LicenseNo : (!ord.IsColumnNull("LetterNo")) ? ord.LetterNo : "";
        //        //stvReport.Program.Text = stores.StoreName;
        //        DateTimePickerEx dtDate = new DateTimePickerEx();
        //        dtDate.Value = DateTimeHelper.ServerDateTime;
        //        stvReport.Date.Text = dtDate.Text;

        //        return stvReport;
        //    }
        //    else if (PaymentType.Constants.CREDIT == ord.PaymentTypeID)
        //    {
        //        var rus = new Institution();
        //        rus.LoadByPrimaryKey(order.RequestedBy);
        //        HCMIS.Desktop.Reports.CreditInvoice stvReport =
        //            new HCMIS.Desktop.Reports.CreditInvoice(CurrentContext.LoggedInUserName);
        //        pl.LoadByOrderID(orderId);
        //        PrepareTheReport(stvReport);
        //        stvReport.DeliveryNoteNo.Text = textDeliveryNote;
        //        stvReport.DataSource = Order.ReorganizeDataViewForSTVPrint_Program(dv, ord.ID, pl.ID,
        //            CurrentContext.UserId, stvLogID, isConversion, generateNewPrintID, includeInsurance);
        //        stvReport.From.Text = GeneralInfo.Current.HospitalName;
        //        stvReport.To.Text = sendToString;

        //        //stvReport.Region.Text = rus.Woreda;
        //        stvReport.Zone.Text = rus.Town;
        //        stvReport.Letter.Text = ord.LetterNo;
        //        //stvReport.Program.Text = stores.StoreName;
        //        DateTimePickerEx dtDate = new DateTimePickerEx();
        //        dtDate.Value = DateTimeHelper.ServerDateTime;
        //        stvReport.Date.Text = dtDate.Text;
        //    }
        //}

        /// <summary>
        /// Re print STV.
        /// </summary>
        /// <param name="dv">The dv.</param>
        /// <param name="orderId">The order id.</param>
        /// <param name="pl">The pl.</param>
        /// <param name="stvLogID">The STV log ID.</param>
        /// <param name="isConversion">if set to <c>true</c> [is conversion].</param>
        /// <param name="generateNewPrintID">if set to <c>true</c> [generate new print ID].</param>
        public XtraReport RePrintSTV(DataView dv, int orderId, PickList pl, int?stvLogID, bool isConversion, bool generateNewPrintID = true)
        {
            HCMIS.Core.Distribution.Services.PrintLogService pLogService = new HCMIS.Core.Distribution.Services.PrintLogService();

            pLogService.StartPrintingSession();

            includeInsurance = XtraMessageBox.Show("Include insurance?", "Insurance", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
            var ord = new Order();

            ord.LoadByPrimaryKey(orderId);
            //loading two times
            var order = new Order();

            order.LoadByPrimaryKey(orderId);

            if (!order.IsPaymentTypeValid())
            {
                //TODO: This needs to be handled well.
                throw new Exception(
                          string.Format("PaymentTypeID has a problem.  The order has a payment type id of {0}", ord.PaymentTypeID));
            }

            var rus = new Institution();

            if (ord.PaymentTypeID == PaymentType.Constants.CASH || ord.PaymentTypeID == PaymentType.Constants.CREDIT)
            {
                rus.LoadByPrimaryKey(order.RequestedBy);
            }

            var stores = new Activity();

            stores.LoadByPrimaryKey(order.FromStore);
            var textDeliveryNote = "";
            var sendToString     = GetReceivingUnitName(order);

            if (stvLogID != null)
            {
                var stvLog = new Issue();
                stvLog.LoadByPrimaryKey(stvLogID.Value);
                if (!stvLog.IsColumnNull("IsDeliveryNote") && stvLog.IsDeliveryNote && isConversion)
                {
                    textDeliveryNote = stvLog.IDPrinted.ToString("00000");
                }
            }
            var    xtraReport     = new XtraReport();
            string stvPrinterName = "";

            if (PaymentType.Constants.CASH == ord.PaymentTypeID && !chkDeliveryNotes.Checked)
            {
                xtraReport = FormatCashRePrintInvoice(ord, dv.Table, rus, pl, false, stvPrinterName, pLogService, _stvLogIdChosen);
                pLogService.CommitPrintLog();
                return(xtraReport);
            }

            else if (PaymentType.Constants.CASH == ord.PaymentTypeID && chkDeliveryNotes.Checked)
            {
                xtraReport = FormatCashRePrintInvoice(ord, pl.DefaultView.ToTable(), rus, pl, true, stvPrinterName, pLogService, _stvLogIdChosen);
                pLogService.CommitPrintLog();
                return(xtraReport);
            }

            else if (PaymentType.Constants.CREDIT == ord.PaymentTypeID && !chkDeliveryNotes.Checked)
            {
                xtraReport = FormatCreditRePrintInvoice(ord, pl.DefaultView.ToTable(), rus, pl, false, stvPrinterName, pLogService, _stvLogIdChosen);
                pLogService.CommitPrintLog();
                return(xtraReport);
            }

            else if (PaymentType.Constants.CREDIT == ord.PaymentTypeID && chkDeliveryNotes.Checked)
            {
                xtraReport = FormatCreditRePrintInvoice(ord, pl.DefaultView.ToTable(), rus, pl, true, stvPrinterName, pLogService, _stvLogIdChosen);
                pLogService.CommitPrintLog();
                return(xtraReport);
            }
            //if (PaymentType.Constants.CASH == ord.PaymentTypeID)
            //{
            //    var stvReport = new HCMIS.Desktop.Reports.CashInvoice(CurrentContext.LoggedInUserName);
            //    pl.LoadByOrderID(orderId);
            //    PrepareTheReport(stvReport);
            //    stvReport.DeliveryNoteNo.Text = textDeliveryNote;
            //    stvReport.DataSource = Order.ReorganizeDataViewForSTVPrint_Program(dv, ord.ID, pl.ID, CurrentContext.UserId, stvLogID, isConversion, generateNewPrintID, includeInsurance);
            //    stvReport.From.Text = GeneralInfo.Current.HospitalName;
            //    stvReport.To.Text = sendToString;
            //    stvReport.TIN.Text = rus.TinNo;
            //    stvReport.VAT.Text = rus.VATNo;
            //    stvReport.Town.Text = rus.Town;
            //    stvReport.Woreda.Text = rus.WoredaText;
            //    stvReport.Zone.Text = rus.ZoneText;
            //    stvReport.Region.Text = rus.Region;

            //    stvReport.License.Text = (!rus.IsColumnNull("Ownership") && rus.IsPrivatelyOwned) ? rus.LicenseNo : (!ord.IsColumnNull("LetterNo")) ? ord.LetterNo : "";
            //    //stvReport.Program.Text = stores.StoreName;
            //    DateTimePickerEx dtDate = new DateTimePickerEx();
            //    dtDate.Value = DateTimeHelper.ServerDateTime;
            //    stvReport.Date.Text = dtDate.Text;

            //    return stvReport;
            //}
            //else if (PaymentType.Constants.CREDIT == ord.PaymentTypeID)
            //{
            //    HCMIS.Desktop.Reports.CreditInvoice stvReport = new HCMIS.Desktop.Reports.CreditInvoice(CurrentContext.LoggedInUserName);
            //    pl.LoadByOrderID(orderId);
            //    PrepareTheReport(stvReport);
            //    stvReport.DeliveryNoteNo.Text = textDeliveryNote;
            //    stvReport.DataSource = Order.ReorganizeDataViewForSTVPrint_Program(dv, ord.ID, pl.ID, CurrentContext.UserId, stvLogID, isConversion, generateNewPrintID, includeInsurance);
            //    stvReport.From.Text = GeneralInfo.Current.HospitalName;
            //    stvReport.To.Text = sendToString;

            //    //stvReport.Region.Text = rus.Woreda;
            //    stvReport.Zone.Text = rus.Town;
            //    stvReport.Letter.Text = ord.LetterNo;
            //    //stvReport.Program.Text = stores.StoreName;
            //    DateTimePickerEx dtDate = new DateTimePickerEx();
            //    dtDate.Value = DateTimeHelper.ServerDateTime;
            //    stvReport.Date.Text = dtDate.Text;


            //    return stvReport;
            //}
            else if (PaymentType.Constants.STV == ord.PaymentTypeID && generateNewPrintID)
            {
                pl.LoadByOrderID(orderId);

                // check if this is a delivery note
                Issue sl = new Issue();
                sl.LoadByPrimaryKey(stvLogID.Value);
                if (!sl.IsColumnNull("IsDeliveryNote") && sl.IsDeliveryNote && !isConversion)
                {
                    var stvReport = new HCMIS.Desktop.Reports.DeliveryNoteForIssue(ord.ID, includeInsurance, CurrentContext.LoggedInUserName);


                    PrepareTheReport(stvReport);
                    stvReport.DataSource = Order.ReorganizeDataViewForSTVPrint_Program(dv, ord.ID, pl.ID, CurrentContext.UserId, stvLogID, isConversion, generateNewPrintID, includeInsurance);
                    stvReport.From.Text  = GeneralInfo.Current.HospitalName;
                    stvReport.To.Text    = sendToString;
                    stvReport.STVNO.Text = Issue.GetLogFor(ord.RefNo);
                    //stvReport.Date.Text = @"(Reprint) " + Convert.ToDateTime(dv[0]["IssueDate"]).ToString("MM/dd/yyyy");
                    DateTimePickerEx dtDate = new DateTimePickerEx();
                    dtDate.Value        = DateTimeHelper.ServerDateTime;
                    stvReport.Date.Text = dtDate.Text;
                    //stvReport.ShowPreview();

                    return(stvReport);
                }
                else
                {
                    var stvReport = new HCMIS.Desktop.Reports.STVonHeadedPaper(ord.ID, includeInsurance, CurrentContext.LoggedInUserName);
                    stvReport.DeliveryNoteNo.Text = textDeliveryNote;

                    PrepareTheReport(stvReport);
                    stvReport.DataSource = Order.ReorganizeDataViewForSTVPrint_Program(dv, ord.ID, pl.ID, CurrentContext.UserId, stvLogID, isConversion, generateNewPrintID, includeInsurance);
                    stvReport.From.Text  = GeneralInfo.Current.HospitalName;
                    stvReport.To.Text    = sendToString;
                    stvReport.STVNO.Text = Issue.GetLogFor(ord.RefNo);
                    //stvReport.Date.Text = @"(Reprint) " + Convert.ToDateTime(dv[0]["IssueDate"]).ToString("MM/dd/yyyy");
                    DateTimePickerEx dtDate = new DateTimePickerEx();
                    dtDate.Value        = DateTimeHelper.ServerDateTime;
                    stvReport.Date.Text = dtDate.Text;
                    //stvReport.ShowPreview();

                    return(stvReport);
                }
            }
            else if (PaymentType.Constants.STV == ord.PaymentTypeID && !generateNewPrintID)
            {
                pl.LoadByOrderID(orderId);
                //System.Windows.Forms.DialogResult result = XtraMessageBox.Show("Include insurance?", "Insurance",
                //MessageBoxButtons.YesNo,
                //MessageBoxIcon.Question);
                Issue log = new Issue();
                log.LoadByPrimaryKey((_reprintId != null) ? _reprintId.Value : stvLogID.Value);


                if (log.IsColumnNull("HasInsurance") || BLL.Settings.IsCenter) //If it is null, we have no idea about what the insurance status was.  So let's ask the user.
                {
                    System.Windows.Forms.DialogResult result = XtraMessageBox.Show("Include insurance?", "Insurance",
                                                                                   MessageBoxButtons.YesNo,
                                                                                   MessageBoxIcon.Question);
                    includeInsurance = result == DialogResult.Yes ? true : false;
                    log.HasInsurance = includeInsurance;
                    log.Save();
                }
                else
                {
                    includeInsurance = log.HasInsurance;
                }

                //bool includeInsurance = (!log.IsColumnNull("HasInsurance"))? log.HasInsurance : false;
                var stvReport = new HCMIS.Desktop.Reports.STVonA4(ord.ID, includeInsurance,
                                                                  CurrentContext.LoggedInUserName);
                stvReport.DeliveryNoteNo.Text = textDeliveryNote;

                PrepareTheReport(stvReport);
                stvReport.DataSource = Order.ReorganizeDataViewForSTVPrint_Program(dv, ord.ID, pl.ID, CurrentContext.UserId, (_reprintId != null) ? _reprintId : stvLogID, isConversion, generateNewPrintID, includeInsurance);
                stvReport.From.Text  = GeneralInfo.Current.HospitalName;
                stvReport.To.Text    = sendToString;
                //stvReport.STVNO.Text =  //STVLog.GetLogFor(ord.RefNo);
                //stvReport.Date.Text = @"(Reprint) " + Convert.ToDateTime(dv[0]["IssueDate"]).ToString("MM/dd/yyyy");
                DateTimePickerEx dtDate = new DateTimePickerEx();
                dtDate.Value        = DateTimeHelper.ServerDateTime;
                stvReport.Date.Text = dtDate.Text;
                return(stvReport);
            }
            return(null);
        }