implemented

- assigning new customer numbers to contacts that don`t have one
- updating customer number counter in company table

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-10-31 22:09:59 +01:00
parent dc0b381aba
commit 4468f45064
14 changed files with 157 additions and 20 deletions

View File

@@ -15,6 +15,7 @@ public class Constants {
public static final String CLONE = "clone";
public static final String CONFIG_DATABASE = "umbrella.modules.document.database";
public static final String CONFIG_TEMPLATES = "umbrella.modules.document.templates";
public static final String CONTACT_ID = "contact_id";
public static final String CONTACTS = "contacts";
public static final String CUSTOMERS = "customers";

View File

@@ -460,28 +460,37 @@ public class DocumentApi extends BaseHandler implements DocumentService {
private boolean postDocument(HttpExchange ex, UmbrellaUser user) throws IOException, UmbrellaException {
var json = json(ex);
if (!(json.has(SENDER) && json.get(SENDER) instanceof JSONObject senderData)) throw missingFieldException(SENDER);
if (!senderData.has(COMPANY) || !(senderData.get(COMPANY) instanceof Number companyId)) throw missingFieldException(COMPANY);
var company = companyService().get(companyId.longValue());
if (!companyService().membership(companyId.longValue(),user.id())) throw forbidden("You are mot a member of company {0}",company);
if (!senderData.has(COMPANY) || !(senderData.get(COMPANY) instanceof Number rawCompId)) throw missingFieldException(COMPANY);
var companyId = rawCompId.longValue();
var company = companyService().get(companyId);
if (!companyService().membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company);
if (!json.has(CUSTOMER) || !(json.get(CUSTOMER) instanceof JSONObject customerData)) throw missingFieldException(CUSTOMER);
if (!json.has(TYPE) || !(json.get(TYPE) instanceof Number docTypeId)) throw missingFieldException(TYPE);
var type = db.getType(docTypeId.intValue());
var customer = Customer.of(customerData);
Template template = new Template(6,companyId.longValue(),"unknwon",null);
Template template = new Template(6,companyId,"unknwon",null);
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 settings = db.getCustomerSettings(companyId,type,customer.id());
var newCustomer = settings == null;
if (newCustomer) settings = CustomerSettings.empty();
var companySettings = db.getCompanySettings(companyId,type);
var nextNumber = companySettings.nextDocId();
String lastHead = settings.header();
String lastFooter = settings.footer();
var sender = Sender.of(senderData);
LOG.log(DEBUG,json.toString(2));
var doc = new Document(0,companyId.longValue(),nextNumber,type, LocalDate.now(), NEW,template,null,lastHead,lastFooter,currency,sep,sender,customer,new PositionList());
var doc = new Document(0,companyId,nextNumber,type, LocalDate.now(), NEW,template,null,lastHead,lastFooter,currency,sep,sender,customer,new PositionList());
var saved = db.save(doc);
if (newCustomer) {
if (customerData.get(CONTACT_ID) instanceof Number contactId) {
var contacts = contactService();
var contact = contacts.load(user.id(), contactId.longValue());
contacts.save(contact.setCustomerNumber(customer.id()));
}
companyService().saveNewCustomer(companyId,customer.id());
}
db.step(companySettings);
return sendContent(ex,saved);
}