Files

138 lines
5.5 KiB
TypeScript

import { api } from '@/lib/api'
import type {
PaginatedResponse,
ShopPOResponse,
InvoiceResponse,
InvoiceStatus,
InvoiceVersionResponse,
PaymentStatus,
PaymentResponse,
PaymentMethod,
EntryMode,
ShopPOStatus,
OutstandingPaymentsResponse,
} from '@/types/api'
import type { PageParams } from './master'
// ─── Shop POs ─────────────────────────────────────────────────────────────────
export interface ShopPOItemBody {
itemId: string
quantity: number
unitPrice?: number | null
/** Free issue accepted from a scheme suggestion; priced 0 by the server. */
isFree?: boolean
schemeId?: string | null
}
export interface CreateShopPOBody {
storeId: string
items: ShopPOItemBody[]
raisedByUserId?: string | null
/** The one flat/slab discount scheme the user accepted. */
discountSchemeIds?: string[]
}
export interface UpdateShopPOItemsBody {
items: ShopPOItemBody[]
discountSchemeIds?: string[]
}
export interface ShopPOListParams extends PageParams {
agentId?: string
routeId?: string
regionId?: string
shopId?: string
confirmed?: boolean
status?: ShopPOStatus
}
export const shopPos = {
list: (params?: ShopPOListParams) =>
api.get<PaginatedResponse<ShopPOResponse>>('/shop-pos', { params }).then(r => r.data),
get: (id: string) =>
api.get<ShopPOResponse>(`/shop-pos/${id}`).then(r => r.data),
confirmedWithoutInvoice: (params?: { agentId?: string; routeId?: string; shopId?: string }) =>
api.get<ShopPOResponse[]>('/shop-pos/confirmed-without-invoice', { params }).then(r => r.data),
create: (body: CreateShopPOBody) =>
api.post<ShopPOResponse>('/shop-pos', body).then(r => r.data),
updateItems: (id: string, body: UpdateShopPOItemsBody) =>
api.put<ShopPOResponse>(`/shop-pos/${id}/items`, body).then(r => r.data),
confirm: (id: string, confirmedByUserId?: string | null) =>
api.post<ShopPOResponse>(`/shop-pos/${id}/confirm`, { confirmedByUserId }).then(r => r.data),
confirmAndCreateInvoice: (id: string, confirmedByUserId?: string | null) =>
api.post<ShopPOResponse>(`/shop-pos/${id}/confirm-and-create-invoice`, { confirmedByUserId }).then(r => r.data),
cancel: (id: string, reason: string, cancelledByUserId?: string | null) =>
api.post<ShopPOResponse>(`/shop-pos/${id}/cancel`, { reason, cancelledByUserId }).then(r => r.data),
}
// ─── Invoices ─────────────────────────────────────────────────────────────────
export interface InvoiceListParams extends PageParams {
agentId?: string
routeId?: string
shopId?: string
status?: InvoiceStatus
paymentStatus?: PaymentStatus
}
export const invoices = {
list: (params?: InvoiceListParams) =>
api.get<PaginatedResponse<InvoiceResponse>>('/invoices', { params }).then(r => r.data),
outstandingSummary: () =>
api.get<OutstandingPaymentsResponse>('/invoices/outstanding-summary').then(r => r.data),
get: (id: string) =>
api.get<InvoiceResponse>(`/invoices/${id}`).then(r => r.data),
create: (shopPOId: string, createdByUserId?: string | null) =>
api.post<InvoiceResponse>('/invoices', { shopPOId, createdByUserId }).then(r => r.data),
print: (id: string, printedByUserId?: string | null) =>
api.post<InvoiceResponse>(`/invoices/${id}/print`, { printedByUserId }).then(r => r.data),
finalize: (id: string, finalizedByUserId?: string | null, adjustments?: { invoiceItemId: string; adjustedQuantity: number; isRemoved?: boolean }[]) =>
api.post<InvoiceResponse>(`/invoices/${id}/finalize`, { finalizedByUserId, adjustments }).then(r => r.data),
updateItems: (id: string, items: { invoiceItemId?: string; itemId?: string; quantity: number; unitPrice?: number | null; isRemoved?: boolean }[], editedByUserId?: string | null, discountSchemeIds?: string[]) =>
api.put<InvoiceResponse>(`/invoices/${id}/items`, { items, editedByUserId, discountSchemeIds }).then(r => r.data),
/** What the discount and total would be for these unsaved lines (draft editor preview). */
previewTotals: (id: string, items: { itemId: string; quantity: number; unitPrice: number; isFree?: boolean; isRemoved?: boolean; schemeId?: string | null }[], discountSchemeIds?: string[]) =>
api.post<{ discountAmount: number; totalAmount: number; freeEntitlements: { schemeId: string; quantity: number }[] }>(`/invoices/${id}/preview-totals`, { items, discountSchemeIds }).then(r => r.data),
versions: (id: string) =>
api.get<InvoiceVersionResponse[]>(`/invoices/${id}/versions`).then(r => r.data),
}
// ─── Payments ─────────────────────────────────────────────────────────────────
export interface PaymentBody {
amount: number
method: PaymentMethod
entryMode?: EntryMode
bankAccountId?: string | null
chequeNumber?: string | null
chequeDate?: string | null
drawerBankName?: string | null
accountHolderName?: string | null
collectedByUserId?: string | null
}
export const payments = {
listByInvoice: (invoiceId: string) =>
api.get<PaymentResponse[]>(`/invoices/${invoiceId}/payments`).then(r => r.data),
create: (invoiceId: string, body: PaymentBody) =>
api.post<PaymentResponse>(`/invoices/${invoiceId}/payments`, body).then(r => r.data),
}