OrderItems.java 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. package restcontroller.webmanage.sale.order;
  2. import beans.accountbalance.Accountbalance;
  3. import beans.accountbalance.CashbillEntity;
  4. import beans.brand.Brand;
  5. import beans.customscheme.CustomScheme;
  6. import beans.datacontrllog.DataContrlLog;
  7. import beans.enterprise.Enterprise;
  8. import beans.itemprice.ItemPrice;
  9. import beans.parameter.Parameter;
  10. import beans.uploaderpdata.UploadDataToERP;
  11. import com.alibaba.fastjson.JSONArray;
  12. import com.alibaba.fastjson.JSONObject;
  13. import common.Controller;
  14. import common.DataFunction;
  15. import common.YosException;
  16. import common.annotation.API;
  17. import common.annotation.CACHEING;
  18. import common.annotation.CACHEING_CLEAN;
  19. import common.data.*;
  20. import io.reactivex.internal.observers.ForEachWhileObserver;
  21. import org.apache.commons.lang.StringUtils;
  22. import restcontroller.R;
  23. import restcontroller.webmanage.sale.aftersalesmag.twriteoffbill_orderchange;
  24. import restcontroller.webmanage.sale.rebate.Rebate;
  25. import restcontroller.webmanage.sale.toolbill.ToolBill;
  26. import utility.ERPDocking;
  27. import java.io.IOException;
  28. import java.math.BigDecimal;
  29. import java.util.ArrayList;
  30. import java.util.HashMap;
  31. import static beans.order.Order.*;
  32. /**
  33. * 销售订单商品明细表
  34. */
  35. public class OrderItems extends Controller {
  36. /**
  37. * 构造函数
  38. *
  39. * @param content
  40. */
  41. public OrderItems(JSONObject content) throws YosException {
  42. super(content);
  43. }
  44. @API(title = "新增或更新", apiversion = R.ID20221109093602.v1.class, intervaltime = 200)
  45. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class})
  46. public String insertOrUpdate() throws YosException {
  47. Long sa_orderid = content.getLong("sa_orderid");
  48. Long sys_enterpriseid = content.getLong("sys_enterpriseid");
  49. JSONArray items = content.getJSONArray("items");
  50. //辅助类
  51. OrderItemsHelper orderItemsHelper = new OrderItemsHelper(this);
  52. ArrayList<String> sqlList = new ArrayList<>();
  53. Rows orderRows = getOrderRows(this, sa_orderid);
  54. if (orderRows.isEmpty()) {
  55. return getErrReturnObject().setErrMsg("数据不存在,无法添加").toString();
  56. } else {
  57. if (!orderRows.get(0).getString("status").equals("新建") && !orderRows.get(0).getString("status").equals("预提交")) {
  58. return getErrReturnObject().setErrMsg("非新建或预提交状态无法操作").toString();
  59. }
  60. }
  61. //通过版本更新订单表头数据
  62. beans.order.Order.updateOrderWithVersion(this);
  63. String type = orderRows.get(0).getString("type");
  64. Long sa_promotionid = orderRows.get(0).getLong("sa_promotionid");
  65. //查询活动
  66. Rows promotionitems = dbConnect.runSqlQuery("select * from sa_promotion_items where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  67. //批量查询商品信息
  68. RowsMap itemRowsMap = orderItemsHelper.getItemRowsMap(items);
  69. BigDecimal sumqty = BigDecimal.ZERO;
  70. for (Object obj : items) {
  71. JSONObject item = (JSONObject) obj;
  72. Long itemid = item.getLong("itemid");
  73. Long sa_orderitemsid = item.getLong("sa_orderitemsid");
  74. BigDecimal qty = item.getBigDecimalValue("qty");
  75. sumqty = sumqty.add(qty);
  76. //商品信息
  77. Row itemRow = getItemRow(itemid);
  78. //是否符合商品起订量和增量判断
  79. orderItemsHelper.compareQty(type, itemRowsMap, item, itemRow, promotionitems);
  80. BigDecimal price1 = new BigDecimal(0);
  81. // SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_新增");
  82. Boolean isInsert = true;
  83. JSONArray customproperties = new JSONArray();
  84. String spec = "";
  85. if (sa_orderitemsid <= 0) {
  86. //获取定制属性
  87. customproperties = orderItemsHelper.getCustomProperties(itemRowsMap, item);
  88. if (itemRow.getString("iscustomsize").equals("1") && customproperties.isEmpty()) {
  89. return getErrReturnObject().setErrMsg(itemRow.getString("itemno") + "的定制信息为空,无法添加").toString();
  90. }
  91. spec = orderItemsHelper.getCustomSpec(itemRowsMap, item);
  92. Rows rows = dbConnect.runSqlQuery("SELECT sa_orderitemsid,price from sa_orderitems WHERE siteid='" + siteid + "' and itemid=" + itemid + " and sa_orderid=" + sa_orderid + " and sa_promotion_itemsid=" + item.getLongValue("sa_promotion_itemsid"));
  93. if (rows.isEmpty()) {
  94. sa_orderitemsid = createTableID("sa_orderitems");
  95. } else {
  96. //System.out.println(beans.order.Order.getDefaultIsRepeatValue(siteid, type));
  97. if (beans.order.Order.getDefaultIsRepeatValue(siteid, type)) {
  98. sa_orderitemsid = createTableID("sa_orderitems");
  99. isInsert = true;
  100. } else {
  101. price1 = rows.get(0).getBigDecimal("price");
  102. sa_orderitemsid = rows.get(0).getLong("sa_orderitemsid");
  103. isInsert = false;
  104. }
  105. }
  106. } else {
  107. Rows rows = dbConnect.runSqlQuery("SELECT sa_orderitemsid,price from sa_orderitems WHERE siteid='" + siteid + "' and sa_orderitemsid=" + sa_orderitemsid);
  108. if (rows.isNotEmpty()) {
  109. price1 = rows.get(0).getBigDecimal("price");
  110. }
  111. isInsert = false;
  112. }
  113. BigDecimal defaultprice;
  114. BigDecimal price;
  115. BigDecimal saleprice;
  116. //价格
  117. ItemPrice itemPrice = ItemPrice.getItemPrice(this, sys_enterpriseid, itemid);
  118. if (!item.containsKey("defaultprice")) {
  119. defaultprice = orderItemsHelper.getDefaultprice(itemPrice);
  120. price = orderItemsHelper.getPrice(orderRows.get(0), itemPrice, item, itemRowsMap);
  121. saleprice = orderItemsHelper.getSalePrice(orderRows.get(0), itemPrice);
  122. } else {
  123. defaultprice = item.getBigDecimalValue("defaultprice");
  124. price = item.getBigDecimalValue("price");
  125. saleprice = orderItemsHelper.getSalePrice(orderRows.get(0), itemPrice);
  126. if (price.compareTo(price1) != 0) {
  127. sqlList.add(DataContrlLog.createLog(this, "sa_order", sa_orderid, "订单商品【" + itemRow.getString("itemname") + "】由单价:" + price1 + "修改为单价:" + price, "订单修改单价").getSQL());
  128. }
  129. }
  130. if (price.compareTo(BigDecimal.ZERO) < 0) {
  131. return getErrReturnObject().setErrMsg("价格不可小于0").toString();
  132. }
  133. if (isInsert) {
  134. sqlList.add(orderItemsHelper.getInsertSQL(customproperties, sa_orderitemsid, item, itemRow, type, defaultprice, price, saleprice, itemPrice.getMarketprice(), spec, promotionitems).getSQL());
  135. } else {
  136. sqlList.add(orderItemsHelper.getUpdateSQL(sa_orderitemsid, item, itemRow, type, defaultprice, price, saleprice).getSQL());
  137. }
  138. }
  139. if (type.equals("促销订单")) {
  140. Rows promotionRows = dbConnect.runSqlQuery("select * from sa_promotion where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  141. if (!promotionRows.isEmpty()) {
  142. if (promotionRows.get(0).getString("type").equals("打包促销")) {
  143. BigDecimal orderaddqty = promotionRows.get(0).getBigDecimal("orderaddqty");
  144. BigDecimal orderminqty = promotionRows.get(0).getBigDecimal("orderminqty");
  145. if (((sumqty.subtract(orderminqty)).remainder(orderaddqty)).compareTo(BigDecimal.ZERO) != 0 || sumqty.compareTo(orderminqty) < 0) {
  146. return getErrReturnObject().setErrMsg("不符合促销活动订购数量规则,修改不成功").toString();
  147. }
  148. }
  149. }
  150. }
  151. dbConnect.runSqlUpdate(sqlList);
  152. //重新排序
  153. updateRowNo(sa_orderid);
  154. //居间费设置
  155. updateRebateFee(sa_orderid);
  156. //调整价格(返利金)
  157. if (orderRows.get(0).getBoolean("rebate_used")) {
  158. setRebateAmount(this, sa_orderid, getMaxUsedRebateAmount(this, sa_orderid));
  159. adjustLastRebateAmount(this, sa_orderid);
  160. }
  161. //更新订单扣款信息
  162. updateAccountclassinfos(sa_orderid);
  163. if (systemclient.equalsIgnoreCase("wechatsaletool")) {
  164. Rows rows = dbConnect.runSqlQuery("SELECT sa_orderitemsid,sa_orderitems_v,itemid from sa_orderitems WHERE sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  165. return getSucReturnObject().setData(rows).toString();
  166. }
  167. //查询商品费用方案方法
  168. orderItemsHelper.executeDataFunction(sa_orderid);
  169. //生成摘要
  170. SQLFactory sqlFactory = new SQLFactory(this, "查询定制费用统计");
  171. sqlFactory.addParameter("sa_orderid", sa_orderid);
  172. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  173. if (rows.isNotEmpty()) {
  174. String remarks = StringUtils.join(rows.toArray("remarks"), ";");
  175. dbConnect.runSqlUpdate("UPDATE sa_order set abstract ='" + remarks + "' WHERE sa_orderid='" + sa_orderid + "'");
  176. }
  177. return getSucReturnObject().toString();
  178. }
  179. /**
  180. * 更新订单扣款信息
  181. */
  182. public String updateAccountclassinfos(Long sa_orderid) throws YosException {
  183. Rows orderRows = getOrderRows(this, sa_orderid);
  184. if (orderRows.isEmpty()) {
  185. return "订单不存在";
  186. }
  187. String type = "";
  188. Rows promotionrows = dbConnect.runSqlQuery("select * from sa_promotion where siteid='" + siteid + "' and sa_promotionid=" + orderRows.get(0).getLong("sa_promotionid"));
  189. Rows promotionenterpriserows = dbConnect.runSqlQuery("select * from sa_promotion_auth where siteid='" + siteid + "'and sys_enterpriseid=" + orderRows.get(0).getLong("pay_enterpriseid") + " and sa_promotionid=" + orderRows.get(0).getLong("sa_promotionid"));
  190. if (promotionrows.isNotEmpty()) {
  191. type = promotionrows.get(0).getString("type");
  192. }
  193. JSONArray jsonArray = new JSONArray();
  194. BigDecimal orderamount = getAmount(this, sa_orderid);
  195. //System.out.println(orderamount);
  196. Rows accountclassrows = dbConnect.runSqlQuery("select t1.*,ifnull(t2.balance,0) balance,ifnull(t2.creditquota,0) creditquota from sa_accountclass t1 left join sa_accountbalance t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid and t2.sys_enterpriseid=" + orderRows.get(0).getLong("pay_enterpriseid") + " where isused=1 and t1.siteid='" + siteid + "' and t1.sa_accountclassid=" + orderRows.get(0).getLong("sa_accountclassid"));
  197. if (accountclassrows.isEmpty()) {
  198. return "未找到扣款账户";
  199. }
  200. JSONObject jsonObject = new JSONObject();
  201. if (type.equals("返利促销")) {
  202. BigDecimal associationamount = promotionrows.get(0).getBigDecimal("associationamount");
  203. long associationaccountclassid = promotionrows.get(0).getLong("associationaccountclassid");
  204. Rows associationaccountclassrows = dbConnect.runSqlQuery("select t1.*,ifnull(t2.balance,0) balance,ifnull(t2.creditquota,0) creditquota from sa_accountclass t1 left join sa_accountbalance t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid and t2.sys_enterpriseid=" + orderRows.get(0).getLong("pay_enterpriseid") + " where isused=1 and t1.siteid='" + siteid + "' and t1.sa_accountclassid=" + associationaccountclassid);
  205. if (promotionenterpriserows.isNotEmpty()) {
  206. if (promotionenterpriserows.get(0).getBoolean("islimit")) {
  207. BigDecimal limitamount = promotionenterpriserows.get(0).getBigDecimal("limitamount");
  208. BigDecimal saledamount = promotionenterpriserows.get(0).getBigDecimal("saledamount");
  209. if (orderamount.compareTo(limitamount.subtract(saledamount)) <= 0) {
  210. //全部优先账户扣款
  211. jsonObject.put("amount", orderamount);
  212. jsonObject.put("sa_accountclassid", accountclassrows.get(0).getLong("sa_accountclassid"));
  213. jsonObject.put("accountname", accountclassrows.get(0).getString("accountname"));
  214. jsonObject.put("balance", accountclassrows.get(0).getBigDecimal("balance"));
  215. jsonObject.put("creditquota", accountclassrows.get(0).getBigDecimal("creditquota"));
  216. jsonArray.add(jsonObject);
  217. } else if (orderamount.compareTo(limitamount.subtract(saledamount)) > 0) {
  218. //全部优先账户扣款
  219. jsonObject.put("amount", limitamount.subtract(saledamount));
  220. jsonObject.put("sa_accountclassid", accountclassrows.get(0).getLong("sa_accountclassid"));
  221. jsonObject.put("accountname", accountclassrows.get(0).getString("accountname"));
  222. jsonObject.put("balance", accountclassrows.get(0).getBigDecimal("balance"));
  223. jsonObject.put("creditquota", accountclassrows.get(0).getBigDecimal("creditquota"));
  224. jsonArray.add(jsonObject);
  225. JSONObject associationjsonObject = new JSONObject();
  226. associationjsonObject.put("amount", orderamount.subtract((limitamount.subtract(saledamount))));
  227. associationjsonObject.put("sa_accountclassid", associationaccountclassrows.get(0).getLong("sa_accountclassid"));
  228. associationjsonObject.put("accountname", associationaccountclassrows.get(0).getString("accountname"));
  229. associationjsonObject.put("balance", associationaccountclassrows.get(0).getBigDecimal("balance"));
  230. associationjsonObject.put("creditquota", associationaccountclassrows.get(0).getBigDecimal("creditquota"));
  231. jsonArray.add(associationjsonObject);
  232. } else {
  233. return "未知错误,请联系管理员";
  234. }
  235. }
  236. } else {
  237. //全部优先账户扣款
  238. jsonObject.put("amount", orderamount);
  239. jsonObject.put("sa_accountclassid", accountclassrows.get(0).getLong("sa_accountclassid"));
  240. jsonObject.put("accountname", accountclassrows.get(0).getString("accountname"));
  241. jsonObject.put("balance", accountclassrows.get(0).getBigDecimal("balance"));
  242. jsonObject.put("creditquota", accountclassrows.get(0).getBigDecimal("creditquota"));
  243. jsonArray.add(jsonObject);
  244. }
  245. } else {
  246. //全部优先账户扣款
  247. jsonObject.put("amount", orderamount);
  248. jsonObject.put("sa_accountclassid", accountclassrows.get(0).getLong("sa_accountclassid"));
  249. jsonObject.put("accountname", accountclassrows.get(0).getString("accountname"));
  250. jsonObject.put("balance", accountclassrows.get(0).getBigDecimal("balance"));
  251. jsonObject.put("creditquota", accountclassrows.get(0).getBigDecimal("creditquota"));
  252. jsonArray.add(jsonObject);
  253. }
  254. //System.out.println(jsonArray.toJSONString());
  255. dbConnect.runSqlUpdate("update sa_order set sa_accountclassinfos='" + jsonArray + "' where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  256. return "true";
  257. }
  258. /**
  259. * 更新居间费信息
  260. */
  261. public void updateRebateFee(Long sa_orderid) throws YosException {
  262. Rows orderRows = getOrderRows(this, sa_orderid);
  263. if (orderRows.isEmpty()) {
  264. return;
  265. }
  266. Long sa_projectid = orderRows.get(0).getLong("sa_projectid");
  267. Rows contractRows = dbConnect.runSqlQuery("SELECT * from sa_contract WHERE sa_projectid = " + sa_projectid + " and type='居间' and status='审核'");
  268. if (contractRows.isEmpty()) {
  269. return;
  270. }
  271. Long sa_contractid = contractRows.get(0).getLong("sa_contractid");
  272. Rows partiesRows = dbConnect.runSqlQuery("SELECT sys_enterpriseid from sa_project_parties WHERE type ='居间服务商' and sa_projectid = " + sa_projectid + " and siteid ='" + siteid + "'");
  273. if (partiesRows.isEmpty()) {
  274. return;
  275. }
  276. Long sys_enterpriseid_temp = partiesRows.get(0).getLong("sys_enterpriseid");
  277. content.put("sys_enterpriseid", sys_enterpriseid_temp);
  278. content.put("ownertable", "sa_order");
  279. content.put("ownerid", sa_orderid);
  280. content.put("billdate", "");
  281. content.put("remarks", "");
  282. content.put("sa_contractid", sa_contractid);
  283. content.put("settlementmode", "线上");
  284. content.put("sa_rebatesettlementid", 0);
  285. new Rebate(content).insertOrUpdate();
  286. }
  287. @API(title = "详情", apiversion = R.ID20221110134302.v1.class)
  288. @CACHEING
  289. public String selectDetail() throws YosException {
  290. Long sa_orderitemsid = content.getLong("sa_orderitemsid");
  291. SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_详情");
  292. sqlFactory.addParameter("siteid", siteid);
  293. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsid);
  294. String sql = sqlFactory.getSQL();
  295. Rows rows = dbConnect.runSqlQuery(sql);
  296. Row row = rows.isNotEmpty() ? rows.get(0) : new Row();
  297. return getSucReturnObject().setData(row).toString();
  298. }
  299. @API(title = "删除", apiversion = R.ID20221109093702.v1.class)
  300. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class})
  301. public String delete() throws YosException {
  302. Long sa_orderid = content.getLong("sa_orderid");
  303. Rows orderRows = getOrderRows(this, sa_orderid);
  304. if (orderRows.isEmpty()) {
  305. return getErrReturnObject().setErrMsg("数据不存在,无法添加").toString();
  306. } else {
  307. if (!orderRows.get(0).getString("status").equals("新建")) {
  308. return getErrReturnObject().setErrMsg("非新建状态无法删除").toString();
  309. }
  310. }
  311. //通过版本更新订单表头数据
  312. beans.order.Order.updateOrderWithVersion(this);
  313. JSONArray sa_orderitemsids = content.getJSONArray("sa_orderitemsids");
  314. SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_删除");
  315. sqlFactory.addParameter("siteid", siteid);
  316. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsids.toArray());
  317. String sql = sqlFactory.getSQL();
  318. dbConnect.runSqlUpdate(sql);
  319. //重新排序
  320. updateRowNo(sa_orderid);
  321. //居间费设置
  322. updateRebateFee(sa_orderid);
  323. //调整价格(返利金)
  324. if (orderRows.get(0).getBoolean("rebate_used")) {
  325. setRebateAmount(this, sa_orderid, getMaxUsedRebateAmount(this, sa_orderid));
  326. adjustLastRebateAmount(this, sa_orderid);
  327. }
  328. // else {
  329. // dbConnect.runSqlUpdate("UPDATE sa_orderitems SET amount=amount-rebateamount,price=amount/qty WHERE sa_orderid='" + sa_orderid + "' and siteid='" + siteid + "' and qty!=0");
  330. // dbConnect.runSqlUpdate("UPDATE sa_orderitems SET amount=0 WHERE sa_orderid='" + sa_orderid + "' and siteid='" + siteid + "' and qty=0");
  331. // }
  332. updateAccountclassinfos(sa_orderid);
  333. return getSucReturnObject().toString();
  334. }
  335. @API(title = "商品行冻结", apiversion = R.ID20230508093003.v1.class)
  336. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class})
  337. public String freeze() throws YosException {
  338. Long sa_orderid = content.getLong("sa_orderid");
  339. //通过版本更新订单表头数据
  340. beans.order.Order.updateOrderWithVersion(this);
  341. JSONArray sa_orderitemsids = content.getJSONArray("sa_orderitemsids");
  342. boolean isfreeze = content.getBooleanValue("isfreeze");
  343. Rows orderRows = dbConnect.runSqlQuery("select * from sa_order where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  344. if (!orderRows.isEmpty()) {
  345. if (!orderRows.get(0).getString("status").equals("审核")) {
  346. if (isfreeze) {
  347. return getErrReturnObject().setErrMsg("非审核状态无法进行冻结").toString();
  348. } else {
  349. return getErrReturnObject().setErrMsg("非审核状态无法进行反冻结").toString();
  350. }
  351. }
  352. } else {
  353. return getErrReturnObject().setErrMsg("该订单不存在").toString();
  354. }
  355. SQLFactory sqlFactory;
  356. if (isfreeze) {
  357. sqlFactory = new SQLFactory(this, "订单商品明细_冻结");
  358. } else {
  359. sqlFactory = new SQLFactory(this, "订单商品明细_反冻结");
  360. }
  361. sqlFactory.addParameter("siteid", siteid);
  362. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsids.toArray());
  363. String sql = sqlFactory.getSQL();
  364. //System.out.println(sql);
  365. dbConnect.runSqlUpdate(sql);
  366. return getSucReturnObject().toString();
  367. }
  368. @API(title = "商品行折扣变更", apiversion = R.ID20230508101703.v1.class)
  369. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class})
  370. public String updateOrderItemsDiscountrate() throws YosException {
  371. Long sa_orderid = content.getLong("sa_orderid");
  372. BigDecimal discountrate = content.getBigDecimal("discountrate");
  373. JSONArray itemids = content.getJSONArray("itemids");
  374. Rows orderRows = dbConnect.runSqlQuery("select * from sa_order where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  375. if (!orderRows.isEmpty()) {
  376. if (!orderRows.get(0).getString("status").equals("新建") && !orderRows.get(0).getString("status").equals("预提交")) {
  377. return getErrReturnObject().setErrMsg("非新建或预提交状态无法进行商品行折扣变更").toString();
  378. }
  379. } else {
  380. return getErrReturnObject().setErrMsg("该订单不存在").toString();
  381. }
  382. if (!(discountrate.compareTo(BigDecimal.ZERO) > 0 && discountrate.compareTo(new BigDecimal(1)) <= 0)) {
  383. return getErrReturnObject().setErrMsg("折扣必须在0-1之间").toString();
  384. }
  385. //通过版本更新订单表头数据
  386. beans.order.Order.updateOrderWithVersion(this);
  387. String sql;
  388. if (itemids.size() == 0) {
  389. sql = "select * from sa_orderitems where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'";
  390. } else {
  391. sql = "select * from sa_orderitems where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "' and itemid in " + itemids.toJSONString();
  392. sql = sql.replace("[", "(").replace("]", ")");
  393. }
  394. Rows orderItemsRows = dbConnect.runSqlQuery(sql);
  395. ArrayList<String> sqlList = new ArrayList<>();
  396. if (!orderItemsRows.isEmpty()) {
  397. for (Row row : orderItemsRows) {
  398. BigDecimal price = row.getBigDecimal("price").multiply(discountrate);
  399. BigDecimal amount = row.getBigDecimal("amount").multiply(discountrate);
  400. sqlList.add("update sa_orderitems set price=" + price + ",amount=" + amount + " where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  401. }
  402. }
  403. sqlList.add(DataContrlLog.createLog(this, "sa_order", sa_orderid, "订单整单折扣变更【" + discountrate + "】", "整单折扣").getSQL());
  404. dbConnect.runSqlUpdate(sqlList);
  405. updateAccountclassinfos(sa_orderid);
  406. return getSucReturnObject().toString();
  407. }
  408. @API(title = "商品行价格刷新(变为原价)", apiversion = R.ID20230518151803.v1.class)
  409. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class})
  410. public String updateOrderItemsPrice() throws YosException {
  411. Long sa_orderid = content.getLong("sa_orderid");
  412. JSONArray itemids = content.getJSONArray("itemids");
  413. Rows orderRows = dbConnect.runSqlQuery("select * from sa_order where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  414. if (!orderRows.isEmpty()) {
  415. if (!orderRows.get(0).getString("status").equals("新建") && !orderRows.get(0).getString("status").equals("预提交")) {
  416. return getErrReturnObject().setErrMsg("非新建或预提交状态无法进行商品行价格刷新").toString();
  417. }
  418. } else {
  419. return getErrReturnObject().setErrMsg("该订单不存在").toString();
  420. }
  421. //通过版本更新订单表头数据
  422. beans.order.Order.updateOrderWithVersion(this);
  423. String sql;
  424. if (itemids.size() == 0) {
  425. sql = "select * from sa_orderitems where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'";
  426. } else {
  427. sql = "select * from sa_orderitems where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "' and itemid in " + itemids.toJSONString();
  428. sql = sql.replace("[", "(").replace("]", ")");
  429. }
  430. Rows orderItemsRows = dbConnect.runSqlQuery(sql);
  431. ArrayList<String> sqlList = new ArrayList<>();
  432. if (!orderItemsRows.isEmpty()) {
  433. for (Row row : orderItemsRows) {
  434. //价格
  435. ItemPrice itemPrice = ItemPrice.getItemPrice(this, orderRows.get(0).getLong("sys_enterpriseid"), row.getLong("itemid"));
  436. BigDecimal price = itemPrice.getPrice();
  437. BigDecimal amount = row.getBigDecimal("qty").multiply(price);
  438. sqlList.add("update sa_orderitems set price=" + price + ",amount=" + amount + " where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  439. }
  440. }
  441. sqlList.add(DataContrlLog.createLog(this, "sa_order", sa_orderid, "订单商品行价格刷新(恢复原价)", "订单商品行价格刷新").getSQL());
  442. dbConnect.runSqlUpdate(sqlList);
  443. updateAccountclassinfos(sa_orderid);
  444. return getSucReturnObject().toString();
  445. }
  446. @API(title = "关闭", apiversion = R.ID20221109093802.v1.class)
  447. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class})
  448. public String close() throws YosException {
  449. Long sa_orderid = content.getLong("sa_orderid");
  450. //通过版本更新订单表头数据
  451. beans.order.Order.updateOrderWithVersion(this);
  452. JSONArray sa_orderitemsids = content.getJSONArray("sa_orderitemsids");
  453. SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_行关闭");
  454. sqlFactory.addParameter("siteid", siteid);
  455. sqlFactory.addParameter("userid", userid);
  456. sqlFactory.addParameter("username", username);
  457. sqlFactory.addParameter("closereason", content.getStringValue("closereason"));
  458. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsids.toArray());
  459. dbConnect.runSqlUpdate(sqlFactory);
  460. //重新排序
  461. updateRowNo(sa_orderid);
  462. return getSucReturnObject().toString();
  463. }
  464. @API(title = "查询列表", apiversion = R.ID20221109093902.v1.class)
  465. public String selectList() throws YosException, IOException {
  466. StringBuffer where = new StringBuffer(" 1=1 ");
  467. if (content.containsKey("where")) {
  468. JSONObject whereObject = content.getJSONObject("where");
  469. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  470. where.append(" and(");
  471. where.append("t1.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  472. where.append("or t1.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  473. where.append("or t1.model like'%").append(whereObject.getString("condition")).append("%' ");
  474. where.append("or t3.spec like'%").append(whereObject.getString("condition")).append("%' ");
  475. where.append("or t1.remarks like'%").append(whereObject.getString("condition")).append("%' ");
  476. where.append(")");
  477. }
  478. }
  479. Long sa_orderid = content.getLong("sa_orderid");
  480. Rows orderRows = getOrderRows(this, sa_orderid);
  481. if (orderRows.isNotEmpty()) {
  482. sys_enterpriseid = orderRows.get(0).getLong("sys_enterpriseid");
  483. }
  484. Long sa_promotionid = orderRows.get(0).getLong("sa_promotionid");
  485. // boolean isExport = content.getBooleanValue("isExport");
  486. // SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_列表", pageSize, pageNumber, pageSorting);
  487. // if (isExport) {
  488. // sqlFactory = new SQLFactory(this, "订单商品明细_列表");
  489. // }
  490. // sqlFactory.addParameter("siteid", siteid);
  491. // sqlFactory.addParameter("sa_orderid", sa_orderid);
  492. // sqlFactory.addParameter_SQL("where", where);
  493. // Rows rows = dbConnect.runSqlQuery(sqlFactory);
  494. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_orderitems");
  495. querySQL.addJoinTable(JOINTYPE.left, "st_invbal_sale", "t2", "t2.siteid = t1.siteid AND t2.itemid = t1.itemid");
  496. querySQL.addJoinTable(JOINTYPE.left, "plm_item", "t3", "t3.itemid = t1.itemid and t3.siteid = t1.siteid",
  497. "orderminqty_auxunit", "orderaddqty_auxunit", "spec", "iscustomsize", "widthschemeid", "lengthschemeid", "standards", "k3outcode");
  498. // querySQL.addJoinTable(JOINTYPE.left, "plm_itemextend", "t4", "t4.itemid = t1.itemid and t4.siteid = t1.siteid",
  499. // "erpitemno", "erpitemname", "material", "prodline", "device", "specalnote");
  500. querySQL.addJoinTable(JOINTYPE.left, "sa_promotion_items", "t4",
  501. "t4.itemid = t1.itemid and t4.siteid = t1.siteid and t4.sa_promotionid=" + sa_promotionid);
  502. querySQL.addQueryFields("candispatchqty", "ifnull(t2.candispatchqty, 0)");
  503. querySQL.addQueryFields("cansaleqty", "ifnull(t2.cansaleqty, 0)");
  504. querySQL.addQueryFields("totalaty", "ifnull(t2.qty, 0)");
  505. querySQL.addQueryFields("stockstatus", "(SELECT (CASE WHEN t2.cansaleqty>=t3.stockstatus1 THEN '充足' WHEN t2.cansaleqty<=t3.stockstatus2 THEN '缺货' ELSE '紧缺' END))");
  506. querySQL.addQueryFields("packageqty", "ifnull(t3.packageqty, 0)");
  507. querySQL.addQueryFields("delivery", "ifnull(t3.delivery, 0)");
  508. querySQL.addQueryFields("orderaddqty", "SELECT (CASE WHEN t4.sa_promotionid>0 THEN t4.orderaddqty ELSE t3.orderaddqty END)");
  509. querySQL.addQueryFields("orderminqty", "SELECT (CASE WHEN t4.sa_promotionid>0 THEN t4.orderminqty ELSE t3.orderminqty END)");
  510. querySQL.setWhere(where.toString()).setTableAlias("t1");
  511. querySQL.setWhere("t1.siteid", siteid);
  512. querySQL.setWhere("t1.sa_orderid", sa_orderid);
  513. querySQL.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
  514. Rows rows = querySQL.query();
  515. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  516. ArrayList<Long> itemnos = rows.toArrayList("itemno", new ArrayList<>());
  517. RowsMap rowsMap = rows.toRowsMap("itemno");
  518. SQLFactory sqlFactory1 = new SQLFactory(this, "商品发货数量汇总");
  519. sqlFactory1.addParameter("siteid", siteid);
  520. sqlFactory1.addParameter_in("itemnos", itemnos);
  521. Rows sumQtyRows = dbConnect.runSqlQuery(sqlFactory1);
  522. RowsMap sumQtyRowsMap = sumQtyRows.toRowsMap("itemno");
  523. SQLFactory sqlFactory2 = new SQLFactory(this, "商品未发货数量汇总");
  524. sqlFactory2.addParameter("siteid", siteid);
  525. sqlFactory2.addParameter_in("itemnos", itemnos);
  526. Rows sumUnQtyRows = dbConnect.runSqlQuery(sqlFactory2);
  527. RowsMap sumUnQtyRowsMap = sumUnQtyRows.toRowsMap("itemno");
  528. SQLFactory sqlFactory3 = new SQLFactory(this, "商品原价汇总");
  529. sqlFactory3.addParameter("siteid", siteid);
  530. sqlFactory3.addParameter("sys_enterpriseid", sys_enterpriseid);
  531. sqlFactory3.addParameter_in("itemnos", itemnos);
  532. Rows itempriceRows = dbConnect.runSqlQuery(sqlFactory3);
  533. RowsMap itempriceRowsMap = itempriceRows.toRowsMap("itemno");
  534. JSONArray jsonArray = new JSONArray();
  535. // JSONObject object = new JSONObject();
  536. // object.put("classname", "getIcinvbal");
  537. // object.put("method", "getMsg");
  538. //
  539. // JSONObject content = new JSONObject();
  540. // content.put("fitemnos", rows.toJsonArray("itemno"));
  541. // object.put("content", content);
  542. //
  543. // WebRequest request = new WebRequest();
  544. // String result = request.doPost(object.toString(),
  545. // "http://60.190.151.198:8089/BYESB/jaxrs/webclientrest");
  546. // if (isJSONArray(result)) {
  547. // jsonArray = JSONArray.parseArray(result);
  548. // }
  549. // for (Object obj : jsonArray) {
  550. // JSONObject jsonObject = (JSONObject) obj;
  551. // if (rowsMap.containsKey(jsonObject.getString("fitemno"))) {
  552. // if (rowsMap.get(jsonObject.getString("fitemno")).isNotEmpty()) {
  553. // rowsMap.get(jsonObject.getString("fitemno")).get(0).put("invbalqty", jsonObject.getBigDecimalValue("FQty"));
  554. // }
  555. // }
  556. // }
  557. ERPDocking erpDocking = new ERPDocking(siteid);
  558. if (rows.toJsonArray("itemno").size() != 0) {
  559. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true")) {
  560. jsonArray = erpDocking.getErpIcinvbalRows(1000, 1, rows.toJsonArray("itemno"));
  561. }
  562. }
  563. if (!jsonArray.isEmpty()) {
  564. for (Object object : jsonArray) {
  565. JSONObject jsonObject = (JSONObject) object;
  566. if (rowsMap.containsKey(jsonObject.getString("fitemno"))) {
  567. if (rowsMap.get(jsonObject.getString("fitemno")).isNotEmpty()) {
  568. for (Row row : rowsMap.get(jsonObject.getString("fitemno"))
  569. ) {
  570. row.put("invbalqty", jsonObject.getBigDecimalValue("fqty"));
  571. }
  572. }
  573. }
  574. }
  575. }
  576. //查询附件
  577. RowsMap attinfoRowsMap = getAttachmentUrl("plm_item", ids);
  578. for (Row row : rows) {
  579. if (itempriceRowsMap.get(row.getString("itemno")).isNotEmpty()) {
  580. row.put("originalprice", itempriceRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("price"));
  581. } else {
  582. row.put("originalprice", BigDecimal.ZERO);
  583. }
  584. if (sumQtyRowsMap.get(row.getString("itemno")).isNotEmpty()) {
  585. row.put("unsoldqty", sumQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("qty"));
  586. } else {
  587. row.put("unsoldqty", BigDecimal.ZERO);
  588. }
  589. if (sumUnQtyRowsMap.get(row.getString("itemno")).isNotEmpty()) {
  590. row.put("undeliqtysum", sumUnQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("undeliqty"));
  591. } else {
  592. row.put("undeliqtysum", BigDecimal.ZERO);
  593. }
  594. if (!row.containsKey("invbalqty")) {
  595. row.put("invbalqty", BigDecimal.ZERO);
  596. }
  597. row.put("attinfos", attinfoRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  598. row.put("contractprice", 0);
  599. //处理日期字段空的情况
  600. row.putIfAbsent("needdate", "");
  601. row.putIfAbsent("deliverydate", "");
  602. }
  603. // if (isExport) {
  604. // //去除不需要导出项
  605. // String[] removeFieldList = {"sa_orderitemsid", "itemid", "sa_orderid", "candispatchqty", "cansaleqty",
  606. // "totalaty", "orderminqty_auxunit", "orderminqty", "orderaddqty_auxunit", "orderaddqty",
  607. // "spec", "delivery", "erpitemno", "erpitemname", "material", "standards", "stockstatus"};
  608. // for (String key : removeFieldList) {
  609. // rows.getFieldList().remove(key);
  610. // }
  611. //
  612. // Rows uploadRows = uploadExcelToObs("order", "订单商品明细_列表", rows, getTitleMap());
  613. // return getSucReturnObject().setData(uploadRows).toString();
  614. // }
  615. return getSucReturnObject().setData(rows).toString();
  616. }
  617. @API(title = "查询订单所有明细列表", apiversion = R.ID20230508111703.v1.class)
  618. public String selectAllList() throws YosException, IOException {
  619. StringBuffer where = new StringBuffer(" 1=1 ");
  620. if (sys_enterpriseid > 0) {
  621. where.append(" and t5.sys_enterpriseid ='").append(sys_enterpriseid).append("' ");
  622. }
  623. if (content.containsKey("where")) {
  624. JSONObject whereObject = content.getJSONObject("where");
  625. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  626. where.append(" and(");
  627. where.append(" t5.sonum like'%").append(whereObject.getString("condition")).append("%' ");
  628. where.append("or t1.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  629. where.append("or t1.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  630. where.append("or t6.agentnum like'%").append(whereObject.getString("condition")).append("%' ");
  631. where.append("or t7.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  632. where.append("or t1.remarks like'%").append(whereObject.getString("condition")).append("%' ");
  633. where.append(")");
  634. }
  635. if (whereObject.containsKey("iteminfo") && !"".equals(whereObject.getString("iteminfo"))) {
  636. where.append(" and(");
  637. where.append("t1.itemno like'%").append(whereObject.getString("iteminfo")).append("%' ");
  638. where.append("or t1.itemname like'%").append(whereObject.getString("iteminfo")).append("%' ");
  639. where.append(")");
  640. }
  641. if (whereObject.containsKey("agentinfo") && !"".equals(whereObject.getString("agentinfo"))) {
  642. where.append(" and(");
  643. where.append("t6.agentnum like'%").append(whereObject.getString("agentinfo")).append("%' ");
  644. where.append("or t7.enterprisename like'%").append(whereObject.getString("agentinfo")).append("%' ");
  645. where.append(")");
  646. }
  647. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  648. where.append(" and DATE_FORMAT(t5.billdate, '%Y-%m-%d') >='").append(whereObject.getString("begindate")).append("' ");
  649. }
  650. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  651. where.append(" and DATE_FORMAT(t5.billdate, '%Y-%m-%d') <='").append(whereObject.getString("enddate")).append("' ");
  652. }
  653. if (whereObject.containsKey("begindate1") && !"".equals(whereObject.getString("begindate1"))) {
  654. where.append(" and DATE_FORMAT(t5.submitdate, '%Y-%m-%d') >='").append(whereObject.getString("begindate1")).append("' ");
  655. }
  656. if (whereObject.containsKey("enddate1") && !"".equals(whereObject.getString("enddate1"))) {
  657. where.append(" and DATE_FORMAT(t5.submitdate, '%Y-%m-%d') <='").append(whereObject.getString("enddate1")).append("' ");
  658. }
  659. if (whereObject.containsKey("begindate2") && !"".equals(whereObject.getString("begindate2"))) {
  660. where.append(" and DATE_FORMAT(t5.checkdate, '%Y-%m-%d') >='").append(whereObject.getString("begindate2")).append("' ");
  661. }
  662. if (whereObject.containsKey("enddate2") && !"".equals(whereObject.getString("enddate2"))) {
  663. where.append(" and DATE_FORMAT(t5.checkdate, '%Y-%m-%d') <='").append(whereObject.getString("enddate2")).append("' ");
  664. }
  665. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  666. where.append(" and t5.status ='").append(whereObject.getString("status")).append("' ");
  667. }
  668. if (whereObject.containsKey("isfreeze") && !"".equals(whereObject.getString("isfreeze"))) {
  669. where.append(" and t1.isfreeze ='").append(whereObject.getString("isfreeze")).append("' ");
  670. }
  671. if (whereObject.containsKey("typemx") && !"".equals(whereObject.getString("typemx"))) {
  672. where.append(" and t5.typemx ='").append(whereObject.getString("typemx")).append("' ");
  673. }
  674. if (whereObject.containsKey("isuncheckout") && !"".equals(whereObject.getString("isuncheckout"))) {
  675. String begindate = "";
  676. String enddate = "";
  677. if (whereObject.containsKey("datetype") && !"".equals(whereObject.getString("datetype"))) {
  678. String datetype = whereObject.getStringValue("datetype");
  679. switch (datetype) {
  680. case "7":
  681. begindate = getDate_Str(-7);
  682. enddate = getDate_Str();
  683. break;
  684. case "30":
  685. begindate = getDate_Str(-30);
  686. enddate = getDate_Str();
  687. break;
  688. case "90":
  689. begindate = getDate_Str(-90);
  690. enddate = getDate_Str();
  691. break;
  692. default:
  693. break;
  694. }
  695. }
  696. if (!begindate.equals("")) {
  697. where.append(" and DATE_FORMAT(t5.submitdate, '%Y-%m-%d') >='").append(begindate).append("' ");
  698. }
  699. if (!enddate.equals("")) {
  700. where.append(" and DATE_FORMAT(t5.submitdate, '%Y-%m-%d') <='").append(enddate).append("' ");
  701. }
  702. String isuncheckout = whereObject.getStringValue("isuncheckout");
  703. if (isuncheckout.equals("1")) {
  704. where.append(" and t1.sa_orderitemsid in (select t1.sa_orderitemsid from sa_orderitems t1 inner join (select t2.sa_orderitemsid,t2.siteid,sum(outwarehouseqty) sumoutwarehouseqty from sa_dispatch_items t2 inner join sa_dispatch t3 on t2.sa_dispatchid=t3.sa_dispatchid and t2.siteid=t3.siteid GROUP BY sa_orderitemsid,siteid) t2 on t1.sa_orderitemsid=t2.sa_orderitemsid and t1.siteid=t2.siteid where t1.qty>ifnull(t2.sumoutwarehouseqty,0) or t1.undeliqty>0)");
  705. } else {
  706. where.append(" and t1.sa_orderitemsid not in (select t1.sa_orderitemsid from sa_orderitems t1 inner join (select t2.sa_orderitemsid,t2.siteid,sum(outwarehouseqty) sumoutwarehouseqty from sa_dispatch_items t2 inner join sa_dispatch t3 on t2.sa_dispatchid=t3.sa_dispatchid and t2.siteid=t3.siteid GROUP BY sa_orderitemsid,siteid) t2 on t1.sa_orderitemsid=t2.sa_orderitemsid and t1.siteid=t2.siteid where t1.qty>ifnull(t2.sumoutwarehouseqty,0) or t1.undeliqty>0)");
  707. }
  708. }
  709. if (whereObject.containsKey("iscancel") && !"".equals(whereObject.getString("iscancel"))) {
  710. String iscancel = whereObject.getStringValue("iscancel");
  711. if (iscancel.equals("1")) {
  712. where.append(" and t5.status ='手工关闭' and not exists(select 1 from st_stockbill_items s1 inner join st_stockbill s2 on s1.st_stockbillid=s2.st_stockbillid and s1.siteid=s2.siteid where s2.status='审核' and s2.rb=1 and s1.sa_orderitemsid=t1.sa_orderitemsid)");
  713. }
  714. }
  715. if (whereObject.containsKey("convenient") && !"".equals(whereObject.getString("convenient"))) {
  716. String convenient = whereObject.getStringValue("convenient");
  717. if (convenient.equals("待确认")) {
  718. where.append(" and t5.status ='预提交' ");
  719. } else if (convenient.equals("待审核")) {
  720. where.append(" and t5.status ='提交' ");
  721. } else if (convenient.equals("待发货")) {
  722. where.append(" and t5.status in ('审核') and not exists(select 1 from st_stockbill_items s1 inner join st_stockbill s2 on s1.st_stockbillid=s2.st_stockbillid and s1.siteid=s2.siteid where s2.status='审核' and s2.rb=1 and s1.sa_orderitemsid=t1.sa_orderitemsid)");
  723. } else if (convenient.equals("已发货")) {
  724. where.append(" and exists(select 1 from st_stockbill_items s1 inner join st_stockbill s2 on s1.st_stockbillid=s2.st_stockbillid and s1.siteid=s2.siteid where s2.status='审核' and s2.rb=1 and s1.sa_orderitemsid=t1.sa_orderitemsid)");
  725. } else if (convenient.equals("已取消")) {
  726. where.append(" and t5.status ='手工关闭' and exists(select 1 from (\n" +
  727. "select s1.sa_orderitemsid,s1.siteid,sum(s1.qty) qty from st_stockbill_items s1 inner join st_stockbill s2 on s1.st_stockbillid=s2.st_stockbillid and s1.siteid=s2.siteid where s2.status='审核' and s2.rb=1 group by s1.sa_orderitemsid,s1.siteid) s right join sa_orderitems s1 on s.sa_orderitemsid=s1.sa_orderitemsid and s.siteid=s1.siteid where s1.qty>ifnull(s.qty,0) and s1.sa_orderitemsid=t1.sa_orderitemsid)");
  728. }
  729. }
  730. }
  731. boolean isExport = content.getBooleanValue("isExport");
  732. // SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_所有列表", pageSize, pageNumber, pageSorting);
  733. // if (isExport) {
  734. // sqlFactory = new SQLFactory(this, "订单商品明细_所有列表");
  735. // }
  736. // sqlFactory.addParameter("siteid", siteid);
  737. // sqlFactory.addParameter_SQL("where", where);
  738. // Rows rows = dbConnect.runSqlQuery(sqlFactory);
  739. QuerySQL querySQL = queryAllListManage(where.toString());
  740. querySQL.setOrderBy(pageSorting);
  741. querySQL.setPage(pageSize, pageNumber);
  742. Rows rows = querySQL.query();
  743. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  744. RowsMap rowsMap = rows.toRowsMap("itemno");
  745. SQLFactory sqlFactory1 = new SQLFactory(this, "订单明细出库数量汇总");
  746. sqlFactory1.addParameter("siteid", siteid);
  747. sqlFactory1.addParameter_in("sa_orderitemsids", rows.toArrayList("sa_orderitemsid", new ArrayList<>()));
  748. Rows sumQtyRows = dbConnect.runSqlQuery(sqlFactory1);
  749. RowsMap sumQtyRowsMap = sumQtyRows.toRowsMap("sa_orderitemsid");
  750. ERPDocking erpDocking = new ERPDocking(siteid);
  751. JSONArray jsonArray = new JSONArray();
  752. if (rows.toJsonArray("itemno").size() != 0) {
  753. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true")) {
  754. jsonArray = erpDocking.getErpIcinvbalRows(200000, 1, rows.toJsonArray("itemno"));
  755. }
  756. }
  757. if (!jsonArray.isEmpty()) {
  758. for (Object object : jsonArray) {
  759. JSONObject jsonObject = (JSONObject) object;
  760. if (rowsMap.containsKey(jsonObject.getString("fitemno"))) {
  761. if (rowsMap.get(jsonObject.getString("fitemno")).isNotEmpty()) {
  762. for (Row row : rowsMap.get(jsonObject.getString("fitemno"))
  763. ) {
  764. row.put("invbalqty", jsonObject.getBigDecimalValue("fqty"));
  765. }
  766. }
  767. }
  768. }
  769. }
  770. //查询附件
  771. RowsMap attinfoRowsMap = getAttachmentUrl("plm_item", ids);
  772. for (Row row : rows) {
  773. if (sumQtyRowsMap.get(row.getString("sa_orderitemsid")).isNotEmpty()) {
  774. row.put("unsoldqty", row.getBigDecimal("qty").subtract(sumQtyRowsMap.get(row.getString("sa_orderitemsid")).get(0).getBigDecimal("sumqty")));
  775. } else {
  776. row.put("unsoldqty", row.getBigDecimal("qty"));
  777. }
  778. if (!row.containsKey("invbalqty")) {
  779. row.put("invbalqty", BigDecimal.ZERO);
  780. }
  781. row.put("attinfos", attinfoRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  782. row.put("contractprice", 0);
  783. }
  784. // if (isExport) {
  785. // //去除不需要导出项
  786. // rows.getFieldList().remove("sa_orderitemsid");
  787. // rows.getFieldList().remove("itemid");
  788. // rows.getFieldList().remove("batchcontrol");
  789. // rows.getFieldList().remove("model");
  790. // rows.getFieldList().remove("conversionrate");
  791. // rows.getFieldList().remove("marketprice");
  792. // rows.getFieldList().remove("defaultprice");
  793. // rows.getFieldList().remove("defaultamount");
  794. // rows.getFieldList().remove("auxqty");
  795. // rows.getFieldList().remove("needdate");
  796. // rows.getFieldList().remove("deliedqty");
  797. // rows.getFieldList().remove("rebateamount");
  798. // rows.getFieldList().remove("invoiceamount");
  799. // rows.getFieldList().remove("writeoffamount");
  800. // rows.getFieldList().remove("auxunit");
  801. // rows.getFieldList().remove("sa_orderid");
  802. // rows.getFieldList().remove("deliverydate");
  803. // rows.getFieldList().remove("returnqty");
  804. // rows.getFieldList().remove("stockno");
  805. // rows.getFieldList().remove("position");
  806. // rows.getFieldList().remove("batchno");
  807. // rows.getFieldList().remove("isfreeze");
  808. // rows.getFieldList().remove("candispatchqty");
  809. // rows.getFieldList().remove("cansaleqty");
  810. // rows.getFieldList().remove("totalaty");
  811. // rows.getFieldList().remove("orderminqty_auxunit");
  812. // rows.getFieldList().remove("orderminqty");
  813. // rows.getFieldList().remove("orderaddqty_auxunit");
  814. // rows.getFieldList().remove("orderaddqty");
  815. // rows.getFieldList().remove("spec");
  816. // rows.getFieldList().remove("packageqty");
  817. // rows.getFieldList().remove("delivery");
  818. // rows.getFieldList().remove("erpitemno");
  819. // rows.getFieldList().remove("erpitemname");
  820. // rows.getFieldList().remove("prodline");
  821. // rows.getFieldList().remove("material");
  822. // rows.getFieldList().remove("device");
  823. // rows.getFieldList().remove("specalnote");
  824. // rows.getFieldList().remove("standards");
  825. // rows.getFieldList().remove("agentnum");
  826. // rows.getFieldList().remove("enterprisename");
  827. // rows.getFieldList().remove("agentnum");
  828. // rows.getFieldList().remove("stockstatus");
  829. // rows.getFieldList().remove("submitdate");
  830. // rows.getFieldList().remove("checkdate");
  831. // 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);
  832. // if (!rowsrolename.isEmpty()) {
  833. // rows.getFieldList().remove("price");
  834. // rows.getFieldList().remove("amount");
  835. // }
  836. //
  837. // Rows uploadRows = uploadExcelToObs("orderdetail", "订单明细列表", rows, getTitleMapAgent());
  838. // return getSucReturnObject().setData(uploadRows).toString();
  839. // }
  840. return getSucReturnObject().setData(rows).toString();
  841. }
  842. //查询订单列表(管理端)
  843. public QuerySQL queryAllListManage(String where) throws YosException {
  844. QuerySQL t11 = SQLFactory.createQuerySQL(this, "sa_dispatch_items",
  845. "sa_orderitemsid", "siteid");
  846. t11.addQueryFields("outwarehouseqty", "sum(outwarehouseqty)");
  847. t11.addGroupBy("sa_orderitemsid,siteid");
  848. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_orderitems",
  849. "sa_orderitemsid", "itemid", "rowno", "itemno", "itemname", "unit", "qty", "undeliqty", "saleprice", "defaultprice", "price", "amount", "remarks", "customproperties", "batchcontrol", "model",
  850. "conversionrate", "marketprice", "defaultprice", "defaultamount", "auxqty", "needdate", "deliedqty", "rebateamount", "invoiceamount", "writeoffamount", "auxunit", "sa_orderid",
  851. "deliverydate", "returnqty", "stockno", "position", "batchno", "isfreeze", "decorationrebateflag");
  852. querySQL.setTableAlias("t1");
  853. querySQL.addJoinTable(JOINTYPE.left, "plm_item", "t3", "t3.itemid = t1.itemid and t3.siteid = t1.siteid",
  854. "orderminqty_auxunit", "orderminqty", "orderaddqty_auxunit", "orderaddqty", "spec", "standards", "packageqty", "delivery", "k3outcode");
  855. querySQL.addJoinTable(JOINTYPE.left, "sa_order", "t5", "t5.sa_orderid = t1.sa_orderid and t5.siteid = t1.siteid",
  856. "sonum", "type", "typemx", "status", "billdate", "submitdate", "checkdate", "tradefield", "closedate", "remarks");
  857. querySQL.addJoinTable(JOINTYPE.left, "sa_agents", "t6", "t6.sys_enterpriseid = t5.sys_enterpriseid and t6.siteid = t5.siteid",
  858. "agentnum");
  859. querySQL.addJoinTable(JOINTYPE.left, "sys_enterprise", "t7", "t7.sys_enterpriseid = t5.sys_enterpriseid and t7.siteid = t5.siteid",
  860. "enterprisename");
  861. querySQL.addJoinTable(JOINTYPE.left, "plm_itemclass", "t8", "t8.itemclassid = t3.marketingcategory and t8.siteid = t3.siteid",
  862. "itemclassname");
  863. querySQL.addJoinTable(JOINTYPE.left, "sys_enterprise_tradefield", "t9", "t9.sys_enterpriseid = t5.sys_enterpriseid and t9.tradefield=t5.tradefield and t9.siteid = t5.siteid",
  864. "sys_enterprise_tradefieldid");
  865. querySQL.addJoinTable(JOINTYPE.left, "sa_salearea", "t10", "t10.sa_saleareaid = t9.sa_saleareaid and t10.siteid = t9.siteid",
  866. "areaname");
  867. querySQL.addJoinTable(JOINTYPE.left, t11, "t11", "t11.sa_orderitemsid = t1.sa_orderitemsid and t11.siteid = t1.siteid");
  868. querySQL.addQueryFields("orderremarks", "t5.remarks");
  869. querySQL.addQueryFields("outwarehouseqty", "ifnull(t11.outwarehouseqty,0)");
  870. //querySQL.addQueryFields("accountclassinfos", "REPLACE(SUBSTRING(JSON_EXTRACT(t5.sa_accountclassinfos, '$[*].accountname'), 2, CHAR_LENGTH(JSON_EXTRACT(t5.sa_accountclassinfos, '$[*].accountname'))-2),'\"','')");
  871. querySQL.setWhere("t1.siteid", siteid);
  872. querySQL.setWhere(where);
  873. querySQL.setWhere("t5.status in ('审核', '关闭','手工关闭')");
  874. return querySQL;
  875. }
  876. //返回导出的标题
  877. public HashMap<String, String> getTitleMap() {
  878. HashMap<String, String> titleMap = new HashMap<>();
  879. titleMap.put("rowno", "行号");
  880. titleMap.put("batchcontrol", "是否批次管理");
  881. titleMap.put("itemno", "产品编号");
  882. titleMap.put("itemname", "产品名称");
  883. titleMap.put("model", "型号");
  884. titleMap.put("conversionrate", "换算率(辅助数量=主单位数量/换算率)");
  885. titleMap.put("price", "单价,折后价(元)");
  886. titleMap.put("amount", "金额,折后金额(元)");
  887. titleMap.put("marketprice", "牌价、市场价(元)");
  888. titleMap.put("defaultprice", "折前价(元)");
  889. titleMap.put("defaultamount", "折前金额(元)");
  890. titleMap.put("qty", "订购数量");
  891. titleMap.put("auxqty", "辅助单位数量");
  892. titleMap.put("needdate", "需求日期");
  893. titleMap.put("deliedqty", "已交货量(主单位)");
  894. titleMap.put("rebateamount", "返利分摊金额");
  895. titleMap.put("undeliqty", "未交货量(主单位)");
  896. titleMap.put("invoiceamount", "已开票金额");
  897. titleMap.put("writeoffamount", "已核销金额");
  898. titleMap.put("unit", "计量单位名称");
  899. titleMap.put("auxunit", "辅助计量单位名称");
  900. titleMap.put("deliverydate", "交货日期");
  901. titleMap.put("returnqty", "退货数量");
  902. titleMap.put("remarks", "备注");
  903. titleMap.put("submitdate", "提交时间");
  904. titleMap.put("checkdate", "审核时间");
  905. return titleMap;
  906. }
  907. //返回导出的标题
  908. public HashMap<String, String> getTitleMapAgent() throws YosException {
  909. HashMap<String, String> titleMap = new HashMap<>();
  910. titleMap.put("rowno", "行号");
  911. titleMap.put("sonum", "订单号");
  912. titleMap.put("status", "订单状态");
  913. titleMap.put("type", "订单类型");
  914. titleMap.put("billdate", "单据日期");
  915. titleMap.put("tradefield", "领域");
  916. titleMap.put("itemno", "产品编码");
  917. titleMap.put("itemname", "品名");
  918. titleMap.put("unit", "单位");
  919. titleMap.put("qty", "数量");
  920. titleMap.put("undeliqty", "未发货数量");
  921. 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);
  922. if (rowsrolename.isEmpty()) {
  923. titleMap.put("price", "单价");
  924. titleMap.put("amount", "金额");
  925. }
  926. titleMap.put("remarks", "订单行备注");
  927. return titleMap;
  928. }
  929. @API(title = "查询可添加商品列表", apiversion = R.ID20221109153502.v1.class)
  930. @CACHEING
  931. public String selectItemList() throws YosException {
  932. StringBuffer where = new StringBuffer(" 1=1 ");
  933. if (content.containsKey("where")) {
  934. JSONObject whereObject = content.getJSONObject("where");
  935. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  936. where.append(" and(");
  937. where.append("t1.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  938. where.append("or t1.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  939. where.append("or t1.model like'%").append(whereObject.getString("condition")).append("%' ");
  940. where.append("or t1.spec like'%").append(whereObject.getString("condition")).append("%' ");
  941. where.append("or t3.material like'%").append(whereObject.getString("condition")).append("%' ");
  942. where.append("or t3.erpitemno like'%").append(whereObject.getString("condition")).append("%' ");
  943. where.append(")");
  944. }
  945. if (whereObject.containsKey("isonsale") && !"".equals(whereObject.getString("isonsale"))) {
  946. where.append(" and t1.isonsale ='").append(whereObject.getString("isonsale")).append("' ");
  947. }
  948. }
  949. Long sa_orderid = content.getLong("sa_orderid");
  950. Long sys_enterpriseid1 = sys_enterpriseid;
  951. Rows orderRows = getOrderRows(this, sa_orderid);
  952. Long sa_brandid = 0L;
  953. String tradefield = "";
  954. String type = "";
  955. String typemx = "";
  956. Long sys_enterpriseid = 0L;
  957. JSONArray sa_accountclassinfos = new JSONArray();
  958. if (orderRows.isNotEmpty()) {
  959. sa_brandid = orderRows.get(0).getLong("sa_brandid");
  960. tradefield = orderRows.get(0).getString("tradefield");
  961. type = orderRows.get(0).getString("type");
  962. typemx = orderRows.get(0).getString("typemx");
  963. sys_enterpriseid = orderRows.get(0).getLong("sys_enterpriseid");
  964. sa_accountclassinfos = orderRows.get(0).getJSONArray("sa_accountclassinfos");
  965. }
  966. boolean iswuliao = false;
  967. for (Object sa_accountclassinfo : sa_accountclassinfos) {
  968. JSONObject jsonObject = (JSONObject) sa_accountclassinfo;
  969. if (jsonObject.getString("accountname").equals("物料账户")) {
  970. iswuliao = true;
  971. }
  972. }
  973. SQLFactory sqlFactory = new SQLFactory(this, "查询可添加商品列表", pageSize, pageNumber, pageSorting);
  974. switch (type) {
  975. // case "总部订单":
  976. // sqlFactory = new SQLFactory(this, "查询可添加商品列表_总部订单", pageSize, pageNumber, pageSorting);
  977. // break;
  978. case "特殊订单":
  979. if (typemx.equals("电商") || typemx.equals("工程")) {
  980. sqlFactory = new SQLFactory(this, "查询可添加商品列表_总部订单", pageSize, pageNumber, pageSorting);
  981. } else {
  982. sqlFactory = new SQLFactory(this, "查询可添加商品列表_特殊订单", pageSize, pageNumber, pageSorting);
  983. }
  984. sqlFactory.addParameter("tradefield", tradefield);
  985. if (sys_enterpriseid1 > 0) {
  986. where.append(" and t1.isonsale ='1' ");
  987. }
  988. break;
  989. default:
  990. StringBuffer where2 = new StringBuffer(" 1=1 ");
  991. Row enterpriseRow = Enterprise.getEnterprise(this, sys_enterpriseid);
  992. boolean saleclassauth = enterpriseRow.getBoolean("saleclassauth");
  993. //需要经营授权
  994. if (saleclassauth) {
  995. where2.append(" and t1.itemclassid in (SELECT itemclassid from sys_enterprise_saleclass WHERE sys_enterpriseid = " + sys_enterpriseid + " and siteid = '" + siteid + "')");
  996. }
  997. sqlFactory.addParameter("sa_brandid", sa_brandid);
  998. sqlFactory.addParameter("tradefield", tradefield);
  999. sqlFactory.addParameter_SQL("where2", where2);
  1000. break;
  1001. }
  1002. if(iswuliao){
  1003. where.append(" and t1.iswuliao ='1' ");
  1004. }
  1005. sqlFactory.addParameter("siteid", siteid);
  1006. sqlFactory.addParameter_SQL("where", where);
  1007. String sql = sqlFactory.getSQL(false);
  1008. Rows rows = dbConnect.runSqlQuery(sql);
  1009. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  1010. //查询附件
  1011. RowsMap attinfoRowsMap = getAttachmentUrl("plm_item", ids);
  1012. //商品领域
  1013. RowsMap tradefieldRowsMap = beans.Item.Item.getTradefieldRowsMap(this, ids);
  1014. //品牌
  1015. RowsMap brandRowsMap = Brand.getBrandRowsMap(this, ids);
  1016. //价格
  1017. HashMap<Long, ItemPrice> itemPriceRowsMap = ItemPrice.getItemPrice(this, sys_enterpriseid, ids);
  1018. //定制信息
  1019. CustomScheme customScheme = new CustomScheme(this);
  1020. for (Row row : rows) {
  1021. row.put("attinfos", attinfoRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  1022. row.put("tradefield", tradefieldRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  1023. row.put("brand", brandRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  1024. if (itemPriceRowsMap.containsKey(row.getLong("itemid"))) {
  1025. row.put("price", itemPriceRowsMap.get(row.getLong("itemid")).getPrice());
  1026. } else {
  1027. row.put("price", 0);
  1028. }
  1029. row = customScheme.setScheme(row, "width");
  1030. row = customScheme.setScheme(row, "length");
  1031. row = customScheme.setScheme(row, "material");
  1032. row = customScheme.setScheme(row, "color");
  1033. row = customScheme.setScheme(row, "cheek");
  1034. if (row.getLong("width") == 0) {
  1035. row.replace("width", null);
  1036. }
  1037. if (row.getLong("length") == 0) {
  1038. row.replace("length", null);
  1039. }
  1040. }
  1041. return getSucReturnObject().setData(rows).toString();
  1042. }
  1043. //获取非工具数据
  1044. public String getOrderItemsList(Long sa_orderid, String tradefield, Long sys_enterpriseid, Long sa_brandid) throws YosException {
  1045. StringBuffer where = new StringBuffer(" 1=1 ");
  1046. Row enterpriseRow = Enterprise.getEnterprise(this, sys_enterpriseid);
  1047. boolean saleclassauth = enterpriseRow.getBoolean("saleclassauth");
  1048. //需要经营授权
  1049. if (saleclassauth) {
  1050. where.append(" and itemclassid in (SELECT itemclassid from sys_enterprise_saleclass WHERE sys_enterpriseid = " + sys_enterpriseid + " and siteid = '" + siteid + "')");
  1051. }
  1052. SQLFactory sqlFactory = new SQLFactory(this, "商品范围");
  1053. sqlFactory.addParameter("siteid", siteid);
  1054. sqlFactory.addParameter("sa_orderid", sa_orderid);
  1055. sqlFactory.addParameter_in("tradefield", tradefield);
  1056. sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  1057. sqlFactory.addParameter("sa_brandid", sa_brandid);
  1058. sqlFactory.addParameter_SQL("where", where);
  1059. return sqlFactory.getSQL(false);
  1060. }
  1061. // //获取特殊数据
  1062. // public String getOrderItemsListSpec(Long sa_orderid, String tradefield, Long sys_enterpriseid) throws YosException {
  1063. // SQLFactory sqlFactory = new SQLFactory(this, "商品范围_特殊");
  1064. // sqlFactory.addParameter("siteid", siteid);
  1065. // sqlFactory.addParameter("sa_orderid", sa_orderid);
  1066. // sqlFactory.addParameter_in("tradefield", tradefield);
  1067. // sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  1068. // return sqlFactory.getSQL(false);
  1069. // }
  1070. @API(title = "订单商品变更", apiversion = R.ID20221110145302.v1.class)
  1071. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class})
  1072. public String changeItem() throws YosException {
  1073. boolean isjiean = content.getBooleanValue("isjiean");
  1074. Long sa_orderid = content.getLong("sa_orderid");
  1075. //通过版本更新订单表头数据
  1076. beans.order.Order.updateOrderWithVersion(this);
  1077. String type = content.getString("type");
  1078. Long sa_orderitems_changeid = createTableID("sa_orderitems_change");
  1079. JSONArray itemifnos = content.getJSONArray("itemifnos");
  1080. BigDecimal oldValue = new BigDecimal(0);
  1081. Rows Rowstatus = dbConnect.runSqlQuery(
  1082. "select * from sa_order where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  1083. if (Rowstatus.isEmpty()) {
  1084. return "订单不存在";
  1085. } else {
  1086. if (!Rowstatus.get(0).getString("status").equals("审核")) {
  1087. return getErrReturnObject().setErrMsg("非审核状态的订单无法进行商品变更").toString();
  1088. }
  1089. }
  1090. ArrayList<String> sqlList = new ArrayList<>();
  1091. // 订单商品变更
  1092. SQLFactory sqlFactory = new SQLFactory(this, "订单商品变更_新增");
  1093. sqlFactory.addParameter("siteid", siteid);
  1094. sqlFactory.addParameter("userid", userid);
  1095. sqlFactory.addParameter("username", username);
  1096. sqlFactory.addParameter("sa_orderitems_changeid", sa_orderitems_changeid);
  1097. sqlFactory.addParameter("sa_orderid", sa_orderid);
  1098. sqlFactory.addParameter("remarks", content.getStringValue("remarks"));
  1099. sqlList.add(sqlFactory.getSQL());
  1100. HashMap<Long, Row> orderdetailmap = new HashMap<>();
  1101. HashMap<Long, BigDecimal> orderdetailchangeamount = new HashMap<>();
  1102. Rows rowsOrder = dbConnect.runSqlQuery("select * from sa_order t1 where t1.sa_orderid=" + sa_orderid);
  1103. BigDecimal ce = new BigDecimal(0);
  1104. for (Object obj : itemifnos) {
  1105. JSONObject jsonObject = (JSONObject) obj;
  1106. Rows rows = dbConnect.runSqlQuery(
  1107. "SELECT sa_orderitemsid,qty,deliedqty,undeliqty,writeoffamount,price from sa_orderitems WHERE sa_orderitemsid="
  1108. + jsonObject.getLongValue("sa_orderitemsid"));
  1109. if (rows.isNotEmpty()) {
  1110. oldValue = rows.get(0).getBigDecimal("qty");
  1111. } else {
  1112. return getErrReturnObject().setErrMsg("订单行不存在").toString();
  1113. }
  1114. if (type.equals("数量")) {
  1115. // 更新数量
  1116. BigDecimal newqty = jsonObject.getBigDecimal("newvalue");
  1117. BigDecimal oldqty = rows.get(0).getBigDecimal("qty");
  1118. BigDecimal oldundeliqty = rows.get(0).getBigDecimal("undeliqty");
  1119. BigDecimal addqty = newqty.subtract(oldqty);// 新增的数量
  1120. if (newqty.compareTo(oldqty) >= 0) {
  1121. return getErrReturnObject().setErrMsg("修改数量必须小于原订单数量" + oldqty).toString();
  1122. }
  1123. if (newqty.compareTo(oldqty.subtract(oldundeliqty)) < 0) {
  1124. return getErrReturnObject().setErrMsg("修改数量必须大于等于已发货数量" + oldqty.subtract(oldundeliqty)).toString();
  1125. }
  1126. if (newqty.compareTo(BigDecimal.ZERO) < 0) {
  1127. return getErrReturnObject().setErrMsg("修改数量不可小于0").toString();
  1128. }
  1129. sqlFactory = new SQLFactory(this, "订单商品明细变更_更新");
  1130. sqlFactory.addParameter("siteid", siteid);
  1131. sqlFactory.addParameter("sa_orderitemsid", jsonObject.getLongValue("sa_orderitemsid"));
  1132. sqlFactory.addParameter("qty", jsonObject.getStringValue("newvalue"));
  1133. // sqlFactory.addParameter("undeliqty", oldundeliqty.add(addqty));
  1134. ce = ce.add(addqty.multiply(rows.get(0).getBigDecimal("price")));
  1135. sqlList.add(sqlFactory.getSQL());
  1136. /**
  1137. * * 核销对冲计算
  1138. */
  1139. {
  1140. /**
  1141. * * 核销对冲计算
  1142. */
  1143. BigDecimal writeoffamount = rows.get(0).getBigDecimal("writeoffamount");// 订单行已核销金额
  1144. //System.out.println(newqty.multiply(rows.get(0).getBigDecimal("price")));
  1145. //System.out.println(writeoffamount);
  1146. if (writeoffamount.compareTo(BigDecimal.ZERO) > 0
  1147. && (newqty.multiply(rows.get(0).getBigDecimal("price"))).compareTo(writeoffamount) < 0) {
  1148. // 如果已核销金额大于0 且调整后折后金额小于已核销金额,则需要将差价进行回冲
  1149. orderdetailmap.put(rows.get(0).getLong("sa_orderitemsid"), rows.get(0));
  1150. //System.out.println(writeoffamount.subtract(newqty.multiply(rows.get(0).getBigDecimal("price"))));
  1151. //System.out.println("sa_orderitemsid:"+rows.get(0).getLong("sa_orderitemsid"));
  1152. orderdetailchangeamount.put(rows.get(0).getLong("sa_orderitemsid"),
  1153. writeoffamount.subtract(newqty.multiply(rows.get(0).getBigDecimal("price"))));
  1154. }
  1155. }
  1156. }
  1157. // 订单商品变更明细
  1158. sqlFactory = new SQLFactory(this, "订单商品变更明细_新增");
  1159. sqlFactory.addParameter("siteid", siteid);
  1160. sqlFactory.addParameter("sa_orderitems_changemxid", createTableID("sa_orderitems_changemx"));
  1161. sqlFactory.addParameter("itemid", jsonObject.getLongValue("itemid"));
  1162. sqlFactory.addParameter("sa_orderitemsid", jsonObject.getLongValue("sa_orderitemsid"));
  1163. sqlFactory.addParameter("sa_orderitems_changeid", sa_orderitems_changeid);
  1164. sqlFactory.addParameter("type", type);
  1165. sqlFactory.addParameter("oldvalue", oldValue);
  1166. sqlFactory.addParameter("newvalue", jsonObject.getStringValue("newvalue"));
  1167. sqlList.add(sqlFactory.getSQL());
  1168. // content.put("sa_orderitemsid", sa_orderitemsid);
  1169. }
  1170. Long sa_cashbillid = 0L;
  1171. if (ce.compareTo(BigDecimal.ZERO) != 0) {
  1172. CashbillEntity cashbillEntity = new CashbillEntity();
  1173. cashbillEntity.setAmount(ce);
  1174. cashbillEntity.setOwnerid(sa_orderid);
  1175. cashbillEntity.setOwnertable("sa_order");
  1176. cashbillEntity
  1177. .setRemarks("订单变更:" + rowsOrder.get(0).getString("sonum") + "订单变更单id:" + sa_orderitems_changeid);
  1178. JSONObject cashbillPay = Accountbalance.createCashbillPay(this, rowsOrder.get(0).getLong("sys_enterpriseid"),
  1179. rowsOrder.get(0).getLong("sa_accountclassid"), cashbillEntity, true);
  1180. sqlList.addAll(cashbillPay.getJSONArray("sqlList").toJavaList(String.class));
  1181. sa_cashbillid = cashbillPay.getLong("sa_cashbillid");
  1182. }
  1183. /**
  1184. * * 核销对冲
  1185. */
  1186. if (!orderdetailmap.isEmpty()) {
  1187. //System.out.println("测试");
  1188. sqlList.addAll(
  1189. new twriteoffbill_orderchange(rowsOrder.get(0), orderdetailmap, orderdetailchangeamount).hedging());
  1190. }
  1191. for (String string : sqlList) {
  1192. //System.out.println(string);
  1193. }
  1194. dbConnect.runSqlUpdate(sqlList);
  1195. // 居间费设置
  1196. updateRebateFee(sa_orderid);
  1197. if (!isjiean) {
  1198. if (Parameter.get("system.erp_dockswitch").equalsIgnoreCase("true")) {
  1199. UploadDataToERP uploadDataToERP = new UploadDataToERP(this, sa_orderitems_changeid, "订单变更");
  1200. boolean issuccess = uploadDataToERP.upload();
  1201. if (!issuccess) {
  1202. return getErrReturnObject().setErrMsg("上传ERP失败,请检查!").toString();
  1203. }
  1204. }
  1205. }
  1206. if (ce.compareTo(BigDecimal.ZERO) != 0) {
  1207. Accountbalance.remindSend(this, rowsOrder.get(0).getLong("sys_enterpriseid"), sa_cashbillid);
  1208. }
  1209. return getSucReturnObject().toString();
  1210. }
  1211. @API(title = "订单商品变更(手动推erp)", apiversion = R.ID20230306161703.v1.class)
  1212. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class})
  1213. public String changeItem1() throws YosException {
  1214. if (Parameter.get("system.erp_dockswitch").equalsIgnoreCase("true")) {
  1215. UploadDataToERP uploadDataToERP = new UploadDataToERP(this, content.getLong("sa_orderitems_changeid"), "订单变更");
  1216. boolean issuccess = uploadDataToERP.upload();
  1217. if (!issuccess) {
  1218. return getErrReturnObject().setErrMsg("上传ERP失败,请检查!").toString();
  1219. } else {
  1220. return getSucReturnObject().toString();
  1221. }
  1222. } else {
  1223. return getErrReturnObject().setErrMsg("erp对接开关未开!").toString();
  1224. }
  1225. }
  1226. /**
  1227. * 获取当前订单的最大行号
  1228. *
  1229. * @param sa_orderid
  1230. * @return
  1231. * @throws YosException
  1232. */
  1233. public Long getRowNo(Long sa_orderid) throws YosException {
  1234. Rows rows = dbConnect.runSqlQuery("SELECT COUNT(0) count from sa_orderitems WHERE sa_orderid = " + sa_orderid + " AND siteid = '" + siteid + "'");
  1235. return rows.get(0).getLong("count") + 1;
  1236. }
  1237. /**
  1238. * 查询商品信息
  1239. *
  1240. * @param itemid
  1241. * @return
  1242. * @throws YosException
  1243. */
  1244. public Row getItemRow(Long itemid) throws YosException {
  1245. SQLFactory sqlFactory = new SQLFactory(this, "查询商品信息");
  1246. sqlFactory.addParameter("siteid", siteid);
  1247. sqlFactory.addParameter("itemid", itemid);
  1248. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL(false));
  1249. return rows.isNotEmpty() ? rows.get(0) : new Row();
  1250. }
  1251. /**
  1252. * 重新对商品行排序
  1253. *
  1254. * @param sa_orderid
  1255. * @throws YosException
  1256. */
  1257. public void updateRowNo(Long sa_orderid) throws YosException {
  1258. String sql = "SELECT sa_orderitemsid from sa_orderitems WHERE sa_orderid= " + sa_orderid + " and siteid = '" + siteid + "' ORDER BY createdate ";
  1259. ArrayList<Long> sa_orderitemsids = dbConnect.runSqlQuery(sql).toArrayList("sa_orderitemsid", new ArrayList<>());
  1260. if (sa_orderitemsids.size() > 0) {
  1261. int rowno = 1;
  1262. StringBuffer where = new StringBuffer("");
  1263. for (Long id : sa_orderitemsids) {
  1264. where.append(" WHEN " + id + " THEN " + rowno);
  1265. rowno++;
  1266. }
  1267. SQLFactory sqlFactory = new SQLFactory(this, "更新行号");
  1268. sqlFactory.addParameter("siteid", siteid);
  1269. sqlFactory.addParameter_SQL("where", where);
  1270. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsids);
  1271. sql = sqlFactory.getSQL(false);
  1272. dbConnect.runSqlUpdate(sql);
  1273. }
  1274. }
  1275. }