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.PelayananPasienDetail;

/**
 * @author salmanoe
 *
 */
@Repository("pendapatanDao")
public interface PendapatanDao extends PagingAndSortingRepository<PelayananPasienDetail, String> {

	String pendapatanDokterStr = "select new Map(dokter.id as idPegawai, " + "dokter.namaLengkap as nama, "
			+ "jenisPetugas.id as idJenisPelaksana, " + "detail.id as detailId, "
			+ "detail.detailJenisProduk as detailJenisProduk, " + "produk.id as idProduk, "
			+ "produk.namaProduk as namaProduk, " + "kelas.id as idKelas, " + "kelas.namaKelas as namaKelas, "
			+ "(select distinct harga.hargaNetto1 from HargaNettoProdukByKelas harga "
			+ "where harga.produkId = produk.id and harga.statusEnabled is true and harga.kelasId = kelas.id "
			+ "and harga.kelasId = pelayananPasien.kelasId) as hargaKelas, "
			+ "(select distinct coalesce(hargaD.hargaNetto1, 0) from HargaNettoProdukByKelasD hargaD "
			+ "left join hargaD.komponenHarga komponenHarga " + "where hargaD.statusEnabled is true "
			+ "and hargaD.produkId = produk.id and hargaD.produkId = pelayananPasien.produkId "
			+ "and hargaD.kelasId = kelas.id and hargaD.kelasId = pelayananPasien.kelasId "
			+ "and komponenHarga.id = pelayananPasienDetail.komponenHargaId) as tarifJasa, "
			+ "pelayananPasien.hargaSatuan as harga, " + "coalesce(pelayananPasien.hargaDiscount,0) as diskon, "
			+ "pelayananPasien.jumlah as count, " + "to_char(pelayananPasien.tglPelayanan,'yyyy-MM-dd') as tanggal, "
			+ "cast(pelayananPasien.tglPelayanan as timestamp) as tanggalPelayanan, "
			+ "coalesce(pelayananPasienDetail.hargaJual, 0) as hargaJual,"
			+ "coalesce(pelayananPasienDetail.hargaDiscount, 0) as hargaDiskon,"
			+ "pelayananPasienDetail.komponenHargaId as idJasa," + "pasienDaftar.kelompokPasienId as kelompokPasienId, "
			+ "pasienDaftar.rekananId as rekananId," + "pasienDaftar.diskonPegawai as statusDiskon, "
			+ "kategoryPegawai.id as kategoryPegawaiId, " + "departemen.id as departemenId, "
			+ "ruangan.id as ruanganId, " + "to_char(kalender.tanggal, 'yyyy-MM-dd') as tanggalKalender, "
			+ "shift.jamMasuk as jamMasuk, " + "shift.jamPulang as jamPulang, " + "shift.namaShift as namaShift) "
			+ "from PelayananPasienPetugas pelayananPasienPetugas, " + "PelayananPasienDetail pelayananPasienDetail, "
			+ "PegawaiJadwalKerja jadwalKerja, " + "Kalender kalender, " + "ShiftKerja shift "
			+ "left join pelayananPasienPetugas.pelayananPasien pelayananPasien "
			+ "left join pelayananPasienPetugas.kdjenispetugaspe jenisPetugas "
			+ "left join pelayananPasienPetugas.kdpegawai dokter " + "left join pelayananPasien.produk produk "
			+ "left join pelayananPasien.kelas kelas " + "left join pelayananPasien.pasienDaftar antrian "
			+ "left join dokter.kategoryPegawai kategoryPegawai " + "left join produk.detailJenisProduk detail "
			+ "left join antrian.ruangan ruangan " + "left join antrian.pasienDaftar pasienDaftar "
			+ "left join detail.jenisProduk jenisProduk " + "left join ruangan.departemen departemen "
			+ "where pelayananPasien.noRec = pelayananPasienDetail.pelayananPasienId "
			+ "and pelayananPasienDetail.pasienDaftarId = antrian.noRec "
			+ "and pelayananPasienDetail.produkId = produk.id " + "and dokter.id = jadwalKerja.pegawaiId "
			+ "and kalender.id = jadwalKerja.tanggalId " + "and shift.id = jadwalKerja.shiftId "
			+ "and to_char(kalender.tanggal, 'yyyy-MM-dd') = to_char(pelayananPasien.tglPelayanan,'yyyy-MM-dd') "
			+ "and pelayananPasienPetugas.statusEnabled is not false "
			+ "and pelayananPasien.statusEnabled is not false " + "and antrian.statusEnabled is not false "
			+ "and pasienDaftar.statusEnabled is not false " + "and pelayananPasienDetail.statusEnabled is true "
			+ "and jenisPetugas.statusEnabled is true " + "and dokter.statusEnabled is true "
			+ "and produk.statusEnabled is true " + "and detail.statusEnabled is true "
			+ "and kelas.statusEnabled is true " + "and kategoryPegawai.statusEnabled is true "
			+ "and ruangan.statusEnabled is true " + "and departemen.statusEnabled is true "
			+ "and jenisProduk.statusEnabled is true " + "and pelayananPasienPetugas.ObjectPegawaiId = :idPegawai "
			+ "and jenisPetugas.id <> :idPetugasPelaksana "
			+ "and to_char(pelayananPasien.tglPelayanan,'yyyy-MM') = :month ";

