-
Notifications
You must be signed in to change notification settings - Fork 7
/
priceCalculator.js
62 lines (51 loc) · 3.03 KB
/
priceCalculator.js
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
var pricing = require('./pricing.js')
module.exports = function calculator(printOptions,volume) {
// make sure printer type is defined in pricing object
if (typeof pricing[printOptions.printer] === 'undefined') {
throw new Error('invalid printer type: ' + printOptions.printer)
}
// just to make things simpler
var printerPricing = pricing[printOptions.printer]
// make sure material and color is defined in pricing object
if (typeof printerPricing[printOptions.material] === 'undefined') {
throw new Error('invalid print material for printer type: ' + printOptions.material)
} else if (typeof printerPricing[printOptions.material][printOptions.color] === 'undefined') {
throw new Error('invalid print color for material and printer type: ' + printOptions.color)
}
// make sure layer resolution discount is defined in pricing object
if (typeof printerPricing.layerResolutionDiscount === 'undefined') {
throw new Error('undefined layer resolution discount: ' + printerPricing.layerResolutionDiscount)
} else if (typeof printerPricing.layerResolutionDiscount[printOptions.layerResolution] === 'undefined') {
throw new Error('invalid layer resolution: ' + printOptions.layerResolution)
}
// make sure infillDiscountThreshhold is a number and is between 0 and 100
if (isNaN(printerPricing.infillDiscountThreshhold) || printerPricing.infillDiscountThreshhold < 0 || printerPricing.infillDiscountThreshhold > 100) {
throw new Error('invalid infill discount threshhold: ' + printerPricing.infillDiscountThreshhold)
}
// make sure all necessary variables are numbers
if (isNaN(printerPricing[printOptions.material][printOptions.color])) {
throw new Error('price for printer, material, and color is NaN: ' + printerPricing[printOptions.material][printOptions.color])
} else if (isNaN(printerPricing.infillDiscount)) {
throw new Error('infill discount is NaN: ' + printerPricing.infillDiscount)
} else if (isNaN(printerPricing.layerResolutionDiscount[printOptions.layerResolution])) {
throw new Error('layer resolution discount is NaN: ' + printerPricing.layerResolutionDiscount[printOptions.layerResolution])
} else if (isNaN(printerPricing.basePrice)) {
throw new Error('minimum price is NaN: ' + printerPricing.basePrice)
}
// make sure volume is a number greater than zero
if (isNaN(volume)) {
throw new Error('volume is NaN: ' + volume)
} else if (volume <= 0) {
throw new Error('volume is not greater than zero: ' + volume)
}
var pricePerCC = printerPricing[printOptions.material][printOptions.color]
var infillDiscount = (printOptions.percentInfill <= printerPricing.infillDiscountThreshhold ?
printerPricing.infillDiscount : 0)
var layerResolutionDiscount = printerPricing.layerResolutionDiscount[printOptions.layerResolution]
var totalDiscount = infillDiscount + layerResolutionDiscount
// make sure discount applied is not greater than 95%
if (totalDiscount > 0.95) {
throw new Error('total discount is greater than 95%: ' + totalDiscount)
}
return ((volume * pricePerCC * (1 - totalDiscount) / 1000) + printerPricing.basePrice)
}