

SELECT 1 As "PartnerType",
       0 As "GLRowsTypeVAT",
       t1."Oid" As "Oid",
       t2."CustomersFrom" As "CustomersFrom",
       t2."Customers" As "Customers",
       t2."F_DateExecution" As "DateExecution",
       t2."DocumentNumber" As "DocumentNumber",
       t2."RegistryNumber" As "RegistryNumber",
       CASE WHEN coalesce(t1."IsReInvoice",false)=false THEN t3."ChartOfAccounts"
                                 ELSE t8."ChartOfAccounts2"
                                 END As "DebitChartOfAccounts",
       t9."ChartOfAccountsIn" As "CreditChartOfAccounts",
       t1."Controlling" As "Controlling",
       t1."JobNumberObjects" As "JobNumberObjects",
       t7."UniqueObjects" As "UniqueObjects",
       t1."CostPlace" As "CostPlace",
       t1."CostHolder" As "CostHolder",
       t2."F_CurrencyRate" As "CurrencyRate",
       t1."Amount" As "AmountHUF",
       CASE WHEN t2."F_CurrencyRate"<>0 AND t6."ShortName"<>'HUF' THEN ROUND(cast(t1."Amount"/t2."F_CurrencyRate" AS numeric),2) ELSE 0 END As "AmountDEV",
       t2."ShortName" As "Note1",                               
       '' As "Note2",
       NULL As "VAT",
       t1."ChartOfAccounts" As "ChartOfAccounts2",
       coalesce(t1."IsReInvoice",false) As "IsReinvoice",
       t1."ContractRows" As "ContractRows",
       coalesce(t1."F_ReadyforBookEntryRows",false) As "ReadyforBookEntryRows",
       t2."Oid" As "RegistryHeader",
       NULL As "InvoiceHeader",
       t2."CurrentWorkflowSystemSteps" As "WorkflowSystemSteps",
       t1."WorkflowMessage" "WorkflowMessage"
from "RegistryRowsFinancialControlling" t1 join "RegistryHeader" t2 on
     t1."RegistryHeader"=t2."Oid" LEFT JOIN "Controlling" t3 ON
     t1."Controlling"=t3."Oid" LEFT JOIN "Customers" t4 ON
     t2."Customers"=t4."Oid" LEFT JOIN "CustomersGLParameters" t5 ON
     t4."CustomersGLParameters"=t5."Oid" LEFT JOIN "CurrencyName" t6 ON
     t2."F_CurrencyName"=t6."Oid" LEFT JOIN "ContractRows" t7 ON
     t1."ContractRows"=t7."Oid" left join "ControllingYear" t8 on
     t3."Oid"=t8."Controlling" and extract(year from cast(t2."F_DateExecution" as timestamp))=t8."AccountYear" left join "CustomersGLParametersYear" t9 on
     t5."Oid"=t9."CustomersGLParameters" and extract(year from cast(t2."F_DateExecution"as timestamp))=t9."AccountYear"
WHERE t1."GCRecord" IS NULL AND
      coalesce(t2."IsEditable",false)=false AND
      t2."F_GLAccounts" IS NULL AND
      coalesce(t1."F_ReadyforBookEntryRows",false)=false

UNION ALL

