Merge branch 'main' into module/contact
This commit is contained in:
@@ -169,6 +169,13 @@ public final class Document implements Mappable {
|
||||
return !dirtyFields.isEmpty() || sender.isDirty() || customer.isDirty();
|
||||
}
|
||||
|
||||
public boolean isDirty(String ... fields){
|
||||
for (var field : fields){
|
||||
if (dirtyFields.contains(field)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isNew(){
|
||||
return dirtyFields.contains(ID);
|
||||
}
|
||||
|
||||
@@ -411,7 +411,13 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
||||
|
||||
private boolean patchDocument(long docId, UmbrellaUser user, HttpExchange ex) throws UmbrellaException, IOException {
|
||||
var doc = getDocument(docId,user).a;
|
||||
doc.patch(json(ex));
|
||||
var data = json(ex);
|
||||
doc.patch(data);
|
||||
if (doc.isDirty(FIELD_FOOTER,FIELD_HEAD)) {
|
||||
var settings = db.getCustomerSettings(doc.companyId(),doc.type(),doc.customer().id());
|
||||
if (settings == null) settings = CustomerSettings.empty();
|
||||
db.save(doc.companyId(),doc.type(),doc.customer().id(), settings.patch(data));
|
||||
}
|
||||
db.save(doc);
|
||||
return ok(ex);
|
||||
}
|
||||
@@ -488,6 +494,7 @@ public class DocumentApi extends BaseHandler implements DocumentService {
|
||||
String currency = company.currency();
|
||||
String sep = company.decimalSeparator();
|
||||
var settings = db.getCustomerSettings(companyId.longValue(),type,customer.id());
|
||||
if (settings == null) settings = CustomerSettings.empty();
|
||||
var companySettings = db.getCompanySettings(companyId.longValue(),type);
|
||||
var nextNumber = companySettings.nextDocId();
|
||||
String lastHead = settings.header();
|
||||
|
||||
@@ -285,12 +285,11 @@ CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255)
|
||||
CustomerSettings settings = null;
|
||||
if (rs.next()) settings = CustomerSettings.of(rs);
|
||||
rs.close();
|
||||
if (settings != null) return settings;
|
||||
|
||||
return settings;
|
||||
} catch (SQLException e) {
|
||||
LOG.log(WARNING,"Failed to load customer settings (company: {0}, document type: {1}",companyId, docType.name(),e);
|
||||
throw new UmbrellaException(500,"Failed to load customer settings (company: {0}, document type: {1}",companyId, docType.name());
|
||||
}
|
||||
throw new UmbrellaException(500,"Failed to load customer settings (company: {0}, document type: {1}",companyId, docType.name());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -557,6 +556,7 @@ CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255)
|
||||
|
||||
@Override
|
||||
public CustomerSettings save(long companyId, Type docType, String customerId, CustomerSettings settings) throws UmbrellaException {
|
||||
if (!settings.isDirty()) return settings;
|
||||
try {
|
||||
replaceInto(TABLE_CUSTOMER_SETTINGS,FIELD_COMPANY_ID,FIELD_DOC_TYPE_ID,FIELD_CUSTOMER_NUMBER,FIELD_DEFAULT_HEADER,FIELD_DEFAULT_FOOTER,FIELD_DEFAULT_MAIL)
|
||||
.values(companyId,docType.id(),customerId,settings.header(),settings.footer(),settings.mailText())
|
||||
|
||||
@@ -3,14 +3,50 @@ package de.srsoftware.umbrella.documents.model;
|
||||
|
||||
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.documents.Constants.*;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
||||
public record CustomerSettings(String header, String footer, String mailText) implements Mappable {
|
||||
public class CustomerSettings implements Mappable {
|
||||
private static final System.Logger LOG = System.getLogger(CustomerSettings.class.getSimpleName());
|
||||
private String footer, header, mailText;
|
||||
private HashSet<String> dirtyFields = new HashSet<>();
|
||||
|
||||
public CustomerSettings(String header, String footer, String mailText){
|
||||
this.header = header;
|
||||
this.footer = footer;
|
||||
this.mailText = mailText;
|
||||
}
|
||||
|
||||
public CustomerSettings clear(){
|
||||
dirtyFields.clear();
|
||||
return this;
|
||||
}
|
||||
public static CustomerSettings empty() {
|
||||
return new CustomerSettings("","","");
|
||||
}
|
||||
|
||||
public String footer(){
|
||||
return footer;
|
||||
}
|
||||
|
||||
public String header() {
|
||||
return header;
|
||||
}
|
||||
|
||||
public boolean isDirty() {
|
||||
return !dirtyFields.isEmpty();
|
||||
}
|
||||
|
||||
public String mailText(){
|
||||
return mailText;
|
||||
}
|
||||
|
||||
public static CustomerSettings of(ResultSet rs) throws SQLException {
|
||||
var header = rs.getString(FIELD_DEFAULT_HEADER);
|
||||
var footer = rs.getString(FIELD_DEFAULT_FOOTER);
|
||||
@@ -22,4 +58,20 @@ public record CustomerSettings(String header, String footer, String mailText) im
|
||||
public Map<String, Object> toMap() {
|
||||
return Map.of(FIELD_FOOTER,footer,FIELD_HEAD,header,CONTENT,mailText);
|
||||
}
|
||||
|
||||
public CustomerSettings patch(JSONObject json) {
|
||||
for (var key : json.keySet()){
|
||||
var valid = true;
|
||||
switch (key){
|
||||
case FIELD_FOOTER:
|
||||
footer = json.getString(key); break;
|
||||
case FIELD_HEAD:
|
||||
header = json.getString(key); break;
|
||||
default:
|
||||
valid = false;
|
||||
}
|
||||
if (valid) dirtyFields.add(key);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
"advertisement" : "Umbrella ist ein Produkt von {producer}.",
|
||||
"allowed_states": "zulässige Status",
|
||||
"amount": "Menge",
|
||||
"archive": "archivieren",
|
||||
|
||||
"bank_account": "Bankverbindung",
|
||||
"base_url": "Basis-URL",
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
"advertisement" : "Umbrella is a product of {producer}.",
|
||||
"allowed_states": "allowed states",
|
||||
"amount": "amount",
|
||||
"archive": "archive",
|
||||
|
||||
"bank_account": "bank account",
|
||||
"base_url": "base URL",
|
||||
|
||||
Reference in New Issue
Block a user