001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not use this file except in compliance with the License. 006// You may obtain a copy of the License at 007// 008// http://www.apache.org/licenses/LICENSE-2.0 009// 010// Unless required by applicable law or agreed to in writing, software 011// distributed under the License is distributed on an "AS IS" BASIS, 012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.model.control.accounting.server.database; 018 019import static com.echothree.model.jooq.server.tables.accounting.TransactionGlEntries.TransactionGlEntries; 020import static org.jooq.impl.DSL.coalesce; 021import static org.jooq.impl.DSL.sum; 022import com.echothree.model.data.accounting.server.entity.Transaction; 023import com.echothree.util.server.persistence.BaseDatabaseQuery; 024import com.echothree.util.server.persistence.EntityPermission; 025import com.echothree.util.server.persistence.Session; 026import java.math.BigDecimal; 027import java.util.List; 028 029public class TransactionBalancedQuery 030 extends BaseDatabaseQuery<TransactionBalancedResult> { 031 032 public TransactionBalancedQuery() { 033 super(EntityPermission.READ_ONLY); 034 } 035 036 public List<TransactionBalancedResult> execute(final Transaction transaction) { 037 var query = getDslContext() 038 .select(coalesce(sum(TransactionGlEntries.ORIGINAL_CREDIT), BigDecimal.ZERO) 039 .sub(coalesce(sum(TransactionGlEntries.ORIGINAL_DEBIT), BigDecimal.ZERO)).as("OriginalDifference"), 040 coalesce(sum(TransactionGlEntries.CREDIT), BigDecimal.ZERO) 041 .sub(coalesce(sum(TransactionGlEntries.DEBIT), BigDecimal.ZERO)).as("Difference")) 042 .from(TransactionGlEntries) 043 .where(TransactionGlEntries.TRANSACTION.eq(transaction.getPrimaryKey()), 044 TransactionGlEntries.THRU_TIME.eq(Session.MAX_TIME)); 045 046 return super.execute(query); 047 } 048 049}