Merge pull request #3269 from monishdeb/CRM-13981
[civicrm-core.git] / templates / CRM / Price / Form / Calculate.tpl
CommitLineData
6a488035
TO
1{*
2 +--------------------------------------------------------------------+
819d0d41 3 | CiviCRM version 4.5 |
6a488035 4 +--------------------------------------------------------------------+
819d0d41 5 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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*}
bc2eeabb 26{if $noCalcValueDisplay neq 'false'}
6a488035
TO
27<div id="pricesetTotal" class="crm-section section-pricesetTotal">
28 <div class="label" id="pricelabel"><label>
ff2c2c38 29 {if ( $extends eq 'Contribution' ) || ( $extends eq 'Membership' )}
391ab162
DG
30 {ts}Total Amount{/ts}{else}{ts}Total Fee(s){/ts}
31 {if $isAdditionalParticipants} {ts}for this participant{/ts}{/if}
ff2c2c38 32 {/if}</label></div>
71447ba5 33 <div class="content calc-value" id="pricevalue" ></div>
6a488035 34</div>
bc2eeabb 35{/if}
6a488035
TO
36<script type="text/javascript">
37{literal}
38
39var totalfee = 0;
40var thousandMarker = '{/literal}{$config->monetaryThousandSeparator}{literal}';
41var seperator = '{/literal}{$config->monetaryDecimalPoint}{literal}';
42var symbol = '{/literal}{$currencySymbol}{literal}';
43var optionSep = '|';
44var priceSet = price = Array( );
45cj("input,#priceset select,#priceset").each(function () {
46
47 if ( cj(this).attr('price') ) {
48 var eleType = cj(this).attr('type');
49 if ( this.tagName == 'SELECT' ) {
50 eleType = 'select-one';
51 }
52 switch( eleType ) {
53
54 case 'checkbox':
55
56 //default calcution of element.
57 eval( 'var option = ' + cj(this).attr('price') ) ;
58 ele = option[0];
59 optionPart = option[1].split(optionSep);
60 addprice = parseFloat( optionPart[0] );
61
8539f25d 62 if( cj(this).prop('checked') ) {
6a488035
TO
63 totalfee += addprice;
64 price[ele] += addprice;
65 }
66
67 //event driven calculation of element.
68 cj(this).click( function(){
69
8539f25d 70 if ( cj(this).prop('checked') ) {
6a488035
TO
71 totalfee += addprice;
72 price[ele] += addprice;
73 } else {
74 totalfee -= addprice;
75 price[ele] -= addprice;
76 }
77 display( totalfee );
78 });
79 display( totalfee );
80 break;
81
82 case 'radio':
83
84 //default calcution of element.
85 eval( 'var option = ' + cj(this).attr('price') );
86 ele = option[0];
87 optionPart = option[1].split(optionSep);
88 addprice = parseFloat( optionPart[0] );
89 if ( ! price[ele] ) {
90 price[ele] = 0;
91 }
92
8539f25d 93 if( cj(this).prop('checked') ) {
6a488035
TO
94 totalfee = parseFloat(totalfee) + addprice - parseFloat(price[ele]);
95 price[ele] = addprice;
96 }
97
98 //event driven calculation of element.
99 cj(this).click( function(){
100 totalfee = parseFloat(totalfee) + addprice - parseFloat(price[ele]);
101 price[ele] = addprice;
102
103 display( totalfee );
104 });
105 display( totalfee );
106 break;
107
108 case 'text':
109
110 //default calcution of element.
111 var textval = parseFloat( cj(this).val() );
112 if ( textval ) {
113 eval( 'var option = '+ cj(this).attr('price') );
114 ele = option[0];
115 if ( ! price[ele] ) {
116 price[ele] = 0;
117 }
118 optionPart = option[1].split(optionSep);
119 addprice = parseFloat( optionPart[0] );
120 var curval = textval * addprice;
121 if ( textval >= 0 ) {
122 totalfee = parseFloat(totalfee) + curval - parseFloat(price[ele]);
123 price[ele] = curval;
124 }
125 }
126
127 //event driven calculation of element.
128 cj(this).bind( 'keyup', function() { calculateText( this );
129 }).bind( 'blur' , function() { calculateText( this );
130 });
131 display( totalfee );
132 break;
133
134 case 'select-one':
135
136 //default calcution of element.
137 var ele = cj(this).attr('id');
138 if ( ! price[ele] ) {
139 price[ele] = 0;
140 }
141 eval( 'var selectedText = ' + cj(this).attr('price') );
142 var addprice = 0;
143 if ( cj(this).val( ) ) {
144 optionPart = selectedText[cj(this).val( )].split(optionSep);
145 addprice = parseFloat( optionPart[0] );
146 }
147
148 if ( addprice ) {
149 totalfee = parseFloat(totalfee) + addprice - parseFloat(price[ele]);
150 price[ele] = addprice;
151 }
152
153 //event driven calculation of element.
154 cj(this).change( function() {
155 var ele = cj(this).attr('id');
156 if ( ! price[ele] ) {
157 price[ele] = 0;
158 }
159 eval( 'var selectedText = ' + cj(this).attr('price') );
160
161 var addprice = 0;
162 if ( cj(this).val( ) ) {
163 optionPart = selectedText[cj(this).val( )].split(optionSep);
164 addprice = parseFloat( optionPart[0] );
165 }
166
167 if ( addprice ) {
168 totalfee = parseFloat(totalfee) + addprice - parseFloat(price[ele]);
169 price[ele] = addprice;
170 } else {
171 totalfee = parseFloat(totalfee) - parseFloat(price[ele]);
172 price[ele] = parseFloat('0');
173 }
174 display( totalfee );
175 });
176 display( totalfee );
177 break;
178 }
179 }
180});
181
182//calculation for text box.
183function calculateText( object ) {
184 eval( 'var option = ' + cj(object).attr('price') );
185 ele = option[0];
186 if ( ! price[ele] ) {
187 price[ele] = 0;
188 }
189 var optionPart = option[1].split(optionSep);
190 addprice = parseFloat( optionPart[0] );
191 var textval = parseFloat( cj(object).attr('value') );
192 var curval = textval * addprice;
193 if ( textval >= 0 ) {
194 totalfee = parseFloat(totalfee) + curval - parseFloat(price[ele]);
195 price[ele] = curval;
196 } else {
197 totalfee = parseFloat(totalfee) - parseFloat(price[ele]);
198 price[ele] = parseFloat('0');
199 }
200 display( totalfee );
201}
1a0d17b9
PJ
202{/literal}
203{if $displayOveride neq 'true'}
204{literal}
6a488035
TO
205//display calculated amount
206function display( totalfee ) {
b1525e97
DL
207 // totalfee is monetary, round it to 2 decimal points so it can
208 // go as a float - CRM-13491
209 totalfee = Math.round(totalfee*100)/100;
6a488035
TO
210 var totalEventFee = formatMoney( totalfee, 2, seperator, thousandMarker);
211 document.getElementById('pricevalue').innerHTML = "<b>"+symbol+"</b> "+totalEventFee;
212 scriptfee = totalfee;
213 scriptarray = price;
214 cj('#total_amount').val( totalfee );
215
216 ( totalfee < 0 ) ? cj('table#pricelabel').addClass('disabled') : cj('table#pricelabel').removeClass('disabled');
217
218}
1a0d17b9
PJ
219{/literal}
220{/if}
221{literal}
6a488035
TO
222//money formatting/localization
223function formatMoney (amount, c, d, t) {
224var n = amount,
225 c = isNaN(c = Math.abs(c)) ? 2 : c,
226 d = d == undefined ? "," : d,
227 t = t == undefined ? "." : t, s = n < 0 ? "-" : "",
228 i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
229 j = (j = i.length) > 3 ? j % 3 : 0;
230 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) : "");
231}
232
233{/literal}
234</script>