package com.jasamedika.medifirst2000.dao.custom.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.dao.custom.KartuPengendaliDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.KartuPengendali;
import com.jasamedika.medifirst2000.entities.KelompokUser;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;

/**
 * Repository Custom class
 * 
 * @author Adik
 * @param <T>
 */
@Repository
public class KartuPengendaliDaoCustomImpl extends CoreDaoImpl<KartuPengendali> implements KartuPengendaliDaoCustom {

	public KartuPengendaliDaoCustomImpl() {
		super(KartuPengendali.class);
	}

	@Override
	public List<Map<String, Object>> findKartuPengendali(int rowStart, int rowEnd, String sort, String dir,
			String dateStart, String dateEnd, Integer pengendaliId, Boolean isPpk, Boolean isUlp, Boolean isPp) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map( ");
		buffer.append(" detail.noRec as noRec, ");
		buffer.append(" produk.namaProduk as namaProduk, ");
		buffer.append(" detail.noUsulan as noUsulanPermintaan, ");
		buffer.append(" header.tanggal as tanggal, ");
		buffer.append(" pengendali.namaPengendali as pengendali, ");
		buffer.append(" detail.keterangan as keterangan, ");
		buffer.append(" detail.totalHargaBarang as total, ");
		buffer.append(" detail.volumeBarang as volume, ");
		buffer.append(" detail.hargaSatuanBarang as hargaSatuan, ");
		buffer.append(" detail.hargaSatuanBarangHps as hargaHps, ");
		buffer.append(" pengendali.namaPengendali as namaPengendali, ");
		buffer.append(" detail.isVerifikasi as isVerifikasi ");
		buffer.append(" ) ");
		buffer.append(" from KartuPengendaliDetail detail ");
		buffer.append(" left join detail.detailRup as detailRup ");
		buffer.append(" left join detailRup.detailSpekAnggaran spek ");
		buffer.append(" left join detail.kartuPengendali header ");
		buffer.append(" left join spek.produk produk ");
		buffer.append(" left join spek.pengendali pengendali,DetailPengadaan detailPengadaan ");
		buffer.append(" left join detailPengadaan.pengadaan pengadaan ");
		buffer.append(" where detail.isActive = true and detailPengadaan.detailRupId=detailRup.id ");

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append("and date(header.tanggal) between :dateStart and :dateEnd ");
		}

		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			buffer.append("and pengendali.id =:pengendaliId ");
		}

		if (CommonUtil.isNotNullOrEmpty(isPpk)) {
			if (isPpk) {
				buffer.append(
						"and (detail.isVerifikasi ='VERIFIKASI_ANGGARAN' or detail.isVerifikasi ='VERIFIKASI_PPK') ");
			}
		} else if (CommonUtil.isNotNullOrEmpty(isUlp)) {
			if (isUlp) {
				buffer.append("and detail.isVerifikasi ='VERIFIKASI_PPK' and pengadaan.id=1 ");
			}
		} else if (CommonUtil.isNotNullOrEmpty(isPp)) {
			if (isPp) {
				buffer.append("and detail.isVerifikasi ='VERIFIKASI_PPK' and pengadaan.id=2 ");
			}
		} else {
			buffer.append(
					"and (detail.isVerifikasi ='VERIFIKASI_ANGGARAN' or detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='VERIFIKASI_PENGENDALI') ");
		}

		if (sort != "") {
			buffer.append("order by detail." + sort + " " + dir);
		} else {
			buffer.append("order by detail.noRec desc ");
		}

		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			query.setParameter("dateStart", DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}

		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			query.setParameter("pengendaliId", pengendaliId);
		}

		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);

		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public int findKartuPengendaliCount(String sort, String dateStart, String dateEnd, Integer pengendaliId,
			Boolean isPpk, Boolean isUlp, Boolean isPp) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(" select count(detail) ");
		buffer.append(" from KartuPengendaliDetail detail ");
		buffer.append(" left join detail.detailRup as detailRup ");
		buffer.append(" left join detailRup.detailSpekAnggaran spek ");
		buffer.append(" left join detail.kartuPengendali header ");
		buffer.append(" left join spek.produk produk ");
		buffer.append(" left join spek.pengendali pengendali,DetailPengadaan detailPengadaan ");
		buffer.append(" left join detailPengadaan.pengadaan pengadaan ");
		buffer.append(" where detail.isActive = true and detailPengadaan.detailRupId=detailRup.id ");

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append("and date(header.tanggal) between :dateStart and :dateEnd ");
		}

		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			buffer.append("and pengendali.id =:pengendaliId ");
		}

		if (CommonUtil.isNotNullOrEmpty(isPpk)) {
			if (isPpk) {
				buffer.append(
						" and detail.isVerifikasi ='VERIFIKASI_ANGGARAN' or detail.isVerifikasi ='VERIFIKASI_PPK' ");
			}
		} else if (CommonUtil.isNotNullOrEmpty(isUlp)) {
			if (isUlp) {
				buffer.append(
						" and (detail.isVerifikasi ='VERIFIKASI_ANGGARAN' or detail.isVerifikasi ='VERIFIKASI_PPK') ");
			}
		}else if (CommonUtil.isNotNullOrEmpty(isPp)) {
			if (isPp) {
				buffer.append("and detail.isVerifikasi ='VERIFIKASI_PPK' and pengadaan.id=2 ");
			}
		} else {
			buffer.append(
						" and (detail.isVerifikasi ='VERIFIKASI_ANGGARAN' or detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='VERIFIKASI_PENGENDALI') ");
		}

		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			query.setParameter("dateStart", DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}

		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			query.setParameter("pengendaliId", pengendaliId);
		}

		return ((Long) query.getSingleResult()).intValue();

	}

	@Override
	public List<Map<String, Object>> generateTemplateExel(String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map( ");
		buffer.append(" detail.noRec as noRec, ");
		buffer.append(" produk.namaProduk as namaProduk, ");
		buffer.append(" detail.noUsulan as noUsulanPermintaan, ");
		buffer.append(" header.tanggal as tanggal, ");
		buffer.append(" pengendali.namaPengendali as pengendali, ");
		buffer.append(" detail.keterangan as keterangan, ");
		buffer.append(" detail.totalHargaBarang as total, ");
		buffer.append(" detail.volumeBarang as volume, ");
		buffer.append(" detail.hargaSatuanBarang as hargaSatuan, ");
		buffer.append(" detail.hargaSatuanBarangHps as hargaHps, ");
		buffer.append(" pengendali.namaPengendali as namaPengendali, ");
		buffer.append(" spek.spesifikasi as spesifikasi, ");
		buffer.append(" detail.isVerifikasi as isVerifikasi ");
		buffer.append(" ) ");
		buffer.append(" from KartuPengendaliDetail detail ");
		buffer.append(" left join detail.detailRup as detailRup ");
		buffer.append(" left join detailRup.detailSpekAnggaran spek ");
		buffer.append(" left join detail.kartuPengendali header ");
		buffer.append(" left join spek.produk produk ");
		buffer.append(" left join spek.pengendali pengendali ");
		buffer.append(" where detail.isActive = true and header.noRec=:noRec  ");

	
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);

	
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public int findKartuPengendaliHeaderCount(String sort, String dateStart, String dateEnd, Integer pengendaliId,Boolean isPpk,Boolean isUlp, Boolean isPp,Boolean isAnggaran,Boolean isPengendali,Boolean isDirekturTerkait, List<Integer> pengedaliIdList) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select ");
		buffer.append(" count(distinct header.noRec) as totalCount ");
		buffer.append(" from KartuPengendaliDetail detail   ");
		buffer.append(" left join detail.detailRup as detailRup   ");
		buffer.append(" left join detailRup.detailSpekAnggaran spek   ");
		buffer.append(" left join spek.detailAnggaran detailAnggaran ");
		buffer.append(" left join detailAnggaran.asalProduk asalProduk ");
		buffer.append(" left join detail.kartuPengendali header   ");
		buffer.append(" left join spek.produk produk  ");
		buffer.append(" left join spek.pengendali pengendali ");
		buffer.append(" left join detailAnggaran.akun akun, ");
		buffer.append(" DetailPengadaan detailPengadaan  ");
		buffer.append(" left join detailPengadaan.pengadaan pengadaan  ");
		buffer.append(" where detail.isActive = true  ");
		buffer.append(" and detailPengadaan.detailRupId=detailRup.id ");
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(header.tanggal) between :dateStart and :dateEnd ");
		}
		
		
		if(CommonUtil.isNotNullOrEmpty(isUlp)){
			if(isUlp){
				buffer.append("and (detail.isVerifikasi ='VERIFIKASI_ULP' or detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_ULP')  ");
				buffer.append(" and pengadaan.id=1 ");
			}
		}else if(CommonUtil.isNotNullOrEmpty(isPp)){
			if(isPp){
				buffer.append("and (detail.isVerifikasi ='VERIFIKASI_PP' or detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_PP')  ");
				buffer.append(" and pengadaan.id=2 ");
			}
		}else if(CommonUtil.isNotNullOrEmpty(isPpk)){
			if(isPpk){
				buffer.append("and (detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_ANGGARAN' or detail.isVerifikasi ='VERIFIKASI_ANGGARAN' OR detail.isVerifikasi ='VERIFIKASI_ULP' or detail.isVerifikasi ='UNVERIFIKASI_ULP') ");
			}
		}else if(CommonUtil.isNotNullOrEmpty(isAnggaran)){
			if(isAnggaran){
				buffer.append("and (detail.isVerifikasi ='VERIFIKASI_DIREKTUR' or detail.isVerifikasi ='VERIFIKASI_ANGGARAN' or detail.isVerifikasi ='UNVERIFIKASI_ANGGARAN' OR detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_PPK' OR detail.isVerifikasi ='VERIFIKASI_ULP' or detail.isVerifikasi ='UNVERIFIKASI_ULP')  ");
			}
			if(CommonUtil.isNotNullOrEmpty(pengedaliIdList)){
				buffer.append(" and spek.pengendaliId in (:pengedaliIdList) ");
			}
		}else if(CommonUtil.isNotNullOrEmpty(isDirekturTerkait)){
			if(isDirekturTerkait){
				buffer.append("and (detail.isVerifikasi ='VERIFIKASI_PENGENDALI' or detail.isVerifikasi ='VERIFIKASI_DIREKTUR' or detail.isVerifikasi ='UNVERIFIKASI_DIREKTUR' or detail.isVerifikasi ='VERIFIKASI_ANGGARAN' or detail.isVerifikasi ='UNVERIFIKASI_ANGGARAN' OR detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_PPK' OR detail.isVerifikasi ='VERIFIKASI_ULP' or detail.isVerifikasi ='UNVERIFIKASI_ULP')  ");
			}
			if(CommonUtil.isNotNullOrEmpty(pengedaliIdList)){
				buffer.append(" and spek.pengendaliId in (:pengedaliIdList) ");
			}
		}else if(CommonUtil.isNotNullOrEmpty(isPengendali)){
			if(isPengendali){
				buffer.append("and (detail.isVerifikasi ='VERIFIKASI_PENGENDALI' or detail.isVerifikasi ='VERIFIKASI_DIREKTUR' or detail.isVerifikasi ='UNVERIFIKASI_DIREKTUR'  or detail.isVerifikasi ='VERIFIKASI_ANGGARAN' or detail.isVerifikasi ='UNVERIFIKASI_ANGGARAN' OR detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_PPK' OR detail.isVerifikasi ='VERIFIKASI_ULP' or detail.isVerifikasi ='UNVERIFIKASI_ULP')  ");
			}
			if(CommonUtil.isNotNullOrEmpty(pengedaliIdList)){
				buffer.append(" and spek.pengendaliId in (:pengedaliIdList) ");
			}
		}
			
			
		Query query = em.createQuery(buffer.toString());
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}
		
		if(CommonUtil.isNotNullOrEmpty(isDirekturTerkait) || CommonUtil.isNotNullOrEmpty(isAnggaran)|| CommonUtil.isNotNullOrEmpty(pengedaliIdList)){
			if(CommonUtil.isNotNullOrEmpty(pengedaliIdList)){
				query.setParameter("pengedaliIdList", pengedaliIdList);
			}
		}

		return ((Long) query.getSingleResult()).intValue();
	}

	@Override
	public List<Map<String, Object>> findKartuPengendaliHeader(int rowStart, int rowEnd, String sort, String dir,String dateStart, String dateEnd, Integer pengendaliId, Boolean isPpk, Boolean isUlp,Boolean isPp,Boolean isAnggaran,Boolean isPengendali,Boolean isDirekturTerkait, List<Integer> pengedaliIdList) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new Map ( ");
		buffer.append(" header.noRec as noRec,");
		buffer.append(" header.noUsulan as noUsulan, ");
		buffer.append(" pengendali.namaPengendali as namaPengendali, ");
		buffer.append(" header.tanggal as tanggal, ");
		buffer.append(" header.paketPengadaan as paketPengadaan, ");
		buffer.append(" asalProduk.asalProduk as asalProduk, ");
		buffer.append(" akun.namaMataAnggaran as namaAccount, ");
		buffer.append(" akun.kodeMataAnggaran as kdAccount, ");
		buffer.append(" sum(detail.hargaSatuanBarang*detail.volumeBarang) as total, ");
		buffer.append(" count(header.noUsulan) as totalCount,pengadaan.namaPengadaan as namaPengadaan ) ");
		buffer.append(" from KartuPengendaliDetail detail   ");
		buffer.append(" left join detail.detailRup as detailRup   ");
		buffer.append(" left join detailRup.detailSpekAnggaran spek   ");
		buffer.append(" left join spek.detailAnggaran detailAnggaran ");
		buffer.append(" left join detailAnggaran.asalProduk asalProduk ");
		buffer.append(" left join detail.kartuPengendali header   ");
		buffer.append(" left join spek.produk produk  ");
		buffer.append(" left join spek.pengendali pengendali ");
		buffer.append(" left join detailAnggaran.akun akun, ");
		buffer.append(" DetailPengadaan detailPengadaan  ");
		buffer.append(" left join detailPengadaan.pengadaan pengadaan  ");
		buffer.append(" where detail.isActive = true  ");
		buffer.append(" and detailPengadaan.detailRupId=detailRup.id ");
		
		if(CommonUtil.isNotNullOrEmpty(isUlp)){
			if(isUlp){
				buffer.append("and (detail.isVerifikasi ='VERIFIKASI_ULP' or detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_ULP')  ");
				buffer.append(" and pengadaan.id=1 ");
			}
		}else if(CommonUtil.isNotNullOrEmpty(isPp)){
			if(isPp){
				buffer.append("and (detail.isVerifikasi ='VERIFIKASI_PP' or detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_PP')  ");
				buffer.append(" and pengadaan.id=2 ");
			}
		}else if(CommonUtil.isNotNullOrEmpty(isPpk)){
			if(isPpk){
				buffer.append("and (detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_ANGGARAN' or detail.isVerifikasi ='VERIFIKASI_ANGGARAN' OR detail.isVerifikasi ='VERIFIKASI_ULP' or detail.isVerifikasi ='UNVERIFIKASI_ULP') ");
			}
		}else if(CommonUtil.isNotNullOrEmpty(isAnggaran)){
			if(isAnggaran){
				buffer.append("and (detail.isVerifikasi ='VERIFIKASI_DIREKTUR' or detail.isVerifikasi ='VERIFIKASI_ANGGARAN' or detail.isVerifikasi ='UNVERIFIKASI_ANGGARAN' OR detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_PPK' OR detail.isVerifikasi ='VERIFIKASI_ULP' or detail.isVerifikasi ='UNVERIFIKASI_ULP')  ");
			}
			if(CommonUtil.isNotNullOrEmpty(pengedaliIdList)){
				buffer.append(" and spek.pengendaliId in (:pengedaliIdList) ");
			}
		}else if(CommonUtil.isNotNullOrEmpty(isDirekturTerkait)){
			if(isDirekturTerkait){
				buffer.append(" and (detail.isVerifikasi ='VERIFIKASI_PENGENDALI' or detail.isVerifikasi ='VERIFIKASI_DIREKTUR' or detail.isVerifikasi ='UNVERIFIKASI_DIREKTUR' or detail.isVerifikasi ='VERIFIKASI_ANGGARAN' or detail.isVerifikasi ='UNVERIFIKASI_ANGGARAN' OR detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_PPK' OR detail.isVerifikasi ='VERIFIKASI_ULP' or detail.isVerifikasi ='UNVERIFIKASI_ULP')  ");
			}
			if(CommonUtil.isNotNullOrEmpty(pengedaliIdList)){
				buffer.append(" and spek.pengendaliId in (:pengedaliIdList) ");
			}
		}else if(CommonUtil.isNotNullOrEmpty(isPengendali)){
			if(isPengendali){
				buffer.append("and (detail.isVerifikasi ='VERIFIKASI_PENGENDALI' or detail.isVerifikasi ='VERIFIKASI_DIREKTUR' or detail.isVerifikasi ='UNVERIFIKASI_DIREKTUR'  or detail.isVerifikasi ='VERIFIKASI_ANGGARAN' or detail.isVerifikasi ='UNVERIFIKASI_ANGGARAN' OR detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_PPK' OR detail.isVerifikasi ='VERIFIKASI_ULP' or detail.isVerifikasi ='UNVERIFIKASI_ULP')  ");
			}
			if(CommonUtil.isNotNullOrEmpty(pengedaliIdList)){
				buffer.append(" and spek.pengendaliId in (:pengedaliIdList) ");
			}
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append(" and date(header.tanggal) between :dateStart and :dateEnd ");
		}
			
		buffer.append(" group by header.noUsulan,");
		buffer.append(" pengendali.namaPengendali,");
		buffer.append(" header.tanggal ,");
		buffer.append(" asalProduk.asalProduk,");
		buffer.append(" akun.namaMataAnggaran, ");
		buffer.append(" akun.kodeMataAnggaran , ");
		buffer.append(" header.noRec,pengadaan.namaPengadaan  ");
		
		Query query = em.createQuery(buffer.toString());
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}
		
		
		if(CommonUtil.isNotNullOrEmpty(isDirekturTerkait) || CommonUtil.isNotNullOrEmpty(isAnggaran) || CommonUtil.isNotNullOrEmpty(isPengendali)){
			if(CommonUtil.isNotNullOrEmpty(pengedaliIdList)){
				query.setParameter("pengedaliIdList", pengedaliIdList);
			}
		}

		List<Map<String, Object>> list = query.getResultList();
	
		return list;
	}

	@Override
	public  List<Map<String, Object>> checkStatusKartuKendaliHeader(Boolean isPpk, Boolean isUlp,Boolean isPp,Boolean isAnggaran,Boolean isPengendali,Boolean isDirekturTerkait,String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new Map(detail.isVerifikasi as isVerifikasi) ");
		buffer.append(" from KartuPengendaliDetail detail   ");
		buffer.append(" where detail.isActive = true and detail.kartuPengendaliId=:noRec  ");
		
		if(CommonUtil.isNotNullOrEmpty(isPp)){
			if(isPp){
				buffer.append("and (detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='VERIFIKASI_PP' or detail.isVerifikasi ='UNVERIFIKASI_PP')  ");
			}
		}
		if(CommonUtil.isNotNullOrEmpty(isUlp)){
			if(isUlp){
				buffer.append("and (detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='VERIFIKASI_ULP' or detail.isVerifikasi ='UNVERIFIKASI_ULP')  ");
			}
		}else if(CommonUtil.isNotNullOrEmpty(isPpk)){
			if(isPpk){
				buffer.append("and (detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_ANGGARAN' or detail.isVerifikasi ='VERIFIKASI_ANGGARAN' OR detail.isVerifikasi ='VERIFIKASI_ULP' or detail.isVerifikasi ='UNVERIFIKASI_ULP') ");
			}
		}else if(CommonUtil.isNotNullOrEmpty(isAnggaran)){
			if(isAnggaran){
				buffer.append("and (detail.isVerifikasi ='VERIFIKASI_DIREKTUR' or detail.isVerifikasi ='VERIFIKASI_ANGGARAN' or detail.isVerifikasi ='UNVERIFIKASI_ANGGARAN' OR detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_PPK' OR detail.isVerifikasi ='VERIFIKASI_ULP' or detail.isVerifikasi ='UNVERIFIKASI_ULP')  ");
			}
		}else if(CommonUtil.isNotNullOrEmpty(isDirekturTerkait)){
			if(isDirekturTerkait){
				buffer.append("and (detail.isVerifikasi ='VERIFIKASI_PENGENDALI' or detail.isVerifikasi ='VERIFIKASI_DIREKTUR' or detail.isVerifikasi ='UNVERIFIKASI_DIREKTUR' or detail.isVerifikasi ='VERIFIKASI_ANGGARAN' or detail.isVerifikasi ='UNVERIFIKASI_ANGGARAN' OR detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_PPK' OR detail.isVerifikasi ='VERIFIKASI_ULP' or detail.isVerifikasi ='UNVERIFIKASI_ULP')  ");
			}
		}else if(CommonUtil.isNotNullOrEmpty(isPengendali)){
			if(isPengendali){
				buffer.append("and (detail.isVerifikasi ='VERIFIKASI_PENGENDALI' or detail.isVerifikasi ='VERIFIKASI_DIREKTUR' or detail.isVerifikasi ='UNVERIFIKASI_DIREKTUR'  or detail.isVerifikasi ='VERIFIKASI_ANGGARAN' or detail.isVerifikasi ='UNVERIFIKASI_ANGGARAN' OR detail.isVerifikasi ='VERIFIKASI_PPK' or detail.isVerifikasi ='UNVERIFIKASI_PPK' OR detail.isVerifikasi ='VERIFIKASI_ULP' or detail.isVerifikasi ='UNVERIFIKASI_ULP')  ");
			}
		}
		buffer.append(" group by detail.isVerifikasi ");
		Query query = em.createQuery(buffer.toString());
		if(CommonUtil.isNotNullOrEmpty(noRec)){
			query.setParameter("noRec", noRec);
		}
		List<Map<String, Object>> list = query.getResultList();
		
		return list;
	}

	@Override
	public Map<String, Object> getKartuPengendaliHeaderByNoRec(String noRec) {


		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new Map ( ");
		buffer.append(" header.noRec as noRec,");
		buffer.append(" header.noUsulan as noUsulan, ");
		buffer.append(" pengendali.namaPengendali as namaPengendali, ");
		buffer.append(" header.tanggal as tanggal, ");
		buffer.append(" asalProduk.asalProduk as asalProduk, ");
		buffer.append(" akun.namaMataAnggaran as namaAccount, ");
		buffer.append(" akun.kodeMataAnggaran as kdAccount, ");
		buffer.append(" sum(detail.hargaSatuanBarang*detail.volumeBarang) as total,  ");
		buffer.append(" coalesce(sum(detail.hargaSatuanBarangHps*detail.volumeBarang),0.0) as subTotal, ");
		buffer.append(" coalesce(sum(detail.pembulatanHps*detail.volumeBarang),0.0) as totalPembulatanHps, ");
		buffer.append(" coalesce(sum(detail.hargaSupplier*detail.volumeBarang),0.0) as totalHargaSupplier, ");
	//	buffer.append(" supplier.namaRekanan as namaSupplier, ");
	//	buffer.append(" supplier.id as isSupplier, ");
		buffer.append(" sum(detail.pembulatan*detail.volumeBarang) as totalPembulatan,  ");
		buffer.append(" sum(detail.ppn) as ppn, ");
		buffer.append(" count(header.noUsulan) as totalCount ) " );
		buffer.append(" from KartuPengendaliDetail detail   ");
		buffer.append(" left join detail.detailRup as detailRup   ");
		buffer.append(" left join detailRup.detailSpekAnggaran spek   ");
		buffer.append(" left join spek.detailAnggaran detailAnggaran ");
		buffer.append(" left join detailAnggaran.asalProduk asalProduk ");
		buffer.append(" left join detail.kartuPengendali header   ");
		buffer.append(" left join spek.produk produk  ");
		buffer.append(" left join spek.pengendali pengendali ");
		buffer.append(" left join detailAnggaran.akun akun ");
		buffer.append(" left join detail.supplier supplier ");
		buffer.append(" where detail.isActive = true  ");
		buffer.append(" and header.noRec=:noRec ");
		buffer.append(" group by header.noUsulan,");
		buffer.append(" pengendali.namaPengendali,");
		buffer.append(" header.tanggal ,");
		buffer.append(" asalProduk.asalProduk,");
		buffer.append(" akun.namaMataAnggaran, ");
		buffer.append(" akun.kodeMataAnggaran , ");
		buffer.append(" header.noRec  ");
		//buffer.append(" ,supplier.namaRekanan,supplier.id");
		
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);
		

		Map<String, Object> list = (Map<String, Object>) query.getSingleResult();
		
		return list;
	
	}

	@Override
	public List<Map<String, Object>> getKartuPengendaliDetailByParent(String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map( ");
		buffer.append(" detail.noRec as noRec, ");
		buffer.append(" produk.namaProduk as namaProduk, ");
		buffer.append(" detail.noUsulan as noUsulanPermintaan, ");
		buffer.append(" header.tanggal as tanggal, ");
		buffer.append(" pengendali.namaPengendali as pengendali, ");
		buffer.append(" detail.keterangan as keterangan, ");
		buffer.append(" detail.totalHargaBarang as total, ");
		buffer.append(" detail.volumeBarang as volume, ");
		buffer.append(" satuan.satuanStandar as satuan, ");
		buffer.append(" detail.hargaSatuanBarang as hargaSatuan, ");
		buffer.append(" detail.hargaSatuanBarangHps as hargaHps, ");
		buffer.append(" pengendali.namaPengendali as namaPengendali, ");
		buffer.append(" (detail.hargaSatuanBarangHps*detail.volumeBarang) as subTotal, ");
		buffer.append(" (detail.hargaSatuanBarang*detail.volumeBarang) as totalHargaBarang, ");
		buffer.append(" detail.isVerifikasi as isVerifikasi, ");
		buffer.append(" coalesce(detail.hargaSupplier,0.0) as hargaSupplier, ");
		buffer.append(" coalesce(supplier.namaRekanan,'-') as namaSupplier ");
		buffer.append(" ) ");
		buffer.append(" from KartuPengendaliDetail detail ");
		buffer.append(" left join detail.detailRup as detailRup ");
		buffer.append(" left join detailRup.detailSpekAnggaran spek ");
		buffer.append(" left join detail.kartuPengendali header ");
		buffer.append(" left join spek.produk produk ");
		buffer.append(" left join produk.satuanStandar satuan ");
		buffer.append(" left join spek.pengendali pengendali ");
		buffer.append(" left join detail.supplier supplier ");
		buffer.append(" where detail.isActive = true and header.noRec=:noRec  ");

	
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);

	
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public int preAddPenerimaanCount(String sort, String dateStart, String dateEnd, Integer supplierId,KelompokUser kelompokUser) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(" select ");
		buffer.append(" count(distinct strukRekapPemenang.noRec) ");
		buffer.append(" from KartuPengendaliDetail detail  ");  
		buffer.append(" left join detail.detailRup as detailRup   ");
		buffer.append(" left join detailRup.detailSpekAnggaran spek   ");
		buffer.append(" left join spek.detailAnggaran detailAnggaran ");
		buffer.append(" left join detailAnggaran.asalProduk asalProduk ");
		buffer.append(" left join detail.kartuPengendali header   ");
		buffer.append(" left join spek.produk produk  ");
		buffer.append(" left join spek.pengendali pengendali ");
		buffer.append(" left join detailAnggaran.akun akun ");
		buffer.append(" left join detailAnggaran.asalProduk asalProduk ");
		buffer.append(" left join produk.satuanStandar satuanStandar ");
		buffer.append(" left join detail.strukRekapPemenang strukRekapPemenang ");
		buffer.append(" left join strukRekapPemenang.supplier supplier ");
		buffer.append(" left join produk.detailJenisProduk detailJenisProduk ");
		buffer.append(" left join detailJenisProduk.jenisProduk jenisProduk ");
		buffer.append(" left join jenisProduk.kelompokProduk kelompokProduk ");
		buffer.append(" left join jenisProduk.kelompokProduk kelompokProduk ");
		buffer.append(" where detail.isActive = true   ");
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			buffer.append(" and strukRekapPemenang.supplierId=:supplierId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append(" and date(strukRekapPemenang.tanggal) between :dateStart and :dateEnd ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(kelompokUser)){
			if(kelompokUser.getKelompokUser().equalsIgnoreCase("bagianUmum")){
				buffer.append(" and kelompokProduk.id in (20) ");
			}else if(kelompokUser.getKelompokUser().equalsIgnoreCase("logistik")){
				buffer.append(" and kelompokProduk.id in (24,5) ");
			}
		}
			
		Query query = em.createQuery(buffer.toString());
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			query.setParameter("supplierId", supplierId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}
		

		return ((Long) query.getSingleResult()).intValue();
	
	}

	@Override
	public List<Map<String, Object>> preAddPenerimaanGrid(int rowStart, int rowEnd, String sort, String dir,String dateStart, String dateEnd, Integer supplierId,KelompokUser kelompokUser) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new Map( ");
		buffer.append(" strukRekapPemenang.noRec as noRec, ");
		buffer.append(" strukRekapPemenang.noRekap as noRekap, ");
		buffer.append(" count(detail.noRec) as totalCount, ");
		buffer.append(" sum(detail.volumeBarang*detail.hargaSupplier) as totalHargaBarangSupplier, ");
		buffer.append(" supplier.namaRekanan as namaSupplier, ");
		buffer.append(" str(date(strukRekapPemenang.tanggal)) as tanggal ) ");
		buffer.append(" from KartuPengendaliDetail detail  ");  
		buffer.append(" left join detail.detailRup as detailRup   ");
		buffer.append(" left join detailRup.detailSpekAnggaran spek   ");
		buffer.append(" left join spek.detailAnggaran detailAnggaran ");
		buffer.append(" left join detailAnggaran.asalProduk asalProduk ");
		buffer.append(" left join detail.kartuPengendali header   ");
		buffer.append(" left join spek.produk produk  ");
		buffer.append(" left join spek.pengendali pengendali ");
		buffer.append(" left join detailAnggaran.akun akun ");
		buffer.append(" left join detailAnggaran.asalProduk asalProduk ");
		buffer.append(" left join produk.satuanStandar satuanStandar ");
		buffer.append(" left join detail.strukRekapPemenang strukRekapPemenang ");
		buffer.append(" left join strukRekapPemenang.supplier supplier ");
		buffer.append(" left join produk.detailJenisProduk detailJenisProduk ");
		buffer.append(" left join detailJenisProduk.jenisProduk jenisProduk ");
		buffer.append(" left join jenisProduk.kelompokProduk kelompokProduk ");
		buffer.append(" where detail.isActive = true and strukRekapPemenang.noRec is not null  ");
		
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			buffer.append(" and strukRekapPemenang.supplierId=:supplierId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append(" and date(strukRekapPemenang.tanggal) between :dateStart and :dateEnd ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(kelompokUser)){
			if(kelompokUser.getKelompokUser().equalsIgnoreCase("bagianUmum")){
				buffer.append(" and kelompokProduk.id in (20) ");
			}else if(kelompokUser.getKelompokUser().equalsIgnoreCase("logistik")){
				buffer.append(" and kelompokProduk.id in (24,5) ");
			}
		}
		
		buffer.append(" group by strukRekapPemenang.noRekap, ");
		buffer.append(" supplier.namaRekanan,strukRekapPemenang.noRec,  ");
		buffer.append(" str(date(strukRekapPemenang.tanggal))");
		
		
		
		Query query = em.createQuery(buffer.toString());
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			query.setParameter("supplierId", supplierId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}

		List<Map<String, Object>> list = query.getResultList();
	
		return list;
	}

	
	@Override
	public int listKontrakCount(String sort, String dateStart, String dateEnd, Integer supplierId,KelompokUser kelompokUser,Integer tahun) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(" select  ");
		buffer.append(" count(distinct rekap.noRekap) ");
		buffer.append(" from KartuPengendaliDetail detail ");
		buffer.append(" left join detail.kartuPengendali header ");
		buffer.append(" left join detail.supplier supplier ");
		buffer.append(" left join detail.strukRekapPemenang rekap ");
		buffer.append(" left join detail.detailRup rup "); 
		buffer.append(" left join rup.detailSpekAnggaran spek ");
		buffer.append(" left join spek.detailAnggaran detailAnggaran ");
		buffer.append(" left join detailAnggaran.kegiatanAnggaran kegiatanAnggaran ");
		buffer.append(" left join kegiatanAnggaran.kegiatanDetail kegiatanDetail ");
		buffer.append(" left join kegiatanAnggaran.headAnggaran as headAnggaran, ");
		buffer.append(" DetailPengadaan detailPengadaan  ");
		buffer.append(" left join detailPengadaan.pengadaan pengadaan  ");
		buffer.append(" where detail.isActive = true  ");
		buffer.append(" and detailPengadaan.detailRupId=rup.id ");
		buffer.append(" and rekap.noRec is not null ");
   
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			buffer.append(" and rekap.supplierId=:supplierId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append(" and date(rekap.tanggal) between :dateStart and :dateEnd ");
		}
		
			
		Query query = em.createQuery(buffer.toString());
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			query.setParameter("supplierId", supplierId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}
		

		return ((Long) query.getSingleResult()).intValue();
	
	}

	@Override
	public List<Map<String, Object>> listKontrakGrid(int rowStart, int rowEnd, String sort, String dir,String dateStart, String dateEnd, Integer supplierId,KelompokUser kelompokUser,Integer tahun) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new Map ( ");
		buffer.append(" rekap.noRekap as noRekap, ");
		buffer.append(" supplier.namaRekanan as namaRekanan, "); 
		buffer.append(" str(date(rekap.tanggal)) as tanggalKontrak, ");
		buffer.append(" sum(detail.hargaSupplier*detail.volumeBarang) as nilaiKontrak, ");
		buffer.append(" supplier.alamatLengkap as alamatLengkapSupplier , ");
		buffer.append(" kegiatanDetail.namaKegiatanDetail as kegiatanDetail, ");
		buffer.append(" pengadaan.namaPengadaan as namaPengadaan, ");
		buffer.append(" headAnggaran.tahun as tahun ) ");
		buffer.append(" from KartuPengendaliDetail detail ");
		buffer.append(" left join detail.kartuPengendali header ");
		buffer.append(" left join detail.supplier supplier ");
		buffer.append(" left join detail.strukRekapPemenang rekap ");
		buffer.append(" left join detail.detailRup rup ");
		buffer.append(" left join rup.detailSpekAnggaran spek ");
		buffer.append(" left join spek.detailAnggaran detailAnggaran ");
		buffer.append(" left join detailAnggaran.kegiatanAnggaran kegiatanAnggaran ");
		buffer.append(" left join kegiatanAnggaran.kegiatanDetail kegiatanDetail ");
		buffer.append(" left join kegiatanAnggaran.headAnggaran as headAnggaran, ");
		buffer.append(" DetailPengadaan detailPengadaan  ");
		buffer.append(" left join detailPengadaan.pengadaan pengadaan  ");
		buffer.append(" where detail.isActive = true  ");
		buffer.append(" and detailPengadaan.detailRupId=rup.id ");
		buffer.append(" and rekap.noRec is not null ");
	
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			buffer.append(" and rekap.supplierId=:supplierId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append(" and date(rekap.tanggal) between :dateStart and :dateEnd ");
		}
		
		
		buffer.append(" group by rekap.noRekap,supplier.namaRekanan , ");
		buffer.append(" str(date(rekap.tanggal)), ");
		buffer.append(" supplier.alamatLengkap, ");
		buffer.append(" kegiatanDetail.namaKegiatanDetail,");
		buffer.append(" pengadaan.namaPengadaan, ");
		buffer.append(" headAnggaran.tahun ");
		
		
		
		Query query = em.createQuery(buffer.toString());
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			query.setParameter("supplierId", supplierId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}

		List<Map<String, Object>> list = query.getResultList();
	
		return list;
	}

	@Override
	public int preAddSppbCount(String sort, String dateStart, String dateEnd, Integer supplierId) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(" select ");
		buffer.append(" count(distinct strukRekapPemenang.noRec) ");
		buffer.append(" from KartuPengendaliDetail detail  ");  
		buffer.append(" left join detail.detailRup as detailRup   ");
		buffer.append(" left join detailRup.detailSpekAnggaran spek   ");
		buffer.append(" left join spek.detailAnggaran detailAnggaran ");
		buffer.append(" left join detailAnggaran.asalProduk asalProduk ");
		buffer.append(" left join detail.kartuPengendali header   ");
		buffer.append(" left join spek.produk produk  ");
		buffer.append(" left join spek.pengendali pengendali ");
		buffer.append(" left join detailAnggaran.akun akun ");
		buffer.append(" left join detailAnggaran.asalProduk asalProduk ");
		buffer.append(" left join produk.satuanStandar satuanStandar ");
		buffer.append(" left join detail.strukRekapPemenang strukRekapPemenang ");
		buffer.append(" left join strukRekapPemenang.supplier supplier ");
		buffer.append(" left join produk.detailJenisProduk detailJenisProduk ");
		buffer.append(" left join detailJenisProduk.jenisProduk jenisProduk ");
		buffer.append(" left join jenisProduk.kelompokProduk kelompokProduk ");
		buffer.append(" left join jenisProduk.kelompokProduk kelompokProduk ");
		buffer.append(" where detail.isActive = true   ");
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			buffer.append(" and strukRekapPemenang.supplierId=:supplierId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append(" and date(strukRekapPemenang.tanggal) between :dateStart and :dateEnd ");
		}
		
			
		Query query = em.createQuery(buffer.toString());
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			query.setParameter("supplierId", supplierId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}
		

		return ((Long) query.getSingleResult()).intValue();
	
	}

	@Override
	public List<Map<String, Object>> preAddSppbGrid(String dateStart, String dateEnd, Integer supplierId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new Map( ");
		buffer.append(" strukRekapPemenang.noRec as noRec, ");
		buffer.append(" strukRekapPemenang.noRekap as noRekap, ");
		buffer.append(" count(detail.noRec) as totalCount, ");
		buffer.append(" sum(detail.volumeBarang*detail.hargaSupplier) as totalHargaBarangSupplier, ");
		buffer.append(" supplier.namaRekanan as namaSupplier, ");
		buffer.append(" str(date(strukRekapPemenang.tanggal)) as tanggal ) ");
		buffer.append(" from KartuPengendaliDetail detail  ");  
		buffer.append(" left join detail.detailRup as detailRup   ");
		buffer.append(" left join detailRup.detailSpekAnggaran spek   ");
		buffer.append(" left join spek.detailAnggaran detailAnggaran ");
		buffer.append(" left join detailAnggaran.asalProduk asalProduk ");
		buffer.append(" left join detail.kartuPengendali header   ");
		buffer.append(" left join spek.produk produk  ");
		buffer.append(" left join spek.pengendali pengendali ");
		buffer.append(" left join detailAnggaran.akun akun ");
		buffer.append(" left join detailAnggaran.asalProduk asalProduk ");
		buffer.append(" left join produk.satuanStandar satuanStandar ");
		buffer.append(" left join detail.strukRekapPemenang strukRekapPemenang ");
		buffer.append(" left join strukRekapPemenang.supplier supplier ");
		buffer.append(" left join produk.detailJenisProduk detailJenisProduk ");
		buffer.append(" left join detailJenisProduk.jenisProduk jenisProduk ");
		buffer.append(" left join jenisProduk.kelompokProduk kelompokProduk ");
		buffer.append(" where detail.isActive = true and strukRekapPemenang.noRec is not null  ");
		
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			buffer.append(" and strukRekapPemenang.supplierId=:supplierId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append(" and date(strukRekapPemenang.tanggal) between :dateStart and :dateEnd ");
		}
		
		
		buffer.append(" group by strukRekapPemenang.noRekap, ");
		buffer.append(" supplier.namaRekanan,strukRekapPemenang.noRec,  ");
		buffer.append(" str(date(strukRekapPemenang.tanggal))");
		
		
		
		Query query = em.createQuery(buffer.toString());
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			query.setParameter("supplierId", supplierId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}

		List<Map<String, Object>> list = query.getResultList();
	
		return list;
	}
	
	
	@Override
	public Boolean CheckSppbTerhadapKontrak(String noRec,Integer kelompokTransaksiSppb) {
		/*StringBuffer buffer = new StringBuffer();
		buffer.append(" select ");
		buffer.append(" coalesce((select sum(kpd.volumeBarang) from KartuPengendaliDetail kpd left join kpd.strukRekapPemenang srp where srp.noRec=:noRec)-sum(detail.qtyproduk),1.0) as sisa ");
		buffer.append(" from StrukPelayananDetail detail ");
		buffer.append(" left join detail.nostruk header ");
		buffer.append(" left join detail.kartuPengendaliDetail kartuPengendaliDetail ");
		buffer.append(" left join kartuPengendaliDetail.strukRekapPemenang rekap ");
		buffer.append(" left join rekap.supplier supplier ");
		buffer.append(" where rekap.noRec=:noRec and header.kdkelompoktransaksi.id in (Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='kdKelompokTransaksiPphp')");*/
		
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select ");
		buffer.append(" coalesce((select sum(kpd.volumeBarang) from KartuPengendaliDetail kpd left join kpd.strukRekapPemenang srp where srp.noRec=:noRec)-sum(detail.qtyproduk),1.0) as sisa ");
		buffer.append(" from StrukPelayananDetail detail ");
		buffer.append(" left join detail.nostruk header ");
		buffer.append(" left join detail.kartuPengendaliDetail kartuPengendaliDetail ");
		buffer.append(" left join kartuPengendaliDetail.strukRekapPemenang rekap ");
		buffer.append(" left join rekap.supplier supplier ");
		buffer.append(" where rekap.noRec=:noRec and header.kdkelompoktransaksi.id=:kelompokTransaksiSppb )");
		
		Query query = em.createQuery(buffer.toString());
		
		query.setParameter("noRec", noRec);
		
		query.setParameter("kelompokTransaksiSppb", kelompokTransaksiSppb);
		
		

		Double result = (Double) query.getSingleResult();
		if(result>=1){
			return false;
		}else{
			return true;
		}
	}

	@Override
	public int sppbCount(String sort, String dateStart, String dateEnd, Integer supplierId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select count(distinct detail.noRec) ");
		buffer.append(" from StrukPelayananDetail detail ");
		buffer.append(" left join detail.nostruk header ");
		buffer.append(" left join detail.kartuPengendaliDetail kartuPengendaliDetail ");
		buffer.append(" left join kartuPengendaliDetail.detailRup rup ");
		buffer.append(" left join rup.detailSpekAnggaran spek ");
		buffer.append(" left join spek.produk produk ");
		buffer.append(" left join kartuPengendaliDetail.strukRekapPemenang strukRekap ");
		buffer.append(" left join kartuPengendaliDetail.kartuPengendali kartuPengendaliHeader ");
		buffer.append(" left join strukRekap.supplier supp ");
		buffer.append(" where detail.kartuPengendaliDetail is not null ");
		buffer.append(" and header.kdkelompoktransaksi.id in (Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='kdKelompokTransaksiSppb') ");
	
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			buffer.append(" and strukRekap.supplierId=:supplierId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append(" and date(header.tglstruk) between :dateStart and :dateEnd ");
		}
			
		Query query = em.createQuery(buffer.toString());
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			query.setParameter("supplierId", supplierId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}

		return ((Long) query.getSingleResult()).intValue();
		
		
	}

	@Override
	public List<Map<String, Object>> sppbList(int rowStart, int rowEnd, String dateStart, String dateEnd,Integer supplierId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map ( ");
		buffer.append(" strukRekap.noRekap as noKontrak, ");
		buffer.append(" header.nostruk as noSppb, ");
		buffer.append(" header.noRec as noRec, ");
		buffer.append(" coalesce(header.verifikasi,'SPPB_BARU') as verifikasi, ");
		buffer.append(" supp.namaRekanan as namaRekanan, ");
		buffer.append(" str(date(header.tglstruk)) as tanggalSppb, ");
		buffer.append(" sum(detail.hargasatuan*detail.qtyproduk) as totalSppb, ");
		buffer.append(" str(date(kartuPengendaliHeader.tanggal)) as tanggalKartuKendali ) ");
		buffer.append(" from StrukPelayananDetail detail ");
		buffer.append(" left join detail.nostruk header ");
		buffer.append(" left join detail.kartuPengendaliDetail kartuPengendaliDetail ");
		buffer.append(" left join kartuPengendaliDetail.detailRup rup ");
		buffer.append(" left join rup.detailSpekAnggaran spek ");
		buffer.append(" left join spek.produk produk ");
		buffer.append(" left join kartuPengendaliDetail.strukRekapPemenang strukRekap ");
		buffer.append(" left join kartuPengendaliDetail.kartuPengendali kartuPengendaliHeader ");
		buffer.append(" left join strukRekap.supplier supp ");
		buffer.append(" where detail.kartuPengendaliDetail is not null ");
		buffer.append(" and header.kdkelompoktransaksi.id in (Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='kdKelompokTransaksiSppb') ");
	
		
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			buffer.append(" and strukRekap.supplierId=:supplierId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append(" and date(header.tglstruk) between :dateStart and :dateEnd ");
		}
		
		
		buffer.append("group by ");
		buffer.append("strukRekap.noRekap,");
		buffer.append("header.nostruk,");
		buffer.append("header.verifikasi,");
		buffer.append("supp.namaRekanan,");
		buffer.append("header.tglstruk,header.noRec,");
		buffer.append("kartuPengendaliHeader.tanggal  ");
		
		
		
		Query query = em.createQuery(buffer.toString());
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			query.setParameter("supplierId", supplierId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}
		
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);

		List<Map<String, Object>> list = query.getResultList();
	
		return list;
	}

	@Override
	public List<Map<String, Object>> preAddPphp(String dateStart, String dateEnd, Integer supplierId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map ( ");
		buffer.append(" strukRekap.noRekap as noKontrak, ");
		buffer.append(" header.nostruk as noSppb, ");
		buffer.append(" header.noRec as noRec, ");
		buffer.append(" coalesce(header.verifikasi,'SPPB_BARU') as verifikasi, ");
		buffer.append(" supp.namaRekanan as namaRekanan, ");
		buffer.append(" str(date(header.tglstruk)) as tanggalSppb, ");
		buffer.append(" sum(detail.hargasatuan*detail.qtyproduk) as totalSppb, ");
		buffer.append(" str(date(kartuPengendaliHeader.tanggal)) as tanggalKartuKendali ) ");
		buffer.append(" from StrukPelayananDetail detail ");
		buffer.append(" left join detail.nostruk header ");
		buffer.append(" left join detail.kartuPengendaliDetail kartuPengendaliDetail ");
		buffer.append(" left join kartuPengendaliDetail.detailRup rup ");
		buffer.append(" left join rup.detailSpekAnggaran spek ");
		buffer.append(" left join spek.produk produk ");
		buffer.append(" left join kartuPengendaliDetail.strukRekapPemenang strukRekap ");
		buffer.append(" left join kartuPengendaliDetail.kartuPengendali kartuPengendaliHeader ");
		buffer.append(" left join strukRekap.supplier supp ");
		buffer.append(" where detail.kartuPengendaliDetail is not null ");
		buffer.append(" and header.kdkelompoktransaksi.id in (Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='kdKelompokTransaksiSppb') and header.verifikasi='VERIFIKASI_PPK' ");
	
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			buffer.append(" and strukRekap.supplierId=:supplierId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append(" and date(header.tglstruk) between :dateStart and :dateEnd ");
		}
		
		
		buffer.append("group by ");
		buffer.append("strukRekap.noRekap,");
		buffer.append("header.nostruk,");
		buffer.append("header.verifikasi,");
		buffer.append("supp.namaRekanan,");
		buffer.append("header.tglstruk,header.noRec,");
		buffer.append("kartuPengendaliHeader.tanggal  ");
		
		
		
		Query query = em.createQuery(buffer.toString());
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			query.setParameter("supplierId", supplierId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}
		

		List<Map<String, Object>> list = query.getResultList();
	
		return list;
	}
	
	@Override
	public int pphpCount(String sort, String dateStart, String dateEnd, Integer supplierId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select count(distinct detail.noRec) ");
		buffer.append(" from StrukPelayananDetail detail ");
		buffer.append(" left join detail.nostruk header ");
		buffer.append(" left join detail.kartuPengendaliDetail kartuPengendaliDetail ");
		buffer.append(" left join kartuPengendaliDetail.detailRup rup ");
		buffer.append(" left join rup.detailSpekAnggaran spek ");
		buffer.append(" left join spek.produk produk ");
		buffer.append(" left join kartuPengendaliDetail.strukRekapPemenang strukRekap ");
		buffer.append(" left join kartuPengendaliDetail.kartuPengendali kartuPengendaliHeader ");
		buffer.append(" left join strukRekap.supplier supp ");
		buffer.append(" where detail.kartuPengendaliDetail is not null ");
		buffer.append(" and header.kdkelompoktransaksi.id in (Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='kdKelompokTransaksiPphp') ");
	
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			buffer.append(" and strukRekap.supplierId=:supplierId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append(" and date(header.tglstruk) between :dateStart and :dateEnd ");
		}
			
		Query query = em.createQuery(buffer.toString());
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			query.setParameter("supplierId", supplierId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}

		return ((Long) query.getSingleResult()).intValue();
		
		
	}

	@Override
	public List<Map<String, Object>> pphpList(int rowStart, int rowEnd, String dateStart, String dateEnd,Integer supplierId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map ( ");
		buffer.append(" strukRekap.noRekap as noKontrak, ");
		buffer.append(" header.nostruk as noPphp, ");
		buffer.append(" header.noRec as noRec, ");
		buffer.append(" supp.namaRekanan as namaRekanan, ");
		buffer.append(" str(date(header.tglstruk)) as tanggalSppb, ");
		buffer.append(" sum(detail.hargasatuan*detail.qtyproduk) as totalSppb, ");
		buffer.append(" str(date(kartuPengendaliHeader.tanggal)) as tanggalKartuKendali ) ");
		buffer.append(" from StrukPelayananDetail detail ");
		buffer.append(" left join detail.nostruk header ");
		buffer.append(" left join detail.kartuPengendaliDetail kartuPengendaliDetail ");
		buffer.append(" left join kartuPengendaliDetail.detailRup rup ");
		buffer.append(" left join rup.detailSpekAnggaran spek ");
		buffer.append(" left join spek.produk produk ");
		buffer.append(" left join kartuPengendaliDetail.strukRekapPemenang strukRekap ");
		buffer.append(" left join kartuPengendaliDetail.kartuPengendali kartuPengendaliHeader ");
		buffer.append(" left join strukRekap.supplier supp ");
		buffer.append(" where detail.kartuPengendaliDetail is not null ");
		buffer.append(" and header.kdkelompoktransaksi.id in (Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='kdKelompokTransaksiPphp') ");
	
		
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			buffer.append(" and strukRekap.supplierId=:supplierId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append(" and date(header.tglstruk) between :dateStart and :dateEnd ");
		}
		
		
		buffer.append("group by ");
		buffer.append("strukRekap.noRekap,");
		buffer.append("header.nostruk,");
		buffer.append("header.verifikasi,");
		buffer.append("supp.namaRekanan,");
		buffer.append("header.tglstruk,header.noRec,");
		buffer.append("kartuPengendaliHeader.tanggal  ");
		
		
		
		Query query = em.createQuery(buffer.toString());
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			query.setParameter("supplierId", supplierId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}
		
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);

		List<Map<String, Object>> list = query.getResultList();
	
		return list;
	}

	


	@Override
	public List<Map<String, Object>> preAddPphpList(String dateStart, String dateEnd,Integer supplierId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map ( ");
		buffer.append(" strukRekap.noRekap as noRekap, ");
		buffer.append(" header.nostruk as noPphp, ");
		buffer.append(" header.noRec as noRec, ");
		buffer.append(" supp.namaRekanan as namaSupplier, ");
		buffer.append(" str(date(header.tglstruk)) as tanggal,count(detail.noRec) as totalCount,  ");
		buffer.append(" sum(detail.hargasatuan*detail.hasilKonversi) as totalHargaBarangSupplier, ");
		buffer.append(" str(date(kartuPengendaliHeader.tanggal)) as tanggalKartuKendali ) ");
		buffer.append(" from StrukPelayananDetail detail ");
		buffer.append(" left join detail.nostruk header ");
		buffer.append(" left join detail.kartuPengendaliDetail kartuPengendaliDetail ");
		buffer.append(" left join kartuPengendaliDetail.detailRup rup ");
		buffer.append(" left join rup.detailSpekAnggaran spek ");
		buffer.append(" left join spek.produk produk ");
		buffer.append(" left join kartuPengendaliDetail.strukRekapPemenang strukRekap ");
		buffer.append(" left join kartuPengendaliDetail.kartuPengendali kartuPengendaliHeader ");
		buffer.append(" left join strukRekap.supplier supp ");
		buffer.append(" where detail.kartuPengendaliDetail is not null ");
		buffer.append(" and header.kdkelompoktransaksi.id in (Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='kdKelompokTransaksiPphp') ");
	
		
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			buffer.append(" and strukRekap.supplierId=:supplierId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append(" and date(header.tglstruk) between :dateStart and :dateEnd ");
		}
		
		
		buffer.append("group by ");
		buffer.append("strukRekap.noRekap,");
		buffer.append("header.nostruk,");
		buffer.append("header.verifikasi,");
		buffer.append("supp.namaRekanan,");
		buffer.append("header.tglstruk,header.noRec,");
		buffer.append("kartuPengendaliHeader.tanggal  ");
		
		
		
		Query query = em.createQuery(buffer.toString());
		
		if(CommonUtil.isNotNullOrEmpty(supplierId)){
			query.setParameter("supplierId", supplierId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}
		

		List<Map<String, Object>> list = query.getResultList();
	
		return list;
	}
	
	@Override
	public Map<String, Object> getHpsHeaderByNoRecReport(String noRec) {


		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new Map (  year(header.tanggal) as tahun,'Harga Perkiraan Sendiri' as title,");
		buffer.append(" coalesce(sum(detail.hargaSupplier*detail.volumeBarang),0.0) as totalHargaSupplier, ");
		buffer.append(" sum(detail.ppn) as totalPpn,coalesce(header.paketPengadaan,'-') as paketPengadaan ) ");
		buffer.append(" from KartuPengendaliDetail detail   ");
		buffer.append(" left join detail.detailRup as detailRup   ");
		buffer.append(" left join detailRup.detailSpekAnggaran spek   ");
		buffer.append(" left join spek.detailAnggaran detailAnggaran ");
		buffer.append(" left join detailAnggaran.asalProduk asalProduk ");
		buffer.append(" left join detail.kartuPengendali header   ");
		buffer.append(" left join spek.produk produk  ");
		buffer.append(" left join produk.satuanStandar satuan  ");
		buffer.append(" left join spek.pengendali pengendali ");
		buffer.append(" left join detailAnggaran.akun akun ");
		buffer.append(" left join detail.supplier supplier ");
		buffer.append(" where detail.isActive = true  and header.noRec=:noRec ");
		buffer.append(" group by header.noRec,");
		buffer.append(" header.paketPengadaan");
		
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);
		

		Map<String, Object> list = (Map<String, Object>) query.getSingleResult();
		
		return list;
	
	}

	@Override
	public List<Map<String, Object>> getHpsDetailByParentReport(String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map( ");
		buffer.append(" produk.namaProduk as namaProduk, ");
		buffer.append(" spek.spesifikasi as spesifikasi, ");
		buffer.append(" satuan.satuanStandar as satuan, ");
		buffer.append(" (detail.hargaSatuanBarangHps*detail.volumeBarang) as totalHps, ");
		buffer.append(" detail.volumeBarang as volume, ");
		buffer.append(" coalesce(detail.hargaSatuanBarangHps,0.0) as hargaHps ");
		buffer.append(" ) ");
		buffer.append(" from KartuPengendaliDetail detail ");
		buffer.append(" left join detail.detailRup as detailRup ");
		buffer.append(" left join detailRup.detailSpekAnggaran spek ");
		buffer.append(" left join detail.kartuPengendali header ");
		buffer.append(" left join spek.produk produk ");
		buffer.append(" left join produk.satuanStandar satuan ");
		buffer.append(" left join spek.pengendali pengendali ");
		buffer.append(" left join detail.supplier supplier ");
		buffer.append(" where detail.isActive = true and header.noRec=:noRec  ");

	
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);

	
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public List<Map<String, Object>> getKartuPengendaliDetailByParentReport(String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map( ");
		buffer.append(" produk.namaProduk as namaProduk, ");
		buffer.append(" produk.kdProduk as kdProduk, ");
		buffer.append(" spek.spesifikasi as spesifikasi, ");
		buffer.append(" satuan.satuanStandar as satuan, ");
		buffer.append(" (detail.hargaSatuanBarangHps*detail.volumeBarang) as subTotal, ");
		buffer.append(" detail.volumeBarang as volume, ");
		buffer.append(" coalesce(detail.hargaSatuanBarangHps,0.0) as harga ");
		buffer.append(" ) ");
		buffer.append(" from KartuPengendaliDetail detail ");
		buffer.append(" left join detail.detailRup as detailRup ");
		buffer.append(" left join detailRup.detailSpekAnggaran spek ");
		buffer.append(" left join detail.kartuPengendali header ");
		buffer.append(" left join spek.produk produk ");
		buffer.append(" left join produk.satuanStandar satuan ");
		buffer.append(" left join spek.pengendali pengendali ");
		buffer.append(" left join detail.supplier supplier ");
		buffer.append(" where detail.isActive = true and header.noRec=:noRec  ");

		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);

	
		List<Map<String, Object>> list = query.getResultList();

		return list;
	
		
	}

	@Override
	public Map<String, Object> getKartuPengendaliHeaderByNoRecReport(String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new Map ( ");
		buffer.append(" header.noUsulan as noUsulan, ");
		buffer.append(" header.tanggalDibutuhkan as tanggalDibutuhkan,");
		buffer.append(" header.tanggal as tanggalUsulan,");
		buffer.append(" header.paketPengadaan as paketPengadaan, ");
		buffer.append(" pengendali.namaPengendali as namaPengendali, ");
		buffer.append(" asalProduk.asalProduk as asalProduk, ");
		buffer.append(" akun.namaMataAnggaran as namaAccount, ");
		buffer.append(" akun.kodeMataAnggaran as kdAccount, ");
		buffer.append(" coalesce(sum(detail.hargaSatuanBarangHps*detail.volumeBarang),0.0) as subTotal, ");
		buffer.append(" sum(detail.ppn) as ppn,");
		buffer.append(" coalesce(sum(detail.hargaSatuanBarangHps*detail.volumeBarang),0.0)+sum(detail.ppn) as total ) ");
		buffer.append(" from KartuPengendaliDetail detail   ");
		buffer.append(" left join detail.detailRup as detailRup   ");
		buffer.append(" left join detailRup.detailSpekAnggaran spek   ");
		buffer.append(" left join spek.detailAnggaran detailAnggaran ");
		buffer.append(" left join detailAnggaran.asalProduk asalProduk ");
		buffer.append(" left join detail.kartuPengendali header   ");
		buffer.append(" left join spek.produk produk  ");
		buffer.append(" left join spek.pengendali pengendali ");
		buffer.append(" left join detailAnggaran.akun akun ");
		buffer.append(" left join detail.supplier supplier ");
		buffer.append(" where detail.isActive = true  ");
		buffer.append(" and header.noRec=:noRec ");
		buffer.append(" group by header.noUsulan,");
		buffer.append(" pengendali.namaPengendali,");
		buffer.append(" header.paketPengadaan,");
		buffer.append(" header.tanggal ,");
		buffer.append(" asalProduk.asalProduk,");
		buffer.append(" akun.namaMataAnggaran, ");
		buffer.append(" akun.kodeMataAnggaran,header.tanggalDibutuhkan ");
		
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);
		
		Map<String, Object> map = (Map<String, Object>) query.getSingleResult();

		return map;
	}
}
