package nc.impl.arap.byzimpl;

import nc.bs.framework.common.NCLocator;
import nc.itf.arap.byzitf.IArapToByzService;
import nc.itf.uap.pf.IplatFormEntry;
import nc.util.arap.arc.ArapArcUtil;
import nc.vo.arap.payable.AggPayableBillVO;
import nc.vo.arap.payable.PayableBillItemVO;
import nc.vo.arap.payable.PayableBillVO;
import nc.vo.org.FinanceOrgVO;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pub.lang.UFDouble;
import uap.json.JSONArray;
import uap.json.JSONObject;

public class ArapToByzImpl implements IArapToByzService {
	private String pk_group = "0001H810000000000HS2";
	private String pk_tradetype = "F1-Cxx-D104";
	private String pk_currtype = "1002Z0100000000001K1";

	@Override
	public AggPayableBillVO payableRecBYZ(JSONObject json) throws Exception {
		AggPayableBillVO aggpayvo = new AggPayableBillVO();
//		InvocationInfoProxy.getInstance().setGroupId(pk_group);
		PayableBillVO headvo = new PayableBillVO();
		setHeadDfault(headvo);

		JSONObject jsonobjet = json.getJSONObject("data").getJSONObject("head");
		String pk_org = jsonobjet.getString("pk_org");

		// 初始化
		ArapArcUtil.initEnvironment("0001H810000000000HS2", new UFDate(jsonobjet.getString("billdate")));
		ArapArcUtil.initSysBilltypeCache(new String[] { "F1" });
		FinanceOrgVO finaceorgvo = ArapArcUtil.selectOrg(pk_org);

		headvo.setPk_org(pk_org);
		headvo.setPk_org_v(finaceorgvo.getPk_vid());
		headvo.setPk_fiorg(pk_org);
		headvo.setPk_fiorg_v(finaceorgvo.getPk_vid());
		headvo.setSett_org(pk_org);
		headvo.setSett_org_v(finaceorgvo.getPk_vid());
//		String maker = jsonobjet.getString("operator");
		headvo.setBillmaker(ArapArcUtil.getPK_syscreater());
		headvo.setCreator(ArapArcUtil.getPK_syscreater());
		headvo.setDef56(jsonobjet.getString("def56"));
		headvo.setDef57(jsonobjet.getString("def57"));
		UFDouble mony = new UFDouble(jsonobjet.getString("amount"));
		headvo.setMoney(mony);
		headvo.setLocal_money(mony);
		headvo.setBilldate(new UFDate(jsonobjet.getString("billdate")));
		headvo.setBusidate(new UFDate(jsonobjet.getString("billdate")));
		if (jsonobjet.has("def21"))
			headvo.setDef21(ArapArcUtil.getCategory(jsonobjet.getString("def21")));

		JSONArray items = json.getJSONObject("data").getJSONArray("bodys");
		String pk_suppler = "";
		PayableBillItemVO[] bodys = new PayableBillItemVO[items.length()];

		for (int i = 0; i < items.length(); i++) {
			JSONObject item = (JSONObject) items.get(i);
			PayableBillItemVO bodyvo = new PayableBillItemVO();
			setBodyDfault(bodyvo);
			bodyvo.setPk_org(pk_org);
			bodyvo.setRowno(i);
			bodyvo.setPk_org_v(finaceorgvo.getPk_vid());
			bodyvo.setPk_fiorg(pk_org);
			bodyvo.setPk_fiorg_v(finaceorgvo.getPk_vid());
			bodyvo.setSett_org(pk_org);
			bodyvo.setSett_org_v(finaceorgvo.getPk_vid());
			bodyvo.setScomment(item.getString("scomment"));
			pk_suppler = item.getString("supplier");
			bodyvo.setSupplier(ArapArcUtil.selectSupplierCode(pk_suppler));
			bodyvo.setBilldate(new UFDate(jsonobjet.getString("billdate")));
			bodyvo.setBusidate(new UFDate(jsonobjet.getString("billdate")));
//			bodyvo.setTaxcodeid(item.getString("taxcodeid"));
//			bodyvo.setTaxrate(new UFDouble(item.getString("taxrate")));
//			UFDouble notax_cr = new UFDouble(item.getString("notax_de"));
//			bodyvo.setLocal_tax_cr(new UFDouble(item.getString("local_tax_de")));
			bodyvo.setMoney_de(new UFDouble(item.getString("money_de")));
//			bodyvo.setNotax_cr(notax_cr);
//			bodyvo.setLocal_notax_cr(notax_cr);
			bodyvo.setLocal_money_cr(mony);
			bodyvo.setMoney_cr(mony);
			bodys[i] = bodyvo;
		}

		headvo.setSupplier(pk_suppler);
		aggpayvo.setParentVO(headvo);
		aggpayvo.setChildrenVO(bodys);
		// modefy by djy 20220305 方案更改，自由态
		NCLocator.getInstance().lookup(IplatFormEntry.class).processAction("SAVE", "F1", null, aggpayvo, null, null);
		return aggpayvo;
	}

	public void setHeadDfault(PayableBillVO headvo) {
		headvo.setPk_group(pk_group);
		headvo.setBillclass("yf");
		headvo.setBillstatus(-1);
		headvo.setSrc_syscode(1);
		headvo.setObjtype(1);
		headvo.setRate(new UFDouble(1));
		headvo.setApprovestatus(-1);
//		headvo.setBilldate(new UFDate());
		headvo.setCreationtime(new UFDateTime());
//		headvo.setBusidate(new UFDate());
		headvo.setBillyear(new UFDate().toString().substring(0, 4));
		headvo.setPk_tradetype(pk_tradetype);
		headvo.setPk_billtype("F1");
		headvo.setPk_tradetypeid(ArapArcUtil.getPk_tradetype(pk_tradetype));
		headvo.setPk_currtype(pk_currtype);
//		headvo.setPk_busitype("1001H6100000000BCY68");
		headvo.setTaxcountryid("0001Z010000000079UJJ");
		headvo.setSyscode(1);
		headvo.setSrc_syscode(1);
	}

	public void setBodyDfault(PayableBillItemVO body) {
		body.setPk_group(pk_group);
		body.setBillclass("yf");
		body.setObjtype(1);
//		body.setBilldate(new UFDate());
//		body.setBusidate(new UFDate());
		body.setPk_tradetype(pk_tradetype);
		body.setPk_billtype("F1");
		body.setPk_tradetypeid(ArapArcUtil.getPk_tradetype(pk_tradetype));
		body.setPk_currtype(pk_currtype);
		body.setBuysellflag(2);
		body.setDirection(-1);
		body.setRate(new UFDouble(1));
	}
}
