1 package org.paneris.jammyjoes.model; 2 3 import java.util.Enumeration; 4 5 import org.melati.poem.CachedCount; 6 import org.melati.poem.CachedSelection; 7 import org.paneris.jammyjoes.model.generated.AffiliateBase; 8 9 public class Affiliate extends AffiliateBase { 10 public Affiliate() {} 11 12 public String ordersSQL(boolean paidonly) { 13 JammyjoesDatabase db = (JammyjoesDatabase)getDatabase(); 14 String paid = (paidonly) ? " AND affiliatepaid IS TRUE" : ""; 15 return "affiliate = " + getTroid() + " AND status = " + 16 db.getOrderStatusTable().getFufilled().getTroid() + paid; 17 } 18 19 public CachedSelection getOrders(boolean paidonly) { 20 return getJammyjoesDatabaseTables().getShopOrderTable().cachedSelection( 21 ordersSQL(paidonly), null); 22 } 23 24 public CachedSelection getOrders() { 25 return getOrders(false); 26 } 27 28 public CachedCount cachedOrdersCount() { 29 return getJammyjoesDatabaseTables().getShopOrderTable().cachedCount(ordersSQL(false)); 30 } 31 32 public double totalOrders() { 33 double total = 0; 34 Enumeration en = getOrders(false).objects(); 35 while (en.hasMoreElements()) { 36 ShopOrder order = (ShopOrder)en.nextElement(); 37 total += order.getAmount().doubleValue(); 38 } 39 return total; 40 } 41 42 public double money(boolean paid) { 43 double total = 0; 44 Enumeration en = getOrders(paid).objects(); 45 while (en.hasMoreElements()) { 46 ShopOrder order = (ShopOrder)en.nextElement(); 47 total += order.getComission().doubleValue() * order.getAmount().doubleValue() / 100; 48 } 49 return total; 50 } 51 52 public double moneyEarnt() { 53 return money(false); 54 } 55 56 public double moneyPaid() { 57 return money(true); 58 } 59 60 public double moneyOwed() { 61 return moneyEarnt() - moneyPaid(); 62 } 63 64 }