inscriptions.js 26.2 KB
Newer Older
Administrator committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
var fingerprint = new Fingerprint({canvas: true}).get(); //Pour associer données au PC utilisé pendant la réunion d'accueil

var latest_odoo_coop_bb = null,
    total_registred = null,

    ncoop_view = $('#new_coop'),
    coop_create = $('#coop_create'),
    next_coop = $('#next_coop'),
    coop_list_btn = $('#coop_list_btn'),
    coop_list_view = $('#coop_list_view'),

    coop_registration_details = $('#coop_registration_details'),

    payment_meaning = $('#payment_meaning'),
    ch_qty = $('#ch_qty'),
    process_state = $('#process_state'),
    subs_cap = $('#subs_cap'),
    m_barcode = $('#m_barcode'),
    sex = $('#sex'),
20
    job = $('#job'),
Administrator committed
21
    self_records = [],
Etienne Freiss committed
22 23
    selected_associate=null,
    associated_old_choice= null,
Administrator committed
24

25
    choose_shift_msg = "Il est nécessaire de choisir un créneau (ABCD ou Volant) avant de pouvoir faire quoique ce soit d'autre.\nUne personne qui souhaite être rattachée au compte d'un autre membre dans le cadre d'un binôme doit choisir le créneau volant.";
Administrator committed
26 27 28


sync.on('change', function (info) {
29 30 31 32 33 34 35 36 37 38 39 40
    // handle change
    need_reload = false;
    var displayed_shift_lat_menu = (sc_lat.css('display') == 'block' && sc_lat.find('.highlighted').length > 0);
    var selected_weeks = week_types.find('.selected_weeks .highlighted').length;

    if (displayed_shift_lat_menu && selected_weeks > 0) {
        $.each(info.change.docs, function(i, e) {
            if (e.shift_template) {
                need_reload = true;
            }
        });
        if (need_reload == true) {
Administrator committed
41
        //On recharge depuis Odoo et on traite les enregistrements depuis CouchDB
42
            retrieve_and_draw_shift_tempates({without_modal: true, shift_listener: true});
Administrator committed
43

44
        }
Administrator committed
45 46 47 48 49 50 51 52 53
    }

}).on('paused', function (err) {
    // replication paused (e.g. replication up to date, user went offline)
    if (err) {
        online = false;
    }


54 55 56 57 58 59
})
    .on('active', function () {
    // replicate resumed (e.g. new changes replicating, user went back online)
        update_completed_count();
        online = true;
    })
Damien Moulard committed
60
    .on('denied', function () {
61 62
    // a document failed to replicate (e.g. due to permissions)
    })
Damien Moulard committed
63
    .on('complete', function () {
64 65 66 67 68 69 70
    // handle complete
    })
    .on('error', function (err) {
    // handle error
        console.log('erreur sync');
        console.log(err);
    });
Administrator committed
71 72 73 74 75


function get_next_shift(st_id, callback) {

    $.ajax({url : '/members/shift_template/next_shift/'+st_id,
76
        dataType :'json'
Administrator committed
77
    })
78 79 80 81 82 83 84 85
        .done(function(rData) {
            if (callback)
                callback(rData.shift);
        })
        .fail(function() {
            if (callback)
                callback(null);
        });
Administrator committed
86 87
}

88 89
function new_coop_validation() {
    coop_list_view.hide();
Administrator committed
90 91
    schoice_view.hide();
    ncoop_view.hide();
92 93 94
    var st = get_shift_name(current_coop.shift_template.data);

    coop_registration_details.find('.shift_template').text(st);
Administrator committed
95
    process_state.html(current_coop.firstname + ' ' +current_coop.lastname);
Damien Moulard committed
96 97
    coop_registration_details.find("#parentName").text("");
    coop_registration_details.find("#parent").attr("hidden", true);
Thibault Grandjean committed
98 99

    if (current_coop.parent_name !== undefined) {
Damien Moulard committed
100 101
        coop_registration_details.find("#parentName").text(current_coop.parent_name);
        coop_registration_details.find("#parent").removeAttr("hidden");
Thibault Grandjean committed
102 103
    }

Etienne Freiss committed
104
    if (current_coop.shift_template.data && current_coop.shift_template.data.id != ASSOCIATE_MEMBER_SHIFT) {
Etienne Freiss committed
105 106 107 108 109 110 111 112 113
        get_next_shift(current_coop.shift_template.data.id, function(data) {
            if (data != null) {
                coop_registration_details.find('#next_shift_registration_detail').show();
                coop_registration_details.find('.next_shift').text(data.date_begin);
            }
            coop_registration_details.show();
        });
    } else {
        coop_registration_details.find('#next_shift_registration_detail').hide();
Administrator committed
114
        coop_registration_details.show();
Etienne Freiss committed
115
    }
Administrator committed
116 117 118
}

