@@ -28,25 +28,45 @@ import LineItems from '../../../components/invoices/line-items';
28
28
import FooterToolbar from '../../../components/layout/footer-toolbar' ;
29
29
import { OrganizationContext } from '../../../providers/contexts' ;
30
30
31
+ let discountValue ;
32
+ let discountType ;
33
+
31
34
const totals = ( lineItems , taxRates ) => {
32
35
let subTotal = currency ( 0 , { separator : '' , symbol : '' } ) ;
33
36
let taxTotal = currency ( 0 , { separator : '' , symbol : '' } ) ;
37
+ let discount = currency ( 0 , { separator : '' , symbol : '' } ) ;
38
+
34
39
35
40
forEach ( lineItems , line => {
36
41
if ( has ( line , 'subtotal' ) ) {
37
42
subTotal = subTotal . add ( line . subtotal ) ;
43
+
44
+ let lineDiscount ;
45
+ if ( discountValue !== 0 | discountValue !== "" ) {
46
+ if ( discountType === "%" ) {
47
+ lineDiscount = currency ( line . subtotal ) . multiply ( discountValue / 100 ) ;
48
+ } else if ( discountType === "currency" ) {
49
+ lineDiscount = currency ( lineDiscount ) . add ( discountValue / lineItems . length ) ;
50
+ }
51
+ }
38
52
if ( has ( line , 'taxRate' ) ) {
39
- const taxRate = get ( taxRates . items , line . taxRate ) ;
53
+ const taxRate = get ( taxRates . items , line . taxRate )
54
+
40
55
if ( taxRate ) {
41
- const lineTax = currency ( line . subtotal ) . multiply ( taxRate . percentage / 100 ) ;
56
+ const afterDiscount = currency ( line . subtotal ) . subtract ( lineDiscount )
57
+ const lineTax = afterDiscount . multiply ( taxRate . percentage / 100 ) ;
42
58
taxTotal = taxTotal . add ( lineTax ) ;
43
59
}
44
60
}
61
+
62
+ discount = discount . add ( lineDiscount )
45
63
}
46
64
} ) ;
47
65
48
- const total = subTotal . add ( taxTotal ) ;
49
- return { subTotal, taxTotal, total } ;
66
+
67
+ const total = currency ( subTotal , { separator : '' , symbol : '' } ) . subtract ( discount ) . add ( taxTotal ) ;
68
+ return { subTotal, taxTotal, total, discount} ;
69
+
50
70
} ;
51
71
52
72
class InvoiceForm extends Component {
@@ -83,6 +103,16 @@ class InvoiceForm extends Component {
83
103
}
84
104
} ;
85
105
106
+ onDiscountTypeChange = ( value ) => {
107
+ discountType = value
108
+ }
109
+
110
+ onDiscountChange = ( value ) => {
111
+ if ( value !== "" | value !== 0 ) {
112
+ discountValue = value
113
+ }
114
+ } ;
115
+
86
116
onStateSelect = ( _id , _rev , key ) => {
87
117
this . props . dispatch ( {
88
118
type : 'invoices/state' ,
@@ -132,7 +162,7 @@ class InvoiceForm extends Component {
132
162
handleSubmit,
133
163
submitting,
134
164
} = this . props ;
135
- const { subTotal, taxTotal, total } = totals ( lineItems , taxRates ) ;
165
+ const { subTotal, taxTotal, total, discount } = totals ( lineItems , taxRates ) ;
136
166
const invoice = get ( invoices . items , get ( this . props , [ 'match' , 'params' , 'id' ] ) ) ;
137
167
138
168
const stateMenu = ( _id , _rev ) => (
@@ -261,6 +291,24 @@ class InvoiceForm extends Component {
261
291
</ Col >
262
292
</ Row >
263
293
294
+ < Row gutter = { 16 } >
295
+ < Col span = { 5 } style = { { marginTop : '20px' } } >
296
+ < Field
297
+ name = "discountValue"
298
+ component = { AInput }
299
+ label = { < Trans > Discount</ Trans > }
300
+ onChange = { ( event , newValue ) =>
301
+ this . onDiscountChange ( newValue )
302
+ }
303
+ />
304
+ </ Col >
305
+ < Col span = { 3 } style = { { marginTop : '20px' } } >
306
+ < Field name = "discountType" component = { ASelect } onDrop = { e => e . preventDefault ( ) } label = " " defaultValue = "%" onChange = { ( value ) => this . onDiscountTypeChange ( value ) } >
307
+ < Select . Option value = "%" > %</ Select . Option >
308
+ < Select . Option value = "currency" > { currency } </ Select . Option >
309
+ </ Field >
310
+ </ Col >
311
+ </ Row >
264
312
< Row gutter = { 16 } >
265
313
< Col span = { 8 } style = { { marginTop : '20px' } } >
266
314
< Field
@@ -299,6 +347,26 @@ class InvoiceForm extends Component {
299
347
</ h4 >
300
348
</ td >
301
349
</ tr >
350
+ { discount > 0 && < tr >
351
+ < td style = { { textAlign : 'right' } } >
352
+ < Trans > Discount</ Trans >
353
+ </ td >
354
+ < td style = { { textAlign : 'right' } } >
355
+ < NumberFormat
356
+ value = { discount }
357
+ format = { {
358
+ style : 'currency' ,
359
+ currency :
360
+ currency || get ( context . state , 'organization.currency' , 'EUR' ) ,
361
+ minimumFractionDigits : get (
362
+ context . state ,
363
+ 'organization.minimum_fraction_digits' ,
364
+ 2
365
+ ) ,
366
+ } }
367
+ />
368
+ </ td >
369
+ </ tr > }
302
370
< tr >
303
371
< td style = { { textAlign : 'right' } } >
304
372
< Trans > Tax</ Trans >
@@ -432,18 +500,22 @@ export default withI18n()(
432
500
''
433
501
) ,
434
502
lineItems : [ { } ] ,
503
+ discountValue : selector ( state , 'discountValue' ) ,
504
+ discountType : selector ( state , 'discountType' ) ,
505
+ discount : selector ( state , 'discount' ) ,
435
506
} ,
436
507
} ) ) (
437
508
reduxForm ( {
438
509
form : 'invoice' ,
439
510
onSubmit : async ( data , dispatch , props ) => {
440
511
const { lineItems, taxRates } = props ;
441
- const { subTotal, taxTotal, total } = totals ( lineItems , taxRates ) ;
512
+ const { subTotal, taxTotal, total, discount } = totals ( lineItems , taxRates ) ;
442
513
return await dispatch ( {
443
514
type : 'invoices/save' ,
444
515
data : {
445
516
...data ,
446
517
subTotal : subTotal . format ( ) ,
518
+ discount : discount . format ( ) ,
447
519
taxTotal : taxTotal . format ( ) ,
448
520
total : total . format ( ) ,
449
521
} ,
0 commit comments