Repository navigation
Expand file tree
/
Copy pathBillEntryController.java
More file actions
279 lines (234 loc) · 9.22 KB
/
Copy pathBillEntryController.java
File metadata and controls
279 lines (234 loc) · 9.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package openconsignment.controller.entry;
import static openconsignment.common.Utils.formatDate;
import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javax.inject.Inject;
import openconsignment.common.Utils;
import openconsignment.entity.BillEntity;
import openconsignment.entity.LREntity;
import openconsignment.model.LR;
import openconsignment.service.BillService;
import openconsignment.service.BuyerService;
import openconsignment.service.SupplierService;
import openconsignment.service.TransportService;
import org.controlsfx.control.textfield.TextFields;
@SuppressWarnings("unused")
public class BillEntryController implements Initializable {
@FXML
private TextField supplier_field;
@FXML
private TextField buyer_name_field;
@FXML
private TextField bill_no_field;
@FXML
private TextField transport_field;
@FXML
private DatePicker date_field;
@FXML
private DatePicker lr_date;
@FXML
private TextField bill_amount_field;
@FXML
private TextField pm_field;
@FXML
private TextField lr_field;
@FXML
private Button save_btn;
@FXML
private Button clear_btn;
@FXML
private Button add_btn;
@FXML
private TableView<LR> lrTable;
@FXML
private TableColumn<LR, String> lr_no_col;
@FXML
private TableColumn<LR, String> pm_col;
@FXML
private Button replace_btn;
@FXML
private Button delete_btn;
private List<LR> lrList;
private final BillService billService;
private final SupplierService supplierService;
private final BuyerService buyerService;
private final TransportService transportService;
@Inject
public BillEntryController(
BillService billService,
SupplierService supplierService,
BuyerService buyerService,
TransportService transportService) {
this.billService = billService;
this.supplierService = supplierService;
this.buyerService = buyerService;
this.transportService = transportService;
}
@Override
public void initialize(URL url, ResourceBundle rb) {
try {
TextFields.bindAutoCompletion(supplier_field, supplierService.getAllSuppliers());
TextFields.bindAutoCompletion(buyer_name_field, buyerService.getAllBuyers());
TextFields.bindAutoCompletion(transport_field, transportService.getAllTransports());
lr_no_col.setCellValueFactory(new PropertyValueFactory<>("lrNo"));
pm_col.setCellValueFactory(new PropertyValueFactory<>("pm"));
lrList = new ArrayList<>();
} catch (Exception ex) {
Utils.showAlert(ex.toString());
Logger.getLogger(BillEntryController.class.getName()).log(Level.SEVERE, ex.toString(), ex);
}
}
@FXML
private void addToList(ActionEvent event) {
if (bill_no_field.getText().isEmpty()
|| lr_field.getText().isEmpty()
|| pm_field.getText().isEmpty()) {
Utils.showAlert("Please ensure that the Bill No., LR No. and PM fields are filled up properly.", 2);
return;
}
lrList.add(new LR(bill_no_field.getText(), lr_field.getText(), pm_field.getText()));
lrTable.setItems(FXCollections.observableList(lrList));
lr_field.clear();
pm_field.clear();
}
@FXML
private void save(ActionEvent event) {
if (supplier_field.getText().isEmpty()
|| buyer_name_field.getText().isEmpty()
|| bill_no_field.getText().isEmpty()
|| date_field.getValue().toString().isEmpty()
|| transport_field.getText().isEmpty()
|| lr_date.getValue().toString().isEmpty()
|| bill_amount_field.getText().isEmpty()
|| lrList.isEmpty()) {
Utils.showAlert("Please ensure to fill up all the fields");
return;
}
List<LREntity> lrEntityList = lrList.stream()
.map(it -> new LREntity(it.getBillNo(), it.getLrNo(), it.getPm()))
.toList();
BillEntity billEntity = BillEntity.builder()
.supplierName(supplier_field.getText())
.buyerName(buyer_name_field.getText())
.billNo(bill_no_field.getText())
.billDate(formatDate(date_field.getValue().toString()))
.transport(transport_field.getText())
.lrDate(formatDate(lr_date.getValue().toString()))
.billAmount(bill_amount_field.getText())
.lrEntities(lrEntityList)
.build();
try {
billService.saveBill(billEntity);
clearAllFields();
Utils.showAlert("Saved Successfully", 1);
} catch (Exception ex) {
Utils.showAlert(ex.toString());
Logger.getLogger(BillEntryController.class.getName()).log(Level.SEVERE, ex.toString(), ex);
}
}
@FXML
private void clearAllFields() {
supplier_field.clear();
buyer_name_field.clear();
bill_no_field.clear();
transport_field.clear();
date_field.setValue(null);
lr_date.setValue(null);
bill_amount_field.clear();
pm_field.clear();
lr_field.clear();
lrList.clear();
lrTable.setItems(FXCollections.observableList(lrList));
}
@FXML
private void replaceInList(ActionEvent event) {
if (lrTable.getSelectionModel().getSelectedIndex() == -1) {
Utils.showAlert("Please select an item from the LR Table", 2);
return;
}
if (bill_no_field.getText().isEmpty()
|| lr_field.getText().isEmpty()
|| pm_field.getText().isEmpty()) {
Utils.showAlert("Please ensure that the Bill No., LR No. and PM fields are filled up properly.", 2);
return;
}
lrList.set(
lrTable.getSelectionModel().getSelectedIndex(),
new LR(bill_no_field.getText(), lr_field.getText(), pm_field.getText()));
lr_field.clear();
pm_field.clear();
lrTable.setItems(FXCollections.observableList(lrList));
}
@FXML
private void deleteFromList(ActionEvent event) {
if (lrTable.getSelectionModel().getSelectedIndex() == -1) {
Utils.showAlert("Please select an item from the LR Table", 2);
return;
}
lrList.remove(lrTable.getSelectionModel().getSelectedIndex());
lrTable.setItems(FXCollections.observableList(lrList));
}
@FXML
private void fetchBill(ActionEvent event) {
try {
BillEntity billEntity = billService.getBill(bill_no_field.getText());
if (null == billEntity) {
Utils.showAlert("Record not found. Please try again later.", 2);
return;
}
supplier_field.setText(billEntity.getSupplierName());
buyer_name_field.setText(billEntity.getBuyerName());
date_field.setValue(Utils.parseDate(billEntity.getBillDate()));
transport_field.setText(billEntity.getTransport());
lr_date.setValue(Utils.parseDate(billEntity.getLrDate()));
bill_amount_field.setText(billEntity.getBillAmount());
lrList = billEntity.getLrEntities().stream()
.map(i -> new LR(i.getBillNo(), i.getLrNo(), i.getPm()))
.collect(Collectors.toCollection(ArrayList::new));
lrTable.setItems(FXCollections.observableArrayList(lrList));
// delete_entry_btn.setDisable(false);
} catch (SQLException ex) {
Utils.showAlert(ex.toString());
Logger.getLogger(BillEntryController.class.getName()).log(Level.SEVERE, ex.toString(), ex);
}
}
@FXML
private void deleteEntry(ActionEvent event) {
Alert alert = new Alert(
Alert.AlertType.CONFIRMATION,
"Are you sure that you want delete " + bill_no_field.getText() + " ?",
ButtonType.YES,
ButtonType.NO,
ButtonType.CANCEL);
alert.showAndWait();
if (alert.getResult() != ButtonType.YES) {
return;
}
try {
billService.deleteBill(bill_no_field.getText());
supplier_field.setText("");
buyer_name_field.setText("");
date_field.setValue(null);
transport_field.setText("");
lr_date.setValue(null);
bill_amount_field.setText("");
lrTable.setItems(FXCollections.observableArrayList());
Utils.showAlert(bill_no_field.getText().toUpperCase() + " Entry was successfully deleted.", 1);
} catch (SQLException ex) {
Utils.showAlert(ex.toString());
Logger.getLogger(BillEntryController.class.getName()).log(Level.SEVERE, ex.toString(), ex);
}
}
}