CORE-POS/IS4C

View on GitHub
fannie/item/departments/sub.js

Summary

Maintainability
B
6 hrs
Test Coverage
/*******************************************************************************

    Copyright 2009 Whole Foods Co-op

    This file is part of Fannie.

    Fannie is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Fannie is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    in the file license.txt along with IT CORE; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*********************************************************************************/

var subDept = (function($) {
    var mod = {};

    var deptList = new Vue({
        el: '#deptselect',
        data: {
            deptID: 0
        },
        methods: {
            show: function() {
                subDept.show(this.deptID);
            }
        }
    });

    var subList = new Vue({
        el: '#subdiv',
        data: {
            subs: [],
            dept: "",
            selected: []
        },
        methods: {
            // Observer layer doesn't grok reduce()
            get: function () {
                return this.selected;
            }
        }
    });

    var alertMsg = function(severity, msg) {
        showBootstrapAlert('#alertarea', severity, msg);
    };

    var errorMsg = function(msg) {
        alertMsg('danger', msg);
    };

    var goodMsg = function(msg) {
        alertMsg('success', msg);
    };

    mod.show = function(did) {
        var d= 'did='+did+'&action=showSubsForDept';
        var name = $('#deptselect option:selected').text();
        $.ajax({
            type: 'post',
            data: d,
            dataType: "json"
        }).fail(function(){
            errorMsg('Error loading sub departments');
        }).done(function(resp){
            subList.subs = resp;
            subList.dept = 'Subdepts in ' + name;
        });
    };

    mod.add = function() {
        var name = $('#newname').val();
        var did = deptList.deptID;
        var d = 'action=addSub&name='+encodeURIComponent(name)+'&did='+did;
        $.ajax({
            type: 'post',
            data: d,
            dataType: "json"
        }).fail(function() {
            errorMsg('Error creating sub department');
        }).done(function(resp){
            subList.subs = resp;
            $('#newname').val('');
            goodMsg('Added sub department ' + name);
        });
    };

    mod.del = function() {
        var did = deptList.deptID;
        var d = 'action=deleteSub&did='+did;
        d += subList.get().reduce((acc, x) => acc + '&sid[]=' + x, "");
        $.ajax({
            type: 'post',
            data: d,
            dataType: "json"
        }).fail(function(){
            errorMsg('Error deleting sub department(s)');
        }).done(function(resp){
            subList.subs = resp;
            goodMsg('Deleted sub department(s)');
        });
    };

    mod.getSettings = function() {
        $.ajax({
            type: 'post',
            data: 'action=getSettings&sid='+$('#subselect').val(),
            dataType: 'json'
        }).fail(function() {
            errorMsg('Error getting tax/fs settings');
        }).done(function(resp) {
            $('#subtax').val(resp.tax);
            $('#subfs').val(resp.fs);
        });
    };

    mod.saveSettings = function() {
        var d = 'action=saveSettings&sid='+$('#subselect').val();
        d+= '&tax='+$('#subtax').val();
        d+= '&fs='+$('#subfs').val();
        $.ajax({
            type: 'post',
            data: d
        }).fail(function() {
            errorMsg('Error getting tax/fs settings');
        }).done(function(resp) {
            goodMsg('Updated tax/fs settings');
        });
    };

    return mod;

}(jQuery));