dispatchItems.java 34 KB

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