Bug 22734: Fund not marked as mandatory when ordering from a staged file
[koha-equinox.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     $("#files").dataTable($.extend(true, {}, dataTablesDefaults, {
5         "aoColumnDefs": [
6             { "bSortable": false, "bSearchable": false, 'aTargets': [ 'NoSort' ] },
7             { "sType": "anti-the", "aTargets" : [ "anti-the" ] },
8             { "sType": "title-string", "aTargets" : [ "title-string" ] }
9         ],
10         "sPaginationType": "four_button",
11         "aaSorting": []
12     }) );
13
14     var all_budget_id = $("#all_budget_id");
15
16     if( !all_budget_id.val() ){
17         $(".fund label, .fund select").addClass("required").prop("required", true);
18         $(".fund span.required").show();
19     }
20
21     all_budget_id.on("change", function(){
22         if( $(this).val() != "" ){
23             $(".fund label, .fund select").removeClass("required").prop("required", false);
24             $(".fund select").each(function(){
25                 if( $(this).val() == '' ){
26                     $(this).val( all_budget_id.val() );
27                 }
28             });
29             $(".fund span.required").hide();
30         } else {
31             $(".fund label, .fund select").addClass("required").prop("required", true);
32             $(".fund span.required").show();
33         }
34     });
35
36     $("#records_to_import fieldset.rows div").hide();
37     $('input:checkbox[name="import_record_id"]').change(function(){
38         var container = $(this).parents("fieldset");
39         if ( $(this).is(':checked') ) {
40             $(container).addClass("selected");
41             $(container).removeClass("unselected");
42             $(container).find("div").toggle(true);
43         } else {
44             $(container).addClass("unselected");
45             $(container).removeClass("selected");
46             $(container).find("div").toggle(false);
47         }
48     } );
49
50     $("input:checkbox").prop("checked", false);
51     $("div.biblio.unselected select").prop('disabled', false);
52     $("div.biblio.unselected input").prop('disabled', false);
53
54     $("#checkAll").click(function(){
55         $("#Aform").checkCheckboxes();
56         $("input:checkbox[name='import_record_id']").change();
57         return false;
58     });
59     $("#unCheckAll").click(function(){
60         $("#Aform").unCheckCheckboxes();
61         $("input:checkbox[name='import_record_id']").change();
62         return false;
63     });
64
65     $("#Aform").on("submit", function(){
66         if ( $("input:checkbox[name='import_record_id']:checked").length < 1 ) {
67             alert( ERR_NO_RECORD_SELECTED );
68             return false;
69         }
70
71         var error = 0;
72         $("input:checkbox[name='import_record_id']:checked").parents('fieldset').find('input[name="quantity"]').each(function(){
73             if ( $(this).val().length < 1 || isNaN( $(this).val() ) ) {
74                 error++;
75             }
76         });
77         if ( error > 0 ) {
78             alert( error + " " + ERR_INVALID_QUANTITY );
79             return false;
80
81         }
82
83         if (! all_budget_id.val() ) {
84             // If there is no default fund
85             error = 0;
86             $(".selected [name='budget_id']").each(function(){
87                 if (!$(this).val()) {
88                     error++;
89                 }
90             });
91             if ( error > 0 ) {
92                 alert( ERR_FUNDS_MISSING );
93                 return false;
94             }
95         }
96
97         return disableUnchecked($(this));
98     });
99
100     $('#tabs').tabs();
101     $(".previewData").on("click", function(e){
102         e.preventDefault();
103         var ltitle = $(this).text();
104         var page = $(this).attr("href");
105         $("#dataPreviewLabel").text(ltitle);
106         $("#dataPreview .modal-body").load(page + " div");
107         $('#dataPreview').modal({show:true});
108     });
109     $("#dataPreview").on("hidden.bs.modal", function(){
110         $("#dataPreviewLabel").html("");
111         $("#dataPreview .modal-body").html("<div id=\"loading\"><img src=\"[% interface | html %]/[% theme | html %]/img/spinner-small.gif\" alt=\"\" /> " + MSG_LOADING + "</div>");
112     });
113 });
114
115 function disableUnchecked(){
116     $("fieldset.biblio.unselected").each(function(){
117         $(this).remove();
118     });
119     return 1;
120 }