/*Debts:*/
select 'INVENTORY' as ACC_DEBIT, INVITEMCATEGORY.ITSNAME as SUBACC, sum(PURCHASEINVOICELINE.ITSTOTAL) as ITSTOTAL
from PURCHASEINVOICELINE
join PURCHASEINVOICE on PURCHASEINVOICE.ITSID = PURCHASEINVOICELINE.ITSOWNER
join INVITEM on INVITEM.ITSID = PURCHASEINVOICELINE.INVITEM
join INVITEMCATEGORY on INVITEMCATEGORY.ITSID = INVITEM.ITSCATEGORY 
where ITSDATE >= :DATE1 and ITSDATE <= :DATE2
group by ACC_DEBIT, SUBACC

union all

select 'SALES_TAX_RECIVABLE' as ACC_DEBIT, TAX.ITSNAME as SUBACC, sum(PURCHASEINVOICETAXLINE.ITSTOTAL) as ITSTOTAL
from PURCHASEINVOICETAXLINE 
join PURCHASEINVOICE on PURCHASEINVOICE.ITSID = PURCHASEINVOICETAXLINE.ITSOWNER
join TAX on TAX.ITSID = PURCHASEINVOICETAXLINE.ITSTAX 
where TAX.DUEMETHOD = 0 and ITSDATE >= :DATE1 and ITSDATE <= :DATE2
group by ACC_DEBIT, SUBACC

union all

select 'ACC_RECIVABLE' as ACC_DEBIT, DEBTORCREDITOR.ITSNAME as SUBACC, sum(SALESINVOICELINE.TOTALWITHTAXES ) as ITSTOTAL
from SALESINVOICELINE
join SALESINVOICE on SALESINVOICE.ITSID = SALESINVOICELINE.ITSOWNER
join DEBTORCREDITOR on DEBTORCREDITOR.ITSID = SALESINVOICE.CUSTOMER 
where ITSDATE >= :DATE1 and ITSDATE <= :DATE2
group by ACC_DEBIT, SUBACC

union all

select 'COGSKNC' as ACC_DEBIT, INVITEMCATEGORY.ITSNAME as SUBACC, sum(SALESINVOICELINE.ITSQUANTITY * INVITEM.KNOWNCOST) as ITSTOTAL
from SALESINVOICELINE
join SALESINVOICE on SALESINVOICE.ITSID = SALESINVOICELINE.ITSOWNER
join INVITEM on INVITEM.ITSID = SALESINVOICELINE.INVITEM
join INVITEMCATEGORY on INVITEMCATEGORY.ITSID = INVITEM.ITSCATEGORY 
where INVITEM.KNOWNCOST is not null and ITSDATE >= :DATE1 and ITSDATE <= :DATE2
group by ACC_DEBIT, SUBACC

/*Credits:*/
select 'ACC_PAYABLE' as ACC_CREDIT, DEBTORCREDITOR.ITSNAME as SUBACC, sum(PURCHASEINVOICELINE.TOTALWITHTAXES ) as ITSTOTAL
from PURCHASEINVOICELINE
join PURCHASEINVOICE on PURCHASEINVOICE.ITSID = PURCHASEINVOICELINE.ITSOWNER
join DEBTORCREDITOR on DEBTORCREDITOR.ITSID = PURCHASEINVOICE.SUPPLIER 
where ITSDATE >= :DATE1 and ITSDATE <= :DATE2
group by ACC_CREDIT,  SUBACC

union all

select 'SALES_TAX_PAYABLE' as ACC_CREDIT, TAX.ITSNAME as SUBACC, sum(SALESINVOICETAXLINE.ITSTOTAL) as ITSTOTAL
from SALESINVOICETAXLINE 
join SALESINVOICE on SALESINVOICE.ITSID = SALESINVOICETAXLINE.ITSOWNER
join TAX on TAX.ITSID = SALESINVOICETAXLINE.ITSTAX 
where TAX.DUEMETHOD = 0 and ITSDATE >= :DATE1 and ITSDATE <= :DATE2
group by ACC_CREDIT, SUBACC

union all

