OrderItemsHelper.java 43 KB

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