OrderItems.java 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. package restcontroller.webmanage.sale.order;
  2. import beans.accountbalance.Accountbalance;
  3. import beans.accountbalance.CashbillEntity;
  4. import beans.brand.Brand;
  5. import beans.datacontrllog.DataContrlLog;
  6. import beans.dataextend.DataExtend;
  7. import beans.datateam.DataTeam;
  8. import beans.enterprise.Enterprise;
  9. import beans.itemprice.ItemPrice;
  10. import beans.parameter.Parameter;
  11. import beans.uploaderpdata.UploadDataToERP;
  12. import com.alibaba.fastjson.JSONArray;
  13. import com.alibaba.fastjson.JSONObject;
  14. import common.Controller;
  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 org.apache.poi.ss.usermodel.CellStyle;
  21. import org.apache.poi.ss.usermodel.DataFormat;
  22. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  23. import org.apache.poi.xssf.usermodel.XSSFSheet;
  24. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  25. import restcontroller.R;
  26. import restcontroller.webmanage.sale.aftersalesmag.twriteoffbill_orderchange;
  27. import restcontroller.webmanage.sale.rebate.Rebate;
  28. import restcontroller.webmanage.sale.toolbill.ToolBilItem;
  29. import restcontroller.webmanage.sale.toolbill.ToolBill;
  30. import utility.ERPDocking;
  31. import java.io.IOException;
  32. import java.math.BigDecimal;
  33. import java.util.ArrayList;
  34. import java.util.HashMap;
  35. import static beans.order.Order.*;
  36. /**
  37. * 销售订单商品明细表
  38. */
  39. public class OrderItems extends Controller {
  40. /**
  41. * 构造函数
  42. *
  43. * @param content
  44. */
  45. public OrderItems(JSONObject content) throws YosException {
  46. super(content);
  47. }
  48. @API(title = "新增或更新", apiversion = R.ID20221109093602.v1.class)
  49. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class})
  50. public String insertOrUpdate() throws YosException {
  51. Long sa_orderid = content.getLong("sa_orderid");
  52. Long sys_enterpriseid = content.getLong("sys_enterpriseid");
  53. // Long sa_contractid = content.getLongValue("sa_contractid");
  54. Rows orderRows = getOrderRows(this, sa_orderid);
  55. if (orderRows.isEmpty()) {
  56. return getErrReturnObject().setErrMsg("数据不存在,无法添加").toString();
  57. }
  58. // sa_contractid = orderRows.get(0).getLong("sa_contractid");
  59. String type = orderRows.get(0).getString("type");
  60. JSONArray items = content.getJSONArray("items");
  61. Long sa_promotionid = orderRows.get(0).getLong("sa_promotionid");
  62. ArrayList<String> sqlList = new ArrayList<>();
  63. //查询erp库存
  64. // Invbal.selectErpInvbal(this, items);
  65. Rows itemRows = dbConnect.runSqlQuery("select itemid,orderaddqty,orderminqty,itemno from plm_item where siteid='" + siteid + "'");
  66. RowsMap itemRowsMap = itemRows.toRowsMap("itemid");
  67. BigDecimal sumqty = BigDecimal.ZERO;
  68. for (Object obj : items) {
  69. JSONObject item = (JSONObject) obj;
  70. Long itemid = item.getLong("itemid");
  71. Long sa_orderitemsid = item.getLong("sa_orderitemsid");
  72. BigDecimal qty = item.getBigDecimalValue("qty");
  73. sumqty = sumqty.add(qty);
  74. if (!type.equals("促销订单")) {
  75. if (itemRowsMap.containsKey(item.getString("itemid"))) {
  76. if (itemRowsMap.get(item.getString("itemid")).isNotEmpty()) {
  77. BigDecimal orderaddqty = itemRowsMap.get(item.getString("itemid")).get(0).getBigDecimal("orderaddqty");
  78. BigDecimal orderminqty = itemRowsMap.get(item.getString("itemid")).get(0).getBigDecimal("orderminqty");
  79. if (((qty.subtract(orderminqty)).remainder(orderaddqty)).compareTo(BigDecimal.ZERO) != 0) {
  80. return getErrReturnObject().setErrMsg("品号为" + itemRowsMap.get(item.getString("itemid")).get(0).getString("itemno") + "的商品数量不符合该商品的起订量和增量").toString();
  81. }
  82. }
  83. }
  84. }
  85. //商品信息
  86. Row itemRow = getItemRow(itemid);
  87. Rows promotionitems = dbConnect.runSqlQuery("select * from sa_promotion_items where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  88. RowsMap promotionitemsRowsMap = promotionitems.toRowsMap("itemid");
  89. if (type.equals("促销订单")) {
  90. if (promotionitemsRowsMap.containsKey(item.getString("itemid"))) {
  91. if (promotionitemsRowsMap.get(item.getString("itemid")).isNotEmpty()) {
  92. BigDecimal saledqty = promotionitemsRowsMap.get(item.getString("itemid")).get(0).getBigDecimal("saledqty");
  93. BigDecimal groupqty = promotionitemsRowsMap.get(item.getString("itemid")).get(0).getBigDecimal("groupqty");
  94. BigDecimal orderaddqty = promotionitemsRowsMap.get(item.getString("itemid")).get(0).getBigDecimal("orderaddqty");
  95. BigDecimal orderminqty = promotionitemsRowsMap.get(item.getString("itemid")).get(0).getBigDecimal("orderminqty");
  96. boolean islimit = promotionitemsRowsMap.get(item.getString("itemid")).get(0).getBoolean("islimit");
  97. if (((qty.subtract(orderminqty)).remainder(orderaddqty)).compareTo(BigDecimal.ZERO) != 0) {
  98. return getErrReturnObject().setErrMsg("品号为" + itemRowsMap.get(item.getString("itemid")).get(0).getString("itemno") + "的商品数量不符合该商品的起订量和增量").toString();
  99. }
  100. if (islimit) {
  101. if (groupqty.compareTo(saledqty.add(qty)) < 0) {
  102. return getErrReturnObject().setErrMsg(itemRow.getString("itemno") + "商品数量已超过促销活动的限定数量【" + groupqty.subtract(saledqty) + "】,无法新建").toString();
  103. }
  104. }
  105. }
  106. }
  107. }
  108. BigDecimal price1 = new BigDecimal(0);
  109. SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_新增");
  110. if (sa_orderitemsid <= 0) {
  111. Rows rows = dbConnect.runSqlQuery("SELECT sa_orderitemsid,price from sa_orderitems WHERE siteid='" + siteid + "' and itemid=" + itemid + " and sa_orderid=" + sa_orderid);
  112. if (rows.isEmpty()) {
  113. sa_orderitemsid = createTableID("sa_orderitems");
  114. } else {
  115. System.out.println(beans.order.Order.getDefaultIsRepeatValue(siteid, type));
  116. if (beans.order.Order.getDefaultIsRepeatValue(siteid, type)) {
  117. sa_orderitemsid = createTableID("sa_orderitems");
  118. sqlFactory = new SQLFactory(this, "订单商品明细_新增");
  119. } else {
  120. price1 = rows.get(0).getBigDecimal("price");
  121. sa_orderitemsid = rows.get(0).getLong("sa_orderitemsid");
  122. sqlFactory = new SQLFactory(this, "订单商品明细_更新");
  123. }
  124. }
  125. } else {
  126. sqlFactory = new SQLFactory(this, "订单商品明细_更新");
  127. }
  128. sqlFactory.addParameter("siteid", siteid);
  129. sqlFactory.addParameter("userid", userid);
  130. sqlFactory.addParameter("username", username);
  131. sqlFactory.addParameter("sa_orderitemsid", sa_orderitemsid);
  132. sqlFactory.addParameter("sa_orderid", sa_orderid);
  133. sqlFactory.addParameter("rowno", 1);
  134. sqlFactory.addParameter("itemid", itemid);
  135. sqlFactory.addParameter("remarks", item.getStringValue("remarks"));
  136. BigDecimal conversionrate = itemRow.getBigDecimal("conversionrate");
  137. if (conversionrate.compareTo(BigDecimal.ZERO) <= 0) {
  138. conversionrate = BigDecimal.valueOf(1);
  139. }
  140. sqlFactory.addParameter("itemno", itemRow.getString("itemno"));
  141. sqlFactory.addParameter("itemname", itemRow.getString("itemname"));
  142. sqlFactory.addParameter("model", itemRow.getString("model"));
  143. sqlFactory.addParameter("unit", itemRow.getString("unit"));
  144. sqlFactory.addParameter("auxunit", itemRow.getString("auxunit"));
  145. sqlFactory.addParameter("batchcontrol", itemRow.getLong("batchcontrol"));
  146. sqlFactory.addParameter("delivery", itemRow.getLong("delivery"));
  147. sqlFactory.addParameter("stockno", item.getOrDefault("stockno", "null"));
  148. sqlFactory.addParameter("position", item.getOrDefault("position", "null"));
  149. sqlFactory.addParameter("batchno", item.getOrDefault("batchno", "null"));
  150. String deliverydate = item.getStringValue("deliverydate");
  151. Rows rows = dbConnect.runSqlQuery("SELECT deliverydate,defaultprice from sa_orderitems WHERE sa_orderitemsid =" + sa_orderitemsid);
  152. if (deliverydate.isEmpty()) {
  153. if (rows.isNotEmpty()) {
  154. deliverydate = rows.get(0).getString("deliverydate");
  155. } else {
  156. deliverydate = "null";
  157. }
  158. }
  159. String needdate = item.getStringValue("needdate");
  160. sqlFactory.addParameter("needdate", needdate.equals("") ? "null" : needdate);
  161. sqlFactory.addParameter("deliverydate", deliverydate.equals("") ? "null" : deliverydate);
  162. sqlFactory.addParameter("conversionrate", conversionrate);
  163. //订购数量
  164. sqlFactory.addParameter("qty", qty);
  165. //辅助单位数量
  166. sqlFactory.addParameter("auxqty", qty.divide(conversionrate));
  167. BigDecimal defaultprice;
  168. BigDecimal price;
  169. //价格
  170. ItemPrice itemPrice = ItemPrice.getItemPrice(this, sys_enterpriseid, itemid);
  171. if (!item.containsKey("defaultprice")) {
  172. switch (type) {
  173. // case "项目订单":
  174. // defaultprice = itemPrice.getContractprice(sa_contractid);
  175. // break;
  176. // case "工具订单":
  177. // defaultprice = itemPrice.getMarketprice();
  178. // break;
  179. case "促销订单":
  180. defaultprice = itemPrice.getGraderateprice();
  181. price = itemPrice.getPromotionPrice(sa_promotionid);
  182. break;
  183. default:
  184. defaultprice = itemPrice.getGraderateprice();
  185. price = itemPrice.getGraderateprice();
  186. }
  187. } else {
  188. price = item.getBigDecimalValue("price");
  189. if (price.compareTo(price1) != 0) {
  190. sqlList.add(DataContrlLog.createLog(this, "sa_order", sa_orderid, "订单商品【" + itemRow.getString("itemname") + "】由单价:" + price1 + "修改为单价:" + price, "订单修改单价").getSQL());
  191. }
  192. defaultprice = item.getBigDecimalValue("defaultprice");
  193. // if (!type.equals("特殊订单") && defaultprice.compareTo(rows.get(0).getBigDecimal("defaultprice")) != 0 && defaultprice.compareTo(itemPrice.getContractprice()) < 0) {
  194. // return getErrReturnObject().setErrMsg("调整价格不可小于协议价").toString();
  195. // }
  196. }
  197. if (type.equals("特殊订单")) {
  198. if (item.containsKey("defaultprice")) {
  199. if (item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO) > 0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO) <= 0) {
  200. sqlFactory.addParameter("price", item.getBigDecimalValue("price"));
  201. //折前金额(元)
  202. sqlFactory.addParameter("amount", price.multiply(qty));
  203. } else if (item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO) <= 0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO) > 0) {
  204. sqlFactory.addParameter("price", item.getBigDecimalValue("amount").divide(qty, 6, BigDecimal.ROUND_HALF_UP));
  205. //折前金额(元)
  206. sqlFactory.addParameter("amount", item.getBigDecimalValue("amount"));
  207. } else {
  208. sqlFactory.addParameter("price", price);
  209. //折前金额(元)
  210. sqlFactory.addParameter("amount", price.multiply(qty));
  211. }
  212. } else {
  213. sqlFactory.addParameter("price", price);
  214. //折前金额(元)
  215. sqlFactory.addParameter("amount", price.multiply(qty));
  216. }
  217. } else {
  218. sqlFactory.addParameter("price", price);
  219. //折前金额(元)
  220. sqlFactory.addParameter("amount", price.multiply(qty));
  221. }
  222. //单价,折后价(元),取合同价
  223. sqlFactory.addParameter("defaultprice", defaultprice);
  224. //金额,折后金额(元)
  225. sqlFactory.addParameter("defaultamount", defaultprice.multiply(qty));
  226. //牌价、市场价(元),标准订单牌价取商品价格,项目订单取合同里的牌价
  227. sqlFactory.addParameter("marketprice", itemPrice.getMarketprice());
  228. sqlList.add(sqlFactory.getSQL());
  229. }
  230. if (type.equals("促销订单")) {
  231. Rows promotionRows = dbConnect.runSqlQuery("select * from sa_promotion where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  232. if (!promotionRows.isEmpty()) {
  233. if (promotionRows.get(0).getString("type").equals("打包促销")) {
  234. BigDecimal orderaddqty = promotionRows.get(0).getBigDecimal("orderaddqty");
  235. BigDecimal orderminqty = promotionRows.get(0).getBigDecimal("orderminqty");
  236. if (((sumqty.subtract(orderminqty)).remainder(orderaddqty)).compareTo(BigDecimal.ZERO) != 0) {
  237. return getErrReturnObject().setErrMsg("不符合促销活动订购数量规则,修改不成功").toString();
  238. }
  239. }
  240. }
  241. }
  242. dbConnect.runSqlUpdate(sqlList);
  243. //重新排序
  244. updateRowNo(sa_orderid);
  245. //居间费设置
  246. updateRebateFee(sa_orderid);
  247. //调整价格(返利金)
  248. if (orderRows.get(0).getBoolean("rebate_used")) {
  249. setRebateAmount(this, sa_orderid, getMaxUsedRebateAmount(this, sa_orderid));
  250. adjustLastRebateAmount(this, sa_orderid);
  251. }
  252. // else {
  253. // dbConnect.runSqlUpdate("UPDATE sa_orderitems SET amount=amount-rebateamount,price=amount/qty WHERE sa_orderid='" + sa_orderid + "' and siteid='" + siteid + "' and qty!=0");
  254. // dbConnect.runSqlUpdate("UPDATE sa_orderitems SET amount=0 WHERE sa_orderid='" + sa_orderid + "' and siteid='" + siteid + "' and qty=0");
  255. // }
  256. return getSucReturnObject().toString();
  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. JSONArray sa_orderitemsids = content.getJSONArray("sa_orderitemsids");
  304. SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_删除");
  305. sqlFactory.addParameter("siteid", siteid);
  306. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsids.toArray());
  307. String sql = sqlFactory.getSQL();
  308. dbConnect.runSqlUpdate(sql);
  309. //重新排序
  310. updateRowNo(sa_orderid);
  311. //居间费设置
  312. updateRebateFee(sa_orderid);
  313. //调整价格(返利金)
  314. Rows orderRows = getOrderRows(this, sa_orderid);
  315. if (orderRows.get(0).getBoolean("rebate_used")) {
  316. setRebateAmount(this, sa_orderid, getMaxUsedRebateAmount(this, sa_orderid));
  317. adjustLastRebateAmount(this, sa_orderid);
  318. }
  319. // else {
  320. // dbConnect.runSqlUpdate("UPDATE sa_orderitems SET amount=amount-rebateamount,price=amount/qty WHERE sa_orderid='" + sa_orderid + "' and siteid='" + siteid + "' and qty!=0");
  321. // dbConnect.runSqlUpdate("UPDATE sa_orderitems SET amount=0 WHERE sa_orderid='" + sa_orderid + "' and siteid='" + siteid + "' and qty=0");
  322. // }
  323. return getSucReturnObject().toString();
  324. }
  325. @API(title = "商品行冻结", apiversion = R.ID20230508093003.v1.class)
  326. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class})
  327. public String freeze() throws YosException {
  328. Long sa_orderid = content.getLong("sa_orderid");
  329. JSONArray sa_orderitemsids = content.getJSONArray("sa_orderitemsids");
  330. boolean isfreeze = content.getBooleanValue("isfreeze");
  331. Rows orderRows = dbConnect.runSqlQuery("select * from sa_order where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  332. if (!orderRows.isEmpty()) {
  333. if (!orderRows.get(0).getString("status").equals("审核")) {
  334. if (isfreeze) {
  335. return getErrReturnObject().setErrMsg("非审核状态无法进行冻结").toString();
  336. } else {
  337. return getErrReturnObject().setErrMsg("非审核状态无法进行反冻结").toString();
  338. }
  339. }
  340. } else {
  341. return getErrReturnObject().setErrMsg("该订单不存在").toString();
  342. }
  343. SQLFactory sqlFactory;
  344. if (isfreeze) {
  345. sqlFactory = new SQLFactory(this, "订单商品明细_冻结");
  346. } else {
  347. sqlFactory = new SQLFactory(this, "订单商品明细_反冻结");
  348. }
  349. sqlFactory.addParameter("siteid", siteid);
  350. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsids.toArray());
  351. String sql = sqlFactory.getSQL();
  352. System.out.println(sql);
  353. dbConnect.runSqlUpdate(sql);
  354. return getSucReturnObject().toString();
  355. }
  356. @API(title = "商品行折扣变更", apiversion = R.ID20230508101703.v1.class)
  357. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class})
  358. public String updateOrderItemsDiscountrate() throws YosException {
  359. Long sa_orderid = content.getLong("sa_orderid");
  360. BigDecimal discountrate = content.getBigDecimal("discountrate");
  361. JSONArray itemids = content.getJSONArray("itemids");
  362. Rows orderRows = dbConnect.runSqlQuery("select * from sa_order where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  363. if (!orderRows.isEmpty()) {
  364. if (!orderRows.get(0).getString("status").equals("新建") && !orderRows.get(0).getString("status").equals("预提交")) {
  365. return getErrReturnObject().setErrMsg("非新建或预提交状态无法进行商品行折扣变更").toString();
  366. }
  367. } else {
  368. return getErrReturnObject().setErrMsg("该订单不存在").toString();
  369. }
  370. String sql;
  371. if (itemids.size() == 0) {
  372. sql = "select * from sa_orderitems where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'";
  373. } else {
  374. sql = "select * from sa_orderitems where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "' and itemid in " + itemids.toJSONString();
  375. sql = sql.replace("[", "(").replace("]", ")");
  376. }
  377. Rows orderItemsRows = dbConnect.runSqlQuery(sql);
  378. ArrayList<String> sqlList = new ArrayList<>();
  379. if (!orderItemsRows.isEmpty()) {
  380. for (Row row : orderItemsRows) {
  381. BigDecimal price = row.getBigDecimal("price").multiply(discountrate);
  382. BigDecimal amount = row.getBigDecimal("amount").multiply(discountrate);
  383. sqlList.add("update sa_orderitems set price=" + price + ",amount=" + amount + " where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  384. }
  385. }
  386. dbConnect.runSqlUpdate(sqlList);
  387. return getSucReturnObject().toString();
  388. }
  389. @API(title = "商品行价格刷新(变为原价)", apiversion = R.ID20230518151803.v1.class)
  390. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class})
  391. public String updateOrderItemsPrice() throws YosException {
  392. Long sa_orderid = content.getLong("sa_orderid");
  393. JSONArray itemids = content.getJSONArray("itemids");
  394. Rows orderRows = dbConnect.runSqlQuery("select * from sa_order where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  395. if (!orderRows.isEmpty()) {
  396. if (!orderRows.get(0).getString("status").equals("新建") && !orderRows.get(0).getString("status").equals("预提交")) {
  397. return getErrReturnObject().setErrMsg("非新建或预提交状态无法进行商品行价格刷新").toString();
  398. }
  399. } else {
  400. return getErrReturnObject().setErrMsg("该订单不存在").toString();
  401. }
  402. String sql;
  403. if (itemids.size() == 0) {
  404. sql = "select * from sa_orderitems where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'";
  405. } else {
  406. sql = "select * from sa_orderitems where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "' and itemid in " + itemids.toJSONString();
  407. sql = sql.replace("[", "(").replace("]", ")");
  408. }
  409. Rows orderItemsRows = dbConnect.runSqlQuery(sql);
  410. ArrayList<String> sqlList = new ArrayList<>();
  411. if (!orderItemsRows.isEmpty()) {
  412. for (Row row : orderItemsRows) {
  413. //价格
  414. ItemPrice itemPrice = ItemPrice.getItemPrice(this, orderRows.get(0).getLong("sys_enterpriseid"), row.getLong("itemid"));
  415. BigDecimal price = itemPrice.getPrice();
  416. BigDecimal amount = row.getBigDecimal("qty").multiply(price);
  417. sqlList.add("update sa_orderitems set price=" + price + ",amount=" + amount + " where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  418. }
  419. }
  420. dbConnect.runSqlUpdate(sqlList);
  421. return getSucReturnObject().toString();
  422. }
  423. @API(title = "关闭", apiversion = R.ID20221109093802.v1.class)
  424. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class})
  425. public String close() throws YosException {
  426. Long sa_orderid = content.getLong("sa_orderid");
  427. JSONArray sa_orderitemsids = content.getJSONArray("sa_orderitemsids");
  428. SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_行关闭");
  429. sqlFactory.addParameter("siteid", siteid);
  430. sqlFactory.addParameter("userid", userid);
  431. sqlFactory.addParameter("username", username);
  432. sqlFactory.addParameter("closereason", content.getStringValue("closereason"));
  433. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsids.toArray());
  434. dbConnect.runSqlUpdate(sqlFactory);
  435. //重新排序
  436. updateRowNo(sa_orderid);
  437. return getSucReturnObject().toString();
  438. }
  439. @API(title = "查询列表", apiversion = R.ID20221109093902.v1.class)
  440. public String selectList() throws YosException, IOException {
  441. StringBuffer where = new StringBuffer(" 1=1 ");
  442. if (content.containsKey("where")) {
  443. JSONObject whereObject = content.getJSONObject("where");
  444. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  445. where.append(" and(");
  446. where.append("t1.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  447. where.append("or t1.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  448. where.append("or t1.model like'%").append(whereObject.getString("condition")).append("%' ");
  449. where.append("or t3.spec like'%").append(whereObject.getString("condition")).append("%' ");
  450. where.append(")");
  451. }
  452. }
  453. Long sa_orderid = content.getLong("sa_orderid");
  454. Rows orderRows = getOrderRows(this, sa_orderid);
  455. if (orderRows.isNotEmpty()) {
  456. sys_enterpriseid = orderRows.get(0).getLong("sys_enterpriseid");
  457. }
  458. boolean isExport = content.getBooleanValue("isExport");
  459. SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_列表", pageSize, pageNumber, pageSorting);
  460. if (isExport) {
  461. sqlFactory = new SQLFactory(this, "订单商品明细_列表");
  462. }
  463. sqlFactory.addParameter("siteid", siteid);
  464. sqlFactory.addParameter("sa_orderid", sa_orderid);
  465. sqlFactory.addParameter_SQL("where", where);
  466. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  467. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  468. RowsMap rowsMap = rows.toRowsMap("itemno");
  469. SQLFactory sqlFactory1 = new SQLFactory(this, "商品发货数量汇总");
  470. sqlFactory1.addParameter("siteid", siteid);
  471. Rows sumQtyRows = dbConnect.runSqlQuery(sqlFactory1);
  472. RowsMap sumQtyRowsMap = sumQtyRows.toRowsMap("itemno");
  473. SQLFactory sqlFactory2 = new SQLFactory(this, "商品未发货数量汇总");
  474. sqlFactory2.addParameter("siteid", siteid);
  475. Rows sumUnQtyRows = dbConnect.runSqlQuery(sqlFactory2);
  476. RowsMap sumUnQtyRowsMap = sumUnQtyRows.toRowsMap("itemno");
  477. ERPDocking erpDocking = new ERPDocking();
  478. JSONArray jsonArray = new JSONArray();
  479. if (rows.toJsonArray("itemno").size() != 0) {
  480. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true") && siteid.equalsIgnoreCase("ccyosg")) {
  481. jsonArray = erpDocking.getErpIcinvbalRows(200000, 1, rows.toJsonArray("itemno"));
  482. }
  483. }
  484. if (!jsonArray.isEmpty()) {
  485. for (Object object : jsonArray) {
  486. JSONObject jsonObject = (JSONObject) object;
  487. if (rowsMap.containsKey(jsonObject.getString("fitemno"))) {
  488. if (rowsMap.get(jsonObject.getString("fitemno")).isNotEmpty()) {
  489. rowsMap.get(jsonObject.getString("fitemno")).get(0).put("invbalqty", jsonObject.getBigDecimalValue("fqty"));
  490. }
  491. }
  492. }
  493. }
  494. //查询附件
  495. RowsMap attinfoRowsMap = getAttachmentUrl("plm_item", ids);
  496. for (Row row : rows) {
  497. if (sumQtyRowsMap.get(row.getString("itemno")).isNotEmpty()) {
  498. row.put("unsoldqty", sumQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("qty"));
  499. } else {
  500. row.put("unsoldqty", BigDecimal.ZERO);
  501. }
  502. if (sumUnQtyRowsMap.get(row.getString("itemno")).isNotEmpty()) {
  503. row.put("undeliqtysum", sumUnQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("undeliqty"));
  504. } else {
  505. row.put("undeliqtysum", BigDecimal.ZERO);
  506. }
  507. if (!row.containsKey("invbalqty")) {
  508. row.put("invbalqty", BigDecimal.ZERO);
  509. }
  510. row.put("attinfos", attinfoRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  511. row.put("contractprice", 0);
  512. }
  513. if (isExport) {
  514. //去除不需要导出项
  515. String[] removeFieldList = {"sa_orderitemsid", "itemid", "sa_orderid", "candispatchqty", "cansaleqty",
  516. "totalaty", "orderminqty_auxunit", "orderminqty", "orderaddqty_auxunit", "orderaddqty",
  517. "spec", "delivery", "erpitemno", "erpitemname", "material", "standards", "stockstatus"};
  518. for (String key : removeFieldList) {
  519. rows.getFieldList().remove(key);
  520. }
  521. Rows uploadRows = uploadExcelToObs("order", "订单商品明细_列表", rows, getTitleMap());
  522. return getSucReturnObject().setData(uploadRows).toString();
  523. }
  524. return getSucReturnObject().setData(rows).toString();
  525. }
  526. @API(title = "查询订单所有明细列表", apiversion = R.ID20230508111703.v1.class)
  527. public String selectAllList() throws YosException, IOException {
  528. StringBuffer where = new StringBuffer(" 1=1 ");
  529. if (sys_enterpriseid > 0) {
  530. where.append(" and t5.sys_enterpriseid ='").append(sys_enterpriseid).append("' ");
  531. }
  532. if (content.containsKey("where")) {
  533. JSONObject whereObject = content.getJSONObject("where");
  534. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  535. where.append(" and(");
  536. where.append(" t5.sonum like'%").append(whereObject.getString("condition")).append("%' ");
  537. where.append("or t1.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  538. where.append("or t1.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  539. where.append("or t6.agentnum like'%").append(whereObject.getString("condition")).append("%' ");
  540. where.append("or t7.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  541. where.append(")");
  542. }
  543. if (whereObject.containsKey("iteminfo") && !"".equals(whereObject.getString("iteminfo"))) {
  544. where.append(" and(");
  545. where.append("t1.itemno like'%").append(whereObject.getString("iteminfo")).append("%' ");
  546. where.append("or t1.itemname like'%").append(whereObject.getString("iteminfo")).append("%' ");
  547. where.append(")");
  548. }
  549. if (whereObject.containsKey("agentinfo") && !"".equals(whereObject.getString("agentinfo"))) {
  550. where.append(" and(");
  551. where.append("t6.agentnum like'%").append(whereObject.getString("agentinfo")).append("%' ");
  552. where.append("or t7.enterprisename like'%").append(whereObject.getString("agentinfo")).append("%' ");
  553. where.append(")");
  554. }
  555. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  556. where.append(" and t5.billdate >='").append(whereObject.getString("begindate")).append("' ");
  557. }
  558. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  559. where.append(" and t5.billdate <='").append(whereObject.getString("enddate")).append("' ");
  560. }
  561. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  562. where.append(" and t5.status ='").append(whereObject.getString("status")).append("' ");
  563. }
  564. if (whereObject.containsKey("isfreeze") && !"".equals(whereObject.getString("isfreeze"))) {
  565. where.append(" and t1.isfreeze ='").append(whereObject.getString("isfreeze")).append("' ");
  566. }
  567. }
  568. boolean isExport = content.getBooleanValue("isExport");
  569. SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_所有列表", pageSize, pageNumber, pageSorting);
  570. if (isExport) {
  571. sqlFactory = new SQLFactory(this, "订单商品明细_所有列表");
  572. }
  573. sqlFactory.addParameter("siteid", siteid);
  574. sqlFactory.addParameter_SQL("where", where);
  575. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  576. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  577. RowsMap rowsMap = rows.toRowsMap("itemno");
  578. SQLFactory sqlFactory1 = new SQLFactory(this, "订单明细发货数量汇总");
  579. sqlFactory1.addParameter("siteid", siteid);
  580. Rows sumQtyRows = dbConnect.runSqlQuery(sqlFactory1);
  581. RowsMap sumQtyRowsMap = sumQtyRows.toRowsMap("sa_orderitemsid");
  582. ERPDocking erpDocking = new ERPDocking();
  583. JSONArray jsonArray = new JSONArray();
  584. if (rows.toJsonArray("itemno").size() != 0) {
  585. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true") && siteid.equalsIgnoreCase("ccyosg")) {
  586. jsonArray = erpDocking.getErpIcinvbalRows(200000, 1, rows.toJsonArray("itemno"));
  587. }
  588. }
  589. if (!jsonArray.isEmpty()) {
  590. for (Object object : jsonArray) {
  591. JSONObject jsonObject = (JSONObject) object;
  592. if (rowsMap.containsKey(jsonObject.getString("fitemno"))) {
  593. if (rowsMap.get(jsonObject.getString("fitemno")).isNotEmpty()) {
  594. rowsMap.get(jsonObject.getString("fitemno")).get(0).put("invbalqty", jsonObject.getBigDecimalValue("fqty"));
  595. }
  596. }
  597. }
  598. }
  599. //查询附件
  600. RowsMap attinfoRowsMap = getAttachmentUrl("plm_item", ids);
  601. for (Row row : rows) {
  602. if (sumQtyRowsMap.get(row.getString("sa_orderitemsid")).isNotEmpty()) {
  603. row.put("unsoldqty", sumQtyRowsMap.get(row.getString("sa_orderitemsid")).get(0).getBigDecimal("qty"));
  604. } else {
  605. row.put("unsoldqty", BigDecimal.ZERO);
  606. }
  607. if (!row.containsKey("invbalqty")) {
  608. row.put("invbalqty", BigDecimal.ZERO);
  609. }
  610. row.put("attinfos", attinfoRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  611. row.put("contractprice", 0);
  612. }
  613. if (isExport) {
  614. //去除不需要导出项
  615. rows.getFieldList().remove("sa_orderitemsid");
  616. rows.getFieldList().remove("itemid");
  617. rows.getFieldList().remove("batchcontrol");
  618. rows.getFieldList().remove("model");
  619. rows.getFieldList().remove("conversionrate");
  620. rows.getFieldList().remove("marketprice");
  621. rows.getFieldList().remove("defaultprice");
  622. rows.getFieldList().remove("defaultamount");
  623. rows.getFieldList().remove("auxqty");
  624. rows.getFieldList().remove("needdate");
  625. rows.getFieldList().remove("deliedqty");
  626. rows.getFieldList().remove("rebateamount");
  627. rows.getFieldList().remove("invoiceamount");
  628. rows.getFieldList().remove("writeoffamount");
  629. rows.getFieldList().remove("auxunit");
  630. rows.getFieldList().remove("sa_orderid");
  631. rows.getFieldList().remove("deliverydate");
  632. rows.getFieldList().remove("returnqty");
  633. rows.getFieldList().remove("stockno");
  634. rows.getFieldList().remove("position");
  635. rows.getFieldList().remove("batchno");
  636. rows.getFieldList().remove("isfreeze");
  637. rows.getFieldList().remove("candispatchqty");
  638. rows.getFieldList().remove("cansaleqty");
  639. rows.getFieldList().remove("totalaty");
  640. rows.getFieldList().remove("orderminqty_auxunit");
  641. rows.getFieldList().remove("orderminqty");
  642. rows.getFieldList().remove("orderaddqty_auxunit");
  643. rows.getFieldList().remove("orderaddqty");
  644. rows.getFieldList().remove("spec");
  645. rows.getFieldList().remove("packageqty");
  646. rows.getFieldList().remove("delivery");
  647. rows.getFieldList().remove("erpitemno");
  648. rows.getFieldList().remove("erpitemname");
  649. rows.getFieldList().remove("prodline");
  650. rows.getFieldList().remove("material");
  651. rows.getFieldList().remove("device");
  652. rows.getFieldList().remove("specalnote");
  653. rows.getFieldList().remove("standards");
  654. rows.getFieldList().remove("agentnum");
  655. rows.getFieldList().remove("enterprisename");
  656. rows.getFieldList().remove("agentnum");
  657. rows.getFieldList().remove("stockstatus");
  658. rows.getFieldList().remove("submitdate");
  659. rows.getFieldList().remove("checkdate");
  660. 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);
  661. if(!rowsrolename.isEmpty()){
  662. rows.getFieldList().remove("price");
  663. rows.getFieldList().remove("amount");
  664. }
  665. Rows uploadRows = uploadExcelToObs("orderdetail", "订单明细列表", rows, getTitleMapAgent());
  666. return getSucReturnObject().setData(uploadRows).toString();
  667. }
  668. return getSucReturnObject().setData(rows).toString();
  669. }
  670. //返回导出的标题
  671. public HashMap<String, String> getTitleMap() {
  672. HashMap<String, String> titleMap = new HashMap<>();
  673. titleMap.put("rowno", "行号");
  674. titleMap.put("batchcontrol", "是否批次管理");
  675. titleMap.put("itemno", "产品编号");
  676. titleMap.put("itemname", "产品名称");
  677. titleMap.put("model", "型号");
  678. titleMap.put("conversionrate", "换算率(辅助数量=主单位数量/换算率)");
  679. titleMap.put("price", "单价,折后价(元)");
  680. titleMap.put("amount", "金额,折后金额(元)");
  681. titleMap.put("marketprice", "牌价、市场价(元)");
  682. titleMap.put("defaultprice", "折前价(元)");
  683. titleMap.put("defaultamount", "折前金额(元)");
  684. titleMap.put("qty", "订购数量");
  685. titleMap.put("auxqty", "辅助单位数量");
  686. titleMap.put("needdate", "需求日期");
  687. titleMap.put("deliedqty", "已交货量(主单位)");
  688. titleMap.put("rebateamount", "返利分摊金额");
  689. titleMap.put("undeliqty", "未交货量(主单位)");
  690. titleMap.put("invoiceamount", "已开票金额");
  691. titleMap.put("writeoffamount", "已核销金额");
  692. titleMap.put("unit", "计量单位名称");
  693. titleMap.put("auxunit", "辅助计量单位名称");
  694. titleMap.put("deliverydate", "交货日期");
  695. titleMap.put("returnqty", "退货数量");
  696. titleMap.put("remarks", "备注");
  697. titleMap.put("submitdate", "提交时间");
  698. titleMap.put("checkdate", "审核时间");
  699. return titleMap;
  700. }
  701. //返回导出的标题
  702. public HashMap<String, String> getTitleMapAgent() throws YosException {
  703. HashMap<String, String> titleMap = new HashMap<>();
  704. titleMap.put("rowno", "行号");
  705. titleMap.put("sonum", "订单号");
  706. titleMap.put("status", "订单状态");
  707. titleMap.put("type", "订单类型");
  708. titleMap.put("billdate", "单据日期");
  709. titleMap.put("tradefield", "领域");
  710. titleMap.put("itemno", "产品编码");
  711. titleMap.put("itemname", "品名");
  712. titleMap.put("unit", "单位");
  713. titleMap.put("qty", "数量");
  714. titleMap.put("undeliqty", "未发货数量");
  715. 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);
  716. if(rowsrolename.isEmpty()){
  717. titleMap.put("price", "单价");
  718. titleMap.put("amount", "金额");
  719. }
  720. titleMap.put("remarks", "订单行备注");
  721. return titleMap;
  722. }
  723. @API(title = "查询可添加商品列表", apiversion = R.ID20221109153502.v1.class)
  724. @CACHEING
  725. public String selectItemList() throws YosException {
  726. StringBuffer where = new StringBuffer(" 1=1 ");
  727. if (content.containsKey("where")) {
  728. JSONObject whereObject = content.getJSONObject("where");
  729. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  730. where.append(" and(");
  731. where.append("t1.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  732. where.append("or t1.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  733. where.append("or t1.model like'%").append(whereObject.getString("condition")).append("%' ");
  734. where.append("or t1.spec like'%").append(whereObject.getString("condition")).append("%' ");
  735. where.append("or t3.material like'%").append(whereObject.getString("condition")).append("%' ");
  736. where.append("or t3.erpitemno like'%").append(whereObject.getString("condition")).append("%' ");
  737. where.append(")");
  738. }
  739. if (whereObject.containsKey("isonsale") && !"".equals(whereObject.getString("isonsale"))) {
  740. where.append(" and t1.isonsale ='").append(whereObject.getString("isonsale")).append("' ");
  741. }
  742. }
  743. Long sa_orderid = content.getLong("sa_orderid");
  744. Long sys_enterpriseid1 = sys_enterpriseid;
  745. Rows orderRows = getOrderRows(this, sa_orderid);
  746. Long sa_brandid = 0L;
  747. String tradefield = "";
  748. String type = "";
  749. String typemx = "";
  750. Long sys_enterpriseid = 0L;
  751. if (orderRows.isNotEmpty()) {
  752. sa_brandid = orderRows.get(0).getLong("sa_brandid");
  753. tradefield = orderRows.get(0).getString("tradefield");
  754. type = orderRows.get(0).getString("type");
  755. typemx = orderRows.get(0).getString("typemx");
  756. sys_enterpriseid = orderRows.get(0).getLong("sys_enterpriseid");
  757. }
  758. SQLFactory sqlFactory = new SQLFactory(this, "查询可添加商品列表", pageSize, pageNumber, pageSorting);
  759. switch (type) {
  760. // case "总部订单":
  761. // sqlFactory = new SQLFactory(this, "查询可添加商品列表_总部订单", pageSize, pageNumber, pageSorting);
  762. // break;
  763. case "特殊订单":
  764. if (typemx.equals("电商") || typemx.equals("工程")) {
  765. sqlFactory = new SQLFactory(this, "查询可添加商品列表_总部订单", pageSize, pageNumber, pageSorting);
  766. } else {
  767. sqlFactory = new SQLFactory(this, "查询可添加商品列表_特殊订单", pageSize, pageNumber, pageSorting);
  768. }
  769. sqlFactory.addParameter("tradefield", tradefield);
  770. if (sys_enterpriseid1 > 0) {
  771. where.append(" and t1.isonsale ='1' ");
  772. }
  773. break;
  774. default:
  775. StringBuffer where2 = new StringBuffer(" 1=1 ");
  776. Row enterpriseRow = Enterprise.getEnterprise(this, sys_enterpriseid);
  777. boolean saleclassauth = enterpriseRow.getBoolean("saleclassauth");
  778. //需要经营授权
  779. if (saleclassauth) {
  780. where2.append(" and t1.itemclassid in (SELECT itemclassid from sys_enterprise_saleclass WHERE sys_enterpriseid = " + sys_enterpriseid + " and siteid = '" + siteid + "')");
  781. }
  782. sqlFactory.addParameter("sa_brandid", sa_brandid);
  783. sqlFactory.addParameter("tradefield", tradefield);
  784. sqlFactory.addParameter_SQL("where2", where2);
  785. break;
  786. }
  787. sqlFactory.addParameter("siteid", siteid);
  788. sqlFactory.addParameter_SQL("where", where);
  789. String sql = sqlFactory.getSQL(false);
  790. Rows rows = dbConnect.runSqlQuery(sql);
  791. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  792. //查询附件
  793. RowsMap attinfoRowsMap = getAttachmentUrl("plm_item", ids);
  794. //商品领域
  795. RowsMap tradefieldRowsMap = beans.Item.Item.getTradefieldRowsMap(this, ids);
  796. //品牌
  797. RowsMap brandRowsMap = Brand.getBrandRowsMap(this, ids);
  798. //价格
  799. HashMap<Long, ItemPrice> itemPriceRowsMap = ItemPrice.getItemPrice(this, sys_enterpriseid, ids);
  800. for (Row row : rows) {
  801. row.put("attinfos", attinfoRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  802. row.put("tradefield", tradefieldRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  803. row.put("brand", brandRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  804. if (itemPriceRowsMap.containsKey(row.getLong("itemid"))) {
  805. row.put("price", itemPriceRowsMap.get(row.getLong("itemid")).getPrice());
  806. } else {
  807. row.put("price", 0);
  808. }
  809. }
  810. return getSucReturnObject().setData(rows).toString();
  811. }
  812. //获取非工具数据
  813. public String getOrderItemsList(Long sa_orderid, String tradefield, Long sys_enterpriseid, Long sa_brandid) throws YosException {
  814. StringBuffer where = new StringBuffer(" 1=1 ");
  815. Row enterpriseRow = Enterprise.getEnterprise(this, sys_enterpriseid);
  816. boolean saleclassauth = enterpriseRow.getBoolean("saleclassauth");
  817. //需要经营授权
  818. if (saleclassauth) {
  819. where.append(" and itemclassid in (SELECT itemclassid from sys_enterprise_saleclass WHERE sys_enterpriseid = " + sys_enterpriseid + " and siteid = '" + siteid + "')");
  820. }
  821. SQLFactory sqlFactory = new SQLFactory(this, "商品范围");
  822. sqlFactory.addParameter("siteid", siteid);
  823. sqlFactory.addParameter("sa_orderid", sa_orderid);
  824. sqlFactory.addParameter_in("tradefield", tradefield);
  825. sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  826. sqlFactory.addParameter("sa_brandid", sa_brandid);
  827. sqlFactory.addParameter_SQL("where", where);
  828. return sqlFactory.getSQL(false);
  829. }
  830. // //获取特殊数据
  831. // public String getOrderItemsListSpec(Long sa_orderid, String tradefield, Long sys_enterpriseid) throws YosException {
  832. // SQLFactory sqlFactory = new SQLFactory(this, "商品范围_特殊");
  833. // sqlFactory.addParameter("siteid", siteid);
  834. // sqlFactory.addParameter("sa_orderid", sa_orderid);
  835. // sqlFactory.addParameter_in("tradefield", tradefield);
  836. // sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  837. // return sqlFactory.getSQL(false);
  838. // }
  839. @API(title = "订单商品变更", apiversion = R.ID20221110145302.v1.class)
  840. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class})
  841. public String changeItem() throws YosException {
  842. boolean isjiean = content.getBooleanValue("isjiean");
  843. Long sa_orderid = content.getLong("sa_orderid");
  844. String type = content.getString("type");
  845. Long sa_orderitems_changeid = createTableID("sa_orderitems_change");
  846. JSONArray itemifnos = content.getJSONArray("itemifnos");
  847. BigDecimal oldValue = new BigDecimal(0);
  848. Rows Rowstatus = dbConnect.runSqlQuery(
  849. "select * from sa_order where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  850. if (Rowstatus.isEmpty()) {
  851. return "订单不存在";
  852. } else {
  853. if (!Rowstatus.get(0).getString("status").equals("审核")) {
  854. return getErrReturnObject().setErrMsg("非审核状态的订单无法进行商品变更").toString();
  855. }
  856. }
  857. ArrayList<String> sqlList = new ArrayList<>();
  858. // 订单商品变更
  859. SQLFactory sqlFactory = new SQLFactory(this, "订单商品变更_新增");
  860. sqlFactory.addParameter("siteid", siteid);
  861. sqlFactory.addParameter("userid", userid);
  862. sqlFactory.addParameter("username", username);
  863. sqlFactory.addParameter("sa_orderitems_changeid", sa_orderitems_changeid);
  864. sqlFactory.addParameter("sa_orderid", sa_orderid);
  865. sqlFactory.addParameter("remarks", content.getStringValue("remarks"));
  866. sqlList.add(sqlFactory.getSQL());
  867. HashMap<Long, Row> orderdetailmap = new HashMap<>();
  868. HashMap<Long, BigDecimal> orderdetailchangeamount = new HashMap<>();
  869. Rows rowsOrder = dbConnect.runSqlQuery("select * from sa_order t1 where t1.sa_orderid=" + sa_orderid);
  870. BigDecimal ce = new BigDecimal(0);
  871. for (Object obj : itemifnos) {
  872. JSONObject jsonObject = (JSONObject) obj;
  873. Rows rows = dbConnect.runSqlQuery(
  874. "SELECT sa_orderitemsid,qty,deliedqty,undeliqty,writeoffamount,price from sa_orderitems WHERE sa_orderitemsid="
  875. + jsonObject.getLongValue("sa_orderitemsid"));
  876. if (rows.isNotEmpty()) {
  877. oldValue = rows.get(0).getBigDecimal("qty");
  878. } else {
  879. return getErrReturnObject().setErrMsg("订单行不存在").toString();
  880. }
  881. if (type.equals("数量")) {
  882. // 更新数量
  883. BigDecimal newqty = jsonObject.getBigDecimal("newvalue");
  884. BigDecimal oldqty = rows.get(0).getBigDecimal("qty");
  885. BigDecimal oldundeliqty = rows.get(0).getBigDecimal("undeliqty");
  886. BigDecimal addqty = newqty.subtract(oldqty);// 新增的数量
  887. if (newqty.compareTo(oldqty) >= 0) {
  888. return getErrReturnObject().setErrMsg("修改数量必须小于原订单数量" + oldqty).toString();
  889. }
  890. if (newqty.compareTo(oldqty.subtract(oldundeliqty)) < 0) {
  891. return getErrReturnObject().setErrMsg("修改数量必须大于等于已发货数量" + oldqty.subtract(oldundeliqty)).toString();
  892. }
  893. if (newqty.compareTo(BigDecimal.ZERO) < 0) {
  894. return getErrReturnObject().setErrMsg("修改数量不可小于0").toString();
  895. }
  896. sqlFactory = new SQLFactory(this, "订单商品明细变更_更新");
  897. sqlFactory.addParameter("siteid", siteid);
  898. sqlFactory.addParameter("sa_orderitemsid", jsonObject.getLongValue("sa_orderitemsid"));
  899. sqlFactory.addParameter("qty", jsonObject.getStringValue("newvalue"));
  900. // sqlFactory.addParameter("undeliqty", oldundeliqty.add(addqty));
  901. ce = ce.add(addqty.multiply(rows.get(0).getBigDecimal("price")));
  902. sqlList.add(sqlFactory.getSQL());
  903. /**
  904. * * 核销对冲计算
  905. */
  906. {
  907. /**
  908. * * 核销对冲计算
  909. */
  910. BigDecimal writeoffamount = rows.get(0).getBigDecimal("writeoffamount");// 订单行已核销金额
  911. //System.out.println(newqty.multiply(rows.get(0).getBigDecimal("price")));
  912. //System.out.println(writeoffamount);
  913. if (writeoffamount.compareTo(BigDecimal.ZERO) > 0
  914. && (newqty.multiply(rows.get(0).getBigDecimal("price"))).compareTo(writeoffamount) < 0) {
  915. // 如果已核销金额大于0 且调整后折后金额小于已核销金额,则需要将差价进行回冲
  916. orderdetailmap.put(rows.get(0).getLong("sa_orderitemsid"), rows.get(0));
  917. //System.out.println(writeoffamount.subtract(newqty.multiply(rows.get(0).getBigDecimal("price"))));
  918. //System.out.println("sa_orderitemsid:"+rows.get(0).getLong("sa_orderitemsid"));
  919. orderdetailchangeamount.put(rows.get(0).getLong("sa_orderitemsid"),
  920. writeoffamount.subtract(newqty.multiply(rows.get(0).getBigDecimal("price"))));
  921. }
  922. }
  923. }
  924. // 订单商品变更明细
  925. sqlFactory = new SQLFactory(this, "订单商品变更明细_新增");
  926. sqlFactory.addParameter("siteid", siteid);
  927. sqlFactory.addParameter("sa_orderitems_changemxid", createTableID("sa_orderitems_changemx"));
  928. sqlFactory.addParameter("itemid", jsonObject.getLongValue("itemid"));
  929. sqlFactory.addParameter("sa_orderitemsid", jsonObject.getLongValue("sa_orderitemsid"));
  930. sqlFactory.addParameter("sa_orderitems_changeid", sa_orderitems_changeid);
  931. sqlFactory.addParameter("type", type);
  932. sqlFactory.addParameter("oldvalue", oldValue);
  933. sqlFactory.addParameter("newvalue", jsonObject.getStringValue("newvalue"));
  934. sqlList.add(sqlFactory.getSQL());
  935. // content.put("sa_orderitemsid", sa_orderitemsid);
  936. }
  937. if (ce.compareTo(BigDecimal.ZERO) != 0) {
  938. CashbillEntity cashbillEntity = new CashbillEntity();
  939. cashbillEntity.setAmount(ce);
  940. cashbillEntity.setOwnerid(sa_orderid);
  941. cashbillEntity.setOwnertable("sa_order");
  942. cashbillEntity
  943. .setRemarks("订单变更:" + rowsOrder.get(0).getString("sonum") + "订单变更单id:" + sa_orderitems_changeid);
  944. sqlList.addAll(Accountbalance.createCashbillPay(this, rowsOrder.get(0).getLong("sys_enterpriseid"),
  945. rowsOrder.get(0).getLong("sa_accountclassid"), cashbillEntity, true));
  946. }
  947. /**
  948. * * 核销对冲
  949. */
  950. if (!orderdetailmap.isEmpty()) {
  951. //System.out.println("测试");
  952. sqlList.addAll(
  953. new twriteoffbill_orderchange(rowsOrder.get(0), orderdetailmap, orderdetailchangeamount).hedging());
  954. }
  955. for (String string : sqlList) {
  956. //System.out.println(string);
  957. }
  958. dbConnect.runSqlUpdate(sqlList);
  959. // 居间费设置
  960. updateRebateFee(sa_orderid);
  961. if (!isjiean) {
  962. if (Parameter.get("system.erp_dockswitch").equalsIgnoreCase("true")) {
  963. UploadDataToERP uploadDataToERP = new UploadDataToERP(this, sa_orderitems_changeid, "订单变更");
  964. boolean issuccess = uploadDataToERP.upload();
  965. if (!issuccess) {
  966. return getErrReturnObject().setErrMsg("上传ERP失败,请检查!").toString();
  967. }
  968. }
  969. }
  970. return getSucReturnObject().toString();
  971. }
  972. @API(title = "订单商品变更(手动推erp)", apiversion = R.ID20230306161703.v1.class)
  973. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class})
  974. public String changeItem1() throws YosException {
  975. if (Parameter.get("system.erp_dockswitch").equalsIgnoreCase("true")) {
  976. UploadDataToERP uploadDataToERP = new UploadDataToERP(this, content.getLong("sa_orderitems_changeid"), "订单变更");
  977. boolean issuccess = uploadDataToERP.upload();
  978. if (!issuccess) {
  979. return getErrReturnObject().setErrMsg("上传ERP失败,请检查!").toString();
  980. } else {
  981. return getSucReturnObject().toString();
  982. }
  983. } else {
  984. return getErrReturnObject().setErrMsg("erp对接开关未开!").toString();
  985. }
  986. }
  987. /**
  988. * 获取当前订单的最大行号
  989. *
  990. * @param sa_orderid
  991. * @return
  992. * @throws YosException
  993. */
  994. public Long getRowNo(Long sa_orderid) throws YosException {
  995. Rows rows = dbConnect.runSqlQuery("SELECT COUNT(0) count from sa_orderitems WHERE sa_orderid = " + sa_orderid + " AND siteid = '" + siteid + "'");
  996. return rows.get(0).getLong("count") + 1;
  997. }
  998. /**
  999. * 查询商品信息
  1000. *
  1001. * @param itemid
  1002. * @return
  1003. * @throws YosException
  1004. */
  1005. public Row getItemRow(Long itemid) throws YosException {
  1006. SQLFactory sqlFactory = new SQLFactory(this, "查询商品信息");
  1007. sqlFactory.addParameter("siteid", siteid);
  1008. sqlFactory.addParameter("itemid", itemid);
  1009. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL(false));
  1010. return rows.isNotEmpty() ? rows.get(0) : new Row();
  1011. }
  1012. /**
  1013. * 重新对商品行排序
  1014. *
  1015. * @param sa_orderid
  1016. * @throws YosException
  1017. */
  1018. public void updateRowNo(Long sa_orderid) throws YosException {
  1019. String sql = "SELECT sa_orderitemsid from sa_orderitems WHERE sa_orderid= " + sa_orderid + " and siteid = '" + siteid + "' ORDER BY createdate ";
  1020. ArrayList<Long> sa_orderitemsids = dbConnect.runSqlQuery(sql).toArrayList("sa_orderitemsid", new ArrayList<>());
  1021. if (sa_orderitemsids.size() > 0) {
  1022. int rowno = 1;
  1023. StringBuffer where = new StringBuffer("");
  1024. for (Long id : sa_orderitemsids) {
  1025. where.append(" WHEN " + id + " THEN " + rowno);
  1026. rowno++;
  1027. }
  1028. SQLFactory sqlFactory = new SQLFactory(this, "更新行号");
  1029. sqlFactory.addParameter("siteid", siteid);
  1030. sqlFactory.addParameter_SQL("where", where);
  1031. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsids);
  1032. sql = sqlFactory.getSQL(false);
  1033. dbConnect.runSqlUpdate(sql);
  1034. }
  1035. }
  1036. }