package com.dev.swms.server.service.fee.pojo;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dev.energy.server.model.EntityFactory;
import com.dev.energy.server.service.pojo.DefaultBaseManager;
import com.dev.energy.server.web.security.UserHolder;
import com.dev.swms.server.model.base.Structure;
import com.dev.swms.server.model.fee.Fee;
import com.dev.swms.server.model.fee.FeeRate;
import com.dev.swms.server.model.fee.FeeType;
import com.dev.swms.server.model.fee.FeeUnit;
import com.dev.swms.server.model.receiving.ASN;
import com.dev.swms.server.model.shipping.PickTicket;
import com.dev.swms.server.service.fee.FeeManager;
import com.dev.swms.server.utils.DateUtil;
import com.dev.swms.server.utils.DoubleUtils;



public class DefaultFeeManager extends DefaultBaseManager implements FeeManager{

	/**保存对账单信息*/
	@Override
	public void autoCreateFeeRate() {
		System.out.println("join.....");
		Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
//		calendar.add(Calendar.YEAR, -1);//当前时间减去一年，即一年前的时间
//		calendar.add(Calendar.MONTH, -1);//当前时间减去一个月，即一个月前的时间
		calendar.add(Calendar.DAY_OF_MONTH,-1); //当前时间减去一天，即一天前的时间
		String fd="2020-04-18";//DateUtil.formatDateYMDToStr(calendar.getTime());//"2019-10-01";//
		Long companyId=new Long("1162");
		List<String> feeDates=new ArrayList<String>();
//		for(int i=0;i<29;i++){
//			Calendar c = Calendar.getInstance();
//			c.setTime(DateUtil.formatStrToDateYMD("2020-02-01"));
//			c.add(Calendar.DAY_OF_MONTH,i);
//			feeDates.add(DateUtil.formatDateYMDToStr(c.getTime()));
//		}
		feeDates.add("2020-04-18");
		companyId=null;
		for(String feeDate:feeDates){
			Session session = ((HibernateDaoSupport)this.commonDao).getSessionFactory().openSession();
			String h="from Fee f where to_char(f.feeDate,'yyyy-mm-dd')='"+feeDate+"' and f.creatPeople='sys'";
			if(companyId!=null){
				h+= " and  f.company.id="+companyId; 
			}
			List<Fee> fs=this.commonDao.findByQuery(h);
			this.commonDao.deleteAll(fs);
			String selectLoc="SELECT st.id,loc.RACK_TYPE,count(distinct(his.LOCATION_id)) loc_count,"
					+ "round(sum(his.QUANTITY/item.CA_COEFFICIENT),4) QUANTITY,"
					+ "round(sum(his.QUANTITY*item.length*item.width*item.height/item.CA_COEFFICIENT),4) VOLUME,"
//					+ "round(sum(his.QUANTITY*item.volume/item.CA_COEFFICIENT),4) VOLUME,"
					+ "round(sum(his.QUANTITY*item.WEIGHT/item.CA_COEFFICIENT),4) WEIGHT,"
					+ "to_char(max(his.CREATE_DATE),'yyyy-mm-dd')  create_date "
					//+ "(select nvl(price,0) from FEE_RATE fr where fr.COMPANY_ID=st.id and fr.type=loc.RACK_TYPE) as loc_price,"
					//+ "(select nvl(feename_id,0) from FEE_RATE fr where fr.COMPANY_ID=st.id and fr.type=loc.RACK_TYPE) as fee_name"
					+ " from INVENTORY_HIS his "
					+ " left join ITEM_KEY ik on ik.id=his.ITEM_KEY_id"
					+ " LEFT JOIN item item on item.id=ik.item_id"
					+ " left join STRUCTURE st on st.id=item.COMPANY_ID"
					+ " left join LOCATION loc on loc.id=his.LOCATION_id"
					+ " where 1=1 and to_char(his.CREATE_DATE,'yyyy-mm-dd')='"+feeDate+"' and his.QUANTITY>0 ";
			if(companyId!=null){
				selectLoc+= " and  st.id="+companyId; 
			}
			selectLoc+= " GROUP BY st.id ,loc.RACK_TYPE,to_char(his.CREATE_DATE,'yyyy-mm-dd')"
			+ " order by st.id  ";
	//		String selectCus="SELECT st.id,z.name,count(distinct(his.LOCATION_id)) loc_count,"
	//				+ "round(sum(his.QUANTITY/item.CA_COEFFICIENT),4) QUANTITY,"
	//				+ "round(sum(his.QUANTITY*item.VOLUME/item.CA_COEFFICIENT),4) VOLUME,"
	//				+ "round(sum(his.QUANTITY*item.WEIGHT/item.CA_COEFFICIENT),4) WEIGHT,"
	//				+ "to_char(his.CREATE_DATE,'yyyy-mm-dd')  create_date"
	//				+ "from STRUCTURE st "
	//				+ "LEFT JOIN item item on item.COMPANY_ID=st.id "
	//				+ "left join ITEM_KEY ik on ik.item_id=item.id"
	//				+ "left join INVENTORY_HIS his on his.ITEM_KEY_ID =ik.id and his.CREATE_DATE>sysdate-1  and his.QUANTITY>0"
	//				+ "left join LOCATION loc on loc.id=his.LOCATION_id"
	//				+ "left join zone z on z.id=loc.ZONE_ID"
	//				+ "GROUP BY st.id ,z.NAME,to_char(his.CREATE_DATE,'yyyy-mm-dd')"
	//				+ "order by st.id  ";
	//		
	//		String selectItem="SELECT st.id,round(sum(his.QUANTITY/item.CA_COEFFICIENT),4) QUANTITY,round(sum(his.QUANTITY*item.VOLUME/item.CA_COEFFICIENT),4) VOLUME,"+
	//				" round(sum(his.QUANTITY*item.WEIGHT/item.CA_COEFFICIENT),4) WEIGHT,"+
	//				" to_char(his.CREATE_DATE,'yyyy-mm-dd')  create_date from INVENTORY_HIS his"+
	//				" left join ITEM_KEY ik on ik.id=his.ITEM_KEY_ID"+
	//				" left join item item on item.id=ik.item_id "+
	//				" left join STRUCTURE st on st.id=item.company_id"+
	//				" where his.CREATE_DATE>sysdate-1  and his.QUANTITY>0"+
	//				" GROUP BY st.id ,to_char(his.CREATE_DATE,'yyyy-mm-dd')";
			List<Long> sts =this.commonDao.findByQuery("select id from Structure where disabled=0");
			Query   queryLoc = session.createSQLQuery(selectLoc);
			List<Object[]> listLoc= (List<Object[]>)queryLoc.list();
			FeeType ftcw=commonDao.get(FeeType.class,new Long("1060"));
			FeeType fthw=commonDao.get(FeeType.class,new Long("1002"));;
			if(listLoc.size()>0){
				for(Object[] arrier:listLoc){
					int i=0;
	//				//货主、库位类型、库位数、数量、体积、重量、日期、库位单价、费用名称
					Long st_id=Long.parseLong(arrier[i++].toString());
					String zone_name=arrier[i++].toString();
					int loc_count=Integer.parseInt(arrier[i++].toString());
					i++;
					double volume=Double.parseDouble(arrier[i++].toString());
					i++;
					Date create_date=DateUtil.formatStrToDateYMD(arrier[i++].toString());
					FeeType ft=null;
					String hql="from FeeRate r where r.company.id="+st_id;
					if(zone_name.equals("HW")){
						hql+=" and r.feeName.name='恒温库位仓储费'";
						ft=fthw;
					}else{
						hql+=" and r.feeName.name='常温库位仓储费'";
						ft=ftcw;
					}
					Structure st=this.commonDao.get(Structure.class, st_id);
					//按库位
					List<FeeRate> fr1=this.commonDao.findByQuery(hql+ " and r.type='"+zone_name+"' order by id desc");
					double price1=0;
					if(fr1.size()>0){
						price1=fr1.get(0).getPrice();
					}
					createFee(st, ft, zone_name, loc_count, price1, create_date, "sys");
					
					//按体积
					price1=0;
					fr1=this.commonDao.findByQuery(hql+ " and r.type='"+FeeUnit.VOLUME+"' order by id desc");
					if(fr1.size()>0){
						price1=fr1.get(0).getPrice();
					}
					createFee(st, ft, FeeUnit.VOLUME, volume, price1, create_date, "sys");
				}
			}else{
				for(Long id :sts){
					Date create_date=DateUtil.formatStrToDateYMD(feeDate);
					Structure st=this.commonDao.get(Structure.class, id);
					//常温 库位、体积
					String hql="from FeeRate r where r.company.id="+id+" and r.feeName.id="+ftcw.getId();
					//按库位
					List<FeeRate> fr1=this.commonDao.findByQuery(hql+ "and r.type='CW' order by id desc");
					double price1=0;
					if(fr1.size()>0){
						price1=fr1.get(0).getPrice();
					}
					createFee(st, ftcw, "CW", 0, price1, create_date, "sys");
					
					//按体积
					price1=0;
					fr1=this.commonDao.findByQuery(hql+ "and r.type='"+FeeUnit.VOLUME+"' order by id desc");
					if(fr1.size()>0){
						price1=fr1.get(0).getPrice();
					}
					createFee(st, ftcw, FeeUnit.VOLUME, 0, price1, create_date, "sys");
					
					
					//恒温 库位、体积
					hql="from FeeRate r where r.company.id="+id+" and r.feeName.id="+fthw.getId();
					//按库位
					price1=0;
					fr1=this.commonDao.findByQuery(hql+ "and r.type='HW' order by id desc");
					if(fr1.size()>0){
						price1=fr1.get(0).getPrice();
					}
					createFee(st, fthw, "HW", 0, price1, create_date, "sys");
					
					//按体积
					price1=0;
					fr1=this.commonDao.findByQuery(hql+ "and r.type='"+FeeUnit.VOLUME+"' order by id desc");
					if(fr1.size()>0){
						price1=fr1.get(0).getPrice();
					}
					createFee(st, fthw, FeeUnit.VOLUME, 0, price1, create_date, "sys");
					
				}
			}
			session.close();
			System.out.println("end.....");
		}
	}
	
	
	/**保存对账单信息*/
	@Override
	public void autoCreateFee() {
		System.out.println("join.....");
		List<String> feeDates=new ArrayList<String>();
		Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
//		calendar.add(Calendar.YEAR, -1);//当前时间减去一年，即一年前的时间
//		calendar.add(Calendar.MONTH, -1);//当前时间减去一个月，即一个月前的时间
		calendar.add(Calendar.DAY_OF_MONTH,-1); //当前时间减去一天，即一天前的时间
		String fd=DateUtil.formatDateYMDToStr(calendar.getTime());//"2019-10-01";//
		Long companyId=new Long("1162");
//		for(int i=0;i<1;i++){
//			Calendar c = Calendar.getInstance();
//			c.setTime(DateUtil.formatStrToDateYMD("2020-06-24"));
//			c.add(Calendar.DAY_OF_MONTH,i);
//			feeDates.add(DateUtil.formatDateYMDToStr(c.getTime()));
//		}
		feeDates.add(fd);
		companyId=null;
		for(String feeDate:feeDates){
			Session session = ((HibernateDaoSupport)this.commonDao).getSessionFactory().openSession();
			String h=" from Fee f where to_char(f.feeDate,'yyyy-mm-dd')='"+feeDate+"' and f.creatPeople='sys'";
			if(companyId!=null){
				h+= " and  f.company.id="+companyId; 
			}
			List<Fee> fs=this.commonDao.findByQuery(h);
			this.commonDao.deleteAll(fs);
			String hql=" select v.id,v.QUANTITY,v.hw_VOLUME,v.cw_VOLUME,v.VOLUME,v.VOLUME2,v.hw_weight,v.cw_weight,v.weight,"
			      +"nvl(v.hw_loc_count,0) hw_loc_count, nvl(v.cw_loc_count,0) cw_loc_count,"
			      +"v.hwkw,v.cwkw,v.hwtj,v.cwtj,v.hwzl,v.cwzl,v.CREATE_DATE"
			      +" from inv_cff_view v where 1=1 ";
		    if(companyId!=null){
				hql+= " and  v.id="+companyId; 
			}
		    hql+=" and v.CREATE_DATE='"+feeDate+"'";
			Query   queryLoc = session.createSQLQuery(hql);
			List<Object[]> listLoc= (List<Object[]>)queryLoc.list();
			FeeType ftcw=commonDao.get(FeeType.class,new Long("1060"));//常温
			FeeType fthw=commonDao.get(FeeType.class,new Long("1002"));;//恒温
			if(listLoc.size()>0){
				for(Object[] arrier:listLoc){
					int i=0;
					//	货主、数量、体积、外箱体积、重量、恒温库位数、常温库位数、常温单价、恒温单价、体积单价、重量单价、日期
					Long st_id=Long.parseLong(arrier[i++].toString());
					double quantity=Double.parseDouble(arrier[i++].toString());
					double hw_volume=Double.parseDouble(arrier[i++].toString());
					double cw_volume=Double.parseDouble(arrier[i++].toString());
					double volume=Double.parseDouble(arrier[i++].toString());
					double volume2=Double.parseDouble(arrier[i++].toString());
					double hw_weight=Double.parseDouble(arrier[i++].toString());
					double cw_weight=Double.parseDouble(arrier[i++].toString());
					double weight=Double.parseDouble(arrier[i++].toString());
					int hw_loc_count=Integer.parseInt(arrier[i++].toString());
					int cw_loc_count=Integer.parseInt(arrier[i++].toString());
					double hwkwdj=Double.parseDouble(arrier[i++].toString());
					double cwkwdj=Double.parseDouble(arrier[i++].toString());
					double hwtjdj=Double.parseDouble(arrier[i++].toString());
					double cwtjdj=Double.parseDouble(arrier[i++].toString());
					double hwzldj=Double.parseDouble(arrier[i++].toString());
					double cwzldj=Double.parseDouble(arrier[i++].toString());
					Date create_date=DateUtil.formatStrToDateYMD(arrier[i++].toString());
					Structure st=this.commonDao.get(Structure.class, st_id);
//					FeeType ft=commonDao.get(FeeType.class,new Long("1060"));
//					hql="from FeeRate r where r.company.id="+st_id+" and r.feeName.id in(1002,1060) ";
					if(hw_loc_count>0){
						createFee(st, fthw, FeeUnit.HW, hw_loc_count, hwkwdj, create_date, "sys");
					}
					if(cw_loc_count>0){
						createFee(st, ftcw, FeeUnit.CW, cw_loc_count, cwkwdj, create_date, "sys");
					}
					if(hw_volume>0){
						createFee(st, fthw, FeeUnit.VOLUME, hw_volume, hwtjdj, create_date, "sys");
					}
					if(cw_volume>0){
						createFee(st, ftcw, FeeUnit.VOLUME, cw_volume, cwtjdj, create_date, "sys");
					}
					if(hw_weight>0){
						createFee(st, fthw, FeeUnit.WEIGHT, hw_weight, hwzldj, create_date, "sys");
					}
					if(cw_weight>0){
						createFee(st, ftcw, FeeUnit.WEIGHT, cw_weight, cwzldj, create_date, "sys");
					}
//					if(volume>0){
//						hql+=" and r.type='"+FeeUnit.VOLUME+"'";
//						List<FeeRate> rs=this.commonDao.findByQuery(hql);
//						if(rs.size()>0){
//							ft=rs.get(0).getFeeName();
//						}
//						createFee(st, ft, FeeUnit.VOLUME, volume, tjdj, create_date, "sys");
//					}
//					if(weight>0){
//						hql+=" and r.type='"+FeeUnit.WEIGHT+"'";
//						List<FeeRate> rs=this.commonDao.findByQuery(hql);
//						if(rs.size()>0){
//							ft=rs.get(0).getFeeName();
//						}
//						createFee(st, ft, FeeUnit.WEIGHT, weight, zldj, create_date, "sys");
//					}
				}
			}
			session.close();
			System.out.println("end.....");
		}
	}
	
