dispatch.java 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. package restcontroller.webmanage.sale.dispatch;
  2. import beans.data.BatchDeleteErr;
  3. import beans.datacontrllog.DataContrlLog;
  4. import beans.dispatch.Dispatch;
  5. import beans.parameter.Parameter;
  6. import com.alibaba.fastjson.JSONArray;
  7. import com.alibaba.fastjson.JSONObject;
  8. import common.Controller;
  9. import common.YosException;
  10. import common.annotation.API;
  11. import common.annotation.CACHEING;
  12. import common.annotation.CACHEING_CLEAN;
  13. import common.data.Row;
  14. import common.data.Rows;
  15. import common.data.RowsMap;
  16. import common.data.SQLFactory;
  17. import org.apache.commons.lang.StringUtils;
  18. import org.apache.log4j.Logger;
  19. import restcontroller.R;
  20. import restcontroller.webmanage.executorService.Executor;
  21. import restcontroller.webmanage.sale.order.Order;
  22. import restcontroller.webmanage.sale.order.OrderItems;
  23. import utility.ERPDocking;
  24. import java.math.BigDecimal;
  25. import java.util.ArrayList;
  26. import static java.time.LocalDate.now;
  27. /**
  28. * 发货单
  29. */
  30. @API(title = "发货单")
  31. public class dispatch extends Controller {
  32. private static Logger logger = Logger.getLogger(dispatch.class);
  33. public dispatch(JSONObject arg0) throws YosException {
  34. super(arg0);
  35. // TODO Auto-generated constructor stub
  36. }
  37. @API(title = "发货单新增更新", apiversion = R.ID20221114135203.v1.class)
  38. @CACHEING_CLEAN(apiversions = {R.ID20221114135303.v1.class, R.ID20221114135403.v1.class, R.ID20230320160203.v1.class})
  39. public String insertormodify_dispatch() throws YosException {
  40. ArrayList<String> sqlList = new ArrayList<>();
  41. // 表名
  42. String tableName = "sa_dispatch";
  43. Long sa_dispatchid = content.getLong("sa_dispatchid");
  44. Long sa_orderid = content.getLong("sa_orderid");
  45. // Long sys_enterpriseid = content.getLong("sys_enterpriseid");
  46. // Long sa_logiscompid = content.getLong("sa_logiscompid");
  47. // Long rec_contactsid = content.getLongValue("rec_contactsid");
  48. String remarks = content.getStringValue("remarks");
  49. String billdate = content.getStringValue("billdate");
  50. SQLFactory sqlFactory = new SQLFactory(this, "发货单新增");
  51. if (sa_dispatchid <= 0) {
  52. sa_dispatchid = createTableID(tableName);
  53. Rows rowsinfo = dbConnect
  54. .runSqlQuery("select contactsid from sys_enterprise_contacts where workaddress=1 and sys_enterpriseid="
  55. + content.getLong("sys_enterpriseid"));
  56. if (rowsinfo.isEmpty()) {
  57. //return getErrReturnObject().setErrMsg("该订单不存在").toString();
  58. }
  59. sqlFactory.addParameter("sa_orderid", sa_orderid);
  60. // 订货企业id
  61. sqlFactory.addParameter("sys_enterpriseid",content.getLong("sys_enterpriseid"));
  62. // 物流公司档案ID
  63. //sqlFactory.addParameter("sa_logiscompid", rowsinfo.get(0).getLong("sa_logiscompid"));
  64. // 合作企业联系人表ID(收货信息)
  65. if(!rowsinfo.isEmpty()){
  66. sqlFactory.addParameter("rec_contactsid", rowsinfo.get(0).getLong("contactsid"));
  67. }else {
  68. sqlFactory.addParameter("rec_contactsid",0);
  69. }
  70. //运费状态
  71. sqlFactory.addParameter("freightstatus","");
  72. sqlFactory.addParameter("remarks",remarks);
  73. sqlFactory.addParameter("billdate", now());
  74. sqlList.add(DataContrlLog.createLog(this, "sa_dispatch", sa_dispatchid, "新增", "发货单新增成功").getSQL());
  75. } else {
  76. Rows rows = dbConnect.runSqlQuery(
  77. "SELECT status,sys_enterpriseid,rec_contactsid,sa_orderid,freightstatus from sa_dispatch WHERE sa_dispatchid = "
  78. + sa_dispatchid);
  79. if (rows.isNotEmpty()) {
  80. if (rows.get(0).getString("status").equals("新建")) {
  81. sqlFactory = new SQLFactory(this, "发货单更新");
  82. if (sa_orderid <= 0) {
  83. sqlFactory.addParameter("sa_orderid", rows.get(0).getLong("sa_orderid"));
  84. // 订货企业id
  85. if (content.getLong("sys_enterpriseid") <= 0) {
  86. sqlFactory.addParameter("sys_enterpriseid", rows.get(0).getLong("sys_enterpriseid"));
  87. } else {
  88. sqlFactory.addParameter("sys_enterpriseid", content.getLong("sys_enterpriseid"));
  89. }
  90. // 物流公司档案ID
  91. // if (content.getLong("sa_logiscompid") <= 0) {
  92. // sqlFactory.addParameter("sa_logiscompid", rows.get(0).getLong("sa_logiscompid"));
  93. // } else {
  94. // sqlFactory.addParameter("sa_logiscompid", content.getLong("sa_logiscompid"));
  95. // }
  96. // 合作企业联系人表ID(收货信息)
  97. if (content.getLong("rec_contactsid") <= 0) {
  98. sqlFactory.addParameter("rec_contactsid", rows.get(0).getLong("rec_contactsid"));
  99. } else {
  100. sqlFactory.addParameter("rec_contactsid", content.getLong("rec_contactsid"));
  101. }
  102. } else {
  103. Rows rowsinfo = dbConnect.runSqlQuery(
  104. "select rec_contactsid,sys_enterpriseid from sa_order where sa_orderid="
  105. + sa_orderid);
  106. if (rowsinfo.isEmpty()) {
  107. return getErrReturnObject().setErrMsg("该发货单不存在").toString();
  108. }
  109. sqlFactory.addParameter("sa_orderid", sa_orderid);
  110. // 订货企业id
  111. sqlFactory.addParameter("sys_enterpriseid", rowsinfo.get(0).getLong("sys_enterpriseid"));
  112. // 物流公司档案ID
  113. //sqlFactory.addParameter("sa_logiscompid", rowsinfo.get(0).getLong("sa_logiscompid"));
  114. // 合作企业联系人表ID(收货信息)
  115. sqlFactory.addParameter("rec_contactsid", rowsinfo.get(0).getLong("rec_contactsid"));
  116. }
  117. //运费状态
  118. if (!StringUtils.isBlank(content.getStringValue("freightstatus"))) {
  119. sqlFactory.addParameter("freightstatus", content.getStringValue("freightstatus"));
  120. } else {
  121. sqlFactory.addParameter("freightstatus", rows.get(0).getString("freightstatus"));
  122. }
  123. sqlFactory.addParameter("billdate", billdate);
  124. sqlFactory.addParameter("remarks", remarks);
  125. sqlList.add(DataContrlLog.createLog(this, "sa_dispatch", sa_dispatchid, "更新", "发货单更新成功").getSQL());
  126. } else {
  127. return getErrReturnObject().setErrMsg("非新建状态下无法编辑").toString();
  128. }
  129. } else {
  130. return getErrReturnObject().setErrMsg("该发货单不存在").toString();
  131. }
  132. }
  133. sqlFactory.addParameter("siteid", siteid);
  134. sqlFactory.addParameter("userid", userid);
  135. sqlFactory.addParameter("username", username);
  136. sqlFactory.addParameter("sa_dispatchid", sa_dispatchid);
  137. // 发货单号createBillCode("dispatchbillno")
  138. sqlFactory.addParameter("billno", createBillCode("dispatchbill"));
  139. sqlList.add(sqlFactory.getSQL());
  140. dbConnect.runSqlUpdate(sqlList);
  141. content.put("sa_dispatchid", sa_dispatchid);
  142. return queryDispatchMain();
  143. }
  144. @API(title = "发货单详情", apiversion = R.ID20221114135303.v1.class)
  145. @CACHEING
  146. public String queryDispatchMain() throws YosException {
  147. Long sa_dispatchid = content.getLong("sa_dispatchid");
  148. SQLFactory sqlFactory = new SQLFactory(this, "发货单详情查询");
  149. sqlFactory.addParameter("sa_dispatchid", sa_dispatchid);
  150. sqlFactory.addParameter("siteid", siteid);
  151. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  152. Rows sum = dbConnect.runSqlQuery("select sum(t1.qty * t2.price) sumamount,sum(t1.qty) qty from sa_dispatch_items t1 inner join sa_orderitems t2 on t1.sa_orderitemsid = t2.sa_orderitemsid and t1.siteid = t2.siteid where t1.siteid='" + siteid + "' AND t1.sa_dispatchid=" + sa_dispatchid);
  153. Row row = rows.isNotEmpty() ? rows.get(0) : new Row();
  154. if (sum.isNotEmpty()) {
  155. row.put("sumamount", sum.get(0).getBigDecimal("sumamount"));
  156. row.put("qty", sum.get(0).getBigDecimal("qty"));
  157. }
  158. //物流
  159. // SQLFactory sqlFactory1 = new SQLFactory(this, "发货单-查询物流信息");
  160. // sqlFactory1.addParameter("siteid", siteid);
  161. // sqlFactory1.addParameter("sa_dispatchid", sa_dispatchid);
  162. // Rows logiscompRows = dbConnect.runSqlQuery(sqlFactory1);
  163. // row.put("logiscomp", logiscompRows.isNotEmpty() ? logiscompRows.get(0) : new Row());
  164. //收货
  165. SQLFactory sqlFactory2 = new SQLFactory(this, "发货单-查询收货信息");
  166. sqlFactory2.addParameter("siteid", siteid);
  167. sqlFactory2.addParameter("sa_dispatchid", sa_dispatchid);
  168. Rows recviceRows = dbConnect.runSqlQuery(sqlFactory2);
  169. row.put("recvice", recviceRows.isNotEmpty() ? recviceRows.get(0) : new Row());
  170. return getSucReturnObject().setData(row).toString();
  171. }
  172. @API(title = "查询发货单列表", apiversion = R.ID20221114135403.v1.class)
  173. @CACHEING
  174. public String queryDispatchList() throws YosException {
  175. StringBuffer where = new StringBuffer(" 1=1 ");
  176. if (content.containsKey("where")) {
  177. JSONObject whereObject = content.getJSONObject("where");
  178. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  179. where.append(" and(");
  180. where.append("t1.billno like'%").append(whereObject.getString("condition")).append("%' ");
  181. where.append("or t3.sonum like'%").append(whereObject.getString("condition")).append("%' ");
  182. where.append("or t2.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  183. where.append("or t1.remarks like'%").append(whereObject.getString("condition")).append("%' ");
  184. where.append(")");
  185. }
  186. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  187. where.append(" and t1.status ='").append(whereObject.getString("status")).append("' ");
  188. }
  189. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  190. where.append(" and t1.billdate >='").append(whereObject.getString("begindate")).append("' ");
  191. }
  192. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  193. where.append(" and t1.billdate <='").append(whereObject.getString("enddate")).append("' ");
  194. }
  195. if (whereObject.containsKey("param") && !"".equals(whereObject.getString("param"))) {
  196. Rows rowsDispatchid = dbConnect.runSqlQuery("select t1.sa_dispatchid from sa_dispatch_items t1 left join sa_orderitems t2 on t1.sa_orderitemsid=t2.sa_orderitemsid and t1.siteid=t2.siteid " +
  197. "left join sa_order t3 on t2.sa_orderid=t3.sa_orderid and t2.siteid=t3.siteid left join plm_item t4 on t1.itemid=t4.itemid and t1.siteid=t4.siteid where t1.siteid='"+siteid+"' and (t4.itemno like '%"+whereObject.getString("param")+"%' or t3.sonum='"+whereObject.getString("param")+"')");
  198. if(rowsDispatchid.toJsonArray("sa_dispatchid").isEmpty()){
  199. where.append(" and t1.sa_dispatchid in (0)");
  200. }else {
  201. String str = rowsDispatchid.toJsonArray("sa_dispatchid").toJSONString();
  202. str = str.replace("[", "(").replace("]", ")");
  203. where.append(" and t1.sa_dispatchid in" ).append(str);
  204. }
  205. }
  206. }
  207. SQLFactory sqlFactory = new SQLFactory(this, "发货单列表查询", pageSize, pageNumber, pageSorting);
  208. sqlFactory.addParameter("siteid", siteid);
  209. sqlFactory.addParameter_SQL("where", where);
  210. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  211. ArrayList<Long> ids = rows.toArrayList("sa_dispatchid", new ArrayList<>());
  212. //查询数量金额
  213. RowsMap dispatchAmountRowsMap = Dispatch.getDispatchAmountAndQty(this, ids);
  214. for (Row row : rows) {
  215. Long id = row.getLong("sa_dispatchid");
  216. if (dispatchAmountRowsMap.get(id.toString()).isNotEmpty()) {
  217. row.put("sumamount", dispatchAmountRowsMap.get(id.toString()).get(0).getBigDecimal("sumamount").toPlainString());
  218. row.put("sumqty", dispatchAmountRowsMap.get(id.toString()).get(0).getBigDecimal("sumqty").toPlainString());
  219. } else {
  220. row.put("sumamount", 0);
  221. row.put("sumqty", 0);
  222. }
  223. }
  224. return getSucReturnObject().setData(rows).toString();
  225. }
  226. @API(title = "经销商列表查询(管理端)", apiversion = R.ID20230324125803.v1.class)
  227. public String query_agentList() throws YosException {
  228. /*
  229. * 过滤条件设置
  230. */
  231. StringBuffer where = new StringBuffer(" 1=1 ");
  232. if (content.containsKey("where")) {
  233. JSONObject whereObject = content.getJSONObject("where");
  234. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  235. where.append(" and(");
  236. where.append("t4.agentnum like'%").append(whereObject.getString("condition")).append("%' ");
  237. where.append("or t3.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  238. where.append(")");
  239. }
  240. }
  241. SQLFactory sqlFactory = new SQLFactory(this, "经销商合作企业查询", pageSize, pageNumber, pageSorting);
  242. sqlFactory.addParameter_SQL("where", where);
  243. sqlFactory.addParameter("siteid", siteid);
  244. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  245. return getSucReturnObject().setData(rows).toString();
  246. }
  247. @API(title = "删除", apiversion = R.ID20221114135503.v1.class)
  248. @CACHEING_CLEAN(apiversions = {R.ID20221114135303.v1.class, R.ID20221114135403.v1.class, R.ID20230320160203.v1.class})
  249. public String delete() throws YosException {
  250. JSONArray sa_dispatchids = content.getJSONArray("sa_dispatchids");
  251. BatchDeleteErr batchDeleteErr = BatchDeleteErr.create(this, sa_dispatchids.size());
  252. for (Object o : sa_dispatchids) {
  253. long sa_dispatchid = Long.parseLong(o.toString());
  254. Rows RowsStatus = dbConnect.runSqlQuery("select sa_dispatchid,status from sa_dispatch where siteid='"
  255. + siteid + "' and sa_dispatchid='" + sa_dispatchid + "'");
  256. Rows logisticsRows = dbConnect.runSqlQuery("select * from sa_logistics_items where siteid='"
  257. + siteid + "' and sa_dispatchid='" + sa_dispatchid + "'");
  258. if (RowsStatus.isNotEmpty()) {
  259. if (!RowsStatus.get(0).getString("status").equals("新建")) {
  260. batchDeleteErr.addErr(sa_dispatchid, "非新建状态的发货单无法删除");
  261. continue;
  262. }
  263. }
  264. if(logisticsRows.isNotEmpty()) {
  265. batchDeleteErr.addErr(sa_dispatchid, "存在发货物流明细信息,无法删除");
  266. continue;
  267. }
  268. dbConnect.runSqlUpdate(
  269. "delete from sa_dispatch where siteid='" + siteid + "' and sa_dispatchid=" + sa_dispatchid);
  270. }
  271. return batchDeleteErr.getReturnObject().toString();
  272. }
  273. @API(title = "审核", apiversion = R.ID20221114135603.v1.class)
  274. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, dispatch.class, dispatchItems.class, R.ID20230320160203.v1.class})
  275. public String check() throws YosException {
  276. Long sa_dispatchid = content.getLong("sa_dispatchid");
  277. Long sa_orderid = 0L;
  278. Rows rows = dbConnect.runSqlQuery("select sa_dispatchid,status,billno,sa_orderid from sa_dispatch where sa_dispatchid ='"
  279. + sa_dispatchid + "' and siteid='" + siteid + "'");
  280. if (rows.isEmpty()) {
  281. return getErrReturnObject().setErrMsg("该发货单不存在")
  282. .toString();
  283. } else {
  284. sa_orderid = rows.get(0).getLong("sa_orderid");
  285. if (!rows.get(0).getString("status").equals("新建")) {
  286. return getErrReturnObject().setErrMsg("单号为:【" + rows.get(0).getString("billno") + "】的发货单为非新建状态,无法审核")
  287. .toString();
  288. }
  289. }
  290. ArrayList<String> sqlList = new ArrayList<>();
  291. 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 sa_dispatch_items where sa_dispatchid="+ sa_dispatchid+")" );
  292. RowsMap rowsMap = rowsOrderDetail.toRowsMap("sa_orderitemsid");
  293. Rows rowsDispatchDetail = dbConnect.runSqlQuery("select t1.qty,t1.sa_orderitemsid,t1.rowno,t2.itemname from sa_dispatch_items t1 left join plm_item t2 on t1.itemid=t2.itemid and t1.siteid=t2.siteid where t1.sa_dispatchid=" + sa_dispatchid);
  294. if (!rowsDispatchDetail.isEmpty()) {
  295. for (Row row : rowsDispatchDetail) {
  296. if (rowsMap.get(row.getString("sa_orderitemsid")).isEmpty()) {
  297. return getErrReturnObject().setErrMsg("行号为:【" + row.getString("rowno") + "】的发货单明细不存在对应得订单行,无法审核")
  298. .toString();
  299. }
  300. if (row.getBigDecimal("qty").compareTo(rowsMap.get(row.getString("sa_orderitemsid")).get(0).getBigDecimal("undeliqty")) == 1) {
  301. return getErrReturnObject().setErrMsg("行号为:【" + row.getString("rowno") + "】的发货单明细的数量大于对应订单行的未发货数量,无法审核")
  302. .toString();
  303. }
  304. sqlList.add("update sa_orderitems set undeliqty=" + rowsMap.get(row.getLong("sa_orderitemsid")).get(0).getBigDecimal("undeliqty").subtract(row.getBigDecimal("qty")) + ",deliedqty=" + (row.getBigDecimal("qty").add(rowsMap.get(row.getLong("sa_orderitemsid")).get(0).getBigDecimal("deliedqty"))) + " where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  305. }
  306. }
  307. SQLFactory sqlFactoryupdate = new SQLFactory(this, "发货单审核");
  308. sqlFactoryupdate.addParameter("siteid", siteid);
  309. sqlFactoryupdate.addParameter("sa_dispatchid", sa_dispatchid);
  310. sqlFactoryupdate.addParameter("checkby", username);
  311. sqlList.add(sqlFactoryupdate.getSQL());
  312. sqlList.add(DataContrlLog.createLog(this, "sa_dispatch", sa_dispatchid, "审核", "发货单审核成功").getSQL());
  313. dbConnect.runSqlUpdate(sqlList);
  314. Executor.sendEml(this, "dispatchrecheck", sa_dispatchid,new ArrayList<>());
  315. return getSucReturnObject().toString();
  316. }
  317. @API(title = "反审核", apiversion = R.ID20221114135703.v1.class)
  318. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, dispatch.class, dispatchItems.class, R.ID20230320160203.v1.class})
  319. public String uncheck() throws YosException {
  320. Long sa_dispatchid = content.getLong("sa_dispatchid");
  321. Long sa_orderid = 0L;
  322. Rows rows = dbConnect.runSqlQuery("select sa_dispatchid,status,billno,sa_orderid from sa_dispatch where sa_dispatchid ='"
  323. + sa_dispatchid + "' and siteid='" + siteid + "'");
  324. if (rows.isEmpty()) {
  325. return getErrReturnObject().setErrMsg("该发货单不存在")
  326. .toString();
  327. } else {
  328. sa_orderid = rows.get(0).getLong("sa_orderid");
  329. if (!rows.get(0).getString("status").equals("审核")) {
  330. return getErrReturnObject().setErrMsg("单号为:【" + rows.get(0).getString("billno") + "】的发货单为非审核状态,无法反审核")
  331. .toString();
  332. }
  333. }
  334. ArrayList<String> sqlList = new ArrayList<>();
  335. 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 sa_dispatch_items where sa_dispatchid="+ sa_dispatchid+")" );
  336. RowsMap rowsMap = rowsOrderDetail.toRowsMap("sa_orderitemsid");
  337. Rows rowsDispatchDetail = dbConnect.runSqlQuery("select t1.qty,t1.sa_orderitemsid,t1.rowno,t2.itemname from sa_dispatch_items t1 left join plm_item t2 on t1.itemid=t2.itemid and t1.siteid=t2.siteid where t1.sa_dispatchid=" + sa_dispatchid);
  338. if (!rowsDispatchDetail.isEmpty()) {
  339. for (Row row : rowsDispatchDetail) {
  340. // if(rowsMap.get(row.getLong("sa_orderitemsid")).isEmpty()) {
  341. // return getErrReturnObject().setErrMsg("行号为:【" + row.getString("rowno") + "】的发货单明细不存在对应得订单行,无法审核")
  342. // .toString();
  343. // }
  344. // if(row.getBigDecimal("qty").compareTo(rowsMap.get(row.getLong("sa_orderitemsid")).get(0).getBigDecimal("undeliqty"))==1) {
  345. // return getErrReturnObject().setErrMsg("行号为:【" + row.getString("rowno") + "】的发货单明细的数量大于对应订单行的未发货数量,无法审核")
  346. // .toString();
  347. // }
  348. 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"));
  349. }
  350. }
  351. SQLFactory sqlFactoryupdate = new SQLFactory(this, "发货单反审核");
  352. sqlFactoryupdate.addParameter("siteid", siteid);
  353. sqlFactoryupdate.addParameter("sa_dispatchid", sa_dispatchid);
  354. sqlFactoryupdate.addParameter("checkby", username);
  355. sqlList.add(sqlFactoryupdate.getSQL());
  356. sqlList.add(DataContrlLog.createLog(this, "sa_dispatch", sa_dispatchid, "反审核", "发货单反审核成功").getSQL());
  357. dbConnect.runSqlUpdate(sqlList);
  358. return getSucReturnObject().toString();
  359. }
  360. @API(title = "手工关闭", apiversion = R.ID20230404134703.v1.class)
  361. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, dispatch.class, dispatchItems.class, R.ID20230320160203.v1.class})
  362. public String unclose() throws YosException {
  363. Long sa_dispatchid = content.getLong("sa_dispatchid");
  364. Long sa_orderid = 0L;
  365. Rows rows = dbConnect.runSqlQuery("select sa_dispatchid,status,billno,sa_orderid from sa_dispatch where sa_dispatchid ='"
  366. + sa_dispatchid + "' and siteid='" + siteid + "'");
  367. if (rows.isEmpty()) {
  368. return getErrReturnObject().setErrMsg("该发货单不存在")
  369. .toString();
  370. } else {
  371. sa_orderid = rows.get(0).getLong("sa_orderid");
  372. if (!rows.get(0).getString("status").equals("复核")) {
  373. return getErrReturnObject().setErrMsg("单号为:【" + rows.get(0).getString("billno") + "】的发货单为非复核状态,无法手工关闭")
  374. .toString();
  375. }
  376. }
  377. //对接erp生成erp发货单
  378. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true")) {
  379. ERPDocking erpDocking =new ERPDocking();
  380. String result =erpDocking.closeErpSainvoice(rows.get(0).getString("billno"),true,this,sa_dispatchid);
  381. if(!result.equals("true")){
  382. return getErrReturnObject().setErrMsg(result).toString();
  383. }else {
  384. }
  385. }
  386. ArrayList<String> sqlList = new ArrayList<>();
  387. 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 sa_dispatch_items where sa_dispatchid="+ sa_dispatchid+")" );
  388. RowsMap rowsMap = rowsOrderDetail.toRowsMap("sa_orderitemsid");
  389. Rows rowsDispatchDetail = dbConnect.runSqlQuery("select t1.qty,t1.outwarehouseqty,t1.sa_orderitemsid,t1.rowno,t2.itemname from sa_dispatch_items t1 left join plm_item t2 on t1.itemid=t2.itemid and t1.siteid=t2.siteid where t1.sa_dispatchid=" + sa_dispatchid);
  390. if (!rowsDispatchDetail.isEmpty()) {
  391. for (Row row : rowsDispatchDetail) {
  392. // if(rowsMap.get(row.getLong("sa_orderitemsid")).isEmpty()) {
  393. // return getErrReturnObject().setErrMsg("行号为:【" + row.getString("rowno") + "】的发货单明细不存在对应得订单行,无法审核")
  394. // .toString();
  395. // }
  396. // if(row.getBigDecimal("qty").compareTo(rowsMap.get(row.getLong("sa_orderitemsid")).get(0).getBigDecimal("undeliqty"))==1) {
  397. // return getErrReturnObject().setErrMsg("行号为:【" + row.getString("rowno") + "】的发货单明细的数量大于对应订单行的未发货数量,无法审核")
  398. // .toString();
  399. // }
  400. sqlList.add("update sa_orderitems set undeliqty=" + rowsMap.get(row.getLong("sa_orderitemsid")).get(0).getBigDecimal("undeliqty").add(row.getBigDecimal("qty").subtract(row.getBigDecimal("outwarehouseqty"))) + ",deliedqty=" + rowsMap.get(row.getLong("sa_orderitemsid")).get(0).getBigDecimal("deliedqty").subtract(row.getBigDecimal("qty").subtract(row.getBigDecimal("outwarehouseqty"))) + " where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  401. }
  402. }
  403. Rows orderRows =dbConnect.runSqlQuery("select distinct t1.sa_orderid from sa_orderitems t1 inner join sa_dispatch_items t2 on t1.siteid=t2.siteid and t1.sa_orderitemsid=t2.sa_orderitemsid where t1.siteid='" + siteid + "' and t2.sa_dispatchid=" + sa_dispatchid);
  404. for (Row row:orderRows) {
  405. dbConnect.runSqlUpdate("update sa_order set status='审核' where sa_orderid=" + row.getLong("sa_orderid") + " and siteid='" + siteid + "'");
  406. }
  407. SQLFactory sqlFactoryupdate = new SQLFactory(this, "发货单关闭");
  408. sqlFactoryupdate.addParameter("siteid", siteid);
  409. sqlFactoryupdate.addParameter("sa_dispatchid", sa_dispatchid);
  410. sqlFactoryupdate.addParameter("closeby", username);
  411. sqlList.add(sqlFactoryupdate.getSQL());
  412. sqlList.add(DataContrlLog.createLog(this, "sa_dispatch", sa_dispatchid, "手工关闭", "发货单手工关闭成功").getSQL());
  413. dbConnect.runSqlUpdate(sqlList);
  414. return getSucReturnObject().toString();
  415. }
  416. @API(title = "自动关闭(erp调用)", apiversion = R.ID20230413110103.v1.class,accesstoken = false)
  417. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, dispatch.class, dispatchItems.class, R.ID20230320160203.v1.class})
  418. public String autoClose() throws YosException {
  419. String finvonum = content.getString("finvonum");
  420. Rows rows = dbConnect.runSqlQuery("select * from sa_dispatch where status='复核' and siteid='ZZYOS' and billno='"+finvonum+"'");
  421. Rows rowsdetail = dbConnect.runSqlQuery("select t1.* from sa_dispatch_items t1 inner join sa_dispatch t2 on t1.sa_dispatchid=t2.sa_dispatchid and t1.siteid=t2.siteid where t2.status='复核' and t1.siteid='ZZYOS'");
  422. RowsMap rowsMap = rowsdetail.toRowsMap("sa_dispatchid");
  423. ArrayList<String> sqlList = new ArrayList<>();
  424. ERPDocking erpDocking =new ERPDocking();
  425. for (Row row : rows) {
  426. String sa_dispatchid = row.getString("sa_dispatchid");
  427. String billno = row.getString("billno");
  428. String result =erpDocking.closeErpSainvoice(billno,false,this, row.getLong("sa_dispatchid"));
  429. if(result.equals("true")){
  430. sqlList.add("update sa_dispatch set status='关闭' where sa_dispatchid='"+sa_dispatchid+"' and siteid='ZZYOS'");
  431. SQLFactory sqlFactory = new SQLFactory(new DataContrlLog(), "数据操作日志新增");
  432. sqlFactory.addParameter("ownertable", "sa_dispatch");
  433. sqlFactory.addParameter("ownerid", sa_dispatchid);
  434. sqlFactory.addParameter("action", "自动关闭");
  435. sqlFactory.addParameter("remarks", "发货单自动关闭成功");
  436. sqlFactory.addParameter("actionuserid", "1");
  437. sqlFactory.addParameter("actionby", "admin");
  438. sqlFactory.addParameter("siteid", "ZZYOS");
  439. sqlList.add(sqlFactory.getSQL());
  440. }else {
  441. logger.info("发货单自动关闭错误:"+result);
  442. return getErrReturnObject().setErrMsg(result).toString();
  443. }
  444. JSONArray jsonArray = erpDocking.queryErpSainvoice(billno);
  445. if(!jsonArray.isEmpty()){
  446. for (Object object:jsonArray) {
  447. JSONObject jsonObject = (JSONObject)object;
  448. sqlList.add("update sa_dispatch_items set outwarehouseqty="+jsonObject.getBigDecimalValue("fqty1")+" where rowno ="+jsonObject.getIntValue("frownum")+" and sa_dispatchid='"+sa_dispatchid+"' and siteid='ZZYOS'");
  449. }
  450. }
  451. }
  452. dbConnect.runSqlUpdate(sqlList);
  453. return getSucReturnObject().toString();
  454. }
  455. @API(title = "复核反复核", apiversion = R.ID20221114135803.v1.class)
  456. @CACHEING_CLEAN(apiversions = {R.ID20221114135303.v1.class, R.ID20221114135403.v1.class, R.ID20230320160203.v1.class})
  457. public String recheck() throws YosException {
  458. Long sa_dispatchid = content.getLong("sa_dispatchid");
  459. boolean isrecheck = content.getBooleanValue("isrecheck");
  460. Rows rows = dbConnect.runSqlQuery("select t1.*,t3.agentnum,t2.enterprisename,t4.sonum,t5.name,t5.phonenumber,t5.address from sa_dispatch t1 left join sys_enterprise t2 on t1.sys_enterpriseid=t2.sys_enterpriseid and t1.siteid=t2.siteid left join sa_agents t3 on t1.sys_enterpriseid=t3.sys_enterpriseid and t1.siteid=t3.siteid left join sa_order t4 on t1.sa_orderid=t4.sa_orderid and t1.siteid=t4.siteid left join sys_enterprise_contacts t5 on t1.rec_contactsid=t5.contactsid and t1.siteid=t5.siteid where t1.sa_dispatchid ='"
  461. + sa_dispatchid + "' and t1.siteid='" + siteid + "'");
  462. Rows rowsdetail = dbConnect.runSqlQuery("select t3.sonum,t2.rowno sorowno,t4.itemno,t1.batchno,t1.rowno,t1.qty,t2.price,t1.remarks from sa_dispatch_items t1 left join sa_orderitems t2 on t1.sa_orderitemsid=t2.sa_orderitemsid and t1.siteid=t2.siteid left join sa_order t3 on t2.sa_orderid=t3.sa_orderid and t2.siteid=t3.siteid left join plm_item t4 on t1.itemid=t4.itemid and t1.siteid=t4.siteid where t1.sa_dispatchid ='"
  463. + sa_dispatchid + "' and t1.siteid='" + siteid + "'");
  464. for (Row row : rows) {
  465. if (isrecheck) {
  466. if (!row.getString("status").equals("审核")) {
  467. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("billno") + "】的发货单为非审核状态,无法复核")
  468. .toString();
  469. }
  470. } else {
  471. if (!row.getString("status").equals("复核")) {
  472. return getErrReturnObject().setErrMsg("单号为:【" + row.getString("billno") + "】的发货单为非复核状态,无法反复核")
  473. .toString();
  474. }
  475. }
  476. }
  477. ArrayList<String> sqlList = new ArrayList<>();
  478. SQLFactory sqlFactoryupdate;
  479. if (isrecheck) {
  480. sqlFactoryupdate = new SQLFactory(this, "发货单复核");
  481. Rows orderRows =dbConnect.runSqlQuery("select distinct t1.sa_orderid from sa_orderitems t1 inner join sa_dispatch_items t2 on t1.siteid=t2.siteid and t1.sa_orderitemsid=t2.sa_orderitemsid where t1.siteid='" + siteid + "' and t2.sa_dispatchid=" + sa_dispatchid);
  482. //对接erp生成erp发货单
  483. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true")) {
  484. ERPDocking erpDocking =new ERPDocking();
  485. String result =erpDocking.createErpSainvoice(rows.get(0),rowsdetail,this,sa_dispatchid);
  486. if(!result.equals("true")){
  487. System.out.println(result);
  488. return getErrReturnObject().setErrMsg(result).toString();
  489. }
  490. }
  491. //判断当前订单是否已发完货,如果发完,则生把订单变为关闭状态
  492. //总数量
  493. Rows totalRows = dbConnect.runSqlQuery("select count(1) count,sa_orderid from sa_orderitems where siteid='" + siteid + "' group by sa_orderid");
  494. RowsMap totalRowsMap=totalRows.toRowsMap("sa_orderid");
  495. //已发完数量
  496. Rows deliRows = dbConnect.runSqlQuery("select count(1) count,sa_orderid from sa_orderitems where undeliqty=0 and siteid='" + siteid + "' group by sa_orderid");
  497. RowsMap deliRowsMap=deliRows.toRowsMap("sa_orderid");
  498. for (Row row:orderRows) {
  499. if(totalRowsMap.containsKey(row.getString("sa_orderid")) && deliRowsMap.containsKey(row.getString("sa_orderid"))){
  500. if (totalRowsMap.get(row.getString("sa_orderid")).get(0).getLong("count") ==deliRowsMap.get(row.getString("sa_orderid")).get(0).getLong("count")) {
  501. dbConnect.runSqlUpdate("update sa_order set status='关闭' where sa_orderid=" + row.getLong("sa_orderid") + " and siteid='" + siteid + "'");
  502. }
  503. }
  504. }
  505. Executor.sendEml(this, "dispatchreccheck", sa_dispatchid,new ArrayList<>());
  506. sqlList.add(DataContrlLog.createLog(this, "sa_dispatch", sa_dispatchid, "复核", "发货单复核成功").getSQL());
  507. } else {
  508. sqlFactoryupdate = new SQLFactory(this, "发货单反复核");
  509. sqlList.add(DataContrlLog.createLog(this, "sa_dispatch", sa_dispatchid, "反复核", "发货单反复核成功").getSQL());
  510. }
  511. sqlFactoryupdate.addParameter("siteid", siteid);
  512. sqlFactoryupdate.addParameter("sa_dispatchid", sa_dispatchid);
  513. sqlFactoryupdate.addParameter("recheckby", username);
  514. sqlList.add(sqlFactoryupdate.getSQL());
  515. dbConnect.runSqlUpdate(sqlList);
  516. return getSucReturnObject().toString();
  517. }
  518. @API(title = "一键生成发货单", apiversion = R.ID20230220100604.v1.class)
  519. @CACHEING_CLEAN(apiversions = {R.ID20221114135303.v1.class, R.ID20221114135403.v1.class, R.ID20230320160203.v1.class})
  520. public String generatedispatch() throws YosException {
  521. Long sa_orderid = content.getLong("sa_orderid");
  522. JSONArray sa_orderitemsids = content.getJSONArray("sa_orderitemsids");
  523. Rows orderRows = dbConnect.runSqlQuery("select freightstatus,sys_enterpriseid,rec_contactsid,status,remarks from sa_order where siteid='" + siteid + "' and sa_orderid=" + sa_orderid);
  524. if (orderRows.isEmpty()) {
  525. return getErrReturnObject().setErrMsg("订单无效").toString();
  526. }
  527. if (!orderRows.get(0).getString("status").equals("审核") && !orderRows.get(0).getString("status").equals("关闭")) {
  528. return getErrReturnObject().setErrMsg("非审核后订单无法生成发货单").toString();
  529. }
  530. ArrayList<String> sqlList = new ArrayList<>();
  531. long sa_dispatchid = createTableID("sa_dispatch");
  532. String billCode = createBillCode("dispatchbill");
  533. SQLFactory sqlFactory = new SQLFactory(this, "发货单新增");
  534. sqlFactory.addParameter("siteid", siteid);
  535. sqlFactory.addParameter("userid", userid);
  536. sqlFactory.addParameter("username", username);
  537. sqlFactory.addParameter("sa_dispatchid", sa_dispatchid);
  538. sqlFactory.addParameter("billno", billCode);
  539. sqlFactory.addParameter("sys_enterpriseid", orderRows.get(0).getLong("sys_enterpriseid"));
  540. sqlFactory.addParameter("rec_contactsid", orderRows.get(0).getLong("rec_contactsid"));
  541. //sqlFactory.addParameter("sa_logiscompid", orderRows.get(0).getLong("sa_logiscompid"));
  542. sqlFactory.addParameter("sa_orderid", sa_orderid);
  543. sqlFactory.addParameter("remarks", orderRows.get(0).getString("remarks"));
  544. sqlFactory.addParameter("freightstatus", orderRows.get(0).getString("freightstatus"));
  545. sqlFactory.addParameter("billdate", now());
  546. sqlList.add(sqlFactory.getSQL());
  547. RowsMap orderitemsRowsMap = dbConnect.runSqlQuery("select itemid,itemno,qty,sa_orderitemsid,sa_orderid,remarks from sa_orderitems where siteid='" + siteid + "' and sa_orderid=" + sa_orderid).toRowsMap("sa_orderitemsid");
  548. RowsMap dispatchitemsRowsMap;
  549. if (dbConnect.runSqlQuery("select 1 from sys_site_parameter where siteid='" + siteid + "' and dispatch_recheck=1").isEmpty()) {
  550. dispatchitemsRowsMap = dbConnect.runSqlQuery("select sa_orderid,sa_orderitemsid,ifnull(sum(qty),0) qty from sa_dispatch t1 inner join sa_dispatch_items t2 on t1.siteid=t2.siteid and t1.sa_dispatchid=t2.sa_dispatchid where t1.siteid='" + siteid + "' and t1.status in ('审核','关闭') group by t1.sa_orderid,t2.sa_orderitemsid").toRowsMap("sa_orderitemsid");
  551. } else {
  552. dispatchitemsRowsMap = dbConnect.runSqlQuery("select sa_orderid,sa_orderitemsid,ifnull(sum(qty),0) qty from sa_dispatch t1 inner join sa_dispatch_items t2 on t1.siteid=t2.siteid and t1.sa_dispatchid=t2.sa_dispatchid where t1.siteid='" + siteid + "' and t1.status in ('复核','关闭') group by t1.sa_orderid,t2.sa_orderitemsid").toRowsMap("sa_orderitemsid");
  553. }
  554. Long rowno = 1L;
  555. for (Object o : sa_orderitemsids) {
  556. String sa_orderitemsid = o.toString();
  557. BigDecimal qty = new BigDecimal("0");
  558. if (orderitemsRowsMap.containsKey(sa_orderitemsid)) {
  559. Rows orderitemsRows = orderitemsRowsMap.get(sa_orderitemsid);
  560. qty = orderitemsRows.get(0).getBigDecimal("qty");
  561. if (dispatchitemsRowsMap.containsKey(sa_orderitemsid)) {
  562. RowsMap dispatchRowsMap = dispatchitemsRowsMap.get(sa_orderitemsid).toRowsMap("sa_orderid");
  563. if (dispatchRowsMap.containsKey(sa_orderid.toString())) {
  564. Rows orderitemRows = dispatchRowsMap.get(sa_orderid.toString());
  565. if (orderitemsRows.get(0).getBigDecimal("qty").compareTo(orderitemRows.get(0).getBigDecimal("qty")) < 1) {
  566. continue;
  567. } else {
  568. qty = qty.subtract(orderitemRows.get(0).getBigDecimal("qty"));
  569. }
  570. }
  571. }
  572. sqlFactory = new SQLFactory(this, "发货单明细新增");
  573. sqlFactory.addParameter("siteid", siteid);
  574. sqlFactory.addParameter("sa_dispatch_itemsid", createTableID("sa_dispatch_items"));
  575. sqlFactory.addParameter("sa_dispatchid", sa_dispatchid);
  576. sqlFactory.addParameter("rowno", rowno);
  577. sqlFactory.addParameter("billno", billCode);
  578. rowno++;
  579. sqlFactory.addParameter("sa_orderitemsid", sa_orderitemsid);
  580. sqlFactory.addParameter("itemid", orderitemsRows.get(0).getLong("itemid"));
  581. sqlFactory.addParameter("qty", qty);
  582. sqlFactory.addParameter("batchcontrol", 0);
  583. sqlFactory.addParameter("batchno", "");
  584. sqlFactory.addParameter("remarks", orderRows.get(0).getString("remarks"));
  585. sqlFactory.addParameter("userid", userid);
  586. sqlFactory.addParameter("username", username);
  587. sqlList.add(sqlFactory.getSQL());
  588. }
  589. }
  590. dbConnect.runSqlUpdate(sqlList);
  591. content.put("sa_dispatchid", sa_dispatchid);
  592. return queryDispatchMain();
  593. }
  594. @API(title = "查询erp对接记录", apiversion = R.ID20230228133403.v1.class)
  595. @CACHEING
  596. public String queryErpupdatelog() throws YosException {
  597. Long sa_dispatchid = content.getLong("sa_dispatchid");
  598. SQLFactory sqlFactory = new SQLFactory(this, "发货单erp对接记录", pageSize, pageNumber, pageSorting);
  599. sqlFactory.addParameter("siteid", siteid);
  600. sqlFactory.addParameter("sa_dispatchid", sa_dispatchid);
  601. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  602. return getSucReturnObject().setData(rows).toString();
  603. }
  604. }