static/org.openpsa.invoices/invoices.js   A
last analyzed

Complexity

Total Complexity 23
Complexity/F 1.28

Size

Lines of Code 106
Function Count 18

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 69
c 0
b 0
f 0
dl 0
loc 106
rs 10
wmc 23
mnd 5
bc 5
fnc 18
bpm 0.2777
cpm 1.2777
noi 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A invoices.js ➔ calculate_total 0 10 3
A invoices.js ➔ format_number 0 6 2
A invoices.js ➔ calculate_row 0 10 2
A invoices.js ➔ hide_invoice_address 0 7 2
1
/* function to parse the input and exchanges , with .
2
*/
3
function parse_input(string) {
4
    return parseFloat(string.replace(',' , '.'));
5
}
6
7
function format_number(input) {
8
    if ($.fn.fmatter) {
9
        return $.fn.fmatter.number(input, $.jgrid.locales[$.jgrid.defaults.locale].formatter);
10
    }
11
    return input.toFixed(2);
12
}
13
14
function calculate_row(id) {
15
    var price_unit = parse_input($("#price_per_unit_" + id).val()),
16
        units = parse_input($("#units_" + id).val()),
17
        sum = 0;
18
    //check if they are numbers
19
    if (!isNaN(price_unit) && !isNaN(units)) {
20
        sum = price_unit * units;
21
    }
22
    $('#row_sum_' + id).html(format_number(sum));
23
}
24
25
function calculate_total(table) {
26
    var total = 0;
27
    table.find('tbody tr').each(function() {
28
        if ($(this).find('input[type="checkbox"]').is(':checked')) {
29
            total += parse_input($(this).find('.units').val()) * parse_input($(this).find('.price_per_unit').val());
30
        }
31
    });
32
33
    table.find('tfoot .totals').text(format_number(total));
34
}
35
36
function hide_invoice_address() {
37
    if ($('#org_openpsa_invoices_use_contact_address').is(':checked')) {
38
        $(".invoice_address").hide();
39
    } else {
40
        $(".invoice_address").show();
41
    }
42
}
43
44
$(document).ready(function() {
45
    if ($('#org_openpsa_invoices_use_contact_address').length > 0) {
46
        hide_invoice_address();
47
        $('#org_openpsa_invoices_use_contact_address').change(function() {
48
            hide_invoice_address();
49
        });
50
    }
51
52
    $('.projects table')
53
        .on('change', 'input[type="text"]', function() {
54
            var task_id = $(this).closest('tr').attr('id').replace('task_', '');
55
            calculate_row(task_id);
56
            calculate_total($(this).closest('table'));
57
        })
58
        .on('change', 'input[type="checkbox"]', function() {
59
            calculate_total($(this).closest('table'));
60
        })
61
        .each(function() {
62
            calculate_total($(this));
63
        })
64
        .parent().on('submit', function() {
65
            $(this).find('.numeric input').each(function() {
66
                $(this).val(parse_input($(this).val()));
67
            });
68
        });
69
70
    $('#add-journal-entry').on('click', function() {
71
        var button = $(this),
72
            dialog,
73
            options = {
74
                title:  this.title,
75
                resizable: false,
76
                modal: true,
77
                buttons: {}
78
            },
79
            form = $('<form action="' + MIDCOM_PAGE_PREFIX + '__mfa/org.openpsa.relatedto/rest/journalentry/" method="post">'),
80
            text = $('<input type="text" required name="title" class="add-journal-text">').appendTo(form),
81
            submit = $('<input type="submit">')
82
                    .hide()
83
                    .appendTo(form);
84
85
        options.buttons[this.dataset.dialogSubmitLabel] = function() {
86
            submit.click();
87
        };
88
        options.buttons[this.dataset.dialogCancelLabel] = function() {
89
            $( this ).dialog( "close" );
90
        };
91
        dialog = $('<div>')
92
            .append(form)
93
            .appendTo($('body'))
94
            .dialog(options);
95
96
        form.on('submit', function(e) {
97
            e.preventDefault();
98
            $.post(this.action, {
99
                linkGuid: button.data('guid'),
100
                title: text.val()
101
            },
102
            function () {
103
                dialog.dialog("close");
104
                window.location.reload();
105
            });
106
        });
107
    });
108
});
109