CREATE TABLE peran
(
    id         INTEGER PRIMARY KEY AUTO_INCREMENT,
    nama       VARCHAR(255) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

INSERT INTO peran (id, nama, created_at, updated_at)
VALUES (1, 'admin', NOW(), NOW()),
       (2, 'pegawai_toko', NOW(), NOW());

CREATE TABLE pengguna
(
    id         INT PRIMARY KEY AUTO_INCREMENT,
    id_peran   INT NOT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    CONSTRAINT fk_pengguna_peran FOREIGN KEY (id_peran) REFERENCES peran (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

-- TO CREATE ADMIN ACCOUNT, READ README.MD

CREATE TABLE toko
(
    id           INT PRIMARY KEY AUTO_INCREMENT,
    nama         VARCHAR(255) NOT NULL,
    alamat       VARCHAR(255) NULL,
    no_telepon   VARCHAR(20) NULL,
    status_aktif BOOLEAN      NOT NULL,
    created_at   TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at   TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at   TIMESTAMP NULL
);

CREATE TABLE konfigurasi_stok_menipis
(
    id              INT PRIMARY KEY AUTO_INCREMENT,
    gagang          INT NOT NULL,
    lensa           INT NOT NULL,
    softlense_biasa INT NOT NULL,
    produk_lainnya  INT NOT NULL,
    created_at      TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at      TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at      TIMESTAMP NULL
);

INSERT INTO konfigurasi_stok_menipis (gagang, lensa, softlense_biasa, produk_lainnya, created_at, updated_at,
                                      deleted_at)
VALUES (0, 0, 0, 0, NOW(), NOW(), NULL);

CREATE TABLE merek_gagang
(
    id         INT PRIMARY KEY AUTO_INCREMENT,
    id_toko    INT          NOT NULL,
    nama       VARCHAR(255) NOT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP NULL,
    CONSTRAINT fk_merek_gagang_toko FOREIGN KEY (id_toko) REFERENCES toko (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE gagang
(
    id              INT PRIMARY KEY AUTO_INCREMENT,
    id_merek_gagang INT          NOT NULL,
    serial          VARCHAR(255) NOT NULL,
    quantity        INT          NOT NULL,
    kode_harga      VARCHAR(255) NULL,
    created_at      TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at      TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at      TIMESTAMP NULL,
    constraint fk_gagang_merek_gagang FOREIGN KEY (id_merek_gagang) REFERENCES merek_gagang (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE tipe_jenis_lensa
(
    id         INT PRIMARY KEY AUTO_INCREMENT,
    nama       VARCHAR(255) NOT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

INSERT INTO tipe_jenis_lensa (id, nama, created_at, updated_at)
VALUES (1, "SPH", NOW(), NOW()),
       (2, "SPH and ADD", NOW(), NOW());

CREATE TABLE jenis_lensa
(
    id                  INT PRIMARY KEY AUTO_INCREMENT,
    id_toko             INT          NOT NULL,
    id_tipe_jenis_lensa INT          NOT NULL,
    nama                VARCHAR(255) NOT NULL,
    created_at          TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at          TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at          TIMESTAMP NULL,
    CONSTRAINT fk_jenis_lensa_toko FOREIGN KEY (id_toko) REFERENCES toko (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_jenis_lensa_tipe_jenis_lensa FOREIGN KEY (id_tipe_jenis_lensa) REFERENCES tipe_jenis_lensa (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE lensa
(
    id             INT PRIMARY KEY AUTO_INCREMENT,
    id_jenis_lensa INT NOT NULL,
    sph            VARCHAR(255) NULL,
    `add`          VARCHAR(255) NULL,
    quantity       INT NOT NULL,
    created_at     TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at     TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at     TIMESTAMP NULL,
    CONSTRAINT fk_lensa_jenis_lensa FOREIGN KEY (id_jenis_lensa) REFERENCES jenis_lensa (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE merging_stok_lensa_toko
(
    id             INT PRIMARY KEY AUTO_INCREMENT,
    id_toko_asal   INT NOT NULL,
    id_toko_tujuan INT NOT NULL,
    created_at     TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at     TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at     TIMESTAMP NULL,
    CONSTRAINT fk_merging_stok_lensa_toko_toko_asal FOREIGN KEY (id_toko_asal) REFERENCES toko (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_merging_stok_lensa_toko_toko_tujuan FOREIGN KEY (id_toko_tujuan) REFERENCES toko (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE merek_softlense_biasa
(
    id         INT PRIMARY KEY AUTO_INCREMENT,
    id_toko    INT          NOT NULL,
    nama       VARCHAR(255) NOT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP NULL,
    CONSTRAINT fk_merek_softlense_biasa_toko FOREIGN KEY (id_toko) REFERENCES toko (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE softlense_biasa
(
    id                       INT PRIMARY KEY AUTO_INCREMENT,
    id_merek_softlense_biasa INT          NOT NULL,
    warna                    VARCHAR(255) NOT NULL,
    diameter                 VARCHAR(255) NOT NULL,
    harga                    INT          NOT NULL,
    quantity                 INT          NOT NULL,
    created_at               TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at               TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at               TIMESTAMP NULL,
    CONSTRAINT fk_softlense_biasa_merek_softlense_biasa FOREIGN KEY (id_merek_softlense_biasa) REFERENCES merek_softlense_biasa (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE produk_lainnya
(
    id         INT PRIMARY KEY AUTO_INCREMENT,
    id_toko    INT          NOT NULL,
    nama       VARCHAR(255) NOT NULL,
    harga      INT          NOT NULL,
    quantity   INT          NOT NULL,
    is_hadiah  BOOLEAN      NOT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP NULL,
    CONSTRAINT fk_produk_lainnya_toko FOREIGN KEY (id_toko) REFERENCES toko (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE jenis_transaksi
(
    id         INT PRIMARY KEY AUTO_INCREMENT,
    nama       VARCHAR(255) NOT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

INSERT INTO jenis_transaksi (id, nama, created_at, updated_at)
VALUES (1, 'Gagang - Lensa', NOW(), NOW()),
       (2, 'Softlense Minus', NOW(), NOW()),
       (3, 'Softlense Biasa', NOW(), NOW()),
       (4, 'Produk Lainnya', NOW(), NOW());

CREATE TABLE transaksi_header
(
    id                 INT PRIMARY KEY AUTO_INCREMENT,
    id_jenis_transaksi INT          NOT NULL,
    id_toko            INT          NOT NULL,
    nama_toko          VARCHAR(255) NOT NULL,
    alamat_toko        VARCHAR(255) NOT NULL,
    no_telepon_toko    VARCHAR(255) NOT NULL,
    no_bon             VARCHAR(255) NULL,
    tanggal_pemesanan  DATE         NOT NULL,
    harga_total        INT          NOT NULL,
    status_pembayaran  BOOLEAN      NOT NULL,
    notes              VARCHAR(1000) NULL,
    alasan_penghapusan VARCHAR(255) NULL,
    created_at         TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at         TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at         TIMESTAMP NULL,
    updated_by         INT          NOT NULL,
    CONSTRAINT fk_transaksi_header_jenis_transaksi FOREIGN KEY (id_jenis_transaksi) REFERENCES jenis_transaksi (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_transaksi_header_toko FOREIGN KEY (id_toko) REFERENCES toko (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE jenis_transaksi_lensa
(
    id         INT PRIMARY KEY AUTO_INCREMENT,
    nama       VARCHAR(255) NOT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

INSERT INTO jenis_transaksi_lensa (id, nama, created_at, updated_at)
VALUES (1, 'Lensa terdaftar di Inventori', NOW(), NOW()),
       (2, 'Preorder Lensa yang terdaftar di Inventori', NOW(), NOW()),
       (3, 'Preorder Lensa yang tidak terdaftar di Inventori', NOW(), NOW()),
       (4, 'Lensa bawa sendiri', NOW(), NOW());

CREATE TABLE transaksi_gagang_lensa
(
    id                             INT PRIMARY KEY AUTO_INCREMENT,
    id_transaksi_header            INT          NOT NULL,
    nama_customer                  VARCHAR(255) NOT NULL,
    alamat_customer                VARCHAR(255) NULL,
    no_telepon_customer            VARCHAR(20) NULL,
    tanggal_selesai_pesanan        DATE NULL,
    tanggal_pengambilan_pesanan    DATE NULL,

    is_gagang_sendiri              BOOLEAN      NOT NULL,
    id_gagang                      INT NULL,
    serial_gagang                  VARCHAR(255) NULL,
    kode_harga_gagang              VARCHAR(255) NULL,
    nama_merek_gagang              VARCHAR(255) NULL,
    jarak_mata                     VARCHAR(255) NULL,

    id_jenis_transaksi_lensa_kiri  INT          NOT NULL,
    id_lensa_kiri                  INT NULL,
    sph_lensa_kiri                 VARCHAR(255) NULL,
    add_lensa_kiri                 VARCHAR(255) NULL,
    nama_jenis_lensa_kiri          VARCHAR(255) NULL,
    sph_lensa_kiri_po              VARCHAR(255) NULL,
    cyl_lensa_kiri_po              VARCHAR(255) NULL,
    axis_lensa_kiri_po             VARCHAR(255) NULL,
    add_lensa_kiri_po              VARCHAR(255) NULL,

    id_jenis_transaksi_lensa_kanan INT          NOT NULL,
    id_lensa_kanan                 INT NULL,
    sph_lensa_kanan                VARCHAR(255) NULL,
    add_lensa_kanan                VARCHAR(255) NULL,
    nama_jenis_lensa_kanan         VARCHAR(255) NULL,
    sph_lensa_kanan_po             VARCHAR(255) NULL,
    cyl_lensa_kanan_po             VARCHAR(255) NULL,
    axis_lensa_kanan_po            VARCHAR(255) NULL,
    add_lensa_kanan_po             VARCHAR(255) NULL,

    nama_jenis_lensa_po            VARCHAR(255) NULL,
    waktu_tiba_lensa_po            DATETIME NULL,
    warna_lensa                    VARCHAR(255) NULL,

    uang_muka                      INT NULL,
    created_at                     TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at                     TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at                     TIMESTAMP NULL,
    updated_by                     INT          NOT NULL,
    CONSTRAINT fk_transaksi_gagang_lensa_transaksi_header FOREIGN KEY (id_transaksi_header) REFERENCES transaksi_header (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_transaksi_gagang_lensa_gagang FOREIGN KEY (id_gagang) REFERENCES gagang (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_transaksi_gagang_lensa_jenis_transaksi_lensa_kiri FOREIGN KEY (id_jenis_transaksi_lensa_kiri) REFERENCES jenis_transaksi_lensa (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_transaksi_gagang_lensa_lensa_kiri FOREIGN KEY (id_lensa_kiri) REFERENCES lensa (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_transaksi_gagang_lensa_jenis_transaksi_lensa_kanan FOREIGN KEY (id_jenis_transaksi_lensa_kanan) REFERENCES jenis_transaksi_lensa (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_transaksi_gagang_lensa_lensa_kanan FOREIGN KEY (id_lensa_kanan) REFERENCES lensa (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE preorder_softlense_minus
(
    id                          INT PRIMARY KEY AUTO_INCREMENT,
    id_transaksi_header         INT          NOT NULL,
    nama_customer               VARCHAR(255) NOT NULL,
    alamat_customer             VARCHAR(255) NULL,
    no_telepon_customer         VARCHAR(20) NULL,
    tanggal_selesai_pesanan     DATE NULL,
    tanggal_pengambilan_pesanan DATE NULL,
    waktu_tiba_po               DATETIME NULL,

    merek                       VARCHAR(255) NOT NULL,
    jenis_lensa                 VARCHAR(255) NOT NULL,
    warna                       VARCHAR(255) NOT NULL,

    sph                         VARCHAR(255) NULL,
    cyl                         VARCHAR(255) NULL,
    axis                        VARCHAR(255) NULL,
    `add`                       VARCHAR(255) NULL,

    uang_muka                   INT NULL,
    created_at                  TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at                  TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at                  TIMESTAMP NULL,
    updated_by                  INT          NOT NULL,
    CONSTRAINT fk_transaksi_preorder_softlense_minus_transaksi_header FOREIGN KEY (id_transaksi_header) REFERENCES transaksi_header (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE transaksi_softlense_biasa
(
    id                  INT PRIMARY KEY AUTO_INCREMENT,
    id_transaksi_header INT NOT NULL,
    id_softlense_biasa  INT NOT NULL,
    quantity            INT NOT NULL,
    created_at          TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at          TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at          TIMESTAMP NULL,
    updated_by          INT NOT NULL,
    CONSTRAINT fk_transaksi_softlense_biasa_transaksi_header FOREIGN KEY (id_transaksi_header) REFERENCES transaksi_header (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_transaksi_softlense_biasa_softlense_biasa FOREIGN KEY (id_softlense_biasa) REFERENCES softlense_biasa (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE transaksi_produk_lainnya
(
    id                  INT PRIMARY KEY AUTO_INCREMENT,
    id_transaksi_header INT     NOT NULL,
    id_produk_lainnya   INT     NOT NULL,
    is_hadiah           BOOLEAN NOT NULL,
    quantity            INT     NOT NULL,
    created_at          TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at          TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at          TIMESTAMP NULL,
    updated_by          INT     NOT NULL,
    CONSTRAINT fk_transaksi_produk_lainnya_transaksi_header FOREIGN KEY (id_transaksi_header) REFERENCES transaksi_header (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_transaksi_produk_lainnya_produk_lainnya FOREIGN KEY (id_produk_lainnya) REFERENCES produk_lainnya (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE jenis_pembayaran
(
    id         INT PRIMARY KEY AUTO_INCREMENT,
    nama       VARCHAR(255) NOT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

INSERT INTO jenis_pembayaran (id, nama, created_at, updated_at)
VALUES (1, 'DP', NOW(), NOW()),
       (2, 'Pelunasan', NOW(), NOW()),
       (3, 'Full Payment', NOW(), NOW());

CREATE TABLE metode_pembayaran
(
    id         INT PRIMARY KEY AUTO_INCREMENT,
    nama       VARCHAR(255) NOT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

INSERT INTO metode_pembayaran (id, nama, created_at, updated_at)
VALUES (1, 'Tunai', NOW(), NOW()),
       (2, 'Transfer Bank', NOW(), NOW());

CREATE TABLE pembayaran
(
    id                   INT PRIMARY KEY AUTO_INCREMENT,
    id_transaksi_header  INT NOT NULL,
    id_jenis_pembayaran  INT NOT NULL,
    id_metode_pembayaran INT NOT NULL,
    total_pembayaran     INT NOT NULL,
    created_at           TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at           TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at           TIMESTAMP NULL,
    updated_by           INT NOT NULL,
    CONSTRAINT fk_pembayaran_transaksi_header FOREIGN KEY (id_transaksi_header) REFERENCES transaksi_header (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_pembayaran_jenis_pembayaran FOREIGN KEY (id_jenis_pembayaran) REFERENCES jenis_pembayaran (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_pembayaran_metode_pembayaran FOREIGN KEY (id_metode_pembayaran) REFERENCES metode_pembayaran (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE pengajuan_transfer_stok
(
    id                    INT PRIMARY KEY AUTO_INCREMENT,
    id_toko_asal          INT     NOT NULL,
    id_toko_tujuan        INT     NOT NULL,
    is_selesai            BOOLEAN NOT NULL,
    tanggal_transfer_stok DATE    NOT NULL,
    notes_asal            VARCHAR(1000) NULL,
    notes_tujuan          VARCHAR(1000) NULL,
    created_at            TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at            TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at            TIMESTAMP NULL,
    updated_by            INT     NOT NULL,
    CONSTRAINT fk_pengajuan_transfer_stok_toko_asal FOREIGN KEY (id_toko_asal) REFERENCES toko (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_pengajuan_transfer_stok_toko_tujuan FOREIGN KEY (id_toko_tujuan) REFERENCES toko (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE kategori
(
    id         INT PRIMARY KEY AUTO_INCREMENT,
    nama       VARCHAR(255) NOT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

INSERT INTO kategori (id, nama, created_at, updated_at)
VALUES (1, 'Gagang', NOW(), NOW()),
       (2, 'Lensa', NOW(), NOW()),
       (3, 'Softlense Biasa', NOW(), NOW()),
       (4, 'Produk Lainnya', NOW(), NOW());

CREATE TABLE pengajuan_transfer_stok_gagang_detail
(
    id                          INT PRIMARY KEY AUTO_INCREMENT,
    id_pengajuan_transfer_stok  INT NOT NULL,
    id_kategori                 INT NOT NULL,
    id_gagang_asal              INT NOT NULL,
    nama_merek_gagang           VARCHAR(255) NULL,
    serial                      VARCHAR(255) NULL,
    quantity_pengajuan_transfer INT NOT NULL,
    quantity_ditransfer         INT NOT NULL,
    id_gagang_tujuan            INT NULL,
    is_not_registered_on_tujuan BOOLEAN NULL,
    created_at                  TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at                  TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at                  TIMESTAMP NULL,
    updated_by                  INT NOT NULL,
    CONSTRAINT fk_pengajuan_transfer_stok_gagang_detail_header FOREIGN KEY (id_pengajuan_transfer_stok) REFERENCES pengajuan_transfer_stok (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_pengajuan_transfer_stok_gagang_detail_kategori FOREIGN KEY (id_kategori) REFERENCES kategori (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_pengajuan_transfer_stok_gagang_detail_gagang_asal FOREIGN KEY (id_gagang_asal) REFERENCES gagang (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_pengajuan_transfer_stok_gagang_detail_gagang_tujuan FOREIGN KEY (id_gagang_tujuan) REFERENCES gagang (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE pengajuan_transfer_stok_lensa_detail
(
    id                          INT PRIMARY KEY AUTO_INCREMENT,
    id_pengajuan_transfer_stok  INT NOT NULL,
    id_kategori                 INT NOT NULL,
    id_lensa_asal               INT NOT NULL,
    id_tipe_jenis_lensa         INT NULL,
    nama_tipe_jenis_lensa       VARCHAR(255) NULL,
    nama_jenis_lensa            VARCHAR(255) NULL,
    sph                         VARCHAR(255) NULL,
    `add`                       VARCHAR(255) NULL,
    quantity_pengajuan_transfer INT NOT NULL,
    quantity_ditransfer         INT NOT NULL,
    id_lensa_tujuan             INT NULL,
    is_not_registered_on_tujuan BOOLEAN NULL,
    created_at                  TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at                  TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at                  TIMESTAMP NULL,
    updated_by                  INT NOT NULL,
    CONSTRAINT fk_pengajuan_transfer_stok_lensa_header FOREIGN KEY (id_pengajuan_transfer_stok) REFERENCES pengajuan_transfer_stok (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_pengajuan_transfer_stok_lensa_detail_kategori FOREIGN KEY (id_kategori) REFERENCES kategori (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_pengajuan_transfer_stok_lensa_asal FOREIGN KEY (id_lensa_asal) REFERENCES lensa (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_pengajuan_transfer_stok_lensa_tujuan FOREIGN KEY (id_lensa_tujuan) REFERENCES lensa (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE pengajuan_transfer_stok_softlense_biasa_detail
(
    id                          INT PRIMARY KEY AUTO_INCREMENT,
    id_pengajuan_transfer_stok  INT NOT NULL,
    id_kategori                 INT NOT NULL,
    id_softlense_biasa_asal     INT NOT NULL,
    nama_merek_softlense_biasa  VARCHAR(255) NULL,
    warna                       VARCHAR(255) NULL,
    diameter                    VARCHAR(255) NULL,
    quantity_pengajuan_transfer INT NOT NULL,
    quantity_ditransfer         INT NOT NULL,
    id_softlense_biasa_tujuan   INT NULL,
    is_not_registered_on_tujuan BOOLEAN NULL,
    created_at                  TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at                  TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at                  TIMESTAMP NULL,
    updated_by                  INT NOT NULL,
    CONSTRAINT pengajuan_transfer_stok_softlense_biasa_header FOREIGN KEY (id_pengajuan_transfer_stok) REFERENCES pengajuan_transfer_stok (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT pengajuan_transfer_stok_softlense_biasa_kategori FOREIGN KEY (id_kategori) REFERENCES kategori (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT pengajuan_transfer_stok_softlense_biasa_asal FOREIGN KEY (id_softlense_biasa_asal) REFERENCES softlense_biasa (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT pengajuan_transfer_stok_softlense_biasa_tujuan FOREIGN KEY (id_softlense_biasa_tujuan) REFERENCES softlense_biasa (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE pengajuan_transfer_stok_produk_lainnya_detail
(
    id                          INT PRIMARY KEY AUTO_INCREMENT,
    id_pengajuan_transfer_stok  INT NOT NULL,
    id_kategori                 INT NOT NULL,
    id_produk_lainnya_asal      INT NULL,
    nama_produk_lainnya         VARCHAR(255) NULL,
    quantity_pengajuan_transfer INT NOT NULL,
    quantity_ditransfer         INT NOT NULL,
    id_produk_lainnya_tujuan    INT NULL,
    is_not_registered_on_tujuan BOOLEAN NULL,
    created_at                  TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at                  TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at                  TIMESTAMP NULL,
    updated_by                  INT NOT NULL,
    CONSTRAINT pengajuan_transfer_stok_produk_lainnya_header FOREIGN KEY (id_pengajuan_transfer_stok) REFERENCES pengajuan_transfer_stok (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT pengajuan_transfer_stok_produk_lainnya_kategori FOREIGN KEY (id_kategori) REFERENCES kategori (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT pengajuan_transfer_stok_produk_lainnya_asal FOREIGN KEY (id_produk_lainnya_asal) REFERENCES produk_lainnya (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT pengajuan_transfer_stok_produk_lainnya_tujuan FOREIGN KEY (id_produk_lainnya_tujuan) REFERENCES produk_lainnya (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);

CREATE TABLE biaya_lain_lain
(
    id         INT PRIMARY KEY AUTO_INCREMENT,
    id_toko    INT          NOT NULL,
    id_metode_pembayaran INT NOT NULL,
    keterangan VARCHAR(255) NOT NULL,
    nominal    INT          NOT NULL,
    tanggal    DATE         NOT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP NULL,
    CONSTRAINT fk_biaya_lain_lain_toko FOREIGN KEY (id_toko) REFERENCES toko (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
    CONSTRAINT fk_biaya_lain_lain_metode_pembayaran FOREIGN KEY (id_metode_pembayaran) REFERENCES metode_pembayaran (id) ON DELETE RESTRICT ON UPDATE RESTRICT
);
