Paybill.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. package restcontroller.webmanage.sale.paybill;
  2. import beans.accountbalance.Accountbalance;
  3. import beans.accountbalance.AccountbalanceEntity;
  4. import beans.accountbalance.CashbillEntity;
  5. import beans.data.BatchDeleteErr;
  6. import beans.datacontrllog.DataContrlLog;
  7. import beans.remind.Remind;
  8. import com.alibaba.fastjson.JSONArray;
  9. import com.alibaba.fastjson.JSONObject;
  10. import common.Controller;
  11. import common.YosException;
  12. import common.annotation.API;
  13. import common.annotation.CACHEING;
  14. import common.annotation.CACHEING_CLEAN;
  15. import common.data.QuerySQL;
  16. import common.data.Row;
  17. import common.data.Rows;
  18. import common.data.SQLFactory;
  19. import restcontroller.R;
  20. import restcontroller.webmanage.executorService.Executor;
  21. import java.io.IOException;
  22. import java.math.BigDecimal;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. @API(title = "打款凭证")
  26. public class Paybill extends Controller {
  27. /**
  28. * 构造函数
  29. *
  30. * @param content
  31. */
  32. public Paybill(JSONObject content) throws YosException {
  33. super(content);
  34. }
  35. @API(title = "列表", apiversion = R.ID20221226152904.v1.class)
  36. @CACHEING
  37. public String query() throws YosException, IOException {
  38. boolean isExport = content.getBooleanValue("isExport");
  39. StringBuffer where = new StringBuffer(" 1=1 ");
  40. if (content.containsKey("where")) {
  41. JSONObject whereObject = content.getJSONObject("where");
  42. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  43. where.append("and (t1.billno like '%").append(whereObject.getString("condition")).append("%'");
  44. where.append("or t2.agentnum like '%").append(whereObject.getString("condition")).append("%'");
  45. where.append("or t3.enterprisename like '%").append(whereObject.getString("condition")).append("%')");
  46. }
  47. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  48. where.append("and t1.status = '").append(whereObject.getString("status")).append("'");
  49. }
  50. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  51. where.append(" and(");
  52. where.append("t1.paydate >='").append(whereObject.getString("begindate")).append("' ");
  53. where.append(")");
  54. }
  55. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  56. where.append(" and(");
  57. where.append("t1.paydate <='").append(whereObject.getString("enddate")).append(" 23:59:59' ");
  58. where.append(")");
  59. }
  60. }
  61. if (usertype == 21 || usertype == 22) {
  62. where.append(" and t1.sys_enterpriseid=").append(sys_enterpriseid);
  63. } else {
  64. where.append(" and t1.status!='新建'");
  65. }
  66. // SQLFactory sqlFactory = new SQLFactory(this, "打款凭证列表", pageSize, pageNumber, pageSorting);
  67. // sqlFactory.addParameter("siteid", siteid);
  68. // sqlFactory.addParameter_SQL("where", where);
  69. // Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  70. if (pageSorting.equals("''")) {
  71. pageSorting = "t1.status desc, t1.createdate desc";
  72. }
  73. QuerySQL querySQL = query(where.toString());
  74. querySQL.setOrderBy(pageSorting);
  75. querySQL.setPage(pageSize, pageNumber);
  76. Rows rows = querySQL.query();
  77. // if (isExport) {
  78. // //去除不需要导出项
  79. // rows.getFieldList().remove("sa_paybillid");
  80. // rows.getFieldList().remove("sys_enterpriseid");
  81. // rows.getFieldList().remove("checkby");
  82. // rows.getFieldList().remove("checkdate");
  83. // rows.getFieldList().remove("remarks");
  84. // rows.getFieldList().remove("enterprisename");
  85. // rows.getFieldList().remove("createby");
  86. // rows.getFieldList().remove("createdate");
  87. //
  88. // rows.getFieldList().remove("sa_paybillid");
  89. // rows.getFieldList().remove("changeuserid");
  90. // rows.getFieldList().remove("changeby");
  91. // rows.getFieldList().remove("changedate");
  92. // rows.getFieldList().remove("bank");
  93. // rows.getFieldList().remove("bankcardno");
  94. // rows.getFieldList().remove("submitdate");
  95. // rows.getFieldList().remove("submitby");
  96. // rows.getFieldList().remove("inbank");
  97. // rows.getFieldList().remove("inbankcardno");
  98. //
  99. // Rows uploadRows = uploadExcelToObs("cashBill", "打款凭证列表", rows, getTitleMap());
  100. // return getSucReturnObject().setData(uploadRows).toString();
  101. // }
  102. return getSucReturnObject().setData(rows).toString();
  103. }
  104. //返回导出的标题
  105. public HashMap<String, String> getTitleMap() {
  106. HashMap<String, String> titleMap = new HashMap<>();
  107. titleMap.put("billno", "凭证编号");
  108. titleMap.put("status", "单据状态");
  109. titleMap.put("agentnum", "经销商编号");
  110. titleMap.put("abbreviation", "经销商简称");
  111. titleMap.put("payer", "付款人");
  112. titleMap.put("paydate", "付款时间");
  113. titleMap.put("period", "回款归属月份");
  114. titleMap.put("amount", "打款金额");
  115. return titleMap;
  116. }
  117. public QuerySQL query(String where) throws YosException {
  118. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_paybill");
  119. querySQL.setTableAlias("t1");
  120. querySQL.addJoinTable(JOINTYPE.inner, "sa_agents", "t2", "t1.siteid = t2.siteid and t1.sys_enterpriseid = t2.sys_enterpriseid"
  121. , "agentnum");
  122. querySQL.addJoinTable(JOINTYPE.inner, "sys_enterprise", "t3", "t1.siteid = t2.siteid and t1.sys_enterpriseid = t3.sys_enterpriseid"
  123. , "enterprisename", "abbreviation");
  124. querySQL.setWhere(where);
  125. querySQL.setWhere("t1.siteid", siteid);
  126. return querySQL;
  127. }
  128. @API(title = "新增或更新", apiversion = R.ID20221226153004.v1.class, intervaltime = 200)
  129. @CACHEING_CLEAN(apiversions = {R.ID20221226152904.v1.class, R.ID20230202100104.v1.class})
  130. public String insertOrUpdate() throws YosException {
  131. ArrayList<String> sqlList = new ArrayList<>();
  132. Long sa_paybillid = content.getLong("sa_paybillid");
  133. if (content.containsKey("sys_enterpriseid")) {
  134. sys_enterpriseid = content.getLong("sys_enterpriseid");
  135. }
  136. String payer = content.getString("payer");
  137. String paydate = content.getString("paydate");
  138. String amount = content.getString("amount");
  139. String remarks = content.getString("remarks");
  140. String bank = content.getString("bank");
  141. String bankcardno = content.getString("bankcardno");
  142. String inbank = content.getString("inbank");
  143. String inbankcardno = content.getString("inbankcardno");
  144. Rows rows = dbConnect.runSqlQuery("select status from sa_paybill where siteid='" + siteid + "' and sa_paybillid=" + sa_paybillid);
  145. SQLFactory sqlFactory = new SQLFactory(this, "打款凭证新增");
  146. if (sa_paybillid <= 0 || rows.isEmpty()) {
  147. sa_paybillid = createTableID("sa_paybill");
  148. String billno = createBillCode("paybill");
  149. sqlFactory.addParameter("billno", billno);
  150. //sqlFactory.addParameter("billno", sa_paybillid);
  151. sqlList.add(DataContrlLog.createLog(this, "sa_paybill", sa_paybillid, "新增", "打款凭证新增").getSQL());
  152. } else {
  153. if (!rows.get(0).getString("status").equals("新建")) {
  154. return getErrReturnObject().setErrMsg("非新建状态不能更改").toString();
  155. }
  156. sqlFactory = new SQLFactory(this, "打款凭证更新");
  157. sqlList.add(DataContrlLog.createLog(this, "sa_paybill", sa_paybillid, "更新", "打款凭证更新").getSQL());
  158. }
  159. sqlFactory.addParameter("siteid", siteid);
  160. sqlFactory.addParameter("sa_paybillid", sa_paybillid);
  161. sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  162. sqlFactory.addParameter("payer", payer);
  163. sqlFactory.addParameter("paydate", paydate);
  164. sqlFactory.addParameter("amount", amount);
  165. sqlFactory.addParameter("remarks", remarks);
  166. sqlFactory.addParameter("bank", bank);
  167. sqlFactory.addParameter("bankcardno", bankcardno);
  168. sqlFactory.addParameter("inbank", inbank);
  169. sqlFactory.addParameter("inbankcardno", inbankcardno);
  170. sqlFactory.addParameter("userid", userid);
  171. sqlFactory.addParameter("username", username);
  172. sqlList.add(sqlFactory.getSQL());
  173. dbConnect.runSqlUpdate(sqlList);
  174. content.put("sa_paybillid", sa_paybillid);
  175. return queryById();
  176. }
  177. @API(title = "删除", apiversion = R.ID20221226153104.v1.class)
  178. @CACHEING_CLEAN(apiversions = {R.ID20221226152904.v1.class, R.ID20230202100104.v1.class})
  179. public String delete() throws YosException {
  180. ArrayList<String> sqlList = new ArrayList<>();
  181. JSONArray sa_paybillids = content.getJSONArray("sa_paybillids");
  182. BatchDeleteErr batchDeleteErr = BatchDeleteErr.create(this, sa_paybillids.size());
  183. for (Object o : sa_paybillids) {
  184. long sa_paybillid = Long.parseLong(o.toString());
  185. Rows rows = dbConnect.runSqlQuery("select status from sa_paybill where siteid='" + siteid + "' and sa_paybillid=" + sa_paybillid);
  186. if (!rows.get(0).getString("status").equals("新建")) {
  187. batchDeleteErr.addErr(sa_paybillid, "非新建状态下无法删除");
  188. continue;
  189. }
  190. sqlList.add("delete from sa_paybill where siteid='" + siteid + "' and sa_paybillid=" + sa_paybillid);
  191. sqlList.add("delete from sa_paybilldetail where siteid='" + siteid + "' and sa_paybillid=" + sa_paybillid);
  192. sqlList.add(DataContrlLog.createLog(this, "sa_paybill", sa_paybillid, "删除", "打款凭证删除").getSQL());
  193. }
  194. dbConnect.runSqlUpdate(sqlList);
  195. return batchDeleteErr.getReturnObject().toString();
  196. }
  197. @API(title = "提交", apiversion = R.ID20221226153204.v1.class)
  198. @CACHEING_CLEAN(apiversions = {R.ID20221226152904.v1.class, R.ID20230202100104.v1.class})
  199. public String submit() throws YosException {
  200. ArrayList<String> sqlList = new ArrayList<>();
  201. Long sa_paybillid = content.getLong("sa_paybillid");
  202. Rows rows = dbConnect.runSqlQuery("select status,amount from sa_paybill where siteid='" + siteid + "' and sa_paybillid=" + sa_paybillid);
  203. Rows accountsumamount = dbConnect.runSqlQuery("select sum(amount) amount from sa_paybilldetail where siteid='" + siteid + "' and sa_paybillid=" + sa_paybillid);
  204. if (rows.isEmpty()) {
  205. return getErrReturnObject().setErrMsg("未选择打款凭证").toString();
  206. }
  207. if (!"新建".equals(rows.get(0).getString("status"))) {
  208. return getErrReturnObject().setErrMsg("非新建状态无法提交").toString();
  209. }
  210. if (rows.get(0).getBigDecimal("amount").compareTo(accountsumamount.get(0).getBigDecimal("amount")) == -1) {
  211. return getErrReturnObject().setErrMsg("账户打款金额总值超出打款总金额").toString();
  212. }
  213. if (rows.get(0).getBigDecimal("amount").compareTo(accountsumamount.get(0).getBigDecimal("amount")) == 1) {
  214. return getErrReturnObject().setErrMsg("打款总金额余" + rows.get(0).getBigDecimal("amount").subtract(accountsumamount.get(0).getBigDecimal("amount")) + "未分配").toString();
  215. }
  216. SQLFactory sqlFactory = new SQLFactory(this, "打款凭证提交");
  217. sqlFactory.addParameter("siteid", siteid);
  218. sqlFactory.addParameter("username", username);
  219. sqlFactory.addParameter("sa_paybillid", sa_paybillid);
  220. sqlList.add(sqlFactory.getSQL());
  221. sqlList.add(DataContrlLog.createLog(this, "sa_paybill", sa_paybillid, "提交", "打款凭证提交").getSQL());
  222. dbConnect.runSqlUpdate(sqlList);
  223. return getSucReturnObject().toString();
  224. }
  225. @API(title = "审核", apiversion = R.ID20221226153304.v1.class)
  226. @CACHEING_CLEAN(apiversions = {R.ID20221226152904.v1.class, R.ID20221009102903.v1.class, R.ID20230111103403.v1.class, R.ID20230202100104.v1.class})
  227. public String check() throws YosException {
  228. ArrayList<String> sqlList = new ArrayList<>();
  229. Long sa_paybillid = content.getLong("sa_paybillid");
  230. Rows rows = dbConnect.runSqlQuery("select sys_enterpriseid,status,billno,remarks from sa_paybill where siteid='" + siteid + "' and sa_paybillid=" + sa_paybillid);
  231. Rows accountclassRows = dbConnect.runSqlQuery("select t1.sa_accountclassid,t1.amount,t2.accountname from sa_paybilldetail t1 left join sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid where t1.siteid='" + siteid + "' and t1.amount>0 and t1.sa_paybillid=" + sa_paybillid);
  232. if (rows.isEmpty()) {
  233. return getErrReturnObject().setErrMsg("未选择打款凭证").toString();
  234. }
  235. if (!"提交".equals(rows.get(0).getString("status"))) {
  236. return getErrReturnObject().setErrMsg("非提交状态无法审核").toString();
  237. }
  238. if (content.containsKey("period") && !"".equals(content.getString("period"))) {
  239. sqlList.add("update sa_paybill set period='" + content.getString("period") + "' where siteid='" + siteid + "' and sa_paybillid=" + sa_paybillid);
  240. } else {
  241. return getErrReturnObject().setErrMsg("未填写会计日期").toString();
  242. }
  243. if (dbConnect.runSqlQuery("SELECT 1 from sa_paybilldetail WHERE sa_paybillid=" + sa_paybillid).isEmpty()) {
  244. return getErrReturnObject().setErrMsg("打款明细为空").toString();
  245. }
  246. SQLFactory sqlFactory = new SQLFactory(this, "打款凭证审核");
  247. sqlFactory.addParameter("siteid", siteid);
  248. sqlFactory.addParameter("sa_paybillid", sa_paybillid);
  249. sqlFactory.addParameter("username", username);
  250. sqlList.add(sqlFactory.getSQL());
  251. sqlList.add(DataContrlLog.createLog(this, "sa_paybill", sa_paybillid, "审核", "打款凭证审核").getSQL());
  252. long sys_enterpriseid = rows.get(0).getLong("sys_enterpriseid");
  253. String billno = rows.get(0).getString("billno");
  254. String remarks = rows.get(0).getString("remarks");
  255. dbConnect.runSqlUpdate(sqlList);
  256. for (Row row : accountclassRows) {
  257. BigDecimal amount = row.getBigDecimal("amount");
  258. CashbillEntity cashbillEntity = new CashbillEntity(amount, remarks, "打款凭证", billno, "sa_paybill", sa_paybillid, "货款", "");
  259. JSONObject cashbillIncome = Accountbalance.createCashbillIncome(this, sys_enterpriseid, row.getLong("sa_accountclassid"), cashbillEntity, false, true);
  260. sqlList = new ArrayList<>(cashbillIncome.getJSONArray("sqlList").toJavaList(String.class));
  261. Long sa_cashbillid = cashbillIncome.getLong("sa_cashbillid");
  262. dbConnect.runSqlUpdate(sqlList);
  263. Accountbalance.remindSend(this, sys_enterpriseid, sa_cashbillid);
  264. String message = "您的打款凭证已审核通过,【" + row.getString("accountname") + "】账户已成功充值【" + amount + "】元";
  265. // sendMsg(message, sa_paybillid, sys_enterpriseid);
  266. }
  267. return getSucReturnObject().toString();
  268. }
  269. public void sendMsg(String content, Long sa_paybillid, Long sys_enterpriseid) throws YosException {
  270. ArrayList<Long> userList = getEnterpriseHrs(sys_enterpriseid).toArrayList("userid", new ArrayList<>());
  271. Remind remind = new Remind(this);
  272. remind.setTitle("打款消息");
  273. remind.setContent(content);
  274. remind.setType("应用");
  275. remind.setObjectid(sa_paybillid);
  276. remind.setObjectname("sa_paybill");
  277. remind.setTouserid(userList);
  278. remind.sendByDialogMsg().createSys_message();
  279. }
  280. @API(title = "详情", apiversion = R.ID20221226153404.v1.class)
  281. public String queryById() throws YosException {
  282. Long sa_paybillid = content.getLong("sa_paybillid");
  283. SQLFactory sqlFactory = new SQLFactory(this, "打款凭证详情");
  284. sqlFactory.addParameter("siteid", siteid);
  285. sqlFactory.addParameter("sa_paybillid", sa_paybillid);
  286. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  287. if (rows.isNotEmpty()) {
  288. rows.get(0).put("attinfos", getAttachmentUrl("sa_paybill", sa_paybillid));
  289. }
  290. return getSucReturnObject().setData(rows.get(0)).toString();
  291. }
  292. @API(title = "退回", apiversion = R.ID20221227110104.v1.class)
  293. @CACHEING_CLEAN(apiversions = {R.ID20221226152904.v1.class, R.ID20230202100104.v1.class})
  294. public String reback() throws YosException {
  295. Long sa_paybillid = content.getLong("sa_paybillid");
  296. SQLFactory sqlFactory = new SQLFactory(this, "打款凭证退回");
  297. sqlFactory.addParameter("siteid", siteid);
  298. sqlFactory.addParameter("sa_paybillid", sa_paybillid);
  299. dbConnect.runSqlUpdate(sqlFactory.getSQL());
  300. Rows rows = dbConnect.runSqlQuery("select * from sa_paybill where siteid='" + siteid + "' and sa_paybillid=" + sa_paybillid);
  301. if (!rows.isEmpty()) {
  302. String message = "您的打款凭证【" + rows.get(0).getString("billno") + "】已退回!";
  303. sendMsg(message, sa_paybillid, sys_enterpriseid);
  304. }
  305. return getSucReturnObject().toString();
  306. }
  307. @API(title = "查询营销账户类型", apiversion = R.ID20221228085004.v1.class)
  308. public String queryAccountclass() throws YosException {
  309. Long sa_paybillid = content.getLong("sa_paybillid");
  310. StringBuffer where = new StringBuffer(" 1=1 ");
  311. if (content.containsKey("where")) {
  312. JSONObject whereObject = content.getJSONObject("where");
  313. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  314. where.append(" and(accountname like'%").append(whereObject.getString("condition")).append("%'");
  315. where.append(" or accountno like '%").append(whereObject.getString("condition")).append("%')");
  316. }
  317. }
  318. SQLFactory sqlFactory = new SQLFactory(this, "营销账户类型列表查询");
  319. sqlFactory.addParameter("siteid", siteid);
  320. sqlFactory.addParameter("sa_paybillid", sa_paybillid);
  321. sqlFactory.addParameter_SQL("where", where);
  322. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  323. return getSucReturnObject().setData(rows).toString();
  324. }
  325. }