174 lines
4.8 KiB
TypeScript
174 lines
4.8 KiB
TypeScript
/**
|
|
* API Client para Clientes y Proveedores
|
|
* Gestiona las operaciones CRUD para clientes y proveedores
|
|
*/
|
|
import { api } from '$lib/api';
|
|
|
|
export interface ClientProviderAddress {
|
|
id?: number;
|
|
streets?: string | null;
|
|
neighborhood?: string | null;
|
|
city?: string | null;
|
|
state?: string | null;
|
|
country?: string | null;
|
|
zip_code?: string | null;
|
|
client_id?: number;
|
|
interior_number?: string | null;
|
|
exterior_number?: string | null;
|
|
municipality?: string | null;
|
|
}
|
|
|
|
export interface ClientProviderPrograms {
|
|
id?: number;
|
|
program_code?: string | null;
|
|
authorization_date?: string | null;
|
|
client_id?: number;
|
|
}
|
|
|
|
export interface ClientProvider {
|
|
id: number;
|
|
rfc: string;
|
|
name: string;
|
|
curp?: string | null;
|
|
residence_country?: string | null;
|
|
domicile_fiscal?: string | null;
|
|
foreign_tax_id?: string | null;
|
|
client_or_provider?: string | null;
|
|
is_active?: boolean;
|
|
tenant_id: number;
|
|
address?: ClientProviderAddress | null;
|
|
programs?: ClientProviderPrograms | null;
|
|
}
|
|
|
|
export interface ClientProviderBasic {
|
|
id: number;
|
|
rfc: string;
|
|
name: string;
|
|
curp?: string | null;
|
|
residence_country?: string | null;
|
|
domicile_fiscal?: string | null;
|
|
foreign_tax_id?: string | null;
|
|
client_or_provider?: string | null;
|
|
is_active?: boolean;
|
|
tenant_id: number;
|
|
}
|
|
|
|
export interface ClientProviderListResponse {
|
|
items: ClientProvider[];
|
|
total: number;
|
|
page: number;
|
|
page_size: number;
|
|
}
|
|
|
|
export interface CreateClientProviderData {
|
|
rfc: string;
|
|
name: string;
|
|
curp?: string | null;
|
|
residence_country?: string | null;
|
|
domicile_fiscal?: string | null;
|
|
foreign_tax_id?: string | null;
|
|
client_or_provider?: string | null;
|
|
is_active?: boolean;
|
|
address?: Omit<ClientProviderAddress, 'id' | 'client_id'> | null;
|
|
programs?: Omit<ClientProviderPrograms, 'id' | 'client_id'> | null;
|
|
}
|
|
|
|
export interface UpdateClientProviderData {
|
|
rfc?: string;
|
|
name?: string;
|
|
curp?: string | null;
|
|
residence_country?: string | null;
|
|
domicile_fiscal?: string | null;
|
|
foreign_tax_id?: string | null;
|
|
client_or_provider?: string | null;
|
|
is_active?: boolean;
|
|
address?: Partial<ClientProviderAddress> | null;
|
|
programs?: Partial<ClientProviderPrograms> | null;
|
|
}
|
|
|
|
/**
|
|
* API para Clientes y Proveedores
|
|
*/
|
|
export const clientsProvidersApi = {
|
|
/**
|
|
* Lista todos los clientes y proveedores con paginación
|
|
* @param companyId - ID de la compañía
|
|
* @param page - Número de página (por defecto 1)
|
|
* @param pageSize - Tamaño de página (por defecto 50)
|
|
* @param filters - Filtros opcionales
|
|
*/
|
|
list: (companyId: number, page = 1, pageSize = 50, filters?: Record<string, any>) => {
|
|
const params = new URLSearchParams({
|
|
company_id: companyId.toString(),
|
|
page: page.toString(),
|
|
page_size: pageSize.toString()
|
|
});
|
|
|
|
if (filters) {
|
|
Object.entries(filters).forEach(([key, value]) => {
|
|
if (value !== undefined && value !== null && value !== '') {
|
|
params.append(key, value.toString());
|
|
}
|
|
});
|
|
}
|
|
|
|
return api.get<ClientProviderListResponse>(
|
|
`/v1/a76/clients-providers/?${params.toString()}`
|
|
);
|
|
},
|
|
|
|
/**
|
|
* Obtiene un cliente/proveedor por ID
|
|
* @param id - ID del cliente/proveedor
|
|
* @param companyId - ID de la compañía
|
|
*/
|
|
get: (id: number, companyId: number) =>
|
|
api.get<ClientProvider>(`/v1/a76/clients-providers/${id}?company_id=${companyId}`),
|
|
|
|
/**
|
|
* Obtiene información básica de un cliente/proveedor
|
|
* @param id - ID del cliente/proveedor
|
|
* @param companyId - ID de la compañía
|
|
*/
|
|
getBasic: (id: number, companyId: number) =>
|
|
api.get<ClientProviderBasic>(
|
|
`/v1/a76/clients-providers/${id}/basic?company_id=${companyId}`
|
|
),
|
|
|
|
/**
|
|
* Crea un nuevo cliente/proveedor
|
|
* @param companyId - ID de la compañía
|
|
* @param data - Datos del cliente/proveedor a crear
|
|
*/
|
|
create: (companyId: number, data: CreateClientProviderData) =>
|
|
api.post<ClientProvider>(`/v1/a76/clients-providers/?company_id=${companyId}`, data),
|
|
|
|
/**
|
|
* Actualiza un cliente/proveedor existente
|
|
* @param id - ID del cliente/proveedor a actualizar
|
|
* @param companyId - ID de la compañía
|
|
* @param data - Datos a actualizar
|
|
*/
|
|
update: (id: number, companyId: number, data: UpdateClientProviderData) =>
|
|
api.patch<ClientProvider>(`/v1/a76/clients-providers/${id}?company_id=${companyId}`, data),
|
|
|
|
/**
|
|
* Alterna el estado activo/inactivo de un cliente/proveedor
|
|
* @param id - ID del cliente/proveedor
|
|
* @param companyId - ID de la compañía
|
|
*/
|
|
toggleStatus: (id: number, companyId: number) =>
|
|
api.put<ClientProvider>(
|
|
`/v1/a76/clients-providers/${id}/toggle-status/?company_id=${companyId}`,
|
|
{}
|
|
),
|
|
|
|
/**
|
|
* Elimina un cliente/proveedor
|
|
* @param id - ID del cliente/proveedor a eliminar
|
|
* @param companyId - ID de la compañía
|
|
*/
|
|
delete: (id: number, companyId: number) =>
|
|
api.delete(`/v1/a76/clients-providers/${id}?company_id=${companyId}`)
|
|
};
|