Tinvoiceapply.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package openapi.restcontroller.wechatapp.tinvoiceapply;
  2. import net.sf.json.JSONArray;
  3. import net.sf.json.JSONObject;
  4. import openapi.base.Controller;
  5. import openapi.base.SQLFactory;
  6. import openapi.base.data.Row;
  7. import openapi.base.data.Rows;
  8. import openapi.base.data.db.DBConnect;
  9. import p2.pao.PaoRemote;
  10. import p2.pao.PaoSetRemote;
  11. import tinvoiceapply.tinvoiceapply;
  12. import java.util.ArrayList;
  13. import java.util.Arrays;
  14. import java.util.HashMap;
  15. public class Tinvoiceapply extends Controller {
  16. public Tinvoiceapply(JSONObject content) {
  17. super(content);
  18. }
  19. public String queryInvoiceApplyList() {
  20. String[] sortmsg = {"case when t1.fstatus='新建'then 1 when t1.fstatus='提交'then 2 when t1.fstatus='审核'then 3 else 4 end,t1.createdate desc "};
  21. String sort = "case when t1.fstatus='新建'then 1 when t1.fstatus='提交'then 2 when t1.fstatus='审核'then 3 else 4 end,t1.createdate desc ";
  22. if (content.containsKey("sort") && Arrays.asList(sortmsg).contains(content.getString("sort"))) {
  23. sort = content.getString("sort");
  24. }
  25. String where = " 1=1 ";
  26. if (content.containsKey("where")) {
  27. JSONObject whereObject = content.getJSONObject("where");
  28. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  29. where = where + " and (t3.fagentname like'%" + whereObject.getString("condition") + "%' or t3.fagentshortname like'%" + whereObject.getString("condition") + "%' or t1.finvoicename like'%" + whereObject.getString("condition") + "%' " +
  30. "or exists(select tinvoicebillid from tinvoicebill where siteid=t1.siteid and tinvoiceapplyid=t1.tinvoiceapplyid and (invoicecode like'%" + whereObject.getString("condition") + "%' or invoiceno like'%" + whereObject.getString("condition") +
  31. "%')))";
  32. }
  33. if (whereObject.containsKey("fbegdate") && !"".equals(whereObject.getString("fbegdate"))) {
  34. where = where + " and t1.checkdate >='" + whereObject.getString("fbegdate") + "'";
  35. }
  36. if (whereObject.containsKey("fenddate") && !"".equals(whereObject.getString("fenddate"))) {
  37. where = where + " and t1.checkdate <='" + whereObject.getString("fenddate") + "'";
  38. }
  39. }
  40. SQLFactory factory = new SQLFactory(this, "经销商发票列表查询", pageSize, pageNumber, sort);
  41. factory.addParameter("fagentnum", fagentnum);
  42. factory.addParameter("siteid", siteid);
  43. factory.addParameter_SQL("where", where);
  44. DBConnect connect = new DBConnect();
  45. Rows rows = connect.runSqlQuery(factory.getSQL());
  46. return getReturnObject_suc_page(rows, true, 1, sortmsg).toString();
  47. }
  48. /**
  49. * 发票详情查询
  50. *
  51. * @return
  52. */
  53. public String queryInvoiceApplyMain() {
  54. long tinvoiceapplyid = content.getLong("tinvoiceapplyid");
  55. SQLFactory factory = new SQLFactory(this, "经销商发票详情查询");
  56. factory.addParameter("tinvoiceapplyid", tinvoiceapplyid);
  57. factory.addParameter("siteid", siteid);
  58. DBConnect connect = new DBConnect();
  59. Rows rows = connect.runSqlQuery(factory.getSQL());
  60. for (Row row : rows) {
  61. SQLFactory orderFactory = new SQLFactory(this, "经销商发票详情查询_订单行");
  62. orderFactory.addParameter("tinvoiceapplyid", tinvoiceapplyid);
  63. orderFactory.addParameter("siteid", siteid);
  64. row.put("orderdetails", connect.runSqlQuery(orderFactory.getSQL()));
  65. SQLFactory invoiceFactory = new SQLFactory(this, "经销商发票详情查询_发票");
  66. invoiceFactory.addParameter("tinvoiceapplyid", tinvoiceapplyid);
  67. invoiceFactory.addParameter("siteid", siteid);
  68. row.put("invoices", connect.runSqlQuery(invoiceFactory.getSQL()));
  69. }
  70. return getReturnObject_suc(rows, false).toString();
  71. }
  72. /**
  73. * 发票新增修改
  74. *
  75. * @return
  76. */
  77. public String insertorModify() {
  78. long tinvoiceapplyid = content.getLong("tinvoiceapplyid");//新建为0
  79. DBConnect connect = new DBConnect();
  80. Rows rows = connect.runSqlQuery("select * from tinvoiceapply where tinvoiceapplyid='" + tinvoiceapplyid + "'");
  81. if (!rows.isEmpty() && !"新建".equals(rows.get(0).getString("fstatus"))) {
  82. return getReturnObject_err("当前状态不可修改").toString();
  83. }
  84. String finvoicename = content.getString("finvoicename");//开票单位
  85. String finvoiceaddress = content.getString("finvoiceaddress");//开票单位地址
  86. String finvoicetaxnumber = content.getString("finvoicetaxnumber");//开票单位税号
  87. String fnotes = content.getString("fnotes");//备注
  88. JSONArray orderdetailsArray = content.getJSONArray("orderdetails");
  89. PaoSetRemote tinvoiceapplySet = null;
  90. try {
  91. tinvoiceapplySet = getP2ServerPaoSet("tinvoiceapply", hrid, "siteid='" + siteid + "' and tinvoiceapplyid='" + tinvoiceapplyid + "'");
  92. tinvoiceapplySet.setApp("INVOICEAPP");
  93. PaoRemote tinvoiceapply = null;
  94. if (tinvoiceapplyid <= 0 || tinvoiceapplySet.isEmpty()) {
  95. tinvoiceapply = tinvoiceapplySet.addAtEnd();
  96. tinvoiceapply.setValue("fagentnum", fagentnum, 11L);
  97. content.put("tinvoiceapplyid", tinvoiceapply.getUniqueIDValue());
  98. } else {
  99. tinvoiceapply = tinvoiceapplySet.getPao(0);
  100. }
  101. tinvoiceapply.setValue("finvoicename", finvoicename, 11L);
  102. tinvoiceapply.setValue("finvoiceaddress", finvoiceaddress, 11L);
  103. tinvoiceapply.setValue("finvoicetaxnumber", finvoicetaxnumber, 2L);
  104. tinvoiceapply.setValue("fnotes", fnotes, 11L);
  105. PaoSetRemote tinvoiceapplyorderset = tinvoiceapply.getPaoSet("tinvoiceapplyorder");
  106. HashMap<String, PaoRemote> tinvoiceapplyordermap = getPaoMap(tinvoiceapplyorderset, "saorderdetailid");
  107. ArrayList<String> donotdeleteid = new ArrayList<>();
  108. for (Object o : orderdetailsArray) {
  109. JSONObject orderdetailObject = (JSONObject) o;
  110. String saorderid = orderdetailObject.getString("saorderid");
  111. String saorderdetailid = orderdetailObject.getString("saorderdetailid");
  112. //double finvoiceqty = orderdetailObject.getDouble("finvoiceqty");//开票数量
  113. double finvoiceamount = orderdetailObject.getDouble("finvoiceamount");//开票金额
  114. PaoRemote detailPao = null;
  115. if (tinvoiceapplyordermap.containsKey(saorderdetailid)) {
  116. detailPao = tinvoiceapplyordermap.get(saorderdetailid);
  117. } else {
  118. /**
  119. * 如果id不为0表示该订单行本身不存在,需要增加行
  120. */
  121. detailPao = tinvoiceapplyorderset.addAtEnd();
  122. detailPao.setValue("siteid", siteid, 11L);
  123. detailPao.setValue("tinvoiceapplyid", tinvoiceapply.getUniqueIDValue(), 11L);
  124. detailPao.setValue("saorderid", saorderid, 2L);
  125. detailPao.setValue("saorderdetailid", saorderdetailid, 2L);
  126. }
  127. if (detailPao != null) {
  128. //detailPao.setValue("finvoiceqty", finvoiceqty, 2L);
  129. detailPao.setValue("finvoiceamount", finvoiceamount, 2L);
  130. }
  131. donotdeleteid.add(saorderdetailid);
  132. }
  133. /**
  134. * 删除多余的行
  135. */
  136. for (String id : tinvoiceapplyordermap.keySet()) {
  137. if (!donotdeleteid.contains(id)) {
  138. if (!tinvoiceapplyordermap.get(id).isModified()) {
  139. tinvoiceapplyordermap.get(id).delete();
  140. }
  141. }
  142. }
  143. tinvoiceapplySet.save();
  144. } catch (Exception e) {
  145. e.printStackTrace();
  146. } finally {
  147. try {
  148. tinvoiceapplySet.close();
  149. } catch (Exception e) {
  150. e.printStackTrace();
  151. }
  152. }
  153. return queryInvoiceApplyMain();
  154. }
  155. public String invoiceApplysubmit() {
  156. long tinvoiceapplyid = content.getLong("tinvoiceapplyid");//新建为0
  157. PaoSetRemote tinvoiceapplySet = null;
  158. try {
  159. tinvoiceapplySet = getP2ServerPaoSet("tinvoiceapply", hrid, "siteid='" + siteid + "' and tinvoiceapplyid='" + tinvoiceapplyid + "'");
  160. tinvoiceapplySet.setApp("INVOICEAPP");
  161. tinvoiceapply tinvoiceapply = (tinvoiceapply) tinvoiceapplySet.getPao(0);
  162. tinvoiceapply.submitvalidate(true);
  163. tinvoiceapply.submit(true);
  164. tinvoiceapplySet.save();
  165. } catch (Exception e) {
  166. return getReturnObject_err(e.getMessage()).toString();
  167. } finally {
  168. try {
  169. tinvoiceapplySet.close();
  170. } catch (Exception e) {
  171. e.printStackTrace();
  172. }
  173. }
  174. return getReturnObject_suc().toString();
  175. }
  176. public String invoiceApplydelete() {
  177. long tinvoiceapplyid = content.getLong("tinvoiceapplyid");//新建为0
  178. DBConnect connect = new DBConnect();
  179. Rows rows = connect.runSqlQuery("select * from tinvoiceapply where tinvoiceapplyid='" + tinvoiceapplyid + "'");
  180. if (rows.isEmpty() || !"新建".equals(rows.get(0).getString("fstatus"))) {
  181. return getReturnObject_err("非新建状态下不可删除").toString();
  182. }
  183. String status = connect.runSqlUpdate("delete from tinvoiceapply where tinvoiceapplyid='" + tinvoiceapplyid + "'");
  184. if ("true".equals(status)) {
  185. return getReturnObject_suc().toString();
  186. } else {
  187. return getReturnObject_err("删除失败!").toString();
  188. }
  189. }
  190. /**
  191. * 开票单位选择
  192. *
  193. * @return
  194. */
  195. public String queryInvoiceName() {
  196. DBConnect connect = new DBConnect();
  197. Rows rows = connect.runSqlQuery("select * from tinvoiceapply_taxview where siteid='" + siteid + "' and fpayagentnum='" + fagentnum + "'");
  198. return getReturnObject_suc(rows, false).toString();
  199. }
  200. /**
  201. * 开票订单行选择
  202. *
  203. * @return
  204. */
  205. public String querySaorderDetail() {
  206. //String finvoicetaxnumber = content.getString("finvoicetaxnumber");
  207. long tinvoiceapplyid = content.getLong("tinvoiceapplyid");
  208. SQLFactory factory = new SQLFactory(this, "开票订单行选择查询");
  209. factory.addParameter("siteid", siteid);
  210. factory.addParameter("fagentnum", fagentnum);
  211. //factory.addParameter("finvoicetaxnumber", finvoicetaxnumber);
  212. factory.addParameter("tinvoiceapplyid", tinvoiceapplyid);
  213. DBConnect connect = new DBConnect();
  214. Rows rows = connect.runSqlQuery(factory.getSQL());
  215. return getReturnObject_suc(rows, false).toString();
  216. }
  217. }