1 package org.paneris.jammyjoes.model;
2
3 import java.sql.Timestamp;
4 import java.util.Enumeration;
5 import java.util.HashSet;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Set;
9
10 import org.melati.poem.AccessPoemException;
11 import org.paneris.jammyjoes.model.generated.ShopOrderBase;
12 import org.paneris.jammyjoes.mvp.AffiliateTotals;
13 import org.paneris.jammyjoes.util.JammyJoesUtil;
14
15 public class ShopOrder extends ShopOrderBase implements Graphable {
16 public ShopOrder() {
17 }
18
19 public void total(AffiliateTotals totals) {
20 totals.total(getAmountForGraph());
21 totals.commission(getComissionAmount());
22 if (getAffiliatepaid().booleanValue()) {
23 totals.paidCommission(getComissionAmount());
24 } else {
25 totals.outstandingCommission(getComissionAmount());
26 }
27 }
28
29 public double getComissionAmount() {
30 if (getComission() == null) {
31 return 0d;
32 }
33 return JammyJoesUtil.roundTo2dp(getAmountForGraph() * getComission().doubleValue() / 100);
34 }
35
36 public double getWrappingAmount() {
37 if (getHasWrapping()) {
38 return WrappingTable.WRAPPING_COST;
39 } else {
40 return 0d;
41 }
42 }
43
44 public boolean getHasWrapping() {
45 if (getWrapping() == null)
46 return false;
47 WrappingTable wrappingTable = getJammyjoesDatabaseTables().getWrappingTable();
48 if (getWrapping().equals(wrappingTable.getNoWrapping()))
49 return false;
50 return true;
51 }
52
53 public double getAmountForGraph() {
54 return getAmountUK().doubleValue() - getDeliveryUK().doubleValue();
55 }
56
57 public Enumeration getItems() {
58 return getJammyjoesDatabaseTables().getShopOrderItemTable().getOrderColumn().selectionWhereEq(
59 troid());
60 }
61
62 public boolean hasStatus(List statuses) {
63 Iterator iter = statuses.iterator();
64 while (iter.hasNext()) {
65 OrderStatus status = (OrderStatus) iter.next();
66 if (getStatus().equals(status))
67 return true;
68 }
69 return false;
70 }
71
72
73 public void removeItems() {
74 if (troid() != null) {
75 Set toRemove = new HashSet();
76 for (Enumeration en = getItems(); en.hasMoreElements();) {
77 toRemove.add(en.nextElement());
78 }
79 for (Iterator it = toRemove.iterator(); it.hasNext();) {
80 ShopOrderItem item = (ShopOrderItem) it.next();
81 item.deleteAndCommit();
82 }
83 }
84 }
85
86 protected void writeLock() {
87 super.writeLock();
88 if (getDate_unsafe() == null) {
89 setDate_unsafe(now());
90 }
91 }
92
93 private static java.sql.Timestamp now() {
94 return new java.sql.Timestamp(new java.util.Date().getTime());
95 }
96
97 public void setStatus(OrderStatus cooked) throws AccessPoemException {
98 if (getFufilleddate_unsafe() == null
99 && cooked == getJammyjoesDatabaseTables().getOrderStatusTable().getFufilled()) {
100 setFufilleddate_unsafe(now());
101 }
102 super.setStatus(cooked);
103 }
104
105 public void setFufilleddate_unsafe(Timestamp cooked) {
106
107 if (getStatus() != getJammyjoesDatabaseTables().getOrderStatusTable().getFufilled()
108 || cooked != null) {
109 super.setFufilleddate_unsafe(cooked);
110 }
111 }
112
113 }