-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathAmpController.java
More file actions
1007 lines (863 loc) · 31.3 KB
/
AmpController.java
File metadata and controls
1007 lines (863 loc) · 31.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Open Hospital Management Information System
*
* Dr M H B Ariyaratne
* Acting Consultant (Health Informatics)
* (94) 71 5812399
* (94) 71 5812399
*/
package com.divudi.bean.pharmacy;
import com.divudi.bean.common.CategoryController;
import com.divudi.bean.common.ConfigOptionApplicationController;
import com.divudi.bean.common.SessionController;
import com.divudi.core.util.JsfUtil;
import com.divudi.core.data.DepartmentType;
import com.divudi.core.data.ItemSupplierPrices;
import com.divudi.core.data.ItemType;
import com.divudi.core.data.SymanticType;
import com.divudi.ejb.BillNumberGenerator;
import com.divudi.ejb.PharmacyBean;
import com.divudi.core.entity.Category;
import com.divudi.core.entity.Department;
import com.divudi.core.entity.Item;
import com.divudi.core.entity.pharmacy.Amp;
import com.divudi.core.entity.pharmacy.Vmp;
import com.divudi.core.entity.pharmacy.Vtm;
import com.divudi.core.entity.pharmacy.VirtualProductIngredient;
import com.divudi.core.facade.AmpFacade;
import com.divudi.core.facade.StockFacade;
import com.divudi.core.facade.VmpFacade;
import com.divudi.core.facade.VirtualProductIngredientFacade;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.event.TabChangeEvent;
import org.primefaces.model.file.UploadedFile;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.Iterator;
/**
*
* @author Dr. M. H. B. Ariyaratne, MBBS, MSc, MD(Health Informatics) Acting
* Consultant (Health Informatics)
*/
@Named
@SessionScoped
public class AmpController implements Serializable {
private static final long serialVersionUID = 1L;
@EJB
private AmpFacade ejbFacade;
@EJB
BillNumberGenerator billNumberBean;
@EJB
private VmpFacade vmpFacade;
@EJB
private VirtualProductIngredientFacade vivFacade;
@EJB
PharmacyBean pharmacyBean;
@EJB
StockFacade stockFacade;
List<Amp> selectedItems;
private Amp current;
private Vtm vtm;
private List<Amp> items = null;
String selectText = "";
private String tabId = "tabVmp";
private VirtualProductIngredient addingVtmInVmp;
private Vmp currentVmp;
List<Amp> itemsByCode = null;
List<Amp> listToRemove = null;
Department department;
List<Amp> itemList;
List<ItemSupplierPrices> itemSupplierPrices;
@Inject
SessionController sessionController;
@Inject
ItemsDistributorsController itemDistributorsController;
@Inject
CategoryController categoryController;
@Inject
VmpController vmpController;
@Inject
private ConfigOptionApplicationController configOptionApplicationController;
private UploadedFile file;
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public String navigateToCreateItemList() {
return "/pharmacy/list_amps?faces-redirect=true"; // Then navigate
}
public String navigateToCreateMedicineList() {
return "/pharmacy/list_medicines?faces-redirect=true"; // Then navigate
}
public void uploadAmps() {
try {
Workbook workbook = new XSSFWorkbook(file.getInputStream());
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Cell categoryNameCell = currentRow.getCell(0); // adjust the index as per your Excel file structure
Cell vmpNameCell = currentRow.getCell(1); // adjust the index as per your Excel file structure
Cell ampNameCell = currentRow.getCell(2); // adjust the index as per your Excel file structure
Cell ampCodeCell = currentRow.getCell(3);
Cell ampBarCodeCell = currentRow.getCell(4);
String categoryName = categoryNameCell.getStringCellValue();
String vmpName = vmpNameCell.getStringCellValue();
String ampName = ampNameCell.getStringCellValue();
String ampCode = null;
if (ampCodeCell.getCellType() == CellType.STRING) {
ampCode = ampCodeCell.getStringCellValue();
} else if (ampCodeCell.getCellType() == CellType.NUMERIC) {
double numericCellValue = ampCodeCell.getNumericCellValue();
ampCode = BigDecimal.valueOf(numericCellValue).toPlainString();
}
String ampBarcode = "";
if (ampBarCodeCell.getCellType() == CellType.STRING) {
ampBarcode = ampBarCodeCell.getStringCellValue();
} else if (ampBarCodeCell.getCellType() == CellType.NUMERIC) {
double numericCellValue = ampBarCodeCell.getNumericCellValue();
ampBarcode = BigDecimal.valueOf(numericCellValue).toPlainString();
}
Category cat = categoryController.findAndCreateCategoryByName(categoryName);
Vmp vmp = vmpController.findOrCreateVmpByName(vmpName);
Amp amp;
amp = findAmpByName(ampName);
if (amp == null) {
amp = new Amp();
}
amp.setCategory(cat);
amp.setVmp(vmp);
amp.setName(ampName);
amp.setCode(ampCode);
amp.setBarcode(ampBarcode);
amp.setSymanticType(SymanticType.Pharmacologic_Substance);
if (amp.getId() == null) {
getFacade().create(amp);
} else {
getFacade().edit(amp);
}
}
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public String navigateToListAllAmps() {
String jpql = "Select amp "
+ " from Amp amp "
+ " where amp.retired=:ret "
+ " order by amp.name";
Map m = new HashMap();
m.put("ret", false);
items = getFacade().findByJpql(jpql, m);
return "/emr/reports/amps?faces-redirect=true";
}
public void fillItemsForItemSupplierPrices() {
List<Amp> amps = getLongCodeItems();
itemSupplierPrices = new ArrayList<>();
for (Amp a : amps) {
ItemSupplierPrices p = new ItemSupplierPrices();
p.setItem(a);
p.setAmp(a);
p.setVmp(a.getVmp());
itemSupplierPrices.add(p);
}
// for (ItemSupplierPrices p : itemSupplierPrices) {
// p.setPp(getPharmacyBean().getLastPurchaseRate(p.getAmp()));
// }
// for (ItemSupplierPrices p : itemSupplierPrices) {
// p.setSp(getPharmacyBean().getLastRetailRate(p.getAmp()));
// }
// for (ItemSupplierPrices p : itemSupplierPrices) {
// p.setSupplier(itemDistributorsController.getDistributor(p.getAmp()));
// }
}
public void fillPricesForItemSupplierPrices() {
// List<Amp> amps = getLongCodeItems();
// itemSupplierPrices = new ArrayList<>();
// for (Amp a : amps) {
// ItemSupplierPrices p = new ItemSupplierPrices();
// p.setItem(a);
// p.setAmp(a);
// p.setVmp(a.getVmp());
// itemSupplierPrices.add(p);
// }
for (ItemSupplierPrices p : itemSupplierPrices) {
p.setPp(getPharmacyBean().getLastPurchaseRate(p.getAmp()));
p.setSp(getPharmacyBean().getLastRetailRate(p.getAmp()));
}
// for (ItemSupplierPrices p : itemSupplierPrices) {
// p.setSupplier(itemDistributorsController.getDistributor(p.getAmp()));
// }
}
public void fillSuppliersForItemSupplierPrices() {
for (ItemSupplierPrices p : itemSupplierPrices) {
p.setSupplier(itemDistributorsController.getDistributor(p.getAmp()));
}
}
public List<Amp> getListToRemove() {
if (listToRemove == null) {
listToRemove = new ArrayList<>();
}
return listToRemove;
}
public void setListToRemove(List<Amp> listToRemove) {
this.listToRemove = listToRemove;
}
public BillNumberGenerator getBillNumberBean() {
return billNumberBean;
}
public void setBillNumberBean(BillNumberGenerator billNumberBean) {
this.billNumberBean = billNumberBean;
}
public PharmacyBean getPharmacyBean() {
return pharmacyBean;
}
public void setPharmacyBean(PharmacyBean pharmacyBean) {
this.pharmacyBean = pharmacyBean;
}
public StockFacade getStockFacade() {
return stockFacade;
}
public void setStockFacade(StockFacade stockFacade) {
this.stockFacade = stockFacade;
}
public List<Amp> getItemList() {
return itemList;
}
public void setItemList(List<Amp> itemList) {
this.itemList = itemList;
}
public double fetchStockQty(Item item) {
String sql;
Map m = new HashMap();
m.put("i", item);
sql = "select sum(s.stock) from Stock s where s.itemBatch.item=:i";
return getStockFacade().findDoubleByJpql(sql, m);
}
public void removeSelectedItems() {
for (Amp s : getListToRemove()) {
double qty = fetchStockQty(s);
if (qty != 0) {
JsfUtil.addErrorMessage(s.getName() + " NOT Removed Beacause there is stock");
continue;
}
s.setRetired(true);
s.setRetireComments("Bulk Remove");
s.setRetirer(getSessionController().getLoggedUser());
getFacade().edit(s);
}
listToRemove = null;
createItemList();
}
public void createMedicineList() {
Map m = new HashMap();
m.put("dep", DepartmentType.Pharmacy);
String sql = "select c from PharmaceuticalItem c "
+ " where c.retired=false "
+ " and (c.departmentType is null "
+ " or c.departmentType=:dep) "
+ " order by c.name";
items = getFacade().findByJpql(sql, m);
}
public void createItemList() {
Map m = new HashMap();
m.put("dep", DepartmentType.Pharmacy);
String sql = "select c from Amp c "
+ " where c.retired=false "
+ " and (c.departmentType is null "
+ " or c.departmentType=:dep) "
+ " order by c.name";
items = getFacade().findByJpql(sql, m);
}
public void createItemListPharmacy() {
Map m = new HashMap();
m.put("dep", DepartmentType.Store);
m.put("dep2", DepartmentType.Inventry);
String sql = "select c from Amp c "
+ " where c.retired=false "
+ " and (c.departmentType is null "
+ " or c.departmentType!=:dep "
+ " or c.departmentType!=:dep2 )"
+ " order by c.name ";
items = getFacade().findByJpql(sql, m);
}
public List<Amp> deleteOrNotItem(boolean b, DepartmentType dt) {
Map m = new HashMap();
String sql = " select c from Amp c where "
+ " (c.departmentType is null"
+ " or c.departmentType!=:dt )";
if (b) {
sql += " and c.retired=false ";
} else {
sql += " and c.retired=true ";
}
m.put("dt", dt);
return getFacade().findByJpql(sql, m);
}
public List<Amp> deleteOrNotStoreItem(boolean b, DepartmentType dt) {
Map m = new HashMap();
String sql = " select c from Amp c where "
+ " c.departmentType=:dt ";
if (b) {
sql += " and c.retired=false ";
} else {
sql += " and c.retired=true ";
}
m.put("dt", dt);
return getFacade().findByJpql(sql, m);
}
public void pharmacyDeleteItem() {
itemList = deleteOrNotItem(false, DepartmentType.Store);
}
public void pharmacyNoDeleteItem() {
itemList = deleteOrNotItem(true, DepartmentType.Store);
}
public void storeDeleteItem() {
itemList = deleteOrNotStoreItem(false, DepartmentType.Store);
}
public void storeNoDeleteItem() {
itemList = deleteOrNotStoreItem(true, DepartmentType.Store);
}
public void onTabChange(TabChangeEvent event) {
setTabId(event.getTab().getId());
}
public List<Amp> getSelectedItems() {
if (selectText.trim().isEmpty()) {
selectedItems = getFacade().findByJpql("select c from Amp c where c.retired=false order by c.name");
} else {
selectedItems = getFacade().findByJpql("select c from Amp c where c.retired=false and (c.name) like '%" + getSelectText().toUpperCase() + "%' order by c.name");
}
return selectedItems;
}
public List<Amp> completeAmp(String qry) {
List<Amp> a = null;
Map m = new HashMap();
m.put("n", "%" + qry + "%");
m.put("dep", DepartmentType.Store);
if (qry != null) {
a = getFacade().findByJpql("select c from Amp c where "
+ " c.retired=false and (c.departmentType!=:dep or c.departmentType is null) "
+ " and ((c.name) like :n or (c.code) "
+ "like :n or (c.barcode) like :n) order by c.name", m, 30);
}
if (a == null) {
a = new ArrayList<>();
}
return a;
}
List<Amp> ampList = null;
public List<Amp> completeAmpByName(String qry) {
Map m = new HashMap();
m.put("n", "%" + qry + "%");
m.put("dep", DepartmentType.Store);
if (qry != null) {
ampList = getFacade().findByJpql("select c from Amp c where "
+ " c.retired=false and"
+ " (c.departmentType is null"
+ " or c.departmentType!=:dep )and "
+ "((c.name) like :n ) order by c.name", m, 30);
}
if (ampList == null) {
ampList = new ArrayList<>();
}
return ampList;
}
public Amp findAmpByName(String name) {
Map m = new HashMap();
m.put("n", name);
if (name == null || name.trim().isEmpty()) {
return null;
}
String jpql = "select c "
+ " from Amp c "
+ " where "
+ " c.retired=:ret"
+ " and c.name=:n";
m.put("ret", false);
m.put("n", name);
Amp amp = getFacade().findFirstByJpql(jpql, m);
return amp;
}
public List<Vmp> completeVmpByName(String qry) {
List<Vmp> vmps = new ArrayList<>();
Map m = new HashMap();
m.put("n", "%" + qry + "%");
m.put("dep", DepartmentType.Store);
if (qry != null) {
vmps = getVmpFacade().findByJpql("select c from Vmp c where "
+ " c.retired=false and"
+ " (c.departmentType is null"
+ " or c.departmentType!=:dep )and "
+ "((c.name) like :n ) order by c.name", m, 30);
}
return vmps;
}
public void prepareAddNewVmp() {
addingVtmInVmp = new VirtualProductIngredient();
}
// public List<Amp> completeAmpByCode(String qry) {
//
// Map m = new HashMap();
// m.put("n", "%" + qry + "%");
// m.put("dep", DepartmentType.Store);
// if (qry != null) {
// ampList = getFacade().findByJpql("select c from Amp c where "
// + " c.retired=false and (c.departmentType is null or c.departmentType!=:dep) and "
// + "((c.code) like :n ) order by c.code", m, 30);
// }
// if (ampList == null) {
// ampList = new ArrayList<>();
// }
// return ampList;
// }
// public List<Amp> completeAmpByBarCode(String qry) {
//
// Map m = new HashMap();
// m.put("n", "%" + qry + "%");
// m.put("dep", DepartmentType.Store);
// String sql = "select c from Amp c where "
// + " c.retired=false and c.departmentType!=:dep and "
// + "((c.barcode) like :n ) order by c.barcode";
//
// if (qry != null) {
// ampList = getFacade().findByJpql(sql, m, 30);
// }
// if (ampList == null) {
// ampList = new ArrayList<>();
// }
// return ampList;
// }
public void prepareAdd() {
current = new Amp();
current.setItemType(ItemType.Amp);
current.setDepartmentType(DepartmentType.Pharmacy);
}
public void listnerCategorySelect() {
if (getCurrent().getCategory().getDescription() == null || getCurrent().getCategory().getDescription().isEmpty()) {
getCurrent().getCategory().setDescription(getCurrent().getName());
}
Map m = new HashMap();
String sql = "select c from Amp c "
+ " where c.retired=false"
+ " and c.category=:cat "
+ " and c.code is not null "
+ " and (c.departmentType is null "
+ " or c.departmentType=:dep) "
+ " order by c.code desc";
m.put("dep", DepartmentType.Pharmacy);
m.put("cat", getCurrent().getCategory());
Amp amp = getFacade().findFirstByJpql(sql, m);
DecimalFormat df = new DecimalFormat("0000");
if (amp != null && !amp.getCode().isEmpty()) {
String s = amp.getCode().substring(2);
int i = Integer.parseInt(s);
i++;
if (getCurrent().getId() != null) {
Amp selectedAmp = getFacade().find(getCurrent().getId());
if (!getCurrent().getCategory().equals(selectedAmp.getCategory())) {
getCurrent().setCode(getCurrent().getCategory().getDescription() + df.format(i));
} else {
getCurrent().setCode(selectedAmp.getCode());
}
} else {
getCurrent().setCode(getCurrent().getCategory().getDescription() + df.format(i));
}
} else {
getCurrent().setCode(getCurrent().getCategory().getDescription() + df.format(1));
}
}
public void setSelectedItems(List<Amp> selectedItems) {
this.selectedItems = selectedItems;
}
public String getSelectText() {
return selectText;
}
private void recreateModel() {
items = null;
}
private boolean errorCheck() {
// if (getCurrent().getInstitution() == null) {
// JsfUtil.addErrorMessage("Please Select Manufacturer");
// return true;
// }
// listnerCategorySelect();
if (current.getCategory() == null) {
// listnerCategorySelect();
JsfUtil.addErrorMessage("Please Select Category");
return true;
}
if (getTabId().equals("tabVmp")) {
if (getCurrent().getVmp() == null) {
JsfUtil.addErrorMessage("Please Select VMP");
return true;
}
}
if (getCurrent().getCode() == null || getCurrent().getCode().isEmpty()) {
JsfUtil.addErrorMessage("Code Empty.You Can't Save Item without Code.");
return true;
}
return false;
}
private boolean errorCheckForGen() {
if (addingVtmInVmp == null) {
return true;
}
if (addingVtmInVmp.getVtm() == null) {
JsfUtil.addErrorMessage("Select Vtm");
return true;
}
if (currentVmp == null) {
return true;
}
if (addingVtmInVmp.getStrength() == 0.0) {
JsfUtil.addErrorMessage("Type Strength");
return true;
}
if (currentVmp.getCategory() == null) {
JsfUtil.addErrorMessage("Select Category");
return true;
}
if (addingVtmInVmp.getStrengthUnit() == null) {
JsfUtil.addErrorMessage("Select Strenth Unit");
return true;
}
return false;
}
public String createVmpName() {
return addingVtmInVmp.getVtm().getName()
+ " " + addingVtmInVmp.getStrength()
+ " " + addingVtmInVmp.getStrengthUnit().getName()
+ " " + currentVmp.getCategory().getName();
}
public String createAmpName() {
if (getTabId().equals("tabGen")) {
return getCurrentVmp().getName();
} else {
return getCurrent().getVmp().getName();
}
}
private void saveVmp() {
if (currentVmp.getName() == null || currentVmp.getName().isEmpty()) {
currentVmp.setName(createVmpName());
}
if (currentVmp.getId() == null || currentVmp.getId() == 0) {
getVmpFacade().create(currentVmp);
} else {
getVmpFacade().edit(currentVmp);
}
}
public void save() {
if (current == null) {
JsfUtil.addErrorMessage("No AMP is selected");
return;
}
if(current.getId() != null){
if(!configOptionApplicationController.getBooleanValueByKey("Enable edit and delete AMP from Pharmacy Administration.", false)){
JsfUtil.addErrorMessage("You have no privilage to edit AMPs.");
return;
}
}
if (current.getName() == null || current.getName().isEmpty()) {
JsfUtil.addErrorMessage("Please add a name to AMP");
return;
}
int maxCodeLeanth = Integer.parseInt(configOptionApplicationController.getShortTextValueByKey("Minimum Number of Characters to Search for Item","4"));
if (current.getCode().trim().length() < maxCodeLeanth){
JsfUtil.addErrorMessage("Minimum " + maxCodeLeanth + " characters are Required for Item Code");
return;
}
if (checkItemCode(current.getCode(), current)) {
JsfUtil.addErrorMessage("This Code has Already been Used.");
return;
}
if (current.getDepartmentType() == null) {
current.setDepartmentType(DepartmentType.Pharmacy);
}
if (current.getVmp() == null) {
JsfUtil.addErrorMessage("No VMP selected");
return;
}
if (current.getCategory() == null) {
if (current.getVmp().getCategory() != null) {
current.setCategory(current.getVmp().getCategory());
JsfUtil.addSuccessMessage("Taken the category from VMP");
} else {
JsfUtil.addErrorMessage("No category");
return;
}
}
if (current.getItemType() == null) {
current.setItemType(ItemType.Amp);
}
if (getCurrent().getId() != null) {
getFacade().edit(current);
JsfUtil.addSuccessMessage("Updated Successfully.");
} else {
current.setCreatedAt(new Date());
current.setCreater(getSessionController().getLoggedUser());
getFacade().create(current);
JsfUtil.addSuccessMessage("Saved Successfully");
recreateModel();
getItems();
}
}
public boolean checkItemCode(String code, Amp savingAmp) {
if(savingAmp==null){
return false;
}
Map m = new HashMap();
String jpql = "select c from Amp c "
+ " where c.retired=false"
+ " and (c.code is not null and c.code=:icode)";
if(savingAmp.getId()!=null){
jpql +=" and c.id <> :id ";
m.put("id", savingAmp.getId());
}
m.put("icode", code);
Amp amp = getFacade().findFirstByJpql(jpql, m);
return amp != null;
}
public void saveSelected() {
if (errorCheck()) {
return;
}
//
// if (getTabId().toString().equals("tabGen")) {
// if (errorCheckForGen()) {
// return;
// }
//
// saveVmp();
// getAddingVtmInVmp().setVmp(currentVmp);
// if (getAddingVtmInVmp().getId() == null || getAddingVtmInVmp().getId() == null) {
// getVivFacade().create(getAddingVtmInVmp());
// } else {
// getVivFacade().edit(getAddingVtmInVmp());
// }
//
// getCurrent().setVmp(currentVmp);
// }
if (current.getName() == null || current.getName().isEmpty()) {
current.setName(createAmpName());
}
current.setDepartmentType(DepartmentType.Pharmacy);
if (getCurrent().getId() != null && getCurrent().getId() > 0) {
getFacade().edit(current);
JsfUtil.addSuccessMessage("Updated Successfully.");
} else {
current.setCreatedAt(new Date());
current.setCreater(getSessionController().getLoggedUser());
getFacade().create(current);
JsfUtil.addSuccessMessage("Saved Successfully");
}
recreateModel();
// getItems();
}
public void saveAmp(Amp amp) {
if (amp == null) {
return;
}
if (amp.getId() == null) {
getFacade().create(amp);
} else {
getFacade().edit(amp);
}
}
public void setSelectText(String selectText) {
this.selectText = selectText;
}
public AmpFacade getEjbFacade() {
return ejbFacade;
}
public void setEjbFacade(AmpFacade ejbFacade) {
this.ejbFacade = ejbFacade;
}
public SessionController getSessionController() {
return sessionController;
}
public void setSessionController(SessionController sessionController) {
this.sessionController = sessionController;
}
public AmpController() {
}
public Amp getCurrent() {
if (current == null) {
current = new Amp();
}
return current;
}
public void setCurrent(Amp current) {
this.current = current;
currentVmp = new Vmp();
addingVtmInVmp = new VirtualProductIngredient();
}
public void delete() {
if (current != null) {
current.setRetired(true);
current.setRetiredAt(new Date());
current.setRetirer(getSessionController().getLoggedUser());
getFacade().edit(current);
JsfUtil.addSuccessMessage("Deleted Successfully");
} else {
JsfUtil.addSuccessMessage("Nothing to Delete");
}
recreateModel();
getItems();
current = null;
getCurrent();
}
private AmpFacade getFacade() {
return ejbFacade;
}
private List<Amp> filteredItems;
public List<Amp> getItems() {
if (items == null) {
items = findItems();
}
return items;
}
public List<Amp> findItems() {
String jpql = "select i "
+ " from Amp i "
+ " where i.retired=:ret"
+ " order by i.name";
Map m = new HashMap();
m.put("ret", false);
return getFacade().findByJpql(jpql, m);
}
public List<Amp> getLongCodeItems() {
List<Amp> lst;
String sql;
sql = "select a from Amp a where a.retired=false and length(a.code) > 5";
lst = getFacade().findByJpql(sql);
return lst;
}
public Vtm getVtm() {
return vtm;
}
public void setVtm(Vtm vtm) {
this.vtm = vtm;
}
public String getTabId() {
return tabId;
}
public void setTabId(String tabId) {
this.tabId = tabId;
}
public VirtualProductIngredient getAddingVtmInVmp() {
if (addingVtmInVmp == null) {
addingVtmInVmp = new VirtualProductIngredient();
}
return addingVtmInVmp;
}
public void setAddingVtmInVmp(VirtualProductIngredient addingVtmInVmp) {
this.addingVtmInVmp = addingVtmInVmp;
}
public Vmp getCurrentVmp() {
if (currentVmp == null) {
currentVmp = new Vmp();
}
return currentVmp;
}
public void setCurrentVmp(Vmp currentVmp) {
this.currentVmp = currentVmp;
getCurrent().setVmp(currentVmp);
}
public VmpFacade getVmpFacade() {
return vmpFacade;
}
public void setVmpFacade(VmpFacade vmpFacade) {
this.vmpFacade = vmpFacade;
}
public VirtualProductIngredientFacade getVivFacade() {
return vivFacade;
}
public void setVivFacade(VirtualProductIngredientFacade vivFacade) {
this.vivFacade = vivFacade;
}
public List<Amp> getFilteredItems() {
return filteredItems;
}
public void setFilteredItems(List<Amp> filteredItems) {
this.filteredItems = filteredItems;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public List<ItemSupplierPrices> getItemSupplierPrices() {
return itemSupplierPrices;
}
public void setItemSupplierPrices(List<ItemSupplierPrices> itemSupplierPrices) {
this.itemSupplierPrices = itemSupplierPrices;
}
public void setItems(List<Amp> items) {
this.items = items;
}
public ConfigOptionApplicationController getConfigOptionApplicationController() {
return configOptionApplicationController;
}
public void setConfigOptionApplicationController(ConfigOptionApplicationController configOptionApplicationController) {
this.configOptionApplicationController = configOptionApplicationController;
}
/**
*
*/
@FacesConverter(forClass = Amp.class)
public static class AmpConverter implements Converter {
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.isEmpty()) {
return null;
}
AmpController controller = (AmpController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "ampController");
return controller.getEjbFacade().find(getKey(value));
}
java.lang.Long getKey(String value) {
long key;
try {
key = Long.parseLong(value);
} catch (Exception e) {
key = 0L;
}
return key;
}
String getStringKey(java.lang.Long value) {
return String.valueOf(value);
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof Amp) {
Amp o = (Amp) object;
return getStringKey(o.getId());