import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;


import org.sql2o.*;

public class Inventory {
  private int mId;
  private int mIngredientId;
  private int mCurrentOnHand;
  private LocalDate mDeliveryDate;
  private LocalDate mExpirationDate;

  public Inventory(int ingredientId, int currentOnHand) {
    mIngredientId = ingredientId;
    mCurrentOnHand = currentOnHand;
    mDeliveryDate = LocalDate.now();
    mExpirationDate = mDeliveryDate.plusDays(Ingredient.find(ingredientId).getShelfLifeDays());
  }

  public int getId() {
    return mId;
  }

  public int getIngredientId() {
    return mIngredientId;
  }

  public int getCurrentOnHand() {
    return mCurrentOnHand;
  }

  public LocalDate getDeliveryDate() {
    return mDeliveryDate;
  }

  public LocalDate getExpirationDate() {
    return mExpirationDate;
  }

  public int getShelfLifeDays() {
    try (Connection con = DB.sql2o.open()) {
      String sql = "SELECT ingredients.shelf_life_days AS mShelfLifeDays FROM ingredients WHERE ingredients.id = :id";
      return con.createQuery(sql)
        .addParameter("id", mIngredientId)
        .executeScalar(Integer.class);
    }
  }

  @Override
  public boolean equals(Object otherInventory) {
    if(!(otherInventory instanceof Inventory)) {
      return false;
    } else {
      Inventory newInventory = (Inventory) otherInventory;
      return this.getId() == newInventory.getId() &&
             this.getIngredientId() == newInventory.getIngredientId() &&
             this.getCurrentOnHand() == newInventory.getCurrentOnHand() &&
             this.getDeliveryDate().equals(newInventory.getDeliveryDate()) &&
             this.getExpirationDate() == newInventory.getExpirationDate();
    }
  }

  public void save() {
    try (Connection con = DB.sql2o.open()) {
      String sql = "INSERT INTO inventories (ingredient_id, current_on_hand, TO_DATE (delivery_date, 'YYYY-MM-DD'), TO_DATE(expiration_date, 'YYYY-MM-DD')) VALUES (:ingredientId, :currentOnHand, :deliveryDate, :expirationDate)";
      mId = (int) con.createQuery(sql, true)
        .addParameter("ingredientId", mIngredientId)
        .addParameter("currentOnHand", mCurrentOnHand)
        .addParameter("deliveryDate", mDeliveryDate)
        .addParameter("expirationDate", mExpirationDate)
        .executeUpdate()
        .getKey();
    }
  }

  public static Inventory find(int inventoryId) {
    try (Connection con = DB.sql2o.open()) {
      String sql = "SELECT id AS mId, ingredient_id AS mIngredientId, current_on_hand AS mCurrentOnHand, delivery_date AS mDeliveryDate, expiration_date AS mExpirationDate FROM inventories WHERE id = :id";
      Inventory inventory = con.createQuery(sql)
        .addParameter("id", inventoryId)
        .executeAndFetchFirst(Inventory.class);
    }
  }

  public static List<Inventory> all() {
    try (Connection con = DB.sql2o.open()) {
      String sql = "SELECT id AS mId, ingredient_id AS mIngredientId, current_on_hand AS mCurrentOnHand, delivery_date AS mDeliveryDate, expiration_date AS mExpirationDate FROM inventories";
      return con.createQuery(sql).executeAndFetch(Inventory.class);
    }
  }

  public List<Ingredient> getIngredients() {
    try (Connection con = DB.sql2o.open()) {
      String sql = "SELECT ingredients.id AS mId, ingredients.name AS mName, ingredients.unit AS mUnit, ingredients.desired_on_hand AS mDesiredOnHand, ingredients.shelf_life_days AS mShelfLifeDays FROM ingredients INNER JOIN inventories ON (ingredients.id = inventories.ingredient_id) ORDER BY ingredients.name.";
    return con.createQuery(sql).executeAndFetch(Ingredient.class);
    }
  }
}

@@ -208,25 +208,22 @@
 private 
-LocalDate
+String
  mDelive
@@ -240,25 +240,22 @@
 private 
-LocalDate
+String
  mExpira
@@ -428,16 +428,27 @@
 te.now()
+.toString()
 ;%0A    mE
@@ -463,29 +463,31 @@
 nDate = 
-mDeliveryDate
+LocalDate.now()
 .plusDay
@@ -537,16 +537,27 @@
 eDays())
+.toString()
 ;%0A  %7D%0A%0A 
@@ -734,33 +734,30 @@
 %7D%0A%0A  public 
-LocalDate
+String
  getDelivery
@@ -805,25 +805,22 @@
  public 
-LocalDate
+String
  getExpi
@@ -1908,17 +1908,8 @@
 nd, 
-TO_DATE (
 deli
@@ -1923,61 +1923,23 @@
 te, 
-'YYYY-MM-DD'), TO_DATE(expiration_date, 'YYYY-MM-DD')
+expiration_date
 ) VA
@@ -1955,17 +1955,18 @@
 gredient
-I
+_i
 d, :curr
@@ -1968,24 +1968,34 @@
 :current
-OnHand, 
+_on_hand, TO_DATE(
 :deliver
@@ -1995,22 +1995,46 @@
 delivery
-D
+_d
 ate, 
+'yyyy-mm-dd'), TO_DATE(
 :expirat
@@ -2036,20 +2036,36 @@
 piration
-D
+_d
 ate
+, 'yyyy-mm-dd')
 )%22;%0A    
@@ -2138,17 +2138,18 @@
 gredient
-I
+_i
 d%22, mIng
@@ -2189,19 +2189,21 @@
 %22current
-OnH
+_on_h
 and%22, mC
@@ -2247,17 +2247,18 @@
 delivery
-D
+_d
 ate%22, mD
@@ -2303,17 +2303,18 @@
 piration
-D
+_d
 ate%22, mE
@@ -2694,29 +2694,14 @@
     
-Inventory inventory =
+return
  con

change localdatetime usage to localdate