package com.tenwa.invoice; import java.rmi.RemoteException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import javax.xml.rpc.ServiceException; import org.dom4j.Document; import org.dom4j.Element; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.amarsoft.are.ARE; import com.amarsoft.are.jbo.JBOTransaction; import com.amarsoft.are.util.StringFunction; import com.amarsoft.awe.util.SqlObject; import com.amarsoft.awe.util.Transaction; import com.amarsoft.dict.als.cache.CacheLoaderFactory; public class IAutokpDo { //参数 private String invoiceParam; //00=专票;02=普票 private String fpzl; //00为正票;01为负票 private String fpzf; private String sid; private String fpdm; private String fphm; public String invoice() { String invoiceParam = this.invoiceParam.replaceAll("@", ","); String requestParam = ""; Document doc; boolean flag = true; String flag_result = ""; IAutokpXML ivoiceXML = new IAutokpXML(); Map>> map = null; if("00".equals(fpzf)){ map = ivoiceXML.getInvoiceParam(invoiceParam); }else{ map = ivoiceXML.getRedbackInvoiceParam(invoiceParam); } for(Map.Entry>> lsm: map.entrySet()){ String contract_no=lsm.getKey(); String responseParam=""; doc = ivoiceXML.createInvoiceXML(lsm,fpzl,fpzf); requestParam = doc.asXML(); try { if(!requestParam.startsWith("error")&&requestParam!=null&&!"".equals(requestParam)){ IAutokpUtils.saveFile(requestParam, contract_no); IAutokpserviceLocator kpl = new IAutokpserviceLocator(); IAutokp autoKP = kpl.getIAutokpPort(); responseParam = autoKP.invoice(requestParam); System.out.println("返回结果:"+responseParam); } } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ServiceException e) { // TODO Auto-generated catch block e.printStackTrace(); } //开票成功 if(!"".endsWith(responseParam)){ IAutokpUtils.saveFile(responseParam, contract_no+"_rnt"); Map responseResult = IAutokpUtils.getResponseResult(responseParam); insertInvoiceLog(lsm,doc,responseResult); if("4011".equals(responseResult.get("fl"))){ insertInvoiceTable(lsm,doc,responseResult); }else{ flag = false; } } } if(flag){ flag_result = "操作成功"; }else{ flag_result = "操作失败"; } return flag_result; } /** * 发票打印 * @return */ public String invoice_pr(){ String requestParam=""; String responseParam=""; String sid = "Invoice_getfph"; IAutokpXML ivoiceXML = new IAutokpXML(); requestParam = ivoiceXML.createInvoiceOthersXML(sid,fpzl,fpdm,fphm); try { IAutokpserviceLocator kpl = new IAutokpserviceLocator(); IAutokp autoKP = kpl.getIAutokpPort(); responseParam = autoKP.invoice_pr(requestParam); } catch (ServiceException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(!"".endsWith(responseParam)){ IAutokpUtils.saveFile(responseParam, fpdm+"-"+fphm+"_rnt"); Map responseResult = IAutokpUtils.getResponseResult(responseParam); if("5011".equals(responseResult.get("fl"))){ System.out.println("打印成功!"); } } return null; } /** * 发票作废 * @return */ public String invoice_fpzf(){ boolean flag = true; String flag_result = ""; String requestParam=""; String responseParam=""; String sid = "Invoice_getfph"; IAutokpXML ivoiceXML = new IAutokpXML(); requestParam = ivoiceXML.createInvoiceOthersXML(sid,fpzl,fpdm,fphm); try { IAutokpUtils.saveFile(requestParam, fpdm+"-"+fphm); IAutokpserviceLocator kpl = new IAutokpserviceLocator(); IAutokp autoKP = kpl.getIAutokpPort(); responseParam = autoKP.invoice_fpzf(requestParam); } catch (ServiceException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(!"".endsWith(responseParam)){ IAutokpUtils.saveFile(responseParam, fpdm+"-"+fphm+"_rnt"); Map responseResult = IAutokpUtils.getResponseResult(responseParam); if("6011".equals(responseResult.get("fl"))){ invalidInvoice(); flag_result = "操作成功"; }else{ flag = false; flag_result = responseResult.get("mess"); } } return flag_result; } /** * 获取待开发票号 * @return */ public String getfph(){ String requestParam=""; String responseParam=""; String SID = "Invoice_getfph"; IAutokpXML ivoiceXML = new IAutokpXML(); requestParam = ivoiceXML.createInvoiceOthersXML(sid,fpzl,fpdm,fphm); try { IAutokpserviceLocator kpl = new IAutokpserviceLocator(); IAutokp autoKP = kpl.getIAutokpPort(); responseParam = autoKP.getfph(requestParam); } catch (ServiceException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(!"".endsWith(responseParam)){ IAutokpUtils.saveFile(responseParam, fpdm+"-"+fphm+"_rnt"); Map responseResult = IAutokpUtils.getResponseResult(responseParam); if("3011".equals(responseResult.get("fl"))){ System.out.println("获取待开发票清单成功"); } } return null; } /** * 发票清单打印 * @return */ public String invoice_qdpr(){ String requestParam=""; String responseParam=""; String SID = "Invoice_qdpr"; try { IAutokpserviceLocator kpl = new IAutokpserviceLocator(); IAutokp autoKP = kpl.getIAutokpPort(); responseParam = autoKP.invoice_qdpr(requestParam); } catch (ServiceException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public String getFpzl() { return fpzl; } public void setFpzl(String fpzl) { this.fpzl = fpzl; } public String getFpzf() { return fpzf; } public void setFpzf(String fpzf) { this.fpzf = fpzf; } public String getInvoiceParam() { return invoiceParam; } public void setInvoiceParam(String invoiceParam) { this.invoiceParam = invoiceParam; } public String getFpdm() { return fpdm; } public void setFpdm(String fpdm) { this.fpdm = fpdm; } public String getFphm() { return fphm; } public void setFphm(String fphm) { this.fphm = fphm; } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public void insertInvoiceTable(Map.Entry>> lsm,Document doc,Map responseResult){ String insertSql = "insert into li_invoice_tj_info(id,contract_no,plan_list,fpzl,fpzf,invoice_money,tax_type,tax_ratio,tax_money,sw_spbm,fpdm,fphm,hztzdh,mxxh,status,payee,tax_creator,tax_checker,inputtime,rele_id)values" +"(REPLACE(UUID(),'-',''),?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; PreparedStatement ps = null; Transaction Sqlca = null; Sqlca = Transaction.createTransaction(CacheLoaderFactory.getDataSource()); Connection con; try { con = Sqlca.getConnection(); boolean autoCom = con.getAutoCommit(); if(autoCom){ con.setAutoCommit(false); } ps = con.prepareStatement(insertSql); Element ele_order = doc.getRootElement().element("Data").element("Order"); Element ele_head = ele_order.element("Head"); Element ele_items = ele_order.element("Items"); String payee = ele_head.element("SKR").getText(); String tax_creator = ele_head.element("KJR").getText(); String tax_checker = ele_head.element("FHR").getText(); String hztzdh = ""; String status = "Y"; String fpdm = responseResult.get("fpdm"); String fphm = responseResult.get("fphm"); if("01".equals(fpzf)){ status = "red"; fpdm = ele_head.element("HCFPDM").getText(); fphm = ele_head.element("HCFPHM").getText(); hztzdh = ele_head.element("HZTZDH").getText(); } List listItems = ele_items.elements(); for(Element item : listItems){ String mxxh = item.element("MXXH").getText(); String planList = ""; String liti_id = ""; String taxType = ""; String contractNo = ""; for(Map map :lsm.getValue()){ if(mxxh.equals(map.get("MXXH"))){ planList = map.get("plan_list"); taxType = map.get("tax_no"); contractNo = map.get("contract_no"); if("01".equals(fpzf)){ liti_id = map.get("liti_id"); String usql = "update li_invoice_tj_info O set O.status='R' where id='"+liti_id+"'"; Sqlca.executeSQL(new SqlObject(usql)); } break; } } String invoice_money = item.element("JE").getText(); String taxRatio = item.element("SLV").getText(); String taxMoney = item.element("shuier").getText(); String sw_spbm = item.element("sw_spbm").getText(); String inputtime = StringFunction.getTodayNow(); ps.setString(1,contractNo); ps.setString(2,planList); ps.setString(3,fpzl); ps.setString(4,fpzf); ps.setString(5,invoice_money); ps.setString(6,taxType); ps.setString(7,taxRatio); ps.setString(8,taxMoney); ps.setString(9,sw_spbm); ps.setString(10,fpdm); ps.setString(11,fphm); ps.setString(12,hztzdh); ps.setString(13,mxxh); ps.setString(14,status); ps.setString(15,payee); ps.setString(16,tax_creator); ps.setString(17,tax_checker); ps.setString(18,inputtime); ps.setString(19,liti_id); ps.addBatch(); } ps.executeBatch(); con.commit(); con.setAutoCommit(autoCom); ps.close(); ps = null; System.out.println("==========开票完毕=========="); } catch (SQLException e) { ARE.getLog().debug("插入天津发票信息表出错,出错消息"+e.getMessage(),e); e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(ps != null){ try { ps.close(); ps = null; } catch (SQLException e) { ARE.getLog().debug("数据库关闭异常:" + e); } } } } public void insertInvoiceLog(Map.Entry>> lsm,Document doc,Map responseResult){ String insertSql = "insert into li_invoice_tj_log(id,contract_no,customer_name,plan_list,tax_type,fpzl,fpzf,invoice_money,tax_ratio,tax_money,payee,tax_creator,tax_checker,fpdm,fphm,mxxh,hztzdh,fl,mess,inputtime)values" +"(REPLACE(UUID(),'-',''),?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; PreparedStatement ps = null; Transaction Sqlca = null; Sqlca = Transaction.createTransaction(CacheLoaderFactory.getDataSource()); Connection con; try { con = Sqlca.getConnection(); boolean autoCom = con.getAutoCommit(); if(autoCom){ con.setAutoCommit(false); } ps = con.prepareStatement(insertSql); Element ele_order = doc.getRootElement().element("Data").element("Order"); Element ele_head = ele_order.element("Head"); Element ele_items = ele_order.element("Items"); String payee = ele_head.element("SKR").getText(); String tax_creator = ele_head.element("KJR").getText(); String tax_checker = ele_head.element("FHR").getText(); String hztzdh = ""; String fl = responseResult.get("fl"); String mess = responseResult.get("mess"); String fpdm = responseResult.get("fpdm"); String fphm = responseResult.get("fphm"); if("01".equals(fpzf)){ fpdm = ele_head.element("HCFPDM").getText(); fphm = ele_head.element("HCFPHM").getText(); hztzdh = ele_head.element("HZTZDH").getText(); } List listItems = ele_items.elements(); for(Element item : listItems){ String mxxh = item.element("MXXH").getText(); String planList = ""; String taxType = ""; String contractNo = ""; String customerName = ""; for(Map map :lsm.getValue()){ if(mxxh.equals(map.get("MXXH"))){ planList = map.get("plan_list"); taxType = map.get("tax_type"); contractNo = map.get("contract_no"); customerName = map.get("customer_name"); break; } } String invoice_money = item.element("JE").getText(); String taxRatio = item.element("SLV").getText(); String taxMoney = item.element("shuier").getText(); String inputtime = StringFunction.getTodayNow(); ps.setString(1,contractNo); ps.setString(2,customerName); ps.setString(3,taxType); ps.setString(4,planList); ps.setString(5,fpzl); ps.setString(6,fpzf); ps.setString(7,invoice_money); ps.setString(8,taxRatio); ps.setString(9,taxMoney); ps.setString(10,payee); ps.setString(11,tax_creator); ps.setString(12,tax_checker); ps.setString(13,fpdm); ps.setString(14,fphm); ps.setString(15,mxxh); ps.setString(16,hztzdh); ps.setString(17, fl); ps.setString(18,mess); ps.setString(19,inputtime); ps.addBatch(); } ps.executeBatch(); con.commit(); con.setAutoCommit(autoCom); ps.close(); ps = null; System.out.println("==========插入日志表完毕=========="); } catch (SQLException e) { ARE.getLog().debug("插入天津发票信息日志表出错,出错消息"+e.getMessage(),e); e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(ps != null){ try { ps.close(); ps = null; } catch (SQLException e) { ARE.getLog().debug("数据库关闭异常:" + e); } } } } public void invalidInvoice(){ Transaction Sqlca = null; Sqlca = Transaction.createTransaction(CacheLoaderFactory.getDataSource()); String usql = "update li_invoice_tj_info O set O.status='N' where fpdm='"+fpdm+"' and fphm='"+fphm+"'"; SqlObject sql; try { sql = new SqlObject(usql); Sqlca.executeSQL(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void redbackInvoice(Transaction Sqlca){ String usql = "update li_invoice_tj_info O set O.status='R' where fpdm='"+fpdm+"' and fphm='"+fphm+"'"; try { SqlObject sql = new SqlObject(usql); Sqlca.executeSQL(sql); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }