//qJRlc7FtDOzcCIkwPKPHUuV3NR8WU3EAYp34/LyBR8gKfJoJgj7UlsAsk8I0Ettn
package nc.vo.arap.pay.vochange;

import nc.itf.arap.fieldmap.IBillFieldGet;
import nc.vo.arap.basebill.BaseItemVO;
import nc.vo.arap.vochange.ArapBillVOChange;
import nc.vo.arappub.calculator.data.RelationItemForCal_Debit;
import nc.vo.pf.change.ChangeVOAdjustContext;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDouble;
import nccloud.commons.lang.StringUtils;
import nc.vo.pub.BusinessException;

/**
 * F3 VO对照前后处理
 * @author wuzhwa
 * @since v6.0 *
 */
public class ArapF3VOChange extends ArapBillVOChange {
	/**
	 * 构造函数
	 */
	public ArapF3VOChange(){
		
		super.local_money=IBillFieldGet.LOCAL_MONEY_DE;//"local_money_cr";
		super.strChange=IBillFieldGet.MONEY_DE;//"money_cr";
		super.syscode= 1;// BillMathCalculator.getProior定义 值 0:应收系统,1:应付;貌似现在这个有问题，返回都是true;
		super.relationforitem = RelationItemForCal_Debit.getInstance();
	}
	
	public AggregatedValueObject adjustAfterChange(AggregatedValueObject srcVO, AggregatedValueObject destVO, ChangeVOAdjustContext adjustContext) throws BusinessException {
		AggregatedValueObject adjustAfterChange = super.adjustAfterChange(srcVO, destVO, adjustContext);
		
		if(((BaseItemVO)(destVO.getChildrenVO()[0])).getTop_billtype()!=null &&
				(IBillFieldGet.F0.equals(((BaseItemVO)(destVO.getChildrenVO()[0])).getTop_billtype())
						||IBillFieldGet.F2.equals(((BaseItemVO)(destVO.getChildrenVO()[0])).getTop_billtype())
						)){
			return adjustAfterChange;
		}
		
		//add by zhengyao3@郑瑶 如果是物业付款单的话，也不做表体行的合并处理 2019年3月19日16:51:04 begin
		String desTradeType = (String) destVO.getParentVO().getAttributeValue(IBillFieldGet.PK_TRADETYPE);
		String wyPaybill = "F3-Cxx-W01";
		/**
		 * 对外账扣付款单(物业)
		 */
		String WY_ZKFKD = "F3-Cxx-W04";
		/**
		 * 报账付款单（物业）
		 */
		String WY_FKD = "F3-Cxx-W02";
		//add by zhengyao3@郑瑶 增加物业的付款单和物业的账扣付款单 2019年7月8日16:33:23 
		if(StringUtils.isNotBlank(desTradeType) && (wyPaybill.equals(desTradeType) 
				|| WY_ZKFKD.equals(desTradeType) 
				|| WY_FKD.equals(desTradeType))){
			/**
			 * add by 符耀铭 2019-8-30 合并报账补丁代码  begin
			 */
			if(wyPaybill.equals(desTradeType)){
				computeMnyToScale(srcVO,adjustAfterChange);//add by 符耀铭 2019-4-17 物业付款单拉物业应付单表体 借方原币金额 通过比例计算出来
			}
			/**
			 * add by 符耀铭 2019-8-30 合并报账补丁代码  end
			 */
			return adjustAfterChange;
		}
		return adjustAfterChange;
	}
	
	/**二开新加方法，物业付款单拉物业应付单表体 借方原币金额 通过比例计算出来
	 * @author 符耀铭
	 * @time 2019-5-7
	 */
	private void computeMnyToScale(AggregatedValueObject srcVO,AggregatedValueObject adjustAfterChange){
		UFDouble mny=new UFDouble(srcVO.getParentVO().getAttributeValue("money").toString());
		//def108 实付金额 def13
		UFDouble sfmny=srcVO.getParentVO().getAttributeValue("def13")==null?UFDouble.ZERO_DBL:new UFDouble(srcVO.getParentVO().getAttributeValue("def13").toString());
		//def109 是否含质保金付款 def14
		Object iszhibaojin =srcVO.getParentVO().getAttributeValue("def14");
		UFBoolean ufiszhibaojin=iszhibaojin==null?UFBoolean.FALSE:new UFBoolean(iszhibaojin.toString());
		if(ufiszhibaojin.booleanValue()){
			/**
			 * modify by 符耀铭 2019-7-8 处理小数尾差问题  begin
			 */
			BaseItemVO[] body=(BaseItemVO[]) adjustAfterChange.getChildrenVO();
			UFDouble sumDouble=UFDouble.ZERO_DBL.setScale(2, UFDouble.ROUND_HALF_UP);
			UFDouble computDouble=UFDouble.ZERO_DBL.setScale(2, UFDouble.ROUND_HALF_UP);
			for(int i=0;i<body.length;i++){
				if(body.length==1 || (i+1)==body.length){
					UFDouble dj =sfmny.sub(sumDouble).setScale(2, UFDouble.ROUND_HALF_UP);
					body[i].setMoney_de(dj);
					body[i].setMoney_bal(dj);
					body[i].setLocal_money_bal(dj);
					body[i].setSettlemoney(dj);
					UFDouble computeTax =body[i].getMoney_de().multiply(body[i].getTaxrate()).div(new UFDouble(100).add(body[i].getTaxrate()));
					//def69 税额 def28
					body[i].setAttributeValue("def28", computeTax);
					body[i].setNotax_de(body[i].getMoney_de().sub(computeTax));
					body[i].setLocal_notax_de(body[i].getMoney_de().sub(computeTax));
					//sumDouble =sumDouble.add(sfmny.sub(sumDouble));
					continue;
				}
				computDouble=body[i].getMoney_de().multiply(sfmny.div(mny)).setScale(2, UFDouble.ROUND_HALF_UP);
				sumDouble =sumDouble.add(computDouble);
				body[i].setMoney_de(computDouble);
				body[i].setMoney_bal(computDouble);
				body[i].setLocal_money_bal(computDouble);
				body[i].setSettlemoney(computDouble);
				UFDouble computeTax =body[i].getMoney_de().multiply(body[i].getTaxrate()).div(new UFDouble(100).add(body[i].getTaxrate()));
				body[i].setAttributeValue("def28", computeTax);
				body[i].setNotax_de(body[i].getMoney_de().sub(computeTax));
				body[i].setLocal_notax_de(body[i].getMoney_de().sub(computeTax));
				/**
				 * modify by 符耀铭 2019-7-8 处理小数尾差问题  end
				 */
			}
			adjustAfterChange.getParentVO().setAttributeValue("money",sumDouble);
		}
	}
}

//qJRlc7FtDOzcCIkwPKPHUuV3NR8WU3EAYp34/LyBR8gKfJoJgj7UlsAsk8I0Ettn