File size: 14,405 Bytes
4fc5037
c086736
 
 
 
 
 
 
 
4fc5037
c086736
 
 
 
 
 
 
4fc5037
c086736
 
 
 
 
4fc5037
c086736
 
 
 
4fc5037
c086736
ae6e810
 
 
c086736
4fc5037
c086736
 
 
 
ae6e810
c086736
4fc5037
c086736
 
 
4fc5037
c086736
 
4fc5037
c086736
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fc5037
c086736
 
 
 
4fc5037
c086736
 
cfdd27a
4fc5037
c086736
 
 
4fc5037
c086736
 
 
 
 
 
 
 
 
4fc5037
c086736
 
 
 
 
 
 
 
 
 
 
 
 
 
4fc5037
c086736
 
 
 
 
 
 
4fc5037
c086736
 
 
 
 
 
e8c73e7
 
c086736
 
 
 
4fc5037
c086736
 
 
4fc5037
c086736
 
 
 
4fc5037
c086736
 
 
 
 
 
 
 
 
 
 
 
4fc5037
c086736
 
 
603b820
c43f98b
 
 
c086736
 
 
37e86eb
c43f98b
 
c086736
 
0740e13
c086736
 
 
0740e13
c086736
 
 
0740e13
c086736
 
 
37e86eb
cfdd27a
 
c086736
cfdd27a
 
c086736
 
 
 
 
 
5ed4984
 
c086736
 
37e86eb
cfdd27a
c086736
 
37e86eb
c086736
ae6e810
 
c086736
 
37e86eb
c086736
ae6e810
 
c086736
 
37e86eb
c086736
ae6e810
 
c086736
 
37e86eb
c086736
ae6e810
 
c086736
 
 
 
 
4fc5037
c086736
 
 
 
 
 
 
4fc5037
c086736
 
 
 
 
 
 
 
 
 
4fc5037
c086736
 
 
4fc5037
c086736
 
4fc5037
c086736
 
 
 
4fc5037
 
c086736
4fc5037
c086736
 
 
 
4fc5037
 
c086736
 
4fc5037
c086736
 
 
 
 
 
 
 
 
4fc5037
c086736
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import gradio as gr
import random
import string
import datetime
from reportlab.lib.pagesizes import A4
from reportlab.lib import colors
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
from reportlab.lib.units import cm

