prepa_odoo.js 25.1 KB
Newer Older
Administrator committed
1 2 3
var to_fill_box = $('#to_fill'),
    with_errors_box = $('#with_errors'),
    coops = {'to_fill': [],
4 5 6 7
        'with_errors': [],
        'waiting_validation_employee':[],
        'waiting_validation_member':[],
        'done':[]},
Administrator committed
8
    validation_next_steps = {'to_fill': 'waiting_validation_employee',
9 10
        'waiting_validation_employee': 'waiting_validation_member',
        'waiting_validation_member': 'done'},
Administrator committed
11 12 13 14 15 16 17 18 19 20 21 22 23

    dashboard = $('#dashboard'),

    coop_validation_form = $('#coop_validation_form'),
    warning_slide = $('#warning_slide'),
    warning_msg = $('#new_warning_form textarea[name="message"]'),
    waiting_validation_employee_div = $('#waiting_validation_employee'),
    waiting_validation_member_div = $('#waiting_validation_member'),
    done_div = $('#done'),
    form_delete = $('#form_delete'),
    problem_delete = $('#problem_delete'),
    vform = $('#coop_validation_form');

24 25 26 27 28 29 30 31 32 33
// date validation
Date.prototype.isValid = function () {

    // If the date object is invalid it
    // will return 'NaN' on getTime()
    // and NaN is never equal to itself.
    return this.getTime() === this.getTime();
};


Administrator committed
34
sync.on('change', function (info) {
35 36 37 38
    // handle change
    if (info.direction == 'pull') {
        retrieve_all_coops();
    }
Administrator committed
39 40 41 42 43
}).on('paused', function (err) {
    // replication paused (e.g. replication up to date, user went offline)
    if (err) {
        online = false;
    }
44 45 46 47 48
})
    .on('active', function () {
    // replicate resumed (e.g. new changes replicating, user went back online)
        online = true;
    })
Damien Moulard committed
49
    .on('denied', function () {
50 51
    // a document failed to replicate (e.g. due to permissions)
    })
Damien Moulard committed
52
    .on('complete', function () {
53 54 55 56 57 58 59
    // handle complete
    })
    .on('error', function (err) {
    // handle error
        console.log('erreur sync');
        console.log(err);
    });
Administrator committed
60 61


62 63 64 65
function home() {
    coop_page.hide();
    warning_slide.hide();
    dashboard.show();
Administrator committed
66 67 68
}

function is_in_coops(mail) {
69 70 71 72 73 74 75 76 77 78 79
    var answer = false;

    for (key in coops) {
        $.each(coops[key], function(i, e) {
            if (e._id == mail) {
                answer = true;
            }
        });
    }

    return answer;
Administrator committed
80 81 82 83
}

// Store/update coop data in couchdb
function store_current_coop_data(callback) {
84
    dbc.put(current_coop, function(err, result) {
Administrator committed
85
        if (!err) {
86 87
            current_coop._rev = result.rev;
            if (callback) callback(err);
Administrator committed
88 89 90
        } else {
            console.log(err);
            if (err.name == 'conflict') {
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
                dbc.get(current_coop._id, {'latest': true})
                    .then(function(retrieved_coop) {
                        if (current_coop.errors) {
                            var err_msg = current_coop.errors;

                            if (retrieved_coop.errors && retrieved_coop.errors != err_msg) {
                                err_msg += ' ' + retrieved_coop.errors;
                                current_coop.errors = err_msg;
                            }

                        }
                        current_coop._rev = retrieved_coop._rev;
                        console.log('Enregistrement redemandé');
                        store_current_coop_data(callback);
                    });
Administrator committed
106 107
            }
        }
108
    });
Administrator committed
109 110 111 112
}

// Remove old document if id (email) changed before applying changes
function put_current_coop_in_buffer_db(callback) {
113 114 115
    var can_continue = true;

    if (typeof current_coop._old_id != "undefined") {
Damien Moulard committed
116
        dbc.remove(current_coop._old_id, current_coop._rev, function(err) {
117 118 119 120 121 122 123 124 125 126 127 128
            if (err) {
                console.log(err); can_continue = false;
            }
        });
        delete current_coop._rev;
        delete current_coop._old_id;
    }
    if (can_continue == true) {
        store_current_coop_data(callback);
    } else {
        alert('Problème de sauvegarde des modifications');
    }
Administrator committed
129 130
}

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
function process_new_warning(event) {
    event.preventDefault();
    var msg = warning_msg.val();

    openModal();
    if (msg.length > 0) {
        current_coop.errors = msg;
        store_current_coop_data(function() {
            retrieve_all_coops();
            closeModal();
            home();
            display_msg_box('Enregistrement signalement problème terminé.', 'success');
        });
    } else {
        closeModal();
        alert('Le message est vide !');
    }
Administrator committed
148 149 150 151
}

