Untitled

 avatar
unknown
java
10 months ago
2.0 kB
31
Indexable
public double getCustomerBalance(int id, Long toTime, boolean expired) {
  if (expired) {
    // Replace these with your actual logic to retrieve configuration values
    int deadline = getConfig("payments.deadline", getConfig("invoices.paytime", 0));

    if (toTime == null) {
      toTime = System.currentTimeMillis() / 1000; // Convert to seconds
    }

    // Build the query based on transaction type and document types
    StringBuilder query = new StringBuilder("SELECT SUM(value * currencyvalue) FROM cash ");
    query.append("LEFT JOIN documents doc ON doc.id = cash.docid ");
    query.append("LEFT JOIN customers cust ON cust.id = cash.customerid ");
    query.append("LEFT JOIN divisions ON divisions.id = cust.divisionid ");
    query.append("WHERE cust.id = ? ");

    List<Integer> docTypes = Arrays.asList(DOC_RECEIPT, DOC_CNOTE, DOC_INVOICE, DOC_DNOTE);

    if (cash.type != 0) {
      query.append("AND cash.time < ? "); // Non-payment transactions before toTime
    } else {
      int payTime = calculatePayTime(cust, divisions);
      query.append("AND cash.time + ").append(payTime * 86400).append(" < ? "); // Payment transactions before deadline
    }

    query.append(" OR (cash.docid IS NOT NULL AND (");
    for (int i = 0; i < docTypes.size(); i++) {
      query.append("doc.type = ?");
      if (i < docTypes.size() - 1) {
        query.append(" OR ");
      }
    }
    query.append(" AND cash.time < ? OR ");

    for (int i = 0; i < docTypes.size(); i++) {
      query.append("doc.type = ?");
      if (i < docTypes.size() - 1) {
        query.append(" OR ");
      }
    }
    query.append(" AND doc.cdate + doc.paytime * 86400 < ?))");

    return executeQuery(query.toString(), id, toTime, docTypes, docTypes, toTime);
  } else {
    StringBuilder query = new StringBuilder("SELECT SUM(value * currencyvalue) FROM cash WHERE customerid = ?");
    if (toTime != null) {
      query.append(" AND time < ?");
    }
    return executeQuery(query.toString(), id, (toTime != null ? toTime : Long.MAX_VALUE));
  }
}
Editor is loading...
Leave a Comment