fix: swaps with discounts (#142)

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

No files matched your search

@@ -546,7 +546,7 @@ class BrightpearlService extends BaseService {
}, },
rows: await this.getBrightpearlRows({ rows: await this.getBrightpearlRows({
region_id: fromOrder.region_id, region_id: fromOrder.region_id,
discounts: [], discounts: fromOrder.discounts,
tax_rate: fromOrder.tax_rate, tax_rate: fromOrder.tax_rate,
items: fromSwap.additional_items, items: fromSwap.additional_items,
shipping_methods: fromSwap.shipping_methods, shipping_methods: fromSwap.shipping_methods,
@@ -15,6 +15,7 @@ export default new mongoose.Schema(
is_giftcard: { type: Boolean, default: false }, is_giftcard: { type: Boolean, default: false },
should_merge: { type: Boolean, default: true }, should_merge: { type: Boolean, default: true },
has_shipping: { type: Boolean, default: false }, has_shipping: { type: Boolean, default: false },
no_discount: { type: Boolean, default: false },
// mongoose doesn't allow multi-type validation but this field allows both // mongoose doesn't allow multi-type validation but this field allows both
// an object containing: // an object containing:
+1
View File
@@ -300,6 +300,7 @@ class SwapService extends BaseService {
title: "Return shipping", title: "Return shipping",
quantity: 1, quantity: 1,
has_shipping: true, has_shipping: true,
no_discount: true,
content: { content: {
unit_price: swap.return_shipping.price, unit_price: swap.return_shipping.price,
quantity: 1, quantity: 1,
+18 -4
View File
@@ -35,7 +35,7 @@ class TotalsService extends BaseService {
* @param {Cart || Order} object - cart or order to calculate subtotal for * @param {Cart || Order} object - cart or order to calculate subtotal for
* @return {int} the calculated subtotal * @return {int} the calculated subtotal
*/ */
getSubtotal(object) { getSubtotal(object, opts = {}) {
let subtotal = 0 let subtotal = 0
if (!object.items) { if (!object.items) {
return subtotal return subtotal
@@ -46,8 +46,15 @@ class TotalsService extends BaseService {
const temp = _.sumBy(item.content, c => c.unit_price * c.quantity) const temp = _.sumBy(item.content, c => c.unit_price * c.quantity)
subtotal += temp * item.quantity subtotal += temp * item.quantity
} else { } else {
subtotal += if (opts.excludeNonDiscounts) {
item.content.unit_price * item.content.quantity * item.quantity 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) return this.rounded(subtotal)
@@ -135,6 +142,13 @@ class TotalsService extends BaseService {
* applied discount * applied discount
*/ */
calculateDiscount_(lineItem, variant, variantPrice, value, discountType) { calculateDiscount_(lineItem, variant, variantPrice, value, discountType) {
if (lineItem.no_discount) {
return {
lineItem,
variant,
amount: 0,
}
}
if (discountType === "percentage") { if (discountType === "percentage") {
return { return {
lineItem, lineItem,
@@ -240,7 +254,7 @@ class TotalsService extends BaseService {
* @return {int} the total discounts amount * @return {int} the total discounts amount
*/ */
async getDiscountTotal(cart) { async getDiscountTotal(cart) {
let subtotal = this.getSubtotal(cart) let subtotal = this.getSubtotal(cart, { excludeNonDiscounts: true })
if (!cart.discounts || !cart.discounts.length) { if (!cart.discounts || !cart.discounts.length) {
return 0 return 0