// Set current coop to previous validation state
function previous_validation_state() {
152 153 154 155 156 157
    for (var key in validation_next_steps) {
        if (current_coop.validation_state == validation_next_steps[key]) {
            current_coop.validation_state = key;
            store_current_coop_data();
            break;
        }
Administrator committed
158 159 160 161
    }
}

function submit_full_coop_form() {
162 163 164 165
    let form = $('#coop_validation_form');
    var form_data = new FormData(form.get(0)),
        m_barcode = form.find('[name="m_barcode"]'),
        sex = $('#sex'),
166
        job = $('#job'),
167
        has_empty_values = false;
Administrator committed
168

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    for (var pair of form_data.entries()) {
        let val = pair[1],
            key = pair[0];

        if ($('input[name="' + key +'"]').get(0)
            .hasAttribute('required') && val.length == 0) {
            has_empty_values = true;
        }
    }

    if (has_empty_values == true) {
        closeModal();
        alert('Vous devez remplir tous les champs pour valider.');
        // If form not submitted, set back coop current validation_state
        previous_validation_state();
    } else {
        form_data.set(
            'firstname',
            vform.find('input[name="firstname"]').val()
                .toFormatedFirstName()
        );
        form_data.set(
            'lastname',
            vform.find('input[name="lastname"]').val()
                .toFormatedLastName()
        );
        form_data.set('odoo_id', current_coop.odoo_id);

        form_data.set('shift_template', JSON.stringify(current_coop.shift_template));
        //Fields beeing disabled : force value
        form_data.set('shares_nb', vform.find('input[name="shares_nb"]').val());
        form_data.set('shares_euros', vform.find('input[name="shares_euros"]').val());
        form_data.set('checks_nb', vform.find('input[name="checks_nb"]').val());
        form_data.set('payment_meaning', vform.find('input[name="payment_meaning"]').val());
        if (m_barcode.length > 0) {
            form_data.set('m_barcode', m_barcode.val());
        }
206 207 208
        if (sex.length > 0) {
            form_data.set('sex', $('input[name="sex"]:checked').val());
        }
209 210 211 212 213 214
        if (job.length > 0) {
            form_data.set(
                'function',
                vform.find('input[name="job"]').val()
            );
        }
215 216
        post_form(
            '/members/coop_validated_data', form_data,
Damien Moulard committed
217
            function(err) {
218 219 220 221 222 223 224
                if (!err) {
                    setTimeout(after_save, 1500);
                } else {
                    console.log(err);
                }
            }
        );
Administrator committed
225 226 227 228
    }
}

function odoo_create_coop() {
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    var current_coop_copy = current_coop;

    current_coop_copy.shift_template = JSON.stringify(current_coop.shift_template);
    $.post({url : '/members/create_from_buffered_data/',
        headers: { "X-CSRFToken": getCookie("csrftoken") },
        data : current_coop_copy,
        dataType :'json'
    })
        .done(function(rData) {
            if (rData.odoo_id && ! isNaN(rData.odoo_id)) {
                after_save();
            } else {
                alert('Erreur pendant l\'enregistrement Odoo');
            }
        })
        .fail(function() {
            after_save(1);
        });
Administrator committed
247 248 249 250 251
}