# 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 PDF
def create_pdf(data, filename):
    doc = SimpleDocTemplate(filename, pagesize=A4, rightMargin=2*cm, leftMargin=2*cm, topMargin=2*cm, bottomMargin=2*cm)
    styles = getSampleStyleSheet()
    elements = []

    # Gaya teks
    title_style = ParagraphStyle(name='Title', fontSize=16, alignment=1, spaceAfter=12)
    normal_style = ParagraphStyle(name='Normal', fontSize=12, spaceAfter=8)
    bold_style = ParagraphStyle(name='Bold', fontSize=12, fontName='Helvetica-Bold', spaceAfter=8)

    # Header
    elements.append(Paragraph("Quotation", title_style))
    elements.append(Paragraph("PT.Arafa Asia", normal_style))
    elements.append(Paragraph("Jl.Griya Cigadung Baru E2, Bandung 40191", normal_style))
    elements.append(Spacer(1, 0.5*cm))

    # Kepada Yth.
    elements.append(Paragraph(f"Kepada Yth.", normal_style))
    elements.append(Paragraph(f"{data['nama_prospek']}", bold_style))
    elements.append(Paragraph(f"{data['alamat_prospek']}", normal_style))
    #elements.append(Paragraph(f"Jenis Prospek: {data['jenis_prospek']}", normal_style))
    elements.append(Spacer(1, 0.5*cm))

    # Tanggal
    elements.append(Paragraph(f"Tanggal: {data['tanggal']}", normal_style))
    elements.append(Spacer(1, 0.5*cm))

    # Tabel Produk
    elements.append(Paragraph("Daftar Produk yang Ditawarkan:", bold_style))
    
    # Data tabel
    table_data = [["Nama Produk", "Jumlah", "Harga (Rp)", "Total (Rp)"]]
    sub_total = 0
    for p in data['produk']:
        total = p['jumlah'] * p['harga']
        sub_total += total
        table_data.append([p['nama'], str(p['jumlah']), f"{p['harga']:,}", f"{total:,}"])
    
    # Hitung diskon dan grand total
    if 'diskon' in data and data['diskon']:
        diskon_percent = float(data['diskon'])
        diskon_amount = (diskon_percent / 100) * sub_total
        grand_total = sub_total - diskon_amount
    else:
        diskon_percent = 0
        diskon_amount = 0
        grand_total = sub_total
    
    # Tambahkan baris sub-total, diskon, dan grand total
    table_data.append(["", "", "Sub-Total", f"{sub_total:,}"])
    if diskon_percent > 0:
        table_data.append(["", "", f"Diskon ({diskon_percent}%)", f"-{diskon_amount:,}"])
    table_data.append(["", "", "Grand Total", f"{grand_total:,}"])
    
    # Buat tabel
    table = Table(table_data, colWidths=[6*cm, 2*cm, 3*cm, 3*cm])
    table.setStyle(TableStyle([
        ('BACKGROUND', (0, 0), (-1, 0), colors.grey),
        ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
        ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
        ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
        ('FONTSIZE', (0, 0), (-1, -1), 12),
        ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
        ('BACKGROUND', (0, 1), (-1, -3), colors.beige),
        ('GRID', (0, 0), (-1, -1), 1, colors.black),
        ('SPAN', (0, -2), (1, -2)),  # Span sub-total
        ('SPAN', (0, -1), (1, -1)),  # Span grand total
        ('ALIGN', (2, -2), (3, -1), 'RIGHT'),  # Align right for totals
    ]))
    if diskon_percent > 0:
        table.setStyle(TableStyle([
            ('SPAN', (0, -3), (1, -3)),  # Span diskon
        ]))
    elements.append(table)
    elements.append(Spacer(1, 0.5*cm))

    # Syarat dan Ketentuan
    syarat_text = f"Syarat dan Ketentuan:<br/>{data['syarat']}" if 'syarat' in data else "Syarat dan Ketentuan: Tidak ada"
    elements.append(Paragraph(syarat_text, normal_style))
    elements.append(Spacer(1, 0.5*cm))

    # Tanda Tangan
    elements.append(Paragraph("Hormat kami,", normal_style))
    elements.append(Paragraph("PT.Arafa Asia", normal_style))

    # Build PDF
    doc.build(elements)
    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:
        try:
            data['diskon'] = float(diskon)
        except ValueError:
            data['diskon'] = 0
    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 PDF
    filename = ''.join(random.choices(string.ascii_letters + string.digits, k=10)) + ".pdf"
    pdf_file = create_pdf(data, filename)
    return pdf_file, warning

