| 1 | |
package org.paneris.jammyjoes.controller; |
| 2 | |
|
| 3 | |
import java.sql.Date; |
| 4 | |
import java.util.Enumeration; |
| 5 | |
import java.util.HashMap; |
| 6 | |
import java.util.Iterator; |
| 7 | |
import java.util.LinkedList; |
| 8 | |
import java.util.List; |
| 9 | |
import java.util.Map; |
| 10 | |
import java.util.Vector; |
| 11 | |
|
| 12 | |
import org.melati.Melati; |
| 13 | |
import org.melati.poem.AccessPoemException; |
| 14 | |
import org.melati.poem.AccessToken; |
| 15 | |
import org.melati.poem.BaseFieldAttributes; |
| 16 | |
import org.melati.poem.Capability; |
| 17 | |
import org.melati.poem.Field; |
| 18 | |
import org.melati.poem.PoemThread; |
| 19 | |
import org.melati.poem.util.EnumUtils; |
| 20 | |
import org.melati.servlet.Form; |
| 21 | |
import org.melati.template.ServletTemplateContext; |
| 22 | |
import org.melati.template.SimpleDateAdaptor; |
| 23 | |
import org.paneris.jammyjoes.model.JammyjoesDatabase; |
| 24 | |
import org.paneris.jammyjoes.model.Product; |
| 25 | |
import org.paneris.jammyjoes.model.StockTransaction; |
| 26 | |
import org.paneris.jammyjoes.model.StockTransactionTypeTable; |
| 27 | |
import org.paneris.jammyjoes.mvp.CollectionSorter; |
| 28 | |
import org.paneris.jammyjoes.servlet.JammyJoesMelatiServlet; |
| 29 | |
import org.paneris.jammyjoes.util.JammyJoesUtil; |
| 30 | |
import org.webmacro.servlet.WebContext; |
| 31 | |
|
| 32 | 0 | public class SalesAnalysis extends JammyJoesMelatiServlet { |
| 33 | |
|
| 34 | |
private static final long serialVersionUID = 1L; |
| 35 | |
|
| 36 | 0 | public class StockTransactionSummary implements Comparable { |
| 37 | |
|
| 38 | |
private Product product; |
| 39 | |
private StockTransactionTypeTable table; |
| 40 | 0 | private int internet = 0; |
| 41 | 0 | private int telephone = 0; |
| 42 | 0 | private int shop = 0; |
| 43 | 0 | private double purchases = 0D; |
| 44 | 0 | private double sales = 0D; |
| 45 | 0 | private double vatSales = 0D; |
| 46 | 0 | private double vatPurchases = 0D; |
| 47 | |
|
| 48 | |
public double getPurchases() { |
| 49 | 0 | return purchases; |
| 50 | |
} |
| 51 | |
|
| 52 | |
public int getInternet() { |
| 53 | 0 | return internet; |
| 54 | |
} |
| 55 | |
|
| 56 | |
public Product getProduct() { |
| 57 | 0 | return product; |
| 58 | |
} |
| 59 | |
|
| 60 | |
public double getSales() { |
| 61 | 0 | return sales; |
| 62 | |
} |
| 63 | |
|
| 64 | |
public int getShop() { |
| 65 | 0 | return shop; |
| 66 | |
} |
| 67 | |
|
| 68 | |
public int getQuantity() { |
| 69 | 0 | return shop + internet + telephone; |
| 70 | |
} |
| 71 | |
|
| 72 | |
public int getTelephone() { |
| 73 | 0 | return telephone; |
| 74 | |
} |
| 75 | |
|
| 76 | |
public double getVatSales() { |
| 77 | 0 | return vatSales; |
| 78 | |
} |
| 79 | |
|
| 80 | |
public double getVatPurchases() { |
| 81 | 0 | return vatPurchases; |
| 82 | |
} |
| 83 | |
|
| 84 | |
public double getVatToPay() { |
| 85 | 0 | return vatSales - vatPurchases; |
| 86 | |
} |
| 87 | |
|
| 88 | 0 | public StockTransactionSummary(StockTransactionTypeTable table) { |
| 89 | 0 | this.table = table; |
| 90 | 0 | } |
| 91 | |
|
| 92 | |
public void add(StockTransaction transaction) { |
| 93 | 0 | this.product = transaction.getProduct(); |
| 94 | 0 | int quantity = transaction.getQuantity().intValue(); |
| 95 | 0 | if (transaction.getType().equals(table.getInternetSale())) { |
| 96 | 0 | internet += quantity; |
| 97 | |
} |
| 98 | 0 | if (transaction.getType().equals(table.getTelephoneSale())) { |
| 99 | 0 | telephone += quantity; |
| 100 | |
} |
| 101 | 0 | if (transaction.getType().equals(table.getShopSale())) { |
| 102 | 0 | shop += quantity; |
| 103 | |
} |
| 104 | 0 | double thisSales = transaction.getRetailpriceincvat().doubleValue() * quantity; |
| 105 | 0 | double thisPurchases = transaction.getCostprice().doubleValue() * quantity; |
| 106 | 0 | if (!transaction.getVatexempt().booleanValue()) { |
| 107 | 0 | vatSales += thisSales / 1.175 * 0.175; |
| 108 | 0 | thisPurchases = thisPurchases * 1.175; |
| 109 | 0 | vatPurchases += thisPurchases / 1.175 * 0.175; |
| 110 | |
} |
| 111 | 0 | purchases += thisPurchases; |
| 112 | 0 | sales += thisSales; |
| 113 | 0 | } |
| 114 | |
|
| 115 | |
public int compareTo(Object o) { |
| 116 | 0 | StockTransactionSummary other = (StockTransactionSummary)o; |
| 117 | 0 | return other.getProfitDouble().compareTo(this.getProfitDouble()); |
| 118 | |
} |
| 119 | |
|
| 120 | |
public double getProfit() { |
| 121 | 0 | return sales - vatSales - purchases +vatPurchases; |
| 122 | |
} |
| 123 | |
|
| 124 | |
public double getMargin() { |
| 125 | 0 | return (getProfit() / sales) * 100; |
| 126 | |
} |
| 127 | |
|
| 128 | |
public Double getProfitDouble() { |
| 129 | 0 | return new Double(getProfit()); |
| 130 | |
} |
| 131 | |
|
| 132 | |
} |
| 133 | |
|
| 134 | |
protected String jammyjoesRequest(Melati melati, ServletTemplateContext context) throws Exception { |
| 135 | |
|
| 136 | 0 | JammyjoesDatabase db = (JammyjoesDatabase) melati.getDatabase(); |
| 137 | 0 | ServletTemplateContext tc = melati.getServletTemplateContext(); |
| 138 | 0 | Capability admin = db.getCanAdminister(); |
| 139 | 0 | AccessToken token = PoemThread.accessToken(); |
| 140 | 0 | if (!token.givesCapability(admin)) |
| 141 | 0 | throw new AccessPoemException(token, admin); |
| 142 | 0 | String template = "view/SalesAnalysis.wm"; |
| 143 | |
|
| 144 | 0 | SimpleDateAdaptor sd = new SimpleDateAdaptor(); |
| 145 | 0 | Date startdate = (Date) sd.rawFrom(tc, "startdate"); |
| 146 | 0 | Date enddate = (Date) sd.rawFrom(tc, "enddate"); |
| 147 | 0 | context.put("startdate", startdate); |
| 148 | 0 | context.put("enddate", enddate); |
| 149 | |
|
| 150 | 0 | Integer transactionType = Form.getIntegerField(tc, "field_type"); |
| 151 | 0 | Integer supplier = Form.getIntegerField(tc, "field_supplier"); |
| 152 | 0 | Integer manufacturer = Form.getIntegerField(tc, "field_manufacturer"); |
| 153 | |
|
| 154 | 0 | context.put( |
| 155 | |
"transactionType", |
| 156 | |
new Field( |
| 157 | |
transactionType, |
| 158 | |
new BaseFieldAttributes(db.getStockTransactionTable().getTypeColumn(), true))); |
| 159 | |
|
| 160 | 0 | context.put( |
| 161 | |
"supplier", |
| 162 | |
new Field( |
| 163 | |
supplier, |
| 164 | |
new BaseFieldAttributes(db.getProductTable().getSupplierColumn(), true))); |
| 165 | 0 | context.put( |
| 166 | |
"manufacturer", |
| 167 | |
new Field( |
| 168 | |
manufacturer, |
| 169 | |
new BaseFieldAttributes(db.getProductTable().getManufacturerColumn(), true))); |
| 170 | |
|
| 171 | 0 | StockTransactionTypeTable sttt = db.getStockTransactionTypeTable(); |
| 172 | |
|
| 173 | 0 | if (Form.getFormNulled(tc, "submit") != null) { |
| 174 | 0 | Vector where = new Vector(); |
| 175 | 0 | where.add("product = product.id"); |
| 176 | |
|
| 177 | 0 | if (transactionType == null) { |
| 178 | 0 | String typeClause = "(type = " + sttt.getInternetSale().getTroid() + " OR "; |
| 179 | 0 | typeClause += "type = " + sttt.getTelephoneSale().getTroid() + " OR "; |
| 180 | 0 | typeClause += "type = " + sttt.getShopSale().getTroid() + ")"; |
| 181 | 0 | where.add(typeClause); |
| 182 | 0 | } else { |
| 183 | 0 | where.add("(type = " + transactionType + ")"); |
| 184 | |
} |
| 185 | |
|
| 186 | 0 | if (startdate != null) { |
| 187 | 0 | where.add("date >= " + JammyJoesUtil.formatDateForSQL(db, startdate)); |
| 188 | |
} |
| 189 | 0 | if (enddate != null) { |
| 190 | 0 | where.add("date < " + JammyJoesUtil.formatDateForSQL(db, enddate)); |
| 191 | |
} |
| 192 | |
|
| 193 | 0 | if (supplier != null) |
| 194 | 0 | where.add("product.supplier = " + supplier); |
| 195 | 0 | if (manufacturer != null) |
| 196 | 0 | where.add("product.manufacturer = " + manufacturer); |
| 197 | |
|
| 198 | 0 | String whereClause = EnumUtils.concatenated(" AND ", where.elements()); |
| 199 | 0 | Enumeration r = db.getStockTransactionTable().selection(whereClause, "product.name", false); |
| 200 | |
|
| 201 | 0 | Map products = new HashMap(); |
| 202 | |
|
| 203 | 0 | while (r.hasMoreElements()) { |
| 204 | 0 | StockTransaction transaction = (StockTransaction) r.nextElement(); |
| 205 | 0 | Integer id = transaction.getProduct().getTroid(); |
| 206 | 0 | StockTransactionSummary summary = (StockTransactionSummary)products.get(id); |
| 207 | 0 | if (summary == null) { |
| 208 | 0 | summary = new StockTransactionSummary(db.getStockTransactionTypeTable()); |
| 209 | |
} |
| 210 | 0 | summary.add(transaction); |
| 211 | 0 | products.put(id, summary); |
| 212 | 0 | } |
| 213 | |
|
| 214 | 0 | int internetTotal = 0; |
| 215 | 0 | int telephoneTotal = 0; |
| 216 | 0 | int shopTotal = 0; |
| 217 | 0 | int stockTotal = 0; |
| 218 | 0 | double purchasesTotal = 0D; |
| 219 | 0 | double salesTotal = 0D; |
| 220 | 0 | double salesVatTotal = 0D; |
| 221 | 0 | double purchasesVatTotal = 0D; |
| 222 | |
|
| 223 | 0 | List results = new LinkedList(); |
| 224 | 0 | Iterator iter = new CollectionSorter().sort(products).iterator(); |
| 225 | 0 | while (iter.hasNext()) { |
| 226 | 0 | StockTransactionSummary summary = (StockTransactionSummary) iter.next(); |
| 227 | 0 | results.add(summary); |
| 228 | 0 | internetTotal += summary.getInternet(); |
| 229 | 0 | telephoneTotal += summary.getTelephone(); |
| 230 | 0 | shopTotal += summary.getShop(); |
| 231 | 0 | purchasesTotal += summary.getPurchases(); |
| 232 | 0 | salesTotal += summary.getSales(); |
| 233 | 0 | salesVatTotal += summary.getVatSales(); |
| 234 | 0 | purchasesVatTotal += summary.getVatPurchases(); |
| 235 | 0 | stockTotal += summary.getProduct().getStocklevel().intValue(); |
| 236 | 0 | } |
| 237 | 0 | double profitTotal = salesTotal; |
| 238 | 0 | profitTotal -= salesVatTotal; |
| 239 | 0 | profitTotal -= purchasesTotal; |
| 240 | 0 | profitTotal += purchasesVatTotal; |
| 241 | |
|
| 242 | 0 | double marginTotal = (profitTotal / salesTotal) * 100; |
| 243 | |
|
| 244 | 0 | double quantityTotal = internetTotal + telephoneTotal + shopTotal; |
| 245 | 0 | WebContext webContext = (WebContext)context.getContext(); |
| 246 | |
|
| 247 | 0 | webContext.put("internetTotal", internetTotal); |
| 248 | 0 | webContext.put("telephoneTotal", telephoneTotal); |
| 249 | 0 | webContext.put("quantityTotal", quantityTotal); |
| 250 | 0 | webContext.put("stockTotal", stockTotal); |
| 251 | 0 | webContext.put("shopTotal", shopTotal); |
| 252 | 0 | webContext.put("purchasesTotal", purchasesTotal); |
| 253 | 0 | webContext.put("salesTotal", salesTotal); |
| 254 | 0 | webContext.put("profitTotal", profitTotal); |
| 255 | 0 | webContext.put("marginTotal", marginTotal); |
| 256 | 0 | webContext.put("salesVatTotal", salesVatTotal); |
| 257 | 0 | webContext.put("purchasesVatTotal", purchasesVatTotal); |
| 258 | 0 | webContext.put("vatToPayTotal", salesVatTotal - purchasesVatTotal); |
| 259 | 0 | webContext.put("results", results); |
| 260 | |
} |
| 261 | 0 | return template; |
| 262 | |
} |
| 263 | |
|
| 264 | |
} |