fix(order): calculate taxes on order edit flows (#11518)
* fix(order): calcualte taxes on order edit flows * merge summary * tests * fix pending difference * comments * claim test * revert method
This commit is contained in:
1 parent
065df75e7d
commit
0c957350a6
19 files changed
+501
-442
No files matched your search
@@ -30,14 +30,14 @@ export interface SetOrderTaxLinesForItemsStepInput {
|
||||
export const setOrderTaxLinesForItemsStepId = "set-order-tax-lines-for-items"
|
||||
/**
|
||||
* This step sets the tax lines of an order's items and shipping methods.
|
||||
*
|
||||
*
|
||||
* :::note
|
||||
*
|
||||
*
|
||||
* You can retrieve an order's details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query),
|
||||
* or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep).
|
||||
*
|
||||
*
|
||||
* :::
|
||||
*
|
||||
*
|
||||
* @example
|
||||
* const data = setOrderTaxLinesForItemsStep({
|
||||
* order: {
|
||||
@@ -74,14 +74,13 @@ export const setOrderTaxLinesForItemsStep = createStep(
|
||||
|
||||
const itemsTaxLinesData = normalizeItemTaxLinesForOrder(item_tax_lines)
|
||||
const setItemTaxLinesPromise = itemsTaxLinesData.length
|
||||
? orderService.setOrderLineItemTaxLines(order.id, itemsTaxLinesData)
|
||||
? orderService.upsertOrderLineItemTaxLines(itemsTaxLinesData)
|
||||
: void 0
|
||||
|
||||
const shippingTaxLinesData =
|
||||
normalizeShippingTaxLinesForOrder(shipping_tax_lines)
|
||||
const setShippingTaxLinesPromise = shippingTaxLinesData.length
|
||||
? await orderService.setOrderShippingMethodTaxLines(
|
||||
order.id,
|
||||
? await orderService.upsertOrderShippingMethodTaxLines(
|
||||
shippingTaxLinesData
|
||||
)
|
||||
: void 0
|
||||
@@ -105,14 +104,13 @@ export const setOrderTaxLinesForItemsStep = createStep(
|
||||
return
|
||||
}
|
||||
|
||||
const { order, existingLineItemTaxLines, existingShippingMethodTaxLines } =
|
||||
const { existingLineItemTaxLines, existingShippingMethodTaxLines } =
|
||||
revertData
|
||||
|
||||
const orderService = container.resolve<IOrderModuleService>(Modules.ORDER)
|
||||
|
||||
if (existingLineItemTaxLines) {
|
||||
await orderService.setOrderLineItemTaxLines(
|
||||
order.id,
|
||||
await orderService.upsertOrderLineItemTaxLines(
|
||||
existingLineItemTaxLines.map((taxLine) => ({
|
||||
description: taxLine.description,
|
||||
tax_rate_id: taxLine.tax_rate_id,
|
||||
@@ -124,8 +122,7 @@ export const setOrderTaxLinesForItemsStep = createStep(
|
||||
)
|
||||
}
|
||||
|
||||
await orderService.setOrderShippingMethodTaxLines(
|
||||
order.id,
|
||||
await orderService.upsertOrderShippingMethodTaxLines(
|
||||
existingShippingMethodTaxLines.map((taxLine) => ({
|
||||
description: taxLine.description,
|
||||
tax_rate_id: taxLine.tax_rate_id,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { OrderSummaryDTO } from "../../order/common"
|
||||
import { BigNumberRawValue, BigNumberValue } from "../../totals"
|
||||
|
||||
/**
|
||||
@@ -1086,91 +1087,4 @@ interface FulfillmentOrderShippingMethodDTO {
|
||||
*
|
||||
* The order summary details.
|
||||
*/
|
||||
type FulfillmentOrderSummaryDTO = {
|
||||
/**
|
||||
* The total of the order summary.
|
||||
*/
|
||||
total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The subtotal of the order summary.
|
||||
*/
|
||||
subtotal: BigNumberValue
|
||||
|
||||
/**
|
||||
* The total tax of the order summary.
|
||||
*/
|
||||
total_tax: BigNumberValue
|
||||
|
||||
/**
|
||||
* The ordered total of the order summary.
|
||||
*/
|
||||
ordered_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The fulfilled total of the order summary.
|
||||
*/
|
||||
fulfilled_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The returned total of the order summary.
|
||||
*/
|
||||
returned_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The return request total of the order summary.
|
||||
*/
|
||||
return_request_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The write off total of the order summary.
|
||||
*/
|
||||
write_off_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The projected total of the order summary.
|
||||
*/
|
||||
projected_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The net total of the order summary.
|
||||
*/
|
||||
net_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The net subtotal of the order summary.
|
||||
*/
|
||||
net_subtotal: BigNumberValue
|
||||
|
||||
/**
|
||||
* The net total tax of the order summary.
|
||||
*/
|
||||
net_total_tax: BigNumberValue
|
||||
|
||||
/**
|
||||
* The balance of the order summary.
|
||||
*/
|
||||
balance: BigNumberValue
|
||||
|
||||
/**
|
||||
* The paid total of the order summary.
|
||||
*/
|
||||
paid_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The refunded total of the order summary.
|
||||
*/
|
||||
refunded_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The pending difference of the order.
|
||||
*/
|
||||
pending_difference: BigNumberValue
|
||||
|
||||
/**
|
||||
* The raw pending difference of the order.
|
||||
*
|
||||
* @ignore
|
||||
*/
|
||||
raw_pending_difference: BigNumberRawValue
|
||||
}
|
||||
type FulfillmentOrderSummaryDTO = OrderSummaryDTO
|
||||
@@ -8,46 +8,13 @@ import { BaseProduct, BaseProductVariant } from "../product/common"
|
||||
import { BaseReturn } from "../return/common"
|
||||
|
||||
export interface BaseOrderSummary {
|
||||
/**
|
||||
* The total of the order including taxes and promotions.
|
||||
*/
|
||||
total: number
|
||||
/**
|
||||
* The total of the order excluding taxes, including promotions.
|
||||
*/
|
||||
subtotal: number
|
||||
/**
|
||||
* The tax totals of the order including promotions.
|
||||
*/
|
||||
total_tax: number
|
||||
/**
|
||||
* The total ordered amount.
|
||||
*/
|
||||
ordered_total: number
|
||||
/**
|
||||
* The total fulfilled amount.
|
||||
*/
|
||||
fulfilled_total: number
|
||||
/**
|
||||
* The total amount of returned items.
|
||||
*/
|
||||
returned_total: number
|
||||
/**
|
||||
* The total amount of the items requested to be returned.
|
||||
*/
|
||||
return_request_total: number
|
||||
/**
|
||||
* The total amount of the items removed from the order.
|
||||
*/
|
||||
write_off_total: number
|
||||
/**
|
||||
* The total amount paid.
|
||||
*/
|
||||
pending_difference: number
|
||||
current_order_total: number
|
||||
original_order_total: number
|
||||
transaction_total: number
|
||||
paid_total: number
|
||||
/**
|
||||
* The total amount refunded
|
||||
*/
|
||||
refunded_total: number
|
||||
accounting_total: number
|
||||
}
|
||||
|
||||
export interface BaseOrderAdjustmentLine {
|
||||
|
||||
@@ -41,104 +41,23 @@ export type OrderChangeStatus =
|
||||
* The order summary details.
|
||||
*/
|
||||
export type OrderSummaryDTO = {
|
||||
/**
|
||||
* The total of the order summary.
|
||||
*/
|
||||
total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The subtotal of the order summary.
|
||||
*/
|
||||
subtotal: BigNumberValue
|
||||
|
||||
/**
|
||||
* The total tax of the order summary.
|
||||
*/
|
||||
total_tax: BigNumberValue
|
||||
|
||||
/**
|
||||
* The ordered total of the order summary.
|
||||
*/
|
||||
ordered_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The fulfilled total of the order summary.
|
||||
*/
|
||||
fulfilled_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The returned total of the order summary.
|
||||
*/
|
||||
returned_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The return request total of the order summary.
|
||||
*/
|
||||
return_request_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The write off total of the order summary.
|
||||
*/
|
||||
write_off_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The projected total of the order summary.
|
||||
*/
|
||||
projected_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The net total of the order summary.
|
||||
*/
|
||||
net_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The net subtotal of the order summary.
|
||||
*/
|
||||
net_subtotal: BigNumberValue
|
||||
|
||||
/**
|
||||
* The net total tax of the order summary.
|
||||
*/
|
||||
net_total_tax: BigNumberValue
|
||||
|
||||
/**
|
||||
* The balance of the order summary.
|
||||
*/
|
||||
balance: BigNumberValue
|
||||
|
||||
/**
|
||||
* The paid total of the order summary.
|
||||
*/
|
||||
paid_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The refunded total of the order summary.
|
||||
*/
|
||||
refunded_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The pending difference of the order.
|
||||
*/
|
||||
pending_difference: BigNumberValue
|
||||
current_order_total: BigNumberValue
|
||||
original_order_total: BigNumberValue
|
||||
transaction_total: BigNumberValue
|
||||
paid_total: BigNumberValue
|
||||
refunded_total: BigNumberValue
|
||||
credit_line_total: BigNumberValue
|
||||
accounting_total: BigNumberValue
|
||||
|
||||
/**
|
||||
* The raw pending difference of the order.
|
||||
*
|
||||
* @ignore
|
||||
*/
|
||||
raw_pending_difference: BigNumberRawValue
|
||||
|
||||
/**
|
||||
* The sum difference of all actions
|
||||
*/
|
||||
difference_sum: BigNumberValue
|
||||
|
||||
/**
|
||||
* The raw sum difference of all actions
|
||||
*
|
||||
* @ignore
|
||||
*/
|
||||
raw_difference_sum: BigNumberRawValue
|
||||
raw_current_order_total: BigNumberRawValue
|
||||
raw_original_order_total: BigNumberRawValue
|
||||
raw_transaction_total: BigNumberRawValue
|
||||
raw_paid_total: BigNumberRawValue
|
||||
raw_refunded_total: BigNumberRawValue
|
||||
raw_credit_line_total: BigNumberRawValue
|
||||
raw_accounting_total: BigNumberRawValue
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2359,6 +2278,11 @@ export interface OrderTransactionDTO {
|
||||
*/
|
||||
order_id: string
|
||||
|
||||
/**
|
||||
* The associated order version
|
||||
*/
|
||||
version: number
|
||||
|
||||
/**
|
||||
* The associated order
|
||||
*
|
||||
|
||||
@@ -1590,6 +1590,35 @@ export interface IOrderModuleService extends IModuleService {
|
||||
sharedContext?: Context
|
||||
): Promise<OrderLineItemAdjustmentDTO[]>
|
||||
|
||||
/**
|
||||
* This method upserts line item adjustments.
|
||||
*
|
||||
* @param {UpsertOrderLineItemAdjustmentDTO[]} data - The line item adjustments to create or update. If the `id` property is provided
|
||||
* in an object, it means an existing line item adjustment will be updated. Otherwise, a new one is created.
|
||||
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<OrderLineItemAdjustmentDTO[]>} The line item adjustments.
|
||||
*
|
||||
* @example
|
||||
* const lineItemAdjustments = await orderModuleService.upsertOrderLineItemAdjustments(
|
||||
* [
|
||||
* {
|
||||
* item_id: "1234",
|
||||
* amount: 10
|
||||
* },
|
||||
* {
|
||||
* id: "123",
|
||||
* item_id: "4321",
|
||||
* amount: 20
|
||||
* }
|
||||
* ]
|
||||
* )
|
||||
*
|
||||
*/
|
||||
upsertOrderLineItemAdjustments(
|
||||
data: UpsertOrderLineItemAdjustmentDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<OrderLineItemAdjustmentDTO[]>
|
||||
|
||||
/**
|
||||
* This method sets the line item adjustments of an order.
|
||||
*
|
||||
@@ -1786,6 +1815,39 @@ export interface IOrderModuleService extends IModuleService {
|
||||
sharedContext?: Context
|
||||
): Promise<OrderShippingMethodAdjustmentDTO[]>
|
||||
|
||||
/**
|
||||
* This method upserts shipping method adjustments.
|
||||
*
|
||||
* @param {(CreateOrderShippingMethodAdjustmentDTO | UpdateOrderShippingMethodAdjustmentDTO)[]} data - The shipping method adjustments to be created
|
||||
* or updated. If an adjustment object has an `id` property, it's updated. Otherwise, a new adjustment is created.
|
||||
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<OrderShippingMethodAdjustmentDTO[]>} The shipping method adjustments.
|
||||
*
|
||||
* @example
|
||||
* const shippingMethodAdjustments = await orderModuleService
|
||||
* .upsertOrderShippingMethodAdjustments(
|
||||
* [
|
||||
* {
|
||||
* shipping_method_id: "123",
|
||||
* code: "50OFF",
|
||||
* amount: 5
|
||||
* },
|
||||
* {
|
||||
* id: "321",
|
||||
* amount: 5
|
||||
* }
|
||||
* ]
|
||||
* )
|
||||
*
|
||||
*/
|
||||
upsertOrderShippingMethodAdjustments(
|
||||
data: (
|
||||
| CreateOrderShippingMethodAdjustmentDTO
|
||||
| UpdateOrderShippingMethodAdjustmentDTO
|
||||
)[],
|
||||
sharedContext?: Context
|
||||
): Promise<OrderShippingMethodAdjustmentDTO[]>
|
||||
|
||||
/**
|
||||
* This method sets the shipping method adjustments of an order.
|
||||
*
|
||||
@@ -1985,6 +2047,31 @@ export interface IOrderModuleService extends IModuleService {
|
||||
sharedContext?: Context
|
||||
): Promise<OrderLineItemTaxLineDTO[]>
|
||||
|
||||
/**
|
||||
* This method upserts line item tax lines.
|
||||
*
|
||||
* @param {(CreateOrderLineItemTaxLineDTO | UpdateOrderLineItemTaxLineDTO)[]} taxLines - The line item tax lines to create or update. If the
|
||||
* tax line object has an `id` property, it'll be updated. Otherwise, a tax line is created.
|
||||
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<OrderLineItemTaxLineDTO[]>} The line item tax lines.
|
||||
*
|
||||
* @example
|
||||
* const lineItemTaxLines = await orderModuleService
|
||||
* .upsertOrderLineItemTaxLines(
|
||||
* [
|
||||
* {
|
||||
* code: "123",
|
||||
* rate: 2
|
||||
* }
|
||||
* ]
|
||||
* )
|
||||
*
|
||||
*/
|
||||
upsertOrderLineItemTaxLines(
|
||||
taxLines: (CreateOrderLineItemTaxLineDTO | UpdateOrderLineItemTaxLineDTO)[],
|
||||
sharedContext?: Context
|
||||
): Promise<OrderLineItemTaxLineDTO[]>
|
||||
|
||||
/**
|
||||
* This method sets the line item tax lines of an order.
|
||||
*
|
||||
@@ -2176,6 +2263,38 @@ export interface IOrderModuleService extends IModuleService {
|
||||
sharedContext?: Context
|
||||
): Promise<OrderShippingMethodTaxLineDTO[]>
|
||||
|
||||
/**
|
||||
* This method upsert shipping method tax lines.
|
||||
*
|
||||
* @param {(CreateOrderShippingMethodTaxLineDTO | UpdateOrderShippingMethodTaxLineDTO)[]} taxLines - The shipping method tax lines to create or update.
|
||||
* If a tax line object has an `id` property, it's updated. Otherwise, a tax line is created.
|
||||
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<OrderShippingMethodTaxLineDTO[]>} The shipping method tax lines.
|
||||
*
|
||||
* @example
|
||||
* const shippingMethodTaxLines = await orderModuleService
|
||||
* .upsertOrderShippingMethodTaxLines(
|
||||
* [
|
||||
* {
|
||||
* code: "123",
|
||||
* rate: 2
|
||||
* },
|
||||
* {
|
||||
* id: "321",
|
||||
* rate: 2
|
||||
* }
|
||||
* ]
|
||||
* )
|
||||
*
|
||||
*/
|
||||
upsertOrderShippingMethodTaxLines(
|
||||
taxLines: (
|
||||
| CreateOrderShippingMethodTaxLineDTO
|
||||
| UpdateOrderShippingMethodTaxLineDTO
|
||||
)[],
|
||||
sharedContext?: Context
|
||||
): Promise<OrderShippingMethodTaxLineDTO[]>
|
||||
|
||||
/**
|
||||
* This method set the shipping method tax lines of an order.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user