XSCK.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package beans.stockbill.bills;
  2. import beans.accountbalance.Accountbalance;
  3. import beans.accountbalance.CashbillEntity;
  4. import beans.parameter.Parameter;
  5. import com.alibaba.fastjson2.JSONObject;
  6. import common.Controller;
  7. import common.YosException;
  8. import common.data.*;
  9. import org.apache.commons.lang.StringUtils;
  10. import java.math.BigDecimal;
  11. import java.util.ArrayList;
  12. public class XSCK extends BasicBill {
  13. long sys_enterpriseid;
  14. public XSCK(Controller controller, long st_stockbillid) throws YosException {
  15. super(controller, st_stockbillid);
  16. sys_enterpriseid = billRow.getLong("sys_enterpriseid");
  17. }
  18. @Override
  19. public void checkValidate(boolean ischeck) throws YosException {
  20. super.checkValidate(ischeck);
  21. Rows accountbalancerows = dbConnect.runSqlQuery("select * from sa_accountbalance t1 inner join sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid where t2.accountname='现金账户' and t1.sys_enterpriseid=" + sys_enterpriseid);
  22. if (accountbalancerows.isEmpty()) {
  23. throw new YosException("账户不存在");
  24. }
  25. String order_paymentnode = Parameter.getString(siteid, "order_paymentnode");
  26. if (ischeck) {
  27. if (itemRows.min("payamount").doubleValue() < 0) {
  28. throw new YosException("扣款金额需大于0");
  29. }
  30. if (StringUtils.isBlank(billRow.getString("outplace"))) {
  31. throw new YosException("发货地不可为空");
  32. }
  33. if ((billRow.getBigDecimal("payamount").add(billRow.getBigDecimal("paydiscountamount"))).compareTo(itemRows.sum("amount")) != 0) {
  34. throw new YosException("扣款金额与应扣金额不符,不能审核");
  35. }
  36. BigDecimal discountamount = accountbalancerows.get(0).getBigDecimal("discountamount");
  37. if (billRow.getBigDecimal("paydiscountamount").compareTo(discountamount) > 0) {
  38. throw new YosException("优惠金额不能大于可用优惠金额");
  39. }
  40. /*
  41. 库存校验
  42. */
  43. /*
  44. 其他校验
  45. */
  46. } else {
  47. if (billRow.getBoolean("isreceiver")) {
  48. throw new YosException("经销商已经确认收货,不可进行反审核");
  49. }
  50. /*
  51. 库存校验
  52. */
  53. /*
  54. 其他校验
  55. */
  56. }
  57. if ((ischeck && rb == 1) || (!ischeck && rb == 0)) {
  58. if (order_paymentnode.equals("3") && !Accountbalance.judgeBalance(dbConnect, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), itemRows.sum("amount"))) {
  59. throw new YosException("账户余额不足,还差" + Accountbalance.InsufficientBalance(dbConnect, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), itemRows.sum("amount")) + "元!");
  60. }
  61. Rows rowsjudge = dbConnect.runSqlQuery("select * from (select sa_orderitemsid,sum(qty) qty from st_stockbill_items where st_stockbillid ='" + st_stockbillid + "' group by sa_orderitemsid) t1 inner join sa_orderitems t2 on t1.sa_orderitemsid=t2.sa_orderitemsid where t1.qty>t2.undeliqty");
  62. if (rowsjudge.isNotEmpty()) {
  63. throw new YosException("存在商品已完全出库,无法反审核");
  64. }
  65. }
  66. }
  67. @Override
  68. public ArrayList<String> getCheckSql(boolean ischeck) throws YosException {
  69. ArrayList<String> sqlList = super.getCheckSql(ischeck);
  70. if (ischeck) {
  71. Rows accountbalancerows = dbConnect.runSqlQuery("select * from sa_accountbalance t1 inner join sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid where t2.accountname='现金账户' and t1.sys_enterpriseid=" + sys_enterpriseid);
  72. String order_paymentnode = Parameter.getString(siteid, "order_paymentnode");
  73. if (rb == 1) {
  74. if (order_paymentnode.equals("3")) {
  75. CashbillEntity entity = getCashbillEntity(billRow.getBigDecimal("payamount"), billRow.getBigDecimal("paydiscountamount"), st_stockbillid, "销售出库单审核", billno, "由销售出库单" + billno + "审核时生成");
  76. JSONObject createCashbillPay = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity, true);
  77. sqlList.addAll(createCashbillPay.getJSONArray("sqlList").toJavaList(String.class));
  78. }
  79. Rows rowsDispatchDetailGroup = dbConnect.runSqlQuery("select sum(t1.qty) qty,t1.sa_orderitemsid,t1.siteid from st_stockbill_items t1 where t1.sa_orderitemsid>0 and t1.st_stockbillid=" + st_stockbillid + " group by t1.sa_orderitemsid,t1.siteid");
  80. for (Row row : rowsDispatchDetailGroup) {
  81. sqlList.add("update sa_orderitems set undeliqty=undeliqty-" + row.getDouble("qty") + ",deliedqty=deliedqty+" + row.getDouble("qty") + ",offstatus=if(billingstatus='保内收费','待核销','不可核销'),offdate=CURRENT_TIME where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  82. }
  83. for (Row row : itemRows) {
  84. if (row.getBoolean("skucontrol")) {
  85. sqlList.add("update st_stockbill_items set wmsuploadflag2=1 where st_stockbill_itemsid=" + row.getLong("st_stockbill_itemsid"));
  86. }
  87. }
  88. sqlList.addAll(updateAccountbalance_freez(isInStock(ischeck)));
  89. } else {
  90. if (order_paymentnode.equals("3")) {
  91. CashbillEntity entity = getCashbillEntity(billRow.getBigDecimal("payamount").negate(), billRow.getBigDecimal("paydiscountamount").negate(), st_stockbillid, "销售出库单审核", billno, "由销售出库单" + billno + "审核时生成");
  92. JSONObject createCashbillPay = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity, true);
  93. sqlList.addAll(createCashbillPay.getJSONArray("sqlList").toJavaList(String.class));
  94. }
  95. Rows rowsOrderDetail = dbConnect.runSqlQuery("select t1.undeliqty,t1.deliedqty,t1.qty,t1.sa_orderitemsid from sa_orderitems t1 where t1.sa_orderitemsid in (select sa_orderitemsid from st_stockbill_items where sa_orderitemsid>0 and st_stockbillid=" + st_stockbillid + ")");
  96. RowsMap rowsMap = rowsOrderDetail.toRowsMap("sa_orderitemsid");
  97. Rows rowsDispatchDetailGroup = dbConnect.runSqlQuery("select sum(t1.qty) qty,t1.sa_orderitemsid,t1.siteid from st_stockbill_items t1 where t1.sa_orderitemsid>0 and t1.st_stockbillid=" + st_stockbillid + " group by t1.sa_orderitemsid,t1.siteid");
  98. for (Row row : rowsDispatchDetailGroup) {
  99. sqlList.add("update sa_orderitems set undeliqty=" + rowsMap.get(row.getLong("sa_orderitemsid")).get(0).getBigDecimal("undeliqty").add(row.getBigDecimal("qty")) + ",deliedqty=" + (rowsMap.get(row.getLong("sa_orderitemsid")).get(0).getBigDecimal("deliedqty").subtract(row.getBigDecimal("qty"))) + " where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  100. }
  101. for (Row row : itemRows) {
  102. if (row.getBoolean("skucontrol")) {
  103. sqlList.add("update st_stockbill_items set wmsuploadflag2=0 where st_stockbill_itemsid=" + row.getLong("st_stockbill_itemsid"));
  104. }
  105. }
  106. }
  107. } else {
  108. Rows accountbalancerows = dbConnect.runSqlQuery("select * from sa_accountbalance t1 inner join sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid where t2.accountname='现金账户' and t1.sys_enterpriseid=" + sys_enterpriseid);
  109. String order_paymentnode = Parameter.getString(siteid, "order_paymentnode");
  110. if (rb == 1) {
  111. if (order_paymentnode.equals("3")) {
  112. CashbillEntity entity = getCashbillEntity(billRow.getBigDecimal("payamount").negate(), billRow.getBigDecimal("paydiscountamount").negate(), st_stockbillid, "销售出库单反审核", billno, "由销售出库单" + billno + "反审核时生成");
  113. JSONObject createCashbillPay = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity, true);
  114. sqlList.addAll(createCashbillPay.getJSONArray("sqlList").toJavaList(String.class));
  115. }
  116. Rows rowsOrderDetail = dbConnect.runSqlQuery("select t1.undeliqty,t1.deliedqty,t1.qty,t1.sa_orderitemsid from sa_orderitems t1 where t1.sa_orderitemsid in (select sa_orderitemsid from st_stockbill_items where sa_orderitemsid>0 and st_stockbillid=" + st_stockbillid + ")");
  117. RowsMap rowsMap = rowsOrderDetail.toRowsMap("sa_orderitemsid");
  118. Rows rowsDispatchDetailGroup = dbConnect.runSqlQuery("select sum(t1.qty) qty,t1.sa_orderitemsid,t1.siteid from st_stockbill_items t1 where t1.sa_orderitemsid>0 and t1.st_stockbillid=" + st_stockbillid + " group by t1.sa_orderitemsid,t1.siteid");
  119. for (Row row : rowsDispatchDetailGroup) {
  120. sqlList.add("update sa_orderitems set undeliqty=" + rowsMap.get(row.getLong("sa_orderitemsid")).get(0).getBigDecimal("undeliqty").add(row.getBigDecimal("qty")) + ",deliedqty=" + (rowsMap.get(row.getLong("sa_orderitemsid")).get(0).getBigDecimal("deliedqty").subtract(row.getBigDecimal("qty"))) + " where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  121. }
  122. for (Row row : itemRows) {
  123. if (row.getBoolean("skucontrol")) {
  124. sqlList.add("update st_stockbill_items set wmsuploadflag2=0 where st_stockbill_itemsid=" + row.getLong("st_stockbill_itemsid"));
  125. }
  126. }
  127. sqlList.addAll(updateAccountbalance_freez(isInStock(ischeck)));
  128. } else {
  129. if (order_paymentnode.equals("3")) {
  130. CashbillEntity entity = getCashbillEntity(billRow.getBigDecimal("payamount"), billRow.getBigDecimal("paydiscountamount"), st_stockbillid, "销售出库单反审核", billno, "由销售出库单" + billno + "反审核时生成");
  131. JSONObject createCashbillPay = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity, true);
  132. sqlList.addAll(createCashbillPay.getJSONArray("sqlList").toJavaList(String.class));
  133. }
  134. Rows rowsDispatchDetailGroup = dbConnect.runSqlQuery("select sum(t1.qty) qty,t1.sa_orderitemsid,t1.siteid from st_stockbill_items t1 where t1.sa_orderitemsid>0 and t1.st_stockbillid=" + st_stockbillid + " group by t1.sa_orderitemsid,t1.siteid");
  135. for (Row row : rowsDispatchDetailGroup) {
  136. sqlList.add("update sa_orderitems set undeliqty=undeliqty-" + row.getDouble("qty") + ",deliedqty=deliedqty+" + row.getDouble("qty") + ",offstatus=if(billingstatus='保内收费','待核销','不可核销'),offdate=CURRENT_TIME where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  137. }
  138. for (Row row : itemRows) {
  139. if (row.getBoolean("skucontrol")) {
  140. sqlList.add("update st_stockbill_items set wmsuploadflag2=1 where st_stockbill_itemsid=" + row.getLong("st_stockbill_itemsid"));
  141. }
  142. }
  143. }
  144. }
  145. return sqlList;
  146. }
  147. /**
  148. * 账户余额冻结
  149. *
  150. * @param addorsub
  151. */
  152. public ArrayList<String> updateAccountbalance_freez(boolean addorsub) throws YosException {
  153. ArrayList<String> sqllist = new ArrayList<>();
  154. /******** 冻结数据 ********/
  155. Rows rows = dbConnect.runSqlQuery("select t4.sa_accountbalanceid,t3.sa_orderid,t3.sonum,ROUND(sum(t1.qty*t2.price*ifnull(t2.pricerate,1)*ifnull(t2.rwpricerate,1)), 2) amount from st_stockbill_items t1 \n" +
  156. " inner join sa_orderitems t2 on t1.sa_orderitemsid=t2.sa_orderitemsid and t1.siteid=t2.siteid\n" +
  157. " inner join sa_order t3 on t2.sa_orderid=t3.sa_orderid and t2.siteid=t3.siteid\n" +
  158. "\t\t\t\t\t\t\t\t\tleft join sa_accountbalance t4 on t4.sa_accountclassid=t3.sa_accountclassid and t4.sys_enterpriseid=t3.sys_enterpriseid and t4.siteid=t3.siteid\n" +
  159. " where t1.st_stockbillid=" + st_stockbillid + " group by t4.sa_accountbalanceid,t3.sa_orderid,t3.sonum");
  160. if (addorsub) {
  161. for (Row row : rows) {
  162. String sonum = row.getString("sonum");
  163. long sa_orderid = row.getLong("sa_orderid");
  164. long sa_accountbalanceid = row.getLong("sa_accountbalanceid");
  165. BigDecimal amount = row.getBigDecimal("amount");
  166. Rows accountbalancefreezrows = dbConnect.runSqlQuery("select sa_accountbalance_freezid from sa_accountbalance_freez where sourceid=" + sa_orderid + " and sourcetable='sa_order' and siteid='" + siteid + "'");
  167. sqllist.add("update sa_accountbalance set freezamount=ifnull(freezamount,0)+" + amount + " where sa_accountbalanceid=" + sa_accountbalanceid + " and siteid='" + siteid + "'");
  168. if (accountbalancefreezrows.isNotEmpty()) {
  169. sqllist.add("update sa_accountbalance_freez set amount=amount+" + amount + " where sa_accountbalance_freezid=" + accountbalancefreezrows.get(0).getLong("sa_accountbalance_freezid"));
  170. } else {
  171. InsertSQL insertSQL = SQLFactory.createInsertSQL(dbConnect, "sa_accountbalance_freez");
  172. insertSQL.setSiteid(siteid);
  173. insertSQL.setValue("sa_accountbalanceid", sa_accountbalanceid);
  174. insertSQL.setValue("sourcetable", "sa_order");
  175. insertSQL.setValue("sourceid", sa_orderid);
  176. insertSQL.setValue("amount", amount);
  177. insertSQL.setValue("remarks", "由销售订单" + sonum + "审核时冻结");
  178. insertSQL.setValue("createby", userInfo.getUserName());
  179. insertSQL.setDateValue("createdate");
  180. sqllist.add(insertSQL.getSQL());
  181. }
  182. }
  183. } else {
  184. for (Row row : rows) {
  185. long sa_orderid = row.getLong("sa_orderid");
  186. long sa_accountbalanceid = row.getLong("sa_accountbalanceid");
  187. BigDecimal amount = row.getBigDecimal("amount");
  188. Rows accountbalancefreezrows = dbConnect.runSqlQuery("select sa_accountbalance_freezid,amount from sa_accountbalance_freez where sourceid=" + sa_orderid + " and sourcetable='sa_order' and siteid='" + siteid + "'");
  189. if (accountbalancefreezrows.size() > 0) {
  190. sqllist.add("update sa_accountbalance set freezamount=ifnull(freezamount,0)-" + amount + " where sa_accountbalanceid=" + sa_accountbalanceid);
  191. sqllist.add("update sa_accountbalance_freez set amount=amount-" + amount + " where sa_accountbalance_freezid=" + accountbalancefreezrows.get(0).getLong("sa_accountbalance_freezid"));
  192. }
  193. }
  194. }
  195. return sqllist;
  196. }
  197. public static CashbillEntity getCashbillEntity(BigDecimal amount, BigDecimal discountamount, Long ownerid, String source, String sourcenotes, String remarks) throws YosException {
  198. CashbillEntity entity = new CashbillEntity();
  199. entity.setAmount(amount);
  200. entity.setDiscountamountamount(discountamount);
  201. entity.setOwnerid(ownerid);
  202. entity.setOwnertable("st_stockbill");
  203. entity.setSource(source);
  204. entity.setRemarks(remarks);
  205. entity.setSourcenote(sourcenotes);
  206. return entity;
  207. }
  208. @Override
  209. public boolean isInStock(boolean fischeck) {
  210. if (fischeck && rb == 0 || !fischeck && rb == 1) {
  211. return true;
  212. }
  213. return false;
  214. }
  215. }