Add InvoiceService and initial invoice dashboard widget
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
import axios, { AxiosResponse } from 'axios';
|
||||
import { Invoice, SalesStatistics as SalesStatistics, LineItem } from '@/types';
|
||||
|
||||
const API_URL = '/api/invoices';
|
||||
const LINE_ITEM_API_URL = '/api/lineitems';
|
||||
|
||||
export default {
|
||||
/**
|
||||
* Retrieves all invoices
|
||||
* @returns Promise<Invoice[]>
|
||||
*/
|
||||
async getAllInvoices(): Promise<Invoice[]> {
|
||||
const response = await axios.get<Invoice[]>(API_URL);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* Retrieves invoice summary for all time
|
||||
* @returns Promise<Invoice[]>
|
||||
*/
|
||||
async getSummaryAll(): Promise<Invoice[]> {
|
||||
const response = await axios.get<Invoice[]>(`${API_URL}/summary`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* Retrieves invoice summary for the current year
|
||||
* @returns Promise<Invoice[]>
|
||||
*/
|
||||
async getSummaryThisYear(): Promise<Invoice[]> {
|
||||
const response = await axios.get<Invoice[]>(`${API_URL}/summaryThisYear`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* Retrieves invoice summary for before the current year
|
||||
* @returns Promise<Invoice[]>
|
||||
*/
|
||||
async getSummaryBeforeThisYear(): Promise<Invoice[]> {
|
||||
const response = await axios.get<Invoice[]>(`${API_URL}/summaryBeforeThisYear`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* Retrieves sales statistics
|
||||
* @returns Promise<InvoiceStatistics>
|
||||
*/
|
||||
async getSalesStatistics(): Promise<SalesStatistics> {
|
||||
const response = await axios.get<SalesStatistics>(`${API_URL}/salesStatistics`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* Creates a new invoice
|
||||
* @param invoice - The invoice data to create
|
||||
* @returns Promise<Invoice>
|
||||
*/
|
||||
async createInvoice(invoice: Partial<Invoice>): Promise<Invoice> {
|
||||
const response = await axios.post<Invoice>(API_URL, invoice);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* Updates an existing invoice
|
||||
* @param invoice - The invoice object to update
|
||||
* @returns Promise<Invoice>
|
||||
*/
|
||||
async updateInvoice(invoice: Partial<Invoice>): Promise<Invoice> {
|
||||
const response = await axios.put<Invoice>(`${API_URL}/${invoice.id}`, invoice);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* Deletes an invoice by ID
|
||||
* @param invoiceId - The id of the invoice to delete
|
||||
* @returns Promise<AxiosResponse>
|
||||
*/
|
||||
async deleteInvoice(invoiceId: number): Promise<AxiosResponse> {
|
||||
const response = await axios.delete(`${API_URL}/${invoiceId}`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* Retrieves line items for a specific invoice
|
||||
* @param invoiceId - The ID of the invoice
|
||||
* @returns Promise<LineItem[]>
|
||||
*/
|
||||
async getLineItems(invoiceId: number): Promise<LineItem[]> {
|
||||
const response = await axios.get<LineItem[]>(`${LINE_ITEM_API_URL}/${invoiceId}`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* Sends a reminder for an invoice
|
||||
* @param invoiceId - The ID of the invoice to remind
|
||||
* @param to - Email address to send the reminder to
|
||||
* @param cc - Optional email address to CC the reminder to
|
||||
* @returns Promise<AxiosResponse>
|
||||
*/
|
||||
async remindInvoice(invoiceId: number, to: string, cc?: string): Promise<AxiosResponse> {
|
||||
const params = { to, cc };
|
||||
const response = await axios.get(`${API_URL}/${invoiceId}/remind`, { params });
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* Exports an invoice as PDF
|
||||
* @param invoiceId - The ID of the invoice to export
|
||||
* @returns Promise<Blob>
|
||||
*/
|
||||
async exportPdf(invoiceId: number): Promise<Blob> {
|
||||
const response = await axios.get(`${API_URL}/${invoiceId}/pdf`, {
|
||||
responseType: 'blob'
|
||||
});
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* Exports an invoice as XML
|
||||
* @param invoiceId - The ID of the invoice to export
|
||||
* @returns Promise<Blob>
|
||||
*/
|
||||
async exportXml(invoiceId: number): Promise<Blob> {
|
||||
const response = await axios.get(`${API_URL}/${invoiceId}/xml`, {
|
||||
responseType: 'blob'
|
||||
});
|
||||
return response.data;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user