7
7
import org .openmrs .api .impl .BaseOpenmrsService ;
8
8
import org .openmrs .attribute .AttributeType ;
9
9
import org .openmrs .module .icare .ICareConfig ;
10
+ import org .openmrs .module .icare .billing .Utils .PaymentStatus ;
10
11
import org .openmrs .module .icare .billing .dao .DiscountDAO ;
11
12
import org .openmrs .module .icare .billing .dao .InvoiceDAO ;
12
13
import org .openmrs .module .icare .billing .dao .PaymentDAO ;
@@ -76,7 +77,7 @@ public List<Invoice> getPendingInvoices(String patientUuid) {
76
77
}
77
78
}
78
79
return invoices ;
79
- //return this.invoiceDAO.findByPatientUuid(patientUuid);
80
+ // return this.invoiceDAO.findByPatientUuid(patientUuid);
80
81
}
81
82
82
83
@ Override
@@ -94,7 +95,7 @@ private InvoiceItem getInvoiceItem(OrderMetaData orderMetaData) {
94
95
Prescription drugOrder = (Prescription ) orderMetaData .getOrder ();
95
96
invoiceItem .setQuantity (drugOrder .getQuantity ());
96
97
} else if (orderMetaData .getOrder () instanceof TestOrder ) {
97
- //TestOrder testOrder = (TestOrder) orderMetaData.getOrder();
98
+ // TestOrder testOrder = (TestOrder) orderMetaData.getOrder();
98
99
invoiceItem .setQuantity (1.0 );
99
100
} else {
100
101
invoiceItem .setQuantity (1.0 );
@@ -122,7 +123,7 @@ private InvoiceItem getTopUpInvoiceItem(OrderMetaData orderMetaData) {
122
123
Prescription drugOrder = (Prescription ) orderMetaData .getOrder ();
123
124
invoiceItem .setQuantity (drugOrder .getQuantity ());
124
125
} else if (orderMetaData .getOrder () instanceof TestOrder ) {
125
- //TestOrder testOrder = (TestOrder) orderMetaData.getOrder();
126
+ // TestOrder testOrder = (TestOrder) orderMetaData.getOrder();
126
127
invoiceItem .setQuantity (1.0 );
127
128
} else {
128
129
invoiceItem .setQuantity (1.0 );
@@ -214,7 +215,7 @@ public <T extends Order> Order processOrder(OrderMetaData<T> orderMetaData, Doub
214
215
}
215
216
}
216
217
217
- //Automatic discount creation for full exempted discounts
218
+ // Automatic discount creation for full exempted discounts
218
219
List <DiscountInvoiceItem > discountInvoiceItems = existingInvoice .getDiscountItems ();
219
220
Boolean isFullExemptedCheck = false ;
220
221
@@ -230,7 +231,7 @@ public <T extends Order> Order processOrder(OrderMetaData<T> orderMetaData, Doub
230
231
231
232
if (isFullExemptedCheck ) {
232
233
for (InvoiceItem invoiceItem : existingInvoice .getInvoiceItems ()) {
233
- //Find the coresponding discount item
234
+ // Find the coresponding discount item
234
235
boolean found = false ;
235
236
for (DiscountInvoiceItem discountItem : discountInvoiceItems ) {
236
237
if (discountItem .getItem ().getUuid ().equals (invoiceItem .getItem ().getUuid ())) {
@@ -300,7 +301,7 @@ public List<Payment> getPatientPayments(String patientUuid) {
300
301
@ Override
301
302
public Payment confirmPayment (Payment payment ) throws Exception {
302
303
Invoice invoice = invoiceDAO .findByUuid (payment .getInvoice ().getUuid ());
303
- //TODO Payments should address particular item prices in the invoice
304
+ // TODO Payments should address particular item prices in the invoice
304
305
payment .setInvoice (invoice );
305
306
Concept paymentType = Context .getService (ConceptService .class ).getConceptByUuid (payment .getPaymentType ().getUuid ());
306
307
if (paymentType == null ) {
@@ -325,8 +326,10 @@ public Payment confirmPayment(Payment payment) throws Exception {
325
326
}
326
327
item .setItem (invoiceItem .getItem ());
327
328
item .setOrder (invoiceItem .getOrder ());
329
+ item .setStatus (PaymentStatus .PAID );
328
330
}
329
331
payment .setReceivedBy (Context .getAuthenticatedUser ().getName ());
332
+ payment .setStatus (PaymentStatus .PAID );
330
333
return this .paymentDAO .save (payment );
331
334
}
332
335
@@ -370,7 +373,7 @@ public Discount discountInvoice(Discount discount) throws Exception {
370
373
}
371
374
discount .setAttachment (obs );
372
375
}
373
- //discount.setCreator(Context.getAuthenticatedUser());
376
+ // discount.setCreator(Context.getAuthenticatedUser());
374
377
375
378
return discountDAO .save (discount );
376
379
}
@@ -427,7 +430,7 @@ public VisitMetaData validateVisitMetaData(VisitWrapper visit) throws Exception
427
430
+ ICareConfig .CONSULTATION_ORDER_TYPE + "'" );
428
431
}
429
432
430
- //Validate the visit with required attributes
433
+ // Validate the visit with required attributes
431
434
if (visit .getServiceConceptUuid () == null ) {
432
435
throw new VisitInvalidException ("Service has not been specified in the visit" );
433
436
}
@@ -516,7 +519,7 @@ public VisitMetaData validateVisitMetaData(VisitWrapper visit) throws Exception
516
519
+ "'." );
517
520
}
518
521
visitMetaData .setConsultationOrderType (consultationOrderType );
519
- //Check the existance of Item Prices
522
+ // Check the existance of Item Prices
520
523
ICareService iCareService = Context .getService (ICareService .class );
521
524
ItemPrice serviceItemPrice = iCareService .getItemPriceByConceptId (serviceConcept .getId (), visit .getPaymentScheme ()
522
525
.getId (), paymentTypeConcept .getId ());
@@ -543,13 +546,13 @@ public VisitMetaData validateVisitMetaData(VisitWrapper visit) throws Exception
543
546
public void processVisit (VisitWrapper visitWrapper , VisitMetaData visitMetaData ) throws VisitInvalidException ,
544
547
ConfigurationException {
545
548
546
- //Start Creating Invoice
549
+ // Start Creating Invoice
547
550
Patient patient = visitWrapper .getVisit ().getPatient ();
548
551
Invoice invoice = new Invoice ();
549
552
invoice .setVisit (visitWrapper .getVisit ());
550
- //invoice.setPatient(patient);
553
+ // invoice.setPatient(patient);
551
554
552
- //Create a patient encounter at the registration
555
+ // Create a patient encounter at the registration
553
556
EncounterService encounterService = Context .getService (EncounterService .class );
554
557
Encounter encounter = new Encounter ();
555
558
encounter .setVisit (visitWrapper .getVisit ());
@@ -571,16 +574,18 @@ public void processVisit(VisitWrapper visitWrapper, VisitMetaData visitMetaData)
571
574
visitWrapper .getVisit ().addEncounter (encounter );
572
575
encounterService .saveEncounter (encounter );
573
576
574
- //Get concepts for payment type and payment scheme to assosciate with item price
577
+ // Get concepts for payment type and payment scheme to assosciate with item
578
+ // price
575
579
576
580
OrderService orderService = Context .getService (OrderService .class );
577
581
VisitService visitService = Context .getService (VisitService .class );
578
- //List<Visit> visits = visitService.getVisitsByPatient(patient);
582
+ // List<Visit> visits = visitService.getVisitsByPatient(patient);
579
583
Date newDate = new Date (System .currentTimeMillis () - TimeUnit .HOURS .toMillis (Integer .valueOf ("24" )));
580
- //If this is the first visit for this patient create a registration fee order
581
- //Check if patient existed in the system to determine whether they should be billed for Registration Fee
584
+ // If this is the first visit for this patient create a registration fee order
585
+ // Check if patient existed in the system to determine whether they should be
586
+ // billed for Registration Fee
582
587
if (patient .getDateCreated ().after (newDate )) {
583
- //TODO Soft code the UUID for fee concept
588
+ // TODO Soft code the UUID for fee concept
584
589
Concept feeConcept = visitMetaData .getRegistrationFeeConcept ();
585
590
586
591
Order order = new Order ();
@@ -592,13 +597,13 @@ public void processVisit(VisitWrapper visitWrapper, VisitMetaData visitMetaData)
592
597
593
598
OrderContext orderContext = new OrderContext ();
594
599
595
- //TODO Softcode to get the current care setting of the visit
600
+ // TODO Softcode to get the current care setting of the visit
596
601
orderContext .setCareSetting (orderService .getCareSetting (1 ));
597
602
encounter .addOrder (order );
598
603
orderService .saveOrder (order , orderContext );
599
604
}
600
605
601
- //Get concepts for service
606
+ // Get concepts for service
602
607
Concept serviceConcept = visitMetaData .getServiceConcept ();
603
608
604
609
Order order = new Order ();
@@ -609,16 +614,16 @@ public void processVisit(VisitWrapper visitWrapper, VisitMetaData visitMetaData)
609
614
order .setOrderType (visitMetaData .getConsultationOrderType ());
610
615
611
616
OrderContext orderContext = new OrderContext ();
612
- //TODO Softcode to get the current care setting of the visit
617
+ // TODO Softcode to get the current care setting of the visit
613
618
orderContext .setCareSetting (orderService .getCareSetting (1 ));
614
619
encounter .addOrder (order );
615
620
orderService .saveOrder (order , orderContext );
616
621
}
617
622
618
623
@ Override
619
624
public Visit createVisit (MethodInvocation invocation ) throws Throwable {
620
- //Visit visit = (Visit) invocation.getArguments()[0];
621
- //VisitWrapper visit = new VisitWrapper((Visit) invocation.getArguments()[0]);
625
+ // Visit visit = (Visit) invocation.getArguments()[0];
626
+ // VisitWrapper visit = new VisitWrapper((Visit) invocation.getArguments()[0]);
622
627
VisitWrapper visitWrapper = new VisitWrapper ((Visit ) invocation .getArguments ()[0 ]);
623
628
VisitService visitService = Context .getVisitService ();
624
629
Visit existingVisit = visitService .getVisitByUuid (visitWrapper .getVisit ().getUuid ());
@@ -734,4 +739,122 @@ public Order createOrderForOngoingDeceasedPatients() throws Exception {
734
739
public List <Object []> getTotalAmountFromPaidInvoices (Date startDate , Date endDate , String provider ) throws Exception {
735
740
return this .invoiceDAO .getTotalAmountFromPaidInvoices (startDate , endDate , provider );
736
741
}
742
+
743
+ @ Override
744
+ public Map <String , Object > processGepgCallbackResponse (Map <String , Object > callbackData ) throws Exception {
745
+
746
+ // System.out.println("Processing callback data: " + callbackData);
747
+
748
+ Map <String , Object > response = new HashMap <>();
749
+
750
+ try {
751
+ if (callbackData .containsKey ("Status" ) && callbackData .containsKey ("FeedbackData" )) {
752
+ Map <String , Object > status = (Map <String , Object >) callbackData .get ("Status" );
753
+ Map <String , Object > feedbackData = (Map <String , Object >) callbackData .get ("FeedbackData" );
754
+
755
+ Map <String , Object > gepgBillSubResp = (Map <String , Object >) feedbackData .get ("gepgBillSubResp" );
756
+ Map <String , Object > billTrxInf = (Map <String , Object >) gepgBillSubResp .get ("BillTrxInf" );
757
+
758
+ String billId = (String ) billTrxInf .get ("BillId" );
759
+ String payCntrNum = (String ) billTrxInf .get ("PayCntrNum" );
760
+ String requestId = (String ) status .get ("RequestId" );
761
+
762
+ // 1. Get invoice from bill
763
+ Invoice invoice = invoiceDAO .findByUuid (billId );
764
+ if (invoice == null ) {
765
+ throw new Exception ("Bill id " + billId + " is not valid" );
766
+ }
767
+ String paymentTypeConceptUuid = Context .getAdministrationService ()
768
+ .getGlobalProperty (ICareConfig .DEFAULT_PAYMENT_TYPE_VIA_CONTROL_NUMBER );
769
+ if (paymentTypeConceptUuid == null ) {
770
+ throw new Exception ("No default payment type based on control number" );
771
+ }
772
+ Concept paymentType = Context .getConceptService ().getConceptByUuid (paymentTypeConceptUuid );
773
+ if (paymentType == null ) {
774
+ throw new Exception ("Payment type concept not found for UUID: " + paymentTypeConceptUuid );
775
+ }
776
+
777
+ Payment payment = new Payment ();
778
+ payment .setPaymentType (paymentType );
779
+ payment .setReferenceNumber (payCntrNum );
780
+ payment .setInvoice (invoice );
781
+
782
+ // Payment Items
783
+ List <PaymentItem > paymentItems = new ArrayList <PaymentItem >();
784
+ for (InvoiceItem invoiceItem : invoice .getInvoiceItems ()) {
785
+ PaymentItem paymentItem = new PaymentItem ();
786
+ paymentItem .setAmount (invoiceItem .getPrice ());
787
+ paymentItem .setOrder (invoiceItem .getOrder ());
788
+ paymentItem .setItem (invoiceItem .getItem ());
789
+ paymentItem .setStatus (PaymentStatus .REQUESTED );
790
+ paymentItems .add (paymentItem );
791
+ }
792
+ // Payment.setItems(paymentItems);
793
+ payment .setReceivedBy ("SYSTEM" );
794
+ payment .setStatus (PaymentStatus .REQUESTED );
795
+ payment .setCreator (Context .getAuthenticatedUser ());
796
+ payment .setUuid (requestId );
797
+ payment .setDateCreated (new Date ());
798
+ new Payment ();
799
+ boolean isUpdated = true ;
800
+ // will used to update Control Number
801
+ // boolean isUpdated = icareService.updateGepgControlNumber(payCntrNum, billId);
802
+
803
+ if (isUpdated ) {
804
+ // Save control number in global property
805
+ Payment savedPayment = this .paymentDAO .save (payment );
806
+ response .put ("referenceNumber" , payCntrNum );
807
+ System .out .println (savedPayment .getUuid ());
808
+ GlobalProperty globalProperty = new GlobalProperty ();
809
+ AdministrationService administrationService = Context .getAdministrationService ();
810
+ globalProperty .setProperty ("gepg.updatedInvoiceItem.icareConnect" );
811
+ // globalProperty.setPropertyValue("Success Control NUmber saved with payment
812
+ // control number "+ savedPayment.getReferenceNumber() + " and uuid " +
813
+ // savedPayment.getUuid());
814
+ administrationService .saveGlobalProperty (globalProperty );
815
+
816
+ response .put ("status" , "Success" );
817
+ response .put ("message" , "Callback processed and control number updated received" );
818
+ } else {
819
+ response .put ("status" , "Error" );
820
+ response .put ("message" , "Failed to update control number for this BillId: " + billId );
821
+ }
822
+ } else {
823
+ System .out .println ("Status or FeedbackData field not found in callback data" );
824
+ response .put ("status" , "error" );
825
+ response .put ("message" , "Status or FeedbackData field not found in callback data" );
826
+ }
827
+ } catch (Exception e ) {
828
+ e .printStackTrace ();
829
+ response .put ("status" , "error" );
830
+ response .put ("message" , "Internal server error" );
831
+ response .put ("error" , e .getMessage ());
832
+ }
833
+ return response ;
834
+ }
835
+
836
+ public String fetchControlNumber (String requestId ) throws Exception {
837
+ String controlNumber = null ;
838
+ long startTime = System .currentTimeMillis ();
839
+ long timeout = 32000 ;
840
+
841
+ while (System .currentTimeMillis () - startTime < timeout ) {
842
+ controlNumber = paymentDAO .getReferenceNumberByRequestId (requestId );
843
+ AdministrationService administrationService = Context .getAdministrationService ();
844
+ GlobalProperty globalProperty = new GlobalProperty ();
845
+ globalProperty .setProperty ("gepg.controlNumberRes.icareConnect" );
846
+ globalProperty .setPropertyValue (controlNumber );
847
+ administrationService .saveGlobalProperty (globalProperty );
848
+ if (controlNumber != null ) {
849
+ break ;
850
+ }
851
+ try {
852
+ Thread .sleep (4000 );
853
+ }
854
+ catch (InterruptedException e ) {
855
+ Thread .currentThread ().interrupt ();
856
+ }
857
+ }
858
+ return controlNumber ;
859
+ }
737
860
}
0 commit comments