File: //www/exchange2/sql/export/TK24733-export-1.02-a-ETR-Repayment-Receipt.sql
/* export all a-ETR Receips [IDP,PDO,TPO,ICP] (unallocated, allocated, on account, returned) from Exchange 2.0 */
/*--------- VALID ------------*/
SELECT
organisation_details.organisation_reference_id as "Originator ID",
IF(organisation_details.organisation_trade_name,organisation_details.organisation_trade_name,organisation_details.organisation_name) as "Originator Name",
debtors_detail.debtor_reference_id as "Credebtor ID",
IF(debtors_detail.trade_name,debtors_detail.trade_name,debtors_detail.debtor_name) as "Credebtor Name",
DATE(manual_transactions.transaction_date) as "Received",
manual_transactions.manual_transaction_id as "Transaction ID",
DATE(DATE(manual_transactions.created_at)) as "Posted",
IF(manual_transactions.false_payment=0 AND manual_transactions.reconcile_ref!=0,DATE(manual_transactions.transaction_date),"") as "Closed",
IF(manual_transactions.false_payment<>0,DATE(returned_transaction.transaction_date),"") as "Returned",
IF(manual_transactions.false_payment<>0,manual_transactions.false_payment,"") as "Returned Transaction ID",
IF(manual_transactions.transaction_type = 7 OR manual_transactions.transaction_type = 110, currencies_orig.currency_code, currencies_deb.currency_code) as "Ccy",
manual_transactions.amount as "Face Value",
IF(manual_transactions.transaction_type = 7 OR manual_transactions.transaction_type = 110, IF(currencies_orig.currency_code="EUR",1.000,currency_rates_orig.currency_rate),
IF(currencies_deb.currency_code="EUR",1.000,currency_rates_deb.currency_rate)) as "Traded Fx",
IF(manual_transactions.transaction_type=110,1,0) as "Deposit",
/*IF(manual_transactions.is_aetr = 1, "a",IF(manual_transactions.transaction_type=7,organisation_details.etr_type,IF(manual_transactions.orpa_id,"b",organisation_details.etr_type))),*/
manual_transactions.transaction_type as "Ex20 Transaction Type",
"a" as "ETR Type"
FROM manual_transactions
LEFT JOIN organisation_details ON organisation_details.user_id = manual_transactions.originator_id
LEFT JOIN debtors_detail ON debtors_detail.debtor_id = manual_transactions.debtor_id
LEFT JOIN currencies as currencies_deb ON currencies_deb.currency_id = debtors_detail.currency_id
LEFT JOIN currencies as currencies_orig ON currencies_orig.currency_id = manual_transactions.currency_id
LEFT JOIN currency_rates as currency_rates_deb ON currency_rates_deb.currency_id = debtors_detail.currency_id AND currency_rates_deb.currency_date = manual_transactions.transaction_date
LEFT JOIN currency_rates as currency_rates_orig ON currency_rates_orig.currency_id = manual_transactions.currency_id AND currency_rates_orig.currency_date = manual_transactions.transaction_date
LEFT JOIN manual_transactions as returned_transaction ON returned_transaction.manual_transaction_id = manual_transactions.false_payment
LEFT JOIN organisation_currencies ON organisation_currencies.user_id = manual_transactions.originator_id AND currencies_orig.currency_id = organisation_currencies.currency_id
LEFT JOIN organisation_currencies as oc_eur ON oc_eur.user_id = manual_transactions.originator_id AND oc_eur.currency_id=1
WHERE manual_transactions.transaction_type IN (1,2,9,19,7,110) AND manual_transactions.is_aetr = 1
/* AND manual_transactions.false_payment=0 AND manual_transactions.reconcile_ref=0 */
ORDER BY "Originator Name", "Credebtor Name";
/*---------------------*/
/*--------- OLD ------------*/
SELECT
organisation_details.organisation_reference_id as "Originator ID",
IF(organisation_details.organisation_trade_name, organisation_details.organisation_trade_name,organisation_details.organisation_name) as "Originator Name",
DATE(manual_transactions.transaction_date) as "Received",
manual_transactions.manual_transaction_id as "Transaction ID",
DATE(DATE(manual_transactions.created_at)) as "Posted",
currencies.currency_code as "Ccy",
manual_transactions.amount as "Face Value",
IF(currencies.currency_code="EUR",1.000,currency_rates.currency_rate) as "Traded Fx",
IF(manual_transactions.transaction_type=110,1,0) as "Deposit",
/*IF(manual_transactions.transaction_type=109,1,0) as "Fee Charge",*/
/*IF(manual_transactions.is_aetr = 1, "a",IF(manual_transactions.transaction_type=7,organisation_details.etr_type,IF(manual_transactions.orpa_id,"b",organisation_details.etr_type))),*/
"a" as "ETR Type"
FROM manual_transactions
LEFT JOIN organisation_details ON organisation_details.user_id = manual_transactions.originator_id
LEFT JOIN currencies ON currencies.currency_id = manual_transactions.currency_id
LEFT JOIN currency_rates ON currency_rates.currency_id = manual_transactions.currency_id AND currency_rates.currency_date = manual_transactions.transaction_date
LEFT JOIN organisation_currencies ON organisation_currencies.user_id = manual_transactions.originator_id AND currencies.currency_id = organisation_currencies.currency_id
LEFT JOIN organisation_currencies as oc_eur ON oc_eur.user_id = manual_transactions.originator_id AND oc_eur.currency_id=1
LEFT JOIN manual_transactions as returned_transaction ON returned_transaction.manual_transaction_id = manual_transactions.false_payment
WHERE manual_transactions.transaction_type IN(7,110) AND manual_transactions.is_aetr = 1 AND manual_transactions.false_payment=0
ORDER BY "Originator Name";
/* export all a-ETR Payments On Account (Returned) [OCPA,DEPOSIT] (returned) from Exchange 2.0 */
SELECT
organisation_details.organisation_reference_id as "Originator ID",
IF(organisation_details.organisation_trade_name, organisation_details.organisation_trade_name,organisation_details.organisation_name) as "Originator Name",
DATE(manual_transactions.transaction_date) as "Received",
manual_transactions.manual_transaction_id as "Transaction ID",
DATE(DATE(manual_transactions.created_at)) as "Posted",
DATE(returned_transaction.transaction_date) as "Returned",
manual_transactions.false_payment as "Returned Transaction ID",
currencies.currency_code as "Ccy",
manual_transactions.amount as "Face Value",
IF(currencies.currency_code="EUR",1.000,currency_rates.currency_rate) as "Traded Fx",
IF(manual_transactions.transaction_type=110,1,0) as "Deposit",
/*IF(manual_transactions.transaction_type=109,1,0) as "Fee Charge",*/
/*IF(manual_transactions.is_aetr = 1, "a",IF(manual_transactions.transaction_type=7,organisation_details.etr_type,IF(manual_transactions.orpa_id,"b",organisation_details.etr_type))),*/
"a" as "ETR Type"
FROM manual_transactions
LEFT JOIN organisation_details ON organisation_details.user_id = manual_transactions.originator_id
LEFT JOIN currencies ON currencies.currency_id = manual_transactions.currency_id
LEFT JOIN currency_rates ON currency_rates.currency_id = manual_transactions.currency_id AND currency_rates.currency_date = manual_transactions.transaction_date
LEFT JOIN organisation_currencies ON organisation_currencies.user_id = manual_transactions.originator_id AND currencies.currency_id = organisation_currencies.currency_id
LEFT JOIN organisation_currencies as oc_eur ON oc_eur.user_id = manual_transactions.originator_id AND oc_eur.currency_id=1
LEFT JOIN manual_transactions as returned_transaction ON returned_transaction.manual_transaction_id = manual_transactions.false_payment
WHERE manual_transactions.transaction_type IN(7,110) AND manual_transactions.is_aetr = 1 AND manual_transactions.false_payment<>0
ORDER BY "Originator Name";