XSCK.java 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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.BaseClass;
  7. import common.Controller;
  8. import common.YosException;
  9. import common.data.*;
  10. import org.apache.commons.lang.StringUtils;
  11. import java.math.BigDecimal;
  12. import java.util.ArrayList;
  13. public class XSCK extends BasicBill {
  14. long sys_enterpriseid;
  15. public XSCK(Controller controller, long st_stockbillid) throws YosException {
  16. super(controller, st_stockbillid);
  17. sys_enterpriseid = billRow.getLong("sys_enterpriseid");
  18. }
  19. @Override
  20. public void checkValidate(boolean ischeck) throws YosException {
  21. super.checkValidate(ischeck);
  22. 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);
  23. if (accountbalancerows.isEmpty()) {
  24. throw new YosException("账户不存在");
  25. }
  26. String order_paymentnode = Parameter.getString(siteid, "order_paymentnode");
  27. if (ischeck) {
  28. if (itemRows.min("payamount").doubleValue() < 0) {
  29. throw new YosException("扣款金额需大于0");
  30. }
  31. if (StringUtils.isBlank(billRow.getString("outplace"))) {
  32. throw new YosException("发货地不可为空");
  33. }
  34. if ((billRow.getBigDecimal("payamount").add(billRow.getBigDecimal("paydiscountamount"))).compareTo(itemRows.sum("amount")) != 0) {
  35. throw new YosException("扣款金额与应扣金额不符,不能审核");
  36. }
  37. BigDecimal discountamount = accountbalancerows.get(0).getBigDecimal("discountamount");
  38. if (billRow.getBigDecimal("paydiscountamount").compareTo(discountamount) > 0) {
  39. if(rb==1){
  40. throw new YosException("优惠金额不能大于可用优惠金额");
  41. }
  42. }
  43. if(billRow.getString("sourceobject").equals("tpartreimbursement")){
  44. QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_tpartreimbursementdetail", "sa_tpartreimbursementdetailid","isverified","qty","sa_orderitemsid");
  45. querySQL.setTableAlias("t1");
  46. querySQL.addJoinTable(BaseClass.JOINTYPE.inner, "sa_tpartreimbursement", "t3", "t1.sa_tpartreimbursementid = t3.sa_tpartreimbursementid AND t1.siteid = t3.siteid");
  47. querySQL.setSiteid(siteid);
  48. querySQL.setWhere("t1.hongbillno='"+billno+"'");
  49. Rows rows = querySQL.query();
  50. for (Row row : rows) {
  51. if (row.getBoolean("isverified")) {
  52. throw new YosException("存在记录已核销,请检查");
  53. }
  54. }
  55. }
  56. if(rb==1){
  57. QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_orderitems","verifiedqty");
  58. querySQL.setTableAlias("t1");
  59. querySQL.setSiteid(siteid);
  60. querySQL.setWhere("t1.sa_orderitemsid",itemRows.toArrayList("sa_orderitemsid"));
  61. Rows rows = querySQL.query();
  62. for (Row row : rows) {
  63. if (row.getBigDecimal("verifiedqty").compareTo(BigDecimal.ZERO)>0) {
  64. throw new YosException("存在对应订单明细已核销,销售出库单无法反审核");
  65. }
  66. }
  67. }
  68. /*
  69. 库存校验
  70. */
  71. /*
  72. 其他校验
  73. */
  74. } else {
  75. if (billRow.getBoolean("isreceiver")) {
  76. throw new YosException("经销商已经确认收货,不可进行反审核");
  77. }
  78. /*
  79. 库存校验
  80. */
  81. /*
  82. 其他校验
  83. */
  84. }
  85. //库存校验
  86. RowsMap invbalsRowsMap = SQLFactory.createQuerySQL(dbConnect, "st_invbal").setWhere("siteid", siteid).setWhere("itemid", itemRows.toArrayList("itemid")).query().toRowsMap("itemid");
  87. for(Row row : itemRows){
  88. if(!row.getBoolean("isnegative") && row.getBoolean("ismodule")){
  89. long itemid = row.getLong("itemid");
  90. long stockid = row.getLong("stockid");
  91. BigDecimal qty =row.getBigDecimal("qty");
  92. if(!isInStock(ischeck)){
  93. if (!invbalsRowsMap.containsKey(String.valueOf(itemid)) || !invbalsRowsMap.get(String.valueOf(itemid)).toRowsMap("stockid").containsKey(String.valueOf(stockid))) {
  94. throw new YosException("行【"+row.getString("rowno")+"】不能负库存出库");
  95. } else {
  96. BigDecimal invbalqty = invbalsRowsMap.get(String.valueOf(itemid)).toRowsMap("stockid").get(String.valueOf(stockid)).get(0).getBigDecimal("qty");
  97. if(invbalqty.compareTo(qty)<0){
  98. throw new YosException("行【"+row.getString("rowno")+"】不能负库存出库");
  99. }
  100. }
  101. }
  102. }
  103. }
  104. if ((ischeck && rb == 1) || (!ischeck && rb == 0)) {
  105. // if (order_paymentnode.equals("3") && !Accountbalance.judgeBalance(dbConnect, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), itemRows.toRowsMap("sa_orderitemsidnum").get("0").sum("amount"))) {
  106. // //throw new YosException("账户余额不足,还差" + Accountbalance.InsufficientBalance(dbConnect, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), itemRows.toRowsMap("sa_orderitemsidnum").get("0").sum("amount")) + "元!");
  107. // }
  108. if(!billRow.getString("sourceobject").equals("tpartreimbursement")){
  109. 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");
  110. if (rowsjudge.isNotEmpty()) {
  111. if(ischeck){
  112. throw new YosException("存在商品已完全出库,无法审核");
  113. }else{
  114. throw new YosException("存在商品已完全出库,无法反审核");
  115. }
  116. }
  117. }
  118. }
  119. }
  120. @Override
  121. public ArrayList<String> getCheckSql(boolean ischeck) throws YosException {
  122. ArrayList<String> sqlList = super.getCheckSql(ischeck);
  123. 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);
  124. if (ischeck) {
  125. String order_paymentnode = Parameter.getString(siteid, "order_paymentnode");
  126. if (rb == 1) {
  127. if (order_paymentnode.equals("3")) {
  128. CashbillEntity entity = getCashbillEntity(billRow.getBigDecimal("payamount"), billRow.getBigDecimal("paydiscountamount"), st_stockbillid, "销售出库单审核", billno, "由销售出库单" + billno + "审核时生成","货款");
  129. JSONObject createCashbillPay = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity, true);
  130. if(!(billRow.getBigDecimal("payamount").compareTo(BigDecimal.ZERO)==0 && billRow.getBigDecimal("paydiscountamount").compareTo(BigDecimal.ZERO)==0)){
  131. sqlList.addAll(createCashbillPay.getJSONArray("sqlList").toJavaList(String.class));
  132. }
  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=ifnull(deliedqty,0)+" + row.getDouble("qty") + ",offstatus=if(billingstatus='保内收费','待核销','不可核销'),offdate=CURRENT_TIME where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  137. sqlList.add("update sa_orderitems set tobeoffqty=case when tobeoffqty+" + row.getDouble("qty") + ">canoffqty then canoffqty else tobeoffqty+" + row.getDouble("qty") + " end where canoffqty>0 and sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  138. }
  139. for (Row row : itemRows) {
  140. if (row.getBoolean("skucontrol")) {
  141. sqlList.add("update st_stockbill_items set wmsuploadflag2=1 where st_stockbill_itemsid=" + row.getLong("st_stockbill_itemsid"));
  142. }
  143. }
  144. sqlList.addAll(updateAccountbalance_freez(isInStock(ischeck)));
  145. //判断是否关闭订单
  146. int orderautoclosepoint = Parameter.getInteger(siteid, "orderautoclosepoint");
  147. if(orderautoclosepoint==2){
  148. ArrayList<Long> sa_orderids = itemRows.toArrayList("sa_orderid",new ArrayList<>());
  149. QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_order", "sa_orderid");
  150. querySQL.addJoinTable(BaseClass.JOINTYPE.inner, "sa_orderitems", "t2", "t2.sa_orderid = t1.sa_orderid and t2.siteid = t1.siteid",
  151. "undeliqty","sa_orderitemsid");
  152. querySQL.setTableAlias("t1");
  153. querySQL.setWhere("t1.siteid", siteid);
  154. querySQL.setWhere("t1.sa_orderid", sa_orderids);
  155. querySQL.setWhere("t1.status='审核' and t2.undeliqty!=0");
  156. Rows rows = querySQL.query();
  157. RowsMap rowsMap =rows.toRowsMap("sa_orderid");
  158. RowsMap itemRowsMap=itemRows.toRowsMap("sa_orderid");
  159. for(Long sa_orderid : sa_orderids){
  160. Rows stockbillitemrows = itemRowsMap.getOrDefault(String.valueOf(sa_orderid),new Rows());
  161. Rows orderrows = rowsMap.getOrDefault(String.valueOf(sa_orderid),new Rows());
  162. RowsMap stockbillitemrowsMap =stockbillitemrows.toRowsMap("sa_orderitemsid");
  163. int count=0;
  164. for(Row row : orderrows){
  165. if(stockbillitemrowsMap.containsKey(row.getString("sa_orderitemsid"))){
  166. if(stockbillitemrowsMap.getOrDefault(row.getString("sa_orderitemsid"),new Rows()).sum("qty").compareTo(row.getBigDecimal("undeliqty"))==0){
  167. count++;
  168. }
  169. }
  170. }
  171. if(orderrows.size()==count){
  172. sqlList.add("update sa_order set status='关闭',closeby='"+controller.username+"',closedate=CURRENT_TIME where sa_orderid=" +sa_orderid);
  173. sqlList.add("UPDATE sa_accountbalance t1 " +
  174. "INNER JOIN sa_accountbalance_freez t2 ON t1.sa_accountbalanceid = t2.sa_accountbalanceid " +
  175. "SET t1.freezamount = case when ifnull(t1.freezamount,0)>t2.amount then t1.freezamount-t2.amount else 0 end " +
  176. "WHERE t1.sa_accountbalanceid="+accountbalancerows.get(0).getLong("sa_accountbalanceid")+" and t2.sourcetable='sa_order' and t2.sourceid="+sa_orderid);
  177. sqlList.add("delete from sa_accountbalance_freez t1 WHERE t1.sourcetable='sa_order' and t1.sourceid="+sa_orderid);
  178. }
  179. }
  180. }
  181. } else {
  182. if (order_paymentnode.equals("3")) {
  183. CashbillEntity entity = getCashbillEntity(billRow.getBigDecimal("payamount").negate(), billRow.getBigDecimal("paydiscountamount").negate(), st_stockbillid, "销售出库单审核", billno, "由销售出库单" + billno + "审核时生成","货款");
  184. JSONObject createCashbillPay = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity, true);
  185. if(!(billRow.getBigDecimal("payamount").compareTo(BigDecimal.ZERO)==0 && billRow.getBigDecimal("paydiscountamount").compareTo(BigDecimal.ZERO)==0)){
  186. sqlList.addAll(createCashbillPay.getJSONArray("sqlList").toJavaList(String.class));
  187. }
  188. }
  189. if(!billRow.getString("sourceobject").equals("tpartreimbursement")){
  190. 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 + ")");
  191. RowsMap rowsMap = rowsOrderDetail.toRowsMap("sa_orderitemsid");
  192. 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");
  193. for (Row row : rowsDispatchDetailGroup) {
  194. 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"));
  195. sqlList.add("update sa_orderitems set tobeoffqty=case when tobeoffqty-" + row.getDouble("qty") + ">0 then tobeoffqty-" + row.getDouble("qty")+" else 0 end where canoffqty>0 and sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  196. }
  197. for (Row row : itemRows) {
  198. if (row.getBoolean("skucontrol")) {
  199. sqlList.add("update st_stockbill_items set wmsuploadflag2=0 where st_stockbill_itemsid=" + row.getLong("st_stockbill_itemsid"));
  200. }
  201. }
  202. int orderautoclosepoint = Parameter.getInteger(siteid, "orderautoclosepoint");
  203. if(orderautoclosepoint==2){
  204. ArrayList<Long> sa_orderids = itemRows.toArrayList("sa_orderid",new ArrayList<>());
  205. for(Long sa_orderid : sa_orderids){
  206. sqlList.add("update sa_order set status='审核',closeby='',closedate=NULL where sa_orderid=" +sa_orderid);
  207. }
  208. }
  209. }else{
  210. QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_tpartreimbursementdetail", "sa_tpartreimbursementdetailid","isverified","qty","sa_orderitemsid");
  211. querySQL.setTableAlias("t1");
  212. querySQL.addJoinTable(BaseClass.JOINTYPE.inner, "sa_tpartreimbursement", "t3", "t1.sa_tpartreimbursementid = t3.sa_tpartreimbursementid AND t1.siteid = t3.siteid");
  213. querySQL.setSiteid(siteid);
  214. querySQL.setWhere("t1.hongbillno='"+billno+"'");
  215. Rows rows = querySQL.query();
  216. QuerySQL querySQLgroup = SQLFactory.createQuerySQL(controller, "sa_tpartreimbursementdetail", "sa_orderitemsid");
  217. querySQLgroup.setTableAlias("t1");
  218. querySQLgroup.addJoinTable(BaseClass.JOINTYPE.left, "sa_orderitems", "t2", "t1.sa_orderitemsid = t2.sa_orderitemsid AND t1.siteid = t2.siteid");
  219. querySQLgroup.setSiteid(siteid);
  220. querySQLgroup.addQueryFields("sumqty", "sum(t1.checkqty)");
  221. querySQLgroup.setWhere("t1.hongbillno='"+ billno+"'");
  222. querySQLgroup.addGroupBy("sa_orderitemsid");
  223. Rows rowsgroup = querySQLgroup.query();
  224. for (Row row : rows) {
  225. sqlList.add("update sa_tpartreimbursementdetail set isverified=1 where sa_tpartreimbursementdetailid=" + row.getLong("sa_tpartreimbursementdetailid"));
  226. }
  227. for (Row row : rowsgroup) {
  228. sqlList.add("update sa_orderitems set verifiedqty=ifnull(verifiedqty,0)+"+row.getBigDecimal("sumqty")+" where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  229. }
  230. }
  231. }
  232. } else {
  233. String order_paymentnode = Parameter.getString(siteid, "order_paymentnode");
  234. if (rb == 1) {
  235. if (order_paymentnode.equals("3")) {
  236. Rows cashbillRows =dbConnect.runSqlQuery("select amount,discountamount from sa_cashbill where ownerid="+st_stockbillid +" and ownertable='st_stockbill' and type=0 and class='货款'");
  237. CashbillEntity entity = getCashbillEntity(cashbillRows.sum("amount").negate(), cashbillRows.sum("discountamount").negate(), st_stockbillid, "销售出库单反审核", billno, "由销售出库单" + billno + "反审核时生成","货款");
  238. JSONObject createCashbillPay = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity, true);
  239. if(!(cashbillRows.sum("amount").negate().compareTo(BigDecimal.ZERO)==0 && cashbillRows.sum("discountamount").negate().compareTo(BigDecimal.ZERO)==0)){
  240. sqlList.addAll(createCashbillPay.getJSONArray("sqlList").toJavaList(String.class));
  241. }
  242. }
  243. 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 + ")");
  244. RowsMap rowsMap = rowsOrderDetail.toRowsMap("sa_orderitemsid");
  245. 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");
  246. for (Row row : rowsDispatchDetailGroup) {
  247. 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"));
  248. sqlList.add("update sa_orderitems set tobeoffqty=case when tobeoffqty-" + row.getDouble("qty") + ">0 then tobeoffqty-" + row.getDouble("qty")+" else 0 end where canoffqty>0 and sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  249. }
  250. for (Row row : itemRows) {
  251. if (row.getBoolean("skucontrol")) {
  252. sqlList.add("update st_stockbill_items set wmsuploadflag2=0 where st_stockbill_itemsid=" + row.getLong("st_stockbill_itemsid"));
  253. }
  254. }
  255. //判断是否关闭订单
  256. int orderautoclosepoint = Parameter.getInteger(siteid, "orderautoclosepoint");
  257. if(orderautoclosepoint==2){
  258. ArrayList<Long> sa_orderids = itemRows.toArrayList("sa_orderid",new ArrayList<>());
  259. for(Long sa_orderid : sa_orderids){
  260. sqlList.add("update sa_order set status='审核',closeby='',closedate=NULL where sa_orderid=" +sa_orderid);
  261. }
  262. }
  263. sqlList.addAll(updateAccountbalance_freez(isInStock(ischeck)));
  264. } else {
  265. if (order_paymentnode.equals("3")) {
  266. Rows cashbillRows =dbConnect.runSqlQuery("select amount,discountamount from sa_cashbill where ownerid="+st_stockbillid +" and ownertable='st_stockbill' and type=0 and class='货款'");
  267. CashbillEntity entity = getCashbillEntity(cashbillRows.sum("amount").negate(), cashbillRows.sum("discountamount").negate(), st_stockbillid, "销售出库单反审核", billno, "由销售出库单" + billno + "反审核时生成","货款");
  268. JSONObject createCashbillPay = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity, true);
  269. if(!(cashbillRows.sum("amount").negate().compareTo(BigDecimal.ZERO)==0 && cashbillRows.sum("discountamount").negate().compareTo(BigDecimal.ZERO)==0)){
  270. sqlList.addAll(createCashbillPay.getJSONArray("sqlList").toJavaList(String.class));
  271. }
  272. }
  273. if(!billRow.getString("sourceobject").equals("tpartreimbursement")){
  274. 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");
  275. for (Row row : rowsDispatchDetailGroup) {
  276. sqlList.add("update sa_orderitems set undeliqty=undeliqty-" + row.getDouble("qty") + ",deliedqty=ifnull(deliedqty,0)+" + row.getDouble("qty") + ",offstatus=if(billingstatus='保内收费','待核销','不可核销'),offdate=CURRENT_TIME where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  277. sqlList.add("update sa_orderitems set tobeoffqty=case when tobeoffqty+" + row.getDouble("qty") + ">canoffqty then canoffqty else tobeoffqty+" + row.getDouble("qty") + " end where canoffqty>0 and sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  278. }
  279. for (Row row : itemRows) {
  280. if (row.getBoolean("skucontrol")) {
  281. sqlList.add("update st_stockbill_items set wmsuploadflag2=1 where st_stockbill_itemsid=" + row.getLong("st_stockbill_itemsid"));
  282. }
  283. }
  284. //判断是否关闭订单
  285. int orderautoclosepoint = Parameter.getInteger(siteid, "orderautoclosepoint");
  286. if(orderautoclosepoint==2){
  287. ArrayList<Long> sa_orderids = itemRows.toArrayList("sa_orderid",new ArrayList<>());
  288. QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_order", "sa_orderid");
  289. querySQL.addJoinTable(BaseClass.JOINTYPE.inner, "sa_orderitems", "t2", "t2.sa_orderid = t1.sa_orderid and t2.siteid = t1.siteid",
  290. "undeliqty","sa_orderitemsid");
  291. querySQL.setTableAlias("t1");
  292. querySQL.setWhere("t1.siteid", siteid);
  293. querySQL.setWhere("t1.sa_orderid", sa_orderids);
  294. querySQL.setWhere("t1.status='审核' and t2.undeliqty!=0");
  295. Rows rows = querySQL.query();
  296. RowsMap rowsMap =rows.toRowsMap("sa_orderid");
  297. RowsMap itemRowsMap=itemRows.toRowsMap("sa_orderid");
  298. for(Long sa_orderid : sa_orderids){
  299. Rows stockbillitemrows = itemRowsMap.getOrDefault(String.valueOf(sa_orderid),new Rows());
  300. Rows orderrows = rowsMap.getOrDefault(String.valueOf(sa_orderid),new Rows());
  301. RowsMap stockbillitemrowsMap =stockbillitemrows.toRowsMap("sa_orderitemsid");
  302. int count=0;
  303. for(Row row : orderrows){
  304. if(stockbillitemrowsMap.containsKey(row.getString("sa_orderitemsid"))){
  305. if(stockbillitemrowsMap.getOrDefault(row.getString("sa_orderitemsid"),new Rows()).sum("qty").compareTo(row.getBigDecimal("undeliqty"))==0){
  306. count++;
  307. }
  308. }
  309. }
  310. if(orderrows.size()==count){
  311. sqlList.add("update sa_order set status='关闭',closeby='"+controller.username+"',closedate=CURRENT_TIME where sa_orderid=" +sa_orderid);
  312. sqlList.add("UPDATE sa_accountbalance t1 " +
  313. "INNER JOIN sa_accountbalance_freez t2 ON t1.sa_accountbalanceid = t2.sa_accountbalanceid " +
  314. "SET t1.freezamount = case when ifnull(t1.freezamount,0)>t2.amount then t1.freezamount-t2.amount else 0 end " +
  315. "WHERE t1.sa_accountbalanceid="+accountbalancerows.get(0).getLong("sa_accountbalanceid")+" and t2.sourcetable='sa_order' and t2.sourceid="+sa_orderid);
  316. sqlList.add("delete from sa_accountbalance_freez t1 WHERE t1.sourcetable='sa_order' and t1.sourceid="+sa_orderid);
  317. }
  318. }
  319. }
  320. }else{
  321. QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_tpartreimbursementdetail", "sa_tpartreimbursementid","sa_tpartreimbursementdetailid","isverified","qty","sa_orderitemsid");
  322. querySQL.setTableAlias("t1");
  323. querySQL.addJoinTable(BaseClass.JOINTYPE.inner, "sa_tpartreimbursement", "t3", "t1.sa_tpartreimbursementid = t3.sa_tpartreimbursementid AND t1.siteid = t3.siteid");
  324. querySQL.setSiteid(siteid);
  325. querySQL.setWhere("t1.hongbillno='"+billno+"'");
  326. Rows rows = querySQL.query();
  327. QuerySQL querySQLgroup = SQLFactory.createQuerySQL(controller, "sa_tpartreimbursementdetail", "sa_orderitemsid");
  328. querySQLgroup.setTableAlias("t1");
  329. querySQLgroup.addJoinTable(BaseClass.JOINTYPE.left, "sa_orderitems", "t2", "t1.sa_orderitemsid = t2.sa_orderitemsid AND t1.siteid = t2.siteid");
  330. querySQLgroup.setSiteid(siteid);
  331. querySQLgroup.addQueryFields("sumqty", "sum(t1.checkqty)");
  332. querySQLgroup.setWhere("t1.hongbillno='"+ billno+"'");
  333. querySQLgroup.addGroupBy("sa_orderitemsid");
  334. Rows rowsgroup = querySQLgroup.query();
  335. for (Row row : rows) {
  336. sqlList.add("update sa_tpartreimbursementdetail set isverified=0 where sa_tpartreimbursementdetailid=" + row.getLong("sa_tpartreimbursementdetailid"));
  337. sqlList.add("update sa_tpartreimbursement set status='审核' where sa_tpartreimbursementid=" + row.getLong("sa_tpartreimbursementid"));
  338. }
  339. for (Row row : rowsgroup) {
  340. sqlList.add("update sa_orderitems set verifiedqty=ifnull(verifiedqty,0)-"+row.getBigDecimal("sumqty")+" where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  341. }
  342. }
  343. }
  344. }
  345. return sqlList;
  346. }
  347. public ArrayList<String> getCheckSql_custamount(boolean ischeck) throws YosException {
  348. ArrayList<String> sqlList = super.getCheckSql_custamount(ischeck);
  349. if (ischeck) {
  350. 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);
  351. String order_paymentnode = Parameter.getString(siteid, "order_paymentnode");
  352. if (rb == 1) {
  353. if (order_paymentnode.equals("3")) {
  354. BigDecimal custamount=BigDecimal.ZERO;
  355. for(Row row :itemRows){
  356. custamount=custamount.add(row.getBigDecimal("custamount").multiply(row.getBigDecimal("qty")));
  357. }
  358. CashbillEntity entity_custamount = getCashbillEntity(custamount, BigDecimal.ZERO, st_stockbillid, "销售出库单审核", billno, "由销售出库单" + billno + "审核时生成","客户档案资料费");
  359. JSONObject createCashbillPay_custamount = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity_custamount, true);
  360. if(custamount.compareTo(BigDecimal.ZERO)!=0){
  361. sqlList.addAll(createCashbillPay_custamount.getJSONArray("sqlList").toJavaList(String.class));
  362. }
  363. }
  364. } else {
  365. if (order_paymentnode.equals("3")) {
  366. BigDecimal custamount=BigDecimal.ZERO;
  367. for(Row row :itemRows){
  368. custamount=custamount.add(row.getBigDecimal("custamount").multiply(row.getBigDecimal("qty")));
  369. }
  370. CashbillEntity entity_custamount = getCashbillEntity(custamount.negate(), BigDecimal.ZERO, st_stockbillid, "销售出库单审核", billno, "由销售出库单" + billno + "审核时生成","客户档案资料费");
  371. JSONObject createCashbillPay_custamount = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity_custamount, true);
  372. if(custamount.negate().compareTo(BigDecimal.ZERO)!=0){
  373. sqlList.addAll(createCashbillPay_custamount.getJSONArray("sqlList").toJavaList(String.class));
  374. }
  375. }
  376. }
  377. } else {
  378. 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);
  379. String order_paymentnode = Parameter.getString(siteid, "order_paymentnode");
  380. if (rb == 1) {
  381. if (order_paymentnode.equals("3")) {
  382. Rows cashbillRows_custamount =dbConnect.runSqlQuery("select amount,discountamount from sa_cashbill where ownerid="+st_stockbillid +" and ownertable='st_stockbill' and type=0 and class='客户档案资料费'");
  383. CashbillEntity entity_custamount = getCashbillEntity(cashbillRows_custamount.sum("amount").negate(), cashbillRows_custamount.sum("discountamount").negate(), st_stockbillid, "销售出库单反审核", billno, "由销售出库单" + billno + "反审核时生成","客户档案资料费");
  384. JSONObject createCashbillPay_custamount = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity_custamount, true);
  385. if(cashbillRows_custamount.sum("amount").negate().compareTo(BigDecimal.ZERO)!=0){
  386. sqlList.addAll(createCashbillPay_custamount.getJSONArray("sqlList").toJavaList(String.class));
  387. }
  388. }
  389. } else {
  390. if (order_paymentnode.equals("3")) {
  391. Rows cashbillRows_custamount =dbConnect.runSqlQuery("select amount,discountamount from sa_cashbill where ownerid="+st_stockbillid +" and ownertable='st_stockbill' and type=0 and class='客户档案资料费'");
  392. CashbillEntity entity_custamount = getCashbillEntity(cashbillRows_custamount.sum("amount").negate(),cashbillRows_custamount.sum("discountamount").negate(), st_stockbillid, "销售出库单审核", billno, "由销售出库单" + billno + "审核时生成","客户档案资料费");
  393. JSONObject createCashbillPay_custamount = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity_custamount, true);
  394. if(cashbillRows_custamount.sum("amount").negate().compareTo(BigDecimal.ZERO)!=0){
  395. sqlList.addAll(createCashbillPay_custamount.getJSONArray("sqlList").toJavaList(String.class));
  396. }
  397. }
  398. }
  399. }
  400. return sqlList;
  401. }
  402. /**
  403. * 账户余额冻结
  404. *
  405. * @param addorsub
  406. */
  407. public ArrayList<String> updateAccountbalance_freez(boolean addorsub) throws YosException {
  408. ArrayList<String> sqllist = new ArrayList<>();
  409. /******** 冻结数据 ********/
  410. Rows rows = dbConnect.runSqlQuery("select t4.sa_accountbalanceid,t3.sa_orderid,t3.sonum,t3.type,ROUND(sum(t1.qty*(t1.price+ifnull(t1.custamount,0))), 2) amount from st_stockbill_items t1 \n" +
  411. " inner join sa_orderitems t2 on t1.sa_orderitemsid=t2.sa_orderitemsid and t1.siteid=t2.siteid\n" +
  412. " inner join sa_order t3 on t2.sa_orderid=t3.sa_orderid and t2.siteid=t3.siteid\n" +
  413. "\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" +
  414. " where t1.st_stockbillid=" + st_stockbillid + " group by t4.sa_accountbalanceid,t3.sa_orderid,t3.sonum");
  415. if (addorsub) {
  416. for (Row row : rows) {
  417. String sonum = row.getString("sonum");
  418. long sa_orderid = row.getLong("sa_orderid");
  419. long sa_accountbalanceid = row.getLong("sa_accountbalanceid");
  420. BigDecimal amount = row.getBigDecimal("amount");
  421. Rows accountbalancefreezrows = dbConnect.runSqlQuery("select sa_accountbalance_freezid from sa_accountbalance_freez where sourceid=" + sa_orderid + " and sourcetable='sa_order' and siteid='" + siteid + "'");
  422. sqllist.add("update sa_accountbalance set freezamount=ifnull(freezamount,0)+" + amount + " where sa_accountbalanceid=" + sa_accountbalanceid + " and siteid='" + siteid + "'");
  423. if (accountbalancefreezrows.isNotEmpty()) {
  424. sqllist.add("update sa_accountbalance_freez set amount=amount+" + amount + " where sa_accountbalance_freezid=" + accountbalancefreezrows.get(0).getLong("sa_accountbalance_freezid"));
  425. } else {
  426. InsertSQL insertSQL = SQLFactory.createInsertSQL(dbConnect, "sa_accountbalance_freez");
  427. insertSQL.setSiteid(siteid);
  428. insertSQL.setValue("sa_accountbalanceid", sa_accountbalanceid);
  429. insertSQL.setValue("sourcetable", "sa_order");
  430. insertSQL.setValue("sourceid", sa_orderid);
  431. insertSQL.setValue("amount", amount);
  432. insertSQL.setValue("remarks", "由"+ (row.getString("type").equals("配件订单")?"配件":"销售")+"订单" + sonum + "审核时冻结");
  433. insertSQL.setValue("createby", userInfo.getUserName());
  434. insertSQL.setDateValue("createdate");
  435. sqllist.add(insertSQL.getSQL());
  436. }
  437. }
  438. } else {
  439. for (Row row : rows) {
  440. long sa_orderid = row.getLong("sa_orderid");
  441. long sa_accountbalanceid = row.getLong("sa_accountbalanceid");
  442. BigDecimal amount = row.getBigDecimal("amount");
  443. Rows accountbalancefreezrows = dbConnect.runSqlQuery("select sa_accountbalance_freezid,amount from sa_accountbalance_freez where sourceid=" + sa_orderid + " and sourcetable='sa_order' and siteid='" + siteid + "'");
  444. if (accountbalancefreezrows.size() > 0) {
  445. if(accountbalancefreezrows.get(0).getBigDecimal("amount").compareTo(amount)>0){
  446. sqllist.add("update sa_accountbalance set freezamount=ifnull(freezamount,0)-" + amount + " where sa_accountbalanceid=" + sa_accountbalanceid);
  447. sqllist.add("update sa_accountbalance_freez set amount=amount-" + amount + " where sa_accountbalance_freezid=" + accountbalancefreezrows.get(0).getLong("sa_accountbalance_freezid"));
  448. }else{
  449. sqllist.add("update sa_accountbalance set freezamount=ifnull(freezamount,0)-" + accountbalancefreezrows.get(0).getBigDecimal("amount") + " where sa_accountbalanceid=" + sa_accountbalanceid);
  450. sqllist.add("update sa_accountbalance_freez set amount=0 where sa_accountbalance_freezid=" + accountbalancefreezrows.get(0).getLong("sa_accountbalance_freezid"));
  451. }
  452. }
  453. }
  454. }
  455. return sqllist;
  456. }
  457. public static CashbillEntity getCashbillEntity(BigDecimal amount, BigDecimal discountamount, Long ownerid, String source, String sourcenotes, String remarks,String type) throws YosException {
  458. CashbillEntity entity = new CashbillEntity();
  459. entity.setAmount(amount);
  460. entity.setDiscountamountamount(discountamount);
  461. entity.setOwnerid(ownerid);
  462. entity.setOwnertable("st_stockbill");
  463. entity.setSource(source);
  464. entity.setRemarks(remarks);
  465. entity.setType(type);
  466. entity.setSourcenote(sourcenotes);
  467. return entity;
  468. }
  469. @Override
  470. public boolean isInStock(boolean fischeck) {
  471. if (fischeck && rb == 0 || !fischeck && rb == 1) {
  472. return true;
  473. }
  474. return false;
  475. }
  476. }