import { Prisma } from '@prisma/client';
import type { GrnCreateInput, PurchaseOrderCreateInput, PurchaseReturnInput, SupplierInvoiceInput, SupplierPaymentInput } from '@ali-ismail/contracts';
import { z } from 'zod';
import { purchaseOrderUpdateSchema } from '@ali-ismail/contracts';
import { PrismaService } from '../prisma/prisma.service';
import { AuditService } from '../audit/audit.service';
import { NumberingService } from '../numbering/numbering.service';
import { BusinessEventsService } from '../notifications/events.service';
import { StockEngineService } from '../inventory/stock-engine.service';
import { CoaService } from '../accounting/coa.service';
import { PostingService } from '../accounting/posting.service';
export declare class PurchasingService {
    private readonly prisma;
    private readonly audit;
    private readonly numbering;
    private readonly events;
    private readonly engine;
    private readonly coa;
    private readonly posting;
    constructor(prisma: PrismaService, audit: AuditService, numbering: NumberingService, events: BusinessEventsService, engine: StockEngineService, coa: CoaService, posting: PostingService);
    /**
     * Modernization: resolve the exact inventory variant for a set of PO/return
     * lines. Rules:
     *  - When a line supplies `variantId`, the variant MUST exist, belong to this
     *    organization + the line's item, and be active. A different variant is
     *    never substituted.
     *  - When a line omits `variantId`, the item's base variant is resolved
     *    (base-only items). If no active base variant exists the line is rejected.
     * Returns a map keyed by `${itemId}:${variantId ?? ''}` → resolved variantId.
     */
    private resolveVariants;
    private computeTotals;
    createPo(organizationId: string, input: PurchaseOrderCreateInput, actorUserId: string): Promise<({
        supplier: {
            name: string;
        };
        lines: ({
            variant: {
                id: string;
                name: string | null;
                sku: string | null;
                variantCode: string;
            };
        } & {
            organizationId: string;
            id: string;
            itemId: string;
            inventoryVariantId: string;
            locationId: string | null;
            uom: string;
            notes: string | null;
            unitCost: Prisma.Decimal;
            discountAmount: Prisma.Decimal;
            itemName: string;
            lineTotal: Prisma.Decimal;
            sortOrder: number;
            orderedQty: Prisma.Decimal;
            receivedQty: Prisma.Decimal;
            poId: string;
        })[];
    } & {
        organizationId: string;
        id: string;
        updatedAt: Date;
        notes: string | null;
        createdAt: Date;
        status: import("@prisma/client").$Enums.PurchaseOrderStatus;
        createdById: string;
        subtotal: Prisma.Decimal;
        discountAmount: Prisma.Decimal;
        idempotencyKey: string | null;
        orderDate: Date;
        poNumber: string;
        supplierId: string;
        expectedDate: Date | null;
        totalAmount: Prisma.Decimal;
        approvedById: string | null;
        approvedAt: Date | null;
        approvalNotes: string | null;
        cancelledAt: Date | null;
        cancelledById: string | null;
        cancelReason: string | null;
        closedAt: Date | null;
        overdueAlertAt: Date | null;
    }) | {
        replayed: boolean;
        lines: {
            organizationId: string;
            id: string;
            itemId: string;
            inventoryVariantId: string;
            locationId: string | null;
            uom: string;
            notes: string | null;
            unitCost: Prisma.Decimal;
            discountAmount: Prisma.Decimal;
            itemName: string;
            lineTotal: Prisma.Decimal;
            sortOrder: number;
            orderedQty: Prisma.Decimal;
            receivedQty: Prisma.Decimal;
            poId: string;
        }[];
        organizationId: string;
        id: string;
        updatedAt: Date;
        notes: string | null;
        createdAt: Date;
        status: import("@prisma/client").$Enums.PurchaseOrderStatus;
        createdById: string;
        subtotal: Prisma.Decimal;
        discountAmount: Prisma.Decimal;
        idempotencyKey: string | null;
        orderDate: Date;
        poNumber: string;
        supplierId: string;
        expectedDate: Date | null;
        totalAmount: Prisma.Decimal;
        approvedById: string | null;
        approvedAt: Date | null;
        approvalNotes: string | null;
        cancelledAt: Date | null;
        cancelledById: string | null;
        cancelReason: string | null;
        closedAt: Date | null;
        overdueAlertAt: Date | null;
    }>;
    updatePo(organizationId: string, poId: string, input: z.infer<typeof purchaseOrderUpdateSchema>, actorUserId: string): Promise<{
        lines: ({
            variant: {
                id: string;
                name: string | null;
                sku: string | null;
                variantCode: string;
            };
        } & {
            organizationId: string;
            id: string;
            itemId: string;
            inventoryVariantId: string;
            locationId: string | null;
            uom: string;
            notes: string | null;
            unitCost: Prisma.Decimal;
            discountAmount: Prisma.Decimal;
            itemName: string;
            lineTotal: Prisma.Decimal;
            sortOrder: number;
            orderedQty: Prisma.Decimal;
            receivedQty: Prisma.Decimal;
            poId: string;
        })[];
    } & {
        organizationId: string;
        id: string;
        updatedAt: Date;
        notes: string | null;
        createdAt: Date;
        status: import("@prisma/client").$Enums.PurchaseOrderStatus;
        createdById: string;
        subtotal: Prisma.Decimal;
        discountAmount: Prisma.Decimal;
        idempotencyKey: string | null;
        orderDate: Date;
        poNumber: string;
        supplierId: string;
        expectedDate: Date | null;
        totalAmount: Prisma.Decimal;
        approvedById: string | null;
        approvedAt: Date | null;
        approvalNotes: string | null;
        cancelledAt: Date | null;
        cancelledById: string | null;
        cancelReason: string | null;
        closedAt: Date | null;
        overdueAlertAt: Date | null;
    }>;
    private lockPo;
    submitPo(organizationId: string, poId: string, actorUserId: string): Promise<{
        organizationId: string;
        id: string;
        updatedAt: Date;
        notes: string | null;
        createdAt: Date;
        status: import("@prisma/client").$Enums.PurchaseOrderStatus;
        createdById: string;
        subtotal: Prisma.Decimal;
        discountAmount: Prisma.Decimal;
        idempotencyKey: string | null;
        orderDate: Date;
        poNumber: string;
        supplierId: string;
        expectedDate: Date | null;
        totalAmount: Prisma.Decimal;
        approvedById: string | null;
        approvedAt: Date | null;
        approvalNotes: string | null;
        cancelledAt: Date | null;
        cancelledById: string | null;
        cancelReason: string | null;
        closedAt: Date | null;
        overdueAlertAt: Date | null;
    }>;
    approvePo(organizationId: string, poId: string, actorUserId: string, notes?: string | null): Promise<{
        organizationId: string;
        id: string;
        updatedAt: Date;
        notes: string | null;
        createdAt: Date;
        status: import("@prisma/client").$Enums.PurchaseOrderStatus;
        createdById: string;
        subtotal: Prisma.Decimal;
        discountAmount: Prisma.Decimal;
        idempotencyKey: string | null;
        orderDate: Date;
        poNumber: string;
        supplierId: string;
        expectedDate: Date | null;
        totalAmount: Prisma.Decimal;
        approvedById: string | null;
        approvedAt: Date | null;
        approvalNotes: string | null;
        cancelledAt: Date | null;
        cancelledById: string | null;
        cancelReason: string | null;
        closedAt: Date | null;
        overdueAlertAt: Date | null;
    }>;
    cancelPo(organizationId: string, poId: string, actorUserId: string, reason: string): Promise<{
        organizationId: string;
        id: string;
        updatedAt: Date;
        notes: string | null;
        createdAt: Date;
        status: import("@prisma/client").$Enums.PurchaseOrderStatus;
        createdById: string;
        subtotal: Prisma.Decimal;
        discountAmount: Prisma.Decimal;
        idempotencyKey: string | null;
        orderDate: Date;
        poNumber: string;
        supplierId: string;
        expectedDate: Date | null;
        totalAmount: Prisma.Decimal;
        approvedById: string | null;
        approvedAt: Date | null;
        approvalNotes: string | null;
        cancelledAt: Date | null;
        cancelledById: string | null;
        cancelReason: string | null;
        closedAt: Date | null;
        overdueAlertAt: Date | null;
    }>;
    listPos(organizationId: string, opts: {
        status?: string;
        supplierId?: string;
        q?: string;
        page: number;
        pageSize: number;
    }): Promise<{
        total: number;
        page: number;
        pageSize: number;
        items: ({
            _count: {
                goodsReceipts: number;
                lines: number;
            };
            supplier: {
                name: string;
            };
        } & {
            organizationId: string;
            id: string;
            updatedAt: Date;
            notes: string | null;
            createdAt: Date;
            status: import("@prisma/client").$Enums.PurchaseOrderStatus;
            createdById: string;
            subtotal: Prisma.Decimal;
            discountAmount: Prisma.Decimal;
            idempotencyKey: string | null;
            orderDate: Date;
            poNumber: string;
            supplierId: string;
            expectedDate: Date | null;
            totalAmount: Prisma.Decimal;
            approvedById: string | null;
            approvedAt: Date | null;
            approvalNotes: string | null;
            cancelledAt: Date | null;
            cancelledById: string | null;
            cancelReason: string | null;
            closedAt: Date | null;
            overdueAlertAt: Date | null;
        })[];
    }>;
    private flagOverduePos;
    getPo(organizationId: string, poId: string): Promise<{
        goodsReceipts: ({
            lines: ({
                variant: {
                    id: string;
                    name: string | null;
                    sku: string | null;
                    variantCode: string;
                };
            } & {
                organizationId: string;
                id: string;
                itemId: string;
                inventoryVariantId: string;
                notes: string | null;
                unitCost: Prisma.Decimal;
                lineTotal: Prisma.Decimal;
                grnId: string;
                poLineId: string;
                deliveredQty: Prisma.Decimal;
                acceptedQty: Prisma.Decimal;
                rejectedQty: Prisma.Decimal;
                batchRef: string | null;
            })[];
        } & {
            organizationId: string;
            id: string;
            locationId: string;
            notes: string | null;
            createdAt: Date;
            idempotencyKey: string | null;
            supplierId: string;
            poId: string;
            grnNumber: string;
            receiveDate: Date;
            challanRef: string | null;
            receivedById: string;
        })[];
        supplier: {
            organizationId: string;
            archivedAt: Date | null;
            isActive: boolean;
            id: string;
            updatedAt: Date;
            code: string;
            name: string;
            notes: string | null;
            createdAt: Date;
            contactPerson: string | null;
            mobile: string | null;
            altMobile: string | null;
            whatsapp: string | null;
            email: string | null;
            address: string | null;
            city: string | null;
            taxNumber: string | null;
            paymentTermsDays: number | null;
            isPreferred: boolean;
        };
        lines: ({
            item: {
                code: string;
                name: string;
                uom: string;
            };
            variant: {
                id: string;
                name: string | null;
                sku: string | null;
                variantCode: string;
                isBase: boolean;
            };
        } & {
            organizationId: string;
            id: string;
            itemId: string;
            inventoryVariantId: string;
            locationId: string | null;
            uom: string;
            notes: string | null;
            unitCost: Prisma.Decimal;
            discountAmount: Prisma.Decimal;
            itemName: string;
            lineTotal: Prisma.Decimal;
            sortOrder: number;
            orderedQty: Prisma.Decimal;
            receivedQty: Prisma.Decimal;
            poId: string;
        })[];
    } & {
        organizationId: string;
        id: string;
        updatedAt: Date;
        notes: string | null;
        createdAt: Date;
        status: import("@prisma/client").$Enums.PurchaseOrderStatus;
        createdById: string;
        subtotal: Prisma.Decimal;
        discountAmount: Prisma.Decimal;
        idempotencyKey: string | null;
        orderDate: Date;
        poNumber: string;
        supplierId: string;
        expectedDate: Date | null;
        totalAmount: Prisma.Decimal;
        approvedById: string | null;
        approvedAt: Date | null;
        approvalNotes: string | null;
        cancelledAt: Date | null;
        cancelledById: string | null;
        cancelReason: string | null;
        closedAt: Date | null;
        overdueAlertAt: Date | null;
    }>;
    /** Supplier-safe PO document. Never reuse the operational detail DTO here. */
    poPrintDto(organizationId: string, poId: string): Promise<{
        poNumber: string;
        supplier: {
            name: string;
        };
        orderDate: Date;
        expectedDate: Date | null;
        status: import("@prisma/client").$Enums.PurchaseOrderStatus;
        totalAmount: string;
        lines: {
            itemName: string;
            uom: string;
            qty: string;
            unitCost: string;
            discount: string;
            lineTotal: string;
        }[];
    }>;
    createGrn(organizationId: string, input: GrnCreateInput, actorUserId: string, actorCreatedPoGuard?: boolean): Promise<({
        lines: ({
            variant: {
                id: string;
                name: string | null;
                sku: string | null;
                variantCode: string;
            };
        } & {
            organizationId: string;
            id: string;
            itemId: string;
            inventoryVariantId: string;
            notes: string | null;
            unitCost: Prisma.Decimal;
            lineTotal: Prisma.Decimal;
            grnId: string;
            poLineId: string;
            deliveredQty: Prisma.Decimal;
            acceptedQty: Prisma.Decimal;
            rejectedQty: Prisma.Decimal;
            batchRef: string | null;
        })[];
    } & {
        organizationId: string;
        id: string;
        locationId: string;
        notes: string | null;
        createdAt: Date;
        idempotencyKey: string | null;
        supplierId: string;
        poId: string;
        grnNumber: string;
        receiveDate: Date;
        challanRef: string | null;
        receivedById: string;
    }) | {
        replayed: boolean;
        lines: {
            organizationId: string;
            id: string;
            itemId: string;
            inventoryVariantId: string;
            notes: string | null;
            unitCost: Prisma.Decimal;
            lineTotal: Prisma.Decimal;
            grnId: string;
            poLineId: string;
            deliveredQty: Prisma.Decimal;
            acceptedQty: Prisma.Decimal;
            rejectedQty: Prisma.Decimal;
            batchRef: string | null;
        }[];
        organizationId: string;
        id: string;
        locationId: string;
        notes: string | null;
        createdAt: Date;
        idempotencyKey: string | null;
        supplierId: string;
        poId: string;
        grnNumber: string;
        receiveDate: Date;
        challanRef: string | null;
        receivedById: string;
    }>;
    listGrns(organizationId: string, opts: {
        poId?: string;
        supplierId?: string;
        page: number;
        pageSize: number;
    }): Promise<{
        total: number;
        page: number;
        pageSize: number;
        items: ({
            location: {
                name: string;
            };
            supplier: {
                name: string;
            };
            lines: ({
                item: {
                    code: string;
                    name: string;
                };
                variant: {
                    id: string;
                    name: string | null;
                    sku: string | null;
                    variantCode: string;
                };
            } & {
                organizationId: string;
                id: string;
                itemId: string;
                inventoryVariantId: string;
                notes: string | null;
                unitCost: Prisma.Decimal;
                lineTotal: Prisma.Decimal;
                grnId: string;
                poLineId: string;
                deliveredQty: Prisma.Decimal;
                acceptedQty: Prisma.Decimal;
                rejectedQty: Prisma.Decimal;
                batchRef: string | null;
            })[];
            po: {
                poNumber: string;
            };
        } & {
            organizationId: string;
            id: string;
            locationId: string;
            notes: string | null;
            createdAt: Date;
            idempotencyKey: string | null;
            supplierId: string;
            poId: string;
            grnNumber: string;
            receiveDate: Date;
            challanRef: string | null;
            receivedById: string;
        })[];
    }>;
    grnPrintDto(organizationId: string, grnId: string): Promise<{
        grnNumber: string;
        poNumber: string;
        supplierName: string;
        receiveDate: Date;
        challanRef: string | null;
        lines: {
            itemCode: string;
            itemName: string;
            uom: string;
            deliveredQty: string;
            acceptedQty: string;
            rejectedQty: string;
        }[];
    }>;
    createReturn(organizationId: string, input: PurchaseReturnInput, actorUserId: string): Promise<({
        lines: ({
            variant: {
                id: string;
                name: string | null;
                sku: string | null;
                variantCode: string;
            };
        } & {
            organizationId: string;
            id: string;
            itemId: string;
            inventoryVariantId: string;
            unitCost: Prisma.Decimal;
            qty: Prisma.Decimal;
            amount: Prisma.Decimal;
            returnId: string;
            avgCostAtReturn: Prisma.Decimal;
        })[];
    } & {
        organizationId: string;
        id: string;
        locationId: string;
        notes: string | null;
        createdAt: Date;
        reason: string;
        createdById: string;
        idempotencyKey: string | null;
        supplierId: string;
        totalAmount: Prisma.Decimal;
        poId: string | null;
        grnId: string | null;
        returnNumber: string;
        returnDate: Date;
    }) | {
        replayed: boolean;
        lines: {
            organizationId: string;
            id: string;
            itemId: string;
            inventoryVariantId: string;
            unitCost: Prisma.Decimal;
            qty: Prisma.Decimal;
            amount: Prisma.Decimal;
            returnId: string;
            avgCostAtReturn: Prisma.Decimal;
        }[];
        organizationId: string;
        id: string;
        locationId: string;
        notes: string | null;
        createdAt: Date;
        reason: string;
        createdById: string;
        idempotencyKey: string | null;
        supplierId: string;
        totalAmount: Prisma.Decimal;
        poId: string | null;
        grnId: string | null;
        returnNumber: string;
        returnDate: Date;
    }>;
    listReturns(organizationId: string, opts: {
        supplierId?: string;
        page: number;
        pageSize: number;
    }): Promise<{
        total: number;
        page: number;
        pageSize: number;
        items: ({
            location: {
                name: string;
            };
            supplier: {
                name: string;
            };
            lines: ({
                item: {
                    code: string;
                    name: string;
                };
                variant: {
                    id: string;
                    name: string | null;
                    sku: string | null;
                    variantCode: string;
                };
            } & {
                organizationId: string;
                id: string;
                itemId: string;
                inventoryVariantId: string;
                unitCost: Prisma.Decimal;
                qty: Prisma.Decimal;
                amount: Prisma.Decimal;
                returnId: string;
                avgCostAtReturn: Prisma.Decimal;
            })[];
        } & {
            organizationId: string;
            id: string;
            locationId: string;
            notes: string | null;
            createdAt: Date;
            reason: string;
            createdById: string;
            idempotencyKey: string | null;
            supplierId: string;
            totalAmount: Prisma.Decimal;
            poId: string | null;
            grnId: string | null;
            returnNumber: string;
            returnDate: Date;
        })[];
    }>;
    createInvoice(organizationId: string, input: SupplierInvoiceInput, actorUserId: string): Promise<{
        organizationId: string;
        id: string;
        updatedAt: Date;
        notes: string | null;
        createdAt: Date;
        status: import("@prisma/client").$Enums.SupplierInvoiceStatus;
        createdById: string;
        grandTotal: Prisma.Decimal;
        paidAmount: Prisma.Decimal;
        creditAmount: Prisma.Decimal;
        subtotal: Prisma.Decimal;
        discountAmount: Prisma.Decimal;
        taxAmount: Prisma.Decimal;
        idempotencyKey: string | null;
        supplierId: string;
        invoiceDate: Date;
        dueDate: Date | null;
        internalNumber: string;
        supplierInvoiceNumber: string;
        dueAlertAt: Date | null;
    } | {
        replayed: boolean;
        organizationId: string;
        id: string;
        updatedAt: Date;
        notes: string | null;
        createdAt: Date;
        status: import("@prisma/client").$Enums.SupplierInvoiceStatus;
        createdById: string;
        grandTotal: Prisma.Decimal;
        paidAmount: Prisma.Decimal;
        creditAmount: Prisma.Decimal;
        subtotal: Prisma.Decimal;
        discountAmount: Prisma.Decimal;
        taxAmount: Prisma.Decimal;
        idempotencyKey: string | null;
        supplierId: string;
        invoiceDate: Date;
        dueDate: Date | null;
        internalNumber: string;
        supplierInvoiceNumber: string;
        dueAlertAt: Date | null;
    }>;
    listInvoices(organizationId: string, opts: {
        supplierId?: string;
        status?: string;
        page: number;
        pageSize: number;
    }): Promise<{
        total: number;
        page: number;
        pageSize: number;
        items: ({
            supplier: {
                name: string;
            };
            grnLinks: ({
                grn: {
                    grnNumber: string;
                };
            } & {
                id: string;
                grnId: string;
                invoiceId: string;
            })[];
        } & {
            organizationId: string;
            id: string;
            updatedAt: Date;
            notes: string | null;
            createdAt: Date;
            status: import("@prisma/client").$Enums.SupplierInvoiceStatus;
            createdById: string;
            grandTotal: Prisma.Decimal;
            paidAmount: Prisma.Decimal;
            creditAmount: Prisma.Decimal;
            subtotal: Prisma.Decimal;
            discountAmount: Prisma.Decimal;
            taxAmount: Prisma.Decimal;
            idempotencyKey: string | null;
            supplierId: string;
            invoiceDate: Date;
            dueDate: Date | null;
            internalNumber: string;
            supplierInvoiceNumber: string;
            dueAlertAt: Date | null;
        })[];
    }>;
    supplierInvoicePrintDto(organizationId: string, invoiceId: string): Promise<{
        internalNumber: string;
        supplierInvoiceNumber: string;
        supplierName: string;
        invoiceDate: Date;
        dueDate: Date | null;
        status: import("@prisma/client").$Enums.SupplierInvoiceStatus;
        grandTotal: string;
        paidAmount: string;
        outstanding: string;
        grns: {
            grnNumber: string;
        }[];
    }>;
    private flagDueInvoices;
    createPayment(organizationId: string, input: SupplierPaymentInput, actorUserId: string): Promise<({
        allocations: {
            organizationId: string;
            id: string;
            amount: Prisma.Decimal;
            invoiceId: string;
            paymentId: string;
        }[];
    } & {
        organizationId: string;
        id: string;
        notes: string | null;
        createdAt: Date;
        createdById: string;
        idempotencyKey: string | null;
        supplierId: string;
        amount: Prisma.Decimal;
        paymentMethodId: string | null;
        financialAccountId: string | null;
        paymentNumber: string;
        paymentDate: Date;
        bankRef: string | null;
    }) | {
        replayed: boolean;
        allocations: {
            organizationId: string;
            id: string;
            amount: Prisma.Decimal;
            invoiceId: string;
            paymentId: string;
        }[];
        organizationId: string;
        id: string;
        notes: string | null;
        createdAt: Date;
        createdById: string;
        idempotencyKey: string | null;
        supplierId: string;
        amount: Prisma.Decimal;
        paymentMethodId: string | null;
        financialAccountId: string | null;
        paymentNumber: string;
        paymentDate: Date;
        bankRef: string | null;
    }>;
    listPayments(organizationId: string, opts: {
        supplierId?: string;
        page: number;
        pageSize: number;
    }): Promise<{
        total: number;
        page: number;
        pageSize: number;
        items: ({
            supplier: {
                name: string;
            };
            allocations: ({
                invoice: {
                    internalNumber: string;
                    supplierInvoiceNumber: string;
                };
            } & {
                organizationId: string;
                id: string;
                amount: Prisma.Decimal;
                invoiceId: string;
                paymentId: string;
            })[];
            paymentMethod: {
                name: string;
            } | null;
        } & {
            organizationId: string;
            id: string;
            notes: string | null;
            createdAt: Date;
            createdById: string;
            idempotencyKey: string | null;
            supplierId: string;
            amount: Prisma.Decimal;
            paymentMethodId: string | null;
            financialAccountId: string | null;
            paymentNumber: string;
            paymentDate: Date;
            bankRef: string | null;
        })[];
    }>;
    supplierPaymentPrintDto(organizationId: string, paymentId: string): Promise<{
        paymentNumber: string;
        paymentDate: Date;
        supplierName: string;
        amount: string;
        bankRef: string | null;
        allocations: {
            invoiceNumber: string;
            amount: string;
        }[];
    }>;
    /** Reconciliation: outstanding = invoices - payments(allocated) - returns credit note (informational). */
    outstanding(organizationId: string, supplierId?: string): Promise<{
        totalOutstanding: string;
        rows: {
            supplierId: string;
            code: string;
            name: string;
            totalInvoiced: string;
            totalPaid: string;
            creditApplied: string;
            returnsValue: string;
            outstanding: string;
        }[];
    }>;
    /** Guard helper: separate creator vs approver is a policy the UI enforces; API enforces permission-based separation. */
    assertApprovalAllowed(user: {
        permissions: string[];
        isSuperAdmin: boolean;
    }): void;
}
//# sourceMappingURL=purchasing.service.d.ts.map