164 lines
5.5 KiB
C#
164 lines
5.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
using System.Threading.Tasks;
|
|
using AdminFinanceRCA.Models;
|
|
using AdminFinanceRCA.Views;
|
|
using Avalonia.Controls;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
|
|
namespace AdminFinanceRCA.ViewModels;
|
|
|
|
public partial class MainWindowViewModel : ViewModelBase
|
|
{
|
|
private readonly FinanzasRepository _repository;
|
|
[ObservableProperty]
|
|
private ObservableCollection<MovimientoCompleto> _movimientos;
|
|
[ObservableProperty]
|
|
private ObservableCollection<Concepto> _conceptos;
|
|
[ObservableProperty]
|
|
private ObservableCollection<DepartTrabajo> _departamentos;
|
|
[ObservableProperty]
|
|
private ObservableCollection<TipoMovimiento> _tiposMovimiento;
|
|
|
|
public MainWindowViewModel()
|
|
{
|
|
_repository = new FinanzasRepository();
|
|
CargarDatos();
|
|
}
|
|
[RelayCommand]
|
|
public void AgregarDepartamento()
|
|
{
|
|
Mantenimiento_DepartTrabajoViewModel mantenimientoDepart = new Mantenimiento_DepartTrabajoViewModel();
|
|
Mantenimiento_DepartTrabajo departTrabajo = new Mantenimiento_DepartTrabajo()
|
|
{
|
|
DataContext = mantenimientoDepart
|
|
};
|
|
departTrabajo.ShowDialog(GetCurrentWindow<MainWindow>());
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void AgregarConcepto()
|
|
{
|
|
Mantenimiento_ConceptoViewModel mantenimiento = new Mantenimiento_ConceptoViewModel();
|
|
Mantenimiento_Concepto conceptoWindow = new Mantenimiento_Concepto()
|
|
{
|
|
DataContext = mantenimiento
|
|
};
|
|
conceptoWindow.ShowDialog(GetCurrentWindow<MainWindow>());
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void AgregarTipoMovimiento()
|
|
{
|
|
Mantenimiento_TipoMovViewModel mantenimiento = new Mantenimiento_TipoMovViewModel();
|
|
Mantenimiento_TipoMovimiento conceptoWindow = new Mantenimiento_TipoMovimiento()
|
|
{
|
|
DataContext = mantenimiento
|
|
};
|
|
conceptoWindow.ShowDialog(GetCurrentWindow<MainWindow>());
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void AgregarMovimiento()
|
|
{
|
|
MovimientoWindowViewModel mwvm = new MovimientoWindowViewModel();
|
|
mwvm.ReloadEvent+= MwvmOnReloadEvent;
|
|
MovimientoWindow mw = new MovimientoWindow()
|
|
{
|
|
Width = 600,
|
|
Height = 450,
|
|
};
|
|
mw.DataContext = mwvm;
|
|
mw.ShowDialog(GetCurrentWindow<MainWindow>());
|
|
}
|
|
|
|
|
|
private async void MwvmOnReloadEvent(object? sender, EventArgs e)
|
|
{
|
|
var movimientosTask = _repository.GetAllMovimientosCompletosAsync();
|
|
Movimientos = new ObservableCollection<MovimientoCompleto>(await movimientosTask);
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void Salir()
|
|
{
|
|
GetCurrentWindow<MainWindow>().Close();
|
|
}
|
|
private async Task<string> ShowSaveFileDialogAsync(Window window, string defaultFileName = "movimientos.csv")
|
|
{
|
|
var saveFileDialog = new SaveFileDialog
|
|
{
|
|
Title = "Guardar archivo CSV",
|
|
InitialFileName = defaultFileName,
|
|
DefaultExtension = "csv",
|
|
Filters = new List<FileDialogFilter>
|
|
{
|
|
new FileDialogFilter
|
|
{
|
|
Name = "Archivos CSV",
|
|
Extensions = new List<string> { "csv" }
|
|
},
|
|
new FileDialogFilter
|
|
{
|
|
Name = "Todos los archivos",
|
|
Extensions = new List<string> { "*" }
|
|
}
|
|
}
|
|
};
|
|
|
|
var result = await saveFileDialog.ShowAsync(window);
|
|
return result;
|
|
}
|
|
|
|
[RelayCommand]
|
|
public async void CrearCsv()
|
|
{
|
|
try
|
|
{
|
|
var window = GetCurrentWindow<MainWindow>(); //WindowService?.GetActiveWindow() ?? WindowService?.GetWindowForViewModel(this);
|
|
if (window != null)
|
|
{
|
|
string result = await ShowSaveFileDialogAsync(window);
|
|
if (!string.IsNullOrEmpty(result))
|
|
{
|
|
var filePath = await _repository.ExportToCsvAsync(result);
|
|
Console.WriteLine($"CSV creado en: {filePath}");
|
|
|
|
// Opcional: abrir el archivo con la aplicación predeterminada
|
|
Process.Start(new ProcessStartInfo(filePath) { UseShellExecute = true });
|
|
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error al crear CSV: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public async void CargarDatos()
|
|
{
|
|
try
|
|
{
|
|
var movimientosTask = _repository.GetAllMovimientosCompletosAsync();
|
|
var conceptosTask = _repository.GetAllConceptosAsync();
|
|
var departamentosTask = _repository.GetAllDepartamentosAsync();
|
|
var tiposTask = _repository.GetAllTiposMovimientoAsync();
|
|
|
|
await Task.WhenAll(movimientosTask, conceptosTask, departamentosTask, tiposTask);
|
|
|
|
// Actualizar collections
|
|
Movimientos = new ObservableCollection<MovimientoCompleto>(await movimientosTask);
|
|
Conceptos = new ObservableCollection<Concepto>(await conceptosTask);
|
|
Departamentos = new ObservableCollection<DepartTrabajo>(await departamentosTask);
|
|
TiposMovimiento = new ObservableCollection<TipoMovimiento>(await tiposTask);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error al cargar datos: {ex.Message}");
|
|
}
|
|
}
|
|
} |