// Save the current coop details from form values
// Then do callback function
function save_current_coop(callback) {
252 253 254
    //_id obligatoire !
    let form = coop_validation_form,
        _id = form.find('[name="email"]').val(),
255
        m_barcode = form.find('[name="m_barcode"]'),
256 257
        sex = form.find('[name="sex"]'),
        job = form.find('[name="job"]');
258 259

    if (current_coop != null && _id.length > 0) {
Administrator committed
260
    //Birthdate verification
261 262 263
        let birthdate = form.find('[name="birthdate"]').val()
            .trim();
        var birthdate_error = false,
264 265
            m_barcode_error = false,
            sex_error = false;
266 267

        if (/([0-9]{2})\/([0-9]{2})\/([0-9]{4})/.exec(birthdate)) {
268
            try {
269
                var jj = RegExp.$1,
270 271
                    mm = RegExp.$2,
                    aaaa = RegExp.$3;
272

273 274
                let tmp_date = aaaa + "-" + mm + "-" + jj;
                // try to create a date object
275

276 277 278
                date_test = new Date(tmp_date);
                // if date is invalid a correction is apply in date object. Check it
                // january start at 0, so we add + 1 for the month
279
                if ((date_test.getDate() !== parseInt(jj)) || ((date_test.getMonth()+1) !== parseInt(mm)) || (date_test.getFullYear() !== parseInt(aaaa)) || !date_test.isValid()) {
280 281
                    birthdate_error = true;
                }
282 283 284 285
                // do not allow years starting with a 0 as it causes bugs later in odoo
                if (aaaa < 1000) {
                    birthdate_error = true;
                }
286
            } catch (Exception) {
287 288
                birthdate_error = true;
            }
289

290 291 292
        } else {
            birthdate_error = true;
        }
Administrator committed
293

294 295


296 297
        let street2_input = form.find('[name="street2"]'),
            phone_input = form.find('[name="phone"]');
Administrator committed
298

299 300 301 302 303 304 305
        current_coop.firstname = form.find('[name="firstname"]').val()
            .toFormatedFirstName();
        current_coop.lastname = form.find('[name="lastname"]').val()
            .toFormatedLastName();
        if (current_coop._id != _id) {
            current_coop._old_id = current_coop._id;
        }
Administrator committed
306

307 308 309
        current_coop._id = _id;
        current_coop.birthdate = birthdate;
        current_coop.address = form.find('[name="address"]').val();
310 311 312 313 314

        if (sex.length > 0) {
            current_coop.sex = $('input[name="sex"]:checked').val();
            if (typeof current_coop.sex == "undefined") sex_error = true;
        }
315 316 317
        if (job.length > 0) {
            current_coop.function = $('input[name="job"]').val();
        }
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
        if (street2_input.length > 0) {
            current_coop.street2 = street2_input.val();
        }
        current_coop.city = form.find('[name="city"]').val();
        current_coop.zip = form.find('[name="zip"]').val();
        current_coop.country = form.find('[name="country"]').val();
        current_coop.mobile = form.find('[name="mobile"]').val();
        if (phone_input.length > 0) {
            current_coop.phone = phone_input.val();
        }
        current_coop.shares_nb = form.find('[name="shares_nb"]').val();
        current_coop.shares_euros = form.find('[name="shares_euros"]').val();
        current_coop.checks_nb = form.find('[name="checks_nb"]').val();
        current_coop.payment_meaning = form.find('[name="payment_meaning"]').val();
        if (m_barcode.length > 0) {
            current_coop.m_barcode = m_barcode.val();
            if (!isValidEAN13(current_coop.m_barcode)) m_barcode_error = true;
        }
336
        if ((birthdate_error == true || m_barcode_error == true || sex_error == true) && callback) {
337 338 339 340 341 342 343 344
            put_current_coop_in_buffer_db();
            closeModal();
            var msg = '';

            if (birthdate_error == true)
                msg += "La date de naissance ne semble pas correcte (jj/mm/aaaa)\n";
            if (m_barcode_error == true)
                msg += "Le code-barre n'est pas valide\n";
345 346
            if (sex_error == true)
                msg += "Une option concernant le sexe doit être cochée\n";
347 348 349 350 351 352 353
            alert(msg);
        } else {
            // Send coop to next step
            if (callback) current_coop.validation_state = validation_next_steps[current_coop.validation_state];
            //if not were are in the case of problem warning (no validation step change)
            put_current_coop_in_buffer_db(callback);
        }
Administrator committed
354 355 356 357
    }
}

// After save, get coops again and reset page
358 359 360
function after_save(err) {
    closeModal();
    if (!err) {
Administrator committed
361
    // put_current_coop_in_buffer_db(retrieve_all_coops);
362 363 364 365 366
        retrieve_all_coops();
        home();
    } else {
        alert('Une erreur est survenue pendant l\'enregistrement');
    }
Administrator committed
367 368 369
}

