| 1 | |
package org.paneris.jammyjoes.controller; |
| 2 | |
|
| 3 | |
|
| 4 | |
import java.util.Date; |
| 5 | |
import java.util.Vector; |
| 6 | |
|
| 7 | |
import org.melati.Melati; |
| 8 | |
import org.melati.MelatiConfig; |
| 9 | |
import org.melati.poem.AccessPoemException; |
| 10 | |
import org.melati.poem.AccessToken; |
| 11 | |
import org.melati.poem.Capability; |
| 12 | |
import org.melati.poem.PoemThread; |
| 13 | |
import org.melati.servlet.Form; |
| 14 | |
import org.melati.template.ServletTemplateContext; |
| 15 | |
import org.melati.util.ContextUtil; |
| 16 | |
import org.paneris.jammyjoes.model.JammyjoesDatabase; |
| 17 | |
import org.paneris.jammyjoes.model.Product; |
| 18 | |
import org.paneris.jammyjoes.model.StockTransaction; |
| 19 | |
import org.paneris.jammyjoes.servlet.JammyJoesMelatiServlet; |
| 20 | |
|
| 21 | |
|
| 22 | 0 | public class StockEntry extends JammyJoesMelatiServlet { |
| 23 | |
|
| 24 | |
private static final long serialVersionUID = 1L; |
| 25 | |
|
| 26 | |
protected String jammyjoesRequest(Melati melati, ServletTemplateContext context) |
| 27 | |
throws Exception { |
| 28 | |
|
| 29 | 0 | JammyjoesDatabase db = (JammyjoesDatabase) melati.getDatabase(); |
| 30 | 0 | ServletTemplateContext tc = melati.getServletTemplateContext(); |
| 31 | 0 | Capability admin = db.getCanAdminister(); |
| 32 | 0 | AccessToken token = PoemThread.accessToken(); |
| 33 | 0 | if (!token.givesCapability(admin)) |
| 34 | 0 | throw new AccessPoemException(token,admin); |
| 35 | 0 | String template = "view/StockEntry.wm"; |
| 36 | 0 | String submit = Form.getFormNulled(tc,"submit"); |
| 37 | 0 | int itemsperpage = Form.getIntegerField(tc,"itemsperpage",new Integer(10)).intValue(); |
| 38 | 0 | Vector items = new Vector(); |
| 39 | 0 | boolean allgood = true; |
| 40 | |
|
| 41 | 0 | if (submit == null) { |
| 42 | 0 | allgood = false; |
| 43 | 0 | for (int i=0; i<itemsperpage; i++) { |
| 44 | 0 | items.add(new Item()); |
| 45 | |
} |
| 46 | |
} else { |
| 47 | 0 | if (submit.equals("Check")) { |
| 48 | 0 | for (int i=0; i<itemsperpage; i++) { |
| 49 | 0 | Integer id = Form.getIntegerField(tc,i+""); |
| 50 | 0 | if (id == null) { |
| 51 | 0 | items.add(new Item()); |
| 52 | |
} else { |
| 53 | 0 | Product p = (Product)db.getProductTable().getIdColumn().firstWhereEq(id); |
| 54 | 0 | if (p == null) { |
| 55 | 0 | allgood = false; |
| 56 | 0 | items.add(new Item(id)); |
| 57 | |
} else { |
| 58 | 0 | items.add(new Item(p)); |
| 59 | |
} |
| 60 | |
} |
| 61 | |
} |
| 62 | |
} |
| 63 | 0 | if (submit.equals("Confirm")) { |
| 64 | 0 | allgood = false; |
| 65 | 0 | for (int i=0; i<itemsperpage; i++) { |
| 66 | 0 | items.add(new Item()); |
| 67 | 0 | Integer id = Form.getIntegerField(tc,i+""); |
| 68 | 0 | if (id != null) { |
| 69 | 0 | Product p = (Product)db.getProductTable().getIdColumn().firstWhereEq(id); |
| 70 | 0 | Integer quantity = Form.getIntegerField(tc,"q"+id); |
| 71 | 0 | java.sql.Date date = (java.sql.Date)MelatiConfig.getSimpleDateAdaptor().rawFrom(melati.getServletTemplateContext(),"field_date"); |
| 72 | 0 | StockTransaction st = p.newStockTransaction(date, db.getStockTransactionTypeTable().getShopSale(),quantity); |
| 73 | 0 | db.getStockTransactionTable().create(st); |
| 74 | |
} |
| 75 | |
} |
| 76 | |
} |
| 77 | |
} |
| 78 | 0 | context.put("allgood",new Boolean(allgood)); |
| 79 | 0 | context.put("items",items); |
| 80 | 0 | context.put("util", new ContextUtil()); |
| 81 | 0 | StockTransaction dummyst = (StockTransaction)db.getStockTransactionTable().newPersistent(); |
| 82 | 0 | if (Form.getFormNulled(tc,"field_date") == null) { |
| 83 | 0 | dummyst.setDate(new java.sql.Date(new Date().getTime())); |
| 84 | |
} else { |
| 85 | 0 | dummyst.setDate((java.sql.Date)MelatiConfig.getSimpleDateAdaptor().rawFrom(melati.getServletTemplateContext(),"field_date")); |
| 86 | |
} |
| 87 | 0 | context.put("st", dummyst); |
| 88 | 0 | return template; |
| 89 | |
} |
| 90 | |
|
| 91 | 0 | public class Item { |
| 92 | |
|
| 93 | |
private Integer id; |
| 94 | |
private Product product; |
| 95 | |
|
| 96 | 0 | private Item() { |
| 97 | 0 | id = null; |
| 98 | 0 | product = null; |
| 99 | 0 | } |
| 100 | |
|
| 101 | 0 | private Item(Integer id) { |
| 102 | 0 | this.id = id; |
| 103 | 0 | product = null; |
| 104 | 0 | } |
| 105 | |
|
| 106 | 0 | private Item(Product p) { |
| 107 | 0 | product = p; |
| 108 | 0 | id = p.getTroid(); |
| 109 | 0 | } |
| 110 | |
|
| 111 | |
public Integer getId() { |
| 112 | 0 | return id; |
| 113 | |
} |
| 114 | |
|
| 115 | |
public Product getProduct() { |
| 116 | 0 | return product; |
| 117 | |
} |
| 118 | |
} |
| 119 | |
|
| 120 | |
} |