dispatchItems.java 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. package restcontroller.webmanage.sale.dispatch;
  2. import beans.data.BatchDeleteErr;
  3. import beans.dispatch.Dispatch;
  4. import beans.parameter.Parameter;
  5. import beans.salearea.SaleArea;
  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.*;
  14. import org.apache.commons.lang.StringUtils;
  15. import org.apache.poi.ss.usermodel.CellStyle;
  16. import org.apache.poi.ss.usermodel.DataFormat;
  17. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  18. import org.apache.poi.xssf.usermodel.XSSFSheet;
  19. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  20. import restcontroller.R;
  21. import restcontroller.sale.promotion.promotion;
  22. import restcontroller.webmanage.sale.order.Order;
  23. import restcontroller.webmanage.sale.order.OrderItems;
  24. import utility.ERPDocking;
  25. import utility.tools.WebRequest;
  26. import java.io.IOException;
  27. import java.math.BigDecimal;
  28. import java.util.ArrayList;
  29. import java.util.HashMap;
  30. public class dispatchItems extends Controller {
  31. public dispatchItems(JSONObject arg0) throws YosException {
  32. super(arg0);
  33. // TODO Auto-generated constructor stub
  34. }
  35. @API(title = "审核订单列表查询", apiversion = R.ID20221114165903.v1.class)
  36. @CACHEING
  37. public String queryCheckOrderList() throws YosException {
  38. /*
  39. * 过滤条件设置
  40. */
  41. StringBuffer where = new StringBuffer(" 1=1 ");
  42. if (content.containsKey("where")) {
  43. JSONObject whereObject = content.getJSONObject("where");
  44. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  45. where.append(" and(");
  46. where.append("t1.sonum like'%").append(whereObject.getString("condition")).append("%' ");
  47. where.append("or t2.abbreviation like'%").append(whereObject.getString("condition")).append("%' ");
  48. where.append("or t2.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  49. where.append("or t1.remarks like'%").append(whereObject.getString("condition")).append("%' ");
  50. where.append(")");
  51. }
  52. if (whereObject.containsKey("sonum") && !"".equals(whereObject.getString("sonum"))) {
  53. where.append(" and t1.sonum ='").append(whereObject.getString("sonum")).append("' ");
  54. }
  55. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  56. where.append(" and DATE_FORMAT(t1.checkdate, '%Y-%m-%d') >='").append(whereObject.getString("begindate")).append("' ");
  57. }
  58. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  59. where.append(" and DATE_FORMAT(t1.checkdate, '%Y-%m-%d') <='").append(whereObject.getString("enddate")).append("' ");
  60. }
  61. }
  62. // String hrid = content.getString("hrid");
  63. SQLFactory sqlFactory = new SQLFactory(this, "审核订单列表查询", pageSize, pageNumber, pageSorting);
  64. sqlFactory.addParameter_SQL("where", where);
  65. sqlFactory.addParameter("sys_enterpriseid", content.getLong("sys_enterpriseid"));
  66. sqlFactory.addParameter("siteid", siteid);
  67. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL(false));
  68. return getSucReturnObject().setData(rows).toString();
  69. }
  70. @API(title = "发货商品列表查询", apiversion = R.ID20221114170003.v1.class)
  71. @CACHEING
  72. public String queryOrderItemgList() throws YosException {
  73. /*
  74. * 过滤条件设置
  75. */
  76. StringBuffer where = new StringBuffer(" 1=1 ");
  77. if (content.containsKey("where")) {
  78. JSONObject whereObject = content.getJSONObject("where");
  79. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  80. where.append(" and(");
  81. where.append("t1.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  82. where.append("or t1.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  83. where.append("or t1.unit like'%").append(whereObject.getString("condition")).append("%' ");
  84. where.append("or t3.sonum like'%").append(whereObject.getString("condition")).append("%' ");
  85. where.append("or t3.typemx like'%").append(whereObject.getString("condition")).append("%' ");
  86. where.append(")");
  87. }
  88. if (whereObject.containsKey("sa_orderid") && !"".equals(whereObject.getString("sa_orderid"))) {
  89. Long sa_orderid = whereObject.getLongValue("sa_orderid");
  90. if (sa_orderid > 0) {
  91. where.append(" and t1.sa_orderid ='").append(sa_orderid).append("' ");
  92. }
  93. }
  94. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  95. where.append(" and DATE_FORMAT(t3.checkdate, '%Y-%m-%d') >='").append(whereObject.getString("begindate")).append("' ");
  96. }
  97. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  98. where.append(" and DATE_FORMAT(t3.checkdate, '%Y-%m-%d') <='").append(whereObject.getString("enddate")).append("' ");
  99. }
  100. }
  101. SQLFactory sqlFactory = new SQLFactory(this, "发货商品列表查询", pageSize, pageNumber, pageSorting);
  102. sqlFactory.addParameter_SQL("where", where);
  103. sqlFactory.addParameter("sys_enterpriseid", content.getLongValue("sys_enterpriseid"));
  104. sqlFactory.addParameter("sa_dispatchid", content.getLongValue("sa_dispatchid"));
  105. sqlFactory.addParameter("siteid", siteid);
  106. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL(false));
  107. return getSucReturnObject().setData(rows).toString();
  108. }
  109. @API(title = "erp批次库存查询", apiversion = R.ID20230427101303.v1.class)
  110. @CACHEING
  111. public String queryErpIcinvbalBatchList() throws YosException {
  112. BigDecimal qty = content.getBigDecimalValue("qty");
  113. JSONArray jsonArray = content.getJSONArray("itemnos");
  114. JSONArray jsonArrayfinal = new JSONArray();
  115. ERPDocking erpDocking = new ERPDocking(siteid);
  116. JSONArray rows = erpDocking.getErpIcinvbalBatchRows(1000, 1, jsonArray);
  117. for (Object object : rows) {
  118. JSONObject jsonObject = (JSONObject) object;
  119. if (jsonObject.getBigDecimalValue("fqty").compareTo(qty) >= 0) {
  120. jsonArrayfinal.add(jsonObject);
  121. }
  122. }
  123. return getSucReturnObject().setData(jsonArrayfinal).toString();
  124. }
  125. @API(title = "新建或修改发货单商品明细", apiversion = R.ID20221115104603.v1.class, intervaltime = 200)
  126. @CACHEING_CLEAN(apiClass = {dispatchItems.class, dispatch.class})
  127. public String insertormodify_dispatchItems() throws YosException {
  128. Long sa_dispatchid = content.getLong("sa_dispatchid");
  129. JSONArray iteminfos = content.getJSONArray("iteminfos");
  130. ArrayList<String> sqlList = new ArrayList<>();
  131. Rows rowscount = dbConnect.runSqlQuery("select billno,status from sa_dispatch where sa_dispatchid=" + sa_dispatchid);
  132. if (!rowscount.isEmpty()) {
  133. if (!rowscount.get(0).getString("status").equals("新建")) {
  134. return getErrReturnObject().setErrMsg("非新建状态的发货单无法新增修改").toString();
  135. }
  136. }
  137. int i = 0;
  138. long maxid = 0;
  139. long[] sa_dispatch_itemsid = createTableID("sa_dispatch_items", iteminfos.size());
  140. Rows maxidRows = dbConnect
  141. .runSqlQuery("select MAX(rowno) rowno from sa_dispatch_items where sa_dispatchid=" + sa_dispatchid);
  142. if (!maxidRows.isEmpty()) {
  143. maxid = maxidRows.get(0).getLong("rowno");
  144. }
  145. for (Object obj : iteminfos) {
  146. JSONObject iteminfo = (JSONObject) obj;
  147. if (iteminfo.getLong("sa_dispatch_itemsid") <= 0 || dbConnect
  148. .runSqlQuery("select sa_dispatch_itemsid from sa_dispatch_items where sa_dispatch_itemsid="
  149. + iteminfo.getLong("sa_dispatch_itemsid"))
  150. .isEmpty()) {
  151. // Rows sa_dispatch_itemsrows = dbConnect.runSqlQuery("select sa_dispatch_itemsid,qty,remarks from sa_dispatch_items where sa_dispatchid="
  152. // + sa_dispatchid + " and sa_orderitemsid=" + iteminfo.getLong("sa_orderitemsid"));
  153. // if (!sa_dispatch_itemsrows.isEmpty()) {
  154. // SQLFactory saleFactory = new SQLFactory(this, "发货单明细更新");
  155. // saleFactory.addParameter("sa_dispatch_itemsid", sa_dispatch_itemsrows.get(0).getLong("sa_dispatch_itemsid"));
  156. // // saleFactory.addParameter("itemno", iteminfo.getString("itemno"));
  157. // saleFactory.addParameter("qty", iteminfo.getBigDecimal("qty").add(sa_dispatch_itemsrows.get(0).getBigDecimal("qty")));
  158. // if (!StringUtils.isBlank(iteminfo.getString("remarks"))) {
  159. // saleFactory.addParameter("remarks", iteminfo.getString("remarks"));
  160. // } else {
  161. // saleFactory.addParameter("remarks", sa_dispatch_itemsrows.get(0).getString("remarks"));
  162. // }
  163. //
  164. // saleFactory.addParameter("userid", userid);
  165. // saleFactory.addParameter("username", username);
  166. // saleFactory.addParameter("billno", rowscount.get(0).getString("billno"));
  167. // saleFactory.addParameter("batchno", iteminfo.getStringValue("batchno"));
  168. // sqlList.add(saleFactory.getSQL());
  169. // } else {
  170. // SQLFactory saleFactory = new SQLFactory(this, "发货单明细新增");
  171. // saleFactory.addParameter("siteid", siteid);
  172. // saleFactory.addParameter("rowno", maxid + i + 1);
  173. // saleFactory.addParameter("sa_dispatch_itemsid", sa_dispatch_itemsid[i]);
  174. // saleFactory.addParameter("sa_dispatchid", sa_dispatchid);
  175. // saleFactory.addParameter("sa_orderitemsid", iteminfo.getLong("sa_orderitemsid"));
  176. // saleFactory.addParameter("itemid", iteminfo.getString("itemid"));
  177. // saleFactory.addParameter("qty", iteminfo.getBigDecimal("qty"));
  178. // saleFactory.addParameter("batchcontrol", iteminfo.getBooleanValue("batchcontrol"));
  179. // saleFactory.addParameter("batchno", "");
  180. // saleFactory.addParameter("remarks", iteminfo.getString("remarks"));
  181. // saleFactory.addParameter("userid", userid);
  182. // saleFactory.addParameter("username", username);
  183. // saleFactory.addParameter("billno", rowscount.get(0).getString("billno"));
  184. // sqlList.add(saleFactory.getSQL());
  185. // }
  186. SQLFactory saleFactory = new SQLFactory(this, "发货单明细新增");
  187. saleFactory.addParameter("siteid", siteid);
  188. saleFactory.addParameter("rowno", maxid + i + 1);
  189. saleFactory.addParameter("sa_dispatch_itemsid", sa_dispatch_itemsid[i]);
  190. saleFactory.addParameter("sa_dispatchid", sa_dispatchid);
  191. saleFactory.addParameter("sa_orderitemsid", iteminfo.getLong("sa_orderitemsid"));
  192. saleFactory.addParameter("itemid", iteminfo.getString("itemid"));
  193. saleFactory.addParameter("qty", iteminfo.getBigDecimal("qty"));
  194. saleFactory.addParameter("batchcontrol", iteminfo.getBooleanValue("batchcontrol"));
  195. saleFactory.addParameter("batchno", "");
  196. saleFactory.addParameter("remarks", iteminfo.getString("remarks"));
  197. saleFactory.addParameter("userid", userid);
  198. saleFactory.addParameter("username", username);
  199. saleFactory.addParameter("billno", rowscount.get(0).getString("billno"));
  200. sqlList.add(saleFactory.getSQL());
  201. i++;
  202. } else {
  203. SQLFactory saleFactory = new SQLFactory(this, "发货单明细更新");
  204. saleFactory.addParameter("sa_dispatch_itemsid", iteminfo.getLong("sa_dispatch_itemsid"));
  205. // saleFactory.addParameter("itemno", iteminfo.getString("itemno"));
  206. saleFactory.addParameter("qty", iteminfo.getBigDecimal("qty"));
  207. saleFactory.addParameter("remarks", iteminfo.getString("remarks"));
  208. saleFactory.addParameter("batchno", iteminfo.getStringValue("batchno"));
  209. saleFactory.addParameter("userid", userid);
  210. saleFactory.addParameter("username", username);
  211. saleFactory.addParameter("billno", rowscount.get(0).getString("billno"));
  212. sqlList.add(saleFactory.getSQL());
  213. }
  214. }
  215. dbConnect.runSqlUpdate(sqlList);
  216. return queryDispatchItemsList();
  217. }
  218. @API(title = "发货单商品明细列表", apiversion = R.ID20221115104703.v1.class)
  219. @CACHEING
  220. public String queryDispatchItemsList() throws YosException {
  221. /*
  222. * 过滤条件设置
  223. */
  224. StringBuffer where = new StringBuffer(" 1=1 ");
  225. if (content.containsKey("where")) {
  226. JSONObject whereObject = content.getJSONObject("where");
  227. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  228. where.append(" and(");
  229. where.append("t2.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  230. where.append("or t2.model like'%").append(whereObject.getString("condition")).append("%' ");
  231. where.append("or t2.spec like'%").append(whereObject.getString("condition")).append("%' ");
  232. where.append("or t2.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  233. where.append(")");
  234. }
  235. }
  236. Long sa_dispatchid = content.getLong("sa_dispatchid");
  237. // String hrid = content.getString("hrid");
  238. SQLFactory sqlFactory = new SQLFactory(this, "发货单明细列表查询", pageSize, pageNumber, pageSorting);
  239. sqlFactory.addParameter_SQL("where", where);
  240. sqlFactory.addParameter("sa_dispatchid", sa_dispatchid);
  241. sqlFactory.addParameter("siteid", siteid);
  242. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  243. RowsMap rowsMap = rows.toRowsMap("itemno");
  244. ERPDocking erpDocking = new ERPDocking(siteid);
  245. JSONArray jsonArray = new JSONArray();
  246. JSONObject object = new JSONObject();
  247. object.put("classname", "getIcinvbal");
  248. object.put("method", "getMsg");
  249. JSONObject content = new JSONObject();
  250. content.put("fitemnos", rows.toJsonArray("itemno"));
  251. object.put("content", content);
  252. WebRequest request = new WebRequest();
  253. //System.out.println(object.toString());
  254. if(siteid.equalsIgnoreCase("lsa")){
  255. String result = request.doPost(object.toString(),
  256. "http://60.190.151.198:8089/BYESB/jaxrs/webclientrest");
  257. if(isJSONArray(result)){
  258. jsonArray = JSONArray.parseArray(result);
  259. }
  260. }else{
  261. if (rows.toJsonArray("itemno").size() != 0) {
  262. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true") ) {
  263. jsonArray = erpDocking.getErpIcinvbalRows(200000, 1, rows.toJsonArray("itemno"));
  264. }
  265. }
  266. }
  267. SQLFactory sqlFactory1 = new SQLFactory(this, "商品发货数量汇总");
  268. sqlFactory1.addParameter("siteid", siteid);
  269. sqlFactory1.addParameter_in("itemno", rows.toArrayList("itemno", new ArrayList<>()));
  270. Rows sumQtyRows = dbConnect.runSqlQuery(sqlFactory1);
  271. RowsMap sumQtyRowsMap = sumQtyRows.toRowsMap("itemno");
  272. if (!jsonArray.isEmpty()) {
  273. for (Object obj : jsonArray) {
  274. JSONObject jsonObject = (JSONObject) obj;
  275. if (rowsMap.containsKey(jsonObject.getString("fitemno"))) {
  276. if (rowsMap.get(jsonObject.getString("fitemno")).isNotEmpty()) {
  277. if (sumQtyRowsMap.containsKey(jsonObject.getString("fitemno"))) {
  278. if(siteid.equalsIgnoreCase("lsa")){
  279. rowsMap.get(jsonObject.getString("fitemno")).get(0).put("delinvbalqty", jsonObject.getBigDecimalValue("FQty").subtract(sumQtyRowsMap.get(jsonObject.getString("fitemno")).get(0).getBigDecimal("qty")));
  280. }else{
  281. rowsMap.get(jsonObject.getString("fitemno")).get(0).put("delinvbalqty", jsonObject.getBigDecimalValue("fqty").subtract(sumQtyRowsMap.get(jsonObject.getString("fitemno")).get(0).getBigDecimal("qty")));
  282. }
  283. } else {
  284. if(siteid.equalsIgnoreCase("lsa")){
  285. rowsMap.get(jsonObject.getString("fitemno")).get(0).put("delinvbalqty", jsonObject.getBigDecimalValue("FQty"));
  286. }else{
  287. rowsMap.get(jsonObject.getString("fitemno")).get(0).put("delinvbalqty", jsonObject.getBigDecimalValue("fqty"));
  288. }
  289. }
  290. if(siteid.equalsIgnoreCase("lsa")){
  291. rowsMap.get(jsonObject.getString("fitemno")).get(0).put("invbalqty", jsonObject.getBigDecimalValue("FQty"));
  292. }else{
  293. rowsMap.get(jsonObject.getString("fitemno")).get(0).put("invbalqty", jsonObject.getBigDecimalValue("fqty"));
  294. }
  295. }
  296. }
  297. }
  298. }
  299. for (Row row : rows) {
  300. if (!row.containsKey("delinvbalqty")) {
  301. row.put("delinvbalqty", row.getBigDecimal("undeliqty").negate());
  302. row.put("invbalqty", 0);
  303. }
  304. }
  305. return getSucReturnObject().setData(rows).toString();
  306. }
  307. @API(title = "发货单商品所有明细列表", apiversion = R.ID20230508113003.v1.class)
  308. @CACHEING
  309. public String queryDispatchAllItemsList() throws YosException, IOException {
  310. /*
  311. * 过滤条件设置
  312. */
  313. StringBuffer where = new StringBuffer(" 1=1 ");
  314. if (sys_enterpriseid > 0) {
  315. where.append(" and t4.sys_enterpriseid ='").append(sys_enterpriseid).append("' ");
  316. where.append(" and ifnull(t1.outwarehouseqty, 0)>0 ");
  317. }
  318. if (content.containsKey("where")) {
  319. JSONObject whereObject = content.getJSONObject("where");
  320. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  321. where.append(" and(");
  322. where.append(" t4.billno like'%").append(whereObject.getString("condition")).append("%' ");
  323. where.append("or t8.sonum like'%").append(whereObject.getString("condition")).append("%' ");
  324. where.append("or t9.agentnum like'%").append(whereObject.getString("condition")).append("%' ");
  325. where.append("or t10.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  326. where.append("or t2.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  327. where.append("or t2.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  328. where.append(")");
  329. }
  330. if (whereObject.containsKey("agentinfo") && !"".equals(whereObject.getString("agentinfo"))) {
  331. where.append(" and(");
  332. where.append("t9.agentnum like'%").append(whereObject.getString("agentinfo")).append("%' ");
  333. where.append("or t10.enterprisename like'%").append(whereObject.getString("agentinfo")).append("%' ");
  334. where.append(")");
  335. }
  336. if (whereObject.containsKey("iteminfo") && !"".equals(whereObject.getString("iteminfo"))) {
  337. where.append(" and(");
  338. where.append("t2.itemno like'%").append(whereObject.getString("iteminfo")).append("%' ");
  339. where.append("or t2.itemname like'%").append(whereObject.getString("iteminfo")).append("%' ");
  340. where.append(")");
  341. }
  342. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  343. where.append(" and DATE_FORMAT(t4.billdate, '%Y-%m-%d') >='").append(whereObject.getString("begindate")).append("' ");
  344. }
  345. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  346. where.append(" and DATE_FORMAT(t4.billdate, '%Y-%m-%d') <='").append(whereObject.getString("enddate")).append("' ");
  347. }
  348. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  349. where.append(" and t4.status ='").append(whereObject.getString("status")).append("' ");
  350. }
  351. if (whereObject.containsKey("isnotsent") && !"".equals(whereObject.getString("isnotsent"))) {
  352. if (whereObject.getBooleanValue("isnotsent")) {
  353. //System.out.println(" and t4.status ='复核' and ifnull(t1.outwarehouseqty, 0)< ifnull(t1.qty, 0)");
  354. where.append(" and t1.sa_dispatchid in(SELECT sa_dispatchid FROM (SELECT sum(outwarehouseqty) outwarehouseqty,sa_dispatchid FROM sa_dispatch_items WHERE siteid='"+siteid+"' GROUP BY sa_dispatchid) a WHERE a.outwarehouseqty=0)");
  355. }
  356. }
  357. }
  358. boolean isExport = content.getBooleanValue("isExport");
  359. // SQLFactory sqlFactory = new SQLFactory(this, "发货单明细所有列表查询", pageSize, pageNumber, pageSorting);
  360. // if (isExport) {
  361. // sqlFactory = new SQLFactory(this, "发货单明细所有列表查询");
  362. // }
  363. // sqlFactory.addParameter_SQL("where", where);
  364. // sqlFactory.addParameter("siteid", siteid);
  365. // Rows rows = dbConnect.runSqlQuery(sqlFactory);
  366. QuerySQL querySQL = queryListManage(where.toString());
  367. querySQL.setOrderBy(pageSorting);
  368. Rows rows;
  369. querySQL.setPage(pageSize, pageNumber);
  370. rows = querySQL.query();
  371. SQLFactory areasqlFactory = new SQLFactory(this, "发货单明细关联区域列表查询");
  372. areasqlFactory.addParameter("siteid", siteid);
  373. areasqlFactory.addParameter_in("sys_enterpriseids", rows.toArrayList("sys_enterpriseid", new ArrayList<>()));
  374. Rows areaRows = dbConnect.runSqlQuery(areasqlFactory);
  375. RowsMap areaRowsMap = areaRows.toRowsMap("sys_enterpriseid");
  376. SQLFactory itemsqlFactory = new SQLFactory(this, "发货单明细关联商品列表查询");
  377. itemsqlFactory.addParameter("siteid", siteid);
  378. itemsqlFactory.addParameter_in("itemids", rows.toArrayList("itemid", new ArrayList<>()));
  379. Rows itemRows = dbConnect.runSqlQuery(itemsqlFactory);
  380. RowsMap itemRowsMap = itemRows.toRowsMap("itemid");
  381. SQLFactory ordersqlFactory = new SQLFactory(this, "发货单明细关联订单列表查询");
  382. ordersqlFactory.addParameter("siteid", siteid);
  383. ordersqlFactory.addParameter_in("sa_orderitemsids", rows.toArrayList("sa_orderitemsid", new ArrayList<>()));
  384. Rows orderRows = dbConnect.runSqlQuery(ordersqlFactory);
  385. RowsMap orderRowsMap = orderRows.toRowsMap("sa_orderitemsid");
  386. for (Row row : rows) {
  387. if (orderRowsMap.containsKey(row.getString("sa_orderitemsid"))) {
  388. row.put("sa_orderid", orderRowsMap.get(row.getString("sa_orderitemsid")).get(0).getString("sa_orderid"));
  389. row.put("sonum", orderRowsMap.get(row.getString("sa_orderitemsid")).get(0).getString("sonum"));
  390. row.put("type", orderRowsMap.get(row.getString("sa_orderitemsid")).get(0).getString("type"));
  391. row.put("orderRowno", orderRowsMap.get(row.getString("sa_orderitemsid")).get(0).getString("orderRowno"));
  392. row.put("price", orderRowsMap.get(row.getString("sa_orderitemsid")).get(0).getDouble("price"));
  393. row.put("undeliqty", orderRowsMap.get(row.getString("sa_orderitemsid")).get(0).getDouble("undeliqty"));
  394. row.put("deliedqty", orderRowsMap.get(row.getString("sa_orderitemsid")).get(0).getDouble("deliedqty"));
  395. row.put("amount", orderRowsMap.get(row.getString("sa_orderitemsid")).get(0).getBigDecimal("price").multiply(row.getBigDecimal("qty")).doubleValue());
  396. }
  397. if (itemRowsMap.containsKey(row.getString("itemid"))) {
  398. row.put("itemno", itemRowsMap.get(row.getString("itemid")).get(0).getString("itemno"));
  399. row.put("itemname", itemRowsMap.get(row.getString("itemid")).get(0).getString("itemname"));
  400. row.put("unitname", itemRowsMap.get(row.getString("itemid")).get(0).getString("unitname"));
  401. }
  402. if (areaRowsMap.containsKey(row.getString("sys_enterpriseid"))) {
  403. row.put("areaname", areaRowsMap.get(row.getString("sys_enterpriseid")).get(0).getString("areaname"));
  404. } else {
  405. row.put("areaname", "");
  406. }
  407. }
  408. RowsMap rowsMap = rows.toRowsMap("itemno");
  409. ERPDocking erpDocking = new ERPDocking(siteid);
  410. JSONArray jsonArray = new JSONArray();
  411. if (rows.toJsonArray("itemno").size() != 0) {
  412. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true") ) {
  413. jsonArray = erpDocking.getErpIcinvbalRows(200000, 1, rows.toJsonArray("itemno"));
  414. }
  415. }
  416. SQLFactory sqlFactory1 = new SQLFactory(this, "商品发货数量汇总");
  417. sqlFactory1.addParameter("siteid", siteid);
  418. sqlFactory1.addParameter_in("itemno", rows.toArrayList("itemno", new ArrayList<>()));
  419. Rows sumQtyRows = dbConnect.runSqlQuery(sqlFactory1);
  420. RowsMap sumQtyRowsMap = sumQtyRows.toRowsMap("itemno");
  421. if (!jsonArray.isEmpty()) {
  422. for (Object object : jsonArray) {
  423. JSONObject jsonObject = (JSONObject) object;
  424. if (rowsMap.containsKey(jsonObject.getString("fitemno"))) {
  425. if (rowsMap.get(jsonObject.getString("fitemno")).isNotEmpty()) {
  426. if (sumQtyRowsMap.containsKey(jsonObject.getString("fitemno"))) {
  427. rowsMap.get(jsonObject.getString("fitemno")).get(0).put("delinvbalqty", jsonObject.getBigDecimalValue("fqty").subtract(sumQtyRowsMap.get(jsonObject.getString("fitemno")).get(0).getBigDecimal("qty")));
  428. } else {
  429. rowsMap.get(jsonObject.getString("fitemno")).get(0).put("delinvbalqty", jsonObject.getBigDecimalValue("fqty"));
  430. }
  431. rowsMap.get(jsonObject.getString("fitemno")).get(0).put("invbalqty", jsonObject.getBigDecimalValue("fqty"));
  432. }
  433. }
  434. }
  435. }
  436. for (Row row : rows) {
  437. if (!row.containsKey("delinvbalqty")) {
  438. row.put("delinvbalqty", row.getBigDecimal("undeliqty").negate().doubleValue());
  439. row.put("invbalqty", 0);
  440. }
  441. }
  442. // if (isExport) {
  443. // //去除不需要导出项
  444. // rows.getFieldList().remove("sa_dispatch_itemsid");
  445. // rows.getFieldList().remove("sa_dispatchid");
  446. // rows.getFieldList().remove("sa_orderitemsid");
  447. // rows.getFieldList().remove("sa_orderid");
  448. // rows.getFieldList().remove("sys_enterpriseid");
  449. // rows.getFieldList().remove("isclose");
  450. // rows.getFieldList().remove("rowno");
  451. // rows.getFieldList().remove("itemid");
  452. // rows.getFieldList().remove("batchno");
  453. // Rows rowsrolename= dbConnect.runSqlQuery("select * from sys_userrole t1 inner join sys_role t2 on t1.roleid=t2.roleid and t1.siteid=t2.siteid where t2.rolename='经销商员工' and t1.userid="+userid);
  454. // if(!rowsrolename.isEmpty()){
  455. // rows.getFieldList().remove("price");
  456. // rows.getFieldList().remove("amount");
  457. // }
  458. // rows.getFieldList().remove("undeliqty");
  459. // rows.getFieldList().remove("deliedqty");
  460. // rows.getFieldList().remove("outwarehousedate");
  461. // Rows uploadRows = uploadExcelToObs("dispatch", "发货单明细列表", rows, getTitleMap());
  462. // return getSucReturnObject().setData(uploadRows).toString();
  463. // }
  464. return getSucReturnObject().setData(rows).toString();
  465. }
  466. //查询订单列表(管理端)
  467. public QuerySQL queryListManage(String where) throws YosException {
  468. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_dispatch_items",
  469. "sa_dispatch_itemsid", "sa_dispatchid", "sa_orderitemsid", "isclose", "rowno", "itemid", "outwarehousedate", "batchno", "remarks");
  470. querySQL.setTableAlias("t1");
  471. querySQL.addJoinTable(JOINTYPE.left, "plm_item", "t2", "t2.itemid = t1.itemid AND t2.siteid = t1.siteid",
  472. "itemno");
  473. querySQL.addJoinTable(JOINTYPE.left, "sa_dispatch", "t4", "t4.sa_dispatchid = t1.sa_dispatchid AND t4.siteid = t1.siteid",
  474. "billno", "status", "billdate", "closedate");
  475. querySQL.addJoinTable(JOINTYPE.left, "sa_orderitems", "t5", "t5.sa_orderitemsid=t1.sa_orderitemsid and t5.siteid=t1.siteid",
  476. "spec");
  477. querySQL.addJoinTable(JOINTYPE.left, "sa_order", "t8", "t8.sa_orderid=t4.sa_orderid and t8.siteid=t4.siteid",
  478. "sonum");
  479. querySQL.addJoinTable(JOINTYPE.left, "sa_agents", "t9", "t9.sys_enterpriseid = t4.sys_enterpriseid and t9.siteid = t4.siteid",
  480. "agentnum");
  481. querySQL.addJoinTable(JOINTYPE.left, "sys_enterprise", "t10", "t10.sys_enterpriseid = t4.sys_enterpriseid and t10.siteid = t4.siteid",
  482. "enterprisename", "sys_enterpriseid");
  483. querySQL.addQueryFields("qty", "ifnull(t1.qty, 0)");
  484. querySQL.addQueryFields("outwarehouseqty", "ifnull(t1.outwarehouseqty, 0)");
  485. querySQL.setWhere("t1.siteid", siteid);
  486. querySQL.setWhere(where);
  487. return querySQL;
  488. }
  489. @API(title = "工厂发货/退货明细", apiversion = R.ID20230626104003.v1.class)
  490. @CACHEING
  491. public String queryDispatchAndAftersalesmagItemsList() throws YosException, IOException {
  492. /*
  493. * 过滤条件设置
  494. */
  495. StringBuffer where = new StringBuffer(" 1=1 ");
  496. if (content.containsKey("where")) {
  497. JSONObject whereObject = content.getJSONObject("where");
  498. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  499. where.append(" and(");
  500. where.append("t.sonum like'%").append(whereObject.getString("condition")).append("%' ");
  501. where.append("or t.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  502. where.append("or t.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  503. where.append("or t.remarks like'%").append(whereObject.getString("condition")).append("%' ");
  504. where.append("or t.billno like'%").append(whereObject.getString("condition")).append("%' ");
  505. where.append(")");
  506. }
  507. if (whereObject.containsKey("type") && !"".equals(whereObject.getString("type"))) {
  508. where.append(" and(");
  509. where.append("t.type ='").append(whereObject.getString("type")).append("' ");
  510. where.append(")");
  511. }
  512. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  513. where.append(" and DATE_FORMAT(t.date, '%Y-%m-%d') >='").append(whereObject.getString("begindate")).append("' ");
  514. }
  515. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  516. where.append(" and DATE_FORMAT(t.date, '%Y-%m-%d') <='").append(whereObject.getString("enddate")).append("' ");
  517. }
  518. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  519. where.append(" and t.status ='").append(whereObject.getString("status")).append("' ");
  520. }
  521. }
  522. SQLFactory sqlFactory = new SQLFactory(this, "工厂发货及退货明细(经销商)", pageSize, pageNumber, pageSorting);
  523. sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  524. sqlFactory.addParameter_SQL("where", where);
  525. sqlFactory.addParameter("siteid", siteid);
  526. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  527. return getSucReturnObject().setData(rows).toString();
  528. }
  529. @API(title = "工厂发货/退货明细(区域经理)", apiversion = R.ID20231030091403.v1.class)
  530. @CACHEING
  531. public String queryDispatchAndAftersalesmagItemsManagerList() throws YosException, IOException {
  532. /*
  533. * 过滤条件设置
  534. */
  535. StringBuffer where = new StringBuffer(" 1=1 ");
  536. if (content.containsKey("where")) {
  537. JSONObject whereObject = content.getJSONObject("where");
  538. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  539. where.append(" and(");
  540. where.append("t.sonum like'%").append(whereObject.getString("condition")).append("%' ");
  541. where.append("or t.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  542. where.append("or t.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  543. where.append("or t.remarks like'%").append(whereObject.getString("condition")).append("%' ");
  544. where.append(")");
  545. }
  546. if (whereObject.containsKey("type") && !"".equals(whereObject.getString("type"))) {
  547. where.append(" and(");
  548. where.append("t.type ='").append(whereObject.getString("type")).append("' ");
  549. where.append(")");
  550. }
  551. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  552. where.append(" and DATE_FORMAT(t.date, '%Y-%m-%d') >='").append(whereObject.getString("begindate")).append("' ");
  553. }
  554. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  555. where.append(" and DATE_FORMAT(t.date, '%Y-%m-%d') <='").append(whereObject.getString("enddate")).append("' ");
  556. }
  557. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  558. where.append(" and t.status ='").append(whereObject.getString("status")).append("' ");
  559. }
  560. }
  561. boolean isExport = content.getBooleanValue("isExport");
  562. SQLFactory areasqlFactory = new SQLFactory(new promotion(content), "区域经理查询下属营销区域");
  563. areasqlFactory.addParameter("siteid", siteid);
  564. areasqlFactory.addParameter("hrid", hrid);
  565. Rows arearows = dbConnect.runSqlQuery(areasqlFactory);
  566. ArrayList<Long> arrayList = arearows.toArrayList("sa_saleareaid", new ArrayList<Long>());
  567. arrayList.addAll(SaleArea.getSubSaleAreaIds(this, arrayList));
  568. SQLFactory sqlFactory = new SQLFactory(this, "工厂发货及退货明细(区域经理)", pageSize, pageNumber, pageSorting);
  569. sqlFactory.addParameter_in("sa_saleareaids", arrayList);
  570. sqlFactory.addParameter_SQL("where", where);
  571. sqlFactory.addParameter("siteid", siteid);
  572. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  573. // if (isExport) {
  574. // //去除不需要导出项
  575. // rows.getFieldList().remove("sa_dispatch_itemsid");
  576. // rows.getFieldList().remove("sa_dispatchid");
  577. // rows.getFieldList().remove("sa_orderitemsid");
  578. // rows.getFieldList().remove("sa_orderid");
  579. // rows.getFieldList().remove("sys_enterpriseid");
  580. // rows.getFieldList().remove("isclose");
  581. // rows.getFieldList().remove("rowno");
  582. // rows.getFieldList().remove("itemid");
  583. // rows.getFieldList().remove("batchno");
  584. // Rows rowsrolename= dbConnect.runSqlQuery("select * from sys_userrole t1 inner join sys_role t2 on t1.roleid=t2.roleid and t1.siteid=t2.siteid where t2.rolename='经销商员工' and t1.userid="+userid);
  585. // if(!rowsrolename.isEmpty()){
  586. // rows.getFieldList().remove("price");
  587. // rows.getFieldList().remove("amount");
  588. // }
  589. // rows.getFieldList().remove("undeliqty");
  590. // rows.getFieldList().remove("deliedqty");
  591. // rows.getFieldList().remove("outwarehousedate");
  592. // Rows uploadRows = uploadExcelToObs("dispatch", "发货单明细列表", rows, getTitleMap());
  593. // return getSucReturnObject().setData(uploadRows).toString();
  594. // }
  595. return getSucReturnObject().setData(rows).toString();
  596. }
  597. //返回导出的标题
  598. public HashMap<String, String> getTitleMap() throws YosException {
  599. HashMap<String, String> titleMap = new HashMap<>();
  600. titleMap.put("billno", "发货单号");
  601. titleMap.put("status", "状态");
  602. titleMap.put("sonum", "订单号");
  603. titleMap.put("enterprisename", "企业名称");
  604. titleMap.put("agentnum", "经销商编号");
  605. titleMap.put("billdate", "发货日期");
  606. titleMap.put("orderRowno", "订单行号");
  607. titleMap.put("itemno", "产品编码");
  608. titleMap.put("itemname", "品名");
  609. titleMap.put("unitname", "单位");
  610. titleMap.put("qty", "发货数量");
  611. titleMap.put("remarks", "发货行备注");
  612. titleMap.put("outwarehouseqty", "已出库数量");
  613. Rows rowsrolename = dbConnect.runSqlQuery("select * from sys_userrole t1 inner join sys_role t2 on t1.roleid=t2.roleid and t1.siteid=t2.siteid where t2.rolename='经销商员工' and t1.userid=" + userid);
  614. if (rowsrolename.isEmpty()) {
  615. titleMap.put("price", "单价");
  616. titleMap.put("amount", "金额");
  617. }
  618. return titleMap;
  619. }
  620. @API(title = "删除明细", apiversion = R.ID20221115104803.v1.class)
  621. @CACHEING_CLEAN(apiClass = {dispatchItems.class, dispatch.class})
  622. public String deletemx() throws YosException {
  623. JSONArray sa_dispatch_itemsids = content.getJSONArray("sa_dispatch_itemsids");
  624. BatchDeleteErr batchDeleteErr = BatchDeleteErr.create(this, sa_dispatch_itemsids.size());
  625. long sa_dispatchid = 0;
  626. for (Object o : sa_dispatch_itemsids) {
  627. long sa_dispatch_itemsid = Long.parseLong(o.toString());
  628. Rows RowsStatus = dbConnect.runSqlQuery("select t1.sa_dispatch_itemsid,t2.status,t1.sa_dispatchid from sa_dispatch_items t1 left join sa_dispatch t2 on t1.sa_dispatchid=t2.sa_dispatchid and t1.siteid=t2.siteid where t1.siteid='"
  629. + siteid + "' and t1.sa_dispatch_itemsid='" + sa_dispatch_itemsid + "'");
  630. if (RowsStatus.isNotEmpty()) {
  631. sa_dispatchid = RowsStatus.get(0).getLong("sa_dispatchid");
  632. if (!RowsStatus.get(0).getString("status").equals("新建")) {
  633. batchDeleteErr.addErr(sa_dispatch_itemsid, "非新建状态的发货单明细无法删除");
  634. continue;
  635. }
  636. }
  637. ArrayList<String> list = new ArrayList<>();
  638. SQLFactory deletesql = new SQLFactory("sql:delete from sa_dispatch_items where siteid='" + siteid
  639. + "' and sa_dispatch_itemsid=" + sa_dispatch_itemsid);
  640. list.add(deletesql.getSQL());
  641. dbConnect.runSqlUpdate(list);
  642. }
  643. //重新排序
  644. updateRowNo(sa_dispatchid);
  645. return batchDeleteErr.getReturnObject().toString();
  646. }
  647. /**
  648. * 重新对发货单行进行排序
  649. *
  650. * @param
  651. * @throws YosException
  652. */
  653. public void updateRowNo(Long sa_dispatchid) throws YosException {
  654. String sql = "SELECT sa_dispatch_itemsid from sa_dispatch_items WHERE sa_dispatchid= " + sa_dispatchid + " and siteid = '" + siteid + "' ORDER BY sa_dispatch_itemsid asc ";
  655. ArrayList<Long> sa_dispatch_itemsids = dbConnect.runSqlQuery(sql).toArrayList("sa_dispatch_itemsid", new ArrayList<>());
  656. ArrayList<String> sqlList = new ArrayList<>();
  657. int rowno = 1;
  658. for (Long id : sa_dispatch_itemsids) {
  659. sqlList.add("UPDATE sa_dispatch_items SET rowno=" + rowno + " WHERE sa_dispatch_itemsid = " + id);
  660. rowno++;
  661. }
  662. dbConnect.runSqlUpdate(sqlList);
  663. }
  664. }