fix: swaps with discounts (#142)

This commit is contained in:
Sebastian Rindom
2020-11-29 12:18:36 +01:00
committed by GitHub
parent 9dbb40e287
commit aae8d5e112
4 changed files with 21 additions and 5 deletions

View File

@@ -546,7 +546,7 @@ class BrightpearlService extends BaseService {
},
rows: await this.getBrightpearlRows({
region_id: fromOrder.region_id,
discounts: [],
discounts: fromOrder.discounts,
tax_rate: fromOrder.tax_rate,
items: fromSwap.additional_items,
shipping_methods: fromSwap.shipping_methods,

View File

@@ -15,6 +15,7 @@ export default new mongoose.Schema(
is_giftcard: { type: Boolean, default: false },
should_merge: { type: Boolean, default: true },
has_shipping: { type: Boolean, default: false },
no_discount: { type: Boolean, default: false },
// mongoose doesn't allow multi-type validation but this field allows both
// an object containing:

View File

@@ -300,6 +300,7 @@ class SwapService extends BaseService {
title: "Return shipping",
quantity: 1,
has_shipping: true,
no_discount: true,
content: {
unit_price: swap.return_shipping.price,
quantity: 1,

View File

@@ -35,7 +35,7 @@ class TotalsService extends BaseService {
* @param {Cart || Order} object - cart or order to calculate subtotal for
* @return {int} the calculated subtotal
*/
getSubtotal(object) {
getSubtotal(object, opts = {}) {
let subtotal = 0
if (!object.items) {
return subtotal
@@ -46,8 +46,15 @@ class TotalsService extends BaseService {
const temp = _.sumBy(item.content, c => c.unit_price * c.quantity)
subtotal += temp * item.quantity
} else {
subtotal +=
item.content.unit_price * item.content.quantity * item.quantity
if (opts.excludeNonDiscounts) {
if (!item.no_discount) {
subtotal +=
item.content.unit_price * item.content.quantity * item.quantity
}
} else {
subtotal +=
item.content.unit_price * item.content.quantity * item.quantity
}
}
})
return this.rounded(subtotal)
@@ -135,6 +142,13 @@ class TotalsService extends BaseService {
* applied discount
*/
calculateDiscount_(lineItem, variant, variantPrice, value, discountType) {
if (lineItem.no_discount) {
return {
lineItem,
variant,
amount: 0,
}
}
if (discountType === "percentage") {
return {
lineItem,
@@ -240,7 +254,7 @@ class TotalsService extends BaseService {
* @return {int} the total discounts amount
*/
async getDiscountTotal(cart) {
let subtotal = this.getSubtotal(cart)
let subtotal = this.getSubtotal(cart, { excludeNonDiscounts: true })
if (!cart.discounts || !cart.discounts.length) {
return 0