1 //@todo functions partially moved from tpl but still need an enclosure / cleanup
4 $('.selector-rows').change(function () {
6 'url': CRM
.url('civicrm/ajax/batch')
9 $("#Entry").ajaxSubmit(options
);
11 //avoid multiple click on submit button
12 $("#_qf_Entry_upload").on('click', function() {
17 checkColumns($(this));
19 cj('.pledge-adjust-option').click(function(){
20 var blockNo
= cj(this).attr('id');
21 cj('select[id="option_type_' + blockNo
+ '"]').show();
22 cj('select[id="option_type_' + blockNo
+ '"]').removeAttr('disabled');
23 cj('#field_' + blockNo
+ '_total_amount').removeAttr('readonly');
25 $('input[name^="soft_credit_contact_"]').on('change', function(){
26 var rowNum
= $(this).attr('id').replace('soft_credit_contact_id_','');
27 var totalAmount
= $('#field_'+rowNum
+'_total_amount').val();
28 //assign total amount as default soft credit amount
29 $('#soft_credit_amount_'+ rowNum
).val(totalAmount
);
30 //assign soft credit type default value if any
31 $('#soft_credit_type_'+ rowNum
).val($('#sct_default_id').val());
37 //calculate the actual total for the batch
38 calculateActualTotal();
40 $('input[id*="_total_amount"]').bind('keyup change', function () {
41 calculateActualTotal();
44 if (CRM
.batch
.type_id
== 1) {
45 // hide all dates if send receipt is checked
48 // hide the receipt date if send receipt is checked
49 $('input[id*="][send_receipt]"]').change(function () {
50 showHideReceipt($(this));
54 else if (CRM
.batch
.type_id
== 2){
55 cj('select[id^="member_option_"]').each(function () {
56 if (cj(this).val() == 1) {
57 cj(this).attr('disabled', true);
61 // set payment info accord to membership type
62 $('select[id*="_membership_type_0"]').change(function () {
63 setPaymentBlock($(this), null);
65 $('select[id*="_membership_type_1"]').change(function () {
66 setPaymentBlock($(this), $(this).val());
71 // line breaks between radio buttons and checkboxes
72 $('input.form-radio').next().after('<br />');
73 $('input.form-checkbox').next().after('<br />');
75 //set the focus on first element
76 $('#primary_contact_1').focus();
82 function setPaymentBlock(form
, memType
) {
83 var rowID
= form
.closest('div.crm-grid-row').attr('entity_id');
84 var dataUrl
= CRM
.url('civicrm/ajax/memType');
87 memType
= cj('select[id="field_' + rowID
+ '_membership_type_1"]').val();
90 cj
.post(dataUrl
, {mtype
: memType
}, function (data
) {
91 cj('#field_' + rowID
+ '_financial_type').val(data
.financial_type_id
);
92 cj('#field_' + rowID
+ '_total_amount').val(data
.total_amount
).change();
96 function hideSendReceipt() {
97 cj('input[id*="][send_receipt]"]').each(function () {
98 showHideReceipt(cj(this));
102 function showHideReceipt(elem
) {
103 var rowID
= elem
.closest('div.crm-grid-row').attr('entity_id');
104 if (elem
.prop('checked')) {
105 cj('.crm-batch-receipt_date-' + rowID
).hide();
108 cj('.crm-batch-receipt_date-' + rowID
).show();
112 function validateRow() {
113 cj('.selector-rows').each(function () {
114 checkColumns(cj(this));
118 function checkColumns(parentRow
) {
119 // show valid row icon if all required data is field
122 var errorExists
= false;
123 var rowID
= parentRow
.closest('div.crm-grid-row').attr('entity_id');
125 parentRow
.find('div .required').each(function () {
126 //special case to handle contact autocomplete select
127 var fieldId
= cj(this).attr('id');
128 if (fieldId
.substring(0, 16) == 'primary_contact_') {
129 // if display value is set then make sure we also check if contact id is set
130 if (!cj(this).val()) {
134 if (cj(this).val() && !cj('input[name="primary_contact_select_id[' + rowID
+ ']"]').val()) {
141 if (!cj(this).val()) {
145 if (cj(this).hasClass('error') && !cj(this).hasClass('valid')) {
155 // this means user has entered some data
157 parentRow
.find("div:first span").prop('class', 'batch-invalid');
160 if (inValidRow
=== 0 && validRow
> 0) {
161 parentRow
.find("div:first span").prop('class', 'batch-valid');
164 parentRow
.find("div:first span").prop('class', 'batch-edit');
169 function calculateActualTotal() {
171 cj('input[id*="_total_amount"]').each(function () {
172 if (cj(this).val()) {
173 total
+= parseFloat(cj(this).val());
177 cj('.batch-actual-total').html(formatMoney(total
));
180 //money formatting/localization
181 function formatMoney(amount
) {
183 var t
= CRM
.setting
.monetaryThousandSeparator
;
184 var d
= CRM
.setting
.monetaryDecimalPoint
;
186 c
= isNaN(c
= Math
.abs(c
)) ? 2 : c
;
187 t
= t
=== undefined ? "." : t
;
188 d
= d
=== undefined ? "," : d
;
191 i
= parseInt(n
= Math
.abs(+n
|| 0).toFixed(c
)) + "",
192 s
= n
< 0 ? "-" : "",
193 j
= (j
= i
.length
) > 3 ? j
% 3 : 0;
195 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) : "");
199 * This function is use to setdefault elements via ajax
201 * @param fname string field name
204 function setFieldValue(fname
, fieldValue
, blockNo
) {
205 var elementId
= cj('[name="field[' + blockNo
+ '][' + fname
+ ']"]');
207 if (elementId
.length
=== 0) {
208 elementId
= cj('input[type=checkbox][name^="field[' + blockNo
+ '][' + fname
+ ']"][type!=hidden]');
211 // if element not found than return
212 if (elementId
.length
=== 0) {
216 //check if it is date element
217 var isDateElement
= elementId
.attr('format');
219 // check if it is wysiwyg element
220 var editor
= elementId
.attr('editor');
222 //get the element type
223 var elementType
= elementId
.attr('type');
225 // set the value for all the elements, elements needs to be handled are
226 // select, checkbox, radio, date fields, text, textarea, multi-select
227 // wysiwyg editor, advanced multi-select ( to do )
228 if (elementType
== 'radio') {
230 elementId
.filter("[value=" + fieldValue
+ "]").prop("checked", true);
233 elementId
.removeProp('checked');
237 if (elementType
== 'checkbox') {
239 elementId
.removeProp('checked');
241 cj
.each(fieldValue
, function (key
, value
) {
242 cj('input[name="field[' + blockNo
+ '][' + fname
+ '][' + value
+ ']"]').prop('checked', true);
250 elemtId
= elementId
.attr('id');
251 oEditor
= CKEDITOR
.instances
[elemtId
];
252 oEditor
.setData(htmlContent
);
255 var elemtId
= element
.attr('id');
256 tinyMCE
.get(elemtId
).setContent(htmlContent
);
260 case 'drupalwysiwyg':
264 elementId
.val(fieldValue
);
268 elementId
.val(fieldValue
);
273 // since we use different display field for date we also need to set it.
274 // also check for date time field and set the value correctly
275 if (isDateElement
&& fieldValue
) {
276 setDateFieldValue(fname
, fieldValue
, blockNo
);
280 function setDateFieldValue(fname
, fieldValue
, blockNo
) {
281 var dateValues
= fieldValue
.split(' ');
283 var actualDateElement
= cj('#field_' + blockNo
+ '_' + fname
);
284 var date_format
= actualDateElement
.attr('format');
285 var altDateFormat
= 'yy-mm-dd';
287 var actualDateValue
= cj
.datepicker
.parseDate(altDateFormat
, dateValues
[0]);
289 // format date according to display field
290 var hiddenDateValue
= cj
.datepicker
.formatDate('mm/dd/yy', actualDateValue
);
292 actualDateElement
.val(hiddenDateValue
);
294 var displayDateValue
= actualDateElement
.val();
295 if (date_format
!= 'mm/dd/yy') {
296 displayDateValue
= cj
.datepicker
.formatDate(date_format
, actualDateValue
);
299 cj('#field_' + blockNo
+ '_' + fname
+ '_display').val(displayDateValue
);
301 // need to fix time formatting
303 cj('#field_' + blockNo
+ '_' + fname
+ '_time').val(dateValues
[1].substr(0, 5));