fm_rem_import.sql 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. SELECT DISTINCT CLE."VIN" AS chassis_number,
  2. CLE."Make Code" AS make_code,
  3. CLE."Posting Date" AS posting_date,
  4. CLE."Due Date" AS due_date,
  5. C."No_" AS account_number,
  6. CLE."Document No_" AS document_number,
  7. CLE."Entry No_" AS entry_number,
  8. CLE."Amount" AS transact_sum,
  9. CLE."Remaining Amount" AS transact_outstanding,
  10. C."No_" AS customer_number,
  11. CASE
  12. WHEN C."No_" = '036201'
  13. THEN 'Alphabet'
  14. ELSE C."Name"
  15. END AS customer_name,
  16. C."Phone No_" AS customer_phone,
  17. C."Address" AS customer_address,
  18. C."Post Code" AS customer_zipcode,
  19. C."City" AS customer_city,
  20. CLE."Salesperson Code" AS salesman_number,
  21. SP."Name" AS salesman_name,
  22. CASE
  23. WHEN CLE."Salesperson Code" = '104'
  24. THEN 'cab'
  25. WHEN CLE."Salesperson Code" = '106'
  26. THEN 'bv'
  27. WHEN CLE."Salesperson Code" = '109'
  28. THEN 'fd'
  29. WHEN CLE."Salesperson Code" = '111'
  30. THEN 'aho'
  31. WHEN CLE."Salesperson Code" = '133'
  32. THEN 'mk'
  33. WHEN CLE."Salesperson Code" = '141'
  34. THEN 'ka'
  35. WHEN CLE."Salesperson Code" = '142'
  36. THEN 'fl'
  37. WHEN CLE."Salesperson Code" = '143'
  38. THEN 'tb'
  39. WHEN CLE."Salesperson Code" = '144'
  40. THEN 'gr'
  41. WHEN CLE."Salesperson Code" = '145'
  42. THEN 'kgu'
  43. WHEN CLE."Salesperson Code" = '148'
  44. THEN 'hbo'
  45. WHEN CLE."Salesperson Code" = '149'
  46. THEN 'sbe'
  47. WHEN CLE."Salesperson Code" = '152'
  48. THEN 'hp'
  49. WHEN CLE."Salesperson Code" = '153'
  50. THEN 'rhe'
  51. WHEN CLE."Salesperson Code" = '154'
  52. THEN 'fja'
  53. WHEN CLE."Salesperson Code" = '302'
  54. THEN 'jja'
  55. WHEN CLE."Salesperson Code" = '303'
  56. THEN 'twa'
  57. WHEN CLE."Salesperson Code" = '304'
  58. THEN 'sdo'
  59. WHEN CLE."Salesperson Code" = '308'
  60. THEN 'tbe'
  61. WHEN CLE."Salesperson Code" = '309'
  62. THEN 'ba'
  63. WHEN CLE."Salesperson Code" = '332'
  64. THEN 'kkl'
  65. WHEN CLE."Salesperson Code" = '341'
  66. THEN 'af'
  67. WHEN CLE."Salesperson Code" = '342'
  68. THEN 'vpo'
  69. WHEN CLE."Salesperson Code" = '343'
  70. THEN 'mku'
  71. WHEN CLE."Salesperson Code" = '344'
  72. THEN 'rwo'
  73. WHEN CLE."Salesperson Code" = '345'
  74. THEN 'osa'
  75. WHEN CLE."Salesperson Code" = '351'
  76. THEN 'ipl'
  77. WHEN CLE."Salesperson Code" = '353'
  78. THEN 'sse'
  79. WHEN CLE."Salesperson Code" = '354'
  80. THEN 'rze'
  81. WHEN CLE."Salesperson Code" = '550'
  82. THEN 'aan'
  83. WHEN CLE."Salesperson Code" = '811'
  84. THEN 'tal'
  85. WHEN CLE."Salesperson Code" = '820'
  86. THEN 'wr'
  87. WHEN CLE."Salesperson Code" = '821'
  88. THEN 'mma'
  89. WHEN CLE."Salesperson Code" = '822'
  90. THEN 'mme'
  91. ELSE ''
  92. END AS salesman_user,
  93. CASE
  94. WHEN E."Name" <> ''
  95. THEN ASH."Service Advisor No_" + ' - ' + E."Name"
  96. ELSE CLE."Salesperson Code" + ' - ' + SP."Name"
  97. END AS verkaeufer,
  98. RCE."Reminder Level" AS reminder_level,
  99. CASE
  100. WHEN RCE."Reminder Level" = '0'
  101. THEN 'Mahnst. 0: Kontoauszug vor Mahnung'
  102. WHEN RCE."Reminder Level" = '1'
  103. THEN 'Mahnst. 1: Zahlungserinnerung'
  104. WHEN RCE."Reminder Level" = '2'
  105. THEN 'Mahnst. 2: RA-Androhung'
  106. WHEN RCE."Reminder Level" = '3'
  107. THEN 'Mahnst. M: Rechtsanwalt'
  108. ELSE NULL
  109. END AS mahnstufe,
  110. CASE
  111. WHEN RCE."Reminder Level" = '0'
  112. THEN 'Mahnst. 1: Zahlungserinnerung'
  113. WHEN RCE."Reminder Level" = '1'
  114. THEN 'Mahnst. 2: RA-Androhung'
  115. WHEN RCE."Reminder Level" = '2'
  116. THEN 'Mahnst. M: Rechtsanwalt'
  117. WHEN RCE."Reminder Level" = '3'
  118. THEN '-'
  119. ELSE NULL
  120. END AS mahnstufe2,
  121. CASE
  122. WHEN CLE."Branch Code" <> ''
  123. THEN CLE."Branch Code"
  124. WHEN substring(CLE."Document No_", 8, 1) = '1'
  125. THEN ('MM')
  126. WHEN substring(CLE."Document No_", 8, 1) = '2'
  127. THEN ('VÖH')
  128. WHEN substring(CLE."Document No_", 8, 1) = '3'
  129. THEN ('KRU')
  130. WHEN substring(CLE."Document No_", 8, 1) = '4'
  131. THEN ('ULM')
  132. WHEN substring(CLE."Document No_", 8, 1) = '5'
  133. THEN ('LL')
  134. ELSE NULL
  135. END AS branch_code,
  136. CASE
  137. WHEN CLE."Branch Code" = 'MM'
  138. THEN ('10')
  139. WHEN CLE."Branch Code" = 'VÖH'
  140. THEN ('20')
  141. WHEN CLE."Branch Code" = 'KRU'
  142. THEN ('30')
  143. WHEN CLE."Branch Code" = 'ULM'
  144. THEN ('40')
  145. WHEN CLE."Branch Code" = 'LL'
  146. THEN ('50')
  147. WHEN CLE."Branch Code" = 'GZ'
  148. THEN ('55')
  149. WHEN substring(CLE."Document No_", 8, 1) = '1'
  150. THEN ('10')
  151. WHEN substring(CLE."Document No_", 8, 1) = '2'
  152. THEN ('20')
  153. WHEN substring(CLE."Document No_", 8, 1) = '3'
  154. THEN ('30')
  155. WHEN substring(CLE."Document No_", 8, 1) = '4'
  156. THEN ('40')
  157. WHEN substring(CLE."Document No_", 8, 1) = '5'
  158. THEN ('50')
  159. ELSE 'n.v.'
  160. END AS filiale_nummer,
  161. CASE
  162. WHEN (
  163. NOT CLE."Main Area" IN (1, 2, 3)
  164. AND NOT left(CLE."Document No_", 2) IN ('AL', 'BF', 'E4', 'HV', 'KA', 'LB', 'LO', 'LP', 'LQ', 'LR', 'RB', 'SP', 'UM')
  165. AND NOT CLE."Document No_" LIKE '166%'
  166. AND NOT CLE."Document No_" LIKE '169%'
  167. AND NOT CLE."Document No_" LIKE 'UB0%'
  168. )
  169. THEN (
  170. CASE
  171. WHEN C."No_" IN ('690101', '690102', '690103', '690104', '690105')
  172. THEN ('RA')
  173. 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')
  174. THEN ('SC')
  175. WHEN left(CLE."Document No_", 3) IN ('D17', 'D18', 'D26', 'D27', 'D36', 'D46', 'D47', 'D56', 'D57')
  176. THEN ('TZ')
  177. WHEN right(CLE."Document No_", 2) = 'NA'
  178. THEN ('NA')
  179. WHEN right(CLE."Document No_", 2) = 'GA'
  180. THEN ('GA')
  181. ELSE ('n.v.')
  182. END
  183. )
  184. WHEN (
  185. (
  186. CLE."Document No_" LIKE 'VRGGFZ%'
  187. OR CLE."Document No_" LIKE 'VGUGFZ%'
  188. OR CLE."Document No_" LIKE 'VRGF%'
  189. OR CLE."Document No_" LIKE 'VRF%'
  190. OR CLE."Document No_" LIKE 'VGGF%'
  191. )
  192. AND VLE."Entry Type" = 1
  193. )
  194. THEN (
  195. CASE
  196. WHEN substring(VLE."Department Code", 3, 1) = '1'
  197. THEN ('NA')
  198. WHEN substring(VLE."Department Code", 3, 1) = '2'
  199. THEN ('GA')
  200. WHEN substring(CLE."Department Code", 3, 1) = '4'
  201. THEN ('SC')
  202. WHEN substring(CLE."Department Code", 3, 1) = '5'
  203. THEN ('TZ')
  204. ELSE ('NA/GA')
  205. END
  206. )
  207. ELSE CASE
  208. WHEN (CLE."Main Area" = 3)
  209. THEN ('SC')
  210. WHEN (CLE."Main Area" = 1)
  211. THEN ('TZ')
  212. WHEN (CLE."Main Area" = 2)
  213. THEN ('NA/GA')
  214. ELSE 'SC/TZ'
  215. END
  216. END AS kostenstelle_text,
  217. CASE
  218. WHEN V."Date of Sales Shipment" > '2000-01-01'
  219. THEN V."Date of Sales Shipment"
  220. ELSE NULL
  221. END AS shipment_date,
  222. CASE
  223. WHEN C."No_" <> C2."No_"
  224. THEN C2."No_"
  225. ELSE ''
  226. END AS customer_number_leasing,
  227. CASE
  228. WHEN C."No_" <> C2."No_"
  229. THEN C2."Name"
  230. ELSE ''
  231. END AS customer_name_leasing,
  232. PSDT1."Text" AS text1,
  233. PSDT2."Text" AS text2
  234. FROM "DMS1"."dbo"."Reisacher Electric Mobility$Customer Ledger Entry" AS CLE
  235. LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Customer" AS C ON C."No_" = CLE."Customer No_"
  236. LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Department" AS D ON CLE."Department Code" = D."Code"
  237. LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Salesperson_Purchaser" AS SP ON CLE."Salesperson Code" = SP."Code"
  238. LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Reminder_Fin_ Charge Entry" AS RCE ON CLE."Entry No_" = RCE."Customer Entry No_"
  239. LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Vehicle Ledger Entry" AS VLE ON CLE."Document No_" = VLE."Document No_"
  240. LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Vehicle" AS V ON CLE."VIN" = V."VIN"
  241. LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Customer" AS C2 ON V."Customer No_" = C2."No_"
  242. LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Posted Sales Document Text" AS PSDT1 ON CLE."Document No_" = PSDT1."Document No_"
  243. AND PSDT1."Line No_" = 10000
  244. AND PSDT1."Header_Footer" = 1
  245. LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Posted Sales Document Text" AS PSDT2 ON CLE."Document No_" = PSDT2."Document No_"
  246. AND PSDT2."Line No_" = 20000
  247. AND PSDT2."Header_Footer" = 1
  248. LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Service Ledger Entry" SLE ON CLE."Document No_" = SLE."Document No_"
  249. LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Archived Service Header" ASH ON SLE."Order No_" = ASH."No_"
  250. LEFT JOIN "DMS1"."dbo"."Reisacher Electric Mobility$Employee" E ON ASH."Service Advisor No_" = E."No_"
  251. WHERE CLE."Remaining Amount" <> 0
  252. AND CLE."Journal Batch Name" <> 'ERSTIMPORT'
  253. AND NOT C."No_" LIKE '600%'
  254. AND CLE."Document No_" NOT IN ('VRGF201000476', 'VGGF234000024')
  255. AND (
  256. (
  257. (
  258. CLE."Document No_" LIKE 'VRGGFZ%'
  259. OR CLE."Document No_" LIKE 'VGUGFZ%'
  260. OR CLE."Document No_" LIKE 'VRGF%'
  261. OR CLE."Document No_" LIKE 'VRF%'
  262. OR CLE."Document No_" LIKE 'VGGF%'
  263. )
  264. AND VLE."Entry Type" = 1
  265. )
  266. OR (
  267. NOT CLE."Main Area" IN (1, 2, 3)
  268. AND NOT left(CLE."Document No_", 2) IN ('AL', 'BF', 'E4', 'HV', 'KA', 'LB', 'LO', 'LP', 'LQ', 'LR', 'RB', 'SP', 'UM')
  269. AND NOT CLE."Document No_" LIKE '166%'
  270. AND NOT CLE."Document No_" LIKE '169%'
  271. AND NOT CLE."Document No_" LIKE 'UB0%'
  272. )
  273. OR (
  274. CLE."Document No_" LIKE 'WRG%'
  275. OR CLE."Document No_" LIKE 'WGG%'
  276. OR CLE."Document No_" LIKE 'WGGS%'
  277. OR CLE."Document No_" LIKE 'VRGGET%'
  278. OR CLE."Document No_" LIKE 'VRT%'
  279. OR CLE."Document No_" LIKE 'VRGT%'
  280. OR CLE."Document No_" LIKE 'VGUGET%'
  281. )
  282. )