Merge pull request #8368 from mlutfy/master-crm18555
[civicrm-core.git] / templates / CRM / Price / Form / Calculate.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.7 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2016 |
6 +--------------------------------------------------------------------+
7 | This file is a part of CiviCRM. |
8 | |
9 | CiviCRM is free software; you can copy, modify, and distribute it |
10 | under the terms of the GNU Affero General Public License |
11 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
12 | |
13 | CiviCRM is distributed in the hope that it will be useful, but |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
16 | See the GNU Affero General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Affero General Public |
19 | License and the CiviCRM Licensing Exception along |
20 | with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25 *}
26
27 {assign var='hideTotal' value=$quickConfig+$noCalcValueDisplay}
28 <div id="pricesetTotal" class="crm-section section-pricesetTotal">
29 <div class="label
30 {if $hideTotal}, hiddenElement{/if}" id="pricelabel">
31 <label>
32 {if ( $extends eq 'Contribution' ) || ( $extends eq 'Membership' )}
33 <span id='amount_sum_label'>{ts}Total Amount{/ts}{else}{ts}Total Fee(s){/ts}</span>
34 {if $isAdditionalParticipants} {ts}for this participant{/ts}{/if}
35 {/if}
36 </label>
37 </div>
38 <div class="content calc-value" {if $hideTotal}style="display:none;"{/if} id="pricevalue" ></div>
39 </div>
40
41 <script type="text/javascript">
42 {literal}
43
44 var thousandMarker = '{/literal}{$config->monetaryThousandSeparator}{literal}';
45 var separator = '{/literal}{$config->monetaryDecimalPoint}{literal}';
46 var symbol = '{/literal}{$currencySymbol}{literal}';
47 var optionSep = '|';
48
49 cj("#priceset [price]").each(function () {
50
51 var elementType = cj(this).attr('type');
52 if ( this.tagName == 'SELECT' ) {
53 elementType = 'select-one';
54 }
55
56 switch(elementType) {
57 case 'checkbox':
58 //event driven calculation of element.
59 cj(this).click(function(){
60 calculateCheckboxLineItemValue(this);
61 display(calculateTotalFee());
62 });
63 calculateCheckboxLineItemValue(this);
64 break;
65
66 case 'radio':
67 //event driven calculation of element.
68 cj(this).click( function(){
69 calculateRadioLineItemValue(this);
70 display(calculateTotalFee());
71 });
72 calculateRadioLineItemValue(this);
73 break;
74
75 case 'text':
76
77 //event driven calculation of element.
78 cj(this).bind( 'keyup', function() {
79 calculateText(this);
80 }).bind( 'blur' , function() {
81 calculateText(this);
82 });
83 //default calculation of element.
84 calculateText(this);
85
86 break;
87
88 case 'select-one':
89 calculateSelectLineItemValue(this);
90
91 //event driven calculation of element.
92 cj(this).change( function() {
93 calculateSelectLineItemValue(this);
94 display(calculateTotalFee());
95 });
96
97
98 break;
99
100 }
101 display(calculateTotalFee());
102 });
103
104 /**
105 * Calculate the value of the line item for a radio value.
106 */
107 function calculateCheckboxLineItemValue(priceElement) {
108 eval( 'var option = ' + cj(priceElement).attr('price') ) ;
109 optionPart = option[1].split(optionSep);
110 price = parseFloat(0);
111 if (cj(priceElement).prop('checked')) {
112 price = parseFloat(optionPart[0]);
113 }
114 cj(priceElement).data('line_raw_total', price);
115 }
116
117 /**
118 * Calculate the value of the line item for a radio value.
119 */
120 function calculateRadioLineItemValue(priceElement) {
121 eval( 'var option = ' + cj(priceElement).attr('price') );
122 optionPart = option[1].split(optionSep);
123 var lineTotal = parseFloat(optionPart[0]);
124 cj(priceElement).data('line_raw_total', lineTotal);
125 var radionGroupName = cj(priceElement).attr("name");
126 // Reset all unchecked options to having a data value of 0.
127 cj('input[name=' + radionGroupName + ']:radio:unchecked').each(
128 function () {
129 cj(this).data('line_raw_total', 0);
130 }
131 );
132 }
133
134 /**
135 * Calculate the value of the line item for a select value.
136 */
137 function calculateSelectLineItemValue(priceElement) {
138 eval( 'var selectedText = ' + cj(priceElement).attr('price') );
139 var price = parseFloat('0');
140 var option = cj(priceElement).val();
141 if (option) {
142 optionPart = selectedText[option].split(optionSep);
143 price = parseFloat(optionPart[0]);
144 }
145 cj(priceElement).data('line_raw_total', price);
146 }
147
148 /**
149 * Calculate the value of the line item for a text box.
150 */
151 function calculateText(priceElement) {
152 //CRM-16034 - comma acts as decimal in price set text pricing
153 var textval = parseFloat(cj(priceElement).val().replace(thousandMarker, ''));
154
155 if (isNaN(textval)) {
156 textval = parseFloat(0);
157 }
158 eval('var option = '+ cj(priceElement).attr('price'));
159 optionPart = option[1].split(optionSep);
160 addprice = parseFloat(optionPart[0]);
161 var curval = textval * addprice;
162 cj(priceElement).data('line_raw_total', curval);
163 display(calculateTotalFee());
164 }
165
166 /**
167 * Calculate the total fee for the visible priceset.
168 */
169 function calculateTotalFee() {
170 var totalFee = 0;
171 cj("#priceset [price]").each(function () {
172 totalFee = totalFee + cj(this).data('line_raw_total');
173 });
174 return totalFee;
175 }
176
177 /**
178 * Display calculated amount.
179 */
180 function display(totalfee) {
181
182 // totalfee is monetary, round it to 2 decimal points so it can
183 // go as a float - CRM-13491
184 totalfee = Math.round(totalfee*100)/100;
185 var totalEventFee = formatMoney( totalfee, 2, separator, thousandMarker);
186 document.getElementById('pricevalue').innerHTML = "<b>"+symbol+"</b> "+totalEventFee;
187
188 cj('#total_amount').val( totalfee );
189 cj('#pricevalue').data('raw-total', totalfee).trigger('change');
190
191 ( totalfee < 0 ) ? cj('table#pricelabel').addClass('disabled') : cj('table#pricelabel').removeClass('disabled');
192 if (typeof skipPaymentMethod == 'function') {
193 // Advice to anyone who, like me, feels hatred towards this if construct ... if you remove the if you
194 // get an error on participant 2 of a event that requires approval & permits multiple registrants.
195 skipPaymentMethod();
196 }
197 }
198
199 //money formatting/localization
200 function formatMoney (amount, c, d, t) {
201 var n = amount,
202 c = isNaN(c = Math.abs(c)) ? 2 : c,
203 d = d == undefined ? "," : d,
204 t = t == undefined ? "." : t, s = n < 0 ? "-" : "",
205 i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
206 j = (j = i.length) > 3 ? j % 3 : 0;
207 return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
208 }
209
210 {/literal}
211 </script>