18 package org.turro.financials.account;
21 import java.io.IOException;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import javax.activation.MimetypesFileTypeMap;
26 import org.amic.util.date.CheckDate;
27 import org.turro.action.IMailSender;
28 import org.turro.action.MailSenders;
29 import org.turro.auth.Authentication;
30 import org.turro.command.Command;
31 import org.turro.command.Context;
32 import org.turro.contacts.Contact;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.elephant.db.WhereClause;
35 import org.turro.elephant.util.DateFormats;
36 import org.turro.elephant.util.DecimalFormats;
37 import org.turro.financials.db.FinancialsPU;
38 import org.turro.financials.entity.Company;
39 import org.turro.financials.entity.Register;
40 import org.turro.financials.entity.RegisterView;
41 import org.turro.financials.model.business.CompanyWrapper;
42 import org.turro.financials.model.register.ViewWrapper;
43 import org.turro.financials.view.ViewListbox;
44 import org.turro.i18n.I_;
45 import org.turro.jpa.Dao;
46 import org.turro.jpa.export.ExportAsStream;
47 import org.turro.jpa.export.ExportQuery;
48 import org.turro.message.MessageQueue;
49 import org.turro.plugin.contacts.IContact;
50 import org.turro.print.Print;
51 import org.turro.sql.SqlClause;
52 import org.turro.zkoss.dialog.InputDialog;
53 import org.turro.zkoss.dialog.InputField;
54 import org.turro.zkoss.print.ReportMenuitem;
55 import org.turro.zul.frame.Framework;
56 import org.zkoss.zhtml.Filedownload;
57 import org.zkoss.zk.ui.HtmlBasedComponent;
65 private Integer year, startPage;
73 protected void print(
final String type) {
77 year =
new CheckDate().getYear();
84 new InputField(
"Year", year, null, 0),
85 new InputField(
"Start page", startPage, null, 0),
86 new InputField(
"View", null, null, 0) {
88 protected HtmlBasedComponent createEditor() {
89 ViewListbox vl = new ViewListbox();
96 public Object execute(Context context) {
98 if(fields.length > 0) {
100 if(
"Year".equals(f.getLabel())) {
102 }
else if(
"Start page".equals(f.getLabel())) {
103 startPage = (Integer) f.getValue() - 1;
104 }
else if(
"View".equals(f.getLabel())) {
105 view = (RegisterView) f.getValue();
109 IContact contact = Authentication.getIContact();
110 MailSenders.getHeavy()
112 .onStart(s -> MessageQueue.pushMessage(contact, I_.get(
"Report will be sent by email")))
113 .onCancel(s -> MessageQueue.pushMessage(contact, I_.get(
"Task already running")))
114 .onBuild(s -> print2execute((IMailSender) s))
115 .send(I_.get(
"Diary book"), I_.get(
"Diary book"));
123 private void print2execute(IMailSender sender) {
124 Collection<Long> lines = getRegisters();
125 if(lines !=
null && lines.size() > 0) {
126 Map parameters =
new HashMap();
127 int fractionDigits = CompanyWrapper.getCompanyCurrency().getDefaultFractionDigits();
128 Company company = CompanyWrapper.getDefaultCompany();
129 Contact companyContact = (Contact) company.getIContact().getContact();
130 parameters.put(
"company", company);
131 parameters.put(
"companyContact", companyContact);
132 parameters.put(
"dateFormat", DateFormats.getDefaultFormat());
133 parameters.put(
"currencyFormat", DecimalFormats.getStringFormat(fractionDigits));
134 parameters.put(
"exportFormat",
"pdf");
135 parameters.put(
"year", year);
136 parameters.put(
"startPage", startPage);
137 parameters.put(
"SUBREPORT_DIR",
"../_reports/financials/accounting/");
138 String file =
"/WEB-INF/_reports/financials/accounting/DiaryBook.jasper";
140 dao =
new FinancialsPU();
141 File result = File.createTempFile(
"report",
"_Diary.pdf");
143 .setTemplate(ElephantContext.getRealPath(file))
144 .setCollection(lines)
145 .setParameters(parameters)
146 .onRead(bean -> dao.find(Register.class, (Long) bean))
148 sender.addAttachment(result);
149 sender.onFinish(x -> sender.removeAttachments());
150 }
catch (IOException ex) {
151 Logger.getLogger(DiaryReport.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
162 wc.
addClause(
"and year(reg.registerDate) = :year");
167 return dao.getResultList(wc);
176 wc.
addClause(
"and year(reg.registerDate) = :year");
190 .send(
I_.
get(
"Export diary"),
I_.
get(
"Export diary"));
193 private static void diaryToExcel(
IMailSender sender) {
195 wc.
addClause(
"select r.view.name, year(r.registerDate), month(r.registerDate),day(r.registerDate), ");
196 wc.
addClause(
"substring(re.account.id, 1, 1), (select ma1.description from MajorAccount ma1 where ma1.account = substring(re.account.id, 1, 1)),");
197 wc.
addClause(
"substring(re.account.id, 1, 2), (select ma2.description from MajorAccount ma2 where ma2.account = substring(re.account.id, 1, 2)),");
198 wc.
addClause(
"substring(re.account.id, 1, 3), (select ma3.description from MajorAccount ma3 where ma3.account = substring(re.account.id, 1, 3)),");
199 wc.
addClause(
"ma.account, ma.description, re.account.id,");
200 wc.
addClause(
"re.account.description, re.debit, re.credit");
201 wc.
addClause(
"from RegisterEntry re join re.register r, MajorAccount ma");
202 wc.
addClause(
"where ma.account = (select max(m.account) from MajorAccount as m");
203 wc.
addClause(
"where re.account.id like concat(m.account, '%'))");
204 wc.
addClause(
"order by r.view.name, r.registerDate");
206 List<String> l = Arrays.asList(
new String[] {
211 "M1",
"M1Desc",
"M2",
"M2Desc",
"M3",
"M3Desc",
212 I_.
get(
"Major account"),
215 I_.
get(
"Description"),
220 Dao dao =
new FinancialsPU();
221 ExportAsStream eq =
new ExportAsStream(dao, l, wc);
222 File result = File.createTempFile(
"report",
"_Diary.xls");
223 eq.generateExcel(result);
226 }
catch (IOException ex) {
227 Logger.getLogger(DiaryReport.class.getName()).log(Level.SEVERE,
null, ex);
238 wc.
addClause(
"substring(re.account.id, 1, 1), (select ma1.description from MajorAccount ma1 where ma1.account = substring(re.account.id, 1, 1)),");
239 wc.
addClause(
"substring(re.account.id, 1, 2), (select ma2.description from MajorAccount ma2 where ma2.account = substring(re.account.id, 1, 2)),");
240 wc.
addClause(
"substring(re.account.id, 1, 3), (select ma3.description from MajorAccount ma3 where ma3.account = substring(re.account.id, 1, 3)),");
241 wc.
addClause(
"ma.account, ma.description, re.account.id,");
242 wc.
addClause(
"re.account.description, sum(re.debit), sum(re.credit)");
243 wc.
addClause(
"from RegisterEntry re join re.register r, MajorAccount ma");
244 wc.
addClause(
"where ma.account = (select max(m.account) from MajorAccount as m");
245 wc.
addClause(
"where re.account.id like concat(m.account, '%'))");
246 wc.
addClause(
"and r.registerDate < :date");
247 wc.
addNamedValue(
"date",
new CheckDate(year, month, 1, 0, 0, 0).addMonths(1).getDate());
249 wc.
addClause(
"and year(r.registerDate) >= :carry");
251 wc.
addClause(
"and not (r.exclude = TRUE and year(r.registerDate) = :year)");
254 wc.
addClause(
"and year(r.registerDate) = :carry");
256 wc.
addClause(
"and not (r.exclude = TRUE or r.closing = TRUE)");
259 wc.
addClause(
"group by r.view.name, year(r.registerDate), month(r.registerDate), re.account.id, ma.account, ma.description");
261 wc.
addClause(
"group by r.view.name, re.account.id, ma.account, ma.description");
267 l = Arrays.asList(
new String[] {
271 "M1",
"M1Desc",
"M2",
"M2Desc",
"M3",
"M3Desc",
272 I_.
get(
"Major account"),
275 I_.
get(
"Description"),
280 l = Arrays.asList(
new String[] {
282 "M1",
"M1Desc",
"M2",
"M2Desc",
"M3",
"M3Desc",
283 I_.
get(
"Major account"),
286 I_.
get(
"Description"),
294 Filedownload.save(eq.
getExcel(name),
new MimetypesFileTypeMap().getContentType(name), name);
298 return SqlClause.select(
"year(max(registerDate))").from(
"Register")
299 .where().equal(
"closing",
true)
300 .and().equal(
"exclude",
false)
301 .and().smallerOrEqual(
"year(registerDate)", year)
302 .and().equal(
"view.id", 1L)
304 .singleResult(Integer.class);
static IMailSender getHeavy()
static IContact getIContact()
static String getSiteName()
void addClause(String clause)
void addNamedValue(String name, Object value)
static void exportDiary()
static void exportAccountBalance(int year, int month, boolean carry, boolean breakdown)
WhereClause getSQLRegisters()
void print(final String type)
static Integer getLastInitialSince(int year)
Collection< Long > getRegisters()
static RegisterView getFormalView()
static String get(String msg)
InputStream getExcel(String sheetName)
static void pushMessage(IContact contact, String msg)
static Framework getCurrent()
IMailSender onFinish(Consumer command)
IMailSender addAttachment(File file)