Spaces:
Sleeping
Sleeping
import gradio as gr | |
import random | |
import string | |
import datetime | |
# Daftar produk dan kategori | |
categories = { | |
"Kategori A": ["Produk A1", "Produk A2", "Produk A3", "Produk A4", "Produk A5", "Produk A6"], | |
"Kategori B": ["Produk B1", "Produk B2", "Produk B3", "Produk B4", "Produk B5", "Produk B6"], | |
"Kategori C": ["Produk C1", "Produk C2", "Produk C3", "Produk C4", "Produk C5", "Produk C6"], | |
"Kategori D": ["Produk D1", "Produk D2", "Produk D3", "Produk D4", "Produk D5", "Produk D6"] | |
} | |
# Fungsi untuk membuat dokumen LaTeX | |
def create_latex(data, filename): | |
latex_content = r""" | |
\documentclass[a4paper,12pt]{article} | |
\usepackage[utf8]{inputenc} | |
\usepackage[T1]{fontenc} | |
\usepackage{lmodern} | |
\usepackage{geometry} | |
\geometry{margin=1in} | |
\usepackage{booktabs} | |
\usepackage{fancyhdr} | |
\pagestyle{fancy} | |
\fancyhf{} | |
\fancyhead[C]{Dokumen Penawaran \\ PT. Contoh Perusahaan \\ Jl. Contoh Alamat No. 123} | |
\fancyfoot[C]{\thepage} | |
\begin{document} | |
\begin{center} | |
\textbf{\LARGE Dokumen Penawaran} \\ | |
\vspace{0.5cm} | |
PT. Contoh Perusahaan \\ | |
Jl. Contoh Alamat No. 123 \\ | |
\vspace{0.5cm} | |
\end{center} | |
Kepada Yth. \\ | |
\textbf{""" + data['nama_prospek'] + r"""} \\ | |
""" + data['alamat_prospek'] + r""" \\ | |
Jenis Prospek: """ + data['jenis_prospek'] + r""" \\ | |
\vspace{0.5cm} | |
Tanggal: """ + data['tanggal'] + r""" \\ | |
\vspace{0.5cm} | |
\textbf{Daftar Produk yang Ditawarkan:} \\ | |
\begin{tabular}{|l|c|r|} | |
\hline | |
\textbf{Nama Produk} & \textbf{Jumlah} & \textbf{Harga (Rp)} \\ | |
\hline | |
""" + "\n ".join([f"{p['nama']} & {p['jumlah']} & {p['harga']:,}" for p in data['produk']]) + r""" \\ | |
\hline | |
\end{tabular} | |
\vspace{0.5cm} | |
Diskon: """ + (f"{data['diskon']}\%" if 'diskon' in data else "Tidak ada") + r""" \\ | |
\vspace{0.5cm} | |
""" + (r"""\textbf{Syarat dan Ketentuan:} \\ | |
""" + data['syarat'] + r""" \\ | |
\vspace{0.5cm}""" if 'syarat' in data else r"Syarat dan Ketentuan: Tidak ada \\ \vspace{0.5cm}") + r""" | |
Hormat kami, \\ | |
\vspace{0.5cm} | |
PT. Contoh Perusahaan | |
\end{document} | |
""" | |
with open(filename, "w") as f: | |
f.write(latex_content) | |
return filename | |
# Fungsi untuk memproses input dan membuat penawaran | |
def buat_penawaran(nama_prospek, alamat_prospek, jenis_prospek, produk_dipilih, jumlah_produk, harga_produk, diskon, tanggal, syarat): | |
data = { | |
'nama_prospek': nama_prospek if nama_prospek else "Prospek Tanpa Nama", | |
'alamat_prospek': alamat_prospek if alamat_prospek else "Alamat Tidak Diketahui", | |
'jenis_prospek': jenis_prospek if jenis_prospek else "Individu", | |
'tanggal': tanggal if tanggal else datetime.date.today().strftime("%Y-%m-%d"), | |
'produk': [] | |
} | |
# Proses produk | |
if produk_dipilih and jumlah_produk and harga_produk: | |
try: | |
jumlah_list = [int(x.strip()) for x in jumlah_produk.split(",") if x.strip()] | |
harga_list = [int(x.strip()) for x in harga_produk.split(",") if x.strip()] | |
for i, p in enumerate(produk_dipilih): | |
j = jumlah_list[i] if i < len(jumlah_list) else 1 | |
h = harga_list[i] if i < len(harga_list) else 100000 | |
data['produk'].append({'nama': p, 'jumlah': j, 'harga': h}) | |
except ValueError: | |
data['produk'].append({'nama': "Produk Contoh", 'jumlah': 1, 'harga': 100000}) | |
warning = "Peringatan: Format jumlah atau harga produk salah. Diganti dengan data contoh." | |
else: | |
data['produk'].append({'nama': "Produk Contoh", 'jumlah': 1, 'harga': 100000}) | |
if diskon: | |
data['diskon'] = diskon | |
if syarat: | |
data['syarat'] = syarat | |
# Validasi data | |
missing_data = [] | |
if not nama_prospek: | |
missing_data.append("Nama Prospek") | |
if not alamat_prospek: | |
missing_data.append("Alamat Prospek") | |
if not jenis_prospek: | |
missing_data.append("Jenis Prospek") | |
if not tanggal: | |
missing_data.append("Tanggal") | |
if not produk_dipilih or not jumlah_produk or not harga_produk: | |
missing_data.append("Produk, Jumlah, atau Harga") | |
warning = "" | |
if missing_data: | |
warning = "Peringatan: Data berikut kurang dan telah diisi dengan asumsi: " + ", ".join(missing_data) + ". Silakan lengkapi data jika perlu." | |
# Buat file LaTeX | |
filename = ''.join(random.choices(string.ascii_letters + string.digits, k=10)) + ".tex" | |
latex_file = create_latex(data, filename) | |
return latex_file, warning | |
# Interface Gradio | |
with gr.Blocks() as demo: | |
gr.Markdown("# Aplikasi Pembuatan Dokumen Penawaran") | |
with gr.Row(): | |
nama_prospek = gr.Textbox(label="Nama Prospek") | |
alamat_prospek = gr.Textbox(label="Alamat Prospek") | |
jenis_prospek = gr.Dropdown(choices=["Individu", "Perusahaan", "Kafe"], label="Jenis Prospek") | |
with gr.Row(): | |
tanggal = gr.Textbox(label="Tanggal Penawaran (YYYY-MM-DD)") | |
with gr.Row(): | |
produk_dipilih = gr.CheckboxGroup(choices=[p for cat in categories.values() for p in cat], label="Pilih Produk") | |
jumlah_produk = gr.Textbox(label="Jumlah Produk (pisahkan dengan koma)") | |
harga_produk = gr.Textbox(label="Harga per Produk (Rp, pisahkan dengan koma)") | |
with gr.Row(): | |
diskon = gr.Textbox(label="Diskon (%)") | |
syarat = gr.Textbox(label="Syarat dan Ketentuan") | |
submit_button = gr.Button("Buat Penawaran") | |
output_file = gr.File(label="Download Dokumen Penawaran (LaTeX, akan dirender sebagai PDF)") | |
warning_text = gr.Textbox(label="Peringatan") | |
submit_button.click( | |
buat_penawaran, | |
inputs=[nama_prospek, alamat_prospek, jenis_prospek, produk_dipilih, jumlah_produk, harga_produk, diskon, tanggal, syarat], | |
outputs=[output_file, warning_text] | |
) | |
demo.launch() |