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