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