fm_aha_import.sql 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. C."Name" 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. '' as salesman_user,
  20. case
  21. when E."Name" <> '' then ASH."Service Advisor No_" + ' - ' + E."Name"
  22. else CLE."Salesperson Code" + ' - ' + SP."Name"
  23. end as verkaeufer,
  24. RCE."Reminder Level" as reminder_level,
  25. case
  26. when RCE."Reminder Level" = '0' then 'Mahnst. 0: Kontoauszug vor Mahnung'
  27. when RCE."Reminder Level" = '1' then 'Mahnst. 1: Zahlungserinnerung'
  28. when RCE."Reminder Level" = '2' then 'Mahnst. 2: RA-Androhung'
  29. when RCE."Reminder Level" = '3' then 'Mahnst. M: Rechtsanwalt'
  30. else NULL end as mahnstufe,
  31. case
  32. when RCE."Reminder Level" = '0' then 'Mahnst. 1: Zahlungserinnerung'
  33. when RCE."Reminder Level" = '1' then 'Mahnst. 2: RA-Androhung'
  34. when RCE."Reminder Level" = '2' then 'Mahnst. M: Rechtsanwalt'
  35. when RCE."Reminder Level" = '3' then '-'
  36. else NULL end as mahnstufe2,
  37. CASE
  38. WHEN CLE."Branch Code" <> '' THEN CLE."Branch Code"
  39. ELSE 'n.v.' END as branch_code,
  40. CASE
  41. WHEN CLE."Branch Code" = 'GÖG' THEN ('60')
  42. WHEN CLE."Branch Code" = 'LEH' THEN ('70')
  43. WHEN CLE."Branch Code" = 'WTB' THEN ('80')
  44. WHEN CLE."Branch Code" = 'AAM' THEN ('60')
  45. ELSE 'n.v.' END as filiale_nummer,
  46. case
  47. WHEN CLE."Document No_" IN ('VRGF206001957') THEN 'NA'
  48. when (not CLE."Main Area" IN (1,2,3)
  49. and not left(CLE."Document No_",2) IN ('AL','BF','E4','HV','KA','LB','LO','LP','LQ','LR','RB','SP','UM')
  50. and not CLE."Document No_" LIKE '166%'
  51. and not CLE."Document No_" LIKE '169%'
  52. and not CLE."Document No_" LIKE 'UB0%') then (
  53. CASE
  54. WHEN C."No_" IN ('690101','690102','690103','690104','690105') THEN ('RA')
  55. 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')
  56. WHEN left(CLE."Document No_",3) IN ('D17','D18','D26','D27','D36','D46','D47','D56','D57') THEN ('TZ')
  57. WHEN right(CLE."Document No_",2) = 'NA' THEN ('NA')
  58. WHEN right(CLE."Document No_",2) = 'GA' THEN ('GA')
  59. ELSE ('n.v.') END )
  60. when ((CLE."Document No_" LIKE 'VRGGFZ%'
  61. or CLE."Document No_" LIKE 'VGUGFZ%'
  62. or CLE."Document No_" LIKE 'VRGF%'
  63. or CLE."Document No_" LIKE 'VRF%'
  64. or CLE."Document No_" LIKE 'VGGF%')
  65. and VLE."Entry Type" = 1) then (
  66. CASE
  67. WHEN substring(VLE."Department Code",3,1) = '1' THEN ('NA')
  68. WHEN substring(VLE."Department Code",3,1) = '2' THEN ('GA')
  69. WHEN substring(CLE."Department Code",3,1) = '4' THEN ('SC')
  70. WHEN substring(CLE."Department Code",3,1) = '5' THEN ('TZ')
  71. ELSE ('NA/GA') END )
  72. else
  73. CASE
  74. WHEN (CLE."Main Area" = 3) THEN ('SC')
  75. WHEN (CLE."Main Area" = 1) THEN ('TZ')
  76. WHEN (CLE."Main Area" = 2) THEN ('NA/GA')
  77. ELSE 'SC/TZ' END
  78. end as kostenstelle_text,
  79. CASE WHEN V."Date of Sales Shipment" > '2000-01-01' THEN V."Date of Sales Shipment" ELSE NULL END as shipment_date,
  80. CASE WHEN C."No_" <> C2."No_" THEN C2."No_" ELSE '' END as customer_number_leasing,
  81. CASE WHEN C."No_" <> C2."No_" THEN C2."Name" ELSE '' END as customer_name_leasing,
  82. PSDT1."Text" as text1,
  83. PSDT2."Text" as text2
  84. from "DMS1"."dbo"."AH Augsburg$Customer Ledger Entry" as CLE
  85. left join "DMS1"."dbo"."AH Augsburg$Customer" as C on C."No_" = CLE."Customer No_"
  86. left join "DMS1"."dbo"."AH Augsburg$Department" as D on CLE."Department Code" = D."Code"
  87. left join "DMS1"."dbo"."AH Augsburg$Salesperson_Purchaser" as SP on CLE."Salesperson Code" = SP."Code"
  88. left join "DMS1"."dbo"."AH Augsburg$Reminder_Fin_ Charge Entry" as RCE on CLE."Entry No_" = RCE."Customer Entry No_"
  89. left join "DMS1"."dbo"."AH Augsburg$Vehicle Ledger Entry" as VLE on CLE."Document No_" = VLE."Document No_"
  90. left join "DMS1"."dbo"."AH Augsburg$Vehicle" as V on CLE."VIN" = V."VIN"
  91. left join "DMS1"."dbo"."AH Augsburg$Customer" as C2 on V."Customer No_" = C2."No_"
  92. left join "DMS1"."dbo"."AH Augsburg$Posted Sales Document Text" as PSDT1 on CLE."Document No_" = PSDT1."Document No_" and PSDT1."Line No_" = 10000 and PSDT1."Header_Footer" = 1
  93. left join "DMS1"."dbo"."AH Augsburg$Posted Sales Document Text" as PSDT2 on CLE."Document No_" = PSDT2."Document No_" and PSDT2."Line No_" = 20000 and PSDT2."Header_Footer" = 1
  94. left join "DMS1"."dbo"."AH Augsburg$Service Ledger Entry" SLE on CLE."Document No_" = SLE."Document No_"
  95. left join "DMS1"."dbo"."AH Augsburg$Archived Service Header" ASH on SLE."Order No_" = ASH."No_"
  96. left join "DMS1"."dbo"."AH Augsburg$Employee" E on ASH."Service Advisor No_" = E."No_"
  97. where CLE."Remaining Amount" <> 0
  98. and CLE."Journal Batch Name" <> 'ERSTIMPORT'
  99. and not C."No_" LIKE '600%'
  100. and CLE."Document No_" NOT IN ('VRGF206000366','VRGF206000656', 'WRG20609530', 'VRGF236004511')
  101. and (
  102. ((CLE."Document No_" LIKE 'VRGGFZ%'
  103. or CLE."Document No_" LIKE 'VGUGFZ%'
  104. or CLE."Document No_" LIKE 'VRGF%'
  105. or CLE."Document No_" LIKE 'VRF%'
  106. or CLE."Document No_" LIKE 'VGGF%')
  107. and VLE."Entry Type" = 1)
  108. or
  109. (not CLE."Main Area" IN (1,2,3)
  110. and not left(CLE."Document No_",2) IN ('AL','BF','E4','HV','KA','LB','LO','LP','LQ','LR','RB','SP','UM')
  111. and not CLE."Document No_" LIKE '166%'
  112. and not CLE."Document No_" LIKE '169%'
  113. and not CLE."Document No_" LIKE 'UB0%')
  114. or
  115. (CLE."Document No_" LIKE 'WRG%'
  116. or CLE."Document No_" LIKE 'WGG%'
  117. or CLE."Document No_" LIKE 'WGGS%'
  118. or CLE."Document No_" LIKE 'VRGGET%'
  119. or CLE."Document No_" LIKE 'VRT%'
  120. or CLE."Document No_" LIKE 'VRGT%'
  121. or CLE."Document No_" LIKE 'VGUGET%')
  122. )