OrderItems.java 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. package restcontroller.webmanage.sale.order;
  2. import beans.accountbalance.Accountbalance;
  3. import beans.accountbalance.CashbillEntity;
  4. import beans.brand.Brand;
  5. import beans.datacontrllog.DataContrlLog;
  6. import beans.dataextend.DataExtend;
  7. import beans.datateam.DataTeam;
  8. import beans.enterprise.Enterprise;
  9. import beans.itemprice.ItemPrice;
  10. import beans.parameter.Parameter;
  11. import beans.uploaderpdata.UploadDataToERP;
  12. import com.alibaba.fastjson.JSONArray;
  13. import com.alibaba.fastjson.JSONObject;
  14. import common.Controller;
  15. import common.YosException;
  16. import common.annotation.API;
  17. import common.annotation.CACHEING;
  18. import common.annotation.CACHEING_CLEAN;
  19. import common.data.Row;
  20. import common.data.Rows;
  21. import common.data.RowsMap;
  22. import common.data.SQLFactory;
  23. import restcontroller.R;
  24. import restcontroller.webmanage.sale.aftersalesmag.twriteoffbill_orderchange;
  25. import restcontroller.webmanage.sale.rebate.Rebate;
  26. import restcontroller.webmanage.sale.toolbill.ToolBilItem;
  27. import restcontroller.webmanage.sale.toolbill.ToolBill;
  28. import utility.ERPDocking;
  29. import java.io.IOException;
  30. import java.math.BigDecimal;
  31. import java.util.ArrayList;
  32. import java.util.HashMap;
  33. import static beans.order.Order.*;
  34. /**
  35. * 销售订单商品明细表
  36. */
  37. public class OrderItems extends Controller {
  38. /**
  39. * 构造函数
  40. *
  41. * @param content
  42. */
  43. public OrderItems(JSONObject content) throws YosException {
  44. super(content);
  45. }
  46. @API(title = "新增或更新", apiversion = R.ID20221109093602.v1.class)
  47. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class})
  48. public String insertOrUpdate() throws YosException {
  49. Long sa_orderid = content.getLong("sa_orderid");
  50. Long sys_enterpriseid = content.getLong("sys_enterpriseid");
  51. // Long sa_contractid = content.getLongValue("sa_contractid");
  52. Rows orderRows = getOrderRows(this, sa_orderid);
  53. if (orderRows.isEmpty()) {
  54. return getErrReturnObject().setErrMsg("数据不存在,无法添加").toString();
  55. }
  56. // sa_contractid = orderRows.get(0).getLong("sa_contractid");
  57. String type = orderRows.get(0).getString("type");
  58. JSONArray items = content.getJSONArray("items");
  59. Long sa_promotionid=orderRows.get(0).getLong("sa_promotionid");
  60. ArrayList<String> sqlList = new ArrayList<>();
  61. //查询erp库存
  62. // Invbal.selectErpInvbal(this, items);
  63. Rows itemRows =dbConnect.runSqlQuery("select itemid,orderaddqty,orderminqty,itemno from plm_item where siteid='"+siteid+"'");
  64. RowsMap itemRowsMap= itemRows.toRowsMap("itemid");
  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.getBigDecimalValue("qty");
  70. if(!type.equals("促销订单")){
  71. if(itemRowsMap.containsKey(item.getString("itemid"))){
  72. if(itemRowsMap.get(item.getString("itemid")).isNotEmpty()){
  73. BigDecimal orderaddqty =itemRowsMap.get(item.getString("itemid")).get(0).getBigDecimal("orderaddqty");
  74. BigDecimal orderminqty =itemRowsMap.get(item.getString("itemid")).get(0).getBigDecimal("orderminqty");
  75. if(((qty.subtract(orderminqty)).remainder(orderaddqty)).compareTo(BigDecimal.ZERO)!=0){
  76. return getErrReturnObject().setErrMsg("品号为"+itemRowsMap.get(item.getString("itemid")).get(0).getString("itemno")+"的商品数量不符合该商品的起订量和增量").toString();
  77. }
  78. }
  79. }
  80. }
  81. //商品信息
  82. Row itemRow = getItemRow(itemid);
  83. Rows promotionitems =dbConnect.runSqlQuery("select * from sa_promotion_items where siteid='"+siteid+"' and sa_promotionid="+sa_promotionid);
  84. RowsMap promotionitemsRowsMap = promotionitems.toRowsMap("itemid");
  85. if(type.equals("促销订单")){
  86. if(promotionitemsRowsMap.containsKey(item.getString("itemid"))){
  87. if(promotionitemsRowsMap.get(item.getString("itemid")).isNotEmpty()){
  88. BigDecimal saledqty =promotionitemsRowsMap.get(item.getString("itemid")).get(0).getBigDecimal("saledqty");
  89. BigDecimal groupqty =promotionitemsRowsMap.get(item.getString("itemid")).get(0).getBigDecimal("groupqty");
  90. boolean islimit =promotionitemsRowsMap.get(item.getString("itemid")).get(0).getBoolean("islimit");
  91. if(islimit){
  92. if(groupqty.compareTo(saledqty.add(qty))<0){
  93. return getErrReturnObject().setErrMsg(itemRow.getString("itemno")+"商品数量已超过促销活动的限定数量【"+groupqty.subtract(saledqty)+"】,无法新建").toString();
  94. }
  95. }
  96. }
  97. }
  98. }
  99. BigDecimal price1=new BigDecimal(0);
  100. SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_新增");
  101. if (sa_orderitemsid <= 0) {
  102. Rows rows = dbConnect.runSqlQuery("SELECT sa_orderitemsid,price from sa_orderitems WHERE siteid='" + siteid + "' and itemid=" + itemid + " and sa_orderid=" + sa_orderid);
  103. if (rows.isEmpty()) {
  104. sa_orderitemsid = createTableID("sa_orderitems");
  105. } else {
  106. price1=rows.get(0).getBigDecimal("price");
  107. sa_orderitemsid = rows.get(0).getLong("sa_orderitemsid");
  108. sqlFactory = new SQLFactory(this, "订单商品明细_更新");
  109. }
  110. } else {
  111. sqlFactory = new SQLFactory(this, "订单商品明细_更新");
  112. }
  113. sqlFactory.addParameter("siteid", siteid);
  114. sqlFactory.addParameter("userid", userid);
  115. sqlFactory.addParameter("username", username);
  116. sqlFactory.addParameter("sa_orderitemsid", sa_orderitemsid);
  117. sqlFactory.addParameter("sa_orderid", sa_orderid);
  118. sqlFactory.addParameter("rowno", 1);
  119. sqlFactory.addParameter("itemid", itemid);
  120. sqlFactory.addParameter("remarks", item.getStringValue("remarks"));
  121. BigDecimal conversionrate = itemRow.getBigDecimal("conversionrate");
  122. if (conversionrate.compareTo(BigDecimal.ZERO) <= 0) {
  123. conversionrate = BigDecimal.valueOf(1);
  124. }
  125. sqlFactory.addParameter("itemno", itemRow.getString("itemno"));
  126. sqlFactory.addParameter("itemname", itemRow.getString("itemname"));
  127. sqlFactory.addParameter("model", itemRow.getString("model"));
  128. sqlFactory.addParameter("unit", itemRow.getString("unit"));
  129. sqlFactory.addParameter("auxunit", itemRow.getString("auxunit"));
  130. sqlFactory.addParameter("batchcontrol", itemRow.getLong("batchcontrol"));
  131. sqlFactory.addParameter("delivery", itemRow.getLong("delivery"));
  132. sqlFactory.addParameter("stockno", item.getOrDefault("stockno", "null"));
  133. sqlFactory.addParameter("position", item.getOrDefault("position", "null"));
  134. sqlFactory.addParameter("batchno", item.getOrDefault("batchno", "null"));
  135. String deliverydate = item.getStringValue("deliverydate");
  136. Rows rows = dbConnect.runSqlQuery("SELECT deliverydate,defaultprice from sa_orderitems WHERE sa_orderitemsid =" + sa_orderitemsid);
  137. if (deliverydate.isEmpty()) {
  138. if (rows.isNotEmpty()) {
  139. deliverydate = rows.get(0).getString("deliverydate");
  140. } else {
  141. deliverydate = "null";
  142. }
  143. }
  144. String needdate = item.getStringValue("needdate");
  145. sqlFactory.addParameter("needdate", needdate.equals("") ? "null" : needdate);
  146. sqlFactory.addParameter("deliverydate", deliverydate.equals("") ? "null" : deliverydate);
  147. sqlFactory.addParameter("conversionrate", conversionrate);
  148. //订购数量
  149. sqlFactory.addParameter("qty", qty);
  150. //辅助单位数量
  151. sqlFactory.addParameter("auxqty", qty.divide(conversionrate));
  152. BigDecimal defaultprice;
  153. BigDecimal price;
  154. //价格
  155. ItemPrice itemPrice = ItemPrice.getItemPrice(this, sys_enterpriseid, itemid);
  156. if (!item.containsKey("defaultprice")) {
  157. switch (type) {
  158. // case "项目订单":
  159. // defaultprice = itemPrice.getContractprice(sa_contractid);
  160. // break;
  161. // case "工具订单":
  162. // defaultprice = itemPrice.getMarketprice();
  163. // break;
  164. case "促销订单":
  165. defaultprice = itemPrice.getGraderateprice();
  166. price= itemPrice.getPromotionPrice(sa_promotionid);
  167. break;
  168. default:
  169. defaultprice = itemPrice.getGraderateprice();
  170. price= itemPrice.getGraderateprice();
  171. }
  172. } else {
  173. price = item.getBigDecimalValue("price");
  174. if(price.compareTo(price1)!=0){
  175. sqlList.add(DataContrlLog.createLog(this, "sa_order", sa_orderid, "订单商品【"+itemRow.getString("itemname")+"】由单价:"+price1+"修改为单价:"+price, "订单修改单价").getSQL());
  176. }
  177. defaultprice = item.getBigDecimalValue("defaultprice");
  178. // if (!type.equals("特殊订单") && defaultprice.compareTo(rows.get(0).getBigDecimal("defaultprice")) != 0 && defaultprice.compareTo(itemPrice.getContractprice()) < 0) {
  179. // return getErrReturnObject().setErrMsg("调整价格不可小于协议价").toString();
  180. // }
  181. }
  182. if(type.equals("特殊订单")){
  183. if(item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO)>0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO)<=0){
  184. sqlFactory.addParameter("price", item.getBigDecimalValue("price"));
  185. //折前金额(元)
  186. sqlFactory.addParameter("amount", price.multiply(qty));
  187. }else if(item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO)<=0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO)>0){
  188. sqlFactory.addParameter("price",item.getBigDecimalValue("amount").divide(qty,4));
  189. //折前金额(元)
  190. sqlFactory.addParameter("amount", item.getBigDecimalValue("amount"));
  191. }
  192. }else {
  193. sqlFactory.addParameter("price", price);
  194. //折前金额(元)
  195. sqlFactory.addParameter("amount", price.multiply(qty));
  196. }
  197. //单价,折后价(元),取合同价
  198. sqlFactory.addParameter("defaultprice", defaultprice);
  199. //金额,折后金额(元)
  200. sqlFactory.addParameter("defaultamount", defaultprice.multiply(qty));
  201. //牌价、市场价(元),标准订单牌价取商品价格,项目订单取合同里的牌价
  202. sqlFactory.addParameter("marketprice", itemPrice.getMarketprice());
  203. sqlList.add(sqlFactory.getSQL());
  204. }
  205. dbConnect.runSqlUpdate(sqlList);
  206. //重新排序
  207. updateRowNo(sa_orderid);
  208. //居间费设置
  209. updateRebateFee(sa_orderid);
  210. //调整价格(返利金)
  211. if (orderRows.get(0).getBoolean("rebate_used")) {
  212. setRebateAmount(this, sa_orderid, getMaxUsedRebateAmount(this, sa_orderid));
  213. adjustLastRebateAmount(this, sa_orderid);
  214. }
  215. // else {
  216. // dbConnect.runSqlUpdate("UPDATE sa_orderitems SET amount=amount-rebateamount,price=amount/qty WHERE sa_orderid='" + sa_orderid + "' and siteid='" + siteid + "' and qty!=0");
  217. // dbConnect.runSqlUpdate("UPDATE sa_orderitems SET amount=0 WHERE sa_orderid='" + sa_orderid + "' and siteid='" + siteid + "' and qty=0");
  218. // }
  219. return getSucReturnObject().toString();
  220. }
  221. /**
  222. * 更新居间费信息
  223. */
  224. public void updateRebateFee(Long sa_orderid) throws YosException {
  225. Rows orderRows = getOrderRows(this, sa_orderid);
  226. if (orderRows.isEmpty()) {
  227. return;
  228. }
  229. Long sa_projectid = orderRows.get(0).getLong("sa_projectid");
  230. Rows contractRows = dbConnect.runSqlQuery("SELECT * from sa_contract WHERE sa_projectid = " + sa_projectid + " and type='居间' and status='审核'");
  231. if (contractRows.isEmpty()) {
  232. return;
  233. }
  234. Long sa_contractid = contractRows.get(0).getLong("sa_contractid");
  235. Rows partiesRows = dbConnect.runSqlQuery("SELECT sys_enterpriseid from sa_project_parties WHERE type ='居间服务商' and sa_projectid = " + sa_projectid + " and siteid ='" + siteid + "'");
  236. if (partiesRows.isEmpty()) {
  237. return;
  238. }
  239. Long sys_enterpriseid_temp = partiesRows.get(0).getLong("sys_enterpriseid");
  240. content.put("sys_enterpriseid", sys_enterpriseid_temp);
  241. content.put("ownertable", "sa_order");
  242. content.put("ownerid", sa_orderid);
  243. content.put("billdate", "");
  244. content.put("remarks", "");
  245. content.put("sa_contractid", sa_contractid);
  246. content.put("settlementmode", "线上");
  247. content.put("sa_rebatesettlementid", 0);
  248. new Rebate(content).insertOrUpdate();
  249. }
  250. @API(title = "详情", apiversion = R.ID20221110134302.v1.class)
  251. @CACHEING
  252. public String selectDetail() throws YosException {
  253. Long sa_orderitemsid = content.getLong("sa_orderitemsid");
  254. SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_详情");
  255. sqlFactory.addParameter("siteid", siteid);
  256. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsid);
  257. String sql = sqlFactory.getSQL();
  258. Rows rows = dbConnect.runSqlQuery(sql);
  259. Row row = rows.isNotEmpty() ? rows.get(0) : new Row();
  260. return getSucReturnObject().setData(row).toString();
  261. }
  262. @API(title = "删除", apiversion = R.ID20221109093702.v1.class)
  263. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class, ToolBill.class})
  264. public String delete() throws YosException {
  265. Long sa_orderid = content.getLong("sa_orderid");
  266. JSONArray sa_orderitemsids = content.getJSONArray("sa_orderitemsids");
  267. SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_删除");
  268. sqlFactory.addParameter("siteid", siteid);
  269. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsids.toArray());
  270. String sql = sqlFactory.getSQL();
  271. dbConnect.runSqlUpdate(sql);
  272. //重新排序
  273. updateRowNo(sa_orderid);
  274. //居间费设置
  275. updateRebateFee(sa_orderid);
  276. //调整价格(返利金)
  277. Rows orderRows = getOrderRows(this, sa_orderid);
  278. if (orderRows.get(0).getBoolean("rebate_used")) {
  279. setRebateAmount(this, sa_orderid, getMaxUsedRebateAmount(this, sa_orderid));
  280. adjustLastRebateAmount(this, sa_orderid);
  281. }
  282. // else {
  283. // dbConnect.runSqlUpdate("UPDATE sa_orderitems SET amount=amount-rebateamount,price=amount/qty WHERE sa_orderid='" + sa_orderid + "' and siteid='" + siteid + "' and qty!=0");
  284. // dbConnect.runSqlUpdate("UPDATE sa_orderitems SET amount=0 WHERE sa_orderid='" + sa_orderid + "' and siteid='" + siteid + "' and qty=0");
  285. // }
  286. return getSucReturnObject().toString();
  287. }
  288. @API(title = "关闭", apiversion = R.ID20221109093802.v1.class)
  289. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class})
  290. public String close() throws YosException {
  291. Long sa_orderid = content.getLong("sa_orderid");
  292. JSONArray sa_orderitemsids = content.getJSONArray("sa_orderitemsids");
  293. SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_行关闭");
  294. sqlFactory.addParameter("siteid", siteid);
  295. sqlFactory.addParameter("userid", userid);
  296. sqlFactory.addParameter("username", username);
  297. sqlFactory.addParameter("closereason", content.getStringValue("closereason"));
  298. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsids.toArray());
  299. dbConnect.runSqlUpdate(sqlFactory);
  300. //重新排序
  301. updateRowNo(sa_orderid);
  302. return getSucReturnObject().toString();
  303. }
  304. @API(title = "查询列表", apiversion = R.ID20221109093902.v1.class)
  305. public String selectList() throws YosException, IOException {
  306. StringBuffer where = new StringBuffer(" 1=1 ");
  307. if (content.containsKey("where")) {
  308. JSONObject whereObject = content.getJSONObject("where");
  309. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  310. where.append(" and(");
  311. where.append("t1.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  312. where.append("or t1.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  313. where.append("or t1.model like'%").append(whereObject.getString("condition")).append("%' ");
  314. where.append("or t3.spec like'%").append(whereObject.getString("condition")).append("%' ");
  315. where.append(")");
  316. }
  317. }
  318. Long sa_orderid = content.getLong("sa_orderid");
  319. Rows orderRows = getOrderRows(this, sa_orderid);
  320. if (orderRows.isNotEmpty()) {
  321. sys_enterpriseid = orderRows.get(0).getLong("sys_enterpriseid");
  322. }
  323. boolean isExport = content.getBooleanValue("isExport");
  324. SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_列表", pageSize, pageNumber, pageSorting);
  325. if (isExport) {
  326. sqlFactory = new SQLFactory(this, "订单商品明细_列表");
  327. }
  328. sqlFactory.addParameter("siteid", siteid);
  329. sqlFactory.addParameter("sa_orderid", sa_orderid);
  330. sqlFactory.addParameter_SQL("where", where);
  331. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  332. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  333. RowsMap rowsMap =rows.toRowsMap("itemno");
  334. SQLFactory sqlFactory1 =new SQLFactory(this,"商品发货数量汇总");
  335. sqlFactory1.addParameter("siteid", siteid);
  336. Rows sumQtyRows = dbConnect.runSqlQuery(sqlFactory1);
  337. RowsMap sumQtyRowsMap= sumQtyRows.toRowsMap("itemno");
  338. ERPDocking erpDocking =new ERPDocking();
  339. JSONArray jsonArray =new JSONArray();
  340. if(rows.toJsonArray("itemno").size()!=0){
  341. if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true")) {
  342. jsonArray=erpDocking.getErpIcinvbalRows(200000, 1,rows.toJsonArray("itemno"));
  343. }
  344. }
  345. if(!jsonArray.isEmpty()){
  346. for (Object object:jsonArray) {
  347. JSONObject jsonObject =(JSONObject)object;
  348. if(rowsMap.containsKey(jsonObject.getString("fitemno"))){
  349. if(rowsMap.get(jsonObject.getString("fitemno")).isNotEmpty()){
  350. rowsMap.get(jsonObject.getString("fitemno")).get(0).put("invbalqty", jsonObject.getBigDecimalValue("fqty"));
  351. }
  352. }
  353. }
  354. }
  355. //查询附件
  356. RowsMap attinfoRowsMap = getAttachmentUrl("plm_item", ids);
  357. for (Row row : rows) {
  358. if(sumQtyRowsMap.get(row.getString("itemno")).isNotEmpty()){
  359. row.put("unsoldqty", sumQtyRowsMap.get(row.getString("itemno")).get(0).getBigDecimal("qty"));
  360. }else {
  361. row.put("unsoldqty", BigDecimal.ZERO);
  362. }
  363. if(!row.containsKey("invbalqty")){
  364. row.put("invbalqty",BigDecimal.ZERO);
  365. }
  366. row.put("attinfos", attinfoRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  367. row.put("contractprice", 0);
  368. }
  369. if (isExport) {
  370. //去除不需要导出项
  371. String[] removeFieldList = {"sa_orderitemsid", "itemid", "sa_orderid", "candispatchqty", "cansaleqty",
  372. "totalaty", "orderminqty_auxunit", "orderminqty", "orderaddqty_auxunit", "orderaddqty",
  373. "spec", "delivery", "erpitemno", "erpitemname", "material", "standards", "stockstatus"};
  374. for (String key : removeFieldList) {
  375. rows.getFieldList().remove(key);
  376. }
  377. Rows uploadRows = uploadExcelToObs("order", "订单商品明细_列表", rows, getTitleMap());
  378. return getSucReturnObject().setData(uploadRows).toString();
  379. }
  380. return getSucReturnObject().setData(rows).toString();
  381. }
  382. //返回导出的标题
  383. public HashMap<String, String> getTitleMap() {
  384. HashMap<String, String> titleMap = new HashMap<>();
  385. titleMap.put("rowno", "行号");
  386. titleMap.put("batchcontrol", "是否批次管理");
  387. titleMap.put("itemno", "产品编号");
  388. titleMap.put("itemname", "产品名称");
  389. titleMap.put("model", "型号");
  390. titleMap.put("conversionrate", "换算率(辅助数量=主单位数量/换算率)");
  391. titleMap.put("price", "单价,折后价(元)");
  392. titleMap.put("amount", "金额,折后金额(元)");
  393. titleMap.put("marketprice", "牌价、市场价(元)");
  394. titleMap.put("defaultprice", "折前价(元)");
  395. titleMap.put("defaultamount", "折前金额(元)");
  396. titleMap.put("qty", "订购数量");
  397. titleMap.put("auxqty", "辅助单位数量");
  398. titleMap.put("needdate", "需求日期");
  399. titleMap.put("deliedqty", "已交货量(主单位)");
  400. titleMap.put("rebateamount", "返利分摊金额");
  401. titleMap.put("undeliqty", "未交货量(主单位)");
  402. titleMap.put("invoiceamount", "已开票金额");
  403. titleMap.put("writeoffamount", "已核销金额");
  404. titleMap.put("unit", "计量单位名称");
  405. titleMap.put("auxunit", "辅助计量单位名称");
  406. titleMap.put("deliverydate", "交货日期");
  407. titleMap.put("returnqty", "退货数量");
  408. titleMap.put("remarks", "备注");
  409. return titleMap;
  410. }
  411. @API(title = "查询可添加商品列表", apiversion = R.ID20221109153502.v1.class)
  412. @CACHEING
  413. public String selectItemList() throws YosException {
  414. StringBuffer where = new StringBuffer(" 1=1 ");
  415. if (content.containsKey("where")) {
  416. JSONObject whereObject = content.getJSONObject("where");
  417. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  418. where.append(" and(");
  419. where.append("t1.itemno like'%").append(whereObject.getString("condition")).append("%' ");
  420. where.append("or t1.itemname like'%").append(whereObject.getString("condition")).append("%' ");
  421. where.append("or t1.model like'%").append(whereObject.getString("condition")).append("%' ");
  422. where.append("or t1.spec like'%").append(whereObject.getString("condition")).append("%' ");
  423. where.append("or t3.material like'%").append(whereObject.getString("condition")).append("%' ");
  424. where.append("or t3.erpitemno like'%").append(whereObject.getString("condition")).append("%' ");
  425. where.append(")");
  426. }
  427. }
  428. Long sa_orderid = content.getLong("sa_orderid");
  429. Rows orderRows = getOrderRows(this, sa_orderid);
  430. Long sa_brandid = 0L;
  431. String tradefield = "";
  432. String type = "";
  433. Long sys_enterpriseid = 0L;
  434. if (orderRows.isNotEmpty()) {
  435. sa_brandid = orderRows.get(0).getLong("sa_brandid");
  436. tradefield = orderRows.get(0).getString("tradefield");
  437. type = orderRows.get(0).getString("type");
  438. sys_enterpriseid = orderRows.get(0).getLong("sys_enterpriseid");
  439. }
  440. SQLFactory sqlFactory = new SQLFactory(this, "查询可添加商品列表", pageSize, pageNumber, pageSorting);
  441. switch (type) {
  442. // case "工具订单":
  443. // sqlFactory = new SQLFactory(this, "查询可添加商品列表_工具订单", pageSize, pageNumber, pageSorting);
  444. // break;
  445. case "特殊订单":
  446. sqlFactory = new SQLFactory(this, "查询可添加商品列表_特殊订单", pageSize, pageNumber, pageSorting);
  447. sqlFactory.addParameter("tradefield", tradefield);
  448. break;
  449. default:
  450. StringBuffer where2 = new StringBuffer(" 1=1 ");
  451. Row enterpriseRow = Enterprise.getEnterprise(this, sys_enterpriseid);
  452. boolean saleclassauth = enterpriseRow.getBoolean("saleclassauth");
  453. //需要经营授权
  454. if (saleclassauth) {
  455. where2.append(" and t1.itemclassid in (SELECT itemclassid from sys_enterprise_saleclass WHERE sys_enterpriseid = " + sys_enterpriseid + " and siteid = '" + siteid + "')");
  456. }
  457. sqlFactory.addParameter("sa_brandid", sa_brandid);
  458. sqlFactory.addParameter("tradefield", tradefield);
  459. sqlFactory.addParameter_SQL("where2", where2);
  460. break;
  461. }
  462. sqlFactory.addParameter("siteid", siteid);
  463. sqlFactory.addParameter_SQL("where", where);
  464. String sql = sqlFactory.getSQL(false);
  465. Rows rows = dbConnect.runSqlQuery(sql);
  466. ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
  467. //查询附件
  468. RowsMap attinfoRowsMap = getAttachmentUrl("plm_item", ids);
  469. //商品领域
  470. RowsMap tradefieldRowsMap = beans.Item.Item.getTradefieldRowsMap(this, ids);
  471. //品牌
  472. RowsMap brandRowsMap = Brand.getBrandRowsMap(this, ids);
  473. //价格
  474. HashMap<Long, ItemPrice> itemPriceRowsMap = ItemPrice.getItemPrice(this, sys_enterpriseid, ids);
  475. for (Row row : rows) {
  476. row.put("attinfos", attinfoRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  477. row.put("tradefield", tradefieldRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  478. row.put("brand", brandRowsMap.getOrDefault(row.getString("itemid"), new Rows()));
  479. if( itemPriceRowsMap.containsKey(row.getLong("itemid"))){
  480. row.put("price", itemPriceRowsMap.get(row.getLong("itemid")).getPrice());
  481. }else {
  482. row.put("price", 0);
  483. }
  484. }
  485. return getSucReturnObject().setData(rows).toString();
  486. }
  487. //获取非工具数据
  488. public String getOrderItemsList(Long sa_orderid, String tradefield, Long sys_enterpriseid, Long sa_brandid) throws YosException {
  489. StringBuffer where = new StringBuffer(" 1=1 ");
  490. Row enterpriseRow = Enterprise.getEnterprise(this, sys_enterpriseid);
  491. boolean saleclassauth = enterpriseRow.getBoolean("saleclassauth");
  492. //需要经营授权
  493. if (saleclassauth) {
  494. where.append(" and itemclassid in (SELECT itemclassid from sys_enterprise_saleclass WHERE sys_enterpriseid = " + sys_enterpriseid + " and siteid = '" + siteid + "')");
  495. }
  496. SQLFactory sqlFactory = new SQLFactory(this, "商品范围");
  497. sqlFactory.addParameter("siteid", siteid);
  498. sqlFactory.addParameter("sa_orderid", sa_orderid);
  499. sqlFactory.addParameter_in("tradefield", tradefield);
  500. sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  501. sqlFactory.addParameter("sa_brandid", sa_brandid);
  502. sqlFactory.addParameter_SQL("where", where);
  503. return sqlFactory.getSQL(false);
  504. }
  505. // //获取特殊数据
  506. // public String getOrderItemsListSpec(Long sa_orderid, String tradefield, Long sys_enterpriseid) throws YosException {
  507. // SQLFactory sqlFactory = new SQLFactory(this, "商品范围_特殊");
  508. // sqlFactory.addParameter("siteid", siteid);
  509. // sqlFactory.addParameter("sa_orderid", sa_orderid);
  510. // sqlFactory.addParameter_in("tradefield", tradefield);
  511. // sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  512. // return sqlFactory.getSQL(false);
  513. // }
  514. @API(title = "订单商品变更", apiversion = R.ID20221110145302.v1.class)
  515. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class})
  516. public String changeItem() throws YosException {
  517. boolean isjiean = content.getBooleanValue("isjiean");
  518. Long sa_orderid = content.getLong("sa_orderid");
  519. String type = content.getString("type");
  520. Long sa_orderitems_changeid = createTableID("sa_orderitems_change");
  521. JSONArray itemifnos = content.getJSONArray("itemifnos");
  522. BigDecimal oldValue = new BigDecimal(0);
  523. Rows Rowstatus = dbConnect.runSqlQuery(
  524. "select * from sa_order where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
  525. if (Rowstatus.isEmpty()) {
  526. return "订单不存在";
  527. } else {
  528. if (!Rowstatus.get(0).getString("status").equals("审核")) {
  529. return getErrReturnObject().setErrMsg("非审核状态的订单无法进行商品变更").toString();
  530. }
  531. }
  532. ArrayList<String> sqlList = new ArrayList<>();
  533. // 订单商品变更
  534. SQLFactory sqlFactory = new SQLFactory(this, "订单商品变更_新增");
  535. sqlFactory.addParameter("siteid", siteid);
  536. sqlFactory.addParameter("userid", userid);
  537. sqlFactory.addParameter("username", username);
  538. sqlFactory.addParameter("sa_orderitems_changeid", sa_orderitems_changeid);
  539. sqlFactory.addParameter("sa_orderid", sa_orderid);
  540. sqlFactory.addParameter("remarks", content.getStringValue("remarks"));
  541. sqlList.add(sqlFactory.getSQL());
  542. HashMap<Long, Row> orderdetailmap = new HashMap<>();
  543. HashMap<Long, BigDecimal> orderdetailchangeamount = new HashMap<>();
  544. Rows rowsOrder = dbConnect.runSqlQuery("select * from sa_order t1 where t1.sa_orderid=" + sa_orderid);
  545. BigDecimal ce = new BigDecimal(0);
  546. for (Object obj : itemifnos) {
  547. JSONObject jsonObject = (JSONObject) obj;
  548. Rows rows = dbConnect.runSqlQuery(
  549. "SELECT sa_orderitemsid,qty,deliedqty,undeliqty,writeoffamount,price from sa_orderitems WHERE sa_orderitemsid="
  550. + jsonObject.getLongValue("sa_orderitemsid"));
  551. if (rows.isNotEmpty()) {
  552. oldValue = rows.get(0).getBigDecimal("qty");
  553. } else {
  554. return getErrReturnObject().setErrMsg("订单行不存在").toString();
  555. }
  556. if (type.equals("数量")) {
  557. // 更新数量
  558. BigDecimal newqty = jsonObject.getBigDecimal("newvalue");
  559. BigDecimal oldqty = rows.get(0).getBigDecimal("qty");
  560. BigDecimal oldundeliqty = rows.get(0).getBigDecimal("undeliqty");
  561. BigDecimal addqty = newqty.subtract(oldqty);// 新增的数量
  562. if (newqty.compareTo(oldqty) >= 0) {
  563. return getErrReturnObject().setErrMsg("修改数量必须小于原订单数量" + oldqty).toString();
  564. }
  565. if (newqty.compareTo(oldqty.subtract(oldundeliqty)) < 0) {
  566. return getErrReturnObject().setErrMsg("修改数量必须大于等于已发货数量" + oldqty.subtract(oldundeliqty)).toString();
  567. }
  568. if (newqty.compareTo(BigDecimal.ZERO) < 0) {
  569. return getErrReturnObject().setErrMsg("修改数量不可小于0").toString();
  570. }
  571. sqlFactory = new SQLFactory(this, "订单商品明细变更_更新");
  572. sqlFactory.addParameter("siteid", siteid);
  573. sqlFactory.addParameter("sa_orderitemsid", jsonObject.getLongValue("sa_orderitemsid"));
  574. sqlFactory.addParameter("qty", jsonObject.getStringValue("newvalue"));
  575. // sqlFactory.addParameter("undeliqty", oldundeliqty.add(addqty));
  576. ce = ce.add(addqty.multiply(rows.get(0).getBigDecimal("price")));
  577. sqlList.add(sqlFactory.getSQL());
  578. /**
  579. * * 核销对冲计算
  580. */
  581. {
  582. /**
  583. * * 核销对冲计算
  584. */
  585. BigDecimal writeoffamount = rows.get(0).getBigDecimal("writeoffamount");// 订单行已核销金额
  586. //System.out.println(newqty.multiply(rows.get(0).getBigDecimal("price")));
  587. //System.out.println(writeoffamount);
  588. if (writeoffamount.compareTo(BigDecimal.ZERO) > 0
  589. && (newqty.multiply(rows.get(0).getBigDecimal("price"))).compareTo(writeoffamount) < 0) {
  590. // 如果已核销金额大于0 且调整后折后金额小于已核销金额,则需要将差价进行回冲
  591. orderdetailmap.put(rows.get(0).getLong("sa_orderitemsid"), rows.get(0));
  592. //System.out.println(writeoffamount.subtract(newqty.multiply(rows.get(0).getBigDecimal("price"))));
  593. //System.out.println("sa_orderitemsid:"+rows.get(0).getLong("sa_orderitemsid"));
  594. orderdetailchangeamount.put(rows.get(0).getLong("sa_orderitemsid"),
  595. writeoffamount.subtract(newqty.multiply(rows.get(0).getBigDecimal("price"))));
  596. }
  597. }
  598. }
  599. // 订单商品变更明细
  600. sqlFactory = new SQLFactory(this, "订单商品变更明细_新增");
  601. sqlFactory.addParameter("siteid", siteid);
  602. sqlFactory.addParameter("sa_orderitems_changemxid", createTableID("sa_orderitems_changemx"));
  603. sqlFactory.addParameter("itemid", jsonObject.getLongValue("itemid"));
  604. sqlFactory.addParameter("sa_orderitemsid", jsonObject.getLongValue("sa_orderitemsid"));
  605. sqlFactory.addParameter("sa_orderitems_changeid", sa_orderitems_changeid);
  606. sqlFactory.addParameter("type", type);
  607. sqlFactory.addParameter("oldvalue", oldValue);
  608. sqlFactory.addParameter("newvalue", jsonObject.getStringValue("newvalue"));
  609. sqlList.add(sqlFactory.getSQL());
  610. // content.put("sa_orderitemsid", sa_orderitemsid);
  611. }
  612. if (ce.compareTo(BigDecimal.ZERO) != 0) {
  613. CashbillEntity cashbillEntity = new CashbillEntity();
  614. cashbillEntity.setAmount(ce);
  615. cashbillEntity.setOwnerid(sa_orderid);
  616. cashbillEntity.setOwnertable("sa_order");
  617. cashbillEntity
  618. .setRemarks("订单变更:" + rowsOrder.get(0).getString("sonum") + "订单变更单id:" + sa_orderitems_changeid);
  619. sqlList.addAll(Accountbalance.createCashbillPay(this, rowsOrder.get(0).getLong("sys_enterpriseid"),
  620. rowsOrder.get(0).getLong("sa_accountclassid"), cashbillEntity, true));
  621. }
  622. /**
  623. * * 核销对冲
  624. */
  625. if (!orderdetailmap.isEmpty()) {
  626. //System.out.println("测试");
  627. sqlList.addAll(
  628. new twriteoffbill_orderchange(rowsOrder.get(0), orderdetailmap, orderdetailchangeamount).hedging());
  629. }
  630. for (String string : sqlList) {
  631. //System.out.println(string);
  632. }
  633. dbConnect.runSqlUpdate(sqlList);
  634. // 居间费设置
  635. updateRebateFee(sa_orderid);
  636. if (!isjiean) {
  637. if (Parameter.get("system.erp_dockswitch").equalsIgnoreCase("true")) {
  638. UploadDataToERP uploadDataToERP = new UploadDataToERP(this, sa_orderitems_changeid, "订单变更");
  639. boolean issuccess = uploadDataToERP.upload();
  640. if (!issuccess) {
  641. return getErrReturnObject().setErrMsg("上传ERP失败,请检查!").toString();
  642. }
  643. }
  644. }
  645. return getSucReturnObject().toString();
  646. }
  647. @API(title = "订单商品变更(手动推erp)", apiversion = R.ID20230306161703.v1.class)
  648. @CACHEING_CLEAN(apiClass = {Order.class, OrderItems.class, restcontroller.sale.order.Order.class})
  649. public String changeItem1() throws YosException {
  650. if (Parameter.get("system.erp_dockswitch").equalsIgnoreCase("true")) {
  651. UploadDataToERP uploadDataToERP = new UploadDataToERP(this, content.getLong("sa_orderitems_changeid"), "订单变更");
  652. boolean issuccess = uploadDataToERP.upload();
  653. if (!issuccess) {
  654. return getErrReturnObject().setErrMsg("上传ERP失败,请检查!").toString();
  655. } else {
  656. return getSucReturnObject().toString();
  657. }
  658. } else {
  659. return getErrReturnObject().setErrMsg("erp对接开关未开!").toString();
  660. }
  661. }
  662. /**
  663. * 获取当前订单的最大行号
  664. *
  665. * @param sa_orderid
  666. * @return
  667. * @throws YosException
  668. */
  669. public Long getRowNo(Long sa_orderid) throws YosException {
  670. Rows rows = dbConnect.runSqlQuery("SELECT COUNT(0) count from sa_orderitems WHERE sa_orderid = " + sa_orderid + " AND siteid = '" + siteid + "'");
  671. return rows.get(0).getLong("count") + 1;
  672. }
  673. /**
  674. * 查询商品信息
  675. *
  676. * @param itemid
  677. * @return
  678. * @throws YosException
  679. */
  680. public Row getItemRow(Long itemid) throws YosException {
  681. SQLFactory sqlFactory = new SQLFactory(this, "查询商品信息");
  682. sqlFactory.addParameter("siteid", siteid);
  683. sqlFactory.addParameter("itemid", itemid);
  684. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL(false));
  685. return rows.isNotEmpty() ? rows.get(0) : new Row();
  686. }
  687. /**
  688. * 重新对商品行排序
  689. *
  690. * @param sa_orderid
  691. * @throws YosException
  692. */
  693. public void updateRowNo(Long sa_orderid) throws YosException {
  694. String sql = "SELECT sa_orderitemsid from sa_orderitems WHERE sa_orderid= " + sa_orderid + " and siteid = '" + siteid + "' ORDER BY createdate ";
  695. ArrayList<Long> sa_orderitemsids = dbConnect.runSqlQuery(sql).toArrayList("sa_orderitemsid", new ArrayList<>());
  696. if (sa_orderitemsids.size() > 0) {
  697. int rowno = 1;
  698. StringBuffer where = new StringBuffer("");
  699. for (Long id : sa_orderitemsids) {
  700. where.append(" WHEN " + id + " THEN " + rowno);
  701. rowno++;
  702. }
  703. SQLFactory sqlFactory = new SQLFactory(this, "更新行号");
  704. sqlFactory.addParameter("siteid", siteid);
  705. sqlFactory.addParameter_SQL("where", where);
  706. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsids);
  707. sql = sqlFactory.getSQL(false);
  708. dbConnect.runSqlUpdate(sql);
  709. }
  710. }
  711. }