Advertisement
Guest User

Untitled

a guest
Feb 21st, 2022
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 28.67 KB | None | 0 0
  1. SET ANSI_NULLS ON
  2. GO
  3. SET QUOTED_IDENTIFIER OFF
  4. GO
  5.  
  6.  
  7.  
  8. ALTER    PROCEDURE [dbo].[cw_zz_GetPtypeBalance]
  9.     (
  10.       @PTypeID VARCHAR(50) ,
  11.       @BTypeID VARCHAR(50) ,
  12.       @ETypeID VARCHAR(50) ,
  13.       @StartDate VARCHAR(10) ,
  14.       @EndDate VARCHAR(10) ,
  15.       @VchType INT ,
  16.       @OperatorID VARCHAR(50),
  17.       @BuyBTypeID VARCHAR(50)   --交易单位 记录销售类单据的购买单位
  18.      
  19.     )
  20. AS  
  21.     SET NOCOUNT ON
  22.  
  23.     DECLARE @SQL VARCHAR(8000)  --动态SQL字符串
  24.     DECLARE @SQL1 VARCHAR(8000) --动态SQL字符串
  25.     DECLARE @SQL2 VARCHAR(8000) --动态SQL字符串
  26.     DECLARE @BTypeCon VARCHAR(1000) ,@BuyBTypeCon VARCHAR(1000) --BType条件字符串
  27.     DECLARE @ETypeCon VARCHAR(100)       --EType条件字符串
  28.     DECLARE @PTypeCon VARCHAR(100)       --PType条件字符串
  29.     DECLARE @VchCon VARCHAR(100)    --VchType条件字符串
  30.     DECLARE @StartDateCon VARCHAR(100)  --开始日期条件字符串
  31.     DECLARE @EndDateCon VARCHAR(100)    --结束日期条件字符串
  32.     DECLARE @AllCon VARCHAR(1000)   --所有条件字符串
  33.     DECLARE @AllCon2 VARCHAR(1000)
  34.     DECLARE @YHAType VARCHAR(100)       --优惠的科目ID
  35.     DECLARE @viewSql VARCHAR(8000)
  36.     DECLARE @viewSql2 VARCHAR(8000)
  37.     DECLARE @RPArype VARCHAR(3000)     --处理期初应收应付
  38.     DECLARE @IniArAp VARCHAR(8000)     --处理期初应收应付
  39.     DECLARE @sz_GatheringDetail VARCHAR(8000),@sz_GatheringDetail_1 VARCHAR(8000) --换掉临时表
  40.    
  41.     -- -2 代替期初应收的虚拟单据编号
  42.     -- -1 代替期初应付的虚拟单据编号
  43.     DECLARE @szMinYearName VARCHAR(50)
  44.     DECLARE @BTableStr VARCHAR(500)
  45.     DECLARE @TableStr VARCHAR(500)
  46.     DECLARE @CondRetail VARCHAR(400)
  47.    
  48.     DECLARE @B1TypeTableStr VARCHAR(200),@B1TypeColumStr VARCHAR(200)  --判断BS连接表
  49.     DECLARE @B2TypeTableStr VARCHAR(200),@B2TypeColumStr VARCHAR(200)  --判断BS连接表
  50.     DECLARE @B3TypeTableStr VARCHAR(200),@B3TypeColumStr VARCHAR(200)  --判断BS连接表
  51.  
  52.     EXEC p_hh_GetRightINNERStr 'B', 'ds', @OperatorID, 'S', @BTableStr OUT
  53.     EXEC p_hh_GetRightINNERStr 'B', 'b', @OperatorID, 'L', @TableStr OUT
  54.     SELECT  @YHAType = '000040000390000'
  55.     SELECT  @BTypeID = LTRIM(RTRIM(@BTypeID))
  56.     SELECT  @StartDate = LTRIM(RTRIM(@StartDate))
  57.     SELECT  @EndDate = LTRIM(RTRIM(@EndDate))
  58.     SELECT  @BuyBTypeID = LTRIM(RTRIM(@BuyBTypeID))
  59.  
  60.     SELECT  @VchCon = ''
  61.     SELECT  @StartDateCon = ''
  62.     SELECT  @EndDateCon = ''
  63.     SET @szMinYearName = ''
  64.     SELECT @CondRetail = ' ((ds.BillBalanceMode in (-1,2)) or (ds.VchType IN (11, 45) AND ds.lsRetail NOT IN (0, 10) and ds.BillBalanceMode = 0))'
  65.     SELECT TOP 1
  66.             @szMinYearName = YearName
  67.     FROM    YearInfo
  68.     ORDER BY YearPeriod
  69.    
  70.     EXEC p_hh_GetBaseTypeStr 'b', 'b1', ' x.btypeid',
  71.         ',b1.bfullname,b1.busercode', @B1TypeTableStr OUT,
  72.         @B1TypeColumStr OUT;
  73.     EXEC p_hh_GetBaseTypeStr 'b', 'b2', ' f.btypeid',
  74.         ',b2.bfullname,b2.busercode', @B2TypeTableStr OUT,
  75.         @B2TypeColumStr OUT;
  76.  
  77.    SELECT @B3TypeTableStr = ' LEFT JOIN dbo.Btype b3 on  x.buyBtypeid = b3.Btypeid ',
  78.            @B3TypeColumStr = ' ,b3.bfullname buybfullname,b3.busercode buybusercode '
  79.    
  80.     SELECT  @BuyBTypeCon = dbo.fn_getsonstr(@BuyBTypeID, 'GB', 'ds')
  81.            
  82.     SELECT  @BTypeCon = dbo.fn_getsonstr(@BTypeID, 'B', 'ds')
  83.        
  84.     SELECT  @ETypeCon = dbo.fn_getsonstr(@ETypeID, 'E', '')
  85.    
  86.     SELECT  @PTypeCon = dbo.fn_getsonstr(@PTypeID, 'P', 'a')
  87.    
  88.     IF @BTypeID = '00000'
  89.         SET @BTypeID = ''
  90.     IF @BuyBTypeID = '00000'
  91.     SET @BuyBTypeID = ''
  92.     /**************************新增单据8.14********************/
  93.     SELECT  @VchCon = CASE @VchType
  94.                         WHEN 4
  95.                         THEN ' and VchType IN (11,161,189,45,89,26,142,115,116,85,93,180,185,188) ' -- 收款单
  96.                         WHEN 66
  97.                         THEN ' and VchType IN (6,34,73,28,143,128,129,84,36,146,181,186,187,192,197) '  -- 付款单
  98.                         WHEN 0 THEN ''                      -- 所有单据
  99.                         ELSE 'VchType Wrong'
  100.                       END
  101.     IF @VchCon = 'VchType Wrong'
  102.         RETURN -1
  103.  
  104.     IF @StartDate <> ''
  105.         SELECT  @StartDateCon = 'Date >= ' + CHAR(39) + @StartDate + CHAR(39)
  106.     IF @EndDate <> ''
  107.         SELECT  @EndDateCon = 'Date <= ' + CHAR(39) + @EndDate + CHAR(39)
  108.    
  109.     SELECT  @AllCon = @BTypeCon + @ETypeCon
  110.     SELECT @AllCon2 = dbo.fn_getsonstr(@BTypeID, 'B', 'd') + dbo.fn_getsonstr(@ETypeID, 'E', 'd')
  111.     IF @StartDateCon <> ''
  112.     BEGIN
  113.         SELECT  @AllCon = @AllCon + ' AND ' + @StartDateCon
  114.         SELECT  @AllCon2 = @AllCon2 + ' AND d.Date >= ' + CHAR(39) + @StartDate + CHAR(39)
  115.     END
  116.     IF @EndDateCon <> ''
  117.     BEGIN
  118.         SELECT  @AllCon = @AllCon + ' AND ' + @EndDateCon
  119.         SELECT  @AllCon2 = @AllCon2 + ' AND d.Date <= ' + CHAR(39) + @EndDate + CHAR(39)
  120.     END
  121.  
  122.     DECLARE @IsUsingFc INT
  123.     SELECT  @IsUsingFc = ISNULL([STATS],0) FROM dbo.syscon WHERE [ORDER]=125
  124.  
  125.     --处理期初应收应付 --期初结算单位等于交易单位
  126.     SELECT  @RPArype = CASE @VchType
  127.                          WHEN 4
  128.                          THEN '
  129.                 select b.btypeid,'''' pusercode,''期初应收'' pfullname,'' '' summary,1 VchType,0 BillType, '' '' etypeid,-2 Vchcode, '' '' Date, '''
  130.                               + @szMinYearName
  131.                               + '期初应收'' Number,ARTotal00Year Total,0 lsFee,ARTotal00Year fcTotal,''00001'' fctypeid,1 fcrate
  132.                 from  dbo.fn_Btypeall(''L'') b ' + @TableStr + '
  133.                 WHERE  b.Btypeid like ''' + @BTypeID + '%''  and  b.gatherBtypeid like ''' + @BuyBTypeID + '%'''
  134.                               + ' and deleted = 0 and ARTotal00Year <> 0'
  135.                 -- 收款单
  136.                          WHEN 66
  137.                          THEN '
  138.                 select b.btypeid,'''' pusercode,''期初应付'' pfullname,'' '' summary,2 VchType,0 BillType, '' '' etypeid,-1 Vchcode, '' '' Date, '''
  139.                               + @szMinYearName
  140.                               + '期初应付'' Number,APTotal00Year Total,0 lsFee,APTotal00Year fcTotal,''00001'' fctypeid,1 fcrate
  141.                 from dbo.fn_Btypeall(''L'') b ' + @TableStr + '
  142.                 WHERE   b.Btypeid like ''' + @BTypeID + '%'' and  b.gatherBtypeid like ''' + @BuyBTypeID + '%'''
  143.                               + ' and deleted = 0 and APTotal00Year <> 0'
  144.             -- 付款单
  145.                          WHEN 0
  146.                          THEN '
  147.                 select b.btypeid,'''' pusercode,''期初应收'' pfullname,'' '' summary,1 VchType,0 BillType, '' '' etypeid, -2 Vchcode, '' '' Date, '''
  148.                               + @szMinYearName
  149.                               + '期初应收'' Number,ARTotal00Year Total,0 lsFee ,ARTotal00Year fcTotal,''00001'' fctypeid,1 fcrate
  150.                 from dbo.fn_Btypeall(''L'') b ' + @TableStr + '
  151.                 WHERE  b.BTYPEID like ''' + @BTypeID + '%'' and  b.gatherBtypeid like ''' + @BuyBTypeID + '%'''
  152.                               + ' and deleted = 0 and ARTotal00Year<>0
  153.                 union all
  154.                 select b.btypeid,'''' pusercode,''期初应付'' pfullname,'' '' summary, 2 VchType,0 BillType, '' '' etypeid, -1 Vchcode,'' '' Date, '''
  155.                               + @szMinYearName
  156.                               + '期初应付'' Number,APTotal00Year Total,0 lsFee ,APTotal00Year fcTotal,''00001'' fctypeid,1 fcrate
  157.                 from dbo.fn_Btypeall(''L'') b ' + @TableStr + '
  158.                 WHERE   b.Btypeid like ''' + @BTypeID + '%'' and  b.gatherBtypeid like ''' + @BuyBTypeID + '%'''
  159.                               + ' and deleted = 0 and APTotal00Year<>0'
  160.                        END
  161.  
  162.     SELECT  @IniArAp = '
  163.         SELECT f.*,
  164.             CASE WHEN BalanceTotal = 0 THEN 1  WHEN  BalanceTotal = VchTotal THEN 0 ElSE 2 END IsFinished,
  165.             ISNULL(k.SubCount,0) AS SubCount,0 AS IgnoreTotal ,0 AS fcIgnoreTotal '+@B2TypeColumStr+','''' buybfullname ,'''' buybusercode
  166.         from
  167.         (SELECT case when e.vchcode = -1 then ''期初应付'' else ''期初应收'' end ptypeid,e.pusercode,e.pfullname,e.btypeid,'' '' buybtypeid,e.vchname,dbo.fn_GetBtypeName(e.btypeid) + e.Number as summary,e.VchType,e.BillType,
  168.                 e.Vchcode,e.Date,gd.GatheringDate,e.Number,e.fctypeid,e.fcrate,
  169.                 e.Total fcvchtotal,
  170.                 (CASE WHEN (e.VchType=2) THEN -1.0 ELSE 1.0 END * SUM(ISNULL(gd.GatheringTotal, 0))) AS fcGatheringTotal ,
  171.                 e.Total - (CASE WHEN (e.VchType=2) THEN -1.0 ELSE 1.0 END*SUM(ISNULL(gd.GatheringTotal, 0))) AS fcBalanceTotal,
  172.                 e.Total VchTotal,
  173.                 (CASE WHEN (e.VchType=2) THEN -1.0 ELSE 1.0 END * SUM(ISNULL(gd.GatheringTotal, 0)) ) AS GatheringTotal,0 gatheringqty,  
  174.                 e.Total - (CASE WHEN (e.VchType=2) THEN -1.0 ELSE 1.0 END*SUM(ISNULL(gd.GatheringTotal, 0))) AS BalanceTotal,0 balanceqty,
  175.                 e.etypeid,e.lsFee,-1 feeFlag,-1 feetype,0 vchqty,0 DlyOrder,'''' unit,0 isoverpay,'''' kwtypeid
  176.             FROM
  177.             (select t.btypeid,t.pusercode,t.pfullname,t.summary,t.VchType,t.BillType,t.etypeid,
  178.             t.Vchcode,t.Date,t.Number,t.Total,ISNULL(v.vName, '''') VchName,t.lsFee,t.fcTotal,t.fctypeid,t.fcrate
  179.             from (' + @RPArype + ') t
  180.             LEFT JOIN
  181.             VchType v ON t.VchType=v.VchType           
  182.             ) e
  183.             LEFT OUTER JOIN
  184.             (select ds.*
  185.             from  dbo.view_zz_GatheringPtypeDetail ds ' + @BTableStr
  186.             + '
  187.             where vchcode in (-2,-1)
  188.             ) gd ON e.btypeid=gd.btypeid
  189.             GROUP BY e.btypeid,e.summary,e.VchType,e.BillType,gd.GatheringDate,
  190.             e.Vchcode,e.Date,e.Number,e.Total,e.etypeid,e.VchName,e.lsFee,e.pusercode,e.pfullname,e.fcTotal,e.fctypeid,e.fcrate
  191.         ) f
  192.         left join
  193.         (SELECT Count(1) SubCount, ds.btypeid
  194.             FROM  dbo.view_zz_GatheringPtypeDetail ds ' + @BTableStr + '
  195.             where vchcode in (-2,-1) and ds.btypeid like '''
  196.             + @BTypeID + '%''' + '
  197.             group by ds.btypeid
  198.         ) k on f.btypeid=k.btypeid ' +@B2TypeTableStr
  199.  
  200.     --处理期初应收应付end
  201.     SELECT  @viewSql = '
  202. SELECT  d.ptypeid,d.pusercode,d.pfullname,d.btypeid,d.buybtypeid,d.VchName,d.summary,d.VchType,d.BillType,
  203.     d.Vchcode,d.Date,d.GatheringDate, d.Number,
  204.     d.fctypeid,d.fcrate,
  205.     CASE WHEN d.VchType IN (45,6,115,129,186,188,189) THEN -1.0*d.fcTotal ELSE d.fcTotal END AS fcVchTotal,
  206.     CASE WHEN (d.VchType IN (34,6,28,73,128,129,143,84,36,146,181,186,187,192,197)) or (feeFlag = 0  or feeFlag = 2) THEN - 1.0 ELSE 1.0 END * SUM(ISNULL(g.fcGatheringTotal, 0)) AS fcGatheringTotal,
  207.     CASE WHEN d.VchType IN (45, 6,115,129,186,188,189) THEN -1.0 * d.fcTotal ELSE d.fcTotal END - CASE WHEN (d.VchType IN (34,6,28,73,128,129,143,84,36,146,181,186,187,192,197)) or (feeFlag = 0  or feeFlag = 2) THEN - 1.0 ELSE 1.0 END * SUM(ISNULL(g.fcGatheringTotal, 0)) AS fcBalanceTotal,
  208.     CASE WHEN d.VchType IN (45,6,115,129,186,188,189) THEN -1.0*d.Total ELSE d.Total END AS VchTotal,
  209.     CASE WHEN (d.VchType IN (34,6,28,73,128,129,143,84,36,146,181,186,187,192,197)) or (feeFlag = 0  or feeFlag = 2) THEN - 1.0 ELSE 1.0 END * SUM(ISNULL(g.GatheringTotal, 0)) AS GatheringTotal,
  210.     CASE WHEN  (d.VchType IN (34,6,28,73,128,129,143,84,36,146,181,186,187,192,197)) or (feeFlag = 0  or feeFlag = 2) THEN - 1.0 ELSE 1.0 END * SUM(ISNULL(g.gatheringqty,0)) AS gatheringqty,
  211.     CASE WHEN d.VchType IN (45, 6,115,129,186,188,189) THEN -1.0 * d.Total ELSE d.Total END - CASE WHEN (d.VchType IN (34,6,28,73,128,129,143,84,36,146,181,186,187,192,197)) or (feeFlag = 0  or feeFlag = 2) THEN - 1.0 ELSE 1.0 END * SUM(ISNULL(g.GatheringTotal, 0)) AS BalanceTotal,
  212.     d.etypeid,d.lsFee,feeFlag,feetype,CASE WHEN d.VchType IN (45, 6,115,129,186,188,189) THEN -1.0*d.qty ELSE d.qty END - CASE WHEN  (d.VchType IN (34,6,28,73,128,129,143,84,36,146,181,186,187,192,197)) or (feeFlag = 0  or feeFlag = 2) THEN - 1.0 ELSE 1.0 END * SUM(ISNULL(g.gatheringqty,0)) AS balanceqty,
  213.     CASE WHEN d.VchType IN (6,45,115,129,186,188,189) THEN -1.0*d.qty ELSE d.qty END AS vchqty,d.DlyOrder,d.unit,d.isoverpay,d.kwtypeid,ndx.lsRetail   
  214. FROM #Tmp'
  215.  
  216. SET @sz_GatheringDetail = 'select t.ptypeid,t.pusercode,t.pfullname,t.btypeid,t.buybtypeid, t.summary, t.VchType, t.BillType,t.etypeid,
  217.     t.Vchcode,t.Date,t.GatheringDate,t.Number,t.Total,ISNULL(v.vName, '''') VchName,t.lsFee ,t.feeFlag, feetype,qty,DlyOrder,unit,isoverpay,kwtypeid,t.fctypeid,t.fcrate,t.fctotal INTO #Tmp
  218.     from (select ptypeid,pusercode,pfullname,btypeid,buybtypeid,summary,ds.VchType,BillType,etypeid,ds.Vchcode,Date,GatheringDate,Number,Total,lsFee ,-1 feeFlag,-1 feetype,qty,DlyOrder,unit, isoverpay,kwtypeid,fctypeid,fcrate,fctotal from
  219.     (select ds.ptypeid,ds.pusercode,ds.pfullname,ds.btypeid,ds.buybtypeid,summary,ds.VchType,BillType,etypeid,ds.Vchcode,Date,GatheringDate,Number,ds.Total total,0 lsFee ,-1 feeFlag,qty,DlyOrder,unit,isoverpay,kwtypeid,ds.fctypeid,ds.fcrate,
  220.      case when isnull(ds.fctypeid,'''') = ''00001'' then ds.total else ds.fctotal end fctotal
  221.     from (select buy.PtypeId,p.pusercode,p.pfullname,ds.Vchcode,ds.bTypeId,ds.bTypeId buybtypeid,
  222.     ds.summary,ds.VchType,ds.BillType,ds.etypeid,ds.Date,ds.GatheringDate,ds.Number,
  223.     CASE WHEN ds.VchType = 143 AND buy.tax_total < 0 THEN buy.tax_total else abs(buy.tax_total) end total,
  224.     CASE WHEN ds.VchType = 143 AND buy.tax_total < 0 THEN -buy.AssQty ELSE abs(buy.AssQty) end qty,buy.DlyOrder,buy.unit, 0 isoverpay,buy.kwtypeid
  225.     ,ds.fctypeid,ds.fcrate,CASE WHEN ds.VchType = 143 AND buy.tax_total < 0 THEN sign(buy.tax_total)*(fc.tax_total) else abs(fc.tax_total) end fctotal
  226.     from Dlyndx ds,dbo.DlyBuy buy LEFT JOIN Fcdly fc ON buy.Vchcode = fc.vchcode AND buy.Vchtype = fc.vchtype AND buy.DlyOrder = fc.dlyorder AND buy.PtypeId = fc.ptypeid
  227.     LEFT JOIN ptype p ON buy.PtypeId = p.ptypeid WHERE ds.Vchcode = buy.Vchcode AND (draft=2) AND (RedOld=''F'') AND ds.VchType IN(6,28,34,73,143) and ds.BillBalanceMode in (-1,2) and buy.pstutas = 0
  228.     and ds.VchType IN(11,161,189,45,89,26,6,34,73,28,142,115,116,143,128,129,85,93,84,36,180,181,185,186,187,188,192,197)
  229.     UNION ALL
  230.     select buy.PtypeId,p.pusercode,p.pfullname,ds.Vchcode,CASE WHEN ds.vchtype IN (11,45,142) THEN ds.GatherBtypeId else ds.bTypeId end btypeid,ds.bTypeId buybtypeid,
  231.     ds.summary,ds.VchType,ds.BillType,ds.etypeid,ds.Date,ds.GatheringDate,ds.Number,
  232.     CASE WHEN buy.vchtype = 142 AND  buy.tax_total > 0 THEN -buy.tax_total ELSE ABS(buy.tax_total) END total,
  233.     CASE WHEN buy.vchtype = 142 AND  buy.tax_total > 0 THEN -buy.AssQty ELSE ABS(buy.AssQty) END   qty,buy.DlyOrder,buy.unit, 0 isoverpay,buy.kwtypeid
  234.     ,ds.fctypeid,ds.fcrate,CASE WHEN buy.vchtype = 142 AND  buy.tax_total > 0 THEN -sign(buy.tax_total)*(fc.tax_total) ELSE ABS(fc.tax_total) END fctotal
  235.     from Dlyndx ds,dbo.DlySale buy LEFT JOIN Fcdly fc ON buy.Vchcode = fc.vchcode AND buy.Vchtype = fc.vchtype AND buy.DlyOrder = fc.dlyorder AND buy.PtypeId = fc.ptypeid
  236.     LEFT JOIN ptype p ON buy.PtypeId = p.ptypeid WHERE ds.Vchcode = buy.Vchcode AND (draft=2) AND (RedOld=''F'') AND ds.VchType IN(11,26,45,89,142) and '+ @CondRetail +' and buy.pstutas <> 1
  237.     and ds.VchType IN(11,161,189,45,89,26,6,34,73,28,142,115,116,143,128,129,85,93,84,36,180,181,185,186,187,188,192,197)
  238.     UNION ALL
  239.     select buy.IStypeid PtypeId,i.pusercode,i.pfullname,ds.Vchcode, ds.GatherBtypeId btypeid,ds.bTypeId buybtypeid,
  240.     ds.summary,ds.VchType,ds.BillType,ds.etypeid,ds.Date,ds.GatheringDate,ds.Number, ABS(buy.tax_total) total, ABS(buy.AssQty) qty,buy.DlyOrder,buy.unit, 0 isoverpay,buy.kwtypeid
  241.     ,ds.fctypeid,ds.fcrate,ABS(buy.tax_total) fctotal  --组合套件不支持外币,所以暂时取本币代替
  242.     from Dlyndx ds,dbo.IsDlySale buy LEFT JOIN ptype i ON buy.IStypeid = i.ptypeid WHERE ds.Vchcode = buy.Vchcode AND (draft=2) AND (RedOld=''F'') AND ds.VchType IN(161,189) and ds.BillBalanceMode in (-1,2) and buy.pstutas = 0
  243.     and ds.VchType IN(11,161,189,45,89,26,6,34,73,28,142,115,116,143,128,129,85,93,84,36,180,181,185,186,187,188,192,197)
  244.     UNION ALL
  245.     select '''' PtypeId,'''' pusercode,'''' pfullname,ds.Vchcode,ds.bTypeId btypeid,ds.bTypeId buybtypeid,
  246.     ds.summary,ds.VchType,ds.BillType,ds.etypeid,ds.Date,ds.GatheringDate,ds.Number, total, 0 qty,0 DlyOrder,'''' unit, 0 isoverpay,'''' kwtypeid
  247.     ,ds.fctypeid,ds.fcrate, ds.fctotal
  248.     from Dlyndx ds WHERE (draft=2) AND (RedOld=''F'') AND ds.VchType IN(115,116,128,129,85,93,84,36,180,181,197) and ds.BillBalanceMode in (-1,2)
  249.     and ds.VchType IN(11,161,189,45,89,26,6,34,73,28,142,115,116,143,128,129,85,93,84,36,180,181,185,186,187,188,192,197)
  250.     UNION ALL
  251.     select buy.PtypeId,p.pusercode,p.pfullname,ds.Vchcode, ds.GatherBtypeId btypeid,ds.bTypeId buybtypeid,
  252.     ds.summary,ds.VchType,ds.BillType,ds.etypeid,ds.Date,ds.GatheringDate,ds.Number, ABS(buy.tax_total) total, ABS(buy.AssQty) qty,buy.DlyOrder,buy.unit, 0 isoverpay,buy.kwtypeid
  253.     ,ds.fctypeid,ds.fcrate,ABS(buy.tax_total) fctotal --委外不支持外币,所以暂时取本币代替
  254.     from Dlyndx ds,dbo.dlyOther buy LEFT JOIN ptype p ON buy.PtypeId = p.ptypeid WHERE ds.Vchcode = buy.Vchcode AND (draft=2) AND (RedOld=''F'') AND ds.VchType IN(146) and ds.BillBalanceMode in (-1,2) and buy.pstutas = 0
  255.     and ds.VchType IN(11,161,189,45,89,26,6,34,73,28,142,115,116,143,128,129,85,93,84,36,180,181,185,186,187,188,192,197)
  256.     UNION ALL
  257.     select buy.PtypeId,p.pusercode,p.pfullname,ds.Vchcode, ds.GatherBtypeId btypeid,ds.bTypeId buybtypeid,
  258.     ds.summary,ds.VchType,ds.BillType,ds.etypeid,ds.Date,ds.GatheringDate,ds.Number, ABS(buy.tax_total) total, ABS(buy.AssQty) qty,buy.DlyOrder,buy.unit, 0 isoverpay,buy.kwtypeid
  259.     ,ds.fctypeid,ds.fcrate,ABS(buy.tax_total) fctotal --不支持外币,所以暂时取本币代替
  260.     from Dlyndx ds,dbo.DlyRation buy LEFT JOIN ptype p ON buy.PtypeId = p.ptypeid WHERE ds.Vchcode = buy.Vchcode AND (draft=2) AND (RedOld=''F'') AND ds.VchType IN(185,186,187,188) and ds.BillBalanceMode in (-1,2) and buy.pstutas = 0
  261.     and ds.VchType IN(11,161,189,45,89,26,6,34,73,28,142,115,116,143,128,129,85,93,84,36,180,181,185,186,187,188,192,197)'
  262.   SET @sz_GatheringDetail_1 = 'UNION ALL
  263.     select buy.PtypeId,p.pusercode,p.pfullname,ds.Vchcode, ds.bTypeId btypeid,ds.bTypeId buybtypeid,
  264.     ds.summary,ds.VchType,ds.BillType,ds.etypeid,ds.Date,ds.GatheringDate,ds.Number, ABS(SaleTotal-Profit) total, ABS(buy.SaleQty) qty,buy.DlyOrder,0 unit, 0 isoverpay,'''' kwtypeid
  265.     ,ds.fctypeid,ds.fcrate,ABS(SaleTotal-Profit) fctotal --联营账款不支持外币,所以暂时取本币代替
  266.     from Dlyndx ds,dbo.Union_Detail buy LEFT JOIN ptype p ON buy.PtypeId = p.ptypeid WHERE ds.Vchcode = buy.Vchcode AND (draft=2) AND (RedOld=''F'') AND ds.VchType = 192 and ds.BillBalanceMode in (-1,2)
  267.     and ds.VchType IN(11,161,189,45,89,26,6,34,73,28,142,115,116,143,128,129,85,93,84,36,180,181,185,186,187,188,192,197)                  
  268.     ) ds  ' + @BTableStr
  269.     + ' WHERE   ds.VchType IN(11,161,189,45,89,26,6,34,73,28,142,115,116,143,128,129,85,93,84,36,180,181,185,186,187,188,192,197) '
  270.     + CASE WHEN @VchCon <> ''
  271.            THEN ' and ' + RIGHT(@VchCon, LEN(@VchCon) - 4)
  272.            ELSE ''
  273.       END + ' ) ds where 1=1'
  274.     + @AllCon + @BuyBTypeCon + ' union all
  275.                 select '''' ptypeid,'''' pusercode,''进货运费'' pfullname,btypeid,'' '' buybtypeid,''运费,进货单''+number summary,VchType,BillType,etypeid,Vchcode,Date,GatheringDate,Number,Total,1 lsFee ,0 feeFlag,-1 feetype,0 qty,0 DlyOrder,'''' unit, 0 isoverpay,'''' kwtypeid,fctypeid,fcrate,fctotal
  276.                 from
  277.                 (select x.feebtypeid btypeid,x.vchtype,x.billtype,x.etypeid,x.vchcode,x.date,x.GatheringDate,x.number,x.draft,x.redold,sum(ds.FeeTotal) total,x.fctypeid,x.fcrate,sum(fc.FeeTotal) fctotal
  278.                     from dbo.fn_GetBtypeTypeid(''' + @OperatorID
  279.                     + ''')  TBRight, dlyndx x,dlybuy ds LEFT JOIN Fcdly fc ON ds.Vchcode = fc.vchcode AND ds.Vchtype = fc.vchtype AND ds.DlyOrder = fc.dlyorder AND ds.PtypeId = fc.ptypeid
  280.                             where TBRight.BTypeid=x.feebtypeid and ds.vchcode=x.vchcode and x.VchType = 34 and x.redold = ''F'' and x.feebtypeid <> '''' and x.BillBalanceMode in (-1,2)
  281.                              
  282.                             group by x.feebtypeid,x.vchtype,x.billtype,x.etypeid,x.vchcode,x.date,x.GatheringDate,x.number,x.draft,x.redold,x.fctypeid,x.fcrate
  283.                             having sum(ds.FeeTotal)<>0
  284.                         ) ds
  285.                         WHERE  VchType = 34 '
  286.                         +
  287.                         '
  288.                         union all
  289.                         select '''' ptypeid,'''' pusercode,''销售运费'' pfullname,btypeid,'' '' buybtypeid,''销售运费,销售单''+number summary,VchType,BillType,etypeid,Vchcode,Date,GatheringDate,Number,
  290.                  Total,1 lsFee ,feeFlag,feetype,0 qty,0 DlyOrder,'''' unit, 0 isoverpay,'''' kwtypeid,fctypeid,fcrate,fctotal
  291.                 from
  292.                 (select FeeBtypeID  btypeid,               
  293.                 CASE WHEN ISNULL(feetype,-1) = 0 THEN 0 ELSE  2  end  feeFlag, -- 0 自付,2 代付结算单位,1  代付运费单位
  294.                 x.vchtype,x.billtype,x.etypeid,x.vchcode,x.date,x.GatheringDate,x.number,x.draft,x.redold,feetype,
  295.                 x.FeeTotal total,x.fctypeid,x.fcrate,x.fcFeeTotal fctotal
  296.                     from dbo.fn_GetBtypeTypeid(''' + @OperatorID
  297.             + ''')  TBRight, dlyndx x  LEFT join DlyndxExte Exte ON x.vchcode = Exte.vchcode
  298.                     where TBRight.BTypeid=x.feebtypeid and x.VchType = 11 and  FeeTotal <> 0 AND x.redold = ''F'' and x.feebtypeid <> ''''
  299.                     AND ((x.BillBalanceMode in (-1,2)) or (x.VchType IN (11, 45) AND x.lsRetail NOT IN (0, 10) and x.BillBalanceMode = 0))
  300.                     --and x.BillBalanceMode in (-1,2)                      
  301.                 ) ds               
  302.                 WHERE   VchType = 11 ' + @AllCon + ' and (draft = 2) AND (RedOld = ''F'')
  303.                          union all
  304.                 select '''' ptypeid,'''' pusercode,''代付运费'' pfullname,btypeid,'' '' buybtypeid,''代付运费,销售单''+number summary,VchType,BillType,etypeid,Vchcode,Date,GatheringDate,Number,
  305.                  Total,1 lsFee ,feeFlag,feetype,0 qty,0 DlyOrder,'''' unit, 0 isoverpay,'''' kwtypeid,fctypeid,fcrate,fctotal
  306.                 from
  307.                 (select x.btypeid,
  308.                 --case WHEN  (ISNULL(feetype,-1) = 0 AND 66 = '  + CAST(@VchType AS VARCHAR(4)) +  ')  OR (ISNULL(feetype,-1) = 1) then 1 else -1 end fee,
  309.                 1  feeFlag, -- 0 自付,2 代付结算单位,2  代付运费单位
  310.                 x.vchtype,x.billtype,x.etypeid,x.vchcode,x.date,x.GatheringDate,x.number,x.draft,x.redold,feetype,
  311.                 x.FeeTotal total,x.fctypeid,x.fcrate,x.fcFeeTotal fctotal
  312.                     from dbo.fn_GetBtypeTypeid(''' + @OperatorID
  313.             + ''')  TBRight, dbo.f_GetGatherBtypeId_Ndx() x , DlyndxExte Exte
  314.                     where TBRight.BTypeid= x.btypeid AND x.vchcode = Exte.vchcode AND x.Vchtype = 11
  315.                     and  FeeTotal <> 0 AND x.redold = ''F'' and x.feebtypeid <> '''' AND exte.feetype = 1
  316.                     AND ((x.BillBalanceMode in (-1,2)) or (x.VchType IN (11, 45) AND x.lsRetail NOT IN (0, 10) and x.BillBalanceMode = 0))
  317.                     --and x.BillBalanceMode in (-1,2)          
  318.                 ) ds               
  319.                 WHERE   VchType = 11 '
  320.                  + @AllCon + ' and (draft = 2) AND (RedOld = ''F'')
  321.                          union all 
  322.                         select '''' ptypeid,'''' pusercode,''加工费'' pfullname,btypeid,'' '' buybtypeid, CASE WHEN VchType = 146  THEN ''加工费,委外加工完工单'' ELSE ''加工费,委外完工退货单'' END + number summary,VchType,BillType,etypeid,Vchcode,Date,GatheringDate,
  323.                             Number,Total,1 lsFee ,-1 feeFlag,-1 feetype,0 qty,0 DlyOrder,'''' unit, 0 isoverpay,'''' kwtypeid,''00001'' as fctypeid,1 fcrate,Total fctotal --不支持外币,所以暂时取本币代替
  324.                         from
  325.                         (select x.btypeid,x.vchtype,x.billtype,x.etypeid,x.vchcode,x.date,x.GatheringDate,x.number,x.draft,x.redold,CASE WHEN x.VchType = 146 then 1 else -1 end * sum(ds.prointaxtotal+ds.OtherTotal) total,x.fctypeid,x.fcrate,0 fctotal
  326.                             from dbo.fn_GetBtypeTypeid(''' + @OperatorID
  327.                     + ''')  TBRight, dlyndx x,dlyother ds
  328.                             where TBRight.BTypeid = x.BTypeid and ds.vchcode = x.vchcode and x.redold = ''F'' and x.BillBalanceMode in (-1,2)
  329.                             group by x.btypeid, x.vchtype,x.billtype,x.etypeid,x.vchcode,x.date,x.GatheringDate,x.number,x.draft,x.redold,x.fctypeid,x.fcrate
  330.                             having sum(ds.prointaxtotal+ds.OtherTotal) <> 0
  331.                 ) ds
  332.                 WHERE  VchType in (146,191) ' + @AllCon
  333.             + CASE WHEN @VchCon <> ''
  334.                    THEN ' and ' + RIGHT(@VchCon, LEN(@VchCon) - 4)
  335.                    ELSE ''
  336.               END
  337.             + ' and (draft = 2) AND (RedOld = ''F'')                                           
  338.            
  339.             ) t LEFT JOIN VchType v ON t.VchType  = v.VchType '
  340.   SELECT @viewSql2 = '
  341.          d
  342.         INNER JOIN dbo.Dlyndx ndx ON ndx.Vchcode = d.Vchcode
  343.         LEFT OUTER JOIN  dbo.view_zz_GatheringPtypeDetail  g
  344.         ON d.Vchcode = g.VchCode AND d.ptypeid = g.ptypeid AND g.DlyOrder = d.DlyOrder and d.lsFee=g.lsFee AND d.feeflag = g.lsfeeflag ' +  
  345.         CASE WHEN @VchType = 0  THEN '' ELSE ' AND (((4 = '  + CAST(@VchType AS VARCHAR(4)) +  ') AND (g.TYPE = 1)) OR ((66 = '  + CAST(@VchType AS VARCHAR(4)) +  ') AND (g.TYPE = 2)))  ' END + '
  346.         GROUP BY d.btypeid,d.buybtypeid,d.summary,d.VchType,d.BillType,g.type,
  347.         d.Vchcode,d.Date,d.GatheringDate,d.Number,d.Total,d.etypeid,d.VchName,d.lsFee,d.feeflag, d.feetype,d.ptypeid,d.qty,d.DlyOrder,d.unit,d.isoverpay,d.kwtypeid,d.pusercode,d.pfullname,ndx.lsRetail,d.fctypeid,d.fcrate,d.fctotal'
  348.  
  349.     SELECT  @SQL = '
  350.     SELECT x.*,ISNULL(d.IgnoreTotal,0) AS IgnoreTotal,ISNULL(d.fcIgnoreTotal,0) AS fcIgnoreTotal  ' +@B1TypeColumStr+ @B3TypeColumStr+' INTO #temp  FROM (SELECT a.ptypeid,a.pusercode,a.pfullname,a.btypeid,a.buybtypeid,a.VchName,a.summary,a.VchType,a.BillType,
  351.     a.Vchcode,a.Date,a.GatheringDate, a.Number,a.fctypeid,a.fcrate,a.fcVchTotal,a.fcGatheringTotal,a.fcBalanceTotal, a.VchTotal,a.GatheringTotal,a.gatheringqty,a.BalanceTotal,a.balanceqty,
  352.     a.etypeid,a.lsFee,a.feeFlag,a.feetype,a.vchqty,a.DlyOrder,a.unit,a.isoverpay,a.kwtypeid,a.IsFinished,
  353.     CASE WHEN a.VchType IN (11, 45) AND a.lsRetail NOT IN (0, 10) THEN 1 ELSE ISNULL(b.SubCount,0) END  AS SubCount
  354.     FROM (SELECT ds.ptypeid,ds.pusercode,ds.pfullname,ds.btypeid,ds.buybtypeid,ds.VchName,ds.summary,ds.VchType,ds.BillType,
  355.     ds.Vchcode,ds.Date,ds.GatheringDate, ds.Number, ds.fctypeid,ds.fcrate,isnull(ds.fcVchTotal,0) fcVchTotal,
  356.     CASE WHEN ds.VchType IN (11, 45) AND ds.lsRetail NOT IN (0, 10) THEN VchTotal ELSE isnull(ds.fcGatheringTotal,0) END AS fcGatheringTotal,
  357.     CASE WHEN ds.VchType IN (11, 45) AND ds.lsRetail NOT IN (0, 10) THEN 0 ELSE isnull(ds.fcBalanceTotal,0) END AS fcBalanceTotal,
  358.     ds.VchTotal,
  359.     CASE WHEN ds.VchType IN (11, 45) AND ds.lsRetail NOT IN (0, 10) THEN VchTotal ELSE ds.GatheringTotal END AS GatheringTotal,
  360.     CASE WHEN ds.VchType IN (11, 45) AND ds.lsRetail NOT IN (0, 10) THEN ds.vchqty ELSE ds.gatheringqty END AS gatheringqty,
  361.     CASE WHEN ds.VchType IN (11, 45) AND ds.lsRetail NOT IN (0, 10) THEN 0 ELSE ds.BalanceTotal END AS BalanceTotal,
  362.     CASE WHEN ds.VchType IN (11, 45) AND ds.lsRetail NOT IN (0, 10) THEN 0 ELSE ds.balanceqty END AS balanceqty,
  363.     ds.etypeid,ds.lsFee,ds.feeFlag,ds.feetype,ds.vchqty,ds.DlyOrder,ds.unit,ds.isoverpay,ds.kwtypeid,ds.lsRetail,
  364.     CASE WHEN ds.VchType IN (11, 45) AND ds.lsRetail NOT IN (0, 10) THEN 1 ELSE
  365.     CASE WHEN ds.lsfee = 1 OR ptypeid = ''优惠'' THEN CASE WHEN BalanceTotal = 0 THEN 1   WHEN BalanceTotal = VchTotal THEN 0 ElSE 2 END ELSE ISNULL(ifn.IsFinished,0) END END AS IsFinished
  366.     FROM ('
  367.     SELECT @SQL2 =  @viewSql2  + ') ds
  368.         LEFT JOIN (
  369.             SELECT gpd.Vchcode,CASE WHEN d.Total = ABS(SUM(gpd.GatheringTotal)) THEN 1
  370.             WHEN ABS(SUM(gpd.GatheringTotal)) < d.Total AND ABS(SUM(gpd.GatheringTotal)) > 0 THEN 2 ELSE 0 END IsFinished
  371.             FROM view_zz_GatheringPtypeDetail gpd LEFT JOIN dbo.Dlyndx d ON d.Vchcode = gpd.vchCode
  372.             WHERE lsFee = 0 AND ptypeid <> ''优惠'' GROUP BY gpd.Vchcode,d.Total
  373.         ) ifn  ON ds.vchcode = ifn.vchcode
  374.                 WHERE 1=1' + @AllCon  + CASE WHEN @VchCon <> ''
  375.                    THEN ' and (' + RIGHT(@VchCon, LEN(@VchCon) - 4) + '  OR feetype IN(0,1))'
  376.                    ELSE ''
  377.               END
  378.             + '  
  379.             ) a
  380.             Left JOIN
  381.             (SELECT Count(1) SubCount,VchCode,lsFee,DlyOrder,lsfeeflag  
  382.             FROM   dbo.view_zz_GatheringPtypeDetail  ds ' + @BTableStr + '
  383.             WHERE  1=1 ' + @BTypeCon + @VchCon + '  
  384.             GROUP BY VchCode, lsFee,DlyOrder,lsfeeflag
  385.             ) b ON a.VchCode = b.VchCode and a.lsFee = b.lsFee AND a.feeflag = b.lsfeeflag AND a.dlyOrder = b.DlyOrder
  386.         ) x
  387.         LEFT JOIN
  388.         (SELECT VchCode,Total AS IgnoreTotal,fcTotal AS fcIgnoreTotal,0 lsFee
  389.         FROM  DlyA ds ' + @BTableStr + '
  390.         WHERE  1=1 ' + @AllCon + @VchCon + ' and (ATypeID = ('''
  391.             + @YHAType
  392.             + ''')  AND vchtype not in (10,35,36,115,116,128,129,130,131,4,66,146,197))
  393.         ) d ON d.VchCode = x.VchCode and d.lsFee = x.lsFee ' +@B1TypeTableStr  +@B3TypeTableStr +'
  394.         '
  395.         IF(@ETypeID = '00000') -- 录入经手人不查期初数据
  396.             SELECT @SQL1 = '
  397.                 union all ' + @IniArAp
  398.     PRINT(@sz_GatheringDetail)
  399.     PRINT(@sz_GatheringDetail_1)       
  400.     print(@SQL)
  401.     PRINT(@viewSql )
  402.     PRINT(@SQL2)
  403.     PRINT(@SQL1)
  404.     print(' SELECT a.*,ISNULL(t.unit1,'''') uname FROM #temp a
  405.             LEFT JOIN (SELECT ptypeid,ucode,unit1 FROM fn_ItemPtypeAll(''00000'',-1)) t ON a.unit = t.ucode
  406.             AND a.ptypeid = t.ptypeid WHERE 1 = 1 ' + @PTypeCon +' ORDER BY VchCode,date,lsFee,dlyorder  
  407.             DROP TABLE #temp
  408.             drop table #Tmp')
  409.     EXEC(@sz_GatheringDetail + @sz_GatheringDetail_1 + @SQL + @viewSql + @SQL2 + @SQL1 +
  410.         ' SELECT a.*,ISNULL(t.unit1,'''') uname FROM #temp a LEFT JOIN (SELECT ptypeid,ucode,unit1
  411.             FROM fn_ItemPtypeAll(''00000'',-1)) t ON a.unit = t.ucode AND a.ptypeid = t.ptypeid WHERE 1 = 1 ' +
  412.             @PTypeCon +' ORDER BY VchCode,date,lsFee,dlyorder  
  413.             DROP TABLE #temp
  414.             drop table #Tmp')
  415.  
  416.     RETURN @@ERROR
  417.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement