OrderItemsHelper.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. package restcontroller.webmanage.sale.order;
  2. import beans.datacontrllog.DataContrlLog;
  3. import beans.itemprice.ItemPrice;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import common.BaseClass;
  7. import common.Controller;
  8. import common.DataFunction;
  9. import common.YosException;
  10. import common.data.*;
  11. import java.math.BigDecimal;
  12. import java.util.ArrayList;
  13. public class OrderItemsHelper extends BaseClass {
  14. Controller controller;
  15. JSONObject content;
  16. public OrderItemsHelper(Controller controller) {
  17. this.controller = controller;
  18. this.content = controller.content;
  19. }
  20. //执行是否自动返利结算
  21. public void executeAutoRebate(Long sa_orderid, Long sys_enterpriseid) throws YosException {
  22. Row enterpriseRow = dbConnect.runSqlQuery(0, "");
  23. }
  24. //查询并执行商品费用方案方法
  25. public void executeDataFunction(Long sa_orderid) throws YosException {
  26. SQLFactory sqlFactory = new SQLFactory(this, "查询商品费用方案方法");
  27. sqlFactory.addParameter("siteid", controller.siteid);
  28. sqlFactory.addParameter("sa_orderid", sa_orderid);
  29. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  30. for (Row row : rows) {
  31. DataFunction df = DataFunction.get(controller, row.getString("functionname"));
  32. df.addParameter("sa_orderid", sa_orderid);
  33. df.action();
  34. }
  35. }
  36. public void checkOffOrderItems(Long sa_orderid, String tablename) throws YosException {
  37. Rows iteminfos = dbConnect.runSqlQuery("select t2.itemid from sa_orderitems t1 inner join " + tablename + " t2 on t1.siteid=t2.siteid and t1.itemid=t2.itemid where t1.sa_orderid="
  38. + sa_orderid + " and t1.siteid='" + controller.siteid + "' and t2.isonsale!=1");
  39. if (iteminfos.isNotEmpty()) {
  40. Row itemRow = dbConnect.runSqlQuery(0, "SELECT itemno from plm_item WHERE itemid=" + iteminfos.get(0).getString("itemid"));
  41. new YosException(false, "检测到品号" + itemRow.getString("itemno") + "未上架,请删除后重试");
  42. }
  43. }
  44. /**
  45. * 是否符合商品起订量和增量判断
  46. *
  47. * @param type
  48. * @param itemRowsMap
  49. * @param item
  50. * @param itemRow
  51. * @param promotionitems
  52. * @throws YosException
  53. */
  54. public void compareQty(String type, RowsMap itemRowsMap, JSONObject item, Row itemRow, Rows promotionitems) throws YosException {
  55. BigDecimal qty = item.getBigDecimalValue("qty");
  56. String itemid = item.getString("itemid");
  57. if (!type.equals("促销订单")) {
  58. if (itemRowsMap.containsKey(itemid)) {
  59. if (itemRowsMap.get(itemid).isNotEmpty()) {
  60. BigDecimal orderaddqty = itemRowsMap.get(itemid).get(0).getBigDecimal("orderaddqty");
  61. BigDecimal orderminqty = itemRowsMap.get(itemid).get(0).getBigDecimal("orderminqty");
  62. if (orderaddqty.compareTo(BigDecimal.ZERO) != 0) {
  63. if (((qty.subtract(orderminqty)).remainder(orderaddqty)).compareTo(BigDecimal.ZERO) != 0 || qty.compareTo(orderminqty) < 0) {
  64. throw new YosException(false, "品号为" + itemRowsMap.get(itemid).get(0).getString("itemno") + "的商品数量不符合该商品的起订量和增量");
  65. }
  66. }
  67. }
  68. }
  69. }
  70. RowsMap pRowsMap = promotionitems.toRowsMap("sa_promotion_itemsid");
  71. if (type.equals("促销订单")) {
  72. if (pRowsMap.containsKey(item.getString("sa_promotion_itemsid"))) {
  73. if (pRowsMap.get(item.getString("sa_promotion_itemsid")).isNotEmpty()) {
  74. BigDecimal saledqty = pRowsMap.get(item.getString("sa_promotion_itemsid")).get(0).getBigDecimal("saledqty");
  75. BigDecimal groupqty = pRowsMap.get(item.getString("sa_promotion_itemsid")).get(0).getBigDecimal("groupqty");
  76. BigDecimal orderaddqty = pRowsMap.get(item.getString("sa_promotion_itemsid")).get(0).getBigDecimal("orderaddqty");
  77. BigDecimal orderminqty = pRowsMap.get(item.getString("sa_promotion_itemsid")).get(0).getBigDecimal("orderminqty");
  78. boolean islimit = pRowsMap.get(item.getString("sa_promotion_itemsid")).get(0).getBoolean("islimit");
  79. if (orderaddqty.compareTo(BigDecimal.ZERO) != 0) {
  80. if (((qty.subtract(orderminqty)).remainder(orderaddqty)).compareTo(BigDecimal.ZERO) != 0 || qty.compareTo(orderminqty) < 0) {
  81. throw new YosException(false, "品号为" + itemRowsMap.get(itemid).get(0).getString("itemno") + "的商品数量不符合该商品的起订量和增量");
  82. }
  83. }
  84. if (islimit) {
  85. if (groupqty.compareTo(saledqty.add(qty)) < 0) {
  86. throw new YosException(false, "品号:" + itemRow.getString("itemno") + "已超过限购数量:" + groupqty.subtract(saledqty));
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. /**
  94. * 获取定制属性
  95. *
  96. * @param itemRowsMap
  97. * @param item
  98. * @return
  99. * @throws YosException
  100. */
  101. public JSONArray getCustomProperties(RowsMap itemRowsMap, JSONObject item) throws YosException {
  102. JSONArray customproperties = new JSONArray();
  103. Rows customschemeRows = dbConnect.runSqlQuery("select * from sa_customscheme_items where siteid='" + controller.siteid + "'");
  104. RowsMap customschemeRowsMap = customschemeRows.toRowsMap("sa_customschemeid");
  105. String itemid = item.getString("itemid");
  106. if (itemRowsMap.containsKey(itemid)) {
  107. if (itemRowsMap.get(itemid).isNotEmpty()) {
  108. if (itemRowsMap.get(itemid).get(0).getLong("sa_customschemeid") > 0 && !itemRowsMap.get(itemid).get(0).getBoolean("iscustomsize")) {
  109. // System.out.println(0000);
  110. if (customschemeRowsMap.containsKey(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid"))) {
  111. // System.out.println(111111);
  112. for (Row customschemeItem : (customschemeRowsMap.get(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid")))) {
  113. // System.out.println(customschemeItem.getString("description"));
  114. // System.out.println(2222);
  115. customproperties.add(customschemeItem.getString("description") + ":" + itemRowsMap.get(itemid).get(0).getString(customschemeItem.getString("value")));
  116. }
  117. }
  118. } else if (itemRowsMap.get(itemid).get(0).getBoolean("iscustomsize")) {
  119. // System.out.println(3333);
  120. if (customschemeRowsMap.containsKey(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid"))) {
  121. // System.out.println(4444);
  122. for (Row customschemeItem : (customschemeRowsMap.get(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid")))) {
  123. // System.out.println(5555);
  124. // System.out.println((customschemeRowsMap.get(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid"))).size());
  125. if (!customschemeItem.getString("value").equals("spec")) {
  126. customproperties.add(customschemeItem.getString("description") + ":" + itemRowsMap.get(itemid).get(0).getString(customschemeItem.getString("value")));
  127. } else {
  128. customproperties.add("尺寸:" + item.getStringValue("length") + "*" + item.getStringValue("width"));
  129. }
  130. }
  131. } else {
  132. customproperties.add("尺寸:" + item.getStringValue("length") + "*" + item.getStringValue("width"));
  133. }
  134. }
  135. }
  136. }
  137. return customproperties;
  138. }
  139. /**
  140. * 获取定制尺寸信息
  141. *
  142. * @param itemRowsMap
  143. * @param item
  144. * @return
  145. * @throws YosException
  146. */
  147. public String getCustomSpec(RowsMap itemRowsMap, JSONObject item) throws YosException {
  148. String spec = "";
  149. Rows customschemeRows = dbConnect.runSqlQuery("select * from sa_customscheme_items where siteid='" + controller.siteid + "'");
  150. RowsMap customschemeRowsMap = customschemeRows.toRowsMap("sa_customschemeid");
  151. String itemid = item.getString("itemid");
  152. if (itemRowsMap.containsKey(itemid)) {
  153. if (itemRowsMap.get(itemid).isNotEmpty()) {
  154. if (!itemRowsMap.get(itemid).get(0).getBoolean("iscustomsize")) {
  155. spec = itemRowsMap.get(itemid).get(0).getString("spec");
  156. } else {
  157. spec = item.getStringValue("length") + "*" + item.getStringValue("width");
  158. }
  159. }
  160. }
  161. return spec;
  162. }
  163. /**
  164. * 批量查询商品信息
  165. *
  166. * @param items
  167. * @return
  168. * @throws YosException
  169. */
  170. public RowsMap getItemRowsMap(JSONArray items) throws YosException {
  171. ArrayList<Long> itemids = new ArrayList<>();
  172. for (Object obj : items) {
  173. JSONObject item = (JSONObject) obj;
  174. itemids.add(item.getLong("itemid"));
  175. }
  176. String sql = "select t1.*,t2.material from plm_item t1 left join plm_itemextend t2 ON t2.itemid = t1.itemid AND t2.siteid = t1.siteid where t1.siteid='" + controller.siteid + "' and t1.itemid in " + itemids;
  177. sql = sql.replace("[", "(").replace("]", ")");
  178. return dbConnect.runSqlQuery(sql).toRowsMap("itemid");
  179. }
  180. public UpdateSQL getUpdateSql() throws YosException {
  181. System.err.println("123");
  182. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(controller, "sa_order");
  183. if (controller.content.containsKey("sa_order_v")) {
  184. updateSQL.setDataVersion(content.getIntValue("sa_order_v"));
  185. }
  186. updateSQL.setValue("changeby", controller.username);
  187. updateSQL.setWhere("sa_orderid", content.getLongValue("sa_orderid"));
  188. updateSQL.setWhere("siteid", controller.siteid);
  189. System.err.println(updateSQL.getSQL());
  190. return updateSQL;
  191. }
  192. String sa_orderitems = "sa_orderitems";
  193. public InsertSQL getInsertSQL(JSONArray customproperties, long sa_orderitemsid, JSONObject item, Row itemRow, String type, BigDecimal defaultprice, BigDecimal price, BigDecimal saleprice, BigDecimal marketprice, String spec, Rows promotionitems) throws YosException {
  194. Long itemid = item.getLong("itemid");
  195. BigDecimal qty = item.getBigDecimalValue("qty");
  196. RowsMap promotionitemsRowsMap = promotionitems.toRowsMap("sa_promotion_itemsid");
  197. String signaturecode = "";
  198. if (type.equals("促销订单")) {
  199. if (promotionitemsRowsMap.containsKey(item.getString("sa_promotion_itemsid"))) {
  200. if (promotionitemsRowsMap.get(item.getString("sa_promotion_itemsid")).isNotEmpty()) {
  201. signaturecode = promotionitemsRowsMap.get(item.getString("sa_promotion_itemsid")).get(0).getString("signaturecode");
  202. }
  203. }
  204. }
  205. InsertSQL insertSQL = SQLFactory.createInsertSQL(controller, sa_orderitems);
  206. insertSQL.setValue("customproperties", customproperties.toJSONString().replace("[", "").replace("]", "").replace("\"", ""));
  207. insertSQL.setValue("siteid", controller.siteid);
  208. insertSQL.setValue("sa_orderitemsid", sa_orderitemsid);
  209. insertSQL.setValue("sa_orderid", content.getLongValue("sa_orderid"));
  210. insertSQL.setValue("rowno", 1);
  211. insertSQL.setValue("itemid", itemid);
  212. insertSQL.setValue("remarks", item.getStringValue("remarks") + signaturecode);
  213. insertSQL.setValue("itemno", itemRow.getString("itemno"));
  214. insertSQL.setValue("itemname", itemRow.getString("itemname"));
  215. insertSQL.setValue("model", itemRow.getString("model"));
  216. insertSQL.setValue("unit", itemRow.getString("unit"));
  217. insertSQL.setValue("auxunit", itemRow.getString("auxunit"));
  218. insertSQL.setValue("batchcontrol", itemRow.getLong("batchcontrol"));
  219. insertSQL.setValue("stockno", item.getOrDefault("stockno", "null"));
  220. insertSQL.setValue("position", item.getOrDefault("position", "null"));
  221. insertSQL.setValue("batchno", item.getOrDefault("batchno", "null"));
  222. insertSQL.setValue("spec", spec);
  223. insertSQL.setValue("sa_promotion_itemsid", item.getLongValue("sa_promotion_itemsid"));
  224. String deliverydate = item.getStringValue("deliverydate");
  225. Rows rows = dbConnect.runSqlQuery("SELECT deliverydate,defaultprice from sa_orderitems WHERE sa_orderitemsid =" + sa_orderitemsid);
  226. if (deliverydate.isEmpty()) {
  227. if (rows.isNotEmpty()) {
  228. deliverydate = rows.get(0).getString("deliverydate");
  229. } else {
  230. deliverydate = "null";
  231. }
  232. }
  233. String needdate = item.getStringValue("needdate");
  234. insertSQL.setValue("needdate", needdate.equals("") ? "null" : needdate);
  235. insertSQL.setValue("deliverydate", deliverydate.equals("") ? "null" : deliverydate);
  236. BigDecimal conversionrate = itemRow.getBigDecimal("conversionrate");
  237. if (conversionrate.compareTo(BigDecimal.ZERO) <= 0) {
  238. conversionrate = BigDecimal.valueOf(1);
  239. }
  240. insertSQL.setValue("conversionrate", conversionrate);
  241. //订购数量
  242. insertSQL.setValue("qty", qty);
  243. insertSQL.setValue("undeliqty", qty);
  244. //辅助单位数量
  245. insertSQL.setValue("auxqty", qty.divide(conversionrate));
  246. if (type.equals("特殊订单")) {
  247. if (item.containsKey("defaultprice")) {
  248. if (item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO) > 0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO) <= 0) {
  249. insertSQL.setValue("price", item.getBigDecimalValue("price"));
  250. //折前金额(元)
  251. insertSQL.setValue("amount", price.multiply(qty));
  252. } else if (item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO) <= 0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO) > 0) {
  253. insertSQL.setValue("price", item.getBigDecimalValue("amount").divide(qty, 6, BigDecimal.ROUND_HALF_UP));
  254. //折前金额(元)
  255. insertSQL.setValue("amount", item.getBigDecimalValue("amount"));
  256. } else {
  257. insertSQL.setValue("price", price);
  258. //折前金额(元)
  259. insertSQL.setValue("amount", price.multiply(qty));
  260. }
  261. } else {
  262. insertSQL.setValue("price", price);
  263. //折前金额(元)
  264. insertSQL.setValue("amount", price.multiply(qty));
  265. }
  266. } else {
  267. insertSQL.setValue("price", price);
  268. //折前金额(元)
  269. insertSQL.setValue("amount", price.multiply(qty));
  270. }
  271. //单价,折后价(元),取合同价
  272. insertSQL.setValue("defaultprice", defaultprice);
  273. insertSQL.setValue("saleprice", saleprice);
  274. //金额,折后金额(元)
  275. insertSQL.setValue("defaultamount", defaultprice.multiply(qty));
  276. //牌价、市场价(元),标准订单牌价取商品价格,项目订单取合同里的牌价
  277. insertSQL.setValue("marketprice", marketprice);
  278. return insertSQL;
  279. }
  280. public UpdateSQL getUpdateSQL(long sa_orderitemsid, JSONObject item, Row itemRow, String type, BigDecimal defaultprice, BigDecimal price, BigDecimal saleprice) throws YosException {
  281. BigDecimal qty = item.getBigDecimalValue("qty");
  282. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(controller, sa_orderitems);
  283. //订购数量
  284. updateSQL.setValue("qty", qty);
  285. //辅助单位数量
  286. BigDecimal conversionrate = itemRow.getBigDecimal("conversionrate");
  287. if (conversionrate.compareTo(BigDecimal.ZERO) <= 0) {
  288. conversionrate = BigDecimal.valueOf(1);
  289. }
  290. updateSQL.setValue("auxqty", qty.divide(conversionrate));
  291. updateSQL.setValue("undeliqty", qty.subtract(itemRow.getBigDecimal("delivery")));
  292. updateSQL.setValue("remarks", item.getStringValue("remarks"));
  293. updateSQL.setValue("stockno", item.getOrDefault("stockno", "null"));
  294. updateSQL.setValue("position", item.getOrDefault("position", "null"));
  295. updateSQL.setValue("batchno", item.getOrDefault("batchno", "null"));
  296. String deliverydate = item.getStringValue("deliverydate");
  297. Rows rows = dbConnect.runSqlQuery("SELECT deliverydate,defaultprice from sa_orderitems WHERE sa_orderitemsid =" + sa_orderitemsid);
  298. if (deliverydate.isEmpty()) {
  299. if (rows.isNotEmpty()) {
  300. deliverydate = rows.get(0).getString("deliverydate");
  301. } else {
  302. deliverydate = "null";
  303. }
  304. }
  305. String needdate = item.getStringValue("needdate");
  306. updateSQL.setValue("needdate", needdate.equals("") ? "null" : needdate);
  307. updateSQL.setValue("deliverydate", deliverydate.equals("") ? "null" : deliverydate);
  308. if (type.equals("特殊订单")) {
  309. if (item.containsKey("defaultprice")) {
  310. if (item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO) > 0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO) <= 0) {
  311. updateSQL.setValue("price", item.getBigDecimalValue("price"));
  312. //折前金额(元)
  313. updateSQL.setValue("amount", price.multiply(qty));
  314. } else if (item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO) <= 0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO) > 0) {
  315. updateSQL.setValue("price", item.getBigDecimalValue("amount").divide(qty, 6, BigDecimal.ROUND_HALF_UP));
  316. //折前金额(元)
  317. updateSQL.setValue("amount", item.getBigDecimalValue("amount"));
  318. } else {
  319. updateSQL.setValue("price", price);
  320. //折前金额(元)
  321. updateSQL.setValue("amount", price.multiply(qty));
  322. }
  323. } else {
  324. updateSQL.setValue("price", price);
  325. //折前金额(元)
  326. updateSQL.setValue("amount", price.multiply(qty));
  327. }
  328. } else {
  329. updateSQL.setValue("price", price);
  330. //折前金额(元)
  331. updateSQL.setValue("amount", price.multiply(qty));
  332. }
  333. //单价,折后价(元),取合同价
  334. updateSQL.setValue("defaultprice", defaultprice);
  335. //金额,折后金额(元)
  336. updateSQL.setValue("defaultamount", defaultprice.multiply(qty));
  337. updateSQL.setValue("saleprice", saleprice);
  338. updateSQL.setWhere("siteid", controller.siteid);
  339. updateSQL.setWhere("sa_orderitemsid", sa_orderitemsid);
  340. return updateSQL;
  341. }
  342. public BigDecimal getDefaultprice(ItemPrice itemPrice) throws YosException {
  343. return itemPrice.getGraderateprice();
  344. }
  345. public BigDecimal getPrice(Row orderRow, ItemPrice itemPrice, JSONObject item, RowsMap itemRowsMap) throws YosException {
  346. String type = orderRow.getString("type");
  347. Long sa_promotionid = orderRow.getLong("sa_promotionid");
  348. BigDecimal price;
  349. switch (type) {
  350. case "促销订单":
  351. price = itemPrice.getPromotionPrice(sa_promotionid);
  352. if (itemRowsMap.containsKey(item.getString("itemid"))) {
  353. if (itemRowsMap.get(item.getString("itemid")).isNotEmpty()) {
  354. if (itemRowsMap.get(item.getString("itemid")).get(0).getBoolean("iscustomsize") && itemRowsMap.get(item.getString("itemid")).get(0).getLong("pricingmetod") == 1) {
  355. price = price.multiply(item.getBigDecimalValue("length").multiply(item.getBigDecimalValue("width")).divide(new BigDecimal("1000000"), 4, BigDecimal.ROUND_HALF_UP));
  356. }
  357. }
  358. }
  359. break;
  360. default:
  361. price = itemPrice.getGraderateprice();
  362. if (itemRowsMap.containsKey(item.getString("itemid"))) {
  363. if (itemRowsMap.get(item.getString("itemid")).isNotEmpty()) {
  364. if (itemRowsMap.get(item.getString("itemid")).get(0).getBoolean("iscustomsize") && itemRowsMap.get(item.getString("itemid")).get(0).getLong("pricingmetod") == 1) {
  365. price = price.multiply(item.getBigDecimalValue("length").multiply(item.getBigDecimalValue("width")).divide(new BigDecimal("1000000"), 4, BigDecimal.ROUND_HALF_UP));
  366. }
  367. }
  368. }
  369. }
  370. return price;
  371. }
  372. public BigDecimal getSalePrice(Row orderRow, ItemPrice itemPrice) throws YosException {
  373. String type = orderRow.getString("type");
  374. Long sa_promotionid = orderRow.getLong("sa_promotionid");
  375. BigDecimal price;
  376. switch (type) {
  377. case "促销订单":
  378. price = itemPrice.getPromotionPrice(sa_promotionid);
  379. break;
  380. default:
  381. price = itemPrice.getGraderateprice();
  382. }
  383. return price;
  384. }
  385. }