common.js 15.9 KB
Newer Older
1 2 3 4 5 6 7
var weeks_name = [
    '?',
    'A',
    'B',
    'C',
    'D'
];
Administrator committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

var online = true; // forcément vrai au chargement ! (si pas de cache)
var current_coop = null;
var st_url = '/members/get_all_shift_templates/';
var schoice_view = $('#shift_choice > div ');
var shift_templates = [];
var shift_table = $('#shift_choice .main_content table');
var sc_lat = $('#shift_choice .lat_menu');
var st_loc_buttons = $('#shift_choice .lat_menu button');
var week_types = $('#week_types');
var volant = null;





24

Administrator committed
25
function get_displayed_weeks() {
26
    displayed_weeks = [];
27
    $('#week_types').find('.selected_weeks :checked').each(function() {
28
        displayed_weeks.push($(this).val());
Administrator committed
29
    });
30

Administrator committed
31 32 33 34
    return displayed_weeks;
}

function get_shift_name(s_data) {
35
    var shift_name = "Inconnu";
36
    if (typeof ASSOCIATE_MEMBER_SHIFT == "undefined") ASSOCIATE_MEMBER_SHIFT = "";
Administrator committed
37
    if (s_data && s_data.week) {
38
        shift_name = weeks_name[s_data.week];
Etienne Freiss committed
39
        if (s_data.type == 2 && typeof manage_ftop != "undefined" && manage_ftop == true && s_data.id != ASSOCIATE_MEMBER_SHIFT) {
Administrator committed
40
            shift_name = 'Volant';
Etienne Freiss committed
41 42
        } else if(s_data.id == ASSOCIATE_MEMBER_SHIFT) {
            shift_name = 'Binôme';
Administrator committed
43
        } else {
44 45
            shift_name += s_data.day + ' - ' + s_data.begin;
            shift_name += ' - ' + s_data.place;
Administrator committed
46 47
        }
    }
48
    return shift_name;
Administrator committed
49 50 51 52 53
}



function subscribe_shift(shift_t_id) {
54 55 56
    var s_data = shift_templates[shift_t_id].data;
    var shift_name = get_shift_name(s_data);

57 58 59 60 61
    if (committees_shift_id !== undefined && committees_shift_id !== "None" && shift_name === "Volant") {
        shift_name = 'des Comités'
    }
    let msg = 'On inscrit le membre au créneau ' + shift_name

62
    openModal(
63
        msg,
64
        function() {
Administrator committed
65 66 67
            closeModal();
            current_coop.shift_template = shift_templates[shift_t_id];
            current_coop.timestamp = Date.now();
68
            current_coop.completed = 'shift';
Administrator committed
69 70 71 72 73 74 75 76
            /*
            if (!current_coop.barcode_base){
             current_coop.barcode_base = get_next_coop_num() || 'Indéterminé';
            }*/

            //total_registred
            dbc.put(current_coop, function callback(err, result) {
                if (!err) {
77 78 79 80 81 82 83 84 85 86 87
                    //console.log('Créneau enregistré !');
                    //current_coop._rev = result.rev;
                    if (context == 'inscription') {
                        update_self_records();
                        new_coop_validation();
                    } else {
                        schoice_view.hide();
                        save_current_coop();
                        display_current_coop_form();
                    }
                    //process_state.find('.s_shift').text('('+shift_name+')')
Administrator committed
88 89 90 91 92
                } else {
                    console.log(err);
                }
            });

93 94
        }
    );
Administrator committed
95 96 97 98

}

function single_shift_click() {
99 100 101 102 103 104 105
    var clicked = $(this);

    if (! clicked.hasClass('full')) {
        var shift_t_id = clicked.data('id');

        subscribe_shift(shift_t_id);
    }
Administrator committed
106 107
}

