private void Imprimir()
        {
            Lfx.Types.OperationResult Res;
            if (this.ReadOnly)
            {
                Res = new Lfx.Types.SuccessOperationResult();
            }
            else
            {
                if (this.Elemento.Existe == false)
                {
                    // Si es nuevo, lo guardo sin preguntar.
                    Res = this.Save();
                }
                else if (this.Changed)
                {
                    // Si es edición, y hay cambios, pregunto si quiere guardar
                    using (Lui.Forms.YesNoDialog Pregunta = new Lui.Forms.YesNoDialog("Hay modificaciones sin guardar (subrayadas en color rojo). Antes de imprimir el ducumento se guardarán las modificaciones. ¿Desea continuar?", "Imprimir")) {
                        Pregunta.DialogButtons = Lui.Forms.DialogButtons.YesNo;
                        this.ShowChanged       = true;
                        if (Pregunta.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            Res = this.Save();
                        }
                        else
                        {
                            Res = new Lfx.Types.CancelOperationResult();
                        }
                        this.ShowChanged = false;
                    }
                }
                else
                {
                    // Es edición y no hay cambios... continúo
                    Res = new Lfx.Types.SuccessOperationResult();
                }
            }

            if (Res.Success)
            {
                Res = this.ControlUnico.BeforePrint();
            }

            if (Res.Success)
            {
                Lbl.Impresion.Impresora Impresora = null;
                if ((System.Windows.Forms.Control.ModifierKeys & Keys.Shift) == Keys.Shift)
                {
                    using (Lui.Printing.PrinterSelectionDialog FormularioSeleccionarImpresora = new Lui.Printing.PrinterSelectionDialog()) {
                        if (FormularioSeleccionarImpresora.ShowDialog() == DialogResult.OK)
                        {
                            Impresora = FormularioSeleccionarImpresora.SelectedPrinter;
                        }
                        else
                        {
                            return;
                        }
                    }
                }

                string NombreDocumento          = Elemento.ToString();
                Lbl.Impresion.CargasPapel Carga = Lbl.Impresion.CargasPapel.Automatica;
                if (Impresora != null && Impresora.CargaPapel == Lbl.Impresion.CargasPapel.Manual)
                {
                    Carga = Lbl.Impresion.CargasPapel.Manual;
                }
                else if (this.Elemento is Lbl.Comprobantes.ComprobanteConArticulos)
                {
                    Lbl.Comprobantes.ComprobanteConArticulos Comprob = this.Elemento as Lbl.Comprobantes.ComprobanteConArticulos;

                    if (Lbl.Comprobantes.PuntoDeVenta.TodosPorNumero[Comprob.PV].Tipo == Lbl.Comprobantes.TipoPv.Fiscal)
                    {
                        Carga = Lbl.Impresion.CargasPapel.Automatica;
                    }
                    else
                    {
                        // El tipo de comprobante puede forzar a una carga manual
                        Carga = Comprob.Tipo.CargaPapel;

                        // Intento averiguar el número de comprobante, en caso de que aun no esté numerado
                        if (Comprob.Numero == 0)
                        {
                            int ProximoNumero = Lbl.Comprobantes.Numerador.ProximoNumero(Comprob);
                            NombreDocumento = NombreDocumento.Replace("00000000", ProximoNumero.ToString("00000000"));
                        }
                    }
                }

                if (Carga == Lbl.Impresion.CargasPapel.Manual)
                {
                    using (Lui.Printing.ManualFeedDialog FormularioCargaManual = new Lui.Printing.ManualFeedDialog()) {
                        FormularioCargaManual.DocumentName = NombreDocumento;
                        // Muestro el nombre de la impresora
                        if (Impresora != null)
                        {
                            FormularioCargaManual.PrinterName = Impresora.Nombre;
                        }
                        else
                        {
                            System.Drawing.Printing.PrinterSettings ObjPrint = new System.Drawing.Printing.PrinterSettings();
                            FormularioCargaManual.PrinterName = ObjPrint.PrinterName;
                        }
                        if (FormularioCargaManual.ShowDialog() == DialogResult.Cancel)
                        {
                            return;
                        }
                    }
                }

                if (Impresora != null && Impresora.EsVistaPrevia)
                {
                    Lazaro.Impresion.ImpresorElemento ImpresorVistaPrevia = Lazaro.Impresion.Instanciador.InstanciarImpresor(this.Elemento, null);
                    ImpresorVistaPrevia.PrintController = new System.Drawing.Printing.PreviewPrintController();
                    Lui.Printing.PrintPreviewForm VistaPrevia = new Lui.Printing.PrintPreviewForm();
                    VistaPrevia.MdiParent             = this.ParentForm.MdiParent;
                    VistaPrevia.PrintPreview.Document = ImpresorVistaPrevia;
                    VistaPrevia.Show();
                }
                else
                {
                    Lfx.Types.OperationProgress Progreso = new Lfx.Types.OperationProgress("Imprimiendo", "El documento se está enviando a la impresora.");
                    if (Impresora != null)
                    {
                        Progreso.Description = "El documento se está enviando a la impresora " + Impresora.ToString();
                    }
                    Progreso.Modal = false;
                    Progreso.Begin();

                    using (IDbTransaction Trans = this.Elemento.Connection.BeginTransaction()) {
                        Lazaro.Impresion.ImpresorElemento Impresor = Lazaro.Impresion.Instanciador.InstanciarImpresor(this.Elemento, Trans);
                        Impresor.Impresora = Impresora;
                        try {
                            Res = Impresor.Imprimir();
                        } catch (Exception ex) {
                            Res = new Lfx.Types.FailureOperationResult(ex.Message);
                        }
                        Progreso.End();
                        if (Res.Success == false)
                        {
                            if (Impresor.Transaction != null)
                            {
                                // Puede que la transacción ya haya sido finalizada por el impresor
                                Impresor.Transaction.Rollback();
                            }
                            Lui.Forms.MessageBox.Show(Res.Message, "Error");
                        }
                        else
                        {
                            if (Impresor.Transaction != null)
                            {
                                // Puede que la transacción ya haya sido finalizada por el impresor
                                Impresor.Transaction.Commit();
                            }
                            this.Elemento.Cargar();
                            this.FromRow(this.Elemento);
                            this.ControlUnico.AfterPrint();
                        }
                    }
                }
            }

            if (Res.Success == false && Res.Message != null)
            {
                Lui.Forms.MessageBox.Show(Res.Message, "Imprimir");
            }
        }
