Paybill.java 18 KB

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