dispatch.java 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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 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.annotation.CACHEING;
  13. import common.annotation.CACHEING_CLEAN;
  14. import common.annotation.QUERYBYROWINDEX;
  15. import common.data.*;
  16. import org.apache.commons.lang.StringUtils;
  17. import org.apache.log4j.Logger;
  18. import restcontroller.R;
  19. import restcontroller.sale.cashbill.cashbill;
  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, intervaltime = 200)
  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 t7.agentnum like'%").append(whereObject.getString("condition")).append("%' ");
  184. where.append("or t1.remarks like'%").append(whereObject.getString("condition")).append("%' ");
  185. where.append(")");
  186. }
  187. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  188. where.append(" and t1.status ='").append(whereObject.getString("status")).append("' ");
  189. }
  190. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  191. where.append(" and DATE_FORMAT(t1.billdate, '%Y-%m-%d') >='").append(whereObject.getString("begindate")).append("' ");
  192. }
  193. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  194. where.append(" and DATE_FORMAT(t1.billdate, '%Y-%m-%d') <='").append(whereObject.getString("enddate")).append("' ");
  195. }
  196. if (whereObject.containsKey("remarks") && !"".equals(whereObject.getString("remarks"))) {
  197. where.append("and t1.remarks like'%").append(whereObject.getString("remarks")).append("%' ");
  198. }
  199. if (whereObject.containsKey("agentinfo") && !"".equals(whereObject.getString("agentinfo"))) {
  200. where.append(" and(");
  201. where.append(" t2.enterprisename like'%").append(whereObject.getString("agentinfo")).append("%' ");
  202. where.append("or t7.agentnum like'%").append(whereObject.getString("agentinfo")).append("%' ");
  203. where.append(")");
  204. }
  205. if (whereObject.containsKey("param") && !"".equals(whereObject.getString("param"))) {
  206. 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 " +
  207. "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.itemname like '%" + whereObject.getString("param") + "%' or t4.itemno like '%" + whereObject.getString("param") + "%' or t3.sonum='" + whereObject.getString("param") + "')");
  208. if (rowsDispatchid.toJsonArray("sa_dispatchid").isEmpty()) {
  209. where.append(" and t1.sa_dispatchid in (0)");
  210. } else {
  211. String str = rowsDispatchid.toJsonArray("sa_dispatchid").toJSONString();
  212. str = str.replace("[", "(").replace("]", ")");
  213. where.append(" and t1.sa_dispatchid in").append(str);
  214. }
  215. }
  216. }
  217. // SQLFactory sqlFactory = new SQLFactory(this, "发货单列表查询", pageSize, pageNumber, pageSorting);
  218. // sqlFactory.addParameter("siteid", siteid);
  219. // sqlFactory.addParameter_SQL("where", where);
  220. // Rows rows = dbConnect.runSqlQuery(sqlFactory);
  221. QuerySQL querySQL = queryDispatchList(where.toString());
  222. querySQL.setPage(pageSize, pageNumber);
  223. querySQL.setOrderBy(pageSorting);
  224. Rows rows = querySQL.query();
  225. ArrayList<Long> ids = rows.toArrayList("sa_dispatchid", new ArrayList<>());
  226. //查询数量金额
  227. RowsMap dispatchAmountRowsMap = Dispatch.getDispatchAmountAndQty(this, ids);
  228. for (Row row : rows) {
  229. Long id = row.getLong("sa_dispatchid");
  230. if (dispatchAmountRowsMap.get(id.toString()).isNotEmpty()) {
  231. row.put("sumamount", dispatchAmountRowsMap.get(id.toString()).get(0).getBigDecimal("sumamount"));
  232. row.put("sumqty", dispatchAmountRowsMap.get(id.toString()).get(0).getBigDecimal("sumqty"));
  233. } else {
  234. row.put("sumamount", 0);
  235. row.put("sumqty", 0);
  236. }
  237. }
  238. return getSucReturnObject().setData(rows).toString();
  239. }
  240. //查询发货单列表
  241. public QuerySQL queryDispatchList(String where) throws YosException {
  242. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_dispatch");
  243. querySQL.setTableAlias("t1");
  244. querySQL.addJoinTable(JOINTYPE.left, "sys_enterprise", "t2", "t1.sys_enterpriseid = t2.sys_enterpriseid AND t1.siteid = t2.siteid",
  245. "enterprisename", "abbreviation");
  246. querySQL.addQueryFields("enterprisephonenumber", "t2.phonenumber");
  247. querySQL.addJoinTable(JOINTYPE.left, "sa_order", "t3", "t1.sa_orderid = t3.sa_orderid AND t1.siteid = t3.siteid");
  248. querySQL.addQueryFields("projectname", "case when t3.type='项目订单' then t9.projectname else t3.projectnote end ");
  249. querySQL.addJoinTable(JOINTYPE.left, "sys_enterprise_contacts", "t5", "t1.rec_contactsid = t5.contactsid AND t1.siteid = t5.siteid",
  250. "province", "city", "county", "address");
  251. querySQL.addQueryFields("contactsphonenumber", "t5.phonenumber");
  252. querySQL.addJoinTable(JOINTYPE.left, "sa_agents", "t7", "t7.sys_enterpriseid = t1.sys_enterpriseid AND t1.siteid = t7.siteid",
  253. "agentnum");
  254. querySQL.addJoinTable(JOINTYPE.left, "sys_dataextend", "t8", "t8.ownertable = 'sa_dispatch' and t8.ownerid = t1.sa_dispatchid AND t1.siteid = t8.siteid",
  255. "erpbillno");
  256. querySQL.addJoinTable(JOINTYPE.left, "sa_project", "t9", "t9.sa_projectid = t3.sa_projectid AND t9.siteid = t3.siteid");
  257. querySQL.setWhere("t1.siteid", siteid);
  258. querySQL.setWhere(where);
  259. return querySQL;
  260. }
  261. @API(title = "经销商列表查询(管理端)", apiversion = R.ID20230324125803.v1.class)
  262. public String query_agentList() throws YosException {
  263. /*
  264. * 过滤条件设置
  265. */
  266. StringBuffer where = new StringBuffer(" 1=1 ");
  267. if (content.containsKey("where")) {
  268. JSONObject whereObject = content.getJSONObject("where");
  269. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  270. where.append(" and(");
  271. where.append("t4.agentnum like'%").append(whereObject.getString("condition")).append("%' ");
  272. where.append("or t3.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  273. where.append(")");
  274. }
  275. }
  276. SQLFactory sqlFactory = new SQLFactory(this, "经销商合作企业查询", pageSize, pageNumber, pageSorting);
  277. sqlFactory.addParameter_SQL("where", where);
  278. sqlFactory.addParameter("siteid", siteid);
  279. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL(false));
  280. return getSucReturnObject().setData(rows).toString();
  281. }
  282. @API(title = "删除", apiversion = R.ID20221114135503.v1.class)
  283. @CACHEING_CLEAN(apiversions = {R.ID20221114135303.v1.class, R.ID20221114135403.v1.class, R.ID20230320160203.v1.class})
  284. public String delete() throws YosException {
  285. JSONArray sa_dispatchids = content.getJSONArray("sa_dispatchids");
  286. BatchDeleteErr batchDeleteErr = BatchDeleteErr.create(this, sa_dispatchids.size());
  287. for (Object o : sa_dispatchids) {
  288. long sa_dispatchid = Long.parseLong(o.toString());
  289. Rows RowsStatus = dbConnect.runSqlQuery("select sa_dispatchid,status from sa_dispatch where siteid='"
  290. + siteid + "' and sa_dispatchid='" + sa_dispatchid + "'");
  291. Rows logisticsRows = dbConnect.runSqlQuery("select * from sa_logistics_items where siteid='"
  292. + siteid + "' and sa_dispatchid='" + sa_dispatchid + "'");
  293. if (RowsStatus.isNotEmpty()) {
  294. if (!RowsStatus.get(0).getString("status").equals("新建")) {
  295. batchDeleteErr.addErr(sa_dispatchid, "非新建状态的发货单无法删除");
  296. continue;
  297. }
  298. }
  299. if (logisticsRows.isNotEmpty()) {
  300. batchDeleteErr.addErr(sa_dispatchid, "存在发货物流明细信息,无法删除");
  301. continue;
  302. }
  303. dbConnect.runSqlUpdate(
  304. "delete from sa_dispatch where siteid='" + siteid + "' and sa_dispatchid=" + sa_dispatchid);
  305. }
  306. return batchDeleteErr.getReturnObject().toString();
  307. }
  308. // @API(title = "审核", apiversion = R.ID20221114135603.v1.class)
  309. // @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, dispatch.class, dispatchItems.class,cashbill.class})
  310. // public String check() throws YosException {
  311. // Long sa_dispatchid = content.getLong("sa_dispatchid");
  312. // Long sa_orderid = 0L;
  313. // Rows rows = dbConnect.runSqlQuery("select sa_dispatchid,status,billno,sa_orderid from sa_dispatch where sa_dispatchid ='"
  314. // + sa_dispatchid + "' and siteid='" + siteid + "'");
  315. // if (rows.isEmpty()) {
  316. // return getErrReturnObject().setErrMsg("该发货单不存在")
  317. // .toString();
  318. // } else {
  319. // sa_orderid = rows.get(0).getLong("sa_orderid");
  320. // if (!rows.get(0).getString("status").equals("新建")) {
  321. // return getErrReturnObject().setErrMsg("单号为:【" + rows.get(0).getString("billno") + "】的发货单为非新建状态,无法审核")
  322. // .toString();
  323. // }
  324. // }
  325. //
  326. // ArrayList<String> sqlList = new ArrayList<>();
  327. // 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+")" );
  328. // RowsMap rowsMap = rowsOrderDetail.toRowsMap("sa_orderitemsid");
  329. // 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);
  330. // if (!rowsDispatchDetail.isEmpty()) {
  331. // for (Row row : rowsDispatchDetail) {
  332. // if (rowsMap.get(row.getString("sa_orderitemsid")).isEmpty()) {
  333. // return getErrReturnObject().setErrMsg("行号为:【" + row.getString("rowno") + "】的发货单明细不存在对应得订单行,无法审核")
  334. // .toString();
  335. // }
  336. // if (row.getBigDecimal("qty").compareTo(rowsMap.get(row.getString("sa_orderitemsid")).get(0).getBigDecimal("undeliqty")) == 1) {
  337. // return getErrReturnObject().setErrMsg("行号为:【" + row.getString("rowno") + "】的发货单明细的数量大于对应订单行的未发货数量,无法审核")
  338. // .toString();
  339. // }
  340. // 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"));
  341. // }
  342. // }
  343. // SQLFactory sqlFactoryupdate = new SQLFactory(this, "发货单审核");
  344. // sqlFactoryupdate.addParameter("siteid", siteid);
  345. // sqlFactoryupdate.addParameter("sa_dispatchid", sa_dispatchid);
  346. // sqlFactoryupdate.addParameter("checkby", username);
  347. // sqlList.add(sqlFactoryupdate.getSQL());
  348. // sqlList.add(DataContrlLog.createLog(this, "sa_dispatch", sa_dispatchid, "审核", "发货单审核成功").getSQL());
  349. // dbConnect.runSqlUpdate(sqlList);
  350. //
  351. //
  352. // Executor.sendEml(this, "dispatchrecheck", sa_dispatchid,new ArrayList<>());
  353. // return getSucReturnObject().toString();
  354. //
  355. // }
  356. //
  357. //
  358. @API(title = "反审核", apiversion = R.ID20221114135703.v1.class)
  359. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, dispatch.class, dispatchItems.class, cashbill.class})
  360. public String uncheck() throws YosException {
  361. Long sa_dispatchid = content.getLong("sa_dispatchid");
  362. Long sa_orderid = 0L;
  363. Rows rows = dbConnect.runSqlQuery("select sa_dispatchid,status,billno,sa_orderid from sa_dispatch where sa_dispatchid ='"
  364. + sa_dispatchid + "' and siteid='" + siteid + "'");
  365. if (rows.isEmpty()) {
  366. return getErrReturnObject().setErrMsg("该发货单不存在")
  367. .toString();
  368. } else {
  369. sa_orderid = rows.get(0).getLong("sa_orderid");
  370. if (!rows.get(0).getString("status").equals("审核")) {
  371. return getErrReturnObject().setErrMsg("单号为:【" + rows.get(0).getString("billno") + "】的发货单为非审核状态,无法反审核")
  372. .toString();
  373. }
  374. }
  375. ArrayList<String> sqlList = new ArrayList<>();
  376. 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 + ")");
  377. RowsMap rowsMap = rowsOrderDetail.toRowsMap("sa_orderitemsid");
  378. 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);
  379. if (!rowsDispatchDetail.isEmpty()) {
  380. for (Row row : rowsDispatchDetail) {
  381. // if(rowsMap.get(row.getLong("sa_orderitemsid")).isEmpty()) {
  382. // return getErrReturnObject().setErrMsg("行号为:【" + row.getString("rowno") + "】的发货单明细不存在对应得订单行,无法审核")
  383. // .toString();
  384. // }
  385. // if(row.getBigDecimal("qty").compareTo(rowsMap.get(row.getLong("sa_orderitemsid")).get(0).getBigDecimal("undeliqty"))==1) {
  386. // return getErrReturnObject().setErrMsg("行号为:【" + row.getString("rowno") + "】的发货单明细的数量大于对应订单行的未发货数量,无法审核")
  387. // .toString();
  388. // }
  389. 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"));
  390. }
  391. }
  392. SQLFactory sqlFactoryupdate = new SQLFactory(this, "发货单反审核");
  393. sqlFactoryupdate.addParameter("siteid", siteid);
  394. sqlFactoryupdate.addParameter("sa_dispatchid", sa_dispatchid);
  395. sqlFactoryupdate.addParameter("checkby", username);
  396. sqlList.add(sqlFactoryupdate.getSQL());
  397. sqlList.add(DataContrlLog.createLog(this, "sa_dispatch", sa_dispatchid, "反审核", "发货单反审核成功").getSQL());
  398. dbConnect.runSqlUpdate(sqlList);
  399. return getSucReturnObject().toString();
  400. }
  401. @API(title = "手工关闭", apiversion = R.ID20230404134703.v1.class)
  402. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, dispatch.class, dispatchItems.class, cashbill.class})
  403. public String unclose() throws YosException {
  404. Long sa_dispatchid = content.getLong("sa_dispatchid");
  405. Long sa_orderid = 0L;
  406. Rows rows = dbConnect.runSqlQuery("select sa_dispatchid,status,billno,sa_orderid from sa_dispatch where sa_dispatchid ='"
  407. + sa_dispatchid + "' and siteid='" + siteid + "'");
  408. if (rows.isEmpty()) {
  409. return getErrReturnObject().setErrMsg("该发货单不存在")
  410. .toString();
  411. } else {
  412. sa_orderid = rows.get(0).getLong("sa_orderid");
  413. if (!rows.get(0).getString("status").equals("复核")) {
  414. return getErrReturnObject().setErrMsg("单号为:【" + rows.get(0).getString("billno") + "】的发货单为非复核状态,无法手工关闭")
  415. .toString();
  416. }
  417. }
  418. //对接erp生成erp发货单
  419. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true") && siteid.equalsIgnoreCase("ccyosg")) {
  420. ERPDocking erpDocking = new ERPDocking();
  421. String result = erpDocking.closeErpSainvoice(rows.get(0).getString("billno"), true, this, sa_dispatchid);
  422. if (!result.equals("true")) {
  423. return getErrReturnObject().setErrMsg(result).toString();
  424. } else {
  425. JSONArray jsonArray = erpDocking.queryErpSainvoice(rows.get(0).getString("billno"));
  426. ArrayList<String> dispatchsqlList = new ArrayList<>();
  427. if (!jsonArray.isEmpty()) {
  428. for (Object object1 : jsonArray) {
  429. JSONObject jsonObject = (JSONObject) object1;
  430. dispatchsqlList.add("update sa_dispatch_items set outwarehouseqty=" + jsonObject.getBigDecimalValue("fqty1") + " where rowno =" + jsonObject.getIntValue("frownum") + " and sa_dispatchid='" + sa_dispatchid + "' and siteid='" + siteid + "'");
  431. }
  432. }
  433. dbConnect.runSqlUpdate(dispatchsqlList);
  434. }
  435. }
  436. ArrayList<String> sqlList = new ArrayList<>();
  437. 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 + ")");
  438. RowsMap rowsMap = rowsOrderDetail.toRowsMap("sa_orderitemsid");
  439. 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);
  440. if (!rowsDispatchDetail.isEmpty()) {
  441. for (Row row : rowsDispatchDetail) {
  442. // if(rowsMap.get(row.getLong("sa_orderitemsid")).isEmpty()) {
  443. // return getErrReturnObject().setErrMsg("行号为:【" + row.getString("rowno") + "】的发货单明细不存在对应得订单行,无法审核")
  444. // .toString();
  445. // }
  446. // if(row.getBigDecimal("qty").compareTo(rowsMap.get(row.getLong("sa_orderitemsid")).get(0).getBigDecimal("undeliqty"))==1) {
  447. // return getErrReturnObject().setErrMsg("行号为:【" + row.getString("rowno") + "】的发货单明细的数量大于对应订单行的未发货数量,无法审核")
  448. // .toString();
  449. // }
  450. 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"));
  451. }
  452. }
  453. 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);
  454. for (Row row : orderRows) {
  455. sqlList.add("update sa_order set status='审核' where sa_orderid=" + row.getLong("sa_orderid") + " and siteid='" + siteid + "'");
  456. }
  457. SQLFactory sqlFactoryupdate = new SQLFactory(this, "发货单关闭");
  458. sqlFactoryupdate.addParameter("siteid", siteid);
  459. sqlFactoryupdate.addParameter("sa_dispatchid", sa_dispatchid);
  460. sqlFactoryupdate.addParameter("closeby", username);
  461. sqlList.add(sqlFactoryupdate.getSQL());
  462. sqlList.add(DataContrlLog.createLog(this, "sa_dispatch", sa_dispatchid, "手工关闭", "发货单手工关闭成功").getSQL());
  463. dbConnect.runSqlUpdate(sqlList);
  464. return getSucReturnObject().toString();
  465. }
  466. @API(title = "行关闭", apiversion = R.ID20230522093703.v1.class)
  467. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, dispatch.class, dispatchItems.class, cashbill.class})
  468. public String closeRows() throws YosException {
  469. JSONArray sa_dispatch_itemsids = content.getJSONArray("sa_dispatch_itemsids");
  470. BatchDeleteErr batchDeleteErr = BatchDeleteErr.create(this, sa_dispatch_itemsids.size());
  471. String sql = "";
  472. if (sa_dispatch_itemsids.size() > 0) {
  473. sql = "select t1.sa_dispatch_itemsid,t1.sa_orderitemsid,t1.qty,t1.outwarehouseqty,t1.sa_dispatchid,t1.rowno,t3.itemno,t2.billno,t2.status,t1.isclose from sa_dispatch_items t1 left join sa_dispatch t2 on t1.sa_dispatchid=t2.sa_dispatchid and t1.siteid=t2.siteid left join plm_item t3 on t1.itemid=t3.itemid and t1.siteid=t3.siteid where t1.sa_dispatch_itemsid in" + sa_dispatch_itemsids.toJSONString() + " and t1.siteid='" + siteid + "'";
  474. sql = sql.replace("[", "(").replace("]", ")");
  475. } else {
  476. return getErrReturnObject().setErrMsg("请选择发货单行后,进行行关闭").toString();
  477. }
  478. Rows rows = dbConnect.runSqlQuery(sql);
  479. if (rows.isEmpty()) {
  480. return getErrReturnObject().setErrMsg("该发货单所选行不存在")
  481. .toString();
  482. } else {
  483. if (!rows.get(0).getString("status").equals("复核")) {
  484. return getErrReturnObject().setErrMsg("单号为:【" + rows.get(0).getString("billno") + "】的发货单为非复核状态,无法行关闭")
  485. .toString();
  486. }
  487. }
  488. ArrayList<String> sqlList = new ArrayList<>();
  489. String orderdetailSql = "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_dispatch_itemsid in" + sa_dispatch_itemsids.toJSONString() + " )";
  490. orderdetailSql = orderdetailSql.replace("[", "(").replace("]", ")");
  491. Rows rowsOrderDetail = dbConnect.runSqlQuery(orderdetailSql);
  492. RowsMap rowsMap = rowsOrderDetail.toRowsMap("sa_orderitemsid");
  493. for (Row row : rows) {
  494. //对接erp生成erp发货单
  495. if (row.getBoolean("isclose")) {
  496. batchDeleteErr.addErr(row.getLong("sa_dispatch_itemsid"), "第" + row.getString("rowno") + "行的发货单明细已关闭,请勿重复关闭");
  497. continue;
  498. }
  499. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true") && siteid.equalsIgnoreCase("ccyosg")) {
  500. ERPDocking erpDocking = new ERPDocking();
  501. String result = erpDocking.closeErpSainvoiceRow(row.getString("billno"), this, row.getLong("rowno"), row.getString("itemno"), row.getLong("sa_dispatchid"));
  502. if (!result.equals("true")) {
  503. batchDeleteErr.addErr(row.getLong("sa_dispatch_itemsid"), result);
  504. continue;
  505. } else {
  506. sqlList.add("update sa_dispatch_items set isclose=1 where sa_dispatch_itemsid=" + row.getLong("sa_dispatch_itemsid"));
  507. 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"));
  508. }
  509. }
  510. }
  511. dbConnect.runSqlUpdate(sqlList);
  512. Rows sumcountRows = dbConnect.runSqlQuery("select count(1) sumcount from sa_dispatch_items where sa_dispatchid=" + rows.get(0).getLong("sa_dispatchid") + " and siteid='" + siteid + "'");
  513. Rows closecountRows = dbConnect.runSqlQuery("select count(1) closecount from sa_dispatch_items where sa_dispatchid=" + rows.get(0).getLong("sa_dispatchid") + " and siteid='" + siteid + "' and isclose=1");
  514. if (sumcountRows.get(0).getLong("sumcount") == closecountRows.get(0).getLong("closecount")) {
  515. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true") && siteid.equalsIgnoreCase("ccyosg")) {
  516. ERPDocking erpDocking = new ERPDocking();
  517. String result = erpDocking.closeErpSainvoice(rows.get(0).getString("billno"), true, this, rows.get(0).getLong("sa_dispatchid"));
  518. if (!result.equals("true")) {
  519. return getErrReturnObject().setErrMsg(result).toString();
  520. } else {
  521. dbConnect.runSqlUpdate("update sa_dispatch set status='手工关闭',closeby='" + username + "',closedate=CURRENT_TIMESTAMP where sa_dispatchid=" + rows.get(0).getLong("sa_dispatchid"));
  522. dbConnect.runSqlUpdate(DataContrlLog.createLog(this, "sa_dispatch", rows.get(0).getLong("sa_dispatchid"), "手工关闭", "发货单手工关闭成功").getSQL());
  523. }
  524. }
  525. }
  526. return batchDeleteErr.getReturnObject().toString();
  527. }
  528. @API(title = "自动关闭(erp调用)", apiversion = R.ID20230413110103.v1.class, accesstoken = false)
  529. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, dispatch.class, dispatchItems.class, cashbill.class})
  530. public String autoClose() throws YosException {
  531. String finvonum = content.getString("finvonum");
  532. Rows rows = dbConnect.runSqlQuery("select * from sa_dispatch where status='复核' and siteid='CCYOSG' and billno='" + finvonum + "'");
  533. 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='CCYOSG'");
  534. RowsMap rowsMap = rowsdetail.toRowsMap("sa_dispatchid");
  535. ArrayList<String> sqlList = new ArrayList<>();
  536. ERPDocking erpDocking = new ERPDocking();
  537. for (Row row : rows) {
  538. String sa_dispatchid = row.getString("sa_dispatchid");
  539. String billno = row.getString("billno");
  540. String result = erpDocking.closeErpSainvoice(billno, false, this, row.getLong("sa_dispatchid"));
  541. if (result.equals("true")) {
  542. sqlList.add("update sa_dispatch set status='关闭' where sa_dispatchid='" + sa_dispatchid + "' and siteid='CCYOSG'");
  543. SQLFactory sqlFactory = new SQLFactory(new DataContrlLog(), "数据操作日志新增");
  544. sqlFactory.addParameter("ownertable", "sa_dispatch");
  545. sqlFactory.addParameter("ownerid", sa_dispatchid);
  546. sqlFactory.addParameter("action", "自动关闭");
  547. sqlFactory.addParameter("remarks", "发货单自动关闭成功");
  548. sqlFactory.addParameter("actionuserid", "1");
  549. sqlFactory.addParameter("actionby", "admin");
  550. sqlFactory.addParameter("siteid", "CCYOSG");
  551. sqlList.add(sqlFactory.getSQL());
  552. } else {
  553. logger.info("发货单自动关闭错误:" + result);
  554. return getErrReturnObject().setErrMsg(result).toString();
  555. }
  556. JSONArray jsonArray = erpDocking.queryErpSainvoice(billno);
  557. if (!jsonArray.isEmpty()) {
  558. for (Object object : jsonArray) {
  559. JSONObject jsonObject = (JSONObject) object;
  560. sqlList.add("update sa_dispatch_items set outwarehouseqty=" + jsonObject.getBigDecimalValue("fqty1") + " where rowno =" + jsonObject.getIntValue("frownum") + " and sa_dispatchid='" + sa_dispatchid + "' and siteid='CCYOSG'");
  561. }
  562. }
  563. }
  564. dbConnect.runSqlUpdate(sqlList);
  565. return getSucReturnObject().toString();
  566. }
  567. @API(title = "复核", apiversion = R.ID20221114135803.v1.class)
  568. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, dispatch.class, dispatchItems.class, cashbill.class})
  569. public String recheck() throws YosException {
  570. Long sa_dispatchid = content.getLong("sa_dispatchid");
  571. Rows dispatchRows = dbConnect.runSqlQuery("select sa_dispatchid,status,billno,sa_orderid from sa_dispatch where sa_dispatchid ='"
  572. + sa_dispatchid + "' and siteid='" + siteid + "'");
  573. if (dispatchRows.isEmpty()) {
  574. return getErrReturnObject().setErrMsg("该发货单不存在")
  575. .toString();
  576. } else {
  577. if (!dispatchRows.get(0).getString("status").equals("新建")) {
  578. return getErrReturnObject().setErrMsg("单号为:【" + dispatchRows.get(0).getString("billno") + "】的发货单为非新建状态,无法复核")
  579. .toString();
  580. }
  581. }
  582. ArrayList<String> sqlList = new ArrayList<>();
  583. 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 + ")");
  584. RowsMap rowsMap = rowsOrderDetail.toRowsMap("sa_orderitemsid");
  585. 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);
  586. if (!rowsDispatchDetail.isEmpty()) {
  587. for (Row row : rowsDispatchDetail) {
  588. if (rowsMap.get(row.getString("sa_orderitemsid")).isEmpty()) {
  589. return getErrReturnObject().setErrMsg("行号为:【" + row.getString("rowno") + "】的发货单明细不存在对应得订单行,无法复核")
  590. .toString();
  591. }
  592. if (row.getBigDecimal("qty").compareTo(rowsMap.get(row.getString("sa_orderitemsid")).get(0).getBigDecimal("undeliqty")) == 1) {
  593. return getErrReturnObject().setErrMsg("行号为:【" + row.getString("rowno") + "】的发货单明细的数量大于对应订单行的未发货数量,无法复核")
  594. .toString();
  595. }
  596. 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"));
  597. }
  598. } else {
  599. return getErrReturnObject().setErrMsg("该发货单明细不存在,无法复核").toString();
  600. }
  601. 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 ='"
  602. + sa_dispatchid + "' and t1.siteid='" + siteid + "'");
  603. 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 ='"
  604. + sa_dispatchid + "' and t1.siteid='" + siteid + "'");
  605. SQLFactory sqlFactoryupdate = new SQLFactory(this, "发货单复核");
  606. 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);
  607. if (orderRows.size() == 1) {
  608. sqlList.add("update sa_dispatch set sa_orderid=" + orderRows.get(0).getLong("sa_orderid") + " where siteid='" + siteid + "' and sa_dispatchid=" + sa_dispatchid);
  609. }
  610. //对接erp生成erp发货单
  611. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true") && siteid.equalsIgnoreCase("ccyosg")) {
  612. ERPDocking erpDocking = new ERPDocking();
  613. String result = erpDocking.createErpSainvoice(rows.get(0), rowsdetail, this, sa_dispatchid);
  614. if (!result.equals("true")) {
  615. System.out.println(result);
  616. return getErrReturnObject().setErrMsg(result).toString();
  617. }
  618. }
  619. // //判断当前订单是否已发完货,如果发完,则生把订单变为关闭状态
  620. // //总数量
  621. // Rows totalRows = dbConnect.runSqlQuery("select count(1) count,sa_orderid from sa_orderitems where siteid='" + siteid + "' group by sa_orderid");
  622. // RowsMap totalRowsMap=totalRows.toRowsMap("sa_orderid");
  623. // //已发完数量
  624. // Rows deliRows = dbConnect.runSqlQuery("select count(1) count,sa_orderid from sa_orderitems where undeliqty=0 and siteid='" + siteid + "' group by sa_orderid");
  625. // RowsMap deliRowsMap=deliRows.toRowsMap("sa_orderid");
  626. // for (Row row:orderRows) {
  627. // if(totalRowsMap.containsKey(row.getString("sa_orderid")) && deliRowsMap.containsKey(row.getString("sa_orderid"))){
  628. // if (totalRowsMap.get(row.getString("sa_orderid")).get(0).getLong("count") ==deliRowsMap.get(row.getString("sa_orderid")).get(0).getLong("count")) {
  629. // dbConnect.runSqlUpdate("update sa_order set status='关闭' where sa_orderid=" + row.getLong("sa_orderid") + " and siteid='" + siteid + "'");
  630. // }
  631. // }
  632. // }
  633. Executor.sendEml(this, "dispatchreccheck", sa_dispatchid, new ArrayList<>());
  634. sqlList.add(DataContrlLog.createLog(this, "sa_dispatch", sa_dispatchid, "复核", "发货单复核成功").getSQL());
  635. sqlFactoryupdate.addParameter("siteid", siteid);
  636. sqlFactoryupdate.addParameter("sa_dispatchid", sa_dispatchid);
  637. sqlFactoryupdate.addParameter("recheckby", username);
  638. sqlList.add(sqlFactoryupdate.getSQL());
  639. dbConnect.runSqlUpdate(sqlList);
  640. Long sys_enterpriseid = rows.get(0).getLong("sys_enterpriseid");
  641. String message = "您的发货单" + rows.get(0).getString("billno") + "已复核,请及时查看!";
  642. sendMsg(message, sa_dispatchid, sys_enterpriseid);
  643. return getSucReturnObject().toString();
  644. }
  645. @API(title = "一键生成发货单", apiversion = R.ID20230220100604.v1.class)
  646. @CACHEING_CLEAN(apiversions = {R.ID20221114135303.v1.class, R.ID20221114135403.v1.class, R.ID20230320160203.v1.class})
  647. public String generatedispatch() throws YosException {
  648. Long sa_orderid = content.getLong("sa_orderid");
  649. JSONArray sa_orderitemsids = content.getJSONArray("sa_orderitemsids");
  650. Rows orderRows = dbConnect.runSqlQuery("select freightstatus,sys_enterpriseid,rec_contactsid,status,remarks from sa_order where siteid='" + siteid + "' and sa_orderid=" + sa_orderid);
  651. if (orderRows.isEmpty()) {
  652. return getErrReturnObject().setErrMsg("订单无效").toString();
  653. }
  654. if (!orderRows.get(0).getString("status").equals("审核") && !orderRows.get(0).getString("status").equals("关闭")) {
  655. return getErrReturnObject().setErrMsg("非审核后订单无法生成发货单").toString();
  656. }
  657. ArrayList<String> sqlList = new ArrayList<>();
  658. long sa_dispatchid = createTableID("sa_dispatch");
  659. String billCode = createBillCode("dispatchbill");
  660. SQLFactory sqlFactory = new SQLFactory(this, "发货单新增");
  661. sqlFactory.addParameter("siteid", siteid);
  662. sqlFactory.addParameter("userid", userid);
  663. sqlFactory.addParameter("username", username);
  664. sqlFactory.addParameter("sa_dispatchid", sa_dispatchid);
  665. sqlFactory.addParameter("billno", billCode);
  666. sqlFactory.addParameter("sys_enterpriseid", orderRows.get(0).getLong("sys_enterpriseid"));
  667. sqlFactory.addParameter("rec_contactsid", orderRows.get(0).getLong("rec_contactsid"));
  668. //sqlFactory.addParameter("sa_logiscompid", orderRows.get(0).getLong("sa_logiscompid"));
  669. sqlFactory.addParameter("sa_orderid", sa_orderid);
  670. sqlFactory.addParameter("remarks", orderRows.get(0).getString("remarks"));
  671. sqlFactory.addParameter("freightstatus", orderRows.get(0).getString("freightstatus"));
  672. sqlFactory.addParameter("billdate", now());
  673. sqlList.add(sqlFactory.getSQL());
  674. RowsMap orderitemsRowsMap = dbConnect.runSqlQuery("select itemid,itemno,qty,sa_orderitemsid,sa_orderid,remarks from sa_orderitems where isfreeze=0 and siteid='" + siteid + "' and sa_orderid=" + sa_orderid).toRowsMap("sa_orderitemsid");
  675. RowsMap dispatchitemsRowsMap;
  676. if (dbConnect.runSqlQuery("select 1 from sys_site_parameter where siteid='" + siteid + "' and dispatch_recheck=1").isEmpty()) {
  677. 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");
  678. } else {
  679. 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");
  680. }
  681. Long rowno = 1L;
  682. for (Object o : sa_orderitemsids) {
  683. String sa_orderitemsid = o.toString();
  684. BigDecimal qty = new BigDecimal("0");
  685. if (orderitemsRowsMap.containsKey(sa_orderitemsid)) {
  686. Rows orderitemsRows = orderitemsRowsMap.get(sa_orderitemsid);
  687. qty = orderitemsRows.get(0).getBigDecimal("qty");
  688. if (dispatchitemsRowsMap.containsKey(sa_orderitemsid)) {
  689. RowsMap dispatchRowsMap = dispatchitemsRowsMap.get(sa_orderitemsid).toRowsMap("sa_orderid");
  690. if (dispatchRowsMap.containsKey(sa_orderid.toString())) {
  691. Rows orderitemRows = dispatchRowsMap.get(sa_orderid.toString());
  692. if (orderitemsRows.get(0).getBigDecimal("qty").compareTo(orderitemRows.get(0).getBigDecimal("qty")) < 1) {
  693. continue;
  694. } else {
  695. qty = qty.subtract(orderitemRows.get(0).getBigDecimal("qty"));
  696. }
  697. }
  698. }
  699. sqlFactory = new SQLFactory(this, "发货单明细新增");
  700. sqlFactory.addParameter("siteid", siteid);
  701. sqlFactory.addParameter("sa_dispatch_itemsid", createTableID("sa_dispatch_items"));
  702. sqlFactory.addParameter("sa_dispatchid", sa_dispatchid);
  703. sqlFactory.addParameter("rowno", rowno);
  704. sqlFactory.addParameter("billno", billCode);
  705. rowno++;
  706. sqlFactory.addParameter("sa_orderitemsid", sa_orderitemsid);
  707. sqlFactory.addParameter("itemid", orderitemsRows.get(0).getLong("itemid"));
  708. sqlFactory.addParameter("qty", qty);
  709. sqlFactory.addParameter("batchcontrol", 0);
  710. sqlFactory.addParameter("batchno", "");
  711. sqlFactory.addParameter("remarks", orderRows.get(0).getString("remarks"));
  712. sqlFactory.addParameter("userid", userid);
  713. sqlFactory.addParameter("username", username);
  714. sqlList.add(sqlFactory.getSQL());
  715. }
  716. }
  717. dbConnect.runSqlUpdate(sqlList);
  718. content.put("sa_dispatchid", sa_dispatchid);
  719. return queryDispatchMain();
  720. }
  721. public void sendMsg(String content, Long sa_dispatchid, Long sys_enterpriseid) throws YosException {
  722. ArrayList<Long> userList = getEnterpriseHrs(sys_enterpriseid).toArrayList("userid", new ArrayList<>());
  723. Remind remind = new Remind(this);
  724. remind.setTitle("发货单消息");
  725. remind.setContent(content);
  726. remind.setType("应用");
  727. remind.setObjectid(sa_dispatchid);
  728. remind.setObjectname("sa_dispatch");
  729. remind.setTouserid(userList);
  730. remind.sendByDialogMsg().createSys_message();
  731. }
  732. @API(title = "查询erp对接记录", apiversion = R.ID20230228133403.v1.class)
  733. @CACHEING
  734. public String queryErpupdatelog() throws YosException {
  735. Long sa_dispatchid = content.getLong("sa_dispatchid");
  736. SQLFactory sqlFactory = new SQLFactory(this, "发货单erp对接记录", pageSize, pageNumber, pageSorting);
  737. sqlFactory.addParameter("siteid", siteid);
  738. sqlFactory.addParameter("sa_dispatchid", sa_dispatchid);
  739. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  740. return getSucReturnObject().setData(rows).toString();
  741. }
  742. }