Ian province abbreviation patch - issue 724
[civicrm-core.git] / templates / CRM / Price / Form / Calculate.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.7 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2015 |
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 {if $noCalcValueDisplay neq 'false'}
27 <div id="pricesetTotal" class="crm-section section-pricesetTotal">
28 <div class="label" id="pricelabel"><label>
29 {if ( $extends eq 'Contribution' ) || ( $extends eq 'Membership' )}
30 {ts}Total Amount{/ts}{else}{ts}Total Fee(s){/ts}
31 {if $isAdditionalParticipants} {ts}for this participant{/ts}{/if}
32 {/if}</label></div>
33 <div class="content calc-value" id="pricevalue" ></div>
34 </div>
35 {/if}
36 <script type="text/javascript">
37 {literal}
38
39 var totalfee = 0;
40 var thousandMarker = '{/literal}{$config->monetaryThousandSeparator}{literal}';
41 var seperator = '{/literal}{$config->monetaryDecimalPoint}{literal}';
42 var symbol = '{/literal}{$currencySymbol}{literal}';
43 var optionSep = '|';
44 var priceSet = price = Array( );
45 cj("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
62 if( cj(this).prop('checked') ) {
63 totalfee += addprice;
64 price[ele] += addprice;
65 }
66
67 //event driven calculation of element.
68 cj(this).click( function(){
69
70 if ( cj(this).prop('checked') ) {
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
93 if( cj(this).prop('checked') ) {
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 calculateText( this );
112
113 //event driven calculation of element.
114 cj(this).bind( 'keyup', function() { calculateText( this );
115 }).bind( 'blur' , function() { calculateText( this );
116 });
117
118 break;
119
120 case 'select-one':
121
122 //default calcution of element.
123 var ele = cj(this).attr('id');
124 if ( ! price[ele] ) {
125 price[ele] = 0;
126 }
127 eval( 'var selectedText = ' + cj(this).attr('price') );
128 var addprice = 0;
129 if ( cj(this).val( ) ) {
130 optionPart = selectedText[cj(this).val( )].split(optionSep);
131 addprice = parseFloat( optionPart[0] );
132 }
133
134 if ( addprice ) {
135 totalfee = parseFloat(totalfee) + addprice - parseFloat(price[ele]);
136 price[ele] = addprice;
137 }
138
139 //event driven calculation of element.
140 cj(this).change( function() {
141 var ele = cj(this).attr('id');
142 if ( ! price[ele] ) {
143 price[ele] = 0;
144 }
145 eval( 'var selectedText = ' + cj(this).attr('price') );
146
147 var addprice = 0;
148 if ( cj(this).val( ) ) {
149 optionPart = selectedText[cj(this).val( )].split(optionSep);
150 addprice = parseFloat( optionPart[0] );
151 }
152
153 if ( addprice ) {
154 totalfee = parseFloat(totalfee) + addprice - parseFloat(price[ele]);
155 price[ele] = addprice;
156 } else {
157 totalfee = parseFloat(totalfee) - parseFloat(price[ele]);
158 price[ele] = parseFloat('0');
159 }
160 display( totalfee );
161 });
162 display( totalfee );
163 break;
164 }
165 }
166 });
167
168 //calculation for text box.
169 function calculateText( object ) {
170 //CRM-16034 - comma acts as decimal in price set text pricing
171 var textval = parseFloat( cj(object).val().replace(thousandMarker, '') );
172
173 eval( 'var option = '+ cj(object).attr('price') );
174 ele = option[0];
175 if ( ! price[ele] ) {
176 price[ele] = 0;
177 }
178 optionPart = option[1].split(optionSep);
179 addprice = parseFloat( optionPart[0] );
180 var curval = textval * addprice;
181 if ( textval >= 0 ) {
182 totalfee = parseFloat(totalfee) + curval - parseFloat(price[ele]);
183 price[ele] = curval;
184 }
185 else {
186 totalfee = parseFloat(totalfee) - parseFloat(price[ele]);
187 price[ele] = parseFloat('0');
188 }
189 display( totalfee );
190 }
191 {/literal}
192 {if $displayOveride neq 'true'}
193 {literal}
194 //display calculated amount
195 function display( totalfee ) {
196 // totalfee is monetary, round it to 2 decimal points so it can
197 // go as a float - CRM-13491
198 totalfee = Math.round(totalfee*100)/100;
199 var totalEventFee = formatMoney( totalfee, 2, seperator, thousandMarker);
200 document.getElementById('pricevalue').innerHTML = "<b>"+symbol+"</b> "+totalEventFee;
201 scriptfee = totalfee;
202 scriptarray = price;
203 cj('#total_amount').val( totalfee );
204 cj('#pricevalue').data('raw-total', totalfee).trigger('change');
205
206 ( totalfee < 0 ) ? cj('table#pricelabel').addClass('disabled') : cj('table#pricelabel').removeClass('disabled');
207 if (typeof skipPaymentMethod == 'function') {
208 skipPaymentMethod();
209 }
210 }
211 {/literal}
212 {/if}
213 {literal}
214 //money formatting/localization
215 function formatMoney (amount, c, d, t) {
216 var n = amount,
217 c = isNaN(c = Math.abs(c)) ? 2 : c,
218 d = d == undefined ? "," : d,
219 t = t == undefined ? "." : t, s = n < 0 ? "-" : "",
220 i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
221 j = (j = i.length) > 3 ? j % 3 : 0;
222 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) : "");
223 }
224
225 {/literal}
226 </script>