Offene_Posten_Autoline.sql 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. select "Accountcode_2",
  2. "Customerreference",
  3. "Documentnumber",
  4. "Documentdate",
  5. "Documenttype",
  6. "Duedate",
  7. "Documentbasetotal",
  8. "Documentcurrtotal",
  9. "Vatamountbase",
  10. "Vatamountcurr",
  11. "Daybooknumber",
  12. "Daybookitemnumber",
  13. "Postingdaybkdate",
  14. "Amountpaidbase",
  15. "Amountpaidcurr",
  16. "Lastpaymentdate",
  17. "Lastpaymentref",
  18. "Lastpmtdaybookno",
  19. "Lastpmtdaybookitm",
  20. "Status",
  21. "Sdiscountoffered001",
  22. "Sdiscountoffered002",
  23. "Sdiscountdate001",
  24. "Sdiscountdate002",
  25. "Debtflag",
  26. "Statementprintedyn",
  27. "Settledflag",
  28. "Miscellaneousref",
  29. "Prcode",
  30. "Prreference",
  31. "Prdateraised",
  32. "Prcomment",
  33. "Prsentto",
  34. "Prfollowupdate",
  35. "Systemmatchnumber",
  36. "Allowancebase",
  37. "Allowancecurrency",
  38. "Sourcemodule",
  39. "Sourcebranch",
  40. "Documentarchiveid",
  41. "Ustaxcodes001",
  42. "Ustaxcodes002",
  43. "Boemagicnumber",
  44. "Taxbilldate",
  45. "Taxbillnumber",
  46. "Taxbillpending",
  47. "Truepostingorigin",
  48. "Truemod",
  49. "Lastletterdate",
  50. "Lastlettertype",
  51. "Controlaccounttype",
  52. "Paymenttypecode",
  53. "Paymentreference",
  54. "Socialidvatreg",
  55. "Originalinvoice",
  56. "Client Db",
  57. "Salesexecutive001_VS_NewVehicle",
  58. "Salesexecutive001_VS_UsedVehicle",
  59. "Salesinvoicenumber_VS_NewVehicle",
  60. "Salesinvoicenumber_VS_UsedVehicle",
  61. "Accountcode",
  62. "Customersname",
  63. "Postcode",
  64. "Kunde",
  65. "Client Db" as "Hauptbetrieb_ID",
  66. '' as "Hauptbetrieb_Name",
  67. "Truepostingorigin" as "Standort_ID",
  68. "Locationcode_GB_Company",
  69. "Description_GB_Company",
  70. "Standort_Name",
  71. "Beleg",
  72. "Lastlettertype" as "Mahnstufe",
  73. "Duedate" as "Bookkeep Date",
  74. "Wipnumber",
  75. "Invoicingoperator",
  76. "Username_GB_User_Service",
  77. "Username_GB_User_Service" as "Serviceberater",
  78. "Creatingoperator",
  79. "Username_GB_User_NewVehicle",
  80. "Username_GB_User_Usedvehicle",
  81. "Sel Name",
  82. "Forderungsart",
  83. "Bereich",
  84. "offen",
  85. "Documentbasetotal" as "Rechnungsbetrag",
  86. SUM("offen") OVER (partition by "Accountcode_2") as "Gesamt offen KD (Info)",
  87. "Tage",
  88. "Staffel",
  89. "Forderungsart" as "Categorycode_SL_SalesledgerAccount"
  90. from
  91. (select T1."AccountCode" as "Accountcode_2",
  92. T4."CategoryCode" as "Forderungsart",
  93. CASE WHEN (((-1 * datediff(day, (getdate()), T1."DueDate"))) BETWEEN 0 AND 14) THEN ('< 2 Wochen') WHEN (((-1 * datediff(day, (getdate()), T1."DueDate"))) BETWEEN 15 AND 28) THEN ('2 - 4 Wochen') WHEN (((-1 * datediff(day, (getdate()), T1."DueDate"))) BETWEEN 29 AND 42) THEN ('4 - 6 Wochen') WHEN (((-1 * datediff(day, (getdate()), T1."DueDate"))) BETWEEN 43 AND 84) THEN ('6 - 12 Wochen') WHEN (((-1 * datediff(day, (getdate()), T1."DueDate"))) > 84) THEN ('> 12 Wochen') WHEN (((-1 * datediff(day, (getdate()), T1."DueDate"))) < 0) THEN ('noch nicht fällig') ELSE null END as "Staffel",
  94. (-1 * datediff(day, (getdate()), T1."DueDate")) as "Tage",
  95. T1."DocumentBaseTotal" as "Documentbasetotal",
  96. T1."DocumentBaseTotal" - T1."AmountPaidBase" as "offen",
  97. CASE WHEN (T2."SalesExecutive001" IS NOT NULL) THEN ('Neuwagen') WHEN (T3."SalesExecutive001" IS NOT NULL) THEN ('Gebrauchtwagen') WHEN (T6."WIPNumber" IS NOT NULL) THEN ('Aftersales') ELSE ('nicht zuzuordnen') END as "Bereich",
  98. CASE WHEN (T7."UserName" IS NOT NULL) THEN (T7."UserName") WHEN (T8."UserName" IS NOT NULL) THEN (T8."UserName") WHEN (T9."UserName" IS NOT NULL) THEN (T9."UserName") ELSE ('n.N.') END as "Sel Name",
  99. T9."UserName" as "Username_GB_User_Usedvehicle",
  100. T8."UserName" as "Username_GB_User_NewVehicle",
  101. T6."CreatingOperator" as "Creatingoperator",
  102. T7."UserName" as "Username_GB_User_Service",
  103. T6."InvoicingOperator" as "Invoicingoperator",
  104. T6."WIPNumber" as "Wipnumber",
  105. T1."DueDate" as "Duedate",
  106. T1."LastLetterType" as "Lastlettertype",
  107. (rtrim((((T1."DocumentNumber"))))) + ' - ' + (convert(varchar(50), year(T1."DocumentDate")) + '-' + convert(varchar(50), month(T1."DocumentDate")) + '-' + convert(varchar(50), day(T1."DocumentDate"))) + ' - ' + T1."CustomerReference" + ' - ' + T1."MiscellaneousRef" + ' - MS: ' + T1."LastLetterType" as "Beleg",
  108. T5."LocationCode" + ' - ' + T5."Description" as "Standort_Name",
  109. T5."Description" as "Description_GB_Company",
  110. T5."LocationCode" as "Locationcode_GB_Company",
  111. T1."TruePostingOrigin" as "Truepostingorigin",
  112. T1."Client_DB" as "Client Db",
  113. T1."AccountCode" + ' - ' + T4."CustomerSName" as "Kunde",
  114. T4."Postcode" as "Postcode",
  115. T4."CustomerSName" as "Customersname",
  116. T4."AccountCode" as "Accountcode",
  117. T3."SalesInvoiceNumber" as "Salesinvoicenumber_VS_UsedVehicle",
  118. T2."SalesInvoiceNumber" as "Salesinvoicenumber_VS_NewVehicle",
  119. T3."SalesExecutive001" as "Salesexecutive001_VS_UsedVehicle",
  120. T2."SalesExecutive001" as "Salesexecutive001_VS_NewVehicle",
  121. T1."OriginalInvoice" as "Originalinvoice",
  122. T1."SocialIDVatReg" as "Socialidvatreg",
  123. T1."PaymentReference" as "Paymentreference",
  124. T1."PaymentTypeCode" as "Paymenttypecode",
  125. T1."ControlAccountType" as "Controlaccounttype",
  126. T1."LastLetterDate" as "Lastletterdate",
  127. T1."TRUEMOD" as "Truemod",
  128. T1."TaxBillPending" as "Taxbillpending",
  129. T1."TaxBillNumber" as "Taxbillnumber",
  130. T1."TaxBillDate" as "Taxbilldate",
  131. T1."BOEMagicNumber" as "Boemagicnumber",
  132. T1."USTaxCodes002" as "Ustaxcodes002",
  133. T1."USTaxCodes001" as "Ustaxcodes001",
  134. T1."DocumentArchiveID" as "Documentarchiveid",
  135. T1."SourceBranch" as "Sourcebranch",
  136. T1."SourceModule" as "Sourcemodule",
  137. T1."AllowanceCurrency" as "Allowancecurrency",
  138. T1."AllowanceBase" as "Allowancebase",
  139. T1."SystemMatchNumber" as "Systemmatchnumber",
  140. T1."PRFollowUpDate" as "Prfollowupdate",
  141. T1."PRSentTo" as "Prsentto",
  142. T1."PRComment" as "Prcomment",
  143. T1."PRDateRaised" as "Prdateraised",
  144. T1."PRReference" as "Prreference",
  145. T1."PRCode" as "Prcode",
  146. T1."MiscellaneousRef" as "Miscellaneousref",
  147. T1."SettledFlag" as "Settledflag",
  148. T1."StatementPrintedYN" as "Statementprintedyn",
  149. T1."DebtFlag" as "Debtflag",
  150. T1."SDiscountDate002" as "Sdiscountdate002",
  151. T1."SDiscountDate001" as "Sdiscountdate001",
  152. T1."SDiscountOffered002" as "Sdiscountoffered002",
  153. T1."SDiscountOffered001" as "Sdiscountoffered001",
  154. T1."Status" as "Status",
  155. T1."LastPmtDaybookItm" as "Lastpmtdaybookitm",
  156. T1."LastPmtDaybookNo" as "Lastpmtdaybookno",
  157. T1."LastPaymentRef" as "Lastpaymentref",
  158. T1."LastPaymentDate" as "Lastpaymentdate",
  159. T1."AmountPaidCurr" as "Amountpaidcurr",
  160. T1."AmountPaidBase" as "Amountpaidbase",
  161. T1."PostingDaybkDate" as "Postingdaybkdate",
  162. T1."DaybookItemNumber" as "Daybookitemnumber",
  163. T1."DaybookNumber" as "Daybooknumber",
  164. T1."VatAmountCurr" as "Vatamountcurr",
  165. T1."VATAmountBase" as "Vatamountbase",
  166. T1."DocumentCurrTotal" as "Documentcurrtotal",
  167. T1."DocumentType" as "Documenttype",
  168. T1."DocumentDate" as "Documentdate",
  169. T1."DocumentNumber" as "Documentnumber",
  170. T1."CustomerReference" as "Customerreference"
  171. from (((((((("AUTOLINE"."import"."SL_SLTransactions" T1 left outer join "AUTOLINE"."import"."VS_NewVehicleStock" T2 on (T2."SalesInvoiceNumber" = T1."DocumentNumber") and (T2."Client_DB" = T1."Client_DB")) left outer join "AUTOLINE"."import"."VS_UsedVehicleStock" T3 on (T1."DocumentNumber" = T3."SalesInvoiceNumber") and (T1."Client_DB" = T3."Client_DB")) left outer join "AUTOLINE"."import"."SL_SalesLedgerAccounts" T4 on (T1."AccountCode" = T4."AccountCode") and (T1."Client_DB" = T4."Client_DB")) left outer join "AUTOLINE"."import"."GB_CompanyLocations" T5 on T5."LocationCode" = T1."TruePostingOrigin") left outer join "AUTOLINE"."import"."SO_HeaderRecords" T6 on (T1."TruePostingOrigin" = T6."Client_DB") and ((substring(T1."MiscellaneousRef", 6, 5)) = (((T6."WIPNumber"))))) left outer join "AUTOLINE"."import"."GB_UserDetails" T7 on ((convert(float, T6."CreatingOperator")) = T7."POSOperator") and ((T6."Department" = T7."CRMDepartment") or (T7."POSOperator" = 888))) left outer join "AUTOLINE"."import"."GB_UserDetails" T8 on T8."UserID" = T2."SalesExecutive001") left outer join "AUTOLINE"."import"."GB_UserDetails" T9 on T9."UserID" = T3."SalesExecutive001")
  172. where (((T1."AmountPaidBase" <> T1."DocumentBaseTotal") and (T1."DocumentBaseTotal" <> 0)) and (not T1."AccountCode" IN ('000999')))
  173. -- order by "Accountcode_2" asc
  174. ) D1