Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-10-02-11-18-44
[civicrm-core.git] / templates / CRM / Batch / Form / Entry.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.4 |
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 class="batch-entry form-item">
27 <div id="help">
28 {ts}Click Validate & Process below when you've entered all items for the batch. You can also Save & Continue Later at any time. Go to Administer > Customize Data and Screens > Profiles > Reserved Profiles > to add, remove or change the order of columns.{/ts}
29 </div>
30 {if $batchAmountMismatch}
31 <div class="status message status-warning">
32 <div
33 class="icon alert-icon"></div> {ts}Total for amounts entered below does not match the expected batch total.{/ts}
34 </div>
35 <div class="crm-button crm-button_qf_Entry_upload_force-save">
36 {$form._qf_Entry_upload_force.html}
37 </div>
38 <div class="clear"></div>
39 {/if}
40 <table class="form-layout-compressed batch-totals">
41 <tr>
42 <td class="label">{ts}Total amount expected{/ts}</td>
43 <td class="right"><span class="batch-expected-total">{$batchTotal|crmMoney}</span></td>
44 </tr>
45 <tr>
46 <td class="label">{ts}Total amount entered{/ts}</td>
47 <td class="right">{$config->defaultCurrencySymbol} <span class="batch-actual-total"></span></td>
48 </tr>
49 </table>
50
51 <div class="crm-copy-fields crm-grid-table" id="crm-batch-entry-table">
52 <div class="crm-grid-header">
53 <div class="crm-grid-cell">&nbsp;</div>
54 <div class="crm-grid-cell">{ts}Contact{/ts}</div>
55 {if $batchType eq 2 }
56 <div class="crm-grid-cell">&nbsp;</div>
57 {/if}
58 {foreach from=$fields item=field key=fieldName}
59 <div class="crm-grid-cell">
60 <img src="{$config->resourceBase}i/copy.png"
61 alt="{ts 1=$field.title}Click to copy %1 from row one to all rows.{/ts}"
62 fname="{$field.name}" class="action-icon"
63 title="{ts}Click here to copy the value in row one to ALL rows.{/ts}"/>{$field.title}
64 </div>
65 {/foreach}
66 </div>
67
68 {section name='i' start=1 loop=$rowCount}
69 {assign var='rowNumber' value=$smarty.section.i.index}
70 <div class="{cycle values="odd-row,even-row"} selector-rows crm-grid-row" entity_id="{$rowNumber}">
71 <div class="compressed crm-grid-cell"><span class="batch-edit"></span></div>
72 {* contact select/create option*}
73 <div class="compressed crm-grid-cell">
74 {include file="CRM/Contact/Form/NewContact.tpl" blockNo = $rowNumber noLabel=true prefix="primary_" newContactCallback="updateContactInfo($rowNumber, 'primary_')"}
75 </div>
76
77 {if $batchType eq 2 }
78 {$form.member_option.$rowNumber.html}
79 {/if}
80
81 {foreach from=$fields item=field key=fieldName}
82 {assign var=n value=$field.name}
83 {if ( $fields.$n.data_type eq 'Date') or ( in_array( $n, array( 'thankyou_date', 'cancel_date', 'receipt_date', 'receive_date', 'join_date', 'membership_start_date', 'membership_end_date' ) ) ) }
84 <div class="compressed crm-grid-cell">
85 <span class="crm-batch-{$n}-{$rowNumber}">
86 {include file="CRM/common/jcalendar.tpl" elementName=$n elementIndex=$rowNumber batchUpdate=1}
87 </span>
88 </div>
89 {elseif $n eq 'soft_credit'}
90 <div class="compressed crm-grid-cell">
91 {include file="CRM/Contact/Form/NewContact.tpl" blockNo = $rowNumber noLabel=true prefix="soft_credit_"}
92 </div>
93 {elseif in_array( $fields.$n.html_type, array('Radio', 'CheckBox'))}
94 <div class="compressed crm-grid-cell">&nbsp;{$form.field.$rowNumber.$n.html}</div>
95 {else}
96 <div class="compressed crm-grid-cell">{$form.field.$rowNumber.$n.html}</div>
97 {/if}
98 {/foreach}
99 </div>
100 {/section}
101 </div>
102 <div class="crm-submit-buttons">{if $fields}{$form._qf_Batch_refresh.html}{/if} &nbsp; {$form.buttons.html}</div>
103 </div>
104 {literal}
105 <script type="text/javascript">
106 cj(function () {
107 cj('.selector-rows').change(function () {
108 var options = {
109 'url': {/literal}"{crmURL p='civicrm/ajax/batch' h=0}"{literal}
110 };
111
112 cj("#Entry").ajaxSubmit(options);
113 });
114
115 cj('#crm-container').on('keyup change', '*.selector-rows', function () {
116 // validate rows
117 checkColumns(cj(this));
118 });
119
120 // validate rows
121 validateRow();
122
123 //calculate the actual total for the batch
124 calculateActualTotal();
125
126 cj('input[id*="_total_amount"]').bind('keyup change', function () {
127 calculateActualTotal();
128 });
129
130 {/literal}{if $batchType eq 1 }{literal}
131 // hide all dates if send receipt is checked
132 hideSendReceipt();
133
134 // hide the receipt date if send receipt is checked
135 cj('input[id*="][send_receipt]"]').change(function () {
136 showHideReceipt(cj(this));
137 });
138
139 {/literal}{else}{literal}
140 cj('select[id^="member_option_"]').each(function () {
141 if (cj(this).val() == 1) {
142 cj(this).attr('disabled', true);
143 }
144 });
145
146 // set payment info accord to membership type
147 cj('select[id*="_membership_type_0"]').change(function () {
148 setPaymentBlock(cj(this), null);
149 });
150
151 cj('select[id*="_membership_type_1"]').change(function () {
152 setPaymentBlock(cj(this), cj(this).val());
153 });
154
155 {/literal}{/if}{literal}
156
157 // line breaks between radio buttons and checkboxes
158 cj('input.form-radio').next().after('<br />');
159 cj('input.form-checkbox').next().after('<br />');
160
161 //set the focus on first element
162 cj('#primary_contact_1').focus();
163
164 });
165
166 function setPaymentBlock(form, memType) {
167 var rowID = form.closest('div.crm-grid-row').attr('entity_id');
168 var dataUrl = {/literal}"{crmURL p='civicrm/ajax/memType' h=0}"{literal};
169
170 if (!memType) {
171 memType = cj('select[id="field_' + rowID + '_membership_type_1"]').val();
172 }
173
174 cj.post(dataUrl, {mtype: memType}, function (data) {
175 cj('#field_' + rowID + '_financial_type').val(data.financial_type_id);
176 cj('#field_' + rowID + '_total_amount').val(data.total_amount).change();
177 }, 'json');
178 }
179
180 function hideSendReceipt() {
181 cj('input[id*="][send_receipt]"]').each(function () {
182 showHideReceipt(cj(this));
183 });
184 }
185
186 function showHideReceipt(elem) {
187 var rowID = elem.closest('div.crm-grid-row').attr('entity_id');
188 if (elem.prop('checked')) {
189 cj('.crm-batch-receipt_date-' + rowID).hide();
190 }
191 else {
192 cj('.crm-batch-receipt_date-' + rowID).show();
193 }
194 }
195
196 function validateRow() {
197 cj('.selector-rows').each(function () {
198 checkColumns(cj(this));
199 });
200 }
201
202 function checkColumns(parentRow) {
203 // show valid row icon if all required data is field
204 var validRow = 0;
205 var inValidRow = 0;
206 var errorExists = false;
207 var rowID = parentRow.closest('div.crm-grid-row').attr('entity_id');
208
209 parentRow.find('div .required').each(function () {
210 //special case to handle contact autocomplete select
211 var fieldId = cj(this).attr('id');
212 if (fieldId.substring(0, 16) == 'primary_contact_') {
213 // if display value is set then make sure we also check if contact id is set
214 if (!cj(this).val()) {
215 inValidRow++;
216 }
217 else {
218 var contactIdElement = cj('input[name="primary_contact_select_id[' + rowID + ']"]');
219 if (cj(this).val() && !contactIdElement.val()) {
220 inValidRow++;
221 errorExists = true;
222 }
223 else if (cj(this).val() && contactIdElement.val()) {
224 // this is hack to remove error span because we are skipping this for autocomplete fields
225 cj(this).next('span.crm-error').remove();
226 }
227 }
228 }
229 else {
230 if (!cj(this).val()) {
231 inValidRow++;
232 }
233 else {
234 if (cj(this).hasClass('error') && (cj(this).hasClass('valid') || cj(this).hasClass('required'))) {
235 errorExists = true;
236 }
237 else {
238 validRow++;
239 }
240 }
241 }
242 });
243
244 // this means user has entered some data
245 if (errorExists) {
246 parentRow.find("div:first span").prop('class', 'batch-invalid');
247 }
248 else {
249 if (inValidRow == 0 && validRow > 0) {
250 parentRow.find("div:first span").prop('class', 'batch-valid');
251 }
252 else {
253 parentRow.find("div:first span").prop('class', 'batch-edit');
254 }
255 }
256 }
257
258 function calculateActualTotal() {
259 var total = 0;
260 cj('input[id*="_total_amount"]').each(function () {
261 if (cj(this).val()) {
262 total += parseFloat(cj(this).val());
263 }
264 });
265
266 cj('.batch-actual-total').html(formatMoney(total));
267 }
268
269 //money formatting/localization
270 function formatMoney(amount) {
271 var c = 2;
272 var t = '{/literal}{$config->monetaryThousandSeparator}{literal}';
273 var d = '{/literal}{$config->monetaryDecimalPoint}{literal}';
274
275 var n = amount,
276 c = isNaN(c = Math.abs(c)) ? 2 : c,
277 d = d == undefined ? "," : d,
278 t = t == undefined ? "." : t, s = n < 0 ? "-" : "",
279 i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
280 j = (j = i.length) > 3 ? j % 3 : 0;
281
282 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) : "");
283 }
284
285 function updateContactInfo(blockNo, prefix) {
286 var contactHiddenElement = 'input[name="' + prefix + 'contact_select_id[' + blockNo + ']"]';
287 var contactId = cj(contactHiddenElement).val();
288
289 var returnProperties = '';
290 var profileFields = new Array();
291 {/literal}
292 {if $contactFields}
293 {foreach from=$contactFields item=val key=fldName}
294 var fldName = "{$fldName}";
295 {literal}
296 if (returnProperties) {
297 returnProperties = returnProperties + ',';
298 }
299 var fld = fldName.split('-');
300 returnProperties = returnProperties + fld[0];
301 profileFields[fld[0]] = fldName;
302 {/literal}
303 {/foreach}
304 {/if}
305 {literal}
306
307 CRM.api('Contact', 'get', {
308 'sequential': '1',
309 'contact_id': contactId,
310 'return': returnProperties },
311 { success: function (data) {
312 cj.each(data.values[0], function (key, value) {
313 // set the values
314 var actualFldName = profileFields[key];
315 if (key == 'country' || key == 'state_province') {
316 idFldName = key + '_id';
317 value = data.values[0][idFldName];
318 }
319 setFieldValue(actualFldName, value, blockNo)
320 });
321
322 // for membership batch entry based on contact we need to enable / disable
323 // add membership select
324 {/literal}{if $batchType eq 2}{literal}
325 CRM.api('Membership', 'get', {
326 'sequential': '1',
327 'contact_id': contactId,
328 },
329 { success: function (data) {
330 if (data.count > 0) {
331 //get the information on membership type
332 var membershipTypeId = data.values[0].membership_type_id;
333 var membershipJoinDate = data.values[0].join_date;
334 CRM.api('MembershipType', 'get', {
335 'sequential': '1',
336 'id': membershipTypeId
337 },
338 { success: function (data) {
339 var memTypeContactId = data.values[0].member_of_contact_id;
340 cj('select[id="member_option_' + blockNo + '"]').removeAttr('disabled').val(2);
341 cj('select[id="field_' + blockNo + '_membership_type_0"]').val(memTypeContactId).change();
342 cj('select[id="field_' + blockNo + '_membership_type_1"]').val(membershipTypeId).change();
343 setDateFieldValue('join_date', membershipJoinDate, blockNo)
344 }
345 });
346 }
347 }
348 });
349 {/literal}{/if}{literal}
350 }
351 });
352 }
353
354 /**
355 * This function is use to setdefault elements via ajax
356 *
357 * @param fname string field name
358 * @return void
359 */
360 function setFieldValue(fname, fieldValue, blockNo) {
361 var elementId = cj('[name="field[' + blockNo + '][' + fname + ']"]');
362
363 if (elementId.length == 0) {
364 elementId = cj('input[type=checkbox][name^="field[' + blockNo + '][' + fname + ']"][type!=hidden]');
365 }
366
367 // if element not found than return
368 if (elementId.length == 0) {
369 return;
370 }
371
372 //check if it is date element
373 var isDateElement = elementId.attr('format');
374
375 // check if it is wysiwyg element
376 var editor = elementId.attr('editor');
377
378 //get the element type
379 var elementType = elementId.attr('type');
380
381 // set the value for all the elements, elements needs to be handled are
382 // select, checkbox, radio, date fields, text, textarea, multi-select
383 // wysiwyg editor, advanced multi-select ( to do )
384 if (elementType == 'radio') {
385 if (fieldValue) {
386 elementId.filter("[value=" + fieldValue + "]").prop("checked", true);
387 }
388 else {
389 elementId.removeProp('checked');
390 }
391 }
392 else {
393 if (elementType == 'checkbox') {
394 // handle checkbox
395 elementId.removeProp('checked');
396 if (fieldValue) {
397 cj.each(fieldValue, function (key, value) {
398 cj('input[name="field[' + blockNo + '][' + fname + '][' + value + ']"]').prop('checked', true);
399 });
400 }
401 }
402 else {
403 if (editor) {
404 switch (editor) {
405 case 'ckeditor':
406 var elemtId = elementId.attr('id');
407 oEditor = CKEDITOR.instances[elemtId];
408 oEditor.setData(htmlContent);
409 break;
410 case 'tinymce':
411 var elemtId = element.attr('id');
412 tinyMCE.get(elemtId).setContent(htmlContent);
413 break;
414 case 'joomlaeditor':
415 // TO DO
416 case 'drupalwysiwyg':
417 // TO DO
418 default:
419 elementId.val(fieldValue);
420 }
421 }
422 else {
423 elementId.val(fieldValue);
424 }
425 }
426 }
427
428 // since we use different display field for date we also need to set it.
429 // also check for date time field and set the value correctly
430 if (isDateElement && fieldValue) {
431 setDateFieldValue(fname, fieldValue, blockNo)
432 }
433 }
434
435 function setDateFieldValue(fname, fieldValue, blockNo) {
436 var dateValues = fieldValue.split(' ');
437
438 var actualDateElement = cj('#field_' + blockNo + '_' + fname);
439 var date_format = actualDateElement.attr('format');
440 var altDateFormat = 'yy-mm-dd';
441
442 var actualDateValue = cj.datepicker.parseDate(altDateFormat, dateValues[0]);
443
444 // format date according to display field
445 var hiddenDateValue = cj.datepicker.formatDate('mm/dd/yy', actualDateValue);
446
447 actualDateElement.val(hiddenDateValue);
448
449 var displayDateValue = actualDateElement.val();
450 if (date_format != 'mm/dd/yy') {
451 displayDateValue = cj.datepicker.formatDate(date_format, actualDateValue);
452 }
453
454 cj('#field_' + blockNo + '_' + fname + '_display').val(displayDateValue);
455
456 // need to fix time formatting
457 if (dateValues[1]) {
458 cj('#field_' + blockNo + '_' + fname + '_time').val(dateValues[1].substr(0, 5));
459 }
460 }
461
462 </script>
463 {/literal}
464
465 {*include batch copy js js file*}
466 {include file="CRM/common/batchCopy.tpl"}