package com.gx.obe.evaluation.step.checker;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.gx.obe.business.vo.TaxFeesCheckResult;
import com.gx.obe.evaluation.step.inter.IChecker;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.web.entity.CheckResult;
import com.gx.obe.web.entity.boq.ConstructProject;
import com.gx.obe.web.entity.evaluation.EvaluationStep;

/**
 * @Description: 单项工程税金费率检查
 * @author guoyr
 */
public class SingleConstructionTaxFeesChecker extends BaseChecker implements IChecker {
	
//	private ConstructProjectService constructProjectService = ServiceFactory.getInstance("constructProjectService", ConstructProjectService.class);
	private List<TaxFeesCheckResult> checkResultTreeList = new ArrayList<TaxFeesCheckResult>();
	private List<TaxFeesCheckResult> checkResultErrorList = new ArrayList<TaxFeesCheckResult>();
	public static final BigDecimal ERROR_RATE = BigDecimal.valueOf(0.5);
	// 默认消项费率
	public static final BigDecimal OUTPUTTAX_RATE = BigDecimal.valueOf(9);
	public static final BigDecimal ERROR_PRICE = BigDecimal.valueOf(20);
	
	public void startCheck(ConstructProject constructProject) {
		BigDecimal ERROR_PRICE = evaluationStep.getEvaluationParamByKey(EvaluationStep.CALCULATION_ERROR_VALUE, BigDecimal.class);
		if (null == ERROR_PRICE || ERROR_PRICE.compareTo(BigDecimal.ZERO) == 0) {
			ERROR_PRICE = BigDecimal.valueOf(20);
		}
		checkResultTreeList.clear();
		checkResultErrorList.clear();
		checkResult = null;
		// 获得工程项目
		if (null != constructProject) {
			// 获得单项工程
			List<ConstructProject> singleConstructionList = constructProject.getChildProjectList();
			if (null != singleConstructionList) {//
				for (ConstructProject singleConstruction : singleConstructionList) {
					
//					TaxFeesCheckResult singleResult = new TaxFeesCheckResult();
//					singleResult.setName(singleConstruction.getProjectName());
//					checkResultTreeList.add(singleResult);
					// 获得单位工程
					List<ConstructProject> unitConstructionList = singleConstruction.getChildProjectList();
					BigDecimal sumPreTaxCost = BigDecimal.ZERO;
					BigDecimal sumInputTax = BigDecimal.ZERO;
					BigDecimal sumOutputTax = BigDecimal.ZERO;
					BigDecimal sumValueAddedTax = BigDecimal.ZERO;
					BigDecimal sumSurchargeTax = BigDecimal.ZERO;
					BigDecimal sumTaxes = BigDecimal.ZERO;
					BigDecimal sumSelfSupplyFee = null;
					if (null != unitConstructionList) {
						for (ConstructProject unitConstruction : unitConstructionList) {
							if (null == singleConstruction.getSurchargeTaxRate()) {
								singleConstruction.setSurchargeTaxRate(unitConstruction.getSurchargeTaxRate());
							}
							// 重新汇总单位工程的税前工程造价、进项税额、销项税额、应纳增值税额、附加税费、税金、甲供材料设备（设备费单位工程没有不能汇总）
							sumPreTaxCost = BigDecimalUtils.add(sumPreTaxCost, unitConstruction.getPreTaxCost());
							sumInputTax = BigDecimalUtils.add(sumInputTax, unitConstruction.getInputTax());
							sumOutputTax = BigDecimalUtils.add(sumOutputTax, unitConstruction.getOutputTax());
							sumValueAddedTax = BigDecimalUtils.add(sumValueAddedTax, unitConstruction.getValueAddedTax());
							sumSurchargeTax = BigDecimalUtils.add(sumSurchargeTax, unitConstruction.getSurchargeTax());
							sumTaxes = BigDecimalUtils.add(sumTaxes, unitConstruction.getTaxes());
							sumSelfSupplyFee = BigDecimalUtils.add(sumSelfSupplyFee, unitConstruction.getSelfSupplyFee());
						}
						singleConstruction.setPreTaxCost(sumPreTaxCost);
						singleConstruction.setInputTax(sumInputTax);
						singleConstruction.setOutputTax(sumOutputTax);
						singleConstruction.setValueAddedTax(sumValueAddedTax);
						singleConstruction.setSurchargeTax(sumSurchargeTax);
						singleConstruction.setTaxes(sumTaxes);
						singleConstruction.setSelfSupplyFee(sumSelfSupplyFee);
					}
					
					// 现在税金按照单项工程清   销项税的计算公示变为 税前工程造价+设备费-甲供材-进项税
					TaxFeesCheckResult unitResult = new TaxFeesCheckResult();
					unitResult.setName(singleConstruction.getProjectName());
					// 检查规费
					unitResult.setPreTaxCost(singleConstruction.getPreTaxCost());
					unitResult.setEquipmentFee(singleConstruction.getEquipmentFee());
					unitResult.setSelfSupplyFee(singleConstruction.getSelfSupplyFee());
					unitResult.setInputTax(singleConstruction.getInputTax());
					unitResult.setOutputTax(singleConstruction.getOutputTax());
					// 销项税额计算基数 税前工程造价+设备费-甲供材-进项税
					BigDecimal outputTaxCount = BigDecimalUtils.add(singleConstruction.getPreTaxCost(), singleConstruction.getEquipmentFee());
					outputTaxCount = BigDecimalUtils.sub(outputTaxCount, singleConstruction.getSelfSupplyFee());
					outputTaxCount = BigDecimalUtils.sub(outputTaxCount, singleConstruction.getInputTax());
					// 以前为税前工程造价-进项税
//					BigDecimalUtils.sub(unitConstruction.getPreTaxCost(), unitConstruction.getInputTax());
					BigDecimal outputTaxRate = BigDecimalUtils.mul(BigDecimalUtils.div(singleConstruction.getOutputTax(), outputTaxCount), BigDecimal.valueOf(100));
//					outputTaxRate = BigDecimalUtils.round(outputTaxRate, 2);
					unitResult.setOutputTaxRate(outputTaxRate);
					boolean reasonable = true;
					//销项税额费率应为固定值9%
					if (null != outputTaxRate && outputTaxRate.compareTo(BigDecimal.ZERO) > 0) {
						if (BigDecimalUtils.difference(outputTaxRate, SingleConstructionTaxFeesChecker.OUTPUTTAX_RATE).compareTo(SingleConstructionTaxFeesChecker.ERROR_RATE) > 0) {
							reasonable = false;
//							unitResult.setOpinion("销项税额费率未按9%计取");
							unitResult.setOpinion("请校验销项税额费率计取情况");
						}
					}
					
					// 增值税应纳税额 = 销项税额-进项税额   
					unitResult.setValueAddedTax(singleConstruction.getValueAddedTax());
					BigDecimal valueAddedTaxCount = BigDecimalUtils.sub(singleConstruction.getOutputTax(), singleConstruction.getInputTax());
					// 增值税应纳税额 = 销项税额-进项税额   ，但汇总到单项工程后再计算就会有问题，招标通要求直接使用原始值。
					valueAddedTaxCount = singleConstruction.getValueAddedTax();
					if (null == valueAddedTaxCount || valueAddedTaxCount.compareTo(BigDecimal.ZERO) < 0) {
						valueAddedTaxCount = BigDecimal.ZERO;
					}
					
					unitResult.setSurchargeTaxRate(singleConstruction.getSurchargeTaxRate());//附加税费费率
					// 附加税费 = 增值税应纳税额*附加税费费率  ， 其中：增值税应纳税额 = 销项税额-进项税额   
					unitResult.setSurchargeTax(singleConstruction.getSurchargeTax());
					BigDecimal calculateSurchargeTax = singleConstruction.getSurchargeTax();
					if (null != singleConstruction.getSurchargeTaxRate()) {
						calculateSurchargeTax = BigDecimalUtils.div(BigDecimalUtils.mul(valueAddedTaxCount, singleConstruction.getSurchargeTaxRate()), BigDecimal.valueOf(100));
//						calculateSurchargeTax = BigDecimalUtils.round(calculateSurchargeTax, 2);
						unitResult.setCalculateSurchargeTax(calculateSurchargeTax);
					}
					
					unitResult.setTaxes(singleConstruction.getTaxes());
					// 税金 = 增值税应纳税额+附加税费
					BigDecimal calculateTaxes = BigDecimalUtils.add(valueAddedTaxCount, calculateSurchargeTax);
//					calculateTaxes = BigDecimalUtils.round(calculateTaxes, 2);
					if (BigDecimalUtils.difference(calculateTaxes, singleConstruction.getTaxes()).compareTo(SingleConstructionTaxFeesChecker.ERROR_PRICE) > 0) {
						reasonable = false;
//						if(null != unitResult.getOpinion()){
//							unitResult.setOpinion(unitResult.getOpinion() +";附加税费未按规定费率计取");
//						}else {
//							
//							unitResult.setOpinion("附加税费未按规定费率计取");
//						}
						if (null != unitResult.getOpinion()) {
							unitResult.setOpinion(unitResult.getOpinion() + ";请校验附加税费率计取情况");
						} else {
							
							unitResult.setOpinion("请校验附加税费率计取情况");
						}
					}
					unitResult.setCalculateTaxes(calculateTaxes);
					if (reasonable) {
						unitResult.setOpinion("按规定费率计取");
					}
					unitResult.setReasonable(reasonable);
					checkResultTreeList.add(unitResult);
//					singleResult.addChild(unitResult);
					if (!reasonable) {
						checkResultErrorList.add(unitResult);
					}
					
				}
				
			}
			
		}
	}
	
	public int getErrorCount() {
		return checkResultErrorList.size();
	}
	
	@SuppressWarnings("unchecked")
	public <T> List<T> getCheckResultDetailTreeList() {
		return (List<T>) checkResultTreeList;
	}
	
	@SuppressWarnings("unchecked")
	public <T> List<T> getCheckResultErrorList() {
		return (List<T>) checkResultErrorList;
	}
	
	public CheckResult getCheckResult() {
		if (null != checkResult) return checkResult;
		checkResult = initCheckResult(checkResult);
		if (checkResultErrorList.size() > 0) {
			checkResult.setErrorNum(checkResultErrorList.size());
			checkResult.setAccessment(com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED);
			StringBuilder error = new StringBuilder();
			error.append(checkResultErrorList.size() + "个单项工程下的税金费率未按照规定费率计取");
			checkResult.setCheckResult(error.toString());
		} else {
			checkResult.setErrorNum(0);
			checkResult.setAccessment(com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED);
			checkResult.setCheckResult("无错误。");
		}
		
		return checkResult;
	}
	
}