function reset_sex_radios() {
119 120 121
    sex.find('input').each(function(i, e) {
        $(e).prop('checked', false);
    });
Administrator committed
122 123 124
}

function create_new_coop() {
Etienne Freiss committed
125 126 127 128 129
    selected_associate= null;
    $('#associate_area').hide();
    $('.chosen_associate').html("");
    $('.chosen_associate_area').hide();
    $('.member_choice').removeClass('choice_active');
Damien Moulard committed
130
    $(".remove_binome_icon").on("click", hide_chosen_associate);
131
    $('input[name="binome"]').prop('checked', false);
132
    local_in_process = getLocalInProcess();
Administrator committed
133
    if (getLocalInProcess().length > 0) {
134
        empty_waiting_local_processes();
Administrator committed
135 136
    } else {
        if (current_coop == null || typeof(current_coop.shift_template) != "undefined") {
137 138
            current_coop = null;
            ncoop_view.find('.title').text('NOUVEAU MEMBRE');
139 140
            coop_create.find('input').not('[type="radio"]')
                .val('');
141 142 143 144 145 146 147 148 149
            coop_list_view.hide();
            schoice_view.hide();
            coop_registration_details.hide();
            process_state.html('');
            payment_meaning.val('');
            ch_qty.val('');
            ch_qty.hide();
            if (m_barcode.length > 0) m_barcode.val('');
            if (sex.length > 0) reset_sex_radios();
Administrator committed
150 151
            ncoop_view.show();
        } else {
152
            alert(choose_shift_msg);
Administrator committed
153 154 155 156 157 158 159 160 161
        }
    }
}
function swipe_to_shift_choice() {
    ncoop_view.hide();
    process_state.html('Inscription <strong>' + current_coop._id + '</strong> <span class="s_shift"></span');
    retrieve_and_draw_shift_tempates();
    schoice_view.show();
}
162
function _really_save_new_coop(email, fname, lname, cap, pm, cn, bc, msex, mjob) {
Administrator committed
163

164
    var coop = current_coop || {};
Administrator committed
165 166

    if (typeof(coop._id) !== "undefined") {
167
        current_email = coop._id;
Administrator committed
168 169 170
        if (email != current_email) {
            //delete current_coop after copying revelant data

Damien Moulard committed
171
            dbc.remove(current_email, coop._rev, function(err) {
Administrator committed
172

173 174 175
                if (err) {
                    return console.log(err);
                }
Administrator committed
176

Damien Moulard committed
177 178
                return null;

Administrator committed
179 180 181 182 183
            });
            delete coop._rev;
        }
    }

184 185 186 187 188 189 190 191
    coop._id = email;
    coop.firstname = fname;
    coop.lastname = lname;
    coop.shares_euros = cap;
    coop.shares_nb = parseInt(cap/10, 10);
    coop.payment_meaning = pm;
    coop.checks_nb = cn;
    coop.fingerprint = fingerprint;
Etienne Freiss committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    if (associated_old_choice == 'existing_member_choice') {
        if (selected_associate!=null) {
            coop.is_associated_people = true;
            coop.parent_id=selected_associate.id;
            coop.parent_name=selected_associate.barcode_base + ' - '+ selected_associate.name;
            coop.shift_template = shift_templates[ASSOCIATE_MEMBER_SHIFT];
        }
    } else if (associated_old_choice == 'new_member_choice' && $('#new_member_input').val()!='') {
        coop.is_associated_people = true;
        coop.parent_name=$('#new_member_input').val();
        delete coop.parent_id;
        coop.shift_template = shift_templates[ASSOCIATE_MEMBER_SHIFT];
    } else {
        delete coop.is_associated_people;
        delete coop.parent_id;
        delete coop.parent_name;
    }
    selected_associate=null;
    $('#new_member_input').val('');
    $('#associate_area').hide();
    $('.chosen_associate_area').hide();
    $('.chosen_associate').html("");
    associated_old_choice= null;

216 217
    if (m_barcode.length > 0) coop.m_barcode = bc;
    if (sex.length > 0) coop.sex = msex;
218
    if (job.length > 0) coop.function = mjob;
219
    coop.validation_state = "to_fill";
Administrator committed
220 221
    dbc.put(coop, function callback(err, result) {
        if (!err) {
222 223
            coop._rev = result.rev;
            current_coop = coop;
Etienne Freiss committed
224
            if (typeof coop.shift_template != "undefined" && coop.shift_template.data.id != ASSOCIATE_MEMBER_SHIFT) {
225 226 227 228
                openModal(
                    'Voulez-vous modifier le créneau choisi ?', swipe_to_shift_choice, 'oui',
                    false, true, show_coop_list
                );
Etienne Freiss committed
229 230
            } else if (coop.is_associated_people && typeof coop.shift_template != "undefined" && coop.shift_template.data.id == ASSOCIATE_MEMBER_SHIFT) {
                ncoop_view.hide();
Etienne Freiss committed
231
                new_coop_validation();
232 233 234 235
            } else {
                swipe_to_shift_choice();
            }
            update_self_records();
Administrator committed
236 237 238 239
        } else {
            alert('Cet email est déjà enregistré !');
            console.log(err);
        }
240
    });
Administrator committed
241 242
}

