// Path: app/src/main/java/org/clintonhealthaccess/lmis/app/models/reports/UtilizationValue.java
// @Getter
// @ToString
// public class UtilizationValue {
//     int day;
//     int value;
//     public UtilizationValue(int day, int value){
//         this.day = day;
//         this.value = value;
//     }
// }


import android.content.Context;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.inject.Inject;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import org.clintonhealthaccess.lmis.app.models.Commodity;
import org.clintonhealthaccess.lmis.app.models.Dispensing;
import org.clintonhealthaccess.lmis.app.models.DispensingItem;
import org.clintonhealthaccess.lmis.app.models.reports.UtilizationValue;
import org.clintonhealthaccess.lmis.app.persistence.DbUtil;
import org.clintonhealthaccess.lmis.app.utils.DateUtil;
import org.clintonhealthaccess.lmis.app.utils.Helpers;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import static com.j256.ormlite.android.apptools.OpenHelperManager.getHelper;
import static com.j256.ormlite.dao.DaoManager.createDao;
        int length = String.valueOf(numberOfDispensingsToPatientsThisMonth).length();
        if (length < 4) {
            for (int i = 0; i < 4 - length; i++) {
                stringOfZeros += "0";
            }
        }
        return String.format("%s%d-%s", stringOfZeros, numberOfDispensingsToPatientsThisMonth + 1, currentMonth);
    }

    private int getDispensingsToPatientsThisMonth() {
        return dbUtil.withDao(Dispensing.class, new DbUtil.Operation<Dispensing, Integer>() {
            @Override
            public Integer operate(Dao<Dispensing, String> dao) throws SQLException {
                QueryBuilder<Dispensing, String> dispensingStringQueryBuilder = dao.queryBuilder();
                Date firstDay = Helpers.firstDayOfMonth(new Date());
                Date lastDay = Helpers.lastDayOfMonth(new Date());
                dispensingStringQueryBuilder.where().between("created", firstDay, lastDay);
                PreparedQuery<Dispensing> query = dispensingStringQueryBuilder.prepare();
                List<Dispensing> dispensingList = dao.query(query);
                return dispensingList.size();
            }


        });
    }

    public int getDispensedTotalValue(Commodity commodity) {
        return GenericService.getTotal(commodity, DateUtil.today(), DateUtil.today(), Dispensing.class, DispensingItem.class, context);
    }

// Path: app/src/main/java/org/clintonhealthaccess/lmis/app/models/reports/UtilizationValue.java
// @Getter
// @ToString
// public class UtilizationValue {
//     int day;
//     int value;
//     public UtilizationValue(int day, int value){
//         this.day = day;
//         this.value = value;
//     }
// }


// Path: app/src/main/java/org/clintonhealthaccess/lmis/app/services/DispensingService.java
import android.content.Context;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.inject.Inject;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import org.clintonhealthaccess.lmis.app.models.Commodity;
import org.clintonhealthaccess.lmis.app.models.Dispensing;
import org.clintonhealthaccess.lmis.app.models.DispensingItem;
import org.clintonhealthaccess.lmis.app.models.reports.UtilizationValue;
import org.clintonhealthaccess.lmis.app.persistence.DbUtil;
import org.clintonhealthaccess.lmis.app.utils.DateUtil;
import org.clintonhealthaccess.lmis.app.utils.Helpers;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import static com.j256.ormlite.android.apptools.OpenHelperManager.getHelper;
import static com.j256.ormlite.dao.DaoManager.createDao;

        int length = String.valueOf(numberOfDispensingsToPatientsThisMonth).length();
        if (length < 4) {
            for (int i = 0; i < 4 - length; i++) {
                stringOfZeros += "0";
            }
        }
        return String.format("%s%d-%s", stringOfZeros, numberOfDispensingsToPatientsThisMonth + 1, currentMonth);
    }

    private int getDispensingsToPatientsThisMonth() {
        return dbUtil.withDao(Dispensing.class, new DbUtil.Operation<Dispensing, Integer>() {
            @Override
            public Integer operate(Dao<Dispensing, String> dao) throws SQLException {
                QueryBuilder<Dispensing, String> dispensingStringQueryBuilder = dao.queryBuilder();
                Date firstDay = Helpers.firstDayOfMonth(new Date());
                Date lastDay = Helpers.lastDayOfMonth(new Date());
                dispensingStringQueryBuilder.where().between("created", firstDay, lastDay);
                PreparedQuery<Dispensing> query = dispensingStringQueryBuilder.prepare();
                List<Dispensing> dispensingList = dao.query(query);
                return dispensingList.size();
            }


        });
    }

    public int getDispensedTotalValue(Commodity commodity) {
        return GenericService.getTotal(commodity, DateUtil.today(), DateUtil.today(), Dispensing.class, DispensingItem.class, context);
    }

    public List<UtilizationValue> getDispensedValues(Commodity commodity, Date startDate, Date endDate, boolean forVial) {