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 (var ConnImprimir = Lfx.Workspace.Master.GetNewConnection("Imprimir comprobante") as Lfx.Data.Connection) using (System.Data.IDbTransaction Trans = ConnImprimir.BeginTransaction()) { var Comprob = new Lbl.Comprobantes.ComprobanteConArticulos(ConnImprimir, IdComprobante); var Controlador = new Lazaro.Base.Controller.ComprobanteController(Trans); ResultadoImpresion = Controlador.Imprimir(Comprob, null); if (ResultadoImpresion.Success) { Trans.Commit(); } else { Trans.Rollback(); } } /* using (Lfx.Data.Connection DatabaseImprimir = Lfx.Workspace.Master.GetNewConnection("Imprimir comprobante")) * using (System.Data.IDbTransaction Trans = DatabaseImprimir.BeginTransaction()) { * var Comprob = new Lbl.Comprobantes.ComprobanteConArticulos(DatabaseImprimir, IdComprobante); * var 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 (var DbImprimir = Lfx.Workspace.Master.GetNewConnection("Imprimir " + TipoElem.ToString() + " " + itemId.ToString()) as Lfx.Data.Connection) { Lbl.IElementoDeDatos Elem = Lbl.Instanciador.Instanciar(TipoElem, DbImprimir, itemId); Lfx.Types.OperationResult Res; using (System.Data.IDbTransaction Trans = DbImprimir.BeginTransaction()) { Lazaro.Base.Util.Impresion.ImpresorElemento Impresor = Lazaro.Base.Util.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); }
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.Talonario) { 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.Base.Util.Impresion.ImpresorElemento ImpresorVistaPrevia = Lazaro.Base.Util.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 if (this.Elemento is Lbl.Comprobantes.Comprobante) { 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()) { var Controlador = new Lazaro.Base.Controller.ComprobanteController(Trans); if (Lfx.Environment.SystemInformation.DesignMode) { Res = Controlador.Imprimir(this.Elemento as Lbl.Comprobantes.Comprobante, Impresora); } else { try { Res = Controlador.Imprimir(this.Elemento as Lbl.Comprobantes.Comprobante, Impresora); } catch (Exception ex) { Res = new Lfx.Types.FailureOperationResult(ex.Message); } } Progreso.End(); if (Res.Success == false) { if (Controlador.Transaction != null) { // Puede que la transacción ya haya sido finalizada por el impresor Controlador.Transaction.Rollback(); Controlador.Transaction = null; } Lui.Forms.MessageBox.Show(Res.Message, "Error"); } else { if (Controlador.Transaction != null) { // Puede que la transacción ya haya sido finalizada por el impresor Controlador.Transaction.Commit(); Controlador.Transaction = null; } this.Elemento.Cargar(); this.FromRow(this.Elemento); this.ControlUnico.AfterPrint(); } } } else { Lazaro.Base.Util.Impresion.ImpresorElemento Impresor = Lazaro.Base.Util.Impresion.Instanciador.InstanciarImpresor(this.Elemento, null); Res = Impresor.Imprimir(); } } if (Res.Success == false && Res.Message != null) { Lui.Forms.MessageBox.Show(Res.Message, "Imprimir"); } }