19 package org.turro.financials.account.logic;
21 import java.io.BufferedReader;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.text.DateFormat;
26 import java.text.ParseException;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.Date;
31 import java.util.List;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
38 import jxl.NumberCell;
41 import jxl.WorkbookSettings;
42 import jxl.read.biff.BiffException;
43 import org.turro.command.Command;
44 import org.turro.command.Context;
45 import org.turro.elephant.context.ElephantContext;
46 import org.turro.elephant.impl.util.FileUtil;
47 import org.turro.file.FileWrapper;
48 import org.turro.financials.db.FinancialsPU;
49 import org.turro.financials.entity.BankAccount;
50 import org.turro.financials.entity.Contract;
51 import org.turro.jpa.Dao;
52 import org.turro.math.Round;
53 import org.zkoss.lang.Strings;
61 private static final String BANK_IMPORT =
"/WEB-INF/bank/n43.txt";
63 public static Collection<StatementEntry>
getMovements(String account, Date start, Date end) {
65 if(!Strings.isBlank(account) && account.length() == 10) {
66 contract = getContract(account);
68 ArrayList<StatementEntry> list =
new ArrayList<>();
69 if(contract !=
null) {
71 for(
BankAccount ba : (List<BankAccount>) dao.getResultList(
72 "select ba from BankAccount ba " +
73 "where ba.contract = ? " +
74 "and ba.transactionDate >= ? " +
75 "and ba.transactionDate <= ?",
90 public Object execute(Context context) {
91 if(!importFromXls(contract, n43)) {
93 importFromFile(contract, n43);
94 }
catch (IOException ex) {
104 private static void importFromFile(
Contract contract, File n43)
throws FileNotFoundException, IOException {
111 while ((line = br.readLine()) !=
null) {
112 if(line.startsWith(
"11")) {
113 if(Strings.isBlank(contract.getGlobalId())) {
114 contract.setGlobalId(line.substring(10, 20));
116 balance = Double.valueOf(line.substring(33, 47)) / 100;
117 if(line.charAt(32) ==
'1') {
120 deleteFromTo(contract, getDate(line.substring(20, 26)), getDate(line.substring(26, 32)));
121 }
else if(line.startsWith(
"22")) {
122 if(bankAccount !=
null) {
123 dao.saveObject(bankAccount);
125 bankAccount =
new BankAccount();
128 bankAccount.
setValueDate(getDate(line.substring(16, 22)));
130 if(line.charAt(27) ==
'1') {
131 bankAccount.
setCredit(Double.valueOf(line.substring(28, 42)) / 100);
132 balance =
new Round(balance - bankAccount.
getCredit()).decimals(2).value();
135 bankAccount.
setDebit(Double.valueOf(line.substring(28, 42)) / 100);
136 balance =
new Round(balance + bankAccount.
getDebit()).decimals(2).value();
139 }
else if(line.startsWith(
"23")) {
140 if(Strings.isBlank(bankAccount.
getConcept())) {
141 bankAccount.
setConcept(line.substring(4).replaceAll(
"\\s+",
" "));
143 }
else if(line.startsWith(
"88")) {
144 if(bankAccount !=
null) {
145 dao.saveObject(bankAccount);
152 private static boolean importFromXls(Contract contract, File n43) {
153 Dao dao =
new FinancialsPU();
155 WorkbookSettings wsRead =
new WorkbookSettings();
156 wsRead.setEncoding(
"ISO-8859-1");
157 Workbook workbook = Workbook.getWorkbook(n43, wsRead);
158 Sheet sheet = workbook.getSheet(0);
159 for (
int row = 3; row < sheet.getRows(); row++) {
160 BankAccount bankAccount =
new BankAccount();
161 bankAccount.setContract(contract);
162 Cell cell = sheet.getCell(1, row);
163 if (cell.getType().equals(CellType.DATE)) {
164 bankAccount.setTransactionDate(((DateCell) cell).getDate());
166 cell = sheet.getCell(2, row);
167 if (cell.getType().equals(CellType.DATE)) {
168 bankAccount.setValueDate(((DateCell) cell).getDate());
170 bankAccount.setRegisterOrder(row - 3);
171 cell = sheet.getCell(4, row);
172 if (cell.getType().equals(CellType.NUMBER)) {
173 double num = ((NumberCell) cell).getValue();
175 bankAccount.setCredit(-num);
177 bankAccount.setDebit(num);
180 cell = sheet.getCell(5, row);
181 if (cell.getType().equals(CellType.NUMBER)) {
182 double num = ((NumberCell) cell).getValue();
183 bankAccount.setBalance(num);
185 cell = sheet.getCell(0, row);
186 if (cell.getType().equals(CellType.LABEL)) {
187 LabelCell label = (LabelCell) cell;
188 bankAccount.setConcept(label.getString());
190 dao.saveObject(bankAccount);
192 }
catch (IOException | BiffException ex) {
193 Logger.getLogger(BankAccounts.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
199 private static ThreadLocal<DateFormat> dateFormat =
new ThreadLocal<DateFormat>() {
201 protected DateFormat initialValue() {
202 return new SimpleDateFormat(
"yyMMdd");
206 private static Date getDate(String date) {
208 return dateFormat.get().parse(date);
209 }
catch (ParseException ex) {
210 Logger.getLogger(BankAccounts.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(date), ex);
215 private static void deleteFromTo(Contract contract, Date start, Date end) {
216 Dao dao =
new FinancialsPU();
218 "delete from BankAccount ba " +
219 "where ba.contract = ? " +
220 "and ba.transactionDate >= ? " +
221 "and ba.transactionDate <= ?",
227 private static Contract getContract(String account) {
228 Dao dao =
new FinancialsPU();
229 return (Contract) dao.getSingleResultOrNull(
230 "select c from Contract c where c.id = ? " +
231 "and c.contractDefinition.asCash = ?",
233 Long.valueOf(account.substring(5)),
234 account.substring(0, 3)
static String getRealPath(String path)
static String logMsg(String msg)
static BufferedReader getBufferedFile(String folder, String file)
void uploadContent(final Command command)
static void importN43(final Contract contract)
static Collection< StatementEntry > getMovements(String account, Date start, Date end)
void setRegisterOrder(long registerOrder)
void setDebit(double debit)
void setTransactionDate(Date transactionDate)
void setValueDate(Date valueDate)
void setCredit(double credit)
void setConcept(String concept)
void setContract(Contract contract)
void setBalance(double balance)