OrderItems.java 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938
  1. package restcontroller.webmanage.sale.order;
  2. import beans.Item.Item;
  3. import beans.attachment.Attachment;
  4. import beans.brand.Brand;
  5. import beans.datacontrllog.DataContrlLog;
  6. import beans.enterprise.Enterprise;
  7. import beans.itemprice.ItemPrice;
  8. import com.alibaba.fastjson2.JSONArray;
  9. import com.alibaba.fastjson2.JSONObject;
  10. import common.Controller;
  11. import common.YosException;
  12. import common.annotation.API;
  13. import common.annotation.CACHEING;
  14. import common.annotation.CACHEING_CLEAN;
  15. import common.data.*;
  16. import org.apache.commons.lang.StringUtils;
  17. import restcontroller.R;
  18. import restcontroller.webmanage.sale.accessoryorder.accessoryorder;
  19. import restcontroller.webmanage.sale.rebate.Rebate;
  20. import restcontroller.webmanage.sale.toolbill.ToolBill;
  21. import utility.ERPDocking;
  22. import java.io.IOException;
  23. import java.math.BigDecimal;
  24. import java.math.RoundingMode;
  25. import java.util.ArrayList;
  26. import java.util.Date;
  27. import java.util.HashMap;
  28. import static beans.order.Order.*;
  29. /**
  30. * 销售订单商品明细表
  31. */
  32. public class OrderItems extends Controller {
  33. /**
  34. * 构造函数
  35. *
  36. * @param content
  37. */
  38. public OrderItems(JSONObject content) throws YosException {
  39. super(content);
  40. }
  41. @API(title = "配件订单明细新增或更新", apiversion = R.ID2025121314111503.v1.class, intervaltime = 200)
  42. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class, accessoryorder.class})
  43. public String insertOrUpdate_pj() throws YosException {
  44. Long sa_orderid = content.getLong("sa_orderid");
  45. Long sys_enterpriseid = content.getLong("sys_enterpriseid");
  46. JSONArray items = content.getJSONArray("items");
  47. //辅助类
  48. OrderItemsHelper orderItemsHelper = new OrderItemsHelper(this);
  49. ArrayList<String> sqlList = new ArrayList<>();
  50. Rows orderRows = getOrderRows(this, sa_orderid);
  51. if (orderRows.isEmpty()) {
  52. return getErrReturnObject().setErrMsg("数据不存在,无法添加").toString();
  53. } else {
  54. if (!orderRows.get(0).getString("status").equals("新建") && !orderRows.get(0).getString("status").equals("提交") && !orderRows.get(0).getString("status").equals("预提交")) {
  55. return getErrReturnObject().setErrMsg("该状态无法操作").toString();
  56. }
  57. }
  58. //通过版本更新订单表头数据
  59. beans.order.Order.updateOrderWithVersion(this);
  60. String type = orderRows.get(0).getString("type");
  61. //批量查询商品信息
  62. RowsMap itemRowsMap = orderItemsHelper.getItemRowsMap(items);
  63. BigDecimal sumqty = BigDecimal.ZERO;
  64. ArrayList<Long> sa_orderitemsids_insert = new ArrayList<>();
  65. for (Object obj : items) {
  66. JSONObject item = (JSONObject) obj;
  67. Long itemid = item.getLong("itemid");
  68. Long sa_orderitemsid = item.getLong("sa_orderitemsid");
  69. BigDecimal qty = item.getBigDecimal("qty");
  70. sumqty = sumqty.add(qty);
  71. Row itemRow = getItemRow(itemid);
  72. if (type.equals("标准订单") && itemRow.getString("ismodule").equals("1")) {
  73. return getErrReturnObject().setErrMsg("商品编号【" + itemRow.getString("itemno") + "】是配件,无法添加").toString();
  74. }
  75. if (type.equals("配件订单") && itemRow.getString("ismodule").equals("0")) {
  76. return getErrReturnObject().setErrMsg("商品编号【" + itemRow.getString("itemno") + "】不是配件,无法添加").toString();
  77. }
  78. Boolean isInsert = true;
  79. JSONArray customproperties = new JSONArray();
  80. if (sa_orderitemsid <= 0) {
  81. // Rows rows = new Rows();
  82. // if(!type.equals("配件订单")){
  83. // 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"));
  84. // }else{
  85. // if(StringUtils.isBlank(item.getStringValue("cardno"))){
  86. // rows= dbConnect.runSqlQuery("SELECT t1.sa_orderitemsid,qty from sa_orderitems t1 WHERE t1.siteid='" + siteid + "' and ifnull(cardno,'')='' and t1.itemid=" + itemid + " and t1.sa_orderid=" + sa_orderid);
  87. // }else{
  88. // rows= dbConnect.runSqlQuery("SELECT t1.sa_orderitemsid,qty from sa_orderitems t1 WHERE t1.siteid='" + siteid + "' and t1.itemid=" + itemid + " and t1.sa_orderid=" + sa_orderid + " and ifnull(t1.cardno,'')='"+item.getStringValue("cardno")+"'");
  89. // }
  90. //
  91. // }
  92. sa_orderitemsid = createTableID("sa_orderitems");
  93. isInsert = true;
  94. // if (rows.isEmpty()) {
  95. // sa_orderitemsid = createTableID("sa_orderitems");
  96. // isInsert = true;
  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. // item.put("qty",item.getBigDecimal("qty").add(rows.get(0).getBigDecimal("qty")));
  106. // isInsert = false;
  107. // }
  108. //
  109. // }
  110. } else {
  111. isInsert = false;
  112. }
  113. if (!isInsert) {
  114. if (dbConnect.runSqlQuery("SELECT 1 from sa_orderitems WHERE itemid=" + itemid + " and sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'").isEmpty()) {
  115. //continue;
  116. }
  117. }
  118. //是否符合商品起订量和增量判断
  119. orderItemsHelper.compareQty(type, itemRowsMap, item, itemRow, new Rows());
  120. BigDecimal defaultprice;
  121. BigDecimal price;
  122. BigDecimal saleprice;
  123. //价格
  124. ItemPrice itemPrice = ItemPrice.getItemPrice(this, sys_enterpriseid, itemid);
  125. price = orderItemsHelper.getPrice(orderRows.get(0), itemPrice, item);
  126. saleprice = orderItemsHelper.getSalePrice(orderRows.get(0), itemPrice, item, itemRowsMap);
  127. if (price.compareTo(BigDecimal.ZERO) < 0) {
  128. return getErrReturnObject().setErrMsg("价格不可小于0").toString();
  129. }
  130. defaultprice = price;
  131. if (isInsert) {
  132. sa_orderitemsids_insert.add(sa_orderitemsid);
  133. sqlList.add(orderItemsHelper.getInsertSQL(customproperties, sa_orderitemsid, item, itemRow, type, defaultprice, price, saleprice, itemPrice.getMarketprice(), "", new Rows(), 0, 0, BigDecimal.ZERO, BigDecimal.valueOf(1), BigDecimal.valueOf(1)).getSQL());
  134. if (item.getLongValue("sa_workorderid") != 0) {
  135. sqlList.add("update sa_workorder_node_items set sourceid=" + sa_orderid + ",sourcetable='sa_order' where sa_workorderid=" + item.getLongValue("sa_workorderid") + " and itemid=" + item.getLong("itemid"));
  136. }
  137. } else {
  138. Rows itemidRows = dbConnect.runSqlQuery("select itemid from sa_orderitems where sa_orderitemsid=" + sa_orderitemsid);
  139. price = item.getBigDecimal("price");
  140. defaultprice = item.getBigDecimal("defaultprice");
  141. if (itemidRows.isNotEmpty()) {
  142. if (itemidRows.get(0).getLong("itemid") != itemid) {
  143. defaultprice = orderItemsHelper.getPrice(orderRows.get(0), itemPrice, item);
  144. price = orderItemsHelper.getPrice(orderRows.get(0), itemPrice, item);
  145. }
  146. }
  147. sqlList.add(orderItemsHelper.getUpdateSQL(sa_orderitemsid, item, itemRow, type, defaultprice, price, saleprice, 0, 0).getSQL());
  148. // if(StringUtils.isNotBlank(item.getStringValue("cardno"))){
  149. // Rows aftersalesbomrows = dbConnect.runSqlQuery("select t2.sa_aftersalesbomid from sa_warrantycard t1 inner join sa_aftersalesbom_items t2 on t1.itemid=t2.itemid inner join sa_aftersalesbom t3 on t2.sa_aftersalesbomid=t3.sa_aftersalesbomid where t1.cardno='"+item.getStringValue("cardno")+"' and t3.status='审核'");
  150. // if(aftersalesbomrows.isNotEmpty()){
  151. // Rows aftersalesbompjrows = dbConnect.runSqlQuery("select qty from sa_aftersalesbom_pjs t1 where t1.sa_aftersalesbomid="+aftersalesbomrows.get(0).getString("sa_aftersalesbomid")+" and t1.itemid="+itemid);
  152. // if(aftersalesbompjrows.isNotEmpty()){
  153. // sqlList.add("update sa_orderitems set canoffqty="+(aftersalesbompjrows.get(0).getBigDecimal("qty").compareTo(qty)>0?qty:aftersalesbompjrows.get(0).getBigDecimal("qty"))+" where sa_orderitemsid=" + sa_orderitemsid);
  154. // }else{
  155. // sqlList.add("update sa_orderitems set canoffqty=1 where sa_orderitemsid=" + sa_orderitemsid);
  156. // }
  157. // }else{
  158. // sqlList.add("update sa_orderitems set canoffqty=1 where sa_orderitemsid=" + sa_orderitemsid);
  159. // }
  160. // }
  161. }
  162. }
  163. dbConnect.runSqlUpdate(sqlList);
  164. //重新排序
  165. updateRowNo(sa_orderid);
  166. //更新订单扣款信息
  167. updateAccountclassinfos(sa_orderid);
  168. if (type.equals("配件订单")) {
  169. dbConnect.runSqlUpdate("UPDATE sa_order o JOIN (SELECT sa_orderid,GROUP_CONCAT(DISTINCT delivery SEPARATOR '+') AS delivery FROM sa_orderitems where sa_orderid=" + sa_orderid + " GROUP BY sa_orderid) od ON o.sa_orderid = od.sa_orderid SET o.delivery = od.delivery ");
  170. }
  171. sqlList = new ArrayList<>();
  172. if (type.equals("配件订单")) {
  173. Rows rowsdetails = dbConnect.runSqlQuery("select t1.price,t1.amount,t1.sa_orderitemsid,t1.itemid,ifnull(t2.iswriteoff,0) iswriteoff,t3.begdate,t3.enddate from sa_orderitems t1 " + "left join plm_item t2 on t1.itemid=t2.itemid and t1.siteid=t2.siteid " + "left join sa_warrantycard t3 on t1.cardno=t3.cardno and t1.siteid=t3.siteid and t3.isvoid=0 and t3.deleted=0 where t1.sa_orderid=" + sa_orderid);
  174. for (Row rowsdetail : rowsdetails) {
  175. String billingstatus = "";
  176. BigDecimal price = rowsdetail.getBigDecimal("price");
  177. BigDecimal amount = rowsdetail.getBigDecimal("amount");
  178. String begdate = rowsdetail.getString("begdate");
  179. String enddate = rowsdetail.getString("enddate");
  180. if (StringUtils.isBlank(begdate) || StringUtils.isBlank(enddate)) {
  181. billingstatus = "保外收费";
  182. } else {
  183. Date date = new Date();
  184. if (date.before(rowsdetail.getDate("enddate"))) {
  185. if (rowsdetail.getBoolean("iswriteoff")) {
  186. billingstatus = "保内收费";
  187. } else {
  188. billingstatus = "保内免费";
  189. if (sa_orderitemsids_insert.contains(rowsdetail.getLong("sa_orderitemsid"))) {
  190. price = BigDecimal.ZERO;
  191. amount = BigDecimal.ZERO;
  192. }
  193. }
  194. } else {
  195. billingstatus = "保外收费";
  196. }
  197. }
  198. if (!billingstatus.equals("保内收费")) {
  199. if (sa_orderitemsids_insert.contains(rowsdetail.getLong("sa_orderitemsid"))) {
  200. sqlList.add("update sa_orderitems set canoffqty=0 where sa_orderitemsid=" + rowsdetail.getLong("sa_orderitemsid"));
  201. }
  202. }
  203. sqlList.add("update sa_orderitems set price=" + price + ",amount=" + amount + ",billingstatus='" + billingstatus + "' where sa_orderitemsid=" + rowsdetail.getLong("sa_orderitemsid"));
  204. }
  205. }
  206. dbConnect.runSqlUpdate(sqlList);
  207. return getSucReturnObject().toString();
  208. }
  209. @API(title = "新增或更新", apiversion = R.ID20221109093602.v1.class, intervaltime = 200)
  210. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class, accessoryorder.class})
  211. public String insertOrUpdate() throws YosException {
  212. Long sa_orderid = content.getLong("sa_orderid");
  213. Long sys_enterpriseid = content.getLong("sys_enterpriseid");
  214. JSONArray items = content.getJSONArray("items");
  215. //辅助类
  216. OrderItemsHelper orderItemsHelper = new OrderItemsHelper(this);
  217. ArrayList<String> sqlList = new ArrayList<>();
  218. Rows orderRows = getOrderRows(this, sa_orderid);
  219. if (orderRows.isEmpty()) {
  220. return getErrReturnObject().setErrMsg("数据不存在,无法添加").toString();
  221. } else {
  222. if (!orderRows.get(0).getString("status").equals("新建") && !orderRows.get(0).getString("status").equals("提交") && !orderRows.get(0).getString("status").equals("预提交")) {
  223. return getErrReturnObject().setErrMsg("该状态无法操作").toString();
  224. }
  225. }
  226. //通过版本更新订单表头数据
  227. beans.order.Order.updateOrderWithVersion(this);
  228. String type = orderRows.get(0).getString("type");
  229. Long sa_promotionid = orderRows.get(0).getLong("sa_promotionid");
  230. //查询活动
  231. Rows promotionitems = dbConnect.runSqlQuery("select * from sa_promotion_items where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  232. //批量查询商品信息
  233. RowsMap itemRowsMap = orderItemsHelper.getItemRowsMap(items);
  234. BigDecimal sumqty = BigDecimal.ZERO;
  235. ArrayList<Long> sa_orderitemsids_insert = new ArrayList<>();
  236. for (Object obj : items) {
  237. JSONObject item = (JSONObject) obj;
  238. Long itemid = item.getLong("itemid");
  239. Long sa_orderitemsid = item.getLong("sa_orderitemsid");
  240. BigDecimal qty = item.getBigDecimal("qty");
  241. sumqty = sumqty.add(qty);
  242. //商品信息
  243. Row itemRow = getItemRow(itemid);
  244. if (type.equals("标准订单") && itemRow.getString("ismodule").equals("1")) {
  245. return getErrReturnObject().setErrMsg("商品编号【" + itemRow.getString("itemno") + "】是配件,无法添加").toString();
  246. }
  247. if (type.equals("配件订单") && itemRow.getString("ismodule").equals("0")) {
  248. return getErrReturnObject().setErrMsg("商品编号【" + itemRow.getString("itemno") + "】不是配件,无法添加").toString();
  249. }
  250. long width = itemRow.getLong("width");
  251. long length = itemRow.getLong("length");
  252. if (item.containsKey("width")) {
  253. width = Long.parseLong(item.getString("width"));
  254. }
  255. if (item.containsKey("length")) {
  256. length = Long.parseLong(item.getString("length"));
  257. }
  258. if (sa_promotionid > 0 && dbConnect.runSqlQuery("SELECT 1 from sa_promotion_items WHERE islimit=1 and groupqty <= saledqty and sa_promotion_itemsid=" + item.getLong("sa_promotion_itemsid") + " and siteid='" + siteid + "' and sa_promotionid='" + sa_promotionid + "'").isNotEmpty()) {
  259. return getErrReturnObject().setErrMsg("商品【" + itemRow.getString("itemno") + "】已售罄,不能添加。").toString();
  260. }
  261. BigDecimal price1 = new BigDecimal(0);
  262. // SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_新增");
  263. Boolean isInsert = true;
  264. JSONArray customproperties = new JSONArray();
  265. String spec = "";
  266. if (sa_orderitemsid <= 0) {
  267. //获取定制属性
  268. customproperties = orderItemsHelper.getCustomProperties(itemRowsMap, item);
  269. if (itemRow.getString("iscustomsize").equals("1") && customproperties.isEmpty()) {
  270. return getErrReturnObject().setErrMsg(itemRow.getString("itemno") + "的定制信息为空,无法添加").toString();
  271. }
  272. spec = orderItemsHelper.getCustomSpec(itemRowsMap, item);
  273. Rows rows = new Rows();
  274. if (!type.equals("配件订单")) {
  275. 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"));
  276. } else {
  277. if (StringUtils.isBlank(item.getStringValue("cardno"))) {
  278. rows = dbConnect.runSqlQuery("SELECT t1.sa_orderitemsid,qty from sa_orderitems t1 WHERE t1.siteid='" + siteid + "' and ifnull(cardno,'')='' and t1.itemid=" + itemid + " and t1.sa_orderid=" + sa_orderid);
  279. } else {
  280. rows = dbConnect.runSqlQuery("SELECT t1.sa_orderitemsid,qty from sa_orderitems t1 WHERE t1.siteid='" + siteid + "' and t1.itemid=" + itemid + " and t1.sa_orderid=" + sa_orderid + " and ifnull(t1.cardno,'')='" + item.getStringValue("cardno") + "'");
  281. }
  282. }
  283. if (rows.isEmpty()) {
  284. sa_orderitemsid = createTableID("sa_orderitems");
  285. isInsert = true;
  286. } else {
  287. //System.out.println(beans.order.Order.getDefaultIsRepeatValue(siteid, type));
  288. if (beans.order.Order.getDefaultIsRepeatValue(siteid, type)) {
  289. sa_orderitemsid = createTableID("sa_orderitems");
  290. isInsert = true;
  291. } else {
  292. //price1 = rows.get(0).getBigDecimal("price");
  293. sa_orderitemsid = rows.get(0).getLong("sa_orderitemsid");
  294. item.put("qty", item.getBigDecimal("qty").add(rows.get(0).getBigDecimal("qty")));
  295. isInsert = false;
  296. }
  297. }
  298. } else {
  299. Rows rows = dbConnect.runSqlQuery("SELECT sa_orderitemsid,price from sa_orderitems WHERE siteid='" + siteid + "' and sa_orderitemsid=" + sa_orderitemsid);
  300. if (rows.isNotEmpty()) {
  301. price1 = rows.get(0).getBigDecimal("price");
  302. }
  303. isInsert = false;
  304. }
  305. if (!isInsert) {
  306. if (dbConnect.runSqlQuery("SELECT 1 from sa_orderitems WHERE itemid=" + itemid + " and sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'").isEmpty()) {
  307. continue;
  308. }
  309. }
  310. //是否符合商品起订量和增量判断
  311. orderItemsHelper.compareQty(type, itemRowsMap, item, itemRow, promotionitems);
  312. BigDecimal defaultprice;
  313. BigDecimal price;
  314. BigDecimal saleprice;
  315. BigDecimal custamount = new BigDecimal(0);
  316. BigDecimal rwpricerate = new BigDecimal(1);
  317. BigDecimal pricerate = new BigDecimal(1);
  318. SQLFactory sqlFactory = new SQLFactory(this, "商品等级价格查询");
  319. sqlFactory.addParameter("siteid", siteid);
  320. sqlFactory.addParameter("itemid", itemid);
  321. sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  322. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  323. if (rows.isNotEmpty()) {
  324. pricerate = rows.get(0).getBigDecimal("discountrate");
  325. //如果折扣率为0,则默认按照1计算
  326. pricerate = (pricerate.compareTo(BigDecimal.ZERO) == 0 ? new BigDecimal(1) : pricerate);
  327. }
  328. custamount = item.getBigDecimal("custamount");
  329. Rows itemrows = dbConnect.runSqlQuery("select topclassnum from plm_item where itemid=" + itemid + " and siteid='" + siteid + "'");
  330. if (!type.equals("配件订单") && "集成灶".equals(itemrows.get(0).getString("topclassnum"))) {
  331. rwpricerate = Enterprise.getRWPriceRate(this, Enterprise.getAgentsID(this, sys_enterpriseid));
  332. }
  333. //价格
  334. ItemPrice itemPrice = ItemPrice.getItemPrice(this, sys_enterpriseid, itemid);
  335. price = orderItemsHelper.getPrice(orderRows.get(0), itemPrice, item);
  336. saleprice = orderItemsHelper.getSalePrice(orderRows.get(0), itemPrice, item, itemRowsMap);
  337. if (price.compareTo(BigDecimal.ZERO) < 0) {
  338. return getErrReturnObject().setErrMsg("价格不可小于0").toString();
  339. }
  340. defaultprice = price;
  341. if (isInsert) {
  342. sa_orderitemsids_insert.add(sa_orderitemsid);
  343. price = price.multiply(rwpricerate).add(custamount);
  344. sqlList.add(orderItemsHelper.getInsertSQL(customproperties, sa_orderitemsid, item, itemRow, type, defaultprice, price, saleprice, itemPrice.getMarketprice(), spec, promotionitems, width, length, custamount, rwpricerate, pricerate).getSQL());
  345. if (item.getLongValue("sa_workorderid") != 0) {
  346. sqlList.add("update sa_workorder_node_items set sourceid=" + sa_orderid + ",sourcetable='sa_order' where sa_workorderid=" + item.getLongValue("sa_workorderid") + " and itemid=" + item.getLong("itemid"));
  347. }
  348. } else {
  349. price = price.multiply(rwpricerate).add(item.getBigDecimal("custamount"));
  350. if (item.containsKey("defaultprice")) {
  351. defaultprice = item.getBigDecimal("defaultprice");
  352. price = defaultprice.multiply(rwpricerate).add(custamount);
  353. }
  354. sqlList.add(orderItemsHelper.getUpdateSQL(sa_orderitemsid, item, itemRow, type, defaultprice, price, saleprice, width, length).getSQL());
  355. }
  356. }
  357. if (type.equals("促销订单")) {
  358. Rows promotionRows = dbConnect.runSqlQuery("select * from sa_promotion where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
  359. if (!promotionRows.isEmpty()) {
  360. if (promotionRows.get(0).getString("type").equals("打包促销")) {
  361. BigDecimal orderaddqty = promotionRows.get(0).getBigDecimal("orderaddqty");
  362. BigDecimal orderminqty = promotionRows.get(0).getBigDecimal("orderminqty");
  363. if (((sumqty.subtract(orderminqty)).remainder(orderaddqty)).compareTo(BigDecimal.ZERO) != 0 || sumqty.compareTo(orderminqty) < 0) {
  364. return getErrReturnObject().setErrMsg("不符合促销活动订购数量规则,修改不成功").toString();
  365. }
  366. }
  367. }
  368. }
  369. dbConnect.runSqlUpdate(sqlList);
  370. //重新排序
  371. updateRowNo(sa_orderid);
  372. //居间费设置
  373. updateRebateFee(sa_orderid);
  374. //调整价格(返利金)
  375. if (orderRows.get(0).getBoolean("rebate_used")) {
  376. setRebateAmount(this, sa_orderid, getMaxUsedRebateAmount(this, sa_orderid));
  377. adjustLastRebateAmount(this, sa_orderid);
  378. }
  379. //更新订单扣款信息
  380. updateAccountclassinfos(sa_orderid);
  381. //查询商品费用方案方法
  382. orderItemsHelper.executeDataFunction(sa_orderid);
  383. //生成摘要
  384. SQLFactory sqlFactory = new SQLFactory(this, "查询定制费用统计");
  385. sqlFactory.addParameter("sa_orderid", sa_orderid);
  386. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  387. if (rows.isNotEmpty()) {
  388. String remarks = StringUtils.join(rows.toArray("remarks"), ";");
  389. dbConnect.runSqlUpdate("UPDATE sa_order set abstract ='" + remarks + "' WHERE sa_orderid='" + sa_orderid + "'");
  390. } else {
  391. dbConnect.runSqlUpdate("UPDATE sa_order set abstract ='' WHERE sa_orderid='" + sa_orderid + "'");
  392. }
  393. if (type.equals("配件订单")) {
  394. dbConnect.runSqlUpdate("UPDATE sa_order o JOIN (SELECT sa_orderid,GROUP_CONCAT(DISTINCT delivery SEPARATOR '+') AS delivery FROM sa_orderitems where sa_orderid=" + sa_orderid + " GROUP BY sa_orderid) od ON o.sa_orderid = od.sa_orderid SET o.delivery = od.delivery ");
  395. }
  396. sqlList = new ArrayList<>();
  397. if (type.equals("配件订单")) {
  398. Rows rowsdetails = dbConnect.runSqlQuery("select t1.price,t1.amount,t1.sa_orderitemsid,t1.itemid,ifnull(t2.iswriteoff,0) iswriteoff,t3.begdate,t3.enddate from sa_orderitems t1 " + "left join plm_item t2 on t1.itemid=t2.itemid and t1.siteid=t2.siteid " + "left join sa_warrantycard t3 on t1.cardno=t3.cardno and t1.siteid=t3.siteid and t3.isvoid=0 and t3.deleted=0 where t1.sa_orderid=" + sa_orderid);
  399. for (Row rowsdetail : rowsdetails) {
  400. String billingstatus = "";
  401. BigDecimal price = rowsdetail.getBigDecimal("price");
  402. BigDecimal amount = rowsdetail.getBigDecimal("amount");
  403. String begdate = rowsdetail.getString("begdate");
  404. String enddate = rowsdetail.getString("enddate");
  405. if (StringUtils.isBlank(begdate) || StringUtils.isBlank(enddate)) {
  406. billingstatus = "保外收费";
  407. } else {
  408. Date date = new Date();
  409. if (date.before(rowsdetail.getDate("enddate"))) {
  410. if (rowsdetail.getBoolean("iswriteoff")) {
  411. billingstatus = "保内收费";
  412. } else {
  413. billingstatus = "保内免费";
  414. if (sa_orderitemsids_insert.contains(rowsdetail.getLong("sa_orderitemsid"))) {
  415. price = BigDecimal.ZERO;
  416. amount = BigDecimal.ZERO;
  417. }
  418. }
  419. } else {
  420. billingstatus = "保外收费";
  421. }
  422. }
  423. if (!billingstatus.equals("保内收费")) {
  424. sqlList.add("update sa_orderitems set canoffqty=0 where sa_orderitemsid=" + rowsdetail.getLong("sa_orderitemsid"));
  425. }
  426. sqlList.add("update sa_orderitems set price=" + price + ",amount=" + amount + ",billingstatus='" + billingstatus + "' where sa_orderitemsid=" + rowsdetail.getLong("sa_orderitemsid"));
  427. }
  428. }
  429. dbConnect.runSqlUpdate(sqlList);
  430. if (systemclient.equalsIgnoreCase("wechatsaletool")) {
  431. rows = dbConnect.runSqlQuery("SELECT sa_orderitemsid,sa_orderitems_v,itemid from sa_orderitems WHERE sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  432. return getSucReturnObject().setData(rows).toString();
  433. }
  434. return getSucReturnObject().toString();
  435. }
  436. @API(title = "一键设置发货方式", apiversion = R.ID2025121316011603.v1.class)
  437. public String oneclickchangedelivery() throws YosException {
  438. String delivery = content.getString("delivery");
  439. long sa_orderid = content.getLong("sa_orderid");
  440. ArrayList<String> sqlList = new ArrayList<>();
  441. sqlList.add("update sa_orderitems set delivery='" + delivery + "' where sa_orderid=" + sa_orderid);
  442. sqlList.add("update sa_order set delivery='" + delivery + "' where sa_orderid=" + sa_orderid);
  443. dbConnect.runSqlUpdate(sqlList);
  444. return getSucReturnObject().toString();
  445. }
  446. @API(title = "更新可核销数", apiversion = R.ID2025111710122903.v1.class)
  447. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class, accessoryorder.class})
  448. public String updatecanoffqty() throws YosException {
  449. Long sa_orderid = content.getLong("sa_orderid");
  450. Long sa_orderitemsid = content.getLong("sa_orderitemsid");
  451. BigDecimal canoffqty = content.getBigDecimal("canoffqty");
  452. JSONArray sa_tpartreimbursementids = new JSONArray();
  453. if (content.containsKey("sa_tpartreimbursementids")) {
  454. sa_tpartreimbursementids = content.getJSONArray("sa_tpartreimbursementids");
  455. }
  456. Rows orderRows = dbConnect.runSqlQuery("SELECT status,sonum,type from sa_order WHERE sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  457. Rows orderitemsRows = dbConnect.runSqlQuery("SELECT sa_orderitemsid,qty,billingstatus,rowno,itemname,canoffqty from sa_orderitems WHERE sa_orderid=" + sa_orderid + " and sa_orderitemsid='" + sa_orderitemsid + "'");
  458. if (orderRows.isEmpty()) {
  459. return getErrReturnObject().setErrMsg("配件订单不存在").toString();
  460. }
  461. Row orderRow = orderRows.get(0);
  462. if (!orderRow.getString("type").equals("配件订单")) {
  463. return getErrReturnObject().setErrMsg("非配件订单无法更新可核销数").toString();
  464. }
  465. if (orderRow.getString("status").equals("审核")) {
  466. return getErrReturnObject().setErrMsg("配件订单【" + orderRow.getString("sonum") + "】已审核,无法更新可核销数").toString();
  467. }
  468. if (orderitemsRows.isEmpty()) {
  469. return getErrReturnObject().setErrMsg("订单明细不存在").toString();
  470. }
  471. Row orderitemsRow = orderitemsRows.get(0);
  472. if (orderitemsRow.getBigDecimal("qty").compareTo(canoffqty) < 0) {
  473. return getErrReturnObject().setErrMsg("可核销数不可大于商品明细行数量").toString();
  474. }
  475. String itemname = orderitemsRow.getString("itemname");
  476. BigDecimal canoffqty_old = orderitemsRow.getBigDecimal("canoffqty");
  477. dbConnect.runSqlUpdate("update sa_orderitems set canoffqty=" + canoffqty + " where sa_orderitemsid=" + sa_orderitemsid);
  478. DataContrlLog.createLog(this, "sa_order", sa_orderid, "更新可核销数", "【" + itemname + "】可核销数量 由" + canoffqty_old + " 变更为 " + canoffqty).insert();
  479. for (Object obj : sa_tpartreimbursementids) {
  480. Long sa_tpartreimbursementid = Long.parseLong(obj.toString());
  481. DataContrlLog.createLog(this, "sa_tpartreimbursement", sa_tpartreimbursementid, "更新可核销数", "【" + itemname + "】可核销数量 由" + canoffqty_old + " 变更为 " + canoffqty).insert();
  482. }
  483. return getSucReturnObject().toString();
  484. }
  485. @API(title = "查询订单重复商品", apiversion = R.ID2024020201095102.v1.class)
  486. public String get() throws YosException {
  487. Long sa_orderid = content.getLongValue("sa_orderid");
  488. JSONArray items = content.getJSONArray("items");
  489. OrderItemsHelper orderItemsHelper = new OrderItemsHelper(this);
  490. //批量查询商品信息
  491. RowsMap itemRowsMap = orderItemsHelper.getItemRowsMap(items);
  492. Rows rows = new Rows();
  493. for (Object obj : items) {
  494. JSONObject item = (JSONObject) obj;
  495. if (item != null) {
  496. Long itemid = item.getLongValue("itemid");
  497. JSONArray customproperties = new JSONArray();
  498. customproperties = orderItemsHelper.getCustomProperties(itemRowsMap, item);
  499. String custompropertie = customproperties.toJSONString().replace("[", "").replace("]", "").replace("\"", "");
  500. Rows temprows = dbConnect.runSqlQuery("SELECT itemname from sa_orderitems WHERE sa_orderid=" + sa_orderid + " and siteid='" + siteid + "' and itemid=" + itemid + " and ifnull(customproperties,'')='" + custompropertie + "'");
  501. rows.addAll(temprows);
  502. }
  503. }
  504. Rows orderRows = beans.order.Order.getOrderRows(this, sa_orderid);
  505. String type = orderRows.get(0).getString("type");
  506. Row resRow = new Row();
  507. resRow.put("items", rows);
  508. resRow.put("isrepeat", beans.order.Order.getDefaultIsRepeatValue(siteid, type));
  509. return getSucReturnObject().setData(resRow).toString();
  510. }
  511. /**
  512. * 更新订单扣款信息
  513. */
  514. public String updateAccountclassinfos(Long sa_orderid) throws YosException {
  515. Rows orderRows = getOrderRows(this, sa_orderid);
  516. if (orderRows.isEmpty()) {
  517. return "订单不存在";
  518. }
  519. String type = "";
  520. Rows promotionrows = dbConnect.runSqlQuery("select * from sa_promotion where siteid='" + siteid + "' and sa_promotionid=" + orderRows.get(0).getLong("sa_promotionid"));
  521. 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"));
  522. if (promotionrows.isNotEmpty()) {
  523. type = promotionrows.get(0).getString("type");
  524. }
  525. JSONArray jsonArray = new JSONArray();
  526. BigDecimal orderamount = getAmount(this, sa_orderid);
  527. //System.out.println(orderamount);
  528. 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"));
  529. if (accountclassrows.isEmpty()) {
  530. return "未找到扣款账户";
  531. }
  532. JSONObject jsonObject = new JSONObject();
  533. if (type.equals("返利促销")) {
  534. BigDecimal associationamount = promotionrows.get(0).getBigDecimal("associationamount");
  535. long associationaccountclassid = promotionrows.get(0).getLong("associationaccountclassid");
  536. 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);
  537. if (promotionenterpriserows.isNotEmpty()) {
  538. if (promotionenterpriserows.get(0).getBoolean("islimit")) {
  539. BigDecimal limitamount = promotionenterpriserows.get(0).getBigDecimal("limitamount");
  540. BigDecimal saledamount = promotionenterpriserows.get(0).getBigDecimal("saledamount");
  541. if (orderamount.compareTo(limitamount.subtract(saledamount)) <= 0) {
  542. //全部优先账户扣款
  543. jsonObject.put("amount", orderamount);
  544. jsonObject.put("sa_accountclassid", accountclassrows.get(0).getLong("sa_accountclassid"));
  545. jsonObject.put("accountname", accountclassrows.get(0).getString("accountname"));
  546. jsonObject.put("balance", accountclassrows.get(0).getBigDecimal("balance"));
  547. jsonObject.put("creditquota", accountclassrows.get(0).getBigDecimal("creditquota"));
  548. jsonArray.add(jsonObject);
  549. } else if (orderamount.compareTo(limitamount.subtract(saledamount)) > 0) {
  550. //全部优先账户扣款
  551. jsonObject.put("amount", limitamount.subtract(saledamount));
  552. jsonObject.put("sa_accountclassid", accountclassrows.get(0).getLong("sa_accountclassid"));
  553. jsonObject.put("accountname", accountclassrows.get(0).getString("accountname"));
  554. jsonObject.put("balance", accountclassrows.get(0).getBigDecimal("balance"));
  555. jsonObject.put("creditquota", accountclassrows.get(0).getBigDecimal("creditquota"));
  556. jsonArray.add(jsonObject);
  557. JSONObject associationjsonObject = new JSONObject();
  558. associationjsonObject.put("amount", orderamount.subtract((limitamount.subtract(saledamount))));
  559. associationjsonObject.put("sa_accountclassid", associationaccountclassrows.get(0).getLong("sa_accountclassid"));
  560. associationjsonObject.put("accountname", associationaccountclassrows.get(0).getString("accountname"));
  561. associationjsonObject.put("balance", associationaccountclassrows.get(0).getBigDecimal("balance"));
  562. associationjsonObject.put("creditquota", associationaccountclassrows.get(0).getBigDecimal("creditquota"));
  563. jsonArray.add(associationjsonObject);
  564. } else {
  565. return "未知错误,请联系管理员";
  566. }
  567. }
  568. } else {
  569. //全部优先账户扣款
  570. jsonObject.put("amount", orderamount);
  571. jsonObject.put("sa_accountclassid", accountclassrows.get(0).getLong("sa_accountclassid"));
  572. jsonObject.put("accountname", accountclassrows.get(0).getString("accountname"));
  573. jsonObject.put("balance", accountclassrows.get(0).getBigDecimal("balance"));
  574. jsonObject.put("creditquota", accountclassrows.get(0).getBigDecimal("creditquota"));
  575. jsonArray.add(jsonObject);
  576. }
  577. } else {
  578. //全部优先账户扣款
  579. jsonObject.put("amount", orderamount);
  580. jsonObject.put("sa_accountclassid", accountclassrows.get(0).getLong("sa_accountclassid"));
  581. jsonObject.put("accountname", accountclassrows.get(0).getString("accountname"));
  582. jsonObject.put("balance", accountclassrows.get(0).getBigDecimal("balance"));
  583. jsonObject.put("creditquota", accountclassrows.get(0).getBigDecimal("creditquota"));
  584. jsonArray.add(jsonObject);
  585. }
  586. //System.out.println(jsonArray.toJSONString());
  587. dbConnect.runSqlUpdate("update sa_order set sa_accountclassinfos='" + jsonArray + "' where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  588. return "true";
  589. }
  590. /**
  591. * 更新居间费信息
  592. */
  593. public void updateRebateFee(Long sa_orderid) throws YosException {
  594. Rows orderRows = getOrderRows(this, sa_orderid);
  595. if (orderRows.isEmpty()) {
  596. return;
  597. }
  598. Long sa_projectid = orderRows.get(0).getLong("sa_projectid");
  599. Rows contractRows = dbConnect.runSqlQuery("SELECT * from sa_contract WHERE sa_projectid = " + sa_projectid + " and type='居间' and status='审核'");
  600. if (contractRows.isEmpty()) {
  601. return;
  602. }
  603. Long sa_contractid = contractRows.get(0).getLong("sa_contractid");
  604. Rows partiesRows = dbConnect.runSqlQuery("SELECT sys_enterpriseid from sa_project_parties WHERE type ='居间服务商' and sa_projectid = " + sa_projectid + " and siteid ='" + siteid + "'");
  605. if (partiesRows.isEmpty()) {
  606. return;
  607. }
  608. Long sys_enterpriseid_temp = partiesRows.get(0).getLong("sys_enterpriseid");
  609. content.put("sys_enterpriseid", sys_enterpriseid_temp);
  610. content.put("ownertable", "sa_order");
  611. content.put("ownerid", sa_orderid);
  612. content.put("billdate", "");
  613. content.put("remarks", "");
  614. content.put("sa_contractid", sa_contractid);
  615. content.put("settlementmode", "线上");
  616. content.put("sa_rebatesettlementid", 0);
  617. new Rebate(content).insertOrUpdate();
  618. }
  619. @API(title = "详情", apiversion = R.ID20221110134302.v1.class)
  620. @CACHEING
  621. public String selectDetail() throws YosException {
  622. Long sa_orderitemsid = content.getLong("sa_orderitemsid");
  623. SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_详情");
  624. sqlFactory.addParameter("siteid", siteid);
  625. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsid);
  626. String sql = sqlFactory.getSQL();
  627. Rows rows = dbConnect.runSqlQuery(sql);
  628. Row row = rows.isNotEmpty() ? rows.get(0) : new Row();
  629. return getSucReturnObject().setData(row).toString();
  630. }
  631. @API(title = "删除", apiversion = R.ID20221109093702.v1.class)
  632. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class, accessoryorder.class})
  633. public String delete() throws YosException {
  634. Long sa_orderid = content.getLong("sa_orderid");
  635. Rows orderRows = getOrderRows(this, sa_orderid);
  636. if (orderRows.isEmpty()) {
  637. return getErrReturnObject().setErrMsg("数据不存在,无法添加").toString();
  638. } else {
  639. if (!orderRows.get(0).getString("status").equals("新建") && !orderRows.get(0).getString("status").equals("提交")) {
  640. return getErrReturnObject().setErrMsg("非新建提交状态无法删除").toString();
  641. }
  642. }
  643. //通过版本更新订单表头数据
  644. beans.order.Order.updateOrderWithVersion(this);
  645. JSONArray sa_orderitemsids = content.getJSONArray("sa_orderitemsids");
  646. if (sa_orderitemsids.isEmpty()) {
  647. return getErrReturnObject().setErrMsg("请选择要删除的订单商品").toString();
  648. }
  649. String packagetypemxsql = "select t1.sa_orderitemsid from sa_orderitems t1 inner join sa_promotion_items t2 on t1.sa_promotion_itemsid=t2.sa_promotion_itemsid and t1.siteid=t2.siteid where t1.siteid='" + siteid + "' and t1.sa_orderid in(select sa_orderid from sa_orderitems where sa_orderitemsid in" + sa_orderitemsids.toJSONString() + ") and t2.packagetypemx in(select distinct t2.packagetypemx from sa_orderitems t1 inner join sa_promotion_items t2 on t1.sa_promotion_itemsid=t2.sa_promotion_itemsid and t1.siteid=t2.siteid where t1.siteid='" + siteid + "' and t1.sa_orderitemsid in" + sa_orderitemsids.toJSONString() + " and ifnull(t2.packagetypemx,'')!='')";
  650. packagetypemxsql = packagetypemxsql.replace("[", "(").replace("]", ")");
  651. Rows packagetypemxrows = dbConnect.runSqlQuery(packagetypemxsql);
  652. sa_orderitemsids.addAll(packagetypemxrows.toJsonArray("sa_orderitemsid"));
  653. String sqldetail = "select sa_workorderid,itemid from sa_orderitems where siteid='" + siteid + "' and sa_orderitemsid in" + sa_orderitemsids.toJSONString();
  654. sqldetail = sqldetail.replace("[", "(").replace("]", ")");
  655. Rows rowsdetail = dbConnect.runSqlQuery(sqldetail);
  656. for (Row row : rowsdetail) {
  657. if (row.getLong("sa_workorderid") != 0) {
  658. dbConnect.runSqlUpdate("update sa_workorder_node_items set sourceid=0,sourcetable='' where sa_workorderid=" + row.getLong("sa_workorderid") + " and itemid=" + row.getLong("itemid"));
  659. }
  660. }
  661. SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_删除");
  662. sqlFactory.addParameter("siteid", siteid);
  663. sqlFactory.addParameter("sa_orderid", sa_orderid);
  664. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsids.toArray());
  665. String sql = sqlFactory.getSQL();
  666. dbConnect.runSqlUpdate(sql);
  667. // DeleteSQL deleteSQL = SQLFactory.createDeleteSQL(this, "sa_order_cost");
  668. // deleteSQL.setSiteid(siteid);
  669. // deleteSQL.setWhere("sa_orderitemsid", sa_orderitemsids.toArray());
  670. // deleteSQL.delete();
  671. //重新排序
  672. updateRowNo(sa_orderid);
  673. //居间费设置
  674. updateRebateFee(sa_orderid);
  675. //调整价格(返利金)
  676. if (orderRows.get(0).getBoolean("rebate_used")) {
  677. setRebateAmount(this, sa_orderid, getMaxUsedRebateAmount(this, sa_orderid));
  678. adjustLastRebateAmount(this, sa_orderid);
  679. }
  680. // else {
  681. // dbConnect.runSqlUpdate("UPDATE sa_orderitems SET amount=amount-rebateamount,price=amount/qty WHERE sa_orderid='" + sa_orderid + "' and siteid='" + siteid + "' and qty!=0");
  682. // dbConnect.runSqlUpdate("UPDATE sa_orderitems SET amount=0 WHERE sa_orderid='" + sa_orderid + "' and siteid='" + siteid + "' and qty=0");
  683. // }
  684. updateAccountclassinfos(sa_orderid);
  685. new OrderItemsHelper(this).executeDataFunction(sa_orderid);
  686. //生成摘要
  687. sqlFactory = new SQLFactory(this, "查询定制费用统计");
  688. sqlFactory.addParameter("sa_orderid", sa_orderid);
  689. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  690. if (rows.isNotEmpty()) {
  691. String remarks = StringUtils.join(rows.toArray("remarks"), ";");
  692. dbConnect.runSqlUpdate("UPDATE sa_order set abstract ='" + remarks + "' WHERE sa_orderid='" + sa_orderid + "'");
  693. } else {
  694. dbConnect.runSqlUpdate("UPDATE sa_order set abstract ='' WHERE sa_orderid='" + sa_orderid + "'");
  695. }
  696. return getSucReturnObject().toString();
  697. }
  698. @API(title = "商品行冻结", apiversion = R.ID20230508093003.v1.class)
  699. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class, accessoryorder.class})
  700. public String freeze() throws YosException {
  701. Long sa_orderid = content.getLong("sa_orderid");
  702. //通过版本更新订单表头数据
  703. beans.order.Order.updateOrderWithVersion(this);
  704. JSONArray sa_orderitemsids = content.getJSONArray("sa_orderitemsids");
  705. boolean isfreeze = content.getBooleanValue("isfreeze");
  706. Rows orderRows = dbConnect.runSqlQuery("select * from sa_order where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  707. if (!orderRows.isEmpty()) {
  708. if (!orderRows.get(0).getString("status").equals("审核")) {
  709. if (isfreeze) {
  710. return getErrReturnObject().setErrMsg("非审核状态无法进行冻结").toString();
  711. } else {
  712. return getErrReturnObject().setErrMsg("非审核状态无法进行反冻结").toString();
  713. }
  714. }
  715. } else {
  716. return getErrReturnObject().setErrMsg("该订单不存在").toString();
  717. }
  718. SQLFactory sqlFactory;
  719. if (isfreeze) {
  720. sqlFactory = new SQLFactory(this, "订单商品明细_冻结");
  721. } else {
  722. sqlFactory = new SQLFactory(this, "订单商品明细_反冻结");
  723. }
  724. sqlFactory.addParameter("siteid", siteid);
  725. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsids.toArray());
  726. String sql = sqlFactory.getSQL();
  727. //System.out.println(sql);
  728. dbConnect.runSqlUpdate(sql);
  729. return getSucReturnObject().toString();
  730. }
  731. @API(title = "商品行折扣变更", apiversion = R.ID20230508101703.v1.class)
  732. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class, accessoryorder.class})
  733. public String updateOrderItemsDiscountrate() throws YosException {
  734. Long sa_orderid = content.getLong("sa_orderid");
  735. BigDecimal discountrate = content.getBigDecimal("discountrate");
  736. JSONArray itemids = content.getJSONArray("itemids");
  737. Rows orderRows = dbConnect.runSqlQuery("select * from sa_order where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  738. if (!orderRows.isEmpty()) {
  739. if (!orderRows.get(0).getString("status").equals("新建") && !orderRows.get(0).getString("status").equals("预提交")) {
  740. return getErrReturnObject().setErrMsg("非新建或预提交状态无法进行商品行折扣变更").toString();
  741. }
  742. } else {
  743. return getErrReturnObject().setErrMsg("该订单不存在").toString();
  744. }
  745. if (!(discountrate.compareTo(BigDecimal.ZERO) > 0 && discountrate.compareTo(new BigDecimal(1)) <= 0)) {
  746. return getErrReturnObject().setErrMsg("折扣必须在0-100之间").toString();
  747. }
  748. //通过版本更新订单表头数据
  749. beans.order.Order.updateOrderWithVersion(this);
  750. String sql;
  751. if (itemids.size() == 0) {
  752. sql = "select * from sa_orderitems where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'";
  753. } else {
  754. sql = "select * from sa_orderitems where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "' and itemid in " + itemids.toJSONString();
  755. sql = sql.replace("[", "(").replace("]", ")");
  756. }
  757. Rows orderItemsRows = dbConnect.runSqlQuery(sql);
  758. ArrayList<String> sqlList = new ArrayList<>();
  759. if (!orderItemsRows.isEmpty()) {
  760. for (Row row : orderItemsRows) {
  761. BigDecimal price = row.getBigDecimal("price").multiply(discountrate);
  762. BigDecimal amount = row.getBigDecimal("amount").multiply(discountrate);
  763. sqlList.add("update sa_orderitems set price=" + price + ",amount=" + amount + " where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  764. }
  765. }
  766. sqlList.add(DataContrlLog.createLog(this, "sa_order", sa_orderid, "订单整单折扣变更【" + discountrate + "】", "整单折扣").getSQL());
  767. dbConnect.runSqlUpdate(sqlList);
  768. updateAccountclassinfos(sa_orderid);
  769. return getSucReturnObject().toString();
  770. }
  771. @API(title = "商品行价格刷新(变为原价)", apiversion = R.ID20230518151803.v1.class)
  772. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class, accessoryorder.class})
  773. public String updateOrderItemsPrice() throws YosException {
  774. Long sa_orderid = content.getLong("sa_orderid");
  775. JSONArray itemids = content.getJSONArray("itemids");
  776. Rows orderRows = dbConnect.runSqlQuery("select * from sa_order where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  777. if (!orderRows.isEmpty()) {
  778. if (!orderRows.get(0).getString("status").equals("新建") && !orderRows.get(0).getString("status").equals("预提交")) {
  779. return getErrReturnObject().setErrMsg("非新建或预提交状态无法进行商品行价格刷新").toString();
  780. }
  781. } else {
  782. return getErrReturnObject().setErrMsg("该订单不存在").toString();
  783. }
  784. //通过版本更新订单表头数据
  785. beans.order.Order.updateOrderWithVersion(this);
  786. String sql;
  787. if (itemids.size() == 0) {
  788. sql = "select * from sa_orderitems where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'";
  789. } else {
  790. sql = "select * from sa_orderitems where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "' and itemid in " + itemids.toJSONString();
  791. sql = sql.replace("[", "(").replace("]", ")");
  792. }
  793. Rows orderItemsRows = dbConnect.runSqlQuery(sql);
  794. ArrayList<String> sqlList = new ArrayList<>();
  795. if (!orderItemsRows.isEmpty()) {
  796. for (Row row : orderItemsRows) {
  797. //价格
  798. ItemPrice itemPrice = ItemPrice.getItemPrice(this, orderRows.get(0).getLong("sys_enterpriseid"), row.getLong("itemid"));
  799. BigDecimal price = itemPrice.getPrice();
  800. BigDecimal amount = row.getBigDecimal("qty").multiply(price);
  801. sqlList.add("update sa_orderitems set price=" + price + ",amount=" + amount + " where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
  802. }
  803. }
  804. sqlList.add(DataContrlLog.createLog(this, "sa_order", sa_orderid, "订单商品行价格刷新(恢复原价)", "订单商品行价格刷新").getSQL());
  805. dbConnect.runSqlUpdate(sqlList);
  806. updateAccountclassinfos(sa_orderid);
  807. return getSucReturnObject().toString();
  808. }
  809. @API(title = "查询列表", apiversion = R.ID20221109093902.v1.class)
  810. public String selectList() throws YosException, IOException {
  811. Long sa_orderid = content.getLong("sa_orderid");
  812. Rows orderRows = getOrderRows(this, sa_orderid);
  813. if (orderRows.isNotEmpty()) {
  814. sys_enterpriseid = orderRows.get(0).getLong("sys_enterpriseid");
  815. }
  816. Long sa_promotionid = orderRows.get(0).getLong("sa_promotionid");//大于0表示为促销订单
  817. String type = orderRows.get(0).getString("type");//订单类型
  818. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_orderitems", "*");
  819. querySQL.setTableAlias("t1");
  820. querySQL.addJoinTable(JOINTYPE.left, "st_invbal_sale", "t2", "t2.siteid = t1.siteid AND t2.itemid = t1.itemid", "cansaleqty", "candispatchqty");
  821. querySQL.addJoinTable(JOINTYPE.left, "plm_item", "t3", "t3.itemid = t1.itemid and t3.siteid = t1.siteid", "orderminqty_auxunit", "orderaddqty_auxunit", "spec", "iscustomsize", "widthschemeid", "lengthschemeid", "standards", "k3outcode", "outplace", "aftersalesitemno");
  822. if (sa_promotionid > 0) {
  823. querySQL.addJoinTable(JOINTYPE.left, "sa_promotion_items", "t4", "t4.sa_promotion_itemsid = t1.sa_promotion_itemsid and t4.siteid = t1.siteid and t4.sa_promotionid=" + sa_promotionid, "packagetypemx", "saledqty", "groupqty");
  824. }
  825. if (type.equals("配件订单")) {
  826. querySQL.addJoinTable(JOINTYPE.left, "sa_workorder", "t5", "t1.sa_workorderid=t5.sa_workorderid and t1.siteid=t5.siteid");
  827. querySQL.addJoinTable(JOINTYPE.left, "sa_warrantycard", "t6", "t6.cardno=t1.cardno and t6.siteid=t1.siteid", "sku", "address");
  828. querySQL.addJoinTable(JOINTYPE.left, "plm_item", "t7", "t7.itemid=t6.itemid and t7.siteid=t6.siteid", "iswriteoff");
  829. }
  830. querySQL.addQueryFields("candispatchqty", "ifnull(t2.candispatchqty, 0)");
  831. querySQL.addQueryFields("cansaleqty", "ifnull(t2.cansaleqty, 0)");
  832. querySQL.addQueryFields("totalaty", "ifnull(t2.qty, 0)");
  833. querySQL.addQueryFields("stockstatus", "(SELECT (CASE WHEN t2.cansaleqty>=t3.stockstatus1 THEN '充足' WHEN t2.cansaleqty<=t3.stockstatus2 THEN '缺货' ELSE '紧缺' END))");
  834. if (sa_promotionid > 0) {
  835. querySQL.addQueryFields("packageqty", "ifnull(t4.packageqty, 0)");
  836. querySQL.addQueryFields("orderaddqty", "t4.orderaddqty");
  837. querySQL.addQueryFields("orderminqty", "t4.orderminqty");
  838. } else {
  839. querySQL.addQueryFields("packageqty", "ifnull(t3.packageqty, 0)");
  840. querySQL.addQueryFields("orderaddqty", "t3.orderaddqty");
  841. querySQL.addQueryFields("orderminqty", "t3.orderminqty");
  842. }
  843. querySQL.addQueryFields("itemdelivery", "ifnull(t3.delivery, 0)");
  844. if (type.equals("配件订单")) {
  845. querySQL.addQueryFields("workorderbillno", "t5.billno");
  846. querySQL.addQueryFields("workorderdate", "t5.createdate");
  847. querySQL.addQueryFields("workorderstatus", "t5.status");
  848. querySQL.addQueryFields("cardname", "t6.name");
  849. querySQL.addQueryFields("cardphonenumber", "t6.phonenumber");
  850. querySQL.addQueryFields("cardbegdate", "t6.begdate");
  851. querySQL.addQueryFields("cardenddate", "t6.enddate");
  852. querySQL.addQueryFields("machineitemno", "t7.itemno");
  853. querySQL.addQueryFields("machineitemname", "t7.itemname");
  854. querySQL.addQueryFields("machinemodel", "t7.model");
  855. querySQL.addQueryFields("machinespec", "t7.spec");
  856. querySQL.addQueryFields("warrantystatus", "CASE WHEN ifnull(t6.enddate,'')='' then '' else CASE WHEN t6.enddate >= CURDATE() THEN '保内' ELSE '保外' END end");
  857. }
  858. querySQL.setCondition("t1.itemno", "t1.itemname", "t1.model", "t3.spec", "t1.remarks");
  859. querySQL.setWhere("t1.siteid", siteid);
  860. querySQL.setWhere("t1.sa_orderid", sa_orderid);
  861. if (content.containsKey("where")) {
  862. JSONObject whereObject = content.getJSONObject("where");
  863. if (sa_promotionid > 0 && whereObject.containsKey("packagetypemx") && !"".equals(whereObject.getString("packagetypemx"))) {
  864. querySQL.setWhere("t4.packagetypemx", whereObject.getString("packagetypemx"));
  865. }
  866. }
  867. querySQL.setPage(pageSize, pageNumber).setOrderBy("t1.rowno asc");
  868. Rows rows = querySQL.query();
  869. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  870. ArrayList<Long> itemnos = rows.toArrayList("itemno", new ArrayList<>());
  871. // SQLFactory sqlFactory1 = new SQLFactory(this, "商品发货数量汇总");
  872. // sqlFactory1.addParameter("siteid", siteid);
  873. // sqlFactory1.addParameter_in("itemnos", itemnos);
  874. // Rows sumQtyRows = dbConnect.runSqlQuery(sqlFactory1);
  875. // RowsMap sumQtyRowsMap = sumQtyRows.toRowsMap("itemno");
  876. //
  877. // SQLFactory sqlFactory2 = new SQLFactory(this, "商品未发货数量汇总");
  878. // sqlFactory2.addParameter("siteid", siteid);
  879. // sqlFactory2.addParameter_in("itemnos", itemnos);
  880. // Rows sumUnQtyRows = dbConnect.runSqlQuery(sqlFactory2);
  881. // RowsMap sumUnQtyRowsMap = sumUnQtyRows.toRowsMap("itemno");
  882. //
  883. //
  884. SQLFactory sqlFactory3 = new SQLFactory(this, "商品原价汇总");
  885. sqlFactory3.addParameter("siteid", siteid);
  886. sqlFactory3.addParameter("sys_enterpriseid", sys_enterpriseid);
  887. sqlFactory3.addParameter_in("itemnos", itemnos);
  888. Rows itempriceRows = dbConnect.runSqlQuery(sqlFactory3);
  889. RowsMap itempriceRowsMap = itempriceRows.toRowsMap("itemno");
  890. QuerySQL tpartreimbursement = SQLFactory.createQuerySQL(this, "sa_tpartreimbursementdetail", "sa_tpartreimbursementid", "sa_orderitemsid").setTableAlias("t1");
  891. tpartreimbursement.addJoinTable(JOINTYPE.inner, "sa_tpartreimbursement", "t2", "t2.sa_tpartreimbursementid=t1.sa_tpartreimbursementid", "billno");
  892. tpartreimbursement.setWhere("t1.sa_orderitemsid", rows.toArrayList("sa_orderitemsid"));
  893. RowsMap tpartreimbursementRowsMap = tpartreimbursement.query().toRowsMap("sa_orderitemsid");
  894. //查询附件
  895. RowsMap attinfoRowsMap = getAttachmentUrl("plm_item", ids);
  896. //默认商品图片
  897. Rows defaultImageRows = Item.getItemdefaultImage(this);
  898. // 封面cover
  899. RowsMap defaultItemCoverRowsMap = getAttachmentUrl("plm_item", ids, "cover");
  900. for (Row row : rows) {
  901. if (itempriceRowsMap.get(row.getString("itemno")).isNotEmpty()) {
  902. row.put("originalprice", itempriceRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("price"));
  903. } else {
  904. row.put("originalprice", BigDecimal.ZERO);
  905. }
  906. row.put("attinfos", attinfoRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  907. row.put("contractprice", 0);
  908. //处理日期字段空的情况
  909. row.putIfAbsent("needdate", "");
  910. row.putIfAbsent("deliverydate", "");
  911. //设置默认商品图片
  912. Rows coverRows = defaultItemCoverRowsMap.get(row.getString("itemid"));
  913. if (coverRows.isNotEmpty()) {
  914. row.put("cover", coverRows.get(0).getString("url"));
  915. } else if (defaultImageRows.isNotEmpty()) {
  916. row.put("cover", defaultImageRows.get(0).getString("url"));
  917. } else {
  918. row.put("cover", "");
  919. }
  920. Long sa_orderitemsid = row.getLong("sa_orderitemsid");
  921. row.put("tpartreimbursement", tpartreimbursementRowsMap.getOrDefault(sa_orderitemsid, new Rows()));
  922. }
  923. return getSucReturnObject().setData(rows).toString();
  924. }
  925. @API(title = "查询订单所有明细列表", apiversion = R.ID20230508111703.v1.class)
  926. public String selectAllList() throws YosException, IOException {
  927. StringBuffer where = new StringBuffer(" 1=1 ");
  928. if (sys_enterpriseid > 0) {
  929. where.append(" and t5.sys_enterpriseid ='").append(sys_enterpriseid).append("' ");
  930. }
  931. if (content.containsKey("where")) {
  932. JSONObject whereObject = content.getJSONObject("where");
  933. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  934. where.append(" and(");
  935. where.append(" t5.sonum like'%").append(whereObject.getString("condition")).append("%' ");
  936. where.append("or t1.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  937. where.append("or t1.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  938. where.append("or t6.agentnum like'%").append(whereObject.getString("condition")).append("%' ");
  939. where.append("or t7.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  940. where.append("or t1.remarks like'%").append(whereObject.getString("condition")).append("%' ");
  941. where.append("or t5.remarks like'%").append(whereObject.getString("condition")).append("%' ");
  942. where.append(")");
  943. }
  944. if (whereObject.containsKey("iteminfo") && !"".equals(whereObject.getString("iteminfo"))) {
  945. where.append(" and(");
  946. where.append("t1.itemno like'%").append(whereObject.getString("iteminfo")).append("%' ");
  947. where.append("or t1.itemname like'%").append(whereObject.getString("iteminfo")).append("%' ");
  948. where.append(")");
  949. }
  950. if (whereObject.containsKey("agentinfo") && !"".equals(whereObject.getString("agentinfo"))) {
  951. where.append(" and(");
  952. where.append("t6.agentnum like'%").append(whereObject.getString("agentinfo")).append("%' ");
  953. where.append("or t7.enterprisename like'%").append(whereObject.getString("agentinfo")).append("%' ");
  954. where.append(")");
  955. }
  956. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  957. where.append(" and t5.billdate >='").append(whereObject.getString("begindate")).append("' ");
  958. }
  959. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  960. where.append(" and t5.billdate <='").append(whereObject.getString("enddate")).append("' ");
  961. }
  962. if (whereObject.containsKey("begindate1") && !"".equals(whereObject.getString("begindate1"))) {
  963. where.append(" and t5.submitdate >='").append(whereObject.getString("begindate1")).append("' ");
  964. }
  965. if (whereObject.containsKey("enddate1") && !"".equals(whereObject.getString("enddate1"))) {
  966. where.append(" and t5.submitdate <='").append(getDate_Str(whereObject.getDate("enddate1"), 1)).append("' ");
  967. }
  968. if (whereObject.containsKey("begindate2") && !"".equals(whereObject.getString("begindate2"))) {
  969. where.append(" and t5.checkdate >='").append(whereObject.getString("begindate2")).append("' ");
  970. }
  971. if (whereObject.containsKey("enddate2") && !"".equals(whereObject.getString("enddate2"))) {
  972. where.append(" and t5.checkdate <='").append(getDate_Str(whereObject.getDate("enddate2"), 1)).append("' ");
  973. }
  974. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  975. where.append(" and t5.status ='").append(whereObject.getString("status")).append("' ");
  976. }
  977. if (whereObject.containsKey("isfreeze") && !"".equals(whereObject.getString("isfreeze"))) {
  978. where.append(" and t1.isfreeze ='").append(whereObject.getString("isfreeze")).append("' ");
  979. }
  980. if (whereObject.containsKey("typemx") && !"".equals(whereObject.getString("typemx"))) {
  981. where.append(" and t5.typemx ='").append(whereObject.getString("typemx")).append("' ");
  982. }
  983. if (whereObject.containsKey("isuncheckout") && !"".equals(whereObject.getString("isuncheckout"))) {
  984. String begindate = "";
  985. String enddate = "";
  986. if (whereObject.containsKey("datetype") && !"".equals(whereObject.getString("datetype"))) {
  987. String datetype = whereObject.getStringValue("datetype");
  988. switch (datetype) {
  989. case "7":
  990. begindate = getDate_Str(-7);
  991. enddate = getDate_Str();
  992. break;
  993. case "30":
  994. begindate = getDate_Str(-30);
  995. enddate = getDate_Str();
  996. break;
  997. case "90":
  998. begindate = getDate_Str(-90);
  999. enddate = getDate_Str();
  1000. break;
  1001. default:
  1002. break;
  1003. }
  1004. }
  1005. if (!begindate.equals("")) {
  1006. where.append(" and DATE_FORMAT(t5.submitdate, '%Y-%m-%d') >='").append(begindate).append("' ");
  1007. }
  1008. if (!enddate.equals("")) {
  1009. where.append(" and DATE_FORMAT(t5.submitdate, '%Y-%m-%d') <='").append(enddate).append("' ");
  1010. }
  1011. String isuncheckout = whereObject.getStringValue("isuncheckout");
  1012. if (isuncheckout.equals("1")) {
  1013. 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)");
  1014. } else {
  1015. 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)");
  1016. }
  1017. }
  1018. if (whereObject.containsKey("iscancel") && !"".equals(whereObject.getString("iscancel"))) {
  1019. String iscancel = whereObject.getStringValue("iscancel");
  1020. if (iscancel.equals("1")) {
  1021. 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)");
  1022. }
  1023. }
  1024. if (whereObject.containsKey("convenient") && !"".equals(whereObject.getString("convenient"))) {
  1025. String convenient = whereObject.getStringValue("convenient");
  1026. if (convenient.equals("待确认")) {
  1027. where.append(" and t5.status ='预提交' ");
  1028. } else if (convenient.equals("待审核")) {
  1029. where.append(" and t5.status ='提交' ");
  1030. } else if (convenient.equals("待发货")) {
  1031. 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)");
  1032. } else if (convenient.equals("已发货")) {
  1033. 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)");
  1034. } else if (convenient.equals("已取消")) {
  1035. where.append(" and t5.status ='手工关闭' and exists(select 1 from (\n" + "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)");
  1036. }
  1037. }
  1038. }
  1039. QuerySQL querySQL = queryAllListManage(where.toString());
  1040. querySQL.setPage(pageSize, pageNumber);
  1041. Rows rows = querySQL.query();
  1042. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  1043. RowsMap rowsMap = rows.toRowsMap("itemno");
  1044. ArrayList<Long> sa_orderitemsid = rows.toArrayList("sa_orderitemsid", new ArrayList<>());
  1045. QuerySQL t11 = SQLFactory.createQuerySQL(this, "sa_dispatch_items", "sa_orderitemsid", "siteid");
  1046. t11.addQueryFields("outwarehouseqty", "sum(outwarehouseqty)");
  1047. t11.setSiteid(siteid);
  1048. t11.setWhere("sa_orderitemsid", sa_orderitemsid);
  1049. t11.addGroupBy("sa_orderitemsid,siteid");
  1050. RowsMap outwarehouseqtyRowsMap = t11.query().toRowsMap("sa_orderitemsid");
  1051. SQLFactory sqlFactory1 = new SQLFactory(this, "订单明细出库数量汇总");
  1052. sqlFactory1.addParameter("siteid", siteid);
  1053. sqlFactory1.addParameter_in("sa_orderitemsids", rows.toArrayList("sa_orderitemsid", new ArrayList<>()));
  1054. Rows sumQtyRows = dbConnect.runSqlQuery(sqlFactory1);
  1055. RowsMap sumQtyRowsMap = sumQtyRows.toRowsMap("sa_orderitemsid");
  1056. ERPDocking erpDocking = new ERPDocking(siteid);
  1057. JSONArray jsonArray = new JSONArray();
  1058. if (rows.toJsonArray("itemno").size() != 0) {
  1059. // if (Parameter.getBoolean("system.ccerp_dockswitch")) {
  1060. // jsonArray = erpDocking.getErpIcinvbalRows(200000, 1, rows.toJsonArray("itemno"));
  1061. // }
  1062. }
  1063. if (!jsonArray.isEmpty()) {
  1064. for (Object object : jsonArray) {
  1065. JSONObject jsonObject = (JSONObject) object;
  1066. if (rowsMap.containsKey(jsonObject.getString("fitemno"))) {
  1067. if (rowsMap.get(jsonObject.getString("fitemno")).isNotEmpty()) {
  1068. for (Row row : rowsMap.get(jsonObject.getString("fitemno"))) {
  1069. row.put("invbalqty", jsonObject.getBigDecimal("fqty"));
  1070. }
  1071. }
  1072. }
  1073. }
  1074. }
  1075. //查询附件
  1076. RowsMap attinfoRowsMap = getAttachmentUrl("plm_item", ids);
  1077. for (Row row : rows) {
  1078. if (sumQtyRowsMap.get(row.getString("sa_orderitemsid")).isNotEmpty()) {
  1079. row.put("unsoldqty", row.getBigDecimal("qty").subtract(sumQtyRowsMap.get(row.getString("sa_orderitemsid")).get(0).getBigDecimal("sumqty")));
  1080. } else {
  1081. row.put("unsoldqty", row.getBigDecimal("qty"));
  1082. }
  1083. if (!row.containsKey("invbalqty")) {
  1084. row.put("invbalqty", BigDecimal.ZERO);
  1085. }
  1086. row.put("attinfos", attinfoRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  1087. row.put("contractprice", 0);
  1088. if (outwarehouseqtyRowsMap.get(row.getString("sa_orderitemsid")).isNotEmpty()) {
  1089. row.put("outwarehouseqty", outwarehouseqtyRowsMap.get(row.getString("sa_orderitemsid")).get(0).getBigDecimal("outwarehouseqty"));
  1090. } else {
  1091. row.put("outwarehouseqty", 0);
  1092. }
  1093. }
  1094. return getSucReturnObject().setData(rows).toString();
  1095. }
  1096. //查询订单列表(管理端)
  1097. public QuerySQL queryAllListManage(String where) throws YosException {
  1098. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_orderitems", "sa_orderitemsid", "itemid", "rowno", "itemno", "itemname", "unit", "qty", "undeliqty", "saleprice", "defaultprice", "price", "amount", "remarks", "customproperties", "batchcontrol", "model", "conversionrate", "marketprice", "defaultprice", "defaultamount", "auxqty", "needdate", "deliedqty", "rebateamount", "invoiceamount", "writeoffamount", "auxunit", "sa_orderid", "deliverydate", "returnqty", "stockno", "position", "batchno", "isfreeze", "decorationrebateflag", "isclose", "custamount");
  1099. querySQL.setTableAlias("t1");
  1100. querySQL.addJoinTable(JOINTYPE.left, "plm_item", "t3", "t3.itemid = t1.itemid and t3.siteid = t1.siteid", "orderminqty_auxunit", "orderminqty", "orderaddqty_auxunit", "orderaddqty", "spec", "standards", "packageqty", "delivery", "k3outcode");
  1101. querySQL.addJoinTable(JOINTYPE.left, "sa_order", "t5", "t5.sa_orderid = t1.sa_orderid and t5.siteid = t1.siteid", "sonum", "type", "typemx", "status", "billdate", "submitdate", "checkdate", "tradefield", "closedate", "createby");
  1102. querySQL.addJoinTable(JOINTYPE.left, "sa_agents", "t6", "t6.sys_enterpriseid = t5.sys_enterpriseid and t6.siteid = t5.siteid", "agentnum");
  1103. querySQL.addJoinTable(JOINTYPE.left, "sys_enterprise", "t7", "t7.sys_enterpriseid = t5.sys_enterpriseid and t7.siteid = t5.siteid", "enterprisename", "abbreviation");
  1104. querySQL.addJoinTable(JOINTYPE.left, "plm_itemclass", "t8", "t8.itemclassid = t3.marketingcategory and t8.siteid = t3.siteid", "itemclassname");
  1105. querySQL.addJoinTable(JOINTYPE.left, "sys_enterprise_tradefield", "t9", "t9.sys_enterpriseid = t5.sys_enterpriseid and t9.tradefield=t5.tradefield and t9.siteid = t5.siteid", "sys_enterprise_tradefieldid");
  1106. querySQL.addJoinTable(JOINTYPE.left, "sa_salearea", "t10", "t10.sa_saleareaid = t9.sa_saleareaid and t10.siteid = t9.siteid", "areaname");
  1107. // querySQL.addJoinTable(JOINTYPE.left, t11, "t11", "t11.sa_orderitemsid = t1.sa_orderitemsid and t11.siteid = t1.siteid");
  1108. querySQL.addJoinTable(JOINTYPE.left, "sa_accountclass", "t12", "t12.sa_accountclassid=t5.sa_accountclassid and t12.siteid=t5.siteid");
  1109. querySQL.addJoinTable(JOINTYPE.left, "sa_promotion", "t13", "t13.sa_promotionid=t5.sa_promotionid and t13.siteid=t5.siteid", "promnum", "promname");
  1110. 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' ),'[',''),']',''),'\\\"',''))");
  1111. querySQL.addQueryFields("orderremarks", "t5.remarks");
  1112. // querySQL.addQueryFields("outwarehouseqty", "ifnull(t11.outwarehouseqty,0)");
  1113. //querySQL.addQueryFields("accountclassinfos", "REPLACE(SUBSTRING(JSON_EXTRACT(t5.sa_accountclassinfos, '$[*].accountname'), 2, CHAR_LENGTH(JSON_EXTRACT(t5.sa_accountclassinfos, '$[*].accountname'))-2),'\"','')");
  1114. querySQL.setWhere("t1.siteid", siteid);
  1115. querySQL.setWhere(where);
  1116. querySQL.setWhere("t5.status in ('审核', '关闭','手工关闭') and t5.type!='配件订单'");
  1117. return querySQL;
  1118. }
  1119. //返回导出的标题
  1120. public HashMap<String, String> getTitleMap() {
  1121. HashMap<String, String> titleMap = new HashMap<>();
  1122. titleMap.put("rowno", "行号");
  1123. titleMap.put("batchcontrol", "是否批次管理");
  1124. titleMap.put("itemno", "产品编号");
  1125. titleMap.put("itemname", "产品名称");
  1126. titleMap.put("model", "型号");
  1127. titleMap.put("conversionrate", "换算率(辅助数量=主单位数量/换算率)");
  1128. titleMap.put("price", "单价,折后价(元)");
  1129. titleMap.put("amount", "金额,折后金额(元)");
  1130. titleMap.put("marketprice", "牌价、市场价(元)");
  1131. titleMap.put("defaultprice", "折前价(元)");
  1132. titleMap.put("defaultamount", "折前金额(元)");
  1133. titleMap.put("qty", "订购数量");
  1134. titleMap.put("auxqty", "辅助单位数量");
  1135. titleMap.put("needdate", "需求日期");
  1136. titleMap.put("deliedqty", "已交货量(主单位)");
  1137. titleMap.put("rebateamount", "返利分摊金额");
  1138. titleMap.put("undeliqty", "未交货量(主单位)");
  1139. titleMap.put("invoiceamount", "已开票金额");
  1140. titleMap.put("writeoffamount", "已核销金额");
  1141. titleMap.put("unit", "计量单位名称");
  1142. titleMap.put("auxunit", "辅助计量单位名称");
  1143. titleMap.put("deliverydate", "交货日期");
  1144. titleMap.put("returnqty", "退货数量");
  1145. titleMap.put("remarks", "备注");
  1146. titleMap.put("submitdate", "提交时间");
  1147. titleMap.put("checkdate", "审核时间");
  1148. return titleMap;
  1149. }
  1150. //返回导出的标题
  1151. public HashMap<String, String> getTitleMapAgent() throws YosException {
  1152. HashMap<String, String> titleMap = new HashMap<>();
  1153. titleMap.put("rowno", "行号");
  1154. titleMap.put("sonum", "订单号");
  1155. titleMap.put("status", "订单状态");
  1156. titleMap.put("type", "订单类型");
  1157. titleMap.put("billdate", "单据日期");
  1158. titleMap.put("tradefield", "领域");
  1159. titleMap.put("itemno", "产品编码");
  1160. titleMap.put("itemname", "品名");
  1161. titleMap.put("unit", "单位");
  1162. titleMap.put("qty", "数量");
  1163. titleMap.put("undeliqty", "未发货数量");
  1164. 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);
  1165. if (rowsrolename.isEmpty()) {
  1166. titleMap.put("price", "单价");
  1167. titleMap.put("amount", "金额");
  1168. }
  1169. titleMap.put("remarks", "订单行备注");
  1170. return titleMap;
  1171. }
  1172. @API(title = "查询可添加商品列表", apiversion = R.ID20221109153502.v1.class)
  1173. @CACHEING
  1174. public String selectItemList() throws YosException {
  1175. Long sa_orderid = content.getLong("sa_orderid");
  1176. boolean ismanage = content.getBooleanValue("ismanage");
  1177. String type = "";
  1178. Long order_sys_enterpriseid = 0L;
  1179. JSONArray sa_accountclassinfos = new JSONArray();
  1180. Rows orderRows = getOrderRows(this, sa_orderid);
  1181. if (orderRows.isNotEmpty()) {
  1182. type = orderRows.get(0).getString("type");
  1183. order_sys_enterpriseid = orderRows.get(0).getLong("sys_enterpriseid");
  1184. sa_accountclassinfos = orderRows.get(0).getJSONArray("sa_accountclassinfos");
  1185. }
  1186. boolean iswuliao = false;
  1187. for (Object sa_accountclassinfo : sa_accountclassinfos) {
  1188. JSONObject jsonObject = (JSONObject) sa_accountclassinfo;
  1189. if (jsonObject.getString("accountname").equals("物料账户")) {
  1190. iswuliao = true;
  1191. }
  1192. }
  1193. QuerySQL itemquery = SQLFactory.createQuerySQL(this, "plm_item", "itemid", "itemno", "itemname", "model", "batchcontrol", "orderminqty_auxunit", "orderminqty", "orderaddqty_auxunit", "orderaddqty", "spec", "standards", "delistingstatus", "marketprice", "packageqty", "iscustomsize", "widthschemeid", "lengthschemeid", "materialschemeid", "colorschemeid", "cheekschemeid", "sa_customschemeid", "custamount", "isonsale").setTableAlias("t1");
  1194. itemquery.addJoinTable(JOINTYPE.left, "plm_unitgroup", "t2", "t1.unitgroupid=t2.unitgroupid ", "conversionrate");
  1195. itemquery.addJoinTable(JOINTYPE.left, "plm_itemextend", "t3", "t1.itemid=t3.itemid ", "material", "erpitemno", "erpitemname", "specalnote", "prodline", "device");
  1196. itemquery.addJoinTable(JOINTYPE.left, "plm_unit", "t4", "t1.unitid=t4.unitid ");
  1197. itemquery.addJoinTable(JOINTYPE.left, "plm_unit", "t5", "t5.unitid=t2.auxunitid ");
  1198. if (!ismanage) {
  1199. if (!type.equals("特殊订单") && Enterprise.getEnterprise(this, order_sys_enterpriseid).getBoolean("saleclassauth")) {
  1200. itemquery.addJoinTable(JOINTYPE.inner, "sys_enterprise_itemauth_view", "t6", "t1.itemid=t6.itemid and t6.sys_enterpriseid=" + order_sys_enterpriseid);
  1201. }
  1202. }
  1203. itemquery.addQueryFields("unit", "t4.unitname");
  1204. itemquery.addQueryFields("auxunit", "t5.unitname");
  1205. itemquery.setSiteid(siteid);
  1206. itemquery.setCondition("t1.itemno", "t1.itemname", "t1.model", "t1.spec", "t1.standards", "t3.material", "t3.erpitemno");
  1207. itemquery.setWhere("status", "审核");
  1208. if (!ismanage) {
  1209. itemquery.setWhere("t1.isonsale=1 and t1.isused=1");
  1210. }
  1211. itemquery.setWhere("t1.ismodule=0");
  1212. if (content_where.containsKey("isonsale")) {
  1213. itemquery.setWhere("t1.isonsale", content_where.getBooleanValue("isonsale"));
  1214. }
  1215. if (iswuliao) {
  1216. itemquery.setWhere("t1.iswuliao", true);
  1217. }
  1218. itemquery.setPage(pageSize, pageNumber);
  1219. Rows rows = itemquery.query();
  1220. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  1221. //默认商品图片
  1222. Rows defaultImageRows = Item.getItemdefaultImage(this);
  1223. //查询附件
  1224. RowsMap attinfoRowsMap = Attachment.get(this, "plm_item", ids);
  1225. // 封面cover
  1226. RowsMap defaultItemCoverRowsMap = getAttachmentUrl("plm_item", ids, "cover");
  1227. //商品领域
  1228. RowsMap tradefieldRowsMap = Item.getTradefieldRowsMap(this, ids);
  1229. //品牌
  1230. RowsMap brandRowsMap = Brand.getBrandRowsMap(this, ids);
  1231. //价格
  1232. HashMap<Long, ItemPrice> itemPriceRowsMap = ItemPrice.getItemPrice(this, order_sys_enterpriseid, ids);
  1233. for (Row row : rows) {
  1234. row.put("attinfos", attinfoRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  1235. row.put("tradefield", tradefieldRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  1236. row.put("brand", brandRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  1237. if (itemPriceRowsMap.containsKey(row.getLong("itemid"))) {
  1238. row.put("price", itemPriceRowsMap.get(row.getLong("itemid")).getPrice());
  1239. } else {
  1240. row.put("price", 0);
  1241. }
  1242. //设置默认商品图片
  1243. Rows coverRows = defaultItemCoverRowsMap.get(row.getString("itemid"));
  1244. if (coverRows.isNotEmpty()) {
  1245. row.put("cover", coverRows.get(0).getString("url"));
  1246. } else if (defaultImageRows.isNotEmpty()) {
  1247. row.put("cover", defaultImageRows.get(0).getString("url"));
  1248. } else {
  1249. row.put("cover", "");
  1250. }
  1251. }
  1252. return getSucReturnObject().setData(rows).toString();
  1253. }
  1254. @API(title = "判断订单中是否存在某个套餐", apiversion = R.ID2024062715055803.v1.class)
  1255. @CACHEING
  1256. public String jundgePackagetypemx() throws YosException {
  1257. String packagetypemx = content.getString("packagetypemx");
  1258. long sa_orderid = content.getLong("sa_orderid");
  1259. Rows rows = dbConnect.runSqlQuery("select t1.sa_orderitemsid from sa_orderitems t1 inner join sa_promotion_items t2 on t1.sa_promotion_itemsid=t2.sa_promotion_itemsid and t1.siteid=t2.siteid where t1.siteid='" + siteid + "' and t1.sa_orderid=" + sa_orderid + " and t2.packagetypemx=" + packagetypemx);
  1260. if (rows.isNotEmpty()) {
  1261. return getSucReturnObject().setData(true).toString();
  1262. } else {
  1263. return getSucReturnObject().setData(false).toString();
  1264. }
  1265. }
  1266. //获取非工具数据
  1267. public String getOrderItemsList(Long sa_orderid, String tradefield, Long sys_enterpriseid, Long sa_brandid) throws YosException {
  1268. StringBuffer where = new StringBuffer(" 1=1 ");
  1269. Row enterpriseRow = Enterprise.getEnterprise(this, sys_enterpriseid);
  1270. boolean saleclassauth = enterpriseRow.getBoolean("saleclassauth");
  1271. //需要经营授权
  1272. if (saleclassauth) {
  1273. where.append(" and itemclassid in (SELECT itemclassid from sys_enterprise_saleclass WHERE sys_enterpriseid = " + sys_enterpriseid + " and siteid = '" + siteid + "')");
  1274. }
  1275. SQLFactory sqlFactory = new SQLFactory(this, "商品范围");
  1276. sqlFactory.addParameter("siteid", siteid);
  1277. sqlFactory.addParameter("sa_orderid", sa_orderid);
  1278. sqlFactory.addParameter_in("tradefield", tradefield);
  1279. sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  1280. sqlFactory.addParameter("sa_brandid", sa_brandid);
  1281. sqlFactory.addParameter_SQL("where", where);
  1282. return sqlFactory.getSQL();
  1283. }
  1284. // //获取特殊数据
  1285. // public String getOrderItemsListSpec(Long sa_orderid, String tradefield, Long sys_enterpriseid) throws YosException {
  1286. // SQLFactory sqlFactory = new SQLFactory(this, "商品范围_特殊");
  1287. // sqlFactory.addParameter("siteid", siteid);
  1288. // sqlFactory.addParameter("sa_orderid", sa_orderid);
  1289. // sqlFactory.addParameter_in("tradefield", tradefield);
  1290. // sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  1291. // return sqlFactory.getSQL(false);
  1292. // }
  1293. // @API(title = "订单商品变更", apiversion = R.ID20221110145302.v1.class)
  1294. // @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class})
  1295. // public String changeItem() throws YosException {
  1296. // boolean isjiean = content.getBooleanValue("isjiean");
  1297. // Long sa_orderid = content.getLong("sa_orderid");
  1298. // //通过版本更新订单表头数据
  1299. // beans.order.Order.updateOrderWithVersion(this);
  1300. //
  1301. // String type = content.getString("type");
  1302. // Long sa_orderitems_changeid = createTableID("sa_orderitems_change");
  1303. // JSONArray itemifnos = content.getJSONArray("itemifnos");
  1304. // BigDecimal oldValue = new BigDecimal(0);
  1305. // Rows Rowstatus = dbConnect.runSqlQuery(
  1306. // "select * from sa_order where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  1307. // if (Rowstatus.isEmpty()) {
  1308. // return "订单不存在";
  1309. // } else {
  1310. // if (!Rowstatus.get(0).getString("status").equals("审核")) {
  1311. // return getErrReturnObject().setErrMsg("非审核状态的订单无法进行商品变更").toString();
  1312. // }
  1313. // }
  1314. // ArrayList<String> sqlList = new ArrayList<>();
  1315. // // 订单商品变更
  1316. // SQLFactory sqlFactory = new SQLFactory(this, "订单商品变更_新增");
  1317. // sqlFactory.addParameter("siteid", siteid);
  1318. // sqlFactory.addParameter("userid", userid);
  1319. // sqlFactory.addParameter("username", username);
  1320. // sqlFactory.addParameter("sa_orderitems_changeid", sa_orderitems_changeid);
  1321. // sqlFactory.addParameter("sa_orderid", sa_orderid);
  1322. // sqlFactory.addParameter("remarks", content.getStringValue("remarks"));
  1323. // sqlList.add(sqlFactory.getSQL());
  1324. //
  1325. // HashMap<Long, Row> orderdetailmap = new HashMap<>();
  1326. // HashMap<Long, BigDecimal> orderdetailchangeamount = new HashMap<>();
  1327. // Rows rowsOrder = dbConnect.runSqlQuery("select * from sa_order t1 where t1.sa_orderid=" + sa_orderid);
  1328. //
  1329. // BigDecimal ce = new BigDecimal(0);
  1330. //
  1331. // for (Object obj : itemifnos) {
  1332. // JSONObject jsonObject = (JSONObject) obj;
  1333. // Rows rows = dbConnect.runSqlQuery(
  1334. // "SELECT sa_orderitemsid,qty,deliedqty,undeliqty,writeoffamount,price from sa_orderitems WHERE sa_orderitemsid="
  1335. // + jsonObject.getLongValue("sa_orderitemsid"));
  1336. // if (rows.isNotEmpty()) {
  1337. // oldValue = rows.get(0).getBigDecimal("qty");
  1338. // } else {
  1339. // return getErrReturnObject().setErrMsg("订单行不存在").toString();
  1340. // }
  1341. // if (type.equals("数量")) {
  1342. // // 更新数量
  1343. // BigDecimal newqty = jsonObject.getBigDecimal("newvalue");
  1344. // BigDecimal oldqty = rows.get(0).getBigDecimal("qty");
  1345. // BigDecimal oldundeliqty = rows.get(0).getBigDecimal("undeliqty");
  1346. // BigDecimal addqty = newqty.subtract(oldqty);// 新增的数量
  1347. // if (newqty.compareTo(oldqty) >= 0) {
  1348. // return getErrReturnObject().setErrMsg("修改数量必须小于原订单数量" + oldqty).toString();
  1349. // }
  1350. // if (newqty.compareTo(oldqty.subtract(oldundeliqty)) < 0) {
  1351. // return getErrReturnObject().setErrMsg("修改数量必须大于等于已发货数量" + oldqty.subtract(oldundeliqty)).toString();
  1352. // }
  1353. // if (newqty.compareTo(BigDecimal.ZERO) < 0) {
  1354. // return getErrReturnObject().setErrMsg("修改数量不可小于0").toString();
  1355. // }
  1356. //
  1357. // sqlFactory = new SQLFactory(this, "订单商品明细变更_更新");
  1358. // sqlFactory.addParameter("siteid", siteid);
  1359. // sqlFactory.addParameter("sa_orderitemsid", jsonObject.getLongValue("sa_orderitemsid"));
  1360. // sqlFactory.addParameter("qty", jsonObject.getStringValue("newvalue"));
  1361. //// sqlFactory.addParameter("undeliqty", oldundeliqty.add(addqty));
  1362. // ce = ce.add(addqty.multiply(rows.get(0).getBigDecimal("price")));
  1363. // sqlList.add(sqlFactory.getSQL());
  1364. // /**
  1365. // * * 核销对冲计算
  1366. // */
  1367. // {
  1368. // /**
  1369. // * * 核销对冲计算
  1370. // */
  1371. // BigDecimal writeoffamount = rows.get(0).getBigDecimal("writeoffamount");// 订单行已核销金额
  1372. // //System.out.println(newqty.multiply(rows.get(0).getBigDecimal("price")));
  1373. // //System.out.println(writeoffamount);
  1374. // if (writeoffamount.compareTo(BigDecimal.ZERO) > 0
  1375. // && (newqty.multiply(rows.get(0).getBigDecimal("price"))).compareTo(writeoffamount) < 0) {
  1376. // // 如果已核销金额大于0 且调整后折后金额小于已核销金额,则需要将差价进行回冲
  1377. // orderdetailmap.put(rows.get(0).getLong("sa_orderitemsid"), rows.get(0));
  1378. // //System.out.println(writeoffamount.subtract(newqty.multiply(rows.get(0).getBigDecimal("price"))));
  1379. // //System.out.println("sa_orderitemsid:"+rows.get(0).getLong("sa_orderitemsid"));
  1380. // orderdetailchangeamount.put(rows.get(0).getLong("sa_orderitemsid"),
  1381. // writeoffamount.subtract(newqty.multiply(rows.get(0).getBigDecimal("price"))));
  1382. // }
  1383. //
  1384. // }
  1385. // }
  1386. // // 订单商品变更明细
  1387. // sqlFactory = new SQLFactory(this, "订单商品变更明细_新增");
  1388. // sqlFactory.addParameter("siteid", siteid);
  1389. // sqlFactory.addParameter("sa_orderitems_changemxid", createTableID("sa_orderitems_changemx"));
  1390. // sqlFactory.addParameter("itemid", jsonObject.getLongValue("itemid"));
  1391. // sqlFactory.addParameter("sa_orderitemsid", jsonObject.getLongValue("sa_orderitemsid"));
  1392. // sqlFactory.addParameter("sa_orderitems_changeid", sa_orderitems_changeid);
  1393. // sqlFactory.addParameter("type", type);
  1394. // sqlFactory.addParameter("oldvalue", oldValue);
  1395. // sqlFactory.addParameter("newvalue", jsonObject.getStringValue("newvalue"));
  1396. //
  1397. // sqlList.add(sqlFactory.getSQL());
  1398. //
  1399. /// / content.put("sa_orderitemsid", sa_orderitemsid);
  1400. //
  1401. // }
  1402. //
  1403. // Long sa_cashbillid = 0L;
  1404. // if (ce.compareTo(BigDecimal.ZERO) != 0) {
  1405. // CashbillEntity cashbillEntity = new CashbillEntity();
  1406. // cashbillEntity.setAmount(ce);
  1407. // cashbillEntity.setDiscountamountamount(BigDecimal.ZERO);
  1408. // cashbillEntity.setOwnerid(sa_orderid);
  1409. // cashbillEntity.setOwnertable("sa_order");
  1410. // cashbillEntity
  1411. // .setRemarks("订单变更:" + rowsOrder.get(0).getString("sonum") + "订单变更单id:" + sa_orderitems_changeid);
  1412. // JSONObject cashbillPay = Accountbalance.createCashbillPay(this, rowsOrder.get(0).getLong("sys_enterpriseid"),
  1413. // rowsOrder.get(0).getLong("sa_accountclassid"), cashbillEntity, true);
  1414. // sqlList.addAll(cashbillPay.getJSONArray("sqlList").toJavaList(String.class));
  1415. // sa_cashbillid = cashbillPay.getLong("sa_cashbillid");
  1416. // }
  1417. //
  1418. // /**
  1419. // * * 核销对冲
  1420. // */
  1421. // if (!orderdetailmap.isEmpty()) {
  1422. // //System.out.println("测试");
  1423. // sqlList.addAll(
  1424. // new twriteoffbill_orderchange(rowsOrder.get(0), orderdetailmap, orderdetailchangeamount).hedging());
  1425. // }
  1426. // for (String string : sqlList) {
  1427. // //System.out.println(string);
  1428. // }
  1429. // dbConnect.runSqlUpdate(sqlList);
  1430. // // 居间费设置
  1431. // updateRebateFee(sa_orderid);
  1432. // if (!isjiean) {
  1433. // if (Parameter.getBoolean("system.erp_dockswitch")) {
  1434. // UploadDataToERP uploadDataToERP = new UploadDataToERP(this, sa_orderitems_changeid, "订单变更");
  1435. // boolean issuccess = uploadDataToERP.upload();
  1436. // if (!issuccess) {
  1437. // return getErrReturnObject().setErrMsg("上传ERP失败,请检查!").toString();
  1438. // }
  1439. // }
  1440. // }
  1441. //
  1442. // if (ce.compareTo(BigDecimal.ZERO) != 0) {
  1443. // Accountbalance.remindSend(this, rowsOrder.get(0).getLong("sys_enterpriseid"), sa_cashbillid);
  1444. // }
  1445. // return getSucReturnObject().toString();
  1446. // }
  1447. // @API(title = "订单商品变更(手动推erp)", apiversion = R.ID20230306161703.v1.class)
  1448. // @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class})
  1449. // public String changeItem1() throws YosException {
  1450. // if (Parameter.getBoolean("system.erp_dockswitch")) {
  1451. // UploadDataToERP uploadDataToERP = new UploadDataToERP(this, content.getLong("sa_orderitems_changeid"), "订单变更");
  1452. // boolean issuccess = uploadDataToERP.upload();
  1453. // if (!issuccess) {
  1454. // return getErrReturnObject().setErrMsg("上传ERP失败,请检查!").toString();
  1455. // } else {
  1456. // return getSucReturnObject().toString();
  1457. // }
  1458. // } else {
  1459. // return getErrReturnObject().setErrMsg("erp对接开关未开!").toString();
  1460. // }
  1461. //
  1462. // }
  1463. @API(title = "行关闭", apiversion = R.ID2025102210393903.v1.class)
  1464. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class})
  1465. public String close() throws YosException {
  1466. boolean isclose = content.getBoolean("isclose");
  1467. long sa_orderid = content.getLong("sa_orderid");
  1468. JSONArray sa_orderitemsids = content.getJSONArray("sa_orderitemsids");
  1469. String closereason = content.getStringValue("closereason");
  1470. ArrayList<String> sqlList = new ArrayList<>();
  1471. Rows rows = getOrderRows(this, sa_orderid);
  1472. if (rows.isEmpty()) {
  1473. return getErrReturnObject().setErrMsg("订单不存在").toString();
  1474. }
  1475. Row row = rows.get(0);
  1476. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_orderitems", "*").setTableAlias("t1");
  1477. querySQL.addJoinTable(JOINTYPE.inner, "sa_order", "t2", "t1.siteid = t2.siteid and t1.sa_orderid = t2.sa_orderid");
  1478. querySQL.setSiteid(siteid);
  1479. querySQL.setWhere("t1.sa_orderitemsid", sa_orderitemsids);
  1480. querySQL.setWhere("t2.sa_orderid", sa_orderid);
  1481. Rows rowsdetails = querySQL.query();
  1482. closevalidate(isclose, row, rowsdetails);
  1483. /******** 行关闭状态更新 ********/
  1484. Date date = getDate();
  1485. BigDecimal returnamount = BigDecimal.ZERO;// 退还金额
  1486. for (Row rowsdetail : rowsdetails) {
  1487. BigDecimal amount = rowsdetail.getBigDecimal("amount");// 订单金额
  1488. BigDecimal price = rowsdetail.getBigDecimal("price");// 单价
  1489. BigDecimal qty = rowsdetail.getBigDecimal("qty");// 订单数量
  1490. BigDecimal undeliqty = rowsdetail.getBigDecimal("undeliqty");
  1491. ;// 订单未发货数量
  1492. BigDecimal sendqty = qty.subtract(undeliqty);
  1493. BigDecimal sendamount = price.multiply(sendqty).setScale(2, RoundingMode.HALF_UP);
  1494. returnamount = returnamount.add(amount.subtract(sendamount));
  1495. sqlList.add("update sa_orderitems set isclose=" + isclose + ",closereason='" + closereason + "',closeby='" + username + "',closedate=CURRENT_TIME where sa_orderitemsid=" + rowsdetail.getLong("sa_orderitemsid"));
  1496. }
  1497. Rows freezRows = dbConnect.runSqlQuery("select * from sa_accountbalance_freez where sourcetable='sa_order' and sourceid=" + sa_orderid);
  1498. Row freezRow = null;
  1499. if (!freezRows.isEmpty()) {
  1500. freezRow = freezRows.get(0);
  1501. }
  1502. if (isclose) {
  1503. if (freezRow != null) {
  1504. BigDecimal amount = freezRow.getBigDecimal("amount");// 冻结金额
  1505. sqlList.add("update sa_accountbalance_freez set amount=" + amount.subtract(returnamount) + " where sa_accountbalance_freezid=" + freezRow.getLong("sa_accountbalance_freezid"));
  1506. sqlList.add("update sa_accountbalance set freezamount=freezamount-" + returnamount + " where sa_accountbalanceid=" + freezRow.getLong("sa_accountbalanceid"));
  1507. if (amount.subtract(returnamount).compareTo(BigDecimal.ZERO) <= 0) {
  1508. sqlList.add("delete from sa_accountbalance_freez where sa_accountbalance_freezid=" + freezRow.getLong("sa_accountbalance_freezid"));
  1509. }
  1510. }
  1511. sqlList.add(DataContrlLog.createLog(this, "sa_order", sa_orderid, "关闭", "行" + rowsdetails.toJsonArray("rowno") + "关闭成功").getSQL());
  1512. } else {
  1513. if (freezRow == null) {
  1514. Rows accountbalanceRows = dbConnect.runSqlQuery("select * from sa_accountbalance where sys_enterpriseid=" + row.getLong("sys_enterpriseid") + " and sa_accountclassid=" + row.getLong("sa_accountclassid"));
  1515. if (accountbalanceRows.isNotEmpty()) {
  1516. InsertSQL frezzInsertSQL = SQLFactory.createInsertSQL(this, "sa_accountbalance_freez");
  1517. frezzInsertSQL.setUniqueid(createTableID("sa_accountbalance_freez"));
  1518. frezzInsertSQL.setSiteid(siteid);
  1519. frezzInsertSQL.setValue("sa_accountbalanceid", accountbalanceRows.get(0).getLong("sa_accountbalanceid"));
  1520. frezzInsertSQL.setValue("sourcetable", "sa_order");
  1521. frezzInsertSQL.setValue("sourceid", sa_orderid);
  1522. frezzInsertSQL.setValue("amount", returnamount);
  1523. frezzInsertSQL.setValue("remarks", "订单反关闭冻结");
  1524. frezzInsertSQL.setValue("createby", username);
  1525. frezzInsertSQL.setDateValue("createdate");
  1526. sqlList.add(frezzInsertSQL.getSQL());
  1527. sqlList.add("update sa_accountbalance set freezamount=freezamount+" + returnamount + " where sa_accountbalanceid=" + accountbalanceRows.get(0).getLong("sa_accountbalanceid"));
  1528. } else {
  1529. return getErrReturnObject().setErrMsg("账户不存在").toString();
  1530. }
  1531. } else {
  1532. BigDecimal amount = freezRow.getBigDecimal("amount");// 冻结金额
  1533. sqlList.add("update sa_accountbalance_freez set amount=" + amount.add(returnamount) + " where sa_accountbalance_freezid=" + freezRow.getLong("sa_accountbalance_freezid"));
  1534. sqlList.add("update sa_accountbalance set freezamount=freezamount+" + returnamount + " where sa_accountbalanceid=" + freezRow.getLong("sa_accountbalanceid"));
  1535. }
  1536. sqlList.add(DataContrlLog.createLog(this, "sa_order", sa_orderid, "反关闭", "行" + rowsdetails.toJsonArray("rowno") + "反关闭成功").getSQL());
  1537. }
  1538. /******** 增加总仓可销售量 ********/
  1539. if (isclose) {
  1540. sqlList.addAll(updateIcinvbal_sale(rowsdetails, true, true));
  1541. } else {
  1542. sqlList.addAll(updateIcinvbal_sale(rowsdetails, false, true));
  1543. }
  1544. int i = dbConnect.runSqlUpdate(sqlList);
  1545. if (i >= 0) {
  1546. if (isclose) {
  1547. if (dbConnect.runSqlQuery("select * from sa_orderitems where isclose=0 and undeliqty!=0 and sa_orderid=" + sa_orderid).isEmpty()) {
  1548. dbConnect.runSqlUpdate("update sa_order set status='关闭',closeby='" + username + "',closedate=CURRENT_TIME where sa_orderid=" + sa_orderid);
  1549. Rows accountbalancerows = dbConnect.runSqlQuery("select * from sa_accountbalance t1 inner join sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid where t2.accountname='现金账户' and t1.sys_enterpriseid=" + row.getLong("sys_enterpriseid"));
  1550. dbConnect.runSqlUpdate("UPDATE sa_accountbalance t1 left JOIN sa_accountbalance_freez t2 ON t1.sa_accountbalanceid = t2.sa_accountbalanceid " + "SET t1.freezamount = case when ifnull(t1.freezamount,0)>ifnull(t2.amount,0) then t1.freezamount-ifnull(t2.amount,0) else 0 end " + "WHERE t1.sa_accountbalanceid=" + accountbalancerows.get(0).getLong("sa_accountbalanceid") + " and t2.sourcetable='sa_order' and t2.sourceid=" + sa_orderid);
  1551. dbConnect.runSqlUpdate("delete from sa_accountbalance_freez t1 WHERE t1.sourcetable='sa_order' and t1.sourceid=" + sa_orderid);
  1552. }
  1553. } else {
  1554. dbConnect.runSqlUpdate("update sa_order set status='审核',closeby='',closedate=null where sa_orderid=" + sa_orderid);
  1555. }
  1556. }
  1557. return getSucReturnObject().toString();
  1558. }
  1559. public void closevalidate(boolean isclose, Row order, Rows orderdetails) throws YosException {
  1560. if (isclose && !order.getString("status").equals("审核")) {
  1561. throw new YosException("非审核状态下不可关闭");
  1562. }
  1563. if (isclose && orderdetails.size() == 0) {
  1564. throw new YosException("请购选行再进行关闭操作");
  1565. }
  1566. if (!isclose && orderdetails.size() == 0) {
  1567. throw new YosException("请购选行再进行取消关闭操作");
  1568. }
  1569. int i = 0;
  1570. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "st_stockbill_items", "*").setTableAlias("t1");
  1571. querySQL.addJoinTable(JOINTYPE.inner, "st_stockbill", "t2", "t1.siteid = t2.siteid and t1.st_stockbillid = t2.st_stockbillid");
  1572. querySQL.setSiteid(siteid);
  1573. querySQL.setWhere("t1.sa_orderitemsid", orderdetails.toArrayList("sa_orderitemsid"));
  1574. querySQL.setWhere("t2.status='新建'");
  1575. Rows rows = querySQL.query();
  1576. RowsMap rowsMap = rows.toRowsMap("sa_orderitemsid");
  1577. for (Row row : orderdetails) {
  1578. int rownum = row.getInteger("rowno");
  1579. if (isclose) {
  1580. if (row.getBoolean("isclose")) {
  1581. throw new YosException("行" + rownum + "已经关闭,不可重复关闭");
  1582. }
  1583. if (row.getBigDecimal("undeliqty").compareTo(BigDecimal.ZERO) == 0) {
  1584. throw new YosException("行" + rownum + "已发货完毕,不可关闭");
  1585. }
  1586. if (rowsMap.containsKey(row.getString("sa_orderitemsid"))) {
  1587. throw new YosException("行" + rownum + "存在新建的销售出库单,不可关闭");
  1588. }
  1589. // if (saorderdetail.getPao(i).getPaoSet("SAINVOICEDETAIL")
  1590. // .count() > 0) {
  1591. // throw new YosException("行" + rownum
  1592. // + "存在发货单,不可关闭");
  1593. // }
  1594. } else {
  1595. if (!row.getBoolean("isclose")) {
  1596. throw new YosException("行" + rownum + "未关闭,不可取消关闭");
  1597. }
  1598. if (row.getDouble("undeliqty") == 0) {
  1599. throw new YosException("行" + rownum + "已发货完毕,不可取消关闭");
  1600. }
  1601. }
  1602. }
  1603. }
  1604. /**
  1605. * 获取当前订单的最大行号
  1606. *
  1607. * @param sa_orderid
  1608. * @return
  1609. * @throws YosException
  1610. */
  1611. public Long getRowNo(Long sa_orderid) throws YosException {
  1612. Rows rows = dbConnect.runSqlQuery("SELECT COUNT(0) count from sa_orderitems WHERE sa_orderid = " + sa_orderid + " AND siteid = '" + siteid + "'");
  1613. return rows.get(0).getLong("count") + 1;
  1614. }
  1615. /**
  1616. * 查询商品信息
  1617. *
  1618. * @param itemid
  1619. * @return
  1620. * @throws YosException
  1621. */
  1622. public Row getItemRow(Long itemid) throws YosException {
  1623. SQLFactory sqlFactory = new SQLFactory(this, "查询商品信息");
  1624. sqlFactory.addParameter("siteid", siteid);
  1625. sqlFactory.addParameter("itemid", itemid);
  1626. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  1627. return rows.isNotEmpty() ? rows.get(0) : new Row();
  1628. }
  1629. /**
  1630. * 重新对商品行排序
  1631. *
  1632. * @param sa_orderid
  1633. * @throws YosException
  1634. */
  1635. public void updateRowNo(Long sa_orderid) throws YosException {
  1636. String sql = "SELECT sa_orderitemsid from sa_orderitems WHERE sa_orderid= " + sa_orderid + " and siteid = '" + siteid + "' ORDER BY createdate ";
  1637. ArrayList<Long> sa_orderitemsids = dbConnect.runSqlQuery(sql).toArrayList("sa_orderitemsid", new ArrayList<>(), false);
  1638. if (sa_orderitemsids.size() > 0) {
  1639. int rowno = 1;
  1640. StringBuffer where = new StringBuffer("");
  1641. for (Long id : sa_orderitemsids) {
  1642. where.append(" WHEN " + id + " THEN " + rowno);
  1643. rowno++;
  1644. }
  1645. SQLFactory sqlFactory = new SQLFactory(this, "更新行号");
  1646. sqlFactory.addParameter("siteid", siteid);
  1647. sqlFactory.addParameter_SQL("where", where);
  1648. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsids);
  1649. sql = sqlFactory.getSQL();
  1650. dbConnect.runSqlUpdate(sql);
  1651. }
  1652. }
  1653. /**
  1654. * @param isinstock 是否增加库存
  1655. * @throws YosException
  1656. */
  1657. public ArrayList<String> updateIcinvbal_sale(Rows rowsdetails, boolean isinstock, boolean close) throws YosException {
  1658. Rows ICINVBAL_SALE = SQLFactory.createQuerySQL(this, "st_invbal_sale").setSiteid(siteid).setWhere("itemid", rowsdetails.toArrayList("itemid")).query();
  1659. RowsMap ICINVBAL_SALERowsMap = ICINVBAL_SALE.toRowsMap("itemid");
  1660. int i = 0;
  1661. ArrayList<String> sqlList = new ArrayList<>();
  1662. for (Row row : rowsdetails) {
  1663. long st_invbal_saleid;
  1664. if (ICINVBAL_SALERowsMap.containsKey(row.getString("itemid"))) {
  1665. st_invbal_saleid = ICINVBAL_SALERowsMap.get(row.getString("itemid")).get(0).getLong("st_invbal_saleid");
  1666. } else {
  1667. st_invbal_saleid = createTableID("st_invbal_sale");
  1668. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "st_invbal_sale");
  1669. insertSQL.setUniqueid(st_invbal_saleid);
  1670. insertSQL.setSiteid(siteid);
  1671. insertSQL.setValue("itemid", row.getString("itemid"));
  1672. insertSQL.setValue("candispatchqty", 0);
  1673. insertSQL.setValue("cansaleqty", 0);
  1674. insertSQL.setValue("qty", 0);
  1675. insertSQL.setValue("changeuserid", userid);
  1676. insertSQL.setValue("changeby", username);
  1677. insertSQL.setDateValue("changedate");
  1678. sqlList.add(insertSQL.getSQL());
  1679. }
  1680. BigDecimal qty = BigDecimal.ZERO;
  1681. if (close) {
  1682. qty = isinstock ? row.getBigDecimal("undeliqty") : row.getBigDecimal("undeliqty").negate();// 如果是扣减则根据数量进行扣减,反之根据未发货数量进行增加
  1683. } else {
  1684. qty = isinstock ? row.getBigDecimal("undeliqty") : row.getBigDecimal("qty").negate();// 如果是扣减则根据数量进行扣减,反之根据未发货数量进行增加
  1685. }
  1686. sqlList.add("update st_invbal_sale set cansaleqty=cansaleqty+" + qty + " where st_invbal_saleid=" + st_invbal_saleid);
  1687. }
  1688. return sqlList;
  1689. }
  1690. @API(title = "刷订单保修状态", apiversion = R.ID2026040214285502.v1.class)
  1691. public String updatebillingstatus() throws YosException {
  1692. System.err.println(getDateTime_Str() + ":准备刷订单保修状态...");
  1693. String sql = "SELECT if(billdate BETWEEN begdate and enddate,if(price>0,'保内收费','保内免费'),'保外收费' ) billingstatus,sa_orderitemsid ,verifiedqty,tobeoffqty,canoffqty,qty from ( " +
  1694. "SELECT t1.sa_orderitemsid,t1.price,t2.billdate, IF(isvoid=1,voiddate,enddate) enddate,begdate,verifiedqty,tobeoffqty,canoffqty,qty from sa_orderitems t1 " +
  1695. "INNER JOIN sa_order t2 ON t2.sa_orderid=t1.sa_orderid " +
  1696. "LEFT JOIN sa_warrantycard t3 ON t3.cardno=t1.cardno " +
  1697. "WHERE t2.type='配件订单' " +
  1698. "and t1.billingstatus is null " +
  1699. "and t2.deleted=0 " +
  1700. "and t2.billdate<='2025-12-05' " +
  1701. "ORDER BY t2.billdate " +
  1702. ") a ";
  1703. // 用于暂存 SQL 的列表
  1704. ArrayList<String> sqlList = new ArrayList<>();
  1705. // 设定批次大小
  1706. int batchSize = 5000;
  1707. Rows rows = dbConnect.runSqlQuery(sql);
  1708. int count = 0; // 计数器
  1709. for (Row row : rows) {
  1710. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "sa_orderitems");
  1711. updateSQL.setSiteid(siteid);
  1712. updateSQL.setUniqueid(row.getLong("sa_orderitemsid"));
  1713. updateSQL.setValue("billingstatus", row.getString("billingstatus"));
  1714. updateSQL.setValue("tobeoffqty", 0);
  1715. if (row.getString("billingstatus").equals("保内收费")) {
  1716. updateSQL.setValue("canoffqty", row.getString("qty"));
  1717. updateSQL.setValue("offstatus", 1);
  1718. } else {
  1719. updateSQL.setValue("canoffqty", 0);
  1720. }
  1721. // 将生成的 SQL 加入列表
  1722. sqlList.add(updateSQL.getSQL());
  1723. count++;
  1724. // 当达到批次大小时,执行更新并清空列表
  1725. if (count % batchSize == 0) {
  1726. System.err.println(getDateTime_Str() + ":正在执行第 " + count + " 条数据的批量更新...");
  1727. dbConnect.runSqlUpdate(sqlList);
  1728. sqlList.clear(); // 清空列表以释放内存
  1729. }
  1730. }
  1731. // 【重要】循环结束后,处理剩余不足 5000 条的数据
  1732. if (!sqlList.isEmpty()) {
  1733. System.err.println(getDateTime_Str() + ":正在执行最后剩余 " + sqlList.size() + " 条数据的批量更新...");
  1734. dbConnect.runSqlUpdate(sqlList);
  1735. sqlList.clear();
  1736. }
  1737. System.err.println(getDateTime_Str() + ":结束刷订单保修状态...");
  1738. return getSucReturnObject().toString();
  1739. }
  1740. }