流程客户.sql 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_ReservationPhotographyTime]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
  2. DROP FUNCTION [dbo].[fn_ReservationPhotographyTime]
  3. GO
  4. CREATE function [dbo].[fn_ReservationPhotographyTime](@CreateDateTime datetime,@ReservationTime datetime, @ordtype char(1))
  5. /******
  6. --获取预约拍照时间 如果是成长套系时返回开单时按排的预计拍照时间
  7. --如果不是成长套系时返回的是开单时间
  8. 创建人:刘工
  9. 创建日期:2015-7-30
  10. 修改人:
  11. 修改说明:
  12. 修改日期:
  13. ******/
  14. Returns datetime
  15. As
  16. Begin
  17. Declare @ReturnDate datetime
  18. set @ReturnDate=@CreateDateTime
  19. if(@ordtype='1' and len(@ReservationTime)>=10)
  20. set @ReturnDate=@ReservationTime
  21. Return @ReturnDate
  22. End
  23. GO
  24. --摄影视图 记录拍摄状态 拍摄时间
  25. IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[Vw_PhotographyView]'))
  26. DROP VIEW [dbo].[Vw_PhotographyView]
  27. GO
  28. CREATE VIEW [dbo].[Vw_PhotographyView]
  29. AS
  30. select
  31. max(Ordpg_Number) as Ordpg_Number,
  32. Ordpg_ViceNumber,
  33. max(Ordpg_PhotographyTime) as Ordpg_PhotographyTime,
  34. --拍摄状态 0:未拍 Ordpg_PhotographyStatus=Ordpg_PhotographyCount: OK(已拍) 否则:拍照中
  35. sum(CONVERT(int,Ordpg_PhotographyStatus)) as Ordpg_PhotographyStatus,
  36. Count(Ordpg_PhotographyStatus) as Ordpg_PhotographyCount
  37. from tb_ErpOrdersPhotography
  38. group by Ordpg_ViceNumber
  39. GO
  40. --订单商品取件状态视图
  41. IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[Vw_OrderProductPickupView]'))
  42. DROP VIEW [dbo].[Vw_OrderProductPickupView]
  43. GO
  44. CREATE VIEW [dbo].[Vw_OrderProductPickupView]
  45. AS
  46. select
  47. max(OPlist_OrderNumber) as OPlist_OrderNumber,
  48. OPlist_ViceNumber,
  49. sum(CONVERT(int,[OPlist_PickupStatus])) as OPlist_PickupStatus,
  50. max(OPlist_PickupTime) as OPlist_PickupTime,
  51. sum(CONVERT(int,OPlist_CompletedStatus)) as OPlist_CompletedStatus,
  52. max(OPlist_CompletedTime) as OPlist_CompletedTime,
  53. count(id) productCount
  54. from [tb_ErpOrderProductList]
  55. where OPlist_Type ='2'
  56. group by OPlist_ViceNumber
  57. GO
  58. --订单流程客户 只显示未取件的客户
  59. IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[Vw_OrderProcessCustomer]'))
  60. DROP VIEW [dbo].[Vw_OrderProcessCustomer]
  61. GO
  62. CREATE VIEW [dbo].[Vw_OrderProcessCustomer]
  63. AS
  64. --订单流程客户 只显示未取件的客户
  65. SELECT tb_ErpOrder.ID,dbo.tb_ErpOrder.Ord_DividedShop, dbo.tb_ErpOrder.Ord_Number, dbo.tb_ErpOrderDigital.Ordv_ViceNumber,
  66. dbo.tb_ErpOrder.Ord_Class,CONVERT(int,Ord_Type) as Ord_Type, Ord_OrderClass,
  67. CASE Ord_SinceOrderNumber WHEN '' THEN Ord_Number ELSE Ord_SinceOrderNumber END AS 订单号,
  68. dbo.fn_CheckOrderType(dbo.tb_ErpOrder.Ord_Type) AS 订单类型,
  69. Cus_Name AS 客户姓名,
  70. Cus_Name_py AS 客户拼音,
  71. Cus_Sex_cs AS 客户性别,
  72. Cus_Telephone AS 客户电话,
  73. [Age_String] AS 年龄,
  74. [Age_Day] AS 天,
  75. [Age_Year] AS 年,
  76. dbo.tb_ErpOrder.Ord_PhotographyCategory AS 套系类别,
  77. dbo.tb_ErpOrder.Ord_CustomerSource AS 客户来源,
  78. dbo.tb_ErpOrder.Ord_SeriesName AS 套系名称,
  79. dbo.tb_ErpOrder.Ord_SeriesPrice AS 套系价格,
  80. (select stuff((select ',' + dbo.fn_CheckUserIDGetUserName( OrdPe_OrdersPerson) from tb_ErpOrdersPerson where OrdPe_OrderNumber = Ord_Number for xml path('')),1,1,'')) as 接单人,
  81. --dbo.fn_ChineseToSpell((select stuff((select ','+dbo.fn_CheckUserIDGetUserName( OrdPe_OrdersPerson) from tb_ErpOrdersPerson where OrdPe_OrderNumber = Ord_Number for xml path('')),1,1,''))) as 接单人拼音,
  82. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime([dbo].[Vw_PhotographyView].Ordpg_PhotographyTime)) AS 拍摄时间,
  83. (case when (isnull([dbo].[Vw_PhotographyView].Ordpg_PhotographyStatus,1)) =0 then '未拍'
  84. else case when (isnull([dbo].[Vw_PhotographyView].Ordpg_PhotographyStatus,1)) = isnull([dbo].[Vw_PhotographyView].Ordpg_PhotographyCount,1) then 'OK' else '拍照中' end end) AS 拍照状态,
  85. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(Ordv_EarlyRepairTime)) AS 初修时间,
  86. dbo.fn_CheckOrderEarlyRepairStatus(dbo.tb_ErpOrderDigital.Ordv_EarlyRepairStatus) AS 初修状态,
  87. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(Ordv_FilmSelectionTime)) AS 选片时间,
  88. dbo.fn_CheckOrderFilmSelectionStatus(dbo.tb_ErpOrderDigital.Ordv_FilmSelectionStatus) AS 选片状态,
  89. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(dbo.tb_ErpOrderDigital.Ordv_DesignerTime)) AS 设计时间,
  90. dbo.fn_CheckOrderDesignerStatus(dbo.tb_ErpOrderDigital.Ordv_DesignerStatus) AS 设计状态,
  91. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(dbo.tb_ErpOrderDigital.Ordv_RefinementTime)) AS 精修时间,
  92. dbo.fn_CheckOrderRefinementStatus(dbo.tb_ErpOrderDigital.Ordv_RefinementStatus) AS 精修状态,
  93. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(dbo.tb_ErpOrderDigital.Ordv_LookDesignTime)) AS 看设计时间,
  94. dbo.fn_CheckOrderLookDesignStatus(dbo.tb_ErpOrderDigital.Ordv_LookDesignStatus) AS 看设计状态,
  95. (case when (isnull([dbo].[Vw_OrderProductPickupView].[OPlist_PickupStatus],1)) =0 then '未取'
  96. else case when (isnull([dbo].[Vw_OrderProductPickupView].[OPlist_PickupStatus],1)) = isnull([dbo].[Vw_OrderProductPickupView].productCount,1) then 'OK' else '部分取件' end end) AS 取件状态,
  97. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime([dbo].[Vw_OrderProductPickupView].OPlist_PickupTime)) AS 取件日期,
  98. (case when (isnull([dbo].[Vw_OrderProductPickupView].OPlist_CompletedStatus,1)) =0 then '未完'
  99. else case when (isnull([dbo].[Vw_OrderProductPickupView].OPlist_CompletedStatus,1)) = isnull([dbo].[Vw_OrderProductPickupView].productCount,1) then 'OK' else '部分完件' end end) AS 齐件状态,
  100. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime([dbo].[Vw_OrderProductPickupView].OPlist_CompletedTime)) AS 齐件日期,
  101. dbo.fn_ReservationPhotographyTime(Ord_CreateDateTime,(select top 1 dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(Ordpg_ReservationPhotographyTime)) from tb_ErpOrdersPhotography where Ordpg_ViceNumber = Ordv_ViceNumber and Ordpg_ReservationPhotographyTime is not null order by Ordpg_ReservationPhotographyTime DESC),Ord_Type) AS 预约日期
  102. ,Ord_CreateDateTime
  103. FROM
  104. dbo.tb_ErpOrder LEFT OUTER JOIN dbo.tb_ErpOrderDigital ON dbo.tb_ErpOrder.Ord_Number = dbo.tb_ErpOrderDigital.Ordv_Number
  105. left join [dbo].[Vw_PhotographyView] on dbo.tb_ErpOrderDigital.Ordv_ViceNumber=[dbo].[Vw_PhotographyView].Ordpg_ViceNumber
  106. left join [dbo].[Vw_OrderProductPickupView] on dbo.tb_ErpOrderDigital.Ordv_ViceNumber=[dbo].[Vw_OrderProductPickupView].OPlist_ViceNumber
  107. left join tempTB_AggregationCustomer on tb_ErpOrder.Ord_Number = GP_OrderNumber
  108. where CONVERT(int,Ord_Type) < 3 and (case when (isnull([dbo].[Vw_OrderProductPickupView].[OPlist_PickupStatus],1)) =0 then '未取'
  109. else case when (isnull([dbo].[Vw_OrderProductPickupView].[OPlist_PickupStatus],1)) = isnull([dbo].[Vw_OrderProductPickupView].productCount,1) then 'OK' else '部分取件' end end) <>'OK'
  110. GO
  111. --订单流程客户 显示已取件和未取件的所有客户 数据效多查询时较慢
  112. IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[Vw_OrderProcessCustomerAll]'))
  113. DROP VIEW [dbo].[Vw_OrderProcessCustomerAll]
  114. GO
  115. CREATE VIEW [dbo].[Vw_OrderProcessCustomerAll]
  116. AS
  117. --订单流程客户 显示已取件和未取件的所有客户 数据效多查询时较慢
  118. SELECT tb_ErpOrder.ID,dbo.tb_ErpOrder.Ord_DividedShop, dbo.tb_ErpOrder.Ord_Number, dbo.tb_ErpOrderDigital.Ordv_ViceNumber,
  119. dbo.tb_ErpOrder.Ord_Class,CONVERT(int,Ord_Type) as Ord_Type, Ord_OrderClass,
  120. CASE Ord_SinceOrderNumber WHEN '' THEN Ord_Number ELSE Ord_SinceOrderNumber END AS 订单号,
  121. dbo.fn_CheckOrderType(dbo.tb_ErpOrder.Ord_Type) AS 订单类型,
  122. Cus_Name AS 客户姓名,
  123. Cus_Name_py AS 客户拼音,
  124. Cus_Sex_cs AS 客户性别,
  125. Cus_Telephone AS 客户电话,
  126. [Age_String] AS 年龄,
  127. [Age_Day] AS 天,
  128. [Age_Year] AS 年,
  129. dbo.tb_ErpOrder.Ord_PhotographyCategory AS 套系类别,
  130. dbo.tb_ErpOrder.Ord_CustomerSource AS 客户来源,
  131. dbo.tb_ErpOrder.Ord_SeriesName AS 套系名称,
  132. dbo.tb_ErpOrder.Ord_SeriesPrice AS 套系价格,
  133. (select stuff((select ',' + dbo.fn_CheckUserIDGetUserName( OrdPe_OrdersPerson) from tb_ErpOrdersPerson where OrdPe_OrderNumber = Ord_Number for xml path('')),1,1,'')) as 接单人,
  134. --dbo.fn_ChineseToSpell((select stuff((select ','+dbo.fn_CheckUserIDGetUserName( OrdPe_OrdersPerson) from tb_ErpOrdersPerson where OrdPe_OrderNumber = Ord_Number for xml path('')),1,1,''))) as 接单人拼音,
  135. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime([dbo].[Vw_PhotographyView].Ordpg_PhotographyTime)) AS 拍摄时间,
  136. (case when (isnull([dbo].[Vw_PhotographyView].Ordpg_PhotographyStatus,1)) =0 then '未拍'
  137. else case when (isnull([dbo].[Vw_PhotographyView].Ordpg_PhotographyStatus,1)) = isnull([dbo].[Vw_PhotographyView].Ordpg_PhotographyCount,1) then 'OK' else '拍照中' end end) AS 拍照状态,
  138. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(Ordv_EarlyRepairTime)) AS 初修时间,
  139. dbo.fn_CheckOrderEarlyRepairStatus(dbo.tb_ErpOrderDigital.Ordv_EarlyRepairStatus) AS 初修状态,
  140. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(Ordv_FilmSelectionTime)) AS 选片时间,
  141. dbo.fn_CheckOrderFilmSelectionStatus(dbo.tb_ErpOrderDigital.Ordv_FilmSelectionStatus) AS 选片状态,
  142. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(dbo.tb_ErpOrderDigital.Ordv_DesignerTime)) AS 设计时间,
  143. dbo.fn_CheckOrderDesignerStatus(dbo.tb_ErpOrderDigital.Ordv_DesignerStatus) AS 设计状态,
  144. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(dbo.tb_ErpOrderDigital.Ordv_RefinementTime)) AS 精修时间,
  145. dbo.fn_CheckOrderRefinementStatus(dbo.tb_ErpOrderDigital.Ordv_RefinementStatus) AS 精修状态,
  146. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(dbo.tb_ErpOrderDigital.Ordv_LookDesignTime)) AS 看设计时间,
  147. dbo.fn_CheckOrderLookDesignStatus(dbo.tb_ErpOrderDigital.Ordv_LookDesignStatus) AS 看设计状态,
  148. (case when (isnull([dbo].[Vw_OrderProductPickupView].[OPlist_PickupStatus],1)) =0 then '未取'
  149. else case when (isnull([dbo].[Vw_OrderProductPickupView].[OPlist_PickupStatus],1)) = isnull([dbo].[Vw_OrderProductPickupView].productCount,1) then 'OK' else '部分取件' end end) AS 取件状态,
  150. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime([dbo].[Vw_OrderProductPickupView].OPlist_PickupTime)) AS 取件日期,
  151. (case when (isnull([dbo].[Vw_OrderProductPickupView].OPlist_CompletedStatus,1)) =0 then '未完'
  152. else case when (isnull([dbo].[Vw_OrderProductPickupView].OPlist_CompletedStatus,1)) = isnull([dbo].[Vw_OrderProductPickupView].productCount,1) then 'OK' else '部分完件' end end) AS 齐件状态,
  153. dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime([dbo].[Vw_OrderProductPickupView].OPlist_CompletedTime)) AS 齐件日期,
  154. dbo.fn_ReservationPhotographyTime(Ord_CreateDateTime,(select top 1 dbo.fn_CheckDateTimeReturn_Date(dbo.fn_CheckDateTime(Ordpg_ReservationPhotographyTime)) from tb_ErpOrdersPhotography where Ordpg_ViceNumber = Ordv_ViceNumber and Ordpg_ReservationPhotographyTime is not null order by Ordpg_ReservationPhotographyTime DESC),Ord_Type) AS 预约日期
  155. ,Ord_CreateDateTime
  156. FROM
  157. dbo.tb_ErpOrder LEFT OUTER JOIN dbo.tb_ErpOrderDigital ON dbo.tb_ErpOrder.Ord_Number = dbo.tb_ErpOrderDigital.Ordv_Number
  158. left join [dbo].[Vw_PhotographyView] on dbo.tb_ErpOrderDigital.Ordv_ViceNumber=[dbo].[Vw_PhotographyView].Ordpg_ViceNumber
  159. left join [dbo].[Vw_OrderProductPickupView] on dbo.tb_ErpOrderDigital.Ordv_ViceNumber=[dbo].[Vw_OrderProductPickupView].OPlist_ViceNumber
  160. left join tempTB_AggregationCustomer on tb_ErpOrder.Ord_Number = GP_OrderNumber
  161. where CONVERT(int,Ord_Type) < 3
  162. GO