Hi Team,
Pls can someone correct my script for itemwise sales register to be exported in txt file for specific date period and brand. I cannot extract any data from this script
def execute(filters=None):
data =
columns = [
“Item Code”,
“Item Name”,
“Brand”,
“Quantity”,
“Rate”,
“Amount”,
“Sales Invoice”,
“Posting Date”
]
# Fetch item sales data
sales_data = frappe.db.sql("""
SELECT
sii.item_code,
sii.item_name,
sii.brand,
sii.qty,
sii.rate,
sii.amount,
sii.parent AS sales_invoice,
si.posting_date
FROM
`tabSales Invoice Item` AS sii
INNER JOIN
`tabSales Invoice` AS si ON sii.parent = si.name
WHERE
si.docstatus = 1
AND sii.brand = %s
AND si.posting_date BETWEEN %s AND %s
ORDER BY
si.posting_date DESC
""", (filters.get("from_date"), filters.get("to_date"), filters.get("brand_name")), as_dict=True)
# Format data for TXT
txt_data = "\t".join(columns) + "\n"
for row in sales_data:
txt_data += "\t".join([
row.item_code or '',
row.item_name or '',
row.brand or '',
str(row.qty or 0),
str(row.rate or 0),
str(row.amount or 0),
row.sales_invoice or '',
row.posting_date.strftime('%Y-%m-%d') if row.posting_date else ''
]) + "\n"
data.append([
row.item_code, row.item_name, row.brand, row.qty,
row.rate, row.amount, row.sales_invoice, row.posting_date
])
# Save to file
file_path = "/private/files/itemwise_sales_register.txt"
with open(get_site_path() + file_path, "w", encoding='utf-8') as file:
file.write(txt_data)
frappe.msgprint(f"TXT Report generated: {file_path}")
return columns, data