Browse Source

返利促销(部分功能更新)

hu 2 years ago
parent
commit
5bf1d5356c

+ 3 - 5
src/custom/restcontroller/sale/cashbill/cashbill.java

@@ -458,9 +458,9 @@ public class cashbill extends Controller {
         }
         QuerySQL querySQL = queryList(where);
         querySQL.setOrderBy(pageSorting);
-        if (!isExport) {
+//        if (!isExport) {
             querySQL.setPage(pageSize, pageNumber);
-        }
+//        }
         Rows rows = querySQL.query();
         ArrayList sa_cashbillids = rows.toArrayList("sa_cashbillid", new ArrayList<>());
         sa_cashbillids.add(0L);
@@ -559,9 +559,7 @@ public class cashbill extends Controller {
         }
         QuerySQL querySQL = queryList(where);
         querySQL.setOrderBy(pageSorting);
-        if (!isExport) {
-            querySQL.setPage(pageSize, pageNumber);
-        }
+        querySQL.setPage(pageSize, pageNumber);
         Rows rows = querySQL.query();
         for (Row row : rows) {
             row.put("amount", row.getBigDecimal("amount"));

+ 3 - 3
src/custom/restcontroller/sale/order/Order.java

@@ -67,9 +67,9 @@ public class Order extends Controller {
         QuerySQL querySQL = queryList(where.toString());
         querySQL.setOrderBy(pageSorting);
         Rows rows;
-        if (!isExport) {
-            querySQL.setPage(pageSize, pageNumber);
-        }
+//        if (!isExport) {
+        querySQL.setPage(pageSize, pageNumber);
+//        }
         rows = querySQL.query();
 
         //查询数据:amount,defaultamount,qty

+ 2 - 3
src/custom/restcontroller/webmanage/sale/dispatch/dispatchItems.java

@@ -346,9 +346,8 @@ public class dispatchItems extends Controller {
         QuerySQL querySQL = queryListManage(where.toString());
         querySQL.setOrderBy(pageSorting);
         Rows rows;
-        if (!isExport) {
-            querySQL.setPage(pageSize, pageNumber);
-        }
+        querySQL.setPage(pageSize, pageNumber);
+
         rows = querySQL.query();
 
         SQLFactory itemsqlFactory = new SQLFactory(this, "发货单明细关联商品列表查询");

+ 1 - 3
src/custom/restcontroller/webmanage/sale/item/Item.java

@@ -415,9 +415,7 @@ public class Item extends Controller {
 //        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
         QuerySQL querySQL = queryList(where.toString());
         querySQL.setOrderBy(pageSorting);
-        if (!isExport) {
-            querySQL.setPage(pageSize, pageNumber);
-        }
+        querySQL.setPage(pageSize, pageNumber);
         Rows rows = querySQL.query();
         // 默认商品图片
         Rows defaultImageRows = getAttachmentUrl("system", (long) 1, "defaultImage");

+ 143 - 88
src/custom/restcontroller/webmanage/sale/order/Order.java

@@ -646,7 +646,6 @@ public class Order extends Controller {
                 }
             }
         }
-        boolean isExport = content.getBooleanValue("isExport");
 //        SQLFactory sqlFactory = new SQLFactory(this, "订单(管理端)_列表", pageSize, pageNumber, pageSorting);
 //        if (isExport) {
 //            sqlFactory = new SQLFactory(this, "订单(管理端)_列表");
@@ -659,9 +658,7 @@ public class Order extends Controller {
         QuerySQL querySQL = queryListManage(where.toString());
         querySQL.setOrderBy(pageSorting);
         Rows rows;
-        if (!isExport) {
-            querySQL.setPage(pageSize, pageNumber);
-        }
+        querySQL.setPage(pageSize, pageNumber);
         rows = querySQL.query();
 
         ArrayList<Long> ids = rows.toArrayList("sa_orderid", new ArrayList<>());
@@ -789,6 +786,8 @@ public class Order extends Controller {
                 "enterprisename");
         querySQL.addJoinTable(JOINTYPE.left, "sa_agents", "t6", "t6.sys_enterpriseid=t1.sys_enterpriseid and t6.siteid = t1.siteid",
                 "agentnum");
+        querySQL.addJoinTable(JOINTYPE.left, "sa_promotion", "t7", "t7.sa_promotionid=t1.sa_promotionid and t7.siteid = t1.siteid",
+                "promnum","promname");
         querySQL.addQueryFields("amount", "CAST(0 AS CHAR(20))");
         querySQL.addQueryFields("qty", "CAST(0 AS CHAR(20))");
         querySQL.setWhere("t1.siteid", siteid);
@@ -908,9 +907,9 @@ public class Order extends Controller {
         QuerySQL querySQL = queryList(where.toString());
         querySQL.setOrderBy(pageSorting);
         Rows rows;
-        if (!isExport) {
-            querySQL.setPage(pageSize, pageNumber);
-        }
+//        if (!isExport) {
+        querySQL.setPage(pageSize, pageNumber);
+//        }
         rows = querySQL.query();
         //查询数据:amount,defaultamount,qty
         ArrayList<Long> sa_orderids = rows.toArrayList("sa_orderid", new ArrayList<>());
@@ -1239,16 +1238,40 @@ public class Order extends Controller {
             }
 
             //促销订单经销商限购金额判断
-            Rows promotion_auth = dbConnect.runSqlQuery(" select * from sa_promotion_auth where sys_enterpriseid="+sys_enterpriseid+" and sa_promotionid="+sa_promotionid);
+            String promotiontype="";
+            BigDecimal associationamount=BigDecimal.ZERO;
+            Rows promotion = dbConnect.runSqlQuery(" select * from sa_promotion where siteid='"+siteid+"'and  sa_promotionid="+sa_promotionid);
+            Rows promotion_auth = dbConnect.runSqlQuery(" select * from sa_promotion_auth where siteid='"+siteid+"'and sys_enterpriseid="+sys_enterpriseid+" and sa_promotionid="+sa_promotionid);
+            if(promotion.isNotEmpty()){
+                promotiontype=promotion.get(0).getString("type");
+                associationamount=promotion.get(0).getBigDecimal("associationamount");
+            }
             if(promotion_auth.isNotEmpty()){
                 boolean islimit = promotion_auth.get(0).getBoolean("islimit");
                 BigDecimal limitamount = promotion_auth.get(0).getBigDecimal("limitamount");
                 BigDecimal saledamount = promotion_auth.get(0).getBigDecimal("saledamount");
+                 System.out.println("limitamount:"+limitamount);
+                System.out.println("saledamount:"+saledamount);
+                System.out.println("associationamount:"+associationamount);
                 if(islimit){
-                    if(amount.compareTo(limitamount.subtract(saledamount))>0){
-                        return getErrReturnObject().setErrMsg("已超过促销方案限定金额,无法提交").toString();
+                    if(!promotiontype.equals("返利促销")){
+                        if(amount.compareTo(limitamount.subtract(saledamount))>0){
+                            return getErrReturnObject().setErrMsg("已超过促销方案限定金额,无法提交").toString();
+                        }
+                        sqlList.add("update sa_promotion_auth set saledamount=saledamount+" + amount + " where sa_promotion_authid=" + promotion_auth.get(0).getLong("sa_promotion_authid"));
+                    }else{
+                        if(amount.subtract((limitamount.subtract(saledamount))).compareTo(associationamount)>0){
+                            return getErrReturnObject().setErrMsg("已超过促销方案限定金额,无法提交").toString();
+                        }
+                        if(amount.compareTo((limitamount.subtract(saledamount)))>0){
+                            sqlList.add("update sa_promotion_auth set saledamount=saledamount+" + (limitamount.subtract(saledamount)) + " where sa_promotion_authid=" + promotion_auth.get(0).getLong("sa_promotion_authid"));
+                        }else{
+                            sqlList.add("update sa_promotion_auth set saledamount=saledamount+" + amount + " where sa_promotion_authid=" + promotion_auth.get(0).getLong("sa_promotion_authid"));
+                        }
+
                     }
-                    sqlList.add("update sa_promotion_auth set saledamount=saledamount+" + amount + " where sa_promotion_authid=" + promotion_auth.get(0).getLong("sa_promotion_authid"));
+
+
                 }
             }
         }
@@ -2825,7 +2848,7 @@ public class Order extends Controller {
         if (orderRow.isEmpty()) {
             return getErrReturnObject().setErrMsg("订单不存在").toString();
         }
-        if (orderRow.get(0).getString("status").equals("关闭")) {
+        if (orderRow.get(0).getString("status").equals("手工关闭")) {
             return getErrReturnObject().setErrMsg("订单已关闭,无法再次手工关闭").toString();
         }
         if (!orderRow.get(0).getString("status").equals("审核")) {
@@ -2845,7 +2868,24 @@ public class Order extends Controller {
         Rows rows = dbConnect.runSqlQuery("SELECT sum(undeliqty*price) totalprice from sa_orderitems WHERE sa_orderid=" + sa_orderid + " and siteid = '" + siteid + "'");
         if (rows.isNotEmpty()) {
             BigDecimal totalprice = rows.get(0).getBigDecimal("totalprice");
-            if (totalprice.compareTo(BigDecimal.ZERO) > 0) {
+            JSONArray sa_accountclassinfos = orderRow.get(0).getJSONArray("sa_accountclassinfos");
+            JSONObject orderaccountclassinfo = new JSONObject();
+            JSONObject promotionaccountclassinfo = new JSONObject();
+            for (Object object:sa_accountclassinfos) {
+                JSONObject jsonObject=(JSONObject)object;
+                if(jsonObject.getLong("sa_accountclassid")==sa_accountclassid){
+                    orderaccountclassinfo=jsonObject;
+                }else{
+                    promotionaccountclassinfo=jsonObject;
+                }
+            }
+            Rows promotion_auth = dbConnect.runSqlQuery(" select * from sa_promotion_auth where siteid='"+siteid+"'and sys_enterpriseid="+sys_enterpriseid+" and sa_promotionid="+ orderRow.get(0).getLong("sa_promotionid"));
+            long sa_promotion_authid=0;
+            if(promotion_auth.isNotEmpty()){
+                sa_promotion_authid=promotion_auth.get(0).getLong("sa_promotion_authid");
+            }
+            if(orderaccountclassinfo.getBigDecimal("amount").compareTo(totalprice)>=0){
+                 System.out.println("1111");
                 CashbillEntity entity = new CashbillEntity();
                 entity.setAmount(totalprice.negate());
                 entity.setSource("订单手工关闭");
@@ -2854,7 +2894,32 @@ public class Order extends Controller {
                 entity.setRemarks("由" + orderRow.get(0).getString("sonum") + "订单手工关闭时自动产生");
                 entity.setType("普通费用");
                 entity.setTypemx("订单关闭退款");
-                sqlList.addAll(Accountbalance.createCashbillPay(this, sys_enterpriseid, sa_accountclassid, entity, true));
+                sqlList.addAll(Accountbalance.createCashbillPay(this, sys_enterpriseid, orderaccountclassinfo.getLongValue("sa_accountclassid"), entity, true));
+                sqlList.add("update sa_promotion_auth set saledamount=saledamount-" + totalprice + " where sa_promotion_authid=" + sa_promotion_authid);
+            }else if(orderaccountclassinfo.getBigDecimal("amount").compareTo(totalprice)<0){
+                System.out.println("222");
+                CashbillEntity entity = new CashbillEntity();
+                entity.setAmount(orderaccountclassinfo.getBigDecimal("amount").negate());
+                entity.setSource("订单手工关闭");
+                entity.setOwnerid(sa_orderid);
+                entity.setOwnertable("sa_order");
+                entity.setRemarks("由" + orderRow.get(0).getString("sonum") + "订单手工关闭时自动产生");
+                entity.setType("普通费用");
+                entity.setTypemx("订单关闭退款");
+                sqlList.addAll(Accountbalance.createCashbillPay(this, sys_enterpriseid, orderaccountclassinfo.getLongValue("sa_accountclassid"), entity, true));
+
+                sqlList.add("update sa_promotion_auth set saledamount=saledamount-" + orderaccountclassinfo.getBigDecimal("amount") + " where sa_promotion_authid=" + sa_promotion_authid);
+
+                CashbillEntity  promotionentity = new CashbillEntity();
+                promotionentity.setAmount((totalprice.subtract(orderaccountclassinfo.getBigDecimal("amount"))).negate());
+                promotionentity.setSource("订单手工关闭");
+                promotionentity.setOwnerid(sa_orderid);
+                promotionentity.setOwnertable("sa_order");
+                promotionentity.setRemarks("由" + orderRow.get(0).getString("sonum") + "订单手工关闭时自动产生");
+                promotionentity.setType("普通费用");
+                promotionentity.setTypemx("订单关闭退款");
+                sqlList.addAll(Accountbalance.createCashbillPay(this, sys_enterpriseid,  promotionaccountclassinfo.getLongValue("sa_accountclassid"),  promotionentity, true));
+
             }
 
         }
@@ -2886,92 +2951,82 @@ public class Order extends Controller {
 
         Rows orderRows = getOrderRows(this, sa_orderid);
         ArrayList<String> sqlList = new ArrayList<>();
-
-//        if (orderRows.isEmpty()) {
-//             System.out.println(111111);
-//            return sqlList;
-//        }
         JSONArray jsonArray = new JSONArray();
-        BigDecimal orderSumamount = BigDecimal.ZERO;
-        Rows classamountrows = dbConnect.runSqlQuery("select t2.marketingcategory topitemclassid,t1.siteid,sum(ifnull(t1.amount,0)) sumamount  from sa_orderitems t1 inner join plm_item t2 on t1.itemid=t2.itemid and t1.siteid=t2.siteid   where t1.sa_orderid='" + sa_orderid + "'  and t1.siteid='" + siteid + "' group by t2.marketingcategory,t1.siteid");
-        for (Row classamountrow : classamountrows) {
-            orderSumamount = orderSumamount.add(classamountrow.getBigDecimal("sumamount"));
-        }
-        if (orderSumamount.compareTo(getAmount(this, sa_orderid)) != 0) {
-            System.out.println(22222);
+        JSONObject jsonObject = new JSONObject();
+        BigDecimal orderamount =  getAmount(this, sa_orderid);
+        Rows accountclassrows = dbConnect.runSqlQuery("select t1.*,ifnull(t2.balance,0) balance,ifnull(t2.creditquota,0) creditquota from sa_accountclass t1 left join sa_accountbalance t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid and t2.sys_enterpriseid=" + (sys_enterpriseid == 0 ? orderRows.get(0).getLong("pay_enterpriseid") : sys_enterpriseid) + " where isused=1 and t1.siteid='" + siteid + "' and t1.sa_accountclassid="+(sa_accountclassid == 0 ? orderRows.get(0).getLong("sa_accountclassid") : sa_accountclassid));
+        if (orderRows.isEmpty()) {
+            //全部优先账户扣款
+            jsonObject.put("amount", orderamount);
+            jsonObject.put("sa_accountclassid", accountclassrows.get(0).getLong("sa_accountclassid"));
+            jsonObject.put("accountname", accountclassrows.get(0).getString("accountname"));
+            jsonObject.put("balance", accountclassrows.get(0).getBigDecimal("balance"));
+            jsonObject.put("creditquota", accountclassrows.get(0).getBigDecimal("creditquota"));
+            jsonArray.add(jsonObject);
+            sqlList.add("update sa_order set sa_accountclassinfos='" + jsonArray + "' where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
             return sqlList;
         }
-
-        RowsMap classamountrowsMap = classamountrows.toRowsMap("topitemclassid");
-        Rows accountclassrows = dbConnect.runSqlQuery("select t1.*,ifnull(t2.balance,0) balance,ifnull(t2.creditquota,0) creditquota from sa_accountclass t1 left join sa_accountbalance t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid and t2.sys_enterpriseid=" + (sys_enterpriseid == 0 ? orderRows.get(0).getLong("pay_enterpriseid") : sys_enterpriseid) + " where isused=1 and t1.siteid='" + siteid + "'");
-        BigDecimal firstpayamount = BigDecimal.ZERO;
-
-
-        if (!accountclassrows.isEmpty()) {
-            for (Row row : accountclassrows) {
-                BigDecimal sumamount = BigDecimal.ZERO;
-                JSONObject jsonObject = new JSONObject();
-                if (row.getJSONArray("bindingcategories") != null && !row.getJSONArray("bindingcategories").isEmpty()) {
-                    for (Object object : row.getJSONArray("bindingcategories")) {
-                        Long topitemclassid = Long.valueOf(object.toString());
-                        if (classamountrowsMap.containsKey(object.toString())) {
-                            if (classamountrowsMap.get(object.toString()).isNotEmpty()) {
-                                sumamount = sumamount.add(classamountrowsMap.get(object.toString()).get(0).getBigDecimal("sumamount").multiply(row.getBigDecimal("maximumdiscount")).setScale(2, BigDecimal.ROUND_HALF_UP));
-                            }
-                        }
-                    }
-                }
-                if (sumamount.compareTo(BigDecimal.ZERO) > 0) {
-                    if ((row.getBigDecimal("balance").add(row.getBigDecimal("creditquota"))).compareTo(BigDecimal.ZERO) <= 0) {
-                        continue;
-                    } else if ((row.getBigDecimal("balance").add(row.getBigDecimal("creditquota"))).compareTo(sumamount) < 0) {
-                        //部分优先账户扣款,部分现金扣款
-                        jsonObject.put("amount", row.getBigDecimal("balance").add(row.getBigDecimal("creditquota")));
-                        jsonObject.put("sa_accountclassid", row.getLong("sa_accountclassid"));
-                        jsonObject.put("accountname", row.getString("accountname"));
-                        jsonObject.put("balance", row.getBigDecimal("balance"));
-                        jsonObject.put("creditquota", row.getBigDecimal("creditquota"));
+        String type="";
+        Rows promotionrows =dbConnect.runSqlQuery("select * from sa_promotion where siteid='"+siteid+"' and  sa_promotionid="+ orderRows.get(0).getLong("sa_promotionid"));
+        Rows promotionenterpriserows =dbConnect.runSqlQuery("select * from sa_promotion_auth where siteid='"+siteid+"'and sys_enterpriseid="+(sys_enterpriseid == 0 ? orderRows.get(0).getLong("pay_enterpriseid") : sys_enterpriseid)+" and  sa_promotionid="+ orderRows.get(0).getLong("sa_promotionid"));
+        if(promotionrows.isNotEmpty()){
+            type=promotionrows.get(0).getString("type");
+        }
+        if(type.equals("返利促销")){
+            BigDecimal associationamount = promotionrows.get(0).getBigDecimal("associationamount");
+            long associationaccountclassid = promotionrows.get(0).getLong("associationaccountclassid");
+            Rows associationaccountclassrows = dbConnect.runSqlQuery("select t1.*,ifnull(t2.balance,0) balance,ifnull(t2.creditquota,0) creditquota from sa_accountclass t1 left join sa_accountbalance t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid and t2.sys_enterpriseid=" + (sys_enterpriseid == 0 ? orderRows.get(0).getLong("pay_enterpriseid") : sys_enterpriseid) + " where isused=1 and t1.siteid='" + siteid + "' and t1.sa_accountclassid="+associationaccountclassid);
+            if(promotionenterpriserows.isNotEmpty()){
+                if(promotionenterpriserows.get(0).getBoolean("islimit")){
+                    BigDecimal limitamount = promotionenterpriserows.get(0).getBigDecimal("limitamount");
+                    BigDecimal saledamount = promotionenterpriserows.get(0).getBigDecimal("saledamount");
+                    if(orderamount.compareTo(limitamount.subtract(saledamount))<=0){
+                        //全部优先账户扣款
+                        jsonObject.put("amount", orderamount);
+                        jsonObject.put("sa_accountclassid", accountclassrows.get(0).getLong("sa_accountclassid"));
+                        jsonObject.put("accountname", accountclassrows.get(0).getString("accountname"));
+                        jsonObject.put("balance", accountclassrows.get(0).getBigDecimal("balance"));
+                        jsonObject.put("creditquota", accountclassrows.get(0).getBigDecimal("creditquota"));
                         jsonArray.add(jsonObject);
-                        firstpayamount = firstpayamount.add(row.getBigDecimal("balance").add(row.getBigDecimal("creditquota")));
-
-                    } else if ((row.getBigDecimal("balance").add(row.getBigDecimal("creditquota"))).compareTo(sumamount) >= 0) {
+                    }else if(orderamount.compareTo(limitamount.subtract(saledamount))>0){
                         //全部优先账户扣款
-                        jsonObject.put("amount", sumamount);
-                        jsonObject.put("sa_accountclassid", row.getLong("sa_accountclassid"));
-                        jsonObject.put("accountname", row.getString("accountname"));
-                        jsonObject.put("balance", row.getBigDecimal("balance"));
-                        jsonObject.put("creditquota", row.getBigDecimal("creditquota"));
+                        jsonObject.put("amount", limitamount.subtract(saledamount));
+                        jsonObject.put("sa_accountclassid", accountclassrows.get(0).getLong("sa_accountclassid"));
+                        jsonObject.put("accountname", accountclassrows.get(0).getString("accountname"));
+                        jsonObject.put("balance", accountclassrows.get(0).getBigDecimal("balance"));
+                        jsonObject.put("creditquota", accountclassrows.get(0).getBigDecimal("creditquota"));
                         jsonArray.add(jsonObject);
-                        firstpayamount = firstpayamount.add(sumamount);
+
+                        JSONObject associationjsonObject = new JSONObject();
+                        associationjsonObject.put("amount", orderamount.subtract((limitamount.subtract(saledamount))));
+                        associationjsonObject.put("sa_accountclassid", associationaccountclassrows.get(0).getLong("sa_accountclassid"));
+                        associationjsonObject.put("accountname", associationaccountclassrows.get(0).getString("accountname"));
+                        associationjsonObject.put("balance", associationaccountclassrows.get(0).getBigDecimal("balance"));
+                        associationjsonObject.put("creditquota", associationaccountclassrows.get(0).getBigDecimal("creditquota"));
+                        jsonArray.add(associationjsonObject);
+                    }else{
+                        return sqlList;
                     }
                 }
-            }
-        }
-        if (getAmount(this, sa_orderid).compareTo(BigDecimal.ZERO) == 0) {
-            Rows xianjinaccountclassrows = dbConnect.runSqlQuery("select t1.*,ifnull(t2.balance,0) balance,ifnull(t2.creditquota,0) creditquota from sa_accountclass t1 left join sa_accountbalance t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid and t2.sys_enterpriseid=" + (sys_enterpriseid == 0 ? orderRows.get(0).getLong("pay_enterpriseid") : sys_enterpriseid) + " where isorder=1 and isused=1 and t1.sa_accountclassid=" + (sa_accountclassid == 0 ? orderRows.get(0).getLong("sa_accountclassid") : sa_accountclassid) + " and t1.siteid='" + siteid + "'");
-            if (xianjinaccountclassrows.isNotEmpty()) {
-                JSONObject jsonObject = new JSONObject();
-                jsonObject.put("amount", BigDecimal.ZERO);
-                jsonObject.put("sa_accountclassid", xianjinaccountclassrows.get(0).getLong("sa_accountclassid"));
-                jsonObject.put("accountname", xianjinaccountclassrows.get(0).getString("accountname"));
-                jsonObject.put("balance", xianjinaccountclassrows.get(0).getBigDecimal("balance"));
-                jsonObject.put("creditquota", xianjinaccountclassrows.get(0).getBigDecimal("creditquota"));
+            }else{
+                //全部优先账户扣款
+                jsonObject.put("amount", orderamount);
+                jsonObject.put("sa_accountclassid", accountclassrows.get(0).getLong("sa_accountclassid"));
+                jsonObject.put("accountname", accountclassrows.get(0).getString("accountname"));
+                jsonObject.put("balance", accountclassrows.get(0).getBigDecimal("balance"));
+                jsonObject.put("creditquota", accountclassrows.get(0).getBigDecimal("creditquota"));
                 jsonArray.add(jsonObject);
             }
+        }else{
+            //全部优先账户扣款
+            jsonObject.put("amount", orderamount);
+            jsonObject.put("sa_accountclassid", accountclassrows.get(0).getLong("sa_accountclassid"));
+            jsonObject.put("accountname", accountclassrows.get(0).getString("accountname"));
+            jsonObject.put("balance", accountclassrows.get(0).getBigDecimal("balance"));
+            jsonObject.put("creditquota", accountclassrows.get(0).getBigDecimal("creditquota"));
+            jsonArray.add(jsonObject);
         }
 
-        if (orderSumamount.compareTo(firstpayamount) > 0) {
-            Rows xianjinaccountclassrows = dbConnect.runSqlQuery("select t1.*,ifnull(t2.balance,0) balance,ifnull(t2.creditquota,0) creditquota from sa_accountclass t1 left join sa_accountbalance t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid and t2.sys_enterpriseid=" + (sys_enterpriseid == 0 ? orderRows.get(0).getLong("pay_enterpriseid") : sys_enterpriseid) + " where isorder=1 and isused=1 and t1.sa_accountclassid=" + (sa_accountclassid == 0 ? orderRows.get(0).getLong("sa_accountclassid") : sa_accountclassid) + " and t1.siteid='" + siteid + "'");
-            if (xianjinaccountclassrows.isNotEmpty()) {
-                JSONObject jsonObject = new JSONObject();
-                jsonObject.put("amount", orderSumamount.subtract(firstpayamount));
-                jsonObject.put("sa_accountclassid", xianjinaccountclassrows.get(0).getLong("sa_accountclassid"));
-                jsonObject.put("accountname", xianjinaccountclassrows.get(0).getString("accountname"));
-                jsonObject.put("balance", xianjinaccountclassrows.get(0).getBigDecimal("balance"));
-                jsonObject.put("creditquota", xianjinaccountclassrows.get(0).getBigDecimal("creditquota"));
-                jsonArray.add(jsonObject);
-            }
-        }
         sqlList.add("update sa_order set sa_accountclassinfos='" + jsonArray + "' where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
         return sqlList;
     }

+ 63 - 83
src/custom/restcontroller/webmanage/sale/order/OrderItems.java

@@ -120,8 +120,8 @@ public class OrderItems extends Controller {
                 }
             } else {
                 Rows rows = dbConnect.runSqlQuery("SELECT sa_orderitemsid,price from sa_orderitems WHERE siteid='" + siteid + "' and sa_orderitemsid=" + sa_orderitemsid);
-                if(rows.isNotEmpty()){
-                    price1 =rows.get(0).getBigDecimal("price");
+                if (rows.isNotEmpty()) {
+                    price1 = rows.get(0).getBigDecimal("price");
                 }
                 isInsert = false;
             }
@@ -185,98 +185,82 @@ public class OrderItems extends Controller {
     /**
      * 更新订单扣款信息
      */
-    public void updateAccountclassinfos(Long sa_orderid) throws YosException {
+    public String updateAccountclassinfos(Long sa_orderid) throws YosException {
 
         Rows orderRows = getOrderRows(this, sa_orderid);
 
 
         if (orderRows.isEmpty()) {
-            return;
-        }
-        JSONArray jsonArray = new JSONArray();
-        BigDecimal orderSumamount = BigDecimal.ZERO;
-        Rows classamountrows = dbConnect.runSqlQuery("select t2.marketingcategory topitemclassid,t1.siteid,sum(ifnull(t1.amount,0)) sumamount  from sa_orderitems t1 inner join plm_item t2 on t1.itemid=t2.itemid and t1.siteid=t2.siteid   where t1.sa_orderid='" + sa_orderid + "' and t1.siteid='" + siteid + "' group by t2.marketingcategory,t1.siteid");
-        for (Row classamountrow : classamountrows) {
-            orderSumamount = orderSumamount.add(classamountrow.getBigDecimal("sumamount"));
+            return "订单不存在";
         }
-        if (orderSumamount.compareTo(getAmount(this, sa_orderid)) != 0) {
-            return;
+        String type="";
+        Rows promotionrows =dbConnect.runSqlQuery("select * from sa_promotion where siteid='"+siteid+"' and  sa_promotionid="+ orderRows.get(0).getLong("sa_promotionid"));
+        Rows promotionenterpriserows =dbConnect.runSqlQuery("select * from sa_promotion_auth where siteid='"+siteid+"'and sys_enterpriseid="+orderRows.get(0).getLong("pay_enterpriseid")+" and  sa_promotionid="+ orderRows.get(0).getLong("sa_promotionid"));
+        if(promotionrows.isNotEmpty()){
+            type=promotionrows.get(0).getString("type");
         }
-
-        RowsMap classamountrowsMap = classamountrows.toRowsMap("topitemclassid");
-        Rows accountclassrows = dbConnect.runSqlQuery("select t1.*,ifnull(t2.balance,0) balance,ifnull(t2.creditquota,0) creditquota from sa_accountclass t1 left join sa_accountbalance t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid and t2.sys_enterpriseid=" + orderRows.get(0).getLong("pay_enterpriseid") + " where isused=1 and t1.siteid='" + siteid + "'");
-        BigDecimal firstpayamount = BigDecimal.ZERO;
-
-
-        if (!accountclassrows.isEmpty()) {
-            for (Row row : accountclassrows) {
-                BigDecimal sumamount = BigDecimal.ZERO;
-                JSONObject jsonObject = new JSONObject();
-                if (row.getJSONArray("bindingcategories") != null && !row.getJSONArray("bindingcategories").isEmpty()) {
-                    for (Object object : row.getJSONArray("bindingcategories")) {
-                        Long topitemclassid = Long.valueOf(object.toString());
-                        if (classamountrowsMap.containsKey(object.toString())) {
-                            if (classamountrowsMap.get(object.toString()).isNotEmpty()) {
-                                sumamount = sumamount.add(classamountrowsMap.get(object.toString()).get(0).getBigDecimal("sumamount").multiply(row.getBigDecimal("maximumdiscount")).setScale(2, BigDecimal.ROUND_HALF_UP));
-                            }
-                        }
-                    }
-                }
-                if (sumamount.compareTo(BigDecimal.ZERO) > 0) {
-                    if ((row.getBigDecimal("balance").add(row.getBigDecimal("creditquota"))).compareTo(BigDecimal.ZERO) <= 0) {
-                        continue;
-                    } else if ((row.getBigDecimal("balance").add(row.getBigDecimal("creditquota"))).compareTo(sumamount) < 0) {
-                        //部分优先账户扣款,部分现金扣款
-                        jsonObject.put("amount", row.getBigDecimal("balance").add(row.getBigDecimal("creditquota")));
-                        jsonObject.put("sa_accountclassid", row.getLong("sa_accountclassid"));
-                        jsonObject.put("accountname", row.getString("accountname"));
-                        jsonObject.put("balance", row.getBigDecimal("balance"));
-                        jsonObject.put("creditquota", row.getBigDecimal("creditquota"));
+        JSONArray jsonArray = new JSONArray();
+        BigDecimal orderamount =  getAmount(this, sa_orderid);
+        System.out.println(orderamount);
+        Rows accountclassrows = dbConnect.runSqlQuery("select t1.*,ifnull(t2.balance,0) balance,ifnull(t2.creditquota,0) creditquota from sa_accountclass t1 left join sa_accountbalance t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid and t2.sys_enterpriseid=" + orderRows.get(0).getLong("pay_enterpriseid") + " where isused=1 and t1.siteid='" + siteid + "' and t1.sa_accountclassid="+orderRows.get(0).getLong("sa_accountclassid"));
+        JSONObject jsonObject = new JSONObject();
+        if(type.equals("返利促销")){
+            BigDecimal associationamount = promotionrows.get(0).getBigDecimal("associationamount");
+            long associationaccountclassid = promotionrows.get(0).getLong("associationaccountclassid");
+            Rows associationaccountclassrows = dbConnect.runSqlQuery("select t1.*,ifnull(t2.balance,0) balance,ifnull(t2.creditquota,0) creditquota from sa_accountclass t1 left join sa_accountbalance t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid and t2.sys_enterpriseid=" + orderRows.get(0).getLong("pay_enterpriseid") + " where isused=1 and t1.siteid='" + siteid + "' and t1.sa_accountclassid="+associationaccountclassid);
+            if(promotionenterpriserows.isNotEmpty()){
+                if(promotionenterpriserows.get(0).getBoolean("islimit")){
+                    BigDecimal limitamount = promotionenterpriserows.get(0).getBigDecimal("limitamount");
+                    BigDecimal saledamount = promotionenterpriserows.get(0).getBigDecimal("saledamount");
+                    if(orderamount.compareTo(limitamount.subtract(saledamount))<=0){
+                        //全部优先账户扣款
+                        jsonObject.put("amount", orderamount);
+                        jsonObject.put("sa_accountclassid", accountclassrows.get(0).getLong("sa_accountclassid"));
+                        jsonObject.put("accountname", accountclassrows.get(0).getString("accountname"));
+                        jsonObject.put("balance", accountclassrows.get(0).getBigDecimal("balance"));
+                        jsonObject.put("creditquota", accountclassrows.get(0).getBigDecimal("creditquota"));
                         jsonArray.add(jsonObject);
-                        firstpayamount = firstpayamount.add(row.getBigDecimal("balance").add(row.getBigDecimal("creditquota")));
-
-                    } else if ((row.getBigDecimal("balance").add(row.getBigDecimal("creditquota"))).compareTo(sumamount) >= 0) {
+                    }else if(orderamount.compareTo(limitamount.subtract(saledamount))>0){
                         //全部优先账户扣款
-                        jsonObject.put("amount", sumamount);
-                        jsonObject.put("sa_accountclassid", row.getLong("sa_accountclassid"));
-                        jsonObject.put("accountname", row.getString("accountname"));
-                        jsonObject.put("balance", row.getBigDecimal("balance"));
-                        jsonObject.put("creditquota", row.getBigDecimal("creditquota"));
+                        jsonObject.put("amount", limitamount.subtract(saledamount));
+                        jsonObject.put("sa_accountclassid", accountclassrows.get(0).getLong("sa_accountclassid"));
+                        jsonObject.put("accountname", accountclassrows.get(0).getString("accountname"));
+                        jsonObject.put("balance", accountclassrows.get(0).getBigDecimal("balance"));
+                        jsonObject.put("creditquota", accountclassrows.get(0).getBigDecimal("creditquota"));
                         jsonArray.add(jsonObject);
-                        firstpayamount = firstpayamount.add(sumamount);
+
+                        JSONObject associationjsonObject = new JSONObject();
+                        associationjsonObject.put("amount", orderamount.subtract((limitamount.subtract(saledamount))));
+                        associationjsonObject.put("sa_accountclassid", associationaccountclassrows.get(0).getLong("sa_accountclassid"));
+                        associationjsonObject.put("accountname", associationaccountclassrows.get(0).getString("accountname"));
+                        associationjsonObject.put("balance", associationaccountclassrows.get(0).getBigDecimal("balance"));
+                        associationjsonObject.put("creditquota", associationaccountclassrows.get(0).getBigDecimal("creditquota"));
+                        jsonArray.add(associationjsonObject);
+                    }else{
+                        return "未知错误,请联系管理员";
                     }
                 }
-            }
-        }
-
-        if (getAmount(this, sa_orderid).compareTo(BigDecimal.ZERO) == 0) {
-            Rows xianjinaccountclassrows = dbConnect.runSqlQuery("select t1.*,ifnull(t2.balance,0) balance,ifnull(t2.creditquota,0) creditquota from sa_accountclass t1 left join sa_accountbalance t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid and t2.sys_enterpriseid=" + orderRows.get(0).getLong("pay_enterpriseid") + " where isorder=1 and isused=1 and t1.sa_accountclassid=" + orderRows.get(0).getLong("sa_accountclassid") + " and t1.siteid='" + siteid + "'");
-            if (xianjinaccountclassrows.isNotEmpty()) {
-                JSONObject jsonObject = new JSONObject();
-                jsonObject.put("amount", BigDecimal.ZERO);
-                jsonObject.put("sa_accountclassid", xianjinaccountclassrows.get(0).getLong("sa_accountclassid"));
-                jsonObject.put("accountname", xianjinaccountclassrows.get(0).getString("accountname"));
-                jsonObject.put("balance", xianjinaccountclassrows.get(0).getBigDecimal("balance"));
-                jsonObject.put("creditquota", xianjinaccountclassrows.get(0).getBigDecimal("creditquota"));
-                jsonArray.add(jsonObject);
-            }
-        }
-
-        if (orderSumamount.compareTo(firstpayamount) > 0) {
-
-            Rows xianjinaccountclassrows = dbConnect.runSqlQuery("select t1.*,ifnull(t2.balance,0) balance,ifnull(t2.creditquota,0) creditquota from sa_accountclass t1 left join sa_accountbalance t2 on t1.sa_accountclassid=t2.sa_accountclassid and t1.siteid=t2.siteid and t2.sys_enterpriseid=" + orderRows.get(0).getLong("pay_enterpriseid") + " where isorder=1 and isused=1 and t1.sa_accountclassid=" + orderRows.get(0).getLong("sa_accountclassid") + " and t1.siteid='" + siteid + "'");
-            if (xianjinaccountclassrows.isNotEmpty()) {
-                JSONObject jsonObject = new JSONObject();
-                jsonObject.put("amount", orderSumamount.subtract(firstpayamount));
-                jsonObject.put("sa_accountclassid", xianjinaccountclassrows.get(0).getLong("sa_accountclassid"));
-                jsonObject.put("accountname", xianjinaccountclassrows.get(0).getString("accountname"));
-                jsonObject.put("balance", xianjinaccountclassrows.get(0).getBigDecimal("balance"));
-                jsonObject.put("creditquota", xianjinaccountclassrows.get(0).getBigDecimal("creditquota"));
+            }else{
+                //全部优先账户扣款
+                jsonObject.put("amount", orderamount);
+                jsonObject.put("sa_accountclassid", accountclassrows.get(0).getLong("sa_accountclassid"));
+                jsonObject.put("accountname", accountclassrows.get(0).getString("accountname"));
+                jsonObject.put("balance", accountclassrows.get(0).getBigDecimal("balance"));
+                jsonObject.put("creditquota", accountclassrows.get(0).getBigDecimal("creditquota"));
                 jsonArray.add(jsonObject);
             }
+        }else{
+            //全部优先账户扣款
+            jsonObject.put("amount", orderamount);
+            jsonObject.put("sa_accountclassid", accountclassrows.get(0).getLong("sa_accountclassid"));
+            jsonObject.put("accountname", accountclassrows.get(0).getString("accountname"));
+            jsonObject.put("balance", accountclassrows.get(0).getBigDecimal("balance"));
+            jsonObject.put("creditquota", accountclassrows.get(0).getBigDecimal("creditquota"));
+            jsonArray.add(jsonObject);
         }
-
+        System.out.println(jsonArray.toJSONString());
         dbConnect.runSqlUpdate("update sa_order set sa_accountclassinfos='" + jsonArray + "' where sa_orderid=" + sa_orderid + " and siteid='" + siteid + "'");
+        return "true";
     }
 
     /**
@@ -522,9 +506,7 @@ public class OrderItems extends Controller {
         querySQL.setWhere(where.toString()).setTableAlias("t1");
         querySQL.setWhere("t1.siteid", siteid);
         querySQL.setWhere("t1.sa_orderid", sa_orderid);
-        if (!isExport) {
-            querySQL.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
-        }
+        querySQL.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
 
 
         Rows rows = querySQL.query();
@@ -717,9 +699,7 @@ public class OrderItems extends Controller {
 //        Rows rows = dbConnect.runSqlQuery(sqlFactory);
         QuerySQL querySQL = queryAllListManage(where.toString());
         querySQL.setOrderBy(pageSorting);
-        if (!isExport) {
             querySQL.setPage(pageSize, pageNumber);
-        }
         Rows rows = querySQL.query();
         ArrayList<Long> ids = rows.toArrayList("itemid", new ArrayList<>());
         RowsMap rowsMap = rows.toRowsMap("itemno");

+ 1 - 0
src/custom/restcontroller/webmanage/sale/order/OrderItemsHelper.java

@@ -202,6 +202,7 @@ public class OrderItemsHelper extends BaseClass {
         insertSQL.setValue("conversionrate", conversionrate);
         //订购数量
         insertSQL.setValue("qty", qty);
+        insertSQL.setValue("undeliqty", qty);
         //辅助单位数量
         insertSQL.setValue("auxqty", qty.divide(conversionrate));
         if (type.equals("特殊订单")) {

+ 2 - 3
src/custom/restcontroller/webmanage/sale/paybill/Paybill.java

@@ -76,9 +76,8 @@ public class Paybill extends Controller {
         }
         QuerySQL querySQL = query(where.toString());
         querySQL.setOrderBy(pageSorting);
-        if (!isExport) {
-            querySQL.setPage(pageSize, pageNumber);
-        }
+        querySQL.setPage(pageSize, pageNumber);
+
         Rows rows = querySQL.query();
 
 //        if (isExport) {

+ 6 - 0
src/custom/restcontroller/webmanage/sale/promotion/SQL/促销方案列表查询.sql

@@ -28,6 +28,10 @@ SELECT
 	t1.changedate,
 	t1.sa_openpromotionid,
 	t1.openamount,
+	t1.associationaccountclassid,
+	t4.accountname associationaccountname,
+	t4.accountno associationaccountno,
+	t1.associationamount,
 	t2.brandname,
 	t3.accountno,
 	t3.accountname 
@@ -37,4 +41,6 @@ FROM
 	AND t1.siteid = t2.siteid
 	LEFT JOIN sa_accountclass t3 ON t1.sa_accountclassid = t3.sa_accountclassid 
 	AND t1.siteid = t3.siteid
+	LEFT JOIN sa_accountclass t4 ON t1.associationaccountclassid = t4.sa_accountclassid
+	AND t1.siteid = t4.siteid
 	where t1.siteid=$siteid$ and $where$

+ 2 - 2
src/custom/restcontroller/webmanage/sale/promotion/SQL/促销方案新增.sql

@@ -1,8 +1,8 @@
 insert into sa_promotion (siteid, sa_promotionid, createuserid, createby, createdate, promnum, changeuserid, changeby,
                           changedate, promname, remarks, type, sa_accountclassid,
                           sa_brandid, tradefield, status, orderminqty, orderaddqty, orderminamount, begdate, enddate,
-                          sa_openpromotionid, openamount, isrelevancepromotion, authmethod)
+                          sa_openpromotionid, openamount, isrelevancepromotion, authmethod,associationaccountclassid,associationamount)
 values ($siteid$, $sa_promotionid$, $userid$, $username$, CURRENT_TIME, $promnum$, $userid$, $username$, CURRENT_TIME,
         $promname$, $remarks$, $type$, $sa_accountclassid$,
         $sa_brandid$, $tradefield$, '新建', $orderminqty$, $orderaddqty$, $orderminamount$, $begdate$, $enddate$,
-        $sa_openpromotionid$, $openamount$, $isrelevancepromotion$, $authmethod$);
+        $sa_openpromotionid$, $openamount$, $isrelevancepromotion$, $authmethod$,$associationaccountclassid$,$associationamount$);

+ 3 - 1
src/custom/restcontroller/webmanage/sale/promotion/SQL/促销方案更新.sql

@@ -16,6 +16,8 @@ SET changeuserid=$userid$,
     sa_openpromotionid=$sa_openpromotionid$,
     openamount=$openamount$,
     isrelevancepromotion=$isrelevancepromotion$,
-    authmethod=$authmethod$
+    authmethod=$authmethod$,
+    associationaccountclassid=$associationaccountclassid$,
+    associationamount=$associationamount$
 WHERE sa_promotionid = $sa_promotionid$
   and siteid = $siteid$

+ 6 - 0
src/custom/restcontroller/webmanage/sale/promotion/SQL/促销方案详情查询.sql

@@ -26,6 +26,10 @@ SELECT t1.promnum,
        t1.changeby,
        t1.changedate,
        t1.sa_openpromotionid,
+       t1.associationaccountclassid,
+	     t5.accountname associationaccountname,
+	     t5.accountno associationaccountno,
+	     t1.associationamount,
        t4.promname              openpromname,
        t1.openamount,
        t2.brandname,
@@ -39,4 +43,6 @@ FROM sa_promotion t1
     AND t1.siteid = t3.siteid
          LEFT JOIN sa_promotion t4 ON t4.sa_promotionid = t1.sa_openpromotionid
     AND t4.siteid = t1.siteid
+    LEFT JOIN sa_accountclass t5 ON t1.associationaccountclassid = t5.sa_accountclassid
+	AND t1.siteid = t5.siteid
 where t1.sa_promotionid = $sa_promotionid$

+ 20 - 2
src/custom/restcontroller/webmanage/sale/promotion/promotion.java

@@ -47,6 +47,8 @@ public class promotion extends Controller {
         String begdate = content.getStringValue("begdate");
         String enddate = content.getStringValue("enddate");
         Long authmethod = content.getLongValue("authmethod");
+        BigDecimal associationamount = content.getBigDecimalValue("associationamount");
+        Long associationaccountclassid = content.getLongValue("associationaccountclassid");
         SQLFactory sqlFactory = new SQLFactory(this, "促销方案新增");
 
         if (sa_promotionid <= 0 || dbConnect
@@ -97,6 +99,8 @@ public class promotion extends Controller {
         sqlFactory.addParameter("begdate", begdate);
         sqlFactory.addParameter("enddate", enddate);
         sqlFactory.addParameter("authmethod", authmethod);
+        sqlFactory.addParameter("associationamount", associationamount);
+        sqlFactory.addParameter("associationaccountclassid", associationaccountclassid);
         // 备注说明
         sqlFactory.addParameter("remarks", remarks);
         sqlList.add(sqlFactory.getSQL());
@@ -163,7 +167,8 @@ public class promotion extends Controller {
         sqlFactory.addParameter("sa_openpromotionid", row.getString("sa_openpromotionid"));
         sqlFactory.addParameter("openamount", row.getBigDecimal("openamount"));
         sqlFactory.addParameter("isrelevancepromotion", row.getString("isrelevancepromotion"));
-
+        sqlFactory.addParameter("associationamount", row.getString("associationamount"));
+        sqlFactory.addParameter("associationaccountclassid", row.getString("associationaccountclassid"));
         sqlFactory.addParameter("begdate", row.getString("begdate"));
         sqlFactory.addParameter("enddate", row.getString("enddate"));
         sqlFactory.addParameter("authmethod", row.getString("authmethod"));
@@ -305,7 +310,7 @@ public class promotion extends Controller {
         Long sa_promotionid = content.getLong("sa_promotionid");
 
         Rows rows = dbConnect
-                .runSqlQuery("select sa_promotionid,status,promnum,promname from sa_promotion where sa_promotionid ='"
+                .runSqlQuery("select sa_promotionid,status,promnum,promname,type,associationaccountclassid,associationamount from sa_promotion where sa_promotionid ='"
                         + sa_promotionid + "' and  siteid='" + siteid + "'");
         if (rows.isEmpty()) {
             return getErrReturnObject().setErrMsg("该促销方案不存在").toString();
@@ -314,6 +319,19 @@ public class promotion extends Controller {
                 return getErrReturnObject().setErrMsg("单号为:【" + rows.get(0).getString("promnum") + "】的促销方案为非新建状态,无法审核")
                         .toString();
             }
+            if(rows.get(0).getString("type").equals("返利促销")){
+                if(rows.get(0).getLong("associationaccountclassid")==0 || rows.get(0).getBigDecimal("associationamount").compareTo(BigDecimal.ZERO)==0){
+                    return getErrReturnObject().setErrMsg("返利促销必须选择关联支付账户和关联支付金额").toString();
+                }
+               Rows islimitRows = dbConnect.runSqlQuery("select ifnull(islimit,0) islimit,count(1) from sa_promotion_auth where siteid='"+siteid+"' and sa_promotionid="+sa_promotionid+" group by ifnull(islimit,0)");
+               if(islimitRows.isEmpty()){
+                   return getErrReturnObject().setErrMsg("返利促销必须授权经销商").toString();
+               } else if(islimitRows.size()>1 || (islimitRows.size()==1 && !islimitRows.get(0).getBoolean("islimit"))){
+                    return getErrReturnObject().setErrMsg("返利促销授权的经销商必须都为限购").toString();
+                }
+            }
+
+
         }
 
         ArrayList<String> sqlList = new ArrayList<>();