Author: bbrossaud Date: 2010-08-18 17:18:43 +0200 (Wed, 18 Aug 2010) New Revision: 52 Url: http://chorem.org/repositories/revision/billy/52 Log: addition cash Added: trunk/billy-ui-zk/src/main/java/org/chorem/billy/ui/CashController.java trunk/billy-ui-zk/src/main/webapp/cashPage.zul Modified: trunk/billy-ui-zk/src/main/java/org/chorem/billy/ui/BillyController.java trunk/billy-ui-zk/src/main/java/org/chorem/billy/ui/CategoryFormController.java trunk/billy-ui-zk/src/main/java/org/chorem/billy/ui/InvoiceController.java trunk/billy-ui-zk/src/main/webapp/billy.zul trunk/pom.xml Modified: trunk/billy-ui-zk/src/main/java/org/chorem/billy/ui/BillyController.java =================================================================== --- trunk/billy-ui-zk/src/main/java/org/chorem/billy/ui/BillyController.java 2010-08-16 10:15:57 UTC (rev 51) +++ trunk/billy-ui-zk/src/main/java/org/chorem/billy/ui/BillyController.java 2010-08-18 15:18:43 UTC (rev 52) @@ -22,4 +22,9 @@ include.setSrc(null); include.setSrc("categoryPage.zul"); } + + public void onClick$cash() { + include.setSrc(null); + include.setSrc("cashPage.zul"); + } } Added: trunk/billy-ui-zk/src/main/java/org/chorem/billy/ui/CashController.java =================================================================== --- trunk/billy-ui-zk/src/main/java/org/chorem/billy/ui/CashController.java (rev 0) +++ trunk/billy-ui-zk/src/main/java/org/chorem/billy/ui/CashController.java 2010-08-18 15:18:43 UTC (rev 52) @@ -0,0 +1,242 @@ +package org.chorem.billy.ui; + +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Set; + +import org.chorem.data.bonzoms.Category; +import org.chorem.data.bonzoms.Company; +import org.chorem.data.bonzoms.Invoice; +import org.chorem.data.bonzoms.SubCategory; +import org.nuiton.wikitty.Criteria; +import org.nuiton.wikitty.WikittyProxy; +import org.nuiton.wikitty.WikittyUtil; +import org.nuiton.wikitty.search.Element; +import org.nuiton.wikitty.search.Search; +import org.zkoss.zk.ui.Component; +import org.zkoss.zk.ui.util.GenericForwardComposer; +import org.zkoss.zul.Column; +import org.zkoss.zul.Columns; +import org.zkoss.zul.Datebox; +import org.zkoss.zul.Grid; +import org.zkoss.zul.Group; +import org.zkoss.zul.Groupfoot; +import org.zkoss.zul.Label; +import org.zkoss.zul.Row; +import org.zkoss.zul.Rows; + +public class CashController extends GenericForwardComposer { + + protected WikittyProxy proxy = ChoremDataProxy.getInstance(); + protected Date startDate = new Date(); + protected Date endDate = new Date(); + + protected Grid cashGrid; + + @Override + public void doAfterCompose(Component comp) throws Exception { + super.doAfterCompose(comp); + } + + protected int getStartMonth(int currentYear, int startYear) { + int startMonth = 1; + if (startYear == currentYear) { + SimpleDateFormat sdf = new SimpleDateFormat("MM"); + startMonth = Integer.valueOf(sdf.format(startDate)); + } + return startMonth; + } + + protected int getEndMonth(int currentYear, int endYear) { + int endMonth = 12; + if (endYear == currentYear) { + SimpleDateFormat sdf = new SimpleDateFormat("MM"); + endMonth = Integer.valueOf(sdf.format(endDate)); + } + return endMonth; + } + + protected void createHeaders(int startYear, int currentYear, int endYear, Columns head) { + NumberFormat nf = new DecimalFormat("00"); + int startMonth = getStartMonth(startYear, currentYear); + int endMonth = getEndMonth(currentYear, endYear); + while (startMonth <= endMonth) { + String label = nf.format(startMonth) + "/" + currentYear; + Column header = new Column(label); + head.appendChild(header); + ++startMonth; + } + } + + protected void createHead() { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); + String start = sdf.format(startDate); + String end = sdf.format(endDate); + int startYear = Integer.valueOf(start); + int endYear = Integer.valueOf(end); + Columns head = new Columns(); + cashGrid.appendChild(head); + Column header = new Column("Categories"); + head.appendChild(header); + for (int currentYear = startYear; currentYear <= endYear; ++currentYear) { + createHeaders(startYear, currentYear, endYear, head); + } + } + + protected Date getDate(int year, final int month) throws ParseException { + int m = month; + int y = year; + if (month > 12) { + m = 0; + ++y; + } + NumberFormat nf = new DecimalFormat("00"); + String monthStr = nf.format(m); + SimpleDateFormat sdf = new SimpleDateFormat("MMyyy"); + return sdf.parse(monthStr + y); + } + + protected double createLabelPrice(Date start, Date end, String id, Label label) throws ParseException { + Double price = new Double(0); + String first = WikittyUtil.formatDate(start); + String last = WikittyUtil.formatDate(end); + Search search = Search.query().eq(Invoice.FQ_FIELD_INVOICE_SUBCATEGORY, id); + search = search.lt(Invoice.FQ_FIELD_INVOICE_PAYMENT, last).ge(Invoice.FQ_FIELD_INVOICE_PAYMENT, first); + Criteria criteria = search.criteria(); + List<Invoice> invoices = proxy.findAllByCriteria(Invoice.class, criteria).getAll(); + String toolTips = ""; + for (Invoice invoice : invoices) { + Double ttc = invoice.getTTCPrice(); + Company company = proxy.restore(Company.class, invoice.getCompany()); + if (company != null) { + toolTips += company.getName(); + } + toolTips += " " + invoice.getNumber() + " = " + invoice.getTTCPrice() + " | "; + if (ttc != null) { + price += ttc; + } + } + label.setTooltiptext(toolTips); + return price; + } + + protected void createLabels(Row row, SubCategory sub, List<Double> total) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); + String start = sdf.format(startDate); + String end = sdf.format(endDate); + int startYear = Integer.valueOf(start); + int endYear = Integer.valueOf(end); + + for (int currentYear = startYear; currentYear <= endYear; ++currentYear) { + + int startMonth = getStartMonth(startYear, currentYear); + int endMonth = getEndMonth(currentYear, endYear); + + for (int count = 0; startMonth <= endMonth; ++count) { + Label label = new Label(); + Date first = getDate(currentYear, startMonth); + Date last = getDate(currentYear, startMonth + 1); + Double price = createLabelPrice(first, last, sub.getWikittyId(), label); + label.setValue(price.toString()); + row.appendChild(label); + if (total.size() < (count + 1)) { + total.add(price); + } else { + Double tmp = total.get(count); + tmp += price; + total.set(count, tmp); + } + ++startMonth; + } + } + } + + protected void createSubRows(List<String> ids, List<Double> total, Rows rows) throws ParseException { + List<SubCategory> subs = proxy.restore(SubCategory.class, ids); + subs = new ArrayList<SubCategory>(subs); + for (SubCategory sub : subs) { + Row row = new Row(); + rows.appendChild(row); + Label label = new Label(sub.getName()); + row.appendChild(label); + createLabels(row, sub, total); + + } + } + + protected void createRows() throws ParseException { + Criteria criteria = Search.query().eq(Element.ELT_EXTENSION, Category.EXT_CATEGORY).criteria(); + criteria = criteria.addSortDescending(Category.FQ_FIELD_CATEGORY_ORDER); + List<Category> categories = proxy.findAllByCriteria(Category.class, criteria).getAll(); + List<Double> total = new ArrayList<Double>(); + Rows rows = new Rows(); + cashGrid.appendChild(rows); + for (Category category : categories) { + Group group = new Group(); + Label groupLabel = new Label(category.getName()); + groupLabel.setStyle("font-weight: bold"); + group.appendChild(groupLabel); + rows.appendChild(group); + Set<String> ids = category.getSubCategory(); + if (ids != null) { + List<Double> prices = new ArrayList<Double>(); + createSubRows(new ArrayList<String>(ids), prices, rows); + int count = 0; + for (Double price : prices) { + if (total.size() < (count + 1)) { + total.add(price); + } else { + Double tmp = total.get(count); + tmp += price; + total.set(count, tmp); + } + Label label = new Label(price.toString()); + label.setStyle("font-weight: bold"); + group.appendChild(label); + ++count; + } + } + } + createTotal(total, rows); + } + + protected void createTotal(List<Double> totals, Rows rows) { + Groupfoot foot = new Groupfoot(); + rows.appendChild(foot); + Label label = new Label("Total"); + label.setStyle("font-weight: bold"); + foot.appendChild(label); + for (Double total : totals) { + Label totalLabel = new Label(total.toString()); + totalLabel.setStyle("font-weight: bold"); + foot.appendChild(totalLabel); + } + } + + protected void createGrid() throws ParseException { + createHead(); + createRows(); + } + + /* + * Events + */ + public void onClick$search() throws ParseException { + List<?> comps = cashGrid.getChildren(); + if (comps != null && !comps.isEmpty()) { + comps.clear(); + } + Datebox date = (Datebox) self.getFellow("startDatebox"); + startDate = date.getValue(); + date = (Datebox) self.getFellow("endDatebox"); + endDate = date.getValue(); + if (startDate != null && endDate != null) { + createGrid(); + } + } +} Modified: trunk/billy-ui-zk/src/main/java/org/chorem/billy/ui/CategoryFormController.java =================================================================== --- trunk/billy-ui-zk/src/main/java/org/chorem/billy/ui/CategoryFormController.java 2010-08-16 10:15:57 UTC (rev 51) +++ trunk/billy-ui-zk/src/main/java/org/chorem/billy/ui/CategoryFormController.java 2010-08-18 15:18:43 UTC (rev 52) @@ -181,9 +181,9 @@ SubCategory subCategory = (SubCategory) item.getValue(); if (subCategory != null) { subCategoryModel.removeData(subCategory); - String id = subCategory.getWikittyId(); - if (id != null && !id.isEmpty()) { - removedSubCategories.add(id); + String version = subCategory.getWikittyVersion(); + if (version != null && !version.equals("0.1")) { + removedSubCategories.add(subCategory.getWikittyId()); } clearSubCategoryFields(); } Modified: trunk/billy-ui-zk/src/main/java/org/chorem/billy/ui/InvoiceController.java =================================================================== --- trunk/billy-ui-zk/src/main/java/org/chorem/billy/ui/InvoiceController.java 2010-08-16 10:15:57 UTC (rev 51) +++ trunk/billy-ui-zk/src/main/java/org/chorem/billy/ui/InvoiceController.java 2010-08-18 15:18:43 UTC (rev 52) @@ -9,8 +9,8 @@ import org.chorem.billy.ui.ChoremDataProxy; import org.chorem.data.bonzoms.Company; import org.chorem.data.bonzoms.Invoice; -import org.chorem.data.bonzoms.Category; import org.chorem.data.bonzoms.Project; +import org.chorem.data.bonzoms.SubCategory; import org.nuiton.wikitty.Criteria; import org.nuiton.wikitty.WikittyProxy; import org.nuiton.wikitty.search.Element; @@ -65,10 +65,9 @@ Listcell cell = new Listcell(); String id = invoice.getSubCategory(); if (id != null && !id.isEmpty()) { - Criteria criteria = Search.query().contains(Category.FQ_FIELD_CATEGORY_SUBCATEGORY, id).criteria(); - Category category = proxy.findByCriteria(Category.class, criteria); - if (category != null) { - String name = category.getName(); + SubCategory sub = proxy.restore(SubCategory.class, id); + if (sub != null) { + String name = sub.getName(); if (name == null) { name = ""; } Modified: trunk/billy-ui-zk/src/main/webapp/billy.zul =================================================================== --- trunk/billy-ui-zk/src/main/webapp/billy.zul 2010-08-16 10:15:57 UTC (rev 51) +++ trunk/billy-ui-zk/src/main/webapp/billy.zul 2010-08-18 15:18:43 UTC (rev 52) @@ -9,6 +9,7 @@ <vbox> <toolbarbutton id="invoices" label="Invoices" /> <toolbarbutton id="categories" label="Categories" /> + <toolbarbutton id="cash" label="Cash" /> </vbox> </groupbox> <vbox id="content" > Added: trunk/billy-ui-zk/src/main/webapp/cashPage.zul =================================================================== --- trunk/billy-ui-zk/src/main/webapp/cashPage.zul (rev 0) +++ trunk/billy-ui-zk/src/main/webapp/cashPage.zul 2010-08-18 15:18:43 UTC (rev 52) @@ -0,0 +1,17 @@ +<?page id="Cash" title="Cash" cacheable="false" language="xul/html" zscriptLanguage="Java"?> +<?meta content="text/html; charset=UTF-8" pageEncoding="UTF-8"?> +<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" root="./cash"?> + +<window id="cash" apply="org.chorem.billy.ui.CashController"> + + <vbox> + <hbox> + Start : <datebox id="startDatebox" format="MM/yyyy"/> <space /> <space /> End : <datebox id="endDatebox" format="MM/yyyy" /> + </hbox> + <button id="search" label="Search" /> + </vbox> + + <grid id="cashGrid"> + </grid> + +</window> Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2010-08-16 10:15:57 UTC (rev 51) +++ trunk/pom.xml 2010-08-18 15:18:43 UTC (rev 52) @@ -229,7 +229,7 @@ <platform>chorem.org</platform> <chorem-data.version>0.1-SNAPSHOT</chorem-data.version> - <zk.version>5.0.2</zk.version> + <zk.version>5.0.3</zk.version> <wikitty.version>2.1-SNAPSHOT</wikitty.version> <!-- Nuiton librairies -->