Tinvoiceapply.java 12 KB

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