	/**保存对账单信息*/
	@Override
	public void autoCreateFee(Date date) {
		System.out.println("join....."); 
		List<String> feeDates=new ArrayList<String>();
//		Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
//		calendar.add(Calendar.YEAR, -1);//当前时间减去一年，即一年前的时间
//		calendar.add(Calendar.MONTH, -1);//当前时间减去一个月，即一个月前的时间
//		calendar.add(Calendar.DAY_OF_MONTH,-1); //当前时间减去一天，即一天前的时间
		String fd=DateUtil.formatDateYMDToStr(date);//"2019-10-01";//
		Long companyId=new Long("1162");
//		for(int i=0;i<1;i++){
//			Calendar c = Calendar.getInstance();
//			c.setTime(DateUtil.formatStrToDateYMD("2020-06-24"));
//			c.add(Calendar.DAY_OF_MONTH,i);
//			feeDates.add(DateUtil.formatDateYMDToStr(c.getTime()));
//		}
		feeDates.add(fd);
		companyId=null;
		for(String feeDate:feeDates){
			Session session = ((HibernateDaoSupport)this.commonDao).getSessionFactory().openSession();
			String h=" from Fee f where to_char(f.feeDate,'yyyy-mm-dd')='"+feeDate+"' and f.creatPeople='sys'";
			if(companyId!=null){
				h+= " and  f.company.id="+companyId; 
			}
			List<Fee> fs=this.commonDao.findByQuery(h);
			this.commonDao.deleteAll(fs);
			String hql=" select v.id,v.QUANTITY,v.hw_VOLUME,v.cw_VOLUME,v.VOLUME,v.VOLUME2,v.hw_weight,v.cw_weight,v.weight,"
			      +"nvl(v.hw_loc_count,0) hw_loc_count, nvl(v.cw_loc_count,0) cw_loc_count,"
			      +"v.hwkw,v.cwkw,v.hwtj,v.cwtj,v.hwzl,v.cwzl,v.CREATE_DATE"
			      +" from inv_cff_view v where 1=1 ";
		    if(companyId!=null){
				hql+= " and  v.id="+companyId; 
			}
		    hql+=" and v.CREATE_DATE='"+feeDate+"'";
			Query   queryLoc = session.createSQLQuery(hql);
			List<Object[]> listLoc= (List<Object[]>)queryLoc.list();
			FeeType ftcw=commonDao.get(FeeType.class,new Long("1060"));//常温
			FeeType fthw=commonDao.get(FeeType.class,new Long("1002"));;//恒温
			if(listLoc.size()>0){
				for(Object[] arrier:listLoc){
					int i=0;
					//	货主、数量、体积、外箱体积、重量、恒温库位数、常温库位数、常温单价、恒温单价、体积单价、重量单价、日期
					Long st_id=Long.parseLong(arrier[i++].toString());
					double quantity=Double.parseDouble(arrier[i++].toString());
					double hw_volume=Double.parseDouble(arrier[i++].toString());
					double cw_volume=Double.parseDouble(arrier[i++].toString());
					double volume=Double.parseDouble(arrier[i++].toString());
					double volume2=Double.parseDouble(arrier[i++].toString());
					double hw_weight=Double.parseDouble(arrier[i++].toString());
					double cw_weight=Double.parseDouble(arrier[i++].toString());
					double weight=Double.parseDouble(arrier[i++].toString());
					int hw_loc_count=Integer.parseInt(arrier[i++].toString());
					int cw_loc_count=Integer.parseInt(arrier[i++].toString());
					double hwkwdj=Double.parseDouble(arrier[i++].toString());
					double cwkwdj=Double.parseDouble(arrier[i++].toString());
					double hwtjdj=Double.parseDouble(arrier[i++].toString());
					double cwtjdj=Double.parseDouble(arrier[i++].toString());
					double hwzldj=Double.parseDouble(arrier[i++].toString());
					double cwzldj=Double.parseDouble(arrier[i++].toString());
					Date create_date=DateUtil.formatStrToDateYMD(arrier[i++].toString());
					Structure st=this.commonDao.get(Structure.class, st_id);
//					FeeType ft=commonDao.get(FeeType.class,new Long("1060"));
//					hql="from FeeRate r where r.company.id="+st_id+" and r.feeName.id in(1002,1060) ";
					if(hw_loc_count>0){
						createFee(st, fthw, FeeUnit.HW, hw_loc_count, hwkwdj, create_date, "sys");
					}
					if(cw_loc_count>0){
						createFee(st, ftcw, FeeUnit.CW, cw_loc_count, cwkwdj, create_date, "sys");
					}
					if(hw_volume>0){
						createFee(st, fthw, FeeUnit.VOLUME, hw_volume, hwtjdj, create_date, "sys");
					}
					if(cw_volume>0){
						createFee(st, ftcw, FeeUnit.VOLUME, cw_volume, cwtjdj, create_date, "sys");
					}
					if(hw_weight>0){
						createFee(st, fthw, FeeUnit.WEIGHT, hw_weight, hwzldj, create_date, "sys");
					}
					if(cw_weight>0){
						createFee(st, ftcw, FeeUnit.WEIGHT, cw_weight, cwzldj, create_date, "sys");
					}
//					if(volume>0){
//						hql+=" and r.type='"+FeeUnit.VOLUME+"'";
//						List<FeeRate> rs=this.commonDao.findByQuery(hql);
//						if(rs.size()>0){
//							ft=rs.get(0).getFeeName();
//						}
//						createFee(st, ft, FeeUnit.VOLUME, volume, tjdj, create_date, "sys");
//					}
//					if(weight>0){
//						hql+=" and r.type='"+FeeUnit.WEIGHT+"'";
//						List<FeeRate> rs=this.commonDao.findByQuery(hql);
//						if(rs.size()>0){
//							ft=rs.get(0).getFeeName();
//						}
//						createFee(st, ft, FeeUnit.WEIGHT, weight, zldj, create_date, "sys");
//					}
				}
			}
			session.close();
			System.out.println("end.....");
		}
	}
	