	String cetakPendapatanStr = "select new Map(pelayananPasienPetugas.noRec as norecPetugas,"
			+ "detail.id as detailId, " + "pasien.namaPasien as namapasien," + "pasien.noCm as noCm,"
			+ "pasien.tglLahir as tglLahir," + "(select distinct harga.hargaNetto1 from HargaNettoProdukByKelas harga "
			+ "where harga.produkId = produk.id and harga.statusEnabled is true and harga.kelasId = kelas.id "
			+ "and harga.kelasId = pelayananPasien.kelasId) as hargaKelas, "
			+ "coalesce(pelayananPasienDetail.hargaJual, 0) as hargaJual,"
			+ "coalesce(pelayananPasienDetail.hargaDiscount, 0) as hargaDiskon,"
			+ "pelayananPasienDetail.komponenHargaId as idKomponenHarga," + "pasienDaftar.noRegistrasi as noRegistrasi,"
			+ "pasienDaftar.tglRegistrasi as tglRegistrasi," + "pasienDaftar.tglPulang as tglPulang,"
			+ "pasienDaftar.kelompokPasienId as kelompokPasienId, " + "pasienDaftar.rekananId as rekananId,"
			+ "pasienDaftar.diskonPegawai as statusDiskon, " + "produk.id as idProduk,"
			+ "produk.namaProduk as namaProduk," + "dokter.id as idPegawai,"
			+ "pelayananPasien.noRec as noRecPelayananPasien," + "pelayananPasien.jumlah as jumlah,"
			+ "pelayananPasien.hargaSatuan as harga," + "coalesce(pelayananPasien.hargaDiscount,0) as diskon,"
			+ "pelayananPasien.tglPelayanan as tglpelayanan,"
			+ "to_char(pelayananPasien.tglPelayanan,'yyyy-MM-dd') as tglpel,"
			+ "cast(pelayananPasien.tglPelayanan as timestamp) as tanggalPelayanan, "
			+ "kategoryPegawai.id as kategoryPegawaiId, " + "kelas.id as idKelas," + "kelas.namaKelas as namaKelas,"
			+ "jenisPetugas.jenisPetugasPe as jenisPetugas, " + "jenisPetugas.id as idJenisPelaksana, "
			+ "departemen.id as departemenId, " + "ruangan.id as ruanganId, " + "ruangan.namaRuangan as ruangan,"
			+ "to_char(kalender.tanggal, 'yyyy-MM-dd') as tanggalKalender, " + "shift.jamMasuk as jamMasuk, "
			+ "shift.jamPulang as jamPulang, " + "shift.namaShift as namaShift) "
			+ "from PelayananPasienPetugas pelayananPasienPetugas, " + "PelayananPasienDetail pelayananPasienDetail, "
			+ "PegawaiJadwalKerja jadwalKerja, " + "Kalender kalender, " + "ShiftKerja shift "
			+ "left join pelayananPasienPetugas.pelayananPasien pelayananPasien "
			+ "left join pelayananPasienPetugas.kdjenispetugaspe jenisPetugas "
			+ "left join pelayananPasienPetugas.kdpegawai dokter " + "left join pelayananPasien.produk produk "
			+ "left join pelayananPasien.kelas kelas " + "left join pelayananPasien.pasienDaftar antrian "
			+ "left join dokter.kategoryPegawai kategoryPegawai " + "left join produk.detailJenisProduk detail "
			+ "left join antrian.ruangan ruangan " + "left join antrian.pasienDaftar pasienDaftar "
			+ "left join detail.jenisProduk jenisProduk " + "left join ruangan.departemen departemen "
			+ "left join pasienDaftar.pasien pasien "
			+ "where pelayananPasien.noRec = pelayananPasienDetail.pelayananPasienId "
			+ "and pelayananPasienDetail.pasienDaftarId = antrian.noRec "
			+ "and pelayananPasienDetail.produkId = produk.id " + "and dokter.id = jadwalKerja.pegawaiId "
			+ "and kalender.id = jadwalKerja.tanggalId " + "and shift.id = jadwalKerja.shiftId "
			+ "and to_char(kalender.tanggal, 'yyyy-MM-dd') = to_char(pelayananPasien.tglPelayanan,'yyyy-MM-dd') "
			+ "and (pelayananPasienPetugas.statusEnabled is null or pelayananPasienPetugas.statusEnabled is true) "
			+ "and (pelayananPasien.statusEnabled is null or pelayananPasien.statusEnabled is true) "
			+ "and (antrian.statusEnabled is null or antrian.statusEnabled is true) "
			+ "and (pasienDaftar.statusEnabled is null or pasienDaftar.statusEnabled is true) "
			+ "and pelayananPasienDetail.statusEnabled is true " + "and jenisPetugas.statusEnabled is true "
			+ "and dokter.statusEnabled is true " + "and produk.statusEnabled is true "
			+ "and detail.statusEnabled is true " + "and kelas.statusEnabled is true "
			+ "and kategoryPegawai.statusEnabled is true " + "and ruangan.statusEnabled is true "
			+ "and departemen.statusEnabled is true " + "and jenisProduk.statusEnabled is true "
			+ "and pelayananPasienPetugas.ObjectPegawaiId = :idPegawai " + "and jenisPetugas.id <> :idPetugasPelaksana "
			+ "and to_char(pelayananPasien.tglPelayanan,'yyyy-MM') = :periode ";

