Author: sbavencoff Date: 2014-07-30 11:17:32 +0200 (Wed, 30 Jul 2014) New Revision: 3874 Url: http://forge.chorem.org/projects/lima/repository/revisions/3874 Log: refs #1044 #1051 : lettering/un-lettering Added: trunk/lima-business-api/src/main/java/org/chorem/lima/business/UnbalancedEntriesException.java Modified: trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/FinancialTransactionService.java trunk/lima-business/src/main/java/org/chorem/lima/business/AccountingRules.java trunk/lima-business/src/main/java/org/chorem/lima/business/accountingrules/DefaultAccountingRules.java trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialTransactionServiceImpl.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/lettering/LetteringViewHandler.java Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/AccountingRules.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/AccountingRules.java 2014-07-30 07:41:15 UTC (rev 3873) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/AccountingRules.java 2014-07-30 09:17:32 UTC (rev 3874) @@ -107,4 +107,12 @@ WithoutEntryBookFinancialTransactionsException, UnbalancedFinancialTransactionsException, NotLockedClosedPeriodicEntryBooksException; + + /** + * Entry rules + * + * @param oldEntries + * @throws LockedFinancialPeriodException, LockedEntryBookException + */ + void addLetter(List<Entry> oldEntries) throws LockedEntryBookException, UnbalancedEntriesException; } Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/accountingrules/DefaultAccountingRules.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/accountingrules/DefaultAccountingRules.java 2014-07-30 07:41:15 UTC (rev 3873) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/accountingrules/DefaultAccountingRules.java 2014-07-30 09:17:32 UTC (rev 3874) @@ -41,6 +41,7 @@ import org.chorem.lima.business.NoEmptyFiscalPeriodException; import org.chorem.lima.business.NotBeginNextDayOfLastFiscalPeriodException; import org.chorem.lima.business.NotLockedClosedPeriodicEntryBooksException; +import org.chorem.lima.business.UnbalancedEntriesException; import org.chorem.lima.business.UnbalancedFinancialTransactionsException; import org.chorem.lima.business.UnfilledEntriesException; import org.chorem.lima.business.UsedAccountException; @@ -60,6 +61,7 @@ import org.chorem.lima.entity.FiscalPeriodTopiaDao; import org.chorem.lima.entity.LimaCallaoTopiaDaoSupplier; +import java.math.BigDecimal; import java.util.Date; import java.util.List; @@ -227,6 +229,39 @@ } } + @Override + public void addLetter(List<Entry> oldEntries) throws LockedEntryBookException, UnbalancedEntriesException { + + ClosedPeriodicEntryBookTopiaDao closedPeriodicEntryBookTopiaDao = getDaoHelper().getClosedPeriodicEntryBookDao(); + FinancialPeriodTopiaDao financialPeriodTopiaDao = getDaoHelper().getFinancialPeriodDao(); + + BigDecimal balance = new BigDecimal(0); + + for (Entry entry : oldEntries) { + + FinancialPeriod financialPeriod = financialPeriodTopiaDao.findByDate(entry.getFinancialTransaction().getTransactionDate()); + + ClosedPeriodicEntryBook closedPeriodicEntryBook = + closedPeriodicEntryBookTopiaDao.findByEntryBookAndFinancialPeriod( + entry.getFinancialTransaction().getEntryBook(), financialPeriod); + if (closedPeriodicEntryBook.isLocked()) { + throw new LockedEntryBookException(closedPeriodicEntryBook); + } + + if (entry.isDebit()) { + balance = balance.add(entry.getAmount()); + } else { + balance = balance.subtract(entry.getAmount()); + } + + } + + if (balance.signum() != 0) { + throw new UnbalancedEntriesException(oldEntries); + } + + } + /** * Rules on update entry : * <p/> Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialTransactionServiceImpl.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialTransactionServiceImpl.java 2014-07-30 07:41:15 UTC (rev 3873) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/ejb/FinancialTransactionServiceImpl.java 2014-07-30 09:17:32 UTC (rev 3874) @@ -40,6 +40,7 @@ import org.chorem.lima.business.LimaException; import org.chorem.lima.business.LockedEntryBookException; import org.chorem.lima.business.LockedFinancialPeriodException; +import org.chorem.lima.business.UnbalancedEntriesException; import org.chorem.lima.business.api.AccountService; import org.chorem.lima.business.api.FinancialTransactionService; import org.chorem.lima.business.utils.LetteringComparator; @@ -285,6 +286,64 @@ } } + @Override + public List<Entry> addLetter(List<Entry> entries) throws UnbalancedEntriesException, LockedEntryBookException { + + List<Entry> oldEntries = Lists.newLinkedList(); + + EntryTopiaDao entryTopiaDao = getDaoHelper().getEntryDao(); + + for (Entry entry : entries) { + + Entry oldEntry = entryTopiaDao.forTopiaIdEquals(entry.getTopiaId()).findUnique(); + + oldEntries.add(oldEntry); + + } + + AccountingRules accountingRules = LimaConfig.getInstance().getAccountingRules(); + + //check rules + accountingRules.addLetter(oldEntries); + + String nextLetters = getNextLetters(); + + for (Entry entry : oldEntries) { + + entry.setLettering(nextLetters); + + //update entry + entryTopiaDao.update(entry); + + } + + return oldEntries; + } + + @Override + public List<Entry> removeLetter(String letter) { + + List<Entry> entries = Lists.newArrayList(); + + if (StringUtils.isNotBlank(letter)) { + + EntryTopiaDao entryTopiaDao = getDaoHelper().getEntryDao(); + + entries = entryTopiaDao.forLetteringEquals(letter).findAll(); + + for (Entry entry : entries) { + + entry.setLettering(""); + + //update entry + entryTopiaDao.update(entry); + + } + } + + return entries; + } + public String findLastLetter(List<String> letters) { String result; Added: trunk/lima-business-api/src/main/java/org/chorem/lima/business/UnbalancedEntriesException.java =================================================================== --- trunk/lima-business-api/src/main/java/org/chorem/lima/business/UnbalancedEntriesException.java (rev 0) +++ trunk/lima-business-api/src/main/java/org/chorem/lima/business/UnbalancedEntriesException.java 2014-07-30 09:17:32 UTC (rev 3874) @@ -0,0 +1,19 @@ +package org.chorem.lima.business; + +import org.chorem.lima.entity.Entry; + +import java.util.Collection; + +/** + * @author Sylvain Bavencoff <bavencoff@codelutin.com> + */ +public class UnbalancedEntriesException extends EntriesException { + + public UnbalancedEntriesException(Collection<Entry> entries) { + super(entries); + } + + public UnbalancedEntriesException(Collection<Entry> entries, Throwable cause) { + super(entries, cause); + } +} Modified: trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/FinancialTransactionService.java =================================================================== --- trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/FinancialTransactionService.java 2014-07-30 07:41:15 UTC (rev 3873) +++ trunk/lima-business-api/src/main/java/org/chorem/lima/business/api/FinancialTransactionService.java 2014-07-30 09:17:32 UTC (rev 3874) @@ -29,9 +29,9 @@ import org.chorem.lima.beans.LetteringFilter; import org.chorem.lima.business.AfterLastFiscalPeriodException; import org.chorem.lima.business.BeforeFirstFiscalPeriodException; -import org.chorem.lima.business.LimaException; import org.chorem.lima.business.LockedEntryBookException; import org.chorem.lima.business.LockedFinancialPeriodException; +import org.chorem.lima.business.UnbalancedEntriesException; import org.chorem.lima.entity.Entry; import org.chorem.lima.entity.EntryBook; import org.chorem.lima.entity.FinancialPeriod; @@ -111,7 +111,6 @@ * Retourne toutes les entrées d'une transaction * pour un compte et la présence d'un lettrage ou (xor) non * @param filter filtre sur les entrees, selon le compte, les dates de debut et de fin, et le lettrage - * @throws LimaException * */ List<Entry> getAllEntrieByDatesAndAccountAndLettering(LetteringFilter filter); @@ -120,4 +119,14 @@ * @param financialTransaction transaction sur laquelle la derniere entree est selectionnee * */ Entry getLastEntry(FinancialTransaction financialTransaction); + + + /** + * Retourne la list des ecritures lettrées + * @param entries la list des ecritures lettrées + * @throws UnbalancedEntriesException, LockedEntryBookException + * */ + List<Entry> addLetter(List<Entry> entries) throws UnbalancedEntriesException, LockedEntryBookException; + + List<Entry> removeLetter(String letter); } Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/lettering/LetteringViewHandler.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/lettering/LetteringViewHandler.java 2014-07-30 07:41:15 UTC (rev 3873) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/lettering/LetteringViewHandler.java 2014-07-30 09:17:32 UTC (rev 3874) @@ -25,7 +25,9 @@ package org.chorem.lima.ui.lettering; -import com.google.common.collect.Maps; +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; import org.apache.commons.lang3.time.DateUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -33,6 +35,7 @@ import org.chorem.lima.beans.LetteringFilterImpl; import org.chorem.lima.business.LockedEntryBookException; import org.chorem.lima.business.LockedFinancialPeriodException; +import org.chorem.lima.business.UnbalancedEntriesException; import org.chorem.lima.business.api.AccountService; import org.chorem.lima.business.api.EntryBookService; import org.chorem.lima.business.api.FinancialPeriodService; @@ -58,7 +61,6 @@ import java.util.Calendar; import java.util.Date; import java.util.List; -import java.util.Map; import static org.nuiton.i18n.I18n.t; @@ -475,8 +477,41 @@ /**Add a group of three letters to n entries*/ public void addLetter() { if (editModel.isLettred()) { - String newLetters = financialTransactionService.getNextLetters(); - changeLetter(newLetters); + int[] entrieSelected = view.getTable().getSelectedRows(); + + LetteringTableModel tableModel = view.getTableModel(); + + List<Entry> entries = Lists.newLinkedList(); + + for (int indexEntry : entrieSelected) { + + Entry entry = tableModel.get(indexEntry); + + entries.add(entry); + } + + try { + + if (!entries.isEmpty()) { + + entries = financialTransactionService.addLetter(entries); + + updateEntries(entries); + } + + } catch (LockedEntryBookException e) { + + errorHelper.showErrorMessage(t("lima.ui.entries.letter.closed.entryBook.error", + e.getClosedPeriodicEntryBook().getEntryBook().getCode(), + e.getClosedPeriodicEntryBook().getEntryBook().getLabel(), + e.getClosedPeriodicEntryBook().getFinancialPeriod().getBeginDate(), + e.getClosedPeriodicEntryBook().getFinancialPeriod().getEndDate())); + + } catch (UnbalancedEntriesException e) { + + errorHelper.showErrorMessage(t("lima.ui.entries.letter.unbalanced.error")); + + } onButtonModeChanged(ButtonMode.DELETTRED); } } @@ -484,40 +519,47 @@ /**Remove a group of three letters to n entries*/ public void removeLetter() { if (editModel.isUnLettred()) { - changeLetter(null); + + int[] entrieSelected = view.getTable().getSelectedRows(); + + LetteringTableModel tableModel = view.getTableModel(); + + if (entrieSelected.length > 0) { + + Entry firstEntry = tableModel.get(entrieSelected[0]); + + String letter = firstEntry.getLettering(); + + List<Entry> entries = financialTransactionService.removeLetter(letter); + + updateEntries(entries); + } + onButtonModeChanged(ButtonMode.LETTRED); } } - /**Add or remove a group of three letters to n entries*/ - protected void changeLetter(String newLetters) { - int[] entrieSelected = view.getTable().getSelectedRows(); + protected void updateEntries(List<Entry> entries) { LetteringTableModel tableModel = view.getTableModel(); - Map<Entry, String> previousLetterMap = Maps.newHashMap(); + for (final Entry entry : entries) { - try { - for (int indexEntry : entrieSelected) { - Entry entry = tableModel.get(indexEntry); - previousLetterMap.put(entry, entry.getLettering()); - entry.setLettering(newLetters); - financialTransactionService.updateEntry(entry); - tableModel.fireTableRowsUpdated(indexEntry, indexEntry); + Entry oldEntry = Iterables.find( + tableModel.getValues(), + new Predicate<Entry>() { + @Override + public boolean apply(Entry input) { + return input.getTopiaId().equals(entry.getTopiaId()); + } + }, + null); + if (oldEntry != null) { + int indexEntry = tableModel.indexOf(oldEntry); + tableModel.setValue(indexEntry, entry); } - } catch (LockedEntryBookException e) { - errorHelper.showErrorMessage(t("lima.ui.entries.letter.closed.entryBook.error", - e.getClosedPeriodicEntryBook().getEntryBook().getCode(), - e.getClosedPeriodicEntryBook().getEntryBook().getLabel(), - e.getClosedPeriodicEntryBook().getFinancialPeriod().getBeginDate(), - e.getClosedPeriodicEntryBook().getFinancialPeriod().getEndDate())); - - // restaure prévious letters - for (Map.Entry<Entry, String> mapEntry : previousLetterMap.entrySet()) { - mapEntry.getKey().setLettering(mapEntry.getValue()); - } } }