@win2win/shared 1.0.130 → 1.0.132
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { TipoIVA } from '../enums';
|
|
2
|
+
interface PartialProduct {
|
|
3
|
+
TIPO_IVA?: TipoIVA;
|
|
4
|
+
PRECIO?: number;
|
|
5
|
+
}
|
|
6
|
+
export declare class ProductPrice {
|
|
7
|
+
private ivaRate;
|
|
8
|
+
private netPrice;
|
|
9
|
+
constructor(product: PartialProduct);
|
|
10
|
+
getNetPrice(): number;
|
|
11
|
+
getIVAPercent(): number;
|
|
12
|
+
getIVA(): number;
|
|
13
|
+
getTotalPrice(): number;
|
|
14
|
+
getPVP(includeIVA?: boolean): number;
|
|
15
|
+
private roundToDecimals;
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ProductPrice = void 0;
|
|
4
|
+
const enums_1 = require("../enums");
|
|
5
|
+
const IVA_RATE = {
|
|
6
|
+
[enums_1.TipoIVA.GENERAL]: 0.21,
|
|
7
|
+
[enums_1.TipoIVA.REDUCIDO]: 0.1,
|
|
8
|
+
[enums_1.TipoIVA.SUPERREDUCIDO]: 0.04,
|
|
9
|
+
[enums_1.TipoIVA.EXENTO]: 0,
|
|
10
|
+
};
|
|
11
|
+
class ProductPrice {
|
|
12
|
+
constructor(product) {
|
|
13
|
+
this.ivaRate = IVA_RATE[product.TIPO_IVA || enums_1.TipoIVA.GENERAL];
|
|
14
|
+
this.netPrice = Number(product.PRECIO || 0);
|
|
15
|
+
}
|
|
16
|
+
getNetPrice() {
|
|
17
|
+
return this.roundToDecimals(this.netPrice);
|
|
18
|
+
}
|
|
19
|
+
getIVAPercent() {
|
|
20
|
+
return this.ivaRate * 100;
|
|
21
|
+
}
|
|
22
|
+
getIVA() {
|
|
23
|
+
return this.roundToDecimals(this.getNetPrice() * this.ivaRate);
|
|
24
|
+
}
|
|
25
|
+
getTotalPrice() {
|
|
26
|
+
return this.getNetPrice() + this.getIVA();
|
|
27
|
+
}
|
|
28
|
+
getPVP(includeIVA = true) {
|
|
29
|
+
if (includeIVA) {
|
|
30
|
+
return this.getTotalPrice();
|
|
31
|
+
}
|
|
32
|
+
return this.getNetPrice();
|
|
33
|
+
}
|
|
34
|
+
roundToDecimals(value, decimals = 2) {
|
|
35
|
+
return Number(value.toFixed(decimals));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.ProductPrice = ProductPrice;
|
|
@@ -7,3 +7,6 @@ export declare function deserialize(stringFn?: string, options?: DeserializeOpti
|
|
|
7
7
|
export declare function deserializeAndRun(stringFn?: string): any;
|
|
8
8
|
export declare function deserializeWithContext(stringFn?: string, context?: Record<string, any>): any;
|
|
9
9
|
export declare function deserializeWithContextAndRun(stringFn?: string, context?: Record<string, any>): any;
|
|
10
|
+
export declare function toCurrency(value: number, symbol?: string, options?: {
|
|
11
|
+
decimals: number;
|
|
12
|
+
}): string;
|
|
@@ -4,6 +4,7 @@ exports.deserialize = deserialize;
|
|
|
4
4
|
exports.deserializeAndRun = deserializeAndRun;
|
|
5
5
|
exports.deserializeWithContext = deserializeWithContext;
|
|
6
6
|
exports.deserializeWithContextAndRun = deserializeWithContextAndRun;
|
|
7
|
+
exports.toCurrency = toCurrency;
|
|
7
8
|
function deserialize(stringFn, options) {
|
|
8
9
|
const { context = {}, run = false, useInvisibleContext = false, } = options || {};
|
|
9
10
|
if (!stringFn || typeof stringFn !== "string")
|
|
@@ -11,7 +12,7 @@ function deserialize(stringFn, options) {
|
|
|
11
12
|
const isEmpty = (obj) => JSON.stringify(obj || {}) === "{}";
|
|
12
13
|
try {
|
|
13
14
|
const cleanedStringFn = stringFn.replace(/\/\/.*$/gm, "").trim();
|
|
14
|
-
const fn = new Function("context", `return (...args) => { with (context) { return (${cleanedStringFn})(${
|
|
15
|
+
const fn = new Function("context", `return (...args) => { with (context) { return (${cleanedStringFn})(${useInvisibleContext || isEmpty(context) ? "" : "context,"} ...args) } }`);
|
|
15
16
|
const runableFn = fn(context);
|
|
16
17
|
return run ? runableFn() : runableFn;
|
|
17
18
|
}
|
|
@@ -37,3 +38,18 @@ function deserializeWithContextAndRun(stringFn, context = {}) {
|
|
|
37
38
|
run: true,
|
|
38
39
|
});
|
|
39
40
|
}
|
|
41
|
+
function toCurrency(value, symbol = "EUR", options) {
|
|
42
|
+
const { decimals = 2 } = options || {};
|
|
43
|
+
const locale = {
|
|
44
|
+
EUR: "de-DE",
|
|
45
|
+
USD: "en-US",
|
|
46
|
+
MXN: "es-MX",
|
|
47
|
+
}[symbol] || "de-DE";
|
|
48
|
+
const formatted = new Intl.NumberFormat(locale, {
|
|
49
|
+
style: "currency",
|
|
50
|
+
currency: symbol,
|
|
51
|
+
minimumFractionDigits: decimals,
|
|
52
|
+
maximumFractionDigits: decimals,
|
|
53
|
+
}).format(value);
|
|
54
|
+
return formatted;
|
|
55
|
+
}
|
package/dist/helpers/index.d.ts
CHANGED
package/dist/helpers/index.js
CHANGED
|
@@ -15,4 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./formatters"), exports);
|
|
18
|
+
__exportStar(require("./ProductPrice"), exports);
|
|
18
19
|
__exportStar(require("./validators"), exports);
|
package/package.json
CHANGED