package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.KelompokTransaksiDao;
import com.jasamedika.medifirst2000.dao.RegistrasiAsetDao;
import com.jasamedika.medifirst2000.dao.StrukPelayananDNoBatchDao;
import com.jasamedika.medifirst2000.dao.StrukPelayananDetailDao;
import com.jasamedika.medifirst2000.dao.custom.StrukPelayananDetailDaoCustom;
import com.jasamedika.medifirst2000.entities.AsalProduk;
import com.jasamedika.medifirst2000.entities.KelompokUser;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.entities.RegistrasiAset;
import com.jasamedika.medifirst2000.entities.SatuanStandar;
import com.jasamedika.medifirst2000.entities.StrukPelayananDetail;
import com.jasamedika.medifirst2000.service.StrukPelayananDetailService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.AsalProdukVO;
import com.jasamedika.medifirst2000.vo.PenerimaanBarangDetailVO;
import com.jasamedika.medifirst2000.vo.ProdukVO;
import com.jasamedika.medifirst2000.vo.RegistrasiAsetVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;
import com.jasamedika.medifirst2000.vo.SatuanStandarVO;


/**
 * 
 * @author adik
 */
@Service("StrukPelayananDetailService")
public class StrukPelayananDetailServiceImpl extends BaseAngaranServiceImpl implements StrukPelayananDetailService{
	
	@Autowired
	private StrukPelayananDetailDao strukPelayananDetailDao;
	
	@Autowired
	private KelompokTransaksiDao kelompokTransaksiDao;
	
	@Autowired
	private StrukPelayananDetailDaoCustom strukPelayananDetailDaoCustom;
	
	@Autowired
	private RegistrasiAsetDao registrasiAsetDao;
	
	@Autowired
	private StrukPelayananDNoBatchDao strukPelayananDNoBatchDao;
	
	@Autowired
	private BaseConverterImpl<AsalProdukVO, AsalProduk> asalProdukConverter;
	
	@Autowired
	private BaseConverterImpl<SatuanStandarVO, SatuanStandar> satuanStandarConverter;

	@Autowired
	private BaseConverterImpl<ProdukVO, Produk> produkConverter;
	
	@Override
	public PenerimaanBarangDetailVO findByNoRec(String noRec) {
		StrukPelayananDetail strukPelayananDetail=strukPelayananDetailDao.findByNorec(noRec);
		PenerimaanBarangDetailVO vo=new PenerimaanBarangDetailVO();
		vo.setJumlahTerima(strukPelayananDetail.getQtyproduk());
		if(CommonUtil.isNotNullOrEmpty(strukPelayananDetail.getKdasalproduk())){
			vo.setAsalProduk(asalProdukConverter.transferModelToVO(strukPelayananDetail.getKdasalproduk(), new AsalProdukVO()));
		}
		if(CommonUtil.isNotNullOrEmpty(strukPelayananDetail.getKdsatuanstandar())){
			vo.setSatuan(satuanStandarConverter.transferModelToVO(strukPelayananDetail.getKdsatuanstandar(), new SatuanStandarVO()));
		}
		
		if(CommonUtil.isNotNullOrEmpty(strukPelayananDetail.getKdproduk())){
			vo.setProduk(produkConverter.transferModelToVO(strukPelayananDetail.getKdproduk(), new ProdukVO()));
		}
		
		
		for(RegistrasiAset asset:registrasiAsetDao.findByStrukPelayananDetil(noRec)){
			RegistrasiAsetVO registrasiAsetVO=new RegistrasiAsetVO();
			RuanganVO r=new RuanganVO();
			if(CommonUtil.isNotNullOrEmpty(asset.getRuangan())){
				r.setId(asset.getRuangan().getId());
				r.setNamaRuangan(asset.getRuangan().getNamaRuangan());
				registrasiAsetVO.setRuangan(r);
			}
			registrasiAsetVO.setNoRegisterAset(asset.getNoRegisterAset());
			registrasiAsetVO.setNoRec(asset.getNoRec());
			vo.getRegistrasiAset().add(registrasiAsetVO);
		}
		
		return vo;
	}

