package tinvoiceapply; import baseclass.PaoCust; import baseclass.tools.GetFieldsName; import baseclass.tools.Math; import openapi.base.data.Row; import openapi.base.data.Rows; import openapi.base.data.db.DBConnect; import p2.pao.PaoRemote; import p2.pao.PaoSet; import p2.pao.PaoSetRemote; import p2.util.P2AppException; import p2.util.P2Exception; import tinvoicebill.tinvoicebill; import java.util.HashMap; public class tinvoiceapply extends PaoCust { public tinvoiceapply(PaoSet arg0) { super(arg0); } @Override public void init() throws P2Exception { super.init(); if (!toBeAdded()) { if ("审核".equals(getString("fstatus"))) { GetFieldsName fieldsName = new GetFieldsName(getName()); setFieldFlag(fieldsName.getFields(), READONLY, true); } } } @Override public void add() throws P2Exception { super.add(); setValue("fbillnum", getBillNum(getThisPaoSet().getApp())); PaoSetRemote tinvoiceapplySet = getPaoSet("$" + getName(), getName(), "fstatus='审核'"); tinvoiceapplySet.setOrderBy("tinvoiceapplyid desc"); tinvoiceapplySet.reset(); if (!tinvoiceapplySet.isEmpty()) { String[] fields = new String[]{"finvoicechecker", "finvoicepayee", "finvoiceclerk"}; copyFieldValue(tinvoiceapplySet.getPao(0), fields, this, fields); } if (getThisPaoSet().getApp().equalsIgnoreCase("INVOICEAPP")) { setValue("ftype", "自动", 11L); } else if (getThisPaoSet().getApp().equalsIgnoreCase("INVOICEOLD")) { setValue("ftype", "手工", 11L); } } @Override public void canDelete() throws P2Exception { if (!"新建".equals(getString("fstatus"))) { throw new P2AppException("", "非新建状态下不可删除"); } super.canDelete(); } @Override public void delete(long l) throws P2Exception { super.delete(l); getPaoSet("tinvoiceapplyorder").deleteAll(); getPaoSet("tinvoicebill").deleteAll(); } public void additemnos(PaoSetRemote saorderdetail) throws P2Exception { String[] fromfields = {"fparentid", "saorderdetailid"}; String[] tpfields = {"saorderid", "saorderdetailid"}; BatchAdd(saorderdetail, fromfields, getPaoSet("tinvoiceapplyorder"), tpfields, true); } @Override public void submitvalidate(boolean issubmit) throws P2Exception { super.submitvalidate(issubmit); PaoSetRemote tinvoiceapplyorderSet = getPaoSet("tinvoiceapplyorder"); /****************** 单据检验 *********************/ if (issubmit) { if (!"新建".equals(getString("fstatus"))) { throw new P2AppException("", "非新建状态下不可提交"); } if (tinvoiceapplyorderSet.isEmpty()) { throw new P2AppException("", "订单信息为空不可提交"); } if (tinvoiceapplyorderSet.min("finvoiceamount") <= 0) { throw new P2AppException("", "订单行开票金额必须大于0"); } int i = 0; PaoRemote detailpao = null; while ((detailpao = tinvoiceapplyorderSet.getPao(i)) != null) { double finvoiceamount = detailpao.getDouble("finvoiceamount");//本次开票金额 PaoRemote saorderdetail = detailpao.getPaoSet("saorderdetail").getPao(0); PaoSetRemote saorder_receivedviewSet = saorderdetail.getPaoSet("saorder_receivedview"); double freceivedamount = 0; if (!saorder_receivedviewSet.isEmpty()) { freceivedamount = saorder_receivedviewSet.getPao(0).getDouble("freceivedamount");//订单已收货金额 } String fsonum = saorderdetail.getString("fsonum");//订单号 String fitemname = saorderdetail.getString("fitemname");//商品名称 double finvoiceamount_order = saorderdetail.getDouble("finvoiceamount");//订单已开票金额 if (finvoiceamount > Math.sub(freceivedamount, finvoiceamount_order)) { throw new P2AppException("", "订单" + fsonum + "商品" + fitemname + "本次开票金额" + finvoiceamount + "超过订单可开票金额" + Math.sub(freceivedamount, finvoiceamount_order)); } i++; } } else { if (!"提交".equals(getString("fstatus"))) { throw new P2AppException("", "非提交状态下不可退回"); } } } String orderinvoicecheck = ""; public String orderinvoicecheck() throws P2Exception { orderinvoicecheck = ""; DBConnect dbConnect = new DBConnect(); Rows rows = dbConnect.runSqlQuery("select distinct t3.fsonum,t3.finvoicetaxnumber,t3.finvoicename from tinvoiceapply t1\n" + "inner join tinvoiceapplyorder t2 on t1.siteid=t2.siteid and t1.tinvoiceapplyid=t2.tinvoiceapplyid\n" + "inner join saorder t3 on t2.siteid=t3.siteid and t2.saorderid=t3.saorderid\n" + "where t1.tinvoiceapplyid='" + getUniqueIDValue() + "' and isnull(t1.finvoicetaxnumber,'')!=isnull(t3.finvoicetaxnumber,'') "); for (Row row : rows) { orderinvoicecheck = orderinvoicecheck + row.getString("fsonum") + ";"; } return orderinvoicecheck; } @Override public void submit(boolean issubmit) throws P2Exception { super.submit(issubmit); if (issubmit) { if (!orderinvoicecheck.equals("")) { PaoSetRemote tinvoiceapplyorderSet = getPaoSet("tinvoiceapplyorder"); HashMap map = getPaoMap(tinvoiceapplyorderSet, "saorderid"); for (String saorderid : map.keySet()) { PaoRemote saorder = map.get(saorderid); saorder.setValue("finvoicetaxnumber", getString("finvoicetaxnumber"), 11L); saorder.setValue("finvoicename", getString("finvoicename"), 11L); saorder.setValue("finvoiceaddress", getString("finvoiceaddress"), 11L); } } } setValue("fstatus", issubmit ? "提交" : "新建", 11L); setValue("submitby", issubmit ? getUserName() : "", 11L); setValue("submitdate", issubmit ? getDateTime() : null, 11L); } public void checkvalidate(boolean ischeck) throws P2Exception { PaoSetRemote tinvoiceapplyorderSet = getPaoSet("tinvoiceapplyorder"); PaoSetRemote tinvoicebillSet = getPaoSet("tinvoicebill"); /****************** 单据检验 *********************/ if (ischeck) { if (!"提交".equals(getString("fstatus"))) { throw new P2AppException("", "非提交状态下不可审核"); } if (tinvoiceapplyorderSet.isEmpty()) { throw new P2AppException("", "订单信息为空不可审核"); } if (tinvoicebillSet.isEmpty()) { throw new P2AppException("", "开票信息为空不可审核"); } if (tinvoiceapplyorderSet.min("finvoiceamount") <= 0) { throw new P2AppException("", "订单行开票金额必须大于0"); } PaoSetRemote tinvoiceapply_totalview = getPaoSet("tinvoiceapply_totalview"); if (tinvoiceapply_totalview.getPao(0).getDouble("forderamount") != tinvoiceapply_totalview.getPao(0).getDouble("fbillamount")) { throw new P2AppException("", "订单金额和开票金额不一致"); } int i = 0; PaoRemote detailpao = null; while ((detailpao = tinvoiceapplyorderSet.getPao(i)) != null) { double finvoiceamount = detailpao.getDouble("finvoiceamount");//本次开票金额 PaoRemote saorderdetail = detailpao.getPaoSet("saorderdetail").getPao(0); PaoSetRemote saorder_receivedviewSet = saorderdetail.getPaoSet("saorder_receivedview"); double freceivedamount = 0; if (!saorder_receivedviewSet.isEmpty()) { freceivedamount = saorder_receivedviewSet.getPao(0).getDouble("freceivedamount");//订单已收货金额 } String fsonum = saorderdetail.getString("fsonum");//订单号 String fitemname = saorderdetail.getString("fitemname");//商品名称 double finvoiceamount_order = saorderdetail.getDouble("finvoiceamount");//订单已开票金额 if (finvoiceamount > Math.sub(freceivedamount, finvoiceamount_order)) { throw new P2AppException("", "订单" + fsonum + "商品" + fitemname + "本次开票金额" + finvoiceamount + "超过订单可开票金额" + Math.sub(freceivedamount, finvoiceamount_order)); } i++; } } else { if (!"手工".equalsIgnoreCase(getString("ftype"))) { int i = 0; while (tinvoicebillSet.getPao(i) != null) { if (!(tinvoicebillSet.getPao(i).getString("status").equals("") || tinvoicebillSet.getPao(i).getString("status").equals("22") || tinvoicebillSet.getPao(i).getString("status").equals("3"))) { throw new P2AppException("", "已对接开票接口,不可进行反审核!"); } i++; } } } } public void check(boolean ischeck) throws P2Exception { if (ischeck) { //if ("手工".equalsIgnoreCase(getString("ftype"))) { PaoSetRemote tinvoiceapplyorderSet = getPaoSet("tinvoiceapplyorder"); int i = 0; PaoRemote detailpao = null; while ((detailpao = tinvoiceapplyorderSet.getPao(i)) != null) { double finvoiceamount_detail = detailpao.getDouble("finvoiceamount");//本次开票金额 PaoRemote saorderdetail = detailpao.getPaoSet("saorderdetail").getPao(0); double finvoiceamount_order = saorderdetail.getDouble("finvoiceamount");//订单已开票金额 saorderdetail.setValue("finvoiceamount", Math.add(finvoiceamount_order, finvoiceamount_detail), 111L); i++; } //} setValue("fstatus", "审核", 11L); setValue("checkby", getUserName(), 11L); setValue("checkdate", getDateTime(), 11L); } else { //if ("手工".equalsIgnoreCase(getString("ftype"))) { PaoSetRemote tinvoiceapplyorderSet = getPaoSet("tinvoiceapplyorder"); int i = 0; PaoRemote detailpao = null; while ((detailpao = tinvoiceapplyorderSet.getPao(i)) != null) { double finvoiceamount_detail = detailpao.getDouble("finvoiceamount");//本次开票金额 PaoRemote saorderdetail = detailpao.getPaoSet("saorderdetail").getPao(0); double finvoiceamount_order = saorderdetail.getDouble("finvoiceamount");//订单已开票金额 saorderdetail.setValue("finvoiceamount", Math.sub(finvoiceamount_order, finvoiceamount_detail), 111L); i++; } // } setValue("fstatus", "提交", 11L); setValue("checkby", "", 11L); setValue("checkdate", "", 11L); } } /** * 一键添加发票 */ public void addBills() throws P2Exception { PaoSetRemote tinvoiceapply_totalview = getPaoSet("tinvoiceapply_totalview"); if (!tinvoiceapply_totalview.isEmpty() && getPaoSet("tinvoicebill").isEmpty()) { double forderamount = tinvoiceapply_totalview.getPao(0).getDouble("forderamount"); double famountlimit = getDouble("famountlimit"); double count = java.lang.Math.ceil(Math.div(forderamount, famountlimit)); PaoSetRemote tinvoicebillSet = getPaoSet("tinvoicebill"); for (int i = 0; i < count; i++) { PaoRemote tinvoicebill = tinvoicebillSet.addAtEnd(); if (count == 1) { PaoSetRemote tinvoicebillitemSet = tinvoicebill.getPaoSet("tinvoicebillitem"); PaoSetRemote tinvoiceapplyorderSet = getPaoSet("tinvoiceapplyorder"); int n = 0; while (tinvoiceapplyorderSet.getPao(n) != null) { PaoRemote tinvoiceapplyorder = tinvoiceapplyorderSet.getPao(n); PaoRemote saorderdetail = tinvoiceapplyorder.getPaoSet("saorderdetail").getPao(0); PaoRemote tinvoicebillitem = tinvoicebillitemSet.addAtEnd(); tinvoicebillitem.setValue("tinvoiceapplyordid", tinvoiceapplyorder.getUniqueIDValue()); tinvoicebillitem.setValue("goodscode", saorderdetail.getString("fitemno")); tinvoicebillitem.setValue("goodsname", saorderdetail.getString("fitemname")); tinvoicebillitem.setValue("spectype", saorderdetail.getString("fspec")); tinvoicebillitem.setValue("unit", saorderdetail.getPaoSet("tunit").getPao(0).getString("fname")); tinvoicebillitem.setValue("num", tinvoiceapplyorder.getDouble("finvoiceqty"), 11L); tinvoicebillitem.setValue("price", tinvoiceapplyorder.getDouble("fprice"), 11L); tinvoicebillitem.setValue("taxincludedamount", tinvoiceapplyorder.getDouble("finvoiceamount"), 11L); n++; } } } } } /** * 根据勾选的蓝票信息生成红票信息 * * @throws P2Exception */ public void addRedBill() throws P2Exception { PaoSetRemote tinvoicebillSet = getPaoSet("tinvoicebill"); int i = 0; tinvoicebill tinvoicebill = null; while ((tinvoicebill = (tinvoicebill) tinvoicebillSet.getPao(i)) != null) { if (tinvoicebill.isSelected()) { if (!tinvoicebill.getString("frb").equals("蓝")) { throw new P2AppException("", "红字发票不可执行此操作"); } if (!tinvoicebill.getString("status").equals("2")) { throw new P2AppException("", "对没有完成开票的数据行不可进行红冲操作"); } } i++; } i = 0; while ((tinvoicebill = (tinvoicebill) tinvoicebillSet.getPao(i)) != null) { if (tinvoicebill.isSelected()) { tinvoicebill.addRedBill(); } i++; } } /** * 对接开票接口进行发票生成 * * @throws P2Exception */ public void createBlueInvoice() throws P2Exception { PaoSetRemote tinvoicebillSet = getPaoSet("tinvoicebill"); if (tinvoicebillSet.getSelection().size() != 1) { throw new P2AppException("", "需勾选一行进行发票创建"); } int i = 0; tinvoicebill tinvoicebill = null; while ((tinvoicebill = (tinvoicebill) tinvoicebillSet.getPao(i)) != null) { if (tinvoicebill.isSelected() && "".equals(tinvoicebill.getString("finvoiceserialnum"))) { tinvoicebill.createBlueInvoice(); try { Thread.sleep(2000L); } catch (Exception e) { } } i++; } } /** * 对接开票接口进行发票生成 * * @throws P2Exception */ public void createRedInvoice() throws P2Exception { PaoSetRemote tinvoicebill_redSet = getPaoSet("tinvoicebill_red"); if (tinvoicebill_redSet.getSelection().size() != 1) { throw new P2AppException("", "需勾选一行进行发票创建"); } int i = 0; tinvoicebill tinvoicebill = null; while ((tinvoicebill = (tinvoicebill) tinvoicebill_redSet.getPao(i)) != null) { if (tinvoicebill.isSelected() && "".equals(tinvoicebill.getString("finvoiceserialnum"))) { tinvoicebill.createRedInvoice(); try { Thread.sleep(2000L); } catch (Exception e) { } } i++; } } // public void queryInvoice() throws P2Exception { // PaoSetRemote tinvoicebillSet = getPaoSet("tinvoicebill"); // int i = 0; // tinvoicebill tinvoicebill = null; // while ((tinvoicebill = (tinvoicebill) tinvoicebillSet.getPao(i)) != null) { // if (!"".equals(tinvoicebill.getString("finvoiceserialnum"))) { // tinvoicebill.queryInvoice(); // } // i++; // } // } // // public void queryRedInvoice() throws P2Exception { // PaoSetRemote tinvoicebill_redSet = getPaoSet("tinvoicebill_red"); // int i = 0; // tinvoicebill tinvoicebill = null; // while ((tinvoicebill = (tinvoicebill) tinvoicebill_redSet.getPao(i)) != null) { // if (!"".equals(tinvoicebill.getString("finvoiceserialnum"))) { // tinvoicebill.queryInvoice(); // } // i++; // } // } @Override public void fieldValidate(Object paoField, String fieldname) throws P2Exception { super.fieldValidate(paoField, fieldname); } @Override public void fieldAction(Object paoField, String fieldname) throws P2Exception { super.fieldAction(paoField, fieldname); if ("FAGENTNUM".equals(fieldname)) { setValue("finvoicetaxnumber", "", 11L); setValue("finvoicename", "", 11L); setValue("finvoiceaddress", "", 11L); } else if ("FINVOICETAXNUMBER".equals(fieldname)) { String finvoicetaxnumber = getString(fieldname); DBConnect dbConnect = new DBConnect(); Rows rows = dbConnect.runSqlQuery("select fphonenumber,(isnull(fbank,'')+isnull(fbankcardid,''))as finvoicebankandaccount from tagents_invoice where ftaxnumber='" + finvoicetaxnumber + "' and fbankcardid is not null"); if (!rows.isEmpty()) { //setValue("FINVOICEMOBILE", rows.get(0).getString("fphonenumber")); setValue("FINVOICEBANKANDACCOUNT", rows.get(0).getString("finvoicebankandaccount")); } else { //setValue("FINVOICEMOBILE", ""); setValue("FINVOICEBANKANDACCOUNT", ""); } } } public void changetype() throws P2Exception { setValue("ftype", "手工", 11L); } }