SELECT 1 As "PartnerType",
       1 As "GLRowsTypeVAT",
       t1."Oid"As " Oid",
       t2."CustomersFrom" As "CustomersFrom",
       t2."Customers" As "Customers",
       coalesce(coalesce(t1."DateVAT",t2."F_DateVAT"),t2."F_DateExecution") As "DateExecution",
       t2."DocumentNumber" As "DocumentNumber",
       t2."RegistryNumber" As "RegistryNumber",
       t3."ChartOfAccountsIn" As "DebitChartOfAccounts",
       case when coalesce(t3."InversState",false)=true then t7."ChartOfAccountsOut" 
            else t9."ChartOfAccountsIn" end "CreditChartOfAccounts",
       NULL As "Controlling",
       NULL As "JobNumberObjects",
       NULL As "UniqueObjects",
       NULL As "CostPlace",
       NULL As "CostHolder",
       1 As "CurrencyRate",
      CASE WHEN t1."VAT" IS NULL THEN 0
            WHEN t1."VAT" IS NOT NULL THEN 
                 case when t3."InversState"=false then ROUND(cast(t1."BruttoAmount"/(1+t3."KeyValue")*t3."KeyValue" as numeric),0)
                      else ROUND(cast(t1."BruttoAmount"/(1+t3."InversKeyValue")*t3."InversKeyValue" as numeric),0) end
       END "AmountHUF",
       0 As "AmountDEV",
       t2."ShortName" As "Note1",                               
       '' As "Note2",
       t1."VAT" As "VAT",
       NULL As "ChartOfAccounts2",
       false As "IsReinvoice",
       NULL As "ContractRows",
       coalesce(t1."F_ReadyforBookEntryRows",false) As "ReadyforBookEntryRows",
       t2."Oid" As "RegistryHeader",
       NULL As "InvoiceHeader",
       t2."CurrentWorkflowSystemSteps" As "WorkflowSystemSteps",
      '' "WorkflowMessage"
from "RegistryRowsFinancial" t1 join "RegistryHeader" t2 on
     t1."RegistryHeader"=t2."Oid" LEFT JOIN "VAT" t3 ON
     t1."VAT"=t3."Oid" LEFT JOIN "Customers" t4 ON
     t2."Customers"=t4."Oid" LEFT JOIN "CustomersGLParameters" t5 ON
     t4."CustomersGLParameters"=t5."Oid" LEFT JOIN "CurrencyName" t6 ON
     t2."F_CurrencyName"=t6."Oid" left join "VATYear" t7 on
     t3."Oid"=t7."VAT" and extract(year from cast(t2."F_DateExecution" as timestamp))=t7."AccountYear" left join "CustomersGLParametersYear" t9 on
     t5."Oid"=t9."CustomersGLParameters" and extract(year from cast(t2."F_DateExecution" as timestamp))=t9."AccountYear"
WHERE t1."GCRecord" IS NULL AND
      coalesce(t2."IsEditable",false)=false AND
      t2."F_GLAccounts" IS NULL AND
      coalesce(t1."F_ReadyforBookEntryRows",false)=false

UNION ALL

SELECT 0 As "PartnerType",
       0 As "GLRowsTypeVAT",
       t1."Oid" As "Oid",
       t2."CustomersFrom" As "CustomersFrom",
       t2."Customers" As "Customers",
       t2."DateExecution" As "DateExecution",
       t2."DocumentNumber" As "DocumentNumber",
       '' As "RegistryNumber",
       t11."ChartOfAccountsOut" As "DebitChartOfAccounts",
       coalesce(t12."ChartOfAccounts",t10."ChartOfAccounts") As "CreditChartOfAccounts",
       coalesce(t8."Controlling",t1."Controlling") As "Controlling",
       coalesce(t8."JobNumberObjects",t1."JobNumberObjects") As "JobNumberObjects",
       coalesce(t8."UniqueObjects",t1."UniqueObjects") As "UniqueObjects",
       coalesce(t8."CostPlace",t1."CostPlace") As "CostPlace",
       coalesce(t8."CostHolder",t1."CostHolder") As "CostHolder",
       t2."CurrencyRate" As "CurrencyRate",
       (t1."ListPriceHUF"-t1."DiscountPriceHUF")*t1."QTT" As "AmountHUF",
       (t1."ListPriceDEV"-t1."DiscountPriceDEV")*t1."QTT" As "AmountDEV",
       t2."Description" As "Note1",                               
       '' As "Note2",
       NULL As "VAT",
       t1."ChartOfAccounts" As "ChartOfAccounts2",
       false As "IsReinvoice",
       t1."ContractRows" As "ContractRows",
       coalesce(t1."ReadyforBookEntryRows",false) As "ReadyforBookEntryRows",
       NULL As "RegistryHeader",
       t2."Oid" As "InvoiceHeader",
       t1."WorkflowSystemSteps" As "WorkflowSystemSteps",
       '' "WorkflowMessage"
