1 package org.paneris.jammyjoes.model;
2
3 import java.sql.Date;
4 import java.util.Map;
5
6 import org.melati.poem.AccessPoemException;
7 import org.melati.poem.DeletionIntegrityPoemException;
8 import org.melati.poem.ValidationPoemException;
9 import org.paneris.jammyjoes.model.generated.StockTransactionBase;
10
11 public class StockTransaction extends StockTransactionBase {
12
13 public StockTransaction() {}
14
15 public void setDate(Date cooked)
16 throws AccessPoemException, ValidationPoemException {
17 if (cooked == null) cooked = new Date(new java.util.Date().getTime());
18 super.setDate(cooked);
19 }
20
21 public final void delete(Map map) throws AccessPoemException, DeletionIntegrityPoemException {
22 int currentValue = 0;
23 if (getQuantity() != null) currentValue = getQuantity().intValue();
24 setThisProductQuantity(-currentValue);
25 super.delete(map);
26 }
27
28 public void setThisProductQuantity(int q) {
29 if (q != 0) {
30 Product p = getProduct();
31 StockTransactionType t = getType();
32 if (p == null) throw new TransactionException("An attempt was made to set the quantity for a transaction that doesn't reference a product");
33 if (t == null) throw new TransactionException("An attempt was made to set the quantity for a transaction that doesn't have a type");
34 p.clearQuantities();
35 int currentValue = 0;
36 if (p.getStocklevel() != null) currentValue = p.getStocklevel().intValue();
37 if (t.getReducestock().booleanValue()) {
38 p.setStocklevel(currentValue - q);
39 } else {
40 p.setStocklevel(currentValue + q);
41 }
42 }
43 }
44
45
46 public void setQuantity(Integer cooked)
47 throws AccessPoemException, ValidationPoemException {
48 int currentValue = 0;
49 int newValue = 0;
50 if (getQuantity() != null) currentValue = getQuantity().intValue();
51 if (cooked != null) newValue = cooked.intValue();
52 setThisProductQuantity(newValue - currentValue);
53 super.setQuantity(cooked);
54 }
55
56 public void setTypeTroid(Integer raw) throws AccessPoemException {
57 if (getType() != null && getType().getTroid() != raw) {
58 int currentValue = 0;
59 if (getQuantity() != null) currentValue = getQuantity().intValue();
60 setThisProductQuantity(-currentValue);
61 super.setTypeTroid(raw);
62 setThisProductQuantity(currentValue);
63 } else {
64 super.setTypeTroid(raw);
65 }
66 }
67
68 public void setProductTroid(Integer raw) throws AccessPoemException {
69 if (getProduct() != null && getProduct().getTroid() != raw) {
70 int currentValue = 0;
71 if (getQuantity() != null) currentValue = getQuantity().intValue();
72 setThisProductQuantity(-currentValue);
73 super.setProductTroid(raw);
74 setThisProductQuantity(currentValue);
75 } else {
76 super.setProductTroid(raw);
77 }
78 }
79
80 }