接单系统财务统计.sql 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. declare @sdate nvarchar(20) --开始时间;
  2. declare @edate nvarchar(20) --结束时间;
  3. set @sdate = '2015-01-07'
  4. set @edate = '2015-07-07'
  5. --财务统计;
  6. --总收入不包含储值卡扣款;
  7. --营业收入不包含其他收入;
  8. --净收入 = 总收入 - 总支出;
  9. --成交订单数: 收了预约收款的订单;
  10. --补款:预约补款;
  11. --摄影二销\化妆二销\选片二销:后期收款;
  12. /*
  13. 总收入 = 订单定金(支付方式 != 储值卡扣款), 订单补款(支付方式 != 储值卡扣款),订单后期(支付方式 != 储值卡扣款),其他收入(支付方式 != 储值卡扣款),积分兑换,会员充值;
  14. 解析:会员充值 有可能会用来交订单定金、订单补款、订单后期,所以这三种收款不能是储值卡扣款的;
  15. 营业收入 = 订单定金(支付方式可以是储值卡扣款), 订单补款(支付方式可以是储值卡扣款), 订单后期(支付方式可以是储值卡扣款);
  16. */
  17. /*
  18. select * FROM gudingfeiyong WHERE dat >= @sdate and dat <= @edate
  19. select * FROM singleincome WHERE dat >= @sdate and dat <= @edate and (sale2type is null or sale2type = '')
  20. select * FROM dindanbukuanview2 WHERE date >= @sdate and date <= @edate
  21. select * FROM singleincomemoney WHERE date >= @sdate and date <= @edate
  22. select * FROM memberview WHERE date >= @sdate and date <= @edate
  23. */
  24. go
  25. --
  26. --注:singleincome表中,sale2type is null or sale2type = '' 表示的是其他收入; 非空的情况下,需要与singleincomemoney结合成视图singleincomemoenyview才是二销收入;
  27. --
  28. CREATE PROCEDURE SP_FINANCIAL_REPORTING
  29. (
  30. @STRAT_DATE NVARCHAR(20), -- 查询起始日期;
  31. @ENDOF_DATE NVARCHAR(20), -- 查询结束日期;
  32. @SUM1 FLOAT OUTPUT, -- 总收入;
  33. @SUM2 FLOAT OUTPUT, -- 营业收入;
  34. @SUM3 FLOAT OUTPUT, -- 总支出;
  35. @SUM4 FLOAT OUTPUT, -- 净收入;
  36. @SUM5 FLOAT OUTPUT, -- 订单数;
  37. @SUM6 FLOAT OUTPUT, -- POS机刷卡金额;
  38. @SUM7 FLOAT OUTPUT, -- 储值卡扣款金额;
  39. @SUM8 FLOAT OUTPUT, -- 积分兑换;
  40. @SUM9 FLOAT OUTPUT -- 柜台现金 如果查询的是当天的,需要显示该值,否则不显示柜台现金(柜台现金只有当天的才有意义);
  41. ) AS
  42. BEGIN
  43. --1总收入,不包含'储值卡扣款',但包含其他收入:
  44. SET @SUM1 = ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM singleincome WHERE paytype <> '储值卡扣款' and dat >= @STRAT_DATE and dat <= @ENDOF_DATE and (sale2type is null or sale2type = '')),0) --不包含'储值卡扣款'的'其他收入';
  45. --SET @SUM1 = ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM singleincome WHERE paytype <> '储值卡扣款' and dat >= @STRAT_DATE and dat <= @ENDOF_DATE ),0) --不包含'储值卡扣款'
  46. SET @SUM1 = @SUM1 + ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM dindanbukuanview2 WHERE paytype <> '储值卡扣款' and date >= @STRAT_DATE and date <= @ENDOF_DATE),0) --不包含'储值卡扣款'
  47. SET @SUM1 = @SUM1 + ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM singleincomemoneyview WHERE paytype <> '储值卡扣款' and date >= @STRAT_DATE and date <= @ENDOF_DATE),0) --不包含'储值卡扣款'
  48. SET @SUM1 = @SUM1 + ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM memberview WHERE date >= @STRAT_DATE and date <= @ENDOF_DATE),0)
  49. --SELECT @SUM1 '总收入'
  50. --2.营业收入,只包含订单收入,不包含其他收入;
  51. --SET @SUM2 = ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM singleincome WHERE dat >= @STRAT_DATE and dat <= @ENDOF_DATE and (sale2type is null or sale2type = '')),0) -- 不包含其他收入,只有订单二销;
  52. SET @SUM2 = ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM dindanbukuanview2 WHERE date >= @STRAT_DATE and date <= @ENDOF_DATE),0)
  53. SET @SUM2 = @SUM2 + ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM singleincomemoneyview WHERE date >= @STRAT_DATE and date <= @ENDOF_DATE),0)
  54. --SELECT @SUM2 '营业收入'
  55. --3.总支出;
  56. SET @SUM3 = ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM gudingfeiyong WHERE dat >= @STRAT_DATE and dat <= @ENDOF_DATE),0)
  57. --SELECT @SUM3 '总支出'
  58. --4.净收入 = 总收入 - 总支出;
  59. SET @SUM4 = ISNULL((@SUM1 - @SUM3),0)
  60. --SELECT @SUM4 '净收入'
  61. --5.成交订单数 = 过去补款的订单不算,只算规定时间内的订单预约收款(交订金)数量; --bz字段命名歧义;
  62. SET @SUM5 = ISNULL((SELECT count(DISTINCT id) FROM dindanbukuanview2 WHERE bz = '预约收款' and date >= @STRAT_DATE and date <= @ENDOF_DATE),0)
  63. --SELECT @SUM5 '订单数'
  64. --6.POS机刷卡金额,其他收入、订单收入、订单二销、会员充值中使用POS机支付方式的所有收款记录;
  65. SET @SUM6 = ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM singleincome WHERE paytype = 'POS机刷卡' and dat >= @STRAT_DATE and dat <= @ENDOF_DATE and (sale2type is null or sale2type = '')),0)
  66. SET @SUM6 = @SUM6 + ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM dindanbukuanview2 WHERE paytype = 'POS机刷卡' and date >= @STRAT_DATE and date <= @ENDOF_DATE),0)
  67. SET @SUM6 = @SUM6 + ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM singleincomemoneyview WHERE paytype = 'POS机刷卡' and date >= @STRAT_DATE and date <= @ENDOF_DATE),0)
  68. SET @SUM6 = @SUM6 + ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM memberview WHERE moneytype = 'POS机刷卡' and date >= @STRAT_DATE and date <= @ENDOF_DATE),0)
  69. --SELECT @SUM6 'POS机刷卡金额'
  70. --7.储值卡扣款金额,其他收入、订单收入、其他二销中使用储值卡扣款的所有记录;
  71. SET @SUM7 = ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM singleincome WHERE paytype = '储值卡扣款' and dat >= @STRAT_DATE and dat <= @ENDOF_DATE and (sale2type is null or sale2type = '')),0)
  72. SET @SUM7 = @SUM7 + ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM dindanbukuanview2 WHERE paytype = '储值卡扣款' and date >= @STRAT_DATE and date <= @ENDOF_DATE),0)
  73. SET @SUM7 = @SUM7 + ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM singleincomemoneyview WHERE paytype = '储值卡扣款' and date >= @STRAT_DATE and date <= @ENDOF_DATE),0)
  74. --SELECT @SUM7 '储值卡扣款金额'
  75. --8.积分兑换;
  76. SET @SUM8 = ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM singleincome WHERE paytype = '积分兑换' and dat >= @STRAT_DATE and dat <= @ENDOF_DATE and (sale2type is null or sale2type = '')),0)
  77. SET @SUM8 = @SUM8 + ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM dindanbukuanview2 WHERE paytype = '积分兑换' and date >= @STRAT_DATE and date <= @ENDOF_DATE),0)
  78. SET @SUM8 = @SUM8 + ISNULL((SELECT SUM(convert(FLOAT,money,10)) FROM singleincomemoneyview WHERE paytype = '积分兑换' and date >= @STRAT_DATE and date <= @ENDOF_DATE),0)
  79. --SELECT @SUM8 '积分兑换'
  80. --9.柜台现金 = 净收入 - 积分兑换 - POS机刷卡;
  81. SET @SUM9 = ISNULL((@SUM4 - @SUM8 - @SUM6),0)
  82. --SELECT @SUM9 '柜台现金'
  83. END
  84. DECLARE @return_value int,@SUM1 float,@SUM2 float,@SUM3 float,@SUM4 float,@SUM5 float,@SUM6 float,@SUM7 float,@SUM8 float,@SUM9 float
  85. EXEC @return_value = [dbo].[SP_FINANCIAL_REPORTING]
  86. @STRAT_DATE = N'2015-07-01',@ENDOF_DATE = N'2015-07-30',
  87. @SUM1 = @SUM1 OUTPUT,@SUM2 = @SUM2 OUTPUT,
  88. @SUM3 = @SUM3 OUTPUT,@SUM4 = @SUM4 OUTPUT,
  89. @SUM5 = @SUM5 OUTPUT,@SUM6 = @SUM6 OUTPUT,
  90. @SUM7 = @SUM7 OUTPUT,@SUM8 = @SUM8 OUTPUT,
  91. @SUM9 = @SUM9 OUTPUT
  92. SELECT @SUM1 as N'总收入',
  93. @SUM2 as N'营业收入',
  94. @SUM3 as N'总支出',
  95. @SUM4 as N'净收入',
  96. @SUM5 as N'成交订单数',
  97. @SUM6 as N'POS机刷卡金额',
  98. @SUM7 as N'储值卡扣款金额',
  99. @SUM8 as N'积分兑换',
  100. @SUM9 as N'柜台现金'
  101. GO