108
function select_shift_among_compact(event, clicked_item = null, subscribe = true) {
109
    var clicked = clicked_item === null ? $(this) : $(clicked_item);
Administrator committed
110 111 112 113
    var day = clicked.closest('td').attr('class');
    var hour = clicked.closest('tr').data('begin');
    var selected = null;
    var worst_score = 1;
114

Administrator committed
115
    displayed_weeks = get_displayed_weeks();
116
    var place_cond = sc_lat.find('.highlighted').data('select') || "";
Administrator committed
117

118
    $.each(shift_templates, function(i, e) {
Administrator committed
119 120 121
        if (e.data) {
            var keep_it = false;
            var place = e.data.place;
122

Administrator committed
123 124 125 126 127 128
            if (place_cond == 'both' || place_cond == place) {
                if (e.data.begin == hour && e.data.day == day) {
                    keep_it = true;
                }
            }

129
            if (keep_it == true && displayed_weeks.indexOf(e.data.week.toString()) > -1) {
Administrator committed
130 131 132 133 134 135 136 137

                if (e.data.reserved / e.data.max < worst_score) {
                    worst_score = e.data.reserved / e.data.max;
                    selected = i;
                }
            }
        }
    });
138

139 140 141 142 143 144 145 146 147
    if (selected && subscribe === true){
        if (typeof partner_data !== "undefined" && typeof partner_data.barcode_base !== "undefined") {
            //click is coming from memberspace
            process_asked_shift_template_change(selected);
        } else {
            subscribe_shift(selected);
        }
        
    }
148 149

    return selected
Administrator committed
150 151 152 153
}


function draw_table(begin_hours, callback) {
154 155
    if (shift_table.length == 0) shift_table = $('#shift_choice table');

156 157 158 159 160 161 162 163 164 165 166
    shift_table.find('tbody tr').remove();
    begin_hours.sort();
    var days = [
        "Lun",
        "Mar",
        "Mer",
        "Jeu",
        "Ven",
        "Sam"
    ];

Administrator committed
167
    if (typeof open_on_sunday != "undefined" && open_on_sunday == true) {
168
        days.push("Dim");
Administrator committed
169
    }
170 171 172 173 174
    $.each(begin_hours, function(i, e) {
        var tr = $('<tr>').attr('data-targ', 'magasin')
            .attr('data-begin', e);

        tr.append($('<td>').text(e));
Administrator committed
175
        for (idx in days) {
176
            tr.append($('<td>').addClass(days[idx]));
Administrator committed
177
        }
178
        shift_table.append(tr);
Administrator committed
179

180 181
    });
    callback();
Administrator committed
182 183 184

}

