- Add invoices feature: types, API service for CRUD, validation, payments, PDF - Add inventory feature: types, API service for stock, movements, warehouses, locations - Add sales feature: types, API service for orders, quotations, PDF - Add purchases feature: types, API service for orders, receipts Part of FASE 2: P0 Frontend API Services
139 lines
4.7 KiB
TypeScript
139 lines
4.7 KiB
TypeScript
import { api } from '@services/api/axios-instance';
|
|
import type {
|
|
Invoice,
|
|
CreateInvoiceDto,
|
|
UpdateInvoiceDto,
|
|
InvoiceFilters,
|
|
InvoicesResponse,
|
|
InvoiceStats,
|
|
} from '../types';
|
|
|
|
const BASE_URL = '/api/v1/invoices';
|
|
|
|
export const invoicesApi = {
|
|
// Get all invoices with filters
|
|
getAll: async (filters?: InvoiceFilters): Promise<InvoicesResponse> => {
|
|
const params = new URLSearchParams();
|
|
if (filters?.search) params.append('search', filters.search);
|
|
if (filters?.invoiceType) params.append('invoiceType', filters.invoiceType);
|
|
if (filters?.invoiceContext) params.append('invoiceContext', filters.invoiceContext);
|
|
if (filters?.status) params.append('status', filters.status);
|
|
if (filters?.partnerId) params.append('partnerId', filters.partnerId);
|
|
if (filters?.dateFrom) params.append('dateFrom', filters.dateFrom);
|
|
if (filters?.dateTo) params.append('dateTo', filters.dateTo);
|
|
if (filters?.page) params.append('page', String(filters.page));
|
|
if (filters?.limit) params.append('limit', String(filters.limit));
|
|
if (filters?.sortBy) params.append('sortBy', filters.sortBy);
|
|
if (filters?.sortOrder) params.append('sortOrder', filters.sortOrder);
|
|
|
|
const response = await api.get<InvoicesResponse>(`${BASE_URL}?${params.toString()}`);
|
|
return response.data;
|
|
},
|
|
|
|
// Get invoice by ID
|
|
getById: async (id: string): Promise<Invoice> => {
|
|
const response = await api.get<Invoice>(`${BASE_URL}/${id}`);
|
|
return response.data;
|
|
},
|
|
|
|
// Create invoice
|
|
create: async (data: CreateInvoiceDto): Promise<Invoice> => {
|
|
const response = await api.post<Invoice>(BASE_URL, data);
|
|
return response.data;
|
|
},
|
|
|
|
// Update invoice
|
|
update: async (id: string, data: UpdateInvoiceDto): Promise<Invoice> => {
|
|
const response = await api.patch<Invoice>(`${BASE_URL}/${id}`, data);
|
|
return response.data;
|
|
},
|
|
|
|
// Delete invoice
|
|
delete: async (id: string): Promise<void> => {
|
|
await api.delete(`${BASE_URL}/${id}`);
|
|
},
|
|
|
|
// Validate invoice (change status from draft to validated)
|
|
validate: async (id: string): Promise<Invoice> => {
|
|
const response = await api.post<Invoice>(`${BASE_URL}/${id}/validate`);
|
|
return response.data;
|
|
},
|
|
|
|
// Send invoice
|
|
send: async (id: string): Promise<Invoice> => {
|
|
const response = await api.post<Invoice>(`${BASE_URL}/${id}/send`);
|
|
return response.data;
|
|
},
|
|
|
|
// Record payment
|
|
recordPayment: async (id: string, data: {
|
|
amount: number;
|
|
paymentMethod: string;
|
|
paymentReference?: string;
|
|
paymentDate?: string;
|
|
}): Promise<Invoice> => {
|
|
const response = await api.post<Invoice>(`${BASE_URL}/${id}/payment`, data);
|
|
return response.data;
|
|
},
|
|
|
|
// Cancel invoice
|
|
cancel: async (id: string, reason?: string): Promise<Invoice> => {
|
|
const response = await api.post<Invoice>(`${BASE_URL}/${id}/cancel`, { reason });
|
|
return response.data;
|
|
},
|
|
|
|
// Void invoice
|
|
void: async (id: string, reason?: string): Promise<Invoice> => {
|
|
const response = await api.post<Invoice>(`${BASE_URL}/${id}/void`, { reason });
|
|
return response.data;
|
|
},
|
|
|
|
// Get invoice stats
|
|
getStats: async (tenantId?: string): Promise<InvoiceStats> => {
|
|
const params = tenantId ? `?tenantId=${tenantId}` : '';
|
|
const response = await api.get<InvoiceStats>(`${BASE_URL}/stats${params}`);
|
|
return response.data;
|
|
},
|
|
|
|
// Generate PDF
|
|
generatePdf: async (id: string): Promise<Blob> => {
|
|
const response = await api.get(`${BASE_URL}/${id}/pdf`, {
|
|
responseType: 'blob',
|
|
});
|
|
return response.data;
|
|
},
|
|
|
|
// Download PDF
|
|
downloadPdf: async (id: string, fileName?: string): Promise<void> => {
|
|
const blob = await invoicesApi.generatePdf(id);
|
|
const url = window.URL.createObjectURL(blob);
|
|
const link = document.createElement('a');
|
|
link.href = url;
|
|
link.download = fileName || `invoice-${id}.pdf`;
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
document.body.removeChild(link);
|
|
window.URL.revokeObjectURL(url);
|
|
},
|
|
|
|
// Get sales invoices
|
|
getSales: async (filters?: Omit<InvoiceFilters, 'invoiceType'>): Promise<InvoicesResponse> => {
|
|
return invoicesApi.getAll({ ...filters, invoiceType: 'sale' });
|
|
},
|
|
|
|
// Get purchase invoices
|
|
getPurchases: async (filters?: Omit<InvoiceFilters, 'invoiceType'>): Promise<InvoicesResponse> => {
|
|
return invoicesApi.getAll({ ...filters, invoiceType: 'purchase' });
|
|
},
|
|
|
|
// Get draft invoices
|
|
getDrafts: async (filters?: Omit<InvoiceFilters, 'status'>): Promise<InvoicesResponse> => {
|
|
return invoicesApi.getAll({ ...filters, status: 'draft' });
|
|
},
|
|
|
|
// Get overdue invoices
|
|
getOverdue: async (filters?: Omit<InvoiceFilters, 'status'>): Promise<InvoicesResponse> => {
|
|
return invoicesApi.getAll({ ...filters, status: 'overdue' });
|
|
},
|
|
};
|