fix: swaps with discounts (#142)
This commit is contained in:
1 parent
9dbb40e287
commit
aae8d5e112
4 files changed
+19
-3
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:
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
@@ -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,9 +46,16 @@ 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 {
|
||||||
|
if (opts.excludeNonDiscounts) {
|
||||||
|
if (!item.no_discount) {
|
||||||
subtotal +=
|
subtotal +=
|
||||||
item.content.unit_price * item.content.quantity * item.quantity
|
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
|
||||||
|
|||||||
Reference in new issue
Block a user