1 package org.paneris.jammyjoes.controller;
2
3 import java.util.Enumeration;
4 import java.util.LinkedList;
5 import java.util.List;
6 import java.util.Vector;
7
8 import org.melati.Melati;
9 import org.melati.poem.AccessPoemException;
10 import org.melati.poem.AccessToken;
11 import org.melati.poem.BaseFieldAttributes;
12 import org.melati.poem.Capability;
13 import org.melati.poem.Field;
14 import org.melati.poem.PoemThread;
15 import org.melati.servlet.Form;
16 import org.melati.template.ServletTemplateContext;
17 import org.melati.template.TemplateContext;
18 import org.melati.template.TemplateEngine;
19 import org.melati.util.Email;
20 import org.melati.poem.util.EnumUtils;
21 import org.melati.util.MelatiStringWriter;
22 import org.paneris.jammyjoes.mail.HtmlMail;
23 import org.paneris.jammyjoes.mail.MailFascade;
24 import org.paneris.jammyjoes.model.JammyjoesDatabase;
25 import org.paneris.jammyjoes.model.Product;
26 import org.paneris.jammyjoes.model.Supplier;
27
28 import org.paneris.jammyjoes.servlet.JammyJoesMelatiServlet;
29
30 public class PurchaseOrder extends JammyJoesMelatiServlet {
31
32 private static final long serialVersionUID = 1L;
33
34 private ProductSearch util;
35
36 protected String jammyjoesRequest(Melati melati, ServletTemplateContext context) throws Exception {
37
38 util = new ProductSearch(null, 0, new Logger(null));
39 JammyjoesDatabase db = (JammyjoesDatabase) melati.getDatabase();
40 Capability admin = db.getCanAdminister();
41 AccessToken token = PoemThread.accessToken();
42 if (!token.givesCapability(admin))
43 throw new AccessPoemException(token, admin);
44 String template = "view/PurchaseOrder.wm";
45
46 Integer type = Form.getIntegerField(context, "field_type");
47 Integer supplier = Form.getIntegerField(context, "field_supplier");
48 Integer manufacturer = Form.getIntegerField(context, "field_manufacturer");
49 boolean sendit = Form.getBooleanField(context, "sendit").booleanValue();
50 context.put("selectedType", type);
51 context.put("selectedManufacturer", manufacturer);
52
53 context.put(
54 "type",
55 new Field(
56 type,
57 new BaseFieldAttributes(db.getProductTable().getTypeColumn(), true)));
58 context.put(
59 "supplier",
60 new Field(
61 supplier,
62 new BaseFieldAttributes(db.getProductTable().getSupplierColumn(), true)));
63 context.put(
64 "manufacturer",
65 new Field(
66 manufacturer,
67 new BaseFieldAttributes(db.getProductTable().getManufacturerColumn(),
68 true)));
69
70 if (sendit || Form.getFormNulled(context, "submit") != null) {
71 Vector where = new Vector();
72 List results = new LinkedList();
73 if (type != null)
74 where.add(q("id") + " = producttype.product and producttype.type = " + type);
75 if (supplier != null)
76 where.add(q("supplier") + " = " + supplier);
77 if (manufacturer != null) {
78 where.add(q("manufacturer") + " = " + manufacturer);
79 }
80 where.add(q("stocklevel") + " <= " + q("reorderlevel"));
81 where.add(q("reorderquantity") + " > 0");
82 where.add(discontinued(db));
83
84 String whereClause = EnumUtils.concatenated(" AND ", where.elements());
85 Enumeration r = db.getProductTable().selection(whereClause, null, false);
86 double totalCostPrice = 0;
87 double totalCostPriceIncVat = 0;
88 double count = 0;
89 while (r.hasMoreElements()) {
90 Product p = (Product) r.nextElement();
91 results.add(p);
92 count++;
93 if (p.getReorderquantity() != null) {
94 totalCostPrice += p.getReorderquantity().doubleValue() * p.getCostprice().doubleValue();
95 totalCostPriceIncVat += p.getReorderquantity().doubleValue()
96 * p.getCostpriceIncVat().doubleValue();
97 }
98 }
99 context.put("totalCostPrice", new Double(totalCostPrice));
100 context.put("totalCostPriceIncVat", new Double(totalCostPriceIncVat));
101 context.put("vat", new Double(totalCostPriceIncVat - totalCostPrice));
102 Supplier supplierRec = (Supplier) db.getSupplierTable().getObject(supplier);
103 context.put("selectedSupplier", supplierRec);
104 if (supplierRec.getMinimumamount() != null
105 && supplierRec.getMinimumamount().doubleValue() > totalCostPriceIncVat)
106 context.put("notenough", Boolean.TRUE);
107 context.put("results", results);
108 if (sendit) {
109 sendJammyJoesEmail(db, melati.getTemplateEngine(), supplierRec.getEmail(), context);
110 context.put("sent", Boolean.TRUE);
111 }
112 }
113 return template;
114 }
115
116 private void sendJammyJoesEmail(JammyjoesDatabase db, TemplateEngine engine, String to, TemplateContext context) throws Exception {
117
118 MelatiStringWriter writer = new MelatiStringWriter();
119 engine.expandTemplate(writer,"view/PurchaseOrderEmail.wm",context);
120 MailFascade facade = new MailFascade(db.getSettingTable().get(Email.SMTPSERVER));
121 HtmlMail mail = facade.createHtmlMail(writer.toString());
122
123 mail.setRecipient("sales@jammyjoes.co.uk");
124 facade.send(mail);
125 }
126
127 private String discontinued(JammyjoesDatabase db) {
128 String discontinued =
129 "(" + q("status") + " != " + db.getProductStatusTable().getDiscontinued().getTroid();
130 discontinued += " AND "
131 + q("status")
132 + " != "
133 + db.getProductStatusTable().getNotStocked().getTroid()
134 + ")";
135 return discontinued;
136 }
137
138 public String q(String name) {
139 return util.q(name);
140 }
141
142 }