import { api } from '@/lib/api' import type { CreateExpenseCategoryRequest, CreateExpenseRequest, CreateExpenseVendorRequest, CreateRecurringExpenseRequest, ExpenseAction, ExpenseAttachmentResponse, ExpenseCategoryResponse, ExpensePaymentRequest, ExpenseResponse, ExpenseStatus, ExpenseVendorResponse, PagedExpenseVendorsResponse, PagedExpensesResponse, PagedRecurringExpensesResponse, RecurringExpenseResponse, RunDueResponse, UpdateExpenseCategoryRequest, UpdateExpenseDraftRequest, UpdateExpenseVendorRequest, UpdateRecurringExpenseRequest, } from '@/types/expenses' // Categories export const expenseCategories = { tree: () => api.get('/expense-categories').then(r => r.data), create: (body: CreateExpenseCategoryRequest) => api.post('/expense-categories', body).then(r => r.data), update: (id: string, body: UpdateExpenseCategoryRequest) => api.put(`/expense-categories/${id}`, body).then(r => r.data), } // Vendors export const expenseVendors = { list: (params?: { search?: string; page?: number; pageSize?: number }) => api.get('/expense-vendors', { params }).then(r => r.data), create: (body: CreateExpenseVendorRequest) => api.post('/expense-vendors', body).then(r => r.data), update: (id: string, body: UpdateExpenseVendorRequest) => api.put(`/expense-vendors/${id}`, body).then(r => r.data), } // Expenses export interface ExpenseListParams { status?: ExpenseStatus categoryId?: string warehouseId?: string from?: string to?: string page?: number pageSize?: number } export const expenses = { list: (params?: ExpenseListParams) => api.get('/expenses', { params }).then(r => r.data), get: (id: string) => api.get(`/expenses/${id}`).then(r => r.data), create: (body: CreateExpenseRequest, action: ExpenseAction = 'Draft') => api.post('/expenses', body, { params: { action } }).then(r => r.data), updateDraft: (id: string, body: UpdateExpenseDraftRequest) => api.put(`/expenses/${id}`, body).then(r => r.data), post: (id: string) => api.post(`/expenses/${id}/post`).then(r => r.data), pay: (id: string, body: ExpensePaymentRequest) => api.post(`/expenses/${id}/pay`, body).then(r => r.data), void: (id: string, reason?: string) => api.post(`/expenses/${id}/void`, { reason }).then(r => r.data), uploadAttachment: (expenseId: string, file: File) => { const form = new FormData() form.append('file', file) return api.post(`/expenses/${expenseId}/attachments`, form, { headers: { 'Content-Type': 'multipart/form-data' }, }).then(r => r.data) }, downloadAttachmentUrl: (attachmentId: string) => `${api.defaults.baseURL}/expense-attachments/${attachmentId}/download`, deleteAttachment: (attachmentId: string) => api.delete(`/expense-attachments/${attachmentId}`), } // Recurring export const recurringExpenses = { list: (params?: { page?: number; pageSize?: number }) => api.get('/recurring-expenses', { params }).then(r => r.data), create: (body: CreateRecurringExpenseRequest) => api.post('/recurring-expenses', body).then(r => r.data), update: (id: string, body: UpdateRecurringExpenseRequest) => api.put(`/recurring-expenses/${id}`, body).then(r => r.data), toggle: (id: string, paused: boolean) => api.post(`/recurring-expenses/${id}/toggle`, { paused }).then(r => r.data), runDue: () => api.post('/recurring-expenses/run-due').then(r => r.data), }