|
|
|
|
@ -22,9 +22,9 @@ import static de.srsoftware.umbrella.core.Constants.FIELD_PRICE_FORMAT;
@@ -22,9 +22,9 @@ import static de.srsoftware.umbrella.core.Constants.FIELD_PRICE_FORMAT;
|
|
|
|
|
import static de.srsoftware.umbrella.core.Constants.FIELD_TIME_ID; |
|
|
|
|
import static de.srsoftware.umbrella.core.Constants.FIELD_TYPE; |
|
|
|
|
import static de.srsoftware.umbrella.core.Constants.FIELD_UNIT; |
|
|
|
|
import static de.srsoftware.umbrella.core.Paths.LIST; |
|
|
|
|
import static de.srsoftware.umbrella.core.Paths.STATES; |
|
|
|
|
import static de.srsoftware.umbrella.core.Paths.*; |
|
|
|
|
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_UNPROCESSABLE; |
|
|
|
|
import static de.srsoftware.umbrella.core.Util.mapValues; |
|
|
|
|
import static de.srsoftware.umbrella.core.Util.request; |
|
|
|
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*; |
|
|
|
|
import static de.srsoftware.umbrella.core.model.Document.State.NEW; |
|
|
|
|
@ -52,6 +52,7 @@ import de.srsoftware.tools.SessionToken;
@@ -52,6 +52,7 @@ import de.srsoftware.tools.SessionToken;
|
|
|
|
|
import de.srsoftware.tools.Tuple; |
|
|
|
|
import de.srsoftware.umbrella.core.BaseHandler; |
|
|
|
|
import de.srsoftware.umbrella.core.ModuleRegistry; |
|
|
|
|
import de.srsoftware.umbrella.core.Paths; |
|
|
|
|
import de.srsoftware.umbrella.core.api.*; |
|
|
|
|
import de.srsoftware.umbrella.core.exceptions.UmbrellaException; |
|
|
|
|
import de.srsoftware.umbrella.core.model.*; |
|
|
|
|
@ -148,7 +149,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -148,7 +149,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
yield switch (head){ |
|
|
|
|
case null -> getDocument(ex,docId,user.get()); |
|
|
|
|
case PATH_PDF -> getRenderedDocument(ex,docId,user.get()); |
|
|
|
|
case SETTINGS -> getDocumentSettings(ex,docId,user.get()); |
|
|
|
|
case Paths.SETTINGS -> getDocumentSettings(ex,docId,user.get()); |
|
|
|
|
default -> super.doGet(path,ex); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
@ -191,7 +192,8 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -191,7 +192,8 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
if (user.isEmpty()) return unauthorized(ex); |
|
|
|
|
var head = path.pop(); |
|
|
|
|
return switch (head){ |
|
|
|
|
case LIST -> listCompaniesDocuments(ex,user.get(),token.orElse(null)); |
|
|
|
|
case LIST -> listCompaniesDocuments(ex,user.get()); |
|
|
|
|
case SEARCH -> postSearch(ex,user.get()); |
|
|
|
|
case TEMPLATES -> postTemplateList(ex,user.get()); |
|
|
|
|
case null -> postDocument(ex,user.get()); |
|
|
|
|
default -> postToDocument(ex,path,user.get(),Long.parseLong(head)); |
|
|
|
|
@ -203,27 +205,6 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -203,27 +205,6 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean sendDocument(HttpExchange ex, Path path, UmbrellaUser user, long docId) throws IOException, UmbrellaException { |
|
|
|
|
var doc = getDocumentWithCompanyData(docId,user); |
|
|
|
|
var rendered = renderDocument(doc,user); |
|
|
|
|
var json = json(ex); |
|
|
|
|
if (!(json.has(EMAIL) && json.get(EMAIL) instanceof String email)) throw missingFieldException(EMAIL); |
|
|
|
|
if (!(json.has(SUBJECT) && json.get(SUBJECT) instanceof String subject)) throw missingFieldException(SUBJECT); |
|
|
|
|
if (!(json.has(CONTENT) && json.get(CONTENT) instanceof String content)) throw missingFieldException(CONTENT); |
|
|
|
|
var settings = new CustomerSettings(doc.head(),doc.footer(),content); |
|
|
|
|
try { |
|
|
|
|
db.save(settings,doc); |
|
|
|
|
} catch (UmbrellaException e) { |
|
|
|
|
LOG.log(WARNING,e); |
|
|
|
|
} |
|
|
|
|
var attachment = new Attachment(doc.number()+".pdf",rendered.mimeType(),rendered.bytes()); |
|
|
|
|
var message = new Message(user,subject,content,null,List.of(attachment)); |
|
|
|
|
var envelope = new Envelope(message,new User(doc.customer().shortName(),new EmailAddress(email),doc.customer().language())); |
|
|
|
|
postBox().send(envelope); |
|
|
|
|
db.save(doc.set(SENT)); |
|
|
|
|
return ok(ex); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean getContacts(HttpExchange ex, UmbrellaUser user, Token token) throws IOException, UmbrellaException { |
|
|
|
|
return sendContent(ex,getLegacyContacts(ex,user,token)); |
|
|
|
|
} |
|
|
|
|
@ -274,15 +255,6 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -274,15 +255,6 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
return sendContent(ex,settings); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private PriceFormat priceFormat(String currency, String language) { |
|
|
|
|
var pattern = switch (currency){ |
|
|
|
|
case "$" -> "$ {0,number,#,###.00}"; |
|
|
|
|
default -> "{0,number,#,###.00} "+currency; |
|
|
|
|
}; |
|
|
|
|
var message = new MessageFormat(pattern, "de".equals(language)? Locale.GERMAN:Locale.US); |
|
|
|
|
return val -> message.format(new Object[]{val/100d}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private DocumentData convert(Document document) throws UmbrellaException { |
|
|
|
|
var currency = switch (document.currency()){ |
|
|
|
|
case "€" -> Currency.EUR; |
|
|
|
|
@ -392,6 +364,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -392,6 +364,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
return content; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean getRenderedDocument(HttpExchange ex, long docId, UmbrellaUser user) throws IOException, UmbrellaException { |
|
|
|
|
var document = getDocumentWithCompanyData(docId,user); |
|
|
|
|
var content = renderDocument(document,user); |
|
|
|
|
@ -412,8 +385,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -412,8 +385,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
return db.listDocs(companyId); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean listCompaniesDocuments(HttpExchange ex, UmbrellaUser user, Token token) throws UmbrellaException { |
|
|
|
|
private boolean listCompaniesDocuments(HttpExchange ex, UmbrellaUser user) throws UmbrellaException { |
|
|
|
|
try { |
|
|
|
|
var json = json(ex); |
|
|
|
|
if (!json.has(COMPANY)) throw missingFieldException(COMPANY); |
|
|
|
|
@ -556,6 +528,17 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -556,6 +528,17 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
return sendContent(ex,templates.stream().map(Template::toMap)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean postSearch(HttpExchange ex, UmbrellaUser user) throws IOException { |
|
|
|
|
var json = json(ex); |
|
|
|
|
if (!(json.has(KEY) && json.get(KEY) instanceof String key)) throw missingFieldException(KEY); |
|
|
|
|
var keys = Arrays.asList(key.split(" ")); |
|
|
|
|
var fulltext = json.has(FULLTEXT) && json.get(FULLTEXT) instanceof Boolean val && val; |
|
|
|
|
|
|
|
|
|
var userCompanyIds = companyService().listCompaniesOf(user).keySet(); |
|
|
|
|
|
|
|
|
|
var documents = db.find(userCompanyIds,keys,fulltext); |
|
|
|
|
return sendContent(ex,mapValues(documents)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean postToDocument(HttpExchange ex, Path path, UmbrellaUser user, long docId) throws IOException, UmbrellaException { |
|
|
|
|
var head = path.pop(); |
|
|
|
|
@ -566,4 +549,34 @@ public class DocumentApi extends BaseHandler implements DocumentService {
@@ -566,4 +549,34 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
|
|
|
|
case null, default -> super.doPost(path,ex); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private PriceFormat priceFormat(String currency, String language) { |
|
|
|
|
var pattern = switch (currency){ |
|
|
|
|
case "$" -> "$ {0,number,#,###.00}"; |
|
|
|
|
default -> "{0,number,#,###.00} "+currency; |
|
|
|
|
}; |
|
|
|
|
var message = new MessageFormat(pattern, "de".equals(language)? Locale.GERMAN:Locale.US); |
|
|
|
|
return val -> message.format(new Object[]{val/100d}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean sendDocument(HttpExchange ex, Path path, UmbrellaUser user, long docId) throws IOException, UmbrellaException { |
|
|
|
|
var doc = getDocumentWithCompanyData(docId,user); |
|
|
|
|
var rendered = renderDocument(doc,user); |
|
|
|
|
var json = json(ex); |
|
|
|
|
if (!(json.has(EMAIL) && json.get(EMAIL) instanceof String email)) throw missingFieldException(EMAIL); |
|
|
|
|
if (!(json.has(SUBJECT) && json.get(SUBJECT) instanceof String subject)) throw missingFieldException(SUBJECT); |
|
|
|
|
if (!(json.has(CONTENT) && json.get(CONTENT) instanceof String content)) throw missingFieldException(CONTENT); |
|
|
|
|
var settings = new CustomerSettings(doc.head(),doc.footer(),content); |
|
|
|
|
try { |
|
|
|
|
db.save(settings,doc); |
|
|
|
|
} catch (UmbrellaException e) { |
|
|
|
|
LOG.log(WARNING,e); |
|
|
|
|
} |
|
|
|
|
var attachment = new Attachment(doc.number()+".pdf",rendered.mimeType(),rendered.bytes()); |
|
|
|
|
var message = new Message(user,subject,content,null,List.of(attachment)); |
|
|
|
|
var envelope = new Envelope(message,new User(doc.customer().shortName(),new EmailAddress(email),doc.customer().language())); |
|
|
|
|
postBox().send(envelope); |
|
|
|
|
db.save(doc.set(SENT)); |
|
|
|
|
return ok(ex); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|