11package org .openimis .imispolicies ;
22
3+ import android .content .Intent ;
34import android .os .Bundle ;
5+ import android .view .MenuItem ;
6+ import android .view .View ;
7+ import android .widget .Button ;
8+ import android .widget .TextView ;
49
510import androidx .activity .EdgeToEdge ;
11+ import androidx .annotation .NonNull ;
612import androidx .appcompat .app .AppCompatActivity ;
713import androidx .core .graphics .Insets ;
814import androidx .core .view .ViewCompat ;
915import androidx .core .view .WindowInsetsCompat ;
16+ import androidx .fragment .app .FragmentActivity ;
17+ import androidx .fragment .app .FragmentManager ;
18+
19+ import com .google .android .material .button .MaterialButton ;
20+ import com .google .android .material .datepicker .MaterialDatePicker ;
21+ import com .google .android .material .textfield .MaterialAutoCompleteTextView ;
22+ import com .google .android .material .textfield .TextInputEditText ;
23+ import com .google .android .material .textfield .TextInputLayout ;
24+
25+ import org .json .JSONArray ;
26+ import org .json .JSONException ;
27+ import org .json .JSONObject ;
28+ import org .openimis .imispolicies .tools .Log ;
29+ import org .openimis .imispolicies .util .JsonDropdownHelper ;
30+
31+ import java .text .ParseException ;
32+ import java .text .SimpleDateFormat ;
33+ import java .util .Calendar ;
34+ import java .util .Date ;
35+ import java .util .Locale ;
1036
1137public class PolicyActivity extends AppCompatActivity {
1238
39+ private TextInputLayout layoutEnrolmentDate , layoutProduct , layoutEffectiveDate , layoutStartDate , layoutExpiryDate , layoutControlNumber ;
40+ private TextView txtPolicyStatus , spPolicyValue , spContribution , spBalance ;
41+ private MaterialButton btnSave ;
42+ private MaterialAutoCompleteTextView ddlProduct ;
43+ private TextInputEditText txtEnrolmentDate , txtEffectiveDate , txtStartDate , txtExpiryDate , AssignedControlNumber ;
44+ private JSONObject policyObject ;
45+ private int policyId = 0 ;
46+ private int familyId ;
47+ ClientAndroidInterface ca ;
48+ boolean hasCycle ;
49+ private int officerId , regionId , districtId , productId ;
50+ private MaterialDatePicker <Long > datePicker ;
51+ private int isOffline = 1 ;
52+
53+
1354 @ Override
1455 protected void onCreate (Bundle savedInstanceState ) {
1556 super .onCreate (savedInstanceState );
16- EdgeToEdge .enable (this );
1757 setContentView (R .layout .activity_policy );
18- ViewCompat .setOnApplyWindowInsetsListener (findViewById (R .id .main ), (v , insets ) -> {
19- Insets systemBars = insets .getInsets (WindowInsetsCompat .Type .systemBars ());
20- v .setPadding (systemBars .left , systemBars .top , systemBars .right , systemBars .bottom );
21- return insets ;
58+ setTitle (getResources ().getString (R .string .AddEditPolicy ));
59+ if (getSupportActionBar () != null ) {
60+ getSupportActionBar ().setDisplayHomeAsUpEnabled (true );
61+ }
62+ initViews ();
63+ setupListeners ();
64+ setupDatePicker ();
65+ defineRequiredField ();
66+ canSave ();
67+ policyObject = new JSONObject ();
68+ ca = new ClientAndroidInterface (this );
69+ policyId = getIntent ().getIntExtra ("PolicyId" , 0 );
70+ familyId = getIntent ().getIntExtra ("FamilyId" , 0 );
71+ regionId = getIntent ().getIntExtra ("RegionId" , 0 );
72+ districtId = getIntent ().getIntExtra ("DistrictId" , 0 );
73+
74+ if (!ca .IsBulkCNUsed ()){
75+ layoutControlNumber .setVisibility (View .GONE );
76+ }
77+ layoutEffectiveDate .setEnabled (false );
78+ txtExpiryDate .setEnabled (false );
79+ officerId = ca .getOfficerId ();
80+
81+ loadProducts (regionId , districtId , null );
82+ }
83+
84+ private void initViews (){
85+ layoutEnrolmentDate = findViewById (R .id .layoutEnrolmentDate );
86+ layoutProduct = findViewById (R .id .layoutProduct );
87+ layoutEffectiveDate = findViewById (R .id .layoutEffectiveDate );
88+ layoutStartDate = findViewById (R .id .layoutStartDate );
89+ layoutExpiryDate = findViewById (R .id .layoutExpiryDate );
90+ layoutControlNumber = findViewById (R .id .layoutControlNumber );
91+ txtPolicyStatus = findViewById (R .id .txtPolicyStatus );
92+ spPolicyValue = findViewById (R .id .spPolicyValue );
93+ spContribution = findViewById (R .id .spContribution );
94+ spBalance = findViewById (R .id .spBalance );
95+ btnSave = findViewById (R .id .btnSavePolicy );
96+ ddlProduct = findViewById (R .id .ddlProduct );
97+ txtEnrolmentDate = findViewById (R .id .txtEnrolmentDate );
98+ txtEffectiveDate = findViewById (R .id .txtEffectiveDate );
99+ txtStartDate = findViewById (R .id .txtStartDate );
100+ txtExpiryDate = findViewById (R .id .txtExpiryDate );
101+ AssignedControlNumber = findViewById (R .id .AssignedControlNumber );
102+ }
103+
104+ private void setupListeners (){
105+ txtEnrolmentDate .setOnClickListener (v -> {
106+ datePicker .show (getSupportFragmentManager (), "DATE_PICKER" );
107+ });
108+
109+ btnSave .setOnClickListener (new View .OnClickListener () {
110+ @ Override
111+ public void onClick (View view ) {
112+ savePolicy ();
113+ }
114+ });
115+ }
116+
117+ private void defineRequiredField (){
118+ layoutEnrolmentDate .setError (" " );
119+ layoutProduct .setError (" " );
120+ layoutStartDate .setError (" " );
121+ layoutExpiryDate .setError (" " );
122+ }
123+
124+ private void canSave (){
125+ if (txtEnrolmentDate .getText ().toString ().isEmpty () ||
126+ txtStartDate .getText ().toString ().isEmpty () ||
127+ txtExpiryDate .getText ().toString ().isEmpty () ||
128+ ddlProduct .getText ().toString ().isEmpty ()
129+ ){
130+ btnSave .setEnabled (false );
131+ } else {
132+ btnSave .setEnabled (true );
133+ }
134+ }
135+
136+ private void loadProducts (int regionId , int districtId , String enrolmentDate ){
137+ try {
138+ String products = ca .getProducts (regionId , districtId , enrolmentDate );
139+ JSONArray productsArray = new JSONArray (products );
140+ JsonDropdownHelper .bindDropdown (
141+ this ,
142+ ddlProduct ,
143+ productsArray ,
144+ "ProductNameCombined" ,
145+ null ,
146+ new JsonDropdownHelper .OnJsonItemSelectedListener () {
147+ @ Override
148+ public void onItemSelected (JSONObject selectedItem , int position ) {
149+ if (selectedItem != null ) {
150+ try {
151+ productId = selectedItem .getInt ("ProdId" );
152+ String enrolDate = txtEnrolmentDate .getText ().toString ().trim ();
153+ policyObject .put ("ddlProduct" , productId );
154+ getPolicyPeriod (enrolDate , productId , familyId , policyId );
155+ canSave ();
156+
157+ if (ca .IsBulkCNUsed ()) {
158+ if (productId == 0 ) {
159+ AssignedControlNumber .setText ("" );
160+ }
161+ String controlNumber = ca .GetNextBulkCn (String .valueOf (productId ));
162+ if (controlNumber .equals ("undefined" )) {
163+ ca .ShowDialog (getResources ().getString (R .string .noBulkCNAvailable ));
164+ AssignedControlNumber .setText ("" );
165+ } else {
166+ AssignedControlNumber .setText ("" );
167+ }
168+ }
169+ } catch (JSONException e ) {
170+ e .printStackTrace ();
171+ }
172+ }
173+ }
174+ }
175+ );
176+ } catch (JSONException e ) {
177+ throw new RuntimeException (e );
178+ }
179+ }
180+
181+ private void setupDatePicker () {
182+ // Create Material Date Picker
183+ MaterialDatePicker .Builder <Long > builder = MaterialDatePicker .Builder .datePicker ();
184+ builder .setTitleText (getResources ().getString (R .string .EnrolmentDate ));
185+ builder .setSelection (MaterialDatePicker .todayInUtcMilliseconds ());
186+
187+ datePicker = builder .build ();
188+
189+ datePicker .addOnPositiveButtonClickListener (selection -> {
190+ Calendar calendar = Calendar .getInstance ();
191+ calendar .setTimeInMillis (selection );
192+ SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd" , Locale .getDefault ());
193+ txtEnrolmentDate .setText (format .format (calendar .getTime ()));
194+ String enrolDate = txtEnrolmentDate .getText ().toString ().trim ();
195+ loadProducts (regionId , districtId , enrolDate );
196+ if (productId != 0 ){
197+ getPolicyPeriod (format .format (calendar .getTime ()), productId , familyId , policyId );
198+ }
199+ canSave ();
200+ });
201+
202+ txtEnrolmentDate .setOnClickListener (v -> {
203+ datePicker .show (getSupportFragmentManager (), "DATE_PICKER" );
22204 });
23205 }
206+
207+ private void getPolicyPeriod (String EnrolmentDate , int ProdId , int FamilyId , int policyId ){
208+ if (EnrolmentDate .length () == 0 || ProdId == 0 ){
209+ return ;
210+ }
211+ try {
212+ String period = ca .getPolicyPeriod (ProdId , EnrolmentDate );
213+ JSONArray periodArray = new JSONArray (period );
214+ String startDate = periodArray .getJSONObject (0 ).getString ("StartDate" );
215+ String expiryDate = periodArray .getJSONObject (0 ).getString ("ExpiryDate" );
216+ hasCycle = periodArray .getJSONObject (0 ).getBoolean ("HasCycle" );
217+
218+ txtStartDate .setText (startDate );
219+ txtExpiryDate .setText (expiryDate );
220+
221+ txtStartDate .setEnabled (hasCycle );
222+
223+ double PolicyValue = ca .getPolicyValue (EnrolmentDate , ProdId , FamilyId , startDate , hasCycle , 0 , "N" , isOffline );
224+ spPolicyValue .setText (String .valueOf (PolicyValue ));
225+ } catch (ParseException e ) {
226+ throw new RuntimeException (e );
227+ } catch (JSONException e ) {
228+ throw new RuntimeException (e );
229+ }
230+ }
231+
232+ private void savePolicy (){
233+ try {
234+ policyObject .put ("ddlOfficer" ,officerId );
235+ policyObject .put ("AssignedControlNumber" , AssignedControlNumber .getText ());
236+ policyObject .put ("hfPolicyValue" , spPolicyValue .getText ());
237+ policyObject .put ("hfPolicyStatus" , txtPolicyStatus .getText ());
238+ policyObject .put ("txtExpiryDate" , txtExpiryDate .getText ());
239+ policyObject .put ("txtEnrolmentDate" , txtEnrolmentDate .getText ());
240+ policyObject .put ("txtStartDate" , txtStartDate .getText ());
241+ policyObject .put ("txtEffectiveDate" , txtEffectiveDate .getText ());
242+
243+ policyId = ca .SavePolicy (policyObject .toString (), familyId , policyId );
244+ if (policyId > 0 ){
245+ FragmentActivity activity = (FragmentActivity ) this ;
246+ FragmentManager fm = activity .getSupportFragmentManager ();
247+ Bundle result = new Bundle ();
248+ result .putBoolean ("refresh" , true );
249+ fm .setFragmentResult ("requestKey" , result );
250+ finish ();
251+ }
252+ } catch (Exception e ) {
253+ throw new RuntimeException (e );
254+ }
255+ }
256+
257+ @ Override
258+ public boolean onOptionsItemSelected (@ NonNull MenuItem item ) {
259+ if (item .getItemId () == android .R .id .home ) {
260+ finish ();
261+ return true ;
262+ }
263+ return super .onOptionsItemSelected (item );
264+ }
24265}
0 commit comments