表1:科目余额表_自动抵消穿透

更新日期:2022-05-29

  1. begin
  2. select distinct accidname,companyname,hbcompanyname
  3. into #temp_CO_JC_Account
  4. from [dbo].[CO_JC_Account]
  5. where 1=1
  6. and hbcompanyname in ($(合并报表_本方单位))
  7. select tt.*,t1.igrade,t1.ccode as ccode2
  8. into #temp_kemuyuechuantou111
  9. from
  10. (
  11. select dbtype,dbName ,companyName,accidname,iyear,iperiod,isbook,ruleversion,rownum,indexname,ccode,jdfx,jsfs,hesuan
  12. from [dbo].[VW_CO_JC_BalanceSheet_zb]
  13. where 1=1
  14. and accidname in (select accidname from #temp_CO_JC_Account)
  15. and iyear=$(合并报表-年度)
  16. and iperiod=(case when $(合并报表-期间) =0 then 1 else $(合并报表-期间) end )
  17. and isbook='$(合并报表-是否包含未记账)'
  18. $(合并报表_报表行号)
  19. $(合并报表_指标名称2)
  20. union all
  21. select dbtype,dbName ,companyName,accidname,iyear,iperiod,isbook,ruleversion,rownum,indexname,ccode,jdfx,jsfs,NULL as hesuan
  22. from [dbo].[VW_CO_JC_Profit_zb]
  23. where 1=1
  24. and accidname in (select accidname from #temp_CO_JC_Account)
  25. and iyear=$(合并报表-年度)
  26. and iperiod=(case when $(合并报表-期间) =0 then 1 else $(合并报表-期间) end )
  27. and isbook='$(合并报表-是否包含未记账)'
  28. $(合并报表_报表行号)
  29. $(合并报表_指标名称2)
  30. )as tt
  31. left join
  32. (
  33. select distinct dbtype,dbName ,companyName,accidname,ccode,igrade,iyear
  34. from CO_JC_Ccode
  35. where 1=1
  36. and accidname in (select accidname from #temp_CO_JC_Account)
  37. and iyear=$(合并报表-年度)
  38. ) as t1 on tt.dbtype=t1.dbtype and tt.accidname=t1.accidname and tt.iyear=t1.iyear and left(t1.ccode,len(tt.ccode))=tt.ccode
  39. where 1=1
  40. and (case when len(isnull(tt.jdfx,N''))<1 and len(isnull(tt.hesuan,N''))<1 then tt.ccode else t1.ccode end) =t1.ccode
  41. select
  42. cast(tt.iyear as varchar(10))+'年'+cast(tt.iperiod as varchar(4))+'月' as iyperiod,
  43. tt.dbtype,tt.dbName ,tt.companyName,tt.accidname,tt.iyear,tt.iperiod,
  44. accountTypeID,accountTypeName,accountCode,
  45. currencyCode,currencyName,igrade,
  46. case when igrade=6 then N' '+accountCode2 when igrade=5 then N' '+accountCode2 when igrade=4 then N' '+accountCode2 when igrade=3 then N' '+accountCode2 when igrade=2 then N' '+accountCode2
  47. else accountCode2 end as accountCode2,
  48. accountName,ccodefx,
  49. case when isdetail=0 then
  50. (case when igrade=6 then N' '+accountName when igrade=5 then N' '+accountName when igrade=4 then N' '+accountName when igrade=3 then N' '+accountName when igrade=2 then N' '+accountName
  51. else accountName end) else (case when igrade=6 then N' '+accountName2 when igrade=5 then N' '+accountName2 when igrade=4 then N' '+accountName2 when igrade=3 then N' '+accountName2 when igrade=2 then N' '+accountName2
  52. else accountName2 end) end as accountName2,
  53. localqc,localjfqc,localdfqc,localjffs,localdffs,localytdjffs,localytddffs,localqm,localjfqm,localdfqm
  54. from
  55. (
  56. SELECT
  57. dbtype,dbName ,companyName,accidname,iyear,iperiod,isbook,
  58. accountTypeID,accountTypeCode,accountTypeName,calDirection,
  59. --currencyID,accountID,
  60. currencyCode,currencyName,
  61. accountCode,accountName,
  62. (Case When len(isnull(isauxaccdepartmentcode,N''))<1 then N'' Else N' [' + isnull(isauxaccdepartmentcode,N'') + N'] ' End) + ISNULL(isauxaccdepartment, N'')
  63. + (Case When len(isnull(isauxaccpersoncode,N''))<1 then N'' Else N' [' + isnull(isauxaccpersoncode,N'') + N'] ' End) + ISNULL(isauxaccperson, N'')
  64. + (Case When len(isnull(isauxacccustomercode,N''))<1 then N'' Else N' [' + isnull(isauxacccustomercode,N'') + N'] ' End) + ISNULL(isauxacccustomer, N'')
  65. + (Case When len(isnull(isauxaccinventorycode,N''))<1 then N'' Else N' [' + isnull(isauxaccinventorycode,N'') + N'] ' End) + ISNULL(isauxaccinventory, N'')
  66. + (Case When len(isnull(isauxaccprojectcode,N''))<1 then N'' Else N' [' + isnull(isauxaccprojectcode,N'') + N'] ' End) + ISNULL(isauxaccproject, N'')
  67. + (Case When len(isnull(isexauxacc1code,N''))<1 then N'' Else N' [' + isnull(isexauxacc1code,N'') + N'] ' End) + ISNULL(isexauxacc1, N'')
  68. + (Case When len(isnull(isexauxacc2code,N''))<1 then N'' Else N' [' + isnull(isexauxacc2code,N'') + N'] ' End) + ISNULL(isexauxacc2, N'')
  69. + (Case When len(isnull(isexauxacc3code,N''))<1 then N'' Else N' [' + isnull(isexauxacc3code,N'') + N'] ' End) + ISNULL(isexauxacc3, N'')
  70. + (Case When len(isnull(isexauxacc4code,N''))<1 then N'' Else N' [' + isnull(isexauxacc4code,N'') + N'] ' End) + ISNULL(isexauxacc4, N'')
  71. + (Case When len(isnull(isexauxacc5code,N''))<1 then N'' Else N' [' + isnull(isexauxacc5code,N'') + N'] ' End) + ISNULL(isexauxacc5, N'')
  72. + (Case When len(isnull(isexauxacc6code,N''))<1 then N'' Else N' [' + isnull(isexauxacc6code,N'') + N'] ' End) + ISNULL(isexauxacc6, N'')
  73. + (Case When len(isnull(isexauxacc7code,N''))<1 then N'' Else N' [' + isnull(isexauxacc7code,N'') + N'] ' End) + ISNULL(isexauxacc7, N'')
  74. + (Case When len(isnull(isexauxacc8code,N''))<1 then N'' Else N' [' + isnull(isexauxacc8code,N'') + N'] ' End) + ISNULL(isexauxacc8, N'')
  75. + (Case When len(isnull(isexauxacc9code,N''))<1 then N'' Else N' [' + isnull(isexauxacc9code,N'') + N'] ' End) + ISNULL(isexauxacc9, N'')
  76. + (Case When len(isnull(isexauxacc10code,N''))<1 then N'' Else N' [' + isnull(isexauxacc10code,N'') + N'] ' End) + ISNULL(isexauxacc10, N'') as accountName2,
  77. case when accountDcdirection='652' then '借' else '贷' end as ccodefx,
  78. --accountID2,accountUnit,
  79. case when isdetail=0 then accountCode else N'' end as accountCode2,
  80. isEndNode,isdetail,
  81. isauxacccustomer,isauxaccdepartment,
  82. isauxaccperson,isauxaccproject,isauxaccinventory,
  83. isexauxacc1,isexauxacc2,isexauxacc3,isexauxacc4,isexauxacc5,
  84. isexauxacc6,isexauxacc7,isexauxacc8,isexauxacc9,isexauxacc10,
  85. --quantityBalance,origAmountBalance,
  86. amountBalance as localqc,
  87. --case when amountBalance>0 then '借' when amountBalance<0 then '贷' else '平' end as localqcfx,
  88. case when amountBalance>0 then amountBalance else 0 end as localjfqc,
  89. case when amountBalance<0 then abs(amountBalance) else 0 end as localdfqc,
  90. --quantityDr,quantityCr,origAmountDr,origAmountCr,
  91. amountDr as localjffs,
  92. amountCr as localdffs,
  93. --cumQuantityDr,cumQuantityCr,
  94. --cumOrigAmountDr,cumOrigAmountCr,
  95. cumAmountDr as localytdjffs,cumAmountCr as localytddffs,
  96. --endquantityBalance,endorigAmountBalance,
  97. endamountBalance as localqm,
  98. case when endamountBalance>0 then endamountBalance else 0 end as localjfqm,
  99. case when endamountBalance<0 then abs(endamountBalance) else 0 end as localdfqm
  100. FROM CO_JC_ERP_Balance
  101. where 1=1
  102. and accidname in (select accidname from #temp_CO_JC_Account)
  103. and ISNULL(isauxacccustomer, N'') in ($(合并报表_对方单位))
  104. and iyear=$(合并报表-年度)
  105. and iperiod=(case when $(合并报表-期间) =0 then 1 else $(合并报表-期间) end )
  106. and isbook='$(合并报表-是否包含未记账)'
  107. ) as tt
  108. inner join #temp_kemuyuechuantou111 as t1 on tt.dbtype=t1.dbtype and tt.accidname=t1.accidname and tt.iyear=t1.iyear
  109. and left(tt.accountcode,len(t1.ccode2))=t1.ccode2
  110. --and tt.accountcode=t1.ccode2
  111. order by accounttypecode,accountcode,igrade, accountName2
  112. drop table #temp_kemuyuechuantou111
  113. drop table #temp_CO_JC_Account
  114. end