btcpayserver/BTCPayServer/wwwroot/pos/common.js

118 lines
4.7 KiB
JavaScript

const posCommon = {
data () {
return {
...srvModel,
amount: null,
tip: null,
tipPercent: null,
discount: null,
discountPercent: null,
payButtonLoading: false
}
},
computed: {
amountNumeric () {
const value = parseFloat(this.amount)
return isNaN(value) ? 0.0 : parseFloat(value.toFixed(this.currencyInfo.divisibility))
},
discountPercentNumeric () {
const value = parseFloat(this.discountPercent)
return isNaN(value) ? 0.0 : parseFloat(value.toFixed(this.currencyInfo.divisibility))
},
discountNumeric () {
return this.amountNumeric && this.discountPercentNumeric
? parseFloat((this.amountNumeric * (this.discountPercentNumeric / 100)).toFixed(this.currencyInfo.divisibility))
: 0.0;
},
amountMinusDiscountNumeric () {
return parseFloat((this.amountNumeric - this.discountNumeric).toFixed(this.currencyInfo.divisibility))
},
tipNumeric () {
if (this.tipPercent) {
return parseFloat((this.amountMinusDiscountNumeric * (this.tipPercent / 100)).toFixed(this.currencyInfo.divisibility))
} else {
if (this.tip < 0) {
this.tip = 0
}
const value = parseFloat(this.tip)
return isNaN(value) ? 0.0 : parseFloat(value.toFixed(this.currencyInfo.divisibility))
}
},
total () {
return this.amountNumeric - this.discountNumeric + this.tipNumeric
},
totalNumeric () {
return parseFloat(parseFloat(this.total).toFixed(this.currencyInfo.divisibility))
},
calculation () {
if (!this.tipNumeric && !this.discountNumeric) return null
let calc = this.formatCurrency(this.amountNumeric, true)
if (this.discountNumeric > 0) calc += ` - ${this.formatCurrency(this.discountNumeric, true)} (${this.discountPercent}%)`
if (this.tipNumeric > 0) calc += ` + ${this.formatCurrency(this.tipNumeric, true)}`
if (this.tipPercent) calc += ` (${this.tipPercent}%)`
return calc
},
posdata () {
const data = {
subTotal: this.amountNumeric,
total: this.totalNumeric
}
if (this.tipNumeric > 0) data.tip = this.tipNumeric
if (this.discountNumeric > 0) data.discountAmount = this.discountNumeric
if (this.discountPercentNumeric > 0) data.discountPercentage = this.discountPercentNumeric
return JSON.stringify(data)
}
},
watch: {
discountPercent (val) {
const value = parseFloat(val)
if (isNaN(value)) this.discountPercent = null
else if (value < 0) this.discountPercent = '0'
else if (value > 100) this.discountPercent = '100'
else this.discountPercent = value.toString()
},
tip (val) {
this.tipPercent = null
}
},
methods: {
handleFormSubmit() {
this.payButtonLoading = true;
},
getLocale(currency) {
switch (currency) {
case 'USD': return 'en-US'
case 'EUR': return 'de-DE'
case 'JPY': return 'ja-JP'
default: return navigator.language
}
},
tipPercentage (percentage) {
this.tipPercent = this.tipPercent !== percentage
? percentage
: null;
},
unsetPayButtonLoading () {
this.payButtonLoading = false
},
formatCrypto (value, withSymbol) {
const symbol = withSymbol ? ` ${this.currencySymbol || this.currencyCode}` : ''
const { divisibility } = this.currencyInfo
return parseFloat(value).toFixed(divisibility) + symbol
},
formatCurrency (value, withSymbol) {
const currency = this.currencyCode
if (currency === 'BTC' || currency === 'SATS') return this.formatCrypto(value, withSymbol)
const { divisibility } = this.currencyInfo
const locale = this.getLocale(currency);
const style = withSymbol ? 'currency' : 'decimal'
const opts = { currency, style, maximumFractionDigits: divisibility, minimumFractionDigits: divisibility }
try {
return new Intl.NumberFormat(locale, opts).format(value)
} catch (err) {
return this.formatCrypto(value, withSymbol)
}
},
}
}