All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.premiumminds.billy.portugal.persistence.dao.jpa.DAOPTTaxImpl Maven / Gradle / Ivy

/*
 * Copyright (C) 2017 Premium Minds.
 *
 * This file is part of billy portugal (PT Pack).
 *
 * billy portugal (PT Pack) is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * billy portugal (PT Pack) is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with billy portugal (PT Pack). If not, see .
 */
package com.premiumminds.billy.portugal.persistence.dao.jpa;

import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQuery;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.persistence.EntityManager;

import com.premiumminds.billy.core.persistence.dao.jpa.DAOTaxImpl;
import com.premiumminds.billy.portugal.persistence.dao.DAOPTTax;
import com.premiumminds.billy.portugal.persistence.entities.PTRegionContextEntity;
import com.premiumminds.billy.portugal.persistence.entities.PTTaxEntity;
import com.premiumminds.billy.portugal.persistence.entities.jpa.JPAPTRegionContextEntity;
import com.premiumminds.billy.portugal.persistence.entities.jpa.JPAPTTaxEntity;
import com.premiumminds.billy.portugal.persistence.entities.jpa.QJPAPTRegionContextEntity;
import com.premiumminds.billy.portugal.persistence.entities.jpa.QJPAPTTaxEntity;

public class DAOPTTaxImpl extends DAOTaxImpl implements DAOPTTax {

    @Inject
    public DAOPTTaxImpl(Provider emProvider) {
        super(emProvider);
    }

    @Override
    public PTTaxEntity getEntityInstance() {
        return new JPAPTTaxEntity();
    }

    @Override
    protected Class getEntityClass() {
        return JPAPTTaxEntity.class;
    }

    @Override
    public List getTaxesForSAFTPT(PTRegionContextEntity context, Date validFrom, Date validTo) {
        QJPAPTTaxEntity tax = QJPAPTTaxEntity.jPAPTTaxEntity;
        JPAQuery query = new JPAQuery<>(this.getEntityManager());

        List predicates = new ArrayList<>();
        BooleanExpression active = tax.active.eq(true);
        predicates.add(active);
        BooleanExpression regionContext = tax.context.eq(context);
        predicates.add(regionContext);
        if (validFrom != null) {
            BooleanExpression dateFrom = tax.validFrom.eq(validFrom);
            predicates.add(dateFrom);
        }

        if (validTo != null) {
            BooleanExpression dateTo = tax.validTo.eq(validTo);
            predicates.add(dateTo);
        }

        query.from(tax);
        for (BooleanExpression e : predicates) {
            query.where(e);
        }
        List list = query.select(tax).fetch();
        List childContexts = null;
        List taxResult = null;
        List taxContextResult = new ArrayList<>();

        for (JPAPTTaxEntity t : list) {
            childContexts = this.getChildContexts((JPAPTRegionContextEntity) t.getContext());
            for (JPAPTRegionContextEntity c : childContexts) {
                taxResult = this.getTaxesForSAFTPT(c, validFrom, validTo);
                if (taxResult != null) {
                    taxContextResult.addAll(taxResult);
                }
            }
        }
        if (taxContextResult != null) {
            list.addAll(taxContextResult);
        }
        return list;
    }

    private List getChildContexts(JPAPTRegionContextEntity parentContext) {
        QJPAPTRegionContextEntity contexts = QJPAPTRegionContextEntity.jPAPTRegionContextEntity;
        JPAQuery query = new JPAQuery<>(this.getEntityManager());

        query.from(contexts).where(contexts.parent.eq(parentContext));
        return query.select(contexts).fetch();
    }

    @Override
    public List getTaxes(PTRegionContextEntity context, Date validFrom, Date validTo) {

        QJPAPTTaxEntity tax = QJPAPTTaxEntity.jPAPTTaxEntity;
        JPAQuery query = new JPAQuery<>(this.getEntityManager());

        query.from(tax);
        List predicates = new ArrayList<>();
        BooleanExpression validFromPredicate = tax.validFrom.eq(validFrom);
        predicates.add(validFromPredicate);
		if (validTo != null) {
			BooleanExpression validToPredicate = tax.validTo.eq(validTo);
			predicates.add(validToPredicate);
		}
        BooleanExpression lessOrEqual = tax.validTo.loe(validFrom);
        predicates.add(lessOrEqual);
        BooleanExpression active = tax.active.eq(true);
        predicates.add(active);
        BooleanExpression contextPredicate = tax.context.eq(context);
        predicates.add(contextPredicate);

        for (BooleanExpression e : predicates) {
            query.where(e);
        }

        List list = query.select(tax).fetch();
        if (context.getParentContext() != null) {
            list.addAll(this.getTaxes((PTRegionContextEntity) context.getParentContext(), validFrom, validTo));
        }
        return list;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy