|
@@ -1,68 +1,224 @@
|
|
|
package function;
|
|
|
|
|
|
+import beans.order.Order;
|
|
|
import common.BaseClass;
|
|
|
import common.Controller;
|
|
|
import common.DataFunction;
|
|
|
import common.YosException;
|
|
|
import common.annotation.Param;
|
|
|
-import common.data.QuerySQL;
|
|
|
-import common.data.Row;
|
|
|
-import common.data.Rows;
|
|
|
-import common.data.SQLFactory;
|
|
|
+import common.data.*;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
|
/**
|
|
|
* 订单费用计算方案1
|
|
|
*/
|
|
|
public class OrderFeeCalculatePlan1 extends DataFunction {
|
|
|
|
|
|
- @Param(paramname = "站点ID", remarks = "站点ID")
|
|
|
+ @Param(paramname = "站点ID", remarks = "站点ID")
|
|
|
String siteid;
|
|
|
- @Param(paramname = "单位费用",issystem = true, remarks = "单位费用(具体一个方案生成的一个单位费用)")
|
|
|
- Double unitfee;
|
|
|
- @Param(paramname = "订单类型",issystem = true, remarks = "订单类型(可多个(英文逗号隔开),具体在哪些订单类型中适用)")
|
|
|
+ @Param(paramname = "单位费用", issystem = true, remarks = "单位费用(具体一个方案生成的一个单位费用)")
|
|
|
+ String unitfee;
|
|
|
+ @Param(paramname = "订单类型", issystem = true, remarks = "订单类型(可多个(英文逗号隔开),具体在哪些订单类型中适用)")
|
|
|
String ordertype;
|
|
|
- @Param(paramname = "是否订单内组合计算",issystem = true, remarks = "是否订单内组合计算(若组合计算则在订单中自动汇总同方案商品一起计算)")
|
|
|
- boolean iscombination;
|
|
|
- @Param(paramname = "是否累加计算",issystem = true, remarks = "是否累加计算(若累加计算则订单内满足时则生成费用)")
|
|
|
- boolean isaccumulation;
|
|
|
- @Param(paramname = "分摊方式",issystem = true, remarks = "分摊方式(订货数量/订货金额,在组合计算方式下,在订单中同费用方案商品明细行中按比例分摊)")
|
|
|
+ @Param(paramname = "是否订单内组合计算", issystem = true, remarks = "是否订单内组合计算(若组合计算则在订单中自动汇总同方案商品一起计算)")
|
|
|
+ String iscombination;
|
|
|
+ @Param(paramname = "是否累加计算", issystem = true, remarks = "是否累加计算(若累加计算则订单内满足时则生成费用)")
|
|
|
+ String isaccumulation;
|
|
|
+ @Param(paramname = "分摊方式", issystem = true, remarks = "分摊方式(订货数量/订货金额,在组合计算方式下,在订单中同费用方案商品明细行中按比例分摊)")
|
|
|
String apportiontype;
|
|
|
- @Param(paramname = "计算依据",issystem = true, remarks = "计算依据(订货数量、订货重量、订货金额)")
|
|
|
+ @Param(paramname = "计算依据", issystem = true, remarks = "计算依据(订货数量、订货重量、订货金额)")
|
|
|
String itemfield;
|
|
|
- @Param(paramname = "满足条件",issystem = true, remarks = "满足条件(数值,大于等于某个值时产生费用)")
|
|
|
+ @Param(paramname = "满足条件", issystem = true, remarks = "满足条件(数值,大于等于某个值时产生费用)")
|
|
|
String condition;
|
|
|
- @Param(paramname = "数值",issystem = true, remarks = "数值(数值,大于等于某个值时产生费用)")
|
|
|
+ @Param(paramname = "数值", issystem = true, remarks = "数值(数值,大于等于某个值时产生费用)")
|
|
|
String value;
|
|
|
- @Param(paramname = "是否独立扣款",issystem = true, remarks = "是否独立扣款(独立扣款则在生成订单收支凭证时费用,若生成独立的支出凭证则需再选择支出分类)")
|
|
|
- boolean ispay;
|
|
|
- @Param(paramname = "是否可退款",issystem = true, remarks = "是否可退款")
|
|
|
- boolean isreturn;
|
|
|
- @Param(paramname = "订单ID", remarks = "订单ID")
|
|
|
+ @Param(paramname = "是否独立扣款", issystem = true, remarks = "是否独立扣款(独立扣款则在生成订单收支凭证时费用,若生成独立的支出凭证则需再选择支出分类)")
|
|
|
+ String ispay;
|
|
|
+ @Param(paramname = "是否可退款", issystem = true, remarks = "是否可退款")
|
|
|
+ String isreturn;
|
|
|
+ @Param(paramname = "订单ID", remarks = "订单ID")
|
|
|
Long sa_orderid;
|
|
|
|
|
|
@Override
|
|
|
public int action() throws YosException {
|
|
|
-
|
|
|
- Rows rows = queryDataFunction(controller, sa_orderid, this.getClass().getSimpleName());
|
|
|
- if (rows.isEmpty()) {
|
|
|
+ //判断当前订单类型是否满足方案
|
|
|
+ Row orderRow = Order.getOrderRows(controller, sa_orderid).get(0);
|
|
|
+ String type = orderRow.getString("type");
|
|
|
+ if (!ordertype.contains(type)) {
|
|
|
+ return super.action();
|
|
|
+ }
|
|
|
+ //查询订单商品和方案
|
|
|
+ Rows itemRows = queryDataFunction(controller, sa_orderid, this.getClass().getSimpleName());
|
|
|
+ if (itemRows.isEmpty()) {
|
|
|
return super.action();
|
|
|
}
|
|
|
- for (Row row : rows) {
|
|
|
- System.err.println(row.getString("itemid"));
|
|
|
+ BigDecimal bdUnitfee = new BigDecimal(unitfee);
|
|
|
+ BigDecimal bdValue = new BigDecimal(value);
|
|
|
+ ArrayList<String> sqlList = new ArrayList<>();
|
|
|
+
|
|
|
+ //清空费用表
|
|
|
+ DeleteSQL deleteSQL = SQLFactory.createDeleteSQL(controller, "sa_order_cost");
|
|
|
+ deleteSQL.setWhere("sa_orderid", sa_orderid);
|
|
|
+ deleteSQL.setWhere("sa_orderitemsid", itemRows.toArray("sa_orderitemsid"));
|
|
|
+ deleteSQL.delete();
|
|
|
+
|
|
|
+
|
|
|
+ //组合内计算(分摊)
|
|
|
+ if (iscombination.equals("1")) {
|
|
|
+ //分摊总金额
|
|
|
+ BigDecimal totalprice = new BigDecimal(0);
|
|
|
+ //订货数量
|
|
|
+ BigDecimal qty = new BigDecimal(0);
|
|
|
+ //订货重量
|
|
|
+ BigDecimal weight = new BigDecimal(0);
|
|
|
+ //订货金额
|
|
|
+ BigDecimal amount = new BigDecimal(0);
|
|
|
+ for (Row row : itemRows) {
|
|
|
+ qty = qty.add(row.getBigDecimal("qty"));
|
|
|
+ amount = amount.add(row.getBigDecimal("amount"));
|
|
|
+ weight = weight.add(row.getBigDecimal("weight"));
|
|
|
+ }
|
|
|
+ if (itemfield.equals("1")) {
|
|
|
+ totalprice = getTotalprice(bdValue, bdUnitfee, qty);
|
|
|
+ }
|
|
|
+ if (itemfield.equals("2")) {
|
|
|
+ totalprice = getTotalprice(bdValue, bdUnitfee, weight);
|
|
|
+ }
|
|
|
+ if (itemfield.equals("3")) {
|
|
|
+ totalprice = getTotalprice(bdValue, bdUnitfee, amount);
|
|
|
+ }
|
|
|
+ //分摊方式,订货数量
|
|
|
+ if (apportiontype.equals("1")) {
|
|
|
+ for (Row row : itemRows) {
|
|
|
+ BigDecimal itemqty = row.getBigDecimal("qty");
|
|
|
+ if (qty.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ BigDecimal price_apportion = totalprice.multiply(itemqty).divide(qty, 2, BigDecimal.ROUND_HALF_UP);
|
|
|
+ sqlList.add(getInsertSQL(row, price_apportion).getSQL());
|
|
|
+ } else {
|
|
|
+ sqlList.add(getInsertSQL(row, BigDecimal.ZERO).getSQL());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ //分摊方式,订货金额
|
|
|
+ if (apportiontype.equals("2")) {
|
|
|
+ for (Row row : itemRows) {
|
|
|
+ BigDecimal itemamount = row.getBigDecimal("amount");
|
|
|
+ if (amount.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ BigDecimal price_apportion = totalprice.multiply(itemamount).divide(amount, 2, BigDecimal.ROUND_HALF_UP);
|
|
|
+ sqlList.add(getInsertSQL(row, price_apportion).getSQL());
|
|
|
+ } else {
|
|
|
+ sqlList.add(getInsertSQL(row, BigDecimal.ZERO).getSQL());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ dbConnect.runSqlUpdate(sqlList);
|
|
|
+ //调整最后一行数据
|
|
|
+ adjustLastData(itemRows, totalprice);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //不组合内计算(不分摊)
|
|
|
+ if (iscombination.equals("0")) {
|
|
|
+ for (Row row : itemRows) {
|
|
|
+ //分摊总金额
|
|
|
+ BigDecimal totalprice = new BigDecimal(0);
|
|
|
+ //订货数量
|
|
|
+ BigDecimal qty = row.getBigDecimal("qty");
|
|
|
+ //订货重量
|
|
|
+ BigDecimal weight = row.getBigDecimal("weight");
|
|
|
+ //订货金额
|
|
|
+ BigDecimal amount = row.getBigDecimal("amount");
|
|
|
+ if (itemfield.equals("1")) {
|
|
|
+ totalprice = getTotalprice(bdValue, bdUnitfee, qty);
|
|
|
+ }
|
|
|
+ if (itemfield.equals("2")) {
|
|
|
+ totalprice = getTotalprice(bdValue, bdUnitfee, weight);
|
|
|
+ }
|
|
|
+ if (itemfield.equals("3")) {
|
|
|
+ totalprice = getTotalprice(bdValue, bdUnitfee, amount);
|
|
|
+ }
|
|
|
+ sqlList.add(getInsertSQL(row, totalprice).getSQL());
|
|
|
+ }
|
|
|
+ dbConnect.runSqlUpdate(sqlList);
|
|
|
}
|
|
|
- System.err.println("siteid:" + siteid);
|
|
|
- System.err.println("sa_orderid:" + sa_orderid);
|
|
|
+
|
|
|
|
|
|
return super.action();
|
|
|
}
|
|
|
|
|
|
- public static Rows queryDataFunction(Controller controller, Long sa_orderid, String classname) throws YosException {
|
|
|
+ public BigDecimal getTotalprice(BigDecimal bdValue, BigDecimal bdUnitfee, BigDecimal itemFieldValue) {
|
|
|
+ if (itemFieldValue.compareTo(bdValue) <= 0
|
|
|
+ && (condition.equals("1") || condition.equals("2") || condition.equals("3") || condition.equals("4"))) {
|
|
|
+ return bdUnitfee;
|
|
|
+ }
|
|
|
+ //数量,大于
|
|
|
+ if (itemFieldValue.compareTo(bdValue) > 0
|
|
|
+ && condition.equals("5")) {
|
|
|
+ if (bdValue.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ if (isaccumulation.equals("1")) {
|
|
|
+ //累加
|
|
|
+ BigDecimal scale = itemFieldValue.divide(bdValue, 0, BigDecimal.ROUND_HALF_UP);
|
|
|
+ return bdUnitfee.multiply(scale);
|
|
|
+ } else {
|
|
|
+ //不累加
|
|
|
+ return bdUnitfee;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return BigDecimal.valueOf(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ //调整第一行数据
|
|
|
+ public void adjustLastData(Rows itemRows, BigDecimal totalprice) throws YosException {
|
|
|
+ SQLFactory sqlFactory = new SQLFactory(this, "统计订单明细费用");
|
|
|
+ sqlFactory.addParameter_in("sa_orderitemsid", itemRows.toArray("sa_orderitemsid"));
|
|
|
+ Rows costamountrows = dbConnect.runSqlQuery(sqlFactory);
|
|
|
+ if (costamountrows.isNotEmpty()) {
|
|
|
+ BigDecimal costamount = costamountrows.get(0).getBigDecimal("costamount");
|
|
|
+ Long sa_orderitemsid = itemRows.get(0).getLong("sa_orderitemsid");
|
|
|
+ if (costamount.compareTo(totalprice) > 0) {
|
|
|
+ BigDecimal temp = costamount.subtract(totalprice);
|
|
|
+ String sql = "UPDATE sa_order_cost SET costamount=costamount-" + temp + " WHERE sa_orderitemsid= " + sa_orderitemsid;
|
|
|
+ dbConnect.runSqlUpdate(sql);
|
|
|
+ }
|
|
|
+ if (costamount.compareTo(totalprice) < 0) {
|
|
|
+ BigDecimal temp = totalprice.subtract(costamount);
|
|
|
+ String sql = "UPDATE sa_order_cost SET costamount=costamount+" + temp + " WHERE sa_orderitemsid= " + sa_orderitemsid;
|
|
|
+ dbConnect.runSqlUpdate(sql);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public InsertSQL getInsertSQL(Row row, BigDecimal costamount) throws YosException {
|
|
|
+ InsertSQL insertSQL = SQLFactory.createInsertSQL(controller, "sa_order_cost");
|
|
|
+ insertSQL.setUniqueid(controller.createTableID("sa_order_cost"));
|
|
|
+ insertSQL.setValue("siteid", siteid);
|
|
|
+ insertSQL.setValue("sa_orderid", sa_orderid);
|
|
|
+ insertSQL.setValue("sa_orderitemsid", row.getLong("sa_orderitemsid"));
|
|
|
+ insertSQL.setValue("itemid", row.getLong("itemid"));
|
|
|
+ insertSQL.setValue("sys_datafunctionid", row.getLong("sys_datafunctionid"));
|
|
|
+ insertSQL.setValue("functionname", functionname);
|
|
|
+ insertSQL.setValue("costamount", costamount);
|
|
|
+
|
|
|
+ return insertSQL;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Rows queryDataFunction(Controller controller, Long sa_orderid, String classname) throws YosException {
|
|
|
QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_orderitems",
|
|
|
- "sa_orderid", "sa_orderitemsid", "itemid");
|
|
|
+ "sa_orderid", "sa_orderitemsid", "itemid", "qty", "amount");
|
|
|
querySQL.addJoinTable(BaseClass.JOINTYPE.inner, "sys_datafunction_bind", "t2", "t2.ownerid=t1.itemid and t2.ownertable='plm_item'",
|
|
|
"sys_datafunctionid");
|
|
|
querySQL.addJoinTable(BaseClass.JOINTYPE.inner, "sys_datafunction", "t3", "sys_datafunctionid=t2.sys_datafunctionid",
|
|
|
"functionname", "classname");
|
|
|
+ querySQL.addJoinTable(BaseClass.JOINTYPE.inner, "plm_item", "t4", "t4.itemid = t1.itemid",
|
|
|
+ "grossweight", "weight");
|
|
|
querySQL.setWhere("t1.sa_orderid", sa_orderid);
|
|
|
querySQL.setWhere("t3.classname", classname);
|
|
|
querySQL.setTableAlias("t1");
|