package com.jasamedika.medifirst2000.dao;

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

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.entities.KartuPengendaliDetail;
import com.jasamedika.medifirst2000.entities.Rekanan;

@Repository("KartuPengendaliDetailDao")
public interface KartuPengendaliDetailDao extends PagingAndSortingRepository<KartuPengendaliDetail, String>{

	KartuPengendaliDetail findByNoRec(String noRec);
	
	@Query(" select new Map( kpd.noRec as noRec,"+
	" produk.namaProduk as namaProduk,"+
	" produk.id as idproduk )"+
	" from KartuPengendaliDetail kpd"+
	" left join kpd.detailRup detailRup"+
	" left join detailRup.detailSpekAnggaran spek"+
	" left join spek.produk produk where kpd.noRec=:noRec")
	public Map<String,Object> findByNoRecMap(@Param("noRec") String noRec);
	
	@Query("select model from KartuPengendaliDetail model where model.noRec=:noRec ")
	public KartuPengendaliDetail findById(@Param("noRec") String noRec);

	@Query("select new map(detail.noRec as noRecs,detail.hargaSatuanBarang as hargaSatuanBarang,detail.volumeBarang as volumeBarang,produk.namaProduk as namaProduk,detail.volumeBarang*detail.hargaSatuanBarang as totalHargaBarang) from KartuPengendaliDetail detail left join detail.detailRup rup left join rup.detailSpekAnggaran spek left join spek.produk produk where detail.noRec in (:noRec)")
	List<Map<String, Object>> kartuPengendliDetailList(@Param("noRec") List<String> noRec);

	@Query("select model from KartuPengendaliDetail model where model.kartuPengendaliId=:noRec ")
	List<KartuPengendaliDetail> findByParent(@Param("noRec") String noRec);

	@Query("select su from KartuPengendaliDetail p left join p.supplier su where p.kartuPengendaliId=:noRec and p.isActive=true group by su  ")
	List<Rekanan> findSupplierBykartuPengendaliDetail(@Param("noRec") String noRec);

	@Query("select p from KartuPengendaliDetail p where p.kartuPengendaliId=:noRec and p.isActive=true and p.supplierId=:id ")
	List<KartuPengendaliDetail> findBySupplierAndKartuPengendaliHeaderList(@Param("noRec") String noRec, @Param("id") Integer id);

