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