Add newStock calculation forgot in previous versions

This commit is contained in:
jacques 2022-07-14 13:23:32 +02:00
parent 70ac8b3d1e
commit e1cfc2a375
2 changed files with 23 additions and 1 deletions

View file

@ -50,8 +50,14 @@ public class AlarmReceiver extends BroadcastReceiver {
PrescriptionDatabase prescriptions = PrescriptionDatabase.getInstanceDatabase(context.getApplicationContext()); PrescriptionDatabase prescriptions = PrescriptionDatabase.getInstanceDatabase(context.getApplicationContext());
PrescriptionsDAO prescriptionsDAO = prescriptions.getPrescriptionsDAO(); PrescriptionsDAO prescriptionsDAO = prescriptions.getPrescriptionsDAO();
List<Prescription> prescriptionList = prescriptionsDAO.getAllMedics(); List<Prescription> prescriptionList = prescriptionsDAO.getAllMedics();
Prescription firstPrescription = null ; Prescription firstPrescription = null;
Prescription currentPrescription;
for (int i=0 ; i < prescriptionList.size(); i++ ) {
currentPrescription = prescriptionList.get(i);
currentPrescription.newStock();
prescriptionsDAO.update(currentPrescription);
}
// Sorting list by dateEndOfStock // Sorting list by dateEndOfStock
Utils.sortPrescriptionList(prescriptionList); Utils.sortPrescriptionList(prescriptionList);

View file

@ -1,5 +1,9 @@
package net.foucry.pilldroid.models; package net.foucry.pilldroid.models;
import static net.foucry.pilldroid.UtilDate.nbOfDaysBetweenDateAndToday;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
@ -129,6 +133,7 @@ public class Prescription implements Serializable {
public void setGeneric_type(Integer generic_type) { public void setGeneric_type(Integer generic_type) {
this.generic_type = generic_type; this.generic_type = generic_type;
} }
public Date getDateEndOfStock() { public Date getDateEndOfStock() {
int numberDayOfTake; int numberDayOfTake;
if (this.getTake() > 0) { if (this.getTake() > 0) {
@ -144,6 +149,17 @@ public class Prescription implements Serializable {
return calendar.getTime(); return calendar.getTime();
} }
public void newStock() {
Date lastUpdate = new Date(getLast_update());
int numberOfDays = UtilDate.nbOfDaysBetweenDateAndToday(lastUpdate);
if (numberOfDays > 0) {
double takeDuringPeriod = this.take * numberOfDays;
setStock((float) (getStock() - takeDuringPeriod));
setLast_update(new Date().getTime());
}
}
} }