243
function store_new_coop(event) {
Administrator committed
244
    event.preventDefault();
245

Administrator committed
246
    var errors = [],
247
        bc = '', // barcode may not be present
248
        msex = '', // sex may not be present
249
        mjob = '', //job may not be present
250
        active_asso_area = $('#associate_area .choice_active'); // need to ckeck if associated data are available
Administrator committed
251
    // 1- Un coop avec le meme mail ne doit pas exister dans odoo (dans base intermediaire, le cas est géré par l'erreur à l'enregistrement)
252 253 254 255 256 257 258
    let email = $('input[name="email"]').val()
            .trim(),
        fname = $('input[name="firstname"]').val()
            .toFormatedFirstName(),
        lname = $('input[name="lastname"]').val()
            .toFormatedLastName();

Administrator committed
259
    if (m_barcode.length > 0) {
260
        bc = m_barcode.val();
261
        if (!isValidEAN13(bc)) errors.push("Le code-barre ne semble pas être un EAN13 valide.");
Administrator committed
262 263
    }
    if (sex.length > 0) {
264
        msex = $('input[name="sex"]:checked').val();
Administrator committed
265
    }
266 267 268
    if (job.length > 0) {
        mjob = $('input[name="job"]').val();
    }
Administrator committed
269

270 271 272 273 274 275
    if (payment_meaning.val() == 'ch') {
        if (ch_qty.val() <1) {
            errors.push("Le nombre de chèque est obligatoire.");
        } else if (ch_qty.val() > max_chq_nb) {
            errors.push("Le nombre de chèque est trop grand.");
        }
Administrator committed
276 277
    }

Félicie committed
278
    if ($('#associate_area').is(':visible')) {
279
        // If user choose yes for binome, a type of association must be selected
Félicie committed
280 281 282 283 284 285 286 287 288
        let associated_data_selected = false;

        if (
            ($(active_asso_area[0]).attr('id') === "new_member_choice")
            ||
            ($(active_asso_area[0]).attr('id') === "existing_member_choice")
        ) {
            associated_data_selected = true;
        }
Félicie committed
289
        if (associated_data_selected === false) errors.push("Un des deux choix concernant la mise en binôme doit être sélectionné");
Félicie committed
290 291 292

    }

293 294 295
    if (active_asso_area.length > 0) {
        // If user click as if a "binôme" is beeing created, data about parent member must exist
        let associated_data_ok = false;
Damien Moulard committed
296

297
        if (
Damien Moulard committed
298 299
            ($(active_asso_area[0]).attr('id') === "new_member_choice" && $('#new_member_input').val()
                .trim().length > 0)
300 301
            ||
            ($(active_asso_area[0]).attr('id') === "existing_member_choice" && $('#existing_member_choice_action .chosen_associate div.member').length > 0)
Damien Moulard committed
302
        ) {
303
            associated_data_ok = true;
Félicie committed
304 305 306
        } else if ($(active_asso_area[0]).attr('id') === "") {
            associated_data_ok = false;
            errors.push("Un des deux choix doit être sélectionné");
307 308 309 310
        }
        if (associated_data_ok === false) errors.push("Le membre 'titulaire' du binôme n'est pas défini");
    }

Administrator committed
311
    $.ajax({url : '/members/exists/' + email,
312
        dataType :'json'
Administrator committed
313
    })
314
        .done(function(rData) {
Administrator committed
315
            if (typeof(rData.answer) == 'boolean' && rData.answer == true) {
316
                errors.push("Il y a déjà un enregistrement Odoo avec cette adresse mail !");
Administrator committed
317
            }
Etienne Freiss committed
318 319 320 321 322
            if (selected_associate!=null) {
                $.ajax({url : '/members/is_associated/' + selected_associate.id,
                    dataType :'json'
                }).done(function(rData) {
                    if (typeof(rData.answer) == 'boolean' && rData.answer == true) {
Félicie committed
323
                        errors.push("Ce membre a déjà un binôme majeur");
Etienne Freiss committed
324 325 326 327
                    }
                    if (errors.length == 0) {
                        _really_save_new_coop(
                            email, fname, lname,
328
                            subs_cap.val(), payment_meaning.val(), ch_qty.val(), bc, msex, mjob
Etienne Freiss committed
329 330 331 332 333 334
                        );

                    } else {
                        alert(errors.join("\n"));
                    }
                });
Administrator committed
335

Etienne Freiss committed
336 337 338 339
            } else {
                if (errors.length == 0) {
                    _really_save_new_coop(
                        email, fname, lname,
340
                        subs_cap.val(), payment_meaning.val(), ch_qty.val(), bc, msex, mjob
Etienne Freiss committed
341 342 343 344 345 346 347
                    );

                } else {
                    alert(errors.join("\n"));
                }
            }
        });
Damien Moulard committed
348
}
Administrator committed
349 350 351 352



