package nc.ui.pu.m25.editor.card.afteredit.body;
import nc.bs.framework.common.NCLocator;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.BeanProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.uap.ctrl.tpl.qry.meta.DefaultConstEnum;
import nc.ui.pub.bill.BillCardPanel;
import nc.ui.bd.pubinfo.BillItem;
import nc.ui.cm.costtrandef.RefMoreSelectedUtils;
import nc.ui.pu.pub.editor.card.listener.ICardBodyAfterEditEventListener;
import nc.ui.pub.beans.UIRefPane;
import nc.ui.pubapp.uif2app.event.card.CardBodyAfterEditEvent;
import nc.vo.bd.defdoc.DefdocVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;

public class Vbdef20 implements ICardBodyAfterEditEventListener {
	IUAPQueryBS iuap=(IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());

	@Override
	public void afterEdit(CardBodyAfterEditEvent arg0) {
		UIRefPane ref=(UIRefPane) arg0.getBillCardPanel().getBodyItem("vbdef20").getComponent();
		String[] pks = ref.getRefPKs();
		StringBuffer sbf = new StringBuffer();
		// 处理PK
		if (pks == null || pks.length <= 0) {
			return;
//			sbf.append(",");
		} else {
			for (int i = 0; i < pks.length; i++) {
				sbf.append(","+pks[i]);
			}
		}
		
		//存放PK
		arg0.getBillCardPanel().setBodyValueAt(sbf.substring(1), arg0.getRow(), "vbdef20");
		UFDouble SumAmount = new UFDouble(0);//不含税
		UFDouble SumTotal = new UFDouble(0);//价税合计
		UFDouble SumTax = new UFDouble(0);//税额
		String shuilv = "";//记录第一条税率，和后面进行对比，若不同则报错不允许保存
		Object shuilvpk = "";
		if(pks.length > 1){			
			for(int j = 0;j < pks.length;j++){
				String pk = pks[j];
				String qrysql = "select * from bd_defdoc where dr=0 and pk_defdoc = '"+pk+"'";
				DefdocVO defdocvo;
				try {
					defdocvo = (DefdocVO) iuap.executeQuery(qrysql, new BeanProcessor(DefdocVO.class));
					Object shortname6 = defdocvo.getShortname6();
					if(shortname6 == null){
						ExceptionUtils.wrappBusinessException("所选发票税率为空，不能保存！");
						return;
					}
					if(j == 0 && shortname6 != null){
						shuilv = shortname6.toString();
					}
					if(j != 0 && !shortname6.toString().equals(shuilv)){
						ExceptionUtils.wrappBusinessException("所选发票税率不同，不能保存！");
						return;
					}
					
				} catch (BusinessException e) {
					// TODO 自动生成的 catch 块
					ExceptionUtils.wrappBusinessException(e.getMessage());
				}
			}
		}
		
		
		for(int i = 0;i < pks.length;i++){
			String pk = pks[i];
			String qrysql = "select * from bd_defdoc where dr=0 and pk_defdoc = '"+pk+"'";
			DefdocVO defdocvo;
			try {
				defdocvo = (DefdocVO) iuap.executeQuery(qrysql, new BeanProcessor(DefdocVO.class));
				String qryshuilv = "select pk_taxcode from bd_taxrate where nvl(dr,0) = 0 and pk_taxcode not in (select pk_taxcode from bd_taxcode where code = '0') and taxrate = '"+defdocvo.getShortname6()+"'";
				try {
					shuilvpk = iuap.executeQuery(qryshuilv, new ColumnProcessor());
					if(shuilvpk == null){
						ExceptionUtils.wrappBusinessException("税率没有对应税码，请检查！");
						return;
					}
				} catch (BusinessException e1) {
					// TODO 自动生成的 catch 块
					ExceptionUtils.wrappBusinessException(e1.getMessage());
				}
				arg0.getBillCardPanel().setHeadItem("vbillcode", defdocvo.getName());//发票号
				arg0.getBillCardPanel().setHeadItem("vdef19", defdocvo.getName4());//开票方
				arg0.getBillCardPanel().setHeadItem("dbilldate", new UFDate(defdocvo.getShortname4()));//发票日期
				arg0.getBillCardPanel().setBodyValueAt(defdocvo.getShortname5(), 0, "vbdef17");//发票分类
				arg0.getBillCardPanel().setBodyValueAt(defdocvo.getShortname6(), 0, "ntaxrate");//税率
				arg0.getBillCardPanel().setBodyValueAt(shuilvpk, 0, "ctaxcodeid");//税码
				Object shortname = defdocvo.getShortname();
				if(shortname != null && shortname.toString() != ""){
					SumAmount = SumAmount.add(new UFDouble(shortname.toString()));
				}
				Object shortname2 = defdocvo.getShortname2();
				if(shortname2 != null && shortname2.toString() != ""){
					SumTotal = SumTotal.add(new UFDouble(shortname2.toString()));
				}
				Object shortname3 = defdocvo.getShortname3();
				if(shortname3 != null && shortname3.toString() != ""){
					SumTax = SumTax.add(new UFDouble(shortname3.toString()));
				}
			} catch (BusinessException e) {
				// TODO 自动生成的 catch 块
				ExceptionUtils.wrappBusinessException(e.getMessage());
			}
		}
		arg0.getBillCardPanel().setBodyValueAt(1, arg0.getRow(), "nastnum");//数量
		arg0.getBillCardPanel().setBodyValueAt(1, arg0.getRow(), "nnum");//主数量
		arg0.getBillCardPanel().setBodyValueAt(SumAmount, arg0.getRow(), "nastorigprice");//无税单价
		arg0.getBillCardPanel().setBodyValueAt(SumAmount, arg0.getRow(), "nastprice");//本币无税单价
		arg0.getBillCardPanel().setBodyValueAt(SumAmount, arg0.getRow(), "norigprice");//主无税单价
		arg0.getBillCardPanel().setBodyValueAt(SumAmount, arg0.getRow(), "nprice");//主本币无税单价
		arg0.getBillCardPanel().setBodyValueAt(SumAmount, arg0.getRow(), "norigmny");//无税金额
		arg0.getBillCardPanel().setBodyValueAt(SumAmount, arg0.getRow(), "nmny");//本币无税金额
		arg0.getBillCardPanel().setBodyValueAt(SumTotal, arg0.getRow(), "nastorigtaxprice");//含税单价
		arg0.getBillCardPanel().setBodyValueAt(SumTotal, arg0.getRow(), "norigtaxprice");//主含税单价
		arg0.getBillCardPanel().setBodyValueAt(SumTotal, arg0.getRow(), "nasttaxprice");//本币含税单价
		arg0.getBillCardPanel().setBodyValueAt(SumTotal, arg0.getRow(), "ntaxprice");//主本币含税单价
		arg0.getBillCardPanel().setBodyValueAt(SumTotal, arg0.getRow(), "norigtaxmny");//价税合计
		arg0.getBillCardPanel().setBodyValueAt(SumTotal, arg0.getRow(), "ntaxmny");//本币价税合计
		arg0.getBillCardPanel().setBodyValueAt(SumTax, arg0.getRow(), "ntax");//税额
		arg0.getBillCardPanel().setBodyValueAt(SumTotal, arg0.getRow(), "ncalcostmny");//计成本金额
		
		
		
		ref.setPKs(pks);
//		DefaultConstEnum refEnum = new DefaultConstEnum(sbf.toString().substring(0, sbf.toString().length()-1), ref.getRefShowName());
//		arg0.getBillCardPanel().setBodyValueAt(refEnum.toString(), arg0.getRow(), "vbdef20");
//		arg0.getBillCardPanel().setBodyValueAt(sbf.toString().substring(0, sbf.toString().length()-1),
//				arg0.getRow(), "vbdef17");

		
	}

}
