dispatchItems.java 33 KB

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