Bug 25563: (bug 24386 follow-up) Don't disable submit button if form has not been...
[koha.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     $("input#add_order").on("click", function(e){
52         e.preventDefault();
53
54         if ( $("input:checkbox[name='import_record_id']:checked").length < 1 ) {
55             alert( ERR_NO_RECORD_SELECTED );
56             return false;
57         }
58
59         var error = 0;
60         $("input:checkbox[name='import_record_id']:checked").parents('fieldset').find('input[name="quantity"]').each(function(){
61             if ( $(this).val().length < 1 || isNaN( $(this).val() ) ) {
62                 error++;
63             }
64         });
65         if ( error > 0 ) {
66             alert( error + " " + ERR_INVALID_QUANTITY );
67             return false;
68
69         }
70
71         error = checkOrderBudgets();
72         if ( error > 0 ) {
73             alert( ERR_FUNDS_MISSING );
74             return false;
75         }
76
77         disableUnchecked($(this.form));
78
79         $(this.form).submit();
80     });
81
82     $('#tabs').tabs();
83     $(".previewData").on("click", function(e){
84         e.preventDefault();
85         var ltitle = $(this).text();
86         var page = $(this).attr("href");
87         $("#dataPreviewLabel").text(ltitle);
88         $("#dataPreview .modal-body").load(page + " div");
89         $('#dataPreview').modal({show:true});
90     });
91     $("#dataPreview").on("hidden.bs.modal", function(){
92         $("#dataPreviewLabel").html("");
93         $("#dataPreview .modal-body").html("<div id=\"loading\"><img src=\"[% interface | html %]/[% theme | html %]/img/spinner-small.gif\" alt=\"\" /> " + MSG_LOADING + "</div>");
94     });
95 });
96
97 function disableUnchecked(){
98     $("fieldset.biblio.unselected").each(function(){
99         $(this).remove();
100     });
101     return 1;
102 }
103
104 function checkOrderBudgets(){
105     var unset_funds = 0;
106     var all_budget_id = $("#all_budget_id");
107     // If we don't have an overarching default set we need to check each selected order
108     if ( !all_budget_id.val() ) {
109         $("fieldset.biblio.rows.selected").each(function(){
110             var default_order_fund = $(this).find("[name='budget_id']");
111             // For each order we see if budget is set for order
112             if( !default_order_fund.val() ){
113                 $(this).find(".item_fund.required").show();
114                 //If not we need to check each item on the order
115                 var item_funds = $(this).find(".budget_code_item");
116                 if( item_funds.length ){
117                     item_funds.each(function(){
118                         if( !$(this).val() ){
119                             $(this).addClass('required').prop("required", true);
120                             unset_funds++;
121                         } else {
122                             $(this).removeClass('required').prop("required", false);
123                         }
124                     });
125                 } else {
126                     //If the order has no items defined then the order level fund is required
127                     default_order_fund.addClass('required').prop("required", true);
128                     $(this).find(".fund span.required").show();
129                     $(this).find(".item_fund.required").hide();
130                     unset_funds++;
131                 }
132             } else {
133                 $(this).find(".fund span.required").hide();
134                 // If fund is set for order then none of the others are required
135                 $(this).find(".budget_code_item").each(function(){
136                     if( !$(this).val() ){
137                         $(this).val( default_order_fund.val() );
138                         $(this).removeClass('required').prop("required", false);
139                     }
140                 });
141                 $(this).removeClass('required').prop("required", false);
142             }
143         });
144     } else {
145         // Default is set overall, we just need to populate it through
146         // to each order/item
147         $("[name='budget_id'],.budget_code_item").each(function(){
148             if( !$(this).val() ){
149                 $(this).val( all_budget_id.val() );
150                 $(this).removeClass('required').prop("required", false);
151                 $(".item_fund.required").hide();
152                 $(".fund span.required").hide();
153             }
154         });
155     }
156     return unset_funds;
157 }