	String komponenFfs = "and pelayananPasienDetail.komponenHargaId in (:listIdKomponenMedis) ";

	String departemen = "and departemen.id = :idDepartemen ";

	String ruangan = "and ruangan.id = :idRuangan ";

	String sortData = "order by produk.namaProduk, kelas.namaKelas, to_char(pelayananPasien.tglPelayanan,'yyyy-MM-dd')";

	String sortCetak = "order by pelayananPasien.tglPelayanan";

	@Query(pendapatanDokterStr + komponenFfs + sortData)
	public List<Map<String, Object>> getPendapatanDokter(@Param("idPegawai") Integer idPegawai,
			@Param("listIdKomponenMedis") List<Integer> listIdKomponenMedis,
			@Param("idPetugasPelaksana") int idPetugasPelaksana, @Param("month") String month);

	@Query(pendapatanDokterStr + departemen + komponenFfs + sortData)
	public List<Map<String, Object>> getPendapatanDepartemenDokter(@Param("idDepartemen") Integer idDepartemen,
			@Param("idPegawai") Integer idPegawai, @Param("listIdKomponenMedis") List<Integer> listIdKomponenMedis,
			@Param("idPetugasPelaksana") int idPetugasPelaksana, @Param("month") String month);

	@Query(pendapatanDokterStr + departemen + ruangan + komponenFfs + sortData)
	public List<Map<String, Object>> getPendapatanRuanganDokter(@Param("idDepartemen") Integer idDepartemen,
			@Param("idRuangan") Integer idRuangan, @Param("idPegawai") Integer idPegawai,
			@Param("listIdKomponenMedis") List<Integer> listIdKomponenMedis,
			@Param("idPetugasPelaksana") int idPetugasPelaksana, @Param("month") String month);