// Validate & store sensitive data from form, check for errors, then do relevant action according to process state
370 371 372 373 374 375 376 377 378 379 380 381
function process_validation_form(event) {
    event.preventDefault();
    //Shares nb and euros are consistent ?
    var shares_nb = parseInt($('[name="shares_nb"]').val(), 10);
    var shares_euros = parseInt($('[name="shares_euros"]').val(), 10);
    var is_valid = true;

    if (!isNaN(shares_nb) && !isNaN(shares_euros)) {
        if (shares_euros != 10*shares_nb) {
            is_valid = false;
            alert('Le nombre de parts et le montant en euros ne correspondent pas !');
        }
Administrator committed
382 383
    }

384
    if (current_coop.payment_meaning == "ch" && current_coop.checks_nb < 1) {
Administrator committed
385
        is_valid = false;
386
        alert('Le nombre de chèques doit être supérieur à 0 !');
Administrator committed
387 388
    }

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
    // Save checks details
    var total = 0;

    if (current_coop.validation_state == 'waiting_validation_employee' && current_coop.payment_meaning == "ch" && current_coop.checks_nb > 1) {
        var checks = [];

        for (var i = 1; i <= current_coop.checks_nb; i++) {
            var check_value = parseInt($('[name="check_' + i + '"]').val());

            if (check_value == 0) {
                is_valid = false;
                alert('Un chèque ne peut pas avoir une valeur à 0 !');
            } else {
                checks.push(check_value);
                total += check_value;
            }
        }

        if (total != $('[name="shares_euros"]').val()) {
            if (is_valid) {
                is_valid = false;
                alert('La somme des chèques ne correspond pas au montant de la cotisation !');
            }
        } else {
            current_coop.checks = JSON.stringify(checks);
        }
Administrator committed
415
    }
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

    if (is_time_to('save_form_in_odoo')) {
        if (is_valid == true) {
            openModal();

            if (current_coop.validation_state == "to_fill") {
                save_current_coop(after_save); // First step, coop info only saved in couchdb
            } else if (current_coop.validation_state == 'waiting_validation_employee') {
                save_current_coop(odoo_create_coop); // Second step, coop is created in odoo
            } else if (current_coop.validation_state == 'waiting_validation_member') {
                save_current_coop(submit_full_coop_form);
            }
        } else {
            alert('Formulaire non valide.');
            closeModal();
        }
Administrator committed
432
    } else {
433
        closeModal();
Administrator committed
434 435 436
    }
}

437 438 439 440 441 442
function process_signaler_click() {
    var coop_msg = '';

    if (current_coop.coop_msg) {
        coop_msg = 'Message coop : ' + current_coop.coop_msg;
    } else {
Administrator committed
443
    //save cuuren_coop with already given data
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
        save_current_coop();
    }

    dashboard.hide();
    coop_page.hide();
    // Connected user can delete a coop if errors exist
    if (!coop_is_connected() || (typeof (current_coop.errors) == "undefined" && typeof (current_coop.coop_msg) == "undefined")) {
        form_delete.hide();
        problem_delete.hide();
    } else if (coop_is_connected()) {
        if (typeof (current_coop.errors) != "undefined" || typeof (current_coop.coop_msg) != "undefined")
            form_delete.show();
        problem_delete.show();
    }

    warning_slide.find('[name="firstname"]').val(current_coop.firstname);
    warning_slide.find('[name="lastname"]').val(current_coop.lastname);
    //warning_slide.find('[name="barcode_base"]').val(current_coop.barcode_base);
    warning_slide.find('[name="message"]').val(current_coop.errors || '');
    warning_slide.find('p.coop_msg').html(coop_msg);
    if (current_coop.odoo_state && current_coop.odoo_state == 'done') {
        form_delete.show();
    }
    warning_slide.show();
Administrator committed
468 469 470
}

// Select the coop being udpated
471 472 473 474 475 476
function set_current_coop(clicked, callback) {
    var id = clicked.id;
    var type = clicked.validation_state;
    var coops_set = null;
    //coop_form is always empty, in order to remove all previous data, which could be associated to another coop.

477 478
    coop_validation_form.find(':input').not('[type="radio"]')
        .val('');
479 480 481 482 483 484 485 486 487 488 489 490
    if (type == 'to_fill') {
        coops_set = coops.to_fill;
    } else if (type == 'with_errors') {
        coops_set = coops.with_errors;
    } else if (type == 'waiting_validation_employee') {
        coops_set = coops.waiting_validation_employee;
    } else if (type == 'waiting_validation_member') {
        coops_set = coops.waiting_validation_member;
    } else if (type == 'done') {
        coops_set = coops.done;
    } else {
        coops_set = coops.to_fill;
Administrator committed
491
    }
492 493 494 495 496 497 498 499 500 501
    if (coops_set != null) {
        if (type != 'done') {
            dashboard.hide();
        }
        for (i in coops_set) {
            if (coops_set[i]._id == id) {
                current_coop = coops_set[i];
                callback();
            }
        }
Administrator committed
502 503 504 505 506
    }
}

