drop view if exists kinerja_penjualan_view;

--gopg:split
CREATE VIEW kinerja_penjualan_view AS
SELECT
    tpc.anggota_id,
    tpc.toko_id,
    jb.nama as nama_jenis,
    jb.kode as kode_jenis,
    tpc.berat,
    tpc.karat,
    tpc.tgl_pesan AS created_at
FROM
    transaksi_pesanan_customs as tpc
    LEFT JOIN jenis_barangs as jb ON jb.id = tpc.jenis_barang_id
WHERE
    tpc.deleted_at IS NULL
    AND tpc.deleted_by IS NULL
UNION
ALL
SELECT
    o.anggota_id,
    o.toko_id,
    jb.nama as nama_jenis,
    jb.kode as kode_jenis,
    ib.berat,
    ib.karat,
    o.tanggal_jual AS created_at
FROM
    orders as o
    LEFT JOIN order_details as od ON od.order_id = o.id
    LEFT JOIN inventory_barangs ib ON ib.id = od.inventory_barang_id
    LEFT JOIN jenis_barangs as jb ON jb.id = ib.jenis_barang_id
WHERE
    o.deleted_at IS NULL
    AND o.deleted_by IS NULL