dd44411cf84c568c59faac5b07ee10e0ad8e314a
[kivitendo-erp.git] / js / kivi.TimeRecording.js
1 namespace('kivi.TimeRecording', function(ns) {
2   'use strict';
3
4   ns.inputs_to_disable = [];
5
6   ns.set_end_date = function() {
7     if ($('#start_date').val() !== '' && $('#end_date').val() === '') {
8       var kivi_start_date  = kivi.format_date(kivi.parse_date($('#start_date').val()));
9       $('#end_date').val(kivi_start_date);
10     }
11   };
12
13   ns.set_current_date_time = function(what) {
14     if (what !== 'start' && what !== 'end') return;
15
16     var $date = $('#' + what + '_date');
17     var $time = $('#' + what + '_time');
18     var date = new Date();
19
20     $date.val(kivi.format_date(date));
21     $time.val(kivi.format_time(date));
22   };
23
24   ns.order_changed = function(value) {
25     if (!value) {
26       $('#time_recording_customer_id').data('customer_vendor_picker').set_item({});
27       $('#time_recording_customer_id_name').prop('disabled', false);
28       $('#time_recording_project_id').data('project_picker').set_item({});
29       $('#time_recording_project_id_name').prop('disabled', false);
30       return;
31     }
32
33     var url = 'controller.pl?action=TimeRecording/ajaj_get_order_info&id='+ value;
34     $.getJSON(url, function(data) {
35       $('#time_recording_customer_id').data('customer_vendor_picker').set_item(data.customer);
36       $('#time_recording_customer_id_name').prop('disabled', true);
37       $('#time_recording_project_id').data('project_picker').set_item(data.project);
38       $('#time_recording_project_id_name').prop('disabled', true);
39     });
40   };
41
42   ns.project_changed = function() {
43     var project_id = $('#time_recording_project_id').val();
44
45     if (!project_id) {
46       $('#time_recording_customer_id_name').prop('disabled', false);
47       return;
48     }
49
50     var url = 'controller.pl?action=TimeRecording/ajaj_get_project_info&id='+ project_id;
51     $.getJSON(url, function(data) {
52       if (data) {
53         $('#time_recording_customer_id').data('customer_vendor_picker').set_item(data.customer);
54         $('#time_recording_customer_id_name').prop('disabled', true);
55       } else {
56         $('#time_recording_customer_id_name').prop('disabled', false);
57       }
58     });
59   };
60
61   ns.set_input_constraints = function() {
62     $(ns.inputs_to_disable).each(function(idx, elt) {
63       if ("customer" === elt) {
64         $('#time_recording_customer_id_name').prop('disabled', true);
65       }
66       if ("project" === elt) {
67         $('#time_recording_project_id_name').prop('disabled', true);
68       }
69     });
70   };
71
72 });
73
74 $(function() {
75   kivi.TimeRecording.set_input_constraints();
76   $('#time_recording_project_id').on('set_item:ProjectPicker', function(){ kivi.TimeRecording.project_changed() });
77 });