$(document).ready(function () { let totalHarga = 0; let biayaAdmin = persentaseKeuntungan; let totalBiayaAdmin = 0; let totalBayar = 0; function tampilTotalHarga(harga) { let tampilTotalNominal = document.getElementById("totalBiaya"); let tampilTotalBiaya = document.getElementById("totalBayar"); let tampilTotalBiayaAdmin = document.getElementById("totalBiayaAdmin"); totalBiayaAdmin = (harga * biayaAdmin) / 100; totalBayar = totalBiayaAdmin + harga; tampilTotalNominal.innerHTML = harga.toLocaleString("id-ID", { style: "currency", currency: "IDR", }); tampilTotalBiaya.innerHTML = totalBayar.toLocaleString("id-ID", { style: "currency", currency: "IDR", }); tampilTotalBiayaAdmin.innerHTML = totalBiayaAdmin.toLocaleString( "id-ID", { style: "currency", currency: "IDR", } ); } $("#selectContact").select2({ placeholder: "Pilih Kontak", ajax: { url: "{{ route('user-contact.get') }}", processResults: function ({ data }) { return { results: $.map(data, function (item) { return { id: item.relasi_kontak, text: item.nama_depan + " " + item.nama_belakang, }; }), }; }, }, }); $("#hargaBarang").change(function () { let harga = $(this).val(); let jumlah = $("#jumlahBarang").val(); let biaya = document.getElementById("totalBiaya"); if (harga.trim() != "") { if (jumlah.trim() != "") { totalHarga = harga * jumlah; } else { totalHarga = harga * 0; } tampilTotalHarga(totalHarga); } }); $("#jumlahBarang").change(function () { let jumlah = $(this).val(); let harga = $("#hargaBarang").val(); let nominal = document.getElementById("jumlah"); if (jumlah.trim() != "") { if (jumlah == 0) { nominal.innerHTML = "Nominal"; } else { nominal.innerHTML = "Nominal (x" + jumlah + ")"; } if (harga.trim() != "") { totalHarga = harga * jumlah; } else { totalHarga = 0 * jumlah; } tampilTotalHarga(totalHarga); } }); $("#selectContact").change(function () { let email = $("#selectContact").val(); if (email === null) { $("#inputPenjual").prop("disabled", false); $("#checkButton").prop("disabled", false); $("#deleteOption").prop("disabled", false); } else { $("#inputPenjual").prop("disabled", true); $("#deleteOption").prop("disabled", false); $("#checkButton").prop("disabled", true); } }); $("#deleteOption").click(function () { $("#selectContact").val(null).trigger("change"); }); $("#inputPenjual").on("input", function () { let emailInput = $("#inputPenjual").val(); if (emailInput.trim() == "") { $("#selectContact").prop("disabled", false); $("#deleteOption").prop("disabled", false); } else { $("#selectContact").prop("disabled", true); $("#deleteOption").prop("disabled", true); } }); $("#checkButton").click(function () { let email = document.querySelector('[name="input_penjual"]').value; const csrfToken = $('meta[name="csrf-token"]').attr("content"); if (email.trim() == "") { Swal.fire({ title: "Gagal", text: "Email tidak boleh kosong", icon: "error", }); } else { if (email != "{{ Auth::user()->email }}") { $.ajaxSetup({ headers: { "X-CSRF-TOKEN": csrfToken, }, }); $.ajax({ url: "{{ route('user-contact.email', ':email') }}".replace( ":email", email ), type: "GET", success: function (response) { if ( response.status && (response.message.nama_bank == null || response.message.nama_bank == "") && (response.message.no_rek == null || response.message.no_rek == "") ) { Swal.fire({ title: "Berhasil", text: "Akun tersedia", icon: "success", }); } else { Swal.fire({ title: "Gagal", text: response.message, icon: "error", }); } }, error: function (error) { Swal.fire({ title: "Gagal", text: "Terjadi error karena, " + error, icon: "error", }); }, }); } else { Swal.fire({ title: "Gagal", text: "Email penjual tidak boleh sama denggan email pembeli", icon: "error", }); } } }); $("#save").on("click", function () { let opsiKontak = document.querySelector( '[name="select_penjual"]' ).value; let inputKontak = document.querySelector( '[name="input_penjual"]' ).value; let namaBarang = document.querySelector('[name="nama_barang"]').value; let hargaBarang = document.querySelector('[name="harga_barang"]').value; let satuanBarang = document.querySelector( '[name="satuan_barang"]' ).value; let jumlahBarang = document.querySelector( '[name="jumlah_barang"]' ).value; let deskripsi = document.querySelector( 'textarea[name="deskripsi"]' ).value; let emptyInput = []; let penjual = ""; if (opsiKontak != "" && inputKontak != "") { penjual = opsiKontak; } else if (opsiKontak == "" && inputKontak != "") { penjual = inputKontak; } else if (inputKontak == "" && opsiKontak != "") { penjual = opsiKontak; } else { emptyInput.push("Kolom penjual tidak boleh kosong"); } if (penjual == "{{ Auth::user()->email }}") { emptyInput.push( "Email penjual tidak boleh sama dengan email pembeli" ); } if (namaBarang == "") { emptyInput.push("Nama barang tidak boleh kosong"); } if (hargaBarang == "") { emptyInput.push("Harga barang tidak boleh kosong"); } if (satuanBarang == "") { emptyInput.push("Satuan barang tidak boleh kosong"); } if (jumlahBarang == "") { emptyInput.push("Jumlah barang tidak boleh kosong"); } if (emptyInput.length > 0) { const emptyInputError = emptyInput.join(", "); Swal.fire({ title: "Gagal", text: emptyInputError, icon: "error", confirmButtonText: "OK", }); } else { const csrfToken = $('meta[name="csrf-token"]').attr("content"); const formData = new FormData(); formData.append("email_penjual", penjual); formData.append("nama_barang", namaBarang); formData.append("satuan_barang", satuanBarang); formData.append("harga_barang", hargaBarang); formData.append("jumlah_barang", jumlahBarang); // formData.append('batas_pengiriman', batasPengiriman); formData.append("deskripsi", deskripsi); formData.append("persentase_keuntungan", biayaAdmin); formData.append("total_keuntungan", totalBiayaAdmin); formData.append("total_harga", totalHarga); formData.append("total_bayar", totalBayar); $(modal).modal("show"); $.ajaxSetup({ headers: { "X-CSRF-TOKEN": csrfToken, }, }); $.ajax({ url: "{{ route('store-transaction.pembeli') }}", type: "POST", data: formData, processData: false, contentType: false, success: function (response) { $(modal).modal("hide"); Swal.fire({ title: response.status ? "Berhasil" : "Gagal", text: response.message, icon: response.status ? "success" : "error", confirmButtonText: "OK", }).then(function () { location.href = "{{ route('user-transaction.index.pembeli') }}"; }); console.log(response); }, error: function (error) { $(modal).modal("hide"); Swal.fire({ title: "Gagal", text: "Gagal mengirimkan data karena " + error, icon: "error", }); console.log(error); }, }); } }); });