	@Query(cetakPendapatanStr + komponenFfs + sortCetak)
	public List<Map<String, Object>> getCetakDetailPasienDokterPerPeriode(@Param("idPegawai") Integer idPegawai,
			@Param("listIdKomponenMedis") List<Integer> listIdKomponenMedis,
			@Param("idPetugasPelaksana") int idPetugasPelaksana, @Param("periode") String periode);

	@Query(cetakPendapatanStr + departemen + komponenFfs + sortCetak)
	public List<Map<String, Object>> getCetakDetailPasienDokterPerPeriodeDepartemen(
			@Param("idDepartemen") Integer idDepartemen, @Param("idPegawai") Integer idPegawai,
			@Param("listIdKomponenMedis") List<Integer> listIdKomponenMedis,
			@Param("idPetugasPelaksana") int idPetugasPelaksana, @Param("periode") String periode);

	@Query(cetakPendapatanStr + departemen + ruangan + komponenFfs + sortCetak)
	public List<Map<String, Object>> getCetakDetailPasienDokterPerPeriodeRuangan(
			@Param("idDepartemen") Integer idDepartemen, @Param("idRuangan") Integer idRuangan,
			@Param("idPegawai") Integer idPegawai, @Param("listIdKomponenMedis") List<Integer> listIdKomponenMedis,
			@Param("idPetugasPelaksana") int idPetugasPelaksana, @Param("periode") String periode);