185 186 187 188 189
function draw_shift_templates(params) {
    if (params && typeof params.external !== "undefined" && params.external == true){
        // Simplified calendar loaded in modal (members_space for ex.)
        shift_table = $('#shift_choice table');
    }
Administrator committed
190
    var existing_shifts = shift_table.find('.shift');
191 192 193

    existing_shifts.off("click", single_shift_click);
    existing_shifts.off("click", select_shift_among_compact);
Administrator committed
194 195
    existing_shifts.remove();

196
    var place_cond = sc_lat.find('.highlighted').data('select') || "";
Administrator committed
197 198 199 200 201 202 203 204
    //warning MAG_NAME should correspond to data.place value of shift_templates objects

    dispo = 0;
    max = 0;


    displayed_weeks = get_displayed_weeks();
    var boxes = {};
205
    var begin_hours = [];
Administrator committed
206 207 208
    //Find out hours to display in table
    //After each operations, begin_hours is an array of "hours"

209 210 211 212
    $.each(shift_templates, function(i, e) {
        if (e.data) {
            var keep_it = false;
            var place = e.data.place;
Administrator committed
213

214 215
            if (e.data.begin <= max_begin_hour && e.data.max > 0 && (place_cond == 'both' || place_cond == place)) {
                keep_it = true;
216
            } 
217 218 219 220 221 222

            if (keep_it == true && displayed_weeks.indexOf(e.data.week.toString()) > -1) {
                var known_begin_hour = false;

                for (idx in begin_hours) {
                    if (e.data.begin == begin_hours[idx]) known_begin_hour = true;
Administrator committed
223
                }
224 225
                if (known_begin_hour == false) begin_hours.push(e.data.begin);

Administrator committed
226
            }
227
        }
Administrator committed
228 229
    });

230
    draw_table(begin_hours, function() {
231 232
        const is_inscription_page = $('body').hasClass('inscriptions');

233
        $.each(shift_templates, function(i, e) {
Administrator committed
234 235 236 237
            if (e.data) {
                var keep_it = false;
                var place = e.data.place;
                // Legacy conditions ('both' was an option when 2 places were used)
238

Administrator committed
239 240 241 242 243 244 245
                if (place_cond == 'both' || place_cond == place) {
                    keep_it = true;
                }

                if (keep_it == true && displayed_weeks.indexOf(e.data.week.toString()) > -1) {
                    if (type == 1) {
                        //Every shift template is displayed
246 247 248 249 250
                        var box = $('<div/>').attr('data-week', e.data.week);

                        box.attr('data-place', place);
                        box.attr('data-id', i);
                        box.attr('title', 'Termine à '+e.data.end);
Administrator committed
251 252 253 254 255 256 257 258
                        box.text(e.data.reserved + '/' + e.data.max);
                        box.addClass('shift');
                        if (e.data.reserved / e.data.max <= .75) {
                            box.addClass('alert');
                        } else if (e.data.reserved == e.data.max) {
                            box.addClass('full');
                        }

259 260
                        var target = shift_table.find('tr[data-begin="'+e.data.begin+'"]').find('td.'+e.data.day);

Administrator committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
                        if (target.length > 0) {
                            max += e.data.max;
                            dispo += (e.data.max - e.data.reserved);
                            target.append(box);
                        }
                    }
                    if (type == 2) {
                        //Shift templates are gathered (places and weeks)
                        //a profile is defined (useful for assigning it a color)
                        profile = 'more_than_50pc';
                        if (e.data.reserved / e.data.max <= .25) {
                            profile = 'less_than_25pc';
                        } else if (e.data.reserved / e.data.max <= .5) {
                            profile = 'less_than_50pc';
                        }
                        //Compile data for each box, including profil
                        if (typeof boxes[e.data.day+'_'+e.data.begin] == "undefined") {
278
                            boxes[e.data.day+'_'+e.data.begin] = {'reserved': e.data.reserved, 'max':e.data.max, 'profil': profile};
Administrator committed
279 280 281 282
                        } else {
                            boxes[e.data.day+'_'+e.data.begin]['reserved'] +=e.data.reserved;
                            boxes[e.data.day+'_'+e.data.begin]['max'] +=e.data.max;
                            var existing_profil = boxes[e.data.day+'_'+e.data.begin]['profil'];
283

Administrator committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297
                            if (existing_profil == 'more_than_50pc' && profile != 'more_than_50pc') {
                                boxes[e.data.day+'_'+e.data.begin]['profil'] = profile;
                            }
                            if (existing_profil == 'less_than_50pc' && profile == 'less_than_25pc') {
                                boxes[e.data.day+'_'+e.data.begin]['profil'] = profile;
                            }
                        }
                    }
                }


            }

        });
298
        
Administrator committed
299
        if (type == 1) {
300
            if (!params || (typeof params.shift_listener !== "undefined" && params.shift_listener == true) || is_inscription_page == true){
301 302
                shift_table.find('.shift').on("click", single_shift_click);
            }
Administrator committed
303 304
        }
        if (type == 2) {
305 306
            for (k in boxes) {
                var k_elts = k.split("_");
Administrator committed
307 308
                var day = k_elts[0];
                var hour = k_elts[1];
309 310
                var target = shift_table.find('tr[data-begin="'+hour+'"]').find('td.'+day);

Administrator committed
311 312 313
                if (target.length > 0) {
                    max += boxes[k].max;
                    dispo += (boxes[k].max - boxes[k].reserved);
314 315
                    var box = $('<div/>').attr('data-type', 'compact');

Administrator committed
316
                    box.addClass('shift');
317
                    box.addClass('b_'+boxes[k].profil);
Administrator committed
318 319 320 321 322 323 324 325
                    if (boxes[k].max <=boxes[k].reserved) {
                        box.addClass('full');
                    }
                    box.text(boxes[k].reserved + '/' + boxes[k].max);
                    target.append(box);
                }

            }
326
            if (!params || (typeof params.shift_listener !== "undefined" && params.shift_listener == true) || is_inscription_page == true){
327 328
                shift_table.find('.shift').on("click", select_shift_among_compact);
            }
Administrator committed
329 330 331 332
        }



333
        sc_lat.find('.info').html(dispo + ' places disponibles<br />(/'+max+')');
334
        if (!params || typeof params.without_modal === "undefined" || (typeof params.without_modal !== "undefined" && params.without_modal == false)) {
335 336
            closeModal();
        } 
337
    });
Administrator committed
338 339 340 341

}


