Просмотр исходного кода

代码整理,销售出库单审核生成的支出凭证的单据日期为销售出库单的单据日期

shenjingwei 3 месяцев назад
Родитель
Сommit
657a08c01a

+ 2 - 5
src/custom/beans/accountbalance/CashbillEntity.java

@@ -24,7 +24,8 @@ public class CashbillEntity {
 
     private String typemx;
 
-    public CashbillEntity() {
+    public CashbillEntity(String billdate) {
+        this.billdate = billdate;
     }
 
     public CashbillEntity(BigDecimal amount,BigDecimal discountamountamount, String remarks, String source, String sourcenote, String ownertable, long ownerid, String type, String typemx) {
@@ -45,10 +46,6 @@ public class CashbillEntity {
         }
         return billdate;
     }
-
-    public void setBilldate(String billdate) {
-        this.billdate = billdate;
-    }
     public String getPeriod() {
         if (this.period == null) {
             period = BaseClass.getDate_Str();

+ 105 - 105
src/custom/beans/stockbill/bills/XSCK.java

@@ -42,16 +42,16 @@ public class XSCK extends BasicBill {
             }
             BigDecimal discountamount = accountbalancerows.get(0).getBigDecimal("discountamount");
             if (billRow.getBigDecimal("paydiscountamount").compareTo(discountamount) > 0) {
-                if(rb==1){
+                if (rb == 1) {
                     throw new YosException("优惠金额不能大于可用优惠金额");
                 }
             }
-            if(billRow.getString("sourceobject").equals("tpartreimbursement")){
-                QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_tpartreimbursementdetail", "sa_tpartreimbursementdetailid","isverified","qty","sa_orderitemsid");
+            if (billRow.getString("sourceobject").equals("tpartreimbursement")) {
+                QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_tpartreimbursementdetail", "sa_tpartreimbursementdetailid", "isverified", "qty", "sa_orderitemsid");
                 querySQL.setTableAlias("t1");
                 querySQL.addJoinTable(BaseClass.JOINTYPE.inner, "sa_tpartreimbursement", "t3", "t1.sa_tpartreimbursementid = t3.sa_tpartreimbursementid AND t1.siteid = t3.siteid");
                 querySQL.setSiteid(siteid);
-                querySQL.setWhere("t1.hongbillno='"+billno+"'");
+                querySQL.setWhere("t1.hongbillno='" + billno + "'");
                 Rows rows = querySQL.query();
                 for (Row row : rows) {
                     if (row.getBoolean("isverified")) {
@@ -70,14 +70,14 @@ public class XSCK extends BasicBill {
             if (billRow.getBoolean("isreceiver")) {
                 throw new YosException("经销商已经确认收货,不可进行反审核");
             }
-            if(rb==1){
-                QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_orderitems","verifiedqty");
+            if (rb == 1) {
+                QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_orderitems", "verifiedqty");
                 querySQL.setTableAlias("t1");
                 querySQL.setSiteid(siteid);
-                querySQL.setWhere("t1.sa_orderitemsid",itemRows.toArrayList("sa_orderitemsid"));
+                querySQL.setWhere("t1.sa_orderitemsid", itemRows.toArrayList("sa_orderitemsid"));
                 Rows rows = querySQL.query();
                 for (Row row : rows) {
-                    if (row.getBigDecimal("verifiedqty").compareTo(BigDecimal.ZERO)>0) {
+                    if (row.getBigDecimal("verifiedqty").compareTo(BigDecimal.ZERO) > 0) {
                         throw new YosException("存在对应订单明细已核销,销售出库单无法反审核");
                     }
                 }
@@ -91,18 +91,18 @@ public class XSCK extends BasicBill {
         }
         //库存校验
         RowsMap invbalsRowsMap = SQLFactory.createQuerySQL(dbConnect, "st_invbal").setWhere("siteid", siteid).setWhere("itemid", itemRows.toArrayList("itemid")).query().toRowsMap("itemid");
-        for(Row row : itemRows){
-            if(!row.getBoolean("isnegative") && row.getBoolean("ismodule")){
+        for (Row row : itemRows) {
+            if (!row.getBoolean("isnegative") && row.getBoolean("ismodule")) {
                 long itemid = row.getLong("itemid");
                 long stockid = row.getLong("stockid");
-                BigDecimal qty =row.getBigDecimal("qty");
-                if(!isInStock(ischeck)){
+                BigDecimal qty = row.getBigDecimal("qty");
+                if (!isInStock(ischeck)) {
                     if (!invbalsRowsMap.containsKey(String.valueOf(itemid)) || !invbalsRowsMap.get(String.valueOf(itemid)).toRowsMap("stockid").containsKey(String.valueOf(stockid))) {
-                        throw new YosException("行【"+row.getString("rowno")+"】不能负库存出库");
+                        throw new YosException("行【" + row.getString("rowno") + "】不能负库存出库");
                     } else {
                         BigDecimal invbalqty = invbalsRowsMap.get(String.valueOf(itemid)).toRowsMap("stockid").get(String.valueOf(stockid)).get(0).getBigDecimal("qty");
-                        if(invbalqty.compareTo(qty)<0){
-                            throw new YosException("行【"+row.getString("rowno")+"】不能负库存出库");
+                        if (invbalqty.compareTo(qty) < 0) {
+                            throw new YosException("行【" + row.getString("rowno") + "】不能负库存出库");
                         }
                     }
                 }
@@ -114,12 +114,12 @@ public class XSCK extends BasicBill {
 //            if (order_paymentnode.equals("3") && !Accountbalance.judgeBalance(dbConnect, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), itemRows.toRowsMap("sa_orderitemsidnum").get("0").sum("amount"))) {
 //                //throw new YosException("账户余额不足,还差" + Accountbalance.InsufficientBalance(dbConnect, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), itemRows.toRowsMap("sa_orderitemsidnum").get("0").sum("amount")) + "元!");
 //            }
-            if(!billRow.getString("sourceobject").equals("tpartreimbursement")){
+            if (!billRow.getString("sourceobject").equals("tpartreimbursement")) {
                 Rows rowsjudge = dbConnect.runSqlQuery("select * from (select sa_orderitemsid,sum(qty) qty from st_stockbill_items where st_stockbillid ='" + st_stockbillid + "' group by sa_orderitemsid) t1 inner join sa_orderitems t2 on t1.sa_orderitemsid=t2.sa_orderitemsid where t1.qty>t2.undeliqty");
                 if (rowsjudge.isNotEmpty()) {
-                    if(ischeck){
+                    if (ischeck) {
                         throw new YosException("存在商品已完全出库,无法审核");
-                    }else{
+                    } else {
                         throw new YosException("存在商品已完全出库,无法反审核");
                     }
 
@@ -138,9 +138,9 @@ public class XSCK extends BasicBill {
             String order_paymentnode = Parameter.getString(siteid, "order_paymentnode");
             if (rb == 1) {
                 if (order_paymentnode.equals("3")) {
-                    CashbillEntity entity = getCashbillEntity(billRow.getBigDecimal("payamount"), billRow.getBigDecimal("paydiscountamount"), st_stockbillid, "销售出库单审核", billno, "由销售出库单" + billno + "审核时生成","货款");
+                    CashbillEntity entity = getCashbillEntity(billRow.getString("billdate"),billRow.getBigDecimal("payamount"), billRow.getBigDecimal("paydiscountamount"), st_stockbillid, "销售出库单审核", billno, "由销售出库单" + billno + "审核时生成", "货款");
                     JSONObject createCashbillPay = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity, true);
-                    if(!(billRow.getBigDecimal("payamount").compareTo(BigDecimal.ZERO)==0 && billRow.getBigDecimal("paydiscountamount").compareTo(BigDecimal.ZERO)==0)){
+                    if (!(billRow.getBigDecimal("payamount").compareTo(BigDecimal.ZERO) == 0 && billRow.getBigDecimal("paydiscountamount").compareTo(BigDecimal.ZERO) == 0)) {
                         sqlList.addAll(createCashbillPay.getJSONArray("sqlList").toJavaList(String.class));
                     }
                 }
@@ -158,58 +158,58 @@ public class XSCK extends BasicBill {
                 sqlList.addAll(updateAccountbalance_freez(isInStock(ischeck)));
                 //判断是否关闭订单
                 int orderautoclosepoint = Parameter.getInteger(siteid, "orderautoclosepoint");
-                if(orderautoclosepoint==2){
-                    ArrayList<Long> sa_orderids = itemRows.toArrayList("sa_orderid",new ArrayList<>());
+                if (orderautoclosepoint == 2) {
+                    ArrayList<Long> sa_orderids = itemRows.toArrayList("sa_orderid", new ArrayList<>());
                     QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_order", "sa_orderid");
                     querySQL.addJoinTable(BaseClass.JOINTYPE.inner, "sa_orderitems", "t2", "t2.sa_orderid = t1.sa_orderid and t2.siteid = t1.siteid",
-                            "undeliqty","sa_orderitemsid");
+                            "undeliqty", "sa_orderitemsid");
                     querySQL.setTableAlias("t1");
                     querySQL.setWhere("t1.siteid", siteid);
                     querySQL.setWhere("t1.sa_orderid", sa_orderids);
                     querySQL.setWhere("t1.status='审核' and t2.undeliqty!=0");
                     Rows rows = querySQL.query();
-                    RowsMap rowsMap =rows.toRowsMap("sa_orderid");
-                    RowsMap itemRowsMap=itemRows.toRowsMap("sa_orderid");
-                    for(Long sa_orderid : sa_orderids){
-                        Rows stockbillitemrows = itemRowsMap.getOrDefault(String.valueOf(sa_orderid),new Rows());
-                        Rows orderrows = rowsMap.getOrDefault(String.valueOf(sa_orderid),new Rows());
-                        RowsMap stockbillitemrowsMap =stockbillitemrows.toRowsMap("sa_orderitemsid");
-                        int count=0;
-                        for(Row row : orderrows){
-                            if(stockbillitemrowsMap.containsKey(row.getString("sa_orderitemsid"))){
-                                if(stockbillitemrowsMap.getOrDefault(row.getString("sa_orderitemsid"),new Rows()).sum("qty").compareTo(row.getBigDecimal("undeliqty"))==0){
+                    RowsMap rowsMap = rows.toRowsMap("sa_orderid");
+                    RowsMap itemRowsMap = itemRows.toRowsMap("sa_orderid");
+                    for (Long sa_orderid : sa_orderids) {
+                        Rows stockbillitemrows = itemRowsMap.getOrDefault(String.valueOf(sa_orderid), new Rows());
+                        Rows orderrows = rowsMap.getOrDefault(String.valueOf(sa_orderid), new Rows());
+                        RowsMap stockbillitemrowsMap = stockbillitemrows.toRowsMap("sa_orderitemsid");
+                        int count = 0;
+                        for (Row row : orderrows) {
+                            if (stockbillitemrowsMap.containsKey(row.getString("sa_orderitemsid"))) {
+                                if (stockbillitemrowsMap.getOrDefault(row.getString("sa_orderitemsid"), new Rows()).sum("qty").compareTo(row.getBigDecimal("undeliqty")) == 0) {
                                     count++;
                                 }
                             }
                         }
-                        if(orderrows.size()==count){
-                            sqlList.add("update sa_order set status='关闭',closeby='"+controller.username+"',closedate=CURRENT_TIME where sa_orderid=" +sa_orderid);
+                        if (orderrows.size() == count) {
+                            sqlList.add("update sa_order set status='关闭',closeby='" + controller.username + "',closedate=CURRENT_TIME where sa_orderid=" + sa_orderid);
                             sqlList.add("UPDATE sa_accountbalance t1 " +
                                     "INNER JOIN sa_accountbalance_freez t2 ON t1.sa_accountbalanceid = t2.sa_accountbalanceid " +
                                     "SET t1.freezamount = case when ifnull(t1.freezamount,0)>t2.amount then t1.freezamount-t2.amount else 0 end " +
-                                    "WHERE t1.sa_accountbalanceid="+accountbalancerows.get(0).getLong("sa_accountbalanceid")+" and t2.sourcetable='sa_order' and t2.sourceid="+sa_orderid);
-                            sqlList.add("delete from sa_accountbalance_freez t1 WHERE t1.sourcetable='sa_order' and t1.sourceid="+sa_orderid);
+                                    "WHERE t1.sa_accountbalanceid=" + accountbalancerows.get(0).getLong("sa_accountbalanceid") + " and t2.sourcetable='sa_order' and t2.sourceid=" + sa_orderid);
+                            sqlList.add("delete from sa_accountbalance_freez t1 WHERE t1.sourcetable='sa_order' and t1.sourceid=" + sa_orderid);
                         }
                     }
 
                 }
             } else {
                 if (order_paymentnode.equals("3")) {
-                    CashbillEntity entity = getCashbillEntity(billRow.getBigDecimal("payamount").negate(), billRow.getBigDecimal("paydiscountamount").negate(), st_stockbillid, "销售出库单审核", billno, "由销售出库单" + billno + "审核时生成","货款");
+                    CashbillEntity entity = getCashbillEntity(billRow.getString("billdate"),billRow.getBigDecimal("payamount").negate(), billRow.getBigDecimal("paydiscountamount").negate(), st_stockbillid, "销售出库单审核", billno, "由销售出库单" + billno + "审核时生成", "货款");
                     JSONObject createCashbillPay = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity, true);
-                    if(!(billRow.getBigDecimal("payamount").compareTo(BigDecimal.ZERO)==0 && billRow.getBigDecimal("paydiscountamount").compareTo(BigDecimal.ZERO)==0)){
+                    if (!(billRow.getBigDecimal("payamount").compareTo(BigDecimal.ZERO) == 0 && billRow.getBigDecimal("paydiscountamount").compareTo(BigDecimal.ZERO) == 0)) {
                         sqlList.addAll(createCashbillPay.getJSONArray("sqlList").toJavaList(String.class));
                     }
 
                 }
-                if(!billRow.getString("sourceobject").equals("tpartreimbursement")){
+                if (!billRow.getString("sourceobject").equals("tpartreimbursement")) {
                     Rows rowsOrderDetail = dbConnect.runSqlQuery("select t1.undeliqty,t1.deliedqty,t1.qty,t1.sa_orderitemsid from sa_orderitems t1 where t1.sa_orderitemsid in (select sa_orderitemsid from st_stockbill_items where sa_orderitemsid>0 and st_stockbillid=" + st_stockbillid + ")");
                     RowsMap rowsMap = rowsOrderDetail.toRowsMap("sa_orderitemsid");
                     Rows rowsDispatchDetailGroup = dbConnect.runSqlQuery("select sum(t1.qty) qty,t1.sa_orderitemsid,t1.siteid from st_stockbill_items t1  where t1.sa_orderitemsid>0 and t1.st_stockbillid=" + st_stockbillid + " group by t1.sa_orderitemsid,t1.siteid");
                     for (Row row : rowsDispatchDetailGroup) {
                         sqlList.add("update sa_orderitems set undeliqty=" + rowsMap.get(row.getLong("sa_orderitemsid")).get(0).getBigDecimal("undeliqty").add(row.getBigDecimal("qty")) + ",deliedqty=" + (rowsMap.get(row.getLong("sa_orderitemsid")).get(0).getBigDecimal("deliedqty").subtract(row.getBigDecimal("qty"))) + " where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
 
-                        sqlList.add("update sa_orderitems set tobeoffqty=case when tobeoffqty-" + row.getDouble("qty") + ">0 then tobeoffqty-" + row.getDouble("qty")+" else 0 end where canoffqty>0 and sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
+                        sqlList.add("update sa_orderitems set tobeoffqty=case when tobeoffqty-" + row.getDouble("qty") + ">0 then tobeoffqty-" + row.getDouble("qty") + " else 0 end where canoffqty>0 and sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
                     }
                     for (Row row : itemRows) {
                         if (row.getBoolean("skucontrol")) {
@@ -218,18 +218,18 @@ public class XSCK extends BasicBill {
                     }
 
                     int orderautoclosepoint = Parameter.getInteger(siteid, "orderautoclosepoint");
-                    if(orderautoclosepoint==2){
-                        ArrayList<Long> sa_orderids = itemRows.toArrayList("sa_orderid",new ArrayList<>());
-                        for(Long sa_orderid : sa_orderids){
-                            sqlList.add("update sa_order set status='审核',closeby='',closedate=NULL where sa_orderid=" +sa_orderid);
+                    if (orderautoclosepoint == 2) {
+                        ArrayList<Long> sa_orderids = itemRows.toArrayList("sa_orderid", new ArrayList<>());
+                        for (Long sa_orderid : sa_orderids) {
+                            sqlList.add("update sa_order set status='审核',closeby='',closedate=NULL where sa_orderid=" + sa_orderid);
                         }
                     }
-                }else{
-                    QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_tpartreimbursementdetail", "sa_tpartreimbursementdetailid","isverified","qty","sa_orderitemsid");
+                } else {
+                    QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_tpartreimbursementdetail", "sa_tpartreimbursementdetailid", "isverified", "qty", "sa_orderitemsid");
                     querySQL.setTableAlias("t1");
                     querySQL.addJoinTable(BaseClass.JOINTYPE.inner, "sa_tpartreimbursement", "t3", "t1.sa_tpartreimbursementid = t3.sa_tpartreimbursementid AND t1.siteid = t3.siteid");
                     querySQL.setSiteid(siteid);
-                    querySQL.setWhere("t1.hongbillno='"+billno+"'");
+                    querySQL.setWhere("t1.hongbillno='" + billno + "'");
 
                     Rows rows = querySQL.query();
 
@@ -239,7 +239,7 @@ public class XSCK extends BasicBill {
                     querySQLgroup.addJoinTable(BaseClass.JOINTYPE.left, "sa_orderitems", "t2", "t1.sa_orderitemsid = t2.sa_orderitemsid AND t1.siteid = t2.siteid");
                     querySQLgroup.setSiteid(siteid);
                     querySQLgroup.addQueryFields("sumqty", "sum(t1.checkqty)");
-                    querySQLgroup.setWhere("t1.hongbillno='"+ billno+"'");
+                    querySQLgroup.setWhere("t1.hongbillno='" + billno + "'");
                     querySQLgroup.addGroupBy("sa_orderitemsid");
                     Rows rowsgroup = querySQLgroup.query();
 
@@ -247,7 +247,7 @@ public class XSCK extends BasicBill {
                         sqlList.add("update sa_tpartreimbursementdetail set isverified=1 where sa_tpartreimbursementdetailid=" + row.getLong("sa_tpartreimbursementdetailid"));
                     }
                     for (Row row : rowsgroup) {
-                        sqlList.add("update sa_orderitems set verifiedqty=ifnull(verifiedqty,0)+"+row.getBigDecimal("sumqty")+" where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
+                        sqlList.add("update sa_orderitems set verifiedqty=ifnull(verifiedqty,0)+" + row.getBigDecimal("sumqty") + " where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
                     }
 
                 }
@@ -259,10 +259,10 @@ public class XSCK extends BasicBill {
             String order_paymentnode = Parameter.getString(siteid, "order_paymentnode");
             if (rb == 1) {
                 if (order_paymentnode.equals("3")) {
-                    Rows cashbillRows =dbConnect.runSqlQuery("select amount,discountamount from sa_cashbill where ownerid="+st_stockbillid +" and ownertable='st_stockbill' and type=0 and class='货款'");
-                    CashbillEntity entity = getCashbillEntity(cashbillRows.sum("amount").negate(), cashbillRows.sum("discountamount").negate(), st_stockbillid, "销售出库单反审核", billno, "由销售出库单" + billno + "反审核时生成","货款");
+                    Rows cashbillRows = dbConnect.runSqlQuery("select amount,discountamount from sa_cashbill where ownerid=" + st_stockbillid + " and ownertable='st_stockbill' and type=0 and class='货款'");
+                    CashbillEntity entity = getCashbillEntity(billRow.getString("billdate"),cashbillRows.sum("amount").negate(), cashbillRows.sum("discountamount").negate(), st_stockbillid, "销售出库单反审核", billno, "由销售出库单" + billno + "反审核时生成", "货款");
                     JSONObject createCashbillPay = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity, true);
-                    if(!(cashbillRows.sum("amount").negate().compareTo(BigDecimal.ZERO)==0 && cashbillRows.sum("discountamount").negate().compareTo(BigDecimal.ZERO)==0)){
+                    if (!(cashbillRows.sum("amount").negate().compareTo(BigDecimal.ZERO) == 0 && cashbillRows.sum("discountamount").negate().compareTo(BigDecimal.ZERO) == 0)) {
                         sqlList.addAll(createCashbillPay.getJSONArray("sqlList").toJavaList(String.class));
                     }
                 }
@@ -272,7 +272,7 @@ public class XSCK extends BasicBill {
                 for (Row row : rowsDispatchDetailGroup) {
                     sqlList.add("update sa_orderitems set undeliqty=" + rowsMap.get(row.getLong("sa_orderitemsid")).get(0).getBigDecimal("undeliqty").add(row.getBigDecimal("qty")) + ",deliedqty=" + (rowsMap.get(row.getLong("sa_orderitemsid")).get(0).getBigDecimal("deliedqty").subtract(row.getBigDecimal("qty"))) + " where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
 
-                    sqlList.add("update sa_orderitems set tobeoffqty=case when tobeoffqty-" + row.getDouble("qty") + ">0 then tobeoffqty-" + row.getDouble("qty")+" else 0 end where canoffqty>0 and sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
+                    sqlList.add("update sa_orderitems set tobeoffqty=case when tobeoffqty-" + row.getDouble("qty") + ">0 then tobeoffqty-" + row.getDouble("qty") + " else 0 end where canoffqty>0 and sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
                 }
                 for (Row row : itemRows) {
                     if (row.getBoolean("skucontrol")) {
@@ -281,24 +281,24 @@ public class XSCK extends BasicBill {
                 }
                 //判断是否关闭订单
                 int orderautoclosepoint = Parameter.getInteger(siteid, "orderautoclosepoint");
-                if(orderautoclosepoint==2){
-                    ArrayList<Long> sa_orderids = itemRows.toArrayList("sa_orderid",new ArrayList<>());
-                    for(Long sa_orderid : sa_orderids){
-                        sqlList.add("update sa_order set status='审核',closeby='',closedate=NULL where sa_orderid=" +sa_orderid);
+                if (orderautoclosepoint == 2) {
+                    ArrayList<Long> sa_orderids = itemRows.toArrayList("sa_orderid", new ArrayList<>());
+                    for (Long sa_orderid : sa_orderids) {
+                        sqlList.add("update sa_order set status='审核',closeby='',closedate=NULL where sa_orderid=" + sa_orderid);
                     }
                 }
                 sqlList.addAll(updateAccountbalance_freez(isInStock(ischeck)));
             } else {
                 if (order_paymentnode.equals("3")) {
-                    Rows cashbillRows =dbConnect.runSqlQuery("select amount,discountamount from sa_cashbill where ownerid="+st_stockbillid +" and ownertable='st_stockbill' and type=0 and class='货款'");
-                    CashbillEntity entity = getCashbillEntity(cashbillRows.sum("amount").negate(), cashbillRows.sum("discountamount").negate(), st_stockbillid, "销售出库单反审核", billno, "由销售出库单" + billno + "反审核时生成","货款");
+                    Rows cashbillRows = dbConnect.runSqlQuery("select amount,discountamount from sa_cashbill where ownerid=" + st_stockbillid + " and ownertable='st_stockbill' and type=0 and class='货款'");
+                    CashbillEntity entity = getCashbillEntity(billRow.getString("billdate"),cashbillRows.sum("amount").negate(), cashbillRows.sum("discountamount").negate(), st_stockbillid, "销售出库单反审核", billno, "由销售出库单" + billno + "反审核时生成", "货款");
                     JSONObject createCashbillPay = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity, true);
-                    if(!(cashbillRows.sum("amount").negate().compareTo(BigDecimal.ZERO)==0 && cashbillRows.sum("discountamount").negate().compareTo(BigDecimal.ZERO)==0)){
+                    if (!(cashbillRows.sum("amount").negate().compareTo(BigDecimal.ZERO) == 0 && cashbillRows.sum("discountamount").negate().compareTo(BigDecimal.ZERO) == 0)) {
                         sqlList.addAll(createCashbillPay.getJSONArray("sqlList").toJavaList(String.class));
                     }
 
                 }
-                if(!billRow.getString("sourceobject").equals("tpartreimbursement")){
+                if (!billRow.getString("sourceobject").equals("tpartreimbursement")) {
                     Rows rowsDispatchDetailGroup = dbConnect.runSqlQuery("select sum(t1.qty) qty,t1.sa_orderitemsid,t1.siteid from st_stockbill_items t1  where t1.sa_orderitemsid>0 and t1.st_stockbillid=" + st_stockbillid + " group by t1.sa_orderitemsid,t1.siteid");
                     for (Row row : rowsDispatchDetailGroup) {
                         sqlList.add("update sa_orderitems set undeliqty=undeliqty-" + row.getDouble("qty") + ",deliedqty=ifnull(deliedqty,0)+" + row.getDouble("qty") + ",offstatus=if(billingstatus='保内收费','待核销','不可核销'),offdate=CURRENT_TIME  where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
@@ -311,48 +311,48 @@ public class XSCK extends BasicBill {
                     }
                     //判断是否关闭订单
                     int orderautoclosepoint = Parameter.getInteger(siteid, "orderautoclosepoint");
-                    if(orderautoclosepoint==2){
-                        ArrayList<Long> sa_orderids = itemRows.toArrayList("sa_orderid",new ArrayList<>());
+                    if (orderautoclosepoint == 2) {
+                        ArrayList<Long> sa_orderids = itemRows.toArrayList("sa_orderid", new ArrayList<>());
                         QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_order", "sa_orderid");
                         querySQL.addJoinTable(BaseClass.JOINTYPE.inner, "sa_orderitems", "t2", "t2.sa_orderid = t1.sa_orderid and t2.siteid = t1.siteid",
-                                "undeliqty","sa_orderitemsid");
+                                "undeliqty", "sa_orderitemsid");
                         querySQL.setTableAlias("t1");
                         querySQL.setWhere("t1.siteid", siteid);
                         querySQL.setWhere("t1.sa_orderid", sa_orderids);
                         querySQL.setWhere("t1.status='审核' and t2.undeliqty!=0");
                         Rows rows = querySQL.query();
-                        RowsMap rowsMap =rows.toRowsMap("sa_orderid");
-                        RowsMap itemRowsMap=itemRows.toRowsMap("sa_orderid");
-                        for(Long sa_orderid : sa_orderids){
-                            Rows stockbillitemrows = itemRowsMap.getOrDefault(String.valueOf(sa_orderid),new Rows());
-                            Rows orderrows = rowsMap.getOrDefault(String.valueOf(sa_orderid),new Rows());
-                            RowsMap stockbillitemrowsMap =stockbillitemrows.toRowsMap("sa_orderitemsid");
-                            int count=0;
-                            for(Row row : orderrows){
-                                if(stockbillitemrowsMap.containsKey(row.getString("sa_orderitemsid"))){
-                                    if(stockbillitemrowsMap.getOrDefault(row.getString("sa_orderitemsid"),new Rows()).sum("qty").compareTo(row.getBigDecimal("undeliqty"))==0){
+                        RowsMap rowsMap = rows.toRowsMap("sa_orderid");
+                        RowsMap itemRowsMap = itemRows.toRowsMap("sa_orderid");
+                        for (Long sa_orderid : sa_orderids) {
+                            Rows stockbillitemrows = itemRowsMap.getOrDefault(String.valueOf(sa_orderid), new Rows());
+                            Rows orderrows = rowsMap.getOrDefault(String.valueOf(sa_orderid), new Rows());
+                            RowsMap stockbillitemrowsMap = stockbillitemrows.toRowsMap("sa_orderitemsid");
+                            int count = 0;
+                            for (Row row : orderrows) {
+                                if (stockbillitemrowsMap.containsKey(row.getString("sa_orderitemsid"))) {
+                                    if (stockbillitemrowsMap.getOrDefault(row.getString("sa_orderitemsid"), new Rows()).sum("qty").compareTo(row.getBigDecimal("undeliqty")) == 0) {
                                         count++;
                                     }
                                 }
                             }
 
-                            if(orderrows.size()==count){
-                                sqlList.add("update sa_order set status='关闭',closeby='"+controller.username+"',closedate=CURRENT_TIME where sa_orderid=" +sa_orderid);
+                            if (orderrows.size() == count) {
+                                sqlList.add("update sa_order set status='关闭',closeby='" + controller.username + "',closedate=CURRENT_TIME where sa_orderid=" + sa_orderid);
                                 sqlList.add("UPDATE sa_accountbalance t1 " +
                                         "INNER JOIN sa_accountbalance_freez t2 ON t1.sa_accountbalanceid = t2.sa_accountbalanceid " +
                                         "SET t1.freezamount = case when ifnull(t1.freezamount,0)>t2.amount then t1.freezamount-t2.amount else 0 end " +
-                                        "WHERE t1.sa_accountbalanceid="+accountbalancerows.get(0).getLong("sa_accountbalanceid")+" and t2.sourcetable='sa_order' and t2.sourceid="+sa_orderid);
-                                sqlList.add("delete from sa_accountbalance_freez t1 WHERE t1.sourcetable='sa_order' and t1.sourceid="+sa_orderid);
+                                        "WHERE t1.sa_accountbalanceid=" + accountbalancerows.get(0).getLong("sa_accountbalanceid") + " and t2.sourcetable='sa_order' and t2.sourceid=" + sa_orderid);
+                                sqlList.add("delete from sa_accountbalance_freez t1 WHERE t1.sourcetable='sa_order' and t1.sourceid=" + sa_orderid);
                             }
                         }
 
                     }
-                }else{
-                    QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_tpartreimbursementdetail", "sa_tpartreimbursementid","sa_tpartreimbursementdetailid","isverified","qty","sa_orderitemsid");
+                } else {
+                    QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_tpartreimbursementdetail", "sa_tpartreimbursementid", "sa_tpartreimbursementdetailid", "isverified", "qty", "sa_orderitemsid");
                     querySQL.setTableAlias("t1");
                     querySQL.addJoinTable(BaseClass.JOINTYPE.inner, "sa_tpartreimbursement", "t3", "t1.sa_tpartreimbursementid = t3.sa_tpartreimbursementid AND t1.siteid = t3.siteid");
                     querySQL.setSiteid(siteid);
-                    querySQL.setWhere("t1.hongbillno='"+billno+"'");
+                    querySQL.setWhere("t1.hongbillno='" + billno + "'");
                     Rows rows = querySQL.query();
 
 
@@ -361,7 +361,7 @@ public class XSCK extends BasicBill {
                     querySQLgroup.addJoinTable(BaseClass.JOINTYPE.left, "sa_orderitems", "t2", "t1.sa_orderitemsid = t2.sa_orderitemsid AND t1.siteid = t2.siteid");
                     querySQLgroup.setSiteid(siteid);
                     querySQLgroup.addQueryFields("sumqty", "sum(t1.checkqty)");
-                    querySQLgroup.setWhere("t1.hongbillno='"+ billno+"'");
+                    querySQLgroup.setWhere("t1.hongbillno='" + billno + "'");
                     querySQLgroup.addGroupBy("sa_orderitemsid");
                     Rows rowsgroup = querySQLgroup.query();
 
@@ -370,7 +370,7 @@ public class XSCK extends BasicBill {
                         sqlList.add("update sa_tpartreimbursement set status='审核' where sa_tpartreimbursementid=" + row.getLong("sa_tpartreimbursementid"));
                     }
                     for (Row row : rowsgroup) {
-                        sqlList.add("update sa_orderitems set verifiedqty=ifnull(verifiedqty,0)-"+row.getBigDecimal("sumqty")+" where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
+                        sqlList.add("update sa_orderitems set verifiedqty=ifnull(verifiedqty,0)-" + row.getBigDecimal("sumqty") + " where sa_orderitemsid=" + row.getLong("sa_orderitemsid"));
                     }
                 }
 
@@ -390,14 +390,14 @@ public class XSCK extends BasicBill {
             String order_paymentnode = Parameter.getString(siteid, "order_paymentnode");
             if (rb == 1) {
                 if (order_paymentnode.equals("3")) {
-                    BigDecimal custamount=BigDecimal.ZERO;
-                    for(Row row :itemRows){
-                        custamount=custamount.add(row.getBigDecimal("custamount").multiply(row.getBigDecimal("qty")));
+                    BigDecimal custamount = BigDecimal.ZERO;
+                    for (Row row : itemRows) {
+                        custamount = custamount.add(row.getBigDecimal("custamount").multiply(row.getBigDecimal("qty")));
                     }
 
-                    CashbillEntity entity_custamount = getCashbillEntity(custamount, BigDecimal.ZERO, st_stockbillid, "销售出库单审核", billno, "由销售出库单" + billno + "审核时生成","客户档案资料费");
+                    CashbillEntity entity_custamount = getCashbillEntity(billRow.getString("billdate"),custamount, BigDecimal.ZERO, st_stockbillid, "销售出库单审核", billno, "由销售出库单" + billno + "审核时生成", "客户档案资料费");
                     JSONObject createCashbillPay_custamount = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity_custamount, true);
-                    if(custamount.compareTo(BigDecimal.ZERO)!=0){
+                    if (custamount.compareTo(BigDecimal.ZERO) != 0) {
                         sqlList.addAll(createCashbillPay_custamount.getJSONArray("sqlList").toJavaList(String.class));
                     }
 
@@ -405,13 +405,13 @@ public class XSCK extends BasicBill {
                 }
             } else {
                 if (order_paymentnode.equals("3")) {
-                    BigDecimal custamount=BigDecimal.ZERO;
-                    for(Row row :itemRows){
-                        custamount=custamount.add(row.getBigDecimal("custamount").multiply(row.getBigDecimal("qty")));
+                    BigDecimal custamount = BigDecimal.ZERO;
+                    for (Row row : itemRows) {
+                        custamount = custamount.add(row.getBigDecimal("custamount").multiply(row.getBigDecimal("qty")));
                     }
-                    CashbillEntity entity_custamount = getCashbillEntity(custamount.negate(), BigDecimal.ZERO, st_stockbillid, "销售出库单审核", billno, "由销售出库单" + billno + "审核时生成","客户档案资料费");
+                    CashbillEntity entity_custamount = getCashbillEntity(billRow.getString("billdate"),custamount.negate(), BigDecimal.ZERO, st_stockbillid, "销售出库单审核", billno, "由销售出库单" + billno + "审核时生成", "客户档案资料费");
                     JSONObject createCashbillPay_custamount = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity_custamount, true);
-                    if(custamount.negate().compareTo(BigDecimal.ZERO)!=0){
+                    if (custamount.negate().compareTo(BigDecimal.ZERO) != 0) {
                         sqlList.addAll(createCashbillPay_custamount.getJSONArray("sqlList").toJavaList(String.class));
                     }
                 }
@@ -421,19 +421,19 @@ public class XSCK extends BasicBill {
             String order_paymentnode = Parameter.getString(siteid, "order_paymentnode");
             if (rb == 1) {
                 if (order_paymentnode.equals("3")) {
-                    Rows cashbillRows_custamount =dbConnect.runSqlQuery("select amount,discountamount from sa_cashbill where ownerid="+st_stockbillid +" and ownertable='st_stockbill' and type=0 and class='客户档案资料费'");
-                    CashbillEntity entity_custamount = getCashbillEntity(cashbillRows_custamount.sum("amount").negate(), cashbillRows_custamount.sum("discountamount").negate(), st_stockbillid, "销售出库单反审核", billno, "由销售出库单" + billno + "反审核时生成","客户档案资料费");
+                    Rows cashbillRows_custamount = dbConnect.runSqlQuery("select amount,discountamount from sa_cashbill where ownerid=" + st_stockbillid + " and ownertable='st_stockbill' and type=0 and class='客户档案资料费'");
+                    CashbillEntity entity_custamount = getCashbillEntity(billRow.getString("billdate"),cashbillRows_custamount.sum("amount").negate(), cashbillRows_custamount.sum("discountamount").negate(), st_stockbillid, "销售出库单反审核", billno, "由销售出库单" + billno + "反审核时生成", "客户档案资料费");
                     JSONObject createCashbillPay_custamount = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity_custamount, true);
-                    if(cashbillRows_custamount.sum("amount").negate().compareTo(BigDecimal.ZERO)!=0){
+                    if (cashbillRows_custamount.sum("amount").negate().compareTo(BigDecimal.ZERO) != 0) {
                         sqlList.addAll(createCashbillPay_custamount.getJSONArray("sqlList").toJavaList(String.class));
                     }
                 }
             } else {
                 if (order_paymentnode.equals("3")) {
-                    Rows cashbillRows_custamount =dbConnect.runSqlQuery("select amount,discountamount from sa_cashbill where ownerid="+st_stockbillid +" and ownertable='st_stockbill' and type=0 and class='客户档案资料费'");
-                    CashbillEntity entity_custamount = getCashbillEntity(cashbillRows_custamount.sum("amount").negate(),cashbillRows_custamount.sum("discountamount").negate(), st_stockbillid, "销售出库单审核", billno, "由销售出库单" + billno + "审核时生成","客户档案资料费");
+                    Rows cashbillRows_custamount = dbConnect.runSqlQuery("select amount,discountamount from sa_cashbill where ownerid=" + st_stockbillid + " and ownertable='st_stockbill' and type=0 and class='客户档案资料费'");
+                    CashbillEntity entity_custamount = getCashbillEntity(billRow.getString("billdate"),cashbillRows_custamount.sum("amount").negate(), cashbillRows_custamount.sum("discountamount").negate(), st_stockbillid, "销售出库单审核", billno, "由销售出库单" + billno + "审核时生成", "客户档案资料费");
                     JSONObject createCashbillPay_custamount = Accountbalance.createCashbillPay(dbConnect, userInfo, sys_enterpriseid, accountbalancerows.get(0).getLong("sa_accountclassid"), entity_custamount, true);
-                    if(cashbillRows_custamount.sum("amount").negate().compareTo(BigDecimal.ZERO)!=0){
+                    if (cashbillRows_custamount.sum("amount").negate().compareTo(BigDecimal.ZERO) != 0) {
                         sqlList.addAll(createCashbillPay_custamount.getJSONArray("sqlList").toJavaList(String.class));
                     }
                 }
@@ -476,7 +476,7 @@ public class XSCK extends BasicBill {
                     insertSQL.setValue("sourcetable", "sa_order");
                     insertSQL.setValue("sourceid", sa_orderid);
                     insertSQL.setValue("amount", amount);
-                    insertSQL.setValue("remarks", "由"+ (row.getString("type").equals("配件订单")?"配件":"销售")+"订单" + sonum + "审核时冻结");
+                    insertSQL.setValue("remarks", "由" + (row.getString("type").equals("配件订单") ? "配件" : "销售") + "订单" + sonum + "审核时冻结");
                     insertSQL.setValue("createby", userInfo.getUserName());
                     insertSQL.setDateValue("createdate");
                     sqllist.add(insertSQL.getSQL());
@@ -489,10 +489,10 @@ public class XSCK extends BasicBill {
                 BigDecimal amount = row.getBigDecimal("amount");
                 Rows accountbalancefreezrows = dbConnect.runSqlQuery("select sa_accountbalance_freezid,amount from sa_accountbalance_freez where sourceid=" + sa_orderid + " and sourcetable='sa_order' and siteid='" + siteid + "'");
                 if (accountbalancefreezrows.size() > 0) {
-                    if(accountbalancefreezrows.get(0).getBigDecimal("amount").compareTo(amount)>0){
+                    if (accountbalancefreezrows.get(0).getBigDecimal("amount").compareTo(amount) > 0) {
                         sqllist.add("update sa_accountbalance set freezamount=ifnull(freezamount,0)-" + amount + " where sa_accountbalanceid=" + sa_accountbalanceid);
                         sqllist.add("update sa_accountbalance_freez set amount=amount-" + amount + " where sa_accountbalance_freezid=" + accountbalancefreezrows.get(0).getLong("sa_accountbalance_freezid"));
-                    }else{
+                    } else {
                         sqllist.add("update sa_accountbalance set freezamount=ifnull(freezamount,0)-" + accountbalancefreezrows.get(0).getBigDecimal("amount") + " where sa_accountbalanceid=" + sa_accountbalanceid);
                         sqllist.add("update sa_accountbalance_freez set amount=0 where sa_accountbalance_freezid=" + accountbalancefreezrows.get(0).getLong("sa_accountbalance_freezid"));
                     }
@@ -504,8 +504,8 @@ public class XSCK extends BasicBill {
     }
 
 
-    public static CashbillEntity getCashbillEntity(BigDecimal amount, BigDecimal discountamount, Long ownerid, String source, String sourcenotes, String remarks,String type) throws YosException {
-        CashbillEntity entity = new CashbillEntity();
+    public static CashbillEntity getCashbillEntity(String billdate, BigDecimal amount, BigDecimal discountamount, Long ownerid, String source, String sourcenotes, String remarks, String type) throws YosException {
+        CashbillEntity entity = new CashbillEntity(billdate);
         entity.setAmount(amount);
         entity.setDiscountamountamount(discountamount);
         entity.setOwnerid(ownerid);

+ 3 - 3
src/custom/restcontroller/webmanage/sale/aftersalesmag/aftersalesmag.java

@@ -734,7 +734,7 @@ public class aftersalesmag extends Controller {
             if (rows.get(0).getDouble("returnamount") > 0) {
                 /******** 生成支出凭证 ********/
                 BigDecimal returnamount = rows.get(0).getBigDecimal("returnamount").negate();
-                CashbillEntity cashbillEntity = new CashbillEntity();
+                CashbillEntity cashbillEntity = new CashbillEntity(getDate_Str());
                 cashbillEntity.setAmount(returnamount);
                 cashbillEntity.setDiscountamountamount(BigDecimal.ZERO);
                 cashbillEntity.setOwnerid(sa_aftersalesmagid);
@@ -864,7 +864,7 @@ public class aftersalesmag extends Controller {
             if (rows.get(0).getDouble("returnamount") > 0) {
                 /******** 生成支出凭证 ********/
                 BigDecimal returnamount = rows.get(0).getBigDecimal("returnamount");
-                CashbillEntity cashbillEntity = new CashbillEntity();
+                CashbillEntity cashbillEntity = new CashbillEntity(getDate_Str());
                 cashbillEntity.setAmount(returnamount);
                 cashbillEntity.setDiscountamountamount(BigDecimal.ZERO);
                 cashbillEntity.setOwnerid(sa_aftersalesmagid);
@@ -1055,7 +1055,7 @@ public class aftersalesmag extends Controller {
             if (rows.get(0).getDouble("returnamount") != 0) {
                 /******** 生成支出凭证 ********/
                 BigDecimal returnamount = rows.get(0).getBigDecimal("returnamount");
-                CashbillEntity cashbillEntity = new CashbillEntity();
+                CashbillEntity cashbillEntity = new CashbillEntity(getDate_Str());
                 cashbillEntity.setAmount(returnamount);
                 cashbillEntity.setDiscountamountamount(BigDecimal.ZERO);
                 cashbillEntity.setOwnerid(sa_aftersalesmagid);

Разница между файлами не показана из-за своего большого размера
+ 281 - 282
src/custom/restcontroller/webmanage/sale/order/Order.java


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

@@ -161,7 +161,7 @@ public class OrderItemsHelper extends BaseClass {
             sqlList.add(updateSQL.getSQL());
 
             //生成收入凭证
-            CashbillEntity entity = new CashbillEntity();
+            CashbillEntity entity = new CashbillEntity(getDate_Str());
             entity.setAmount(approvedamount);
             entity.setDiscountamountamount(BigDecimal.ZERO);
             entity.setOwnerid(sa_orderid);

+ 125 - 237
src/custom/restcontroller/webmanage/sale/period/SQL/获取暂存账户数据.sql

@@ -1,284 +1,172 @@
-with
-    t2 as(-- -- -- -- -- 当年第一天到开始日期所有货款-- -- -- -- -- -- -- -- -- -
-        SELECT t3.agentnum fagentnum,
-               case when t1.rb='1'then t2.amount else -t2.amount end ftaxamount  FROM st_stockbill T1
-                                                                                          INNER JOIN st_stockbill_items  T2 ON T1.st_stockbillid=t2.st_stockbillid
-                                                                                          left JOIN sa_agents t3 ON t1.sys_enterpriseid=t3.sys_enterpriseid
-        where t1.status='审核' and t1.type='销售出库'
-          and t1.billdate>= DATE_FORMAT($begindate$, '%Y-01-01')
-          and t1.billdate<$enddate$
-    ),
-    c as(-- -- -- -- -- 当年第一天到开始日期所有档案费-- -- -- -- -- -- -- -- -- -
-        select t3.agentnum fagentnum,t1.amount  from sa_cashbill  t1
-                                                         inner join  sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid
-                                                         left join sa_agents t3 on t1.sys_enterpriseid=t3.sys_enterpriseid
-        where t1.status='审核' and t1.type=0 and  t2.accountno='01' and t1.class='客户档案资料费'
-          and t1.checkdate>=  DATE_FORMAT($begindate$, '%Y-01-01')
-          and t1.checkdate < $enddate$
-    ),
-    t5 as(-- -- -- -- -- 当年第一天到开始日期所有档案费-- -- -- -- -- -- -- -- -- -
-        select t3.agentnum fagentnum,t1.amount  from sa_cashbill t1
-                                                         inner join  sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid
-                                                         left join sa_agents t3 on t1.sys_enterpriseid=t3.sys_enterpriseid
-
-        where t1.status='审核' and  t1.type=1 and  t2.accountno='01' and  t1.class = '客户档案资料费'
-
-          and  t1.checkdate>=  DATE_FORMAT($begindate$, '%Y-01-01')
-          and  t1.checkdate < $enddate$
-    ),
-    d as(-- -- -- -- -- 期间内返资料费-- -- -- -- -- -- -- -- -- -
-        select t3.agentnum fagentnum,t1.amount  from sa_cashbill t1
-                                                         inner join  sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid
-                                                         left join sa_agents t3 on t1.sys_enterpriseid=t3.sys_enterpriseid
-        where t1.status='审核' and  t1.type=1 and  t2.accountno='01' and  t1.class = '客户档案资料费'  and
-
-          and  t1.checkdate>= $begindate$
-          and  t1.checkdate <= $enddate$
-
-    ),
-    h as (
-        select t3.agentnum fagentnum,t1.amount  from sa_cashbill t1
-                                                         inner join  sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid
-                                                         left join sa_agents t3 on t1.sys_enterpriseid=t3.sys_enterpriseid
-        where t1.status='审核' and  t2.accountno='01' and t1.class='客户档案资料费' and t1.ftype=0
-          and  t1.checkdate>= $begindate$
-          and  t1.checkdate <= $enddate$
-    ),
-    t3 as(-- -- -- -- -- -- -- -期间内回款-- -- -- -- -- -- -- -- -- -- -- -- -
-        select t3.agentnum fagentnum,t1.amount  from sa_cashbill t1
-                                                         inner join  sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid
-                                                         left join sa_agents t3 on t1.sys_enterpriseid=t3.sys_enterpriseid
-        where t1.type =1 and t1.status ='审核' and t2.accountno='01' and ifnull(t1.class,'') != '客户档案资料费'
-    and  t1.billdate>= $begindate$
-    and  t1.billdate <= $enddate$
-    ),
-
-
-    t4 as(-- -- -- -- -- -- -- -当年第一天到开始日期回款(计算上期结余)-- -- -- -- -- -- -- -- -- -- -- -- -
-select t3.agentnum fagentnum,t1.amount  from sa_cashbill t1
-    inner join  sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid
-    left join sa_agents t3 on t1.sys_enterpriseid=t3.sys_enterpriseid
-where t1.type =1  and t1.status ='审核' and  t2.accountno='01' and ifnull(t1.class,0) != '客户档案资料费'
-  and t1.billdate>= DATE_FORMAT($begindate$, '%Y-01-01')
-  and  t1.billdate < $enddate$
-    ),
-    t6 as(-- -- -- -- -- -- -- -- -- -- -本年第一天到开始日期退款-- 退货和报废退钱(计算上期结余)-- -- -- -- -- -- -- -- -- -
-select t4.agentnum fagentnum,ifnull(t2.amount,0) famount from st_stockbill t1
-    join st_stockbill_items t2 on t2.st_stockbillid=t1.st_stockbillid and t2.siteid=t1.siteid
-    join plm_item t3 on t3.itemid=t2.itemid and t3.siteid=t2.siteid and t3.topclassnum not in('其他费用','配件','0')
-    left join sa_agents t4 on t4.sys_enterpriseid=t1.sys_enterpriseid
-where t1.status='审核' and t1.type='其他入库' and t1.typemx in ('正品入库','报废入库')
-  and t1.billdate>= DATE_FORMAT($begindate$, '%Y-01-01')
-  and t1.billdate<$enddate$
-    ),
-    b as(
+with h as (select t3.agentnum, t1.amount
+           from sa_cashbill t1
+                    inner join sa_accountclass t2 on t1.sa_accountclassid = t2.sa_accountclassid
+                    left join sa_agents t3 on t1.sys_enterpriseid = t3.sys_enterpriseid
+           where t1.status = '审核'
+             and t1.type = 0
+             and t2.accountno = '01'
+             and t1.class = '客户档案资料费'
+             and t1.checkdate >= $begdate$
+             and t1.checkdate <= $enddate$
+             and t1.siteid = $siteid$),
+     d as (-- -- -- -- -- 期间内返资料费-- -- -- -- -- -- -- -- -- -
+         select t3.agentnum, t1.amount
+         from sa_cashbill t1
+                  inner join sa_accountclass t2 on t1.sa_accountclassid = t2.sa_accountclassid
+                  left join sa_agents t3 on t1.sys_enterpriseid = t3.sys_enterpriseid
+         where t1.status = '审核'
+           and t1.type = 1
+           and t2.accountno = '01'
+           and t1.class = '客户档案资料费'
+           and t1.checkdate >= $begdate$
+           and t1.checkdate <= $enddate$
+           and t1.siteid = $siteid$),
+     t3 as (-- -- -- -- -- -- -- -期间内回款-- -- -- -- -- -- -- -- -- -- -- -- -
+         select t3.agentnum, t1.amount
+         from sa_cashbill t1
+                  inner join sa_accountclass t2 on t1.sa_accountclassid = t2.sa_accountclassid
+                  left join sa_agents t3 on t1.sys_enterpriseid = t3.sys_enterpriseid
+         where t1.type = 1
+           and t1.status = '审核'
+           and t2.accountno = '01'
+           and ifnull(t1.class, '') != '客户档案资料费'
+    and t1.billdate>= $begdate$
+    and t1.billdate<= $enddate$
+    and t1.siteid=$siteid$
+    ), b as (
 -- -- -- -- -- -- -- -- -- -- -- -- -- -货款运费、货款其他、货款优惠、货款回收、上期结余、可用优惠-- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-select t1.fagentnum,sum(hkyf) '货款运费',sum(hkqtfy) '货款其他',sum(hkyh) '货款优惠',sum(hkhs) '货款回收',
-    sum(sqjy) '上期结余',sum(kyyh) '可用优惠',sum(bftk) '退款',sum(hkpj) '货款配件',sum(cz) '充值',SUM(yfzlf) 已返资料费,SUM(zlf) 资料费
-from(
-    select -- 运费
-    t3.agentnum fagentnum,
-    case when t1.rb='1' then ifnull(t2.amount,0) else -ifnull(t2.amount,0) end hkyf,0 hkqtfy, 0 hkyh,0 hkhs,0 sqjy,0 kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,0 zlf
-    from  st_stockbill t1
+select t1.agentnum, sum(hkyf) '货款运费', sum(hkqtfy) '货款其他', sum(hkyh) '货款优惠', sum(hkhs) '货款回收', sum(sqjy) '上期结余', sum(kyyh) '可用优惠', sum(bftk) '退款', sum(hkpj) '货款配件', sum(cz) '充值', SUM(yfzlf) 已返资料费, SUM(zlf) 资料费
+from (
+    select                                                                                                               -- 货款运费
+    t3.agentnum, if( rb=1, t2.amount, -t2.amount ) hkyf, 0 hkqtfy, 0 hkyh, 0 hkhs, 0 sqjy, 0 kyyh, 0 bftk, 0 hkpj, 0 cz, 0 yfzlf, 0 zlf
+    from st_stockbill t1
     left join st_stockbill_items t2 on t1.st_stockbillid = t2.st_stockbillid and t1.siteid = t2.siteid
     left join sa_agents t3 on t1.sys_enterpriseid = t3.sys_enterpriseid and t1.siteid = t3.siteid
     left join plm_item t4 on t2.itemid = t4.itemid and t2.siteid = t4.siteid
-    where t1.status='审核' and t1.type ='销售出库' and t4.itemno='90001' -- 运费
-    and t1.billdate>=$begindate$ and t1.billdate<=$enddate$
--- 货款其他 = 其他费用
-    union all-- 其他费用
-    select
-    t3.agentnum fagentnum,
-    0 hkyf,case when rb='1' then ifnull(t2.amount,0) else -ifnull(t2.amount,0) end hkqtfy, 0 hkyh,0 hkhs,0 sqjy,0 kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,0 zlf
-    from  st_stockbill t1
+    where t1.status='审核' and t1.type ='销售出库' and t4.itemno='90001' and t2.amount!=0
+    and t1.billdate>= $begdate$
+    and t1.billdate<= $enddate$
+    and t1.siteid=$siteid$
+    union all
+    select                                                                                                               -- 货款其他 = 其他费用
+    t3.agentnum, 0 hkyf, if( rb=1, t2.amount, -t2.amount ) hkqtfy, 0 hkyh, 0 hkhs, 0 sqjy, 0 kyyh, 0 bftk, 0 hkpj, 0 cz, 0 yfzlf, 0 zlf
+    from st_stockbill t1
     left join st_stockbill_items t2 on t1.st_stockbillid = t2.st_stockbillid and t1.siteid = t2.siteid
     left join sa_agents t3 on t1.sys_enterpriseid = t3.sys_enterpriseid and t1.siteid = t3.siteid
     left join plm_item t4 on t2.itemid = t4.itemid and t2.siteid = t4.siteid
-    where t1.status='审核' and t1.type ='销售出库' and t4.itemno!='90001' and t4.topclassnum='其他费用'   -- 其他费用
-    and t1.billdate>=$begindate$ and t1.billdate<=$enddate$
-    union all-- 货款优惠蓝字
-    select
-    t3.agentnum fagentnum,
-    0 hkyf,0 hkqtfy,t1.paydiscountamount hkyh,0 hkhs,0 sqjy,0 kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,0 zlf
-    from  st_stockbill t1
-    left join sa_agents t3 on t1.sys_enterpriseid = t3.sys_enterpriseid and t1.siteid = t3.siteid
-    where t1.status='审核' and t1.type ='销售出库' and t1.rb='1'
-    and t1.billdate>=$begindate$ and t1.billdate<=$enddate$
-    union all-- 货款优惠红字
-    select
-    t3.agentnum fagentnum,
-    0 hkyf,0 hkqtfy,-t1.paydiscountamount hkyh,0 hkhs,0 sqjy,0 kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,0 zlf
-    from  st_stockbill t1
+    where t1.status='审核' and t1.type ='销售出库' and t4.itemno!='90001' and t4.topclassnum='其他费用' and t2.amount!=0
+    and t1.billdate>= $begdate$
+    and t1.billdate<= $enddate$
+    and t1.siteid=$siteid$
+    union all
+    select                                                                                                               -- 货款优惠蓝字
+    t3.agentnum, 0 hkyf, 0 hkqtfy, if(t1.rb=1, t1.paydiscountamount, -t1.paydiscountamount) hkyh, 0 hkhs, 0 sqjy, 0 kyyh, 0 bftk, 0 hkpj, 0 cz, 0 yfzlf, 0 zlf
+    from st_stockbill t1
     left join sa_agents t3 on t1.sys_enterpriseid = t3.sys_enterpriseid and t1.siteid = t3.siteid
-    where t1.status='审核' and t1.type ='销售出库' and t1.rb='0'
-    and t1.billdate>=$begindate$ and t1.billdate<=$enddate$
+    where t1.status='审核' and t1.type ='销售出库' and t1.paydiscountamount!=0
+    and t1.billdate>= $begdate$
+    and t1.billdate<= $enddate$
+    and t1.siteid=$siteid$
 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 回收货款-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
     union all-- 回收货款
     select
-    t3.fagentnum,0 hkyf,0 hkqtfy,0 hkyh,t3.famount hkhs,0 sqjy,0 kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,0 zlf
+    t3.agentnum, 0 hkyf, 0 hkqtfy, 0 hkyh, t3.amount hkhs, 0 sqjy, 0 kyyh, 0 bftk, 0 hkpj, 0 cz, 0 yfzlf, 0 zlf
     from t3
     union all-- 返资料费
-    select d.fagentnum,0 hkyf,0 hkqtfy,0 hkyh,0 hkhs,0 sqjy,0 kyyh,0 bftk,0 hkpj,0 cz,d.famount yfzlf,0 zlf
-
+    select d.agentnum, 0 hkyf, 0 hkqtfy, 0 hkyh, 0 hkhs, 0 sqjy, 0 kyyh, 0 bftk, 0 hkpj, 0 cz, d.amount yfzlf, 0 zlf
     from d
     union all-- 资料费
-    select h.fagentnum,0 hkyf,0 hkqtfy,0 hkyh,0 hkhs,0 sqjy,0 kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,h.famount zlf
-
+    select h.agentnum, 0 hkyf, 0 hkqtfy, 0 hkyh, 0 hkhs, 0 sqjy, 0 kyyh, 0 bftk, 0 hkpj, 0 cz, 0 yfzlf, h.amount zlf
     from h
-
-
-
 -- -- -- -- -- -- -- -计算上期结余-- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--- 上期结余=本年期初-所有货款+回款+退款+优惠(本年第一天到开始日期)-- -- -- -- -- -- -- -- -- -- -
-
-    union all-- 本年期初余额
-
-    select  t3.agentnum fagentnum,
-    0 hkyf,0 hkqtfy,0 hkyh,0 hkhs,t1.balance sqjy,0 kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,0 zlf
-    from sa_cashbeginbal t1
-    inner join  sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid
-    left join sa_agents t3 on t1.sys_enterpriseid = t3.sys_enterpriseid and t1.siteid = t3.siteid
-    where t2.accountno='01' and t1.year=year($begindate$)
-    union all-- 货款(本年第一天到开始日期)
-    select t2.fagentnum,
-    0 hkyf,0 hkqtfy, 0 hkyh,0 hkhs,-t2.ftaxamount sqjy,0 kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,0 zlf
-    from t2
-    union all-- 资料费(本年第一天到开始日期)
-    select c.fagentnum,
-    0 hkyf,0 hkqtfy, 0 hkyh,0 hkhs,-c.famount sqjy,0 kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,0 zlf
-    from c
-    union all-- 返资料费(本年第一天到开始日期)
-    select t5.fagentnum,
-    0 hkyf,0 hkqtfy, 0 hkyh,0 hkhs,t5.famount sqjy,0 kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,0 zlf
-    from t5
-    union all-- 回款(本年第一天到开始日期)
-    select t4.fagentnum,
-    0 hkyf,0 hkqtfy, 0 hkyh,0 hkhs,t4.famount sqjy,0 kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,0 zlf
-    from t4
-    union all-- 退款(本年第一天到开始日期)
-    select t6.fagentnum,
-    0 hkyf,0 hkqtfy, 0 hkyh,0 hkhs,t6.famount sqjy,0 kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,0 zlf
-    from t6
-    union all-- 货款优惠蓝字(本年第一天到开始日期所有优惠)
-    select
-    t3.agentnum fagentnum,
-    0 hkyf,0 hkqtfy,0 hkyh,0 hkhs,t1.paydiscountamount sqjy,0 kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,0 zlf
-    from  st_stockbill t1
-    left join sa_agents t3 on t1.sys_enterpriseid = t3.sys_enterpriseid and t1.siteid = t3.siteid
-    where t1.status='审核' and t1.type ='销售出库' and t1.rb='1'
-    and t1.billdate>=DATE_FORMAT($begindate$, '%Y-01-01')
-    and t1.billdate<$enddate$
-    union all-- 货款优惠红字(本你那第一天到开始日期所有优惠)
-    select
-    t3.agentnum fagentnum,
-    0 hkyf,0 hkqtfy,0 hkyh,0 hkhs,-t1.paydiscountamount sqjy,0 kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,0 zlf
-    from  st_stockbill t1
-    left join sa_agents t3 on t1.sys_enterpriseid = t3.sys_enterpriseid and t1.siteid = t3.siteid
-    where t1.status='审核' and t1.type ='销售出库' and t1.rb='0'
-    and t1.billdate>=DATE_FORMAT($begindate$, '%Y-01-01')
-    and t1.billdate<$begindate$
-    union all-- 维修配件(本年第一天到开始日期所有送修单中的维修配件)
-    select t3.agentnum fagentnum,
-    0 hkyf,0 hkqtfy, 0 hkyh,0 hkhs,-ifnull(t2.agentcost,0) sqjy,0 kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,0 zlf
-    from sa_sendrepair t1
-    join sa_sendrepair_detail t2 on t2.sa_sendrepairid=t1.sa_sendrepairid and t2.siteid=t1.siteid
-    left join sa_agents t3 on t1.sys_enterpriseid = t3.sys_enterpriseid and t1.siteid = t3.siteid
-    where t1.status='审核'
-    and t1.checkdate>=DATE_FORMAT($begindate$, '%Y-01-01')
-    and t1.checkdate<$enddate$
-
--- 可用优惠=本年期初+所有回收优惠-支出优惠
-    union all-- 本年期初优惠
-    select  t3.agentnum fagentnum,
-    0 hkyf,0 hkqtfy, 0 hkyh,0 hkhs,0 sqjy,t1.DISCOUNTAMOUNT kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,0 zlf
+-- 上期结余=本年期初-所有货款+回款+退款+优惠(本年第一天到开始日期)-- -- -- -- -- -- -- -- -- --
+    union all-- 本年期初余额 本年期初优惠
+    select t3.agentnum, 0 hkyf, 0 hkqtfy, 0 hkyh, 0 hkhs, t1.balance sqjy, t1.DISCOUNTAMOUNT kyyh, 0 bftk, 0 hkpj, 0 cz, 0 yfzlf, 0 zlf
     from sa_cashbeginbal t1
-    inner join  sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid
+    inner join sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid
     left join sa_agents t3 on t1.sys_enterpriseid = t3.sys_enterpriseid and t1.siteid = t3.siteid
-    where t2.accountno='01'
-    and t1.year=year($begindate$)
-    union ALL-- 收入优惠+
-    select
-    t3.agentnum fagentnum,
-    0 hkyf,0 hkqtfy, 0 hkyh,0 hkhs,0 sqjy,ifnull(t1.DISCOUNTAMOUNT,0) kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,0 zlf
+    where t2.accountno='01' and t1.year= year ($begdate$) and t1.month=1 and t1.siteid=$siteid$
+    union ALL-- 收入优惠 支出优惠
+    select t3.agentnum, 0 hkyf, 0 hkqtfy, 0 hkyh, 0 hkhs, 0 sqjy, if(t1.type=1,t1.DISCOUNTAMOUNT,-t1.DISCOUNTAMOUNT)  kyyh, 0 bftk, 0 hkpj, 0 cz, 0 yfzlf, 0 zlf
     from sa_cashbill t1
-    inner join  sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid
+    inner join sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid
     left join sa_agents t3 on t1.sys_enterpriseid = t3.sys_enterpriseid and t1.siteid = t3.siteid
-    where t1.type =1 and t1.status ='审核' and t2.accountno='01'
-    and t1.billdate>=DATE_FORMAT($begindate$, '%Y-01-01')
-    and t1.billdate<$enddate$
-    union all-- 支出优惠-
-    select
-    t3.agentnum fagentnum,
-    0 hkyf,0 hkqtfy, 0 hkyh,0 hkhs,0 sqjy,-ifnull(t1.DISCOUNTAMOUNT,0) kyyh,0 bftk,0 hkpj,0 cz,0 yfzlf,0 zlf
-    from sa_cashbill t1
-    inner join  sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid
-    left join sa_agents t3 on t1.sys_enterpriseid = t3.sys_enterpriseid and t1.siteid = t3.siteid
-    where t1.type =0 and t1.status ='审核' and t2.accountno='01'
-    and t1.billdate>=DATE_FORMAT($begindate$, '%Y-01-01')
-    and t1.billdate<$enddate$
+    where t1.status ='审核' and t2.accountno='01' and t1.DISCOUNTAMOUNT!=0
+    and t1.billdate>=$begdate$
+    and t1.billdate<=$enddate$
+    and t1.siteid=$siteid$
     union all-- 报废退钱
-    select t4.agentnum fagentnum,0 hkyf,0 hkqtfy, 0 hkyh,0 hkhs,0 sqjy,0 kyyh,-ifnull(t2.amount,0) bftk,0 hkpj,0 cz,0 yfzlf,0 zlf from st_stockbill t1
+    select t4.agentnum, 0 hkyf, 0 hkqtfy, 0 hkyh, 0 hkhs, 0 sqjy, 0 kyyh, -ifnull(t2.amount, 0) bftk, 0 hkpj, 0 cz, 0 yfzlf, 0 zlf from st_stockbill t1
     join st_stockbill_items t2 on t2.st_stockbillid=t1.st_stockbillid and t2.siteid=t1.siteid
-    join plm_item t3 on t3.itemid=t2.itemid and t3.siteid=t2.siteid and t3.topclassnum not in('其他费用','配件','0')
+    join plm_item t3 on t3.itemid=t2.itemid and t3.siteid=t2.siteid and t3.topclassnum not in ('其他费用', '配件', '0')
     left join sa_agents t4 on t1.sys_enterpriseid = t4.sys_enterpriseid and t1.siteid = t4.siteid
     where t1.status='审核' and t1.type='其他入库' and t1.typemx ='报废入库' AND t2.amount!=0
-    and t1.billdate>=$begindate$ and t1.billdate<=$enddate$
-
-    union all -- 货款配件=销售配件费+维修配件费
-    select -- 销售配件费
-    t3.agentnum fagentnum,
-    0 hkyf,0 hkqtfy, 0 hkyh,0 hkhs,0 sqjy,0 kyyh,0 bftk,case when rb='1' then ifnull(t2.amount,0) else -ifnull(t2.amount,0) end hkpj,0 cz,0 yfzlf,0 zlf
-    from  st_stockbill t1
+    and t1.billdate>=$begdate$
+    and t1.billdate<=$enddate$
+    and t1.siteid=$siteid$
+    union all                                                                                                            -- 货款配件=销售配件费+维修配件费
+    select                                                                                                               -- 销售配件费
+    t3.agentnum, 0 hkyf, 0 hkqtfy, 0 hkyh, 0 hkhs, 0 sqjy, 0 kyyh, 0 bftk,if(t1.rb=1,t2.amount,-t2.amount) hkpj, 0 cz, 0 yfzlf, 0 zlf
+    from st_stockbill t1
     join st_stockbill_items t2 on t2.st_stockbillid=t1.st_stockbillid and t2.siteid=t1.siteid
     left join sa_agents t3 on t1.sys_enterpriseid = t3.sys_enterpriseid and t1.siteid = t3.siteid
     join plm_item t4 on t2.itemid = t4.itemid and t2.siteid = t4.siteid and t4.topclassnum='配件'
-    where t1.status='审核' and t1.type ='销售出库'
-    and t1.billdate>=$begindate$ and t1.billdate<=$enddate$
+    where t1.status='审核' and t1.type ='销售出库' and t2.amount!=0
+    and t1.billdate>=$begdate$
+    and t1.billdate<=$enddate$
+    and t1.siteid=$siteid$
 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 此处缺少维修配件费-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
     union all-- 维修配件
-    select t3.agentnum fagentnum,
-    0 hkyf,0 hkqtfy, 0 hkyh,0 hkhs,0 sqjy,0 kyyh,0 bftk,ifnull(t2.agentcost,0) hkpj,0 cz,0 yfzlf,0 zlf
+    select t3.agentnum, 0 hkyf, 0 hkqtfy, 0 hkyh, 0 hkhs, 0 sqjy, 0 kyyh, 0 bftk, ifnull(t2.agentcost, 0) hkpj, 0 cz, 0 yfzlf, 0 zlf
     from sa_sendrepair t1
     join sa_sendrepair_detail t2 on t2.sa_sendrepairid=t1.sa_sendrepairid and t2.siteid=t1.siteid
     left join sa_agents t3 on t1.sys_enterpriseid = t3.sys_enterpriseid and t1.siteid = t3.siteid
     where t1.status='审核'
-    and t1.checkdate>=$begindate$ and t1.checkdate<=$enddate$
-    union all -- 充值
+    and t1.checkdate>=$begdate$
+    and t1.checkdate<=$enddate$
+    and t1.siteid=$siteid$
+    union all                                                                                                            -- 充值
     select
-    t3.agentnum fagentnum,
-    0 hkyf,0 hkqtfy, 0 hkyh,0 hkhs,0 sqjy,0 kyyh,0 bftk,0 hkpj,ifnull(t1.DISCOUNTAMOUNT,0) cz,0 yfzlf,0 zlf
+    t3.agentnum , 0 hkyf, 0 hkqtfy, 0 hkyh, 0 hkhs, 0 sqjy, 0 kyyh, 0 bftk, 0 hkpj, ifnull(t1.DISCOUNTAMOUNT, 0) cz, 0 yfzlf, 0 zlf
     from sa_cashbill t1
-    inner join  sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid
+    inner join sa_accountclass t2 on t1.sa_accountclassid=t2.sa_accountclassid
     left join sa_agents t3 on t1.sys_enterpriseid = t3.sys_enterpriseid and t1.siteid = t3.siteid
     where t1.type ='收入' and t1.status ='审核' and t2.accountno='01'
-    and t1.billdate>=$begindate$ and t1.billdate<=$enddate$
+    and t1.billdate>=$begdate$
+    and t1.billdate<=$enddate$
+    and t1.siteid=$siteid$
 
-    ) t1 group by t1.fagentnum
-    ) ,
+    ) t1
+group by t1.agentnum
+    ),
     aaa as (
-select t.fagentnum,sum(t.ftaxamount) ftaxamount from(
-    SELECT t4.agentnum fagentnum,t3.topclassnum,
-    ifnull(case when t1.rb='1' then t2.amount else -t2.amount end,0) ftaxamount
+select t.agentnum, sum(t.ftaxamount) ftaxamount
+from (
+    SELECT t4.agentnum, t3.topclassnum, if( t1.rb=1 , t2.amount , -t2.amount ) ftaxamount
     FROM st_stockbill T1
-    INNER JOIN st_stockbill_items  T2 ON T1.st_stockbillid=t2.st_stockbillid
-    inner join plm_item t3 on t2.itemid=t3.itemid and t3.topclassnum not in('其他费用','配件','0')
+    INNER JOIN st_stockbill_items T2 ON T1.st_stockbillid=t2.st_stockbillid
+    inner join plm_item t3 on t2.itemid=t3.itemid and t3.topclassnum not in ('其他费用', '配件', '0')
     left join sa_agents t4 on t1.sys_enterpriseid = t4.sys_enterpriseid and t1.siteid = t4.siteid
-    where t1.status='审核' and t1.type='销售出库'
-    and t1.billdate>=$begindate$ and t1.billdate<=$enddate$
+    where t1.status='审核' and t1.type='销售出库' and t2.amount!=0
+    and t1.billdate>=$begdate$
+    and t1.billdate<=$enddate$
+    and t1.siteid=$siteid$
     union all
-    select t4.agentnum fagentnum,t3.topclassnum,-ifnull(t2.amount,0) ftaxamount
+    select t4.agentnum, t3.topclassnum, -t2.amount ftaxamount
     FROM st_stockbill T1
-    INNER JOIN st_stockbill_items  T2 ON T1.st_stockbillid=t2.st_stockbillid
-    inner join plm_item t3 on t2.itemid=t3.itemid and t3.topclassnum not in('其他费用','配件','0')
+    INNER JOIN st_stockbill_items T2 ON T1.st_stockbillid=t2.st_stockbillid
+    inner join plm_item t3 on t2.itemid=t3.itemid and t3.topclassnum not in ('其他费用', '配件', '0')
     left join sa_agents t4 on t1.sys_enterpriseid = t4.sys_enterpriseid and t1.siteid = t4.siteid
     where t1.status='审核' and t1.type='其他入库' and t1.typemx='正品入库' and t2.amount!=0
-    and t1.billdate>=$begindate$ and t1.billdate<=$enddate$
-    ) t group by t.fagentnum
+    and t1.billdate>=$begdate$
+    and t1.billdate<=$enddate$
+    and t1.siteid=$siteid$
+    ) t
+group by t.agentnum
     )
-select
-    d.fagentnum '经销商代号',d.sys_enterpriseid,
-        b.kyyh,
-    isnull(b.上期结余,0)+isnull(b.货款回收,0)+ isnull(b.已返资料费,0)-( isnull(aaa.ftaxamount,0)+isnull(b.货款其他,0)+isnull(b.货款运费,0)+isnull(b.货款配件,0)+isnull(b.退款,0)- isnull(b.货款优惠,0))- isnull(b.资料费,0) bqjy
-from  tagents d
-          left join b on b.fagentnum=d.fagentnum
-          left join aaa on aaa.fagentnum = d.fagentnum
+select d.agentnum '经销商代号',d.sys_enterpriseid,
+       b.可用优惠 as kyyh,
+       ifnull(b.上期结余, 0) + ifnull(b.货款回收, 0) + ifnull(b.已返资料费, 0) -(ifnull(aaa.ftaxamount, 0) + ifnull(b.货款其他, 0) + ifnull(b.货款运费, 0) + ifnull(b.货款配件, 0) +ifnull(b.退款, 0) - ifnull(b.货款优惠, 0)) - ifnull(b.资料费, 0) bqjy
+from sa_agents d
+         left join b on b.agentnum = d.agentnum
+         left join aaa on aaa.agentnum = d.agentnum

+ 28 - 56
src/custom/restcontroller/webmanage/sale/period/period.java

@@ -136,76 +136,48 @@ public class period extends Controller {
                     "delete from st_period where siteid='" + siteid + "' and st_periodid=" + st_periodid);
         }
         return batchDeleteErr.getReturnObject().toString();
-
     }
 
-    @API(title = "账户结账", apiversion = R.ID2025091310230303.v1.class)
+    @API(title = "账户结账(年结)", apiversion = R.ID2025091310230303.v1.class)
     @CACHEING_CLEAN(apiClass = {period.class})
     public String cashbeginbalclose() throws YosException {
-        Long st_periodid = content.getLong("st_periodid");
-        boolean isclose = content.getBoolean("isclose");
-        Rows rows = dbConnect.runSqlQuery("select * from st_period where siteid='" + siteid + "' and st_periodid=" + st_periodid);
-        if (rows.isEmpty()) {
-            return getErrReturnObject().setErrMsg("会计期间不存在").toString();
-        }
+        int closeYear = content.getInteger("year");//结算年份
         Rows accountrows = dbConnect.runSqlQuery("select sa_accountclassid from sa_accountclass where siteid='" + siteid + "' and accountnoa='01'");
         if (accountrows.isEmpty()) {
             return getErrReturnObject().setErrMsg("01账户不存在").toString();
         }
         ArrayList<String> sqlList = new ArrayList<>();
-        UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "st_period");
-        updateSQL.setUniqueid(st_periodid);
-        updateSQL.setSiteid(siteid);
-        updateSQL.setValue("isclose", isclose);
-        updateSQL.setValue("closeby", isclose ? username : "null");
-        if (isclose) {
-            updateSQL.setDateValue("closedate");
+        sqlList.add("delete from sa_cashbeginbal where year=" + closeYear + " and month=1");
+
+//        SQLFactory sqlFactory = new SQLFactory(this, "获取暂存账户数据");
+//        sqlFactory.addParameter("siteid", siteid);
+//        sqlFactory.addParameter("begdate", closeYear + "-01-01");
+//        sqlFactory.addParameter("enddate", closeYear + "-12-31");
+        String begdate = closeYear + "-01-01";
+        String enddate = closeYear + "-12-31";
+        Rows cashbeginbalRows;
+        if (closeYear < 2026) {
+            cashbeginbalRows = dbConnect.runSqlQuery("call cashbalcheck_old('" + begdate + "','" + enddate + "','" + siteid + "')");
         } else {
-            updateSQL.setValue("closedate", "null");
+            cashbeginbalRows = dbConnect.runSqlQuery("call cashbalcheck('" + begdate + "','" + enddate + "','" + siteid + "')");
         }
-
-        sqlList.add(updateSQL.getSQL());
-
-        LocalDate today = LocalDate.of(rows.get(0).getInteger("year"), rows.get(0).getInteger("month"), 1);
-        // 获取下一个月的第一天
-        LocalDate nextMonthFirstDay = today.with(today.plusMonths(1)).withDayOfMonth(1);
-        if (isclose) {
-
-            SQLFactory sqlFactory = new SQLFactory(this, "获取暂存账户数据");
-            sqlFactory.addParameter_SQL("begindate", nextMonthFirstDay.getYear() + "-12-31");
-            sqlFactory.addParameter_SQL("enddate", nextMonthFirstDay.getYear() + "-12-31");
-            Rows cashbeginbalRows = dbConnect.runSqlQuery(sqlFactory);
-
-            if (cashbeginbalRows.isNotEmpty()) {
-                for (Row row : cashbeginbalRows) {
-                    InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "sa_cashbeginbal");
-                    insertSQL.setUniqueid(createTableID("sa_cashbeginbal"));
-                    insertSQL.setSiteid(siteid);
-                    insertSQL.setValue("month", nextMonthFirstDay.getMonthValue());
-                    insertSQL.setValue("year", nextMonthFirstDay.getYear());
-                    insertSQL.setValue("sys_enterpriseid", row.getLong("sys_enterpriseid"));
-                    insertSQL.setValue("balance", row.getBigDecimal("bqjy"));
-                    insertSQL.setValue("creditquota", 0);
-                    insertSQL.setValue("discountamount", row.getBigDecimal("kyyh"));
-                    insertSQL.setValue("sa_accountclassid", accountrows.get(0).getLong("sa_accountclassid"));
-                    insertSQL.setValue("remarks", "年结转入");
-
-                    insertSQL.setValue("createby", username);
-                    insertSQL.setDateValue("createdate");
-                    insertSQL.setValue("changeby", username);
-                    insertSQL.setDateValue("changedate");
-                    sqlList.add(insertSQL.getSQL());
-                }
+        if (cashbeginbalRows.isNotEmpty()) {
+            for (Row row : cashbeginbalRows) {
+                InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "sa_cashbeginbal");
+                insertSQL.setUniqueid(createTableID("sa_cashbeginbal"));
+                insertSQL.setSiteid(siteid);
+                insertSQL.setValue("year", closeYear + 1);
+                insertSQL.setValue("month", 1);
+                insertSQL.setValue("sys_enterpriseid", row.getLong("sys_enterpriseid"));
+                insertSQL.setValue("balance", row.getBigDecimal("balance"));
+                insertSQL.setValue("discountamount", row.getBigDecimal("discountamount"));
+                insertSQL.setValue("creditquota", 0);
+                insertSQL.setValue("sa_accountclassid", accountrows.get(0).getLong("sa_accountclassid"));
+                insertSQL.setValue("remarks", "年结转入");
+                sqlList.add(insertSQL.getSQL());
             }
-            sqlList.add(
-                    DataContrlLog.createLog(this, "st_period", st_periodid, "账户结账", "会计期间账户结账成功").getSQL());
-        } else {
-            sqlList.add("delete from sa_cashbeginbal where year=" + nextMonthFirstDay.getYear() + " and month=" + nextMonthFirstDay.getMonthValue());
-            sqlList.add(
-                    DataContrlLog.createLog(this, "st_period", st_periodid, "反账户结账", "会计期间反账户结账成功").getSQL());
         }
         dbConnect.runSqlUpdate(sqlList);
-
         return getSucReturnObject().toString();
     }
 

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

@@ -783,8 +783,6 @@ public class promotion extends Controller {
         long sys_enterpriseid = promotionauthRows.get(0).getLong("sys_enterpriseid");
         BigDecimal saledamount = promotionauthRows.get(0).getBigDecimal("saledamount");
         BigDecimal rebateratio = promotionauthRows.get(0).getBigDecimal("rebateratio");
-        System.out.println(saledamount);
-        System.out.println(rebateratio);
         String agentnum = "";
         Rows enterpriserows = dbConnect.runSqlQuery("select * from sa_agents where sys_enterpriseid ='" + sys_enterpriseid + "' and siteid='" + siteid + "'");
         if (enterpriserows.isNotEmpty()) {
@@ -800,7 +798,7 @@ public class promotion extends Controller {
             Rows accountclassrows = dbConnect.runSqlQuery("select sa_accountclassid from  sa_accountclass where  accountname ='返利金账户' and siteid='" + siteid + "'");
             if (accountclassrows.isNotEmpty()) {
                 long sa_accountclassid = accountclassrows.get(0).getLong("sa_accountclassid");
-                CashbillEntity entity = new CashbillEntity();
+                CashbillEntity entity = new CashbillEntity(rows.get(0).getString("billdate"));
                 entity.setAmount(saledamount.multiply(rebateratio));
                 entity.setDiscountamountamount(BigDecimal.ZERO);
                 entity.setOwnerid(sa_promotion_authid);

+ 2 - 2
src/custom/restcontroller/webmanage/sale/rebateaccount/rebateaccounthead.java

@@ -489,7 +489,7 @@ public class rebateaccounthead extends Controller {
         if (ischeck) {
             for (Row row : rebateaccounts) {
                 if (row.getBigDecimal("approvedamount").compareTo(BigDecimal.ZERO) > 0) {
-                    CashbillEntity entity = new CashbillEntity();
+                    CashbillEntity entity = new CashbillEntity(rows.get(0).getString("billdate") );
                     entity.setAmount(row.getBigDecimal("approvedamount"));
                     entity.setDiscountamountamount(BigDecimal.ZERO);
                     entity.setOwnerid(row.getLong("sa_rebateaccountid"));
@@ -507,7 +507,7 @@ public class rebateaccounthead extends Controller {
         } else {
             for (Row row : rebateaccounts) {
                 if (row.getBigDecimal("approvedamount").compareTo(BigDecimal.ZERO) > 0) {
-                    CashbillEntity entity = new CashbillEntity();
+                    CashbillEntity entity = new CashbillEntity(rows.get(0).getString("billdate"));
                     entity.setAmount(row.getBigDecimal("approvedamount").negate());
                     entity.setDiscountamountamount(BigDecimal.ZERO);
                     entity.setOwnerid(row.getLong("sa_rebateaccountid"));

Разница между файлами не показана из-за своего большого размера
+ 225 - 229
src/custom/restcontroller/webmanage/sale/sendrepair/sendrepair.java


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

@@ -295,6 +295,7 @@ public class stockbill extends Controller {
             }
             sqlList.add(DataContrlLog.createLog(this, "st_stockbill", st_stockbillid, "更新", "单据日期【" + content.getString("billdate") + "】调整成功").getSQL());
             sqlList.add("update st_stockbill set billdate='" + content.getString("billdate") + "',period='" + period + "' where st_stockbillid=" + st_stockbillid);
+            sqlList.add("update sa_cashbill set billdate='" + content.getString("billdate") + "' where ownertable='st_stockbill' and ownerid=" + st_stockbillid);
         }
         dbConnect.runSqlUpdate(sqlList);
         HashMap<String, Object> result = new HashMap<>();
@@ -1831,8 +1832,8 @@ public class stockbill extends Controller {
     }
 
 
-    public CashbillEntity getCashbillEntity(BigDecimal amount, BigDecimal discountamount, Long ownerid, String source, String sourcenotes, String remarks) throws YosException {
-        CashbillEntity entity = new CashbillEntity();
+    public CashbillEntity getCashbillEntity(String billdate,BigDecimal amount, BigDecimal discountamount, Long ownerid, String source, String sourcenotes, String remarks) throws YosException {
+        CashbillEntity entity = new CashbillEntity(billdate);
         entity.setAmount(amount);
         entity.setDiscountamountamount(discountamount);
         entity.setOwnerid(ownerid);

+ 1 - 1
src/custom/restcontroller/webmanage/sale/toolbill/ToolBill.java

@@ -711,7 +711,7 @@ public class ToolBill extends Controller {
 
 
     public CashbillEntity getCashbillEntity(BigDecimal amount, Long ownerid, String source, String remarks) throws YosException {
-        CashbillEntity entity = new CashbillEntity();
+        CashbillEntity entity = new CashbillEntity(getDate_Str());
         entity.setAmount(amount);
         entity.setDiscountamountamount(BigDecimal.ZERO);
         entity.setOwnerid(ownerid);

Некоторые файлы не были показаны из-за большого количества измененных файлов