package com.kingdee.eas.scm.sm.pur.client;

import java.awt.event.ActionEvent;
import java.sql.SQLException;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.scm.sm.pur.PurRequestFactory;
import com.kingdee.eas.scm.sm.pur.PurRequestInfo;
import com.kingdee.eas.util.SysUtil;
import com.kingdee.eas.util.client.MsgBox;
import com.kingdee.eas.xr.XRSQLFacadeFactory;
import com.kingdee.jdbc.rowset.IRowSet;

public class PurRequestEditUIPIEx extends PurRequestEditUI {

	public PurRequestEditUIPIEx() throws Exception {
		super();
		// TODO Auto-generated constructor stub
	}

	public void onLoad() throws Exception {
		super.onLoad();

	}

	protected void verifyInput(ActionEvent e) throws Exception {

	}

	/*
	 * to广告制作费改为没有收到上家制作费款，不允许发起采购申请
	 * by Sera on 2019.5.5
	 */
	protected void checkValidDataForSubmit() throws Exception {
		// 调用保存方法
		ActionEvent e = null;
		actionSave_actionPerformed(e);

		super.checkValidDataForSubmit();

		PurRequestInfo purRequestinfo = PurRequestFactory.getRemoteInstance()
				.getPurRequestInfo(new ObjectUuidPK(editData.getId()));

		// 采购申请维修标识resetFlag
		String resetFlag = "1";
		if (purRequestinfo.get("isGg") != null) {
			if (purRequestinfo.get("isGg").toString() == "true") {
				resetFlag = "0";// 勾选采购申请维修
			}
		}
		// 当广告合同字段不为空且采购申请维修未勾选时执行制作费的校验方法
		if ((purRequestinfo.get("saleContract") != null) && (resetFlag == "1")) {
			// 从采购申请单得到广告合同Id
			StringBuffer sql0 = new StringBuffer();
			String purId = purRequestinfo.getId().toString();
			String htId = "";
			sql0.append("SELECT CFSALECONTRACTID myId FROM T_SM_PurRequest where FID ='"
							+ purId + "'");

			try {
				IRowSet rowset0 = XRSQLFacadeFactory.getRemoteInstance()
						.executeQuery(sql0.toString(), null);
				while (rowset0.next()) {
					htId = rowset0.getString("myId").toString();
				}
			} catch (BOSException e1) {
				e1.printStackTrace();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}

			// 判断该thId是否存在于广告合同
			String sql1 = "SELECT fid FROM CT_GGL_SaleContract where fid='"
					+ htId + "'";
			IRowSet rowset1 = XRSQLFacadeFactory.getRemoteInstance()
					.executeQuery(sql1.toString(), null);

			// 当广告合同Id存在于广告时，判断广告合同分录制作费的情况
			if (rowset1.size() == 1) {
				/*
				 * sql查询条件为where FPARENTID =广告合同ID && CFRECTYPEID（制作费类型）=制作费
				 */
				String sql2 = " SELECT sum(CFRecAmount) amount  from CT_GGL_SaleContractE2 where FPARENTID='"
						+ htId
						+ "'and CFRECTYPEID='JQcAAAABlkKqLJSN' group by FPARENTID  ";
				IRowSet rowset = XRSQLFacadeFactory.getRemoteInstance()
						.executeQuery(sql2.toString(), null);

				// 广告合同部分收款
				if (rowset.size() == 2) {
					MsgBox.showWarning("部分制作费未收款，无法生成采购申请单！");
					SysUtil.abort();
				}
				
				// 广告合同全部未收款
				if (rowset.size() == 1) {
					String num = "";
					while (rowset.next()) {
						num = rowset.getString("isYes").toString();// 0为未收款
					}
					if (num.equals("0")) {
						MsgBox.showWarning("制作费未收款，无法生成采购申请单!");
						SysUtil.abort();
					}
				}
				
				// 广告合同没有制作费
				if (rowset.size() == 0) {
					MsgBox.showWarning("所属广告合同没有制作费，无法生成采购申请单!");
					SysUtil.abort();
				}
			}
		}
	}
   /*
    * to广告制作费改为没有收到上家制作费款，不允许发起采购申请
	* 结束
    */
}
