OrderItems.java 79 KB

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