package ficashbill; import baseclass.PaoCust; import baseclass.tools.GetFieldsName; import baseclass.tools.Math; import p2.pao.PaoRemote; import p2.pao.PaoSet; import p2.pao.PaoSetRemote; import p2.util.P2AppException; import p2.util.P2Exception; import saorder.saorder; import taccbal.taccbal; import java.util.Calendar; import java.util.Date; /** * 收支凭证表主类 */ public class ficashbill extends PaoCust { /** * 构造函数 * * @param arg0 */ public ficashbill(PaoSet arg0) { super(arg0); } /** * 初始化类 * * @throws P2Exception */ @Override public void init() throws P2Exception { super.init(); if (!toBeAdded()) { /* * 非新建状态下或者该数据是其他单据生成的或者待审标记是true的情况下,将数据设为只读。 */ if (!"新建".equals(getString("fstatus")) || (getInt("FSOURCEID") > 0 && !getString("fsourceobject").equalsIgnoreCase(getName())) || getBoolean("FWILLCHECK")) { setFieldFlag(new GetFieldsName(getName()).getFields(), READONLY, true); if ("提交".equals(getString("fstatus"))) { setFieldFlag("fconfirmamount", READONLY, false); } } /* * 非新建状态下,将待审标记设为可编辑 */ if ("新建".equals(getString("fstatus"))) { setFieldFlag("FWILLCHECK", READONLY, false); } } if (getDouble("famount") > 0) { PaoSetRemote set = getPaoSet(Calendar.getInstance().getTimeInMillis() + getName(), getName(), "fstatus='审核' and ftype='" + getString("ftype") + "' and fsourceid='" + getUniqueIDValue() + "' and fsourceobject='" + getName() + "'"); setValue("FUNWRITEOFFAMOUNT", getDouble("FAMOUNT") + set.sum("famount") - getDouble("FWRITEOFFAMOUNT"), 11l); } } @Override public void add() throws P2Exception { super.add(); //获取当前应用名称 String appname = getThisPaoSet().getApp(); //设置单号编号 if (appname != null) { setValue("fbillnum", getBillNum(appname)); } if ("RECBILL".equals(appname)) { //如果是工厂方新建收入凭证 setValue("ftype", "收入"); } else if ("RECBILL2".equals(appname)) { //如果是经销商新建收入凭证,则获取该账号的归属经销商 String fagentnum = getAgentnumByHrid(getUserName()); setValue("fagentnum", fagentnum); //默认账户设为01 setValue("faccclsnum", "01", 11L); setValue("ftype", "收入"); //CREATEFLAG表示制单方,true表示工厂方,false表示客户方。默认值为true setValue("createflag", false); } else if ("RECBILL3".equals(appname)) { //返利金 setValue("faccclsnum", "02", 11L); setValue("ftype", "收入"); setValue("ftypemx", "返利金", 11L); } else if ("PAYBILL".equals(appname)) { //支出凭证 setValue("ftype", "支出"); } else if ("PAYBILL2".equals(appname)) { //支出凭证 setValue("faccclsnum", "02", 11L); setValue("ftype", "支出"); setValue("ftypemx", "返利金", 11L); } } @Override public void canDelete() throws P2Exception { super.canDelete(); if (!"新建".equals(getString("fstatus"))) { throw new P2AppException("", "非新建状态不可删除"); } if (getInt("fsourceid") > 0 && !("sendrepair".equalsIgnoreCase(getString("fsourceobject")) || (getName().equalsIgnoreCase(getString("fsourceobject"))))) { throw new P2AppException("", "该单为系统自动生成,不可删除"); } if (getBoolean("fwillcheck")) { throw new P2AppException("", "待审单据不可删除,如需删除,请清除待审标记"); } if (!getPaoSet("ficashbill_saorderdetail").isEmpty()) { throw new P2AppException("", "存在工具借出单表体,请先予以删除!"); } } public void submitValidate(boolean issubmit) throws P2Exception { if (issubmit && !"新建".equals(getString("fstatus"))) { throw new P2AppException("", "非新建状态不可提交"); } if (issubmit && getBoolean("createflag")) { throw new P2AppException("", "非经销商制单不可提交"); } if (issubmit && !"RECBILL2".equalsIgnoreCase(getThisPaoSet().getApp())) { throw new P2AppException("", "非打款凭证不可提交"); } if (issubmit && getDouble("famount") == 0) { throw new P2AppException("", "金额为0,不可提交"); } if (!issubmit && !"提交".equals(getString("fstatus"))) { throw new P2AppException("", "非提交状态不可退回"); } } @Override public void submit(boolean issubmit) throws P2Exception { setValue("fstatus", issubmit ? "提交" : "新建", 11L); setValue("submitby", issubmit ? getUserName() : "", 11L); setValue("submitdate", issubmit ? getDateTime() : null, 11L); } @Override public void checkvalidate(boolean ischeck) throws P2Exception { super.checkvalidate(ischeck); boolean createflag = getBoolean("createflag"); if (createflag && !"新建".equals(getString("fstatus"))) { throw new P2AppException("", "非新建状态不可审核"); } if (!createflag && !"提交".equals(getString("fstatus"))) { throw new P2AppException("", "非提交状态不可审核"); } if (getBoolean("fwillcheck")) { throw new P2AppException("", "请将待审标记清除,并确认经销商及金额后再审核"); } if (getDouble("famount") == 0) { throw new P2AppException("", "金额为0,不可审核"); } if ("工具押金".equals(getString("ftypemxdetail")) && "收入".equals(getString("ftype"))) { PaoSetRemote ficashbill_saorderdetailSet = getPaoSet("ficashbill_saorderdetail"); if (ficashbill_saorderdetailSet.isEmpty()) { throw new P2AppException("", "请选择工具借出单"); } if (getDouble("famount") > 0) { //押金收取 int i = 0; PaoRemote detailpao = null; while ((detailpao = ficashbill_saorderdetailSet.getPao(i)) != null) { if (detailpao.getPaoSet("saorderdetail").isEmpty()) { throw new P2AppException("", "订单行不存在!"); } PaoRemote saorderdetailPao = detailpao.getPaoSet("saorderdetail").getPao(0); if (saorderdetailPao.getDouble("fprice") > 0) { throw new P2AppException("", "工具借出单" + saorderdetailPao.getString("fsonum") + "已收取押金,不可重复收取"); } else if (!"审核,关闭".contains(saorderdetailPao.getPaoSet("saorder").getPao(0).getString("fstatus"))) { throw new P2AppException("", "工具借出单" + saorderdetailPao.getString("fsonum") + "已退回,不可收取"); } i++; } } else { //押金退还 int i = 0; PaoRemote detailpao = null; while ((detailpao = ficashbill_saorderdetailSet.getPao(i)) != null) { if (detailpao.getPaoSet("saorderdetail").isEmpty()) { throw new P2AppException("", "订单行不存在!"); } PaoRemote saorderdetailPao = detailpao.getPaoSet("saorderdetail").getPao(0); if (saorderdetailPao.getDouble("fprice") == 0) { throw new P2AppException("", "工具借出单" + saorderdetailPao.getString("fsonum") + "还未收取押金,不可退回"); } i++; } } } double famount = getDouble("famount"); // 支出凭证 if (!ischeck && famount > 0) { PaoSetRemote taccbal_viewSet = getPaoSet("taccbal_view"); // 账户可用余额 double fcanuseamount = taccbal_viewSet.isEmpty() ? 0 : taccbal_viewSet.getPao(0).getDouble("fcanuseamount"); if (fcanuseamount < famount) { throw new P2AppException("", "单据金额大于账户可用余额" + fcanuseamount); } if ("支出".equals(getString("ftype")) && "返利金".equals(getString("ftypemx"))) { PaoSetRemote ficashbill_flSet = getPaoSet("ficashbill_fl"); if (ficashbill_flSet.isEmpty()) { throw new P2AppException("", "返利代金券" + getString("fbillnum_fl") + "不存在!"); } if (ficashbill_flSet.getPao(0).getDouble("fbalance") < famount) { throw new P2AppException("", "返利代金券" + getString("fbillnum_fl") + "余额不足!"); } Date date = ficashbill_flSet.getPao(0).getDate("fenddate"); if (date.before(getDate())) { throw new P2AppException("", "返利金代金券已过有效期"); } } } if (getName().equalsIgnoreCase(getString("fsourceobject"))) { if (famount >= 0) { throw new P2AppException("", "对冲凭证必须为负数"); } //原凭证 PaoSetRemote cashbillset = getPaoSet(Calendar.getInstance().getTimeInMillis() + getName(), getName(), "ficashbillid=" + getLong("fsourceid")); if (cashbillset.isEmpty()) { throw new P2AppException("", "原凭证不存在"); } PaoRemote cashbill = cashbillset.getPao(0); double funwriteoffamount = cashbill.getDouble("funwriteoffamount");//原凭证未核销金额 if (-famount > funwriteoffamount) { throw new P2AppException("", "对冲金额绝对值必须小于原凭证未核销金额"); } } } /** * 收支凭证审核 * * @param isincom 是否收入,true表示为收入凭证审核,false表示为支出凭证审核 * @throws P2Exception */ @Override public void check(boolean isincom) throws P2Exception { deleteAssignment(); super.check(isincom); // 单据金额 double famount = isincom ? getDouble("famount") : -getDouble("famount"); double fbillamount = famount; String faccclsnum = getString("faccclsnum"); String fagentnum = getString("fagentnum"); if (getSysVars().getBoolean("fisrebate") && getPaoSet("taccclass").getPao(0).getBoolean("fisrebate")) { /** * 如果当前账户需要进行返利计算 */ if ("收入".equals(getString("ftype"))) { /** * 收入凭证审核生成返利收入凭证 */ PaoSetRemote taccclass_rebateSet = getPaoSet("#taccclass_rebate", "taccclass_rebate", "frebatetime='收入凭证审核' and faccclsnum='" + faccclsnum + "'"); int i = 0; while (taccclass_rebateSet.getPao(i) != null) { //返利账户 String frebateaccclsnum = taccclass_rebateSet.getPao(i).getString("frebateaccclsnum"); //返利比例 double frebaterate = taccclass_rebateSet.getPao(i).getDouble("frebaterate"); //返利金额 double rebateamount = Math.mul(fbillamount, frebaterate); //如果返利账户和当前账户一致,则直接将返返利金额计算到单据金额中,同时生成一张额外到收入凭证 if (frebateaccclsnum.equals(faccclsnum)) { //返利金额 famount = Math.add(famount, rebateamount); } else { PaoSetRemote taccbalset = getPaoSet("$taccbal" + frebateaccclsnum, "taccbal", "faccclsnum='" + frebateaccclsnum + "' and fagentnum='" + fagentnum + "'"); taccbal accpao = null; if (taccbalset.isEmpty()) { accpao = (taccbal) taccbalset.addAtEnd(); accpao.setfields(fagentnum, frebateaccclsnum); } else { accpao = (taccbal) taccbalset.getPao(0); } accpao.setValue("fbalance", Math.add(accpao.getDouble("fbalance"), rebateamount)); } ficashbill ficashbill = (ficashbill) getThisPaoSet().addAtEnd(); ficashbill.setValues("RECBILL", fagentnum, getString("ftype"), frebateaccclsnum, getDate("fdate"), rebateamount, "收入凭证" + getString("fbillnum") + "审核时生成;" + fbillamount + "*" + frebaterate + "=" + rebateamount, getName(), getUniqueIDValue(), "审核", getDateTime(), getUserName(), getDateTime()); ficashbill.setValue("ftypemx", "政策"); ficashbill.setValue("ftypemxdetail", "支持"); i++; } } else if ("支出".equals(getString("ftype")) && !getPaoSet("saorder").isEmpty()) { /** * 订单生成审核生成返利收入凭证 */ PaoSetRemote taccclass_rebateSet = getPaoSet("#taccclass_rebate", "taccclass_rebate", "frebatetime='订单扣款' and faccclsnum='" + faccclsnum + "'"); int i = 0; while (taccclass_rebateSet.getPao(i) != null) { //返利账户 String frebateaccclsnum = taccclass_rebateSet.getPao(i).getString("frebateaccclsnum"); //返利比例 double frebaterate = taccclass_rebateSet.getPao(i).getDouble("frebaterate"); //返利金额 double rebateamount = Math.mul(-fbillamount, frebaterate); //如果返利账户和当前账户一致,则直接将返返利金额计算到单据金额中,同时生成一张额外到收入凭证 if (frebateaccclsnum.equals(faccclsnum)) { //返利金额 famount = Math.add(famount, rebateamount); } else { PaoSetRemote taccbalset = getPaoSet("$taccbal" + frebateaccclsnum, "taccbal", "faccclsnum='" + frebateaccclsnum + "' and fagentnum='" + fagentnum + "'"); taccbal accpao = null; if (taccbalset.isEmpty()) { accpao = (taccbal) taccbalset.addAtEnd(); accpao.setfields(fagentnum, frebateaccclsnum); } else { accpao = (taccbal) taccbalset.getPao(0); } accpao.setValue("fbalance", Math.add(accpao.getDouble("fbalance"), rebateamount)); } ficashbill ficashbill = (ficashbill) getThisPaoSet().addAtEnd(); ficashbill.setValues("RECBILL", fagentnum, "收入", frebateaccclsnum, getDate("fdate"), rebateamount, getString("fnotes") + ";" + -fbillamount + "*" + frebaterate + "=" + rebateamount, getString("fsourceobject"), getLong("fsourceid"), "审核", getDateTime(), getUserName(), getDateTime()); ficashbill.setValue("ftypemx", "政策"); ficashbill.setValue("ftypemxdetail", "支持"); i++; } } } PaoSetRemote taccbalset = getPaoSet("taccbal"); taccbal taccbal = null; if (taccbalset.isEmpty()) { taccbal = (taccbal) taccbalset.addAtEnd(); taccbal.setfields(fagentnum, faccclsnum); } else { taccbal = (taccbal) taccbalset.getPao(0); } taccbal.setValue("fbalance", Math.add(taccbal.getDouble("fbalance"), famount)); PaoSetRemote ficashbill_flSet = getPaoSet("ficashbill_fl"); if (!ficashbill_flSet.isEmpty()) { ficashbill_flSet.getPao(0).setValue("fbalance", Math.add(ficashbill_flSet.getPao(0).getDouble("fbalance"), famount), 2L); } if ((isincom && "收入".equals(getString("ftype"))) || (!isincom && "支出".equals(getString("ftype")))) { if(famount > 0){ sendEmail(getMailBySalesGroup(getString("fagentnum"),getString("siteid")), "收入凭证", "经销商编号:"+getString("FAGENTNUM")+",客户名称:"+getString("FAGENTSHORTNAME")+",客户有新的收入,收入金额:"+famount); } setValue("ftaccbalbalance", taccbal.getDouble("fbalance"), 11L); setValue("fstatus", "审核", 11L); setValue("checkby", getUserName(), 11L); setValue("checkdate", getDateTime(), 11L); updateSaorder(); } else if ((isincom && "支出".equals(getString("ftype"))) || (!isincom && "收入".equals(getString("ftype")))) { setValue("fstatus", "新建", 11L); setValue("checkby", "", 11L); setValue("checkdate", "", 11L); } } /** * 收支凭证审核 * * @param isincom 是否收入,true表示为收入凭证审核,false表示为支出凭证审核 * @throws P2Exception */ public void check(boolean isincom, PaoRemote taccbal) throws P2Exception { super.check(isincom); // 单据金额 double famount = isincom ? getDouble("famount") : -getDouble("famount"); taccbal.setValue("fbalance", Math.add(taccbal.getDouble("fbalance"), famount)); if ((isincom && "收入".equals(getString("ftype"))) || (!isincom && "支出".equals(getString("ftype")))) { setValue("ftaccbalbalance", taccbal.getDouble("fbalance"), 11L); setValue("fstatus", "审核", 11L); setValue("checkby", getUserName(), 11L); setValue("checkdate", getDateTime(), 11L); updateSaorder(); } else if ((isincom && "支出".equals(getString("ftype"))) || (!isincom && "收入".equals(getString("ftype")))) { setValue("fstatus", "新建", 11L); setValue("checkby", "", 11L); setValue("checkdate", "", 11L); } } /** * 作废验证 * * @throws P2Exception */ public void cancellationvalidate() throws P2Exception { if (!"审核".equals(getString("fstatus"))) { throw new P2AppException("", "非审核状态下不可作废"); } if (getLong("fsourceid") > 0) { throw new P2AppException("", "该单由其他单据生成,不可作废"); } /* if (getPaoSet("TPERIOD").getPao(0).getBoolean("fisclose")) { throw new P2AppException("", "当前会计期间已关闭,不可进行此操作"); }*/ } /** * 作废 * * @throws P2Exception */ public void cancellation() throws P2Exception { // 单据金额 double famount = "收入".equals(getString("ftype")) ? getDouble("famount") : -getDouble("famount"); String fagentnum = getString("fagentnum"); PaoSetRemote taccbalset = getPaoSet("taccbal"); taccbal taccbal = null; if (taccbalset.isEmpty()) { taccbal = (taccbal) taccbalset.addAtEnd(); taccbal.setfields(fagentnum, getString("faccclsnum")); } else { taccbal = (taccbal) taccbalset.getPao(0); } taccbal.setValue("fbalance", Math.sub(taccbal.getDouble("fbalance"), famount)); setValue("fstatus", "作废", 11L); if ("收入".equals(getString("ftype"))) { PaoSetRemote cashbillSet = getPaoSet("$ficashbill", "ficashbill", "fstatus='审核'and fsourceobject='ficashbill' and fsourceid='" + getUniqueIDValue() + "'"); int i = 0; PaoRemote cashpao = null; while ((cashpao = cashbillSet.getPao(i)) != null) { String frebateaccclsnum = cashpao.getString("faccclsnum"); double rebateamount = cashpao.getDouble("famount"); if (frebateaccclsnum.equals(getString("faccclsnum"))) { taccbal.setValue("fbalance", Math.sub(taccbal.getDouble("fbalance"), rebateamount)); } else { PaoSetRemote rebateaccbalset = getPaoSet("$taccbal" + frebateaccclsnum, "taccbal", "faccclsnum='" + frebateaccclsnum + "' and fagentnum='" + fagentnum + "'"); taccbal accpao = null; if (rebateaccbalset.isEmpty()) { accpao = (taccbal) rebateaccbalset.addAtEnd(); accpao.setfields(fagentnum, frebateaccclsnum); } else { accpao = (taccbal) rebateaccbalset.getPao(0); } accpao.setValue("fbalance", Math.sub(accpao.getDouble("fbalance"), rebateamount)); } cashpao.setValue("fstatus", "作废", 11L); i++; } } } public void setValues(String appname, String fagentnum, String ftype, String faccclsnum, Date fdate, double famount, String fnotes, String sourceobject, long sourceid, String fstatus, Date createdate, String checkby, Date checkdate) throws P2Exception { setValue("fbillnum", getBillNum(appname)); setValue("ftype", ftype); setValue("fagentnum", fagentnum, 11L); setValue("faccclsnum", faccclsnum, 11L); setValue("fdate", fdate); setValue("famount", famount); setValue("fnotes", fnotes); setValue("fsourceobject", sourceobject); setValue("fsourceid", sourceid); setValue("fstatus", fstatus); setValue("createdate", createdate); setValue("checkby", checkby); setValue("checkdate", checkdate); } @Override public void fieldAction(Object paoField, String fieldname) throws P2Exception { super.fieldAction(paoField, fieldname); if ("FAMOUNT".equals(fieldname)) { if ("返利金".equals(getString("ftypemx"))) { setValue("fbalance", getDouble(fieldname)); } } } @Override public void fieldValidate(Object paoField, String fieldname) throws P2Exception { super.fieldValidate(paoField, fieldname); if ("FAMOUNT".equals(fieldname)) { if ("返利金".equals(getString("ftypemx"))) { if (getDouble(fieldname) < 0) { throw new P2AppException("", "金额不可小于0"); } } // if (getDouble(fieldname) == 0) { // throw new P2AppException("", "金额不可等于0"); // } if (getName().equalsIgnoreCase(getString("fsourceobject"))) { if (getDouble(fieldname) >= 0) { throw new P2AppException("", "对冲凭证必须为负数"); } //原凭证 PaoSetRemote cashbillset = getPaoSet(Calendar.getInstance().getTimeInMillis() + getName(), getName(), "ficashbillid=" + getLong("fsourceid")); if (cashbillset.isEmpty()) { throw new P2AppException("", "原凭证不存在"); } PaoRemote cashbill = cashbillset.getPao(0); double funwriteoffamount = cashbill.getDouble("famount");//原凭证未核销金额 if (-getDouble(fieldname) > funwriteoffamount) { throw new P2AppException("", "对冲金额绝对值必须小于原凭证未核销金额"); } } } } /** * 押金单添加订单 * * @param saorderSet */ public void addsaorders(PaoSetRemote saorderSet) throws P2Exception { PaoSetRemote ficashbill_saorderdetail = getPaoSet("ficashbill_saorderdetail"); int i = 0; PaoRemote saorder = null; while ((saorder = saorderSet.getPao(i)) != null) { if (saorder.isSelected()) { PaoSetRemote saorderdetailSet = saorder.getPaoSet("saorderdetail"); int n = 0; while (saorderdetailSet.getPao(n) != null) { PaoRemote newpao = ficashbill_saorderdetail.addAtEnd(); newpao.setValue("ficashbillid", getUniqueIDValue()); newpao.setValue("saorderid", saorder.getUniqueIDValue()); newpao.setValue("saorderdetailid", saorderdetailSet.getPao(n).getUniqueIDValue()); newpao.setValue("fqty", saorderdetailSet.getPao(n).getDouble("fqty")); n++; } } i++; } } /** * 押金凭证更新工具借出单 */ public void updateSaorder() throws P2Exception { if ("工具押金".equals(getString("ftypemxdetail")) && "收入".equals(getString("ftype")) && getDouble("famount") > 0 && !(getOwner() instanceof saorder)) { PaoSetRemote ficashbill_saorderdetail = getPaoSet("ficashbill_saorderdetail"); int i = 0; PaoRemote saorderdetail = null; while ((saorderdetail = ficashbill_saorderdetail.getPao(i)) != null) { PaoSetRemote orderdetailset = saorderdetail.getPaoSet("saorderdetail"); if (!orderdetailset.isEmpty()) { orderdetailset.getPao(0).setValue("fprice", saorderdetail.getDouble("fprice"), 2L); } i++; } } } public void writeoffValidate() throws P2Exception { if (!"审核".equals(getString("fstatus"))) { throw new P2AppException("", "非审核状态不可进行核销操作"); } if (!"收入".equals(getString("ftype"))) { throw new P2AppException("", "非收入凭证不可进行核销操作"); } if (getDouble("funwriteoffamount") == 0) { throw new P2AppException("", "未核销金额为0!"); } } public long writeoff() throws P2Exception { PaoSetRemote twriteoffbillSet = getPaoSet("twriteoffbill"); twriteoffbillSet.setApp("WRITEOFF"); PaoRemote pao = twriteoffbillSet.addAtEnd(); pao.setValue("fdate", getDate()); pao.setValue("fagentnum", getString("fagentnum")); pao.setValue("ficashbillnum", getString("fbillnum")); return pao.getUniqueIDValue(); } public long createredcashbill() throws P2Exception { if (!"审核".equalsIgnoreCase(getString("fstatus"))) { throw new P2AppException("", "非审核状态下不可进行对冲操作"); } double famount = getDouble("famount");//凭证未核销金额 if (famount <= 0) { throw new P2AppException("", "该凭证无法进行对冲操作"); } double funwriteoffamount = getDouble("funwriteoffamount");//凭证未核销金额 if (funwriteoffamount <= 0) { throw new P2AppException("", "当前凭证已无法进行对冲操作"); } PaoSetRemote ficashbillSet = getPaoSet("$$ficashbill", "ficashbill"); ficashbillSet.setApp(getThisPaoSet().getApp().toUpperCase()); PaoRemote pao = ficashbillSet.addAtEnd(); pao.setValue("fsourceid", getUniqueIDValue()); pao.setValue("fsourceobject", "FICASHBILL"); pao.setValue("fagentnum", getString("fagentnum")); pao.setValue("faccclsnum", getString("faccclsnum")); pao.setValue("ftypemx", getString("ftypemx"), 11L); pao.setValue("ftypemxdetail", getString("ftypemxdetail"), 11L); pao.setValue("fnotes", "凭证对冲", 11L); pao.setValue("fdate", getDate("fdate")); pao.setValue("famount", -funwriteoffamount); return pao.getUniqueIDValue(); } }