hi @lasalesi thanks for reply,
i runing my code, but not respon…
code JS:
frappe.query_reports[“Sales invoice script”] = {
“filters”: [
],
onload: function (report) {
report.page.add_inner_button(__('Download to CSV'), function () {
return frappe.call({
method: "erpnext.accounts.report.sales_invoice_script.sales_invoice_script.get_my_csv",
callback: function () {
download("report.csv", r.message.content);
frappe.msgprint("CSV Berhasil Di Download");
}
});
function download(filename, content) {
var element = document.createElement('a');
element.setAttribute('href', 'data:application/octet-stream;charset=utf-8,' + encodeURIComponent(content));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
})
}
}
code PY:
@frappe.whitelist()
def download():
import re
import csv
import os
import sys
import datetime
csv.register_dialect('unixpwd', quoting=csv.QUOTE_ALL)
filename = datetime.datetime.now().strftime("report-develop-%Y-%m-%d-%H-%M.csv")
# filename = os.path.join('..', path)
with open(filename, 'w+') as f:
writer = csv.writer(f, 'unixpwd')
writer.writerow(["FK", "KD_JENIS_TRANSAKSI", "FG_PENGGANTI", "NOMOR_FAKTUR", "MASA_PAJAK", "TAHUN_PAJAK", "TANGGAL_FAKTUR", "NPWP", "NAMA", "ALAMAT_LENGKAP", "JUMLAH_DPP", "JUMLAH_PPN", "JUMLAH_PPNBM", "ID_KETERANGAN_TAMBAHAN", "FG_UANG_MUKA", "UANG_MUKA_DPP", "UANG_MUKA_PPN", "UANG_MUKA_PPNBM", "REFERENSI"])
writer.writerow(["LT", "NPWP", "NAMA", "JALAN", "BLOK", "NOMOR", "RT", "RW", "KECAMATAN", "KELURAHAN", "KABUPATEN", "PROPINSI", "KODE_POS", "NOMOR_TELEPON"])
writer.writerow(["OF", "KODE_OBJEK", "NAMA", "HARGA_SATUAN", "JUMLAH_BARANG","HARGA_TOTAL", "DISKON", "DPP", "PPN", "TARIF_PPNBM", "PPNBM"])
parents = frappe.db.sql("select MONTH(posting_date) as month,Year(posting_date) as year, posting_date, tax_id, customer, address_display, name,(select SUM((price_list_rate*qty)-net_amount) as JUMLAH_DPP1 from `tabSales Invoice Item` where `tabSales Invoice Item`.parent=`tabSales Invoice`.name) as JUMLAH_DPP from `tabSales Invoice`", as_dict=1)
for parent in parents:
writer.writerow(["FK", "1", "", "0", parent['month'], parent['year'], parent['posting_date'], parent['tax_id'], parent['customer'], parent['address_display'], parent['JUMLAH_DPP'], parent['JUMLAH_DPP']*0.1, "0", "", "0", "0", "0", "0", parent['name']])
children = frappe.db.sql(
"select item_name, price_list_rate, qty, (price_list_rate*qty) as amount, net_amount, ((price_list_rate*qty)-net_amount) as dpp from `tabSales Invoice Item` where parent=%s", parent.name, as_dict=1)
for child in children:
writer.writerow(["OF", "", child['item_name'], child['price_list_rate'], child['qty'], child['amount'], child['net_amount'], child['dpp'], child['dpp']*0.1])
@frappe.whitelist()
def get_my_csv():
content = download()
return {‘content’: content}
thanks