How orders, invoices, and payments actually move through this NetSuite account — reconstructed from 22,000+ document-link events and the record-level audit trail — measured against the standard order-to-cash and procure-to-pay designs.
This analysis reconstructed the account's end-to-end transaction flows directly from the document-link graph (nexttransactionlinelink, 22,762 line-level link events across 40 distinct transition types) and the record-level audit trail (systemnote), rather than from process documentation. Four conclusions stand out.
First, execution discipline in operations is high. 88.4% of fulfilled orders ship and invoice on the same day; the purchase-to-receipt cycle runs at a median of 0 days with a maximum of 3. The transactional machinery is fast and consistent.
Second, the constraint is not operations — it is receivables. The open invoice queue totals $964,127 (32.7% of all-time invoiced revenue) at an average age of 128 days. $472.9K is more than 90 days past due, including single invoices outstanding 460 and 327 days. By contrast, vendor bills are paid at a median of 3 days — the account systematically extends credit it does not collect while surrendering payables float it is entitled to.
Third, a structured anomaly ran May–July 2026. Eighteen orders followed a uniform inverted pattern — invoiced exactly ~15 days after order, shipped ~46 days after order — inverting the fulfill-then-bill sequence. The same window contains the account's only fulfillment backlog (average ship lag rose from a 0-day baseline to 6–9 days) and virtually all post-creation edit churn. The pattern's uniformity indicates a deliberate arrangement (bill-and-hold-like) or scripted activity, not organic delay; it warrants a policy determination because it carries revenue-recognition implications.
Fourth, the rework loops are small but currently unmanaged. Of four return authorizations opened since July, only one has completed the receive-and-credit cycle; one has held received goods without a credit memo for 37 days. The quotation stage is bypassed for ~99% of orders, and of the 10 quotes that do exist, half expired unactioned.
Classic process mining requires an event log: case ID, activity, timestamp. NetSuite does not expose one directly, so three sources were combined to synthesize it:
| Source | Role in reconstruction | Volume |
|---|---|---|
nexttransactionlinelink | The document-flow edge list: every line-level link between a predecessor and successor transaction, typed (OrdBill, ShipRcpt, Payment, SaleRet…) and dated on both ends. This is the account's de facto event log. | 22,762 links · 40 transition types |
transaction | Case attributes: type, status (single-letter codes), business dates, due dates, open amounts, entities. | ~7,900 documents |
systemnote | Record-level audit trail: post-creation field edits, actor, and originating context (UI form / Suitelet / RESTlet) — the signal for edit-churn rework invisible in document links. | 1,001 events on transactions |
Cases were built per order: each Sales Order (743 with downstream links) and Purchase Order (724) was traced through its first shipment/receipt, first invoice/bill, and payment, with lag computed at each hop. Conformance was tested by classifying every case into its observed variant (the actual sequence of steps) and comparing against the reference model below. All 14 queries used are reproduced verbatim in the Appendix.
The transition-frequency map below is drawn purely from link events — it shows where documents actually go, weighted by how many document pairs traverse each edge. Solid edges carry the volume; dashed nodes are stages the standard model expects but the data shows are nearly vacant.
Every linked sales order was classified into its observed variant. The standard sequence (fulfill, then invoice) holds for 95.7% of cases — but the shape of conformance is a same-day collapse, and the exceptions are not random.
The 20 invoice-before-shipment cases are dominated by an 18-order cluster (SO4270–SO4287, May–July 2026) with a strikingly uniform signature: invoice raised ~15 days after order date, goods shipped ~46 days after order date — a 29–31 day window in which revenue was billed against unshipped inventory. The affected customers repeat (Design Excellence Ltd. ×4, Meetz Industries, JBL Inc., Godric Motors ×2, Hugo Limited, Marshall Industries, Blockster Inc., Jones Manufacturing). The uniform offsets rule out organic delay; this is either a deliberate billing arrangement (bill-and-hold-like terms) or scripted/batch activity.
| Order | Ordered | Invoiced | Shipped | Bill precedes ship by | Customer |
|---|---|---|---|---|---|
| SO4270 | 2026-05-03 | 2026-05-18 | 2026-06-18 | 31 d | Design Excellence Ltd. |
| SO4271 | 2026-05-03 | 2026-05-18 | 2026-06-18 | 31 d | Godric Motors |
| SO4274 | 2026-05-08 | 2026-05-23 | 2026-06-23 | 31 d | Meetz Industries |
| SO4281 | 2026-07-02 | 2026-07-17 | 2026-08-17 | 31 d | Marshall Industries |
| SO4287 | 2026-07-13 | 2026-07-31 | 2026-08-31 | 31 d | Design Excellence Ltd. |
| …13 further cluster orders with identical ~+15 d / ~+46 d offsets (full list reproducible via Q7), plus outlier SO3910 — invoiced on order date, shipped 365 days later. | |||||
Eleven orders show a completed shipment with no downstream invoice link: $4,836.79 of delivered goods with no bill raised. Individually small, but this is precisely the defect class that recurs silently. Largest: SO3174 (Shirley Stephens, $2,270.64, shipped 2026-08-08); the rest span June–September 2026 (Q8 lists all 11).
P2P is the mirror image: near-perfect. 722 of 724 traced POs received before billing (only 2 billed before receipt), median receipt lag 0 days, maximum 3 days, bill raised ~1 day post-receipt. The 3-way-match sequence is effectively self-enforcing here.
| Stage transition | Cases | Median | Average | Max | Reading |
|---|---|---|---|---|---|
| Sales Order → Shipment | 742 | 0 d | 1.6 d | 365 d | Excellent baseline; average distorted by the May–Jul cluster |
| Sales Order → Invoice | 732 | 0 d | 0.6 d | — | Billing keeps pace with fulfillment |
| Invoice → Payment (paid pop.) | 736 | 0 d | 1.7 d | 272 d | Survivors pay fast — the problem population is the unpaid queue (§7) |
| Purchase Order → Receipt | 724 | 0 d | 0.0 d | 3 d | Effectively instantaneous |
| Receipt → Vendor Bill | 705 | 1 d | 1.0 d | — | Tight 3-way match |
| Vendor Bill → Payment | 1,004 | 3 d | 3.5 d | — | 991 of 1,004 paid within 5 days — faster than terms require (§7) |
Monthly average ship-lag was zero for eight consecutive months, then rose sharply in May 2026 and self-cleared by August. The backlog is entirely attributable to the 18-order inverted cluster plus a handful of adjacent delays — 18 orders exceeded 30 days in the window. No aggregate KPI on the standard dashboards would surface this: monthly shipment counts stayed normal (33–44/month) while individual orders quietly aged.
| Queue | Docs | Avg age | Max age | Value | Observation |
|---|---|---|---|---|---|
| Open invoices (AR) | 39 | 128 d | 536 d | $964,127 | The dominant bottleneck — see §7 |
| SOs pending fulfillment (B) | 40 | 18 d | 99 d | $73,709 | Watch: several already exceed the 30-day threshold |
| SOs pending approval (A) | 13 | 12 d | 21 d | $55,892 | Approval latency delays $56K of pipeline |
| Open vendor bills (A) | 9 | 26 d | 74 d | $180,934 | Includes Davidson Leasing $120,000 (19 d) and two 43–74 d intercompany allocations |
| Vendor bills pending approval (D) | 4 | 27 d | 82 d | $3,114 | Oldest: Bedline $1,500 stalled 82 days in approval |
| POs pending billing (F) | 19 | 30 d | 103 d | $29,707 | Receipt done, bill never arrived — accrual exposure |
| Open RMAs | 2 | 18 d | 20 d | $206 | Plus one received-not-credited (§6.1) |
Return volume is negligible (4 RMAs against 788 orders — 0.5%), and returns only began in July 2026. But the loop that exists does not close: the designed sequence RMA → receive goods → issue credit has completed exactly once.
| RMA | Opened | Customer | Goods received | Credit issued | State |
|---|---|---|---|---|---|
| RMA23 | 2026-07-05 | Design Excellence Ltd. | Yes | Yes | Complete — the only closed loop |
| RMA24 | 2026-07-16 | Design Excellence Ltd. | Yes | No | Received 37 days ago, customer still awaiting credit |
| RMA25 | 2026-08-02 | Design Excellence Ltd. | No | No | Awaiting goods, 20 d |
| RMA26 | 2026-08-06 | Design Excellence Ltd. | No | No | Awaiting goods, 16 d |
All four RMAs belong to one customer — Design Excellence Ltd. — who is also the most frequent name in the inverted-billing cluster (§4.1). This account concentrates a disproportionate share of every anomaly in the dataset and merits a dedicated account review.
Post-creation edits are the invisible rework of ERP processes. The audit trail shows a starkly bimodal distribution: 774 of 788 sales orders (98.2%) were never edited after creation, while 14 orders absorbed all the churn — 9 to 39 field-change events each, touching 12–16 distinct fields (amounts, revenue status, addresses, shipped/picked quantities). Those 14 churned orders sit in the same April–August window and the same SO42xx range as the inverted-billing cluster. Actor analysis (Q10) attributes the edits to a single administrator, executed predominantly through Suitelet and RESTlet contexts (SLT 71%, RST 12%) rather than the UI — i.e., tool-driven activity, consistent with the scripted-pattern hypothesis in §4.1.
10 estimates all-time vs. 788 orders. One converted (10%), five expired (50%), four remain open. Quoting appeared in July 2026, presumably as a new practice — at current conversion it adds an administrative step with no measurable funnel value. Either commit to it (route the wholesale channel through quotes, track conversion) or drop it.
Putting both cash cycles side by side exposes the account's most expensive habit. Vendor bills are settled at a median of 3 days — 991 of 1,004 within 5 days — surrendering essentially all payables float (on Net-30 terms, ~26 days of free financing per bill). Customers who pay do so quickly, but the unpaid queue is aging without intervention: 39 invoices, $964K, of which 49% is beyond 90 days past due.
| Invoice | Customer | Due date | Days past due | Open amount |
|---|---|---|---|---|
| INV790 | Global Information | 2025-12-03 | 262 | $110,579.17 |
| INV782 | Red Rivers Consulting | 2026-03-20 | 155 | $102,905.76 |
| INV774 | Magna Tech Limited | 2026-07-21 | 32 | $97,942.27 |
| INV783 | Falcon Systems | 2026-06-01 | 82 | $86,007.39 |
| INV791 | Mercury Co. | 2025-05-19 | 460 | $80,079.02 |
| INV789 | Gotter inc. | 2025-12-09 | 256 | $68,119.00 |
| INV759 | Blockster Inc. | 2026-06-03 | 80 | $53,424.00 |
| INV788 | Haskell Associates | 2025-09-29 | 327 | $43,940.75 |
| INV784 | John G. Roche Opticians | 2026-04-15 | 129 | $31,810.19 |
| INV780 | Informics International | 2026-03-23 | 152 | $29,239.18 |
| Top 10 concentration | $704,047 · 73% of open AR | |||
The paid population proves customers in this book can pay fast (median 0 days; only 44 of 736 paid invoices were ever late). The aged queue is therefore not a customer-behavior problem across the portfolio — it is a small set of large accounts with no active collections process. Ten phone calls address $704K.
49% of open AR sits past the 90-day mark, including invoices at 460, 327, 262 and 256 days. Open AR equals 32.7% of all-time invoiced revenue. The paid population's near-instant behavior demonstrates the gap is process absence, not customer inability.
18 orders billed ~15 days after order and shipped ~46 days after order — a uniform 29–31-day billed-but-unshipped window, repeated across 9 customers. Revenue-recognition treatment depends on whether documented bill-and-hold criteria exist; none are referenced on the transactions.
$4,836.79 of delivered goods with no bill raised, spanning June–September 2026. No standard report flags this population; it was only visible by set-differencing the link graph.
Median bill-to-payment of 3 days across 1,004 payments; 98.7% paid within 5 days. Unless early-payment discounts are being captured (none evident on the bills), this forgoes ~26 days of float on the entire payables book while $964K of receivables goes uncollected.
Average ship lag rose from a 0-day 8-month baseline to 6.0–8.9 days; 18 orders exceeded 30 days. Monthly shipment counts stayed normal throughout, so volume dashboards showed nothing. Cleared by August without intervention — meaning it could recur the same silent way.
Four vendor bills pending approval (oldest 82 days), 13 sales orders pending approval averaging 12 days, and a $120K vendor bill sitting open-unapproved-for-payment 19 days. Nineteen POs are received-but-unbilled up to 103 days (accrual exposure at close).
Of 4 RMAs, one completed the full receive-and-credit cycle; RMA24's goods were received 37 days ago with no credit memo issued. All four RMAs belong to one customer (Design Excellence Ltd.), who also recurs throughout the F2 cluster.
10 estimates vs. 788 orders; 10% conversion; 50% expiry. The stage exists in the reference model and in the account's form set, but not in practice.
98.2% of sales orders have zero post-creation edits; 14 orders absorb all churn (up to 39 field changes), executed predominantly via Suitelet/RESTlet contexts by a single administrator, coinciding with the F2 cluster window.
Several August–September shipments carry dates earlier than their order dates (negative lags), proving trandate is an editable business date, not an event timestamp. Cycle-time figures herein are therefore best-estimate, floor-biased where backdating occurred (see §10).
CustInvc status A bucketed by days-past-due, reviewed weekly. Target: nothing crosses 60 days without a documented action.nexttransactionlinelink exposes document dates (previousdate/nextdate), not system event times, at day granularity. Same-day sequences cannot be internally ordered, and backdated documents (F10) can produce negative lags. All cycle times are day-granular best estimates.foreign* columns; the account is single-currency USD).nexttransactionlink table contains only 22 rows in this account; all flow reconstruction uses the line-level nexttransactionlinelink table (22,762 rows), which is complete.All queries are SuiteQL, run read-only against the live account on August 22, 2026. Each is reproducible as-is. Conventions follow the account's house style (uppercase keywords, standard aliases, rounded money).
SELECT
previoustype,
linktype,
nexttype,
COUNT(DISTINCT previousdoc || '-' || nextdoc) AS doc_pairs,
COUNT(*) AS line_links
FROM nexttransactionlinelink
GROUP BY previoustype, linktype, nexttype
ORDER BY COUNT(*) DESCSELECT type, status, COUNT(*) AS n
FROM transaction
WHERE type IN ('SalesOrd','CustInvc','ItemShip','CustPymt','RtnAuth','CustCred',
'Estimate','PurchOrd','ItemRcpt','VendBill','VendPymt','CashSale','CashRfnd')
GROUP BY type, status
ORDER BY type, COUNT(*) DESCSELECT
COUNT(*) AS so_count,
ROUND(AVG(first_ship - so_date), 1) AS avg_so_to_ship,
ROUND(MEDIAN(first_ship - so_date), 1) AS med_so_to_ship,
MAX(first_ship - so_date) AS max_so_to_ship,
ROUND(AVG(first_inv - so_date), 1) AS avg_so_to_inv,
SUM(CASE WHEN first_inv < first_ship THEN 1 ELSE 0 END) AS invoiced_before_ship,
SUM(CASE WHEN first_inv = first_ship THEN 1 ELSE 0 END) AS invoiced_same_day
FROM (
SELECT l.previousdoc AS so_id,
MIN(l.previousdate) AS so_date,
MIN(CASE WHEN l.nexttype = 'ItemShip' THEN l.nextdate END) AS first_ship,
MIN(CASE WHEN l.nexttype = 'CustInvc' THEN l.nextdate END) AS first_inv
FROM nexttransactionlinelink l
WHERE l.previoustype = 'SalesOrd'
GROUP BY l.previousdoc
)SELECT
TO_CHAR(so_date, 'YYYY-MM') AS month,
COUNT(*) AS shipped_orders,
ROUND(AVG(lag), 1) AS avg_lag,
MAX(lag) AS max_lag,
SUM(CASE WHEN lag > 30 THEN 1 ELSE 0 END) AS over_30d
FROM (
SELECT MIN(l.previousdate) AS so_date,
MIN(CASE WHEN l.nexttype = 'ItemShip' THEN l.nextdate END)
- MIN(l.previousdate) AS lag
FROM nexttransactionlinelink l
WHERE l.previoustype = 'SalesOrd'
GROUP BY l.previousdoc
)
WHERE lag IS NOT NULL AND so_date >= TO_DATE('2025-09-01', 'YYYY-MM-DD')
GROUP BY TO_CHAR(so_date, 'YYYY-MM')
ORDER BY TO_CHAR(so_date, 'YYYY-MM')-- PO -> receipt / bill
SELECT COUNT(*) AS po_count,
ROUND(AVG(first_rcpt - po_date), 1) AS avg_po_to_rcpt,
MAX(first_rcpt - po_date) AS max_po_to_rcpt,
ROUND(AVG(first_bill - po_date), 1) AS avg_po_to_bill,
SUM(CASE WHEN first_bill < first_rcpt THEN 1 ELSE 0 END) AS billed_before_receipt
FROM (
SELECT l.previousdoc AS po_id,
MIN(l.previousdate) AS po_date,
MIN(CASE WHEN l.nexttype = 'ItemRcpt' THEN l.nextdate END) AS first_rcpt,
MIN(CASE WHEN l.nexttype = 'VendBill' THEN l.nextdate END) AS first_bill
FROM nexttransactionlinelink l
WHERE l.previoustype = 'PurchOrd'
GROUP BY l.previousdoc
);
-- Bill -> payment (DPO proxy)
SELECT COUNT(*) AS bill_count,
ROUND(MEDIAN(pay_date - bill_date), 1) AS med_days_to_pay,
SUM(CASE WHEN pay_date - bill_date <= 5 THEN 1 ELSE 0 END) AS paid_within_5d
FROM (
SELECT l.previousdoc AS bill_id,
MIN(l.previousdate) AS bill_date,
MIN(l.nextdate) AS pay_date
FROM nexttransactionlinelink l
WHERE l.previoustype = 'VendBill' AND l.linktype = 'Payment'
GROUP BY l.previousdoc
)SELECT variant, COUNT(*) AS orders
FROM (
SELECT l.previousdoc,
CASE
WHEN MIN(CASE WHEN l.nexttype='ItemShip' THEN l.nextdate END) IS NULL
THEN 'No shipment yet'
WHEN MIN(CASE WHEN l.nexttype='CustInvc' THEN l.nextdate END) IS NULL
THEN 'Shipped, never invoiced'
WHEN MIN(CASE WHEN l.nexttype='CustInvc' THEN l.nextdate END)
< MIN(CASE WHEN l.nexttype='ItemShip' THEN l.nextdate END)
THEN 'Invoiced BEFORE shipped'
WHEN MIN(CASE WHEN l.nexttype='CustInvc' THEN l.nextdate END)
= MIN(CASE WHEN l.nexttype='ItemShip' THEN l.nextdate END)
THEN 'Shipped + invoiced same day'
ELSE 'Shipped then invoiced later'
END AS variant
FROM nexttransactionlinelink l
WHERE l.previoustype = 'SalesOrd'
GROUP BY l.previousdoc
)
GROUP BY variant
ORDER BY COUNT(*) DESCSELECT so.tranid AS so_num,
TO_CHAR(so.trandate, 'YYYY-MM-DD') AS so_date,
TO_CHAR(x.first_inv, 'YYYY-MM-DD') AS invoiced,
TO_CHAR(x.first_ship, 'YYYY-MM-DD') AS shipped,
x.first_ship - x.first_inv AS inv_leads_ship_by
FROM (
SELECT l.previousdoc AS so_id,
MIN(CASE WHEN l.nexttype='ItemShip' THEN l.nextdate END) AS first_ship,
MIN(CASE WHEN l.nexttype='CustInvc' THEN l.nextdate END) AS first_inv
FROM nexttransactionlinelink l
WHERE l.previoustype = 'SalesOrd'
GROUP BY l.previousdoc
) x
JOIN transaction so ON so.id = x.so_id
WHERE x.first_inv < x.first_ship
ORDER BY x.first_ship - x.first_inv DESCSELECT so.tranid,
TO_CHAR(so.trandate, 'YYYY-MM-DD') AS so_date,
so.status,
c.entityid AS customer,
ROUND(ABS(so.foreigntotal), 2) AS amt,
TO_CHAR(x.first_ship, 'YYYY-MM-DD') AS shipped
FROM (
SELECT l.previousdoc AS so_id,
MIN(CASE WHEN l.nexttype='ItemShip' THEN l.nextdate END) AS first_ship,
MIN(CASE WHEN l.nexttype='CustInvc' THEN l.nextdate END) AS first_inv
FROM nexttransactionlinelink l
WHERE l.previoustype = 'SalesOrd'
GROUP BY l.previousdoc
) x
JOIN transaction so ON so.id = x.so_id
LEFT JOIN customer c ON c.id = so.entity
WHERE x.first_ship IS NOT NULL AND x.first_inv IS NULL
ORDER BY ABS(so.foreigntotal) DESCSELECT t.type, t.status, COUNT(*) AS n,
ROUND(AVG(TRUNC(SYSDATE) - t.trandate), 0) AS avg_age_days,
MAX(TRUNC(SYSDATE) - t.trandate) AS max_age_days,
ROUND(SUM(t.foreigntotal), 2) AS total_value
FROM transaction t
WHERE (t.type = 'SalesOrd' AND t.status IN ('A','B','E','F'))
OR (t.type = 'CustInvc' AND t.status = 'A')
OR (t.type = 'PurchOrd' AND t.status IN ('A','B','F'))
OR (t.type = 'VendBill' AND t.status IN ('A','D'))
OR (t.type = 'Estimate' AND t.status = 'A')
OR (t.type = 'RtnAuth' AND t.status IN ('A','B'))
GROUP BY t.type, t.status
ORDER BY t.type, t.status-- Distribution of post-creation edits per sales order
SELECT churn_bucket, COUNT(*) AS orders
FROM (
SELECT t.id,
CASE WHEN COUNT(sn.recordid) = 0 THEN '0 edits'
WHEN COUNT(sn.recordid) <= 3 THEN '1-3'
WHEN COUNT(sn.recordid) <= 8 THEN '4-8'
WHEN COUNT(sn.recordid) <= 15 THEN '9-15'
ELSE '16+' END AS churn_bucket
FROM transaction t
LEFT JOIN systemnote sn
ON sn.recordid = t.id AND sn.recordtypeid = -30 AND sn.type = 2
WHERE t.type = 'SalesOrd'
GROUP BY t.id
)
GROUP BY churn_bucket;
-- Actor and originating context (UI form vs Suitelet vs RESTlet)
SELECT e.entityid AS actor, sn.context, COUNT(*) AS edits
FROM systemnote sn
JOIN transaction t ON t.id = sn.recordid
LEFT JOIN employee e ON e.id = sn.name
WHERE sn.recordtypeid = -30 AND sn.type = 2 AND t.type = 'SalesOrd'
GROUP BY e.entityid, sn.context
ORDER BY COUNT(*) DESCSELECT CASE
WHEN TRUNC(SYSDATE) - inv.duedate <= 0 THEN 'Current'
WHEN TRUNC(SYSDATE) - inv.duedate <= 30 THEN '1-30'
WHEN TRUNC(SYSDATE) - inv.duedate <= 90 THEN '31-90'
WHEN TRUNC(SYSDATE) - inv.duedate <= 180 THEN '91-180'
ELSE '180+' END AS aging_bucket,
COUNT(*) AS invoices,
ROUND(SUM(inv.foreignamountunpaid), 2) AS open_amt
FROM transaction inv
WHERE inv.type = 'CustInvc' AND inv.status = 'A'
GROUP BY CASE
WHEN TRUNC(SYSDATE) - inv.duedate <= 0 THEN 'Current'
WHEN TRUNC(SYSDATE) - inv.duedate <= 30 THEN '1-30'
WHEN TRUNC(SYSDATE) - inv.duedate <= 90 THEN '31-90'
WHEN TRUNC(SYSDATE) - inv.duedate <= 180 THEN '91-180'
ELSE '180+' ENDSELECT inv.tranid,
TO_CHAR(inv.trandate, 'YYYY-MM-DD') AS inv_date,
TO_CHAR(inv.duedate, 'YYYY-MM-DD') AS due,
TRUNC(SYSDATE) - inv.duedate AS days_overdue,
c.entityid AS customer,
ROUND(inv.foreignamountunpaid, 2) AS open_amt
FROM transaction inv
LEFT JOIN customer c ON c.id = inv.entity
WHERE inv.type = 'CustInvc' AND inv.status = 'A'
ORDER BY inv.foreignamountunpaid DESC
FETCH FIRST 12 ROWS ONLYSELECT vb.tranid,
TO_CHAR(vb.trandate, 'YYYY-MM-DD') AS bill_date,
vb.status,
TRUNC(SYSDATE) - vb.trandate AS age_days,
v.entityid AS vendor,
ROUND(ABS(vb.foreigntotal), 2) AS amt
FROM transaction vb
LEFT JOIN vendor v ON v.id = vb.entity
WHERE vb.type = 'VendBill' AND vb.status IN ('A','D')
ORDER BY vb.status DESC, ABS(vb.foreigntotal) DESC-- Every estimate and whether it ever converted
SELECT est.tranid,
TO_CHAR(est.trandate, 'YYYY-MM-DD') AS est_date,
est.status,
ROUND(est.foreigntotal, 2) AS total,
MAX(CASE WHEN l.nexttype = 'SalesOrd' THEN 1 ELSE 0 END) AS converted
FROM transaction est
LEFT JOIN nexttransactionlinelink l
ON l.previousdoc = est.id AND l.previoustype = 'Estimate'
WHERE est.type = 'Estimate'
GROUP BY est.tranid, est.trandate, est.status, est.foreigntotal
ORDER BY est.trandate;
-- Each RMA traced through receipt and credit
SELECT ra.tranid,
TO_CHAR(ra.trandate, 'YYYY-MM-DD') AS ra_date,
ra.status,
MAX(CASE WHEN l.nexttype = 'ItemRcpt' THEN 1 ELSE 0 END) AS received,
MAX(CASE WHEN l.nexttype = 'CustCred' THEN 1 ELSE 0 END) AS credited,
c.entityid AS customer
FROM transaction ra
LEFT JOIN nexttransactionlinelink l
ON l.previousdoc = ra.id AND l.previoustype = 'RtnAuth'
LEFT JOIN customer c ON c.id = ra.entity
WHERE ra.type = 'RtnAuth'
GROUP BY ra.tranid, ra.trandate, ra.status, c.entityid
ORDER BY ra.trandate