import { apiClient } from './client'; import type { ARAllocation, ARCreditNote } from '../types/api'; import type { BranchSelection } from '../store/branchStore'; export async function fetchCreditNotes(branchScope: BranchSelection, customerId?: number): Promise { const response = await apiClient.get('/ar/credit-notes', { params: { branchScope: branchScope === 'MAIN' ? 'MAIN' : String(branchScope), customerId }, }); return response.data; } export async function fetchCreditNote(id: number): Promise { const response = await apiClient.get(`/ar/credit-notes/${id}`); return response.data; } export interface CreditNoteLineInput { description: string; quantity: number; unitPrice: number; revenueGLAccountId: number; taxCodeId?: number; } export interface CreateCreditNoteInput { customerId: number; creditNoteDate: string; reference?: string; reason?: string; lines: CreditNoteLineInput[]; } export async function createCreditNote(input: CreateCreditNoteInput): Promise { const response = await apiClient.post('/ar/credit-notes', input); return response.data; } export async function postCreditNote(id: number): Promise { const response = await apiClient.post(`/ar/credit-notes/${id}/post`); return response.data; } export async function voidCreditNote(id: number): Promise { const response = await apiClient.post(`/ar/credit-notes/${id}/void`); return response.data; } export async function allocateCreditNote(id: number, allocations: { invoiceId: number; amount: number }[]): Promise { const response = await apiClient.post(`/ar/credit-notes/${id}/allocations`, { allocations }); return response.data; }