feat(medusa): adds support for gift cards (#132)

* fix(medusa-plugin-brightpearl): adds gift cards to gift card nominal code

* fix: allow orders with 0 total to be created

* fix: gift card ready

* tests passing

* docs: brightpearl comment

* docs: clearer REMEMBER note
This commit is contained in:
Sebastian Rindom
2020-10-28 15:55:50 +01:00
committed by GitHub
parent b33fcfa182
commit f2c62cd232
17 changed files with 316 additions and 113 deletions
+2 -1
View File
@@ -13,8 +13,9 @@ Sends orders to Brightpearl, listens for stock movements, handles returns.
default_status_id: [the status id to assign new orders with] (optional: defaults to 1)
payment_method_code: [the method code to register payments with] (optional: defaults to 1220)
sales_account_code: [nominal code to assign line items to] (optional: defaults to 4000)
shipping_account_code: [nominal code to assign shipping line to] (optional: defaults to 4040)jk
shipping_account_code: [nominal code to assign shipping line to] (optional: defaults to 4040)
discount_account_code: [nominal code to use for Discount-type refunds] (optional)
gift_card_account_code: [nominal code to use for gift card products and redeems] (optional: default to 4000)
inventory_sync_cron: [cron pattern for inventory sync, if left out the job will not be created] (default: false)
```
@@ -501,7 +501,7 @@ class BrightpearlService extends BaseService {
({ discount_rule }) => discount_rule.type !== "free_shipping"
)
let lineDiscounts = []
if (discount) {
if (discount && !discount.is_giftcard) {
lineDiscounts = this.totalsService_.getLineDiscounts(fromOrder, discount)
}
@@ -511,7 +511,7 @@ class BrightpearlService extends BaseService {
item.content.variant.sku
)
const discount = lineDiscounts.find((l) => item._id === l.item._id) || {
const ld = lineDiscounts.find((l) => item._id === l.item._id) || {
amount: 0,
}
@@ -522,7 +522,7 @@ class BrightpearlService extends BaseService {
row.name = item.title
}
row.net = this.totalsService_.rounded(
item.content.unit_price * item.quantity - discount.amount
item.content.unit_price * item.quantity - ld.amount
)
row.tax = this.totalsService_.rounded(row.net * fromOrder.tax_rate)
row.quantity = item.quantity
@@ -530,10 +530,34 @@ class BrightpearlService extends BaseService {
row.externalRef = item._id
row.nominalCode = this.options.sales_account_code || "4000"
if (item.is_giftcard) {
row.nominalCode = this.options.gift_card_account_code || "4000"
}
return row
})
)
// If a gift card was applied to the order we reduce the order amount
// correspondingly. This reduces the amount payable, while debiting the
// gift card account that was previously credited, when the gift card was
// purchased.
if (discount && discount.is_giftcard) {
const discountTotal = await this.totalsService_.getDiscountTotal(
fromOrder
)
lines.push({
name: `Gift Card: ${discount.code}`,
net: -1 * discountTotal,
tax: this.totalsService_.rounded(
-1 * discountTotal * fromOrder.tax_rate
),
quantity: 1,
taxCode: region.tax_code,
nominalCode: this.options.gift_card_account_code || "4000",
})
}
const shippingTotal = this.totalsService_.getShippingTotal(fromOrder)
const shippingMethods = fromOrder.shipping_methods
if (shippingMethods.length > 0) {