Author: meynier Date: 2013-08-14 14:14:58 +0200 (Wed, 14 Aug 2013) New Revision: 383 Url: http://chorem.org/projects/chorem/repository/revisions/383 Log: Updated model for the new page. Added ADC extension and a method in CHoremClient Modified: trunk/chorem-entities/src/main/java/org/chorem/ChoremClient.java trunk/chorem-entities/src/main/java/org/chorem/project/AdcCalculation.java trunk/chorem-entities/src/main/java/org/chorem/project/Calculation.java trunk/chorem-entities/src/main/java/org/chorem/project/TaskCalculation.java trunk/chorem-entities/src/main/xmi/chorem-model.properties trunk/chorem-entities/src/main/xmi/chorem-model.zargo Modified: trunk/chorem-entities/src/main/java/org/chorem/ChoremClient.java =================================================================== --- trunk/chorem-entities/src/main/java/org/chorem/ChoremClient.java 2013-08-09 10:06:59 UTC (rev 382) +++ trunk/chorem-entities/src/main/java/org/chorem/ChoremClient.java 2013-08-14 12:14:58 UTC (rev 383) @@ -39,6 +39,7 @@ import org.chorem.entities.Quotation18Migration; import org.chorem.entities.QuotationMigration; import org.chorem.entities.Task; +import org.chorem.entities.Time; import org.nuiton.util.ApplicationConfig; import org.nuiton.wikitty.WikittyClient; import org.nuiton.wikitty.WikittyService; @@ -55,6 +56,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; + import org.parboiled.common.StringUtils; /** @@ -64,7 +66,7 @@ public class ChoremClient extends WikittyClient { static protected Map<String, WikittyService> ws = new HashMap<String, WikittyService>(); - + protected ChoremClient(ApplicationConfig config, WikittyService ws) { super(config, ws); this.getMigrationRegistry().put(Invoice.EXT_INVOICE, new InvoiceMigration()); @@ -100,53 +102,53 @@ return result; } -// /** -// * Returns new ChoremClient instance with specified security token -// * -// * @param token can be null, if no authorisation token already exist (no login done) -// * @return -// */ -// static public ChoremClient getClient(String token) { -// ApplicationConfig config = ChoremConfig.getConfig(); -// WikittyService ws = getWikittyService(config); -// ChoremClient result = new ChoremClient(config, ws); -// result.setSecurityToken(token); -// -// return result; -// } -// -// /** -// * Return new ChoremClient instance with authentication done with login and -// * password found in application configuration. If no login found -// * ChoremClient returned doesn't have authentication. -// * -// * @return -// */ -// static public ChoremClient getClient() { -// ApplicationConfig config = ChoremConfig.getConfig(); -// String login = config.getOption(ChoremConfigOption.CHOREM_LOGIN.key); -// String password = config.getOption(ChoremConfigOption.CHOREM_PASSWORD.key); -// -// ChoremClient result = getClient(login, password); -// -// return result; -// } -// -// /** -// * return new ChoremClient instance with authentication done with -// * login and password in argument, only if login is not null and not blank -// * -// * @param login login used for authentication -// * @param password password used for authentication -// */ -// static public ChoremClient getClient(String login, String password) { -// ChoremClient result = getClient(null); -// -// if (StringUtils.isNotBlank(login)) { -// result.login(login, password); -// } -// return result; -// } + // /** + // * Returns new ChoremClient instance with specified security token + // * + // * @param token can be null, if no authorisation token already exist (no login done) + // * @return + // */ + // static public ChoremClient getClient(String token) { + // ApplicationConfig config = ChoremConfig.getConfig(); + // WikittyService ws = getWikittyService(config); + // ChoremClient result = new ChoremClient(config, ws); + // result.setSecurityToken(token); + // + // return result; + // } + // + // /** + // * Return new ChoremClient instance with authentication done with login and + // * password found in application configuration. If no login found + // * ChoremClient returned doesn't have authentication. + // * + // * @return + // */ + // static public ChoremClient getClient() { + // ApplicationConfig config = ChoremConfig.getConfig(); + // String login = config.getOption(ChoremConfigOption.CHOREM_LOGIN.key); + // String password = config.getOption(ChoremConfigOption.CHOREM_PASSWORD.key); + // + // ChoremClient result = getClient(login, password); + // + // return result; + // } + // + // /** + // * return new ChoremClient instance with authentication done with + // * login and password in argument, only if login is not null and not blank + // * + // * @param login login used for authentication + // * @param password password used for authentication + // */ + // static public ChoremClient getClient(String login, String password) { + // ChoremClient result = getClient(null); + // + // if (StringUtils.isNotBlank(login)) { + // result.login(login, password); + // } + // return result; + // } /** * Returns WikittyService to use. This WikittyService is singleton. @@ -186,7 +188,7 @@ exts.addAll(org.chorem.entities.GoalAbstract.extensions); exts.addAll(org.chorem.entities.IntervalAbstract.extensions); exts.addAll(org.chorem.entities.InterviewAbstract.extensions); -// exts.addAll(org.chorem.entities.InvoiceableAbstract.extensions); + // exts.addAll(org.chorem.entities.InvoiceableAbstract.extensions); exts.addAll(org.chorem.entities.InvoiceAbstract.extensions); exts.addAll(org.chorem.entities.MissionAbstract.extensions); exts.addAll(org.chorem.entities.NoteAbstract.extensions); @@ -207,39 +209,39 @@ exts.addAll(org.chorem.entities.VacationRequestAbstract.extensions); exts.addAll(org.chorem.entities.WarrantyAbstract.extensions); exts.addAll(org.chorem.entities.WorkerAbstract.extensions); - - + + exts.addAll(org.nuiton.wikitty.entities.WikittyAuthorisationAbstract.extensions); exts.addAll(org.nuiton.wikitty.entities.WikittyGroupAbstract.extensions); exts.addAll(org.nuiton.wikitty.entities.WikittyHookAbstract.extensions); exts.addAll(org.nuiton.wikitty.entities.WikittyLabelAbstract.extensions); exts.addAll(org.nuiton.wikitty.entities.WikittyTreeNodeAbstract.extensions); exts.addAll(org.nuiton.wikitty.entities.WikittyUserAbstract.extensions); - + // Ajout de TAG value specifique au objet deja genere dans Wikitty -//org.chorem.entities.WikittyTreeNode.attribute.name.tagvalue.help=Le nom pour cette catégorie (ex: dépense, Salaire, ...) -//org.chorem.entities.WikittyTreeNode.attribute.attachment.tagvalue.visible=false -//org.chorem.entities.WikittyTreeNode.attribute.parent.tagvalue.help=La catégorie Parente de celle-ci (ex: dépense pour Loyer) -//org.chorem.entities.WikittyTreeNode.attribute.parent.tagvalue.allowed=Category + //org.chorem.entities.WikittyTreeNode.attribute.name.tagvalue.help=Le nom pour cette catégorie (ex: dépense, Salaire, ...) + //org.chorem.entities.WikittyTreeNode.attribute.attachment.tagvalue.visible=false + //org.chorem.entities.WikittyTreeNode.attribute.parent.tagvalue.help=La catégorie Parente de celle-ci (ex: dépense pour Loyer) + //org.chorem.entities.WikittyTreeNode.attribute.parent.tagvalue.allowed=Category WikittyExtension e = org.nuiton.wikitty.entities.WikittyTreeNodeAbstract.extensionWikittyTreeNode; e.setVersion(WikittyUtil.incrementMajorRevision(e.getTagValue("version"))); // toujours faire attention d'etre a +1 par rapport a l'objet genere e.getFieldType("name").addTagValue("help", "Le nom pour cette catégorie (ex: dépense, Salaire, ...)"); e.getFieldType("attachment").addTagValue("visible", "false"); e.getFieldType("parent").addTagValue("help", "La catégorie Parente de celle-ci (ex: dépense pour Loyer)"); e.getFieldType("parent").addTagValue("allowed", "Category"); - + result.storeExtension(null, exts); ws.put(domain, result); } } } - + return result; } - + /** * Returns the attachments linked with a wikitty id ordered by date * @@ -248,13 +250,13 @@ */ public List<Attachment> getAttachments(String wikittyId) { WikittyQuery criteria = new WikittyQueryMaker() - .eq(Attachment.FQ_FIELD_ATTACHMENT_TARGET, wikittyId).end(); + .eq(Attachment.FQ_FIELD_ATTACHMENT_TARGET, wikittyId).end(); WikittyQueryResult<Attachment> result = findAllByQuery(Attachment.class, criteria); List<Attachment> attachments = result.getAll(); return attachments; } - + /** * Returns the contact details linked with a wikitty id * @@ -263,7 +265,7 @@ */ public List<ContactDetails> getContactDetails(String wikittyId) { WikittyQuery criteria = new WikittyQueryMaker() - .eq(ContactDetails.FQ_FIELD_CONTACTDETAILS_TARGET, wikittyId).end(); + .eq(ContactDetails.FQ_FIELD_CONTACTDETAILS_TARGET, wikittyId).end(); WikittyQueryResult<ContactDetails> result = findAllByQuery(ContactDetails.class, criteria); List<ContactDetails> contactDetails = result.getAll(); @@ -316,9 +318,9 @@ // on regarde si l'utilateur a une societe (au moins, on la prend au hasard :)) WikittyQuery q = new WikittyQueryMaker() - .select(Employee.FQ_FIELD_EMPLOYEE_COMPANY) - .eq(Employee.FQ_FIELD_EMPLOYEE_PERSON, user) - .end(); + .select(Employee.FQ_FIELD_EMPLOYEE_COMPANY) + .eq(Employee.FQ_FIELD_EMPLOYEE_PERSON, user) + .end(); companyId = findByQuery(q); // on a toujours pas de societe, on utilise celle de la config de l'application @@ -346,9 +348,9 @@ String userId = getUser().getWikittyId(); // on recupere toutes les companies pour lequel le user est salarie WikittyQuery q = new WikittyQueryMaker() - .select(Employee.FQ_FIELD_EMPLOYEE_COMPANY) - .eq(Employee.FQ_FIELD_EMPLOYEE_PERSON, userId) - .end(); + .select(Employee.FQ_FIELD_EMPLOYEE_COMPANY) + .eq(Employee.FQ_FIELD_EMPLOYEE_PERSON, userId) + .end(); WikittyQueryResult<Company> result = findAllByQuery(Company.class, q); return result.getAll(); } @@ -369,77 +371,93 @@ return result; } + + + /** + * Fetch the task from the given quotation + * Simple wikitty query + * @param q Quotation + * @return unmodifiable list of task from the quotation + */ + public List<Task> getTasks(Quotation q) { + WikittyQuery taskQuery = new WikittyQueryMaker() + .eq(Task.ELEMENT_FIELD_TASK_QUOTATION, q) + .end(); + + taskQuery.addSortAscending(Quotation.ELEMENT_FIELD_INTERVAL_BEGINDATE); + + WikittyQueryResult<Task> taskResult = findAllByQuery(Task.class, taskQuery); + return taskResult.getAll(); + } + /** + * Fetch the times from the given task + * @param t Task + * @return unmodifiable list of times from the task + */ + public List<Time> getTimes(Task t) { + //Fetch the time objects from the task + WikittyQuery timeQuery = new WikittyQueryMaker() + .eq(Time.ELEMENT_FIELD_TIME_TASK, t) + .end(); + + timeQuery.addSortAscending(Quotation.ELEMENT_FIELD_INTERVAL_BEGINDATE); + + WikittyQueryResult<Time> timeResult = findAllByQuery(Time.class, timeQuery); + + return timeResult.getAll(); + } + /** + * Returns the daily return of the given empoyee + * @param e + * @return + */ + public double getDailyReturn(Employee e) { - /** - * Fetch the task from the given quotation - * Simple wikitty query - * @param q Quotation - * @param client chorem client - * @return unmodifiable list of task from the quotation - */ - public List<Task> getTasks(Quotation q) { - WikittyQuery taskQuery = new WikittyQueryMaker() - .eq(Task.ELEMENT_FIELD_TASK_QUOTATION, q) - .end(); - - taskQuery.addSortAscending(Quotation.ELEMENT_FIELD_INTERVAL_BEGINDATE); + Wikitty w = this.restore(e.getWikittyId()); + Wikitty companyW = null; + if(e.getCompany(false) != null) + companyW = restore(e.getCompany(false).getWikittyId()); - WikittyQueryResult<Task> taskResult = findAllByQuery(Task.class, taskQuery); - return taskResult.getAll(); - } - - /** - * Returns the daily return of the given empoyee - * @param e - * @return - */ - public double getDailyReturn(Employee e) { - - Wikitty w = this.restore(e.getWikittyId()); - Wikitty companyW = null; - if(e.getCompany(false) != null) - companyW = restore(e.getCompany(false).getWikittyId()); - - if(w.hasExtension(EmployeeHR.EXT_EMPLOYEEHR) && - w.getFieldAsDouble(EmployeeHR.EXT_EMPLOYEEHR, EmployeeHR.FIELD_EMPLOYEEHR_DAILYRETURN) != 0) { - return w.getFieldAsDouble(EmployeeHR.EXT_EMPLOYEEHR, EmployeeHR.FIELD_EMPLOYEEHR_DAILYRETURN); - } - else if(companyW != null && companyW.hasExtension(CompanyHR.EXT_COMPANYHR) - && companyW.getFieldAsDouble(CompanyHR.EXT_COMPANYHR, EmployeeHR.FIELD_EMPLOYEEHR_DAILYRETURN) != 0) { - return companyW.getFieldAsDouble(CompanyHR.EXT_COMPANYHR, EmployeeHR.FIELD_EMPLOYEEHR_DAILYRETURN); - } - else { - return this.getConfiguration().getDailyReturn(); - } - } - /** - * Returns the daily hours worked of the given empoyee - * @param e - * @return - */ - public double getDailyHoursWorked(Employee e) { - - Wikitty w = this.restore(e.getWikittyId()); - Wikitty companyW = null; - if(e.getCompany(false) != null) - companyW = restore(e.getCompany(false).getWikittyId()); - - if(companyW.hasExtension(CompanyHR.EXT_COMPANYHR) && - companyW.getFieldAsDouble(CompanyHR.EXT_COMPANYHR,CompanyHR.FIELD_COMPANYHR_DAILYHOURSWORKED) != 0) { - if(w.hasExtension(EmployeeHR.EXT_EMPLOYEEHR) && w.getFieldAsDouble(EmployeeHR.EXT_EMPLOYEEHR, EmployeeHR.FIELD_EMPLOYEEHR_PARTIALTIME) != 0) { - return companyW.getFieldAsDouble(CompanyHR.EXT_COMPANYHR, CompanyHR.FIELD_COMPANYHR_DAILYHOURSWORKED) * - (w.getFieldAsDouble(EmployeeHR.EXT_EMPLOYEEHR, EmployeeHR.FIELD_EMPLOYEEHR_PARTIALTIME)/100); - } - else { - return companyW.getFieldAsDouble(CompanyHR.EXT_COMPANYHR, CompanyHR.FIELD_COMPANYHR_DAILYHOURSWORKED); - } - - } - else { - return this.getConfiguration().getDailyHoursWorked(); - } - } - - + if(w.hasExtension(EmployeeHR.EXT_EMPLOYEEHR) && + w.getFieldAsDouble(EmployeeHR.EXT_EMPLOYEEHR, EmployeeHR.FIELD_EMPLOYEEHR_DAILYRETURN) != 0) { + return w.getFieldAsDouble(EmployeeHR.EXT_EMPLOYEEHR, EmployeeHR.FIELD_EMPLOYEEHR_DAILYRETURN); + } + else if(companyW != null && companyW.hasExtension(CompanyHR.EXT_COMPANYHR) + && companyW.getFieldAsDouble(CompanyHR.EXT_COMPANYHR, EmployeeHR.FIELD_EMPLOYEEHR_DAILYRETURN) != 0) { + return companyW.getFieldAsDouble(CompanyHR.EXT_COMPANYHR, EmployeeHR.FIELD_EMPLOYEEHR_DAILYRETURN); + } + else { + return this.getConfiguration().getDailyReturn(); + } + } + /** + * Returns the daily hours worked of the given empoyee + * @param e + * @return + */ + public double getDailyHoursWorked(Employee e) { + + Wikitty w = this.restore(e.getWikittyId()); + Wikitty companyW = null; + if(e.getCompany(false) != null) + companyW = restore(e.getCompany(false).getWikittyId()); + + if(companyW.hasExtension(CompanyHR.EXT_COMPANYHR) && + companyW.getFieldAsDouble(CompanyHR.EXT_COMPANYHR,CompanyHR.FIELD_COMPANYHR_DAILYHOURSWORKED) != 0) { + if(w.hasExtension(EmployeeHR.EXT_EMPLOYEEHR) && w.getFieldAsDouble(EmployeeHR.EXT_EMPLOYEEHR, EmployeeHR.FIELD_EMPLOYEEHR_PARTIALTIME) != 0) { + return companyW.getFieldAsDouble(CompanyHR.EXT_COMPANYHR, CompanyHR.FIELD_COMPANYHR_DAILYHOURSWORKED) * + (w.getFieldAsDouble(EmployeeHR.EXT_EMPLOYEEHR, EmployeeHR.FIELD_EMPLOYEEHR_PARTIALTIME)/100); + } + else { + return companyW.getFieldAsDouble(CompanyHR.EXT_COMPANYHR, CompanyHR.FIELD_COMPANYHR_DAILYHOURSWORKED); + } + + } + else { + return this.getConfiguration().getDailyHoursWorked(); + } + } + + } Modified: trunk/chorem-entities/src/main/java/org/chorem/project/AdcCalculation.java =================================================================== --- trunk/chorem-entities/src/main/java/org/chorem/project/AdcCalculation.java 2013-08-09 10:06:59 UTC (rev 382) +++ trunk/chorem-entities/src/main/java/org/chorem/project/AdcCalculation.java 2013-08-14 12:14:58 UTC (rev 383) @@ -5,6 +5,7 @@ import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; +import java.util.Map; import org.chorem.ChoremClient; import org.chorem.ChoremUtil; @@ -12,168 +13,305 @@ import org.chorem.entities.EmployeeHR; import org.chorem.entities.FinancialTransaction; import org.chorem.entities.Interval; -import org.chorem.entities.Quotation; import org.chorem.entities.Time; import org.chorem.entities.Company; import org.nuiton.wikitty.query.WikittyQuery; import org.nuiton.wikitty.query.WikittyQueryMaker; import org.nuiton.wikitty.query.WikittyQueryResult; -import com.arjuna.ats.jta.transaction.Transaction; public class AdcCalculation { - - /** - * Calculates the total gains made by the company in one year - * @return - */ - public static double getTotalExpenses(ChoremClient client, Company company, Date start, Date end) { - - WikittyQuery expenseQuery = new WikittyQueryMaker() - .and() - .exteq(FinancialTransaction.EXT_FINANCIALTRANSACTION) - .or() - .containsOne(FinancialTransaction.ELEMENT_FIELD_FINANCIALTRANSACTION_PAYER, getEmployeesHR(client, company)) - .eq(FinancialTransaction.ELEMENT_FIELD_FINANCIALTRANSACTION_PAYER, company) - .close() - .bw(FinancialTransaction.ELEMENT_FIELD_FINANCIALTRANSACTION_PAYMENTDATE, start, end) - .end(); - - WikittyQueryResult<FinancialTransaction> result = client.findAllByQuery(FinancialTransaction.class, expenseQuery); - - double total = 0; - for(FinancialTransaction ft : result.getAll()) { - total += ft.getAmount(); - } - - return total; - } - - - - /** - * calculates all the salaries of the employees ponderated by the productivity rate - * @return - */ - public static double getTotalSalaries(ChoremClient client, Company company) { - //pondéré par pct de productivité - - - double total = 0; - for(EmployeeHR e : getEmployeesHR(client, company)) { - total += e.getSalary() * (e.getProductivityRate()/100); - } - return total; - } - - private static List<EmployeeHR> getEmployeesHR(ChoremClient client, Company company) { - WikittyQuery employeeQuery = new WikittyQueryMaker().and() - .exteq(EmployeeHR.EXT_EMPLOYEEHR) - .eq(Employee.ELEMENT_FIELD_EMPLOYEE_COMPANY, company) - .end(); - WikittyQueryResult<EmployeeHR> result = client.findAllByQuery(EmployeeHR.class, employeeQuery); - return result.getAll(); - - } - - /** - * Calculates the sum of all the times object linked to the company - * @return - */ - public static double getTotalTimes(ChoremClient client, Company company, Date start, Date end) { - WikittyQuery empQuery = new WikittyQueryMaker() - .eq(Employee.ELEMENT_FIELD_EMPLOYEE_COMPANY, company).end(); - WikittyQueryResult<Employee> empResult = client.findAllByQuery(Employee.class, empQuery); - - WikittyQuery timeQuery = new WikittyQueryMaker() - .and() - .exteq(Time.EXT_TIME) - .containsOne(Time.ELEMENT_FIELD_TIME_EMPLOYEE, empResult.getAll()) - .or() - .bw(Interval.FQ_FIELD_INTERVAL_BEGINDATE, start, end) - .bw(Interval.FQ_FIELD_INTERVAL_ENDDATE, start, end) - .end(); - WikittyQueryResult<Time> result = client.findAllByQuery(Time.class, timeQuery); + protected ChoremClient client; + protected Company company; + protected EmployeeHR employee; + protected List<EmployeeHR> employees; + + protected Date start; + protected Date end; + + public AdcCalculation(ChoremClient client, EmployeeHR employee) { + this.employee = employee; + this.company = employee.getCompany(false); + this.client = client; + this.employees = null; + initDate(); + } + + public AdcCalculation(ChoremClient client, List<EmployeeHR> employees) { + this.employees = employees; + this.company = employees.get(0).getCompany(false); + this.client = client; + this.employee = null; + initDate(); + } + + private void initDate() { + Calendar cend = new GregorianCalendar(); + cend.add(Calendar.YEAR, -1); + cend.set(Calendar.MONTH, Calendar.DECEMBER); + cend.set(Calendar.DAY_OF_MONTH, 31); + + Calendar cstart = new GregorianCalendar(); + cstart.add(Calendar.YEAR, -1); + cstart.set(Calendar.MONTH, Calendar.JANUARY); + cstart.set(Calendar.DAY_OF_MONTH, 1); + + start = cstart.getTime(); + end = cend.getTime(); + } + + + /** + * Calculates the total gains made by the company in one year + * @return + */ + public double getTotalExpenses() { - double total = 0; - for(Time t : result.getAll()) { - total += ChoremUtil.getPeriodInHours(t.getBeginDate(), t.getEndDate()); - } + //Construct the query + //TODO : Use wikitty function for sum. + WikittyQuery expenseQuery = new WikittyQueryMaker() + .and() + .exteq(FinancialTransaction.EXT_FINANCIALTRANSACTION) //of type financial transaction + .or() + //The payer is an employee of the company or the company itself + .containsOne(FinancialTransaction.ELEMENT_FIELD_FINANCIALTRANSACTION_PAYER, getEmployeesHR()) + .eq(FinancialTransaction.ELEMENT_FIELD_FINANCIALTRANSACTION_PAYER, company) + .close() + //The payment hapened during the year + .bw(FinancialTransaction.FQ_FIELD_FINANCIALTRANSACTION_EMITTEDDATE, start, end) + .end(); - return total; - } - - /** - * Returns the employee's adc - * @return - */ - public static double getAdc(ChoremClient client, EmployeeHR e) { - Company company = e.getCompany(false); - Date end = new GregorianCalendar().getTime(); - Calendar cstart = new GregorianCalendar(); - cstart.add(Calendar.YEAR, -1); - Date start = cstart.getTime(); - - double expenses = getTotalExpenses(client, company, start, end); - double salaries = getTotalSalaries(client, company); - double times = getTotalTimes(client, company, start, end); - - double dailyHoursWorked = client.getDailyHoursWorked(e); - double dailySalary = client.restore(e.getWikittyId()) - .getFieldAsDouble(EmployeeHR.EXT_EMPLOYEEHR,EmployeeHR.FIELD_EMPLOYEEHR_SALARY)/21.5; - - System.out.println("EXPENSES : " + expenses); - System.out.println("SALARIES : " + salaries); - System.out.println("TIMES : " + times); - System.out.println("DHW : " + dailyHoursWorked); - System.out.println("SALARY : " + dailySalary); - - - double adc = ( (expenses/salaries)/times ) * dailyHoursWorked * dailySalary; - //dailySalary = dailySalary *1.6; //charges patronales - //double adc = dailySalary; - - return adc; - } - - public static HashMap<String, Double> getMultipleAdc(ChoremClient client, List<EmployeeHR> employees) { + WikittyQueryResult<FinancialTransaction> result = client.findAllByQuery(FinancialTransaction.class, expenseQuery); + + //Calculates the total + double total = 0; + for(FinancialTransaction ft : result.getAll()) { + total += ft.getAmount(); + } + + return total; + } + + /** + * Calculates the employee cost at full time + * @return + */ + public double getMonthlyFullTimeCost(EmployeeHR e) { + double salary = e.getSalary(); + double otherPayments = e.getOtherPayments(); + double partialTime = e.getPartialTime(); + + return ( ( salary + otherPayments) * (partialTime/100) ); + } + + + /** + * calculates the estimated number of prodctive days when the employee will work + * @return + */ + public double getEstimatedNumberOfProductiveDays(EmployeeHR e) { + int daysInYear = 218; //It would be *much* better to calculate this or at least get it from input + double partialTime = e.getPartialTime(); + double productivityRate = e.getProductivityRate(); + + return (daysInYear * (partialTime/100) * (productivityRate/100)); + } + + public double getRealNumberOfProductiveDays(EmployeeHR e) { + + //fetch the times of all the employees of the company that have worked between $start an $end + WikittyQuery timeQuery = new WikittyQueryMaker() + .and() + .exteq(Time.EXT_TIME) + .eq(Time.ELEMENT_FIELD_TIME_EMPLOYEE, e) + .or() + .bw(Interval.FQ_FIELD_INTERVAL_BEGINDATE, start, end) + .bw(Interval.FQ_FIELD_INTERVAL_ENDDATE, start, end) + .end(); + WikittyQueryResult<Time> result = client.findAllByQuery(Time.class, timeQuery); + + //Calculates the total + double total = 0; + for(Time t : result.getAll()) { + total += ChoremUtil.getPeriodInHours(t.getBeginDate(), t.getEndDate()); + + } + + return (total/ client.getDailyHoursWorked(e) ) + * (e.getProductivityRate()/100); + } + + /** + * + * @return + */ + public double getYearCost(EmployeeHR e, boolean real) { + double result = 0; + if(real) { + result = getMonthlyFullTimeCost(e) * getRealNumberOfProductiveDays(e); + } + else { + result = getMonthlyFullTimeCost(e) * getEstimatedNumberOfProductiveDays(e); + } + return result; + } + + public double getTotalYearCost(boolean real) { + + double total = 0; + for(EmployeeHR e : getEmployeesHR()) { + total += getYearCost(e, real); + } + return total; + } + + + + /** + * Get all the employees of the company having the EmployeeHR extension + * @param client + * @param company + * @return + */ + protected List<EmployeeHR> getEmployeesHR() { + + WikittyQuery employeeQuery = new WikittyQueryMaker().and() + .exteq(EmployeeHR.EXT_EMPLOYEEHR) + .eq(Employee.ELEMENT_FIELD_EMPLOYEE_COMPANY, company) + .end(); + WikittyQueryResult<EmployeeHR> result = client.findAllByQuery(EmployeeHR.class, employeeQuery); + return result.getAll(); + + } + + /** + * Calculates the sum of all the times object linked to the company + * @return + */ + public double getTotalTimes() { + + WikittyQuery empQuery = new WikittyQueryMaker() + .eq(Employee.ELEMENT_FIELD_EMPLOYEE_COMPANY, company).end(); + WikittyQueryResult<Employee> empResult = client.findAllByQuery(Employee.class, empQuery); + + //fetch the times of all the employees of the company that have worked between $start an $end + WikittyQuery timeQuery = new WikittyQueryMaker() + .and() + .exteq(Time.EXT_TIME) + .containsOne(Time.ELEMENT_FIELD_TIME_EMPLOYEE, empResult.getAll()) + .or() + .bw(Interval.FQ_FIELD_INTERVAL_BEGINDATE, start, end) + .bw(Interval.FQ_FIELD_INTERVAL_ENDDATE, start, end) + .end(); + WikittyQueryResult<Time> result = client.findAllByQuery(Time.class, timeQuery); + + //Calculates the total + double total = 0; + for(Time t : result.getAll()) { + total += ChoremUtil.getPeriodInHours(t.getBeginDate(), t.getEndDate()); + } + + return total; + } + + /** + * Returns the employee's adc + * @return + */ + public double getAdc(boolean real) { + + + double expenses = getTotalExpenses(); + double productiveDays = 0; + if(real) { + productiveDays = getRealNumberOfProductiveDays(employee); + } + else { + productiveDays = getEstimatedNumberOfProductiveDays(employee); + } + + double cost = getYearCost(employee, real); + double totalCost = getTotalYearCost(real); + + System.out.println("EXPENSES : " + expenses); + System.out.println("PROD DAYS : " + productiveDays); + System.out.println("COST : " + expenses); + System.out.println("TOTAL COST : " + expenses); + + double adc = ( (expenses * cost) / totalCost ) / productiveDays; + + + return adc; + } + + public double getAdc() { + return getAdc(false); + } + public Map<String, Double> getMultipleAdc() { + return getMultipleAdc(false); + } + + /** + * Returns multiple adc form the given employee list. + * @param client + * @param employees + * @return + */ + public Map<String, Double> getMultipleAdc(boolean real) { if(employees.size() == 0) return null; - Company company = employees.get(0).getCompany(false); - Date end = new GregorianCalendar().getTime(); - Calendar cstart = new GregorianCalendar(); - cstart.add(Calendar.YEAR, -1); - Date start = cstart.getTime(); - double expenses = getTotalExpenses(client, company, start, end); - double salaries = getTotalSalaries(client, company); - double times = getTotalTimes(client, company, start, end); - + Calendar cend = new GregorianCalendar(); + cend.add(Calendar.YEAR, -1); + cend.set(Calendar.MONTH, Calendar.DECEMBER); + cend.set(Calendar.DAY_OF_MONTH, 31); + + Calendar cstart = new GregorianCalendar(); + cstart.add(Calendar.YEAR, -1); + cstart.set(Calendar.MONTH, Calendar.JANUARY); + cstart.set(Calendar.DAY_OF_MONTH, 1); + + Date start = cstart.getTime(); + Date end = cend.getTime(); + + //Calculate all the "static" values before + + double expenses = getTotalExpenses(); + + double totalCost = getTotalYearCost(real); + + //Then, for each employee + HashMap<String, Double> adcs = new HashMap<String, Double>(); for(EmployeeHR e : employees) { - double dailyHoursWorked = client.getDailyHoursWorked(e); - double dailySalary = client.restore(e.getWikittyId()) - .getFieldAsDouble(EmployeeHR.EXT_EMPLOYEEHR, EmployeeHR.FIELD_EMPLOYEEHR_SALARY)/21.5; - System.out.println("EXPENSES : " + expenses); - System.out.println("SALAIRES : " + salaries); - System.out.println("TIMES : " + times); - System.out.println("DAILY HW : " + dailyHoursWorked); - System.out.println("DAILY SALARY : " + dailySalary); - adcs.put(e.getWikittyId(),( (expenses/salaries)/times ) * dailyHoursWorked * dailySalary); + this.employee = e; + + double productiveDays = 0; + if(real) { + productiveDays = getRealNumberOfProductiveDays(e); + } + else { + productiveDays = getEstimatedNumberOfProductiveDays(e); + } + double cost = getYearCost(e, real); + + Double adc = ( ( (expenses * cost) / totalCost ) / productiveDays); + if(adc.isNaN()) { + adc = 0.0; + } + adcs.put(e.getWikittyId(),adc); } - //dailySalary = dailySalary *1.6; //charges patronales - //double adc = dailySalary; - + return adcs; - - } - - - - - - + } + + + + + + + } Modified: trunk/chorem-entities/src/main/java/org/chorem/project/Calculation.java =================================================================== --- trunk/chorem-entities/src/main/java/org/chorem/project/Calculation.java 2013-08-09 10:06:59 UTC (rev 382) +++ trunk/chorem-entities/src/main/java/org/chorem/project/Calculation.java 2013-08-14 12:14:58 UTC (rev 383) @@ -15,249 +15,249 @@ */ public abstract class Calculation<T> { - /**Main object (Task or Quotation)*/ - protected T e; - /** Chorme client */ - protected ChoremClient client; - /** Total price of the object */ - protected double amount; - /** Estimated number of days */ - protected double nbDays; + /**Main object (Task or Quotation)*/ + protected T e; + /** Chorme client */ + protected ChoremClient client; + /** Total price of the object */ + protected double amount; + /** Estimated number of days */ + protected double nbDays; - protected static final int SEC_PER_HOUR = 3600; - protected static final int WORKING_HOURS_PER_DAY = 7; - /* Seuil de rentabilite productif */ + protected static final int SEC_PER_HOUR = 3600; + protected static final int WORKING_HOURS_PER_DAY = 7; + /* Seuil de rentabilite productif */ - //attributes to store the calculations - private Double adr = null; - private Double realDays = null; - private Double deltaDays = null; - private Double realAdr = null; - private Double expectedProfit = null; - private Double lossOrProfit = null; - private Double resultPerDay = null; - private Double avgReturn = null; - private Double realReturn = null; + //attributes to store the calculations + private Double adr = null; + private Double realDays = null; + private Double deltaDays = null; + private Double realAdr = null; + private Double expectedProfit = null; + private Double lossOrProfit = null; + private Double resultPerDay = null; + private Double avgReturn = null; + private Double realReturn = null; - /** - * - * @param e Element that will serve for the calculations - * @param amount Amount of this element - * @param nbDays Estimated number of days for this element - * @param client Instance of ChoremClient - */ - public Calculation(T e, double amount, double nbDays, ChoremClient client) { - this.e = e; - this.client = client; - this.amount = amount; - this.nbDays = nbDays; - } + /** + * + * @param e Element that will serve for the calculations + * @param amount Amount of this element + * @param nbDays Estimated number of days for this element + * @param client Instance of ChoremClient + */ + public Calculation(T e, double amount, double nbDays, ChoremClient client) { + this.e = e; + this.client = client; + this.amount = amount; + this.nbDays = nbDays; + } - /** - * Makes all the calcuations and store them into the attributes - */ - public void calculate() { + /** + * Makes all the calcuations and store them into the attributes + */ + public void calculate() { - this.adr = adr(); - this.realDays = realDays(); - this.deltaDays = deltaDays(); - this.realAdr = realAdr(); - this.expectedProfit = expectedProfit(); - this.lossOrProfit = lossOrProfit(); - this.resultPerDay = resultPerDay(); - this.avgReturn = avgReturn(); - this.realReturn = realReturn(); - } - - /** - * Calculates the real number of days from the Time objects. - * @return - */ - public abstract double realDays(); - - - /** - * Return the times (in hour) per employee for the current object - * @return - */ - protected abstract HashMap<Employee, Double> getTimes(); - - /** - * Return the percentages for the current object - * @return - */ - protected abstract HashMap<Employee, Double> getPercentages(); - - public HashMap<Employee, Double> getTimePercentages() { - HashMap<Employee, Double> times = getTimes(); - HashMap<Employee, Double> timePercentages = new HashMap<Employee, Double>(); - double sum = 0; - Set<Employee> keySet = times.keySet(); - for (Employee key : keySet) { - sum+= times.get(key); - } - for (Employee key : keySet) { - timePercentages.put(key, (times.get(key)/sum)*100); - } - - - return timePercentages; - } - /** - * Return the average Return for all the employees, ponderated by the percentages - * @return - */ - public double avgReturn() { - - //Fetch the percentages per employee - HashMap<Employee, Double> percentages = getPercentages(); - if(percentages.size() == 0) { - return client.getConfiguration().getDailyReturn(); - } - Set<Employee> keySet = percentages.keySet(); - double avgReturn = 0; - for(Employee key : keySet) { - avgReturn += client.getDailyReturn(key)*(percentages.get(key)/100); - } - - return avgReturn; - } - - /** - * Calculates the real daily return from the different times object and the daily return of the employees. - * @return - */ - public double realReturn() { - HashMap<Employee, Double> times = getTimes(); - if(times.size() == 0) { - return client.getConfiguration().getDailyReturn(); - } - double realReturn = 0; - Set<Employee> keySet = times.keySet(); - for(Employee key : keySet) { - double srp = client.getDailyReturn(key); - double hoursPerDay = client.getDailyHoursWorked(key); - - //TODO :Find a way to use the different work hours - - //} - realReturn += srp* ( times.get(key) / hoursPerDay ) ; - } - realReturn = realReturn/getRealDays(); - - return realReturn; - } - - /** - * Returns the average daily rate, which is the amount divided by the estimated number of days. - * @return the ADR - */ - public double adr() { - return amount/nbDays; - } + this.adr = adr(); + this.realDays = realDays(); + this.deltaDays = deltaDays(); + this.realAdr = realAdr(); + this.expectedProfit = expectedProfit(); + this.lossOrProfit = lossOrProfit(); + this.resultPerDay = resultPerDay(); + this.avgReturn = avgReturn(); + this.realReturn = realReturn(); + } - + /** + * Calculates the real number of days from the Time objects. + * @return + */ + public abstract double realDays(); - /** - * Gives the difference between the real number of days and the estimated value - * @return - */ - public double deltaDays() { - return getRealDays() - nbDays; - } - /** - * Calculates the real ADR from the real number of days. - * @return - */ - public double realAdr() { - double realDays = getRealDays(); + /** + * Return the times (in hour) per employee for the current object + * @return + */ + protected abstract HashMap<Employee, Double> getTimes(); - return amount/realDays; - } + /** + * Return the percentages for the current object + * @return + */ + protected abstract HashMap<Employee, Double> getPercentages(); - /** - * Profit calculated from the estimated ADR and the estimated numer of days - * @return - */ - public double expectedProfit() { - return amount - (nbDays*getAvgReturn()); - } + public HashMap<Employee, Double> getTimePercentages() { + HashMap<Employee, Double> times = getTimes(); + HashMap<Employee, Double> timePercentages = new HashMap<Employee, Double>(); + double sum = 0; + Set<Employee> keySet = times.keySet(); + for (Employee key : keySet) { + sum+= times.get(key); + } + for (Employee key : keySet) { + timePercentages.put(key, (times.get(key)/sum)*100); + } - /** - * Real profit (or loss) done when the quotation is closed. - * @return - */ - public double lossOrProfit() { - return amount - (getRealDays() * getRealReturn()); - } - /** - * Average profit/loss per day - * @return - */ - public double resultPerDay() { - return getLossOrProfit() / getRealDays(); - } - - - - /* ########################################################################################## - * ######################### GETTERS ####################################################### - * ########################################################################################## - * - * The getters returns the attributes if it has been calculated and calculates directly the value otherwise - */ + return timePercentages; + } + /** + * Return the average Return for all the employees, ponderated by the percentages + * @return + */ + public double avgReturn() { - public double getAdr() { - if(adr == null) - adr = adr(); - return adr; - } - public double getRealDays() { - if(realDays == null) - realDays = realDays(); - return realDays; - } - public double getDeltaDays() { - if(deltaDays == null) - deltaDays = deltaDays(); - return deltaDays; - } - public double getRealAdr() { - if(realAdr == null) - realAdr = realAdr(); - return realAdr; - } - public double getExpectedProfit() { - if(expectedProfit == null) - expectedProfit = expectedProfit(); - return expectedProfit; - } - public double getLossOrProfit() { - if(lossOrProfit == null) - lossOrProfit = lossOrProfit(); - return lossOrProfit; - } - public double getResultPerDay() { - if(resultPerDay == null) - resultPerDay = resultPerDay(); - return resultPerDay; - } - - public double getAvgReturn() { - if(avgReturn == null) - avgReturn = avgReturn(); - return avgReturn; - } - public double getRealReturn() { - if(realReturn == null) - realReturn = realReturn(); - return realReturn; - } - - public T getObject() { - return e; - } + //Fetch the percentages per employee + HashMap<Employee, Double> percentages = getPercentages(); + if(percentages.size() == 0) { + return client.getConfiguration().getDailyReturn(); + } + Set<Employee> keySet = percentages.keySet(); + double avgReturn = 0; + for(Employee key : keySet) { + avgReturn += client.getDailyReturn(key)*(percentages.get(key)/100); + } + + return avgReturn; + } + + /** + * Calculates the real daily return from the different times object and the daily return of the employees. + * @return + */ + public double realReturn() { + HashMap<Employee, Double> times = getTimes(); + if(times.size() == 0) { + return client.getConfiguration().getDailyReturn(); + } + double realReturn = 0; + Set<Employee> keySet = times.keySet(); + for(Employee key : keySet) { + double srp = client.getDailyReturn(key); + double hoursPerDay = client.getDailyHoursWorked(key); + + //TODO :Find a way to use the different work hours + + //} + realReturn += srp* ( times.get(key) / hoursPerDay ) ; + } + realReturn = realReturn/getRealDays(); + + return realReturn; + } + + /** + * Returns the average daily rate, which is the amount divided by the estimated number of days. + * @return the ADR + */ + public double adr() { + return amount/nbDays; + } + + + + /** + * Gives the difference between the real number of days and the estimated value + * @return + */ + public double deltaDays() { + return getRealDays() - nbDays; + } + + /** + * Calculates the real ADR from the real number of days. + * @return + */ + public double realAdr() { + double realDays = getRealDays(); + + return amount/realDays; + } + + /** + * Profit calculated from the estimated ADR and the estimated numer of days + * @return + */ + public double expectedProfit() { + return amount - (nbDays*getAvgReturn()); + } + + /** + * Real profit (or loss) done when the quotation is closed. + * @return + */ + public double lossOrProfit() { + return amount - (getRealDays() * getRealReturn()); + } + + /** + * Average profit/loss per day + * @return + */ + public double resultPerDay() { + return getLossOrProfit() / getRealDays(); + } + + + + /* ########################################################################################## + * ######################### GETTERS ####################################################### + * ########################################################################################## + * + * The getters returns the attributes if it has been calculated and calculates directly the value otherwise + */ + + public double getAdr() { + if(adr == null) + adr = adr(); + return adr; + } + public double getRealDays() { + if(realDays == null) + realDays = realDays(); + return realDays; + } + public double getDeltaDays() { + if(deltaDays == null) + deltaDays = deltaDays(); + return deltaDays; + } + public double getRealAdr() { + if(realAdr == null) + realAdr = realAdr(); + return realAdr; + } + public double getExpectedProfit() { + if(expectedProfit == null) + expectedProfit = expectedProfit(); + return expectedProfit; + } + public double getLossOrProfit() { + if(lossOrProfit == null) + lossOrProfit = lossOrProfit(); + return lossOrProfit; + } + public double getResultPerDay() { + if(resultPerDay == null) + resultPerDay = resultPerDay(); + return resultPerDay; + } + + public double getAvgReturn() { + if(avgReturn == null) + avgReturn = avgReturn(); + return avgReturn; + } + public double getRealReturn() { + if(realReturn == null) + realReturn = realReturn(); + return realReturn; + } + + public T getObject() { + return e; + } } Modified: trunk/chorem-entities/src/main/java/org/chorem/project/TaskCalculation.java =================================================================== --- trunk/chorem-entities/src/main/java/org/chorem/project/TaskCalculation.java 2013-08-09 10:06:59 UTC (rev 382) +++ trunk/chorem-entities/src/main/java/org/chorem/project/TaskCalculation.java 2013-08-14 12:14:58 UTC (rev 383) @@ -75,13 +75,9 @@ protected HashMap<Employee, Double> getTimes() { HashMap<Employee, Double> times = new HashMap<Employee, Double>(); - //Feth the time objects from the task - WikittyQuery timeQuery = new WikittyQueryMaker() - .eq(Time.ELEMENT_FIELD_TIME_TASK, e) - .end(); - WikittyQueryResult<Time> timeResult = client.findAllByQuery(Time.class, timeQuery); - for(Time t : timeResult.getAll()) { + + for(Time t : client.getTimes(e)) { Employee emp = t.getEmployee(false); if(emp != null) { if(times.containsKey(emp)) { Modified: trunk/chorem-entities/src/main/xmi/chorem-model.properties =================================================================== --- trunk/chorem-entities/src/main/xmi/chorem-model.properties 2013-08-09 10:06:59 UTC (rev 382) +++ trunk/chorem-entities/src/main/xmi/chorem-model.properties 2013-08-14 12:14:58 UTC (rev 383) @@ -34,7 +34,15 @@ org.chorem.entities.Attachment.attribute.mimetype.tagvalue.choiceQuery=SELECT Attachment.mimetype WHERE extension=Attachment org.chorem.entities.Attachment.attribute.content.tagvalue.help=Le fichier \u00e0 importer org.chorem.entities.Attachment.attribute.contentUrl.tagvalue.help=Si on ne souhaite pas importer le fichier, on peut indiquer ici sont URL (ex: http://commun.codelutin.home/Clients/...) + # +# ADC +# +org.chorem.entities.ADC.class.tagvalue.version=1.0 +org.chorem.entities.Cancelled.class.tagvalue.toString=%ADC.employeeHR|noemployee%s %ADC.year|noyear%s %ADC.value|novalue$s + + +# # Cancelled # org.chorem.entities.Cancelled.class.tagvalue.version=1.0 Modified: trunk/chorem-entities/src/main/xmi/chorem-model.zargo =================================================================== (Binary files differ)