FROM "InvoiceRows" t1 JOIN "InvoiceHeader" t2 ON
     t1."InvoiceHeader"=t2."Oid" LEFT JOIN "Controlling" t3 ON
     t1."Controlling"=t3."Oid" LEFT JOIN "Customers" t4 ON
     t2."Customers"=t4."Oid" LEFT JOIN "CustomersGLParameters" t5 ON
     t4."CustomersGLParameters"=t5."Oid" LEFT JOIN "CurrencyName" t6 ON
     t2."CurrencyName"=t6."Oid" LEFT JOIN "InvoiceType" t7 ON
     t2."InvoiceType"=t7."Oid" LEFT JOIN "ContractRows" t8 ON
     t1."ContractRows"=t8."Oid" LEFT JOIN "Controlling" t9 ON
     t8."Controlling"=t9."Oid" left join "ControllingYear" t10 on
     t3."Oid"=t10."Controlling" and extract(year from cast(t2."DateExecution" as timestamp))=t10."AccountYear" left join "CustomersGLParametersYear" t11 on
     t5."Oid"=t11."CustomersGLParameters" and extract(year from cast(t2."DateExecution" as timestamp))=t11."AccountYear" left join "ControllingYear" t12 on
     t9."Oid"=t12."Controlling" and extract(year from cast(t2."DateExecution" as timestamp))=t12."AccountYear"
WHERE t1."GCRecord" IS NULL AND
      coalesce(t2."IsEditable",false)=false AND
      t2."GLAccounts" IS NULL AND
      coalesce(t1."ReadyforBookEntryRows",false)=false AND
      t7."InvoiceTypeBase" IN (1,2,3) AND
      coalesce(t2."IsProforma",false)=false

UNION ALL

SELECT 0 As "PartnerType",
       1 As "GLRowsTypeVAT",
       t1."Oid" As "Oid",
       t2."CustomersFrom" As "CustomersFrom",
       t2."Customers" As "Customers",
       t2."DateExecution" As "DateExecution",
       t2."DocumentNumber" As "DocumentNumber",
       '' As "RegistryNumber",
       t9."ChartOfAccountsOut" As "DebitChartOfAccounts",
       t7."ChartOfAccountsOut" As "CreditChartOfAccounts",
       NULL As "Controlling",
       NULL As "JobNumberObjects",
       NULL As "UniqueObjects",
       NULL As "CostPlace",
       NULL As "CostHolder",
       1 As "CurrencyRate2",
       t1."AmountVATHUF" As "AmountHUF",
       t1."AmountVATDEV" As "AmountDEV",
       t2."Description" As "Note1",                               
       '' As "Note2",
       t1."VAT" As "VAT",
       NULL As "ChartOfAccounts2",
       false As "IsReinvoice",
       NULL As "ContractRows",
       coalesce(t1."ReadyforBookEntryRows",false) As "ReadyforBookEntryRows",
       NULL As "RegistryHeader",
       t2."Oid" As "InvoiceHeader",
       Null As "WorkflowSystemSteps",
       '' "WorkflowMessage"
from "InvoiceVATRows" t1 join "InvoiceHeader" t2 on
     t1."InvoiceHeader"=t2."Oid" LEFT JOIN "VAT" t3 ON
     t1."VAT"=t3."Oid" LEFT JOIN "Customers" t4 ON
     t2."Customers"=t4."Oid" LEFT JOIN "CustomersGLParameters" t5 ON
     t4."CustomersGLParameters"=t5."Oid" LEFT JOIN "CurrencyName" t6 ON
     t2."CurrencyName"=t6."Oid" LEFT JOIN "InvoiceType" t10 ON
     t2."InvoiceType"=t10."Oid" left join "VATYear" t7 on
     t3."Oid"=t7."VAT" and extract(year from cast(t2."DateExecution" as timestamp))=t7."AccountYear" left join "CustomersGLParametersYear" t9 on
     t5."Oid"=t9."CustomersGLParameters" and extract(year from cast(t2."DateExecution" as timestamp))=t9."AccountYear"
WHERE t1."GCRecord" IS NULL AND
      coalesce(t2."IsEditable",false)=false AND
      t2."GLAccounts" IS NULL AND
      coalesce(t1."ReadyforBookEntryRows",false)=false AND
      coalesce(t2."IsProforma",false)=false AND
      t10."InvoiceTypeBase" IN (1,2,3)