	Fee createFee(Structure company,FeeType ft,String unit,double quantity,double price,Date feeDate,String creatPeople){
		Fee f =EntityFactory.getEntity(Fee.class);
		f.setCompany(company);
		f.setUnit(unit);
		f.setQuantity(DoubleUtils.format3Fraction(quantity));
		f.setPrice(DoubleUtils.format2Fraction(price));
		f.setFeeName(ft);
//		f.setPayOrReceive();
		f.setStatus("OPEN");
		f.setFeeDate(feeDate);
		f.setCreatDate(new Date());
		f.setCreatPeople(creatPeople);
		f.setFeeMenoy(DoubleUtils.format3Fraction(f.getQuantity()*f.getPrice()));
		this.commonDao.store(f);
		return f;
		
	}

	@Override
	public void savefee(Fee fee) {
		commonDao.store(fee);
	}

	@Override
	public void deleteAll(List<Long> ids) {
		for(Long id :ids){
			Fee fee =this.load(Fee.class, id);
			commonDao.delete(fee);
		}
		
	}
	
	@Override
	public void autoAsnFee(Long asnId) {
		ASN asn =this.commonDao.get(ASN.class, asnId);
		List<FeeRate> rs=this.commonDao.findByQuery("from FeeRate f where f.company.id="+asn.getCompany().getId()
				+" and f.node in ('入库','出入库')");
		for(FeeRate r:rs){
			double quantity=1;
			if(r.getType().equals(FeeUnit.WEIGHT)){
				quantity=asn.getWeight();
			}else if(r.getType().equals(FeeUnit.VOLUME)){
				quantity=asn.getVolume();
			}else if(r.getType().equals(FeeUnit.QUANTITY)){
				quantity=asn.getExpectedQuantity();
			}else if(r.getType().equals(FeeUnit.ZD)){
				quantity=1;
			}
			Fee f=createFee(asn.getCompany(), r.getFeeName(), r.getType(), quantity, r.getPrice(), new Date(), UserHolder.getUser().getName());
			f.setRemark(f.getFeeMenoy()+"入库"+asn.getCode());
			f.setPayOrReceive("应收");
			f.setType("ASN");
			f.setRateId(r.getId());
			f.setOrderId(asn.getId());
			f.setOrderCode(asn.getCode());
			this.commonDao.store(f);
			
		}
	}
	
