Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | {* |
2 | +--------------------------------------------------------------------+ | |
1188c7a8 | 3 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 4 | | | |
1188c7a8 TO |
5 | | This work is published under the GNU AGPLv3 license with some | |
6 | | permitted exceptions and without any warranty. For full license | | |
7 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 TO |
8 | +--------------------------------------------------------------------+ |
9 | *} | |
70af95e4 | 10 | |
11 | {assign var='hideTotal' value=$quickConfig+$noCalcValueDisplay} | |
07f7746a | 12 | |
6a488035 | 13 | <div id="pricesetTotal" class="crm-section section-pricesetTotal"> |
07f7746a ML |
14 | <div id="pricelabel" class="label {if $hideTotal}hiddenElement{/if}"> |
15 | {if ($extends eq 'Contribution') || ($extends eq 'Membership')} | |
16 | <span id='amount_sum_label'>{ts}Total Amount{/ts}</span> | |
17 | {else} | |
99b30d6a ML |
18 | {if $isAdditionalParticipants} |
19 | <span id='amount_sum_label'>{ts}Total for this participant{/ts}</span> | |
20 | {else} | |
21 | <span id='amount_sum_label'>{ts}Total{/ts}</span> | |
22 | {/if} | |
8cdd6191 | 23 | {/if} |
70af95e4 | 24 | </div> |
07f7746a | 25 | <div class="content calc-value" {if $hideTotal}style="display:none;"{/if} id="pricevalue"></div> |
6a488035 | 26 | </div> |
70af95e4 | 27 | |
6a488035 TO |
28 | <script type="text/javascript"> |
29 | {literal} | |
30 | ||
6a488035 | 31 | var thousandMarker = '{/literal}{$config->monetaryThousandSeparator}{literal}'; |
2975161b | 32 | var separator = '{/literal}{$config->monetaryDecimalPoint}{literal}'; |
6a488035 TO |
33 | var symbol = '{/literal}{$currencySymbol}{literal}'; |
34 | var optionSep = '|'; | |
5ec4b965 | 35 | |
07f7746a | 36 | // Recalculate the total fees based on user selection |
2975161b | 37 | cj("#priceset [price]").each(function () { |
07f7746a ML |
38 | var elementType = cj(this).attr('type'); |
39 | if (this.tagName == 'SELECT') { | |
40 | elementType = 'select-one'; | |
41 | } | |
6a488035 | 42 | |
07f7746a ML |
43 | switch(elementType) { |
44 | case 'checkbox': | |
45 | cj(this).click(function(){ | |
2975161b | 46 | calculateCheckboxLineItemValue(this); |
07f7746a ML |
47 | display(calculateTotalFee()); |
48 | }); | |
49 | calculateCheckboxLineItemValue(this); | |
8a653c0c | 50 | break; |
51 | ||
52 | case 'radio': | |
8a653c0c | 53 | cj(this).click( function(){ |
2975161b | 54 | calculateRadioLineItemValue(this); |
55 | display(calculateTotalFee()); | |
8a653c0c | 56 | }); |
2975161b | 57 | calculateRadioLineItemValue(this); |
8a653c0c | 58 | break; |
6a488035 | 59 | |
07f7746a ML |
60 | case 'text': |
61 | cj(this).bind( 'keyup', function() { | |
62 | calculateText(this); | |
63 | }).bind( 'blur' , function() { | |
64 | calculateText(this); | |
65 | }); | |
66 | //default calculation of element. | |
2975161b | 67 | calculateText(this); |
07f7746a | 68 | break; |
6a488035 | 69 | |
07f7746a | 70 | case 'select-one': |
2975161b | 71 | calculateSelectLineItemValue(this); |
2975161b | 72 | |
07f7746a ML |
73 | cj(this).change(function() { |
74 | calculateSelectLineItemValue(this); | |
75 | display(calculateTotalFee()); | |
76 | }); | |
77 | break; | |
6a488035 | 78 | } |
07f7746a | 79 | |
2975161b | 80 | display(calculateTotalFee()); |
6a488035 TO |
81 | }); |
82 | ||
2975161b | 83 | /** |
84 | * Calculate the value of the line item for a radio value. | |
85 | */ | |
86 | function calculateCheckboxLineItemValue(priceElement) { | |
87 | eval( 'var option = ' + cj(priceElement).attr('price') ) ; | |
88 | optionPart = option[1].split(optionSep); | |
89 | price = parseFloat(0); | |
90 | if (cj(priceElement).prop('checked')) { | |
91 | price = parseFloat(optionPart[0]); | |
92 | } | |
93 | cj(priceElement).data('line_raw_total', price); | |
94 | } | |
95 | ||
96 | /** | |
97 | * Calculate the value of the line item for a radio value. | |
98 | */ | |
99 | function calculateRadioLineItemValue(priceElement) { | |
100 | eval( 'var option = ' + cj(priceElement).attr('price') ); | |
101 | optionPart = option[1].split(optionSep); | |
102 | var lineTotal = parseFloat(optionPart[0]); | |
103 | cj(priceElement).data('line_raw_total', lineTotal); | |
104 | var radionGroupName = cj(priceElement).attr("name"); | |
105 | // Reset all unchecked options to having a data value of 0. | |
106 | cj('input[name=' + radionGroupName + ']:radio:unchecked').each( | |
107 | function () { | |
108 | cj(this).data('line_raw_total', 0); | |
109 | } | |
110 | ); | |
111 | } | |
112 | ||
3ab30779 | 113 | /** |
114 | * Calculate the value of the line item for a select value. | |
115 | */ | |
116 | function calculateSelectLineItemValue(priceElement) { | |
117 | eval( 'var selectedText = ' + cj(priceElement).attr('price') ); | |
2975161b | 118 | var price = parseFloat('0'); |
119 | var option = cj(priceElement).val(); | |
120 | if (option) { | |
121 | optionPart = selectedText[option].split(optionSep); | |
122 | price = parseFloat(optionPart[0]); | |
3ab30779 | 123 | } |
2975161b | 124 | cj(priceElement).data('line_raw_total', price); |
3ab30779 | 125 | } |
126 | ||
2975161b | 127 | /** |
128 | * Calculate the value of the line item for a text box. | |
129 | */ | |
130 | function calculateText(priceElement) { | |
a8036ea1 | 131 | //CRM-16034 - comma acts as decimal in price set text pricing |
210072f7 | 132 | //CRM-19937 - dollar sign easy mistake to make by users. |
f2509640 | 133 | var textval = parseFloat(cj(priceElement).val().replace(thousandMarker, '').replace(symbol, '')); |
2975161b | 134 | |
135 | if (isNaN(textval)) { | |
136 | textval = parseFloat(0); | |
137 | } | |
138 | eval('var option = '+ cj(priceElement).attr('price')); | |
139 | optionPart = option[1].split(optionSep); | |
140 | addprice = parseFloat(optionPart[0]); | |
141 | var curval = textval * addprice; | |
142 | cj(priceElement).data('line_raw_total', curval); | |
143 | display(calculateTotalFee()); | |
6a488035 | 144 | } |
2975161b | 145 | |
146 | /** | |
147 | * Calculate the total fee for the visible priceset. | |
148 | */ | |
149 | function calculateTotalFee() { | |
150 | var totalFee = 0; | |
151 | cj("#priceset [price]").each(function () { | |
152 | totalFee = totalFee + cj(this).data('line_raw_total'); | |
153 | }); | |
154 | return totalFee; | |
155 | } | |
156 | ||
157 | /** | |
158 | * Display calculated amount. | |
159 | */ | |
160 | function display(totalfee) { | |
5b3bb90c ML |
161 | // totalfee is monetary, round it to 2 decimal points so it can |
162 | // go as a float - CRM-13491 | |
163 | totalfee = Math.round(totalfee*100)/100; | |
fa1a821d | 164 | var totalFormattedFee = symbol + ' ' + CRM.formatMoney(totalfee, true); |
5b3bb90c | 165 | cj('#pricevalue').html(totalFormattedFee); |
2975161b | 166 | |
5b3bb90c ML |
167 | cj('#total_amount').val( totalfee ); |
168 | cj('#pricevalue').data('raw-total', totalfee).trigger('change'); | |
2975161b | 169 | |
5b3bb90c ML |
170 | if (totalfee < 0) { |
171 | cj('table#pricelabel').addClass('disabled'); | |
172 | } | |
173 | else { | |
174 | cj('table#pricelabel').removeClass('disabled'); | |
175 | } | |
6a488035 | 176 | |
5b3bb90c ML |
177 | if (typeof skipPaymentMethod == 'function') { |
178 | // Advice to anyone who, like me, feels hatred towards this if construct ... if you remove the if you | |
179 | // get an error on participant 2 of a event that requires approval & permits multiple registrants. | |
180 | skipPaymentMethod(); | |
181 | } | |
6a488035 | 182 | } |
2975161b | 183 | |
6a488035 TO |
184 | {/literal} |
185 | </script> |