export interface FinancialCategory { id: string; tenant_id: string; name: string; type: 'income' | 'expense'; color: string; is_active: boolean; } export interface BankAccount { id: string; tenant_id: string; name: string; bank_name: string; initial_balance: number; current_balance: number; is_active: boolean; } export interface Entity { id: string; tenant_id: string; name: string; document: string; email: string; phone: string; type: 'customer' | 'supplier' | 'both'; status: string; address: string; city: string; state: string; zip: string; notes: string; } export interface FinancialTransaction { id: string; tenant_id: string; account_id?: string; category_id?: string; entity_id?: string; crm_customer_id?: string; company_id?: string; description: string; amount: number; type: 'income' | 'expense'; status: 'pending' | 'paid' | 'cancelled'; due_date?: string; payment_date?: string; payment_method?: string; attachments: string[]; } export interface Product { id: string; tenant_id: string; name: string; sku: string; description: string; price: number; cost_price: number; type: 'product' | 'service'; stock_quantity: number; is_active: boolean; } export interface Order { id: string; tenant_id: string; customer_id?: string; entity_id?: string; status: 'draft' | 'confirmed' | 'completed' | 'cancelled'; total_amount: number; notes: string; created_at: string; } export interface OrderItem { id: string; order_id: string; product_id: string; quantity: number; unit_price: number; total_price: number; } // API Services const BASE_URL = '/api/erp'; async function fetchWithAuth(url: string, options: RequestInit = {}) { const token = typeof window !== 'undefined' ? localStorage.getItem('token') : null; const response = await fetch(url, { cache: 'no-store', ...options, headers: { ...options.headers, 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json', }, }); if (!response.ok) { throw new Error(`API error: ${response.status}`); } // Handle empty responses (like 200 OK with no body or 204 No Content) const contentType = response.headers.get('content-type'); if (!contentType || !contentType.includes('application/json')) { return { status: 'ok' }; } const text = await response.text(); if (!text) return null; try { const data = JSON.parse(text); return data === null ? [] : data; } catch (e) { return null; } } export const erpApi = { // Finance getFinancialCategories: () => fetchWithAuth(`${BASE_URL}/finance/categories`), createFinancialCategory: (data: Partial) => fetchWithAuth(`${BASE_URL}/finance/categories`, { method: 'POST', body: JSON.stringify(data) }), getBankAccounts: () => fetchWithAuth(`${BASE_URL}/finance/accounts`), createBankAccount: (data: Partial) => fetchWithAuth(`${BASE_URL}/finance/accounts`, { method: 'POST', body: JSON.stringify(data) }), updateBankAccount: (id: string, data: Partial) => fetchWithAuth(`${BASE_URL}/finance/accounts/${id}`, { method: 'PUT', body: JSON.stringify(data) }), deleteBankAccount: (id: string) => fetchWithAuth(`${BASE_URL}/finance/accounts/${id}`, { method: 'DELETE' }), getTransactions: () => fetchWithAuth(`${BASE_URL}/finance/transactions`), createTransaction: (data: Partial) => fetchWithAuth(`${BASE_URL}/finance/transactions`, { method: 'POST', body: JSON.stringify(data) }), updateTransaction: (id: string, data: Partial) => fetchWithAuth(`${BASE_URL}/finance/transactions/${id}`, { method: 'PUT', body: JSON.stringify(data) }), deleteTransaction: (id: string) => fetchWithAuth(`${BASE_URL}/finance/transactions/${id}`, { method: 'DELETE' }), // Products getProducts: () => fetchWithAuth(`${BASE_URL}/products`), createProduct: (data: Partial) => fetchWithAuth(`${BASE_URL}/products`, { method: 'POST', body: JSON.stringify(data) }), updateProduct: (id: string, data: Partial) => fetchWithAuth(`${BASE_URL}/products/${id}`, { method: 'PUT', body: JSON.stringify(data) }), deleteProduct: (id: string) => fetchWithAuth(`${BASE_URL}/products/${id}`, { method: 'DELETE' }), // Orders getOrders: () => fetchWithAuth(`${BASE_URL}/orders`), createOrder: (data: { order: Partial, items: Partial[] }) => fetchWithAuth(`${BASE_URL}/orders`, { method: 'POST', body: JSON.stringify(data) }), deleteOrder: (id: string) => fetchWithAuth(`${BASE_URL}/orders/${id}`, { method: 'DELETE' }), // Entities getEntities: (type?: string) => fetchWithAuth(`${BASE_URL}/entities${type ? `?type=${type}` : ''}`), createEntity: (data: Partial) => fetchWithAuth(`${BASE_URL}/entities`, { method: 'POST', body: JSON.stringify(data) }), updateEntity: (id: string, data: Partial) => fetchWithAuth(`${BASE_URL}/entities/${id}`, { method: 'PUT', body: JSON.stringify(data) }), deleteEntity: (id: string) => fetchWithAuth(`${BASE_URL}/entities/${id}`, { method: 'DELETE' }), };