273 lines
5.9 KiB
TypeScript
273 lines
5.9 KiB
TypeScript
import apiClient from "./api/client";
|
|
|
|
export interface PaginatedResponse<T> {
|
|
data: T[];
|
|
meta: {
|
|
total: number;
|
|
page: number;
|
|
limit: number;
|
|
totalPages: number;
|
|
hasNextPage: boolean;
|
|
hasPreviousPage: boolean;
|
|
};
|
|
}
|
|
|
|
export interface InvoiceItem {
|
|
id: string;
|
|
description: string;
|
|
quantity: number;
|
|
unitPrice: number;
|
|
total: number;
|
|
}
|
|
|
|
export interface Invoice {
|
|
id: string;
|
|
invoiceNumber: string;
|
|
customerName: string;
|
|
customerEmail: string;
|
|
customerPhone: string;
|
|
amount: number;
|
|
currency: string;
|
|
type: "SALES" | "PURCHASE";
|
|
status: "DRAFT" | "PENDING" | "PAID" | "OVERDUE" | "CANCELLED";
|
|
issueDate: string;
|
|
dueDate: string;
|
|
paidDate?: string;
|
|
description: string;
|
|
notes: string;
|
|
taxAmount: number;
|
|
discountAmount: number;
|
|
isScanned: boolean;
|
|
scannedData?: any;
|
|
userId: string;
|
|
items: InvoiceItem[];
|
|
createdAt: string;
|
|
updatedAt: string;
|
|
}
|
|
|
|
export interface Proforma {
|
|
id: string;
|
|
proformaNumber: string;
|
|
customerName: string;
|
|
customerEmail: string;
|
|
customerPhone: string;
|
|
amount: number;
|
|
currency: string;
|
|
issueDate: string;
|
|
dueDate: string;
|
|
description: string;
|
|
notes: string;
|
|
taxAmount: number;
|
|
discountAmount: number;
|
|
pdfPath: string;
|
|
userId: string;
|
|
items: InvoiceItem[];
|
|
createdAt: string;
|
|
updatedAt: string;
|
|
}
|
|
|
|
export interface ProformaRequestItem {
|
|
id?: string;
|
|
itemName: string;
|
|
itemDescription?: string;
|
|
quantity: number;
|
|
unitOfMeasure: string;
|
|
technicalSpecifications?: Record<string, any>;
|
|
createdAt?: string;
|
|
}
|
|
|
|
export interface ProformaRequest {
|
|
id: string;
|
|
requesterId: string;
|
|
title: string;
|
|
description: string;
|
|
category: "EQUIPMENT" | "SERVICE" | "MIXED";
|
|
status:
|
|
| "DRAFT"
|
|
| "OPEN"
|
|
| "UNDER_REVIEW"
|
|
| "REVISION_REQUESTED"
|
|
| "CLOSED"
|
|
| "CANCELLED";
|
|
submissionDeadline: string;
|
|
allowRevisions: boolean;
|
|
paymentTerms: string;
|
|
incoterms?: string;
|
|
taxIncluded: boolean;
|
|
discountStructure?: string;
|
|
validityPeriod?: number;
|
|
attachments?: { name: string; url: string }[];
|
|
items: ProformaRequestItem[];
|
|
createdAt: string;
|
|
updatedAt: string;
|
|
}
|
|
|
|
export interface InvoiceFilters {
|
|
page?: number;
|
|
limit?: number;
|
|
status?: string;
|
|
type?: string;
|
|
startDate?: string;
|
|
endDate?: string;
|
|
customerName?: string;
|
|
invoiceNumber?: string;
|
|
search?: string;
|
|
}
|
|
|
|
export interface ProformaFilters {
|
|
page?: number;
|
|
limit?: number;
|
|
startDate?: string;
|
|
endDate?: string;
|
|
customerName?: string;
|
|
proformaNumber?: string;
|
|
search?: string;
|
|
}
|
|
|
|
export interface ProformaRequestFilters {
|
|
page?: number;
|
|
limit?: number;
|
|
status?: string;
|
|
category?: string;
|
|
search?: string;
|
|
deadlineFrom?: string;
|
|
deadlineTo?: string;
|
|
}
|
|
|
|
class InvoiceService {
|
|
/**
|
|
* Get all invoices
|
|
*/
|
|
async getInvoices(
|
|
filters: InvoiceFilters = {},
|
|
): Promise<PaginatedResponse<Invoice>> {
|
|
const response = await apiClient.get<PaginatedResponse<Invoice>>(
|
|
"/invoices",
|
|
{
|
|
params: filters,
|
|
},
|
|
);
|
|
return response.data;
|
|
}
|
|
|
|
/**
|
|
* Create a new invoice
|
|
*/
|
|
async createInvoice(data: any): Promise<Invoice> {
|
|
const response = await apiClient.post<Invoice>("/invoices", data);
|
|
return response.data;
|
|
}
|
|
|
|
/**
|
|
* Update an existing invoice
|
|
*/
|
|
async updateInvoice(id: string, data: any): Promise<Invoice> {
|
|
const response = await apiClient.put<Invoice>(`/invoices/${id}`, data);
|
|
return response.data;
|
|
}
|
|
|
|
/**
|
|
* Delete an invoice
|
|
*/
|
|
async deleteInvoice(id: string): Promise<void> {
|
|
await apiClient.delete(`/invoices/${id}`);
|
|
}
|
|
|
|
/**
|
|
* Get all proforma invoices
|
|
*/
|
|
async getProformas(
|
|
filters: ProformaFilters = {},
|
|
): Promise<PaginatedResponse<Proforma>> {
|
|
const response = await apiClient.get<PaginatedResponse<Proforma>>(
|
|
"/proforma",
|
|
{
|
|
params: filters,
|
|
},
|
|
);
|
|
return response.data;
|
|
}
|
|
|
|
/**
|
|
* Create a new proforma invoice
|
|
*/
|
|
async createProforma(data: any): Promise<Proforma> {
|
|
const response = await apiClient.post<Proforma>("/proforma", data);
|
|
return response.data;
|
|
}
|
|
|
|
/**
|
|
* Update an existing proforma invoice
|
|
*/
|
|
async updateProforma(id: string, data: any): Promise<Proforma> {
|
|
const response = await apiClient.put<Proforma>(`/proforma/${id}`, data);
|
|
return response.data;
|
|
}
|
|
|
|
/**
|
|
* Delete a proforma invoice
|
|
*/
|
|
async deleteProforma(id: string): Promise<void> {
|
|
await apiClient.delete(`/proforma/${id}`);
|
|
}
|
|
|
|
/**
|
|
* Get all proforma requests (admin view)
|
|
*/
|
|
async getProformaRequests(
|
|
filters: ProformaRequestFilters = {},
|
|
): Promise<PaginatedResponse<ProformaRequest>> {
|
|
const response = await apiClient.get<PaginatedResponse<ProformaRequest>>(
|
|
"/admin/proforma-requests",
|
|
{
|
|
params: filters,
|
|
},
|
|
);
|
|
return response.data;
|
|
}
|
|
|
|
/**
|
|
* Get proforma request details (admin view)
|
|
*/
|
|
async getProformaRequestDetails(id: string): Promise<ProformaRequest> {
|
|
const response = await apiClient.get<ProformaRequest>(
|
|
`/admin/proforma-requests/${id}`,
|
|
);
|
|
return response.data;
|
|
}
|
|
/**
|
|
* Create a new proforma request
|
|
*/
|
|
async createProformaRequest(data: any): Promise<ProformaRequest> {
|
|
const response = await apiClient.post<ProformaRequest>(
|
|
"/proforma-requests",
|
|
data,
|
|
);
|
|
return response.data;
|
|
}
|
|
/**
|
|
* Update an existing proforma request
|
|
*/
|
|
async updateProformaRequest(id: string, data: any): Promise<ProformaRequest> {
|
|
const response = await apiClient.put<ProformaRequest>(
|
|
`/proforma-requests/${id}`,
|
|
data,
|
|
);
|
|
return response.data;
|
|
}
|
|
/**
|
|
* Close a proforma request
|
|
*/
|
|
async closeProformaRequest(id: string): Promise<void> {
|
|
await apiClient.post(`/proforma-requests/${id}/close`);
|
|
}
|
|
/**
|
|
* Cancel a proforma request
|
|
*/
|
|
async cancelProformaRequest(id: string): Promise<void> {
|
|
await apiClient.post(`/proforma-requests/${id}/cancel`);
|
|
}
|
|
}
|
|
|
|
export const invoiceService = new InvoiceService();
|