inscriptions.js 15.4 KB
Newer Older
Administrator committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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'),

    self_records = [],

23
    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
24 25 26


sync.on('change', function (info) {
27 28 29 30 31 32 33 34 35 36 37 38
    // 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
39
        //On recharge depuis Odoo et on traite les enregistrements depuis CouchDB
40
            retrieve_and_draw_shift_tempates('without_modal');
Administrator committed
41

42
        }
Administrator committed
43 44 45 46 47 48 49 50 51
    }

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


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


function get_next_shift(st_id, callback) {

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

86 87
function new_coop_validation() {
    coop_list_view.hide();
Administrator committed
88 89 90
    schoice_view.hide();
    ncoop_view.hide();
    var barcode_base = current_coop.barcode_base;
91
    var st = get_shift_name(current_coop.shift_template.data);
Administrator committed
92
    //coop_registration_details.find('.numbox').text('N° '+ barcode_base);
93 94

    coop_registration_details.find('.shift_template').text(st);
Administrator committed
95
    process_state.html(current_coop.firstname + ' ' +current_coop.lastname);
96
    get_next_shift(current_coop.shift_template.data.id, function(data) {
Administrator committed
97
        if (data != null)
98
            coop_registration_details.find('.next_shift').text(data.date_begin);
Administrator committed
99 100 101 102 103 104
        coop_registration_details.show();
    });

}

function reset_sex_radios() {
105 106 107
    sex.find('input').each(function(i, e) {
        $(e).prop('checked', false);
    });
Administrator committed
108 109 110
}

function create_new_coop() {
111
    local_in_process = getLocalInProcess();
Administrator committed
112
    if (getLocalInProcess().length > 0) {
113
        empty_waiting_local_processes();
Administrator committed
114 115
    } else {
        if (current_coop == null || typeof(current_coop.shift_template) != "undefined") {
116 117
            current_coop = null;
            ncoop_view.find('.title').text('NOUVEAU MEMBRE');
118 119
            coop_create.find('input').not('[type="radio"]')
                .val('');
120 121 122 123 124 125 126 127 128
            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
129 130
            ncoop_view.show();
        } else {
131
            alert(choose_shift_msg);
Administrator committed
132 133 134 135 136 137 138 139 140 141
        }
    }

}
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();
}
142
function _really_save_new_coop(email, fname, lname, cap, pm, cn, bc, msex) {
Administrator committed
143

144
    var coop = current_coop || {};
Administrator committed
145 146

    if (typeof(coop._id) !== "undefined") {
147
        current_email = coop._id;
Administrator committed
148 149 150
        if (email != current_email) {
            //delete current_coop after copying revelant data

151
            dbc.remove(current_email, coop._rev, function(err, response) {
Administrator committed
152

153 154 155
                if (err) {
                    return console.log(err);
                }
Administrator committed
156 157 158 159 160 161 162
                //console.log(response);

            });
            delete coop._rev;
        }
    }

163 164 165 166 167 168 169 170 171 172 173
    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;
    if (m_barcode.length > 0) coop.m_barcode = bc;
    if (sex.length > 0) coop.sex = msex;
    coop.validation_state = "to_fill";
Administrator committed
174 175
    dbc.put(coop, function callback(err, result) {
        if (!err) {
176 177 178 179 180 181 182 183 184 185 186
            coop._rev = result.rev;
            current_coop = coop;
            if (typeof coop.shift_template != "undefined") {
                openModal(
                    'Voulez-vous modifier le créneau choisi ?', swipe_to_shift_choice, 'oui',
                    false, true, show_coop_list
                );
            } else {
                swipe_to_shift_choice();
            }
            update_self_records();
Administrator committed
187 188 189 190
        } else {
            alert('Cet email est déjà enregistré !');
            console.log(err);
        }
191
    });
Administrator committed
192 193
}

194
function store_new_coop(event) {
Administrator committed
195 196
    event.preventDefault();
    var errors = [],
197
        bc = '', // barcode may not be present
198
        msex = ''; // sex may not be present
Administrator committed
199
    // 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)
200 201 202 203 204 205 206
    let email = $('input[name="email"]').val()
            .trim(),
        fname = $('input[name="firstname"]').val()
            .toFormatedFirstName(),
        lname = $('input[name="lastname"]').val()
            .toFormatedLastName();

Administrator committed
207
    if (m_barcode.length > 0) {
208
        bc = m_barcode.val();
209
        if (!isValidEAN13(bc)) errors.push("Le code-barre ne semble pas être un EAN13 valide.");
Administrator committed
210 211
    }
    if (sex.length > 0) {
212
        msex = $('input[name="sex"]:checked').val();
Administrator committed
213 214
    }

215 216 217 218 219 220
    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
221 222 223
    }

    $.ajax({url : '/members/exists/' + email,
224
        dataType :'json'
Administrator committed
225
    })
226
        .done(function(rData) {
Administrator committed
227
            if (typeof(rData.answer) == 'boolean' && rData.answer == true) {
228
                errors.push("Il y a déjà un enregistrement Odoo avec cette adresse mail !");
Administrator committed
229
            }
230
        });