Beispiel #2
0
        private static object ExecImprimir(string comando)
        {
            string SubComandoImprimir = Lfx.Types.Strings.GetNextToken(ref comando, " ").Trim();

            switch (SubComandoImprimir.ToUpperInvariant())
            {
            case "COMPROBANTE":
            case "COMPROB":
                int IdComprobante = Lfx.Types.Parsing.ParseInt(Lfx.Types.Strings.GetNextToken(ref comando, " "));

                Lfx.Types.OperationResult ResultadoImpresion;

                using (Lfx.Data.Connection DataBaseImprimir = Lfx.Workspace.Master.GetNewConnection("Imprimir comprobante"))
                    using (System.Data.IDbTransaction Trans = DataBaseImprimir.BeginTransaction()) {
                        Lbl.Comprobantes.ComprobanteConArticulos Comprob = new Lbl.Comprobantes.ComprobanteConArticulos(DataBaseImprimir, IdComprobante);
                        Lazaro.Impresion.Comprobantes.ImpresorComprobanteConArticulos Impresor = new Impresion.Comprobantes.ImpresorComprobanteConArticulos(Comprob, Trans);
                        ResultadoImpresion = Impresor.Imprimir();
                        if (ResultadoImpresion.Success)
                        {
                            Trans.Commit();
                        }
                        else
                        {
                            Trans.Rollback();
                        }
                    }

                return(ResultadoImpresion);

            default:
                int  itemId   = Lfx.Types.Parsing.ParseInt(Lfx.Types.Strings.GetNextToken(ref comando, " ").Trim());
                Type TipoElem = Lbl.Instanciador.InferirTipo(SubComandoImprimir);
                if (TipoElem != null && itemId > 0)
                {
                    using (Lfx.Data.Connection DbImprimir = Lfx.Workspace.Master.GetNewConnection("Imprimir " + TipoElem.ToString() + " " + itemId.ToString())) {
                        Lbl.IElementoDeDatos      Elem = Lbl.Instanciador.Instanciar(TipoElem, DbImprimir, itemId);
                        Lfx.Types.OperationResult Res;
                        using (System.Data.IDbTransaction Trans = DbImprimir.BeginTransaction()) {
                            Lazaro.Impresion.ImpresorElemento Impresor = Lazaro.Impresion.Instanciador.InstanciarImpresor(Elem, Trans);

                            string ImprimirEn = Lfx.Types.Strings.GetNextToken(ref comando, " ").Trim().ToUpperInvariant();
                            if (ImprimirEn == "EN")
                            {
                                // El nombre de la impresora es lo que resta del comando
                                // No lo puedo separar con GetNextToken porque puede contener espacios
                                string NombreImpresora = comando;
                                Impresor.Impresora = Lbl.Impresion.Impresora.InstanciarImpresoraLocal(DbImprimir, NombreImpresora);
                            }

                            Res = Impresor.Imprimir();
                            if (Res.Success)
                            {
                                Trans.Commit();
                            }
                            else
                            {
                                Trans.Rollback();
                            }
                        }
                        return(Res);
                    }
                }
                break;
            }

            return(null);
        }