11import  Component  from  '@ember/component' ; 
22import  {  computed  }  from  '@ember/object' ; 
3+ import  {  debounce  }  from  '@ember/runloop' ; 
34import  FormMixin  from  'open-event-frontend/mixins/form' ; 
45import  {  inject  as  service  }  from  '@ember/service' ; 
56import  {  sumBy ,  merge  }  from  'lodash-es' ; 
@@ -27,13 +28,6 @@ export default Component.extend(FormMixin, {
2728    } 
2829  } ) , 
2930
30-   showTaxIncludedMessage : computed ( 'taxInfo.isTaxIncludedInPrice' ,  function ( )  { 
31-     if  ( this . taxInfo  !==  null )  { 
32-       return  ( this . taxInfo . isTaxIncludedInPrice ) ; 
33-     } 
34-     return  false ; 
35-   } ) , 
36- 
3731  accessCodeTickets  : A ( ) , 
3832  discountedTickets  : A ( ) , 
3933
@@ -88,8 +82,7 @@ export default Component.extend(FormMixin, {
8882    ) ; 
8983  } ) , 
9084
91-   orderAmountInput : 
computed ( '[email protected] ' ,  'order.discountCode' ,  function ( )  {  92-     console . log ( this . order . tickets . toArray ( ) ) ; 
85+   orderAmountInput : 
computed ( '[email protected] ' ,  '[email protected] ' ,  'order.discountCode' ,  function ( )  {  9386
9487    return  { 
9588      tickets : this . order . tickets . toArray ( ) . map ( ticket  =>  ( { 
@@ -115,15 +108,16 @@ export default Component.extend(FormMixin, {
115108          this . set ( 'code' ,  null ) ; 
116109          this . order . set ( 'accessCode' ,  undefined ) ; 
117110          this . order . set ( 'discountCode' ,  undefined ) ; 
111+           this . tickets . forEach ( ticket  =>  { 
112+             ticket . set ( 'discount' ,  null ) ; 
113+           } ) ; 
118114          this . accessCodeTickets . forEach ( ticket  =>  { 
119115            ticket . set ( 'isHidden' ,  true ) ; 
120116            this . tickets . removeObject ( ticket ) ; 
121117          } ) ; 
122-           this . discountedTickets . forEach ( ticket  =>  { 
123-             ticket . set ( 'discount' ,  0 ) ; 
124-           } ) ; 
125118          this . accessCodeTickets . clear ( ) ; 
126119          this . discountedTickets . clear ( ) ; 
120+           this . send ( 'updateOrderAmount' ) ; 
127121        } 
128122
129123      } 
@@ -150,13 +144,10 @@ export default Component.extend(FormMixin, {
150144        const  discountCode  =  await  this . store . queryRecord ( 'discount-code' ,  {  eventIdentifier : this . event . id ,  code : this . promotionalCode ,  include : 'event,tickets'  } ) ; 
151145        const  discountCodeEvent  =  await  discountCode . event ; 
152146        if  ( this . currentEventIdentifier  ===  discountCodeEvent . identifier )  { 
153-           const  discountType  =  discountCode . type ; 
154-           const  discountValue  =  discountCode . value ; 
155147          this . order . set ( 'discountCode' ,  discountCode ) ; 
156148          const  tickets  =  await  discountCode . tickets ; 
157149          tickets . forEach ( ticket  =>  { 
158-             const  ticketPrice  =  ticket . price ; 
159-             const  discount  =  discountType  ===  'amount'  ? Math . min ( ticketPrice ,  discountValue )  : ticketPrice  *  ( discountValue  /  100 ) ; 
150+             const  discount  =  discountCode . type  ===  'amount'  ? Math . min ( ticket . price ,  discountCode . value )  : ticket . price  *  ( discountCode . value  /  100 ) ; 
160151            ticket . set ( 'discount' ,  discount ) ; 
161152            this . discountedTickets . addObject ( ticket ) ; 
162153            this . set ( 'invalidPromotionalCode' ,  false ) ; 
@@ -179,19 +170,16 @@ export default Component.extend(FormMixin, {
179170        this . set ( 'promotionalCodeApplied' ,  true ) ; 
180171        this . set ( 'promotionalCode' ,  'Promotional code applied successfully' ) ; 
181172      } 
182-       this . order . set ( 'amount' ,  this . total ) ; 
183173      this . send ( 'updateOrderAmount' ) ; 
184174    } , 
185175
186176    async  updateOrder ( ticket ,  count )  { 
187177      ticket . set ( 'orderQuantity' ,  count ) ; 
188-       this . order . set ( 'amount' ,  this . total ) ; 
189-       if  ( ! this . total )  { 
190-         this . order . set ( 'amount' ,  0 ) ; 
191-       } 
192178      if  ( count  >  0 )  { 
193179        this . order . tickets . addObject ( ticket ) ; 
194180      }  else  { 
181+         ticket . set ( 'subTotal' ,  null ) ; 
182+         ticket . set ( 'discountInfo' ,  null ) ; 
195183        if  ( this . order . tickets . includes ( ticket ) )  { 
196184          this . order . tickets . removeObject ( ticket ) ; 
197185        } 
@@ -201,18 +189,18 @@ export default Component.extend(FormMixin, {
201189    } , 
202190
203191    async  updateOrderAmount ( )  { 
204-       console . log ( this . orderAmountInput ) ; 
205- 
206192      this . set ( 'orderAmount' ,  await  this . loader . post ( '/orders/calculate-amount' ,  this . orderAmountInput ) ) ; 
207- 
208-       console . log ( this . orderAmount ) ; 
209193    } , 
210194
211195    handleKeyPress ( )  { 
212196      if  ( event . code  ===  'Enter' )  { 
213197        this . send ( 'applyPromotionalCode' ) ; 
214198        this . set ( 'code' ,  this . promotionalCode ) ; 
215199      } 
200+     } , 
201+ 
202+     onChangeDonation ( )  { 
203+       debounce ( this ,  ( )  =>  this . send ( 'updateOrderAmount' ) ,  this . tickets ,  250 ) ; 
216204    } 
217205  } , 
218206  didInsertElement ( )  { 
0 commit comments