package com.jasamedika.medifirst2000.service.impl;

import java.util.Date;
import java.util.HashMap;
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.RunningNumberDao;
import com.jasamedika.medifirst2000.dao.StrukKirimDao;
import com.jasamedika.medifirst2000.entities.AsalProduk;
import com.jasamedika.medifirst2000.entities.KirimProduk;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.RunningNumber;
import com.jasamedika.medifirst2000.entities.StrukKirim;
import com.jasamedika.medifirst2000.entities.StrukOrder;
import com.jasamedika.medifirst2000.service.KirimProdukService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.StringUtil;
import com.jasamedika.medifirst2000.vo.AsalProdukVO;
import com.jasamedika.medifirst2000.vo.KirimPesananBarangDetailVO;
import com.jasamedika.medifirst2000.vo.KirimPesananBarangVO;
import com.jasamedika.medifirst2000.vo.ProdukVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;
import com.jasamedika.medifirst2000.vo.StrukOrderVO;

@Service("kirimProdukService")
public class KirimProdukServiceImpl implements KirimProdukService {
	
	@Autowired
	private RunningNumberDao runningNumberDao; 
	
	@Autowired
	private StrukKirimDao strukKirimDao; 
	
	@Autowired
	private BaseConverterImpl<StrukOrderVO, StrukOrder> strukOrderConverter;
	
	@Autowired
	private BaseConverterImpl<AsalProdukVO, AsalProduk> asalProdukConverter;
	
	@Autowired
	private BaseConverterImpl<ProdukVO, Produk> produkConverter;
	
	@Autowired
	private BaseConverterImpl<RuanganVO,Ruangan> ruanganConverter;
	
	@Override
	@Transactional(readOnly=false)
	public Map<String, Object> saveKirimProduk(KirimPesananBarangVO vo) {

		StrukKirim strukKirim=new StrukKirim();
		strukKirim.setTglkirim(new Date());
		strukKirim.setNokirim(getRunningNumber("StrukPelayanan"));
		if(CommonUtil.isNotNullOrEmpty(vo.getStrukOrder())){
			strukKirim.setNoorder(strukOrderConverter.transferVOToModel(vo.getStrukOrder(), new StrukOrder()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getRuanganPenerima())){
			strukKirim.setKdruanganasal(ruanganConverter.transferVOToModel(vo.getRuanganPenerima(), new Ruangan()));
		}
		
		if(CommonUtil.isNotNullOrEmpty(vo.getRuanganPengirim())){
			strukKirim.setKdruangantujuan(ruanganConverter.transferVOToModel(vo.getRuanganPengirim(), new Ruangan()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getKirimPesananBarangDetail())){
			for(KirimPesananBarangDetailVO kirimPesananBarangDetailVO:vo.getKirimPesananBarangDetail()){
				KirimProduk kirimProduk=new KirimProduk();
				kirimProduk.setQtyProduk(kirimPesananBarangDetailVO.getKirim());
				kirimProduk.setNoKirim(strukKirim);
				
				kirimProduk.setSatuan("");
				kirimProduk.setTglpelayanan(strukKirim.getTglkirim());
				
				if(CommonUtil.isNotNullOrEmpty(kirimPesananBarangDetailVO.getProduk())){
					kirimProduk.setProduk(produkConverter.transferVOToModel(kirimPesananBarangDetailVO.getProduk(), new Produk()));
				}
				if(CommonUtil.isNotNullOrEmpty(kirimPesananBarangDetailVO.getAsalProduk())){
					kirimProduk.setAsalProduk(asalProdukConverter.transferVOToModel(kirimPesananBarangDetailVO.getAsalProduk(),new AsalProduk()));
				}
				kirimProduk.setHargaDiscount(0.0);
				kirimProduk.setHargaNetto(0.0);
				kirimProduk.setHargaPph(0.0);
				kirimProduk.setHargaPpn(0.0);
				kirimProduk.setHargaSatuan(0.0);
				kirimProduk.setHargaTambahan(0.0);
				kirimProduk.setPersenDiscount(0.0);
				kirimProduk.setQtyProdukKonfirmasi(0.0);
				kirimProduk.setQtyProdukRetur(0.0);
				kirimProduk.setQtyProdukTerima(0.0);
				kirimProduk.setSatuan("-");
				strukKirim.getKirimProduk().add(kirimProduk);
			}
		}
		strukKirimDao.save(strukKirim);
		Map<String,Object>result=new HashMap<String,Object>();
		result.put("noKirim", strukKirim.getNokirim());
		
		return result;
	}
	
	private String getRunningNumber(String kegunaan) {
		RunningNumber runningNumber = runningNumberDao.findByResetAndKegunaan("0", kegunaan);
		if(runningNumber==null)
		{
			runningNumber = new RunningNumber();
			runningNumber.setKegunaan(kegunaan);
			runningNumber.setReset("0");
			runningNumber.setNomerTerbaru((Integer)0);
		}
		Integer number=(runningNumber.getNomerTerbaru()+(Integer)1);
		runningNumber.setNomerTerbaru(runningNumber.getNomerTerbaru()+(Integer)1);
		String noOrderIntern = StringUtil.formatNumber(number.toString(), 10);
		return noOrderIntern;
	}
	
}