	@Override
	public void autoPickFee(Long pickId) {
		PickTicket pt =this.commonDao.get(PickTicket.class, pickId);
		List<FeeRate> rs=this.commonDao.findByQuery("from FeeRate f where f.company.id="+pt.getCompany().getId()
				+" and f.node in ('出库','出入库')");
		for(FeeRate r:rs){
			double quantity=1;
			String unit=r.getType();
			if(r.getType().equals(FeeUnit.WEIGHT)){
				quantity=pt.getWeight();
			}else if(r.getType().equals(FeeUnit.VOLUME)){
				quantity=pt.getVolume();
			}else if(r.getType().equals(FeeUnit.QUANTITY)){
				quantity=pt.getPickedQuantity();
			}
			String remark="";
			if(r.getShipmentMethod()!=null){
				if(pt.getShipmentMethod()!=null&&!pt.getShipmentMethod().equals("")&&pt.getShipmentMethod().equals(r.getShipmentMethod())){
					remark="运输方式:"+pt.getShipmentMethod()+";";
				}else{
					continue;
				}
			}
			if(r.getPackType()!=null){
				if(pt.getPackType1()!=null&&!pt.getPackType1().equals("")&&pt.getPackType1().equals(r.getPackType())){
					remark="包装方式1:"+r.getPackType()+";";
					quantity=pt.getQuantity1();
					unit=FeeUnit.ZD;
				}else if(pt.getPackType2()!=null&&!pt.getPackType2().equals("")&&pt.getPackType2().equals(r.getPackType())){
					remark="包装方式2:"+r.getPackType()+";";
					quantity=pt.getQuantity2();
					unit=FeeUnit.ZD;
				}else if(pt.getPackType3()!=null&&!pt.getPackType3().equals("")&&pt.getPackType3().equals(r.getPackType())){
					remark="包装方式3:"+r.getPackType()+";";
					quantity=pt.getQuantity3();
					unit=FeeUnit.ZD;
				}else{
					continue;
				}
			}
			Fee f=createFee(pt.getCompany(), r.getFeeName(), r.getType(), quantity, r.getPrice(), new Date(), UserHolder.getUser().getName());
			f.setRemark(remark+f.getFeeMenoy()+"出库"+pt.getCode());
			f.setPayOrReceive("应收");
			f.setType("PickTicket");
			f.setRateId(r.getId());
			f.setOrderCode(pt.getCode());
			f.setOrderId(pt.getId());
			this.commonDao.store(f);
			
		}
	}
	
	@Override
	public void deleteBillFee(Long id,String type) {
		List<Fee> fs=this.commonDao.findByQuery("from Fee f where f.orderId="+id+" and type='"+type+"'");
		for(Fee f:fs){
			this.commonDao.delete(f);
		}
		
	}

}
