-- Group: cashReceipts -- Name: detail -- Notes: used by dspCashReceipts -- There are 2 display modes for this query: -- 1) Legacy mode which uses A/R Application information -- 2) Version 3.3.0 upgrade mode which uses Cash Receipt information -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. -- See www.xtuple.com/CPAL for the full text of the software license. -- Posted cash receipts SELECT arapply_id, 1 AS type, '' AS cashrcpt_number, cust_number, cust_name, false AS voided, true AS posted, arapply_postdate AS postdate, ( CASE WHEN (arapply_source_doctype='C') THEN WHEN (arapply_source_doctype='R') THEN WHEN (arapply_fundstype='C') THEN WHEN (arapply_fundstype='T') THEN WHEN (arapply_fundstype='M') THEN WHEN (arapply_fundstype='V') THEN WHEN (arapply_fundstype='A') THEN WHEN (arapply_fundstype='D') THEN WHEN (arapply_fundstype='R') THEN WHEN (arapply_fundstype='K') THEN WHEN (arapply_fundstype='W') THEN WHEN (arapply_fundstype='O') THEN END || ' ' || CASE WHEN (arapply_source_doctype IN ('C','R')) THEN TEXT(arapply_source_docnumber) ELSE arapply_refnumber END ) AS source, COALESCE(cashrcptitem_cashrcpt_id,cashrcptmisc_cashrcpt_id,-1) AS source_xtidrole, ( CASE WHEN (arapply_target_doctype='D') THEN WHEN (arapply_target_doctype='I') THEN ELSE END || ' ' || TEXT(arapply_target_docnumber) ) AS target, COALESCE(arapply_target_aropen_id,-1) AS target_xtidrole, arapply_applied AS applied, 'curr' AS applied_xtnumericrole, currtobase(arapply_curr_id,arapply_applied,arapply_postdate) AS base_applied, formatDate(arapply_postdate) AS f_postdate, formatMoney(arapply_applied) AS f_applied, formatMoney(currtobase(arapply_curr_id,arapply_applied,arapply_postdate)) AS f_base_applied, 'curr' AS base_applied_xtnumericrole, 0 AS base_applied_xttotalrole, currConcat(arapply_curr_id) AS currAbbr, arapply_postdate AS sortdate FROM custinfo LEFT OUTER JOIN custgrpitem ON (custgrpitem_cust_id=cust_id), arapply LEFT OUTER JOIN cashrcptitem ON ((arapply_reftype='CRA') AND (arapply_ref_id=cashrcptitem_id)) LEFT OUTER JOIN cashrcptmisc ON ((arapply_reftype='CRD') AND (arapply_ref_id=cashrcptmisc_id)) WHERE ( (arapply_cust_id=cust_id) AND (arapply_postdate BETWEEN AND ) AND (arapply_source_doctype ='K') AND (cust_id=) AND (cust_custtype_id=) AND (custgrpitem_custgrp_id=) AND (cust_custtype_id IN (SELECT custtype_id FROM custtype WHERE (custtype_code ~ ))) ) -- Unposted cash receipts UNION SELECT cashrcpt_id, 2 AS type, '' AS cashrcpt_number, cust_number, cust_name, false AS voided, false AS posted, cashrcpt_distdate AS postdate, ( CASE WHEN (cashrcpt_fundstype='C') THEN WHEN (cashrcpt_fundstype='T') THEN WHEN (cashrcpt_fundstype='M') THEN WHEN (cashrcpt_fundstype='V') THEN WHEN (cashrcpt_fundstype='A') THEN WHEN (cashrcpt_fundstype='D') THEN WHEN (cashrcpt_fundstype='R') THEN WHEN (cashrcpt_fundstype='K') THEN WHEN (cashrcpt_fundstype='W') THEN WHEN (cashrcpt_fundstype='O') THEN END || ' ' || cashrcpt_docnumber ) AS source, cashrcpt_id AS source_xtidrole, AS target, -1 AS target_xtidrole, cashrcpt_amount AS applied, 'curr' AS applied_xtnumericrole, (cashrcpt_amount / cashrcpt_curr_rate) AS base_applied, formatDate(cashrcpt_distdate) AS f_postdate, formatMoney(cashrcpt_amount) AS f_applied, formatMoney(cashrcpt_amount / cashrcpt_curr_rate) AS f_base_applied, 'curr' AS base_applied_xtnumericrole, 0 AS base_applied_xttotalrole, currConcat(cashrcpt_curr_id) AS currAbbr, cashrcpt_distdate AS sortdate FROM cashrcpt, custinfo LEFT OUTER JOIN custgrpitem ON (custgrpitem_cust_id=cust_id) WHERE ( (NOT cashrcpt_posted) AND (cashrcpt_cust_id=cust_id) AND (cashrcpt_distdate BETWEEN AND ) AND (cust_id=) AND (cust_custtype_id=) AND (custgrpitem_custgrp_id=) AND (cust_custtype_id IN (SELECT custtype_id FROM custtype WHERE (custtype_code ~ ))) ) -- Cash Advance UNION SELECT aropen_id, 3 AS type, '' AS cashrcpt_number, cust_number, cust_name, false AS voided, true AS posted, aropen_docdate AS postdate, ( CASE WHEN (substr(aropen_notes, 16, 1)='C') THEN WHEN (substr(aropen_notes, 16, 1)='T') THEN WHEN (substr(aropen_notes, 16, 1)='M') THEN WHEN (substr(aropen_notes, 16, 1)='V') THEN WHEN (substr(aropen_notes, 16, 1)='A') THEN WHEN (substr(aropen_notes, 16, 1)='D') THEN WHEN (substr(aropen_notes, 16, 1)='R') THEN WHEN (substr(aropen_notes, 16, 1)='K') THEN WHEN (substr(aropen_notes, 16, 1)='W') THEN WHEN (substr(aropen_notes, 16, 1)='O') THEN END || ' ' || substr(aropen_notes, 18) ) AS source, cashrcptitem_cashrcpt_id AS source_xtidrole, CASE WHEN aropen_doctype = 'R' THEN ELSE END AS target, aropen_id AS target_xtidrole, aropen_amount AS applied, 'curr' AS applied_xtnumericrole, aropen_amount / aropen_curr_rate AS base_applied, formatDate(aropen_docdate) AS f_postdate, formatMoney(aropen_amount) AS f_applied, formatMoney(aropen_amount / aropen_curr_rate) AS f_base_applied, 'curr' AS base_applied_xtnumericrole, 0 AS base_applied_xttotalrole, currConcat(aropen_curr_id) AS currAbbr, aropen_duedate AS sortdate FROM custinfo LEFT OUTER JOIN custgrpitem ON (custgrpitem_cust_id=cust_id), aropen JOIN cashrcptitem ON (aropen_id=cashrcptitem_aropen_id) WHERE ( (aropen_cust_id=cust_id) AND (aropen_doctype IN ('R','C')) AND (aropen_docdate BETWEEN AND ) AND (cust_id=) AND (cust_custtype_id=) AND (custgrpitem_custgrp_id=) AND (cust_custtype_id IN (SELECT custtype_id FROM custtype WHERE (custtype_code ~ ))) ) ORDER BY sortdate, source; -- New Display Mode SELECT id, type, cashrcpt_number, cust_number, cust_name, postdate, posted, voided, source, source_xtidrole, posted, target, target_xtidrole, applied, base_applied, formatDate(postdate) AS f_postdate, formatMoney(applied) AS f_applied, formatMoney(base_applied) AS f_base_applied, currAbbr, sortdate, 'curr' AS applied_xtnumericrole, 'curr' AS base_applied_xtnumericrole, base_applied_xttotalrole, type AS xtindentrole FROM ( -- Cash Receipt headers SELECT cashrcpt_id AS id, 0 AS type, cashrcpt_number, cust_number, cust_name, cashrcpt_distdate AS postdate, cashrcpt_posted AS posted, cashrcpt_void AS voided, ( CASE WHEN (cashrcpt_fundstype='C') THEN WHEN (cashrcpt_fundstype='T') THEN WHEN (cashrcpt_fundstype='M') THEN WHEN (cashrcpt_fundstype='V') THEN WHEN (cashrcpt_fundstype='A') THEN WHEN (cashrcpt_fundstype='D') THEN WHEN (cashrcpt_fundstype='R') THEN WHEN (cashrcpt_fundstype='K') THEN WHEN (cashrcpt_fundstype='W') THEN WHEN (cashrcpt_fundstype='O') THEN END || ' ' || cashrcpt_docnumber ) AS source, cashrcpt_id AS source_xtidrole, CASE WHEN (cashrcpt_void) THEN WHEN (NOT cashrcpt_posted) THEN ELSE '' END AS target, -1 AS target_xtidrole, cashrcpt_amount AS applied, (cashrcpt_amount / cashrcpt_curr_rate) AS base_applied, currConcat(cashrcpt_curr_id) AS currAbbr, cashrcpt_distdate AS sortdate, 0 AS base_applied_xttotalrole, -1 AS arapply_id FROM cashrcpt JOIN custinfo ON (cust_id=cashrcpt_cust_id) LEFT OUTER JOIN custgrpitem ON (custgrpitem_cust_id=cust_id) WHERE ( (cashrcpt_distdate BETWEEN AND ) AND (cust_id=) AND (cust_custtype_id=) AND (custgrpitem_custgrp_id=) AND (cust_custtype_id IN (SELECT custtype_id FROM custtype WHERE (custtype_code ~ ))) AND (cashrcpt_fundstype=) ) -- Cash Receipt items UNION SELECT cashrcpt_id AS id, 1 AS type, '', '', '', cashrcpt_distdate AS postdate, cashrcpt_posted AS posted, cashrcpt_void AS voided, '' AS source, cashrcpt_id AS source_xtidrole, ( CASE WHEN (aropen_doctype='D') THEN WHEN (aropen_doctype='I') THEN WHEN (aropen_doctype='C') THEN WHEN (aropen_doctype='R') THEN ELSE END || ' ' || TEXT(aropen_docnumber) ) AS target, aropen_id AS target_xtidrole, COALESCE(arapply_applied, cashrcptitem_amount) AS applied, (COALESCE(arapply_applied,cashrcptitem_amount) / cashrcpt_curr_rate) AS base_applied, currConcat(cashrcpt_curr_id) AS currAbbr, cashrcpt_distdate AS sortdate, -1 AS base_applied_xttotalrole, arapply_id FROM cashrcpt JOIN custinfo ON (cust_id=cashrcpt_cust_id) LEFT OUTER JOIN custgrpitem ON (custgrpitem_cust_id=cust_id) JOIN cashrcptitem ON (cashrcptitem_cashrcpt_id=cashrcpt_id) JOIN aropen ON (aropen_id=cashrcptitem_aropen_id) LEFT OUTER JOIN arapply ON ((arapply_target_aropen_id=aropen_id OR arapply_source_aropen_id=aropen_id) AND arapply_reftype='CRA' AND arapply_ref_id=cashrcptitem_id) WHERE ( (cashrcpt_distdate BETWEEN AND ) AND (cust_id=) AND (cust_custtype_id=) AND (custgrpitem_custgrp_id=) AND (cust_custtype_id IN (SELECT custtype_id FROM custtype WHERE (custtype_code ~ ))) AND (cashrcpt_fundstype=) ) -- Cash Receipt misc UNION SELECT cashrcpt_id AS id, 1 AS type, '', '', '', cashrcpt_distdate AS postdate, cashrcpt_posted AS posted, cashrcpt_void AS voided, '' AS source, cashrcpt_id AS source_xtidrole, formatGLAccount(cashrcptmisc_accnt_id) AS target, -1 AS target_xtidrole, COALESCE(arapply_applied, cashrcptmisc_amount) AS applied, (COALESCE(arapply_applied, cashrcptmisc_amount) / cashrcpt_curr_rate) AS base_applied, currConcat(cashrcpt_curr_id) AS currAbbr, cashrcpt_distdate AS sortdate, -1 AS base_applied_xttotalrole, arapply_id FROM cashrcpt JOIN custinfo ON (cust_id=cashrcpt_cust_id) LEFT OUTER JOIN custgrpitem ON (custgrpitem_cust_id=cust_id) JOIN cashrcptmisc ON (cashrcptmisc_cashrcpt_id=cashrcpt_id) LEFT OUTER JOIN arapply ON (arapply_reftype='CRD' AND arapply_ref_id=cashrcptmisc_id) WHERE ( (cashrcpt_distdate BETWEEN AND ) AND (cust_id=) AND (cust_custtype_id=) AND (custgrpitem_custgrp_id=) AND (cust_custtype_id IN (SELECT custtype_id FROM custtype WHERE (custtype_code ~ ))) AND (cashrcpt_fundstype=) ) ) AS data ORDER BY sortdate, id, type, target;