Accountbalance.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. package beans.accountbalance;
  2. import beans.remind.Remind;
  3. import com.alibaba.fastjson.JSONObject;
  4. import common.BaseClass;
  5. import common.Controller;
  6. import common.YosException;
  7. import common.data.Rows;
  8. import common.data.SQLFactory;
  9. import org.apache.commons.lang.StringUtils;
  10. import restcontroller.webmanage.executorService.Executor;
  11. import java.math.BigDecimal;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. public class Accountbalance extends BaseClass {
  15. /**
  16. * @param controller
  17. * @param sys_enterpriseid //企业id
  18. * @param sa_accountclassid //营销账户id
  19. * @return AccountbalanceEntity //营销账户实体(可用余额(getCanuseamount),账户余额(getBalance),信用额度(getCreditquota))
  20. * @throws YosException
  21. */
  22. public static AccountbalanceEntity getAccountbalance(Controller controller, long sys_enterpriseid, long sa_accountclassid) throws YosException {
  23. SQLFactory sqlFactory = new SQLFactory(new Accountbalance(), "账户余额查询");
  24. sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  25. sqlFactory.addParameter("sa_accountclassid", sa_accountclassid);
  26. sqlFactory.addParameter("siteid", controller.siteid);
  27. Rows rows = controller.dbConnect.runSqlQuery(sqlFactory.getSQL());
  28. AccountbalanceEntity accountbalanceEntity = new AccountbalanceEntity();
  29. if (!rows.isEmpty()) {
  30. accountbalanceEntity.setBalance(rows.get(0).getBigDecimal("balance"));
  31. accountbalanceEntity.setCreditquota(rows.get(0).getBigDecimal("creditquota"));
  32. accountbalanceEntity.setDiscountamount(rows.get(0).getBigDecimal("discountamount"));
  33. accountbalanceEntity.setFreezamount(rows.get(0).getBigDecimal("freezamount"));
  34. accountbalanceEntity.setCanuseamount(rows.get(0).getBigDecimal("balance").add(rows.get(0).getBigDecimal("creditquota")).add(rows.get(0).getBigDecimal("discountamount")).subtract(rows.get(0).getBigDecimal("freezamount")));
  35. }
  36. return accountbalanceEntity;
  37. }
  38. /**
  39. * 判断当前账户余额是否足够
  40. *
  41. * @param controller
  42. * @param sys_enterpriseid
  43. * @param sa_accountclassid
  44. * @param amount
  45. * @return
  46. * @throws YosException
  47. */
  48. public static boolean judgeBalance(Controller controller, long sys_enterpriseid, long sa_accountclassid, BigDecimal amount) throws YosException {
  49. AccountbalanceEntity accountbalanceEntity = getAccountbalance(controller, sys_enterpriseid, sa_accountclassid);
  50. BigDecimal canuseamount = accountbalanceEntity.getCanuseamount();
  51. if (canuseamount != null) {
  52. if (canuseamount.compareTo(amount) == 1 || canuseamount.compareTo(amount) == 0) {
  53. return true;
  54. } else {
  55. return false;
  56. }
  57. } else {
  58. return false;
  59. }
  60. }
  61. /**
  62. * 返回不足金额
  63. *
  64. * @param controller
  65. * @param sys_enterpriseid
  66. * @param sa_accountclassid
  67. * @param amount
  68. * @return
  69. * @throws YosException
  70. */
  71. public static BigDecimal InsufficientBalance(Controller controller, long sys_enterpriseid, long sa_accountclassid, BigDecimal amount) throws YosException {
  72. AccountbalanceEntity accountbalanceEntity = getAccountbalance(controller, sys_enterpriseid, sa_accountclassid);
  73. BigDecimal canuseamount = accountbalanceEntity.getCanuseamount();
  74. if (canuseamount != null) {
  75. if (canuseamount.compareTo(amount) == 1 || canuseamount.compareTo(amount) == 0) {
  76. return BigDecimal.ZERO;
  77. } else {
  78. return amount.subtract(canuseamount);
  79. }
  80. } else {
  81. return amount;
  82. }
  83. }
  84. /**
  85. * 创建收入凭证(返回SQL)
  86. *
  87. * @param controller
  88. * @param sys_enterpriseid 企业id
  89. * @param sa_accountclassid 账户id
  90. * @param cashbillEntity 收支凭证实体
  91. * @param ischeck 是否审核
  92. * @return
  93. * @throws YosException
  94. */
  95. public static JSONObject createCashbillIncome(Controller controller, long sys_enterpriseid, long sa_accountclassid, CashbillEntity cashbillEntity, boolean ischeck, boolean isupdatek3flag) throws YosException {
  96. ArrayList<String> sqlList = new ArrayList<>();
  97. long sa_cashbillid = controller.createTableID("sa_cashbill");
  98. String billcode = controller.createBillCode("cashbill");
  99. SQLFactory sqlFactory = new SQLFactory(new Accountbalance(), "收支凭证新增");
  100. sqlFactory.addParameter("billno", billcode);
  101. sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  102. sqlFactory.addParameter("sa_accountclassid", sa_accountclassid);
  103. sqlFactory.addParameter("type", 1);
  104. sqlFactory.addParameter("siteid", controller.siteid);
  105. sqlFactory.addParameter("remarks", cashbillEntity.getRemarks());
  106. sqlFactory.addParameter("amount", cashbillEntity.getAmount());
  107. sqlFactory.addParameter("discountamount", cashbillEntity.getDiscountamountamount());
  108. sqlFactory.addParameter("sa_cashbillid", sa_cashbillid);
  109. sqlFactory.addParameter("userid", controller.userid);
  110. sqlFactory.addParameter("username", controller.username);
  111. sqlFactory.addParameter("ownertable", cashbillEntity.getOwnertable());
  112. sqlFactory.addParameter("ownerid", cashbillEntity.getOwnerid());
  113. sqlFactory.addParameter("source", cashbillEntity.getSource());
  114. sqlFactory.addParameter("sourcenote", cashbillEntity.getSourcenote());
  115. sqlFactory.addParameter("sa_cashbillid_rebate", "null");
  116. sqlFactory.addParameter("class", cashbillEntity.getType());
  117. sqlFactory.addParameter("subclass", cashbillEntity.getTypemx());
  118. sqlFactory.addParameter("period", cashbillEntity.getPeriod());
  119. if (ischeck) {
  120. BigDecimal balance = BigDecimal.ZERO; //当前账户余额
  121. sqlFactory.addParameter("status", "审核");
  122. if (isupdatek3flag) {
  123. sqlFactory.addParameter("updatek3flag", "0");
  124. } else {
  125. sqlFactory.addParameter("updatek3flag", "");
  126. }
  127. sqlFactory.addParameter("checkby", StringUtils.isBlank(controller.username) ? "autocheck" : controller.username);
  128. sqlFactory.addParameter_SQL("checkdate", "CURRENT_TIME");
  129. // 审核逻辑判断
  130. Rows rowsaccountbalance = controller.dbConnect.runSqlQuery(
  131. "select sa_accountbalanceid,balance,creditquota,discountamount from sa_accountbalance where sys_enterpriseid ='"
  132. + sys_enterpriseid + "' and sa_accountclassid='" + sa_accountclassid + "' and siteid='"
  133. + controller.siteid + "'");
  134. if (rowsaccountbalance.isEmpty()) {
  135. SQLFactory accountbalanceaddSqlFactory = new SQLFactory(new Accountbalance(), "营销账户余额新增");
  136. accountbalanceaddSqlFactory.addParameter("sa_accountbalanceid",
  137. controller.createTableID("sa_accountbalance"));
  138. accountbalanceaddSqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  139. accountbalanceaddSqlFactory.addParameter("sa_accountclassid", sa_accountclassid);
  140. accountbalanceaddSqlFactory.addParameter("siteid", controller.siteid);
  141. accountbalanceaddSqlFactory.addParameter("balance", cashbillEntity.getAmount());
  142. accountbalanceaddSqlFactory.addParameter("discountamount", cashbillEntity.getDiscountamountamount());
  143. accountbalanceaddSqlFactory.addParameter("userid", controller.userid);
  144. accountbalanceaddSqlFactory.addParameter("username", controller.username);
  145. balance = cashbillEntity.getAmount();
  146. sqlList.add(accountbalanceaddSqlFactory.getSQL());
  147. } else {
  148. BigDecimal newbalance = cashbillEntity.getAmount().add(rowsaccountbalance.get(0).getBigDecimal("balance"));
  149. BigDecimal newdiscountamount = cashbillEntity.getDiscountamountamount().add(rowsaccountbalance.get(0).getBigDecimal("discountamount"));
  150. sqlList.add("update sa_accountbalance set balance='" + newbalance
  151. + "',discountamount='"+newdiscountamount+"',changedate=CURRENT_TIME,changeby ='" + controller.username + "',changeuserid='"
  152. + controller.userid + "' where sys_enterpriseid ='" + sys_enterpriseid
  153. + "' and sa_accountclassid='" + sa_accountclassid + "' and siteid='" + controller.siteid
  154. + "'");
  155. balance = newbalance;
  156. }
  157. sqlFactory.addParameter("balance", balance);
  158. Rows accountclassrows = controller.dbConnect.runSqlQuery("select * from sa_accountclass where siteid='" + controller.siteid + "' and sa_accountclassid=" + sa_accountclassid);
  159. if (!accountclassrows.isEmpty()) {
  160. String content = "您的" + accountclassrows.get(0).getString("accountname") + "已收入【" + cashbillEntity.getAmount() + "】元";
  161. new Accountbalance().sendMsg(controller, content, cashbillEntity.getOwnerid(), sys_enterpriseid);
  162. }
  163. } else {
  164. sqlFactory.addParameter("status", "新建");
  165. sqlFactory.addParameter("checkby", "null");
  166. sqlFactory.addParameter("checkdate", "null");
  167. sqlFactory.addParameter("updatek3flag", "");
  168. sqlFactory.addParameter("balance", 0);
  169. }
  170. sqlList.add(sqlFactory.getSQL());
  171. JSONObject jsonObject = new JSONObject();
  172. jsonObject.put("sa_cashbillid", sa_cashbillid);
  173. jsonObject.put("sqlList", sqlList);
  174. return jsonObject;
  175. }
  176. /**
  177. * 创建支出凭证(返回SQL)
  178. *
  179. * @param controller
  180. * @param sys_enterpriseid 企业id
  181. * @param sa_accountclassid 账户id
  182. * @param cashbillEntity 收支凭证实体
  183. * @param ischeck 是否审核
  184. * @return
  185. * @throws YosException
  186. */
  187. public static JSONObject createCashbillPay(Controller controller, long sys_enterpriseid, long sa_accountclassid, CashbillEntity cashbillEntity, boolean ischeck) throws YosException {
  188. ArrayList<String> sqlList = new ArrayList<>();
  189. long sa_cashbillid = controller.createTableID("sa_cashbill");
  190. String billcode = controller.createBillCode("cashbill");
  191. SQLFactory sqlFactory = new SQLFactory(new Accountbalance(), "收支凭证新增");
  192. sqlFactory.addParameter("billno", billcode);
  193. sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  194. sqlFactory.addParameter("sa_accountclassid", sa_accountclassid);
  195. sqlFactory.addParameter("type", 0);
  196. sqlFactory.addParameter("siteid", controller.siteid);
  197. sqlFactory.addParameter("remarks", cashbillEntity.getRemarks());
  198. sqlFactory.addParameter("amount", cashbillEntity.getAmount());
  199. sqlFactory.addParameter("discountamount", cashbillEntity.getDiscountamountamount());
  200. sqlFactory.addParameter("sa_cashbillid", sa_cashbillid);
  201. sqlFactory.addParameter("userid", controller.userid);
  202. sqlFactory.addParameter("username", controller.username);
  203. sqlFactory.addParameter("ownertable", cashbillEntity.getOwnertable());
  204. sqlFactory.addParameter("ownerid", cashbillEntity.getOwnerid());
  205. sqlFactory.addParameter("source", cashbillEntity.getSource());
  206. sqlFactory.addParameter("sourcenote", cashbillEntity.getSourcenote());
  207. sqlFactory.addParameter("class", cashbillEntity.getType());
  208. sqlFactory.addParameter("subclass", cashbillEntity.getTypemx());
  209. sqlFactory.addParameter("sa_cashbillid_rebate", "null");
  210. sqlFactory.addParameter("updatek3flag", "");
  211. sqlFactory.addParameter("period", cashbillEntity.getPeriod());
  212. if (ischeck) {
  213. BigDecimal balance = BigDecimal.ZERO; //当前账户余额
  214. sqlFactory.addParameter("status", "审核");
  215. sqlFactory.addParameter("checkby", controller.username);
  216. sqlFactory.addParameter_SQL("checkdate", "CURRENT_TIME");
  217. // 审核逻辑判断
  218. Rows rowsaccountbalance = controller.dbConnect.runSqlQuery(
  219. "select sa_accountbalanceid,balance,creditquota,discountamount from sa_accountbalance where sys_enterpriseid ='"
  220. + sys_enterpriseid + "' and sa_accountclassid='" + sa_accountclassid + "' and siteid='"
  221. + controller.siteid + "'");
  222. if (rowsaccountbalance.isEmpty()) {
  223. // SQLFactory sqlFactory = new SQLFactory(this, "营销账户余额新增");
  224. // sqlFactory.addParameter("sa_accountbalanceid", createTableID("sa_accountbalance"));
  225. // sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  226. // sqlFactory.addParameter("sa_accountclassid", sa_accountclassid);
  227. // sqlFactory.addParameter("siteid", siteid);
  228. // sqlFactory.addParameter("balance", -rows.get(0).getLong("amount"));
  229. // sqlFactory.addParameter("userid", userid);
  230. // sqlFactory.addParameter("username", username);
  231. // sqlList.add(sqlFactory.getSQL());
  232. throw new YosException(false, "该营销账户不存在,支出凭证无法审核");
  233. } else {
  234. BigDecimal canuseamount = rowsaccountbalance.get(0).getBigDecimal("creditquota").add(rowsaccountbalance.get(0).getBigDecimal("balance")).add(rowsaccountbalance.get(0).getBigDecimal("discountamount"));
  235. if ((cashbillEntity.getAmount().add(cashbillEntity.getDiscountamountamount())).compareTo(canuseamount) == 1) {
  236. throw new YosException(false, "该营销账户可用余额不足,支出凭证无法审核");
  237. }
  238. BigDecimal newbalance = rowsaccountbalance.get(0).getBigDecimal("balance").subtract(cashbillEntity.getAmount());
  239. BigDecimal newdiscountamount = rowsaccountbalance.get(0).getBigDecimal("discountamount").subtract(cashbillEntity.getDiscountamountamount());
  240. sqlList.add("update sa_accountbalance set balance='" + newbalance + "',discountamount='"+newdiscountamount+"',changedate=CURRENT_TIME,changeby ='" + controller.username + "',changeuserid='" + controller.userid + "' where sys_enterpriseid ='" + sys_enterpriseid + "' and sa_accountclassid='" + sa_accountclassid + "' and siteid='" + controller.siteid + "'");
  241. balance = newbalance;
  242. }
  243. sqlFactory.addParameter("balance", balance);
  244. Rows accountclassrows = controller.dbConnect.runSqlQuery("select * from sa_accountclass where siteid='" + controller.siteid + "' and sa_accountclassid=" + sa_accountclassid);
  245. if (!accountclassrows.isEmpty()) {
  246. String content = "您的" + accountclassrows.get(0).getString("accountname") + "已支出【" + cashbillEntity.getAmount() + "】元";
  247. new Accountbalance().sendMsg(controller, content, cashbillEntity.getOwnerid(), sys_enterpriseid);
  248. }
  249. } else {
  250. sqlFactory.addParameter("status", "新建");
  251. sqlFactory.addParameter("checkby", "");
  252. sqlFactory.addParameter("checkdate", "");
  253. sqlFactory.addParameter("balance", 0);
  254. sqlFactory.addParameter("updatek3flag", "");
  255. }
  256. sqlList.add(sqlFactory.getSQL());
  257. JSONObject jsonObject = new JSONObject();
  258. jsonObject.put("sa_cashbillid", sa_cashbillid);
  259. jsonObject.put("sqlList", sqlList);
  260. return jsonObject;
  261. }
  262. //订单手动关闭专用
  263. public static JSONObject createCashbillPay2(Controller controller, long sys_enterpriseid, long sa_accountclassid, CashbillEntity cashbillEntity, boolean ischeck) throws YosException {
  264. ArrayList<String> sqlList = new ArrayList<>();
  265. long sa_cashbillid = controller.createTableID("sa_cashbill");
  266. String billcode = controller.createBillCode("cashbill");
  267. SQLFactory sqlFactory = new SQLFactory(new Accountbalance(), "收支凭证新增");
  268. sqlFactory.addParameter("billno", billcode);
  269. sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  270. sqlFactory.addParameter("sa_accountclassid", sa_accountclassid);
  271. sqlFactory.addParameter("type", 0);
  272. sqlFactory.addParameter("siteid", controller.siteid);
  273. sqlFactory.addParameter("remarks", cashbillEntity.getRemarks());
  274. sqlFactory.addParameter("amount", cashbillEntity.getAmount());
  275. sqlFactory.addParameter("discountamount", cashbillEntity.getDiscountamountamount());
  276. sqlFactory.addParameter("sa_cashbillid", sa_cashbillid);
  277. sqlFactory.addParameter("userid", controller.userid);
  278. sqlFactory.addParameter("username", controller.username);
  279. sqlFactory.addParameter("ownertable", cashbillEntity.getOwnertable());
  280. sqlFactory.addParameter("ownerid", cashbillEntity.getOwnerid());
  281. sqlFactory.addParameter("source", cashbillEntity.getSource());
  282. sqlFactory.addParameter("sourcenote", cashbillEntity.getSourcenote());
  283. sqlFactory.addParameter("class", cashbillEntity.getType());
  284. sqlFactory.addParameter("subclass", cashbillEntity.getTypemx());
  285. sqlFactory.addParameter("sa_cashbillid_rebate", "null");
  286. sqlFactory.addParameter("updatek3flag", "");
  287. sqlFactory.addParameter("period", cashbillEntity.getPeriod());
  288. if (ischeck) {
  289. BigDecimal balance = BigDecimal.ZERO; //当前账户余额
  290. sqlFactory.addParameter("status", "审核");
  291. sqlFactory.addParameter("checkby", controller.username);
  292. sqlFactory.addParameter_SQL("checkdate", "CURRENT_TIME");
  293. // 审核逻辑判断
  294. Rows rowsaccountbalance = controller.dbConnect.runSqlQuery(
  295. "select sa_accountbalanceid,balance,creditquota from sa_accountbalance where sys_enterpriseid ='"
  296. + sys_enterpriseid + "' and sa_accountclassid='" + sa_accountclassid + "' and siteid='"
  297. + controller.siteid + "'");
  298. if (rowsaccountbalance.isEmpty()) {
  299. // SQLFactory sqlFactory = new SQLFactory(this, "营销账户余额新增");
  300. // sqlFactory.addParameter("sa_accountbalanceid", createTableID("sa_accountbalance"));
  301. // sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  302. // sqlFactory.addParameter("sa_accountclassid", sa_accountclassid);
  303. // sqlFactory.addParameter("siteid", siteid);
  304. // sqlFactory.addParameter("balance", -rows.get(0).getLong("amount"));
  305. // sqlFactory.addParameter("userid", userid);
  306. // sqlFactory.addParameter("username", username);
  307. // sqlList.add(sqlFactory.getSQL());
  308. throw new YosException(false, "该营销账户不存在,支出凭证无法审核");
  309. } else {
  310. // BigDecimal canuseamount = rowsaccountbalance.get(0).getBigDecimal("creditquota").add(rowsaccountbalance.get(0).getBigDecimal("balance"));
  311. // if (cashbillEntity.getAmount().compareTo(canuseamount) == 1) {
  312. // throw new YosException(false, "该营销账户可用余额不足,支出凭证无法审核");
  313. // }
  314. BigDecimal newbalance = rowsaccountbalance.get(0).getBigDecimal("balance").subtract(cashbillEntity.getAmount());
  315. BigDecimal newdiscountamount = rowsaccountbalance.get(0).getBigDecimal("discountamount").subtract(cashbillEntity.getDiscountamountamount());
  316. sqlList.add("update sa_accountbalance set balance='" + newbalance + "',discountamount='"+newdiscountamount+"',changedate=CURRENT_TIME,changeby ='" + controller.username + "',changeuserid='" + controller.userid + "' where sys_enterpriseid ='" + sys_enterpriseid + "' and sa_accountclassid='" + sa_accountclassid + "' and siteid='" + controller.siteid + "'");
  317. balance = newbalance;
  318. }
  319. sqlFactory.addParameter("balance", balance);
  320. Rows accountclassrows = controller.dbConnect.runSqlQuery("select * from sa_accountclass where siteid='" + controller.siteid + "' and sa_accountclassid=" + sa_accountclassid);
  321. if (!accountclassrows.isEmpty()) {
  322. String content = "您的" + accountclassrows.get(0).getString("accountname") + "已支出【" + cashbillEntity.getAmount() + "】元";
  323. new Accountbalance().sendMsg(controller, content, cashbillEntity.getOwnerid(), sys_enterpriseid);
  324. }
  325. } else {
  326. sqlFactory.addParameter("status", "新建");
  327. sqlFactory.addParameter("checkby", "");
  328. sqlFactory.addParameter("checkdate", "");
  329. sqlFactory.addParameter("balance", 0);
  330. sqlFactory.addParameter("updatek3flag", "");
  331. }
  332. sqlList.add(sqlFactory.getSQL());
  333. JSONObject jsonObject = new JSONObject();
  334. jsonObject.put("sa_cashbillid", sa_cashbillid);
  335. jsonObject.put("sqlList", sqlList);
  336. return jsonObject;
  337. }
  338. public static JSONObject createCashbillPay(Controller controller, long sys_enterpriseid, long sa_accountclassid, CashbillEntity cashbillEntity, boolean ischeck, Long sa_cashbillid_rebate) throws YosException {
  339. ArrayList<String> sqlList = new ArrayList<>();
  340. long sa_cashbillid = controller.createTableID("sa_cashbill");
  341. String billcode = controller.createBillCode("cashbill");
  342. SQLFactory sqlFactory = new SQLFactory(new Accountbalance(), "收支凭证新增");
  343. sqlFactory.addParameter("billno", billcode);
  344. sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  345. sqlFactory.addParameter("sa_accountclassid", sa_accountclassid);
  346. sqlFactory.addParameter("type", 0);
  347. sqlFactory.addParameter("siteid", controller.siteid);
  348. sqlFactory.addParameter("remarks", cashbillEntity.getRemarks());
  349. sqlFactory.addParameter("amount", cashbillEntity.getAmount());
  350. sqlFactory.addParameter("discountamount", cashbillEntity.getDiscountamountamount());
  351. sqlFactory.addParameter("sa_cashbillid", sa_cashbillid);
  352. sqlFactory.addParameter("userid", controller.userid);
  353. sqlFactory.addParameter("username", controller.username);
  354. sqlFactory.addParameter("ownertable", cashbillEntity.getOwnertable());
  355. sqlFactory.addParameter("ownerid", cashbillEntity.getOwnerid());
  356. sqlFactory.addParameter("source", cashbillEntity.getSource());
  357. sqlFactory.addParameter("sourcenote", cashbillEntity.getSourcenote());
  358. sqlFactory.addParameter("sa_cashbillid_rebate", sa_cashbillid_rebate);
  359. sqlFactory.addParameter("class", cashbillEntity.getType());
  360. sqlFactory.addParameter("subclass", cashbillEntity.getTypemx());
  361. sqlFactory.addParameter("period", cashbillEntity.getPeriod());
  362. if (ischeck) {
  363. BigDecimal balance = BigDecimal.ZERO; //当前账户余额
  364. sqlFactory.addParameter("status", "审核");
  365. sqlFactory.addParameter("checkby", controller.username);
  366. sqlFactory.addParameter_SQL("checkdate", "CURRENT_TIME");
  367. // 审核逻辑判断
  368. Rows rowsaccountbalance = controller.dbConnect.runSqlQuery(
  369. "select sa_accountbalanceid,balance,creditquota from sa_accountbalance where sys_enterpriseid ='"
  370. + sys_enterpriseid + "' and sa_accountclassid='" + sa_accountclassid + "' and siteid='"
  371. + controller.siteid + "'");
  372. if (rowsaccountbalance.isEmpty()) {
  373. throw new YosException(false, "该营销账户不存在,支出凭证无法审核");
  374. } else {
  375. BigDecimal canuseamount = rowsaccountbalance.get(0).getBigDecimal("creditquota").add(rowsaccountbalance.get(0).getBigDecimal("balance"));
  376. if (cashbillEntity.getAmount().compareTo(canuseamount) == 1 ) {
  377. throw new YosException(false, "该营销账户可用余额不足,支出凭证无法审核");
  378. }
  379. BigDecimal newbalance = rowsaccountbalance.get(0).getBigDecimal("balance").subtract(cashbillEntity.getAmount());
  380. BigDecimal newdiscountamount = rowsaccountbalance.get(0).getBigDecimal("discountamount").subtract(cashbillEntity.getDiscountamountamount());
  381. sqlList.add("update sa_accountbalance set balance='" + newbalance + "',discountamount='"+newdiscountamount+"',changedate=CURRENT_TIME,changeby ='" + controller.username + "',changeuserid='" + controller.userid + "' where sys_enterpriseid ='" + sys_enterpriseid + "' and sa_accountclassid='" + sa_accountclassid + "' and siteid='" + controller.siteid + "'");
  382. balance = newbalance;
  383. }
  384. sqlFactory.addParameter("balance", balance);
  385. Rows accountclassrows = controller.dbConnect.runSqlQuery("select * from sa_accountclass where siteid='" + controller.siteid + "' and sa_accountclassid=" + sa_accountclassid);
  386. if (!accountclassrows.isEmpty()) {
  387. String content = "您的" + accountclassrows.get(0).getString("accountname") + "已支出【" + cashbillEntity.getAmount() + "】元";
  388. new Accountbalance().sendMsg(controller, content, cashbillEntity.getOwnerid(), sys_enterpriseid);
  389. }
  390. } else {
  391. sqlFactory.addParameter("status", "新建");
  392. sqlFactory.addParameter("checkby", "");
  393. sqlFactory.addParameter("checkdate", "");
  394. sqlFactory.addParameter("balance", 0);
  395. }
  396. sqlList.add(sqlFactory.getSQL());
  397. JSONObject jsonObject = new JSONObject();
  398. jsonObject.put("sa_cashbillid", sa_cashbillid);
  399. jsonObject.put("sqlList", sqlList);
  400. return jsonObject;
  401. }
  402. /**
  403. * 创建收入凭证(直接执行SQL语句)
  404. *
  405. * @param controller
  406. * @param sys_enterpriseid 企业id
  407. * @param sa_accountclassid 账户id
  408. * @param cashbillEntity 凭证实体
  409. * @param ischeck 是否审核
  410. * @throws YosException
  411. */
  412. public static void createCashbillIncomeWithoutSQL(Controller controller, long sys_enterpriseid, long sa_accountclassid,
  413. CashbillEntity cashbillEntity,
  414. boolean ischeck, boolean isupdatek3flag) throws YosException {
  415. JSONObject jsonObject = createCashbillIncome(controller, sys_enterpriseid, sa_accountclassid, cashbillEntity, ischeck, isupdatek3flag);
  416. controller.dbConnect.runSqlUpdate(new ArrayList<>(jsonObject.getJSONArray("sqlList").toJavaList(String.class)));
  417. if (ischeck) {
  418. remindSend(controller, sys_enterpriseid, jsonObject.getLong("sa_cashbillid"));
  419. }
  420. }
  421. /**
  422. * 创建支出凭证(直接执行SQL语句)
  423. *
  424. * @param controller
  425. * @param sys_enterpriseid 企业id
  426. * @param sa_accountclassid 账户id
  427. * @param cashbillEntity 凭证实体
  428. * @param ischeck 是否审核
  429. * @throws YosException
  430. */
  431. public static void createCashbillPayWithoutSQL(Controller controller, long sys_enterpriseid, long sa_accountclassid,
  432. CashbillEntity cashbillEntity,
  433. boolean ischeck) throws YosException {
  434. JSONObject jsonObject = createCashbillPay(controller, sys_enterpriseid, sa_accountclassid, cashbillEntity, ischeck);
  435. controller.dbConnect.runSqlUpdate(new ArrayList<>(jsonObject.getJSONArray("sqlList").toJavaList(String.class)));
  436. if (ischeck) {
  437. remindSend(controller, sys_enterpriseid, jsonObject.getLong("sa_cashbillid"));
  438. }
  439. }
  440. /**
  441. * 查询企业审核通过的有效期内的返利金余额
  442. *
  443. * @param controller
  444. * @param sys_enterpriseid
  445. * @return
  446. * @throws YosException
  447. */
  448. public static BigDecimal getRebateBalance(Controller controller, Long sys_enterpriseid) throws YosException {
  449. Rows rows = controller.dbConnect.runSqlQuery("SELECT sum(rebate_balance) rebate_balance from sa_cashbill WHERE class='返利金' and STATUS ='审核' and rebate_enddate>=CURRENT_TIME and sys_enterpriseid= " + sys_enterpriseid + " and siteid ='" + controller.siteid + "'");
  450. return rows.get(0).getBigDecimal("rebate_balance");
  451. }
  452. /**
  453. * 更新经销商装修返利表金额
  454. *
  455. * @param controller
  456. * @param sys_enterpriseid
  457. * @param isrebateupdate
  458. * @param amount
  459. * @return
  460. * @throws YosException
  461. */
  462. public static ArrayList<String> rebateupdate(Controller controller, Long sys_enterpriseid, boolean isrebateupdate, BigDecimal amount) throws YosException {
  463. ArrayList<String> sqlList = new ArrayList<>();
  464. Rows rows = controller.dbConnect.runSqlQuery("SELECT * from sa_agentdecorationtrebate where sys_enterpriseid= " + sys_enterpriseid + " and siteid ='" + controller.siteid + "'");
  465. if (!rows.isEmpty()) {
  466. if (isrebateupdate) {
  467. sqlList.add("update sa_agentdecorationtrebate set balance=" + rows.get(0).getBigDecimal("balance").subtract(amount) + " where sa_agentdecorationtrebateid=" + rows.get(0).getLong("sa_agentdecorationtrebateid") + " and siteid ='" + controller.siteid + "'");
  468. } else {
  469. sqlList.add("update sa_agentdecorationtrebate set balance=" + rows.get(0).getBigDecimal("balance").add(amount) + " where sa_agentdecorationtrebateid=" + rows.get(0).getLong("sa_agentdecorationtrebateid") + " and siteid ='" + controller.siteid + "'");
  470. }
  471. }
  472. return sqlList;
  473. }
  474. public void sendMsg(Controller controller, String content, Long sa_orderid, Long sys_enterpriseid) throws YosException {
  475. ArrayList<Long> userList = getEnterpriseHrs(sys_enterpriseid, controller.siteid).toArrayList("userid", new ArrayList<>());
  476. Remind remind = new Remind(controller);
  477. remind.setTitle("凭证消息");
  478. remind.setContent(content);
  479. remind.setType("应用");
  480. remind.setObjectid(sa_orderid);
  481. remind.setObjectname("sa_order");
  482. remind.setTouserid(userList);
  483. remind.createSys_message();
  484. }
  485. public void sendMsg(String siteid, String content, Long sa_orderid, Long sys_enterpriseid) throws YosException {
  486. ArrayList<Long> userList = getEnterpriseHrs(sys_enterpriseid, siteid).toArrayList("userid", new ArrayList<>());
  487. Remind remind = new Remind(siteid);
  488. remind.setTitle("凭证消息");
  489. remind.setContent(content);
  490. remind.setType("应用");
  491. remind.setObjectid(sa_orderid);
  492. remind.setObjectname("sa_order");
  493. remind.setTouserid(userList);
  494. remind.createSys_message();
  495. }
  496. public static void remindSend(Controller controller, Long sys_enterpriseid, Long sa_cashbillid) throws YosException {
  497. if (sa_cashbillid != 0) {
  498. String siteid = controller.siteid;
  499. Rows enterpriseHrRows = controller.dbConnect.runSqlQuery("select userid from sys_enterprise_hr where siteid='" + siteid + "' and isleader=1 and sys_enterpriseid=" + sys_enterpriseid);
  500. ArrayList<Long> extraUserList = enterpriseHrRows.toArrayList("userid", new ArrayList<>());
  501. HashMap<String, Object> extradata = new HashMap<>();
  502. extradata.put("extraUserList", extraUserList);
  503. Executor.sendEml(controller, "cashbill_check", sa_cashbillid, siteid, extradata);
  504. }
  505. }
  506. }