|
|
@@ -1,8 +1,11 @@
|
|
|
package restcontroller.webmanage.sale.order;
|
|
|
|
|
|
+import beans.accountbalance.Accountbalance;
|
|
|
+import beans.accountbalance.CashbillEntity;
|
|
|
import beans.datacontrllog.DataContrlLog;
|
|
|
import beans.itemclass.ItemClass;
|
|
|
import beans.itemprice.ItemPrice;
|
|
|
+import beans.order.Order;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import common.BaseClass;
|
|
|
@@ -30,102 +33,117 @@ public class OrderItemsHelper extends BaseClass {
|
|
|
|
|
|
//执行是否自动返利结算
|
|
|
public void executeAutoRebate(Long sa_orderid, Long sys_enterpriseid) throws YosException {
|
|
|
-//
|
|
|
-// Rows enterpriseRows = dbConnect.runSqlQuery("SELECT isrebate from sys_enterprise WHERE sys_enterpriseid=" + sys_enterpriseid);
|
|
|
-// if (enterpriseRows.isEmpty()) {
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// //是否自动返利结算
|
|
|
-// Boolean isrebate = enterpriseRows.get(0).getBoolean("isrebate");
|
|
|
-// if (!isrebate) {
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// //查询有效期内,余额大于0 的经销商装修补贴返利
|
|
|
-// 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";
|
|
|
-// Rows rows = dbConnect.runSqlQuery(sql);
|
|
|
-// if (rows.isEmpty()) {
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// //查询装修返利方案
|
|
|
-// Rows rebateschemeRows = dbConnect.runSqlQuery("select * from sa_rebatescheme where siteid='" + controller.siteid + "'");
|
|
|
-// if (rebateschemeRows.isEmpty()) {
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// ArrayList<String> sqlList = new ArrayList<>();
|
|
|
-// Long sa_rebateschemeid = rebateschemeRows.get(0).getLong("sa_rebateschemeid");
|
|
|
-//
|
|
|
-// Rows rebatecategorys = dbConnect.runSqlQuery("select * from sa_rebatecategory where siteid='" + controller.siteid + "' and sa_rebateschemeid=" + sa_rebateschemeid);
|
|
|
-// Rows rebateunproducts = dbConnect.runSqlQuery("select * from sa_rebateunproducts where siteid='" + controller.siteid + "' and sa_rebateschemeid=" + sa_rebateschemeid);
|
|
|
-// Rows agentdecorationtrebates = dbConnect.runSqlQuery("select * from sa_agentdecorationtrebate where siteid='" + controller.siteid + "' and sys_enterpriseid=" + sys_enterpriseid);
|
|
|
-// Rows agentrebatecategorys = dbConnect.runSqlQuery("select * from sa_agentrebatecategory where siteid='" + controller.siteid + "'");
|
|
|
-// RowsMap agentdecorationtrebatesRowsMap = agentdecorationtrebates.toRowsMap("sys_enterpriseid");
|
|
|
-// RowsMap agentrebatecategorysRowsMap = agentrebatecategorys.toRowsMap("sys_enterpriseid");
|
|
|
-// String itemids = rebateunproducts.toJsonArray("itemid").toString();
|
|
|
-// itemids = itemids.replace("[", "(").replace("]", ")");
|
|
|
-//
|
|
|
-// ArrayList<Long> itemclsids = rebatecategorys.toArrayList("itemclassid", new ArrayList<Long>());
|
|
|
-//
|
|
|
-// RowsMap amountRowsMap = CalculateAmount(itemclsids, sys_enterpriseid, itemids);
|
|
|
-// RowsMap ordersRowsMap = QuerySaorderNum(itemclsids, sys_enterpriseid, itemids);
|
|
|
-// RowsMap orderDetailIdRowsMap = QuerySaorderDetailId(itemclsids, sys_enterpriseid, itemids);
|
|
|
-//
|
|
|
-// for (Row row : rebateaccounts) {
|
|
|
-// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
-// String begindate = "";
|
|
|
-// String enddate = "";
|
|
|
-// BigDecimal amount = BigDecimal.ZERO;
|
|
|
-// BigDecimal balance = BigDecimal.ZERO;
|
|
|
-// BigDecimal brate = BigDecimal.ZERO;
|
|
|
-//
|
|
|
-//
|
|
|
-//// String agentitemclsnums = null;
|
|
|
-// if (agentdecorationtrebatesRowsMap.containsKey(row.getString("sys_enterpriseid"))) {
|
|
|
-// if (!agentdecorationtrebatesRowsMap.get(row.getString("sys_enterpriseid")).isEmpty()) {
|
|
|
-// begindate = sdf.format(agentdecorationtrebatesRowsMap.get(row.getString("sys_enterpriseid")).get(0).getDate("begindate"));
|
|
|
-// if (begindate.compareTo("2023-08-26") < 0) {
|
|
|
-// begindate = "2023-08-26";
|
|
|
-// }
|
|
|
-// enddate = sdf.format(agentdecorationtrebatesRowsMap.get(row.getString("sys_enterpriseid")).get(0).getDate("enddate"));
|
|
|
-// amount = agentdecorationtrebatesRowsMap.get(row.getString("sys_enterpriseid")).get(0).getBigDecimal("amount");
|
|
|
-// balance = agentdecorationtrebatesRowsMap.get(row.getString("sys_enterpriseid")).get(0).getBigDecimal("balance");
|
|
|
-// brate = agentdecorationtrebatesRowsMap.get(row.getString("sys_enterpriseid")).get(0).getBigDecimal("brate");
|
|
|
-// }
|
|
|
-// }
|
|
|
-// final String begindate1 = begindate;
|
|
|
-// final String enddate1 = enddate;
|
|
|
-// List<Row> selectamountList = amountRowsMap.get(row.getString("sys_enterpriseid")).stream().filter(row1 -> sdf.format(row1.getDate("closedate")).compareTo(enddate1) <= 0 && sdf.format(row1.getDate("closedate")).compareTo(begindate1) >= 0).collect(Collectors.toList());
|
|
|
-// List<Row> selectorderDetailIdList = orderDetailIdRowsMap.get(row.getString("sys_enterpriseid")).stream().filter(row1 -> sdf.format(row1.getDate("closedate")).compareTo(enddate1) <= 0 && sdf.format(row1.getDate("closedate")).compareTo(begindate1) >= 0).collect(Collectors.toList());
|
|
|
-// List<Row> selectordersList = ordersRowsMap.get(row.getString("sys_enterpriseid")).stream().filter(row1 -> sdf.format(row1.getDate("closedate")).compareTo(enddate1) <= 0 && sdf.format(row1.getDate("closedate")).compareTo(begindate1) >= 0).collect(Collectors.toList());
|
|
|
-//
|
|
|
-// BigDecimal saorderamount = new BigDecimal("0");
|
|
|
-// for (Row row2 : selectamountList) {
|
|
|
-// if (row2.containsKey("amount")) {
|
|
|
-// saorderamount = saorderamount.add(row2.getBigDecimal("amount"));
|
|
|
-// }
|
|
|
-// }
|
|
|
-// System.out.println(saorderamount);
|
|
|
-// if (balance.compareTo(saorderamount.multiply(brate)) >= 0) {
|
|
|
-// sqlList.add("update sa_rebateaccount set approvedamount=" + saorderamount.multiply(brate) + ",rebateamount=" + saorderamount.multiply(brate) + ",accumulatedamount=" + saorderamount + " where sa_rebateaccountid=" + row.getLong("sa_rebateaccountid"));
|
|
|
-// } else if (balance.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
-// sqlList.add("update sa_rebateaccount set approvedamount=" + balance + ",rebateamount=" + balance + ",accumulatedamount=" + saorderamount + " where sa_rebateaccountid=" + row.getLong("sa_rebateaccountid"));
|
|
|
-// }
|
|
|
-// sqlList.add("update sa_orderitems set decorationrebateflag=1,rebatesourceid=" + rows.get(0).getLong("sa_rebateaccountheadid") + ",rebatesourceobject='sa_rebateaccounthead' where sa_orderitemsid in" + toJsonArray("sa_orderitemsid", selectorderDetailIdList).toJSONString().replace("[", "(").replace("]", ")"));
|
|
|
-//
|
|
|
-// for (Row order : selectordersList) {
|
|
|
-// SQLFactory sqlFactory = new SQLFactory(this, "返利结算单孙体明细新增");
|
|
|
-// sqlFactory.addParameter("siteid", controller.siteid);
|
|
|
-// sqlFactory.addParameter("sa_rebateaccountdetailid", createTableID("sa_rebateaccountdetail"));
|
|
|
-// sqlFactory.addParameter("sa_rebateaccountid", row.getLong("sa_rebateaccountid"));
|
|
|
-// sqlFactory.addParameter("sa_orderid", order.getLong("sa_orderid"));
|
|
|
-// sqlFactory.addParameter("rateamount", order.getBigDecimal("amount").multiply(brate));
|
|
|
-// sqlFactory.addParameter("amount", order.getBigDecimal("amount"));
|
|
|
-// sqlList.add(sqlFactory.getSQL());
|
|
|
-//
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-//
|
|
|
-// dbConnect.runSqlUpdate(sqlList);
|
|
|
+
|
|
|
+ Rows orderRows = Order.getOrderRows(controller, sa_orderid);
|
|
|
+ if (orderRows.get(0).getString("type").equals("促销订单")) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Rows enterpriseRows = dbConnect.runSqlQuery("SELECT isrebate from sys_enterprise WHERE sys_enterpriseid=" + sys_enterpriseid);
|
|
|
+ if (enterpriseRows.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //是否自动返利结算
|
|
|
+ Boolean isrebate = enterpriseRows.get(0).getBoolean("isrebate");
|
|
|
+ if (!isrebate) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //查询有效期内,余额大于0 的经销商装修补贴返利
|
|
|
+ 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";
|
|
|
+ Rows agentdecorationtrebatRows = dbConnect.runSqlQuery(sql);
|
|
|
+ if (agentdecorationtrebatRows.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //查询装修返利方案
|
|
|
+ Rows rebateschemeRows = dbConnect.runSqlQuery("select * from sa_rebatescheme where siteid='" + controller.siteid + "'");
|
|
|
+ if (rebateschemeRows.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ ArrayList<String> sqlList = new ArrayList<>();
|
|
|
+ Long sa_rebateschemeid = rebateschemeRows.get(0).getLong("sa_rebateschemeid");
|
|
|
+ Long sa_accountclassid = rebateschemeRows.get(0).getLong("sa_accountclassid");
|
|
|
+
|
|
|
+ Rows rebatecategorys = dbConnect.runSqlQuery("select itemclassid from sa_rebatecategory where siteid='" + controller.siteid + "' and sa_rebateschemeid=" + sa_rebateschemeid);
|
|
|
+ Rows rebateunproducts = dbConnect.runSqlQuery("select itemid from sa_rebateunproducts where siteid='" + controller.siteid + "' and sa_rebateschemeid=" + sa_rebateschemeid);
|
|
|
+
|
|
|
+ ArrayList<Long> itemclsids = rebatecategorys.toArrayList("itemclassid", new ArrayList<Long>());
|
|
|
+
|
|
|
+ itemclsids.addAll(ItemClass.getSubItemClassIds(controller, itemclsids));
|
|
|
+ itemclsids.add(0l);
|
|
|
+ ArrayList<Long> itemids = rebateunproducts.toArrayList("itemid", new ArrayList<Long>());
|
|
|
+ itemids.add(0l);
|
|
|
+
|
|
|
+ SQLFactory sqlFactory = new SQLFactory(controller, "查询返利数据");
|
|
|
+ sqlFactory.addParameter("sa_orderid", sa_orderid);
|
|
|
+ sqlFactory.addParameter("siteid", controller.siteid);
|
|
|
+ sqlFactory.addParameter("siteid", controller.siteid);
|
|
|
+ sqlFactory.addParameter_in("itemclassid", itemclsids);
|
|
|
+ sqlFactory.addParameter_in("itemid", itemids);
|
|
|
+
|
|
|
+ Rows rows = controller.dbConnect.runSqlQuery(sqlFactory);
|
|
|
+ BigDecimal saorderamount = BigDecimal.ZERO;
|
|
|
+ for (Row row : rows) {
|
|
|
+ saorderamount = saorderamount.add(row.getBigDecimal("amount"));
|
|
|
+ }
|
|
|
+ //查询定制费用
|
|
|
+ sql = "SELECT ifnull(sum(costamount),0) costamount from sa_order_cost WHERE sa_orderid=" + sa_orderid;
|
|
|
+ BigDecimal costamount = dbConnect.runSqlQuery(0, sql).getBigDecimal("costamount");
|
|
|
+ saorderamount = saorderamount.add(costamount);
|
|
|
+
|
|
|
+
|
|
|
+ BigDecimal balance = agentdecorationtrebatRows.get(0).getBigDecimal("balance");
|
|
|
+ BigDecimal brate = agentdecorationtrebatRows.get(0).getBigDecimal("brate");
|
|
|
+
|
|
|
+
|
|
|
+ BigDecimal approvedamount = BigDecimal.ZERO;
|
|
|
+ if (balance.compareTo(saorderamount.multiply(brate)) >= 0) {
|
|
|
+ approvedamount = saorderamount.multiply(brate);
|
|
|
+ } else if (balance.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ approvedamount = balance;
|
|
|
+ }
|
|
|
+ System.out.println(saorderamount);
|
|
|
+ System.out.println(approvedamount);
|
|
|
+
|
|
|
+ if (approvedamount.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ ArrayList<Long> sa_orderitemsids = rows.toArrayList("sa_orderitemsid", new ArrayList<>());
|
|
|
+ sa_orderitemsids.add(0l);
|
|
|
+
|
|
|
+ //更新订单明细的装修返利标志
|
|
|
+ UpdateSQL updateSQL = SQLFactory.createUpdateSQL(controller, "sa_orderitems");
|
|
|
+ updateSQL.setWhere("sa_orderitemsid", sa_orderitemsids);
|
|
|
+ updateSQL.setValue("decorationrebateflag", "1");
|
|
|
+ sqlList.add(updateSQL.getSQL());
|
|
|
+
|
|
|
+ //更新sa_agentdecorationtrebate
|
|
|
+ updateSQL = SQLFactory.createUpdateSQL(controller, "sa_agentdecorationtrebate");
|
|
|
+ updateSQL.setWhere("sys_enterpriseid", sys_enterpriseid);
|
|
|
+ updateSQL.setSiteid(controller.siteid);
|
|
|
+ updateSQL.setValue("balance", balance.subtract(approvedamount));
|
|
|
+ sqlList.add(updateSQL.getSQL());
|
|
|
+
|
|
|
+ //生成收入凭证
|
|
|
+ CashbillEntity entity = new CashbillEntity();
|
|
|
+ entity.setAmount(approvedamount);
|
|
|
+ entity.setOwnerid(sa_orderid);
|
|
|
+ entity.setOwnertable("sa_order");
|
|
|
+ entity.setSource("订单自动返利");
|
|
|
+ entity.setSourcenote(orderRows.get(0).getString("sonum"));
|
|
|
+ entity.setRemarks(orderRows.get(0).getString("sonum") + "订单自动返利");
|
|
|
+ JSONObject cashbillIncome = Accountbalance.createCashbillIncome(controller, sys_enterpriseid, sa_accountclassid, entity, true, true);
|
|
|
+ sqlList.addAll(cashbillIncome.getJSONArray("sqlList").toJavaList(String.class));
|
|
|
+
|
|
|
+ dbConnect.runSqlUpdate(sqlList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public JSONArray toJsonArray(String column, List<Row> list) {
|
|
|
+ JSONArray array = new JSONArray();
|
|
|
+ array.add("0");
|
|
|
+ for (Row row : list) {
|
|
|
+ Object value = row.get(column);
|
|
|
+ array.add(value);
|
|
|
+ }
|
|
|
+ return array;
|
|
|
}
|
|
|
|
|
|
public RowsMap CalculateAmount(ArrayList<Long> itemclsids, Long sys_enterpriseid,
|
|
|
@@ -175,24 +193,22 @@ public class OrderItemsHelper extends BaseClass {
|
|
|
Rows rows = null;
|
|
|
itemclsids.addAll(ItemClass.getSubItemClassIds(controller, itemclsids));
|
|
|
itemclsids.add(0l);
|
|
|
- if (itemids.equals("()")) {
|
|
|
- rows = dbConnect.runSqlQuery("select t2.closedate,t2.sys_enterpriseid,t1.sa_orderitemsid from sa_orderitems t1 \n" +
|
|
|
- "inner join sa_order t2 on t1.sa_orderid=t2.sa_orderid and t1.siteid=t2.siteid\n" +
|
|
|
- "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,t1.sa_orderitemsid");
|
|
|
-
|
|
|
- } else {
|
|
|
- rows = dbConnect.runSqlQuery("select t2.closedate,t2.sys_enterpriseid,t1.sa_orderitemsid from sa_orderitems t1 \n" +
|
|
|
- "inner join sa_order t2 on t1.sa_orderid=t2.sa_orderid and t1.siteid=t2.siteid\n" +
|
|
|
- "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,t1.sa_orderitemsid");
|
|
|
-
|
|
|
-
|
|
|
+ String sql = "select t2.closedate,t2.sys_enterpriseid,t1.sa_orderitemsid from sa_orderitems t1 " +
|
|
|
+ "inner join sa_order t2 on t1.sa_orderid=t2.sa_orderid and t1.siteid=t2.siteid " +
|
|
|
+ "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 + "' ";
|
|
|
+ if (!itemids.equals("()")) {
|
|
|
+ sql = sql + " and t1.itemid not in" + itemids;
|
|
|
}
|
|
|
+ sql = sql + "group by t2.sys_enterpriseid,t2.closedate,t1.sa_orderitemsid";
|
|
|
+ System.err.println(sql);
|
|
|
+ rows = dbConnect.runSqlQuery(sql);
|
|
|
return rows.toRowsMap("sys_enterpriseid");
|
|
|
-// if(rows.toJsonArray("sa_orderitemsid").isEmpty()){
|
|
|
-// return "(0)";
|
|
|
-// }else{
|
|
|
-// return rows.toJsonArray("sa_orderitemsid").toJSONString().replace("[", "(").replace("]", ")");
|
|
|
-// }
|
|
|
|
|
|
|
|
|
}
|