342
function retrieve_and_draw_shift_tempates(params) {
343
    if (shift_table.length == 0) shift_table = $('#shift_choice table');
344
    if (!params || typeof params.without_modal === "undefined" || (typeof params.without_modal !== "undefined" && params.without_modal == false)) {
345 346
        openModal();
    } 
Administrator committed
347 348 349

    shift_table.find('.shift').remove();
    $.ajax({url : st_url,
350
        dataType :'json'
Administrator committed
351
    })
352 353 354
        .done(function(rData) {
            shift_templates = rData.creneaux;
            $.each(shift_templates, function(i, e) {
Administrator committed
355

356
                if (e.data.type == 2 && volant == null) {
357
                    // has comitee shift
358
                    if (typeof committees_shift_id !== "undefined" && committees_shift_id !== "None") {
359 360 361 362 363 364
                        if (e.data.id == parseInt(committees_shift_id)) {
                            volant = e.data.id
                        }
                    } else {
                        volant = e.data.id;
                    }
365 366
                }
            });
367 368 369 370
            if (typeof dbc !== "undefined") {
                dbc.allDocs({include_docs: true, descending: true}, function(err, resp) {
                    if (err) {
                        return console.log(err);
371
                    }
372 373 374
                    $.each(resp.rows, function(i, e) {
                        if (e.doc.shift_template && typeof(e.doc.shift_template.data) != "undefined") {
                            try {
375 376 377 378 379
                                if (typeof e.doc.odoo_id == "undefined" || isNaN(e.doc.odoo_id)) {
                                    if (typeof shift_templates[e.doc.shift_template.data.id] != "undefined")
                                        shift_templates[e.doc.shift_template.data.id]['data']['reserved'] += 1;
                                }
                                
380 381 382 383
                            } catch (ec) {
                                console.log(ec);
                            }
                        }
Administrator committed
384

385
                    });
386
                    draw_shift_templates(params);
387
                });
388
            } else {
389
                draw_shift_templates(params);
390 391
            }
            
Administrator committed
392 393


394
        });
Administrator committed
395 396
}

397
function filter_weeks(params) {
Administrator committed
398
    var clicked = $(this);
399
    var week_types = $('#week_types');
Administrator committed
400 401 402 403 404 405
    var parent_div = clicked.closest('div');
    var w1 = week_types.find('#dw1');
    var w2 = week_types.find('#dw2');
    var w3 = week_types.find('#dw3');
    var w4 = week_types.find('#dw4');

406
    if (parent_div.hasClass('oddeven_selector')) {
Administrator committed
407
        // Paires ou impaires has been clicked
408 409 410 411
        if (clicked.is(':checked')) {
            if (clicked.val() == 0) {
                w1.prop('checked', true);
                w3.prop('checked', true);
Administrator committed
412
            } else {
413 414
                w2.prop('checked', true);
                w4.prop('checked', true);
Administrator committed
415 416
            }
        } else {
417 418 419
            if (clicked.val() == 0) {
                w1.prop('checked', false);
                w3.prop('checked', false);
Administrator committed
420
            } else {
421 422
                w2.prop('checked', false);
                w4.prop('checked', false);
Administrator committed
423 424 425 426 427 428 429
            }
        }

    }

    //unhilight or hilight Paire / Impaires according weeks selections
    if (w1.is(':checked') && w3.is(':checked')) {
430
        $('#even_weeks').prop('checked', true);
Administrator committed
431 432
    }
    if (w2.is(':checked') && w4.is(':checked')) {
433
        $('#odd_weeks').prop('checked', true);
Administrator committed
434 435 436
    }

    if (!w1.is(':checked') || !w3.is(':checked')) {
437
        $('#even_weeks').prop('checked', false);
Administrator committed
438 439
    }
    if (!w2.is(':checked') || !w4.is(':checked')) {
440
        $('#odd_weeks').prop('checked', false);
Administrator committed
441
    }
442
    draw_shift_templates(params);
Administrator committed
443 444 445
}

function shift_loc_selection() {
446 447 448 449
    var clicked = $(this);

    st_loc_buttons.removeClass('highlighted');
    clicked.addClass('highlighted');
450
    if (clicked.data('select') !== 'Volant' && clicked.data('select') !== 'Exemption') {
451
        retrieve_and_draw_shift_tempates();
452
    } else if (clicked.data('select') === 'Volant') {
453 454 455 456
        //shift_templates[volant] is not always set (when call from bdm interface)
        if (typeof volant !== "undefined" && typeof shift_templates[volant] !== "undefined") {
            subscribe_shift(volant);
        } 
457 458
    } else if (clicked.data('select') === 'Exemption') {
        subscribe_shift(exemptions_shift_id);
459
    }
Administrator committed
460 461 462 463 464

}

st_loc_buttons.click(shift_loc_selection);

465 466 467
week_types.find('input').change(() => {
    filter_weeks({shift_listener: true});
});