-- view_transactions_header
 SELECT a.id AS transaction_id,
    a.zonid,
    a.product_id,
    b.name AS product_name,
    a.category_id,
    a.customer_id,
    a.type,
    a.base_price,
    a.selling_price,
    a.upline_markup,
    a.status,
    a.createtime,
    a.updatetime
   FROM (transactions a
     JOIN products b ON (((a.product_id)::text = (b.id)::text)));

-- view_products
   SELECT a.id AS transaction_id,
    a.zonid,
    a.product_id,
    b.name AS product_name,
    a.category_id,
    c.type AS category_type,
    a.customer_id,
    a.type,
    a.base_price,
    a.selling_price,
    a.upline_markup,
    a.status,
    a.createtime,
    a.updatetime
   FROM ((transactions a
     JOIN products b ON (((a.product_id)::text = (b.id)::text)))
     JOIN product_categories c ON (((b.category_id)::text = (c.id)::text)));

-- view_merchants_profit
 SELECT a.upline_zonid AS zonid,
    a.createtime,
    b.product_id,
    a.amount,
    a.is_withdrawn,
    'MARKUP'::text AS type
   FROM (markup_shares a
     JOIN transactions b ON (((b.id)::text = (a.transaction_id)::text)))
  WHERE ((b.status)::text = ANY ((ARRAY['SUCCESS'::character varying, 'PENDING'::character varying])::text[]))
UNION ALL
 SELECT transactions.zonid,
    transactions.createtime,
    transactions.product_id,
    (transactions.selling_price - (transactions.base_price + transactions.upline_markup)) AS amount,
    true AS is_withdrawn,
    'TRANSACTION'::text AS type
   FROM transactions
  WHERE ((transactions.status)::text = ANY ((ARRAY['SUCCESS'::character varying, 'PENDING'::character varying])::text[]));