select 'INVENTORY' as ACC_CREDIT, INVITEMCATEGORY.ITSNAME as SUBACC, sum(SALESINVOICELINE.ITSQUANTITY * INVITEM.KNOWNCOST) as ITSTOTAL
from SALESINVOICELINE
join SALESINVOICE on SALESINVOICE.ITSID = SALESINVOICELINE.ITSOWNER
join INVITEM on INVITEM.ITSID = SALESINVOICELINE.INVITEM
join INVITEMCATEGORY on INVITEMCATEGORY.ITSID = INVITEM.ITSCATEGORY 
where INVITEM.KNOWNCOST is not null and ITSDATE >= :DATE1 and ITSDATE <= :DATE2
group by ACC_CREDIT, SUBACC

union all

select 'SALES' as ACC_CREDIT, INVITEMCATEGORY.ITSNAME as SUBACC, sum(SALESINVOICELINE.ITSTOTAL) as ITSTOTAL
from SALESINVOICELINE
join SALESINVOICE on SALESINVOICE.ITSID = SALESINVOICELINE.ITSOWNER
join INVITEM on INVITEM.ITSID = SALESINVOICELINE.INVITEM
join INVITEMCATEGORY on INVITEMCATEGORY.ITSID = INVITEM.ITSCATEGORY 
where ITSDATE >= :DATE1 and ITSDATE <= :DATE2
group by ACC_CREDIT, SUBACC

