stockbill.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. package restcontroller.webmanage.cucuAbutment.stockbill;
  2. import beans.accountbalance.Accountbalance;
  3. import beans.accountbalance.CashbillEntity;
  4. import beans.datacontrllog.DataContrlLog;
  5. import beans.parameter.Parameter;
  6. import beans.remind.Remind;
  7. import com.alibaba.fastjson.JSONArray;
  8. import com.alibaba.fastjson.JSONObject;
  9. import common.Controller;
  10. import common.YosException;
  11. import common.annotation.API;
  12. import common.data.Row;
  13. import common.data.Rows;
  14. import common.data.RowsMap;
  15. import common.data.SQLFactory;
  16. import org.apache.commons.lang.StringUtils;
  17. import org.apache.log4j.Logger;
  18. import restcontroller.R;
  19. import restcontroller.webmanage.executorService.Executor;
  20. import service.AftersalesmagAutoRecheckService;
  21. import javax.swing.text.StyledEditorKit;
  22. import java.math.BigDecimal;
  23. import java.text.SimpleDateFormat;
  24. import java.util.ArrayList;
  25. import java.util.Date;
  26. import java.util.List;
  27. public class stockbill extends Controller {
  28. /**
  29. * 构造函数
  30. *
  31. * @param content
  32. */
  33. public stockbill(JSONObject content) throws YosException {
  34. super(content);
  35. }
  36. private static Logger logger = Logger.getLogger(stockbill.class);
  37. /**
  38. * 更新YOS销售出库单
  39. *
  40. * @return
  41. * @throws YosException
  42. */
  43. @API(title = "更新YOS销售出库单", apiversion = R.ID20231115095103.v1.class, accesstoken = false)
  44. public String updateIcstockbill() throws YosException {
  45. String msg = "";
  46. int code = 1;
  47. ArrayList<String> sqlList = new ArrayList<>();
  48. // 单据编号
  49. String fbillnum = content.getString("fbillnum");
  50. try {
  51. // 经销商编号
  52. String fagentnum = content.getString("fagentnum");
  53. long sys_enterpriseid = 0;
  54. if (dbConnect.runSqlQuery("select sys_enterpriseid from sa_agents where siteid='CCYOSG' and agentnum='" + fagentnum + "'").isNotEmpty()) {
  55. sys_enterpriseid = dbConnect.runSqlQuery("select sys_enterpriseid from sa_agents where siteid='CCYOSG' and agentnum='" + fagentnum + "'").get(0).getLong("sys_enterpriseid");
  56. } else {
  57. msg = "yos中不存在【" + fagentnum + "】经销商";
  58. return getErrReturnObject().setErrMsg(msg).toString();
  59. }
  60. // 单据日期
  61. String fdate = content.getString("fdate");
  62. // 会计期间
  63. // String fperiod = content.getString("fperiod");
  64. // 红蓝字
  65. String frb = content.getString("frb");
  66. // 表头仓库
  67. // String fstockno = content.getString("fstockno");
  68. // 单据类型明细
  69. //String ftypemx = content.getString("ftypemx");
  70. // 单据状态
  71. String fstatus = content.getString("fstatus");
  72. // 制单人
  73. String createby = content.getString("createby");
  74. // 制单日期
  75. String createdate = content.getString("createdate");
  76. // 审核人
  77. String checkby = content.getString("checkby");
  78. // 审核日期
  79. String checkdate = content.getString("checkdate");
  80. // 备注
  81. String fnotes = content.getString("fnotes");
  82. siteid = "CCYOSG";
  83. Long sa_cashbillid = 0L;
  84. Rows stockbillRows = dbConnect.runSqlQuery("select * from st_stockbill where siteid='CCYOSG' and status='审核' and billno='" + fbillnum + "'");
  85. Long st_stockbillid = 0L;
  86. if (stockbillRows.isEmpty() && "审核".equals(fstatus)) {
  87. /**
  88. * DRP不存在销售出库单,且中间表状态为审核且不在删除状态时,DRP中需新增该销售出库单
  89. */
  90. SQLFactory sqlFactory = new SQLFactory(this, "销售出库单新增");
  91. st_stockbillid = createTableID("st_stockbill");
  92. sqlFactory.addParameter("siteid", "CCYOSG");
  93. sqlFactory.addParameter("st_stockbillid", st_stockbillid);
  94. sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  95. sqlFactory.addParameter("billno", fbillnum);
  96. sqlFactory.addParameter("type", "销售出库");
  97. sqlFactory.addParameter("rb", frb.equals("蓝") ? 1 : 0);
  98. sqlFactory.addParameter("billdate", fdate);
  99. sqlFactory.addParameter("createby", createby);
  100. sqlFactory.addParameter("createdate", getDate(createdate));
  101. sqlFactory.addParameter("changeby", createby);
  102. sqlFactory.addParameter("changedate", getDate(createdate));
  103. sqlFactory.addParameter("checkby", checkby);
  104. sqlFactory.addParameter("checkdate", getDate(checkdate));
  105. sqlFactory.addParameter("remarks", fnotes);
  106. sqlList.add(sqlFactory.getSQL());
  107. JSONArray detailarray = content.getJSONArray("icstockbilldetails");
  108. StringBuffer str = new StringBuffer();
  109. List<Long> sa_dispatch_itemsids = new ArrayList<>();
  110. sa_dispatch_itemsids.add(0l);
  111. for (Object object : detailarray) {
  112. JSONObject jsonObject = (JSONObject) object;
  113. if (str.toString().equals("")) {
  114. str.append("'" + jsonObject.getString("fitemno") + "'");
  115. } else {
  116. str.append(",'" + jsonObject.getString("fitemno") + "'");
  117. }
  118. sa_dispatch_itemsids.add(jsonObject.getLong("sainvoicedetailid"));
  119. }
  120. Rows rows;
  121. if (!str.toString().equals("")) {
  122. rows = dbConnect.runSqlQuery("select * from plm_item where siteid='CCYOSG' and itemno in " + "(" + str.toString() + ")");
  123. } else {
  124. rows = dbConnect.runSqlQuery("select * from plm_item where siteid='CCYOSG' and itemno in('')");
  125. }
  126. RowsMap itemRowsMap = rows.toRowsMap("itemno");
  127. Rows distiptchRows = dbConnect.runSqlQuery("select t2.price,t1.sa_dispatch_itemsid,t1.sa_orderitemsid,t2.sa_orderid,t3.sys_enterpriseid,t4.sonum,t5.itemno,t5.itemname from sa_dispatch_items t1 inner join sa_orderitems t2 on t1.sa_orderitemsid=t2.sa_orderitemsid and t1.siteid=t2.siteid inner join sa_dispatch t3 on t1.sa_dispatchid=t3.sa_dispatchid and t1.siteid=t3.siteid left join sa_order t4 on t2.sa_orderid=t4.sa_orderid and t2.siteid=t4.siteid left join plm_item t5 on t1.itemid=t5.itemid and t1.siteid=t5.siteid where t3.status in('审核','复核','关闭') and t1.siteid='CCYOSG' and t1.sa_dispatch_itemsid in " + sa_dispatch_itemsids.toString().replace("[", "(").replace("]", ")"));
  128. RowsMap distiptchRowsMap = distiptchRows.toRowsMap("sa_dispatch_itemsid");
  129. Rows aftersalesmagRows = dbConnect.runSqlQuery("select t1.price,t1.sa_aftersalesmag_itemsid,t1.sa_orderitemsid,t2.sa_orderid from sa_aftersalesmag_items t1 inner join sa_orderitems t2 on t1.sa_orderitemsid=t2.sa_orderitemsid and t1.siteid=t2.siteid where t1.siteid='CCYOSG' and t1.sa_aftersalesmag_itemsid in " + sa_dispatch_itemsids.toString().replace("[", "(").replace("]", ")"));
  130. RowsMap aftersalesmagRowsMap = aftersalesmagRows.toRowsMap("sa_aftersalesmag_itemsid");
  131. BigDecimal amountTotal = BigDecimal.ZERO;
  132. for (Object object : detailarray) {
  133. JSONObject jsonObject = (JSONObject) object;
  134. String frownum = jsonObject.getString("frownum");
  135. String fitemno = jsonObject.getString("fitemno");
  136. System.err.println(frownum + ":" + fitemno);
  137. BigDecimal fqty = new BigDecimal(jsonObject.getString("fqty"));
  138. fqty = fqty.compareTo(BigDecimal.ZERO) < 0 ? fqty.negate() : fqty;
  139. //String fdetailstockno = jsonObject.getString("fstockno");
  140. long sainvoicedetailid = jsonObject.getLong("sainvoicedetailid");
  141. //String fbatchno = jsonObject.getString("fbatchno");
  142. String fdetailnotes = jsonObject.getString("fnotes");
  143. SQLFactory sqlFactorydetail = new SQLFactory(this, "销售出库单明细新增");
  144. sqlFactorydetail.addParameter("siteid", "CCYOSG");
  145. sqlFactorydetail.addParameter("st_stockbill_itemsid", createTableID("st_stockbill_items"));
  146. sqlFactorydetail.addParameter("st_stockbillid", st_stockbillid);
  147. sqlFactorydetail.addParameter("rowno", frownum);
  148. sqlFactorydetail.addParameter("sa_dispatch_itemsid", sainvoicedetailid);
  149. sqlFactorydetail.addParameter("remarks", fnotes);
  150. if (itemRowsMap.containsKey(fitemno) && itemRowsMap.get(fitemno).isNotEmpty()) {
  151. sqlFactorydetail.addParameter("itemid", itemRowsMap.get(fitemno).get(0).getLong("itemid"));
  152. } else {
  153. sqlFactorydetail.addParameter("itemid", 0);
  154. }
  155. sqlFactorydetail.addParameter("remarks", fdetailnotes);
  156. sqlFactorydetail.addParameter("qty", fqty);
  157. if (frb.equals("蓝")) {
  158. if (distiptchRowsMap.containsKey(String.valueOf(sainvoicedetailid)) && distiptchRowsMap.get(sainvoicedetailid).isNotEmpty()) {
  159. sqlFactorydetail.addParameter("price", distiptchRowsMap.get(sainvoicedetailid).get(0).getBigDecimal("price"));
  160. amountTotal = amountTotal.add(fqty.multiply(distiptchRowsMap.get(sainvoicedetailid).get(0).getBigDecimal("price")));
  161. sqlFactorydetail.addParameter("amount", fqty.multiply(distiptchRowsMap.get(sainvoicedetailid).get(0).getBigDecimal("price")));
  162. sqlFactorydetail.addParameter("sa_orderitemsid", distiptchRowsMap.get(sainvoicedetailid).get(0).getLong("sa_orderitemsid"));
  163. sqlFactorydetail.addParameter("sa_orderid", distiptchRowsMap.get(sainvoicedetailid).get(0).getLong("sa_orderid"));
  164. System.out.println(amountTotal);
  165. // //您的订单【XXX】商品【XXX】工厂已发货,出库单号【XXX】
  166. // String message = "您的订单【" + distiptchRowsMap.get(sainvoicedetailid).get(0).getString("sonum") + "】商品【" + distiptchRowsMap.get(sainvoicedetailid).get(0).getString("itemname") + "】工厂已发货,出库单号【" + fbillnum + "】!";
  167. // sendMsg(message, st_stockbillid, distiptchRowsMap.get(sainvoicedetailid).get(0).getLong("sys_enterpriseid"));
  168. } else {
  169. sqlFactorydetail.addParameter("price", 0);
  170. sqlFactorydetail.addParameter("amount", 0);
  171. sqlFactorydetail.addParameter("sa_orderitemsid", 0);
  172. sqlFactorydetail.addParameter("sa_orderid", 0);
  173. }
  174. // /**
  175. // * 更新发货单数量
  176. // */
  177. // sqlList.add("update sa_dispatch_items set outwarehousedate=CURRENT_TIME,outwarehouseqty=ifnull(outwarehouseqty,0)+" + fqty + " where siteid='CCYOSG' and sa_dispatch_itemsid=" + sainvoicedetailid);
  178. } else {
  179. if (aftersalesmagRowsMap.containsKey(String.valueOf(sainvoicedetailid)) && aftersalesmagRowsMap.get(sainvoicedetailid).isNotEmpty()) {
  180. logger.info(aftersalesmagRowsMap.get(sainvoicedetailid).get(0).toJsonObject().toString());
  181. sqlFactorydetail.addParameter("price", aftersalesmagRowsMap.get(sainvoicedetailid).get(0).getBigDecimal("price"));
  182. sqlFactorydetail.addParameter("amount", fqty.multiply(aftersalesmagRowsMap.get(sainvoicedetailid).get(0).getBigDecimal("price")));
  183. logger.info(aftersalesmagRowsMap.get(sainvoicedetailid).get(0).getBigDecimal("sa_orderitemsid"));
  184. logger.info(aftersalesmagRowsMap.get(sainvoicedetailid).get(0).getBigDecimal("sa_orderid"));
  185. sqlFactorydetail.addParameter("sa_orderitemsid", aftersalesmagRowsMap.get(sainvoicedetailid).get(0).getLong("sa_orderitemsid"));
  186. sqlFactorydetail.addParameter("sa_orderid", aftersalesmagRowsMap.get(sainvoicedetailid).get(0).getLong("sa_orderid"));
  187. } else {
  188. sqlFactorydetail.addParameter("price", 0);
  189. sqlFactorydetail.addParameter("amount", 0);
  190. sqlFactorydetail.addParameter("sa_orderitemsid", 0);
  191. sqlFactorydetail.addParameter("sa_orderid", 0);
  192. }
  193. }
  194. sqlList.add(sqlFactorydetail.getSQL());
  195. }
  196. // if (frb.equals("蓝")) {
  197. // BigDecimal billamount = BigDecimal.ZERO;
  198. // //返利比例
  199. // String icstockbillrebateaccount = Parameter.get(siteid, "icstockbillrebateaccount"); //经销商销售出库单返利账户
  200. // String icstockbillrebaterate1 = Parameter.get(siteid, "icstockbillrebaterate"); //销售出库单返利比例
  201. // if (!StringUtils.isBlank(icstockbillrebateaccount) && !StringUtils.isBlank(icstockbillrebaterate1)) {
  202. // BigDecimal icstockbillrebaterate = new BigDecimal(icstockbillrebaterate1);
  203. // //经销商返利
  204. // BigDecimal rebateamount = icstockbillrebaterate.multiply(amountTotal);
  205. // System.out.println(rebateamount);
  206. // CashbillEntity entity = new CashbillEntity();
  207. // entity.setAmount(rebateamount);
  208. // entity.setOwnerid(st_stockbillid);
  209. // entity.setOwnertable("st_stockbill");
  210. // entity.setSource("");
  211. // entity.setRemarks("销售出库单" + fbillnum + "返利");
  212. // JSONObject cashbillIncome = Accountbalance.createCashbillIncome(this, sys_enterpriseid, Long.valueOf(icstockbillrebateaccount).longValue(), entity, true, false);
  213. // sqlList.addAll(cashbillIncome.getJSONArray("sqlList").toJavaList(String.class));
  214. // sa_cashbillid = cashbillIncome.getLong("sa_cashbillid");
  215. // }
  216. //
  217. //
  218. // }
  219. sqlList.add("update st_stockbill set status='审核',checkby='" + checkby + "',checkdate=CURRENT_TIMESTAMP where st_stockbillid = " + st_stockbillid + " and siteid='CCYOSG'");
  220. } else if (!stockbillRows.isEmpty() && "审核".equals(fstatus)) {
  221. /**
  222. * DRP存在销售出库单,且中间表状态为审核且不在删除状态时,DRP中需将当前销售出库单先反审核,修改内容后再进行审核
  223. */
  224. //销售出库单反审核逻辑待添加
  225. // if (frb.equals("蓝")) {
  226. // BigDecimal billamount = BigDecimal.ZERO;
  227. // Rows stockbilldetails = dbConnect.runSqlQuery("select * from st_stockbill_items where st_stockbillid ='"
  228. // + stockbillRows.get(0).getLong("st_stockbillid") + "' and siteid='" + siteid + "'");
  229. // for (Row row : stockbilldetails) {
  230. // billamount = billamount.add(row.getBigDecimal("amount").abs().negate());
  231. // }
  232. //
  233. // //返利比例
  234. // String icstockbillrebateaccount = Parameter.get(siteid, "icstockbillrebateaccount"); //经销商销售出库单返利账户
  235. // String icstockbillrebaterate1 = Parameter.get(siteid, "icstockbillrebaterate"); //销售出库单返利比例
  236. // BigDecimal icstockbillrebaterate = new BigDecimal(icstockbillrebaterate1);
  237. // //经销商返利
  238. // BigDecimal rebateamount = icstockbillrebaterate.multiply(billamount);
  239. //
  240. // CashbillEntity entity = new CashbillEntity();
  241. // entity.setAmount(rebateamount);
  242. // entity.setOwnerid(stockbillRows.get(0).getLong("st_stockbillid"));
  243. // entity.setOwnertable("st_stockbill");
  244. // entity.setSource("");
  245. // entity.setRemarks("销售出库单" + fbillnum + "反返利");
  246. // JSONObject cashbillIncome = Accountbalance.createCashbillIncome(this, sys_enterpriseid, Long.valueOf(icstockbillrebateaccount).longValue(), entity, true, false);
  247. // sqlList.addAll(cashbillIncome.getJSONArray("sqlList").toJavaList(String.class));
  248. // sa_cashbillid = cashbillIncome.getLong("sa_cashbillid");
  249. // /**
  250. // * 更新发货单数量
  251. // */
  252. // Rows stockbilldetailrows = dbConnect.runSqlQuery("select t1.sa_dispatch_itemsid,sum(t1.qty) qty,t2.sa_dispatchid from st_stockbill_items t1 inner join sa_dispatch_items t2 on t1.sa_dispatch_itemsid=t2.sa_dispatch_itemsid and t1.siteid=t2.siteid where t1.st_stockbillid = " + stockbillRows.get(0).getLong("st_stockbillid") + " and t1.siteid='CCYOSG' group by t1.sa_dispatch_itemsid");
  253. // for (Row row : stockbilldetailrows) {
  254. // sqlList.add("update sa_dispatch_items set outwarehousedate=CURRENT_TIME,outwarehouseqty=ifnull(outwarehouseqty,0)-" + row.getBigDecimal("qty") + " where siteid='CCYOSG' and sa_dispatch_itemsid=" + row.getLong("sa_dispatch_itemsid"));
  255. // sqlList.add("update sa_dispatch set closedate=null,closeby='',status='复核' where siteid='CCYOSG' and sa_dispatchid=" + row.getLong("sa_dispatchid"));
  256. // }
  257. // }
  258. sqlList.add("update st_stockbill set status='新建',checkby='',checkdate=null where st_stockbillid = " + stockbillRows.get(0).getLong("st_stockbillid") + " and siteid='CCYOSG'");
  259. msg = "DRP单据状态为审核,现已反审核,下次同步时进行审核操作";
  260. code = 0;
  261. } else if (!stockbillRows.isEmpty() && ("新建".equals(fstatus))) {
  262. /**
  263. * DRP存在收入凭证,且中间表状态为新建或在删除状态时,DRP中需将当前收入销售出库单先反审核
  264. */
  265. //销售出库单反审核逻辑待添加
  266. // if (frb.equals("蓝")) {
  267. // BigDecimal billamount = BigDecimal.ZERO;
  268. // Rows stockbilldetails = dbConnect.runSqlQuery("select * from st_stockbill_items where st_stockbillid ='"
  269. // + stockbillRows.get(0).getLong("st_stockbillid") + "' and siteid='" + siteid + "'");
  270. // for (Row row : stockbilldetails) {
  271. // billamount = billamount.add(row.getBigDecimal("amount").abs().negate());
  272. // }
  273. //
  274. // //返利比例
  275. // String icstockbillrebateaccount = Parameter.get(siteid, "icstockbillrebateaccount"); //经销商销售出库单返利账户
  276. // String icstockbillrebaterate1 = Parameter.get(siteid, "icstockbillrebaterate"); //销售出库单返利比例
  277. // BigDecimal icstockbillrebaterate = new BigDecimal(icstockbillrebaterate1);
  278. // //经销商返利
  279. // BigDecimal rebateamount = icstockbillrebaterate.multiply(billamount);
  280. //
  281. // CashbillEntity entity = new CashbillEntity();
  282. // entity.setAmount(rebateamount);
  283. // entity.setOwnerid(stockbillRows.get(0).getLong("st_stockbillid"));
  284. // entity.setOwnertable("st_stockbill");
  285. // entity.setSource("");
  286. // entity.setRemarks("销售出库单" + fbillnum + "反返利");
  287. // JSONObject cashbillIncome = Accountbalance.createCashbillIncome(this, sys_enterpriseid, Long.valueOf(icstockbillrebateaccount).longValue(), entity, true, false);
  288. // sqlList.addAll(cashbillIncome.getJSONArray("sqlList").toJavaList(String.class));
  289. // sa_cashbillid = cashbillIncome.getLong("sa_cashbillid");
  290. // /**
  291. // * 更新发货单数量
  292. // */
  293. // Rows stockbilldetailrows = dbConnect.runSqlQuery("select t1.sa_dispatch_itemsid,sum(t1.qty) qty,t2.sa_dispatchid from st_stockbill_items t1 inner join sa_dispatch_items t2 on t1.sa_dispatch_itemsid=t2.sa_dispatch_itemsid and t1.siteid=t2.siteid where t1.st_stockbillid = " + stockbillRows.get(0).getLong("st_stockbillid") + " and t1.siteid='CCYOSG' group by t1.sa_dispatch_itemsid");
  294. // for (Row row : stockbilldetailrows) {
  295. // sqlList.add("update sa_dispatch_items set outwarehousedate=CURRENT_TIME,outwarehouseqty=ifnull(outwarehouseqty,0)-" + row.getBigDecimal("qty") + " where siteid='CCYOSG' and sa_dispatch_itemsid=" + row.getLong("sa_dispatch_itemsid"));
  296. // sqlList.add("update sa_dispatch set closedate=null,closeby='',status='复核' where siteid='CCYOSG' and sa_dispatchid=" + row.getLong("sa_dispatchid"));
  297. // }
  298. //
  299. //
  300. // }
  301. sqlList.add("update st_stockbill set status='新建',checkby='',checkdate=null where st_stockbillid = " + stockbillRows.get(0).getLong("st_stockbillid") + " and siteid='CCYOSG'");
  302. }
  303. dbConnect.runSqlUpdate(sqlList);
  304. if (stockbillRows.isEmpty() && "审核".equals(fstatus)) {
  305. ArrayList<Long> userIdList = dbConnect.runSqlQuery("select userid from sys_enterprise_hr where siteid='" + siteid + "' and sys_enterpriseid=" + sys_enterpriseid).toArrayList("userid", new ArrayList<>());
  306. Executor.sendEml(this, "stocck_check", st_stockbillid, siteid, userIdList);
  307. }
  308. Rows stockbillrows = dbConnect.runSqlQuery("select st_stockbillid,status,rb from st_stockbill where billno='" + fbillnum + "' and siteid='CCYOSG'");
  309. // if (frb.equals("蓝")) {
  310. // //查询可关闭的发货单
  311. // Rows dispatchrows = dbConnect.runSqlQuery("select t.sa_dispatchid from (select sa_dispatchid,sum(qty) sumqty,sum(ifnull(outwarehouseqty,0)) sumoutwarehouseqty from sa_dispatch_items where sa_dispatchid in(select sa_dispatchid from sa_dispatch_items where sa_dispatch_itemsid in(select t1.sa_dispatch_itemsid from st_stockbill_items t1 inner join st_stockbill t2 on t1.st_stockbillid=t2.st_stockbillid and t1.siteid=t2.siteid where t1.st_stockbillid =" + stockbillrows.get(0).getLong("st_stockbillid") + " and t1.siteid='CCYOSG')) group by sa_dispatchid)t where t.sumqty=t.sumoutwarehouseqty");
  312. // Rows undispatchrows = dbConnect.runSqlQuery("select t.sa_dispatchid from (select sa_dispatchid,sum(qty) sumqty,sum(ifnull(outwarehouseqty,0)) sumoutwarehouseqty from sa_dispatch_items where sa_dispatchid in(select sa_dispatchid from sa_dispatch_items where sa_dispatch_itemsid in(select t1.sa_dispatch_itemsid from st_stockbill_items t1 inner join st_stockbill t2 on t1.st_stockbillid=t2.st_stockbillid and t1.siteid=t2.siteid where t1.st_stockbillid =" + stockbillrows.get(0).getLong("st_stockbillid") + " and t1.siteid='CCYOSG')) group by sa_dispatchid)t where t.sumqty!=t.sumoutwarehouseqty");
  313. // ArrayList<String> sqldispatch = new ArrayList<>();
  314. // for (Row row : dispatchrows) {
  315. // sqldispatch.add("update sa_dispatch set status='关闭',closedate=current_time,closeby='admin' where sa_dispatchid='" + row.getLong("sa_dispatchid") + "' and siteid='CCYOSG'");
  316. // SQLFactory sqlFactory = new SQLFactory(this, "数据操作日志新增");
  317. // sqlFactory.addParameter("sys_contrllogid", createTableID("sys_contrllog"));
  318. // sqlFactory.addParameter("ownertable", "sa_dispatch");
  319. // sqlFactory.addParameter("ownerid", row.getLong("sa_dispatchid"));
  320. // sqlFactory.addParameter("action", "自动关闭");
  321. // sqlFactory.addParameter("remarks", "发货单自动关闭成功");
  322. // sqlFactory.addParameter("actionuserid", "1");
  323. // sqlFactory.addParameter("actionby", "admin");
  324. // sqlFactory.addParameter("siteid", "CCYOSG");
  325. // sqldispatch.add(sqlFactory.getSQL());
  326. // }
  327. // for (Row row : undispatchrows) {
  328. // sqldispatch.add("update sa_dispatch set status='复核',closedate=null,closeby='' where sa_dispatchid='" + row.getLong("sa_dispatchid") + "' and siteid='CCYOSG'");
  329. // sqldispatch.add("delete from sys_contrllog where ownertable='sa_dispatch' and action='自动关闭' and ownerid='" + row.getLong("sa_dispatchid") + "' and siteid='CCYOSG'");
  330. // }
  331. //
  332. // dbConnect.runSqlUpdate(sqldispatch);
  333. //
  334. // //查询可关闭的订单
  335. // //订单出库总数量
  336. // Rows orderinstockrows = dbConnect.runSqlQuery("select t1.sa_orderid,t1.siteid,sum(ifnull(t2.outwarehouseqty,0)) sumoutwarehouseqty from sa_orderitems t1 left join sa_dispatch_items t2 on t1.sa_orderitemsid=t2.sa_orderitemsid and t1.siteid=t2.siteid where t1.sa_orderid in(select t3.sa_orderid from st_stockbill_items t1 inner join sa_dispatch_items t2 on t1.sa_dispatch_itemsid=t2.sa_dispatch_itemsid and t1.siteid=t2.siteid inner join sa_orderitems t3 on t2.sa_orderitemsid=t3.sa_orderitemsid and t2.siteid=t3.siteid where t1.st_stockbillid =" + stockbillrows.get(0).getLong("st_stockbillid") + " and t1.siteid='CCYOSG') group by t1.sa_orderid,t1.siteid");
  337. // //订单总数量
  338. // Rows ordersumrows = dbConnect.runSqlQuery("select t1.sa_orderid,t1.siteid,sum(qty) sumqty from sa_orderitems t1 where t1.sa_orderid in(select t3.sa_orderid from st_stockbill_items t1 inner join sa_dispatch_items t2 on t1.sa_dispatch_itemsid=t2.sa_dispatch_itemsid and t1.siteid=t2.siteid inner join sa_orderitems t3 on t2.sa_orderitemsid=t3.sa_orderitemsid and t2.siteid=t3.siteid where t1.st_stockbillid =" + stockbillrows.get(0).getLong("st_stockbillid") + " and t1.siteid='CCYOSG') group by t1.sa_orderid,t1.siteid");
  339. // RowsMap orderinstockRowsMap = orderinstockrows.toRowsMap("sa_orderid");
  340. //
  341. //
  342. // ArrayList<String> sqlorder = new ArrayList<>();
  343. // for (Row row : ordersumrows) {
  344. // if (orderinstockRowsMap.containsKey(row.getString("sa_orderid"))) {
  345. // if (orderinstockRowsMap.get(row.getString("sa_orderid")).get(0).getBigDecimal("sumoutwarehouseqty").compareTo(row.getBigDecimal("sumqty")) == 0) {
  346. // sqlorder.add("update sa_order set status='关闭',closedate=current_time,closeby='admin' where sa_orderid='" + row.getLong("sa_orderid") + "' and siteid='CCYOSG'");
  347. // SQLFactory sqlFactory = new SQLFactory(this, "数据操作日志新增");
  348. // sqlFactory.addParameter("sys_contrllogid", createTableID("sys_contrllog"));
  349. // sqlFactory.addParameter("ownertable", "sa_order");
  350. // sqlFactory.addParameter("ownerid", row.getLong("sa_orderid"));
  351. // sqlFactory.addParameter("action", "自动关闭");
  352. // sqlFactory.addParameter("remarks", "订单自动关闭成功");
  353. // sqlFactory.addParameter("actionuserid", "1");
  354. // sqlFactory.addParameter("actionby", "admin");
  355. // sqlFactory.addParameter("siteid", "CCYOSG");
  356. // sqlorder.add(sqlFactory.getSQL());
  357. // } else {
  358. // sqlorder.add("update sa_order set status='审核',closedate=null,closeby='' where sa_orderid='" + row.getLong("sa_orderid") + "' and siteid='CCYOSG'");
  359. // sqlorder.add("delete from sys_contrllog where ownertable='sa_order' and action='自动关闭' and ownerid='" + row.getLong("sa_orderid") + "' and siteid='CCYOSG'");
  360. // }
  361. // } else {
  362. // sqlorder.add("update sa_order set status='审核',closedate=null,closeby='' where sa_orderid='" + row.getLong("sa_orderid") + "' and siteid='CCYOSG'");
  363. // sqlorder.add("delete from sys_contrllog where ownertable='sa_order' and action='自动关闭' and ownerid='" + row.getLong("sa_orderid") + "' and siteid='CCYOSG'");
  364. // }
  365. //
  366. // }
  367. // dbConnect.runSqlUpdate(sqlorder);
  368. //
  369. // }
  370. /**
  371. * 如单据为新建状态,则,先把单据强行删除掉
  372. */
  373. dbConnect.runSqlUpdate("delete from st_stockbill where status='新建'");
  374. } catch (Exception e) {
  375. e.printStackTrace();
  376. msg = e.getMessage();
  377. if (msg == null) {
  378. msg = "";
  379. }
  380. code = 0;
  381. dbConnect.runSqlUpdate("delete from st_stockbill_items where st_stockbillid in(select st_stockbillid from st_stockbill where siteid='CCYOSG' and billno='" + fbillnum + "')");
  382. dbConnect.runSqlUpdate("delete from st_stockbill where billno='" + fbillnum + "' and siteid='CCYOSG'");
  383. }
  384. JSONObject returnObject = new JSONObject();
  385. returnObject.put("msg", msg);
  386. returnObject.put("code", code);
  387. return returnObject.toString();
  388. }
  389. public String getDate(String date) {
  390. try {
  391. Date a = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(date);
  392. return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(a);
  393. } catch (Exception e) {
  394. e.printStackTrace();
  395. }
  396. return null;
  397. }
  398. public void sendMsg(String content, Long st_stockbillid, Long sys_enterpriseid) throws YosException {
  399. ArrayList<Long> userList = getEnterpriseHrs(sys_enterpriseid).toArrayList("userid", new ArrayList<>());
  400. Remind remind = new Remind(this);
  401. remind.setTitle("销售出库单消息");
  402. remind.setContent(content);
  403. remind.setType("应用");
  404. remind.setObjectid(st_stockbillid);
  405. remind.setObjectname("st_stockbill");
  406. remind.setTouserid(userList);
  407. remind.sendByDialogMsg().createSys_message();
  408. }
  409. }