Skip to content

Commit

Permalink
add fix for calendar id usage for sync purpose.
Browse files Browse the repository at this point in the history
  • Loading branch information
Satya Narayan committed Jul 28, 2014
1 parent 7fdc13b commit a416492
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.ButterKnife;
import butterknife.InjectView;
import com.mifos.mifosxdroid.R;
import com.mifos.objects.db.Loan;

import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;


public class ClientListAdapter extends BaseAdapter {

Expand Down Expand Up @@ -114,13 +112,17 @@ public void onFocusChange(View v, boolean hasFocus) {
try {
double changedValue = Double.parseDouble(((EditText) v).getText().toString());
listPaidAmounts.put(loan, changedValue);
loan.totalDue = changedValue;
loan.save();
notifyDataSetChanged();
if (loan.totalDue != changedValue) {
loan.totalDue = changedValue;
loan.save();
notifyDataSetChanged();
}
} catch (NumberFormatException e) {
Toast.makeText(context, context.getString(R.string.error_invalid_amount), Toast.LENGTH_SHORT).show();
}
}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,28 @@
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import android.view.*;
import android.widget.*;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.mifos.mifosxdroid.GroupActivity;
import com.mifos.mifosxdroid.LoginActivity;
import com.mifos.mifosxdroid.OfflineCenterInputActivity;
import com.mifos.mifosxdroid.R;
import com.mifos.mifosxdroid.adapters.CenterAdapter;
import com.mifos.objects.db.AttendanceType;
import com.mifos.objects.db.Client;
import com.mifos.objects.db.CollectionMeetingCalendar;
import com.mifos.objects.db.Currency;
import com.mifos.objects.db.EntityType;
import com.mifos.objects.db.Loan;
import com.mifos.objects.db.MeetingCenter;
import com.mifos.objects.db.MeetingDate;
import com.mifos.objects.db.MifosGroup;
import com.mifos.objects.db.OfflineCenter;
import com.mifos.objects.db.RepaymentTransaction;
import com.mifos.objects.db.Status;
import com.mifos.objects.db.*;
import com.mifos.services.API;
import com.mifos.utils.DateHelper;
import com.mifos.utils.Network;
import com.mifos.utils.SaveOfflineDataHelper;
import com.orm.query.Select;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

import java.util.ArrayList;
import java.util.List;


public class CenterListFragment extends Fragment implements AdapterView.OnItemClickListener,
SaveOfflineDataHelper.OfflineDataSaveListener {
Expand Down Expand Up @@ -125,13 +104,15 @@ private List<MeetingCenter> getAllCenters() {
private void getData() {

if (Network.isOnline(getActivity().getApplicationContext())) {
String dateFormant = "dd-MMM-yyyy";
String locale = "en";
SharedPreferences preferences = getActivity().getSharedPreferences(OfflineCenterInputActivity.PREF_CENTER_DETAILS, Context.MODE_PRIVATE);
int staffId = preferences.getInt(OfflineCenterInputActivity.STAFF_ID_KEY, -1);
String meetingDate = DateHelper.getDateAsStringUsedForCollectionSheetPayload(preferences.getString(OfflineCenterInputActivity.TRANSACTION_DATE_KEY, null));
int branchId = preferences.getInt(OfflineCenterInputActivity.BRANCH_ID_KEY, -1);
Log.i(TAG, "staffId:" + staffId + ", meetingDate: " + meetingDate + " , branchId:" + branchId);
//TODO -- Need to ask --- Hardcoding date format and locale
API.centerService.getCenterList("dd-MMM-yyyy", "en", meetingDate, branchId, staffId,
//TODO -- Need to ask --- Hard coding date format and locale
API.centerService.getCenterList(dateFormant, locale, meetingDate, branchId, staffId,
new Callback<List<OfflineCenter>>() {
@Override
public void success(List<OfflineCenter> centers, Response response) {
Expand All @@ -152,7 +133,10 @@ public void success(List<OfflineCenter> centers, Response response) {

@Override
public void failure(RetrofitError error) {

Toast.makeText(getActivity(), "Please login to continue", Toast.LENGTH_LONG).show();
getActivity().finish();
Intent intent = new Intent(getActivity(), LoginActivity.class);
startActivity(intent);
}
});
}
Expand Down Expand Up @@ -187,9 +171,8 @@ private void startCenterInputActivity() {
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
Toast.makeText(getActivity(), "These is no data this input, please re-enter the details", Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), "Offline data has been cleared.", Toast.LENGTH_LONG).show();
getActivity().finish();
;
startActivity(new Intent(getActivity(), OfflineCenterInputActivity.class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,10 @@
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import android.view.*;
import android.widget.*;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.mifos.mifosxdroid.ClientActivity;
import com.mifos.mifosxdroid.OfflineCenterInputActivity;
import com.mifos.mifosxdroid.R;
Expand All @@ -37,9 +29,6 @@
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;


public class GroupFragment extends Fragment implements AdapterView.OnItemClickListener, RepaymentTransactionSyncService.SyncFinishListener {

Expand Down Expand Up @@ -128,7 +117,6 @@ private void setAdapter() {

private List<MifosGroup> getAllGroups() {
return Select.from(MifosGroup.class).where(Condition.prop("center_id").eq(centerId)).list();
//return Select.from(MifosGroup.class).list();
}

@Override
Expand All @@ -143,23 +131,17 @@ public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
public void onSyncFinish(String message, boolean isSyncable) {
MenuItemCompat.setActionView(syncItem, null);
Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
if (!isSyncable) {
SharedPreferences preferences = getActivity().getSharedPreferences(OfflineCenterInputActivity.PREF_CENTER_DETAILS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
getActivity().finish();
Intent intent = new Intent(getActivity(), OfflineCenterInputActivity.class);
startActivity(intent);
} else
if (isSyncable) {
setCenterAsSynced();
}
}

private void setCenterAsSynced() {
if (centerId != -1) {
List<MeetingCenter> center = Select.from(MeetingCenter.class).where(com.orm.query.Condition.prop("center_id").eq(centerId)).list();
center.get(0).setIsSynced(1);
center.get(0).save();
getActivity().finish();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

public class CollectionMeetingCalendar extends SugarRecord<CollectionMeetingCalendar> {
private int calendarInstanceId;
private long calendarId;
private int entityId;
private EntityType entityType;
private String title;
Expand All @@ -33,6 +34,14 @@ public boolean isNew() {
return count == 0;
}

public long getCalendarId() {
return calendarId;
}

public void setCalendarId(long calendarId) {
this.calendarId = calendarId;
}

public MeetingDate getMeetingCalendarDate() {
return meetingCalendarDate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,26 @@
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;

import com.mifos.mifosxdroid.OfflineCenterInputActivity;
import com.mifos.objects.db.AttendanceType;
import com.mifos.objects.db.Client;
import com.mifos.objects.db.Currency;
import com.mifos.objects.db.Loan;
import com.mifos.objects.db.MifosGroup;
import com.mifos.objects.db.RepaymentTransaction;
import com.mifos.objects.db.*;
import com.mifos.services.data.BulkRepaymentTransactions;
import com.mifos.services.data.CollectionSheetPayload;
import com.mifos.services.data.SaveResponse;
import com.mifos.utils.Constants;
import com.mifos.utils.DateHelper;
import com.mifos.utils.Network;
import com.orm.query.Select;
import retrofit.RetrofitError;

import java.util.ArrayList;
import java.util.List;

import retrofit.RetrofitError;

public class RepaymentTransactionSyncService {

private static final String TAG = RepaymentTransactionSyncService.class.getSimpleName();
private final String tag = getClass().getSimpleName();
private SyncFinishListener syncFinishListener;
private long centerId;

public RepaymentTransactionSyncService(SyncFinishListener syncFinishListener, long centerId) {
this.syncFinishListener = syncFinishListener;
this.centerId = centerId;
Expand All @@ -43,13 +37,15 @@ public RepaymentTransactionSyncService(SyncFinishListener syncFinishListener, lo
public void syncRepayments() {
List<RepaymentTransaction> transactions = Select.from(RepaymentTransaction.class).list();
Log.i(TAG, "Fetching transactions from Database:" + transactions);

List<MeetingCenter> centerList = Select.from(MeetingCenter.class).where(com.orm.query.Condition.prop("center_id").eq(centerId)).list();
MeetingCenter center = centerList.get(0);
if (transactions.size() > 0) {
List<BulkRepaymentTransactions> repaymentTransactions = new ArrayList<BulkRepaymentTransactions>();

for (RepaymentTransaction transaction : transactions) {
Loan loan = transaction.getLoan();
if (loan.getAccountStatusId() == 300) {
if (loan.getAccountStatusId() == 300) //ToDO need to ask about hard coding
{
repaymentTransactions.add(new BulkRepaymentTransactions(loan.getLoanId(), transaction.getTransactionAmount()));
}
}
Expand All @@ -60,7 +56,8 @@ public void syncRepayments() {

CollectionSheetPayload payload = new CollectionSheetPayload();
payload.bulkRepaymentTransactions = repaymentTransactionArray;

payload.setCalendarId(center.getCollectionMeetingCalendar().getCalendarId());
payload.setTransactionDate(DateHelper.getPayloadDate());
SaveCollectionSheetTask task = new SaveCollectionSheetTask();
task.execute(payload);
} else
Expand All @@ -81,6 +78,7 @@ public interface SyncFinishListener {
}

private class SaveCollectionSheetTask extends AsyncTask<CollectionSheetPayload, Void, Void> {
private boolean isResponseNull = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
Expand All @@ -98,12 +96,12 @@ protected Void doInBackground(CollectionSheetPayload... collectionSheetPayloads)
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
// deleteAllOfflineCollectionSheetData();
}
} else
isResponseNull = true;
} catch (RetrofitError error) {

isResponseNull = true;
} catch (Exception ex) {

isResponseNull = true;
}
}
return null;
Expand All @@ -112,7 +110,10 @@ protected Void doInBackground(CollectionSheetPayload... collectionSheetPayloads)
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
syncFinishListener.onSyncFinish("Sync is completed successfully", true);
if (!isResponseNull)
syncFinishListener.onSyncFinish("Sync is completed successfully", true);
else
syncFinishListener.onSyncFinish("Couldn't sync the data, please try again", true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Payload
{
private String dateFormat = "dd MMMM YYYY";
private String locale = "en";
private int calendarId;
private long calendarId;
private String transactionDate;

public String getDateFormat()
Expand All @@ -32,12 +32,12 @@ public void setLocale(String locale)
this.locale = locale;
}

public int getCalendarId()
public long getCalendarId()
{
return calendarId;
}

public void setCalendarId(int calendarId)
public void setCalendarId(long calendarId)
{
this.calendarId = calendarId;
}
Expand Down
19 changes: 19 additions & 0 deletions mifosng-android/src/main/java/com/mifos/utils/DateHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

package com.mifos.utils;

import android.content.Context;
import android.content.SharedPreferences;
import com.mifos.mifosxdroid.OfflineCenterInputActivity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
Expand Down Expand Up @@ -177,4 +181,19 @@ public static String getMonthName(int month) {
return monthName;
}

public static String getPayloadDate() {
SharedPreferences preferences = Constants.applicationContext.getSharedPreferences(OfflineCenterInputActivity.PREF_CENTER_DETAILS, Context.MODE_PRIVATE);
String date = preferences.getString(OfflineCenterInputActivity.TRANSACTION_DATE_KEY, null);
final StringBuilder builder = new StringBuilder();
if (date != null) {
String[] splittedDate = date.split("-");
int month = Integer.parseInt(splittedDate[1]);
builder.append(splittedDate[0]);
builder.append(" ");
builder.append(DateHelper.getMonthName(month));
builder.append(" ");
builder.append(splittedDate[2]);
}
return builder.toString();
}
}
Loading

0 comments on commit a416492

Please sign in to comment.