	@Query("select new Map(pelayananPasienPetugas.noRec as norecPetugas," + "detail.id as detailId, "
			+ "pasien.namaPasien as namapasien," + "pasien.noCm as noCm," + "pasien.tglLahir as tglLahir,"
			+ "(select distinct harga.hargaNetto1 from HargaNettoProdukByKelas harga "
			+ "where harga.produkId = produk.id and harga.statusEnabled is true and harga.kelasId = kelas.id "
			+ "and harga.kelasId = pelayananPasien.kelasId) as hargaKelas, "
			+ "coalesce(pelayananPasienDetail.hargaJual, 0) as hargaJual,"
			+ "coalesce(pelayananPasienDetail.hargaDiscount, 0) as hargaDiscount,"
			+ "pelayananPasien.noRec as noRecPelayananPasien," + "pelayananPasien.hargaSatuan as harga,"
			+ "coalesce(pelayananPasien.hargaDiscount,0) as diskon, " + "pelayananPasien.jumlah as jumlah,"
			+ "pelayananPasien.tglPelayanan as tglpelayanan,"
			+ "to_char(pelayananPasien.tglPelayanan,'yyyy-MM-dd') as tglpel,"
			+ "cast(pelayananPasien.tglPelayanan as timestamp) as tanggalPelayanan, " + "kelas.id as idKelas,"
			+ "kelas.namaKelas as namaKelas," + "pasienDaftar.noRegistrasi as noRegistrasi,"
			+ "pasienDaftar.tglRegistrasi as tglRegistrasi," + "pasienDaftar.tglPulang as tglPulang,"
			+ "pasienDaftar.kelompokPasienId as kelompokPasienId, " + "pasienDaftar.rekananId as rekananId, "
			+ "pasienDaftar.diskonPegawai as statusDiskon, " + "produk.id as idProduk,"
			+ "produk.namaProduk as namaProduk," + "dokter.id as idPegawai,"
			+ "kategoryPegawai.id as kategoryPegawaiId, " + "jenisPetugas.id as idJenisPelaksana, "
			+ "jenisPetugas.jenisPetugasPe as jenisPetugas, " + "departemen.id as departemenId, "
			+ "ruangan.id as ruanganId, " + "ruangan.namaRuangan as ruangan,"
			+ "to_char(kalender.tanggal, 'yyyy-MM-dd') as tanggalKalender, " + "shift.jamMasuk as jamMasuk, "
			+ "shift.jamPulang as jamPulang, " + "shift.namaShift as namaShift) "
			+ "from PelayananPasienPetugas pelayananPasienPetugas, " + "PelayananPasienDetail pelayananPasienDetail, "
			+ "PegawaiJadwalKerja jadwalKerja, " + "Kalender kalender, " + "ShiftKerja shift "
			+ "left join pelayananPasienPetugas.pelayananPasien pelayananPasien "
			+ "left join pelayananPasienPetugas.kdjenispetugaspe jenisPetugas "
			+ "left join pelayananPasienPetugas.kdpegawai dokter " + "left join pelayananPasien.produk produk "
			+ "left join pelayananPasien.kelas kelas " + "left join pelayananPasien.pasienDaftar antrian "
			+ "left join dokter.kategoryPegawai kategoryPegawai " + "left join produk.detailJenisProduk detail "
			+ "left join antrian.ruangan ruangan " + "left join antrian.pasienDaftar pasienDaftar "
			+ "left join detail.jenisProduk jenisProduk " + "left join ruangan.departemen departemen "
			+ "left join pasienDaftar.pasien pasien "
			+ "where pelayananPasien.noRec = pelayananPasienDetail.pelayananPasienId "
			+ "and pelayananPasienDetail.pasienDaftarId = antrian.noRec "
			+ "and pelayananPasienDetail.produkId = produk.id " + "and dokter.id = jadwalKerja.pegawaiId "
			+ "and kalender.id = jadwalKerja.tanggalId " + "and shift.id = jadwalKerja.shiftId "
			+ "and to_char(kalender.tanggal, 'yyyy-MM-dd') = to_char(pelayananPasien.tglPelayanan,'yyyy-MM-dd') "
			+ "and (pelayananPasienPetugas.statusEnabled is null or pelayananPasienPetugas.statusEnabled is true) "
			+ "and (pelayananPasien.statusEnabled is null or pelayananPasien.statusEnabled is true) "
			+ "and (antrian.statusEnabled is null or antrian.statusEnabled is true) "
			+ "and (pasienDaftar.statusEnabled is null or pasienDaftar.statusEnabled is true) "
			+ "and pelayananPasienDetail.statusEnabled is true " + "and jenisPetugas.statusEnabled is true "
			+ "and dokter.statusEnabled is true " + "and produk.statusEnabled is true "
			+ "and detail.statusEnabled is true " + "and kelas.statusEnabled is true "
			+ "and kategoryPegawai.statusEnabled is true " + "and ruangan.statusEnabled is true "
			+ "and departemen.statusEnabled is true " + "and jenisProduk.statusEnabled is true "
			+ "and antrian.kelasId = :idKelas " + "and pelayananPasienPetugas.ObjectPegawaiId = :idPegawai "
			+ "and pelayananPasienDetail.komponenHargaId = :idKomponenHarga " + "and produk.id = :idProduk "
			+ "and jenisPetugas.id <> :idPetugasPelaksana "
			+ "and to_char(pelayananPasien.tglPelayanan,'yyyy-MM-dd') = :tgl "
			+ "order by pelayananPasien.tglPelayanan")
	public List<Map<String, Object>> getDetailPasienFeeForService(@Param("idKelas") Integer idKelas,
			@Param("idPegawai") Integer idPegawai, @Param("idProduk") Integer idProduk,
			@Param("idKomponenHarga") Integer idKomponenHarga, @Param("idPetugasPelaksana") int idPetugasPelaksana,
			@Param("tgl") String tgl);

}
