1 /* global dataTablesDefaults __ template_path */
3 $(document).ready(function() {
4 $("#Aform").preventDoubleFormSubmit();
5 $("#files").dataTable($.extend(true, {}, dataTablesDefaults, {
7 { "bSortable": false, "bSearchable": false, 'aTargets': [ 'NoSort' ] },
8 { "sType": "anti-the", "aTargets" : [ "anti-the" ] }
10 "sPaginationType": "full",
15 var all_budget_id = $("#all_budget_id");
17 $("#all_budget_id,[name='budget_id'],.budget_code_item,[name='import_record_id']").on("change", function(){
21 $(".order_details").hide();
22 $('input:checkbox[name="import_record_id"]').change(function(){
23 var container = $(this).parents("tr");
24 if ( $(this).is(':checked') ) {
25 $(container).addClass("selected");
26 $(container).removeClass("unselected");
27 $(container).find(".order_details").toggle(true);
29 $(container).addClass("unselected");
30 $(container).removeClass("selected");
31 $(container).find(".order_details").toggle(false);
35 $("input:checkbox").prop("checked", false);
36 $("div.biblio.unselected select").prop('disabled', false);
37 $("div.biblio.unselected input").prop('disabled', false);
39 $("#checkAll").click(function(e){
41 $("input:checkbox[name='import_record_id']").prop("checked", true).change();
43 $("#unCheckAll").click(function(e){
45 $("input:checkbox[name='import_record_id']").prop("checked", false).change();
48 $("input#add_order").on("click", function(e){
51 if ( $("input:checkbox[name='import_record_id']:checked").length < 1 ) {
52 alert( __("There is no record selected") );
57 $("input:checkbox[name='import_record_id']:checked").parents('fieldset').find('input[name="quantity"]').each(function(){
58 if ( $(this).val().length < 1 || isNaN( $(this).val() ) ) {
63 alert(error + " " + __("quantity values are not filled in or are not numbers") );
68 error = checkOrderBudgets();
70 alert( __("Some funds are not defined in item records") );
74 if (0 < CheckMandatorySubfields(this.form)) {
76 $('.nav-tabs .items_info').tab('show');
78 alert(__('Some required item subfields are not set'));
82 disableUnchecked($(this.form));
84 $(this.form).submit();
87 $(".previewData").on("click", function(e){
89 var ltitle = $(this).text();
90 var page = $(this).attr("href");
91 $("#dataPreviewLabel").text(ltitle);
92 $("#dataPreview .modal-body").load(page + " div");
93 $('#dataPreview').modal({show:true});
95 $("#dataPreview").on("hidden.bs.modal", function(){
96 $("#dataPreviewLabel").html("");
97 $("#dataPreview .modal-body").html("<div id=\"loading\"><img src=\"" + template_path + "/img/spinner-small.gif\" alt=\"\" /> " + __("Loading") + "</div>");
101 function disableUnchecked(){
102 $("fieldset.biblio.unselected").each(function(){
108 function checkOrderBudgets(){
110 var all_budget_id = $("#all_budget_id");
111 // If we don't have an overarching default set we need to check each selected order
112 if ( !all_budget_id.val() ) {
113 $("fieldset.biblio.rows.selected").each(function(){
114 var default_order_fund = $(this).find("[name='budget_id']");
115 // For each order we see if budget is set for order
116 if( !default_order_fund.val() ){
117 $(this).find(".item_fund.required").show();
118 //If not we need to check each item on the order
119 var item_funds = $(this).find(".budget_code_item");
120 if( item_funds.length ){
121 item_funds.each(function(){
122 if( !$(this).val() ){
123 $(this).addClass('required').prop("required", true);
126 $(this).removeClass('required').prop("required", false);
130 //If the order has no items defined then the order level fund is required
131 default_order_fund.addClass('required').prop("required", true);
132 $(this).find(".fund span.required").show();
133 $(this).find(".item_fund.required").hide();
137 $(this).find(".fund span.required").hide();
138 // If fund is set for order then none of the others are required
139 $(this).find(".budget_code_item").each(function(){
140 if( !$(this).val() ){
141 $(this).val( default_order_fund.val() );
142 $(this).removeClass('required').prop("required", false);
145 $(this).removeClass('required').prop("required", false);
149 // Default is set overall, we just need to populate it through
150 // to each order/item
151 $("[name='budget_id'],.budget_code_item").each(function(){
152 if( !$(this).val() ){
153 $(this).val( all_budget_id.val() );
154 $(this).removeClass('required').prop("required", false);
155 $(".item_fund.required").hide();
156 $(".fund span.required").hide();