Administrator committed
231 232

    if (errors.length == 0) {
233 234 235 236
        _really_save_new_coop(
            email, fname, lname,
            subs_cap.val(), payment_meaning.val(), ch_qty.val(), bc, msex
        );
Administrator committed
237 238

    } else {
239
        alert(errors.join("\n"));
Administrator committed
240 241 242 243 244 245 246
    }

}



function set_current_coop_by_id(id) {
247 248 249
    coop = null;
    $.each(self_records, function(i, e) {
        if (e._id == id) {
Administrator committed
250 251 252
            current_coop = coop = e;
        }
    });
253

Administrator committed
254 255 256 257
    return coop;
}

function modify_current_coop() {
258
    c_shift = '';
Administrator committed
259 260 261 262 263 264 265
    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');
266 267 268 269 270
    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');
Administrator committed
271
    if (current_coop.checks_nb.length > 0) {
272 273
        ch_qty.val(current_coop.checks_nb);
        ch_qty.show();
Administrator committed
274
    } else {
275
        ch_qty.hide();
Administrator committed
276
    }
277 278
    subs_cap.val(current_coop.shares_euros);
    if (m_barcode.length > 0) m_barcode.val(current_coop.m_barcode);
Administrator committed
279
    if (sex.length > 0) {
280 281
        reset_sex_radios();
        $('#' + current_coop.sex + '_sex').prop('checked', true);
Administrator committed
282 283 284
    }
    ncoop_view.show();
}
285 286 287 288 289
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
290 291 292 293
        modify_current_coop();
    }
}

294 295 296 297
function modify_c_shift() {
    var clicked = $(this);
    var coop_id = clicked.closest('tr').find('td.coop div')
        .data('id');
Administrator committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

    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();
    }
}

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
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
348 349 350 351
    }

}

352 353 354 355 356 357
function update_completed_count() {
    dbc.query(
        'index/by_completed',
        {key:'shift'},
        function(err, result) {
            if (!err) {
Administrator committed
358 359 360 361
                //console.log(result);
                total_registred = result.rows.length;

            }
362 363
        }
    );
Administrator committed
364 365
}

366 367 368 369 370 371 372 373 374 375
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
376
                    if (odoo_state != 'done')
377 378
                        self_records.push(e.doc);
                });
Administrator committed
379
                if (self_records.length > 0) {
380
                    coop_list_btn.show();
Administrator committed
381 382 383
                }

            }
384 385
        }
    );
Administrator committed
386 387
}

388
function get_next_coop_num() {
Administrator committed
389 390 391
    var next_num = null;

    if (online == true) {
392
        odoo_num = get_latest_odoo_coop_bb();
Administrator committed
393 394 395 396 397 398
        if (total_registred) {
            next_num = odoo_num + total_registred +1;
        } else {
            next_num = odoo_num + 1;
        }
    }
399 400

    return next_num;
Administrator committed
401 402 403

}

404 405
function get_latest_odoo_coop_bb() {
    if (latest_odoo_coop_bb == null) {
Administrator committed
406 407 408
        $.ajax({url : '/members/latest_coop_id/',
            dataType :'json'
        })
409 410
            .done(function(rData) {
                latest_odoo_coop_bb = rData.latest_coop_id;
Administrator committed
411

412
            });
Administrator committed
413
    } else {
414
        return latest_odoo_coop_bb;
Administrator committed
415 416 417 418

    }
}

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
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
439 440 441

}

442
function show_shift_calendar() {
Administrator committed
443 444 445 446 447 448 449 450 451 452 453
    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() {
454 455 456 457
    var local_in_process = localStorage.getItem("in_process") || '[]';


    return JSON.parse(local_in_process);
Administrator committed
458 459 460
}

function setLocalInProcess(lip) {
461
    localStorage.setItem("in_process", JSON.stringify(lip));
Administrator committed
462 463 464 465 466
}

function keep_in_process_work(event) {
    //If data registration is in process, save it in localStorage
    if (current_coop != null && typeof (current_coop.shift_template) == "undefined") {
467
        local_in_process = getLocalInProcess();
Administrator committed
468 469
        // Dont push it again if already stored
        var found = false;
470 471

        $.each(local_in_process, function(i, e) {
Administrator committed
472 473 474 475
            if (current_coop._id == e._id)
                found = true;
        });
        if (found == false) {
476 477
            local_in_process.push(current_coop);
            setLocalInProcess(local_in_process);
Administrator committed
478 479 480 481 482 483
        }

    }
}

function empty_waiting_local_processes() {
484 485 486
    local_in_process = getLocalInProcess();
    if (local_in_process.length > 0) {
        current_coop = local_in_process.pop();
Administrator committed
487
        swipe_to_shift_choice();
488
        setLocalInProcess(local_in_process);
Administrator committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    }
}

$('#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();


$('#shift_calendar').click(show_shift_calendar);

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

506 507 508 509 510 511 512
payment_meaning.change(function() {
    if ($(this).val() == 'ch') {
        ch_qty.show();
    } else {
        ch_qty.hide();
        ch_qty.val('');
    }
Administrator committed
513 514 515 516 517
});

window.addEventListener("beforeunload", keep_in_process_work);

empty_waiting_local_processes();