Tinvoiceapply_saler.java 13 KB

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