stockbillitems.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. package restcontroller.webmanage.sale.stockbill;
  2. import beans.brand.Brand;
  3. import beans.data.BatchDeleteErr;
  4. import beans.datacontrllog.DataContrlLog;
  5. import beans.itemprice.ItemPrice;
  6. import com.alibaba.fastjson2.JSONArray;
  7. import com.alibaba.fastjson2.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 restcontroller.R;
  16. import java.math.BigDecimal;
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. public class stockbillitems extends Controller {
  21. public stockbillitems(JSONObject content) throws YosException {
  22. super(content);
  23. }
  24. @API(title = "出库商品列表查询", apiversion = R.ID2025050614581203.v1.class)
  25. @CACHEING
  26. public String queryOrderItemgList() throws YosException {
  27. /*
  28. * 过滤条件设置
  29. */
  30. StringBuffer where = new StringBuffer(" 1=1 ");
  31. if (content.containsKey("where")) {
  32. JSONObject whereObject = content.getJSONObject("where");
  33. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  34. where.append(" and(");
  35. where.append("t1.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  36. where.append("or t1.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  37. where.append("or t1.unit like'%").append(whereObject.getString("condition")).append("%' ");
  38. where.append("or t3.sonum like'%").append(whereObject.getString("condition")).append("%' ");
  39. where.append("or t3.typemx like'%").append(whereObject.getString("condition")).append("%' ");
  40. where.append(")");
  41. }
  42. if (whereObject.containsKey("sa_orderid") && !"".equals(whereObject.getString("sa_orderid"))) {
  43. Long sa_orderid = whereObject.getLongValue("sa_orderid");
  44. if (sa_orderid > 0) {
  45. where.append(" and t1.sa_orderid ='").append(sa_orderid).append("' ");
  46. }
  47. }
  48. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  49. where.append(" and t3.checkdate >='").append(whereObject.getString("begindate")).append("' ");
  50. }
  51. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  52. where.append(" and t3.checkdate <='").append(getDate_Str(whereObject.getDate("enddate"), 1)).append("' ");
  53. }
  54. if (whereObject.containsKey("type") && !"".equals(whereObject.getString("type"))) {
  55. where.append(" and t3.type ='").append(whereObject.getString("type")).append("' ");
  56. }
  57. }
  58. // SQLFactory sqlFactory = new SQLFactory(this, "发货商品列表查询", pageSize, pageNumber, pageSorting);
  59. // sqlFactory.addParameter_SQL("where", where);
  60. // sqlFactory.addParameter("sys_enterpriseid", content.getLongValue("sys_enterpriseid"));
  61. // sqlFactory.addParameter("sa_dispatchid", content.getLongValue("sa_dispatchid"));
  62. // sqlFactory.addParameter("siteid", siteid);
  63. //
  64. // Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  65. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_orderitems", "sa_orderitemsid", "sa_orderid",
  66. "itemid", "unit", "undeliqty", "remarks", "rowno", "customproperties", "price", "defaultprice", "custamount");
  67. querySQL.setTableAlias("t1");
  68. querySQL.addJoinTable(JOINTYPE.left, "plm_item", "t2", "t1.siteid = t2.siteid and t1.itemid = t2.itemid", "itemno", "itemname", "model", "spec", "batchcontrol", "icaddqty", "icminqty", "outplace");
  69. querySQL.addJoinTable(JOINTYPE.left, "sa_order", "t3", "t1.siteid=t3.siteid and t1.sa_orderid=t3.sa_orderid", "sonum", "billdate", "createdate", "submitdate", "checkdate", "typemx", "abstract", "delivery", "invoicename", "invoicetaxno", "invoiceaddress");
  70. querySQL.addQueryFields("orderremarks", "t3.remarks");
  71. querySQL.setSiteid(siteid);
  72. querySQL.setWhere("t3.STATUS = '审核' and ifnull(t1.undeliqty, 0) !=0 and ifnull(t1.isfreeze, 0) =0 AND ifnull( t1.isclose, 0 )= 0");
  73. querySQL.setWhere("t3.sys_enterpriseid", content.getLongValue("sys_enterpriseid"));
  74. querySQL.setWhere("if(ifnull(t2.batchcontrol,0)!=0,1=1,not EXISTS(select 1 from st_stockbill_items where st_stockbillid='" + content.getLongValue("st_stockbillid") + "' and sa_orderitemsid=t1.sa_orderitemsid))");
  75. //querySQL.setWhere("t1.sa_orderitemsid not in(select t1.sa_orderitemsid from (select t1.sa_orderitemsid,t1.siteid,SUM(t1.qty) qty from sa_dispatch_items t1 inner join sa_dispatch t2 on t1.sa_dispatchid=t2.sa_dispatchid and t1.siteid=t2.siteid where t2.status in('新建','提交') group by t1.sa_orderitemsid,t1.siteid) t1 inner join sa_orderitems t3 on t1.sa_orderitemsid=t3.sa_orderitemsid and t1.siteid=t3.siteid where t3.undeliqty=t1.qty)");
  76. querySQL.setWhere(where.toString());
  77. querySQL.setPage(pageSize, pageNumber);
  78. Rows rows = querySQL.query();
  79. return getSucReturnObject().setData(rows).toString();
  80. }
  81. @API(title = "商品档案添加商品", apiversion = R.ID2025101713485003.v1.class)
  82. public String queryItemList() throws YosException {
  83. long st_stockbillid = content.getLong("st_stockbillid");
  84. Rows stockbillrows = dbConnect.runSqlQuery("select sys_enterpriseid from st_stockbill where st_stockbillid=" + st_stockbillid);
  85. if (stockbillrows.isEmpty()) {
  86. return getErrReturnObject().setErrMsg("该出入库单不存在").toString();
  87. }
  88. /*
  89. * 过滤条件设置
  90. */
  91. StringBuffer where = new StringBuffer(" 1=1 ");
  92. if (content.containsKey("where")) {
  93. JSONObject whereObject = content.getJSONObject("where");
  94. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  95. where.append(" and(");
  96. where.append("t1.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  97. where.append("or t1.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  98. where.append("or t1.unit like'%").append(whereObject.getString("condition")).append("%' ");
  99. where.append(")");
  100. }
  101. }
  102. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "plm_item",
  103. "itemid", "itemno", "itemname", "model", "spec", "batchcontrol", "outplace", "icaddqty", "icminqty");
  104. querySQL.setTableAlias("t1");
  105. querySQL.addJoinTable(JOINTYPE.left, "plm_unit", "t2", "t1.siteid = t2.siteid and t1.unitid = t2.unitid", "unitname");
  106. querySQL.setSiteid(siteid);
  107. querySQL.addQueryFields("qty", "t1.icminqty");
  108. querySQL.setWhere("t1.STATUS = '审核' and t1.isused=1");
  109. querySQL.setWhere("t1.itemid not in (select itemid from st_stockbill_items where st_stockbillid='" + st_stockbillid + "')");
  110. //querySQL.setWhere("t1.sa_orderitemsid not in(select t1.sa_orderitemsid from (select t1.sa_orderitemsid,t1.siteid,SUM(t1.qty) qty from sa_dispatch_items t1 inner join sa_dispatch t2 on t1.sa_dispatchid=t2.sa_dispatchid and t1.siteid=t2.siteid where t2.status in('新建','提交') group by t1.sa_orderitemsid,t1.siteid) t1 inner join sa_orderitems t3 on t1.sa_orderitemsid=t3.sa_orderitemsid and t1.siteid=t3.siteid where t3.undeliqty=t1.qty)");
  111. querySQL.setWhere(where.toString());
  112. querySQL.setPage(pageSize, pageNumber);
  113. Rows rows = querySQL.query();
  114. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  115. //查询附件
  116. RowsMap attinfoRowsMap = getAttachmentUrl("plm_item", ids);
  117. //商品领域
  118. RowsMap tradefieldRowsMap = beans.Item.Item.getTradefieldRowsMap(this, ids);
  119. //品牌
  120. RowsMap brandRowsMap = Brand.getBrandRowsMap(this, ids);
  121. //价格
  122. HashMap<Long, ItemPrice> itemPriceRowsMap = ItemPrice.getItemPrice(this, stockbillrows.get(0).getLong("sys_enterpriseid"), ids);
  123. for (Row row : rows) {
  124. row.put("attinfos", attinfoRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  125. row.put("tradefield", tradefieldRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  126. row.put("brand", brandRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  127. if (itemPriceRowsMap.containsKey(row.getLong("itemid"))) {
  128. row.put("price", itemPriceRowsMap.get(row.getLong("itemid")).getPrice());
  129. } else {
  130. row.put("price", 0);
  131. }
  132. }
  133. return getSucReturnObject().setData(rows).toString();
  134. }
  135. @API(title = "出入库单明细新增更新", apiversion = R.ID2025050614284803.v1.class)
  136. @CACHEING_CLEAN(apiClass = {stockbill.class, stockbillitems.class})
  137. public String insertormodify_stcokbillItems() throws YosException {
  138. Long st_stockbillid = content.getLong("st_stockbillid");
  139. JSONArray iteminfos = content.getJSONArray("iteminfos");
  140. ArrayList<String> sqlList = new ArrayList<>();
  141. Rows rowscount = dbConnect.runSqlQuery("select billno,status,sourceobject,stockid,type,name,phonenumber,address,departmentid from st_stockbill where st_stockbillid=" + st_stockbillid);
  142. if (!rowscount.isEmpty()) {
  143. if (!rowscount.get(0).getString("status").equals("新建")) {
  144. return getErrReturnObject().setErrMsg("非新建状态的出库单无法新增修改").toString();
  145. }
  146. if (rowscount.get(0).getString("sourceobject").equals("tpartreimbursement")) {
  147. return getErrReturnObject().setErrMsg("由核销生成的出入库单无法新增修改").toString();
  148. }
  149. } else {
  150. return getErrReturnObject().setErrMsg("出入库单不存在").toString();
  151. }
  152. int i = 0;
  153. long maxid = 0;
  154. long[] st_stockbill_itemsid = createTableID("st_stockbill_items", iteminfos.size());
  155. Rows maxidRows = dbConnect
  156. .runSqlQuery("select MAX(rowno) rowno from st_stockbill_items where st_stockbillid=" + st_stockbillid);
  157. Rows detailRows = dbConnect
  158. .runSqlQuery("select rowno,price,defaultprice,discountrate,st_stockbill_itemsid from st_stockbill_items where st_stockbillid=" + st_stockbillid);
  159. RowsMap detailRowsMap = detailRows.toRowsMap("st_stockbill_itemsid");
  160. if (!maxidRows.isEmpty()) {
  161. maxid = maxidRows.get(0).getLong("rowno");
  162. }
  163. List<Long> list = new ArrayList<>();
  164. for (Object object : iteminfos) {
  165. list.add(((JSONObject) object).getLong("itemid"));
  166. }
  167. RowsMap itemRowsMap = SQLFactory.createQuerySQL(this, "plm_item", "*").setTableAlias("t1").addJoinTable(JOINTYPE.left, "st_stock", "t2", "t1.siteid = t2.siteid and t1.stockno = t2.stockno", "stockid").setWhere("itemid", list).query().toRowsMap("itemid");
  168. RowsMap stockRowsMap = SQLFactory.createQuerySQL(this, "st_stock", "stockid", "stockno").query().toRowsMap("stockno");
  169. for (Object obj : iteminfos) {
  170. JSONObject iteminfo = (JSONObject) obj;
  171. if (!iteminfo.containsKey("custamount")) {
  172. iteminfo.put("custamount", 0);
  173. }
  174. if (iteminfo.getLong("st_stockbill_itemsid") <= 0 || dbConnect
  175. .runSqlQuery("select st_stockbill_itemsid from st_stockbill_items where st_stockbill_itemsid="
  176. + iteminfo.getLong("st_stockbill_itemsid"))
  177. .isEmpty()) {
  178. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "st_stockbill_items");
  179. insertSQL.setUniqueid(st_stockbill_itemsid[i]);
  180. insertSQL.setSiteid(siteid);
  181. insertSQL.setValue("rowno", maxid + i + 1);
  182. insertSQL.setValue("st_stockbillid", st_stockbillid);
  183. insertSQL.setValue("sa_orderitemsid", iteminfo.getLong("sa_orderitemsid"));
  184. if (rowscount.get(0).getString("type").equals("其他入库")) {
  185. if (rowscount.get(0).getLong("stockid") != 0) {
  186. insertSQL.setValue("stockid", rowscount.get(0).getLong("stockid"));
  187. } else {
  188. insertSQL.setValue("stockid", itemRowsMap.containsKey(iteminfo.getStringValue("itemid")) ? itemRowsMap.get(iteminfo.getStringValue("itemid")).get(0).getLong("stockid") : iteminfo.getLongValue("stockid"));
  189. }
  190. } else if (rowscount.get(0).getString("type").equals("销售出库")) {
  191. if (itemRowsMap.containsKey(iteminfo.getStringValue("itemid"))) {
  192. if (itemRowsMap.get(iteminfo.getStringValue("itemid")).get(0).getBoolean("ismodule")) {
  193. insertSQL.setValue("stockid", stockRowsMap.get("105").get(0).getLong("stockid"));
  194. } else {
  195. insertSQL.setValue("stockid", stockRowsMap.get("101").get(0).getLong("stockid"));
  196. }
  197. }
  198. } else if (rowscount.get(0).getString("type").equals("其他出库")) {
  199. if (rowscount.get(0).getLong("stockid") != 0) {
  200. insertSQL.setValue("stockid", rowscount.get(0).getLong("stockid"));
  201. } else {
  202. insertSQL.setValue("stockid", itemRowsMap.containsKey(iteminfo.getStringValue("itemid")) ? itemRowsMap.get(iteminfo.getStringValue("itemid")).get(0).getLong("stockid") : iteminfo.getLongValue("stockid"));
  203. }
  204. } else {
  205. insertSQL.setValue("stockid", iteminfo.getLong("stockid"));
  206. }
  207. insertSQL.setValue("itemid", iteminfo.getStringValue("itemid"));
  208. insertSQL.setValue("sa_orderid", iteminfo.getLongValue("sa_orderid"));
  209. insertSQL.setValue("qty", iteminfo.getStringValue("qty"));
  210. insertSQL.setValue("discountrate", 1);
  211. insertSQL.setValue("sa_dispatch_itemsid", 0);
  212. insertSQL.setValue("defaultprice", iteminfo.getBigDecimal("price").subtract(iteminfo.getBigDecimal("custamount")));
  213. insertSQL.setValue("remarks", iteminfo.getStringValue("remarks"));
  214. insertSQL.setValue("itemno", itemRowsMap.containsKey(iteminfo.getStringValue("itemid")) ? itemRowsMap.get(iteminfo.getStringValue("itemid")).get(0).getString("itemno") : iteminfo.getString("itemno"));
  215. insertSQL.setValue("itemname", itemRowsMap.containsKey(iteminfo.getStringValue("itemid")) ? itemRowsMap.get(iteminfo.getStringValue("itemid")).get(0).getString("itemname") : iteminfo.getString("itemname"));
  216. insertSQL.setValue("model", itemRowsMap.containsKey(iteminfo.getStringValue("itemid")) ? itemRowsMap.get(iteminfo.getStringValue("itemid")).get(0).getString("model") : iteminfo.getString("model"));
  217. insertSQL.setValue("price", iteminfo.getBigDecimal("price").subtract(iteminfo.getBigDecimal("custamount")));
  218. insertSQL.setValue("amount", iteminfo.getBigDecimal("qty").multiply((iteminfo.getBigDecimal("price").subtract(iteminfo.getBigDecimal("custamount")))));
  219. insertSQL.setValue("custamount", iteminfo.getBigDecimal("custamount"));
  220. insertSQL.setValue("untaxedprice", iteminfo.getBigDecimal("price").divide(new BigDecimal(1.13), 2, BigDecimal.ROUND_HALF_UP));
  221. insertSQL.setValue("untaxedamount", iteminfo.getBigDecimal("qty").multiply(iteminfo.getBigDecimal("price").divide(new BigDecimal(1.13), 2, BigDecimal.ROUND_HALF_UP)));
  222. insertSQL.setValue("skucontrol", itemRowsMap.containsKey(iteminfo.getStringValue("itemid")) ? itemRowsMap.get(iteminfo.getStringValue("itemid")).get(0).getBoolean("skucontrol") : iteminfo.getBoolean("skucontrol"));
  223. i++;
  224. if (StringUtils.isBlank(rowscount.get(0).getString("name"))
  225. || StringUtils.isBlank(rowscount.get(0).getString("phonenumber"))
  226. || StringUtils.isBlank(rowscount.get(0).getString("address"))) {
  227. Rows orderRows = dbConnect.runSqlQuery("select * from sa_order where sa_orderid=" + iteminfo.getLongValue("sa_orderid"));
  228. if (orderRows.isNotEmpty()) {
  229. sqlList.add("update st_stockbill set name='" + orderRows.get(0).getString("contact") + "',phonenumber='" + orderRows.get(0).getString("phonenumber") + "',address='" + orderRows.get(0).getString("address") + "' where st_stockbillid=" + st_stockbillid);
  230. }
  231. } else {
  232. String depname = "";
  233. Rows departmentrows = dbConnect.runSqlQuery("select depname from sys_department where departmentid=" + rowscount.get(0).getLong("departmentid"));
  234. if (departmentrows.isNotEmpty()) {
  235. depname = departmentrows.get(0).getString("depname");
  236. }
  237. if (depname.equals("售后部")) {
  238. Rows orderRows = dbConnect.runSqlQuery("select * from sa_order where sa_orderid=" + iteminfo.getLongValue("sa_orderid"));
  239. if (orderRows.isNotEmpty()) {
  240. sqlList.add("update st_stockbill set name='" + orderRows.get(0).getString("contact") + "',phonenumber='" + orderRows.get(0).getString("phonenumber") + "',address='" + orderRows.get(0).getString("address") + "' where st_stockbillid=" + st_stockbillid);
  241. }
  242. }
  243. }
  244. sqlList.add(insertSQL.getSQL());
  245. } else {
  246. Rows rows = dbConnect.runSqlQuery(
  247. "SELECT status from st_stockbill WHERE st_stockbillid = "
  248. + st_stockbillid);
  249. if (rows.isNotEmpty()) {
  250. if (rows.get(0).getString("status").equals("新建")) {
  251. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "st_stockbill_items");
  252. updateSQL.setUniqueid(iteminfo.getLong("st_stockbill_itemsid"));
  253. updateSQL.setSiteid(siteid);
  254. updateSQL.setValue("sa_orderitemsid", iteminfo.getLong("sa_orderitemsid"));
  255. updateSQL.setValue("itemid", iteminfo.getString("itemid"));
  256. updateSQL.setValue("sa_orderid", iteminfo.getString("sa_orderid"));
  257. updateSQL.setValue("qty", iteminfo.getString("qty"));
  258. updateSQL.setValue("discountrate", iteminfo.getString("discountrate"));
  259. updateSQL.setValue("defaultprice", iteminfo.getString("defaultprice"));
  260. updateSQL.setValue("remarks", iteminfo.getString("remarks"));
  261. updateSQL.setValue("itemno", iteminfo.getString("itemno"));
  262. updateSQL.setValue("itemname", iteminfo.getString("itemname"));
  263. updateSQL.setValue("model", iteminfo.getString("model"));
  264. updateSQL.setValue("price", iteminfo.getString("price"));
  265. updateSQL.setValue("amount", iteminfo.getBigDecimal("qty").multiply(iteminfo.getBigDecimal("price")));
  266. updateSQL.setValue("untaxedprice", iteminfo.getBigDecimal("price").divide(new BigDecimal(1.13), 2, BigDecimal.ROUND_HALF_UP));
  267. updateSQL.setValue("untaxedamount", iteminfo.getBigDecimal("qty").multiply(iteminfo.getBigDecimal("price").divide(new BigDecimal(1.13), 2, BigDecimal.ROUND_HALF_UP)));
  268. updateSQL.setValue("batchno", iteminfo.getString("batchno"));
  269. updateSQL.setValue("stockid", iteminfo.getString("stockid"));
  270. updateSQL.setValue("skucontrol", itemRowsMap.containsKey(iteminfo.getStringValue("itemid")) ? itemRowsMap.get(iteminfo.getStringValue("itemid")).get(0).getBoolean("skucontrol") : iteminfo.getBoolean("skucontrol"));
  271. if (detailRowsMap.containsKey(iteminfo.getString("st_stockbill_itemsid"))) {
  272. if (iteminfo.getBigDecimal("discountrate").compareTo(detailRowsMap.get(iteminfo.getString("st_stockbill_itemsid")).get(0).getBigDecimal("discountrate")) != 0
  273. || iteminfo.getBigDecimal("defaultprice").compareTo(detailRowsMap.get(iteminfo.getString("st_stockbill_itemsid")).get(0).getBigDecimal("defaultprice")) != 0
  274. || iteminfo.getBigDecimal("price").compareTo(detailRowsMap.get(iteminfo.getString("st_stockbill_itemsid")).get(0).getBigDecimal("price")) != 0) {
  275. sqlList.add(DataContrlLog.createLog(this, "st_stockbill", st_stockbillid, "明细金额变更", "行号【" + detailRowsMap.get(iteminfo.getString("st_stockbill_itemsid")).get(0).getString("rowno") + "】原价:" + iteminfo.getBigDecimal("defaultprice") + ",折后价:" + iteminfo.getBigDecimal("price") + ",折扣率:" + iteminfo.getBigDecimal("discountrate")).getSQL());
  276. }
  277. }
  278. sqlList.add(updateSQL.getSQL());
  279. } else {
  280. return getErrReturnObject().setErrMsg("非新建状态下无法编辑").toString();
  281. }
  282. } else {
  283. return getErrReturnObject().setErrMsg("该销售出库单不存在").toString();
  284. }
  285. }
  286. }
  287. dbConnect.runSqlUpdate(sqlList);
  288. Rows rowsdetail = dbConnect.runSqlQuery("select amount,qty from st_stockbill_items where st_stockbillid=" + st_stockbillid);
  289. Rows stockbillrows = dbConnect.runSqlQuery("select t1.sys_enterpriseid,t1.paydiscountamount,t2.depname from st_stockbill t1 left join sys_department t2 on t1.departmentid=t2.departmentid where t1.st_stockbillid=" + st_stockbillid);
  290. Rows accountbalancerows = dbConnect.runSqlQuery("select * from sa_accountbalance t1 inner join sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid where t2.accountname='现金账户' and t1.sys_enterpriseid=" + stockbillrows.get(0).getLong("sys_enterpriseid"));
  291. String depname = stockbillrows.get(0).getString("depname");
  292. BigDecimal payamount = rowsdetail.sum("amount");
  293. if (depname.equals("售后部")) {
  294. if (accountbalancerows.isNotEmpty()) {
  295. if (accountbalancerows.get(0).getBigDecimal("discountamount").compareTo(BigDecimal.ZERO) > 0) {
  296. if (accountbalancerows.get(0).getBigDecimal("discountamount").compareTo(payamount) > 0) {
  297. dbConnect.runSqlUpdate("update st_stockbill set payamount=0,paydiscountamount=" + payamount + " where st_stockbillid=" + st_stockbillid);
  298. } else {
  299. dbConnect.runSqlUpdate("update st_stockbill set payamount=" + payamount.subtract(accountbalancerows.get(0).getBigDecimal("discountamount")) + ",paydiscountamount=" + accountbalancerows.get(0).getBigDecimal("discountamount") + " where st_stockbillid=" + st_stockbillid);
  300. }
  301. } else {
  302. dbConnect.runSqlUpdate("update st_stockbill set payamount=" + payamount + ",paydiscountamount=0 where st_stockbillid=" + st_stockbillid);
  303. }
  304. } else {
  305. dbConnect.runSqlUpdate("update st_stockbill set payamount=" + payamount + ",paydiscountamount=0 where st_stockbillid=" + st_stockbillid);
  306. }
  307. } else {
  308. dbConnect.runSqlUpdate("update st_stockbill set payamount=" + payamount + ",paydiscountamount=0 where st_stockbillid=" + st_stockbillid);
  309. }
  310. return querStcokbillItemsList();
  311. }
  312. @API(title = "出入库单明细列表", apiversion = R.ID20230719154303.v1.class)
  313. @CACHEING
  314. public String querStcokbillItemsList() throws YosException {
  315. /*
  316. * 过滤条件设置
  317. */
  318. StringBuffer where = new StringBuffer(" 1=1 ");
  319. if (content.containsKey("where")) {
  320. JSONObject whereObject = content.getJSONObject("where");
  321. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  322. where.append(" and(");
  323. where.append("t2.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  324. where.append("or t2.model like'%").append(whereObject.getString("condition")).append("%' ");
  325. where.append("or t2.spec like'%").append(whereObject.getString("condition")).append("%' ");
  326. where.append("or t2.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  327. where.append(")");
  328. }
  329. }
  330. Long st_stockbillid = content.getLong("st_stockbillid");
  331. Rows stockbillrows = dbConnect.runSqlQuery("select st_stockbillid,status,billno,rb,type from st_stockbill where st_stockbillid ='"
  332. + st_stockbillid + "' and siteid='" + siteid + "'");
  333. String type = "销售出库";
  334. if (stockbillrows.isNotEmpty()) {
  335. type = stockbillrows.get(0).getString("type");
  336. }
  337. SQLFactory sqlFactory;
  338. if (type.equals("销售出库")) {
  339. sqlFactory = new SQLFactory(this, "出入库单明细列表查询");
  340. } else {
  341. sqlFactory = new SQLFactory(this, "出入库单明细列表查询(红)");
  342. }
  343. sqlFactory.addParameter_SQL("where", where);
  344. sqlFactory.addParameter("st_stockbillid", st_stockbillid);
  345. sqlFactory.addParameter("siteid", siteid);
  346. // Rows rows = dbConnect.runSqlQuery(sqlFactory);
  347. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sys_site_parameter", "sys_site_parameterid");
  348. querySQL.setTableAlias("t0");
  349. querySQL.addJoinTable(JOINTYPE.right, sqlFactory, "t1", "t0.siteid='111'", "*");
  350. querySQL.setPage(pageSize, pageNumber);
  351. Rows rows = querySQL.query();
  352. Rows stockbillskuRows = dbConnect.runSqlQuery("select t1.sku,t1.st_stockbill_itemsid from st_stockbill_items_sku t1 left join sa_itemsku t2 on t1.sku=t2.sku where t1.st_stockbillid!=t1.st_stockbill_itemsid and t1.st_stockbillid=" + st_stockbillid);
  353. RowsMap stockbillskuRowsMap = stockbillskuRows.toRowsMap("st_stockbill_itemsid");
  354. for (Row row : rows) {
  355. if (stockbillskuRowsMap.containsKey(row.getString("st_stockbill_itemsid"))) {
  356. row.put("scanqty", stockbillskuRowsMap.get(row.getString("st_stockbill_itemsid")).size());
  357. } else {
  358. row.put("scanqty", 0);
  359. }
  360. }
  361. return getSucReturnObject().setData(rows).toString();
  362. }
  363. @API(title = "删除明细", apiversion = R.ID2025050614510403.v1.class)
  364. @CACHEING_CLEAN(apiClass = {stockbill.class, stockbillitems.class})
  365. public String deletemx() throws YosException {
  366. JSONArray st_stockbill_itemsids = content.getJSONArray("st_stockbill_itemsids");
  367. BatchDeleteErr batchDeleteErr = BatchDeleteErr.create(this, st_stockbill_itemsids.size());
  368. long st_stockbillid = 0;
  369. for (Object o : st_stockbill_itemsids) {
  370. long st_stockbill_itemsid = Long.parseLong(o.toString());
  371. Rows RowsStatus = dbConnect.runSqlQuery("select t1.st_stockbill_itemsid,t2.status,t1.st_stockbillid from st_stockbill_items t1 left join st_stockbill t2 on t1.st_stockbillid=t2.st_stockbillid and t1.siteid=t2.siteid where t1.siteid='"
  372. + siteid + "' and t1.st_stockbill_itemsid='" + st_stockbill_itemsid + "'");
  373. if (RowsStatus.isNotEmpty()) {
  374. st_stockbillid = RowsStatus.get(0).getLong("st_stockbillid");
  375. if (!RowsStatus.get(0).getString("status").equals("新建")) {
  376. batchDeleteErr.addErr(st_stockbill_itemsid, "非新建状态的出库单明细无法删除");
  377. continue;
  378. }
  379. }
  380. ArrayList<String> list = new ArrayList<>();
  381. list.add("delete from st_stockbill_items where siteid='" + siteid
  382. + "' and st_stockbill_itemsid=" + st_stockbill_itemsid);
  383. list.add("delete from st_stockbill_items_sku where siteid='" + siteid
  384. + "' and st_stockbill_itemsid=" + st_stockbill_itemsid);
  385. dbConnect.runSqlUpdate(list);
  386. }
  387. //重新排序
  388. updateRowNo(st_stockbillid);
  389. Rows rowsdetail = dbConnect.runSqlQuery("select amount,qty from st_stockbill_items where st_stockbillid=" + st_stockbillid);
  390. Rows stockbillrows = dbConnect.runSqlQuery("select t1.sys_enterpriseid,t1.paydiscountamount,t2.depname from st_stockbill t1 left join sys_department t2 on t1.departmentid=t2.departmentid where t1.st_stockbillid=" + st_stockbillid);
  391. Rows accountbalancerows = dbConnect.runSqlQuery("select * from sa_accountbalance t1 inner join sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid where t2.accountname='现金账户' and t1.sys_enterpriseid=" + stockbillrows.get(0).getLong("sys_enterpriseid"));
  392. String depname = stockbillrows.get(0).getString("depname");
  393. BigDecimal payamount = rowsdetail.sum("amount");
  394. if (depname.equals("售后部")) {
  395. if (accountbalancerows.isNotEmpty()) {
  396. if (accountbalancerows.get(0).getBigDecimal("discountamount").compareTo(BigDecimal.ZERO) > 0) {
  397. if (accountbalancerows.get(0).getBigDecimal("discountamount").compareTo(payamount) > 0) {
  398. dbConnect.runSqlUpdate("update st_stockbill set payamount=0,paydiscountamount=" + payamount + " where st_stockbillid=" + st_stockbillid);
  399. } else {
  400. dbConnect.runSqlUpdate("update st_stockbill set payamount=" + payamount.subtract(accountbalancerows.get(0).getBigDecimal("discountamount")) + ",paydiscountamount=" + accountbalancerows.get(0).getBigDecimal("discountamount") + " where st_stockbillid=" + st_stockbillid);
  401. }
  402. } else {
  403. dbConnect.runSqlUpdate("update st_stockbill set payamount=" + payamount + ",paydiscountamount=0 where st_stockbillid=" + st_stockbillid);
  404. }
  405. } else {
  406. dbConnect.runSqlUpdate("update st_stockbill set payamount=" + payamount + ",paydiscountamount=0 where st_stockbillid=" + st_stockbillid);
  407. }
  408. } else {
  409. dbConnect.runSqlUpdate("update st_stockbill set payamount=" + payamount + ",paydiscountamount=0 where st_stockbillid=" + st_stockbillid);
  410. }
  411. return batchDeleteErr.getReturnObject().toString();
  412. }
  413. /**
  414. * 重新对发货单行进行排序
  415. *
  416. * @param
  417. * @throws YosException
  418. */
  419. public void updateRowNo(Long st_stockbillid) throws YosException {
  420. String sql = "SELECT st_stockbill_itemsid from st_stockbill_items WHERE st_stockbillid= " + st_stockbillid + " and siteid = '" + siteid + "' ORDER BY st_stockbill_itemsid asc ";
  421. ArrayList<Long> st_stockbill_itemsids = dbConnect.runSqlQuery(sql).toArrayList("st_stockbill_itemsid", new ArrayList<>());
  422. ArrayList<String> sqlList = new ArrayList<>();
  423. int rowno = 1;
  424. for (Long id : st_stockbill_itemsids) {
  425. sqlList.add("UPDATE st_stockbill_items SET rowno=" + rowno + " WHERE st_stockbill_itemsid = " + id);
  426. rowno++;
  427. }
  428. dbConnect.runSqlUpdate(sqlList);
  429. }
  430. }