	@Override
	public Map<String, Object> gridPenerimaanBarang(Integer page, Integer limit, String sort, String dir,String tanggalTerimAwal, String tanggalTerimAhir, String nofaktur, Integer idProduk,Integer supplierId) {
		Integer kdKelompokTransaksi=Integer.parseInt((String) this.GetSettingDataFixed("KdPenerimaanBarangLogistik"));
		KelompokUser kelompokUser=getKelompokUserLogin();

		int totalRow = strukPelayananDetailDaoCustom.gridPenerimaanBarangCount(tanggalTerimAwal,tanggalTerimAhir,nofaktur,idProduk,kdKelompokTransaksi,kelompokUser,supplierId);
		int totalPages = 0;

		int pageRequested = page;

		if (totalRow > 0) {
			totalPages = (int) Math.ceil((double) totalRow / (double) limit);
		} else {
			totalPages = 0;
		}

		if (pageRequested > totalPages)
			pageRequested = totalPages;
		int rowStart = pageRequested * limit - limit;
		if (rowStart < 0) {
			rowStart = 0;
		}
		int rowEnd = limit;
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String,Object>> listData=new ArrayList<Map<String,Object>>();
		for(Map<String, Object> data:strukPelayananDetailDaoCustom.gridPenerimaanBarangList(rowStart, rowEnd, sort, dir, tanggalTerimAwal,tanggalTerimAhir,nofaktur,idProduk,kdKelompokTransaksi,kelompokUser,supplierId)){
			if(kelompokUser.getKelompokUser().equalsIgnoreCase("logistik")){
				data.put("jenisPenerimaan", "logistik");
			}else if(kelompokUser.getKelompokUser().equalsIgnoreCase("bagianUmum")){
				data.put("jenisPenerimaan", "bagianUmum");
			}
			listData.add(data);
		}
		result.put("data", listData);
		result.put("totalPages", totalPages);
		result.put("totalRow", totalRow);
		return result;
	
	}

	@Override
	public Map<String, Object> findByNoRecBatch(String noRec) {
		Map<String,Object>result=new HashMap<String,Object>();
		result.put("strukPelayananDetail",strukPelayananDetailDao.getStrukPelayananDetail(noRec));
		result.put("noBatch", strukPelayananDNoBatchDao.findNoBatchByParent(noRec));
		return result;
	}
	

	@Override
	@Transactional(readOnly=false)
	public Map<String, Object> listPenerimaanBarang(Integer page, Integer limit, String sort, String dir,String tanggalAwal, String tanggalAhir, Integer produkId, Integer jenisProdukId, Integer kelompokProdukId, String noTerima) {
		Integer ruanganId=getRuanganByUserLogin().getId();
		Integer kelompokTransaksiId=kelompokTransaksiDao.findById(Integer.parseInt((String) this.GetSettingDataFixed("KdPenerimaanBarangLogistik"))).getId();
		int totalRow = strukPelayananDetailDaoCustom.gridPenerimaanCountSupplier(tanggalAwal, tanggalAhir, produkId, ruanganId, kelompokTransaksiId,jenisProdukId,kelompokProdukId,noTerima);
		int totalPages = 0;
		int pageRequested = page;

		if (totalRow > 0) {
			totalPages = (int) Math.ceil((double) totalRow / (double) limit);
		} else {
			totalPages = 0;
		}

		if (pageRequested > totalPages)
			pageRequested = totalPages;
		int rowStart = pageRequested * limit - limit;
		if (rowStart < 0) {
			rowStart = 0;
		}
		int rowEnd = limit;
		Map<String, Object> result=new HashMap<String,Object>();
		result.put("data", strukPelayananDetailDaoCustom.gridPenerimaanSupplier(rowStart, rowEnd, sort, dir, tanggalAwal, tanggalAhir, produkId, ruanganId, kelompokTransaksiId,jenisProdukId,kelompokProdukId,noTerima));
		result.put("totalPages", totalPages);
		result.put("totalRow", totalRow);
		return result;
	}
	

	@Override
	public Map<String, Object> detailPenerimaan(String noRec) {
		
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("header", strukPelayananDetailDaoCustom.findStrukKirimHeaderByNoRecReport(noRec));
		data.put("detail", strukPelayananDetailDaoCustom.findStrukKirimDetailByNoRecReport(noRec));
		data.put("total", strukPelayananDetailDaoCustom.totalPenerimaanSupplier(noRec));
		data.put("yangMenerima", "-");
		data.put("yangMennyerahkan", "-");
		data.put("nipYangMennyerahkan", "-");
		data.put("nipYangMenerima", "-");
		data.put("mengetahui", "-");
		data.put("nipMengetahui", "-");
		return data;
	
	}

	
	
	
}
