Bug 24819: (follow-up) Rename suggestion_search to suggestor_search
[koha-ffzg.git] / koha-tmpl / intranet-tmpl / prog / js / addorderiso2709.js
1 /* global dataTablesDefaults ERR_NO_RECORD_SELECTED ERR_INVALID_QUANTITY ERR_FUNDS_MISSING MSG_LOADING */
2
3 $(document).ready(function() {
4     $("#Aform").preventDoubleFormSubmit();
5     $("#files").dataTable($.extend(true, {}, dataTablesDefaults, {
6         "aoColumnDefs": [
7             { "bSortable": false, "bSearchable": false, 'aTargets': [ 'NoSort' ] },
8             { "sType": "anti-the", "aTargets" : [ "anti-the" ] },
9             { "sType": "title-string", "aTargets" : [ "title-string" ] }
10         ],
11         "sPaginationType": "full",
12         "aaSorting": []
13     }) );
14
15     checkOrderBudgets();
16     var all_budget_id = $("#all_budget_id");
17
18     $("#all_budget_id,[name='budget_id'],.budget_code_item,[name='import_record_id']").on("change", function(){
19         checkOrderBudgets();
20     });
21
22     $("#records_to_import fieldset.rows div").hide();
23     $('input:checkbox[name="import_record_id"]').change(function(){
24         var container = $(this).parents("fieldset");
25         if ( $(this).is(':checked') ) {
26             $(container).addClass("selected");
27             $(container).removeClass("unselected");
28             $(container).find("div").toggle(true);
29         } else {
30             $(container).addClass("unselected");
31             $(container).removeClass("selected");
32             $(container).find("div").toggle(false);
33         }
34     } );
35
36     $("input:checkbox").prop("checked", false);
37     $("div.biblio.unselected select").prop('disabled', false);
38     $("div.biblio.unselected input").prop('disabled', false);
39
40     $("#checkAll").click(function(){
41         $("#Aform").checkCheckboxes();
42         $("input:checkbox[name='import_record_id']").change();
43         return false;
44     });
45     $("#unCheckAll").click(function(){
46         $("#Aform").unCheckCheckboxes();
47         $("input:checkbox[name='import_record_id']").change();
48         return false;
49     });
50
51     $("#Aform").on("submit", function(){
52         if ( $("input:checkbox[name='import_record_id']:checked").length < 1 ) {
53             alert( ERR_NO_RECORD_SELECTED );
54             return false;
55         }
56
57         var error = 0;
58         $("input:checkbox[name='import_record_id']:checked").parents('fieldset').find('input[name="quantity"]').each(function(){
59             if ( $(this).val().length < 1 || isNaN( $(this).val() ) ) {
60                 error++;
61             }
62         });
63         if ( error > 0 ) {
64             alert( error + " " + ERR_INVALID_QUANTITY );
65             return false;
66
67         }
68
69         error = checkOrderBudgets();
70         if ( error > 0 ) {
71             alert( ERR_FUNDS_MISSING );
72             return false;
73         }
74
75         return disableUnchecked($(this));
76     });
77
78     $('#tabs').tabs();
79     $(".previewData").on("click", function(e){
80         e.preventDefault();
81         var ltitle = $(this).text();
82         var page = $(this).attr("href");
83         $("#dataPreviewLabel").text(ltitle);
84         $("#dataPreview .modal-body").load(page + " div");
85         $('#dataPreview').modal({show:true});
86     });
87     $("#dataPreview").on("hidden.bs.modal", function(){
88         $("#dataPreviewLabel").html("");
89         $("#dataPreview .modal-body").html("<div id=\"loading\"><img src=\"[% interface | html %]/[% theme | html %]/img/spinner-small.gif\" alt=\"\" /> " + MSG_LOADING + "</div>");
90     });
91 });
92
93 function disableUnchecked(){
94     $("fieldset.biblio.unselected").each(function(){
95         $(this).remove();
96     });
97     return 1;
98 }
99
100 function checkOrderBudgets(){
101     var unset_funds = 0;
102     var all_budget_id = $("#all_budget_id");
103     // If we don't have an overarching default set we need to check each selected order
104     if ( !all_budget_id.val() ) {
105         $("fieldset.biblio.rows.selected").each(function(){
106             var default_order_fund = $(this).find("[name='budget_id']");
107             // For each order we see if budget is set for order
108             if( !default_order_fund.val() ){
109                 $(this).find(".item_fund.required").show();
110                 //If not we need to check each item on the order
111                 var item_funds = $(this).find(".budget_code_item");
112                 if( item_funds.length ){
113                     item_funds.each(function(){
114                         if( !$(this).val() ){
115                             $(this).addClass('required').prop("required", true);
116                             unset_funds++;
117                         } else {
118                             $(this).removeClass('required').prop("required", false);
119                         }
120                     });
121                 } else {
122                     //If the order has no items defined then the order level fund is required
123                     default_order_fund.addClass('required').prop("required", true);
124                     $(this).find(".fund span.required").show();
125                     $(this).find(".item_fund.required").hide();
126                     unset_funds++;
127                 }
128             } else {
129                 $(this).find(".fund span.required").hide();
130                 // If fund is set for order then none of the others are required
131                 $(this).find(".budget_code_item").each(function(){
132                     if( !$(this).val() ){
133                         $(this).val( default_order_fund.val() );
134                         $(this).removeClass('required').prop("required", false);
135                     }
136                 });
137                 $(this).removeClass('required').prop("required", false);
138             }
139         });
140     } else {
141         // Default is set overall, we just need to populate it through
142         // to each order/item
143         $("[name='budget_id'],.budget_code_item").each(function(){
144             if( !$(this).val() ){
145                 $(this).val( all_budget_id.val() );
146                 $(this).removeClass('required').prop("required", false);
147                 $(".item_fund.required").hide();
148                 $(".fund span.required").hide();
149             }
150         });
151     }
152     return unset_funds;
153 }