Coverage Report - org.paneris.jammyjoes.model.Product
 
Classes in this File Line Coverage Branch Coverage Complexity
Product
0%
0/153
0%
0/54
1.773
Product$StockValues
0%
0/13
0%
0/2
1.773
 
 1  
 package org.paneris.jammyjoes.model;
 2  
 
 3  
 import java.sql.Date;
 4  
 import java.util.Enumeration;
 5  
 
 6  
 import org.melati.poem.AccessPoemException;
 7  
 import org.melati.poem.Persistent;
 8  
 import org.melati.poem.ValidationPoemException;
 9  
 import org.melati.util.StringUtils;
 10  
 import org.paneris.jammyjoes.model.generated.ProductBase;
 11  
 import org.paneris.jammyjoes.util.JammyJoesUtil;
 12  
 
 13  
 public class Product extends ProductBase {
 14  
 
 15  
   private Double quantitySold;
 16  
   private Double quantityPurchased;
 17  
   private Double quantityShopSale;
 18  
   private Double quantityInternetSale;
 19  
   private Double quantityTelephoneSale;
 20  
   private Double quantityStockCheckIncrease;
 21  
   private Double quantityStockCheckDecrease;
 22  
   private Double quantityCorrectLevel;
 23  
   private Double quantityAjustmentRequired;
 24  
 
 25  
   public void setVatexempt(Boolean cooked) throws AccessPoemException, ValidationPoemException {
 26  0
     super.setVatexempt(cooked);
 27  0
     setRetailpriceincvat(getRetailpriceincvat());
 28  0
   }
 29  
 
 30  0
   public Product() {
 31  0
   }
 32  
 
 33  
   public boolean hasStatus(ProductStatus status) {
 34  0
     if (getStatus().equals(status)) {
 35  0
       return true;
 36  
     }
 37  0
     return false;
 38  
   }
 39  
 
 40  
   public boolean getHasStock() {
 41  0
     if (getStocklevel().intValue() > 0) {
 42  0
       return true;
 43  
     }
 44  0
     return false;
 45  
   }
 46  
 
 47  
   public boolean getDoWeSellIt() {
 48  0
     if (getHasStock()) {
 49  0
       return true;
 50  
     }
 51  0
     ProductStatusTable statuses =  getJammyjoesDatabaseTables().getProductStatusTable();
 52  0
     if (hasStatus(statuses.getNew()) || hasStatus(statuses.getNormallyStocked())) {
 53  0
       return true;
 54  
     }
 55  0
     return false;
 56  
   }
 57  
 
 58  
   public boolean getDiscontinued() {
 59  0
     if (getStatus().equals(getJammyjoesDatabaseTables().getProductStatusTable().getDiscontinued())){
 60  0
       return true;
 61  
     }
 62  0
     return false;
 63  
   }
 64  
   
 65  
   public Double getPreSaleprice() {
 66  0
     return getSaleprice();
 67  
   }
 68  
 
 69  
   // there doesn't seem to be a better place to put this
 70  
   public Double getCostpriceIncVat() {
 71  0
     if (isVatexempt()) {
 72  0
       return new Double(getCostprice().doubleValue());
 73  
     } else {
 74  0
       return new Double(getCostprice().doubleValue() * 1.175);
 75  
     }
 76  
   }
 77  
 
 78  
   public void setRetailpriceincvat(Double cooked)
 79  
     throws AccessPoemException, ValidationPoemException {
 80  0
     if (cooked != null) {
 81  0
       if (isVatexempt()) {
 82  0
         setRetailprice(cooked.doubleValue());
 83  
       } else {
 84  0
         setRetailprice(cooked.doubleValue() / 1.175);
 85  
       }
 86  
     }
 87  0
     super.setRetailpriceincvat(cooked);
 88  0
   }
 89  
 
 90  
   private boolean isVatexempt() {
 91  0
     return getVatexempt().booleanValue();
 92  
   }
 93  
 
 94  
   public void setRetailprice(Double cooked) throws AccessPoemException, ValidationPoemException {
 95  0
     calculateMargin(cooked, getCostprice());
 96  0
     super.setRetailprice(cooked);
 97  0
   }
 98  
 
 99  
   public void setCostprice(Double cooked) throws AccessPoemException, ValidationPoemException {
 100  0
     calculateMargin(getRetailprice(), cooked);
 101  0
     super.setCostprice(cooked);
 102  0
   }
 103  
 
 104  
   public void calculateMargin(Double retailprice, Double costprice) {
 105  0
     if (retailprice != null && costprice != null) {
 106  0
       double margin = retailprice.doubleValue() - costprice.doubleValue();
 107  0
       setMargin(margin);
 108  0
       setMarginpercent(margin / retailprice.doubleValue() * 100);
 109  0
       setMarkuppercent(margin / costprice.doubleValue() * 100);
 110  
     }
 111  0
   }
 112  
 
 113  
   public void clearQuantities() {
 114  0
     quantitySold = null;
 115  0
     quantityPurchased = null;
 116  0
     quantityShopSale = null;
 117  0
     quantityInternetSale = null;
 118  0
     quantityTelephoneSale = null;
 119  0
     quantityStockCheckIncrease = null;
 120  0
     quantityStockCheckDecrease = null;
 121  0
     quantityCorrectLevel = null;
 122  0
     quantityAjustmentRequired = null;
 123  0
   }
 124  
 
 125  
   private void calculateQuantities() {
 126  0
     StockTransactionTypeTable st = getJammyjoesDatabaseTables().getStockTransactionTypeTable();
 127  0
     quantityPurchased = calculateQuantity(st.getPurchase());
 128  0
     quantityShopSale = calculateQuantity(st.getShopSale());
 129  0
     quantityInternetSale = calculateQuantity(st.getInternetSale());
 130  0
     quantityTelephoneSale = calculateQuantity(st.getTelephoneSale());
 131  0
     quantityStockCheckIncrease = calculateQuantity(st.getStockCheckIncreasee());
 132  0
     quantityStockCheckDecrease = calculateQuantity(st.getStockCheckDecrease());
 133  0
     quantitySold = new Double(quantityShopSale.doubleValue() + quantityInternetSale.doubleValue() + quantityTelephoneSale.doubleValue());
 134  0
     quantityCorrectLevel =
 135  
       new Double(
 136  
         quantityPurchased.doubleValue()
 137  
           - quantitySold.doubleValue()
 138  
           + quantityStockCheckIncrease.doubleValue()
 139  
           - quantityStockCheckDecrease.doubleValue());
 140  0
     quantityAjustmentRequired = quantityCorrectLevel;
 141  0
     if (getStocklevel() != null)
 142  0
       quantityAjustmentRequired =
 143  
         new Double(quantityCorrectLevel.doubleValue() - getStocklevel().doubleValue());
 144  0
   }
 145  
 
 146  
   private Double calculateQuantity(StockTransactionType type) {
 147  0
     return calculateQuantityToDate(type, null);
 148  
   }
 149  
 
 150  
   private Double calculateQuantityToDate(StockTransactionType type, Date date) {
 151  0
     return calculateQuantity(type, date, true);
 152  
   }
 153  
 
 154  
   private Double calculateQuantityFromDate(StockTransactionType type, Date date) {
 155  0
     return calculateQuantity(type, date, false);
 156  
   }
 157  
 
 158  
   private Double calculateQuantity(StockTransactionType type, Date date, boolean to) {
 159  0
     double total = 0;
 160  0
     String selection =
 161  
       q("product") + " = " + getTroid() + " AND " + q("type") + " = " + type.getTroid();
 162  
     // FIXME date format for query
 163  0
     if (date != null) {
 164  0
       selection += " AND " + q("date");
 165  0
       if (to) {
 166  0
         selection += " < ";
 167  
       } else {
 168  0
         selection += " >= ";
 169  
       }
 170  0
       selection += JammyJoesUtil.formatDateForSQL(getDatabase(), date);
 171  
     }
 172  0
     Enumeration e =
 173  
       getJammyjoesDatabaseTables()
 174  
         .getStockTransactionTable()
 175  
         .cachedSelection(selection, null)
 176  
         .objects();
 177  0
     while (e.hasMoreElements()) {
 178  0
       StockTransaction t = (StockTransaction) e.nextElement();
 179  0
       total += t.getQuantity().doubleValue();
 180  0
     }
 181  0
     return new Double(total);
 182  
   }
 183  
 
 184  
   private String q(String name) {
 185  0
       StringBuffer b = new StringBuffer();
 186  0
       StringUtils.appendQuoted(b, name, '"');
 187  0
       return b.toString();
 188  
   }
 189  
 
 190  
   public double getQuantityPurchased() {
 191  0
     if (quantityPurchased == null)
 192  0
       calculateQuantities();
 193  0
     return quantityPurchased.doubleValue();
 194  
   }
 195  
 
 196  
   public double getQuantityShopSale() {
 197  0
     if (quantityShopSale == null)
 198  0
       calculateQuantities();
 199  0
     return quantityShopSale.doubleValue();
 200  
   }
 201  
 
 202  
   public double getQuantityInternetSale() {
 203  0
     if (quantityInternetSale == null)
 204  0
       calculateQuantities();
 205  0
     return quantityInternetSale.doubleValue();
 206  
   }
 207  
 
 208  
   public double getQuantityTelephoneSale() {
 209  0
     if (quantityTelephoneSale == null)
 210  0
       calculateQuantities();
 211  0
     return quantityTelephoneSale.doubleValue();
 212  
   }
 213  
 
 214  
   public double getQuantityStockCheckIncrease() {
 215  0
     if (quantityStockCheckIncrease == null)
 216  0
       calculateQuantities();
 217  0
     return quantityStockCheckIncrease.doubleValue();
 218  
   }
 219  
 
 220  
   public double getQuantityStockCheckDecrease() {
 221  0
     if (quantityStockCheckDecrease == null)
 222  0
       calculateQuantities();
 223  0
     return quantityStockCheckDecrease.doubleValue();
 224  
   }
 225  
 
 226  
   public double getQuantitySold() {
 227  0
     if (quantitySold == null)
 228  0
       calculateQuantities();
 229  0
     return quantitySold.doubleValue();
 230  
   }
 231  
 
 232  
   public double getQuantityCorrectLevel() {
 233  0
     if (quantityCorrectLevel == null)
 234  0
       calculateQuantities();
 235  0
     return quantityCorrectLevel.doubleValue();
 236  
   }
 237  
 
 238  
   public int getQuantityAjustmentRequired() {
 239  0
     if (quantityAjustmentRequired == null)
 240  0
       calculateQuantities();
 241  0
     return quantityAjustmentRequired.intValue();
 242  
   }
 243  
 
 244  
   public double getRequiredOrderValue() {
 245  0
     return getCostprice().doubleValue() * getReorderquantity().intValue();
 246  
   }
 247  
 
 248  
   /**
 249  
    * when duplicating, the new Persistent needs a zero stock level
 250  
    */
 251  
   public Persistent duplicated() throws AccessPoemException {
 252  0
     Product prod = (Product) super.duplicated();
 253  0
     prod.setStocklevel(0);
 254  0
     return prod;
 255  
   }
 256  
 
 257  
   public Double getStockLevel(Date date) {
 258  0
     StockTransactionTypeTable st = getJammyjoesDatabaseTables().getStockTransactionTypeTable();
 259  0
     Double quantityPurchased = calculateQuantityToDate(st.getPurchase(), date);
 260  0
     Double quantityStockCheckIncrease = calculateQuantityToDate(st.getStockCheckIncreasee(), date);
 261  0
     Double quantityStockCheckDecrease = calculateQuantityToDate(st.getStockCheckDecrease(), date);
 262  0
     Double quantitySold = getQuantitySoldToDate(date);
 263  0
     return new Double(
 264  
       quantityPurchased.doubleValue()
 265  
         - quantitySold.doubleValue()
 266  
         + quantityStockCheckIncrease.doubleValue()
 267  
         - quantityStockCheckDecrease.doubleValue());
 268  
   }
 269  
 
 270  
   public Double getQuantitySoldToDate(Date date) {
 271  0
     StockTransactionTypeTable st = getJammyjoesDatabaseTables().getStockTransactionTypeTable();
 272  0
     Double quantityShopSale = calculateQuantityToDate(st.getShopSale(), date);
 273  0
     Double quantityInternetSale = calculateQuantityToDate(st.getInternetSale(), date);
 274  0
     Double quantityTelephoneSale = calculateQuantityToDate(st.getTelephoneSale(), date);
 275  0
     Double quantitySold =
 276  
       new Double(quantityShopSale.doubleValue() + quantityInternetSale.doubleValue() + quantityTelephoneSale.doubleValue());
 277  0
     return quantitySold;
 278  
   }
 279  
 
 280  
   public Double getQuantitySoldFromDate(Date date) {
 281  0
     StockTransactionTypeTable st = getJammyjoesDatabaseTables().getStockTransactionTypeTable();
 282  0
     Double quantityShopSale = calculateQuantityFromDate(st.getShopSale(), date);
 283  0
     Double quantityTelephoneSale = calculateQuantityToDate(st.getTelephoneSale(), date);
 284  0
     Double quantityInternetSale = calculateQuantityFromDate(st.getInternetSale(), date);
 285  0
     Double quantitySold =
 286  
       new Double(quantityShopSale.doubleValue() + quantityInternetSale.doubleValue() + quantityTelephoneSale.doubleValue());
 287  0
     return quantitySold;
 288  
   }
 289  
 
 290  
   public StockValues getStockLevels(Date date) {
 291  0
     double stockLevel = 0;
 292  0
     if (getStocklevel() != null)
 293  0
       stockLevel = getStocklevel().doubleValue();
 294  0
     if (date != null)
 295  0
       stockLevel = getStockLevel(date).doubleValue();
 296  0
     return new StockValues(this, stockLevel);
 297  
   }
 298  
 
 299  
   public class StockValues {
 300  
     private Product product;
 301  0
     private double stockLevel = 0;
 302  
 
 303  0
     public StockValues(Product p, double sl) {
 304  0
       product = p;
 305  0
       stockLevel = sl;
 306  0
     }
 307  
 
 308  
     public double getValue(Double price) {
 309  0
       if (price == null)
 310  0
         return 0;
 311  0
       return stockLevel * price.doubleValue();
 312  
     }
 313  
 
 314  
     public double getLevel() {
 315  0
       return stockLevel;
 316  
     }
 317  
 
 318  
     public double getCostPriceExVat() {
 319  0
       return getValue(product.getCostprice());
 320  
     }
 321  
 
 322  
     public double getCostPriceIncVat() {
 323  0
       return product.getCostpriceIncVat().doubleValue();
 324  
     }
 325  
 
 326  
     public double getRetailPriceExVat() {
 327  0
       return getValue(product.getRetailprice());
 328  
     }
 329  
 
 330  
     public double getRetailPriceIncVat() {
 331  0
       return getValue(product.getRetailpriceincvat());
 332  
     }
 333  
   }
 334  
 
 335  
   public Boolean getNew() {
 336  0
     if (getStatus().equals(getJammyjoesDatabaseTables().getProductStatusTable().getNew()))
 337  0
       return Boolean.TRUE;
 338  0
     return Boolean.FALSE;
 339  
   }
 340  
 
 341  
   public StockTransaction newStockTransaction(Date date, StockTransactionType type, Integer quantity) {
 342  0
     StockTransaction st = (StockTransaction)getJammyjoesDatabaseTables().getStockTransactionTable().newPersistent();
 343  0
     st.setProduct(this);
 344  0
     st.setDate(date);
 345  0
     st.setType(type);
 346  0
     st.setQuantity(quantity);
 347  0
     st.setCostprice(getCostprice());
 348  0
     st.setRetailpriceincvat(getRetailpriceincvat());
 349  0
     st.setVatexempt(getVatexempt());
 350  0
     return st;
 351  
   }
 352  
 }