function set_current_coop_by_id(id) {
353 354 355
    coop = null;
    $.each(self_records, function(i, e) {
        if (e._id == id) {
Administrator committed
356 357 358
            current_coop = coop = e;
        }
    });
359

Administrator committed
360 361 362 363
    return coop;
}

function modify_current_coop() {
364
    c_shift = '';
Administrator committed
365 366 367 368 369 370 371
    if (current_coop.shift_template) {
        c_shift = get_shift_name(current_coop.shift_template.data);
        c_shift = '(' + c_shift + ')';
    }
    process_state.html('Modification <strong>' + current_coop._id + '</strong> <span class="s_shift">'+c_shift+'</span');
    coop_list_view.hide();
    ncoop_view.find('.title').text('MODIFICATION DU MEMBRE');
372 373 374 375 376
    ncoop_view.find('input[name="lastname"]').val(current_coop.lastname);
    ncoop_view.find('input[name="firstname"]').val(current_coop.firstname);
    ncoop_view.find('input[name="email"]').val(current_coop._id);
    payment_meaning.find('option').removeAttr('selected');
    payment_meaning.find('option[value="'+current_coop.payment_meaning+'"]').attr('selected', 'selected');
377
    if (current_coop.checks_nb && current_coop.checks_nb.length > 0) {
378 379
        ch_qty.val(current_coop.checks_nb);
        ch_qty.show();
Administrator committed
380
    } else {
381
        ch_qty.hide();
Administrator committed
382
    }
Etienne Freiss committed
383 384 385 386 387 388 389 390
    if (current_coop.is_associated_people) {
        $('.member_choice').removeClass('choice_active');
        $('#associate_area').show();
        if (current_coop.parent_id) {
            $('#existing_member_choice_action').show();
            $('#new_member_choice_action').hide();
            $('#existing_member_choice').addClass('choice_active');
            var member_button = '<div>' + current_coop.parent_name + '</div>';
Damien Moulard committed
391

Félicie committed
392
            $('.chosen_associate').html(member_button);
Etienne Freiss committed
393 394 395
            $('.chosen_associate_area').show();
            associated_old_choice = 'existing_member_choice';

Félicie committed
396

Félicie committed
397

Etienne Freiss committed
398 399 400 401 402 403 404 405 406 407
        } else {
            $('#new_member_choice_action').show();
            $('#existing_member_choice_action').hide();
            $('#new_member_choice').addClass('choice_active');
            $('#new_member_input').val(current_coop.parent_name);
            $('.chosen_associate').html('');
            $('.chosen_associate_area').hide();
            associated_old_choice = 'new_member_choice';
        }
    }
408 409
    subs_cap.val(current_coop.shares_euros);
    if (m_barcode.length > 0) m_barcode.val(current_coop.m_barcode);
Administrator committed
410
    if (sex.length > 0) {
411 412
        reset_sex_radios();
        $('#' + current_coop.sex + '_sex').prop('checked', true);
Administrator committed
413
    }
414 415 416
    if (job.length > 0) {
        ncoop_view.find('input[name="job"]').val(current_coop.function);
    }
Administrator committed
417 418
    ncoop_view.show();
}
Félicie committed
419 420

