drop view if exists view_history_penjualan;

--gopg:split

CREATE VIEW view_history_penjualan AS
SELECT
  'Penjualan' :: text AS jenis_transaksi,
  orders.toko_id,
  orders.no_invoice,
  orders.total_harga,
  orders.dp,
  orders.total_bayar,
  orders.potongan,
  NULL :: text AS status,
  orders.type_pembayaran,
  orders.tanggal_jual AS created_at,
  orders.tunai_nominal,
  orders.edc_nominal,
  orders.transfer_nominal,
  orders.deleted_by,
  orders.deleted_at
FROM
  orders
WHERE
  orders.deleted_at IS NULL
  AND orders.deleted_by IS NULL
UNION
SELECT
  'Penjualan Jasa' :: text AS jenis_transaksi,
  transaksi_jasas.toko_id,
  transaksi_jasas.no_invoice,
  transaksi_jasas.total_harga,
  transaksi_jasas.dp,
  0 AS total_bayar,
  transaksi_jasas.potongan,
  transaksi_jasas.status_pesanan AS status,
  transaksi_jasas.type_pembayaran,
  transaksi_jasas.tgl_pesan AS created_at,
  transaksi_jasas.tunai_nominal,
  transaksi_jasas.edc_nominal,
  transaksi_jasas.transfer_nominal,
  transaksi_jasas.deleted_by,
  transaksi_jasas.deleted_at
FROM
  transaksi_jasas
WHERE
  transaksi_jasas.deleted_at IS NULL
  AND transaksi_jasas.deleted_by IS NULL
UNION
SELECT
  'Penjualan Kustom' :: text AS jenis_transaksi,
  transaksi_pesanan_customs.toko_id,
  transaksi_pesanan_customs.no_invoice,
  transaksi_pesanan_customs.harga_produk AS total_harga,
  transaksi_pesanan_customs.dp,
  transaksi_pesanan_customs.total_bayar,
  transaksi_pesanan_customs.potongan,
  transaksi_pesanan_customs.status,
  transaksi_pesanan_customs.type_pembayaran,
  transaksi_pesanan_customs.tgl_pesan AS created_at,
  transaksi_pesanan_customs.tunai_nominal,
  transaksi_pesanan_customs.edc_nominal,
  transaksi_pesanan_customs.transfer_nominal,
  transaksi_pesanan_customs.deleted_by,
  transaksi_pesanan_customs.deleted_at
FROM
  transaksi_pesanan_customs
WHERE
  transaksi_pesanan_customs.deleted_at IS NULL
  AND transaksi_pesanan_customs.deleted_by IS NULL;