group BusinessInvoice;

CHECK_TENANT() ::= "tenant_record_id = :tenantRecordId"
AND_CHECK_TENANT() ::= "AND <CHECK_TENANT()>"

getInvoice(invoice_id) ::= <<
select
  invoice_id
, invoice_number
, created_date
, updated_date
, account_id
, account_key
, invoice_date
, target_date
, currency
, balance
, amount_paid
, amount_charged
, amount_credited
, tenant_record_id
from bin
where invoice_id = :invoice_id
<AND_CHECK_TENANT()>
limit 1
;
>>

getInvoicesForAccount(account_id) ::= <<
select
  invoice_id
, invoice_number
, created_date
, updated_date
, account_id
, account_key
, invoice_date
, target_date
, currency
, balance
, amount_paid
, amount_charged
, amount_credited
, tenant_record_id
from bin
where account_id = :account_id
<AND_CHECK_TENANT()>
order by created_date asc
;
>>

getInvoicesForAccountByKey(account_key) ::= <<
select
  invoice_id
, invoice_number
, created_date
, updated_date
, account_id
, account_key
, invoice_date
, target_date
, currency
, balance
, amount_paid
, amount_charged
, amount_credited
, tenant_record_id
from bin
where account_key = :account_key
<AND_CHECK_TENANT()>
order by created_date asc
;
>>

createInvoice() ::= <<
insert into bin (
  invoice_id
, invoice_number
, created_date
, updated_date
, account_id
, account_key
, invoice_date
, target_date
, currency
, balance
, amount_paid
, amount_charged
, amount_credited
, account_record_id
, tenant_record_id
) values (
  :invoice_id
, :invoice_number
, :created_date
, :updated_date
, :account_id
, :account_key
, :invoice_date
, :target_date
, :currency
, :balance
, :amount_paid
, :amount_charged
, :amount_credited
, :accountRecordId
, :tenantRecordId
);
>>

updateInvoice() ::= <<
update bin set
  updated_date = :updated_date
, invoice_number = :invoice_number
, account_key = :account_key
, invoice_date = :invoice_date
, target_date = :target_date
, currency = :currency
, balance = :balance
, amount_paid = :amount_paid
, amount_charged = :amount_charged
, amount_credited = :amount_credited
where invoice_id = :invoice_id
<AND_CHECK_TENANT()>
;
>>

deleteInvoice(invoice_id) ::= <<
delete from bin where invoice_id = :invoice_id <AND_CHECK_TENANT()>;
>>

deleteInvoicesForAccount(account_id) ::= <<
delete from bin where account_id = :account_id <AND_CHECK_TENANT()>;
>>

test() ::= <<
select 1 from bin where <CHECK_TENANT()> limit 1;
>>