function hide_chosen_associate() {
Félicie committed
421
    selected_associate=null;
Félicie committed
422
    $(".chosen_associate_area").hide();
Félicie committed
423
    $('.chosen_associate').html("");
Félicie committed
424 425
}

426 427 428 429 430
function modify_coop_by_btn_triggered() {
    var clicked = $(this);
    var coop_id = clicked.find('div').data('id');

    if (set_current_coop_by_id(coop_id) != null) {
Administrator committed
431 432 433 434
        modify_current_coop();
    }
}

435 436 437 438
function modify_c_shift() {
    var clicked = $(this);
    var coop_id = clicked.closest('tr').find('td.coop div')
        .data('id');
Administrator committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

    if (set_current_coop_by_id(coop_id) != null) {
        ncoop_view.hide();
        coop_list_view.hide();
        c_shift = clicked.text();
        if (c_shift.length > 0) {
            c_shift = '(' + c_shift + ')';
        }
        process_state.html('Modification <strong>' + coop_id + '</strong> <span class="s_shift">'+c_shift+'</span');
        retrieve_and_draw_shift_tempates();
        sc_lat.find('button[data-select="both"]').addClass('highlighted');
        schoice_view.show();
    }
}

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
function show_coop_list() {
    if (current_coop == null || typeof (current_coop.shift_template) != "undefined") {
        ncoop_view.hide();
        schoice_view.hide();
        coop_registration_details.hide();
        coop_list_view.show();
        var table = coop_list_view.find('table');
        //evitons les fuites de mémoire potentielles

        table.find('td.coop').off('click', modify_coop_by_btn_triggered);
        table.find('td.c_shift').off('click', modify_c_shift);
        table.find('tbody tr').remove();
        $.each(self_records, function(i, e) {
            var tr = $('<tr/>');
            var coop = $('<div/>').attr('data-id', e._id)
                .html(e.firstname + ' ' + e.lastname + ' - '+e._id);
            var s_t = '';

            if (e.shift_template) {
                var s_data = e.shift_template.data;

                s_t = get_shift_name(s_data);
            }
            var shift = $('<div/>').html(s_t);

            tr.append($('<td/>').addClass('coop')
                .append(coop));
            tr.append($('<td/>').addClass('c_shift')
                .append(shift));
            table.append(tr);
        });
        table.find('td.coop').on('click', modify_coop_by_btn_triggered);
        table.find('td.c_shift').on('click', modify_c_shift);
    } else {
        alert(choose_shift_msg);
Administrator committed
489 490 491 492
    }

}

493 494 495 496 497 498
function update_completed_count() {
    dbc.query(
        'index/by_completed',
        {key:'shift'},
        function(err, result) {
            if (!err) {
Administrator committed
499 500 501 502
                //console.log(result);
                total_registred = result.rows.length;

            }
503 504
        }
    );
Administrator committed
505 506
}

507 508 509 510 511 512 513 514 515 516
function update_self_records() {
    dbc.query(
        'index/by_fp',
        {key:fingerprint, include_docs : true},
        function(err, result) {
            if (!err) {
                self_records = [];
                $.each(result.rows, function(i, e) {
                    var odoo_state = e.doc.odoo_state || 'void';

Administrator committed
517
                    if (odoo_state != 'done')
518 519
                        self_records.push(e.doc);
                });
Administrator committed
520
                if (self_records.length > 0) {
521
                    coop_list_btn.show();
Administrator committed
522 523 524
                }

            }
525 526
        }
    );
Administrator committed
527 528
}

529
function get_next_coop_num() {
Administrator committed
530 531 532
    var next_num = null;

    if (online == true) {
533
        odoo_num = get_latest_odoo_coop_bb();
Administrator committed
534 535 536 537 538 539
        if (total_registred) {
            next_num = odoo_num + total_registred +1;
        } else {
            next_num = odoo_num + 1;
        }
    }
540 541

    return next_num;
Administrator committed
542 543 544

}

545 546
function get_latest_odoo_coop_bb() {
    if (latest_odoo_coop_bb == null) {
Administrator committed
547 548 549
        $.ajax({url : '/members/latest_coop_id/',
            dataType :'json'
        })
550 551
            .done(function(rData) {
                latest_odoo_coop_bb = rData.latest_coop_id;
Administrator committed
552

553
            });
Administrator committed
554
    } else {
555
        return latest_odoo_coop_bb;
Administrator committed
556 557

    }
Damien Moulard committed
558 559

    return null;
Administrator committed
560 561
}

562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
function generate_email() {
    var fname = $('input[name="firstname"]').val()
        .toFormatedFirstName();
    var lname = $('input[name="lastname"]').val()
        .toFormatedFirstName();

    if (fname.length > 0 && lname.length > 0) {
        var d = new Date();
        var num_part = (d.getYear() -100).toString() + (d.getMonth()+1).toString() + d.getDate().toString();

        // toFormatedFirstName replaces spaces by - (needed for email)
        var email = 'coop-' + fname + '.' + lname + num_part + '@' + email_domain;
        //get out accents

        email = email.normalize('NFD').replace(/[\u0300-\u036f]/g, "")
            .replace(/ +/g, '-');
        $('input[name="email"]').val(email.toLowerCase());
    } else {
        alert('Le prénom et le nom doivent être renseignés pour générer le mail');
    }
Administrator committed
582 583 584

}

585
function show_shift_calendar() {
Administrator committed
586 587 588 589 590 591 592 593 594 595 596
    if (current_coop == null || typeof (current_coop.shift_template) != "undefined") {
        //Seulement pour information, pas de coop associé
        current_coop = null;
        coop_list_view.hide();
        ncoop_view.hide();
        retrieve_and_draw_shift_tempates();
        schoice_view.show();
    }
}

function getLocalInProcess() {
597 598 599 600
    var local_in_process = localStorage.getItem("in_process") || '[]';


    return JSON.parse(local_in_process);
Administrator committed
601 602 603
}

function setLocalInProcess(lip) {
604
    localStorage.setItem("in_process", JSON.stringify(lip));
Administrator committed
605 606
}

Damien Moulard committed
607
function keep_in_process_work() {
Administrator committed
608 609
    //If data registration is in process, save it in localStorage
    if (current_coop != null && typeof (current_coop.shift_template) == "undefined") {
610
        local_in_process = getLocalInProcess();
Administrator committed
611 612
        // Dont push it again if already stored
        var found = false;
613 614

        $.each(local_in_process, function(i, e) {
Administrator committed
615 616 617 618
            if (current_coop._id == e._id)
                found = true;
        });
        if (found == false) {
619 620
            local_in_process.push(current_coop);
            setLocalInProcess(local_in_process);
Administrator committed
621 622 623 624 625 626
        }

    }
}

function empty_waiting_local_processes() {
627 628 629
    local_in_process = getLocalInProcess();
    if (local_in_process.length > 0) {
        current_coop = local_in_process.pop();
Administrator committed
630
        swipe_to_shift_choice();
631
        setLocalInProcess(local_in_process);
Administrator committed
632 633 634 635 636 637 638 639 640 641
    }
}

$('#create_new_coop').click(create_new_coop);
next_coop.click(create_new_coop);
coop_list_btn.click(show_coop_list);
$('#coop_create').submit(store_new_coop);
$('#generate_email').click(generate_email);
$('#odoo_user_connect').click();

Félicie committed
642 643 644 645 646 647 648 649 650 651
$('#no_binome').click(function() {
    $('#associate_area').hide();
    $('#new_member_input').val('');
    $('#associate_area .choice_active').removeClass("choice_active");
    associated_old_choice = null;
    if (current_coop !=null) {
        delete current_coop.parent_name;
        delete current_coop.parent_id;
        delete current_coop.is_associated_people;
        delete current_coop.shift_template;
Etienne Freiss committed
652 653 654
    }
});

Félicie committed
655 656 657 658 659 660 661 662 663
$('#add_binome').click(function() {
    $('#associate_area').show();
    $('.member_choice').removeClass('choice_active');
    $('#existing_member_choice_action').hide();
    $('#new_member_choice_action').hide();
    associated_old_choice = null;

});