// Archive a member record: delete from temp db
function coop_form_delete() {
507
    try {
Administrator committed
508
    //Call django method (instead of pouchdb call)
509 510 511 512 513 514 515 516 517 518 519 520
        post_form(
            '/members/remove_data_from_couchdb',
            {email: current_coop._id},
            function(err, result) {
                if (!err) {
                    if (typeof(result.msg) != "undefined") {
                        alert(result.msg);
                    } else if (result.action === null) {
                        display_msg_box('Données supprimées.');
                        current_coop = null;
                        closeModal();
                        home();
Administrator committed
521
                    }
522 523 524 525 526 527
                }
            }
        );
    } catch (e) {
        console.log(e);
    }
Administrator committed
528 529 530
}

function coop_problem_delete() {
531 532 533 534 535 536 537
    delete current_coop.errors;
    delete current_coop.coop_msg;
    store_current_coop_data(function() {
        retrieve_all_coops();
        closeModal();
        home();
    });
Administrator committed
538 539
}

540
function open_coop_form(e) {
541 542 543
    try {
        if (e) {
            var clicked = $(this);
544

545 546 547
            coop_target = {};
            coop_target.id = clicked.data('id');
            coop_target.validation_state = clicked.closest('div.main').attr('id');
548
        } else {
549
            return display_current_coop_form();
550
        }
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567

        set_current_coop(coop_target, function() {
            if (coop_target.validation_state == 'to_fill') {
                display_current_coop_form();
            } else if ((coop_target.validation_state == 'waiting_validation_employee' || coop_target.validation_state == 'waiting_validation_member') && coop_is_connected()) {
                display_current_coop_form();
            } else if (coop_target.validation_state == 'done') {
                ask_for_deletion();
            } else {
                process_signaler_click();
            }
        });
    } catch (err) {
        error = {msg: err.name + ' : ' + err.message, ctx: 'open_coop_form'};
        console.error(error);
        report_JS_error(error, 'prepa-odoo');
    }
Damien Moulard committed
568 569

    return null;
Administrator committed
570 571 572
}

function ask_for_deletion() {
573 574 575 576
    var msg = 'Voulez-vous archiver ';

    msg += current_coop.firstname + ' ' + current_coop.lastname + ' ?';
    openModal(msg, coop_form_delete, 'Oui');
Administrator committed
577 578 579
}

function ask_for_problem_deletion() {
580 581 582
    var msg = 'Voulez-vous enlever ' + current_coop.firstname + ' ' + current_coop.lastname + ' des coops à problème ?';

    openModal(msg, coop_problem_delete, 'Oui');
Administrator committed
583
}
584
function add_coop_to_box(box, coop) {
585 586
    try {
        var info = coop.firstname + ' ' + coop.lastname;
587

588 589 590
        if (coop.odoo_state && coop.odoo_state == 'done') {
            info = coop.barcode_base + ' - ' + info;
        }
591

592 593 594 595 596 597 598 599 600 601 602
        info = $('<span/>').text(info);
        var cbox = $('<div/>').addClass('coop')
            .attr('data-id', coop._id)
            .append(info);
        // Only connected user can do these actions

        if ([
            "waiting_validation_employee",
            "waiting_validation_member",
            "done"
        ].indexOf(coop.validation_state) > -1
Administrator committed
603
      && !coop_is_connected()) {
604 605 606 607 608 609 610
            cbox.addClass('coop_no_select');
        }
        box.append(cbox);
    } catch (e) {
        err = {msg: e.name + ' : ' + e.message, ctx: 'add_coop_to_box'};
        console.error(err);
        report_JS_error(err, 'prepa-odoo');
611
    }
Administrator committed
612 613 614 615
}

//Called after having retrieved all coops
function dispatch_coops_in_boxes() {
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
    try {
        $('div.coop').off('click', open_coop_form);
        to_fill_box.html('');
        with_errors_box.html('');
        waiting_validation_employee_div.find('.elts').html('');
        waiting_validation_member_div.find('.elts').html('');
        done_div.find('.elts').html('');

        $.each(coops.to_fill, function(i, e) {
            add_coop_to_box(to_fill_box, e);
        });
        $.each(coops.with_errors, function(i, e) {
            add_coop_to_box(with_errors_box, e);
        });
        $.each(coops.waiting_validation_employee, function(i, e) {
            add_coop_to_box(waiting_validation_employee_div.find('.elts'), e);
        });
        $.each(coops.waiting_validation_member, function(i, e) {
            add_coop_to_box(waiting_validation_member_div.find('.elts'), e);
        });
        $.each(coops.done, function(i, e) {
            add_coop_to_box(done_div.find('.elts'), e);
        });
639

640 641 642 643 644 645 646
        $('div.coop').on('click', open_coop_form);
        $('div.coop_no_select').off('click', open_coop_form);
    } catch (e) {
        err = {msg: e.name + ' : ' + e.message, ctx: 'dispatch_coops_in_boxes'};
        console.error(err);
        report_JS_error(err, 'prepa-odoo');
    }
Administrator committed
647 648
}

