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