/*Balance:*/
select ACCOUNT.ITSNUMBER, ACCOUNT.ITSNAME, SUBACC,
case when ACCOUNT.NORMALBALANCETYPE = 0 then ALL_RECORDS.DEBIT - ALL_RECORDS.CREDIT
else 0 end as DEBIT,
case when ACCOUNT.NORMALBALANCETYPE = 1 then ALL_RECORDS.CREDIT - ALL_RECORDS.DEBIT
else 0 end as CREDIT
from
  (
    select ACC, SUBACC, sum(DEBIT) as DEBIT, sum(CREDIT) as CREDIT
    from 
      (
        select ACC_DEBIT as ACC, SUBACC, ITSTOTAL as DEBIT, 0.00 as CREDIT
        from 
          (
              select 'INVENTORY' as ACC_DEBIT, INVITEMCATEGORY.ITSNAME as SUBACC, sum(PURCHASEINVOICELINE.ITSTOTAL) as ITSTOTAL
              from PURCHASEINVOICELINE
              join PURCHASEINVOICE on PURCHASEINVOICE.ITSID = PURCHASEINVOICELINE.ITSOWNER
              join INVITEM on INVITEM.ITSID = PURCHASEINVOICELINE.INVITEM
              join INVITEMCATEGORY on INVITEMCATEGORY.ITSID = INVITEM.ITSCATEGORY 
              where ITSDATE >= 1459458000000 and ITSDATE <= 1460622396965
              group by ACC_DEBIT, SUBACC

              union all

              select 'SALES_TAX_RECIVABLE' as ACC_DEBIT, TAX.ITSNAME as SUBACC, sum(PURCHASEINVOICETAXLINE.ITSTOTAL) as ITSTOTAL
              from PURCHASEINVOICETAXLINE 
              join PURCHASEINVOICE on PURCHASEINVOICE.ITSID = PURCHASEINVOICETAXLINE.ITSOWNER
              join TAX on TAX.ITSID = PURCHASEINVOICETAXLINE.ITSTAX 
              where TAX.DUEMETHOD = 0 and ITSDATE >= 1459458000000 and ITSDATE <= 1460622396965
              group by ACC_DEBIT, SUBACC

              union all

              select 'ACC_RECIVABLE' as ACC_DEBIT, DEBTORCREDITOR.ITSNAME as SUBACC, sum(SALESINVOICELINE.TOTALWITHTAXES ) as ITSTOTAL
              from SALESINVOICELINE
              join SALESINVOICE on SALESINVOICE.ITSID = SALESINVOICELINE.ITSOWNER
              join DEBTORCREDITOR on DEBTORCREDITOR.ITSID = SALESINVOICE.CUSTOMER 
              where ITSDATE >= 1459458000000 and ITSDATE <= 1460622396965
              group by ACC_DEBIT,  SUBACC

              union all

              select 'COGSKNC' as ACC_DEBIT, INVITEMCATEGORY.ITSNAME as SUBACC, sum(SALESINVOICELINE.ITSQUANTITY * INVITEM.KNOWNCOST) as ITSTOTAL
              from SALESINVOICELINE
              join SALESINVOICE on SALESINVOICE.ITSID = SALESINVOICELINE.ITSOWNER
              join INVITEM on INVITEM.ITSID = SALESINVOICELINE.INVITEM
              join INVITEMCATEGORY on INVITEMCATEGORY.ITSID = INVITEM.ITSCATEGORY 
              where INVITEM.KNOWNCOST is not null and ITSDATE >= 1459458000000 and ITSDATE <= 1460622396965
              group by ACC_DEBIT, SUBACC
          )
          
        union all
          
        select ACC_CREDIT as ACC, SUBACC, 0.00 as DEBIT, ITSTOTAL as CREDIT
        from 
          (
            select 'ACC_PAYABLE' as ACC_CREDIT, DEBTORCREDITOR.ITSNAME as SUBACC, sum(PURCHASEINVOICELINE.TOTALWITHTAXES ) as ITSTOTAL
            from PURCHASEINVOICELINE
            join PURCHASEINVOICE on PURCHASEINVOICE.ITSID = PURCHASEINVOICELINE.ITSOWNER
            join DEBTORCREDITOR on DEBTORCREDITOR.ITSID = PURCHASEINVOICE.SUPPLIER 
            where ITSDATE >= 1459458000000 and ITSDATE <= 1460622396965
            group by ACC_CREDIT,  SUBACC

            union all

            select 'SALES_TAX_PAYABLE' as ACC_CREDIT, TAX.ITSNAME as SUBACC, sum(SALESINVOICETAXLINE.ITSTOTAL) as ITSTOTAL
            from SALESINVOICETAXLINE 
            join SALESINVOICE on SALESINVOICE.ITSID = SALESINVOICETAXLINE.ITSOWNER
            join TAX on TAX.ITSID = SALESINVOICETAXLINE.ITSTAX 
            where TAX.DUEMETHOD = 0 and ITSDATE >= 1459458000000 and ITSDATE <= 1460622396965
            group by ACC_CREDIT, SUBACC

            union all

            select 'INVENTORY' as ACC_CREDIT, INVITEMCATEGORY.ITSNAME as SUBACC, sum(SALESINVOICELINE.ITSQUANTITY * INVITEM.KNOWNCOST) as ITSTOTAL
            from SALESINVOICELINE
            join SALESINVOICE on SALESINVOICE.ITSID = SALESINVOICELINE.ITSOWNER
            join INVITEM on INVITEM.ITSID = SALESINVOICELINE.INVITEM
            join INVITEMCATEGORY on INVITEMCATEGORY.ITSID = INVITEM.ITSCATEGORY 
            where INVITEM.KNOWNCOST is not null and ITSDATE >= 1459458000000 and ITSDATE <= 1460622396965
            group by ACC_CREDIT, SUBACC

            union all

            select 'SALES' as ACC_CREDIT, INVITEMCATEGORY.ITSNAME as SUBACC, sum(SALESINVOICELINE.ITSTOTAL) as ITSTOTAL
            from SALESINVOICELINE
            join SALESINVOICE on SALESINVOICE.ITSID = SALESINVOICELINE.ITSOWNER
            join INVITEM on INVITEM.ITSID = SALESINVOICELINE.INVITEM
            join INVITEMCATEGORY on INVITEMCATEGORY.ITSID = INVITEM.ITSCATEGORY 
            where ITSDATE >= 1459458000000 and ITSDATE <= 1460622396965
            group by ACC_CREDIT, SUBACC
          )
      )
    group by ACC, SUBACC
  ) as ALL_RECORDS
join ACCOUNT on ALL_RECORDS.ACC = ACCOUNT.ITSID
order by ACCOUNT.ITSNUMBER, SUBACC;
