import { PoolClient } from '@evershop/postgres-query-builder'; /** * G2 of the import engine: write an order that already happened. * * `importOrder` is the one place that knows how a historical order is made. It * writes the order, its items, its two addresses, its payment transactions and * its activity log in one transaction, with a placeholder cart so that `cart_id` * is satisfied, and it does what a new order does not do: * * - the stock is **not** taken again and the event `order_created` is **not** * written (the two triggers step aside while the transaction says it is * importing, see checkout migration 1.0.13); * - the coupon's `used_time` **is** raised (an old use of a coupon is real use); * - the status of the order is resolved from the given payment and shipment * statuses (`resolveOrderStatus`), never typed in; * - no e-mail is sent: core sends none from the order service. * * Nothing is computed from the store's present prices, taxes or shipping rules: * the order is a record of what was charged, so the amounts come from the data. * The totals of the order are derived from the lines so that they agree with them. */ /** A line of a historical order. Amounts are per unit, before the line's discount. */ export interface HistoricalOrderItem { /** The product, by its sku. It must exist in the store. */ sku: string; /** Or by id; the sku then only names the line. */ productId?: number; name: string; qty: number; /** The unit price without tax. */ price: number; /** The unit price with tax. Default: `price` plus `taxPercent`. */ priceInclTax?: number; /** Default: the rate between `price` and `priceInclTax`, else 0. */ taxPercent?: number; /** The discount of the whole line, without tax. Default 0. */ discountAmount?: number; weight?: number; thumbnail?: string; /** What the line was about, as text (variant options). */ variantOptions?: string; noShippingRequired?: boolean; } /** A transaction of the payment, in the order it happened. */ export interface HistoricalPaymentTransaction { transactionId?: string; /** `authorize`, `capture`, `refund`, `void`, … as the payment method names them. */ type: string; amount: number; paymentAction: string; parentTransactionId?: string; additionalInformation?: string; createdAt?: Date | string; } export interface HistoricalOrderInput { /** Default: the next number, as a new order would get it. */ orderNumber?: string; /** When the order was placed. Default: now. */ createdAt?: Date | string; currency: string; customer?: { id?: number | null; email?: string; fullName?: string; }; coupon?: string | null; items: HistoricalOrderItem[]; /** The columns of `order_address`, as stored. Not validated: it is history. */ shippingAddress?: Record; billingAddress?: Record; shipping?: { methodName?: string; methodData?: Record; feeExclTax?: number; feeInclTax?: number; taxAmount?: number; note?: string; }; paymentMethod?: string; paymentMethodName?: string; /** A key of `oms.order.paymentStatus`. */ paymentStatus: string; /** * The rollup of `order.shipment_status`. Default: `delivered` when no line is * shipped, else `pending`. */ shipmentStatus?: string; transactions?: HistoricalPaymentTransaction[]; /** Extra lines of the order's activity log. */ activity?: Array<{ comment: string; customerNotified?: boolean; createdAt?: Date | string; }>; metaData?: Record; } export interface ImportOrderOptions { /** * The caller's connection, already in a transaction (the caller commits or rolls * back). The flag the triggers read lasts for the transaction, so a connection * that is not in one is refused. **It stays on until that transaction ends**: do * not write a new order (a checkout) on the same transaction after this call. */ connection?: PoolClient; } export interface ImportedOrder { orderId: number; uuid: string; orderNumber: string; status: string; paymentStatus: string; shipmentStatus: string; } /** * The amounts of an order from its lines and its shipping, as one function of the * data: no query, no clock, no configuration. A line's tax is the tax of what the * customer paid for it, after the line's discount. */ export declare function priceHistoricalOrder(items: readonly HistoricalOrderItem[], shipping?: HistoricalOrderInput['shipping']): { lines: { item: HistoricalOrderItem; taxPercent: number; discount: number; unitInclTax: number; lineTotal: number; lineTotalInclTax: number; withDiscount: number; withDiscountInclTax: number; taxBeforeDiscount: number; taxAmount: number; }[]; order: { sub_total: number; sub_total_incl_tax: number; sub_total_with_discount: number; sub_total_with_discount_incl_tax: number; discount_amount: number; tax_amount: number; tax_amount_before_discount: number; shipping_fee_excl_tax: number; shipping_fee_incl_tax: number; shipping_tax_amount: number; total_tax_amount: number; grand_total: number; total_qty: number; total_weight: number; }; }; /** * Whether an order that already happened was placed (its `order_placed` was * emitted), so it gets `placed_at`: its payment went further than pending, * failed or canceled, a payment was recorded for it, or it is a Cash on * Delivery order (placed when created, canceled or not). The rule oms * migration 1.0.11 used for the orders already in the store. */ export declare function wasOrderPlaced(paymentStatus: string, transactionCount: number, paymentMethod?: string): boolean; /** * Writes an order that already happened (see the top of the file). In its own * transaction unless `options.connection` is given. * * @throws when the data is not an order (no item, an unknown product, a status * that the store does not have, an address with no known country); nothing is * written then. */ export declare const importOrder: (data: HistoricalOrderInput, options?: ImportOrderOptions) => Promise; export declare function hookBeforeImportOrder(callback: (this: { data: HistoricalOrderInput; }, ...args: [data: HistoricalOrderInput, connection: PoolClient]) => void | Promise, priority?: number): void; export declare function hookAfterImportOrder(callback: (this: { data: HistoricalOrderInput; }, ...args: [ result: ImportedOrder, data: HistoricalOrderInput, connection: PoolClient ]) => void | Promise, priority?: number): void;