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