Merge branch 'integration/dummy-project' of github.com:medusajs/medusa into integration/dummy-project
This commit is contained in:
13
packages/medusa-plugin-economic/.babelrc
Normal file
13
packages/medusa-plugin-economic/.babelrc
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"plugins": [
|
||||
"@babel/plugin-proposal-class-properties",
|
||||
"@babel/plugin-transform-instanceof",
|
||||
"@babel/plugin-transform-classes"
|
||||
],
|
||||
"presets": ["@babel/preset-env"],
|
||||
"env": {
|
||||
"test": {
|
||||
"plugins": ["@babel/plugin-transform-runtime"]
|
||||
}
|
||||
}
|
||||
}
|
||||
9
packages/medusa-plugin-economic/.eslintrc
Normal file
9
packages/medusa-plugin-economic/.eslintrc
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"plugins": ["prettier"],
|
||||
"extends": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"semi": "error",
|
||||
"no-unused-expressions": "true"
|
||||
}
|
||||
}
|
||||
15
packages/medusa-plugin-economic/.gitignore
vendored
Normal file
15
packages/medusa-plugin-economic/.gitignore
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
/lib
|
||||
node_modules
|
||||
.DS_store
|
||||
.env*
|
||||
/*.js
|
||||
!index.js
|
||||
!jest.config.js
|
||||
|
||||
/dist
|
||||
|
||||
/api
|
||||
/services
|
||||
/models
|
||||
/subscribers
|
||||
|
||||
9
packages/medusa-plugin-economic/.npmignore
Normal file
9
packages/medusa-plugin-economic/.npmignore
Normal file
@@ -0,0 +1,9 @@
|
||||
/lib
|
||||
node_modules
|
||||
.DS_store
|
||||
.env*
|
||||
/*.js
|
||||
!index.js
|
||||
yarn.lock
|
||||
|
||||
|
||||
7
packages/medusa-plugin-economic/.prettierrc
Normal file
7
packages/medusa-plugin-economic/.prettierrc
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
1
packages/medusa-plugin-economic/index.js
Normal file
1
packages/medusa-plugin-economic/index.js
Normal file
@@ -0,0 +1 @@
|
||||
// noop
|
||||
3
packages/medusa-plugin-economic/jest.config.js
Normal file
3
packages/medusa-plugin-economic/jest.config.js
Normal file
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
testEnvironment: "node",
|
||||
}
|
||||
11482
packages/medusa-plugin-economic/package-lock.json
generated
Normal file
11482
packages/medusa-plugin-economic/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
43
packages/medusa-plugin-economic/package.json
Normal file
43
packages/medusa-plugin-economic/package.json
Normal file
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"name": "medusa-plugin-economic",
|
||||
"version": "1.0.0",
|
||||
"description": "E-conomic financial reporting",
|
||||
"main": "index.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/medusajs/medusa",
|
||||
"directory": "packages/medusa-plugin-economic"
|
||||
},
|
||||
"author": "Oliver Juhl",
|
||||
"license": "AGPL-3.0-or-later",
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.7.5",
|
||||
"@babel/core": "^7.7.5",
|
||||
"@babel/node": "^7.7.4",
|
||||
"@babel/plugin-proposal-class-properties": "^7.7.4",
|
||||
"@babel/plugin-transform-instanceof": "^7.8.3",
|
||||
"@babel/plugin-transform-runtime": "^7.7.6",
|
||||
"@babel/preset-env": "^7.7.5",
|
||||
"@babel/register": "^7.7.4",
|
||||
"@babel/runtime": "^7.9.6",
|
||||
"client-sessions": "^0.8.0",
|
||||
"cross-env": "^5.2.1",
|
||||
"eslint": "^6.8.0",
|
||||
"jest": "^25.5.2"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "babel src -d .",
|
||||
"prepare": "cross-env NODE_ENV=production npm run build",
|
||||
"watch": "babel -w src --out-dir . --ignore **/__tests__",
|
||||
"test": "jest"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/plugin-transform-classes": "^7.9.5",
|
||||
"axios": "^0.19.2",
|
||||
"body-parser": "^1.19.0",
|
||||
"express": "^4.17.1",
|
||||
"medusa-core-utils": "^0.3.0",
|
||||
"medusa-interfaces": "^0.3.0",
|
||||
"medusa-test-utils": "^0.3.0"
|
||||
}
|
||||
}
|
||||
194
packages/medusa-plugin-economic/src/services/economic.js
Normal file
194
packages/medusa-plugin-economic/src/services/economic.js
Normal file
@@ -0,0 +1,194 @@
|
||||
import axios from "axios"
|
||||
import moment from "moment"
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
|
||||
ECONOMIC_BASE_URL = "https://restapi.e-conomic.com"
|
||||
|
||||
class EconomicService extends BaseService {
|
||||
/**
|
||||
* @param {Object} options - options defined in `medusa-config.js`
|
||||
* {
|
||||
* secret_token: "foo",
|
||||
* agreement_token: "bar",
|
||||
* customer_number_dk: 012
|
||||
* customer_number_eu: 345
|
||||
* customer_number_world: 678,
|
||||
* vat_number: 42,
|
||||
* unit_number: 42,
|
||||
* payment_terms_number: 42,
|
||||
* shipping_product_number: 42,
|
||||
* layout_number: 42,
|
||||
* vatzone_number_eu: 42,
|
||||
* vatzone_number_dk: 42,
|
||||
* vatzone_number_world: 42,
|
||||
* recipient_name: "Webshop customer"
|
||||
* }
|
||||
*/
|
||||
constructor({ orderService, totalsService, regionService }, options) {
|
||||
super()
|
||||
|
||||
this.orderService_ = orderService
|
||||
|
||||
this.totalsService_ = totalsService
|
||||
|
||||
this.regionService_ = regionService
|
||||
|
||||
this.options_ = options
|
||||
|
||||
this.economic_ = axios.create({
|
||||
baseURL: ECONOMIC_BASE_URL,
|
||||
headers: {
|
||||
"X-AppSecretToken": options.secret_token,
|
||||
"X-AgreementGrantToken": options.agreement_token,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
decideCustomerAndVatNumber_(country) {
|
||||
// Add these to utils
|
||||
const EUCountries = []
|
||||
const WorldCountries = []
|
||||
|
||||
const flag = false
|
||||
switch (flag) {
|
||||
case EUCountries.includes(country): {
|
||||
return {
|
||||
vat: this.options_.vatzone_number_eu,
|
||||
customer: this.options_.customer_number_eu,
|
||||
}
|
||||
}
|
||||
case WorldCountries.includes(country): {
|
||||
return {
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async createEconomicLinesFromOrder(order) {
|
||||
let order_lines = []
|
||||
// Find the discount, that is not free shipping
|
||||
const discount = order.discounts.find(
|
||||
({ discount_rule }) => discount_rule.type !== "free_shipping"
|
||||
)
|
||||
// If the discount has an item specific allocation method,
|
||||
// we need to fetch the discount for each item
|
||||
const itemDiscounts = await this.totalsService_.getAllocationItemDiscounts(
|
||||
discount,
|
||||
order
|
||||
)
|
||||
|
||||
order.items.forEach((item) => {
|
||||
// For bundles, we create an order line for each item in the bundle
|
||||
if (Array.isArray(item.content)) {
|
||||
item.content.forEach((c) => {
|
||||
const total_amount = c.unit_price * c.quantity * (taxRate + 1)
|
||||
|
||||
order_lines.push({
|
||||
lineNumber: order_lines.length + 1,
|
||||
sortKey: 1,
|
||||
unit: {
|
||||
unitNumber: this.options_.unit_number,
|
||||
},
|
||||
product: {
|
||||
productNumber: c.product.sku,
|
||||
},
|
||||
quantity: c.quantity,
|
||||
// Do we include taxes on this bad boy?
|
||||
unitNetPrice: total_amount,
|
||||
})
|
||||
})
|
||||
} else {
|
||||
const total_amount = item.content.unit_price * item.content.quantity
|
||||
|
||||
// Find the discount for current item and default to 0
|
||||
const itemDiscount =
|
||||
(itemDiscounts &&
|
||||
itemDiscounts.find((el) => el.lineItem._id === item._id)) ||
|
||||
0
|
||||
|
||||
// Withdraw discount from the total item amount
|
||||
const total_discount_amount = total_amount - itemDiscount
|
||||
|
||||
order_lines.push({
|
||||
lineNumber: order_lines.length + 1,
|
||||
sortKey: 1,
|
||||
unit: {
|
||||
unitNumber: this.options_.unit_number,
|
||||
},
|
||||
product: {
|
||||
productNumber: item.content.product.sku,
|
||||
},
|
||||
quantity: item.content.quantity,
|
||||
// Do we include taxes on this bad boy?
|
||||
unitNetPrice: total_discount_amount,
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async createInvoiceFromOrder(order, lineItems) {
|
||||
// Fetch currency code from order region
|
||||
const {
|
||||
currency_code,
|
||||
billing_address,
|
||||
} = await this.regionService_.retrieve(order.region_id)
|
||||
|
||||
const vatZoneAndCustomer = this.decideCustomerAndVatNumber_(
|
||||
billing_address.country
|
||||
)
|
||||
|
||||
return {
|
||||
date: moment().format("YYYY-MM-DD"),
|
||||
currency: currency_code,
|
||||
paymentTerms: {
|
||||
paymentTermsNumber: this.options_.payment_terms_number,
|
||||
},
|
||||
references: {
|
||||
other: order._id,
|
||||
},
|
||||
customer: {
|
||||
customerNumber: vatZoneAndCustomer.customer,
|
||||
},
|
||||
recipient: {
|
||||
name: this.options_.recipient_name,
|
||||
vatZone: {
|
||||
vatZoneNumber: vatZoneAndCustomer.vat,
|
||||
},
|
||||
},
|
||||
layout: {
|
||||
layoutNumber: this.options_.layout_number,
|
||||
},
|
||||
lines,
|
||||
}
|
||||
}
|
||||
|
||||
async draftEconomicInvoice(orderId) {
|
||||
const order = await this.orderService_.retrieve(orderId)
|
||||
const invoice = await this.createInvoiceFromOrder(order)
|
||||
|
||||
try {
|
||||
const draftInvoice = await this.economic_.post(
|
||||
`${ECONOMIC_BASE_URL}/invoices/drafts`,
|
||||
invoice
|
||||
)
|
||||
|
||||
await this.orderService_.setMetadata(
|
||||
order._id,
|
||||
"economicDraftId",
|
||||
draftInvoice.draftInvoiceNumber
|
||||
)
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default EconomicService
|
||||
@@ -0,0 +1,10 @@
|
||||
export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
try {
|
||||
const customerService = req.scope.resolve("customerService")
|
||||
let customer = await customerService.retrieve(id)
|
||||
res.json({ customer })
|
||||
} catch (err) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
export default async (req, res) => {
|
||||
const selector = {}
|
||||
|
||||
try {
|
||||
const customerService = req.scope.resolve("customerService")
|
||||
const customers = await customerService.list(selector)
|
||||
|
||||
res.json({ customers })
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user