1
1
import { Model } from 'objection' ;
2
2
import TenantModel from 'models/TenantModel' ;
3
3
import { getExlusiveTaxAmount , getInclusiveTaxAmount } from '@/utils/taxRate' ;
4
+ import { DiscountType } from '@/interfaces' ;
5
+
6
+ // Subtotal (qty * rate) (tax inclusive)
7
+ // Subtotal Tax Exclusive (Subtotal - Tax Amount)
8
+ // Discount (Is percentage ? amount * discount : discount)
9
+ // Total (Subtotal - Discount)
4
10
5
11
export default class ItemEntry extends TenantModel {
6
12
public taxRate : number ;
7
13
public discount : number ;
8
14
public quantity : number ;
9
15
public rate : number ;
10
16
public isInclusiveTax : number ;
11
-
17
+ public discountType : DiscountType ;
12
18
/**
13
19
* Table name.
14
20
* @returns {string }
@@ -31,10 +37,24 @@ export default class ItemEntry extends TenantModel {
31
37
*/
32
38
static get virtualAttributes ( ) {
33
39
return [
40
+ // Amount (qty * rate)
34
41
'amount' ,
42
+
35
43
'taxAmount' ,
36
- 'amountExludingTax' ,
37
- 'amountInclusingTax' ,
44
+
45
+ // Subtotal (qty * rate) + (tax inclusive)
46
+ 'subtotalInclusingTax' ,
47
+
48
+ // Subtotal Tax Exclusive (Subtotal - Tax Amount)
49
+ 'subtotalExcludingTax' ,
50
+
51
+ // Subtotal (qty * rate) + (tax inclusive)
52
+ 'subtotal' ,
53
+
54
+ // Discount (Is percentage ? amount * discount : discount)
55
+ 'discountAmount' ,
56
+
57
+ // Total (Subtotal - Discount)
38
58
'total' ,
39
59
] ;
40
60
}
@@ -45,7 +65,15 @@ export default class ItemEntry extends TenantModel {
45
65
* @returns {number }
46
66
*/
47
67
get total ( ) {
48
- return this . amountInclusingTax ;
68
+ return this . subtotal - this . discountAmount ;
69
+ }
70
+
71
+ /**
72
+ * Total (excluding tax).
73
+ * @returns {number }
74
+ */
75
+ get totalExcludingTax ( ) {
76
+ return this . subtotalExcludingTax - this . discountAmount ;
49
77
}
50
78
51
79
/**
@@ -57,19 +85,27 @@ export default class ItemEntry extends TenantModel {
57
85
return this . quantity * this . rate ;
58
86
}
59
87
88
+ /**
89
+ * Subtotal amount (tax inclusive).
90
+ * @returns {number }
91
+ */
92
+ get subtotal ( ) {
93
+ return this . subtotalInclusingTax ;
94
+ }
95
+
60
96
/**
61
97
* Item entry amount including tax.
62
98
* @returns {number }
63
99
*/
64
- get amountInclusingTax ( ) {
100
+ get subtotalInclusingTax ( ) {
65
101
return this . isInclusiveTax ? this . amount : this . amount + this . taxAmount ;
66
102
}
67
103
68
104
/**
69
- * Item entry amount excluding tax.
105
+ * Subtotal amount ( tax exclusive) .
70
106
* @returns {number }
71
107
*/
72
- get amountExludingTax ( ) {
108
+ get subtotalExcludingTax ( ) {
73
109
return this . isInclusiveTax ? this . amount - this . taxAmount : this . amount ;
74
110
}
75
111
@@ -78,7 +114,9 @@ export default class ItemEntry extends TenantModel {
78
114
* @returns {number }
79
115
*/
80
116
get discountAmount ( ) {
81
- return this . amount * ( this . discount / 100 ) ;
117
+ return this . discountType === DiscountType . Percentage
118
+ ? this . amount * ( this . discount / 100 )
119
+ : this . discount ;
82
120
}
83
121
84
122
/**
0 commit comments