Files
sistema-caja/src/danielcortes/xyz/controllers/ManagerController.java
2019-01-08 17:45:22 -03:00

214 lines
8.1 KiB
Java

/*
* MIT License
*
* Copyright (c) 2018-2019 Daniel Cortes
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package danielcortes.xyz.controllers;
import danielcortes.xyz.models.caja.Caja;
import danielcortes.xyz.models.caja.CajaDAO;
import danielcortes.xyz.models.documentos.Documentos;
import danielcortes.xyz.models.documentos.DocumentosDAO;
import danielcortes.xyz.models.documentos.MysqlDocumentosDAO;
import danielcortes.xyz.models.efectivo.Efectivo;
import danielcortes.xyz.models.efectivo.EfectivoDAO;
import danielcortes.xyz.models.efectivo.MysqlEfectivoDAO;
import danielcortes.xyz.models.egreso.EgresoDAO;
import danielcortes.xyz.models.ingreso.IngresoDAO;
import danielcortes.xyz.models.ingreso.MysqlIngresoDAO;
import danielcortes.xyz.models.tipo_egreso.TipoEgresoDAO;
import danielcortes.xyz.models.egreso.MysqlEgresoDAO;
import danielcortes.xyz.models.tipo_egreso.MysqlTipoEgresoDAO;
import danielcortes.xyz.models.tipo_ingreso.MysqlTipoIngresoDAO;
import danielcortes.xyz.models.tipo_ingreso.TipoIngreso;
import danielcortes.xyz.models.tipo_ingreso.TipoIngresoDAO;
import danielcortes.xyz.views.*;
import javax.swing.*;
import java.awt.*;
import java.time.LocalDate;
/**
* Controlador destinado a controlar la vista de ManagerView
* Ademas es la que crea las vistas internas en un CardLayaut junto a sus controladores
*/
public class ManagerController {
private ManagerView view;
private IngresosController ingresosController;
private EgresosController egresosController;
private ArqueoController arqueoController;
private InformesController informesController;
private CajaDAO cajaDAO;
private DocumentosDAO documentosDAO;
private EfectivoDAO efectivoDAO;
private EgresoDAO egresoDAO;
private IngresoDAO ingresoDAO;
private TipoEgresoDAO tipoEgresoDAO;
private TipoIngresoDAO tipoIngresoDAO;
/**
* Crea el controlador
* Necesita todos las interfaces DAO para poder asignarselos a sus vistas,
* esto con el objetivo que sean facilmente intercambiables.
*
* Llama a los metodos que:
* - Cargan el contenido del CardLayout
* - Selecciona una fecha inicial
* - Genera los eventos de la vista
* - Presiona el boton de la vista inicial
*/
public ManagerController(ManagerView view, CajaDAO cajaDAO, DocumentosDAO documentosDAO, EfectivoDAO efectivoDAO, EgresoDAO egresoDAO, IngresoDAO ingresoDAO, TipoEgresoDAO tipoEgresoDAO, TipoIngresoDAO tipoIngresoDAO) {
this.view = view;
this.cajaDAO = cajaDAO;
this.documentosDAO = documentosDAO;
this.efectivoDAO = efectivoDAO;
this.egresoDAO = egresoDAO;
this.ingresoDAO = ingresoDAO;
this.tipoEgresoDAO = tipoEgresoDAO;
this.tipoIngresoDAO = tipoIngresoDAO;
this.loadCardContents();
this.setUpDate();
this.setUpViewEvents();
this.pressInitialButton();
}
/**
* Coloca la fecha actual en el datepicker y luego llama a actualizar las cajas de las vistas
*/
private void setUpDate(){
this.view.getDatePicker().setDateToToday();
this.updateCaja();
}
/**
* Setea los eventos de los botones y el datepicker
* - Cada vez que se cambia la fecha en el datepicker se llama a actualizar la caja
* - Cuando se presiona uno de los botones muestra la vista correspondiente en el cardlayout
*/
private void setUpViewEvents() {
this.view.getEgresosButton().addActionListener(e -> {
CardLayout layout = (CardLayout) this.view.getCardPanel().getLayout();
layout.show(this.view.getCardPanel(), "EGRESOS");
});
this.view.getIngresosButton().addActionListener(e -> {
CardLayout layout = (CardLayout) this.view.getCardPanel().getLayout();
layout.show(this.view.getCardPanel(), "INGRESOS");
});
this.view.getArqueoButton().addActionListener(e -> {
this.arqueoController.updateResumen();
CardLayout layout = (CardLayout) this.view.getCardPanel().getLayout();
layout.show(this.view.getCardPanel(), "ARQUEO");
});
this.view.getInformesButton().addActionListener(e -> {
CardLayout layout = (CardLayout) this.view.getCardPanel().getLayout();
layout.show(this.view.getCardPanel(), "INFORMES");
});
this.view.getDatePicker().addDateChangeListener(e -> updateCaja());
}
/**
* Llama a update caja con la fecha seleccionada en el datepicker en los controladores del manager.
*/
private void updateCaja(){
LocalDate selectedDate = this.view.getDatePicker().getDate();
Caja caja = this.cajaDAO.findByFecha(selectedDate);
if(caja == null){
caja = new Caja();
caja.setFecha(selectedDate);
this.cajaDAO.insertCaja(caja);
Efectivo efectivo = new Efectivo();
efectivo.setCaja(caja);
this.efectivoDAO.insertDefaultEfectivo(efectivo);
Documentos documentos = new Documentos();
documentos.setCaja(caja);
this.documentosDAO.insertDefaultDocumentos(documentos);
}
this.ingresosController.updateCaja(caja);
this.egresosController.updateCaja(caja);
this.arqueoController.updateCaja(caja);
}
/**
* Llama a los metodos que cargan las vistas que estaran dentro del cardlayout
*/
private void loadCardContents() {
this.loadEgresosView();
this.loadIngresosView();
this.loadArqueoView();
this.loadInformesView();
}
/**
* Crea la vista de ingresos, la agrega a el cardlayout y se le es asignado a su controlador
*/
private void loadIngresosView() {
IngresosView ingresosView = new IngresosView();
this.view.getCardPanel().add(ingresosView.getContentPanel(), "INGRESOS");
this.ingresosController = new IngresosController(ingresosView, this.ingresoDAO, this.tipoIngresoDAO);
}
/**
* Crea la vista de egresos, la agrega a el cardlayout y se le es asignado a su controlador
*/
private void loadEgresosView() {
EgresosView egresosView = new EgresosView();
this.view.getCardPanel().add(egresosView.getContentPanel(), "EGRESOS");
this.egresosController = new EgresosController(egresosView, this.egresoDAO, this.tipoEgresoDAO);
}
/**
* Crea la vista de arqueo, la agrega a el cardlayout y se le es asignado a su controlador
*/
private void loadArqueoView() {
ArqueoView arqueoView = new ArqueoView();
this.view.getCardPanel().add(arqueoView.getContentPanel(), "ARQUEO");
this.arqueoController = new ArqueoController(arqueoView, this.efectivoDAO, this.documentosDAO, this.ingresoDAO, this.egresoDAO);
}
private void loadInformesView() {
InformesView informesView = new InformesView();
this.view.getCardPanel().add(informesView.getContentPanel(), "INFORMES");
this.informesController = new InformesController(informesView);
}
/**
* Activa el primer boton del manager
*/
private void pressInitialButton() {
this.view.getIngresosButton().doClick();
}
}