Bug 36035: Wrong text colour in addorderiso2709.pl
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / addorderiso2709.js
1 /* global dataTablesDefaults __ template_path */
2
3 $(document).ready(function() {
4     $("#Aform").preventDoubleFormSubmit();
5     $("#files").dataTable($.extend(true, {}, dataTablesDefaults, {
6         "columnDefs":  [
7             { "orderable":  false, "searchable":  false, "targets":  [ 'NoSort' ] },
8             { "type":  "anti-the", "targets":  [ "anti-the" ] }
9         ],
10         "pagingType":  "full",
11         "order":  []
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     $(".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("order-selected");
26             $(container).removeClass("order-unselected");
27             $(container).find(".order_details").toggle(true);
28         } else {
29             $(container).addClass("order-unselected");
30             $(container).removeClass("order-selected");
31             $(container).find(".order_details").toggle(false);
32         }
33     } );
34
35     $("input:checkbox").prop("checked", false);
36     $("div.biblio.order-unselected select").prop('disabled', false);
37     $("div.biblio.order-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 funds are not defined in item records") );
71             return false;
72         }
73
74         if (0 < CheckMandatorySubfields(this.form)) {
75             // Open the item tab
76             $('.nav-tabs .items_info').tab('show');
77
78             alert(__('Some required item subfields are not set'));
79             return false;
80         }
81
82         disableUnchecked($(this.form));
83
84         $(this.form).submit();
85     });
86
87     $(".previewData").on("click", function(e){
88         e.preventDefault();
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});
94     });
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>");
98     });
99 });
100
101 function disableUnchecked(){
102     $("fieldset.biblio.order-unselected").each(function(){
103         $(this).remove();
104     });
105     return 1;
106 }
107
108 function checkOrderBudgets(){
109     var unset_funds = 0;
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.order-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);
124                             unset_funds++;
125                         } else {
126                             $(this).removeClass('required').prop("required", false);
127                         }
128                     });
129                 } else {
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();
134                     unset_funds++;
135                 }
136             } else {
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);
143                     }
144                 });
145                 $(this).removeClass('required').prop("required", false);
146             }
147         });
148     } else {
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();
157             }
158         });
159     }
160     return unset_funds;
161 }