I am developing a customized version of Point-of-Sale app. There are different suppliers from whom the products are purchased and to whom the products are sometimes returned. The following information is stored in SQLite while returning the product.
- Product name
- Return quantity
- Return price
- Supplier name
- Return date
I intend to perform the ∑(return price * return quantity) of products returned to every individual supplier for a particular date using Query option.
Although, I am able to perform ∑(return price * return quantity) based on individual supplier, for some reasons I am unable to perform the sum based on date query.
I am using a spinner to select the supplier and using a date picker for choosing a date. I am not sure where I am going wrong. All the codes are posted below. Any assistance in pointing the errors would be helpful
A method inside db activity to perform Math function
public double supplierReturnsTotal(String supplierID, String date) {
SQLiteDatabase db = helper.getReadableDatabase();
double result = 0;
String selection = VivzHelper.COLUMN_SUPPLIER_RETURNS_SUPPLIER_ID + " =? "
+ " AND " + VivzHelper.COLUMN_SUPPLIER_RETURNS_DATE + " =? ";
String[] selectionArgs = {supplierID, date};
Cursor c = db.query(VivzHelper.SUPPLIER_RETURNS_TABLE,
new String[] { "sum(" + VivzHelper.COLUMN_SUPPLIER_RETURNS_PRICE + ")" },
selection,
selectionArgs,
null /* you may add a GROUP BY attribute (e.g. KEY_Test) here */,
null /* and here a HAVING for your group-by-clause */,
null);
if (c.moveToFirst()) {
result = c.getDouble(0);
}
c.close();
return result;
}
Initializing variables
// For date picker
public static final String DEFAULT = "N/A";
ImageButton btn;
Calendar c = Calendar.getInstance();
int year_x = c.get(Calendar.YEAR);
int month_x = c.get(Calendar.MONTH);
int day_x = c.get(Calendar.DATE);
static final int DIALOG_ID = 0;
TextView myDate;
// Supplier Name using shared preferences
String param1 = null;
// Initializing date
// DD-MM-YYYY using tag1 for displaying to user
// YYYY-MM-DD using tag2 for query
String tag1, tag2;
Date picker code
public void showDialogOnButtonClick() {
btn = (ImageButton) findViewById(R.id.supplierReturnsDatumButton);
btn.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DIALOG_ID);
}
}
);
}
@Override
protected Dialog onCreateDialog(int id) {
if (id == DIALOG_ID) {
return new DatePickerDialog(this, dPickerListener, year_x, month_x, day_x);
}
return null;
}
public DatePickerDialog.OnDateSetListener dPickerListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
year_x = year;
month_x = monthOfYear + 1; //In java month starts with 0. Hence + 1
day_x = dayOfMonth;
String datum = Integer.toString(day_x);
String monat = Integer.toString(month_x);
String jahr = Integer.toString(year_x);
//Below procedure adds 0 before the numeric value of datum and monat if their numeric value is below 10 (say 2 becomes 02)
tag1 = (day_x < 10 ? ("0" + datum) : (datum)) + "-" + (month_x < 10 ? ("0" + monat) : (monat)) + "-" + jahr;
tag2 = jahr + "-" + (month_x < 10 ? ("0" + monat) : (monat)) + "-" + (day_x < 10 ? ("0" + datum) : (datum));
Message.message(SupplierReturns.this, tag1);
myDate.setText(tag1);
}
};
aggregate() called inside onCreate() of Java activity
public void aggregate() {
//Shared Preferences////////////////////////////////////////////////////////////////////////////////
SharedPreferences myPreferences = getSharedPreferences("sharedSupplierData", Context.MODE_PRIVATE);
param1 = myPreferences.getString("sharedSupplierName", DEFAULT);
if (param1.equals(DEFAULT)) {
Message.message(this, "No data was found");
} else {
Message.message(this, "Data loaded Successfully");
}
String supplierID = vivzHelper.getSupplierID(param1);
if (tag2.length() != 0) {
double sumValue = vivzHelper.supplierReturnsTotal(supplierID, tag2);
DecimalFormat df = new DecimalFormat("#.##");
returnSum.setText(df.format(sumValue));
} else {
date.setToNow();
String meinDatum = String.valueOf(date.format("%Y-%m-%d"));
double sumValue = vivzHelper.supplierReturnsTotal(supplierID, meinDatum);
DecimalFormat df = new DecimalFormat("#.##");
returnSum.setText(df.format(sumValue));
}
}
The idea is to select a supplier, a date using date picker, use the Selection in Query option of SQLite, filter the date based on supplier (code is working when using without date) and date (code crashes).
Aucun commentaire:
Enregistrer un commentaire