Merge pull request #17931 from civicrm/5.28
[civicrm-core.git] / templates / CRM / Price / Form / Calculate.tpl
CommitLineData
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}
18 <span id='amount_sum_label'>{ts}Total Fee(s){/ts}{if $isAdditionalParticipants} {ts}for this participant{/ts}{/if}</span>
8cdd6191 19 {/if}
70af95e4 20 </div>
07f7746a 21 <div class="content calc-value" {if $hideTotal}style="display:none;"{/if} id="pricevalue"></div>
6a488035 22</div>
70af95e4 23
6a488035
TO
24<script type="text/javascript">
25{literal}
26
6a488035 27var thousandMarker = '{/literal}{$config->monetaryThousandSeparator}{literal}';
2975161b 28var separator = '{/literal}{$config->monetaryDecimalPoint}{literal}';
6a488035
TO
29var symbol = '{/literal}{$currencySymbol}{literal}';
30var optionSep = '|';
5ec4b965 31
07f7746a 32// Recalculate the total fees based on user selection
2975161b 33cj("#priceset [price]").each(function () {
07f7746a
ML
34 var elementType = cj(this).attr('type');
35 if (this.tagName == 'SELECT') {
36 elementType = 'select-one';
37 }
6a488035 38
07f7746a
ML
39 switch(elementType) {
40 case 'checkbox':
41 cj(this).click(function(){
2975161b 42 calculateCheckboxLineItemValue(this);
07f7746a
ML
43 display(calculateTotalFee());
44 });
45 calculateCheckboxLineItemValue(this);
8a653c0c 46 break;
47
48 case 'radio':
8a653c0c 49 cj(this).click( function(){
2975161b 50 calculateRadioLineItemValue(this);
51 display(calculateTotalFee());
8a653c0c 52 });
2975161b 53 calculateRadioLineItemValue(this);
8a653c0c 54 break;
6a488035 55
07f7746a
ML
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.
2975161b 63 calculateText(this);
07f7746a 64 break;
6a488035 65
07f7746a 66 case 'select-one':
2975161b 67 calculateSelectLineItemValue(this);
2975161b 68
07f7746a
ML
69 cj(this).change(function() {
70 calculateSelectLineItemValue(this);
71 display(calculateTotalFee());
72 });
73 break;
6a488035 74 }
07f7746a 75
2975161b 76 display(calculateTotalFee());
6a488035
TO
77});
78
2975161b 79/**
80 * Calculate the value of the line item for a radio value.
81 */
82function 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 */
95function 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
3ab30779 109/**
110 * Calculate the value of the line item for a select value.
111 */
112function calculateSelectLineItemValue(priceElement) {
113 eval( 'var selectedText = ' + cj(priceElement).attr('price') );
2975161b 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]);
3ab30779 119 }
2975161b 120 cj(priceElement).data('line_raw_total', price);
3ab30779 121}
122
2975161b 123/**
124 * Calculate the value of the line item for a text box.
125 */
126function calculateText(priceElement) {
a8036ea1 127 //CRM-16034 - comma acts as decimal in price set text pricing
210072f7 128 //CRM-19937 - dollar sign easy mistake to make by users.
f2509640 129 var textval = parseFloat(cj(priceElement).val().replace(thousandMarker, '').replace(symbol, ''));
2975161b 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());
6a488035 140}
2975161b 141
142/**
143 * Calculate the total fee for the visible priceset.
144 */
145function 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 */
156function display(totalfee) {
5b3bb90c
ML
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;
fa1a821d 160 var totalFormattedFee = symbol + ' ' + CRM.formatMoney(totalfee, true);
5b3bb90c 161 cj('#pricevalue').html(totalFormattedFee);
2975161b 162
5b3bb90c
ML
163 cj('#total_amount').val( totalfee );
164 cj('#pricevalue').data('raw-total', totalfee).trigger('change');
2975161b 165
5b3bb90c
ML
166 if (totalfee < 0) {
167 cj('table#pricelabel').addClass('disabled');
168 }
169 else {
170 cj('table#pricelabel').removeClass('disabled');
171 }
6a488035 172
5b3bb90c
ML
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 }
6a488035 178}
2975161b 179
6a488035
TO
180{/literal}
181</script>