OrderItemsHelper.java 19 KB

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