private void UiGridVistaDetalle_ValidatingEditor(object sender, BaseContainerValidateEditorEventArgs e)
        {
            var view = sender as GridView;

            if (view == null || view.FocusedColumn.FieldName != "UNITARY_PRICE")
            {
                return;
            }
            var registro = (PolizaDetalle)UiGridVistaDetalle.GetRow(view.FocusedRowHandle);

            if (Convert.ToDecimal(e.Value.ToString()) < 0)
            {
                e.Valid     = false;
                e.ErrorText = "Cantidad inválida.";
            }
            else
            {
                registro.CUSTOMS_AMOUNT = registro.QTY * Convert.ToDecimal(e.Value.ToString());
            }
        }
 private void UiBotonGrabar_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
 {
     UiGridVistaEncabezado.Focus();
     UiGridVistaDetalle.Focus();
     if (!Polizas.ToList().Exists(p => p.IS_SELECTED))
     {
         return;
     }
     if (!ValidarDocumentosDeInicializacion())
     {
         return;
     }
     if (!UiGridVistaDetalle.ValidateEditor())
     {
         return;
     }
     if (EsConsolidado)
     {
         EstablecerPreciosUnitariosDeConsolidado();
     }
     UsuarioDeseaGrabarCosto?.Invoke(null, null);
 }
        private void GenerarExcelDetalle()
        {
            var poliza = Polizas.FirstOrDefault(x => x.IS_SELECTED);

            if (poliza == null)
            {
                InteraccionConUsuarioServicio.Mensaje("Debe seleccionar una poliza para generar la plantilla.");
                return;
            }
            SaveFileDialog dialogoGuardar = new SaveFileDialog();

            dialogoGuardar.Filter           = "Excel xlsx (*.xlsx)|*.xlsx";
            dialogoGuardar.FilterIndex      = 2;
            dialogoGuardar.RestoreDirectory = true;

            if (dialogoGuardar.ShowDialog() == DialogResult.OK)
            {
                path = dialogoGuardar.FileName;

                XLWorkbook   workbook  = new XLWorkbook();
                IXLWorksheet worksheet = workbook.Worksheets.Add("Detalle");
                worksheet.Protect("Mobility2016$$");

                worksheet.Cell(2, 2).Value = "Póliza";
                worksheet.Cell(2, 3).Value = poliza.CODIGO_POLIZA;

                worksheet.Cell(2, 2).Style.Font.Bold = true;

                worksheet.Cell(3, 2).Value           = "Fecha Generación";
                worksheet.Cell(3, 3).Value           = DateTime.Now;
                worksheet.Cell(3, 2).Style.Font.Bold = true;


                int inicioEncabezadoX = 5;
                int inicioEncabezadoY = 2;

                worksheet.Column(inicioEncabezadoY + 0).Width = 20;
                worksheet.Column(inicioEncabezadoY + 1).Width = 60;
                worksheet.Column(inicioEncabezadoY + 2).Width = 20;
                worksheet.Column(inicioEncabezadoY + 3).Width = 20;
                worksheet.Column(inicioEncabezadoY + 4).Width = 10;

                //Encabezado Tabla
                for (int j = 0; j <= UiGridVistaDetalle.Columns.Count - 1; j++)
                {
                    worksheet.Cell(inicioEncabezadoX, inicioEncabezadoY + j).Value                      = UiGridVistaDetalle.Columns[j].Caption;
                    worksheet.Cell(inicioEncabezadoX, inicioEncabezadoY + j).Style.Font.Bold            = true;
                    worksheet.Cell(inicioEncabezadoX, inicioEncabezadoY + j).Style.Fill.BackgroundColor = XLColor.BabyBlueEyes;
                    worksheet.Cell(inicioEncabezadoX, inicioEncabezadoY + j).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center);
                }

                //Datos
                for (int i = 0; i <= UiGridVistaDetalle.RowCount - 1; i++)
                {
                    for (int j = 0; j <= UiGridVistaDetalle.Columns.Count - 1; j++)
                    {
                        //Celda de total
                        if (j == 4)
                        {
                            worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY).FormulaR1C1 = "=RC[-2]*RC[-1]";
                            worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY).Style.NumberFormat.Format = " #,##0.00";
                            worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY).DataType = XLCellValues.Number;
                        }
                        else
                        {
                            worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY).Value =
                                (UiGridVistaDetalle.GetRowCellValue(i, UiGridVistaDetalle.Columns[j]).ToString());
                            worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY)
                            .Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Left);
                        }

                        if (j == 3) //Celda de costo unitario
                        {
                            worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY).DataType = XLCellValues.Number;
                            worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY).Style.NumberFormat.Format = " #,##0.00";
                            worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY)
                            .Style.Protection.SetLocked(false);
                            // worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY).DataValidation.Decimal.Between(0, double.MaxValue - 1);
                        }
                    }
                }

                //Convertir en tabla de excel
                IXLRange rngTable   = worksheet.Range(worksheet.Cell(inicioEncabezadoX, inicioEncabezadoY), worksheet.Cell(inicioEncabezadoX + UiGridVistaDetalle.RowCount, inicioEncabezadoY + UiGridVistaDetalle.Columns.Count - 1));
                IXLTable excelTable = rngTable.CreateTable();
                excelTable.ShowTotalsRow = true;

                excelTable.Field(4).TotalsRowFunction = XLTotalsRowFunction.Sum;
                excelTable.Field(3).TotalsRowFunction = XLTotalsRowFunction.Sum;
                excelTable.Field(2).TotalsRowFunction = XLTotalsRowFunction.Sum;
                excelTable.Field(1).TotalsRowFunction = XLTotalsRowFunction.Count;
                excelTable.Field(0).TotalsRowLabel    = "Total:";
                excelTable.ShowAutoFilter             = false;

                //Guardar Excel
                if ((File.Exists(path)))
                {
                    System.IO.File.Delete(path);
                }
                workbook.SaveAs(path);
                Action <bool> abrirExcel = AbrirExcel;

                InteraccionConUsuarioServicio.Confirmar("Archivo generado exitosamente, ¿Desea abrir el archivo?",
                                                        abrirExcel); //MessageBox.Show("Archivo generado exitosamente, ¿Desea abrir el archivo?", "Operación exitosa", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            }
        }