DROP TABLE IF EXISTS vv_GLAccountRowsforBookEntry;
DROP VIEW IF EXISTS vv_GLAccountRowsforBookEntry;
CREATE VIEW vv_GLAccountRowsforBookEntry
AS
SELECT 1 PartnerType,
       0 GLRowsTypeVAT,
       t1.Oid Oid,
       t2.CustomersFrom CustomersFrom,
       t2.Customers Customers,
       t2.F_DateExecution DateExecution,
       t2.DocumentNumber DocumentNumber,
       t2.RegistryNumber RegistryNumber,
       CASE WHEN ifnull(t1.IsReinvoice,0)=0 THEN t3.ChartOfAccounts
                                 ELSE t3.ChartOfAccounts2
                                 END DebitChartOfAccounts,
       t5.ChartOfAccountsIn CreditChartOfAccounts,
       t1.Controlling Controlling,
       t1.JobNumberObjects JobNumberObjects,
       t7.UniqueObjects UniqueObjects,
       t1.CostPlace CostPlace,
       t1.CostHolder CostHolder,
       t2.F_CurrencyRate CurrencyRate,
       t1.Amount AmountHUF,
       CASE WHEN t2.F_CurrencyRate<>0 and t6.ShortName<>'HUF' THEN ROUND(t1.Amount/t2.F_CurrencyRate,2) ELSE 0 END AmountDEV,
       t2.ShortName Note1,                               
       '' Note2,
       NULL VAT,
       t1.ChartOfAccounts ChartOfAccounts2,
       ifnull(t1.IsReinvoice,0) IsReinvoice,
       t1.ContractRows ContractRows,
       ifnull(t1.F_ReadyforBookEntryRows,0) ReadyforBookEntryRows,
       t2.Oid RegistryHeader,
       Null InvoiceHeader,
       t2.CurrentWorkflowSystemSteps 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 
WHERE t1.GCRecord IS NULL AND
      ifnull(t2.IsEditable,0)=0 AND
      t2.F_GLAccounts IS NULL AND
      ifnull(t1.F_ReadyforBookEntryRows,0)=0

UNION ALL

SELECT 1 PartnerType,
       1 GLRowsTypeVAT,
       t1.Oid Oid,
       t2.CustomersFrom CustomersFrom,
       t2.Customers Customers,
       ifnull(ifnull(t1.DateVAT,t2.F_DateVAT),t2.F_DateExecution) DateExecution,
       t2.DocumentNumber DocumentNumber,
       t2.RegistryNumber RegistryNumber,
       t3.ChartOfAccountsIn DebitChartOfAccounts,
       case when ifnull(t3.InversState,0)=1 then t3.ChartOfAccountsOut 
            else t5.ChartOfAccountsIn end CreditChartOfAccounts,
       NULL Controlling,
       NULL JobNumberObjects,
       NULL UniqueObjects,
       NULL CostPlace,
       NULL CostHolder,
       1 CurrencyRate,
       CASE WHEN t1.VAT IS NULL THEN 0
            WHEN t1.VAT IS NOT NULL THEN 
                    case when t3.InversState=0 then ROUND(t1.BruttoAmount/(1+t3.KeyValue)*t3.KeyValue,0)
                         else ROUND(t1.BruttoAmount*t3.InversKeyValue,0) end
                      END AmountHUF,
       0 AmountDEV,
       t2.ShortName Note1,                               
       '' Note2,
       t1.VAT VAT,
       NULL ChartOfAccounts2,
       0 IsReinvoice,
       NULL ContractRows,
       ifnull(t1.F_ReadyforBookEntryRows,0) ReadyforBookEntryRows,
       t2.Oid RegistryHeader,
       NULL InvoiceHeader,
       t2.CurrentWorkflowSystemSteps 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 
WHERE t1.GCRecord IS NULL AND
      ifnull(t2.IsEditable,0)=0 AND
      t2.F_GLAccounts IS NULL AND
      ifnull(t1.F_ReadyforBookEntryRows,0)=0

UNION ALL

SELECT 0 PartnerType,
       0 GLRowsTypeVAT,
       t1.Oid Oid,
       t2.CustomersFrom CustomersFrom,
       t2.Customers Customers,
       t2.DateExecution DateExecution,
       t2.DocumentNumber DocumentNumber,
       '' RegistryNumber,
       t5.ChartOfAccountsOut DebitChartOfAccounts,
       ifnull(t9.ChartOfAccounts,t3.ChartOfAccounts) CreditChartOfAccounts,
       ifnull(t8.Controlling,t1.Controlling) Controlling,
       ifnull(t8.JobNumberObjects,t1.JobNumberObjects) JobNumberObjects,
       ifnull(t8.UniqueObjects,t1.UniqueObjects) UniqueObjects,
       ifnull(t8.CostPlace,t1.CostPlace) CostPlace,
       ifnull(t8.CostHolder,t1.CostHolder) CostHolder,
       t2.CurrencyRate CurrencyRate,
       (t1.ListPriceHUF-t1.DiscountPriceHUF)*t1.QTT AmountHUF,
       (t1.ListPriceDEV-t1.DiscountPriceDEV)*t1.QTT AmountDEV,
       t2.Description Note1,                               
       '' Note2,
       NULL VAT,
       t1.ChartOfAccounts ChartOfAccounts2,
       0 IsReinvoice,
       t1.ContractRows ContractRows,
       ifnull(t1.ReadyforBookEntryRows,0) ReadyforBookEntryRows,
       NULL RegistryHeader,
       t2.Oid InvoiceHeader,
       t1.WorkflowSystemSteps 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
WHERE t1.GCRecord IS NULL AND
      ifnull(t2.IsEditable,0)=0 AND
      t2.GLAccounts IS NULL AND
      ifnull(t1.ReadyforBookEntryRows,0)=0 AND
      t7.InvoiceTypeBase in (1,2,3) and
      ifnull(t2.IsProforma,0)=0

UNION ALL

SELECT 0 PartnerType,
       1 GLRowsTypeVAT,
       t1.Oid Oid,
       t2.CustomersFrom CustomersFrom,
       t2.Customers Customers,
       t2.DateExecution DateExecution,
       t2.DocumentNumber DocumentNumber,
       '' RegistryNumber,
       t5.ChartOfAccountsOut DebitChartOfAccounts,
       t3.ChartOfAccountsOut CreditChartOfAccounts,
       NULL Controlling,
       NULL JobNumberObjects,
       NULL UniqueObjects,
       NULL CostPlace,
       NULL CostHolder,
       1 CurrencyRate,
       t1.AmountVATHUF AmountHUF,
       t1.AmountVATDEV AmountDEV,
       t2.Description Note1,                               
       '' Note2,
       t1.VAT VAT,
       NULL ChartOfAccounts2,
       0 IsReinvoice,
       NULL ContractRows,
       ifnull(t1.ReadyforBookEntryRows,0) ReadyforBookEntryRows,
       NULL RegistryHeader,
       t2.Oid InvoiceHeader,
       Null 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 t7 ON
     t2.InvoiceType=t7.Oid
WHERE t1.GCRecord IS NULL AND
      ifnull(t2.IsEditable,0)=0 AND
      t2.GLAccounts IS NULL AND
      ifnull(t1.ReadyforBookEntryRows,0)=0 AND
      ifnull(t2.IsProforma,0)=0 AND
      t7.InvoiceTypeBase in (1,2,3)
