OrderItemsHelper.java 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. package restcontroller.webmanage.sale.order;
  2. import beans.accountbalance.Accountbalance;
  3. import beans.accountbalance.CashbillEntity;
  4. import beans.itemclass.ItemClass;
  5. import beans.itemprice.ItemPrice;
  6. import beans.order.Order;
  7. import beans.parameter.Parameter;
  8. import com.alibaba.fastjson2.JSONArray;
  9. import com.alibaba.fastjson2.JSONObject;
  10. import common.BaseClass;
  11. import common.Controller;
  12. import common.DataFunction;
  13. import common.YosException;
  14. import common.data.*;
  15. import org.apache.commons.lang.StringUtils;
  16. import java.math.BigDecimal;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. public class OrderItemsHelper extends BaseClass {
  20. Controller controller;
  21. JSONObject content;
  22. public OrderItemsHelper(Controller controller) {
  23. this.controller = controller;
  24. this.content = controller.content;
  25. }
  26. public void isAccountCanUsed(Long sa_accountclassid) throws YosException {
  27. if (dbConnect.runSqlQuery("select * from sa_accountclass WHERE isused=1 and isorder=1 and sa_accountclassid=" + sa_accountclassid).isEmpty()) {
  28. throw new YosException(false, "当资金账户未启用或者不支持订货");
  29. }
  30. }
  31. public long getAccountClassId(String type) throws YosException {
  32. Long sa_accountclassid = content.getLong("sa_accountclassid");
  33. if (controller.siteid.equalsIgnoreCase("CCYOSG")) {
  34. if ((type.equals("标准订单") || type.equals("特殊订单")) && content.getString("tradefield").equals("GC工程")) {
  35. return 69L;
  36. }
  37. if ((type.equals("标准订单") || type.equals("特殊订单")) && content.getString("tradefield").equals("整装工程")) {
  38. return 96L;
  39. }
  40. } else {
  41. if ((type.equals("标准订单") || type.equals("特殊订单")) && sa_accountclassid > 0) {
  42. return sa_accountclassid;
  43. }
  44. }
  45. if (type.equals("促销订单")) {
  46. Rows accountclassRows = dbConnect.runSqlQuery("select sa_accountclassid from sa_promotion where siteid='" + controller.siteid + "' and sa_promotionid=" + content.getLongValue("sa_promotionid"));
  47. if (!accountclassRows.isEmpty()) {
  48. return accountclassRows.get(0).getLong("sa_accountclassid");
  49. } else {
  50. return beans.order.Order.getDefaultAccount(controller.siteid, type);
  51. }
  52. }
  53. return beans.order.Order.getDefaultAccount(controller.siteid, type);
  54. }
  55. //执行是否自动返利结算
  56. public void executeAutoRebate(Long sa_orderid, Long sys_enterpriseid) throws YosException {
  57. Rows orderRows = Order.getOrderRows(controller, sa_orderid);
  58. if (orderRows.get(0).getString("type").equals("促销订单")) {
  59. return;
  60. }
  61. //是否自动返利结算
  62. String isrebate = Parameter.getString(controller.siteid, "isrebate");
  63. if (!isrebate.equals("1")) {
  64. return;
  65. }
  66. //查询有效期内,余额大于0 的经销商装修补贴返利
  67. String sql = "select * from sa_agentdecorationtrebate WHERE sys_enterpriseid=" + sys_enterpriseid + " and siteid='" + controller.siteid + "' and begindate <= current_date and enddate>=current_date and balance>0";
  68. Rows agentdecorationtrebatRows = dbConnect.runSqlQuery(sql);
  69. if (agentdecorationtrebatRows.isEmpty()) {
  70. return;
  71. }
  72. //查询装修返利方案
  73. Rows rebateschemeRows = dbConnect.runSqlQuery("select * from sa_rebatescheme where siteid='" + controller.siteid + "'");
  74. if (rebateschemeRows.isEmpty()) {
  75. return;
  76. }
  77. ArrayList<String> sqlList = new ArrayList<>();
  78. Long sa_rebateschemeid = rebateschemeRows.get(0).getLong("sa_rebateschemeid");
  79. Long sa_accountclassid = rebateschemeRows.get(0).getLong("sa_accountclassid");
  80. Rows rebatecategorys = dbConnect.runSqlQuery("select itemclassid from sa_rebatecategory where siteid='" + controller.siteid + "' and sa_rebateschemeid=" + sa_rebateschemeid);
  81. Rows rebateunproducts = dbConnect.runSqlQuery("select itemid from sa_rebateunproducts where siteid='" + controller.siteid + "' and sa_rebateschemeid=" + sa_rebateschemeid);
  82. Rows rebateunjoinproducts = dbConnect.runSqlQuery("select * from sa_rebateunjoinproducts where siteid='" + controller.siteid + "' and sa_rebateschemeid=" + sa_rebateschemeid);
  83. ArrayList<Long> itemclsids = rebatecategorys.toArrayList("itemclassid", new ArrayList<Long>());
  84. itemclsids.addAll(ItemClass.getSubItemClassIds(controller, itemclsids));
  85. itemclsids.add(0l);
  86. ArrayList<Long> itemids = rebateunproducts.toArrayList("itemid", new ArrayList<Long>());
  87. itemids.add(0l);
  88. ArrayList<Long> joinitemids = rebateunjoinproducts.toArrayList("itemid", new ArrayList<Long>());
  89. String where =" 1=1 ";
  90. if(!joinitemids.isEmpty()){
  91. where=where+" and t1.itemid in"+joinitemids;
  92. }
  93. SQLFactory sqlFactory = new SQLFactory(controller, "查询返利数据");
  94. sqlFactory.addParameter("sa_orderid", sa_orderid);
  95. sqlFactory.addParameter("siteid", controller.siteid);
  96. sqlFactory.addParameter("siteid", controller.siteid);
  97. sqlFactory.addParameter_in("itemclassid", itemclsids);
  98. sqlFactory.addParameter_in("itemid", itemids);
  99. sqlFactory.addParameter_SQL("where", where);
  100. Rows rows = controller.dbConnect.runSqlQuery(sqlFactory);
  101. BigDecimal saorderamount = BigDecimal.ZERO;
  102. for (Row row : rows) {
  103. saorderamount = saorderamount.add(row.getBigDecimal("amount"));
  104. }
  105. //查询定制费用
  106. sql = "SELECT ifnull(sum(costamount),0) costamount from sa_order_cost WHERE sa_orderid=" + sa_orderid;
  107. BigDecimal costamount = dbConnect.runSqlQuery(0, sql).getBigDecimal("costamount");
  108. saorderamount = saorderamount.add(costamount);
  109. BigDecimal balance = agentdecorationtrebatRows.get(0).getBigDecimal("balance");
  110. BigDecimal brate = agentdecorationtrebatRows.get(0).getBigDecimal("brate");
  111. BigDecimal approvedamount = BigDecimal.ZERO;
  112. if (balance.compareTo(saorderamount.multiply(brate)) >= 0) {
  113. approvedamount = saorderamount.multiply(brate);
  114. } else if (balance.compareTo(BigDecimal.ZERO) > 0) {
  115. approvedamount = balance;
  116. }
  117. // System.out.println(saorderamount);
  118. // System.out.println(approvedamount);
  119. if (approvedamount.compareTo(BigDecimal.ZERO) > 0) {
  120. ArrayList<Long> sa_orderitemsids = rows.toArrayList("sa_orderitemsid", new ArrayList<>());
  121. sa_orderitemsids.add(0l);
  122. //更新订单明细的装修返利标志
  123. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(controller, "sa_orderitems");
  124. updateSQL.setWhere("sa_orderitemsid", sa_orderitemsids);
  125. updateSQL.setValue("decorationrebateflag", "1");
  126. sqlList.add(updateSQL.getSQL());
  127. //更新sa_agentdecorationtrebate
  128. updateSQL = SQLFactory.createUpdateSQL(controller, "sa_agentdecorationtrebate");
  129. updateSQL.setWhere("sys_enterpriseid", sys_enterpriseid);
  130. updateSQL.setSiteid(controller.siteid);
  131. updateSQL.setValue("balance", balance.subtract(approvedamount));
  132. sqlList.add(updateSQL.getSQL());
  133. //生成收入凭证
  134. CashbillEntity entity = new CashbillEntity();
  135. entity.setAmount(approvedamount);
  136. entity.setDiscountamountamount(BigDecimal.ZERO);
  137. entity.setOwnerid(sa_orderid);
  138. entity.setOwnertable("sa_order");
  139. entity.setSource("订单自动返利");
  140. entity.setSourcenote(orderRows.get(0).getString("sonum"));
  141. entity.setRemarks(orderRows.get(0).getString("sonum") + "订单自动返利");
  142. entity.setType("公司支持");
  143. entity.setTypemx("装修返利");
  144. JSONObject cashbillIncome = Accountbalance.createCashbillIncome(controller, sys_enterpriseid, sa_accountclassid, entity, true, true);
  145. sqlList.addAll(cashbillIncome.getJSONArray("sqlList").toJavaList(String.class));
  146. dbConnect.runSqlUpdate(sqlList);
  147. }
  148. }
  149. public JSONArray toJsonArray(String column, List<Row> list) {
  150. JSONArray array = new JSONArray();
  151. array.add("0");
  152. for (Row row : list) {
  153. Object value = row.get(column);
  154. array.add(value);
  155. }
  156. return array;
  157. }
  158. public RowsMap CalculateAmount(ArrayList<Long> itemclsids, Long sys_enterpriseid,
  159. String itemids) throws YosException {
  160. Rows rows = null;
  161. itemclsids.addAll(ItemClass.getSubItemClassIds(controller, itemclsids));
  162. itemclsids.add(0l);
  163. if (itemids.equals("()")) {
  164. rows = dbConnect.runSqlQuery("select t2.closedate,t2.sys_enterpriseid,sum(if(t1.isclose,t1.amount,t1.price*(t1.qty-t1.undeliqty))) amount from sa_orderitems t1 \n" +
  165. "inner join sa_order t2 on t1.sa_orderid=t2.sa_orderid and t1.siteid=t2.siteid\n" +
  166. "inner join plm_item t3 on t1.itemid=t3.itemid and t1.siteid=t3.siteid where t2.type !='促销订单' and ifnull( t2.closedate,'')!='' and t2.sa_accountclassid!=0 and t2.sys_enterpriseid =" + sys_enterpriseid + " and t2.status in('关闭') and t1.itemid in(select itemid from sa_itemsaleclass where itemclassid in" + itemclsids.toString().replace("[", "(").replace("]", ")") + " ) and t1.DECORATIONREBATEFLAG=0 and t1.siteid='" + controller.siteid + "' group by t2.sys_enterpriseid,t2.closedate");
  167. } else {
  168. rows = dbConnect.runSqlQuery("select t2.closedate,t2.sys_enterpriseid,sum(if(t1.isclose,t1.amount,t1.price*(t1.qty-t1.undeliqty))) amount from sa_orderitems t1 \n" +
  169. "inner join sa_order t2 on t1.sa_orderid=t2.sa_orderid and t1.siteid=t2.siteid\n" +
  170. "inner join plm_item t3 on t1.itemid=t3.itemid and t1.siteid=t3.siteid where t2.type !='促销订单' and ifnull( t2.closedate,'')!='' and t2.sa_accountclassid!=0 and t2.sys_enterpriseid =" + sys_enterpriseid + " and t2.status in('关闭') and t1.itemid not in" + itemids + " and t1.itemid in(select itemid from sa_itemsaleclass where itemclassid in" + itemclsids.toString().replace("[", "(").replace("]", ")") + " ) and t1.DECORATIONREBATEFLAG=0 and t1.siteid='" + controller.siteid + "' group by t2.sys_enterpriseid,t2.closedate");
  171. }
  172. return rows.toRowsMap("sys_enterpriseid");
  173. }
  174. public RowsMap QuerySaorderNum(ArrayList<Long> itemclsids, Long sys_enterpriseid,
  175. String itemids) throws YosException {
  176. Rows rows = null;
  177. itemclsids.addAll(ItemClass.getSubItemClassIds(controller, itemclsids));
  178. itemclsids.add(0l);
  179. if (itemids.equals("()")) {
  180. rows = dbConnect.runSqlQuery("select t2.closedate,t2.sys_enterpriseid,t2.sa_orderid,SUM(if(t1.isclose,t1.amount,t1.price*(t1.qty-t1.undeliqty))) amount from sa_orderitems t1 \n" +
  181. "inner join sa_order t2 on t1.sa_orderid=t2.sa_orderid and t1.siteid=t2.siteid\n" +
  182. "inner join plm_item t3 on t1.itemid=t3.itemid and t1.siteid=t3.siteid where t2.type !='促销订单' and ifnull( t2.closedate,'')!='' and t2.sa_accountclassid!=0 and t2.sys_enterpriseid =" + sys_enterpriseid + " and t2.status in('关闭') and t1.itemid in(select itemid from sa_itemsaleclass where itemclassid in" + itemclsids.toString().replace("[", "(").replace("]", ")") + " ) and t1.DECORATIONREBATEFLAG=0 and t1.siteid='" + controller.siteid + "' group by t2.sa_orderid,t2.sys_enterpriseid,t2.closedate");
  183. } else {
  184. rows = dbConnect.runSqlQuery("select t2.closedate,t2.sys_enterpriseid,t2.sa_orderid,SUM(if(t1.isclose,t1.amount,t1.price*(t1.qty-t1.undeliqty))) amount from sa_orderitems t1 \n" +
  185. "inner join sa_order t2 on t1.sa_orderid=t2.sa_orderid and t1.siteid=t2.siteid\n" +
  186. "inner join plm_item t3 on t1.itemid=t3.itemid and t1.siteid=t3.siteid where t2.type !='促销订单' and ifnull( t2.closedate,'')!='' and t2.sa_accountclassid!=0 and t2.sys_enterpriseid =" + sys_enterpriseid + " and t2.status in('关闭') and t1.itemid not in" + itemids + " and t1.itemid in(select itemid from sa_itemsaleclass where itemclassid in" + itemclsids.toString().replace("[", "(").replace("]", ")") + " ) and t1.DECORATIONREBATEFLAG=0 and t1.siteid='" + controller.siteid + "' group by t2.sa_orderid,t2.sys_enterpriseid,t2.closedate");
  187. }
  188. return rows.toRowsMap("sys_enterpriseid");
  189. }
  190. public RowsMap QuerySaorderDetailId(ArrayList<Long> itemclsids, Long sys_enterpriseid,
  191. String itemids) throws YosException {
  192. Rows rows = null;
  193. itemclsids.addAll(ItemClass.getSubItemClassIds(controller, itemclsids));
  194. itemclsids.add(0l);
  195. String sql = "select t2.closedate,t2.sys_enterpriseid,t1.sa_orderitemsid from sa_orderitems t1 " +
  196. "inner join sa_order t2 on t1.sa_orderid=t2.sa_orderid and t1.siteid=t2.siteid " +
  197. "inner join plm_item t3 on t1.itemid=t3.itemid and t1.siteid=t3.siteid " +
  198. "where t2.type !='促销订单' " +
  199. "and ifnull( t2.closedate,'')!='' " +
  200. "and t2.sa_accountclassid!=0 and t2.sys_enterpriseid =" + sys_enterpriseid + " " +
  201. "and t2.status in('关闭') " +
  202. "and t1.itemid in(select itemid from sa_itemsaleclass where itemclassid in" + itemclsids.toString().replace("[", "(").replace("]", ")") + " ) " +
  203. "and t1.DECORATIONREBATEFLAG=0 and t1.siteid='" + controller.siteid + "' ";
  204. if (!itemids.equals("()")) {
  205. sql = sql + " and t1.itemid not in" + itemids;
  206. }
  207. sql = sql + "group by t2.sys_enterpriseid,t2.closedate,t1.sa_orderitemsid";
  208. System.err.println(sql);
  209. rows = dbConnect.runSqlQuery(sql);
  210. return rows.toRowsMap("sys_enterpriseid");
  211. }
  212. // //创建返利结算单表头
  213. // public InsertSQL getrebateaccounthead(Long sa_rebateaccountheadid) throws YosException {
  214. // InsertSQL insertSQL = SQLFactory.createInsertSQL(controller, "sa_rebateaccounthead");
  215. // insertSQL.setUniqueid(sa_rebateaccountheadid).setSiteid(controller.siteid);
  216. // insertSQL.setValue("billdate", getDate_Str());
  217. // insertSQL.setValue("status", "新建");
  218. // insertSQL.setValue("billno", controller.createBillCode("rebateaccountheadbill"));
  219. // return insertSQL;
  220. // }
  221. //
  222. // //创建返利结算单表体
  223. // public InsertSQL getrebateaccount(Long sa_rebateaccountid, Long sa_rebateaccountheadid, Long sys_enterpriseid) throws YosException {
  224. // InsertSQL insertSQL = SQLFactory.createInsertSQL(controller, "sa_rebateaccount");
  225. // insertSQL.setUniqueid(sa_rebateaccountid).setSiteid(controller.siteid);
  226. // //核准金额
  227. // insertSQL.setValue("approvedamount", 10);
  228. // //装修返利金额
  229. // insertSQL.setValue("rebateamount", 10);
  230. // insertSQL.setValue("sa_rebateaccountheadid", sa_rebateaccountheadid);
  231. // //合作企业档案ID
  232. // insertSQL.setValue("sys_enterpriseid", sys_enterpriseid);
  233. // //行号
  234. // insertSQL.setValue("rowno", 1);
  235. // //累计订购返利商品金额
  236. // insertSQL.setValue("accumulatedamount", 10);
  237. // return insertSQL;
  238. // }
  239. //
  240. // //创建返利结算单表体
  241. // public InsertSQL getrebateaccountdetail(Long sa_orderid, Long sa_rebateaccountid) throws YosException {
  242. // InsertSQL insertSQL = SQLFactory.createInsertSQL(controller, "sa_rebateaccountdetail");
  243. // insertSQL.setUniqueid(createTableID("sa_rebateaccountdetail")).setSiteid(controller.siteid);
  244. // //核准金额
  245. // insertSQL.setValue("sa_orderid", sa_orderid);
  246. // //装修返利金额
  247. // insertSQL.setValue("sa_rebateaccountid", sa_rebateaccountid);
  248. // insertSQL.setValue("rateamount", 10);
  249. // //合作企业档案ID
  250. // insertSQL.setValue("amount", 10);
  251. // return insertSQL;
  252. // }
  253. //查询并执行商品费用方案方法
  254. public void executeDataFunction(Long sa_orderid) throws YosException {
  255. SQLFactory sqlFactory = new SQLFactory(this, "查询商品费用方案方法");
  256. sqlFactory.addParameter("siteid", controller.siteid);
  257. sqlFactory.addParameter("sa_orderid", sa_orderid);
  258. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  259. if(rows.isEmpty()){
  260. //清空费用表
  261. DeleteSQL deleteSQL = SQLFactory.createDeleteSQL(controller, "sa_order_cost");
  262. deleteSQL.setWhere("sa_orderid", sa_orderid);
  263. deleteSQL.delete();
  264. }
  265. if(rows.isNotEmpty()){
  266. DataFunction df = DataFunction.get(controller, rows.get(0).getString("functionname"));
  267. df.addParameter("sa_orderid", sa_orderid);
  268. df.action();
  269. }
  270. }
  271. public void checkOffOrderItems(Long sa_orderid, String tablename, Long sa_promotionid) throws YosException {
  272. String sql = "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="
  273. + sa_orderid + " and t1.siteid='" + controller.siteid + "' and t2.isonsale!=1";
  274. if (tablename.equals("sa_promotion_items")) {
  275. sql = sql + " and t2.sa_promotionid=" + sa_promotionid;
  276. }
  277. Rows iteminfos = dbConnect.runSqlQuery(sql);
  278. if (iteminfos.isNotEmpty()) {
  279. Row itemRow = dbConnect.runSqlQuery(0, "SELECT itemno from plm_item WHERE itemid=" + iteminfos.get(0).getString("itemid"));
  280. //throw new YosException(false, "检测到品号" + itemRow.getString("itemno") + "未上架,请删除后重试");
  281. }
  282. }
  283. public void checkOffOrderItems(String tablename, Long itemid, Long sa_promotionid) throws YosException {
  284. String sql = "select t1.itemid from " + tablename + " t1 where t1.siteid='" + controller.siteid + "' and t1.isonsale!=1 and t1.itemid=" + itemid;
  285. if (tablename.equals("sa_promotion_items")) {
  286. sql = sql + " and t1.sa_promotionid=" + sa_promotionid;
  287. }
  288. Rows iteminfos = dbConnect.runSqlQuery(sql);
  289. if (iteminfos.isNotEmpty()) {
  290. Row itemRow = dbConnect.runSqlQuery(0, "SELECT itemno from plm_item WHERE itemid=" + iteminfos.get(0).getString("itemid"));
  291. //throw new YosException(false, "检测到品号" + itemRow.getString("itemno") + "未上架,请删除后重试");
  292. }
  293. }
  294. /**
  295. * 是否符合商品起订量和增量判断
  296. *
  297. * @param type
  298. * @param itemRowsMap
  299. * @param item
  300. * @param itemRow
  301. * @param promotionitems
  302. * @throws YosException
  303. */
  304. public void compareQty(String type, RowsMap itemRowsMap, JSONObject item, Row itemRow, Rows promotionitems) throws YosException {
  305. BigDecimal qty = item.getBigDecimal("qty");
  306. String itemid = item.getString("itemid");
  307. Rows itemRows = itemRowsMap.getOrDefault(itemid, new Rows());
  308. if (qty.compareTo(BigDecimal.ZERO) == 0) {
  309. throw new YosException(false, "品号为" + itemRows.get(0).getString("itemno") + "的商品的添加数量为0,不允许添加");
  310. }
  311. if (!type.equals("促销订单")) {
  312. if (!type.equals("特殊订单")) {
  313. if (itemRowsMap.containsKey(itemid)) {
  314. if (itemRows.isNotEmpty()) {
  315. BigDecimal orderaddqty = itemRows.get(0).getBigDecimal("orderaddqty");
  316. BigDecimal orderminqty = itemRows.get(0).getBigDecimal("orderminqty");
  317. if (orderaddqty.compareTo(BigDecimal.ZERO) != 0) {
  318. if (((qty.subtract(orderminqty)).remainder(orderaddqty)).compareTo(BigDecimal.ZERO) != 0 || qty.compareTo(orderminqty) < 0) {
  319. throw new YosException(false, "品号为" + itemRows.get(0).getString("itemno") + "的商品数量不符合该商品的起订量和增量");
  320. }
  321. }
  322. }
  323. }
  324. } else {
  325. if (qty.compareTo(BigDecimal.ZERO) <= 0) {
  326. throw new YosException(false, "品号为" + itemRows.get(0).getString("itemno") + "的商品数量必须大于0");
  327. }
  328. }
  329. }
  330. RowsMap pRowsMap = promotionitems.toRowsMap("sa_promotion_itemsid");
  331. long sa_promotionid =0;
  332. if(promotionitems.isNotEmpty()){
  333. sa_promotionid = promotionitems.get(0).getLong("sa_promotionid");
  334. }
  335. if (type.equals("促销订单")) {
  336. if (pRowsMap.containsKey(item.getString("sa_promotion_itemsid"))) {
  337. String saPromotionItemsid = item.getString("sa_promotion_itemsid");
  338. if (pRowsMap.get(saPromotionItemsid).isNotEmpty()) {
  339. BigDecimal saledqty = pRowsMap.get(saPromotionItemsid).get(0).getBigDecimal("saledqty");
  340. BigDecimal groupqty = pRowsMap.get(saPromotionItemsid).get(0).getBigDecimal("groupqty");
  341. BigDecimal orderaddqty = pRowsMap.get(saPromotionItemsid).get(0).getBigDecimal("orderaddqty");
  342. BigDecimal orderminqty = pRowsMap.get(saPromotionItemsid).get(0).getBigDecimal("orderminqty");
  343. boolean islimit = pRowsMap.get(saPromotionItemsid).get(0).getBoolean("islimit");
  344. Rows rows =dbConnect.runSqlQuery("select * from sa_promotion where sa_promotionid="+sa_promotionid);
  345. if(rows.isNotEmpty()){
  346. if(!rows.get(0).getString("type").equals("套餐活动")){
  347. if (orderaddqty.compareTo(BigDecimal.ZERO) != 0) {
  348. if (((qty.subtract(orderminqty)).remainder(orderaddqty)).compareTo(BigDecimal.ZERO) != 0 || qty.compareTo(orderminqty) < 0) {
  349. throw new YosException(false, "品号为" + itemRowsMap.get(itemid).get(0).getString("itemno") + "的商品数量不符合该商品的起订量和增量");
  350. }
  351. }
  352. }
  353. }
  354. if (islimit) {
  355. if (groupqty.compareTo(saledqty.add(qty)) < 0) {
  356. throw new YosException(false, "品号:" + itemRow.getString("itemno") + "已超过限购数量:" + groupqty.subtract(saledqty));
  357. }
  358. }
  359. }
  360. }
  361. }
  362. }
  363. /**
  364. * 获取定制属性
  365. *
  366. * @param itemRowsMap
  367. * @param item
  368. * @return
  369. * @throws YosException
  370. */
  371. public JSONArray getCustomProperties(RowsMap itemRowsMap, JSONObject item) throws YosException {
  372. JSONArray customproperties = new JSONArray();
  373. Rows customschemeRows = dbConnect.runSqlQuery("select * from sa_customscheme_items where siteid='" + controller.siteid + "'");
  374. RowsMap customschemeRowsMap = customschemeRows.toRowsMap("sa_customschemeid");
  375. String itemid = item.getString("itemid");
  376. if (itemRowsMap.containsKey(itemid)) {
  377. if (itemRowsMap.get(itemid).isNotEmpty()) {
  378. if (itemRowsMap.get(itemid).get(0).getLong("sa_customschemeid") > 0 && !itemRowsMap.get(itemid).get(0).getBoolean("iscustomsize")) {
  379. // System.out.println(0000);
  380. if (customschemeRowsMap.containsKey(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid"))) {
  381. // System.out.println(111111);
  382. for (Row customschemeItem : (customschemeRowsMap.get(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid")))) {
  383. // System.out.println(customschemeItem.getString("description"));
  384. // System.out.println(2222);
  385. customproperties.add(customschemeItem.getString("description") + ":" + itemRowsMap.get(itemid).get(0).getString(customschemeItem.getString("value")));
  386. }
  387. }
  388. } else if (itemRowsMap.get(itemid).get(0).getBoolean("iscustomsize")) {
  389. // System.out.println(3333);
  390. if (customschemeRowsMap.containsKey(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid"))) {
  391. // System.out.println(4444);
  392. for (Row customschemeItem : (customschemeRowsMap.get(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid")))) {
  393. // System.out.println(5555);
  394. // System.out.println((customschemeRowsMap.get(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid"))).size());
  395. if (!customschemeItem.getString("value").equals("spec")) {
  396. // customproperties.add(customschemeItem.getString("description") + ":" + itemRowsMap.get(itemid).get(0).getString(customschemeItem.getString("value")));
  397. } else {
  398. customproperties.add(getcustomproperties(item));
  399. }
  400. }
  401. } else {
  402. customproperties.add(getcustomproperties(item));
  403. }
  404. }
  405. }
  406. }
  407. return customproperties;
  408. }
  409. public String getcustomproperties(JSONObject item) {
  410. String material = item.getStringValue("custommaterial");
  411. String colors = item.getStringValue("customcolors");
  412. String cheek = item.getStringValue("customcheek");
  413. String length = item.getStringValue("length", false, "0");
  414. String width = item.getStringValue("width", false, "0");
  415. String str = "";
  416. if (!StringUtils.isBlank(material) && !material.equals("0")) {
  417. str = "选项:" + material;
  418. }
  419. if (!StringUtils.isBlank(colors) && !colors.equals("0")) {
  420. if (str.isEmpty()) {
  421. str = "颜色:" + colors;
  422. } else {
  423. str = str + ";" + "颜色:" + colors;
  424. }
  425. }
  426. if (!StringUtils.isBlank(cheek) && !cheek.equals("0")) {
  427. if (str.isEmpty()) {
  428. str = "工艺:" + cheek;
  429. } else {
  430. str = str + ";" + "工艺:" + cheek;
  431. }
  432. }
  433. if (!length.equals("0") && !width.equals("0")) {
  434. String size = "尺寸:" + length + "×" + width;
  435. if (str.isEmpty()) {
  436. str = size;
  437. } else {
  438. str = str + ";" + size;
  439. }
  440. }
  441. return str;
  442. }
  443. /**
  444. * 获取定制尺寸信息
  445. *
  446. * @param itemRowsMap
  447. * @param item
  448. * @return
  449. * @throws YosException
  450. */
  451. public String getCustomSpec(RowsMap itemRowsMap, JSONObject item) throws YosException {
  452. String spec = "";
  453. Rows customschemeRows = dbConnect.runSqlQuery("select * from sa_customscheme_items where siteid='" + controller.siteid + "'");
  454. RowsMap customschemeRowsMap = customschemeRows.toRowsMap("sa_customschemeid");
  455. String itemid = item.getString("itemid");
  456. if (itemRowsMap.containsKey(itemid)) {
  457. if (itemRowsMap.get(itemid).isNotEmpty()) {
  458. if (!itemRowsMap.get(itemid).get(0).getBoolean("iscustomsize")) {
  459. spec = itemRowsMap.get(itemid).get(0).getString("spec");
  460. } else {
  461. spec = item.getStringValue("length") + "*" + item.getStringValue("width");
  462. }
  463. }
  464. }
  465. return spec;
  466. }
  467. /**
  468. * 批量查询商品信息
  469. *
  470. * @param items
  471. * @return
  472. * @throws YosException
  473. */
  474. public RowsMap getItemRowsMap(JSONArray items) throws YosException {
  475. ArrayList<Long> itemids = new ArrayList<>();
  476. for (Object obj : items) {
  477. JSONObject item = (JSONObject) obj;
  478. itemids.add(item.getLong("itemid"));
  479. }
  480. if (itemids.size() == 0) {
  481. itemids.add(-1L);
  482. }
  483. 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;
  484. sql = sql.replace("[", "(").replace("]", ")");
  485. return dbConnect.runSqlQuery(sql).toRowsMap("itemid");
  486. }
  487. public UpdateSQL getUpdateSql() throws YosException {
  488. System.err.println("123");
  489. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(controller, "sa_order");
  490. if (controller.content.containsKey("sa_order_v")) {
  491. updateSQL.setDataVersion(content.getIntValue("sa_order_v"));
  492. }
  493. updateSQL.setValue("changeby", controller.username);
  494. updateSQL.setWhere("sa_orderid", content.getLongValue("sa_orderid"));
  495. updateSQL.setWhere("siteid", controller.siteid);
  496. System.err.println(updateSQL.getSQL());
  497. return updateSQL;
  498. }
  499. String sa_orderitems = "sa_orderitems";
  500. public InsertSQL getInsertSQL(JSONArray customproperties, long sa_orderitemsid, JSONObject item, Row itemRow, String type,
  501. BigDecimal defaultprice, BigDecimal price, BigDecimal saleprice, BigDecimal marketprice,
  502. String spec, Rows promotionitems,long width,long length,BigDecimal custamount,BigDecimal rwpricerate,BigDecimal pricerate) throws YosException {
  503. Long itemid = item.getLong("itemid");
  504. BigDecimal qty = item.getBigDecimal("qty");
  505. RowsMap promotionitemsRowsMap = promotionitems.toRowsMap("sa_promotion_itemsid");
  506. String signaturecode = "";
  507. if (type.equals("促销订单")) {
  508. if (promotionitemsRowsMap.containsKey(item.getString("sa_promotion_itemsid"))) {
  509. if (promotionitemsRowsMap.get(item.getString("sa_promotion_itemsid")).isNotEmpty()) {
  510. signaturecode = promotionitemsRowsMap.get(item.getString("sa_promotion_itemsid")).get(0).getString("signaturecode");
  511. }
  512. }
  513. }
  514. InsertSQL insertSQL = SQLFactory.createInsertSQL(controller, sa_orderitems);
  515. insertSQL.setValue("customproperties", customproperties.toJSONString().replace("[", "").replace("]", "").replace("\"", ""));
  516. insertSQL.setValue("siteid", controller.siteid);
  517. insertSQL.setValue("sa_orderitemsid", sa_orderitemsid);
  518. insertSQL.setValue("sa_orderid", content.getLongValue("sa_orderid"));
  519. insertSQL.setValue("rowno", 1);
  520. insertSQL.setValue("itemid", itemid);
  521. insertSQL.setValue("remarks", item.getStringValue("remarks") + signaturecode);
  522. insertSQL.setValue("itemno", itemRow.getString("itemno"));
  523. insertSQL.setValue("itemname", itemRow.getString("itemname"));
  524. insertSQL.setValue("model", itemRow.getString("model"));
  525. insertSQL.setValue("unit", itemRow.getString("unit"));
  526. insertSQL.setValue("auxunit", itemRow.getString("auxunit"));
  527. insertSQL.setValue("batchcontrol", itemRow.getLong("batchcontrol"));
  528. insertSQL.setValue("stockno", item.getOrDefault("stockno", "null"));
  529. insertSQL.setValue("position", item.getOrDefault("position", "null"));
  530. insertSQL.setValue("batchno", item.getOrDefault("batchno", "null"));
  531. insertSQL.setValue("spec", spec);
  532. insertSQL.setValue("custamount",custamount);
  533. insertSQL.setValue("rwpricerate",rwpricerate);
  534. insertSQL.setValue("pricerate",pricerate);
  535. insertSQL.setValue("sa_promotion_itemsid", item.getLongValue("sa_promotion_itemsid"));
  536. insertSQL.setValue("delivery", item.getStringValue("delivery"));
  537. insertSQL.setValue("cardno", item.getStringValue("cardno"));
  538. insertSQL.setValue("sa_workorderid", item.getLongValue("sa_workorderid"));
  539. String deliverydate = item.getStringValue("deliverydate");
  540. Rows rows = dbConnect.runSqlQuery("SELECT deliverydate,defaultprice from sa_orderitems WHERE sa_orderitemsid =" + sa_orderitemsid);
  541. if (deliverydate.isEmpty()) {
  542. if (rows.isNotEmpty()) {
  543. deliverydate = rows.get(0).getString("deliverydate");
  544. } else {
  545. deliverydate = "null";
  546. }
  547. }
  548. String needdate = item.getStringValue("needdate");
  549. insertSQL.setValue("needdate", needdate.equals("") ? "null" : needdate);
  550. insertSQL.setValue("deliverydate", deliverydate.equals("") ? "null" : deliverydate);
  551. BigDecimal conversionrate = itemRow.getBigDecimal("conversionrate");
  552. if (conversionrate.compareTo(BigDecimal.ZERO) <= 0) {
  553. conversionrate = BigDecimal.valueOf(1);
  554. }
  555. insertSQL.setValue("conversionrate", conversionrate);
  556. //订购数量
  557. insertSQL.setValue("qty", qty);
  558. insertSQL.setValue("undeliqty", qty);
  559. insertSQL.setValue("deliedqty", 0);
  560. //辅助单位数量
  561. insertSQL.setValue("auxqty", qty.divide(conversionrate));
  562. if (type.equals("特殊订单")) {
  563. if (item.containsKey("defaultprice")) {
  564. if (item.getBigDecimal("price").compareTo(BigDecimal.ZERO) > 0 && item.getBigDecimal("amount").compareTo(BigDecimal.ZERO) <= 0) {
  565. insertSQL.setValue("price", item.getBigDecimal("price"));
  566. //折前金额(元)
  567. insertSQL.setValue("amount", price.multiply(qty));
  568. } else if (item.getBigDecimal("price").compareTo(BigDecimal.ZERO) <= 0 && item.getBigDecimal("amount").compareTo(BigDecimal.ZERO) > 0) {
  569. insertSQL.setValue("price", item.getBigDecimal("amount").divide(qty, 6, BigDecimal.ROUND_HALF_UP));
  570. //折前金额(元)
  571. insertSQL.setValue("amount", item.getBigDecimal("amount"));
  572. } else {
  573. insertSQL.setValue("price", price);
  574. //折前金额(元)
  575. insertSQL.setValue("amount", price.multiply(qty));
  576. }
  577. } else {
  578. insertSQL.setValue("price", price);
  579. //折前金额(元)
  580. insertSQL.setValue("amount", price.multiply(qty));
  581. }
  582. } else {
  583. insertSQL.setValue("price", price);
  584. //折前金额(元)
  585. insertSQL.setValue("amount", price.multiply(qty));
  586. }
  587. //单价,折后价(元),取合同价
  588. insertSQL.setValue("defaultprice", defaultprice);
  589. insertSQL.setValue("saleprice", saleprice);
  590. //金额,折后金额(元)
  591. insertSQL.setValue("defaultamount", defaultprice.multiply(qty));
  592. //牌价、市场价(元),标准订单牌价取商品价格,项目订单取合同里的牌价
  593. insertSQL.setValue("marketprice", marketprice);
  594. insertSQL.setValue("width", width);
  595. insertSQL.setValue("length", length);
  596. if(type.equals("配件订单")){
  597. if(StringUtils.isBlank(item.getStringValue("cardno"))){
  598. insertSQL.setValue("canoffqty", qty);
  599. }else{
  600. 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")+"'");
  601. if(aftersalesbomrows.isNotEmpty()){
  602. 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);
  603. if(aftersalesbompjrows.isNotEmpty()){
  604. insertSQL.setValue("canoffqty", aftersalesbompjrows.get(0).getBigDecimal("qty").compareTo(qty)>0?qty:aftersalesbompjrows.get(0).getBigDecimal("qty"));
  605. }else{
  606. insertSQL.setValue("canoffqty", 1);
  607. }
  608. }else{
  609. insertSQL.setValue("canoffqty", 1);
  610. }
  611. }
  612. }else{
  613. insertSQL.setValue("canoffqty", 0);
  614. }
  615. return insertSQL;
  616. }
  617. public UpdateSQL getUpdateSQL(long sa_orderitemsid, JSONObject item, Row itemRow, String type, BigDecimal defaultprice, BigDecimal price, BigDecimal saleprice,long width,long length) throws YosException {
  618. BigDecimal qty = item.getBigDecimal("qty");
  619. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(controller, sa_orderitems);
  620. //订购数量
  621. updateSQL.setValue("qty", qty);
  622. //辅助单位数量
  623. BigDecimal conversionrate = itemRow.getBigDecimal("conversionrate");
  624. if (conversionrate.compareTo(BigDecimal.ZERO) <= 0) {
  625. conversionrate = BigDecimal.valueOf(1);
  626. }
  627. updateSQL.setValue("itemid", item.getLongValue("itemid"));
  628. updateSQL.setValue("itemno", itemRow.getString("itemno"));
  629. updateSQL.setValue("itemname", itemRow.getString("itemname"));
  630. updateSQL.setValue("model", itemRow.getString("model"));
  631. updateSQL.setValue("unit", itemRow.getString("unit"));
  632. updateSQL.setValue("auxqty", qty.divide(conversionrate));
  633. updateSQL.setValue("undeliqty", qty);
  634. updateSQL.setValue("remarks", item.getStringValue("remarks"));
  635. updateSQL.setValue("stockno", item.getOrDefault("stockno", "null"));
  636. updateSQL.setValue("position", item.getOrDefault("position", "null"));
  637. updateSQL.setValue("batchno", item.getOrDefault("batchno", "null"));
  638. updateSQL.setValue("custamount", item.getBigDecimal("custamount"));
  639. updateSQL.setValue("delivery", item.getStringValue("delivery"));
  640. updateSQL.setValue("cardno", item.getStringValue("cardno"));
  641. updateSQL.setValue("sa_workorderid", item.getLongValue("sa_workorderid"));
  642. String deliverydate = item.getStringValue("deliverydate");
  643. Rows rows = dbConnect.runSqlQuery("SELECT deliverydate,defaultprice from sa_orderitems WHERE sa_orderitemsid =" + sa_orderitemsid);
  644. if (deliverydate.isEmpty()) {
  645. if (rows.isNotEmpty()) {
  646. deliverydate = rows.get(0).getString("deliverydate");
  647. } else {
  648. deliverydate = "null";
  649. }
  650. }
  651. String needdate = item.getStringValue("needdate");
  652. updateSQL.setValue("needdate", needdate.equals("") ? "null" : needdate);
  653. updateSQL.setValue("deliverydate", deliverydate.equals("") ? "null" : deliverydate);
  654. if (type.equals("特殊订单")) {
  655. if (item.containsKey("defaultprice")) {
  656. if (item.getBigDecimal("price").compareTo(BigDecimal.ZERO) > 0 && item.getBigDecimal("amount").compareTo(BigDecimal.ZERO) <= 0) {
  657. updateSQL.setValue("price", item.getBigDecimal("price"));
  658. //折前金额(元)
  659. updateSQL.setValue("amount", price.multiply(qty));
  660. } else if (item.getBigDecimal("price").compareTo(BigDecimal.ZERO) <= 0 && item.getBigDecimal("amount").compareTo(BigDecimal.ZERO) > 0) {
  661. updateSQL.setValue("price", item.getBigDecimal("amount").divide(qty, 6, BigDecimal.ROUND_HALF_UP));
  662. //折前金额(元)
  663. updateSQL.setValue("amount", item.getBigDecimal("amount"));
  664. } else {
  665. updateSQL.setValue("price", price);
  666. //折前金额(元)
  667. updateSQL.setValue("amount", price.multiply(qty));
  668. }
  669. } else {
  670. updateSQL.setValue("price", price);
  671. //折前金额(元)
  672. updateSQL.setValue("amount", price.multiply(qty));
  673. }
  674. } else {
  675. updateSQL.setValue("price", price);
  676. //折前金额(元)
  677. updateSQL.setValue("amount", price.multiply(qty));
  678. }
  679. //单价,折后价(元),取合同价
  680. updateSQL.setValue("defaultprice", defaultprice);
  681. //金额,折后金额(元)
  682. updateSQL.setValue("defaultamount", defaultprice.multiply(qty));
  683. updateSQL.setValue("saleprice", saleprice);
  684. updateSQL.setValue("width", width);
  685. updateSQL.setValue("length", length);
  686. updateSQL.setWhere("siteid", controller.siteid);
  687. updateSQL.setWhere("sa_orderitemsid", sa_orderitemsid);
  688. return updateSQL;
  689. }
  690. public BigDecimal getDefaultprice(ItemPrice itemPrice) throws YosException {
  691. return itemPrice.getGraderateprice();
  692. }
  693. public BigDecimal getPrice(Row orderRow, ItemPrice itemPrice, JSONObject item) throws YosException {
  694. String type = orderRow.getString("type");
  695. Long sa_promotionid = orderRow.getLong("sa_promotionid");
  696. BigDecimal price;
  697. switch (type) {
  698. case "促销订单":
  699. price = itemPrice.getPromotionPrice(sa_promotionid,item.getLong("sa_promotion_itemsid"));
  700. break;
  701. default:
  702. price = itemPrice.getGraderateprice();
  703. }
  704. return price;
  705. }
  706. public BigDecimal getSalePrice(Row orderRow, ItemPrice itemPrice,JSONObject item,RowsMap itemRowsMap) throws YosException {
  707. BigDecimal price = itemPrice.getGraderateprice();
  708. if (itemRowsMap.containsKey(item.getString("itemid"))) {
  709. if (itemRowsMap.get(item.getString("itemid")).isNotEmpty()) {
  710. if (itemRowsMap.get(item.getString("itemid")).get(0).getBoolean("iscustomsize") && itemRowsMap.get(item.getString("itemid")).get(0).getLong("pricingmetod") == 1) {
  711. price = price.multiply(item.getBigDecimal("length").multiply(item.getBigDecimal("width")).divide(new BigDecimal("1000000"), 4, BigDecimal.ROUND_HALF_UP));
  712. }
  713. }
  714. }
  715. return price;
  716. }
  717. }