Economic plugin
This commit is contained in:
@@ -2,6 +2,7 @@ import axios from "axios"
|
|||||||
import moment from "moment"
|
import moment from "moment"
|
||||||
import { BaseService } from "medusa-interfaces"
|
import { BaseService } from "medusa-interfaces"
|
||||||
import { MedusaError } from "medusa-core-utils"
|
import { MedusaError } from "medusa-core-utils"
|
||||||
|
import EUCountries from "../utils/eu-countries"
|
||||||
|
|
||||||
const ECONOMIC_BASE_URL = "https://restapi.e-conomic.com"
|
const ECONOMIC_BASE_URL = "https://restapi.e-conomic.com"
|
||||||
|
|
||||||
@@ -44,30 +45,25 @@ class EconomicService extends BaseService {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
decideCustomerAndVatNumber_(country) {
|
decideCustomerAndVatNumber_(country_code) {
|
||||||
// Add these to utils
|
const upperCased = country_code.toUpperCase()
|
||||||
const EUCountries = []
|
if (EUCountries.includes(upperCased)) {
|
||||||
const WorldCountries = []
|
return {
|
||||||
|
vat: this.options_.vatzone_number_eu,
|
||||||
|
customer: this.options_.customer_number_eu,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const flag = false
|
if (upperCased === "DK") {
|
||||||
switch (flag) {
|
return {
|
||||||
case EUCountries.includes(country): {
|
vat: this.options_.vatzone_number_dk,
|
||||||
return {
|
customer: this.options_.customer_number_dk,
|
||||||
vat: this.options_.vatzone_number_eu,
|
|
||||||
customer: this.options_.customer_number_eu,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case WorldCountries.includes(country): {
|
}
|
||||||
return {
|
|
||||||
vat: this.options_.vatzone_number_world,
|
return {
|
||||||
customer: this.options_.customer_number_world,
|
vat: this.options_.vatzone_number_world,
|
||||||
}
|
customer: this.options_.customer_number_world,
|
||||||
}
|
|
||||||
default:
|
|
||||||
return {
|
|
||||||
vat: this.options_.vatzone_number_dk,
|
|
||||||
customer: this.options_.customer_number_dk,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,10 +75,13 @@ class EconomicService extends BaseService {
|
|||||||
)
|
)
|
||||||
// If the discount has an item specific allocation method,
|
// If the discount has an item specific allocation method,
|
||||||
// we need to fetch the discount for each item
|
// we need to fetch the discount for each item
|
||||||
const itemDiscounts = await this.totalsService_.getAllocationItemDiscounts(
|
let itemDiscounts = []
|
||||||
discount,
|
if (discount) {
|
||||||
order
|
itemDiscounts = await this.totalsService_.getAllocationItemDiscounts(
|
||||||
)
|
discount,
|
||||||
|
order
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
order.items.forEach((item) => {
|
order.items.forEach((item) => {
|
||||||
// For bundles, we create an order line for each item in the bundle
|
// For bundles, we create an order line for each item in the bundle
|
||||||
@@ -135,13 +134,12 @@ class EconomicService extends BaseService {
|
|||||||
|
|
||||||
async createInvoiceFromOrder(order) {
|
async createInvoiceFromOrder(order) {
|
||||||
// Fetch currency code from order region
|
// Fetch currency code from order region
|
||||||
const {
|
const { currency_code } = await this.regionService_.retrieve(
|
||||||
currency_code,
|
order.region_id
|
||||||
billing_address,
|
)
|
||||||
} = await this.regionService_.retrieve(order.region_id)
|
|
||||||
|
|
||||||
const vatZoneAndCustomer = this.decideCustomerAndVatNumber_(
|
const vatZoneAndCustomer = this.decideCustomerAndVatNumber_(
|
||||||
billing_address.country
|
order.billing_address.country_code
|
||||||
)
|
)
|
||||||
|
|
||||||
const lines = await this.createEconomicLinesFromOrder(order)
|
const lines = await this.createEconomicLinesFromOrder(order)
|
||||||
@@ -174,7 +172,6 @@ class EconomicService extends BaseService {
|
|||||||
async draftEconomicInvoice(orderId) {
|
async draftEconomicInvoice(orderId) {
|
||||||
const order = await this.orderService_.retrieve(orderId)
|
const order = await this.orderService_.retrieve(orderId)
|
||||||
const invoice = await this.createInvoiceFromOrder(order)
|
const invoice = await this.createInvoiceFromOrder(order)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const draftInvoice = await this.economic_.post(
|
const draftInvoice = await this.economic_.post(
|
||||||
`${ECONOMIC_BASE_URL}/invoices/drafts`,
|
`${ECONOMIC_BASE_URL}/invoices/drafts`,
|
||||||
@@ -184,7 +181,7 @@ class EconomicService extends BaseService {
|
|||||||
await this.orderService_.setMetadata(
|
await this.orderService_.setMetadata(
|
||||||
order._id,
|
order._id,
|
||||||
"economicDraftId",
|
"economicDraftId",
|
||||||
draftInvoice.draftInvoiceNumber
|
draftInvoice.data.draftInvoiceNumber
|
||||||
)
|
)
|
||||||
|
|
||||||
const invoiceOrder = await this.orderService_.retrieve(order._id)
|
const invoiceOrder = await this.orderService_.retrieve(order._id)
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
export default [
|
||||||
|
"BE",
|
||||||
|
"BG",
|
||||||
|
"CZ",
|
||||||
|
"DK",
|
||||||
|
"DE",
|
||||||
|
"EE",
|
||||||
|
"IE",
|
||||||
|
"EL",
|
||||||
|
"ES",
|
||||||
|
"FR",
|
||||||
|
"HR",
|
||||||
|
"IT",
|
||||||
|
"CY",
|
||||||
|
"LV",
|
||||||
|
"LT",
|
||||||
|
"LU",
|
||||||
|
"HU",
|
||||||
|
"MT",
|
||||||
|
"NL",
|
||||||
|
"AT",
|
||||||
|
"PL",
|
||||||
|
"PT",
|
||||||
|
"RO",
|
||||||
|
"SI",
|
||||||
|
"SK",
|
||||||
|
"FI",
|
||||||
|
"SE",
|
||||||
|
"UK",
|
||||||
|
]
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
Object.defineProperty(exports, "__esModule", {
|
||||||
|
value: true
|
||||||
|
});
|
||||||
|
exports["default"] = void 0;
|
||||||
|
var _default = ["BE", "BG", "CZ", "DK", "DE", "EE", "IE", "EL", "ES", "FR", "HR", "IT", "CY", "LV", "LT", "LU", "HU", "MT", "NL", "AT", "PL", "PT", "RO", "SI", "SK", "FI", "SE", "UK"];
|
||||||
|
exports["default"] = _default;
|
||||||
@@ -230,7 +230,6 @@ class OrderService extends BaseService {
|
|||||||
return this.retrieve(order._id)
|
return this.retrieve(order._id)
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.log(error)
|
|
||||||
throw error
|
throw error
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -287,7 +286,7 @@ class OrderService extends BaseService {
|
|||||||
// If payment status is not authorized, we throw
|
// If payment status is not authorized, we throw
|
||||||
if (paymentStatus !== "authorized") {
|
if (paymentStatus !== "authorized") {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
MedusaError.types.INVALID_ARGUMENT,
|
MedusaError.Types.INVALID_ARGUMENT,
|
||||||
"Payment method is not authorized"
|
"Payment method is not authorized"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user