 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Contracts.Report;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;


namespace GZ_LTHReportForms.YingShouBaoBiao
{
    [Description("应收情况概况表按(合同性质)区分"), HotUpdate]
    public class YingShouHTXZ : SysReportBaseService
    {
        //对报表进行初始化
        public override void Initialize()
        {
            base.Initialize();
            //设置报表类型为普通类型
            this.ReportProperty.ReportType = ReportType.REPORTTYPE_NORMAL;
            //设置是否通过插件创建临时表
            this.IsCreateTempTableByPlugin = true;
            //设置是否分组汇总
            this.ReportProperty.IsGroupSummary = true;
        }
        //获取过滤条件信息(构造单据信息)
        public override ReportTitles GetReportTitles(IRptParams filter)
        {
            //创建标题对象
            ReportTitles reportTitles = new ReportTitles();
            //获取自定义过滤条件
            DynamicObject customFilter = filter.FilterParameter.CustomFilter;
            if (customFilter != null)
            {
                //获取组织名称
                string multiOrgnNameValues = this.GetMultiOrgnNameValues(customFilter["F_YKQC_OrgId_qtr"] as DynamicObject);
                //添加标题
                reportTitles.AddTitle("F_YKQC_OrgId_qtr", multiOrgnNameValues);
            }
            //返回标题
            return reportTitles;
        }
        //获取组织名称
        private string GetMultiOrgnNameValues(DynamicObject orgIdStrings)
        {
            //获取组织名称
            List<string> list = new List<string>();
            //初始化结果字符串
            string result = string.Empty;
            //如果组织ID字符串不为空
            if (orgIdStrings != null)
            {
                //获取查询服务
                IQueryService service = Kingdee.BOS.Contracts.ServiceFactory.GetService<IQueryService>(base.Context);
                //创建查询参数
                QueryBuilderParemeter para = new QueryBuilderParemeter
                {
                    FormId = "ORG_Organizations",
                    //查询组织名称
                    SelectItems = SelectorItemInfo.CreateItems("FNAME"),
                    //过滤条件，根据组织ID和区域ID
                    FilterClauseWihtKey = string.Format("FORGID IN ({0}) AND FLOCALEID = {1}", orgIdStrings["Id"].Long2Int(), base.Context.UserLocale.LCID)
                };
                //获取动态对象集合
                DynamicObjectCollection dynamicObjects = service.GetDynamicObjectCollection(base.Context, para, null);
                //遍历获取到的动态对象
                foreach (DynamicObject current in dynamicObjects)
                {
                    //将组织名称添加到列表
                    list.Add(current["FNAME"].ToString());
                }
                //如果列表不为空
                if (list.Count > 0)
                {
                    result = string.Join(",", list.ToArray());
                }
            }
            //返回组织名称字符串
            return result;
        }
        //设置单据列
        public override ReportHeader GetReportHeaders(IRptParams filter)
        {
            //创建表头对象
            ReportHeader header = new ReportHeader();
            //获取上一年十二月
            DateTime lastYearDecember = new DateTime(DateTime.Now.Year - 1, 12, 1);
            string formattedDate = lastYearDecember.ToString("yyyy-MM");
            //获取当前月份
            DateTime thisYearDecember = DateTime.Now;
            string thisDate = thisYearDecember.ToString("yyyy-MM");
            header.AddChild("FHTXZ", new LocaleValue("合同性质", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar).ColIndex = 0;
            header.AddChild("FJSLX", new LocaleValue("结算类型", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar).ColIndex = 1;

            
            var header1 = header.AddChild(formattedDate + "情况", new LocaleValue(formattedDate + "情况"));
            header1.AddChild("期初已出货欠款汇总", new LocaleValue("期初已出货欠款汇总", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
            header1.AddChild("期初已到期欠款", new LocaleValue("期初已到期欠款", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
            var header2 = header.AddChild(thisDate + "情况", new LocaleValue(thisDate + "情况"));
            header2.AddChild("本月已出货欠款汇总", new LocaleValue("本月已出货欠款汇总", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
            header2.AddChild("本月已到期欠款", new LocaleValue("本月已到期欠款", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
            var header3 = header.AddChild("变化", new LocaleValue("变化"));
            header3.AddChild("已出货欠款汇总", new LocaleValue("已出货欠款汇总", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
            header3.AddChild("已到期欠款", new LocaleValue("已到期欠款", this.Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
            foreach (var item in header.GetChilds())
            {
                item.Width = 200;
            }
            return header;
        }
        //创建临时表
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            //获取过滤条件
            string Filter = GetFilterWhere(filter);

            string sql = string.Format(@"/*dialect*/
DECLARE @ThisDate VARCHAR(1000);
DECLARE @ResultDate VARCHAR(1000);
-- 取上一年的年底日期
SET @ResultDate = FORMAT(DATEADD(DAY, -1, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)),'yyyy-MM-dd');
--取当前日期
SET @ThisDate = FORMAT(GETDATE(),'yyyy-MM-dd')

SELECT W.*,x.FNAME into #RecCond FROM  T_SAL_ORDERPLAN W       
INNER JOIN T_BD_RecCondition_L X on X.FID = W.F_RecConditionId       
      
SELECT  D.FID,sum(b.fqty) AS 'FQTY',sum(E.fallamount) AS 'FAMOUNT'       
  INTO #SAL      
  FROM T_SAL_DELIVERYNOTICE A       
  INNER JOIN T_SAL_DELIVERYNOTICEENTRY B ON A.FID = B.FID       
  INNER JOIN T_SAL_DELIVERYNOTICEENTRY_F E ON E.FENTRYID = B.FENTRYID      
  INNER JOIN T_SAL_DELIVERYNOTICEENTRY_LK  C ON C.FENTRYID = B.FENTRYID       
  INNER JOIN T_SAL_ORDERENTRY D ON D.FID = C.FSBILLID  AND D.FENTRYID = C.FSID      
  WHERE A.FDOCUMENTSTATUS= 'C'      
  GROUP BY D.FID      
      
      
  SELECT  D.FID,sum(b.fqty) AS 'FQTY',sum(E.fallamount) AS 'FAMOUNT'       
  INTO #RETURN      
  FROM T_SAL_RETURNNOTICE  A       
  INNER JOIN T_SAL_RETURNNOTICEENTRY  B ON A.FID = B.FID       
  INNER JOIN T_SAL_RETURNNOTICEENTRY_F E ON E.FENTRYID = B.FENTRYID      
  INNER JOIN T_SAL_RETURNNOTICEENTRY_LK  C ON C.FENTRYID = B.FENTRYID       
  INNER JOIN T_SAL_ORDERENTRY D ON D.FID = C.FSBILLID  AND D.FENTRYID = C.FSID       
  WHERE A.FDOCUMENTSTATUS= 'C'      
  GROUP BY D.FID      
      
      
  SELECT  F.FID,sum(b.FREALQTY) AS 'FQTY',sum(G.FALLAMOUNT_LC) AS 'FAMOUNT'      
  INTO #OUTSTOCK      
  FROM  T_SAL_OUTSTOCK A       
  INNER JOIN T_SAL_OUTSTOCKENTRY B ON A.FID = B.FID       
  INNER JOIN T_SAL_OUTSTOCKENTRY_F G ON G.FENTRYID = B.FENTRYID      
  INNER JOIN T_SAL_OUTSTOCKENTRY_LK C ON C.FENTRYID = B.FENTRYID      
  INNER JOIN T_SAL_DELIVERYNOTICEENTRY  D ON D.FID = C.FSBILLID AND D.FENTRYID = C.FSID      
  INNER JOIN T_SAL_DELIVERYNOTICEENTRY_LK E ON E.FENTRYID = D.FENTRYID        
  INNER JOIN T_SAL_ORDERENTRY F ON F.FID = E.FSBILLID  AND F.FENTRYID = E.FSID      
  WHERE A.FDOCUMENTSTATUS= 'C'      
  GROUP BY F.FID      
      
  SELECT  F.FID,sum(b.FREALQTY) AS 'FQTY',sum(G.FALLAMOUNT_LC) AS 'FAMOUNT'       
  INTO #RETURNSTOCK      
  FROM T_SAL_RETURNSTOCK A       
  INNER JOIN T_SAL_RETURNSTOCKENTRY B ON A.FID = B.FID       
  INNER JOIN T_SAL_RETURNSTOCKENTRY_F G ON G.FENTRYID = B.FENTRYID      
  INNER JOIN T_SAL_RETURNSTOCKENTRY_LK C ON C.FENTRYID = B.FENTRYID      
  INNER JOIN T_SAL_RETURNNOTICEENTRY  D ON D.FID = C.FSBILLID AND D.FENTRYID = C.FSID      
  INNER JOIN T_SAL_RETURNNOTICEENTRY_LK E ON E.FENTRYID = D.FENTRYID        
  INNER JOIN T_SAL_ORDERENTRY F ON F.FID = E.FSBILLID  AND F.FENTRYID = E.FSID      
  WHERE A.FDOCUMENTSTATUS= 'C'      
  GROUP BY F.FID      
      
  SELECT  b.FORDERENTRYID as 'FENTRYID',sum(b.FPriceQty) AS 'FQTY',sum(b.FALLAMOUNT) AS 'FAMOUNT'      
  INTO #RECE      
  FROM t_AR_receivable A       
  INNER JOIN t_AR_receivableENTRY B ON A.FID = B.FID         
  WHERE A.FDOCUMENTSTATUS= 'C'      
  GROUP BY b.FORDERENTRYID  


SELECT 
S.FCAPTION AS '合同性质',
CASE WHEN S.FCAPTION = '仪表' AND a.F_XMYSSJ <> '' then '项目已做完，回部分款'      
    WHEN S.FCAPTION = '仪表' AND a.F_XMYSSJ = '' then '项目未完工，欠款合同'       
    WHEN S.FCAPTION <> '仪表' AND y.FID  is not null then '款到发货'       
    WHEN S.FCAPTION <> '仪表' AND z.FID is not null then '协议付款'       
    ELSE '预收款+到货/调试款+质保金' END AS '结算类型',
0 AS '期初已出货欠款汇总',
CASE WHEN ROW_NUMBER() OVER (PARTITION BY A.Fid,n.FSaleId,X.FNAME ORDER BY n.FSaleId,X.FNAME) != 1 THEN null   
 WHEN X.FNAME in ('预付款(仪表)','预付款(系统)') THEN null    
 WHEN X.FNAME in ('货到收款(仪表)','货到收款(系统)') AND DATEADD(day, W.F_DAY, CK.F_SFSIGNATURETIME) < @ResultDate AND W.FRECADVANCERATE = 100 THEN ISNULL(Q.累计发货金额,0)-ISNULL(v.累计退货金额,0)    
 WHEN X.FNAME in ('货到收款(仪表)','货到收款(系统)') AND DATEADD(day, W.F_DAY, CK.F_SFSIGNATURETIME) < @ResultDate THEN ISNULL(W.F_MBBA_Decimal_uky,0)     
 WHEN X.FNAME in ('货到票到收款(系统)','货到票到收款(仪表)') AND DATEADD(day, W.F_DAY,FP.F_DATE) < @ResultDate AND W.FRECADVANCERATE = 100 THEN ISNULL(Q.累计发货金额,0)-ISNULL(v.累计退货金额,0)    
 WHEN X.FNAME in ('货到票到收款(系统)','货到票到收款(仪表)') AND DATEADD(day, W.F_DAY,FP.F_DATE) < @ResultDate THEN ISNULL(W.F_MBBA_Decimal_uky,0)     
 WHEN X.FNAME in ('款到发货(仪表)','款到发货(系统)') AND DATEADD(day, W.F_DAY,Q.发货日期) < @ResultDate AND W.FRECADVANCERATE = 100 THEN ISNULL(Q.累计发货金额,0)-ISNULL(v.累计退货金额,0)    
 WHEN X.FNAME in ('款到发货(仪表)','款到发货(系统)') AND DATEADD(day, W.F_DAY,Q.发货日期) < @ResultDate THEN ISNULL(W.F_MBBA_Decimal_uky,0)     
 ELSE null--款到发货(仪表)    
 END AS '期初已到期欠款',
0 AS '本月已出货欠款汇总',
CASE WHEN ROW_NUMBER() OVER (PARTITION BY A.Fid,n.FSaleId,X.FNAME ORDER BY n.FSaleId,X.FNAME) != 1 THEN null   
 WHEN X.FNAME in ('预付款(仪表)','预付款(系统)') THEN null    
 WHEN X.FNAME in ('货到收款(仪表)','货到收款(系统)') AND DATEADD(day, W.F_DAY, CK.F_SFSIGNATURETIME) < @ThisDate AND W.FRECADVANCERATE = 100 THEN ISNULL(Q.累计发货金额,0)-ISNULL(v.累计退货金额,0)    
 WHEN X.FNAME in ('货到收款(仪表)','货到收款(系统)') AND DATEADD(day, W.F_DAY, CK.F_SFSIGNATURETIME) < @ThisDate THEN ISNULL(W.F_MBBA_Decimal_uky,0)     
 WHEN X.FNAME in ('货到票到收款(系统)','货到票到收款(仪表)') AND DATEADD(day, W.F_DAY,FP.F_DATE) < @ThisDate AND W.FRECADVANCERATE = 100 THEN ISNULL(Q.累计发货金额,0)-ISNULL(v.累计退货金额,0)    
 WHEN X.FNAME in ('货到票到收款(系统)','货到票到收款(仪表)') AND DATEADD(day, W.F_DAY,FP.F_DATE) < @ThisDate THEN ISNULL(W.F_MBBA_Decimal_uky,0)     
 WHEN X.FNAME in ('款到发货(仪表)','款到发货(系统)') AND DATEADD(day, W.F_DAY,Q.发货日期) < @ThisDate AND W.FRECADVANCERATE = 100 THEN ISNULL(Q.累计发货金额,0)-ISNULL(v.累计退货金额,0)    
 WHEN X.FNAME in ('款到发货(仪表)','款到发货(系统)') AND DATEADD(day, W.F_DAY,Q.发货日期) < @ThisDate THEN ISNULL(W.F_MBBA_Decimal_uky,0)     
 ELSE null--款到发货(仪表)    
 END AS '本月已到期欠款'
INTO #TEMP1
FROM T_SAL_ORDER A --销售订单表头      
 LEFT JOIN      
 (Select a2.FENTRYID,a2.FDATAVALUE From T_BAS_ASSISTANTDATAENTRY a1 JOIN T_BAS_ASSISTANTDATAENTRY_L a2 on a1.FENTRYID = a2.FENTRYID      
 Where a1.FID = '670cc977263353') B on A.F_ORDERCATEGORY = B.FENTRYID      
 --INNER JOIN T_SAL_orderentry b on a.FID = B.FID --销售订单表体      
 INNER JOIN T_BD_CUSTOMER_L C ON C.FCUSTID= A.FCUSTID AND C.FLOCALEID= 2052 --客户附属表       
 INNER JOIN T_SAL_ORDERFIN  F ON F.FID  = A.FID       
 INNER JOIN T_BD_CUSTOMER I ON I .FCUSTID = A.FCUSTID      
 INNER JOIN T_SAL_ORDERFIN J ON J.FID= A.FID       
 LEFT JOIN  T_BAS_PREBDONE_L G ON G.FID = A.F_projectname AND G.FLocaleID =2052      
 LEFT JOIN  T_BAS_PREBDTHREE H ON H.FID = A.F_BON      
 lEFT JOIN V_ASSISTANTDATA K ON K.FENTRYID = I.FCompanyNature       
 lEFT JOIN V_BD_SALESMAN_L L ON L.fid = A.FSALERID        
 left JOIN T_PerformanceSharing M ON M.FSOURCEBILLNO = A.FBILLNO      
 left JOIN T_PerformanceSharingEntry N ON N.FID=M.FID      
 LEFT JOIN V_BD_SALESMAN_L O ON O.fid= N.FSALEID       
 LEFT JOIN V_ASSISTANTDATA P ON P.FENTRYID = I.F_CREDITCLASSIFICATION      
 LEFT JOIN T_SAL_OUTSTOCK CK on CK.F_CONTRACTNUMBER = A.F_CONTRACTNUMBER --销售出库单      
 LEFT JOIN T_IV_SALESIC FP on FP.F_PAPERNUMBER = A.F_CONTRACTNUMBER --销售增值税专用发票      
 LEFT JOIN       
 (      
  SELECT F_Papercontract,MAX(CONVERT(NVARCHAR(10), A.FDATE,23)) AS '发货日期',SUM(B.FBILLALLAMOUNT_LC) AS '累计发货金额'  FROM T_SAL_DELIVERYNOTICE A         
  INNER JOIN T_SAL_DELIVERYNOTICEFIN B ON A.FID =B.FID       
  WHERE A.FDOCUMENTSTATUS= 'C'      
  GROUP BY A.F_Papercontract      
 ) Q ON Q.F_PAPERCONTRACT = A.F_contractnumber --获取发货通知单最大的发货日期      
 LEFT JOIN T_META_FORMENUMITEM R ON r.FVALUE  = A.F_OrderType AND R.FID =  'bc2c1d77-0f4b-41d2-b61b-e29554c570d4'      
 LEFT JOIN T_META_FORMENUMITEM_L S ON S.FENUMID = R.FENUMID AND S.FLOCALEID = 2052        
 LEFT JOIN       
 (      
  SELECT F_Paper ,CONVERT(NVARCHAR(10), MAX(FDATE),23) AS '开票日期',CONVERT(NVARCHAR(10),      
  MAX(FDATE),23) AS '没有红色的开票日期'       
  FROM t_AR_receivable A        
  INNER JOIN T_AR_RECEIVABLEENTRY B ON A.FID = B.FID       
  WHERE A.FDOCUMENTSTATUS = 'C' AND A.FCancelStatus = 'A'      
  GROUP BY F_Paper      
 ) T ON T.F_PAPER = A.F_contractnumber      
 LEFT JOIN V_ASSISTANTDATA U ON U.FENTRYID = A.F_Ordercategory      
 LEFT JOIN       
 (      
  SELECT F_contractnumber,MAX(CONVERT(NVARCHAR(10), A.FDATE,23)) AS '退货日期',SUM(B.FBILLALLAMOUNT_LC) AS '累计退货金额'  FROM T_SAL_RETURNSTOCK A         
  INNER JOIN T_SAL_RETURNSTOCKFIN B ON A.FID =B.FID       
  WHERE A.FDOCUMENTSTATUS= 'C'      
  GROUP BY A.F_contractnumber      
 ) V ON V.F_contractnumber = A.F_contractnumber --获取销售退货单       
 LEFT JOIN  ERPTOHR.HYHRV3.dbo.v_erp_empinfo AC ON AC.名称=L.FNAME        
 LEFT JOIN  ERPTOHR.HYHRV3.dbo.v_erp_empinfo ACC ON ACC.名称=O.FNAME        
 --LEFT JOIN  temp20241206 AC ON AC.名称=L.FNAME        
 --LEFT JOIN  temp20241206 ACC ON ACC.名称=O.FNAME        
 LEFT JOIN T_SAL_ORDERPLAN W ON W.FID= A.FID       
 LEFT JOIN T_BD_RecCondition_L X on X.FID = W.F_RecConditionId       
 LEFT JOIN  #RecCond Y ON Y.FID =A.FID  AND Y.FNAME LIKE '%质保金%'        
 LEFT JOIN  (select distinct fid from #RecCond Z where Z.FNAME LIKE '%款到%') z on z.fid =a.fid       
 LEFT JOIN  (select distinct fid from #RecCond AA where AA.FNAME LIKE '%协议%') AA on AA.fid =a.fid        
 LEFT JOIN (SELECT distinct F_Papercontract FROM T_SAL_DELIVERYNOTICE WHERE F_SFYFH ='是') AB ON AB.F_PAPERCONTRACT = A.F_contractnumber      
 LEFT JOIN       
 (      
  SELECT       
  F_Papercontract,CAST( sum(b.FALLAMOUNT) AS REAL) AS '发票总金额',      
  CAST (sum(CASE WHEN F_Papercontract  = 'Y' THEN b.FALLAMOUNT ELSE 0 END  ) AS REAL) AS '发票总金额(普票存财务)'      
  FROM t_AR_receivable A      
  INNER JOIN T_AR_RECEIVABLEFIN B ON A.FID = B.FID       
  GROUP BY F_Papercontract      
 ) AD ON AD.F_PAPERCONTRACT = A.F_contractnumber      
 LEFT JOIN      
 (      
  SELECT a.F_Papercontract,      
  avg(d.FCURWRITTENOFFAMOUNT) as '累计收款'       
        
  FROM t_AR_receivable A        
  INNER JOIN T_AR_RECEIVABLEENTRY E ON E.FID =A.FID       
  INNER JOIN T_AR_RECMacthLogENTRY B ON A.FBILLNO = B.FSRCBILLNO      
  INNER JOIN T_AR_RECMacthLog C ON C.FID = B.FID       
  INNER JOIN T_AR_RECMacthLogENTRY D ON D.FID = B.FID      
  AND D.FSOURCETYPE = '36cf265bd8c3452194ed9c83ec5e73d2'       
  WHERE       
  1=1         
  group by a.F_Papercontract      
 ) AE ON AE.F_Papercontract = A.F_contractnumber      
 WHERE 1=1 AND A.FDOCUMENTSTATUS = 'C'  AND B.FDATAVALUE != 'Z'          
 AND ((F.FBILLALLAMOUNT_LC * isnull(n.FSHARERATE,100) /100)!=0)
{1}
 ORDER BY A.F_contractnumber 



 SELECT 
    合同性质 AS 合同性质2,
    结算类型 AS 结算类型2,
    CONVERT(DECIMAL(10, 2),SUM(ISNULL(期初已出货欠款汇总, 0)),2) AS '期初已出货欠款汇总2',
    CONVERT(DECIMAL(10, 2),SUM(ISNULL(期初已到期欠款, 0)),2) AS '期初已到期欠款2',
    CONVERT(DECIMAL(10, 2),SUM(ISNULL(本月已出货欠款汇总, 0)),2) AS '本月已出货欠款汇总2',
    CONVERT(DECIMAL(10, 2),SUM(ISNULL(本月已到期欠款, 0)),2) AS '本月已到期欠款2',
    0 AS '已出货欠款汇总',
	0 AS '已到期欠款'
    INTO #TEMP2
FROM 
    #TEMP1
GROUP BY 
    结算类型,合同性质
ORDER BY 
    结算类型,合同性质 DESC;

ALTER TABLE #TEMP2 ALTER COLUMN 已出货欠款汇总 NUMERIC(10, 2);

ALTER TABLE #TEMP2 ALTER COLUMN 已到期欠款 NUMERIC(10, 2);

update #TEMP2 SET 已出货欠款汇总 = CONVERT(DECIMAL(10, 2),(本月已出货欠款汇总2 - 期初已出货欠款汇总2),2)

update #TEMP2 SET 已到期欠款 = CONVERT(DECIMAL(10, 2),(本月已到期欠款2 - 期初已到期欠款2),2)

SELECT
ROW_NUMBER() OVER (ORDER BY B.合同性质2  DESC) AS FID,
ROW_NUMBER() OVER (ORDER BY B.结算类型2 DESC) AS FIDENTITYID,
B.合同性质2 AS 'FHTXZ',
B.结算类型2 AS 'FJSLX',
CASE 
WHEN B.期初已出货欠款汇总2 = 0 THEN null
ELSE B.期初已出货欠款汇总2 END AS '期初已出货欠款汇总',
CASE 
WHEN B.期初已到期欠款2 = 0 THEN null
ELSE B.期初已到期欠款2 END AS '期初已到期欠款',
CASE 
WHEN B.本月已出货欠款汇总2  = 0 THEN null
ELSE B.本月已出货欠款汇总2  END AS '本月已出货欠款汇总',
CASE 
WHEN B.本月已到期欠款2 = 0 THEN null
ELSE B.本月已到期欠款2 END AS '本月已到期欠款',
CASE 
WHEN B.已出货欠款汇总 = 0 THEN null
ELSE B.已出货欠款汇总 END AS '已出货欠款汇总',
CASE 
WHEN B.已到期欠款 = 0 THEN null
ELSE B.已到期欠款 END AS '已到期欠款' 
INTO {0} FROM #TEMP1 A INNER JOIN #TEMP2 B ON A.结算类型 = B.结算类型2
GROUP BY B.合同性质2 ,B.结算类型2,B.期初已出货欠款汇总2,B.期初已到期欠款2,B.本月已出货欠款汇总2,B.本月已到期欠款2,B.已出货欠款汇总,B.已到期欠款
ORDER BY FID 
", tableName, Filter);
            //执行SQL并动态创建报表
            DBUtils.ExecuteDynamicObject(this.Context, sql);

        }
        //获取过滤条件
        private string GetFilterWhere(IRptParams fileter)
        {
            //获取自定义过滤条件
            DynamicObject customFilter = fileter.FilterParameter.CustomFilter;
            //创建字符串构建器
            StringBuilder strwhere = new StringBuilder();
            var orgFid = customFilter["F_YKQC_OrgId_qtr"] as DynamicObject;
            if (orgFid != null)
            {
                string org = string.IsNullOrWhiteSpace(customFilter["F_YKQC_OrgId_qtr"].ToString()) ? "" :
                    string.Format("AND A.FSaleOrgId IN ({0})", orgFid["Id"].Long2Int());
                //添加组织过滤条件
                strwhere.AppendLine(org);
            }
            return strwhere.ToString();
        }
        //添加合计列
        public override List<SummaryField> GetSummaryColumnInfo(IRptParams filter)
        {
            var result = base.GetSummaryColumnInfo(filter);
            result.Add(new SummaryField("期初已出货欠款汇总", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("期初已到期欠款", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("本月已出货欠款汇总", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("本月已到期欠款", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("已出货欠款汇总", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("已到期欠款", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            return result;
        }
    }
}
