fm_ahr_import.sql 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. select distinct
  2. CLE."VIN" as chassis_number,
  3. CLE."Make Code" as make_code,
  4. CLE."Posting Date" as posting_date,
  5. CLE."Due Date" as due_date,
  6. C."No_" as account_number,
  7. CLE."Document No_" as document_number,
  8. CLE."Entry No_" as entry_number,
  9. CLE."Amount" as transact_sum,
  10. CLE."Remaining Amount" as transact_outstanding,
  11. C."No_" as customer_number,
  12. case when C."No_" = '036201' then 'Alphabet' else C."Name" end as customer_name,
  13. C."Phone No_" as customer_phone,
  14. C."Address" as customer_address,
  15. C."Post Code" as customer_zipcode,
  16. C."City" as customer_city,
  17. CLE."Salesperson Code" as salesman_number,
  18. SP."Name" as salesman_name,
  19. case
  20. when CLE."Salesperson Code" = '104' then 'cab'
  21. when CLE."Salesperson Code" = '106' then 'bv'
  22. when CLE."Salesperson Code" = '109' then 'fd'
  23. when CLE."Salesperson Code" = '111' then 'aho'
  24. when CLE."Salesperson Code" = '133' then 'mk'
  25. when CLE."Salesperson Code" = '141' then 'ka'
  26. when CLE."Salesperson Code" = '142' then 'fl'
  27. when CLE."Salesperson Code" = '143' then 'tb'
  28. when CLE."Salesperson Code" = '144' then 'gr'
  29. when CLE."Salesperson Code" = '145' then 'kgu'
  30. when CLE."Salesperson Code" = '148' then 'hbo'
  31. when CLE."Salesperson Code" = '149' then 'sbe'
  32. when CLE."Salesperson Code" = '152' then 'hp'
  33. when CLE."Salesperson Code" = '153' then 'rhe'
  34. when CLE."Salesperson Code" = '154' then 'fja'
  35. when CLE."Salesperson Code" = '302' then 'jja'
  36. when CLE."Salesperson Code" = '303' then 'twa'
  37. when CLE."Salesperson Code" = '304' then 'sdo'
  38. when CLE."Salesperson Code" = '308' then 'tbe'
  39. when CLE."Salesperson Code" = '309' then 'ba'
  40. when CLE."Salesperson Code" = '332' then 'kkl'
  41. when CLE."Salesperson Code" = '341' then 'af'
  42. when CLE."Salesperson Code" = '342' then 'vpo'
  43. when CLE."Salesperson Code" = '343' then 'mku'
  44. when CLE."Salesperson Code" = '344' then 'rwo'
  45. when CLE."Salesperson Code" = '345' then 'osa'
  46. when CLE."Salesperson Code" = '351' then 'ipl'
  47. when CLE."Salesperson Code" = '353' then 'sse'
  48. when CLE."Salesperson Code" = '354' then 'rze'
  49. when CLE."Salesperson Code" = '550' then 'aan'
  50. when CLE."Salesperson Code" = '811' then 'tal'
  51. when CLE."Salesperson Code" = '820' then 'wr'
  52. when CLE."Salesperson Code" = '821' then 'mma'
  53. when CLE."Salesperson Code" = '822' then 'mme'
  54. else '' end as salesman_user,
  55. case
  56. when E."Name" <> '' then ASH."Service Advisor No_" + ' - ' + E."Name"
  57. else CLE."Salesperson Code" + ' - ' + SP."Name"
  58. end as verkaeufer,
  59. RCE."Reminder Level" as reminder_level,
  60. case
  61. when RCE."Reminder Level" = '0' then 'Mahnst. 0: Kontoauszug vor Mahnung'
  62. when RCE."Reminder Level" = '1' then 'Mahnst. 1: Zahlungserinnerung'
  63. when RCE."Reminder Level" = '2' then 'Mahnst. 2: RA-Androhung'
  64. when RCE."Reminder Level" = '3' then 'Mahnst. M: Rechtsanwalt'
  65. else NULL end as mahnstufe,
  66. case
  67. when RCE."Reminder Level" = '0' then 'Mahnst. 1: Zahlungserinnerung'
  68. when RCE."Reminder Level" = '1' then 'Mahnst. 2: RA-Androhung'
  69. when RCE."Reminder Level" = '2' then 'Mahnst. M: Rechtsanwalt'
  70. when RCE."Reminder Level" = '3' then '-'
  71. else NULL end as mahnstufe2,
  72. CASE
  73. WHEN CLE."Branch Code" <> '' THEN CLE."Branch Code"
  74. WHEN substring(CLE."Document No_" ,8,1) = '1' THEN ('MM')
  75. WHEN substring(CLE."Document No_" ,8,1) = '2' THEN ('VÖH')
  76. WHEN substring(CLE."Document No_" ,8,1) = '3' THEN ('KRU')
  77. WHEN substring(CLE."Document No_" ,8,1) = '4' THEN ('ULM')
  78. WHEN substring(CLE."Document No_" ,8,1) = '5' THEN ('LL')
  79. ELSE null END as branch_code,
  80. CASE
  81. WHEN CLE."Branch Code" = 'MM' THEN ('10')
  82. WHEN CLE."Branch Code" = 'VÖH' THEN ('20')
  83. WHEN CLE."Branch Code" = 'KRU' THEN ('30')
  84. WHEN CLE."Branch Code" = 'ULM' THEN ('40')
  85. WHEN CLE."Branch Code" = 'LL' THEN ('50')
  86. WHEN CLE."Branch Code" = 'GZ' THEN ('55')
  87. WHEN substring(CLE."Document No_" ,8,1) = '1' THEN ('10')
  88. WHEN substring(CLE."Document No_" ,8,1) = '2' THEN ('20')
  89. WHEN substring(CLE."Document No_" ,8,1) = '3' THEN ('30')
  90. WHEN substring(CLE."Document No_" ,8,1) = '4' THEN ('40')
  91. WHEN substring(CLE."Document No_" ,8,1) = '5' THEN ('50')
  92. ELSE 'n.v.' END as filiale_nummer,
  93. case
  94. when (not CLE."Main Area" IN (1,2,3)
  95. and not left(CLE."Document No_",2) IN ('AL','BF','E4','HV','KA','LB','LO','LP','LQ','LR','RB','SP','UM')
  96. and not CLE."Document No_" LIKE '166%'
  97. and not CLE."Document No_" LIKE '169%'
  98. and not CLE."Document No_" LIKE 'UB0%') then (
  99. CASE
  100. WHEN C."No_" IN ('690101','690102','690103','690104','690105') THEN ('RA')
  101. WHEN left(CLE."Document No_",3) IN ('D12','D13','D14','D15','D16','D23','D24','D25','D32','D34','D35','D43','D44','D45','D51','D52','D54','D55') THEN ('SC')
  102. WHEN left(CLE."Document No_",3) IN ('D17','D18','D26','D27','D36','D46','D47','D56','D57') THEN ('TZ')
  103. WHEN right(CLE."Document No_",2) = 'NA' THEN ('NA')
  104. WHEN right(CLE."Document No_",2) = 'GA' THEN ('GA')
  105. ELSE ('n.v.') END )
  106. when ((CLE."Document No_" LIKE 'VRGGFZ%'
  107. or CLE."Document No_" LIKE 'VGUGFZ%'
  108. or CLE."Document No_" LIKE 'VRGF%'
  109. or CLE."Document No_" LIKE 'VRF%'
  110. or CLE."Document No_" LIKE 'VGGF%')
  111. and VLE."Entry Type" = 1) then (
  112. CASE
  113. WHEN substring(VLE."Department Code",3,1) = '1' THEN ('NA')
  114. WHEN substring(VLE."Department Code",3,1) = '2' THEN ('GA')
  115. WHEN substring(CLE."Department Code",3,1) = '4' THEN ('SC')
  116. WHEN substring(CLE."Department Code",3,1) = '5' THEN ('TZ')
  117. ELSE ('NA/GA') END )
  118. else
  119. CASE
  120. WHEN (CLE."Main Area" = 3) THEN ('SC')
  121. WHEN (CLE."Main Area" = 1) THEN ('TZ')
  122. WHEN (CLE."Main Area" = 2) THEN ('NA/GA')
  123. ELSE 'SC/TZ' END
  124. end as kostenstelle_text,
  125. CASE WHEN V."Date of Sales Shipment" > '2000-01-01' THEN V."Date of Sales Shipment" ELSE NULL END as shipment_date,
  126. CASE WHEN C."No_" <> C2."No_" THEN C2."No_" ELSE '' END as customer_number_leasing,
  127. CASE WHEN C."No_" <> C2."No_" THEN C2."Name" ELSE '' END as customer_name_leasing,
  128. PSDT1."Text" as text1,
  129. PSDT2."Text" as text2
  130. from "DMS1"."dbo"."AH Reisacher$Customer Ledger Entry" as CLE
  131. left join "DMS1"."dbo"."AH Reisacher$Customer" as C on C."No_" = CLE."Customer No_"
  132. left join "DMS1"."dbo"."AH Reisacher$Department" as D on CLE."Department Code" = D."Code"
  133. left join "DMS1"."dbo"."AH Reisacher$Salesperson_Purchaser" as SP on CLE."Salesperson Code" = SP."Code"
  134. left join "DMS1"."dbo"."AH Reisacher$Reminder_Fin_ Charge Entry" as RCE on CLE."Entry No_" = RCE."Customer Entry No_"
  135. left join "DMS1"."dbo"."AH Reisacher$Vehicle Ledger Entry" as VLE on CLE."Document No_" = VLE."Document No_"
  136. left join "DMS1"."dbo"."AH Reisacher$Vehicle" as V on CLE."VIN" = V."VIN"
  137. left join "DMS1"."dbo"."AH Reisacher$Customer" as C2 on V."Customer No_" = C2."No_"
  138. left join "DMS1"."dbo"."AH Reisacher$Posted Sales Document Text" as PSDT1 on CLE."Document No_" = PSDT1."Document No_" and PSDT1."Line No_" = 10000 and PSDT1."Header_Footer" = 1
  139. left join "DMS1"."dbo"."AH Reisacher$Posted Sales Document Text" as PSDT2 on CLE."Document No_" = PSDT2."Document No_" and PSDT2."Line No_" = 20000 and PSDT2."Header_Footer" = 1
  140. left join "DMS1"."dbo"."AH Reisacher$Service Ledger Entry" SLE on CLE."Document No_" = SLE."Document No_"
  141. left join "DMS1"."dbo"."AH Reisacher$Archived Service Header" ASH on SLE."Order No_" = ASH."No_"
  142. left join "DMS1"."dbo"."AH Reisacher$Employee" E on ASH."Service Advisor No_" = E."No_"
  143. where CLE."Remaining Amount" <> 0
  144. and CLE."Journal Batch Name" <> 'ERSTIMPORT'
  145. and not C."No_" LIKE '600%'
  146. and CLE."Document No_" not in ('VRGF201000476', 'VGGF234000024')
  147. and (
  148. ((CLE."Document No_" LIKE 'VRGGFZ%'
  149. or CLE."Document No_" LIKE 'VGUGFZ%'
  150. or CLE."Document No_" LIKE 'VRGF%'
  151. or CLE."Document No_" LIKE 'VRF%'
  152. or CLE."Document No_" LIKE 'VGGF%')
  153. and VLE."Entry Type" = 1)
  154. or
  155. (not CLE."Main Area" IN (1,2,3)
  156. and not left(CLE."Document No_",2) IN ('AL','BF','E4','HV','KA','LB','LO','LP','LQ','LR','RB','SP','UM')
  157. and not CLE."Document No_" LIKE '166%'
  158. and not CLE."Document No_" LIKE '169%'
  159. and not CLE."Document No_" LIKE 'UB0%')
  160. or
  161. (CLE."Document No_" LIKE 'WRG%'
  162. or CLE."Document No_" LIKE 'WGG%'
  163. or CLE."Document No_" LIKE 'WGGS%'
  164. or CLE."Document No_" LIKE 'VRGGET%'
  165. or CLE."Document No_" LIKE 'VRT%'
  166. or CLE."Document No_" LIKE 'VRGT%'
  167. or CLE."Document No_" LIKE 'VGUGET%')
  168. )