-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
default-shipping-calculator.ts
70 lines (65 loc) · 2.33 KB
/
default-shipping-calculator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { LanguageCode } from '@vendure/common/lib/generated-types';
import { RequestContext } from '../../api/common/request-context';
import { ShippingCalculator } from './shipping-calculator';
export enum TaxSetting {
include = 'include',
exclude = 'exclude',
auto = 'auto',
}
export const defaultShippingCalculator = new ShippingCalculator({
code: 'default-shipping-calculator',
description: [{ languageCode: LanguageCode.en, value: 'Default Flat-Rate Shipping Calculator' }],
args: {
rate: {
type: 'int',
defaultValue: 0,
ui: { component: 'currency-form-input' },
label: [{ languageCode: LanguageCode.en, value: 'Shipping price' }],
},
includesTax: {
type: 'string',
defaultValue: TaxSetting.auto,
ui: {
component: 'select-form-input',
options: [
{
label: [{ languageCode: LanguageCode.en, value: 'Includes tax' }],
value: TaxSetting.include,
},
{
label: [{ languageCode: LanguageCode.en, value: 'Excludes tax' }],
value: TaxSetting.exclude,
},
{
label: [{ languageCode: LanguageCode.en, value: 'Auto (based on Channel)' }],
value: TaxSetting.auto,
},
],
},
label: [{ languageCode: LanguageCode.en, value: 'Price includes tax' }],
},
taxRate: {
type: 'int',
defaultValue: 0,
ui: { component: 'number-form-input', suffix: '%' },
label: [{ languageCode: LanguageCode.en, value: 'Tax rate' }],
},
},
calculate: (ctx, order, args) => {
return {
price: args.rate,
taxRate: args.taxRate,
priceIncludesTax: getPriceIncludesTax(ctx, args.includesTax as any),
};
},
});
function getPriceIncludesTax(ctx: RequestContext, setting: TaxSetting): boolean {
switch (setting) {
case TaxSetting.auto:
return ctx.channel.pricesIncludeTax;
case TaxSetting.exclude:
return false;
case TaxSetting.include:
return true;
}
}