From 927aefce2a6addbeab4637acaed05fae18f600bc Mon Sep 17 00:00:00 2001 From: HARSH Date: Fri, 1 Nov 2019 11:39:29 +0530 Subject: [PATCH 1/3] 1. Updated day wise details when new insertion is made in collection, sale and cattle feed activities. 2. Added functionality to get min/ max rate if fat crosses lower/upper bounds. 3. Fixed bug which wasn't selecting rate if the member collects both types of milk. --- .../activities/CattleFeedActivity.java | 5 +- .../activities/CollectionActivity.java | 45 ++++----- .../activities/SaleActivity.java | 6 +- .../dairymanagement/database/DBQuery.java | 97 ++++++++++++++++--- 4 files changed, 113 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/activities/CattleFeedActivity.java b/app/src/main/java/com/kshitijharsh/dairymanagement/activities/CattleFeedActivity.java index d9b26a8..44a55af 100644 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/activities/CattleFeedActivity.java +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/activities/CattleFeedActivity.java @@ -232,7 +232,10 @@ public void onClick(View view) { qty.setText(""); item.setSelected(false); particulars.setText(""); - cattleDetails.setVisibility(View.GONE); + //Update day wise details + todayDate.setText(date.getText().toString()); + totAmt.setText(String.valueOf(dbClass.getCollecedAmtFromDate(date.getText().toString()))); + txtCode.requestFocus(); cashCredit.clearCheck(); diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/activities/CollectionActivity.java b/app/src/main/java/com/kshitijharsh/dairymanagement/activities/CollectionActivity.java index 6c46f67..f17d641 100644 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/activities/CollectionActivity.java +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/activities/CollectionActivity.java @@ -304,8 +304,8 @@ public void onCheckedChanged(RadioGroup group, int checkedId) { public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton rb = group.findViewById(checkedId); if (null != rb) { - //Toast.makeText(SaleActivity.this, rb.getText(), Toast.LENGTH_SHORT).show(); cowBuff = rb.getText().toString(); + cowBuf.setText(cowBuff); } } @@ -329,6 +329,8 @@ public void onClick(View view) { snf.setText(""); radioGroup.clearCheck(); radioGroupMorEve.clearCheck(); + swapBoth.setVisibility(View.GONE); + swapCB.setVisibility(View.VISIBLE); collectionDetails.setVisibility(View.GONE); if (todayDetails.getVisibility() == View.VISIBLE) todayDetails.setVisibility(View.GONE); @@ -441,17 +443,17 @@ public void onClick(View view) { swapBoth.setVisibility(View.GONE); swapCB.setVisibility(View.VISIBLE); txtCode.requestFocus(); - collectionDetails.setVisibility(View.GONE); - if (todayDetails.getVisibility() == View.VISIBLE) - todayDetails.setVisibility(View.GONE); + //Update day wise details + todayDate.setText(date.getText().toString()); + totAmt.setText(String.valueOf(dbClass.getCollecedAmtFromDate(date.getText().toString()))); + totLit.setText(String.valueOf(dbClass.getCollecedMilkFromDate(date.getText().toString()))); } // else { // Toast.makeText(CollectionActivity.this, "Please enter required values", Toast.LENGTH_SHORT).show(); // } + } else { + Toast.makeText(CollectionActivity.this, "Please enter required values", Toast.LENGTH_SHORT).show(); } -// else { -// Toast.makeText(CollectionActivity.this, "Please enter required values", Toast.LENGTH_SHORT).show(); -// } } } @@ -566,35 +568,26 @@ private void initNames() { } public void getRateAmt(float deg, float fat, float qty, String cobf) { - Cursor c; + float val; + if (degree.getHint().toString().equals("SNF")) { - c = dbQuery.getRateFromSNF(deg, fat, cobf, rateGroupNo); + val = dbQuery.getRateFromSNF(deg, fat, cobf, rateGroupNo); } else { if (settingsPrefs.equals("true")) { float s = 0; if (!snf.getText().toString().equals("")) s = Float.parseFloat(snf.getText().toString()); - c = dbQuery.getRateFromSNF(s, fat, cobf, rateGroupNo); + val = dbQuery.getRateFromSNF(s, fat, cobf, rateGroupNo); } else if (settingsPrefs.equals("false")) { - c = dbQuery.getRate(deg, fat, cobf, rateGroupNo); + val = dbQuery.getRate(deg, fat, cobf, rateGroupNo); } else { -// Toast.makeText(this, String.valueOf(fat) + " " + cobf + rateGroupNo, Toast.LENGTH_SHORT).show(); - c = dbQuery.getRateFromFat(fat, cobf, rateGroupNo); + val = dbQuery.getRateFromFat(fat, cobf, rateGroupNo); } } - float val; - c.moveToFirst(); - if (c.getCount() > 0) { - val = c.getFloat(c.getColumnIndex("rate")); - rate.setText(String.valueOf(val)); - a = qty * val; - amt.setText(String.valueOf(a)); - } else { - Toast.makeText(this, "Value not found!", Toast.LENGTH_SHORT).show(); - rate.setText(""); - amt.setText(""); - } - c.close(); + + rate.setText(String.valueOf(val)); + a = qty * val; + amt.setText(String.valueOf(a)); } public void getMemNameFromID(int id) { diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/activities/SaleActivity.java b/app/src/main/java/com/kshitijharsh/dairymanagement/activities/SaleActivity.java index bd481fc..74d8435 100644 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/activities/SaleActivity.java +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/activities/SaleActivity.java @@ -373,7 +373,11 @@ public void onClick(View view) { radioGroupCB.clearCheck(); // swapBoth.setVisibility(View.GONE); // swapCB.setVisibility(View.VISIBLE); - saleDetails.setVisibility(View.GONE); + //Update day wise details + todayDate.setText(date.getText().toString()); + totAmt.setText(String.valueOf(dbClass.getCollecedAmtFromDate(date.getText().toString()))); + totLit.setText(String.valueOf(dbClass.getCollecedMilkFromDate(date.getText().toString()))); + txtCode.requestFocus(); cashCredit.clearCheck(); diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/database/DBQuery.java b/app/src/main/java/com/kshitijharsh/dairymanagement/database/DBQuery.java index fdc5349..f596204 100644 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/database/DBQuery.java +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/database/DBQuery.java @@ -9,18 +9,17 @@ import java.util.ArrayList; import java.util.List; +import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.COLUMN_BUFFALO_RATE; import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.COLUMN_COWBF_TYPE; +import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.COLUMN_COW_RATE; import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.COLUMN_MEMB_CODE; import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.COLUMN_MEMB_NAME; import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.COLUMN_MEMB_TYPE; -import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.COLUMN_RATEGRP_NO; -import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.TABLE_MEMBER; - -import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.COLUMN_RATEGRNO; import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.COLUMN_RATEGRNAME; +import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.COLUMN_RATEGRNO; +import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.COLUMN_RATEGRP_NO; import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.COLUMN_RATE_TYPE; -import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.COLUMN_COW_RATE; -import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.COLUMN_BUFFALO_RATE; +import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.TABLE_MEMBER; import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.TABLE_RATEGRPMASTER; public class DBQuery { @@ -93,34 +92,108 @@ public Cursor getAllRateGroups() { COLUMN_RATEGRNAME); } - public Cursor getRate(float degree, float fat, String cobf, int rateGrpNo) { + //Getting Rate for particular degree/ fat/ milk type from database + + public float getRate(float degree, float fat, String cobf, int rateGrpNo) { String cb; if (cobf.equals("Buffalo")) cb = "B"; else cb = "C"; String query = "SELECT rate from ratemst where degree='" + degree + "' AND fat='" + fat + "' AND cobf='" + cb + "' AND rtgrno='" + rateGrpNo + "'"; - return db.rawQuery(query, null); + Cursor c = db.rawQuery(query, null); + c.moveToFirst(); + + float val; + if (c.getCount() > 0) { + val = c.getFloat(c.getColumnIndex("rate")); + c.close(); + } else { + String temp = "SELECT MIN(fat) as fat from ratemst where cobf='" + cb + "' AND rtgrno='" + rateGrpNo + "'"; + Cursor tmp = db.rawQuery(temp, null); + tmp.moveToFirst(); // Not checked for count > 0 + float minFat = tmp.getFloat(0); + tmp.close(); + String q; + if (fat < minFat) { + q = "SELECT MIN(rate) as rate from ratemst where cobf='" + cb + "' AND rtgrno='" + rateGrpNo + "'"; + } else { + q = "SELECT MAX(rate) as rate from ratemst where cobf='" + cb + "' AND rtgrno='" + rateGrpNo + "'"; + } + Cursor cursor = db.rawQuery(q, null); + cursor.moveToFirst(); // Not checked for count > 0 + val = cursor.getFloat(0); + cursor.close(); + } + return val; } - public Cursor getRateFromFat(float fat, String cobf, int rateGrpNo) { + public float getRateFromFat(float fat, String cobf, int rateGrpNo) { String cb; if (cobf.equals("Buffalo")) cb = "B"; else cb = "C"; String query = "SELECT rate from ratemst where fat='" + fat + "' AND cobf='" + cb + "' AND rtgrno='" + rateGrpNo + "'"; - return db.rawQuery(query, null); + Cursor c = db.rawQuery(query, null); + c.moveToFirst(); + + float val; + if (c.getCount() > 0) { + val = c.getFloat(c.getColumnIndex("rate")); + c.close(); + } else { + String temp = "SELECT MIN(fat) as fat from ratemst where cobf='" + cb + "' AND rtgrno='" + rateGrpNo + "'"; + Cursor tmp = db.rawQuery(temp, null); + tmp.moveToFirst(); // Not checked for count > 0 + float minFat = tmp.getFloat(0); + tmp.close(); + String q; + if (fat < minFat) { + q = "SELECT MIN(rate) as rate from ratemst where cobf='" + cb + "' AND rtgrno='" + rateGrpNo + "'"; + } else { + q = "SELECT MAX(rate) as rate from ratemst where cobf='" + cb + "' AND rtgrno='" + rateGrpNo + "'"; + } + Cursor cursor = db.rawQuery(q, null); + cursor.moveToFirst(); // Not checked for count > 0 + val = cursor.getFloat(0); + cursor.close(); + } + return val; } - public Cursor getRateFromSNF(float snf, float fat, String cobf, int rateGrpNo) { + public float getRateFromSNF(float snf, float fat, String cobf, int rateGrpNo) { String cb; if (cobf.equals("Buffalo")) cb = "B"; else cb = "C"; String query = "SELECT rate from ratemst where snf='" + snf + "' AND fat='" + fat + "' AND cobf='" + cb + "' AND rtgrno='" + rateGrpNo + "'"; - return db.rawQuery(query, null); + Cursor c = db.rawQuery(query, null); + c.moveToFirst(); + + float val; + if (c.getCount() > 0) { + val = c.getFloat(0); + c.close(); + } else { + String temp = "SELECT MIN(fat) as fat from ratemst where cobf='" + cb + "' AND rtgrno='" + rateGrpNo + "'"; + Cursor tmp = db.rawQuery(temp, null); + tmp.moveToFirst(); // Not checked for count > 0 + float minFat = tmp.getFloat(c.getColumnIndex("fat")); + tmp.close(); + String q; + if (fat < minFat) { + q = "SELECT MIN(rate) as rate from ratemst where cobf='" + cb + "' AND rtgrno='" + rateGrpNo + "'"; + } else { + q = "SELECT MAX(rate) as rate from ratemst where cobf='" + cb + "' AND rtgrno='" + rateGrpNo + "'"; + } + Cursor cursor = db.rawQuery(q, null); + cursor.moveToFirst(); // Not checked for count > 0 + val = cursor.getFloat(0); + cursor.close(); + } + return val; } public void addNewMem(String name, int zone, int cowBuff, int memType, int rateGrp) { From 19a7835cfd058e8cb72a541eea0120abdbbcf5df Mon Sep 17 00:00:00 2001 From: HARSH Date: Fri, 1 Nov 2019 12:25:41 +0530 Subject: [PATCH 2/3] 1. Added cash/credit field in sale and cattle feed database. 2. Added check for existence of base database. --- .../activities/CattleFeedActivity.java | 18 +++++++--- .../activities/CollectionActivity.java | 21 +++++++----- .../activities/MainActivity.java | 30 +++++++++++++--- .../activities/SaleActivity.java | 34 +++++++++++++------ .../database/DatabaseClass.java | 18 ++++++---- 5 files changed, 88 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/activities/CattleFeedActivity.java b/app/src/main/java/com/kshitijharsh/dairymanagement/activities/CattleFeedActivity.java index 44a55af..02eae30 100644 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/activities/CattleFeedActivity.java +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/activities/CattleFeedActivity.java @@ -112,6 +112,10 @@ protected void onCreate(Bundle savedInstanceState) { button.setChecked(true); memDetails.setVisibility(View.VISIBLE); } + + todayDate.setText(date.getText().toString()); + totAmt.setText(String.valueOf(dbClass.getCattleAmtFromDate(date.getText().toString()))); + cattleDetails.setVisibility(View.VISIBLE); } qty.addTextChangedListener(new TextWatcher() { @@ -197,13 +201,19 @@ public void onCheckedChanged(RadioGroup group, int checkedId) { save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - String p; + String p, cashCr; if (date.getText().toString().equals("") || rate.getText().toString().equals("") || amt.getText().toString().equals("") || qty.getText().toString().equals("") || item.getSelectedItem().equals("Select Item") || cashCredit.getCheckedRadioButtonId() == -1) { Toast.makeText(CattleFeedActivity.this, "Please enter required values", Toast.LENGTH_SHORT).show(); } else { int memId = 0; String memName = "Not available"; int cashCrId = cashCredit.getCheckedRadioButtonId(); + RadioButton crDr = findViewById(cashCrId); + if (crDr.getText().toString().equals("Cash")) + cashCr = "1"; + else + cashCr = "2"; + if (cashCrId == R.id.radioButtonCredit) { if (!txtCode.getText().toString().equals("")) memId = Integer.parseInt(txtCode.getText().toString()); @@ -220,9 +230,9 @@ public void onClick(View view) { p = particulars.getText().toString(); if (bundle != null) { - dbClass.editCattle(id, date.getText().toString(), memId, memName, label, quantity, r, a, p); + dbClass.editCattle(id, date.getText().toString(), memId, memName, label, quantity, r, a, p, cashCr); } else { - dbClass.addCattle(date.getText().toString(), memId, memName, label, quantity, r, a, p); + dbClass.addCattle(date.getText().toString(), memId, memName, label, quantity, r, a, p, cashCr); Toast.makeText(CattleFeedActivity.this, "Added Successfully", Toast.LENGTH_LONG).show(); } // date.setText(R.string.select_date); @@ -234,7 +244,7 @@ public void onClick(View view) { particulars.setText(""); //Update day wise details todayDate.setText(date.getText().toString()); - totAmt.setText(String.valueOf(dbClass.getCollecedAmtFromDate(date.getText().toString()))); + totAmt.setText(String.valueOf(dbClass.getCattleAmtFromDate(date.getText().toString()))); txtCode.requestFocus(); diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/activities/CollectionActivity.java b/app/src/main/java/com/kshitijharsh/dairymanagement/activities/CollectionActivity.java index f17d641..f04703f 100644 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/activities/CollectionActivity.java +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/activities/CollectionActivity.java @@ -102,6 +102,14 @@ protected void onCreate(Bundle savedInstanceState) { radioGroup = findViewById(R.id.cowBuff); radioGroup.clearCheck(); + swapBoth = findViewById(R.id.swapBoth); + swapCB = findViewById(R.id.swapCB); + + collectionDetails = findViewById(R.id.collection_details); + todayDate = findViewById(R.id.today_date); + totAmt = findViewById(R.id.tot_amt); + totLit = findViewById(R.id.tot_lit); + final Bundle bundle = getIntent().getExtras(); if (bundle != null) { typeLayout.setVisibility(View.GONE); @@ -138,6 +146,11 @@ protected void onCreate(Bundle savedInstanceState) { ((RadioButton) radioGroup.findViewById(R.id.radioButtonBoth)).setChecked(true); break; } + + todayDate.setText(date.getText().toString()); + totAmt.setText(String.valueOf(dbClass.getCollecedAmtFromDate(date.getText().toString()))); + totLit.setText(String.valueOf(dbClass.getCollecedMilkFromDate(date.getText().toString()))); + collectionDetails.setVisibility(View.VISIBLE); } if (settingsPrefs.equals("false")) { @@ -291,14 +304,6 @@ public void onCheckedChanged(RadioGroup group, int checkedId) { } }); - swapBoth = findViewById(R.id.swapBoth); - swapCB = findViewById(R.id.swapCB); - - collectionDetails = findViewById(R.id.collection_details); - todayDate = findViewById(R.id.today_date); - totAmt = findViewById(R.id.tot_amt); - totLit = findViewById(R.id.tot_lit); - radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/activities/MainActivity.java b/app/src/main/java/com/kshitijharsh/dairymanagement/activities/MainActivity.java index 8542f7b..8a05dee 100644 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/activities/MainActivity.java +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/activities/MainActivity.java @@ -5,6 +5,7 @@ import android.content.pm.PackageManager; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteException; import android.os.Bundle; import android.os.Environment; import android.os.Handler; @@ -16,24 +17,25 @@ import android.widget.Toast; import com.kshitijharsh.dairymanagement.R; -import com.kshitijharsh.dairymanagement.utils.SqliteExporter; import com.kshitijharsh.dairymanagement.database.DBHelper; import com.kshitijharsh.dairymanagement.database.DBQuery; import com.kshitijharsh.dairymanagement.database.DatabaseClass; +import com.kshitijharsh.dairymanagement.utils.SqliteExporter; import java.io.File; import java.io.IOException; import java.util.Objects; -import static com.kshitijharsh.dairymanagement.utils.Constants.CONST.BACKUP_DIRECTORY; -import static com.kshitijharsh.dairymanagement.utils.Constants.CONST.EXT_DIRECTORY; import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.TABLE_ITEM; import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.TABLE_MEMBER; import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.TABLE_RATEGRPMASTER; import static com.kshitijharsh.dairymanagement.database.BaseContract.BaseEntry.TABLE_RATEMASTER; +import static com.kshitijharsh.dairymanagement.utils.Constants.CONST.BACKUP_DIRECTORY; +import static com.kshitijharsh.dairymanagement.utils.Constants.CONST.EXT_DIRECTORY; public class MainActivity extends AppCompatActivity { public static final int ACCESS_EXTERNAL_STORAGE = 1; + private static final String DB_FULL_PATH = Environment.getExternalStorageDirectory() + "/Winsoft/base.db"; private boolean exit = false; DatabaseClass dc; DBHelper dbHelper; @@ -68,13 +70,21 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis // If request is cancelled, the result arrays are empty. if (grantResults[0] == PackageManager.PERMISSION_DENIED) { //Permission is denied - Toast.makeText(this, "Storage permission was required, app will exit now...", Toast.LENGTH_SHORT).show(); + Toast.makeText(this, "Storage permission was required, app will exit now...", Toast.LENGTH_LONG).show(); new Handler().postDelayed(new Runnable() { @Override public void run() { finish(); } },3*1000); + } else if (!checkDataBase()) { + Toast.makeText(this, "Database not found! App will exit now...", Toast.LENGTH_LONG).show(); + new Handler().postDelayed(new Runnable() { + @Override + public void run() { + finish(); + } + }, 3 * 1000); } else { dbHelper = new DBHelper(this); db = dbHelper.getReadableDatabase(); @@ -161,4 +171,16 @@ public void launchMember(View view) { public void launchSettings(View view) { startActivity(new Intent(MainActivity.this, SettingsActivity.class)); } + + private boolean checkDataBase() { + SQLiteDatabase checkDB = null; + try { + checkDB = SQLiteDatabase.openDatabase(DB_FULL_PATH, null, + SQLiteDatabase.OPEN_READONLY); + checkDB.close(); + } catch (SQLiteException e) { + return false; + } + return true; + } } diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/activities/SaleActivity.java b/app/src/main/java/com/kshitijharsh/dairymanagement/activities/SaleActivity.java index 74d8435..8f951b2 100644 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/activities/SaleActivity.java +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/activities/SaleActivity.java @@ -91,6 +91,14 @@ protected void onCreate(Bundle savedInstanceState) { cashCredit = findViewById(R.id.cash_credit); memDetails = findViewById(R.id.member_details_layout); + swapBoth = findViewById(R.id.swapBoth); +// swapCB = findViewById(R.id.swapCB); + + saleDetails = findViewById(R.id.sale_details); + todayDate = findViewById(R.id.today_date); + totAmt = findViewById(R.id.tot_amt); + totLit = findViewById(R.id.tot_lit); + final Bundle bundle = getIntent().getExtras(); if (bundle != null) { id = bundle.getString("id"); @@ -131,6 +139,11 @@ protected void onCreate(Bundle savedInstanceState) { memDetails.setVisibility(View.VISIBLE); } + todayDate.setText(date.getText().toString()); + totAmt.setText(String.valueOf(dbClass.getAmtFromDate(date.getText().toString()))); + totLit.setText(String.valueOf(dbClass.getMilkFromDate(date.getText().toString()))); + saleDetails.setVisibility(View.VISIBLE); + } rate.addTextChangedListener(new TextWatcher() { @@ -217,13 +230,7 @@ public void onCheckedChanged(RadioGroup group, int checkedId) { } }); - swapBoth = findViewById(R.id.swapBoth); -// swapCB = findViewById(R.id.swapCB); - saleDetails = findViewById(R.id.sale_details); - todayDate = findViewById(R.id.today_date); - totAmt = findViewById(R.id.tot_amt); - totLit = findViewById(R.id.tot_lit); radioGroupCB.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override @@ -303,6 +310,7 @@ public void onClick(View view) { @SuppressLint("SetTextI18n") @Override public void onClick(View view) { + String cashCr; if (date.getText().toString().equals("") || quantity.getText().toString().equals("") || radioGroup.getCheckedRadioButtonId() == -1 || cashCredit.getCheckedRadioButtonId() == -1) { Toast.makeText(SaleActivity.this, "Please enter required values", Toast.LENGTH_SHORT).show(); } else { @@ -310,6 +318,12 @@ public void onClick(View view) { int memCode = 0; String memName = "Not available"; int cashCrId = cashCredit.getCheckedRadioButtonId(); + RadioButton crDr = findViewById(cashCrId); + if (crDr.getText().toString().equals("Cash")) + cashCr = "1"; + else + cashCr = "2"; + if (cashCrId == R.id.radioButtonCredit) { if (!txtCode.getText().toString().equals("")) memCode = Integer.parseInt(txtCode.getText().toString()); @@ -356,9 +370,9 @@ public void onClick(View view) { m = "2"; } if (bundle != null) - dbClass.editSale(id, date.getText().toString(), memCode, memName, me, cb, lit, f, r, Float.parseFloat(amt.getText().toString())); + dbClass.editSale(id, date.getText().toString(), memCode, memName, me, cb, lit, f, r, Float.parseFloat(amt.getText().toString()), cashCr); else - dbClass.addSale(date.getText().toString(), memCode, memName, m, cb, lit, f, r, Float.parseFloat(amt.getText().toString())); + dbClass.addSale(date.getText().toString(), memCode, memName, m, cb, lit, f, r, Float.parseFloat(amt.getText().toString()), cashCr); Toast.makeText(SaleActivity.this, "Added Successfully", Toast.LENGTH_LONG).show(); edtName.setText(""); // branch.setText(""); @@ -375,8 +389,8 @@ public void onClick(View view) { // swapCB.setVisibility(View.VISIBLE); //Update day wise details todayDate.setText(date.getText().toString()); - totAmt.setText(String.valueOf(dbClass.getCollecedAmtFromDate(date.getText().toString()))); - totLit.setText(String.valueOf(dbClass.getCollecedMilkFromDate(date.getText().toString()))); + totAmt.setText(String.valueOf(dbClass.getAmtFromDate(date.getText().toString()))); + totLit.setText(String.valueOf(dbClass.getMilkFromDate(date.getText().toString()))); txtCode.requestFocus(); diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/database/DatabaseClass.java b/app/src/main/java/com/kshitijharsh/dairymanagement/database/DatabaseClass.java index 3d47365..4d48256 100644 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/database/DatabaseClass.java +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/database/DatabaseClass.java @@ -11,7 +11,7 @@ public class DatabaseClass extends SQLiteOpenHelper { public final static String DATABASE_NAME = "records.db"; - private static final int DATABASE_VERSION = 3; + private static final int DATABASE_VERSION = 4; private SQLiteDatabase db; private File dbPath; @@ -26,8 +26,8 @@ public DatabaseClass(final Context context) { public void onCreate(SQLiteDatabase db) { //db.execSQL("CREATE TABLE member(memb_code INTEGER PRIMARY KEY, memb_name TEXT, zoon_code INTEGER, Cobf_type INTEGER, memb_type INTEGER, accno INTEGER, rategrno INTEGER, bank_code INTEGER, BankAcNo INTEGER, membNam_Eng TEXT, AcNo INTEGER);"); db.execSQL("CREATE TABLE collectionTransactions (_id INTEGER PRIMARY KEY AUTOINCREMENT, trnDate TEXT, membCode INTEGER, memName TEXT, cobf TEXT, morEve TEXT, degree FLOAT, liters FLOAT, fat FLOAT, rate FLOAT, amount FLOAT);"); - db.execSQL("CREATE TABLE saleTransactions (_id INTEGER PRIMARY KEY AUTOINCREMENT, trnDate TEXT, membCode INTEGER, memName TEXT, mornEve TEXT, cobf TEXT, liters FLOAT, fat FLOAT, rate FLOAT, amount FLOAT);"); - db.execSQL("CREATE TABLE cattleTransactions (_id INTEGER PRIMARY KEY AUTOINCREMENT, trnDate TEXT, memId INTEGER, memName TEXT, itemName TEXT, quantity FLOAT, rate FLOAT, amount FLOAT, particulars TEXT);"); + db.execSQL("CREATE TABLE saleTransactions (_id INTEGER PRIMARY KEY AUTOINCREMENT, trnDate TEXT, membCode INTEGER, memName TEXT, mornEve TEXT, cobf TEXT, liters FLOAT, fat FLOAT, rate FLOAT, amount FLOAT, cashCr TEXT);"); + db.execSQL("CREATE TABLE cattleTransactions (_id INTEGER PRIMARY KEY AUTOINCREMENT, trnDate TEXT, memId INTEGER, memName TEXT, itemName TEXT, quantity FLOAT, rate FLOAT, amount FLOAT, particulars TEXT, cashCr TEXT);"); } @Override @@ -55,7 +55,7 @@ public void addColl(String date, int membCode, String name, String cobf, String getWritableDatabase().insert("collectionTransactions", "trnDate", values); } - public void addSale(String date, int membCode, String name, String morEve, String cobf, float liters, float fat, float rate, float amount) { + public void addSale(String date, int membCode, String name, String morEve, String cobf, float liters, float fat, float rate, float amount, String crdr) { ContentValues values = new ContentValues(10); values.put("trnDate", date); values.put("membCode", membCode); @@ -66,10 +66,11 @@ public void addSale(String date, int membCode, String name, String morEve, Strin values.put("fat", fat); values.put("rate", rate); values.put("amount", amount); + values.put("cashCr", crdr); getWritableDatabase().insert("saleTransactions", "trnDate", values); } - public void addCattle(String date, int lgr, String name, String item, float qty, float rate, float amt, String part) { + public void addCattle(String date, int lgr, String name, String item, float qty, float rate, float amt, String part, String crdr) { ContentValues values = new ContentValues(9); values.put("trnDate", date); values.put("memId", lgr); @@ -79,6 +80,7 @@ public void addCattle(String date, int lgr, String name, String item, float qty, values.put("rate", rate); values.put("amount", amt); values.put("particulars", part); + values.put("cashCr", crdr); getWritableDatabase().insert("cattleTransactions", "trnDate", values); } @@ -229,7 +231,7 @@ public void editColl(String id, String date, int membCode, String name, String c db.update("collectionTransactions", values, "_id=" + id, null); } - public void editSale(String id, String date, int membCode, String name, String morEve, String cobf, float liters, float fat, float rate, float amount) { + public void editSale(String id, String date, int membCode, String name, String morEve, String cobf, float liters, float fat, float rate, float amount, String crdr) { ContentValues values = new ContentValues(10); values.put("trnDate", date); values.put("membCode", membCode); @@ -240,12 +242,13 @@ public void editSale(String id, String date, int membCode, String name, String m values.put("fat", fat); values.put("rate", rate); values.put("amount", amount); + values.put("cashCr", crdr); SQLiteDatabase db = this.getWritableDatabase(); db.update("saleTransactions", values, "_id=" + id, null); } - public void editCattle(String id, String date, int lgr, String name, String item, float qty, float rate, float amt, String part) { + public void editCattle(String id, String date, int lgr, String name, String item, float qty, float rate, float amt, String part, String crdr) { ContentValues values = new ContentValues(9); values.put("trnDate", date); values.put("memId", lgr); @@ -255,6 +258,7 @@ public void editCattle(String id, String date, int lgr, String name, String item values.put("rate", rate); values.put("amount", amt); values.put("particulars", part); + values.put("cashCr", crdr); SQLiteDatabase db = this.getWritableDatabase(); db.update("cattleTransactions", values, "_id=" + id, null); From 2afa9efae0fdabca2f831966d812dec5fa5b2bfa Mon Sep 17 00:00:00 2001 From: HARSH Date: Fri, 1 Nov 2019 19:50:41 +0530 Subject: [PATCH 3/3] Added support to import to database from csv files. --- .idea/assetWizardSettings.xml | 4 +- .idea/caches/build_file_checksums.ser | Bin 537 -> 537 bytes app/build.gradle | 6 +- .../dairymanagement/ItemClickListener.java | 7 - .../activities/MainActivity.java | 79 +++++- .../adapters/CattleAdapter.java | 1 - .../adapters/CollectionAdapter.java | 1 - .../adapters/MemberAdapter.java | 2 - .../dairymanagement/adapters/SaleAdapter.java | 2 - .../dairymanagement/utils/Constants.java | 2 +- .../dairymanagement/utils/FileUtils.java | 4 +- .../utils/FilterDialogFragment.java | 150 ------------ .../dairymanagement/utils/Filters.java | 112 --------- .../dairymanagement/utils/SqliteExporter.java | 10 +- .../dairymanagement/utils/SqliteImporter.java | 225 ++++++++++++++++++ app/src/main/res/drawable/ic_import.xml | 10 + app/src/main/res/menu/menu_main.xml | 9 + 17 files changed, 324 insertions(+), 300 deletions(-) delete mode 100644 app/src/main/java/com/kshitijharsh/dairymanagement/ItemClickListener.java delete mode 100644 app/src/main/java/com/kshitijharsh/dairymanagement/utils/FilterDialogFragment.java delete mode 100644 app/src/main/java/com/kshitijharsh/dairymanagement/utils/Filters.java create mode 100644 app/src/main/java/com/kshitijharsh/dairymanagement/utils/SqliteImporter.java create mode 100644 app/src/main/res/drawable/ic_import.xml create mode 100644 app/src/main/res/menu/menu_main.xml diff --git a/.idea/assetWizardSettings.xml b/.idea/assetWizardSettings.xml index a453638..e3c54e5 100644 --- a/.idea/assetWizardSettings.xml +++ b/.idea/assetWizardSettings.xml @@ -18,7 +18,7 @@ @@ -29,7 +29,7 @@ diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 03eeb7ed9e744d40cbfbbe9cfe9adff649b4d12c..81c7a253572597a1cdabf36a9378888136cd1203 100644 GIT binary patch delta 35 tcmV+;0NnqX1epYom;@#1+g_2Jybu)4YyNxw-?aaJPrtZRo{N)c0kJuj5@`Sc delta 35 tcmV+;0NnqX1epYom;@a5xc!lwybv}k%-yaPgmYfPoTr0C(QlJz0kJBU5dr`J diff --git a/app/build.gradle b/app/build.gradle index 145c583..c50650d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -39,9 +39,9 @@ dependencies { implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation group: 'com.opencsv', name: 'opencsv', version: '4.1' - // ButterKnife (for sample code brevity) - implementation 'com.jakewharton:butterknife:8.8.1' - annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' +// // ButterKnife (for sample code brevity) +// implementation 'com.jakewharton:butterknife:8.8.1' +// annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/ItemClickListener.java b/app/src/main/java/com/kshitijharsh/dairymanagement/ItemClickListener.java deleted file mode 100644 index cf1f195..0000000 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/ItemClickListener.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.kshitijharsh.dairymanagement; - -import android.os.Bundle; - -public interface ItemClickListener { - void onClick(Bundle bundle); -} diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/activities/MainActivity.java b/app/src/main/java/com/kshitijharsh/dairymanagement/activities/MainActivity.java index 8a05dee..166ad0b 100644 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/activities/MainActivity.java +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/activities/MainActivity.java @@ -13,6 +13,9 @@ import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.util.Log; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.widget.Toast; @@ -20,7 +23,9 @@ import com.kshitijharsh.dairymanagement.database.DBHelper; import com.kshitijharsh.dairymanagement.database.DBQuery; import com.kshitijharsh.dairymanagement.database.DatabaseClass; +import com.kshitijharsh.dairymanagement.utils.FileUtils; import com.kshitijharsh.dairymanagement.utils.SqliteExporter; +import com.kshitijharsh.dairymanagement.utils.SqliteImporter; import java.io.File; import java.io.IOException; @@ -64,6 +69,7 @@ private void checkForStoragePermission() { ACCESS_EXTERNAL_STORAGE); } } + @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); @@ -76,7 +82,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis public void run() { finish(); } - },3*1000); + }, 3 * 1000); } else if (!checkDataBase()) { Toast.makeText(this, "Database not found! App will exit now...", Toast.LENGTH_LONG).show(); new Handler().postDelayed(new Runnable() { @@ -86,21 +92,22 @@ public void run() { } }, 3 * 1000); } else { + FileUtils.createDirIfNotExist(FileUtils.getAppDir() + "/imports"); dbHelper = new DBHelper(this); db = dbHelper.getReadableDatabase(); - Cursor member = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+ TABLE_MEMBER +"'", null); - Cursor rateMaster = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+ TABLE_RATEMASTER +"'", null); - Cursor item = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+ TABLE_ITEM +"'", null); - Cursor rateGrpMaster = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+ TABLE_RATEGRPMASTER +"'", null); + Cursor member = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '" + TABLE_MEMBER + "'", null); + Cursor rateMaster = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '" + TABLE_RATEMASTER + "'", null); + Cursor item = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '" + TABLE_ITEM + "'", null); + Cursor rateGrpMaster = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '" + TABLE_RATEGRPMASTER + "'", null); - if (member.getCount()<=0 || rateMaster.getCount()<=0 || item.getCount() <=0 || rateGrpMaster.getCount() <=0) { + if (member.getCount() <= 0 || rateMaster.getCount() <= 0 || item.getCount() <= 0 || rateGrpMaster.getCount() <= 0) { Toast.makeText(this, "Required tables not found in database, app will exit now...", Toast.LENGTH_LONG).show(); new Handler().postDelayed(new Runnable() { @Override public void run() { finish(); } - },3*1000); + }, 3 * 1000); } member.close(); rateMaster.close(); @@ -112,13 +119,16 @@ public void run() { Log.d(getClass().getSimpleName(), "First Query Result: " + query.getMembercount()); //Toast.makeText(this, DBHelper.DB_PATH, Toast.LENGTH_LONG).show(); query.close(); +// db.endTransaction(); + db.close(); + dbHelper.close(); } } - public void exitApp(){ - if(exit){ + public void exitApp() { + if (exit) { finish(); - }else { + } else { exit = true; // Toast.makeText(this,"Press back again to exit",Toast.LENGTH_SHORT).show(); new Handler().postDelayed(new Runnable() { @@ -126,7 +136,7 @@ public void exitApp(){ public void run() { exit = false; } - },3*1000); + }, 3 * 1000); } } @@ -152,7 +162,7 @@ public void initiateExport(View view) { + File.separator + BACKUP_DIRECTORY, Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); - Toast.makeText(this, "Error: "+ e.toString(), Toast.LENGTH_SHORT).show(); + Toast.makeText(this, "Error: " + e.toString(), Toast.LENGTH_SHORT).show(); } } @@ -183,4 +193,49 @@ private boolean checkDataBase() { } return true; } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.menu_main, menu); + return super.onCreateOptionsMenu(menu); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == R.id.menu_main) { + initiateImport(); + return true; + } + return super.onOptionsItemSelected(item); + } + + public void initiateImport() { + DBHelper helper = new DBHelper(this); + SQLiteDatabase sqLiteDatabase = helper.getWritableDatabase(); + try { + + int res = SqliteImporter.importData(sqLiteDatabase); +// Toast.makeText(this, res, Toast.LENGTH_SHORT).show(); + switch (res) { + case 0: + Toast.makeText(this, "No CSV files to import", Toast.LENGTH_SHORT).show(); + break; + case -1: + Toast.makeText(this, "File names does not match!", Toast.LENGTH_SHORT).show(); + break; + case 1: + Toast.makeText(this, "Successfully Imported.", Toast.LENGTH_SHORT).show(); + break; + default: + Toast.makeText(this, "Some problem occurred...", Toast.LENGTH_SHORT).show(); + } + } catch (Exception e) { + Toast.makeText(this, "Error while importing, try again.", Toast.LENGTH_SHORT).show(); + Log.e("Import Exception: ", e.toString()); + } +// sqLiteDatabase.endTransaction(); + sqLiteDatabase.close(); + helper.close(); + } } diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/adapters/CattleAdapter.java b/app/src/main/java/com/kshitijharsh/dairymanagement/adapters/CattleAdapter.java index 9ebd880..c5ca74f 100644 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/adapters/CattleAdapter.java +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/adapters/CattleAdapter.java @@ -16,7 +16,6 @@ import android.widget.TextView; import android.widget.Toast; -import com.kshitijharsh.dairymanagement.ItemClickListener; import com.kshitijharsh.dairymanagement.R; import com.kshitijharsh.dairymanagement.activities.CattleFeedActivity; import com.kshitijharsh.dairymanagement.database.DatabaseClass; diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/adapters/CollectionAdapter.java b/app/src/main/java/com/kshitijharsh/dairymanagement/adapters/CollectionAdapter.java index 403f694..87f85c5 100644 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/adapters/CollectionAdapter.java +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/adapters/CollectionAdapter.java @@ -16,7 +16,6 @@ import android.widget.TextView; import android.widget.Toast; -import com.kshitijharsh.dairymanagement.ItemClickListener; import com.kshitijharsh.dairymanagement.R; import com.kshitijharsh.dairymanagement.activities.CollectionActivity; import com.kshitijharsh.dairymanagement.database.DatabaseClass; diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/adapters/MemberAdapter.java b/app/src/main/java/com/kshitijharsh/dairymanagement/adapters/MemberAdapter.java index 19d1e9a..a724cbb 100644 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/adapters/MemberAdapter.java +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/adapters/MemberAdapter.java @@ -4,7 +4,6 @@ import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; -import android.icu.text.UnicodeSetSpanner; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.widget.RecyclerView; @@ -17,7 +16,6 @@ import android.widget.TextView; import android.widget.Toast; -import com.kshitijharsh.dairymanagement.ItemClickListener; import com.kshitijharsh.dairymanagement.R; import com.kshitijharsh.dairymanagement.activities.MemberActivity; import com.kshitijharsh.dairymanagement.database.DBQuery; diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/adapters/SaleAdapter.java b/app/src/main/java/com/kshitijharsh/dairymanagement/adapters/SaleAdapter.java index c925372..532bbe5 100644 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/adapters/SaleAdapter.java +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/adapters/SaleAdapter.java @@ -16,11 +16,9 @@ import android.widget.TextView; import android.widget.Toast; -import com.kshitijharsh.dairymanagement.ItemClickListener; import com.kshitijharsh.dairymanagement.R; import com.kshitijharsh.dairymanagement.activities.SaleActivity; import com.kshitijharsh.dairymanagement.database.DatabaseClass; -import com.kshitijharsh.dairymanagement.model.Member; import com.kshitijharsh.dairymanagement.model.Sale; import java.util.ArrayList; diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/utils/Constants.java b/app/src/main/java/com/kshitijharsh/dairymanagement/utils/Constants.java index 585e5be..bbd28d5 100644 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/utils/Constants.java +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/utils/Constants.java @@ -3,7 +3,7 @@ public class Constants { public static class CONST { public static final String EXT_DIRECTORY = "/Winsoft"; - public static final String BACKUP_DIRECTORY = "backup"; + public static final String BACKUP_DIRECTORY = "exports"; public static final String MY_PATH = "/data/data/com.kshitijharsh.dairymanagement/databases/records.db"; public static final String FIELD_NAME = "name"; diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/utils/FileUtils.java b/app/src/main/java/com/kshitijharsh/dairymanagement/utils/FileUtils.java index be2bece..0dc0ad0 100644 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/utils/FileUtils.java +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/utils/FileUtils.java @@ -8,11 +8,11 @@ public class FileUtils { - static String getAppDir() { + public static String getAppDir() { return Environment.getExternalStorageDirectory() + EXT_DIRECTORY; } - static File createDirIfNotExist(String path) { + public static File createDirIfNotExist(String path) { File dir = new File(path); if( !dir.exists() ){ dir.mkdir(); diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/utils/FilterDialogFragment.java b/app/src/main/java/com/kshitijharsh/dairymanagement/utils/FilterDialogFragment.java deleted file mode 100644 index 4261678..0000000 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/utils/FilterDialogFragment.java +++ /dev/null @@ -1,150 +0,0 @@ -package com.kshitijharsh.dairymanagement.utils; - -import android.content.Context; -import android.os.Bundle; -import android.support.annotation.Nullable; -import android.support.v4.app.DialogFragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.Spinner; - -import com.kshitijharsh.dairymanagement.R; - -import butterknife.BindView; -import butterknife.ButterKnife; -import butterknife.OnClick; - -public class FilterDialogFragment extends DialogFragment { - - public static final String TAG = "FilterDialog"; - - public interface FilterListener { - - void onFilter(Filters filters); - - } - - private View mRootView; - - @BindView(R.id.spinner_category) - Spinner mCategorySpinner; - - @BindView(R.id.spinner_sort) - Spinner mSortSpinner; - - @BindView(R.id.spinner_price) - Spinner mPriceSpinner; - - private FilterListener mFilterListener; - - @Nullable - @Override - public View onCreateView(LayoutInflater inflater, - @Nullable ViewGroup container, - @Nullable Bundle savedInstanceState) { - mRootView = inflater.inflate(R.layout.dialog_filters, container, false); - ButterKnife.bind(this, mRootView); - - return mRootView; - } - - @Override - public void onAttach(Context context) { - super.onAttach(context); - - if (context instanceof FilterListener) { - mFilterListener = (FilterListener) context; - } - } - - @Override - public void onResume() { - super.onResume(); - getDialog().getWindow().setLayout( - ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.WRAP_CONTENT); - } - - @OnClick(R.id.button_search) - public void onSearchClicked() { - if (mFilterListener != null) { - mFilterListener.onFilter(getFilters()); - } - - dismiss(); - } - - @OnClick(R.id.button_cancel) - public void onCancelClicked() { - dismiss(); - } - - @Nullable - private String getSelectedType() { - String selected = (String) mCategorySpinner.getSelectedItem(); - if (getString(R.string.value_any_type).equals(selected)) { - return null; - } else { - return selected; - } - } - - @Nullable - private String getSelectedTime() { - String selected = (String) mPriceSpinner.getSelectedItem(); - if (getString(R.string.value_any_time).equals(selected)) { - return null; - } else { - return selected; - } - } - - @Nullable - private String getSelectedSortBy() { - String selected = (String) mSortSpinner.getSelectedItem(); - if (getString(R.string.sort_by_name).equals(selected)) { - return Constants.CONST.FIELD_NAME; - } - if (getString(R.string.sort_by_code).equals(selected)) { - return Constants.CONST.FIELD_CODE; - } - - return null; - } - -// @Nullable -// private Query.Direction getSortDirection() { -// String selected = (String) mSortSpinner.getSelectedItem(); -// if (getString(R.string.sort_by_rating).equals(selected)) { -// return Query.Direction.DESCENDING; -// } if (getString(R.string.sort_by_price).equals(selected)) { -// return Query.Direction.ASCENDING; -// } if (getString(R.string.sort_by_popularity).equals(selected)) { -// return Query.Direction.DESCENDING; -// } -// -// return null; -// } - - public void resetFilters() { - if (mRootView != null) { - mCategorySpinner.setSelection(0); - mPriceSpinner.setSelection(0); - mSortSpinner.setSelection(0); - } - } - - public Filters getFilters() { - Filters filters = new Filters(); - - if (mRootView != null) { - filters.setType(getSelectedType()); - filters.setTime(getSelectedTime()); - filters.setSortBy(getSelectedSortBy()); -// filters.setSortDirection(getSortDirection()); - } - - return filters; - } -} diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/utils/Filters.java b/app/src/main/java/com/kshitijharsh/dairymanagement/utils/Filters.java deleted file mode 100644 index f0a36e5..0000000 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/utils/Filters.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.kshitijharsh.dairymanagement.utils; - -import android.content.Context; -import android.text.TextUtils; - -import com.kshitijharsh.dairymanagement.R; - -/** - * Sort and Filter - * member code, cow / buffalo, morning / evening, litres, amount, - * TODO sort by date and Sort Direction - */ -public class Filters { - - private String type = null; - private String time = null; - private String sortBy = null; -// private Query.Direction sortDirection = null; - - public Filters() { - } - - public static Filters getDefault() { - Filters filters = new Filters(); - filters.setSortBy(Constants.CONST.FIELD_NAME); -// filters.setSortDirection(Query.Direction.DESCENDING); - - return filters; - } - - public boolean hasType() { - return !(TextUtils.isEmpty(type)); - } - - public boolean hasTime() { - return !(TextUtils.isEmpty(time)); - } - - public boolean hasSortBy() { - return !(TextUtils.isEmpty(sortBy)); - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public String getTime() { - return time; - } - - public void setTime(String time) { - this.time = time; - } - - public String getSortBy() { - return sortBy; - } - - public void setSortBy(String sortBy) { - this.sortBy = sortBy; - } - -// public Query.Direction getSortDirection() { -// return sortDirection; -// } -// -// public void setSortDirection(Query.Direction sortDirection) { -// this.sortDirection = sortDirection; -// } - -// public String getSearchDescription(Context context) { -// StringBuilder desc = new StringBuilder(); -// -// if (type == null && time == null) { -// desc.append(""); -// desc.append(context.getString(R.string.all_records)); -// desc.append(""); -// } -// -// if (type != null) { -// desc.append(""); -// desc.append(type); -// desc.append(""); -// } -// -// if (type != null && time != null) { -// desc.append(" in "); -// } -// -// if (time != null) { -// desc.append(""); -// desc.append(time); -// desc.append(""); -// } -// -// return desc.toString(); -// } -// -// public String getOrderDescription(Context context) { -// if (Restaurant.FIELD_PRICE.equals(sortBy)) { -// return context.getString(R.string.sorted_by_price); -// } else if (Restaurant.FIELD_POPULARITY.equals(sortBy)) { -// return context.getString(R.string.sorted_by_popularity); -// } else { -// return context.getString(R.string.sorted_by_rating); -// } -// } -} diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/utils/SqliteExporter.java b/app/src/main/java/com/kshitijharsh/dairymanagement/utils/SqliteExporter.java index ecf95b9..72d53a6 100644 --- a/app/src/main/java/com/kshitijharsh/dairymanagement/utils/SqliteExporter.java +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/utils/SqliteExporter.java @@ -25,26 +25,26 @@ public static String export(SQLiteDatabase db) throws IOException { if( !FileUtils.isExternalStorageWritable() ){ throw new IOException("Cannot write to external storage"); } - File backupDir = FileUtils.createDirIfNotExist(FileUtils.getAppDir() + "/backup"); + File backupDir = FileUtils.createDirIfNotExist(FileUtils.getAppDir() + "/exports"); String fileName = createBackupFileName(); File backupFile = new File(backupDir, fileName); boolean success = backupFile.createNewFile(); if(!success){ - throw new IOException("Failed to create the backup file"); + throw new IOException("Failed to create the export file"); } List tables = getTablesOnDataBase(db); - Log.d(TAG, "Started to fill the backup file in " + backupFile.getAbsolutePath()); + Log.d(TAG, "Started to fill the export file in " + backupFile.getAbsolutePath()); long starTime = System.currentTimeMillis(); writeCsv(backupFile, db, tables); long endTime = System.currentTimeMillis(); - Log.d(TAG, "Creating backup took " + (endTime - starTime) + "ms."); + Log.d(TAG, "Creating export took " + (endTime - starTime) + "ms."); return backupFile.getAbsolutePath(); } private static String createBackupFileName(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HHmm"); - return "db_backup_" + sdf.format(new Date()) + ".csv"; + return "db_export_" + sdf.format(new Date()) + ".csv"; } private static List getTablesOnDataBase(SQLiteDatabase db) { diff --git a/app/src/main/java/com/kshitijharsh/dairymanagement/utils/SqliteImporter.java b/app/src/main/java/com/kshitijharsh/dairymanagement/utils/SqliteImporter.java new file mode 100644 index 0000000..2314b4d --- /dev/null +++ b/app/src/main/java/com/kshitijharsh/dairymanagement/utils/SqliteImporter.java @@ -0,0 +1,225 @@ +package com.kshitijharsh.dairymanagement.utils; + +import android.content.ContentValues; +import android.database.sqlite.SQLiteDatabase; +import android.util.Log; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.FilenameFilter; + +public class SqliteImporter { + + private static final String IMPORT_PATH = FileUtils.getAppDir() + "/imports"; + + private static FilenameFilter csvFilter = new FilenameFilter() { + File f; + + public boolean accept(File dir, String name) { + if (name.endsWith(".csv") || name.endsWith(".CSV")) { + return true; + } + f = new File(dir.getAbsolutePath() + "/" + name); + + return f.isDirectory(); + } + }; + + // return 1 = success, -1 = bad file name, 0 = no csv files found + // FileNotFound and IO Exceptions need to be handled + public static int importData(SQLiteDatabase db) throws Exception { + File directory = new File(IMPORT_PATH); + String[] files = directory.list(csvFilter); + + if (files.length > 0) { + for (String fileName : files) { + Log.e("Files", fileName); + FileReader file = new FileReader(IMPORT_PATH + File.separator + fileName); + BufferedReader buffer = new BufferedReader(file); + String line = ""; + + if (fileName.equalsIgnoreCase("member.csv")) { + String tableName = "member"; +// String columns = "memb_code, memb_name, zoon_code, Cobf_type, memb_type, accno, rategrno, bank_code, BankAcNo, membName_Eng, AcNo"; +// String str1 = "INSERT INTO " + tableName + " (" + columns + ") values("; +// String str2 = ");"; + + Log.e("member", "IN"); + db.beginTransaction(); + while ((line = buffer.readLine()) != null) { + Log.e("Lines:", line); +// StringBuilder sb = new StringBuilder(str1); + String[] str = line.split(","); +// if (str.length != 11) { +// Log.d("CSVParser", "Skipping Bad CSV Row"); +// continue; +// } +// sb.append("'" + str[0] + "',"); +// sb.append(str[1] + "',"); +// sb.append(str[2] + "',"); +// sb.append(str[3] + "',"); +// sb.append(str[4] + "',"); +// sb.append(str[5] + "',"); +// sb.append(str[6] + "',"); +// sb.append(str[7] + "',"); +// sb.append(str[8] + "',"); +// sb.append(str[9] + "',"); +// sb.append(str[10] + "'"); +// sb.append(str2); +// db.execSQL(sb.toString()); + ContentValues cv = new ContentValues(11); + cv.put("memb_code", str[0].trim()); + cv.put("memb_name", str[1].trim()); + cv.put("zoon_code", str[2].trim()); + cv.put("Cobf_type", str[3].trim()); + cv.put("memb_type", str[4].trim()); + cv.put("accno", str[5].trim()); + cv.put("rategrno", str[6].trim()); + cv.put("bank_code", str[7].trim()); + cv.put("BankAcNo", str[8].trim()); + cv.put("membName_Eng", str[9].trim()); + cv.put("AcNo", str[10].trim()); + db.insert(tableName, "memb_code", cv); + } + db.setTransactionSuccessful(); + db.endTransaction(); + + return 1; + } + if (fileName.equalsIgnoreCase("item.csv")) { + String tableName = "item"; +// String columns = "itcode, itname, opqty, oprate, opbal, issue, rece, clqty, rate, purchAc, saleAc, saleCr"; +// String str1 = "INSERT INTO " + tableName + " (" + columns + ") values("; +// String str2 = ");"; + + Log.e("item", "IN"); + db.beginTransaction(); + while ((line = buffer.readLine()) != null) { + Log.e("Lines:", line); +// StringBuilder sb = new StringBuilder(str1); + String[] str = line.split(","); +// if (str.length != 12) { +// Log.d("CSVParser", "Skipping Bad CSV Row"); +// continue; +// } +// sb.append("'" + str[0] + "',"); +// sb.append(str[1] + "',"); +// sb.append(str[2] + "',"); +// sb.append(str[3] + "',"); +// sb.append(str[4] + "',"); +// sb.append(str[5] + "',"); +// sb.append(str[6] + "',"); +// sb.append(str[7] + "',"); +// sb.append(str[8] + "',"); +// sb.append(str[9] + "',"); +// sb.append(str[10] + "',"); +// sb.append(str[11] + "'"); +// sb.append(str2); +// db.execSQL(sb.toString()); + ContentValues cv = new ContentValues(12); + cv.put("itcode", str[0].trim()); + cv.put("itname", str[1].trim()); + cv.put("opqty", str[2].trim()); + cv.put("oprate", str[3].trim()); + cv.put("opbal", str[4].trim()); + cv.put("issue", str[5].trim()); + cv.put("rece", str[6].trim()); + cv.put("clqty", str[7].trim()); + cv.put("rate", str[8].trim()); + cv.put("purchAc", str[9].trim()); + cv.put("saleAc", str[10].trim()); + cv.put("saleCr", str[11].trim()); + db.insert(tableName, "itcode", cv); + } + db.setTransactionSuccessful(); + db.endTransaction(); + + return 1; + } + if (fileName.equalsIgnoreCase("ratemst.csv")) { + String tableName = "ratemst"; +// String columns = "rtgrno, RtDate, cobf, rtno, fat, rate, degree, snf"; +// String str1 = "INSERT INTO " + tableName + " (" + columns + ") values("; +// String str2 = ");"; + + Log.e("ratemaster", "IN"); + db.beginTransaction(); + while ((line = buffer.readLine()) != null) { + Log.e("Lines:", line); +// StringBuilder sb = new StringBuilder(str1); + String[] str = line.split(","); +// if (str.length != 8) { +// Log.d("CSVParser", "Skipping Bad CSV Row"); +// continue; +// } +// sb.append("'" + str[0] + "',"); +// sb.append(str[1] + "',"); +// sb.append(str[2] + "',"); +// sb.append(str[3] + "',"); +// sb.append(str[4] + "',"); +// sb.append(str[5] + "',"); +// sb.append(str[6] + "',"); +// sb.append(str[7] + "'"); +// sb.append(str2); +// db.execSQL(sb.toString()); + ContentValues cv = new ContentValues(8); + cv.put("rtgrno", str[0].trim()); + cv.put("RtDate", str[1].trim()); + cv.put("cobf", str[2].trim()); + cv.put("rtno", str[3].trim()); + cv.put("fat", str[4].trim()); + cv.put("rate", str[5].trim()); + cv.put("degree", str[6].trim()); + cv.put("snf", str[7].trim()); + db.insert(tableName, "rtno", cv); + } + db.setTransactionSuccessful(); + db.endTransaction(); + + return 1; + } + if (fileName.equalsIgnoreCase("Rt_grmst.csv")) { + String tableName = "Rt_grmst"; +// String columns = "RateGrno, RateGrname, RateTyp, CowRate, BufRate"; +// String str1 = "INSERT INTO " + tableName + " (" + columns + ") values("; +// String str2 = ");"; + + Log.e("Rt_grmst", "IN"); + db.beginTransaction(); + while ((line = buffer.readLine()) != null) { + Log.e("Lines:", line); +// StringBuilder sb = new StringBuilder(str1); + String[] str = line.split(","); + if (str.length != 5) { + Log.e("CSVParser", "Skipping Bad CSV Row"); + continue; + } +// sb.append("'" + str[0] + "',"); +// sb.append(str[1] + "',"); +// sb.append(str[2] + "',"); +// sb.append(str[3] + "'"); +// sb.append(str[4] + "'"); +// sb.append(str2); +// db.execSQL(sb.toString()); + ContentValues cv = new ContentValues(5); + cv.put("RateGrno", str[0].trim()); + cv.put("RateGrname", str[1].trim()); + cv.put("RateTyp", str[2].trim()); + cv.put("CowRate", str[3].trim()); + cv.put("BufRate", str[4].trim()); + + } + db.setTransactionSuccessful(); + db.endTransaction(); + + return 1; + } + Log.e("Return:", "-1 Bad file name"); + return -1; + } + } + Log.e("Return:", "0 No files"); + return 0; + } +} diff --git a/app/src/main/res/drawable/ic_import.xml b/app/src/main/res/drawable/ic_import.xml new file mode 100644 index 0000000..2c266d9 --- /dev/null +++ b/app/src/main/res/drawable/ic_import.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml new file mode 100644 index 0000000..f12a583 --- /dev/null +++ b/app/src/main/res/menu/menu_main.xml @@ -0,0 +1,9 @@ + + + + \ No newline at end of file