# HTML untuk mengintegrasikan annyang
annyang_html = """
<div>
    <button id="startSpeech">Mulai Pengenalan Suara</button>
    <button id="stopSpeech" style="display:none;">Hentikan Pengenalan Suara</button>
    <p id="speechStatus">Status: Menunggu perintah suara...</p>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/annyang/2.6.1/annyang.min.js"></script>
<script>
if (annyang) {
    // Konfigurasi annyang untuk bahasa Indonesia
    annyang.setLanguage('id-ID');
    
    // Definisikan perintah suara
    var commands = {
        'nama *value': function(value) {
            document.getElementById('nama_prospek').querySelector('textarea').value = value;
                const inputEvent = new Event('input', { bubbles: true, cancelable: true });
                document.getElementById('nama_prospek').querySelector('textarea').dispatchEvent(inputEvent);
            
            document.getElementById('speechStatus').innerText = 'Nama prospek diisi: ' + value;
        },
        'alamat *value': function(value) {
            document.getElementById('alamat_prospek').querySelector('textarea').value = value;
                const inputEvent = new Event('input', { bubbles: true, cancelable: true });
                document.getElementById('alamat_prospek').querySelector('textarea').dispatchEvent(inputEvent);
        },
        'jenis individu': function() {
            document.getElementById('jenis_prospek').querySelector('input').value = 'Individu';
            document.getElementById('speechStatus').innerText = 'Jenis prospek: Individu';
        },
        'jenis perusahaan': function() {
            document.getElementById('jenis_prospek').querySelector('input').value = 'Perusahaan';
            document.getElementById('speechStatus').innerText = 'Jenis prospek: Perusahaan';
        },
        'jenis kafe': function() {
            document.getElementById('jenis_prospek').querySelector('input').value = 'Kafe';
            document.getElementById('speechStatus').innerText = 'Jenis prospek: Kafe';
        },
        'tanggal *value': function(value) {
            document.getElementById('tanggal').querySelector('textarea').value = value;
            document.getElementById('tanggal').querySelector('textarea').innerHTML = value;
            document.getElementById('tanggal').querySelector('textarea').innerText = value;
            document.getElementById('speechStatus').innerText = 'Tanggal diisi: ' + value;
                const inputEvent = new Event('input', { bubbles: true, cancelable: true });
                document.getElementById('tanggal').querySelector('textarea').dispatchEvent(inputEvent);
        },
        'produk *value': function(value) {
            var checkboxes = document.querySelectorAll('input[name="produk_dipilih"]');
            checkboxes.forEach(function(cb) {
                if (cb.value.toLowerCase().includes(value.toLowerCase())) {
                    cb.checked = true;
                    //const inputEvent = new Event('input', { bubbles: true, cancelable: true });
                    //cb.dispatchEvent(inputEvent);                   
                }
            });
            document.getElementById('speechStatus').querySelector('textarea').innerText = 'Produk dipilih: ' + value;
            
        },
        'jumlah *value': function(value) {
            document.getElementById('jumlah_produk').querySelector('textarea').value = value;
            document.getElementById('speechStatus').innerText = 'Jumlah produk diisi: ' + value;
                const inputEvent = new Event('input', { bubbles: true, cancelable: true });
                document.getElementById('jumlah_produk').querySelector('textarea').dispatchEvent(inputEvent);
        },
        'harga *value': function(value) {
            document.getElementById('harga_produk').querySelector('textarea').value = value;
            document.getElementById('speechStatus').innerText = 'Harga produk diisi: ' + value;
                const inputEvent = new Event('input', { bubbles: true, cancelable: true });
                document.getElementById('harga_produk').querySelector('textarea').dispatchEvent(inputEvent);
        },
        'diskon *value': function(value) {
            document.getElementById('diskon').querySelector('textarea').value = value;
            document.getElementById('speechStatus').innerText = 'Diskon diisi: ' + value;
                const inputEvent = new Event('input', { bubbles: true, cancelable: true });
                document.getElementById('diskon').querySelector('textarea').dispatchEvent(inputEvent);
        },
        'syarat *value': function(value) {
            document.getElementById('syarat').querySelector('textarea').value = value;
            document.getElementById('speechStatus').innerText = 'Syarat dan ketentuan diisi: ' + value;
                const inputEvent = new Event('input', { bubbles: true, cancelable: true });
                document.getElementById('syarat').querySelector('textarea').dispatchEvent(inputEvent);
        }
    };

    // Tambahkan perintah ke annyang
    annyang.addCommands(commands);

    // Event untuk tombol mulai
    document.getElementById('startSpeech').addEventListener('click', function() {
        annyang.start({ autoRestart: true, continuous: true });
        document.getElementById('startSpeech').style.display = 'none';
        document.getElementById('stopSpeech').style.display = 'inline';
        document.getElementById('speechStatus').innerText = 'Status: Mendengarkan perintah suara...';
    });

    // Event untuk tombol hentikan
    document.getElementById('stopSpeech').addEventListener('click', function() {
        annyang.abort();
        document.getElementById('startSpeech').style.display = 'inline';
        document.getElementById('stopSpeech').style.display = 'none';
        document.getElementById('speechStatus').innerText = 'Status: Pengenalan suara dihentikan.';
    });
}
</script>
"""

# Interface Gradio
with gr.Blocks() as demo:
    gr.Markdown("# Aplikasi Pembuatan Dokumen Penawaran")
    
    # Komponen HTML untuk annyang
    gr.HTML(annyang_html)
    
    with gr.Row():
        nama_prospek = gr.Textbox(label="Nama Prospek", elem_id="nama_prospek")
        alamat_prospek = gr.Textbox(label="Alamat Prospek", elem_id="alamat_prospek")
        jenis_prospek = gr.Dropdown(choices=["Individu", "Perusahaan", "Kafe"], label="Jenis Prospek", elem_id="jenis_prospek")
    
    with gr.Row():
        tanggal = gr.Textbox(label="Tanggal Penawaran (YYYY-MM-DD)", elem_id="tanggal")
    
    with gr.Row():
        produk_dipilih = gr.CheckboxGroup(choices=[p for cat in categories.values() for p in cat], label="Pilih Produk", elem_id="produk_dipilih")
        jumlah_produk = gr.Textbox(label="Jumlah Produk (pisahkan dengan koma)", elem_id="jumlah_produk")
        harga_produk = gr.Textbox(label="Harga per Produk (Rp, pisahkan dengan koma)", elem_id="harga_produk")
    
    with gr.Row():
        diskon = gr.Textbox(label="Diskon (%)", elem_id="diskon")
        syarat = gr.Textbox(label="Syarat dan Ketentuan", elem_id="syarat")
    
    submit_button = gr.Button("Buat Penawaran")
    
    output_file = gr.File(label="Download Dokumen Penawaran (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()