This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository chorem. See http://git.chorem.org/chorem.git commit 79a93ec73bcdf0224187d223d5b8b57b81628618 Author: Jean Couteau <jean.couteau@gmail.com> Date: Fri Feb 5 16:08:33 2016 +0100 refs #1313 : Fix BillingPerAccount report --- .../financial/BillingPerAccountReportAction.java | 77 +++++++++++++++++----- .../financial/reports/billingPerAccountReport.jsp | 4 ++ 2 files changed, 65 insertions(+), 16 deletions(-) diff --git a/chorem-webmotion/src/main/java/org/chorem/webmotion/actions/financial/BillingPerAccountReportAction.java b/chorem-webmotion/src/main/java/org/chorem/webmotion/actions/financial/BillingPerAccountReportAction.java index a1ea506..868aebe 100644 --- a/chorem-webmotion/src/main/java/org/chorem/webmotion/actions/financial/BillingPerAccountReportAction.java +++ b/chorem-webmotion/src/main/java/org/chorem/webmotion/actions/financial/BillingPerAccountReportAction.java @@ -22,12 +22,15 @@ package org.chorem.webmotion.actions.financial; */ import org.chorem.ChoremClient; +import org.chorem.ChoremQueryMaker; import org.chorem.entities.*; import org.chorem.webmotion.actions.sales.SalesData; import org.chorem.webmotion.actions.sales.SalesReportHelper; import org.debux.webmotion.server.WebMotionController; import org.debux.webmotion.server.render.Render; import org.nuiton.wikitty.WikittyClient; +import org.nuiton.wikitty.entities.Element; +import org.nuiton.wikitty.entities.Wikitty; import org.nuiton.wikitty.query.WikittyQuery; import org.nuiton.wikitty.query.WikittyQueryMaker; @@ -56,10 +59,17 @@ public class BillingPerAccountReportAction extends WebMotionController { Map<Company, SalesData> billingData = getBillingPerAccountData(from, to, client); + Double total = 0.0; + + for (Map.Entry<Company, SalesData> entry : billingData.entrySet()) { + total += entry.getValue().getSales(); + } + List<Integer> listAllYearsInChorem = BillingReportHelper.listAllYears(client); return renderView("financial/reports/billingPerAccountReport.jsp", "data", billingData, + "total", total, "allYears", listAllYearsInChorem, "fromYear", from, "toYear", to); @@ -67,22 +77,20 @@ public class BillingPerAccountReportAction extends WebMotionController { protected Map<Company,SalesData> getBillingPerAccountData(String firstYear, String lastYear, - WikittyClient client){ + ChoremClient client){ Date last = BillingReportHelper.getLastDayOfYear(Integer.valueOf(lastYear)); Date first = BillingReportHelper.getFirstDayOfYear(Integer.valueOf(firstYear)); - System.out.println(firstYear); - System.out.println(first); - - System.out.println(lastYear); - System.out.println(last); - Map<Company,SalesData> salesData = new LinkedHashMap<>(); + String companyId = client.getConfiguration().getDefaultCompany(); + //get all the bills between first and last - WikittyQuery billsQuery = new WikittyQueryMaker() + WikittyQuery billsQuery = new ChoremQueryMaker().and() .bw(FinancialTransaction.FQ_FIELD_FINANCIALTRANSACTION_EMITTEDDATE, first, last) + .containsOne(FinancialTransaction.FQ_FIELD_FINANCIALTRANSACTION_BENEFICIARY) + .select(Element.ID).filterOnCompanyOrEmployee(companyId) .end(); List<FinancialTransaction> bills = client.findAllByQuery(FinancialTransaction.class, @@ -90,22 +98,61 @@ public class BillingPerAccountReportAction extends WebMotionController { Map<String,SalesData> salesDataPerId = new LinkedHashMap<>(); + Set<String> payersId = new HashSet<>(); + for (FinancialTransaction bill:bills){ + String payer = bill.getPayer(); + if (payer != null) { + payersId.add(payer); + } + } + + List<Wikitty> payersWikitty = client.restore(new ArrayList<>(payersId)); + + Map<String,Company> companiesById = new HashMap<>(); + + List<String> companiesId = new ArrayList<>(); + + Map <String, Wikitty> wikittyById = new HashMap<>(); + + for (Wikitty wkt:payersWikitty){ + if (wkt.hasExtension(Company.EXT_COMPANY)){ + companiesById.put(wkt.getWikittyId(), client.castTo(Company.class, wkt)); + } else if (wkt.hasExtension(Employee.EXT_EMPLOYEE)){ + companiesId.add(wkt.getFieldAsWikitty(Employee.EXT_EMPLOYEE, Employee.FIELD_EMPLOYEE_COMPANY)); + wikittyById.put(wkt.getWikittyId(), wkt); + } + } + + List<Company> companies = client.restore(Company.class, companiesId, false); + for (Company company:companies){ + if (company != null) { + companiesById.put(company.getWikittyId(), company); + } + } + //group bills by account and sum values for (FinancialTransaction bill:bills){ - System.out.println(bill.getReference()); - String companyId = bill.getPayer(); + String payerId = bill.getPayer(); SalesData accountData; - if (salesDataPerId.containsKey(companyId)){ - accountData = salesDataPerId.get(companyId); + + //if payer is an employee, replace payerId with the employee company + if (!companiesById.containsKey(payerId)) { + Wikitty wkt = wikittyById.get(payerId); + if (wkt != null){ + payerId = wkt.getFieldAsWikitty(Employee.EXT_EMPLOYEE, Employee.FIELD_EMPLOYEE_COMPANY); + } + } + + if (salesDataPerId.containsKey(payerId)){ + accountData = salesDataPerId.get(payerId); } else { accountData = new SalesData(); } accountData.setSales(accountData.getSales() + bill.getAmount()); accountData.setQuotations(accountData.getQuotations()+1); - salesDataPerId.put(companyId, accountData); + salesDataPerId.put(payerId, accountData); } - System.out.println(salesDataPerId); //get accounts and create a map id->company List<Company> accounts = client.restore(Company.class, new ArrayList<>(salesDataPerId.keySet()), false); @@ -116,8 +163,6 @@ public class BillingPerAccountReportAction extends WebMotionController { } } - System.out.println(accountsById); - //populate the output for (Map.Entry<String, SalesData> entry : salesDataPerId.entrySet()) { Company account = accountsById.get(entry.getKey()); diff --git a/chorem-webmotion/src/main/webapp/WEB-INF/jsp/financial/reports/billingPerAccountReport.jsp b/chorem-webmotion/src/main/webapp/WEB-INF/jsp/financial/reports/billingPerAccountReport.jsp index 1be0524..b3100f0 100644 --- a/chorem-webmotion/src/main/webapp/WEB-INF/jsp/financial/reports/billingPerAccountReport.jsp +++ b/chorem-webmotion/src/main/webapp/WEB-INF/jsp/financial/reports/billingPerAccountReport.jsp @@ -93,6 +93,10 @@ <td>${account.value.mean} €</td> </tr> </c:forEach> + <tr> + <td>Total</td> + <td>${total} €</td> + </tr> </tbody> </table> -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.