QA fixes
[civicrm-core.git] / templates / CRM / Batch / Form / Entry.js
CommitLineData
5b1666f7 1//@todo functions partially moved from tpl but still need an enclosure / cleanup
2// jslinting etc
3cc60a06 3CRM.$(function($) {
ae8f569f 4 $('.selector-rows').change(function () {
5b1666f7 5 var options = {
6 'url': CRM.url('civicrm/ajax/batch')
7 };
8
ae8f569f 9 $("#Entry").ajaxSubmit(options);
5b1666f7 10
11 // validate rows
ae8f569f 12 checkColumns($(this));
5b1666f7 13 });
04e6444d 14 cj('.pledge-adjust-option').click(function(){
15 var blockNo = cj(this).attr('id');
16 cj('select[id="option_type_' + blockNo + '"]').show();
17 cj('select[id="option_type_' + blockNo + '"]').removeAttr('disabled');
18 cj('#field_' + blockNo + '_total_amount').removeAttr('readonly');
19 });
9e1854a1 20 $('input[name^="soft_credit_contact_"]').on('change', function(){
21 var rowNum = $(this).attr('id').replace('soft_credit_contact_id_','');
ae8f569f 22 var totalAmount = $('#field_'+rowNum+'_total_amount').val();
5b1666f7 23 //assign total amount as default soft credit amount
ae8f569f 24 $('#soft_credit_amount_'+ rowNum).val(totalAmount);
51fa20cb 25 //assign soft credit type default value if any
9e1854a1 26 $('#soft_credit_type_'+ rowNum).val($('#sct_default_id').val());
5b1666f7 27 });
28
29 // validate rows
30 validateRow();
31
32 //calculate the actual total for the batch
33 calculateActualTotal();
34
ae8f569f 35 $('input[id*="_total_amount"]').bind('keyup change', function () {
5b1666f7 36 calculateActualTotal();
37 });
38
39 if (CRM.batch.type_id == 1) {
40 // hide all dates if send receipt is checked
41 hideSendReceipt();
42
43 // hide the receipt date if send receipt is checked
ae8f569f
CW
44 $('input[id*="][send_receipt]"]').change(function () {
45 showHideReceipt($(this));
5b1666f7 46 });
47
48 }
04e6444d 49 else if (CRM.batch.type_id == 2){
50 cj('select[id^="member_option_"]').each(function () {
51 if (cj(this).val() == 1) {
52 cj(this).attr('disabled', true);
53 }
54 });
5b1666f7 55
56 // set payment info accord to membership type
ae8f569f
CW
57 $('select[id*="_membership_type_0"]').change(function () {
58 setPaymentBlock($(this), null);
5b1666f7 59 });
ae8f569f
CW
60 $('select[id*="_membership_type_1"]').change(function () {
61 setPaymentBlock($(this), $(this).val());
5b1666f7 62 });
63
64 }
65
66 // line breaks between radio buttons and checkboxes
ae8f569f
CW
67 $('input.form-radio').next().after('<br />');
68 $('input.form-checkbox').next().after('<br />');
5b1666f7 69
70 //set the focus on first element
ae8f569f 71 $('#primary_contact_1').focus();
5b1666f7 72
73});
74
75
5b1666f7 76
1283a565 77function setPaymentBlock(form, memType) {
78 var rowID = form.closest('div.crm-grid-row').attr('entity_id');
79 var dataUrl = CRM.url('civicrm/ajax/memType');
80
81 if (!memType) {
82 memType = cj('select[id="field_' + rowID + '_membership_type_1"]').val();
83 }
84
85 cj.post(dataUrl, {mtype: memType}, function (data) {
86 cj('#field_' + rowID + '_financial_type').val(data.financial_type_id);
87 cj('#field_' + rowID + '_total_amount').val(data.total_amount).change();
88 }, 'json');
89}
90
91function hideSendReceipt() {
92 cj('input[id*="][send_receipt]"]').each(function () {
93 showHideReceipt(cj(this));
94 });
95}
96
97function showHideReceipt(elem) {
98 var rowID = elem.closest('div.crm-grid-row').attr('entity_id');
99 if (elem.prop('checked')) {
100 cj('.crm-batch-receipt_date-' + rowID).hide();
101 }
102 else {
103 cj('.crm-batch-receipt_date-' + rowID).show();
104 }
105}
106
107function validateRow() {
108 cj('.selector-rows').each(function () {
109 checkColumns(cj(this));
110 });
111}
112
113function checkColumns(parentRow) {
114 // show valid row icon if all required data is field
115 var validRow = 0;
116 var inValidRow = 0;
117 var errorExists = false;
118 var rowID = parentRow.closest('div.crm-grid-row').attr('entity_id');
119
120 parentRow.find('div .required').each(function () {
121 //special case to handle contact autocomplete select
122 var fieldId = cj(this).attr('id');
123 if (fieldId.substring(0, 16) == 'primary_contact_') {
124 // if display value is set then make sure we also check if contact id is set
125 if (!cj(this).val()) {
126 inValidRow++;
127 }
128 else {
129 if (cj(this).val() && !cj('input[name="primary_contact_select_id[' + rowID + ']"]').val()) {
130 inValidRow++;
131 errorExists = true;
132 }
133 }
134 }
135 else {
136 if (!cj(this).val()) {
137 inValidRow++;
138 }
139 else {
140 if (cj(this).hasClass('error') && !cj(this).hasClass('valid')) {
141 errorExists = true;
142 }
143 else {
144 validRow++;
145 }
146 }
147 }
148 });
149
150 // this means user has entered some data
151 if (errorExists) {
152 parentRow.find("div:first span").prop('class', 'batch-invalid');
153 }
154 else {
155 if (inValidRow == 0 && validRow > 0) {
156 parentRow.find("div:first span").prop('class', 'batch-valid');
157 }
158 else {
159 parentRow.find("div:first span").prop('class', 'batch-edit');
160 }
161 }
162}
163
164function calculateActualTotal() {
165 var total = 0;
166 cj('input[id*="_total_amount"]').each(function () {
167 if (cj(this).val()) {
168 total += parseFloat(cj(this).val());
169 }
170 });
171
172 cj('.batch-actual-total').html(formatMoney(total));
173}
174
175//money formatting/localization
176function formatMoney(amount) {
177 var c = 2;
178 var t = CRM.setting.monetaryThousandSeparator;
179 var d = CRM.setting.monetaryDecimalPoint;
180
181 var n = amount,
182 c = isNaN(c = Math.abs(c)) ? 2 : c,
183 d = d == undefined ? "," : d,
184 t = t == undefined ? "." : t, s = n < 0 ? "-" : "",
185 i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
186 j = (j = i.length) > 3 ? j % 3 : 0;
187
188 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) : "");
189}
190
191/**
192 * This function is use to setdefault elements via ajax
193 *
194 * @param fname string field name
195 * @return void
196 */
197function setFieldValue(fname, fieldValue, blockNo) {
198 var elementId = cj('[name="field[' + blockNo + '][' + fname + ']"]');
199
200 if (elementId.length == 0) {
201 elementId = cj('input[type=checkbox][name^="field[' + blockNo + '][' + fname + ']"][type!=hidden]');
202 }
203
204 // if element not found than return
205 if (elementId.length == 0) {
206 return;
207 }
208
209 //check if it is date element
210 var isDateElement = elementId.attr('format');
211
212 // check if it is wysiwyg element
213 var editor = elementId.attr('editor');
214
215 //get the element type
216 var elementType = elementId.attr('type');
217
218 // set the value for all the elements, elements needs to be handled are
219 // select, checkbox, radio, date fields, text, textarea, multi-select
220 // wysiwyg editor, advanced multi-select ( to do )
221 if (elementType == 'radio') {
222 if (fieldValue) {
223 elementId.filter("[value=" + fieldValue + "]").prop("checked", true);
224 }
225 else {
226 elementId.removeProp('checked');
227 }
228 }
229 else {
230 if (elementType == 'checkbox') {
231 // handle checkbox
232 elementId.removeProp('checked');
233 if (fieldValue) {
234 cj.each(fieldValue, function (key, value) {
235 cj('input[name="field[' + blockNo + '][' + fname + '][' + value + ']"]').prop('checked', true);
236 });
237 }
238 }
239 else {
240 if (editor) {
241 switch (editor) {
242 case 'ckeditor':
243 var elemtId = elementId.attr('id');
244 oEditor = CKEDITOR.instances[elemtId];
245 oEditor.setData(htmlContent);
246 break;
247 case 'tinymce':
248 var elemtId = element.attr('id');
249 tinyMCE.get(elemtId).setContent(htmlContent);
250 break;
251 case 'joomlaeditor':
252 // TO DO
253 case 'drupalwysiwyg':
254 // TO DO
255 default:
256 elementId.val(fieldValue);
257 }
258 }
259 else {
260 elementId.val(fieldValue);
261 }
262 }
263 }
264
265 // since we use different display field for date we also need to set it.
266 // also check for date time field and set the value correctly
267 if (isDateElement && fieldValue) {
268 setDateFieldValue(fname, fieldValue, blockNo)
269 }
270}
271
272function setDateFieldValue(fname, fieldValue, blockNo) {
273 var dateValues = fieldValue.split(' ');
274
275 var actualDateElement = cj('#field_' + blockNo + '_' + fname);
276 var date_format = actualDateElement.attr('format');
277 var altDateFormat = 'yy-mm-dd';
278
279 var actualDateValue = cj.datepicker.parseDate(altDateFormat, dateValues[0]);
280
281 // format date according to display field
282 var hiddenDateValue = cj.datepicker.formatDate('mm/dd/yy', actualDateValue);
283
284 actualDateElement.val(hiddenDateValue);
285
286 var displayDateValue = actualDateElement.val();
287 if (date_format != 'mm/dd/yy') {
288 displayDateValue = cj.datepicker.formatDate(date_format, actualDateValue);
289 }
290
291 cj('#field_' + blockNo + '_' + fname + '_display').val(displayDateValue);
292
293 // need to fix time formatting
294 if (dateValues[1]) {
295 cj('#field_' + blockNo + '_' + fname + '_time').val(dateValues[1].substr(0, 5));
296 }
297}