Created
February 15, 2018 15:10
-
-
Save ajfmo/8ee8abb3b2185ea2bec4fc5ac179b4fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ajfmo.sislic.view; | |
import java.net.URL; | |
import java.util.Optional; | |
import java.util.ResourceBundle; | |
import ajfmo.sislic.controller.EmployeeDAO; | |
import ajfmo.sislic.controller.ProductTransactionDAO; | |
import ajfmo.sislic.controller.SucursalDAO; | |
import ajfmo.sislic.controller.TransactionsDAO; | |
import ajfmo.sislic.entities.Empleados; | |
import ajfmo.sislic.entities.Movproductos; | |
import ajfmo.sislic.entities.Productos; | |
import ajfmo.sislic.entities.Sucursales; | |
import ajfmo.sislic.utils.HibernateUtil; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.event.EventHandler; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.Alert; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.ButtonType; | |
import javafx.scene.control.ComboBox; | |
import javafx.scene.control.DatePicker; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.TableColumn.CellEditEvent; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import javafx.scene.control.cell.TextFieldTableCell; | |
import javafx.stage.Stage; | |
import javafx.util.converter.DoubleStringConverter; | |
public class TransactionView implements Initializable { | |
// Objects | |
private final TransactionsDAO transactions = new TransactionsDAO(); | |
private final ProductTransactionDAO productTransaction = new ProductTransactionDAO(); | |
private final EmployeeDAO employee = new EmployeeDAO(); | |
private final SucursalDAO warehouse = new SucursalDAO(); | |
// Collections | |
private ObservableList<Empleados> employeeList; | |
private ObservableList<Sucursales> warehouseList; | |
private ObservableList<Productos> products; | |
// FXML | |
@FXML | |
private DatePicker dpFechaInicial, dpFechaFinal; | |
@FXML | |
private ComboBox<Sucursales> cboSucursal; | |
@FXML | |
private ComboBox<Empleados> cboEmpleado; | |
@FXML | |
private Button btnBuscar, btnEliminar, btnGuardar, btnCancelar, btnSalir; | |
@FXML | |
private TableView<Productos> tblTransactions; | |
@FXML | |
private TableColumn<Productos, String> colCodigo; | |
@FXML | |
private TableColumn<Productos, String> colDescripcion; | |
@FXML | |
private TableColumn<Productos, Double> colExistenciaI; | |
@FXML | |
private TableColumn<Productos, Double> colExistenciaF; | |
@FXML | |
private TableColumn<Movproductos, Double> colDiferencia; | |
/*****************/ | |
/** **/ | |
/** Source Code **/ | |
/** **/ | |
/*****************/ | |
@Override | |
public void initialize(URL location, ResourceBundle resources) { | |
employeeList = FXCollections.observableArrayList(); | |
warehouseList = FXCollections.observableArrayList(); | |
products = FXCollections.observableArrayList(); | |
fillWarehouseCbo(warehouseList); | |
fillEmployeeCbo(employeeList); | |
cboSucursal.setItems(warehouseList); | |
cboEmpleado.setItems(employeeList); | |
colExistenciaF.setCellFactory(TextFieldTableCell.forTableColumn(new DoubleStringConverter())); | |
colExistenciaF.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Productos, Double>>() { | |
@Override | |
public void handle(CellEditEvent<Productos, Double> event) { | |
event.getTableView().getItems().get(event.getTablePosition().getRow()) | |
.setExistencia(event.getNewValue()); | |
// Code to get calculated that other column | |
} | |
}); | |
} | |
/** | |
* Fill employee comboBox | |
*/ | |
private void fillEmployeeCbo(ObservableList<Empleados> employeeList) { | |
for (Empleados employees : employee.employeesCriteria()) { | |
employeeList.add(employees); | |
} | |
} | |
/** | |
* Fill warehouse combobox | |
*/ | |
private void fillWarehouseCbo(ObservableList<Sucursales> warehouseList) { | |
for (Sucursales warehouses : warehouse.warehouseCriteria()) { | |
warehouseList.add(warehouses); | |
} | |
} | |
/** | |
* Fill table | |
*/ | |
private void fillTransactionTbl(ObservableList<Productos> productsTx/* , int id_movimiento */) { | |
for (Productos products : productTransaction.productTxCriteria(/* id_movimiento */)) { | |
productsTx.add(products); | |
} | |
} | |
/** | |
* Create transaction | |
*/ | |
@FXML | |
private void create() { | |
transactions.createTransaction(dpFechaInicial.getValue(), dpFechaFinal.getValue(), cboSucursal.getValue(), | |
cboEmpleado.getValue()); | |
} | |
/** | |
* Search transaction if not exists then create a transaction and then fill the | |
* tableview with the products avaible in the database, user proceed to make | |
* changes in the tableview and when is finished saves the productsTransaction | |
* into MovProductos | |
*/ | |
// TODO: MEJORAR HACIENDO QUE APAREZCA UN CUADRO CUANDO SE HACE CLICK EN | |
// BUSCAR Y QUE SALGA UN CUADRO CON TODOS LOS MOVIMIENTOS PERMITIENDO AL USUARIO | |
// SELECCIONAR EL MOVIMIENTO QUE NECESITA BUSCANDOLO A TRAVES DE UN TEXTBOX QUE | |
// INDIQUE EL ID DEL MOVIMIENTO O LA FECHA | |
@FXML | |
private void search() { | |
Object resultset = transactions.searchTransaction(dpFechaInicial.getValue(), dpFechaFinal.getValue(), | |
cboSucursal.getValue(), cboEmpleado.getValue()); | |
if (resultset != null) { | |
// Si existe entonces llena la tabla con la informaccion necesaria | |
// para esto se necesita un metodo que ejecute un query donde se seleccione de | |
// la tabla MovProductos | |
// segun el id del movimiento (valor del resultset) y llene todas las columnas | |
// from MovProductos as o where o.movimiento_id =: movimiento_id | |
// hqlquery.setParameter("movimiento_id", movimiento_id); | |
// en la vista iria un for que recorra todos los resultados que cumplan con la | |
// condicion del query | |
System.out.println("Id del movimiento " + resultset); | |
} else { | |
Alert alert = new Alert(Alert.AlertType.CONFIRMATION); | |
alert.setTitle("Registro no disponible"); | |
alert.setHeaderText( | |
"El movimiento que busca no existe, Fecha inicial " + dpFechaInicial.getValue().toString() | |
+ " Fecha final " + dpFechaFinal.getValue().toString() + " Sucursal " | |
+ cboSucursal.getValue().toString() + " Empleado " + cboEmpleado.getValue().toString()); | |
alert.setContentText("Desea Crear movimiento?"); | |
Optional<ButtonType> result = alert.showAndWait(); | |
if (result.get() == ButtonType.OK) { | |
create(); | |
resultset = transactions.searchTransaction(dpFechaInicial.getValue(), dpFechaFinal.getValue(), | |
cboSucursal.getValue(), cboEmpleado.getValue()); | |
System.out.println("Id del movimiento " + resultset); | |
fillTransactionTbl(products); | |
tblTransactions.setItems(products); | |
colCodigo.setCellValueFactory(new PropertyValueFactory<Productos, String>("codigo")); | |
colDescripcion.setCellValueFactory(new PropertyValueFactory<Productos, String>("descrip")); | |
colExistenciaI.setCellValueFactory(new PropertyValueFactory<Productos, Double>("existencia")); | |
} | |
} | |
} | |
/** | |
* delete | |
*/ | |
@FXML | |
private void delete() { | |
} | |
/** | |
* Cancel Operation | |
*/ | |
@FXML | |
private void cancel() { | |
dpFechaInicial.setValue(null); | |
dpFechaFinal.setValue(null); | |
cboSucursal.setValue(null); | |
cboEmpleado.setValue(null); | |
} | |
/** | |
* Exit frame | |
*/ | |
@FXML | |
private void exit() { | |
if (HibernateUtil.getSessionFactory() != null) { | |
HibernateUtil.shutdown(); | |
HibernateUtil.getSessionFactory().close(); | |
} | |
Stage stage = (Stage) btnSalir.getScene().getWindow(); | |
HibernateUtil.shutdown(); | |
stage.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment