-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddPaymentController.java
More file actions
400 lines (331 loc) · 13.3 KB
/
Copy pathAddPaymentController.java
File metadata and controls
400 lines (331 loc) · 13.3 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package openconsignment.controller.entry.payment;
import static openconsignment.common.Utils.formatDate;
import java.net.URL;
import java.sql.SQLException;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
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 javafx.scene.text.Text;
import javax.inject.Inject;
import openconsignment.common.Constants;
import openconsignment.common.Utils;
import openconsignment.entity.BillEntity;
import openconsignment.entity.PaymentEntity;
import openconsignment.entity.PaymentItemEntity;
import openconsignment.model.PaymentItem;
import openconsignment.service.BillService;
import openconsignment.service.PaymentService;
import openconsignment.service.SupplierService;
public class AddPaymentController implements Initializable {
private List<PaymentItem> paymentItems;
private List<String> supplierNameComboList;
private List<String> billNoComboList;
private int previouslyDue;
private static final Logger logger = Logger.getLogger(AddPaymentController.class.getName());
@FXML
private TextField dd_no_field;
@FXML
private DatePicker dd_date_field;
@FXML
private DatePicker voucher_date_field;
@FXML
private TextField voucher_no_field;
@FXML
private Button add_payment_btn;
@FXML
private Button replace_payment_btn;
@FXML
private Button delete_payment_btn;
@FXML
private TableColumn<?, ?> bill_no_col;
@FXML
private TableColumn<?, ?> bill_amt_col;
@FXML
private TableColumn<?, ?> dd_no_col;
@FXML
private TableColumn<?, ?> dd_date_col;
@FXML
private TableColumn<?, ?> bill_dt_col;
@FXML
private TableColumn<?, ?> buyer_col;
@FXML
private TableColumn<?, ?> due_col;
@FXML
private TableColumn<?, ?> amount_paid_col;
@FXML
private TableView<PaymentItem> payment_tableview;
@FXML
private TextField amount_paid_field;
@FXML
private TextField buyer_name_field;
@FXML
private TextField bill_date_field;
@FXML
private TextField bill_amount_field;
@FXML
private ComboBox<String> supplier_name_combo;
@FXML
private ComboBox<String> bill_no_combo;
@FXML
private TextField due_amount_field;
@FXML
private TableColumn<?, ?> bank_col;
@FXML
private TextField bank_field;
@FXML
private TextField total_amount_paid_field;
@FXML
private Button save_btn;
@FXML
private Button clear_btn;
@FXML
private Text last_voucher_field;
private final BillService billService;
private final PaymentService paymentService;
private final SupplierService supplierService;
@Inject
public AddPaymentController(
BillService billService, PaymentService paymentService, SupplierService supplierService) {
this.billService = billService;
this.paymentService = paymentService;
this.supplierService = supplierService;
}
@Override
public void initialize(URL url, ResourceBundle rb) {
paymentItems = new ArrayList<>();
supplierNameComboList = new ArrayList<>();
billNoComboList = new ArrayList<>();
bill_no_col.setCellValueFactory(new PropertyValueFactory<>("billNo"));
bill_amt_col.setCellValueFactory(new PropertyValueFactory<>("billAmount"));
bill_dt_col.setCellValueFactory(new PropertyValueFactory<>("billDate"));
buyer_col.setCellValueFactory(new PropertyValueFactory<>("buyerName"));
bank_col.setCellValueFactory(new PropertyValueFactory<>("bank"));
dd_no_col.setCellValueFactory(new PropertyValueFactory<>("ddNo"));
dd_date_col.setCellValueFactory(new PropertyValueFactory<>("ddDate"));
due_col.setCellValueFactory(new PropertyValueFactory<>("due"));
amount_paid_col.setCellValueFactory(new PropertyValueFactory<>("amountPaid"));
refreshPaymentTableView();
fillSupplierCombo();
updateLastVoucher();
}
private void refreshPaymentTableView() {
payment_tableview.setItems(FXCollections.observableArrayList(paymentItems));
}
@FXML
private void addPayment(ActionEvent ignoredEvent) {
if (bill_no_combo.getValue().isEmpty()
|| bill_amount_field.getText().isEmpty()
|| buyer_name_field.getText().isEmpty()
|| supplier_name_combo.getValue().isEmpty()
|| bank_field.getText().isEmpty()
|| bank_field.getText().isEmpty()
|| due_amount_field.getText().isEmpty()
|| amount_paid_field.getText().isEmpty()
|| dd_no_field.getText().isEmpty()
|| dd_date_field.getValue().toString().isEmpty()) {
Utils.showAlert("Please check whether the fields are properly filled or not.", 2);
return;
}
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(Constants.DATE_TIME_FORMAT);
paymentItems.add(new PaymentItem(
bill_no_combo.getValue(),
bill_amount_field.getText(),
bill_date_field.getText(),
buyer_name_field.getText(),
due_amount_field.getText(),
amount_paid_field.getText(),
bank_field.getText(),
dd_no_field.getText(),
dateTimeFormatter.format(dd_date_field.getValue())));
refreshPaymentTableView();
clearRepeatingFields();
updateTotalAmountPaid();
}
@FXML
private void replacePayment(ActionEvent ignoredEvent) {
if (payment_tableview.getSelectionModel().getSelectedIndex() == -1) {
Utils.showAlert("Please select an item from the Payment Table", 2);
return;
}
if (bill_no_combo.getValue().isEmpty()
|| bill_amount_field.getText().isEmpty()
|| buyer_name_field.getText().isEmpty()
|| supplier_name_combo.getValue().isEmpty()
|| bank_field.getText().isEmpty()
|| bank_field.getText().isEmpty()
|| due_amount_field.getText().isEmpty()
|| amount_paid_field.getText().isEmpty()
|| dd_no_field.getText().isEmpty()
|| dd_date_field.getValue().toString().isEmpty()) {
Utils.showAlert("Please check whether the fields are properly filled or not.", 2);
return;
}
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(Constants.DATE_TIME_FORMAT);
paymentItems.set(
payment_tableview.getSelectionModel().getSelectedIndex(),
new PaymentItem(
bill_no_combo.getValue(),
bill_amount_field.getText(),
bill_date_field.getText(),
buyer_name_field.getText(),
due_amount_field.getText(),
amount_paid_field.getText(),
bank_field.getText(),
dd_no_field.getText(),
dateTimeFormatter.format(dd_date_field.getValue())));
refreshPaymentTableView();
clearRepeatingFields();
updateTotalAmountPaid();
}
@FXML
private void deletePayment(ActionEvent ignoredEvent) {
if (payment_tableview.getSelectionModel().getSelectedIndex() == -1) {
Utils.showAlert("Please select an item from the Payment Table", 2);
return;
}
paymentItems.remove(payment_tableview.getSelectionModel().getSelectedIndex());
refreshPaymentTableView();
updateTotalAmountPaid();
}
private void clearRepeatingFields() {
bill_date_field.setText("");
buyer_name_field.setText("");
bill_amount_field.setText("0");
due_amount_field.setText("0");
amount_paid_field.setText("0");
bank_field.setText("");
dd_no_field.setText("");
dd_date_field.setValue(null);
}
private void updateTotalAmountPaid() {
int totalAmountPaid = 0;
for (PaymentItem paymentItem : paymentItems) {
totalAmountPaid += Integer.parseInt(paymentItem.getAmountPaid());
}
total_amount_paid_field.setText(Integer.toString(totalAmountPaid));
}
@FXML
private void clearAllFields() {
clearRepeatingFields();
voucher_no_field.setText("");
voucher_date_field.setValue(null);
supplier_name_combo.setDisable(false);
supplier_name_combo.getSelectionModel().clearSelection();
billNoComboList.clear();
bill_no_combo.setItems(FXCollections.observableArrayList(billNoComboList));
}
@FXML
private void saveData(ActionEvent ignoredEvent) {
Alert alert = new Alert(
Alert.AlertType.CONFIRMATION,
"Are you sure that you want save " + voucher_no_field.getText() + " ?",
ButtonType.YES,
ButtonType.NO,
ButtonType.CANCEL);
alert.showAndWait();
if (alert.getResult() != ButtonType.YES) {
return;
}
if (voucher_no_field.getText().isEmpty()
|| voucher_date_field.getValue() == null
|| voucher_date_field.getValue().toString().isEmpty()) {
Utils.showAlert("Check whether the Voucher No. and the Voucher Date is properly filled", 2);
return;
}
List<PaymentItemEntity> paymentItemEntities = paymentItems.stream()
.map(it -> PaymentItemEntity.builder()
.voucherNo(voucher_no_field.getText())
.buyerName(it.getBuyerName())
.billNo(it.getBillNo())
.billDate(formatDate(it.getBillDate()))
.billAmount(it.getBillAmount())
.dueAmount(it.getDue())
.amountPaid(it.getAmountPaid())
.bank(it.getBank())
.ddNo(it.getDdNo())
.ddDate(formatDate(it.getDdDate()))
.build())
.toList();
PaymentEntity paymentEntity = PaymentEntity.builder()
.voucherNo(voucher_no_field.getText())
.voucherDate(formatDate(voucher_date_field.getValue().toString()))
.supplierName(supplier_name_combo.getValue())
.totalAmount(total_amount_paid_field.getText())
.items(paymentItemEntities)
.build();
try {
paymentService.savePayment(paymentEntity);
Utils.showAlert("Saved Successfully", 1);
clearAllFields();
} catch (SQLException ex) {
Utils.showAlert(ex.toString());
Logger.getLogger(PaymentEntryController.class.getName()).log(Level.SEVERE, ex.toString(), ex);
}
clearAllFields();
paymentItems.clear();
refreshPaymentTableView();
updateLastVoucher();
}
private void updateLastVoucher() {
try {
String answer = paymentService.getLastVoucher();
last_voucher_field.setText("Last Voucher No. : " + answer);
} catch (SQLException ex) {
Utils.showAlert(ex.toString());
logger.log(Level.SEVERE, ex.toString(), ex);
}
}
private void fillSupplierCombo() {
try {
supplierNameComboList = supplierService.getAllSuppliers();
supplier_name_combo.setItems(FXCollections.observableArrayList(supplierNameComboList));
} catch (SQLException ex) {
Utils.showAlert(ex.toString());
logger.log(Level.SEVERE, "Failed to populate the supplier combo", ex);
}
}
@FXML
private void fillBillNoCombo(ActionEvent ignoredEvent) {
try {
billNoComboList = paymentService.fetchPendingBillNosForSupplier(supplier_name_combo.getValue());
bill_no_combo.setItems(FXCollections.observableArrayList(billNoComboList));
} catch (SQLException ex) {
Utils.showAlert(ex.toString());
logger.log(Level.SEVERE, "Failed to populate bill number combo", ex);
}
}
@FXML
private void fetchData(ActionEvent ignoredEvent) {
if (null == bill_no_combo.getValue()) {
return;
}
try {
BillEntity billEntity = billService.getBill(bill_no_combo.getValue());
buyer_name_field.setText(billEntity.getBuyerName());
bill_date_field.setText(billEntity.getBillDate());
bill_amount_field.setText(billEntity.getBillAmount());
previouslyDue = paymentService.fetchPendingAmountForBillNo(bill_no_combo.getValue());
updateDueAmount();
} catch (SQLException ex) {
Utils.showAlert(ex.toString());
logger.log(Level.SEVERE, ex.toString(), ex);
}
}
@FXML
private void updateDueAmount() {
int amountPaid = 0;
try {
amountPaid = Integer.parseInt(amount_paid_field.getText());
} catch (Exception ex) {
logger.log(Level.WARNING, "cannot be converted to integer");
}
due_amount_field.setText(Integer.toString(previouslyDue - amountPaid));
}
}