Etienne Freiss committed
664 665 666 667 668 669 670 671 672 673 674 675 676 677
$('.member_choice').on('click', function() {

    if (associated_old_choice !=null && associated_old_choice!=$(this).attr('id')) {
        $('#'+$(this).attr('id')+'_action').show();
        $('#'+associated_old_choice+'_action').hide();
        $('#'+associated_old_choice).removeClass('choice_active');
    } else if (associated_old_choice ==null) {
        $('#'+$(this).attr('id')+'_action').show();

    }
    associated_old_choice=$(this).attr('id');
    $(this).addClass('choice_active');

});
Administrator committed
678 679

$('#shift_calendar').click(show_shift_calendar);
Etienne Freiss committed
680 681 682 683 684 685
$('#search_member_input').keypress((event) => {
    if (event.keyCode==13) {
        event.preventDefault();
        searchMembersForAssociate();
    }
});
Administrator committed
686 687 688 689 690

//get_latest_odoo_coop_bb();
update_self_records();
update_completed_count();

691 692 693 694 695 696 697
payment_meaning.change(function() {
    if ($(this).val() == 'ch') {
        ch_qty.show();
    } else {
        ch_qty.hide();
        ch_qty.val('');
    }
Administrator committed
698 699 700 701 702
});

window.addEventListener("beforeunload", keep_in_process_work);

empty_waiting_local_processes();
Etienne Freiss committed
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736

/**
 * Display the members from the search result
 */
function display_possible_members() {
    $('.search_member_results_area').show();
    $('.search_member_results').empty();
    $('.btn_possible_member').off();

    $('.chosen_associate').html("");
    $('.chosen_associate_area').hide();

    let no_result = true;

    if (members_search_results.length > 0) {
        for (member of members_search_results) {
            $(".search_results_text").show();
            no_result = false;

            // Display results (possible members) as buttons
            var member_button = '<button class="btn--success btn_possible_member" member_id="'
                + member.id + '">'
                + member.barcode_base + ' - ' + member.name
                + '</button>';

            $('.search_member_results').append(member_button);
        }

        // Set action on member button click
        $('.btn_possible_member').on('click', function() {
            for (member of members_search_results) {
                if (member.id == $(this).attr('member_id')) {
                    selected_associate = member;

737
                    var member_button = '<div  member_id="' + member.id + '" class="member">' + member.barcode_base + ' - ' + member.name + '</div>';
Etienne Freiss committed
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

                    $('.chosen_associate').html(member_button);
                    $('.chosen_associate_area').show();

                    $('.search_member_results').empty();
                    $('.search_member_results_area').hide();
                    $('#search_member_input').val('');
                    break;
                }
            }
        });
    }

    if (no_result === true) {
        $(".search_results_text").hide();
        $('.search_member_results').html(`<p>
            <i>Aucun résultat ! Vérifiez votre recherche, ou si le.la membre n'est pas déjà dans le tableau...</i>
        </p>`);
    }
}


Etienne Freiss committed
760
/**
Damien Moulard committed
761
 * Search for members to associate a new member with an old one.
Etienne Freiss committed
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
 */
function searchMembersForAssociate() {
    let search_str = $('#search_member_input').val();

    if (search_str) {
        $.ajax({
            url: '/members/search/' + search_str+ "?search_type=members",
            dataType : 'json',
            success: function(data) {
                members_search_results = [];

                for (member of data.res) {
                    if (member.is_member || member.is_associated_people) {
                        members_search_results.push(member);
                    }
                }

                display_possible_members();
            },
            error: function() {
                err = {
                    msg: "erreur serveur lors de la recherche de membres",
                    ctx: 'search_member_form.search_members'
                };
                report_JS_error(err, 'members.admin');

                $.notify("Erreur lors de la recherche de membre, il faut ré-essayer plus tard...", {
                    globalPosition:"top right",
                    className: "error"
                });
            }
        });
    } else {
        members_search_results = [];
        display_possible_members();
    }
}


Etienne Freiss committed
801 802 803 804
$(document).ready(function() {
    retrieve_and_draw_shift_tempates();
    // Set action to search for the member
    $('#search_member_button').on('click', function() {
Etienne Freiss committed
805
        searchMembersForAssociate();
Etienne Freiss committed
806 807
    });

808 809 810
    if (committees_shift_id !== "None") {
        $("#shift_choice button[data-select='Volant']").text("Comités");
    }
Etienne Freiss committed
811
});