Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow invoices download as csv #1911

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions dashboard/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,22 @@ export function validateSubdomain(subdomain) {
return null;
}

export function downloadAsCSV(data, filename) {
if (!data || data.length === 0) return;
let result = [];
result[0] = Object.keys(data[0]);
data.forEach(row => {
result.push(Object.values(row));
});
const csv = result.map(row => Object.values(row).join(',')).join('\n');
const blob = new Blob([csv], { type: 'text/csv' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
}

export { utils };
export { default as dayjs } from './utils/dayjs';
24 changes: 24 additions & 0 deletions dashboard/src2/components/InvoiceTable.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<template>
<div>
<div class="flex justify-end">
<Button
icon-left="download"
class="shrink-0"
@click="$resources.downloadInvoice.submit"
>
<span class="text-sm">Download as CSV</span>
</Button>
</div>
<div v-if="doc" class="overflow-x-auto">
<table
class="text w-full border-separate border-spacing-y-2 text-base font-normal text-gray-900"
Expand Down Expand Up @@ -132,6 +141,7 @@
</template>
<script>
import { getPlans } from '../data/plans';
import { downloadAsCSV } from '../../src/utils';

export default {
name: 'InvoiceTable',
Expand All @@ -143,6 +153,20 @@ export default {
doctype: 'Invoice',
name: this.invoiceId
};
},
downloadInvoice() {
return {
url: 'press.api.billing.fetch_invoice_items',
makeParams() {
return {
invoice: this.invoiceId
};
},
onSuccess(data) {
let filename = `${this.invoiceId}.csv`;
downloadAsCSV(data, filename);
}
};
}
},
computed: {
Expand Down
19 changes: 19 additions & 0 deletions press/api/billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,3 +643,22 @@ def total_unpaid_amount():
)[0]
or 0
) + negative_balance


@frappe.whitelist()
def fetch_invoice_items(invoice):
return frappe.get_all(
"Invoice Item",
{"parent": invoice, "parenttype": "Invoice"},
[
"document_type",
"document_name",
"rate",
"quantity",
"amount",
"plan",
"description",
"discount",
"site",
],
)