· 8 years ago · Feb 27, 2018, 07:50 AM
1package ajfmo.sislic.view;
2
3import java.net.URL;
4import java.util.Optional;
5import java.util.ResourceBundle;
6
7import ajfmo.sislic.controller.EmployeeDAO;
8import ajfmo.sislic.controller.ProductTransactionDAO;
9import ajfmo.sislic.controller.SucursalDAO;
10import ajfmo.sislic.controller.TransactionsDAO;
11import ajfmo.sislic.entities.Empleados;
12import ajfmo.sislic.entities.Movproductos;
13import ajfmo.sislic.entities.Productos;
14import ajfmo.sislic.entities.Sucursales;
15import ajfmo.sislic.utils.HibernateUtil;
16import javafx.collections.FXCollections;
17import javafx.collections.ObservableList;
18import javafx.event.EventHandler;
19import javafx.fxml.FXML;
20import javafx.fxml.Initializable;
21import javafx.scene.control.Alert;
22import javafx.scene.control.Button;
23import javafx.scene.control.ButtonType;
24import javafx.scene.control.ComboBox;
25import javafx.scene.control.DatePicker;
26import javafx.scene.control.TableColumn;
27import javafx.scene.control.TableView;
28import javafx.scene.control.TableColumn.CellEditEvent;
29import javafx.scene.control.cell.PropertyValueFactory;
30import javafx.scene.control.cell.TextFieldTableCell;
31import javafx.stage.Stage;
32import javafx.util.converter.DoubleStringConverter;
33
34public class TransactionView implements Initializable {
35
36 // Objects
37 private final TransactionsDAO transactions = new TransactionsDAO();
38 private final ProductTransactionDAO productTransaction = new ProductTransactionDAO();
39 private final EmployeeDAO employee = new EmployeeDAO();
40 private final SucursalDAO warehouse = new SucursalDAO();
41
42 // Collections
43 private ObservableList<Empleados> employeeList;
44 private ObservableList<Sucursales> warehouseList;
45 private ObservableList<Productos> products;
46
47 // FXML
48 @FXML
49 private DatePicker dpFechaInicial, dpFechaFinal;
50 @FXML
51 private ComboBox<Sucursales> cboSucursal;
52 @FXML
53 private ComboBox<Empleados> cboEmpleado;
54 @FXML
55 private Button btnBuscar, btnEliminar, btnGuardar, btnCancelar, btnSalir;
56 @FXML
57 private TableView<Productos> tblTransactions;
58 @FXML
59 private TableColumn<Productos, String> colCodigo;
60 @FXML
61 private TableColumn<Productos, String> colDescripcion;
62 @FXML
63 private TableColumn<Productos, Double> colExistenciaI;
64 @FXML
65 private TableColumn<Productos, Double> colExistenciaF;
66 @FXML
67 private TableColumn<Movproductos, Double> colDiferencia;
68
69 /*****************/
70 /** **/
71 /** Source Code **/
72 /** **/
73 /*****************/
74
75 @Override
76 public void initialize(URL location, ResourceBundle resources) {
77 employeeList = FXCollections.observableArrayList();
78 warehouseList = FXCollections.observableArrayList();
79 products = FXCollections.observableArrayList();
80
81 fillWarehouseCbo(warehouseList);
82 fillEmployeeCbo(employeeList);
83
84 cboSucursal.setItems(warehouseList);
85 cboEmpleado.setItems(employeeList);
86
87 colExistenciaF.setCellFactory(TextFieldTableCell.forTableColumn(new DoubleStringConverter()));
88 colExistenciaF.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Productos, Double>>() {
89 @Override
90 public void handle(CellEditEvent<Productos, Double> event) {
91 event.getTableView().getItems().get(event.getTablePosition().getRow())
92 .setExistencia(event.getNewValue());
93
94 // Code to get calculated that other column
95 }
96 });
97
98 }
99
100 /**
101 * Fill employee comboBox
102 */
103 private void fillEmployeeCbo(ObservableList<Empleados> employeeList) {
104 for (Empleados employees : employee.employeesCriteria()) {
105 employeeList.add(employees);
106 }
107 }
108
109 /**
110 * Fill warehouse combobox
111 */
112 private void fillWarehouseCbo(ObservableList<Sucursales> warehouseList) {
113 for (Sucursales warehouses : warehouse.warehouseCriteria()) {
114 warehouseList.add(warehouses);
115 }
116 }
117
118 /**
119 * Fill table
120 */
121 private void fillTransactionTbl(ObservableList<Productos> productsTx/* , int id_movimiento */) {
122 for (Productos products : productTransaction.productTxCriteria(/* id_movimiento */)) {
123 productsTx.add(products);
124 }
125 }
126
127 /**
128 * Create transaction
129 */
130 @FXML
131 private void create() {
132 transactions.createTransaction(dpFechaInicial.getValue(), dpFechaFinal.getValue(), cboSucursal.getValue(),
133 cboEmpleado.getValue());
134 }
135
136 /**
137 * Search transaction if not exists then create a transaction and then fill the
138 * tableview with the products avaible in the database, user proceed to make
139 * changes in the tableview and when is finished saves the productsTransaction
140 * into MovProductos
141 */
142 // TODO: MEJORAR HACIENDO QUE APAREZCA UN CUADRO CUANDO SE HACE CLICK EN
143 // BUSCAR Y QUE SALGA UN CUADRO CON TODOS LOS MOVIMIENTOS PERMITIENDO AL USUARIO
144 // SELECCIONAR EL MOVIMIENTO QUE NECESITA BUSCANDOLO A TRAVES DE UN TEXTBOX QUE
145 // INDIQUE EL ID DEL MOVIMIENTO O LA FECHA
146 @FXML
147 private void search() {
148 Object resultset = transactions.searchTransaction(dpFechaInicial.getValue(), dpFechaFinal.getValue(),
149 cboSucursal.getValue(), cboEmpleado.getValue());
150 if (resultset != null) {
151 // Si existe entonces llena la tabla con la informaccion necesaria
152 // para esto se necesita un metodo que ejecute un query donde se seleccione de
153 // la tabla MovProductos
154 // segun el id del movimiento (valor del resultset) y llene todas las columnas
155 // from MovProductos as o where o.movimiento_id =: movimiento_id
156 // hqlquery.setParameter("movimiento_id", movimiento_id);
157 // en la vista iria un for que recorra todos los resultados que cumplan con la
158 // condicion del query
159
160 System.out.println("Id del movimiento " + resultset);
161
162 } else {
163 Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
164 alert.setTitle("Registro no disponible");
165 alert.setHeaderText(
166 "El movimiento que busca no existe, Fecha inicial " + dpFechaInicial.getValue().toString()
167 + " Fecha final " + dpFechaFinal.getValue().toString() + " Sucursal "
168 + cboSucursal.getValue().toString() + " Empleado " + cboEmpleado.getValue().toString());
169 alert.setContentText("Desea Crear movimiento?");
170 Optional<ButtonType> result = alert.showAndWait();
171 if (result.get() == ButtonType.OK) {
172 create();
173
174 resultset = transactions.searchTransaction(dpFechaInicial.getValue(), dpFechaFinal.getValue(),
175 cboSucursal.getValue(), cboEmpleado.getValue());
176
177 System.out.println("Id del movimiento " + resultset);
178
179 fillTransactionTbl(products);
180 tblTransactions.setItems(products);
181
182 colCodigo.setCellValueFactory(new PropertyValueFactory<Productos, String>("codigo"));
183 colDescripcion.setCellValueFactory(new PropertyValueFactory<Productos, String>("descrip"));
184 colExistenciaI.setCellValueFactory(new PropertyValueFactory<Productos, Double>("existencia"));
185
186 }
187 }
188 }
189
190 /**
191 * delete
192 */
193 @FXML
194 private void delete() {
195
196 }
197
198 /**
199 * Cancel Operation
200 */
201 @FXML
202 private void cancel() {
203 dpFechaInicial.setValue(null);
204 dpFechaFinal.setValue(null);
205 cboSucursal.setValue(null);
206 cboEmpleado.setValue(null);
207 }
208
209 /**
210 * Exit frame
211 */
212 @FXML
213 private void exit() {
214 if (HibernateUtil.getSessionFactory() != null) {
215 HibernateUtil.shutdown();
216 HibernateUtil.getSessionFactory().close();
217 }
218 Stage stage = (Stage) btnSalir.getScene().getWindow();
219 HibernateUtil.shutdown();
220 stage.close();
221 }
222
223}