	@Query("select new map ( " +
		    " coalesce(spek.spesifikasi,'-') as spesifikasi, "+
			" produk.namaProduk as namaProduk, " +
			" produk.id as idProduk, " +
			" asalProduk.asalProduk as asalProduk,  " +
			" asalProduk.id as idAsalProduk,  " +
			" pengendali.namaPengendali as namaPengendali, " +
			" asalProduk.asalProduk as asalProduk, " +
			" kartuPengendaliDetail.hargaSupplier as hargaSupplier, " +
			" kartuPengendaliDetail.volumeBarang as volumeBarang, " +
			" kartuPengendaliDetail.noRec as noRec, " +
			" satuanStandar.satuanStandar as satuanStandar,  " +
			" satuanStandar.id as idSatuan," +
			" produk.namaProduk as namaProduk," +
			" spek.volumeBarang as jumlahOrder," +
			" sum(detail.qtyproduk) as totalDiterima," +
			" (spek.volumeBarang)-(sum(detail.qtyproduk)) as sisa )" +
			" from StrukPelayananDetail detail" +
			" left join detail.nostruk header" +
			" right join detail.kartuPengendaliDetail kartuPengendaliDetail" +
			" left join kartuPengendaliDetail.detailRup detailRup" +
			" left join detailRup.detailSpekAnggaran spek" +
			" left join spek.produk produk" +
			" left join spek.detailAnggaran detailAnggaran " +
			" left join detailAnggaran.asalProduk asalProduk " +
			" left join kartuPengendaliDetail.kartuPengendali header   " +
			" left join spek.produk produk  " +
			" left join spek.pengendali pengendali " +
			" left join detailAnggaran.akun akun " +
			" left join detailAnggaran.asalProduk asalProduk " +
			" left join produk.satuanStandar satuanStandar " +
			" left join kartuPengendaliDetail.strukRekapPemenang strukRekapPemenang " +
			" where kartuPengendaliDetail.noRec is not null and strukRekapPemenang.noRec=:noRec " +
			" group by " +
			" produk.namaProduk, " +
			" produk.id, " +
			" asalProduk.asalProduk,  " +
			" asalProduk.id,  " +
			" pengendali.namaPengendali, " + 
			" asalProduk.asalProduk, " +
			" kartuPengendaliDetail.hargaSupplier, " + 
			" kartuPengendaliDetail.volumeBarang, " +
			" kartuPengendaliDetail.noRec, " +
			" satuanStandar.satuanStandar,  " +
			" satuanStandar.id, " +
			" spek.volumeBarang, " +
			" produk.namaProduk,spek.spesifikasi " +
			" having coalesce(sum(detail.qtyproduk),0.0) !=spek.volumeBarang " ) 
	List<Map<String,Object>> getKartuPengendaliDetailStrukRekapKontrak(@Param("noRec") String noRec);
	
	
	@Query("select new map ( " +
			" produk.namaProduk as namaProduk, " +
			" produk.id as idProduk, " +
			" asalProduk.asalProduk as asalProduk,  " +
			" asalProduk.id as idAsalProduk,  " +
			" pengendali.namaPengendali as namaPengendali, " +
			" asalProduk.asalProduk as asalProduk, " +
			" kartuPengendaliDetail.hargaSupplier as hargaSupplier, " +
			" kartuPengendaliDetail.volumeBarang as volumeBarang, " +
			" kartuPengendaliDetail.noRec as noRec, " +
			" detail.noRec as noRecStrukPelayananDetail, " +
			" satuanStandar.satuanStandar as satuanStandar,  " +
			" satuanStandar.id as idSatuan," +
			" produk.namaProduk as namaProduk," +
			" detail.qtyproduk as jumlahOrder," +
			" sum(detail.qtyproduk) as totalDiterima )" +
			" from StrukPelayananDetail detail" +
			" left join detail.nostruk header" +
			" right join detail.kartuPengendaliDetail kartuPengendaliDetail" +
			" left join kartuPengendaliDetail.detailRup detailRup" +
			" left join detailRup.detailSpekAnggaran spek" +
			" left join spek.produk produk" +
			" left join spek.detailAnggaran detailAnggaran " +
			" left join detailAnggaran.asalProduk asalProduk " +
			" left join kartuPengendaliDetail.kartuPengendali kartuPengendali   " +
			" left join spek.produk produk  " +
			" left join spek.pengendali pengendali " +
			" left join detailAnggaran.akun akun " +
			" left join detailAnggaran.asalProduk asalProduk " +
			" left join produk.satuanStandar satuanStandar " +
			" left join kartuPengendaliDetail.strukRekapPemenang strukRekapPemenang " +
			" where kartuPengendaliDetail.noRec is not null and header.noRec=:noRec " +
			" group by " +
			" produk.namaProduk, " +
			" produk.id, " +
			" asalProduk.asalProduk,  " +
			" asalProduk.id,  " +
			" pengendali.namaPengendali, " + 
			" asalProduk.asalProduk, " +
			" kartuPengendaliDetail.hargaSupplier, " + 
			" kartuPengendaliDetail.volumeBarang, " +
			" kartuPengendaliDetail.noRec, " +
			" satuanStandar.satuanStandar,  " +
			" satuanStandar.id, " +
			" spek.volumeBarang, " +
			" produk.namaProduk," +
		    "detail.noRec " ) 
	List<Map<String,Object>> getKartuPengendaliDetailLogistik(@Param("noRec") String noRec);

	@Query("Select detailSpekAnggaran.volumeBarang-sum(strukPelayananaDetail.qtyproduk) from StrukPelayananDetail strukPelayananaDetail "
			+ "left join strukPelayananaDetail.kartuPengendaliDetail kartuPengendaliDetail "
			+ "left join strukPelayananaDetail.nostruk strukPelayanan "
			+ "left join kartuPengendaliDetail.detailRup rup "
			+ "left join rup.detailSpekAnggaran detailSpekAnggaran "
			+ "where kartuPengendaliDetail.noRec=:noRec "
			+ "and strukPelayanan.kdkelompoktransaksi=(Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='kdKelompokTransaksiSppb') "
			+ "group by kartuPengendaliDetail.noRec,detailSpekAnggaran.volumeBarang")
	Double getSisaPenerimaan(@Param("noRec") String noRec);
	
	@Query("Select sum(strukPelayananaDetail.qtyproduk) from StrukPelayananDetail strukPelayananaDetail where strukPelayananaDetail.strukPelayananDetail.noRec=:noRec ")
	Double getSisaPenerimaanLogistik(@Param("noRec") String noRec);
	
}
