Finalizes first draft of economic plugin
This commit is contained in:
@@ -17,7 +17,11 @@ class EconomicService extends BaseService {
|
|||||||
* unit_number: 42,
|
* unit_number: 42,
|
||||||
* payment_terms_number: 42,
|
* payment_terms_number: 42,
|
||||||
* shipping_product_number: 42,
|
* shipping_product_number: 42,
|
||||||
* layout_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) {
|
constructor({ orderService, totalsService, regionService }, options) {
|
||||||
@@ -36,10 +40,38 @@ class EconomicService extends BaseService {
|
|||||||
headers: {
|
headers: {
|
||||||
"X-AppSecretToken": options.secret_token,
|
"X-AppSecretToken": options.secret_token,
|
||||||
"X-AgreementGrantToken": options.agreement_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) {
|
async createEconomicLinesFromOrder(order) {
|
||||||
let order_lines = []
|
let order_lines = []
|
||||||
// Find the discount, that is not free shipping
|
// Find the discount, that is not free shipping
|
||||||
@@ -63,7 +95,7 @@ class EconomicService extends BaseService {
|
|||||||
lineNumber: order_lines.length + 1,
|
lineNumber: order_lines.length + 1,
|
||||||
sortKey: 1,
|
sortKey: 1,
|
||||||
unit: {
|
unit: {
|
||||||
unitNumber: 1,
|
unitNumber: this.options_.unit_number,
|
||||||
},
|
},
|
||||||
product: {
|
product: {
|
||||||
productNumber: c.product.sku,
|
productNumber: c.product.sku,
|
||||||
@@ -89,7 +121,7 @@ class EconomicService extends BaseService {
|
|||||||
lineNumber: order_lines.length + 1,
|
lineNumber: order_lines.length + 1,
|
||||||
sortKey: 1,
|
sortKey: 1,
|
||||||
unit: {
|
unit: {
|
||||||
unitNumber: 1,
|
unitNumber: this.options_.unit_number,
|
||||||
},
|
},
|
||||||
product: {
|
product: {
|
||||||
productNumber: item.content.product.sku,
|
productNumber: item.content.product.sku,
|
||||||
@@ -104,30 +136,35 @@ class EconomicService extends BaseService {
|
|||||||
|
|
||||||
async createInvoiceFromOrder(order, lineItems) {
|
async createInvoiceFromOrder(order, lineItems) {
|
||||||
// Fetch currency code from order region
|
// Fetch currency code from order region
|
||||||
const { currency_code } = await this.regionService_.retrieve(
|
const {
|
||||||
order.region_id
|
currency_code,
|
||||||
|
billing_address,
|
||||||
|
} = await this.regionService_.retrieve(order.region_id)
|
||||||
|
|
||||||
|
const vatZoneAndCustomer = this.decideCustomerAndVatNumber_(
|
||||||
|
billing_address.country
|
||||||
)
|
)
|
||||||
|
|
||||||
const invoice = {
|
return {
|
||||||
date: moment().format("YYYY-MM-DD"),
|
date: moment().format("YYYY-MM-DD"),
|
||||||
currency: currency_code,
|
currency: currency_code,
|
||||||
paymentTerms: {
|
paymentTerms: {
|
||||||
paymentTermsNumber: 14,
|
paymentTermsNumber: this.options_.payment_terms_number,
|
||||||
},
|
},
|
||||||
references: {
|
references: {
|
||||||
other: order._id,
|
other: order._id,
|
||||||
},
|
},
|
||||||
customer: {
|
customer: {
|
||||||
customerNumber,
|
customerNumber: vatZoneAndCustomer.customer,
|
||||||
},
|
},
|
||||||
recipient: {
|
recipient: {
|
||||||
name: "Webshop Customer",
|
name: this.options_.recipient_name,
|
||||||
vatZone: {
|
vatZone: {
|
||||||
vatZoneNumber,
|
vatZoneNumber: vatZoneAndCustomer.vat,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
layout: {
|
layout: {
|
||||||
layoutNumber: 19,
|
layoutNumber: this.options_.layout_number,
|
||||||
},
|
},
|
||||||
lines,
|
lines,
|
||||||
}
|
}
|
||||||
@@ -135,6 +172,22 @@ 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)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user