649 650 651 652 653 654 655 656 657 658 659 660 661 662
function handle_legacy_states(rows) {
    $.each(rows, function(i, e) {
        if (typeof (e.doc.validation_state) == "undefined") {
            if (typeof (e.doc.odoo_state) !== "undefined" && e.doc.odoo_state == 'done') {
                rows[i].doc.validation_state = 'done';
            } else if (typeof (e.doc.odoo_id) == "undefined" && typeof (e.doc.birthdate) != "undefined") {
                rows[i].doc.validation_state = 'waiting_validation_employee';
            } else if (typeof (e.doc.odoo_id) !== "undefined") {
                rows[i].doc.validation_state = 'waiting_validation_member';
            } else {
                rows[i].doc.validation_state = 'to_fill';
            }
        }
    });
Administrator committed
663

664
    return rows;
Administrator committed
665 666 667 668
}

// first function called when loading page
function retrieve_all_coops() {
669 670 671 672
    try {
        dbc.allDocs({include_docs: true, descending: true}, function(err, resp) {
            if (err) {
                return console.log(err);
673
            }
674 675 676 677 678 679 680 681 682 683 684 685 686 687
            coops = {'to_fill': [], 'with_errors': [], 'waiting_validation_employee':[], 'waiting_validation_member':[], 'done':[]};
            $.each(handle_legacy_states(resp.rows), function(i, e) {
                if (e.doc.firstname) {
                    if (e.doc.errors) {
                        coops.with_errors.push(e.doc);
                    } else if (e.doc.validation_state == "to_fill") {
                        coops.to_fill.push(e.doc);
                    } else if (e.doc.validation_state == "waiting_validation_employee") {
                        coops.waiting_validation_employee.push(e.doc);
                    } else if (e.doc.validation_state == "waiting_validation_member") {
                        coops.waiting_validation_member.push(e.doc);
                    } else if (e.doc.validation_state == "done") {
                        coops.done.push(e.doc);
                    }
Administrator committed
688

689 690 691 692
                    if (e.doc.coop_msg) {
                        coops.with_errors.push(e.doc);
                    }
                }
693

694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
            });

            coops['to_fill'].sort(function(a, b) {
                return a.barcode_base - b.barcode_base;
            });
            coops['with_errors'].sort(function(a, b) {
                return a.barcode_base - b.barcode_base;
            });
            coops['waiting_validation_employee'].sort(function(a, b) {
                return a.barcode_base - b.barcode_base;
            });
            coops['waiting_validation_member'].sort(function(a, b) {
                return a.odoo_id - b.odoo_id;
            });
            coops['done'].sort(function(a, b) {
                return b.timestamp - a.timestamp;
            });
            dispatch_coops_in_boxes();
Damien Moulard committed
712 713

            return null;
714
        });
715 716 717 718 719
    } catch (err) {
        error = {msg: err.name + ' : ' + err.message, ctx: 'retrieve_all_coops'};
        console.log(error);
        report_JS_error(error, 'prepa-odoo');
    }
Administrator committed
720 721 722
}

$(document).ready(function() {
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
    retrieve_all_coops();

    coop_validation_form.submit(process_validation_form);
    coop_validation_form.find('[name="signaler"]').click(process_signaler_click);
    $('#new_warning_form').submit(process_new_warning);

    $('.next_step button').click(function() {
        var clicked = $(this);

        if (clicked.data('action') == 'coop_form') {
            warning_slide.hide();
            open_coop_form();
        } else if (clicked.data('action') == 'form_delete') {
            ask_for_deletion();
        } else if (clicked.data('action') == 'problem_delete') {
            ask_for_problem_deletion();
        } else {
            current_coop = null;
            home();
        }
    });
744 745 746
    $('[name="birthdate"]').helpFillDate();
    $('[name="phone"]').helpFillTel();
    $('[name="mobile"]').helpFillTel();
Administrator committed
747
});